]> Git Repo - linux.git/blob - block/fops.c
block: use iomap for writes to block devices
[linux.git] / block / fops.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 1991, 1992  Linus Torvalds
4  * Copyright (C) 2001  Andrea Arcangeli <[email protected]> SuSE
5  * Copyright (C) 2016 - 2020 Christoph Hellwig
6  */
7 #include <linux/init.h>
8 #include <linux/mm.h>
9 #include <linux/blkdev.h>
10 #include <linux/buffer_head.h>
11 #include <linux/mpage.h>
12 #include <linux/uio.h>
13 #include <linux/namei.h>
14 #include <linux/task_io_accounting_ops.h>
15 #include <linux/falloc.h>
16 #include <linux/suspend.h>
17 #include <linux/fs.h>
18 #include <linux/iomap.h>
19 #include <linux/module.h>
20 #include "blk.h"
21
22 static inline struct inode *bdev_file_inode(struct file *file)
23 {
24         return file->f_mapping->host;
25 }
26
27 static int blkdev_get_block(struct inode *inode, sector_t iblock,
28                 struct buffer_head *bh, int create)
29 {
30         bh->b_bdev = I_BDEV(inode);
31         bh->b_blocknr = iblock;
32         set_buffer_mapped(bh);
33         return 0;
34 }
35
36 static blk_opf_t dio_bio_write_op(struct kiocb *iocb)
37 {
38         blk_opf_t opf = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
39
40         /* avoid the need for a I/O completion work item */
41         if (iocb_is_dsync(iocb))
42                 opf |= REQ_FUA;
43         return opf;
44 }
45
46 static bool blkdev_dio_unaligned(struct block_device *bdev, loff_t pos,
47                               struct iov_iter *iter)
48 {
49         return pos & (bdev_logical_block_size(bdev) - 1) ||
50                 !bdev_iter_is_aligned(bdev, iter);
51 }
52
53 #define DIO_INLINE_BIO_VECS 4
54
55 static ssize_t __blkdev_direct_IO_simple(struct kiocb *iocb,
56                 struct iov_iter *iter, unsigned int nr_pages)
57 {
58         struct block_device *bdev = I_BDEV(iocb->ki_filp->f_mapping->host);
59         struct bio_vec inline_vecs[DIO_INLINE_BIO_VECS], *vecs;
60         loff_t pos = iocb->ki_pos;
61         bool should_dirty = false;
62         struct bio bio;
63         ssize_t ret;
64
65         if (blkdev_dio_unaligned(bdev, pos, iter))
66                 return -EINVAL;
67
68         if (nr_pages <= DIO_INLINE_BIO_VECS)
69                 vecs = inline_vecs;
70         else {
71                 vecs = kmalloc_array(nr_pages, sizeof(struct bio_vec),
72                                      GFP_KERNEL);
73                 if (!vecs)
74                         return -ENOMEM;
75         }
76
77         if (iov_iter_rw(iter) == READ) {
78                 bio_init(&bio, bdev, vecs, nr_pages, REQ_OP_READ);
79                 if (user_backed_iter(iter))
80                         should_dirty = true;
81         } else {
82                 bio_init(&bio, bdev, vecs, nr_pages, dio_bio_write_op(iocb));
83         }
84         bio.bi_iter.bi_sector = pos >> SECTOR_SHIFT;
85         bio.bi_ioprio = iocb->ki_ioprio;
86
87         ret = bio_iov_iter_get_pages(&bio, iter);
88         if (unlikely(ret))
89                 goto out;
90         ret = bio.bi_iter.bi_size;
91
92         if (iov_iter_rw(iter) == WRITE)
93                 task_io_account_write(ret);
94
95         if (iocb->ki_flags & IOCB_NOWAIT)
96                 bio.bi_opf |= REQ_NOWAIT;
97
98         submit_bio_wait(&bio);
99
100         bio_release_pages(&bio, should_dirty);
101         if (unlikely(bio.bi_status))
102                 ret = blk_status_to_errno(bio.bi_status);
103
104 out:
105         if (vecs != inline_vecs)
106                 kfree(vecs);
107
108         bio_uninit(&bio);
109
110         return ret;
111 }
112
113 enum {
114         DIO_SHOULD_DIRTY        = 1,
115         DIO_IS_SYNC             = 2,
116 };
117
118 struct blkdev_dio {
119         union {
120                 struct kiocb            *iocb;
121                 struct task_struct      *waiter;
122         };
123         size_t                  size;
124         atomic_t                ref;
125         unsigned int            flags;
126         struct bio              bio ____cacheline_aligned_in_smp;
127 };
128
129 static struct bio_set blkdev_dio_pool;
130
131 static void blkdev_bio_end_io(struct bio *bio)
132 {
133         struct blkdev_dio *dio = bio->bi_private;
134         bool should_dirty = dio->flags & DIO_SHOULD_DIRTY;
135
136         if (bio->bi_status && !dio->bio.bi_status)
137                 dio->bio.bi_status = bio->bi_status;
138
139         if (atomic_dec_and_test(&dio->ref)) {
140                 if (!(dio->flags & DIO_IS_SYNC)) {
141                         struct kiocb *iocb = dio->iocb;
142                         ssize_t ret;
143
144                         WRITE_ONCE(iocb->private, NULL);
145
146                         if (likely(!dio->bio.bi_status)) {
147                                 ret = dio->size;
148                                 iocb->ki_pos += ret;
149                         } else {
150                                 ret = blk_status_to_errno(dio->bio.bi_status);
151                         }
152
153                         dio->iocb->ki_complete(iocb, ret);
154                         bio_put(&dio->bio);
155                 } else {
156                         struct task_struct *waiter = dio->waiter;
157
158                         WRITE_ONCE(dio->waiter, NULL);
159                         blk_wake_io_task(waiter);
160                 }
161         }
162
163         if (should_dirty) {
164                 bio_check_pages_dirty(bio);
165         } else {
166                 bio_release_pages(bio, false);
167                 bio_put(bio);
168         }
169 }
170
171 static ssize_t __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
172                 unsigned int nr_pages)
173 {
174         struct block_device *bdev = I_BDEV(iocb->ki_filp->f_mapping->host);
175         struct blk_plug plug;
176         struct blkdev_dio *dio;
177         struct bio *bio;
178         bool is_read = (iov_iter_rw(iter) == READ), is_sync;
179         blk_opf_t opf = is_read ? REQ_OP_READ : dio_bio_write_op(iocb);
180         loff_t pos = iocb->ki_pos;
181         int ret = 0;
182
183         if (blkdev_dio_unaligned(bdev, pos, iter))
184                 return -EINVAL;
185
186         if (iocb->ki_flags & IOCB_ALLOC_CACHE)
187                 opf |= REQ_ALLOC_CACHE;
188         bio = bio_alloc_bioset(bdev, nr_pages, opf, GFP_KERNEL,
189                                &blkdev_dio_pool);
190         dio = container_of(bio, struct blkdev_dio, bio);
191         atomic_set(&dio->ref, 1);
192         /*
193          * Grab an extra reference to ensure the dio structure which is embedded
194          * into the first bio stays around.
195          */
196         bio_get(bio);
197
198         is_sync = is_sync_kiocb(iocb);
199         if (is_sync) {
200                 dio->flags = DIO_IS_SYNC;
201                 dio->waiter = current;
202         } else {
203                 dio->flags = 0;
204                 dio->iocb = iocb;
205         }
206
207         dio->size = 0;
208         if (is_read && user_backed_iter(iter))
209                 dio->flags |= DIO_SHOULD_DIRTY;
210
211         blk_start_plug(&plug);
212
213         for (;;) {
214                 bio->bi_iter.bi_sector = pos >> SECTOR_SHIFT;
215                 bio->bi_private = dio;
216                 bio->bi_end_io = blkdev_bio_end_io;
217                 bio->bi_ioprio = iocb->ki_ioprio;
218
219                 ret = bio_iov_iter_get_pages(bio, iter);
220                 if (unlikely(ret)) {
221                         bio->bi_status = BLK_STS_IOERR;
222                         bio_endio(bio);
223                         break;
224                 }
225                 if (iocb->ki_flags & IOCB_NOWAIT) {
226                         /*
227                          * This is nonblocking IO, and we need to allocate
228                          * another bio if we have data left to map. As we
229                          * cannot guarantee that one of the sub bios will not
230                          * fail getting issued FOR NOWAIT and as error results
231                          * are coalesced across all of them, be safe and ask for
232                          * a retry of this from blocking context.
233                          */
234                         if (unlikely(iov_iter_count(iter))) {
235                                 bio_release_pages(bio, false);
236                                 bio_clear_flag(bio, BIO_REFFED);
237                                 bio_put(bio);
238                                 blk_finish_plug(&plug);
239                                 return -EAGAIN;
240                         }
241                         bio->bi_opf |= REQ_NOWAIT;
242                 }
243
244                 if (is_read) {
245                         if (dio->flags & DIO_SHOULD_DIRTY)
246                                 bio_set_pages_dirty(bio);
247                 } else {
248                         task_io_account_write(bio->bi_iter.bi_size);
249                 }
250                 dio->size += bio->bi_iter.bi_size;
251                 pos += bio->bi_iter.bi_size;
252
253                 nr_pages = bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS);
254                 if (!nr_pages) {
255                         submit_bio(bio);
256                         break;
257                 }
258                 atomic_inc(&dio->ref);
259                 submit_bio(bio);
260                 bio = bio_alloc(bdev, nr_pages, opf, GFP_KERNEL);
261         }
262
263         blk_finish_plug(&plug);
264
265         if (!is_sync)
266                 return -EIOCBQUEUED;
267
268         for (;;) {
269                 set_current_state(TASK_UNINTERRUPTIBLE);
270                 if (!READ_ONCE(dio->waiter))
271                         break;
272                 blk_io_schedule();
273         }
274         __set_current_state(TASK_RUNNING);
275
276         if (!ret)
277                 ret = blk_status_to_errno(dio->bio.bi_status);
278         if (likely(!ret))
279                 ret = dio->size;
280
281         bio_put(&dio->bio);
282         return ret;
283 }
284
285 static void blkdev_bio_end_io_async(struct bio *bio)
286 {
287         struct blkdev_dio *dio = container_of(bio, struct blkdev_dio, bio);
288         struct kiocb *iocb = dio->iocb;
289         ssize_t ret;
290
291         WRITE_ONCE(iocb->private, NULL);
292
293         if (likely(!bio->bi_status)) {
294                 ret = dio->size;
295                 iocb->ki_pos += ret;
296         } else {
297                 ret = blk_status_to_errno(bio->bi_status);
298         }
299
300         iocb->ki_complete(iocb, ret);
301
302         if (dio->flags & DIO_SHOULD_DIRTY) {
303                 bio_check_pages_dirty(bio);
304         } else {
305                 bio_release_pages(bio, false);
306                 bio_put(bio);
307         }
308 }
309
310 static ssize_t __blkdev_direct_IO_async(struct kiocb *iocb,
311                                         struct iov_iter *iter,
312                                         unsigned int nr_pages)
313 {
314         struct block_device *bdev = I_BDEV(iocb->ki_filp->f_mapping->host);
315         bool is_read = iov_iter_rw(iter) == READ;
316         blk_opf_t opf = is_read ? REQ_OP_READ : dio_bio_write_op(iocb);
317         struct blkdev_dio *dio;
318         struct bio *bio;
319         loff_t pos = iocb->ki_pos;
320         int ret = 0;
321
322         if (blkdev_dio_unaligned(bdev, pos, iter))
323                 return -EINVAL;
324
325         if (iocb->ki_flags & IOCB_ALLOC_CACHE)
326                 opf |= REQ_ALLOC_CACHE;
327         bio = bio_alloc_bioset(bdev, nr_pages, opf, GFP_KERNEL,
328                                &blkdev_dio_pool);
329         dio = container_of(bio, struct blkdev_dio, bio);
330         dio->flags = 0;
331         dio->iocb = iocb;
332         bio->bi_iter.bi_sector = pos >> SECTOR_SHIFT;
333         bio->bi_end_io = blkdev_bio_end_io_async;
334         bio->bi_ioprio = iocb->ki_ioprio;
335
336         if (iov_iter_is_bvec(iter)) {
337                 /*
338                  * Users don't rely on the iterator being in any particular
339                  * state for async I/O returning -EIOCBQUEUED, hence we can
340                  * avoid expensive iov_iter_advance(). Bypass
341                  * bio_iov_iter_get_pages() and set the bvec directly.
342                  */
343                 bio_iov_bvec_set(bio, iter);
344         } else {
345                 ret = bio_iov_iter_get_pages(bio, iter);
346                 if (unlikely(ret)) {
347                         bio_put(bio);
348                         return ret;
349                 }
350         }
351         dio->size = bio->bi_iter.bi_size;
352
353         if (is_read) {
354                 if (user_backed_iter(iter)) {
355                         dio->flags |= DIO_SHOULD_DIRTY;
356                         bio_set_pages_dirty(bio);
357                 }
358         } else {
359                 task_io_account_write(bio->bi_iter.bi_size);
360         }
361
362         if (iocb->ki_flags & IOCB_HIPRI) {
363                 bio->bi_opf |= REQ_POLLED | REQ_NOWAIT;
364                 submit_bio(bio);
365                 WRITE_ONCE(iocb->private, bio);
366         } else {
367                 if (iocb->ki_flags & IOCB_NOWAIT)
368                         bio->bi_opf |= REQ_NOWAIT;
369                 submit_bio(bio);
370         }
371         return -EIOCBQUEUED;
372 }
373
374 static ssize_t blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
375 {
376         unsigned int nr_pages;
377
378         if (!iov_iter_count(iter))
379                 return 0;
380
381         nr_pages = bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS + 1);
382         if (likely(nr_pages <= BIO_MAX_VECS)) {
383                 if (is_sync_kiocb(iocb))
384                         return __blkdev_direct_IO_simple(iocb, iter, nr_pages);
385                 return __blkdev_direct_IO_async(iocb, iter, nr_pages);
386         }
387         return __blkdev_direct_IO(iocb, iter, bio_max_segs(nr_pages));
388 }
389
390 static int blkdev_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
391                 unsigned int flags, struct iomap *iomap, struct iomap *srcmap)
392 {
393         struct block_device *bdev = I_BDEV(inode);
394         loff_t isize = i_size_read(inode);
395
396         iomap->bdev = bdev;
397         iomap->offset = ALIGN_DOWN(offset, bdev_logical_block_size(bdev));
398         if (iomap->offset >= isize)
399                 return -EIO;
400         iomap->type = IOMAP_MAPPED;
401         iomap->addr = iomap->offset;
402         iomap->length = isize - iomap->offset;
403         iomap->flags |= IOMAP_F_BUFFER_HEAD;
404         return 0;
405 }
406
407 static const struct iomap_ops blkdev_iomap_ops = {
408         .iomap_begin            = blkdev_iomap_begin,
409 };
410
411 static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
412 {
413         return block_write_full_page(page, blkdev_get_block, wbc);
414 }
415
416 static int blkdev_read_folio(struct file *file, struct folio *folio)
417 {
418         return block_read_full_folio(folio, blkdev_get_block);
419 }
420
421 static void blkdev_readahead(struct readahead_control *rac)
422 {
423         mpage_readahead(rac, blkdev_get_block);
424 }
425
426 static int blkdev_write_begin(struct file *file, struct address_space *mapping,
427                 loff_t pos, unsigned len, struct page **pagep, void **fsdata)
428 {
429         return block_write_begin(mapping, pos, len, pagep, blkdev_get_block);
430 }
431
432 static int blkdev_write_end(struct file *file, struct address_space *mapping,
433                 loff_t pos, unsigned len, unsigned copied, struct page *page,
434                 void *fsdata)
435 {
436         int ret;
437         ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
438
439         unlock_page(page);
440         put_page(page);
441
442         return ret;
443 }
444
445 const struct address_space_operations def_blk_aops = {
446         .dirty_folio    = block_dirty_folio,
447         .invalidate_folio = block_invalidate_folio,
448         .read_folio     = blkdev_read_folio,
449         .readahead      = blkdev_readahead,
450         .writepage      = blkdev_writepage,
451         .write_begin    = blkdev_write_begin,
452         .write_end      = blkdev_write_end,
453         .migrate_folio  = buffer_migrate_folio_norefs,
454         .is_dirty_writeback = buffer_check_dirty_writeback,
455 };
456
457 /*
458  * for a block special file file_inode(file)->i_size is zero
459  * so we compute the size by hand (just as in block_read/write above)
460  */
461 static loff_t blkdev_llseek(struct file *file, loff_t offset, int whence)
462 {
463         struct inode *bd_inode = bdev_file_inode(file);
464         loff_t retval;
465
466         inode_lock(bd_inode);
467         retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode));
468         inode_unlock(bd_inode);
469         return retval;
470 }
471
472 static int blkdev_fsync(struct file *filp, loff_t start, loff_t end,
473                 int datasync)
474 {
475         struct block_device *bdev = I_BDEV(filp->f_mapping->host);
476         int error;
477
478         error = file_write_and_wait_range(filp, start, end);
479         if (error)
480                 return error;
481
482         /*
483          * There is no need to serialise calls to blkdev_issue_flush with
484          * i_mutex and doing so causes performance issues with concurrent
485          * O_SYNC writers to a block device.
486          */
487         error = blkdev_issue_flush(bdev);
488         if (error == -EOPNOTSUPP)
489                 error = 0;
490
491         return error;
492 }
493
494 blk_mode_t file_to_blk_mode(struct file *file)
495 {
496         blk_mode_t mode = 0;
497
498         if (file->f_mode & FMODE_READ)
499                 mode |= BLK_OPEN_READ;
500         if (file->f_mode & FMODE_WRITE)
501                 mode |= BLK_OPEN_WRITE;
502         if (file->private_data)
503                 mode |= BLK_OPEN_EXCL;
504         if (file->f_flags & O_NDELAY)
505                 mode |= BLK_OPEN_NDELAY;
506
507         /*
508          * If all bits in O_ACCMODE set (aka O_RDWR | O_WRONLY), the floppy
509          * driver has historically allowed ioctls as if the file was opened for
510          * writing, but does not allow and actual reads or writes.
511          */
512         if ((file->f_flags & O_ACCMODE) == (O_RDWR | O_WRONLY))
513                 mode |= BLK_OPEN_WRITE_IOCTL;
514
515         return mode;
516 }
517
518 static int blkdev_open(struct inode *inode, struct file *filp)
519 {
520         struct block_device *bdev;
521
522         /*
523          * Preserve backwards compatibility and allow large file access
524          * even if userspace doesn't ask for it explicitly. Some mkfs
525          * binary needs it. We might want to drop this workaround
526          * during an unstable branch.
527          */
528         filp->f_flags |= O_LARGEFILE;
529         filp->f_mode |= FMODE_BUF_RASYNC | FMODE_CAN_ODIRECT;
530
531         /*
532          * Use the file private data to store the holder for exclusive openes.
533          * file_to_blk_mode relies on it being present to set BLK_OPEN_EXCL.
534          */
535         if (filp->f_flags & O_EXCL)
536                 filp->private_data = filp;
537
538         bdev = blkdev_get_by_dev(inode->i_rdev, file_to_blk_mode(filp),
539                                  filp->private_data, NULL);
540         if (IS_ERR(bdev))
541                 return PTR_ERR(bdev);
542
543         if (bdev_nowait(bdev))
544                 filp->f_mode |= FMODE_NOWAIT;
545
546         filp->f_mapping = bdev->bd_inode->i_mapping;
547         filp->f_wb_err = filemap_sample_wb_err(filp->f_mapping);
548         return 0;
549 }
550
551 static int blkdev_release(struct inode *inode, struct file *filp)
552 {
553         blkdev_put(I_BDEV(filp->f_mapping->host), filp->private_data);
554         return 0;
555 }
556
557 static ssize_t
558 blkdev_direct_write(struct kiocb *iocb, struct iov_iter *from)
559 {
560         size_t count = iov_iter_count(from);
561         ssize_t written;
562
563         written = kiocb_invalidate_pages(iocb, count);
564         if (written) {
565                 if (written == -EBUSY)
566                         return 0;
567                 return written;
568         }
569
570         written = blkdev_direct_IO(iocb, from);
571         if (written > 0) {
572                 kiocb_invalidate_post_direct_write(iocb, count);
573                 iocb->ki_pos += written;
574                 count -= written;
575         }
576         if (written != -EIOCBQUEUED)
577                 iov_iter_revert(from, count - iov_iter_count(from));
578         return written;
579 }
580
581 static ssize_t blkdev_buffered_write(struct kiocb *iocb, struct iov_iter *from)
582 {
583         return iomap_file_buffered_write(iocb, from, &blkdev_iomap_ops);
584 }
585
586 /*
587  * Write data to the block device.  Only intended for the block device itself
588  * and the raw driver which basically is a fake block device.
589  *
590  * Does not take i_mutex for the write and thus is not for general purpose
591  * use.
592  */
593 static ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
594 {
595         struct file *file = iocb->ki_filp;
596         struct block_device *bdev = I_BDEV(file->f_mapping->host);
597         struct inode *bd_inode = bdev->bd_inode;
598         loff_t size = bdev_nr_bytes(bdev);
599         size_t shorted = 0;
600         ssize_t ret;
601
602         if (bdev_read_only(bdev))
603                 return -EPERM;
604
605         if (IS_SWAPFILE(bd_inode) && !is_hibernate_resume_dev(bd_inode->i_rdev))
606                 return -ETXTBSY;
607
608         if (!iov_iter_count(from))
609                 return 0;
610
611         if (iocb->ki_pos >= size)
612                 return -ENOSPC;
613
614         if ((iocb->ki_flags & (IOCB_NOWAIT | IOCB_DIRECT)) == IOCB_NOWAIT)
615                 return -EOPNOTSUPP;
616
617         size -= iocb->ki_pos;
618         if (iov_iter_count(from) > size) {
619                 shorted = iov_iter_count(from) - size;
620                 iov_iter_truncate(from, size);
621         }
622
623         ret = file_remove_privs(file);
624         if (ret)
625                 return ret;
626
627         ret = file_update_time(file);
628         if (ret)
629                 return ret;
630
631         if (iocb->ki_flags & IOCB_DIRECT) {
632                 ret = blkdev_direct_write(iocb, from);
633                 if (ret >= 0 && iov_iter_count(from))
634                         ret = direct_write_fallback(iocb, from, ret,
635                                         blkdev_buffered_write(iocb, from));
636         } else {
637                 ret = blkdev_buffered_write(iocb, from);
638         }
639
640         if (ret > 0)
641                 ret = generic_write_sync(iocb, ret);
642         iov_iter_reexpand(from, iov_iter_count(from) + shorted);
643         return ret;
644 }
645
646 static ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
647 {
648         struct block_device *bdev = I_BDEV(iocb->ki_filp->f_mapping->host);
649         loff_t size = bdev_nr_bytes(bdev);
650         loff_t pos = iocb->ki_pos;
651         size_t shorted = 0;
652         ssize_t ret = 0;
653         size_t count;
654
655         if (unlikely(pos + iov_iter_count(to) > size)) {
656                 if (pos >= size)
657                         return 0;
658                 size -= pos;
659                 shorted = iov_iter_count(to) - size;
660                 iov_iter_truncate(to, size);
661         }
662
663         count = iov_iter_count(to);
664         if (!count)
665                 goto reexpand; /* skip atime */
666
667         if (iocb->ki_flags & IOCB_DIRECT) {
668                 ret = kiocb_write_and_wait(iocb, count);
669                 if (ret < 0)
670                         goto reexpand;
671                 file_accessed(iocb->ki_filp);
672
673                 ret = blkdev_direct_IO(iocb, to);
674                 if (ret >= 0) {
675                         iocb->ki_pos += ret;
676                         count -= ret;
677                 }
678                 iov_iter_revert(to, count - iov_iter_count(to));
679                 if (ret < 0 || !count)
680                         goto reexpand;
681         }
682
683         ret = filemap_read(iocb, to, ret);
684
685 reexpand:
686         if (unlikely(shorted))
687                 iov_iter_reexpand(to, iov_iter_count(to) + shorted);
688         return ret;
689 }
690
691 #define BLKDEV_FALLOC_FL_SUPPORTED                                      \
692                 (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |           \
693                  FALLOC_FL_ZERO_RANGE | FALLOC_FL_NO_HIDE_STALE)
694
695 static long blkdev_fallocate(struct file *file, int mode, loff_t start,
696                              loff_t len)
697 {
698         struct inode *inode = bdev_file_inode(file);
699         struct block_device *bdev = I_BDEV(inode);
700         loff_t end = start + len - 1;
701         loff_t isize;
702         int error;
703
704         /* Fail if we don't recognize the flags. */
705         if (mode & ~BLKDEV_FALLOC_FL_SUPPORTED)
706                 return -EOPNOTSUPP;
707
708         /* Don't go off the end of the device. */
709         isize = bdev_nr_bytes(bdev);
710         if (start >= isize)
711                 return -EINVAL;
712         if (end >= isize) {
713                 if (mode & FALLOC_FL_KEEP_SIZE) {
714                         len = isize - start;
715                         end = start + len - 1;
716                 } else
717                         return -EINVAL;
718         }
719
720         /*
721          * Don't allow IO that isn't aligned to logical block size.
722          */
723         if ((start | len) & (bdev_logical_block_size(bdev) - 1))
724                 return -EINVAL;
725
726         filemap_invalidate_lock(inode->i_mapping);
727
728         /* Invalidate the page cache, including dirty pages. */
729         error = truncate_bdev_range(bdev, file_to_blk_mode(file), start, end);
730         if (error)
731                 goto fail;
732
733         switch (mode) {
734         case FALLOC_FL_ZERO_RANGE:
735         case FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE:
736                 error = blkdev_issue_zeroout(bdev, start >> SECTOR_SHIFT,
737                                              len >> SECTOR_SHIFT, GFP_KERNEL,
738                                              BLKDEV_ZERO_NOUNMAP);
739                 break;
740         case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE:
741                 error = blkdev_issue_zeroout(bdev, start >> SECTOR_SHIFT,
742                                              len >> SECTOR_SHIFT, GFP_KERNEL,
743                                              BLKDEV_ZERO_NOFALLBACK);
744                 break;
745         case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE | FALLOC_FL_NO_HIDE_STALE:
746                 error = blkdev_issue_discard(bdev, start >> SECTOR_SHIFT,
747                                              len >> SECTOR_SHIFT, GFP_KERNEL);
748                 break;
749         default:
750                 error = -EOPNOTSUPP;
751         }
752
753  fail:
754         filemap_invalidate_unlock(inode->i_mapping);
755         return error;
756 }
757
758 static int blkdev_mmap(struct file *file, struct vm_area_struct *vma)
759 {
760         struct inode *bd_inode = bdev_file_inode(file);
761
762         if (bdev_read_only(I_BDEV(bd_inode)))
763                 return generic_file_readonly_mmap(file, vma);
764
765         return generic_file_mmap(file, vma);
766 }
767
768 const struct file_operations def_blk_fops = {
769         .open           = blkdev_open,
770         .release        = blkdev_release,
771         .llseek         = blkdev_llseek,
772         .read_iter      = blkdev_read_iter,
773         .write_iter     = blkdev_write_iter,
774         .iopoll         = iocb_bio_iopoll,
775         .mmap           = blkdev_mmap,
776         .fsync          = blkdev_fsync,
777         .unlocked_ioctl = blkdev_ioctl,
778 #ifdef CONFIG_COMPAT
779         .compat_ioctl   = compat_blkdev_ioctl,
780 #endif
781         .splice_read    = filemap_splice_read,
782         .splice_write   = iter_file_splice_write,
783         .fallocate      = blkdev_fallocate,
784 };
785
786 static __init int blkdev_init(void)
787 {
788         return bioset_init(&blkdev_dio_pool, 4,
789                                 offsetof(struct blkdev_dio, bio),
790                                 BIOSET_NEED_BVECS|BIOSET_PERCPU_CACHE);
791 }
792 module_init(blkdev_init);
This page took 0.081496 seconds and 4 git commands to generate.