]> Git Repo - linux.git/blob - block/fops.c
blk-mq: don't handle non-flush requests in blk_insert_flush
[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 "blk.h"
19
20 static inline struct inode *bdev_file_inode(struct file *file)
21 {
22         return file->f_mapping->host;
23 }
24
25 static int blkdev_get_block(struct inode *inode, sector_t iblock,
26                 struct buffer_head *bh, int create)
27 {
28         bh->b_bdev = I_BDEV(inode);
29         bh->b_blocknr = iblock;
30         set_buffer_mapped(bh);
31         return 0;
32 }
33
34 static unsigned int dio_bio_write_op(struct kiocb *iocb)
35 {
36         unsigned int op = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
37
38         /* avoid the need for a I/O completion work item */
39         if (iocb->ki_flags & IOCB_DSYNC)
40                 op |= REQ_FUA;
41         return op;
42 }
43
44 #define DIO_INLINE_BIO_VECS 4
45
46 static void blkdev_bio_end_io_simple(struct bio *bio)
47 {
48         struct task_struct *waiter = bio->bi_private;
49
50         WRITE_ONCE(bio->bi_private, NULL);
51         blk_wake_io_task(waiter);
52 }
53
54 static ssize_t __blkdev_direct_IO_simple(struct kiocb *iocb,
55                 struct iov_iter *iter, unsigned int nr_pages)
56 {
57         struct block_device *bdev = iocb->ki_filp->private_data;
58         struct bio_vec inline_vecs[DIO_INLINE_BIO_VECS], *vecs;
59         loff_t pos = iocb->ki_pos;
60         bool should_dirty = false;
61         struct bio bio;
62         ssize_t ret;
63
64         if ((pos | iov_iter_alignment(iter)) &
65             (bdev_logical_block_size(bdev) - 1))
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         bio_init(&bio, vecs, nr_pages);
78         bio_set_dev(&bio, bdev);
79         bio.bi_iter.bi_sector = pos >> 9;
80         bio.bi_write_hint = iocb->ki_hint;
81         bio.bi_private = current;
82         bio.bi_end_io = blkdev_bio_end_io_simple;
83         bio.bi_ioprio = iocb->ki_ioprio;
84
85         ret = bio_iov_iter_get_pages(&bio, iter);
86         if (unlikely(ret))
87                 goto out;
88         ret = bio.bi_iter.bi_size;
89
90         if (iov_iter_rw(iter) == READ) {
91                 bio.bi_opf = REQ_OP_READ;
92                 if (iter_is_iovec(iter))
93                         should_dirty = true;
94         } else {
95                 bio.bi_opf = dio_bio_write_op(iocb);
96                 task_io_account_write(ret);
97         }
98         if (iocb->ki_flags & IOCB_NOWAIT)
99                 bio.bi_opf |= REQ_NOWAIT;
100         if (iocb->ki_flags & IOCB_HIPRI)
101                 bio_set_polled(&bio, iocb);
102
103         submit_bio(&bio);
104         for (;;) {
105                 set_current_state(TASK_UNINTERRUPTIBLE);
106                 if (!READ_ONCE(bio.bi_private))
107                         break;
108                 if (!(iocb->ki_flags & IOCB_HIPRI) || !bio_poll(&bio, NULL, 0))
109                         blk_io_schedule();
110         }
111         __set_current_state(TASK_RUNNING);
112
113         bio_release_pages(&bio, should_dirty);
114         if (unlikely(bio.bi_status))
115                 ret = blk_status_to_errno(bio.bi_status);
116
117 out:
118         if (vecs != inline_vecs)
119                 kfree(vecs);
120
121         bio_uninit(&bio);
122
123         return ret;
124 }
125
126 enum {
127         DIO_MULTI_BIO           = 1,
128         DIO_SHOULD_DIRTY        = 2,
129         DIO_IS_SYNC             = 4,
130 };
131
132 struct blkdev_dio {
133         union {
134                 struct kiocb            *iocb;
135                 struct task_struct      *waiter;
136         };
137         size_t                  size;
138         atomic_t                ref;
139         unsigned int            flags;
140         struct bio              bio ____cacheline_aligned_in_smp;
141 };
142
143 static struct bio_set blkdev_dio_pool;
144
145 static void blkdev_bio_end_io(struct bio *bio)
146 {
147         struct blkdev_dio *dio = bio->bi_private;
148         bool should_dirty = dio->flags & DIO_SHOULD_DIRTY;
149
150         if (bio->bi_status && !dio->bio.bi_status)
151                 dio->bio.bi_status = bio->bi_status;
152
153         if (!(dio->flags & DIO_MULTI_BIO) || atomic_dec_and_test(&dio->ref)) {
154                 if (!(dio->flags & DIO_IS_SYNC)) {
155                         struct kiocb *iocb = dio->iocb;
156                         ssize_t ret;
157
158                         WRITE_ONCE(iocb->private, NULL);
159
160                         if (likely(!dio->bio.bi_status)) {
161                                 ret = dio->size;
162                                 iocb->ki_pos += ret;
163                         } else {
164                                 ret = blk_status_to_errno(dio->bio.bi_status);
165                         }
166
167                         dio->iocb->ki_complete(iocb, ret, 0);
168                         if (dio->flags & DIO_MULTI_BIO)
169                                 bio_put(&dio->bio);
170                 } else {
171                         struct task_struct *waiter = dio->waiter;
172
173                         WRITE_ONCE(dio->waiter, NULL);
174                         blk_wake_io_task(waiter);
175                 }
176         }
177
178         if (should_dirty) {
179                 bio_check_pages_dirty(bio);
180         } else {
181                 bio_release_pages(bio, false);
182                 bio_put(bio);
183         }
184 }
185
186 static ssize_t __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
187                 unsigned int nr_pages)
188 {
189         struct block_device *bdev = iocb->ki_filp->private_data;
190         struct blk_plug plug;
191         struct blkdev_dio *dio;
192         struct bio *bio;
193         bool do_poll = (iocb->ki_flags & IOCB_HIPRI);
194         bool is_read = (iov_iter_rw(iter) == READ), is_sync;
195         loff_t pos = iocb->ki_pos;
196         int ret = 0;
197
198         if ((pos | iov_iter_alignment(iter)) &
199             (bdev_logical_block_size(bdev) - 1))
200                 return -EINVAL;
201
202         bio = bio_alloc_kiocb(iocb, nr_pages, &blkdev_dio_pool);
203
204         dio = container_of(bio, struct blkdev_dio, bio);
205         is_sync = is_sync_kiocb(iocb);
206         if (is_sync) {
207                 dio->flags = DIO_IS_SYNC;
208                 dio->waiter = current;
209                 bio_get(bio);
210         } else {
211                 dio->flags = 0;
212                 dio->iocb = iocb;
213         }
214
215         dio->size = 0;
216         if (is_read && iter_is_iovec(iter))
217                 dio->flags |= DIO_SHOULD_DIRTY;
218
219         /*
220          * Don't plug for HIPRI/polled IO, as those should go straight
221          * to issue
222          */
223         if (!(iocb->ki_flags & IOCB_HIPRI))
224                 blk_start_plug(&plug);
225
226         for (;;) {
227                 bio_set_dev(bio, bdev);
228                 bio->bi_iter.bi_sector = pos >> 9;
229                 bio->bi_write_hint = iocb->ki_hint;
230                 bio->bi_private = dio;
231                 bio->bi_end_io = blkdev_bio_end_io;
232                 bio->bi_ioprio = iocb->ki_ioprio;
233
234                 ret = bio_iov_iter_get_pages(bio, iter);
235                 if (unlikely(ret)) {
236                         bio->bi_status = BLK_STS_IOERR;
237                         bio_endio(bio);
238                         break;
239                 }
240
241                 if (is_read) {
242                         bio->bi_opf = REQ_OP_READ;
243                         if (dio->flags & DIO_SHOULD_DIRTY)
244                                 bio_set_pages_dirty(bio);
245                 } else {
246                         bio->bi_opf = dio_bio_write_op(iocb);
247                         task_io_account_write(bio->bi_iter.bi_size);
248                 }
249                 if (iocb->ki_flags & IOCB_NOWAIT)
250                         bio->bi_opf |= REQ_NOWAIT;
251
252                 dio->size += bio->bi_iter.bi_size;
253                 pos += bio->bi_iter.bi_size;
254
255                 nr_pages = bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS);
256                 if (!nr_pages) {
257                         if (do_poll)
258                                 bio_set_polled(bio, iocb);
259                         submit_bio(bio);
260                         if (do_poll)
261                                 WRITE_ONCE(iocb->private, bio);
262                         break;
263                 }
264                 if (!(dio->flags & DIO_MULTI_BIO)) {
265                         /*
266                          * AIO needs an extra reference to ensure the dio
267                          * structure which is embedded into the first bio
268                          * stays around.
269                          */
270                         if (!is_sync)
271                                 bio_get(bio);
272                         dio->flags |= DIO_MULTI_BIO;
273                         atomic_set(&dio->ref, 2);
274                         do_poll = false;
275                 } else {
276                         atomic_inc(&dio->ref);
277                 }
278
279                 submit_bio(bio);
280                 bio = bio_alloc(GFP_KERNEL, nr_pages);
281         }
282
283         if (!(iocb->ki_flags & IOCB_HIPRI))
284                 blk_finish_plug(&plug);
285
286         if (!is_sync)
287                 return -EIOCBQUEUED;
288
289         for (;;) {
290                 set_current_state(TASK_UNINTERRUPTIBLE);
291                 if (!READ_ONCE(dio->waiter))
292                         break;
293
294                 if (!do_poll || !bio_poll(bio, NULL, 0))
295                         blk_io_schedule();
296         }
297         __set_current_state(TASK_RUNNING);
298
299         if (!ret)
300                 ret = blk_status_to_errno(dio->bio.bi_status);
301         if (likely(!ret))
302                 ret = dio->size;
303
304         bio_put(&dio->bio);
305         return ret;
306 }
307
308 static ssize_t blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
309 {
310         unsigned int nr_pages;
311
312         if (!iov_iter_count(iter))
313                 return 0;
314
315         nr_pages = bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS + 1);
316         if (is_sync_kiocb(iocb) && nr_pages <= BIO_MAX_VECS)
317                 return __blkdev_direct_IO_simple(iocb, iter, nr_pages);
318
319         return __blkdev_direct_IO(iocb, iter, bio_max_segs(nr_pages));
320 }
321
322 static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
323 {
324         return block_write_full_page(page, blkdev_get_block, wbc);
325 }
326
327 static int blkdev_readpage(struct file * file, struct page * page)
328 {
329         return block_read_full_page(page, blkdev_get_block);
330 }
331
332 static void blkdev_readahead(struct readahead_control *rac)
333 {
334         mpage_readahead(rac, blkdev_get_block);
335 }
336
337 static int blkdev_write_begin(struct file *file, struct address_space *mapping,
338                 loff_t pos, unsigned len, unsigned flags, struct page **pagep,
339                 void **fsdata)
340 {
341         return block_write_begin(mapping, pos, len, flags, pagep,
342                                  blkdev_get_block);
343 }
344
345 static int blkdev_write_end(struct file *file, struct address_space *mapping,
346                 loff_t pos, unsigned len, unsigned copied, struct page *page,
347                 void *fsdata)
348 {
349         int ret;
350         ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
351
352         unlock_page(page);
353         put_page(page);
354
355         return ret;
356 }
357
358 static int blkdev_writepages(struct address_space *mapping,
359                              struct writeback_control *wbc)
360 {
361         return generic_writepages(mapping, wbc);
362 }
363
364 const struct address_space_operations def_blk_aops = {
365         .set_page_dirty = __set_page_dirty_buffers,
366         .readpage       = blkdev_readpage,
367         .readahead      = blkdev_readahead,
368         .writepage      = blkdev_writepage,
369         .write_begin    = blkdev_write_begin,
370         .write_end      = blkdev_write_end,
371         .writepages     = blkdev_writepages,
372         .direct_IO      = blkdev_direct_IO,
373         .migratepage    = buffer_migrate_page_norefs,
374         .is_dirty_writeback = buffer_check_dirty_writeback,
375 };
376
377 /*
378  * for a block special file file_inode(file)->i_size is zero
379  * so we compute the size by hand (just as in block_read/write above)
380  */
381 static loff_t blkdev_llseek(struct file *file, loff_t offset, int whence)
382 {
383         struct inode *bd_inode = bdev_file_inode(file);
384         loff_t retval;
385
386         inode_lock(bd_inode);
387         retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode));
388         inode_unlock(bd_inode);
389         return retval;
390 }
391
392 static int blkdev_fsync(struct file *filp, loff_t start, loff_t end,
393                 int datasync)
394 {
395         struct block_device *bdev = filp->private_data;
396         int error;
397
398         error = file_write_and_wait_range(filp, start, end);
399         if (error)
400                 return error;
401
402         /*
403          * There is no need to serialise calls to blkdev_issue_flush with
404          * i_mutex and doing so causes performance issues with concurrent
405          * O_SYNC writers to a block device.
406          */
407         error = blkdev_issue_flush(bdev);
408         if (error == -EOPNOTSUPP)
409                 error = 0;
410
411         return error;
412 }
413
414 static int blkdev_open(struct inode *inode, struct file *filp)
415 {
416         struct block_device *bdev;
417
418         /*
419          * Preserve backwards compatibility and allow large file access
420          * even if userspace doesn't ask for it explicitly. Some mkfs
421          * binary needs it. We might want to drop this workaround
422          * during an unstable branch.
423          */
424         filp->f_flags |= O_LARGEFILE;
425         filp->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC;
426
427         if (filp->f_flags & O_NDELAY)
428                 filp->f_mode |= FMODE_NDELAY;
429         if (filp->f_flags & O_EXCL)
430                 filp->f_mode |= FMODE_EXCL;
431         if ((filp->f_flags & O_ACCMODE) == 3)
432                 filp->f_mode |= FMODE_WRITE_IOCTL;
433
434         bdev = blkdev_get_by_dev(inode->i_rdev, filp->f_mode, filp);
435         if (IS_ERR(bdev))
436                 return PTR_ERR(bdev);
437
438         filp->private_data = bdev;
439         filp->f_mapping = bdev->bd_inode->i_mapping;
440         filp->f_wb_err = filemap_sample_wb_err(filp->f_mapping);
441         return 0;
442 }
443
444 static int blkdev_close(struct inode *inode, struct file *filp)
445 {
446         struct block_device *bdev = filp->private_data;
447
448         blkdev_put(bdev, filp->f_mode);
449         return 0;
450 }
451
452 /*
453  * Write data to the block device.  Only intended for the block device itself
454  * and the raw driver which basically is a fake block device.
455  *
456  * Does not take i_mutex for the write and thus is not for general purpose
457  * use.
458  */
459 static ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
460 {
461         struct block_device *bdev = iocb->ki_filp->private_data;
462         struct inode *bd_inode = bdev->bd_inode;
463         loff_t size = i_size_read(bd_inode);
464         struct blk_plug plug;
465         size_t shorted = 0;
466         ssize_t ret;
467
468         if (bdev_read_only(bdev))
469                 return -EPERM;
470
471         if (IS_SWAPFILE(bd_inode) && !is_hibernate_resume_dev(bd_inode->i_rdev))
472                 return -ETXTBSY;
473
474         if (!iov_iter_count(from))
475                 return 0;
476
477         if (iocb->ki_pos >= size)
478                 return -ENOSPC;
479
480         if ((iocb->ki_flags & (IOCB_NOWAIT | IOCB_DIRECT)) == IOCB_NOWAIT)
481                 return -EOPNOTSUPP;
482
483         size -= iocb->ki_pos;
484         if (iov_iter_count(from) > size) {
485                 shorted = iov_iter_count(from) - size;
486                 iov_iter_truncate(from, size);
487         }
488
489         blk_start_plug(&plug);
490         ret = __generic_file_write_iter(iocb, from);
491         if (ret > 0)
492                 ret = generic_write_sync(iocb, ret);
493         iov_iter_reexpand(from, iov_iter_count(from) + shorted);
494         blk_finish_plug(&plug);
495         return ret;
496 }
497
498 static ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
499 {
500         struct block_device *bdev = iocb->ki_filp->private_data;
501         loff_t size = i_size_read(bdev->bd_inode);
502         loff_t pos = iocb->ki_pos;
503         size_t shorted = 0;
504         ssize_t ret;
505
506         if (pos >= size)
507                 return 0;
508
509         size -= pos;
510         if (iov_iter_count(to) > size) {
511                 shorted = iov_iter_count(to) - size;
512                 iov_iter_truncate(to, size);
513         }
514
515         ret = generic_file_read_iter(iocb, to);
516         iov_iter_reexpand(to, iov_iter_count(to) + shorted);
517         return ret;
518 }
519
520 #define BLKDEV_FALLOC_FL_SUPPORTED                                      \
521                 (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |           \
522                  FALLOC_FL_ZERO_RANGE | FALLOC_FL_NO_HIDE_STALE)
523
524 static long blkdev_fallocate(struct file *file, int mode, loff_t start,
525                              loff_t len)
526 {
527         struct inode *inode = bdev_file_inode(file);
528         struct block_device *bdev = I_BDEV(inode);
529         loff_t end = start + len - 1;
530         loff_t isize;
531         int error;
532
533         /* Fail if we don't recognize the flags. */
534         if (mode & ~BLKDEV_FALLOC_FL_SUPPORTED)
535                 return -EOPNOTSUPP;
536
537         /* Don't go off the end of the device. */
538         isize = i_size_read(bdev->bd_inode);
539         if (start >= isize)
540                 return -EINVAL;
541         if (end >= isize) {
542                 if (mode & FALLOC_FL_KEEP_SIZE) {
543                         len = isize - start;
544                         end = start + len - 1;
545                 } else
546                         return -EINVAL;
547         }
548
549         /*
550          * Don't allow IO that isn't aligned to logical block size.
551          */
552         if ((start | len) & (bdev_logical_block_size(bdev) - 1))
553                 return -EINVAL;
554
555         filemap_invalidate_lock(inode->i_mapping);
556
557         /* Invalidate the page cache, including dirty pages. */
558         error = truncate_bdev_range(bdev, file->f_mode, start, end);
559         if (error)
560                 goto fail;
561
562         switch (mode) {
563         case FALLOC_FL_ZERO_RANGE:
564         case FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE:
565                 error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
566                                             GFP_KERNEL, BLKDEV_ZERO_NOUNMAP);
567                 break;
568         case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE:
569                 error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
570                                              GFP_KERNEL, BLKDEV_ZERO_NOFALLBACK);
571                 break;
572         case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE | FALLOC_FL_NO_HIDE_STALE:
573                 error = blkdev_issue_discard(bdev, start >> 9, len >> 9,
574                                              GFP_KERNEL, 0);
575                 break;
576         default:
577                 error = -EOPNOTSUPP;
578         }
579
580  fail:
581         filemap_invalidate_unlock(inode->i_mapping);
582         return error;
583 }
584
585 const struct file_operations def_blk_fops = {
586         .open           = blkdev_open,
587         .release        = blkdev_close,
588         .llseek         = blkdev_llseek,
589         .read_iter      = blkdev_read_iter,
590         .write_iter     = blkdev_write_iter,
591         .iopoll         = iocb_bio_iopoll,
592         .mmap           = generic_file_mmap,
593         .fsync          = blkdev_fsync,
594         .unlocked_ioctl = blkdev_ioctl,
595 #ifdef CONFIG_COMPAT
596         .compat_ioctl   = compat_blkdev_ioctl,
597 #endif
598         .splice_read    = generic_file_splice_read,
599         .splice_write   = iter_file_splice_write,
600         .fallocate      = blkdev_fallocate,
601 };
602
603 static __init int blkdev_init(void)
604 {
605         return bioset_init(&blkdev_dio_pool, 4,
606                                 offsetof(struct blkdev_dio, bio),
607                                 BIOSET_NEED_BVECS|BIOSET_PERCPU_CACHE);
608 }
609 module_init(blkdev_init);
This page took 0.065506 seconds and 4 git commands to generate.