@@ -1011,20 +1011,50 @@ int netfs_readpage(struct file *file,
1011
1011
}
1012
1012
EXPORT_SYMBOL (netfs_readpage );
1013
1013
1014
- static void netfs_clear_thp (struct page * page )
1014
+ /**
1015
+ * netfs_skip_page_read - prep a page for writing without reading first
1016
+ * @page: page being prepared
1017
+ * @pos: starting position for the write
1018
+ * @len: length of write
1019
+ *
1020
+ * In some cases, write_begin doesn't need to read at all:
1021
+ * - full page write
1022
+ * - write that lies in a page that is completely beyond EOF
1023
+ * - write that covers the the page from start to EOF or beyond it
1024
+ *
1025
+ * If any of these criteria are met, then zero out the unwritten parts
1026
+ * of the page and return true. Otherwise, return false.
1027
+ */
1028
+ static bool netfs_skip_page_read (struct page * page , loff_t pos , size_t len )
1015
1029
{
1016
- unsigned int i ;
1030
+ struct inode * inode = page -> mapping -> host ;
1031
+ loff_t i_size = i_size_read (inode );
1032
+ size_t offset = offset_in_thp (page , pos );
1033
+
1034
+ /* Full page write */
1035
+ if (offset == 0 && len >= thp_size (page ))
1036
+ return true;
1037
+
1038
+ /* pos beyond last page in the file */
1039
+ if (pos - offset >= i_size )
1040
+ goto zero_out ;
1041
+
1042
+ /* Write that covers from the start of the page to EOF or beyond */
1043
+ if (offset == 0 && (pos + len ) >= i_size )
1044
+ goto zero_out ;
1017
1045
1018
- for (i = 0 ; i < thp_nr_pages (page ); i ++ )
1019
- clear_highpage (page + i );
1046
+ return false;
1047
+ zero_out :
1048
+ zero_user_segments (page , 0 , offset , offset + len , thp_size (page ));
1049
+ return true;
1020
1050
}
1021
1051
1022
1052
/**
1023
1053
* netfs_write_begin - Helper to prepare for writing
1024
1054
* @file: The file to read from
1025
1055
* @mapping: The mapping to read from
1026
1056
* @pos: File position at which the write will begin
1027
- * @len: The length of the write in this page
1057
+ * @len: The length of the write (may extend beyond the end of the page chosen)
1028
1058
* @flags: AOP_* flags
1029
1059
* @_page: Where to put the resultant page
1030
1060
* @_fsdata: Place for the netfs to store a cookie
@@ -1061,8 +1091,6 @@ int netfs_write_begin(struct file *file, struct address_space *mapping,
1061
1091
struct inode * inode = file_inode (file );
1062
1092
unsigned int debug_index = 0 ;
1063
1093
pgoff_t index = pos >> PAGE_SHIFT ;
1064
- int pos_in_page = pos & ~PAGE_MASK ;
1065
- loff_t size ;
1066
1094
int ret ;
1067
1095
1068
1096
DEFINE_READAHEAD (ractl , file , NULL , mapping , index );
@@ -1090,13 +1118,8 @@ int netfs_write_begin(struct file *file, struct address_space *mapping,
1090
1118
* within the cache granule containing the EOF, in which case we need
1091
1119
* to preload the granule.
1092
1120
*/
1093
- size = i_size_read (inode );
1094
1121
if (!ops -> is_cache_enabled (inode ) &&
1095
- ((pos_in_page == 0 && len == thp_size (page )) ||
1096
- (pos >= size ) ||
1097
- (pos_in_page == 0 && (pos + len ) >= size ))) {
1098
- netfs_clear_thp (page );
1099
- SetPageUptodate (page );
1122
+ netfs_skip_page_read (page , pos , len )) {
1100
1123
netfs_stat (& netfs_n_rh_write_zskip );
1101
1124
goto have_page_no_wait ;
1102
1125
}
0 commit comments