29
29
#include <linux/pagevec.h>
30
30
#include <linux/uio.h>
31
31
#include <linux/mman.h>
32
+ #include <linux/backing-dev.h>
32
33
#include "ext4.h"
33
34
#include "ext4_jbd2.h"
34
35
#include "xattr.h"
@@ -219,7 +220,8 @@ ext4_dax_write_iter(struct kiocb *iocb, struct iov_iter *from)
219
220
static ssize_t ext4_buffered_write_iter (struct kiocb * iocb ,
220
221
struct iov_iter * from )
221
222
{
222
- struct inode * inode = file_inode (iocb -> ki_filp );
223
+ struct file * file = iocb -> ki_filp ;
224
+ struct inode * inode = file_inode (file );
223
225
ssize_t ret ;
224
226
225
227
if (!inode_trylock (inode )) {
@@ -232,25 +234,41 @@ static ssize_t ext4_buffered_write_iter(struct kiocb *iocb,
232
234
if (ret <= 0 )
233
235
goto out ;
234
236
235
- ret = __generic_file_write_iter (iocb , from );
236
- inode_unlock (inode );
237
+ ret = file_remove_privs (file );
238
+ if (ret )
239
+ goto out ;
237
240
238
- if (ret > 0 )
239
- ret = generic_write_sync (iocb , ret );
241
+ ret = file_update_time (file );
242
+ if (ret )
243
+ goto out ;
240
244
241
- return ret ;
245
+ /* We can write back this queue in page reclaim */
246
+ current -> backing_dev_info = inode_to_bdi (inode );
247
+ ret = generic_perform_write (file , from , iocb -> ki_pos );
248
+ if (likely (ret > 0 ))
249
+ iocb -> ki_pos += ret ;
250
+ current -> backing_dev_info = NULL ;
242
251
243
252
out :
244
253
inode_unlock (inode );
254
+
255
+ if (ret > 0 )
256
+ ret = generic_write_sync (iocb , ret );
257
+
245
258
return ret ;
246
259
}
247
260
248
261
static ssize_t ext4_dio_write_iter (struct kiocb * iocb , struct iov_iter * from )
249
262
{
250
- struct inode * inode = file_inode (iocb -> ki_filp );
263
+ struct file * file = iocb -> ki_filp ;
264
+ struct inode * inode = file_inode (file );
251
265
int unaligned_aio = 0 ;
252
266
int overwrite = 0 ;
253
267
ssize_t ret ;
268
+ struct address_space * mapping = file -> f_mapping ;
269
+ ssize_t status ;
270
+ loff_t pos , endbyte ;
271
+ ssize_t err ;
254
272
255
273
if (!inode_trylock (inode )) {
256
274
if (iocb -> ki_flags & IOCB_NOWAIT )
@@ -260,7 +278,7 @@ static ssize_t ext4_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)
260
278
261
279
ret = ext4_write_checks (iocb , from );
262
280
if (ret <= 0 )
263
- goto out ;
281
+ goto err_out ;
264
282
265
283
/*
266
284
* Unaligned direct AIO must be serialized among each other as zeroing
@@ -282,11 +300,59 @@ static ssize_t ext4_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)
282
300
overwrite = 1 ;
283
301
} else if (iocb -> ki_flags & IOCB_NOWAIT ) {
284
302
ret = - EAGAIN ;
285
- goto out ;
303
+ goto err_out ;
286
304
}
287
305
}
288
306
289
- ret = __generic_file_write_iter (iocb , from );
307
+ ret = file_remove_privs (file );
308
+ if (ret )
309
+ goto err_out ;
310
+
311
+ ret = file_update_time (file );
312
+ if (ret )
313
+ goto err_out ;
314
+
315
+ ret = generic_file_direct_write (iocb , from );
316
+ /*
317
+ * If the write stopped short of completing, fall back to
318
+ * buffered writes. Some filesystems do this for writes to
319
+ * holes, for example. For DAX files, a buffered write will
320
+ * not succeed (even if it did, DAX does not handle dirty
321
+ * page-cache pages correctly).
322
+ */
323
+ if (ret < 0 || !iov_iter_count (from ))
324
+ goto out ;
325
+
326
+ inode_unlock (inode );
327
+
328
+ /*direct io partial done, fallen into buffer io. */
329
+ pos = iocb -> ki_pos ;
330
+ status = ext4_buffered_write_iter (iocb , from );
331
+ if (status < 0 )
332
+ return ret ? ret : status ;
333
+
334
+ /*
335
+ * We need to ensure that the page cache pages are written to
336
+ * disk and invalidated to preserve the expected O_DIRECT
337
+ * semantics.
338
+ */
339
+ endbyte = pos + status - 1 ;
340
+ err = filemap_write_and_wait_range (mapping , pos , endbyte );
341
+ if (err == 0 ) {
342
+ iocb -> ki_pos = endbyte + 1 ;
343
+ ret += status ;
344
+ invalidate_mapping_pages (mapping ,
345
+ pos >> PAGE_SHIFT ,
346
+ endbyte >> PAGE_SHIFT );
347
+ } else {
348
+ /*
349
+ * We don't know how much we wrote, so just return
350
+ * the number of bytes which were direct-written
351
+ */
352
+ }
353
+ return ret ;
354
+
355
+ out :
290
356
/*
291
357
* Unaligned direct AIO must be the only IO in flight. Otherwise
292
358
* overlapping aligned IO after unaligned might result in data
@@ -301,7 +367,7 @@ static ssize_t ext4_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)
301
367
302
368
return ret ;
303
369
304
- out :
370
+ err_out :
305
371
inode_unlock (inode );
306
372
return ret ;
307
373
}
0 commit comments