1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
5 * Copyright (C) 2016 - 2020 Christoph Hellwig
7 #include <linux/init.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>
18 #include <linux/module.h>
21 static inline struct inode *bdev_file_inode(struct file *file)
23 return file->f_mapping->host;
26 static int blkdev_get_block(struct inode *inode, sector_t iblock,
27 struct buffer_head *bh, int create)
29 bh->b_bdev = I_BDEV(inode);
30 bh->b_blocknr = iblock;
31 set_buffer_mapped(bh);
35 static unsigned int dio_bio_write_op(struct kiocb *iocb)
37 unsigned int op = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
39 /* avoid the need for a I/O completion work item */
40 if (iocb->ki_flags & IOCB_DSYNC)
45 #define DIO_INLINE_BIO_VECS 4
47 static void blkdev_bio_end_io_simple(struct bio *bio)
49 struct task_struct *waiter = bio->bi_private;
51 WRITE_ONCE(bio->bi_private, NULL);
52 blk_wake_io_task(waiter);
55 static ssize_t __blkdev_direct_IO_simple(struct kiocb *iocb,
56 struct iov_iter *iter, unsigned int nr_pages)
58 struct block_device *bdev = iocb->ki_filp->private_data;
59 struct bio_vec inline_vecs[DIO_INLINE_BIO_VECS], *vecs;
60 loff_t pos = iocb->ki_pos;
61 bool should_dirty = false;
65 if ((pos | iov_iter_alignment(iter)) &
66 (bdev_logical_block_size(bdev) - 1))
69 if (nr_pages <= DIO_INLINE_BIO_VECS)
72 vecs = kmalloc_array(nr_pages, sizeof(struct bio_vec),
78 bio_init(&bio, vecs, nr_pages);
79 bio_set_dev(&bio, bdev);
80 bio.bi_iter.bi_sector = pos >> SECTOR_SHIFT;
81 bio.bi_write_hint = iocb->ki_hint;
82 bio.bi_private = current;
83 bio.bi_end_io = blkdev_bio_end_io_simple;
84 bio.bi_ioprio = iocb->ki_ioprio;
86 ret = bio_iov_iter_get_pages(&bio, iter);
89 ret = bio.bi_iter.bi_size;
91 if (iov_iter_rw(iter) == READ) {
92 bio.bi_opf = REQ_OP_READ;
93 if (iter_is_iovec(iter))
96 bio.bi_opf = dio_bio_write_op(iocb);
97 task_io_account_write(ret);
99 if (iocb->ki_flags & IOCB_NOWAIT)
100 bio.bi_opf |= REQ_NOWAIT;
101 if (iocb->ki_flags & IOCB_HIPRI)
102 bio_set_polled(&bio, iocb);
106 set_current_state(TASK_UNINTERRUPTIBLE);
107 if (!READ_ONCE(bio.bi_private))
109 if (!(iocb->ki_flags & IOCB_HIPRI) || !bio_poll(&bio, NULL, 0))
112 __set_current_state(TASK_RUNNING);
114 bio_release_pages(&bio, should_dirty);
115 if (unlikely(bio.bi_status))
116 ret = blk_status_to_errno(bio.bi_status);
119 if (vecs != inline_vecs)
128 DIO_SHOULD_DIRTY = 1,
135 struct task_struct *waiter;
140 struct bio bio ____cacheline_aligned_in_smp;
143 static struct bio_set blkdev_dio_pool;
145 static void blkdev_bio_end_io(struct bio *bio)
147 struct blkdev_dio *dio = bio->bi_private;
148 bool should_dirty = dio->flags & DIO_SHOULD_DIRTY;
150 if (bio->bi_status && !dio->bio.bi_status)
151 dio->bio.bi_status = bio->bi_status;
153 if (atomic_dec_and_test(&dio->ref)) {
154 if (!(dio->flags & DIO_IS_SYNC)) {
155 struct kiocb *iocb = dio->iocb;
158 WRITE_ONCE(iocb->private, NULL);
160 if (likely(!dio->bio.bi_status)) {
164 ret = blk_status_to_errno(dio->bio.bi_status);
167 dio->iocb->ki_complete(iocb, ret);
170 struct task_struct *waiter = dio->waiter;
172 WRITE_ONCE(dio->waiter, NULL);
173 blk_wake_io_task(waiter);
178 bio_check_pages_dirty(bio);
180 bio_release_pages(bio, false);
185 static ssize_t __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
186 unsigned int nr_pages)
188 struct block_device *bdev = iocb->ki_filp->private_data;
189 struct blk_plug plug;
190 struct blkdev_dio *dio;
192 bool is_read = (iov_iter_rw(iter) == READ), is_sync;
193 loff_t pos = iocb->ki_pos;
196 if ((pos | iov_iter_alignment(iter)) &
197 (bdev_logical_block_size(bdev) - 1))
200 bio = bio_alloc_kiocb(iocb, nr_pages, &blkdev_dio_pool);
202 dio = container_of(bio, struct blkdev_dio, bio);
203 atomic_set(&dio->ref, 1);
205 * Grab an extra reference to ensure the dio structure which is embedded
206 * into the first bio stays around.
210 is_sync = is_sync_kiocb(iocb);
212 dio->flags = DIO_IS_SYNC;
213 dio->waiter = current;
220 if (is_read && iter_is_iovec(iter))
221 dio->flags |= DIO_SHOULD_DIRTY;
223 blk_start_plug(&plug);
226 bio_set_dev(bio, bdev);
227 bio->bi_iter.bi_sector = pos >> SECTOR_SHIFT;
228 bio->bi_write_hint = iocb->ki_hint;
229 bio->bi_private = dio;
230 bio->bi_end_io = blkdev_bio_end_io;
231 bio->bi_ioprio = iocb->ki_ioprio;
233 ret = bio_iov_iter_get_pages(bio, iter);
235 bio->bi_status = BLK_STS_IOERR;
241 bio->bi_opf = REQ_OP_READ;
242 if (dio->flags & DIO_SHOULD_DIRTY)
243 bio_set_pages_dirty(bio);
245 bio->bi_opf = dio_bio_write_op(iocb);
246 task_io_account_write(bio->bi_iter.bi_size);
248 if (iocb->ki_flags & IOCB_NOWAIT)
249 bio->bi_opf |= REQ_NOWAIT;
251 dio->size += bio->bi_iter.bi_size;
252 pos += bio->bi_iter.bi_size;
254 nr_pages = bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS);
259 atomic_inc(&dio->ref);
261 bio = bio_alloc(GFP_KERNEL, nr_pages);
264 blk_finish_plug(&plug);
270 set_current_state(TASK_UNINTERRUPTIBLE);
271 if (!READ_ONCE(dio->waiter))
275 __set_current_state(TASK_RUNNING);
278 ret = blk_status_to_errno(dio->bio.bi_status);
286 static void blkdev_bio_end_io_async(struct bio *bio)
288 struct blkdev_dio *dio = container_of(bio, struct blkdev_dio, bio);
289 struct kiocb *iocb = dio->iocb;
292 if (likely(!bio->bi_status)) {
296 ret = blk_status_to_errno(bio->bi_status);
299 iocb->ki_complete(iocb, ret);
301 if (dio->flags & DIO_SHOULD_DIRTY) {
302 bio_check_pages_dirty(bio);
304 bio_release_pages(bio, false);
309 static ssize_t __blkdev_direct_IO_async(struct kiocb *iocb,
310 struct iov_iter *iter,
311 unsigned int nr_pages)
313 struct block_device *bdev = iocb->ki_filp->private_data;
314 struct blkdev_dio *dio;
316 loff_t pos = iocb->ki_pos;
319 if ((pos | iov_iter_alignment(iter)) &
320 (bdev_logical_block_size(bdev) - 1))
323 bio = bio_alloc_kiocb(iocb, nr_pages, &blkdev_dio_pool);
324 dio = container_of(bio, struct blkdev_dio, bio);
327 bio_set_dev(bio, bdev);
328 bio->bi_iter.bi_sector = pos >> SECTOR_SHIFT;
329 bio->bi_write_hint = iocb->ki_hint;
330 bio->bi_end_io = blkdev_bio_end_io_async;
331 bio->bi_ioprio = iocb->ki_ioprio;
333 if (iov_iter_is_bvec(iter)) {
335 * Users don't rely on the iterator being in any particular
336 * state for async I/O returning -EIOCBQUEUED, hence we can
337 * avoid expensive iov_iter_advance(). Bypass
338 * bio_iov_iter_get_pages() and set the bvec directly.
340 bio_iov_bvec_set(bio, iter);
342 ret = bio_iov_iter_get_pages(bio, iter);
344 bio->bi_status = BLK_STS_IOERR;
349 dio->size = bio->bi_iter.bi_size;
351 if (iov_iter_rw(iter) == READ) {
352 bio->bi_opf = REQ_OP_READ;
353 if (iter_is_iovec(iter)) {
354 dio->flags |= DIO_SHOULD_DIRTY;
355 bio_set_pages_dirty(bio);
358 bio->bi_opf = dio_bio_write_op(iocb);
359 task_io_account_write(bio->bi_iter.bi_size);
362 if (iocb->ki_flags & IOCB_HIPRI) {
363 bio->bi_opf |= REQ_POLLED | REQ_NOWAIT;
365 WRITE_ONCE(iocb->private, bio);
367 if (iocb->ki_flags & IOCB_NOWAIT)
368 bio->bi_opf |= REQ_NOWAIT;
374 static ssize_t blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
376 unsigned int nr_pages;
378 if (!iov_iter_count(iter))
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);
387 return __blkdev_direct_IO(iocb, iter, bio_max_segs(nr_pages));
390 static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
392 return block_write_full_page(page, blkdev_get_block, wbc);
395 static int blkdev_readpage(struct file * file, struct page * page)
397 return block_read_full_page(page, blkdev_get_block);
400 static void blkdev_readahead(struct readahead_control *rac)
402 mpage_readahead(rac, blkdev_get_block);
405 static int blkdev_write_begin(struct file *file, struct address_space *mapping,
406 loff_t pos, unsigned len, unsigned flags, struct page **pagep,
409 return block_write_begin(mapping, pos, len, flags, pagep,
413 static int blkdev_write_end(struct file *file, struct address_space *mapping,
414 loff_t pos, unsigned len, unsigned copied, struct page *page,
418 ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
426 static int blkdev_writepages(struct address_space *mapping,
427 struct writeback_control *wbc)
429 return generic_writepages(mapping, wbc);
432 const struct address_space_operations def_blk_aops = {
433 .set_page_dirty = __set_page_dirty_buffers,
434 .readpage = blkdev_readpage,
435 .readahead = blkdev_readahead,
436 .writepage = blkdev_writepage,
437 .write_begin = blkdev_write_begin,
438 .write_end = blkdev_write_end,
439 .writepages = blkdev_writepages,
440 .direct_IO = blkdev_direct_IO,
441 .migratepage = buffer_migrate_page_norefs,
442 .is_dirty_writeback = buffer_check_dirty_writeback,
446 * for a block special file file_inode(file)->i_size is zero
447 * so we compute the size by hand (just as in block_read/write above)
449 static loff_t blkdev_llseek(struct file *file, loff_t offset, int whence)
451 struct inode *bd_inode = bdev_file_inode(file);
454 inode_lock(bd_inode);
455 retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode));
456 inode_unlock(bd_inode);
460 static int blkdev_fsync(struct file *filp, loff_t start, loff_t end,
463 struct block_device *bdev = filp->private_data;
466 error = file_write_and_wait_range(filp, start, end);
471 * There is no need to serialise calls to blkdev_issue_flush with
472 * i_mutex and doing so causes performance issues with concurrent
473 * O_SYNC writers to a block device.
475 error = blkdev_issue_flush(bdev);
476 if (error == -EOPNOTSUPP)
482 static int blkdev_open(struct inode *inode, struct file *filp)
484 struct block_device *bdev;
487 * Preserve backwards compatibility and allow large file access
488 * even if userspace doesn't ask for it explicitly. Some mkfs
489 * binary needs it. We might want to drop this workaround
490 * during an unstable branch.
492 filp->f_flags |= O_LARGEFILE;
493 filp->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC;
495 if (filp->f_flags & O_NDELAY)
496 filp->f_mode |= FMODE_NDELAY;
497 if (filp->f_flags & O_EXCL)
498 filp->f_mode |= FMODE_EXCL;
499 if ((filp->f_flags & O_ACCMODE) == 3)
500 filp->f_mode |= FMODE_WRITE_IOCTL;
502 bdev = blkdev_get_by_dev(inode->i_rdev, filp->f_mode, filp);
504 return PTR_ERR(bdev);
506 filp->private_data = bdev;
507 filp->f_mapping = bdev->bd_inode->i_mapping;
508 filp->f_wb_err = filemap_sample_wb_err(filp->f_mapping);
512 static int blkdev_close(struct inode *inode, struct file *filp)
514 struct block_device *bdev = filp->private_data;
516 blkdev_put(bdev, filp->f_mode);
521 * Write data to the block device. Only intended for the block device itself
522 * and the raw driver which basically is a fake block device.
524 * Does not take i_mutex for the write and thus is not for general purpose
527 static ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
529 struct block_device *bdev = iocb->ki_filp->private_data;
530 struct inode *bd_inode = bdev->bd_inode;
531 loff_t size = bdev_nr_bytes(bdev);
532 struct blk_plug plug;
536 if (bdev_read_only(bdev))
539 if (IS_SWAPFILE(bd_inode) && !is_hibernate_resume_dev(bd_inode->i_rdev))
542 if (!iov_iter_count(from))
545 if (iocb->ki_pos >= size)
548 if ((iocb->ki_flags & (IOCB_NOWAIT | IOCB_DIRECT)) == IOCB_NOWAIT)
551 size -= iocb->ki_pos;
552 if (iov_iter_count(from) > size) {
553 shorted = iov_iter_count(from) - size;
554 iov_iter_truncate(from, size);
557 blk_start_plug(&plug);
558 ret = __generic_file_write_iter(iocb, from);
560 ret = generic_write_sync(iocb, ret);
561 iov_iter_reexpand(from, iov_iter_count(from) + shorted);
562 blk_finish_plug(&plug);
566 static ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
568 struct block_device *bdev = iocb->ki_filp->private_data;
569 loff_t size = bdev_nr_bytes(bdev);
570 loff_t pos = iocb->ki_pos;
574 if (unlikely(pos + iov_iter_count(to) > size)) {
578 if (iov_iter_count(to) > size) {
579 shorted = iov_iter_count(to) - size;
580 iov_iter_truncate(to, size);
584 ret = generic_file_read_iter(iocb, to);
586 if (unlikely(shorted))
587 iov_iter_reexpand(to, iov_iter_count(to) + shorted);
591 #define BLKDEV_FALLOC_FL_SUPPORTED \
592 (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
593 FALLOC_FL_ZERO_RANGE | FALLOC_FL_NO_HIDE_STALE)
595 static long blkdev_fallocate(struct file *file, int mode, loff_t start,
598 struct inode *inode = bdev_file_inode(file);
599 struct block_device *bdev = I_BDEV(inode);
600 loff_t end = start + len - 1;
604 /* Fail if we don't recognize the flags. */
605 if (mode & ~BLKDEV_FALLOC_FL_SUPPORTED)
608 /* Don't go off the end of the device. */
609 isize = bdev_nr_bytes(bdev);
613 if (mode & FALLOC_FL_KEEP_SIZE) {
615 end = start + len - 1;
621 * Don't allow IO that isn't aligned to logical block size.
623 if ((start | len) & (bdev_logical_block_size(bdev) - 1))
626 filemap_invalidate_lock(inode->i_mapping);
628 /* Invalidate the page cache, including dirty pages. */
629 error = truncate_bdev_range(bdev, file->f_mode, start, end);
634 case FALLOC_FL_ZERO_RANGE:
635 case FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE:
636 error = blkdev_issue_zeroout(bdev, start >> SECTOR_SHIFT,
637 len >> SECTOR_SHIFT, GFP_KERNEL,
638 BLKDEV_ZERO_NOUNMAP);
640 case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE:
641 error = blkdev_issue_zeroout(bdev, start >> SECTOR_SHIFT,
642 len >> SECTOR_SHIFT, GFP_KERNEL,
643 BLKDEV_ZERO_NOFALLBACK);
645 case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE | FALLOC_FL_NO_HIDE_STALE:
646 error = blkdev_issue_discard(bdev, start >> SECTOR_SHIFT,
647 len >> SECTOR_SHIFT, GFP_KERNEL, 0);
654 filemap_invalidate_unlock(inode->i_mapping);
658 const struct file_operations def_blk_fops = {
660 .release = blkdev_close,
661 .llseek = blkdev_llseek,
662 .read_iter = blkdev_read_iter,
663 .write_iter = blkdev_write_iter,
664 .iopoll = iocb_bio_iopoll,
665 .mmap = generic_file_mmap,
666 .fsync = blkdev_fsync,
667 .unlocked_ioctl = blkdev_ioctl,
669 .compat_ioctl = compat_blkdev_ioctl,
671 .splice_read = generic_file_splice_read,
672 .splice_write = iter_file_splice_write,
673 .fallocate = blkdev_fallocate,
676 static __init int blkdev_init(void)
678 return bioset_init(&blkdev_dio_pool, 4,
679 offsetof(struct blkdev_dio, bio),
680 BIOSET_NEED_BVECS|BIOSET_PERCPU_CACHE);
682 module_init(blkdev_init);