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 blk_opf_t dio_bio_write_op(struct kiocb *iocb)
37 blk_opf_t opf = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
39 /* avoid the need for a I/O completion work item */
40 if (iocb_is_dsync(iocb))
45 static bool blkdev_dio_unaligned(struct block_device *bdev, loff_t pos,
46 struct iov_iter *iter)
48 return pos & (bdev_logical_block_size(bdev) - 1) ||
49 !bdev_iter_is_aligned(bdev, iter);
52 #define DIO_INLINE_BIO_VECS 4
54 static ssize_t __blkdev_direct_IO_simple(struct kiocb *iocb,
55 struct iov_iter *iter, unsigned int nr_pages)
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;
64 if (blkdev_dio_unaligned(bdev, pos, iter))
67 if (nr_pages <= DIO_INLINE_BIO_VECS)
70 vecs = kmalloc_array(nr_pages, sizeof(struct bio_vec),
76 if (iov_iter_rw(iter) == READ) {
77 bio_init(&bio, bdev, vecs, nr_pages, REQ_OP_READ);
78 if (user_backed_iter(iter))
81 bio_init(&bio, bdev, vecs, nr_pages, dio_bio_write_op(iocb));
83 bio.bi_iter.bi_sector = pos >> SECTOR_SHIFT;
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) == WRITE)
92 task_io_account_write(ret);
94 if (iocb->ki_flags & IOCB_NOWAIT)
95 bio.bi_opf |= REQ_NOWAIT;
97 submit_bio_wait(&bio);
99 bio_release_pages(&bio, should_dirty);
100 if (unlikely(bio.bi_status))
101 ret = blk_status_to_errno(bio.bi_status);
104 if (vecs != inline_vecs)
113 DIO_SHOULD_DIRTY = 1,
120 struct task_struct *waiter;
125 struct bio bio ____cacheline_aligned_in_smp;
128 static struct bio_set blkdev_dio_pool;
130 static void blkdev_bio_end_io(struct bio *bio)
132 struct blkdev_dio *dio = bio->bi_private;
133 bool should_dirty = dio->flags & DIO_SHOULD_DIRTY;
135 if (bio->bi_status && !dio->bio.bi_status)
136 dio->bio.bi_status = bio->bi_status;
138 if (atomic_dec_and_test(&dio->ref)) {
139 if (!(dio->flags & DIO_IS_SYNC)) {
140 struct kiocb *iocb = dio->iocb;
143 WRITE_ONCE(iocb->private, NULL);
145 if (likely(!dio->bio.bi_status)) {
149 ret = blk_status_to_errno(dio->bio.bi_status);
152 dio->iocb->ki_complete(iocb, ret);
155 struct task_struct *waiter = dio->waiter;
157 WRITE_ONCE(dio->waiter, NULL);
158 blk_wake_io_task(waiter);
163 bio_check_pages_dirty(bio);
165 bio_release_pages(bio, false);
170 static ssize_t __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
171 unsigned int nr_pages)
173 struct block_device *bdev = iocb->ki_filp->private_data;
174 struct blk_plug plug;
175 struct blkdev_dio *dio;
177 bool is_read = (iov_iter_rw(iter) == READ), is_sync;
178 blk_opf_t opf = is_read ? REQ_OP_READ : dio_bio_write_op(iocb);
179 loff_t pos = iocb->ki_pos;
182 if (blkdev_dio_unaligned(bdev, pos, iter))
185 if (iocb->ki_flags & IOCB_ALLOC_CACHE)
186 opf |= REQ_ALLOC_CACHE;
187 bio = bio_alloc_bioset(bdev, nr_pages, opf, GFP_KERNEL,
189 dio = container_of(bio, struct blkdev_dio, bio);
190 atomic_set(&dio->ref, 1);
192 * Grab an extra reference to ensure the dio structure which is embedded
193 * into the first bio stays around.
197 is_sync = is_sync_kiocb(iocb);
199 dio->flags = DIO_IS_SYNC;
200 dio->waiter = current;
207 if (is_read && user_backed_iter(iter))
208 dio->flags |= DIO_SHOULD_DIRTY;
210 blk_start_plug(&plug);
213 bio->bi_iter.bi_sector = pos >> SECTOR_SHIFT;
214 bio->bi_private = dio;
215 bio->bi_end_io = blkdev_bio_end_io;
216 bio->bi_ioprio = iocb->ki_ioprio;
218 ret = bio_iov_iter_get_pages(bio, iter);
220 bio->bi_status = BLK_STS_IOERR;
224 if (iocb->ki_flags & IOCB_NOWAIT) {
226 * This is nonblocking IO, and we need to allocate
227 * another bio if we have data left to map. As we
228 * cannot guarantee that one of the sub bios will not
229 * fail getting issued FOR NOWAIT and as error results
230 * are coalesced across all of them, be safe and ask for
231 * a retry of this from blocking context.
233 if (unlikely(iov_iter_count(iter))) {
234 bio_release_pages(bio, false);
235 bio_clear_flag(bio, BIO_REFFED);
237 blk_finish_plug(&plug);
240 bio->bi_opf |= REQ_NOWAIT;
244 if (dio->flags & DIO_SHOULD_DIRTY)
245 bio_set_pages_dirty(bio);
247 task_io_account_write(bio->bi_iter.bi_size);
249 dio->size += bio->bi_iter.bi_size;
250 pos += bio->bi_iter.bi_size;
252 nr_pages = bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS);
257 atomic_inc(&dio->ref);
259 bio = bio_alloc(bdev, nr_pages, opf, GFP_KERNEL);
262 blk_finish_plug(&plug);
268 set_current_state(TASK_UNINTERRUPTIBLE);
269 if (!READ_ONCE(dio->waiter))
273 __set_current_state(TASK_RUNNING);
276 ret = blk_status_to_errno(dio->bio.bi_status);
284 static void blkdev_bio_end_io_async(struct bio *bio)
286 struct blkdev_dio *dio = container_of(bio, struct blkdev_dio, bio);
287 struct kiocb *iocb = dio->iocb;
290 WRITE_ONCE(iocb->private, NULL);
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 bool is_read = iov_iter_rw(iter) == READ;
315 blk_opf_t opf = is_read ? REQ_OP_READ : dio_bio_write_op(iocb);
316 struct blkdev_dio *dio;
318 loff_t pos = iocb->ki_pos;
321 if (blkdev_dio_unaligned(bdev, pos, iter))
324 if (iocb->ki_flags & IOCB_ALLOC_CACHE)
325 opf |= REQ_ALLOC_CACHE;
326 bio = bio_alloc_bioset(bdev, nr_pages, opf, GFP_KERNEL,
328 dio = container_of(bio, struct blkdev_dio, bio);
331 bio->bi_iter.bi_sector = pos >> SECTOR_SHIFT;
332 bio->bi_end_io = blkdev_bio_end_io_async;
333 bio->bi_ioprio = iocb->ki_ioprio;
335 if (iov_iter_is_bvec(iter)) {
337 * Users don't rely on the iterator being in any particular
338 * state for async I/O returning -EIOCBQUEUED, hence we can
339 * avoid expensive iov_iter_advance(). Bypass
340 * bio_iov_iter_get_pages() and set the bvec directly.
342 bio_iov_bvec_set(bio, iter);
344 ret = bio_iov_iter_get_pages(bio, iter);
350 dio->size = bio->bi_iter.bi_size;
353 if (user_backed_iter(iter)) {
354 dio->flags |= DIO_SHOULD_DIRTY;
355 bio_set_pages_dirty(bio);
358 task_io_account_write(bio->bi_iter.bi_size);
361 if (iocb->ki_flags & IOCB_HIPRI) {
362 bio->bi_opf |= REQ_POLLED | REQ_NOWAIT;
364 WRITE_ONCE(iocb->private, bio);
366 if (iocb->ki_flags & IOCB_NOWAIT)
367 bio->bi_opf |= REQ_NOWAIT;
373 static ssize_t blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
375 unsigned int nr_pages;
377 if (!iov_iter_count(iter))
380 nr_pages = bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS + 1);
381 if (likely(nr_pages <= BIO_MAX_VECS)) {
382 if (is_sync_kiocb(iocb))
383 return __blkdev_direct_IO_simple(iocb, iter, nr_pages);
384 return __blkdev_direct_IO_async(iocb, iter, nr_pages);
386 return __blkdev_direct_IO(iocb, iter, bio_max_segs(nr_pages));
389 static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
391 return block_write_full_page(page, blkdev_get_block, wbc);
394 static int blkdev_read_folio(struct file *file, struct folio *folio)
396 return block_read_full_folio(folio, blkdev_get_block);
399 static void blkdev_readahead(struct readahead_control *rac)
401 mpage_readahead(rac, blkdev_get_block);
404 static int blkdev_write_begin(struct file *file, struct address_space *mapping,
405 loff_t pos, unsigned len, struct page **pagep, void **fsdata)
407 return block_write_begin(mapping, pos, len, pagep, blkdev_get_block);
410 static int blkdev_write_end(struct file *file, struct address_space *mapping,
411 loff_t pos, unsigned len, unsigned copied, struct page *page,
415 ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
423 static int blkdev_writepages(struct address_space *mapping,
424 struct writeback_control *wbc)
426 return generic_writepages(mapping, wbc);
429 const struct address_space_operations def_blk_aops = {
430 .dirty_folio = block_dirty_folio,
431 .invalidate_folio = block_invalidate_folio,
432 .read_folio = blkdev_read_folio,
433 .readahead = blkdev_readahead,
434 .writepage = blkdev_writepage,
435 .write_begin = blkdev_write_begin,
436 .write_end = blkdev_write_end,
437 .writepages = blkdev_writepages,
438 .direct_IO = blkdev_direct_IO,
439 .migrate_folio = buffer_migrate_folio_norefs,
440 .is_dirty_writeback = buffer_check_dirty_writeback,
444 * for a block special file file_inode(file)->i_size is zero
445 * so we compute the size by hand (just as in block_read/write above)
447 static loff_t blkdev_llseek(struct file *file, loff_t offset, int whence)
449 struct inode *bd_inode = bdev_file_inode(file);
452 inode_lock(bd_inode);
453 retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode));
454 inode_unlock(bd_inode);
458 static int blkdev_fsync(struct file *filp, loff_t start, loff_t end,
461 struct block_device *bdev = filp->private_data;
464 error = file_write_and_wait_range(filp, start, end);
469 * There is no need to serialise calls to blkdev_issue_flush with
470 * i_mutex and doing so causes performance issues with concurrent
471 * O_SYNC writers to a block device.
473 error = blkdev_issue_flush(bdev);
474 if (error == -EOPNOTSUPP)
480 static int blkdev_open(struct inode *inode, struct file *filp)
482 struct block_device *bdev;
485 * Preserve backwards compatibility and allow large file access
486 * even if userspace doesn't ask for it explicitly. Some mkfs
487 * binary needs it. We might want to drop this workaround
488 * during an unstable branch.
490 filp->f_flags |= O_LARGEFILE;
491 filp->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC;
493 if (filp->f_flags & O_NDELAY)
494 filp->f_mode |= FMODE_NDELAY;
495 if (filp->f_flags & O_EXCL)
496 filp->f_mode |= FMODE_EXCL;
497 if ((filp->f_flags & O_ACCMODE) == 3)
498 filp->f_mode |= FMODE_WRITE_IOCTL;
500 bdev = blkdev_get_by_dev(inode->i_rdev, filp->f_mode, filp);
502 return PTR_ERR(bdev);
504 filp->private_data = bdev;
505 filp->f_mapping = bdev->bd_inode->i_mapping;
506 filp->f_wb_err = filemap_sample_wb_err(filp->f_mapping);
510 static int blkdev_close(struct inode *inode, struct file *filp)
512 struct block_device *bdev = filp->private_data;
514 blkdev_put(bdev, filp->f_mode);
519 * Write data to the block device. Only intended for the block device itself
520 * and the raw driver which basically is a fake block device.
522 * Does not take i_mutex for the write and thus is not for general purpose
525 static ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
527 struct block_device *bdev = iocb->ki_filp->private_data;
528 struct inode *bd_inode = bdev->bd_inode;
529 loff_t size = bdev_nr_bytes(bdev);
530 struct blk_plug plug;
534 if (bdev_read_only(bdev))
537 if (IS_SWAPFILE(bd_inode) && !is_hibernate_resume_dev(bd_inode->i_rdev))
540 if (!iov_iter_count(from))
543 if (iocb->ki_pos >= size)
546 if ((iocb->ki_flags & (IOCB_NOWAIT | IOCB_DIRECT)) == IOCB_NOWAIT)
549 size -= iocb->ki_pos;
550 if (iov_iter_count(from) > size) {
551 shorted = iov_iter_count(from) - size;
552 iov_iter_truncate(from, size);
555 blk_start_plug(&plug);
556 ret = __generic_file_write_iter(iocb, from);
558 ret = generic_write_sync(iocb, ret);
559 iov_iter_reexpand(from, iov_iter_count(from) + shorted);
560 blk_finish_plug(&plug);
564 static ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
566 struct block_device *bdev = iocb->ki_filp->private_data;
567 loff_t size = bdev_nr_bytes(bdev);
568 loff_t pos = iocb->ki_pos;
573 if (unlikely(pos + iov_iter_count(to) > size)) {
577 shorted = iov_iter_count(to) - size;
578 iov_iter_truncate(to, size);
581 count = iov_iter_count(to);
583 goto reexpand; /* skip atime */
585 if (iocb->ki_flags & IOCB_DIRECT) {
586 struct address_space *mapping = iocb->ki_filp->f_mapping;
588 if (iocb->ki_flags & IOCB_NOWAIT) {
589 if (filemap_range_needs_writeback(mapping, pos,
595 ret = filemap_write_and_wait_range(mapping, pos,
601 file_accessed(iocb->ki_filp);
603 ret = blkdev_direct_IO(iocb, to);
608 iov_iter_revert(to, count - iov_iter_count(to));
609 if (ret < 0 || !count)
613 ret = filemap_read(iocb, to, ret);
616 if (unlikely(shorted))
617 iov_iter_reexpand(to, iov_iter_count(to) + shorted);
621 #define BLKDEV_FALLOC_FL_SUPPORTED \
622 (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
623 FALLOC_FL_ZERO_RANGE | FALLOC_FL_NO_HIDE_STALE)
625 static long blkdev_fallocate(struct file *file, int mode, loff_t start,
628 struct inode *inode = bdev_file_inode(file);
629 struct block_device *bdev = I_BDEV(inode);
630 loff_t end = start + len - 1;
634 /* Fail if we don't recognize the flags. */
635 if (mode & ~BLKDEV_FALLOC_FL_SUPPORTED)
638 /* Don't go off the end of the device. */
639 isize = bdev_nr_bytes(bdev);
643 if (mode & FALLOC_FL_KEEP_SIZE) {
645 end = start + len - 1;
651 * Don't allow IO that isn't aligned to logical block size.
653 if ((start | len) & (bdev_logical_block_size(bdev) - 1))
656 filemap_invalidate_lock(inode->i_mapping);
658 /* Invalidate the page cache, including dirty pages. */
659 error = truncate_bdev_range(bdev, file->f_mode, start, end);
664 case FALLOC_FL_ZERO_RANGE:
665 case FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE:
666 error = blkdev_issue_zeroout(bdev, start >> SECTOR_SHIFT,
667 len >> SECTOR_SHIFT, GFP_KERNEL,
668 BLKDEV_ZERO_NOUNMAP);
670 case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE:
671 error = blkdev_issue_zeroout(bdev, start >> SECTOR_SHIFT,
672 len >> SECTOR_SHIFT, GFP_KERNEL,
673 BLKDEV_ZERO_NOFALLBACK);
675 case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE | FALLOC_FL_NO_HIDE_STALE:
676 error = blkdev_issue_discard(bdev, start >> SECTOR_SHIFT,
677 len >> SECTOR_SHIFT, GFP_KERNEL);
684 filemap_invalidate_unlock(inode->i_mapping);
688 static int blkdev_mmap(struct file *file, struct vm_area_struct *vma)
690 struct inode *bd_inode = bdev_file_inode(file);
692 if (bdev_read_only(I_BDEV(bd_inode)))
693 return generic_file_readonly_mmap(file, vma);
695 return generic_file_mmap(file, vma);
698 const struct file_operations def_blk_fops = {
700 .release = blkdev_close,
701 .llseek = blkdev_llseek,
702 .read_iter = blkdev_read_iter,
703 .write_iter = blkdev_write_iter,
704 .iopoll = iocb_bio_iopoll,
706 .fsync = blkdev_fsync,
707 .unlocked_ioctl = blkdev_ioctl,
709 .compat_ioctl = compat_blkdev_ioctl,
711 .splice_read = generic_file_splice_read,
712 .splice_write = iter_file_splice_write,
713 .fallocate = blkdev_fallocate,
716 static __init int blkdev_init(void)
718 return bioset_init(&blkdev_dio_pool, 4,
719 offsetof(struct blkdev_dio, bio),
720 BIOSET_NEED_BVECS|BIOSET_PERCPU_CACHE);
722 module_init(blkdev_init);