@@ -4235,10 +4235,77 @@ static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
4235
4235
return ret ;
4236
4236
}
4237
4237
4238
+ /*
4239
+ * Preallocate blocks for a write request, if it is possible and helpful to do
4240
+ * so. Returns a positive number if blocks may have been preallocated, 0 if no
4241
+ * blocks were preallocated, or a negative errno value if something went
4242
+ * seriously wrong. Also sets FI_PREALLOCATED_ALL on the inode if *all* the
4243
+ * requested blocks (not just some of them) have been allocated.
4244
+ */
4245
+ static int f2fs_preallocate_blocks (struct kiocb * iocb , struct iov_iter * iter )
4246
+ {
4247
+ struct inode * inode = file_inode (iocb -> ki_filp );
4248
+ struct f2fs_sb_info * sbi = F2FS_I_SB (inode );
4249
+ const loff_t pos = iocb -> ki_pos ;
4250
+ const size_t count = iov_iter_count (iter );
4251
+ struct f2fs_map_blocks map = {};
4252
+ bool dio = (iocb -> ki_flags & IOCB_DIRECT ) &&
4253
+ !f2fs_force_buffered_io (inode , iocb , iter );
4254
+ int flag ;
4255
+ int ret ;
4256
+
4257
+ /* If it will be an out-of-place direct write, don't bother. */
4258
+ if (dio && f2fs_lfs_mode (sbi ))
4259
+ return 0 ;
4260
+
4261
+ /* No-wait I/O can't allocate blocks. */
4262
+ if (iocb -> ki_flags & IOCB_NOWAIT )
4263
+ return 0 ;
4264
+
4265
+ /* If it will be a short write, don't bother. */
4266
+ if (fault_in_iov_iter_readable (iter , count ))
4267
+ return 0 ;
4268
+
4269
+ if (f2fs_has_inline_data (inode )) {
4270
+ /* If the data will fit inline, don't bother. */
4271
+ if (pos + count <= MAX_INLINE_DATA (inode ))
4272
+ return 0 ;
4273
+ ret = f2fs_convert_inline_inode (inode );
4274
+ if (ret )
4275
+ return ret ;
4276
+ }
4277
+
4278
+ /* Do not preallocate blocks that will be written partially in 4KB. */
4279
+ map .m_lblk = F2FS_BLK_ALIGN (pos );
4280
+ map .m_len = F2FS_BYTES_TO_BLK (pos + count );
4281
+ if (map .m_len > map .m_lblk )
4282
+ map .m_len -= map .m_lblk ;
4283
+ else
4284
+ map .m_len = 0 ;
4285
+ map .m_may_create = true;
4286
+ if (dio ) {
4287
+ map .m_seg_type = f2fs_rw_hint_to_seg_type (inode -> i_write_hint );
4288
+ flag = F2FS_GET_BLOCK_PRE_DIO ;
4289
+ } else {
4290
+ map .m_seg_type = NO_CHECK_TYPE ;
4291
+ flag = F2FS_GET_BLOCK_PRE_AIO ;
4292
+ }
4293
+
4294
+ ret = f2fs_map_blocks (inode , & map , 1 , flag );
4295
+ /* -ENOSPC is only a fatal error if no blocks could be allocated. */
4296
+ if (ret < 0 && !(ret == - ENOSPC && map .m_len > 0 ))
4297
+ return ret ;
4298
+ if (ret == 0 )
4299
+ set_inode_flag (inode , FI_PREALLOCATED_ALL );
4300
+ return map .m_len ;
4301
+ }
4302
+
4238
4303
static ssize_t f2fs_file_write_iter (struct kiocb * iocb , struct iov_iter * from )
4239
4304
{
4240
4305
struct file * file = iocb -> ki_filp ;
4241
4306
struct inode * inode = file_inode (file );
4307
+ loff_t target_size ;
4308
+ int preallocated ;
4242
4309
ssize_t ret ;
4243
4310
4244
4311
if (unlikely (f2fs_cp_error (F2FS_I_SB (inode )))) {
@@ -4262,84 +4329,54 @@ static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
4262
4329
4263
4330
if (unlikely (IS_IMMUTABLE (inode ))) {
4264
4331
ret = - EPERM ;
4265
- goto unlock ;
4332
+ goto out_unlock ;
4266
4333
}
4267
4334
4268
4335
if (is_inode_flag_set (inode , FI_COMPRESS_RELEASED )) {
4269
4336
ret = - EPERM ;
4270
- goto unlock ;
4337
+ goto out_unlock ;
4271
4338
}
4272
4339
4273
4340
ret = generic_write_checks (iocb , from );
4274
4341
if (ret > 0 ) {
4275
- bool preallocated = false;
4276
- size_t target_size = 0 ;
4277
- int err ;
4278
-
4279
- if (fault_in_iov_iter_readable (from , iov_iter_count (from )))
4280
- set_inode_flag (inode , FI_NO_PREALLOC );
4281
-
4282
- if ((iocb -> ki_flags & IOCB_NOWAIT )) {
4342
+ if (iocb -> ki_flags & IOCB_NOWAIT ) {
4283
4343
if (!f2fs_overwrite_io (inode , iocb -> ki_pos ,
4284
4344
iov_iter_count (from )) ||
4285
4345
f2fs_has_inline_data (inode ) ||
4286
4346
f2fs_force_buffered_io (inode , iocb , from )) {
4287
- clear_inode_flag (inode , FI_NO_PREALLOC );
4288
- inode_unlock (inode );
4289
4347
ret = - EAGAIN ;
4290
- goto out ;
4348
+ goto out_unlock ;
4291
4349
}
4292
- goto write ;
4293
4350
}
4294
-
4295
- if (is_inode_flag_set (inode , FI_NO_PREALLOC ))
4296
- goto write ;
4297
-
4298
4351
if (iocb -> ki_flags & IOCB_DIRECT ) {
4299
- /*
4300
- * Convert inline data for Direct I/O before entering
4301
- * f2fs_direct_IO().
4302
- */
4303
- err = f2fs_convert_inline_inode (inode );
4304
- if (err )
4305
- goto out_err ;
4306
- /*
4307
- * If force_buffere_io() is true, we have to allocate
4308
- * blocks all the time, since f2fs_direct_IO will fall
4309
- * back to buffered IO.
4310
- */
4311
- if (!f2fs_force_buffered_io (inode , iocb , from ) &&
4312
- f2fs_lfs_mode (F2FS_I_SB (inode )))
4313
- goto write ;
4352
+ ret = f2fs_convert_inline_inode (inode );
4353
+ if (ret )
4354
+ goto out_unlock ;
4314
4355
}
4315
- preallocated = true;
4356
+ /* Possibly preallocate the blocks for the write. */
4316
4357
target_size = iocb -> ki_pos + iov_iter_count (from );
4317
-
4318
- err = f2fs_preallocate_blocks (iocb , from );
4319
- if (err ) {
4320
- out_err :
4321
- clear_inode_flag (inode , FI_NO_PREALLOC );
4322
- inode_unlock (inode );
4323
- ret = err ;
4324
- goto out ;
4358
+ preallocated = f2fs_preallocate_blocks (iocb , from );
4359
+ if (preallocated < 0 ) {
4360
+ ret = preallocated ;
4361
+ goto out_unlock ;
4325
4362
}
4326
- write :
4363
+
4327
4364
ret = __generic_file_write_iter (iocb , from );
4328
- clear_inode_flag (inode , FI_NO_PREALLOC );
4329
4365
4330
- /* if we couldn 't write data, we should deallocate blocks . */
4331
- if (preallocated && i_size_read (inode ) < target_size ) {
4366
+ /* Don 't leave any preallocated blocks around past i_size . */
4367
+ if (preallocated > 0 && i_size_read (inode ) < target_size ) {
4332
4368
down_write (& F2FS_I (inode )-> i_gc_rwsem [WRITE ]);
4333
4369
filemap_invalidate_lock (inode -> i_mapping );
4334
4370
f2fs_truncate (inode );
4335
4371
filemap_invalidate_unlock (inode -> i_mapping );
4336
4372
up_write (& F2FS_I (inode )-> i_gc_rwsem [WRITE ]);
4337
4373
}
4374
+ clear_inode_flag (inode , FI_PREALLOCATED_ALL );
4338
4375
4339
4376
if (ret > 0 )
4340
4377
f2fs_update_iostat (F2FS_I_SB (inode ), APP_WRITE_IO , ret );
4341
4378
}
4342
- unlock :
4379
+ out_unlock :
4343
4380
inode_unlock (inode );
4344
4381
out :
4345
4382
trace_f2fs_file_write_iter (inode , iocb -> ki_pos ,
0 commit comments