1414#include <linux/pagevec.h>
1515#include <linux/blkdev.h>
1616#include <linux/bio.h>
17+ #include <linux/swap.h>
1718#include <linux/prefetch.h>
1819#include <linux/uio.h>
1920#include <linux/cleancache.h>
@@ -54,7 +55,7 @@ static bool __is_cp_guaranteed(struct page *page)
5455
5556static enum count_type __read_io_type (struct page * page )
5657{
57- struct address_space * mapping = page -> mapping ;
58+ struct address_space * mapping = page_file_mapping ( page ) ;
5859
5960 if (mapping ) {
6061 struct inode * inode = mapping -> host ;
@@ -1585,7 +1586,7 @@ static int f2fs_read_single_page(struct inode *inode, struct page *page,
15851586 sector_t block_nr ;
15861587 int ret = 0 ;
15871588
1588- block_in_file = (sector_t )page -> index ;
1589+ block_in_file = (sector_t )page_index ( page ) ;
15891590 last_block = block_in_file + nr_pages ;
15901591 last_block_in_file = (i_size_read (inode ) + blocksize - 1 ) >>
15911592 blkbits ;
@@ -1618,7 +1619,8 @@ static int f2fs_read_single_page(struct inode *inode, struct page *page,
16181619 block_nr = map -> m_pblk + block_in_file - map -> m_lblk ;
16191620 SetPageMappedToDisk (page );
16201621
1621- if (!PageUptodate (page ) && !cleancache_get_page (page )) {
1622+ if (!PageUptodate (page ) && (!PageSwapCache (page ) &&
1623+ !cleancache_get_page (page ))) {
16221624 SetPageUptodate (page );
16231625 goto confused ;
16241626 }
@@ -1716,7 +1718,7 @@ static int f2fs_mpage_readpages(struct address_space *mapping,
17161718 prefetchw (& page -> flags );
17171719 list_del (& page -> lru );
17181720 if (add_to_page_cache_lru (page , mapping ,
1719- page -> index ,
1721+ page_index ( page ) ,
17201722 readahead_gfp_mask (mapping )))
17211723 goto next_page ;
17221724 }
@@ -1740,7 +1742,7 @@ static int f2fs_mpage_readpages(struct address_space *mapping,
17401742
17411743static int f2fs_read_data_page (struct file * file , struct page * page )
17421744{
1743- struct inode * inode = page -> mapping -> host ;
1745+ struct inode * inode = page_file_mapping ( page ) -> host ;
17441746 int ret = - EAGAIN ;
17451747
17461748 trace_f2fs_readpage (page , DATA );
@@ -1749,7 +1751,8 @@ static int f2fs_read_data_page(struct file *file, struct page *page)
17491751 if (f2fs_has_inline_data (inode ))
17501752 ret = f2fs_read_inline_data (inode , page );
17511753 if (ret == - EAGAIN )
1752- ret = f2fs_mpage_readpages (page -> mapping , NULL , page , 1 , false);
1754+ ret = f2fs_mpage_readpages (page_file_mapping (page ),
1755+ NULL , page , 1 , false);
17531756 return ret ;
17541757}
17551758
@@ -2851,13 +2854,14 @@ int f2fs_release_page(struct page *page, gfp_t wait)
28512854
28522855static int f2fs_set_data_page_dirty (struct page * page )
28532856{
2854- struct address_space * mapping = page -> mapping ;
2855- struct inode * inode = mapping -> host ;
2857+ struct inode * inode = page_file_mapping (page )-> host ;
28562858
28572859 trace_f2fs_set_page_dirty (page , DATA );
28582860
28592861 if (!PageUptodate (page ))
28602862 SetPageUptodate (page );
2863+ if (PageSwapCache (page ))
2864+ return __set_page_dirty_nobuffers (page );
28612865
28622866 if (f2fs_is_atomic_file (inode ) && !f2fs_is_commit_atomic_write (inode )) {
28632867 if (!IS_ATOMIC_WRITTEN_PAGE (page )) {
@@ -2949,6 +2953,126 @@ int f2fs_migrate_page(struct address_space *mapping,
29492953}
29502954#endif
29512955
2956+ #ifdef CONFIG_SWAP
2957+ /* Copied from generic_swapfile_activate() to check any holes */
2958+ static int check_swap_activate (struct file * swap_file , unsigned int max )
2959+ {
2960+ struct address_space * mapping = swap_file -> f_mapping ;
2961+ struct inode * inode = mapping -> host ;
2962+ unsigned blocks_per_page ;
2963+ unsigned long page_no ;
2964+ unsigned blkbits ;
2965+ sector_t probe_block ;
2966+ sector_t last_block ;
2967+ sector_t lowest_block = -1 ;
2968+ sector_t highest_block = 0 ;
2969+
2970+ blkbits = inode -> i_blkbits ;
2971+ blocks_per_page = PAGE_SIZE >> blkbits ;
2972+
2973+ /*
2974+ * Map all the blocks into the extent list. This code doesn't try
2975+ * to be very smart.
2976+ */
2977+ probe_block = 0 ;
2978+ page_no = 0 ;
2979+ last_block = i_size_read (inode ) >> blkbits ;
2980+ while ((probe_block + blocks_per_page ) <= last_block && page_no < max ) {
2981+ unsigned block_in_page ;
2982+ sector_t first_block ;
2983+
2984+ cond_resched ();
2985+
2986+ first_block = bmap (inode , probe_block );
2987+ if (first_block == 0 )
2988+ goto bad_bmap ;
2989+
2990+ /*
2991+ * It must be PAGE_SIZE aligned on-disk
2992+ */
2993+ if (first_block & (blocks_per_page - 1 )) {
2994+ probe_block ++ ;
2995+ goto reprobe ;
2996+ }
2997+
2998+ for (block_in_page = 1 ; block_in_page < blocks_per_page ;
2999+ block_in_page ++ ) {
3000+ sector_t block ;
3001+
3002+ block = bmap (inode , probe_block + block_in_page );
3003+ if (block == 0 )
3004+ goto bad_bmap ;
3005+ if (block != first_block + block_in_page ) {
3006+ /* Discontiguity */
3007+ probe_block ++ ;
3008+ goto reprobe ;
3009+ }
3010+ }
3011+
3012+ first_block >>= (PAGE_SHIFT - blkbits );
3013+ if (page_no ) { /* exclude the header page */
3014+ if (first_block < lowest_block )
3015+ lowest_block = first_block ;
3016+ if (first_block > highest_block )
3017+ highest_block = first_block ;
3018+ }
3019+
3020+ page_no ++ ;
3021+ probe_block += blocks_per_page ;
3022+ reprobe :
3023+ continue ;
3024+ }
3025+ return 0 ;
3026+
3027+ bad_bmap :
3028+ pr_err ("swapon: swapfile has holes\n" );
3029+ return - EINVAL ;
3030+ }
3031+
3032+ static int f2fs_swap_activate (struct swap_info_struct * sis , struct file * file ,
3033+ sector_t * span )
3034+ {
3035+ struct inode * inode = file_inode (file );
3036+ int ret ;
3037+
3038+ if (!S_ISREG (inode -> i_mode ))
3039+ return - EINVAL ;
3040+
3041+ if (f2fs_readonly (F2FS_I_SB (inode )-> sb ))
3042+ return - EROFS ;
3043+
3044+ ret = f2fs_convert_inline_inode (inode );
3045+ if (ret )
3046+ return ret ;
3047+
3048+ ret = check_swap_activate (file , sis -> max );
3049+ if (ret )
3050+ return ret ;
3051+
3052+ set_inode_flag (inode , FI_PIN_FILE );
3053+ f2fs_precache_extents (inode );
3054+ f2fs_update_time (F2FS_I_SB (inode ), REQ_TIME );
3055+ return 0 ;
3056+ }
3057+
3058+ static void f2fs_swap_deactivate (struct file * file )
3059+ {
3060+ struct inode * inode = file_inode (file );
3061+
3062+ clear_inode_flag (inode , FI_PIN_FILE );
3063+ }
3064+ #else
3065+ static int f2fs_swap_activate (struct swap_info_struct * sis , struct file * file ,
3066+ sector_t * span )
3067+ {
3068+ return - EOPNOTSUPP ;
3069+ }
3070+
3071+ static void f2fs_swap_deactivate (struct file * file )
3072+ {
3073+ }
3074+ #endif
3075+
29523076const struct address_space_operations f2fs_dblock_aops = {
29533077 .readpage = f2fs_read_data_page ,
29543078 .readpages = f2fs_read_data_pages ,
@@ -2961,6 +3085,8 @@ const struct address_space_operations f2fs_dblock_aops = {
29613085 .releasepage = f2fs_release_page ,
29623086 .direct_IO = f2fs_direct_IO ,
29633087 .bmap = f2fs_bmap ,
3088+ .swap_activate = f2fs_swap_activate ,
3089+ .swap_deactivate = f2fs_swap_deactivate ,
29643090#ifdef CONFIG_MIGRATION
29653091 .migratepage = f2fs_migrate_page ,
29663092#endif
0 commit comments