@@ -113,20 +113,91 @@ static int simplefs_writepage(struct page *page, struct writeback_control *wbc)
113113 * the data into the page cache. This function checks if the write operation
114114 * can complete and allocates the necessary blocks through block_write_begin().
115115 */
116- #if SIMPLEFS_AT_LEAST (6 , 12 , 0 )
116+ #if SIMPLEFS_AT_LEAST (6 , 15 , 0 )
117+ static int simplefs_write_begin (const struct kiocb * iocb ,
118+ struct address_space * mapping ,
119+ loff_t pos ,
120+ unsigned int len ,
121+ struct folio * * foliop ,
122+ void * * fsdata )
123+ {
124+ struct file * file = iocb -> ki_filp ;
125+ struct simplefs_sb_info * sbi = SIMPLEFS_SB (file -> f_inode -> i_sb );
126+ int err ;
127+ uint32_t nr_allocs = 0 ;
128+
129+ if (pos + len > SIMPLEFS_MAX_FILESIZE )
130+ return - ENOSPC ;
131+
132+ nr_allocs = max (pos + len , file -> f_inode -> i_size ) / SIMPLEFS_BLOCK_SIZE ;
133+ if (nr_allocs > file -> f_inode -> i_blocks - 1 )
134+ nr_allocs -= file -> f_inode -> i_blocks - 1 ;
135+ else
136+ nr_allocs = 0 ;
137+ if (nr_allocs > sbi -> nr_free_blocks )
138+ return - ENOSPC ;
139+
140+ err = block_write_begin (mapping , pos , len , foliop , simplefs_file_get_block );
141+ if (err < 0 )
142+ pr_err ("newly allocated blocks reclaim not implemented yet\n" );
143+ return err ;
144+ }
145+ #elif SIMPLEFS_AT_LEAST (6 , 12 , 0 )
117146static int simplefs_write_begin (struct file * file ,
118147 struct address_space * mapping ,
119148 loff_t pos ,
120149 unsigned int len ,
121150 struct folio * * foliop ,
122151 void * * fsdata )
152+ {
153+ struct simplefs_sb_info * sbi = SIMPLEFS_SB (file -> f_inode -> i_sb );
154+ int err ;
155+ uint32_t nr_allocs = 0 ;
156+
157+ if (pos + len > SIMPLEFS_MAX_FILESIZE )
158+ return - ENOSPC ;
159+
160+ nr_allocs = max (pos + len , file -> f_inode -> i_size ) / SIMPLEFS_BLOCK_SIZE ;
161+ if (nr_allocs > file -> f_inode -> i_blocks - 1 )
162+ nr_allocs -= file -> f_inode -> i_blocks - 1 ;
163+ else
164+ nr_allocs = 0 ;
165+ if (nr_allocs > sbi -> nr_free_blocks )
166+ return - ENOSPC ;
167+
168+ err = block_write_begin (mapping , pos , len , foliop , simplefs_file_get_block );
169+ if (err < 0 )
170+ pr_err ("newly allocated blocks reclaim not implemented yet\n" );
171+ return err ;
172+ }
123173#elif SIMPLEFS_AT_LEAST (5 , 19 , 0 )
124174static int simplefs_write_begin (struct file * file ,
125175 struct address_space * mapping ,
126176 loff_t pos ,
127177 unsigned int len ,
128178 struct page * * pagep ,
129179 void * * fsdata )
180+ {
181+ struct simplefs_sb_info * sbi = SIMPLEFS_SB (file -> f_inode -> i_sb );
182+ int err ;
183+ uint32_t nr_allocs = 0 ;
184+
185+ if (pos + len > SIMPLEFS_MAX_FILESIZE )
186+ return - ENOSPC ;
187+
188+ nr_allocs = max (pos + len , file -> f_inode -> i_size ) / SIMPLEFS_BLOCK_SIZE ;
189+ if (nr_allocs > file -> f_inode -> i_blocks - 1 )
190+ nr_allocs -= file -> f_inode -> i_blocks - 1 ;
191+ else
192+ nr_allocs = 0 ;
193+ if (nr_allocs > sbi -> nr_free_blocks )
194+ return - ENOSPC ;
195+
196+ err = block_write_begin (mapping , pos , len , pagep , simplefs_file_get_block );
197+ if (err < 0 )
198+ pr_err ("newly allocated blocks reclaim not implemented yet\n" );
199+ return err ;
200+ }
130201#else
131202static int simplefs_write_begin (struct file * file ,
132203 struct address_space * mapping ,
@@ -135,13 +206,11 @@ static int simplefs_write_begin(struct file *file,
135206 unsigned int flags ,
136207 struct page * * pagep ,
137208 void * * fsdata )
138- #endif
139209{
140210 struct simplefs_sb_info * sbi = SIMPLEFS_SB (file -> f_inode -> i_sb );
141211 int err ;
142212 uint32_t nr_allocs = 0 ;
143213
144- /* Check if the write can be completed (enough space?) */
145214 if (pos + len > SIMPLEFS_MAX_FILESIZE )
146215 return - ENOSPC ;
147216
@@ -153,33 +222,38 @@ static int simplefs_write_begin(struct file *file,
153222 if (nr_allocs > sbi -> nr_free_blocks )
154223 return - ENOSPC ;
155224
156- /* prepare the write */
157- #if SIMPLEFS_AT_LEAST (6 , 12 , 0 )
158- err = block_write_begin (mapping , pos , len , foliop , simplefs_file_get_block );
159- #elif SIMPLEFS_AT_LEAST (5 , 19 , 0 )
160- err = block_write_begin (mapping , pos , len , pagep , simplefs_file_get_block );
161- #else
162225 err = block_write_begin (mapping , pos , len , flags , pagep ,
163226 simplefs_file_get_block );
164- #endif
165- /* if this failed, reclaim newly allocated blocks */
166227 if (err < 0 )
167228 pr_err ("newly allocated blocks reclaim not implemented yet\n" );
168229 return err ;
169230}
231+ #endif
170232
171233/* Called by the VFS after writing data from a write() syscall to the page
172234 * cache. This function updates inode metadata and truncates the file if
173235 * necessary.
174236 */
175- #if SIMPLEFS_AT_LEAST (6 , 12 , 0 )
237+ #if SIMPLEFS_AT_LEAST (6 , 15 , 0 )
238+ static int simplefs_write_end (const struct kiocb * iocb ,
239+ struct address_space * mapping ,
240+ loff_t pos ,
241+ unsigned int len ,
242+ unsigned int copied ,
243+ struct folio * foliop ,
244+ void * fsdata )
245+ {
246+ struct inode * inode = iocb -> ki_filp -> f_inode ;
247+ #elif SIMPLEFS_AT_LEAST(6, 12, 0)
176248static int simplefs_write_end (struct file * file ,
177249 struct address_space * mapping ,
178250 loff_t pos ,
179251 unsigned int len ,
180252 unsigned int copied ,
181253 struct folio * foliop ,
182254 void * fsdata )
255+ {
256+ struct inode * inode = file -> f_inode ;
183257#else
184258static int simplefs_write_end (struct file * file ,
185259 struct address_space * mapping ,
@@ -188,9 +262,9 @@ static int simplefs_write_end(struct file *file,
188262 unsigned int copied ,
189263 struct page * page ,
190264 void * fsdata )
191- #endif
192265{
193266 struct inode * inode = file -> f_inode ;
267+ #endif
194268 struct simplefs_inode_info * ci = SIMPLEFS_INODE (inode );
195269 struct super_block * sb = inode -> i_sb ;
196270#if SIMPLEFS_AT_LEAST (6 , 6 , 0 )
@@ -199,7 +273,10 @@ static int simplefs_write_end(struct file *file,
199273 uint32_t nr_blocks_old ;
200274
201275 /* Complete the write() */
202- #if SIMPLEFS_AT_LEAST (6 , 12 , 0 )
276+ #if SIMPLEFS_AT_LEAST (6 , 15 , 0 )
277+ int ret =
278+ generic_write_end (iocb , mapping , pos , len , copied , foliop , fsdata );
279+ #elif SIMPLEFS_AT_LEAST (6 , 12 , 0 )
203280 int ret =
204281 generic_write_end (file , mapping , pos , len , copied , foliop , fsdata );
205282#else
@@ -242,9 +319,15 @@ static int simplefs_write_end(struct file *file,
242319 /* Read ei_block to remove unused blocks */
243320 bh_index = sb_bread (sb , ci -> ei_block );
244321 if (!bh_index ) {
322+ #if SIMPLEFS_AT_LEAST (6 , 15 , 0 )
323+ pr_err ("Failed to truncate '%s'. Lost %llu blocks\n" ,
324+ iocb -> ki_filp -> f_path .dentry -> d_name .name ,
325+ nr_blocks_old - inode -> i_blocks );
326+ #else
245327 pr_err ("Failed to truncate '%s'. Lost %llu blocks\n" ,
246328 file -> f_path .dentry -> d_name .name ,
247329 nr_blocks_old - inode -> i_blocks );
330+ #endif
248331 goto end ;
249332 }
250333 index = (struct simplefs_file_ei_block * ) bh_index -> b_data ;
@@ -486,7 +569,9 @@ const struct address_space_operations simplefs_aops = {
486569#else
487570 .readpage = simplefs_readpage ,
488571#endif
572+ #if !SIMPLEFS_AT_LEAST (6 , 15 , 0 )
489573 .writepage = simplefs_writepage ,
574+ #endif
490575 .write_begin = simplefs_write_begin ,
491576 .write_end = simplefs_write_end ,
492577};
0 commit comments