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
8 #include <linux/init.h>
10 #include <linux/fcntl.h>
11 #include <linux/slab.h>
12 #include <linux/kmod.h>
13 #include <linux/major.h>
14 #include <linux/device_cgroup.h>
15 #include <linux/highmem.h>
16 #include <linux/blkdev.h>
17 #include <linux/backing-dev.h>
18 #include <linux/module.h>
19 #include <linux/blkpg.h>
20 #include <linux/magic.h>
21 #include <linux/buffer_head.h>
22 #include <linux/swap.h>
23 #include <linux/pagevec.h>
24 #include <linux/writeback.h>
25 #include <linux/mpage.h>
26 #include <linux/mount.h>
27 #include <linux/pseudo_fs.h>
28 #include <linux/uio.h>
29 #include <linux/namei.h>
30 #include <linux/log2.h>
31 #include <linux/cleancache.h>
32 #include <linux/task_io_accounting_ops.h>
33 #include <linux/falloc.h>
34 #include <linux/part_stat.h>
35 #include <linux/uaccess.h>
36 #include <linux/suspend.h>
40 struct block_device bdev;
41 struct inode vfs_inode;
44 static const struct address_space_operations def_blk_aops;
46 static inline struct bdev_inode *BDEV_I(struct inode *inode)
48 return container_of(inode, struct bdev_inode, vfs_inode);
51 struct block_device *I_BDEV(struct inode *inode)
53 return &BDEV_I(inode)->bdev;
55 EXPORT_SYMBOL(I_BDEV);
57 static void bdev_write_inode(struct block_device *bdev)
59 struct inode *inode = bdev->bd_inode;
62 spin_lock(&inode->i_lock);
63 while (inode->i_state & I_DIRTY) {
64 spin_unlock(&inode->i_lock);
65 ret = write_inode_now(inode, true);
67 char name[BDEVNAME_SIZE];
68 pr_warn_ratelimited("VFS: Dirty inode writeback failed "
69 "for block device %s (err=%d).\n",
70 bdevname(bdev, name), ret);
72 spin_lock(&inode->i_lock);
74 spin_unlock(&inode->i_lock);
77 /* Kill _all_ buffers and pagecache , dirty or not.. */
78 static void kill_bdev(struct block_device *bdev)
80 struct address_space *mapping = bdev->bd_inode->i_mapping;
82 if (mapping_empty(mapping))
86 truncate_inode_pages(mapping, 0);
89 /* Invalidate clean unused buffers and pagecache. */
90 void invalidate_bdev(struct block_device *bdev)
92 struct address_space *mapping = bdev->bd_inode->i_mapping;
94 if (mapping->nrpages) {
96 lru_add_drain_all(); /* make sure all lru add caches are flushed */
97 invalidate_mapping_pages(mapping, 0, -1);
99 /* 99% of the time, we don't need to flush the cleancache on the bdev.
100 * But, for the strange corners, lets be cautious
102 cleancache_invalidate_inode(mapping);
104 EXPORT_SYMBOL(invalidate_bdev);
107 * Drop all buffers & page cache for given bdev range. This function bails
108 * with error if bdev has other exclusive owner (such as filesystem).
110 int truncate_bdev_range(struct block_device *bdev, fmode_t mode,
111 loff_t lstart, loff_t lend)
114 * If we don't hold exclusive handle for the device, upgrade to it
115 * while we discard the buffer cache to avoid discarding buffers
116 * under live filesystem.
118 if (!(mode & FMODE_EXCL)) {
119 int err = bd_prepare_to_claim(bdev, truncate_bdev_range);
124 truncate_inode_pages_range(bdev->bd_inode->i_mapping, lstart, lend);
125 if (!(mode & FMODE_EXCL))
126 bd_abort_claiming(bdev, truncate_bdev_range);
131 * Someone else has handle exclusively open. Try invalidating instead.
132 * The 'end' argument is inclusive so the rounding is safe.
134 return invalidate_inode_pages2_range(bdev->bd_inode->i_mapping,
135 lstart >> PAGE_SHIFT,
139 static void set_init_blocksize(struct block_device *bdev)
141 unsigned int bsize = bdev_logical_block_size(bdev);
142 loff_t size = i_size_read(bdev->bd_inode);
144 while (bsize < PAGE_SIZE) {
149 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
152 int set_blocksize(struct block_device *bdev, int size)
154 /* Size must be a power of two, and between 512 and PAGE_SIZE */
155 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
158 /* Size cannot be smaller than the size supported by the device */
159 if (size < bdev_logical_block_size(bdev))
162 /* Don't change the size if it is same as current */
163 if (bdev->bd_inode->i_blkbits != blksize_bits(size)) {
165 bdev->bd_inode->i_blkbits = blksize_bits(size);
171 EXPORT_SYMBOL(set_blocksize);
173 int sb_set_blocksize(struct super_block *sb, int size)
175 if (set_blocksize(sb->s_bdev, size))
177 /* If we get here, we know size is power of two
178 * and it's value is between 512 and PAGE_SIZE */
179 sb->s_blocksize = size;
180 sb->s_blocksize_bits = blksize_bits(size);
181 return sb->s_blocksize;
184 EXPORT_SYMBOL(sb_set_blocksize);
186 int sb_min_blocksize(struct super_block *sb, int size)
188 int minsize = bdev_logical_block_size(sb->s_bdev);
191 return sb_set_blocksize(sb, size);
194 EXPORT_SYMBOL(sb_min_blocksize);
197 blkdev_get_block(struct inode *inode, sector_t iblock,
198 struct buffer_head *bh, int create)
200 bh->b_bdev = I_BDEV(inode);
201 bh->b_blocknr = iblock;
202 set_buffer_mapped(bh);
206 static struct inode *bdev_file_inode(struct file *file)
208 return file->f_mapping->host;
211 static unsigned int dio_bio_write_op(struct kiocb *iocb)
213 unsigned int op = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
215 /* avoid the need for a I/O completion work item */
216 if (iocb->ki_flags & IOCB_DSYNC)
221 #define DIO_INLINE_BIO_VECS 4
223 static void blkdev_bio_end_io_simple(struct bio *bio)
225 struct task_struct *waiter = bio->bi_private;
227 WRITE_ONCE(bio->bi_private, NULL);
228 blk_wake_io_task(waiter);
232 __blkdev_direct_IO_simple(struct kiocb *iocb, struct iov_iter *iter,
233 unsigned int nr_pages)
235 struct file *file = iocb->ki_filp;
236 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
237 struct bio_vec inline_vecs[DIO_INLINE_BIO_VECS], *vecs;
238 loff_t pos = iocb->ki_pos;
239 bool should_dirty = false;
244 if ((pos | iov_iter_alignment(iter)) &
245 (bdev_logical_block_size(bdev) - 1))
248 if (nr_pages <= DIO_INLINE_BIO_VECS)
251 vecs = kmalloc_array(nr_pages, sizeof(struct bio_vec),
257 bio_init(&bio, vecs, nr_pages);
258 bio_set_dev(&bio, bdev);
259 bio.bi_iter.bi_sector = pos >> 9;
260 bio.bi_write_hint = iocb->ki_hint;
261 bio.bi_private = current;
262 bio.bi_end_io = blkdev_bio_end_io_simple;
263 bio.bi_ioprio = iocb->ki_ioprio;
265 ret = bio_iov_iter_get_pages(&bio, iter);
268 ret = bio.bi_iter.bi_size;
270 if (iov_iter_rw(iter) == READ) {
271 bio.bi_opf = REQ_OP_READ;
272 if (iter_is_iovec(iter))
275 bio.bi_opf = dio_bio_write_op(iocb);
276 task_io_account_write(ret);
278 if (iocb->ki_flags & IOCB_NOWAIT)
279 bio.bi_opf |= REQ_NOWAIT;
280 if (iocb->ki_flags & IOCB_HIPRI)
281 bio_set_polled(&bio, iocb);
283 qc = submit_bio(&bio);
285 set_current_state(TASK_UNINTERRUPTIBLE);
286 if (!READ_ONCE(bio.bi_private))
288 if (!(iocb->ki_flags & IOCB_HIPRI) ||
289 !blk_poll(bdev_get_queue(bdev), qc, true))
292 __set_current_state(TASK_RUNNING);
294 bio_release_pages(&bio, should_dirty);
295 if (unlikely(bio.bi_status))
296 ret = blk_status_to_errno(bio.bi_status);
299 if (vecs != inline_vecs)
310 struct task_struct *waiter;
315 bool should_dirty : 1;
320 static struct bio_set blkdev_dio_pool;
322 static int blkdev_iopoll(struct kiocb *kiocb, bool wait)
324 struct block_device *bdev = I_BDEV(kiocb->ki_filp->f_mapping->host);
325 struct request_queue *q = bdev_get_queue(bdev);
327 return blk_poll(q, READ_ONCE(kiocb->ki_cookie), wait);
330 static void blkdev_bio_end_io(struct bio *bio)
332 struct blkdev_dio *dio = bio->bi_private;
333 bool should_dirty = dio->should_dirty;
335 if (bio->bi_status && !dio->bio.bi_status)
336 dio->bio.bi_status = bio->bi_status;
338 if (!dio->multi_bio || atomic_dec_and_test(&dio->ref)) {
340 struct kiocb *iocb = dio->iocb;
343 if (likely(!dio->bio.bi_status)) {
347 ret = blk_status_to_errno(dio->bio.bi_status);
350 dio->iocb->ki_complete(iocb, ret, 0);
354 struct task_struct *waiter = dio->waiter;
356 WRITE_ONCE(dio->waiter, NULL);
357 blk_wake_io_task(waiter);
362 bio_check_pages_dirty(bio);
364 bio_release_pages(bio, false);
369 static ssize_t __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
370 unsigned int nr_pages)
372 struct file *file = iocb->ki_filp;
373 struct inode *inode = bdev_file_inode(file);
374 struct block_device *bdev = I_BDEV(inode);
375 struct blk_plug plug;
376 struct blkdev_dio *dio;
378 bool is_poll = (iocb->ki_flags & IOCB_HIPRI) != 0;
379 bool is_read = (iov_iter_rw(iter) == READ), is_sync;
380 loff_t pos = iocb->ki_pos;
381 blk_qc_t qc = BLK_QC_T_NONE;
384 if ((pos | iov_iter_alignment(iter)) &
385 (bdev_logical_block_size(bdev) - 1))
388 bio = bio_alloc_kiocb(iocb, nr_pages, &blkdev_dio_pool);
390 dio = container_of(bio, struct blkdev_dio, bio);
391 dio->is_sync = is_sync = is_sync_kiocb(iocb);
393 dio->waiter = current;
400 dio->multi_bio = false;
401 dio->should_dirty = is_read && iter_is_iovec(iter);
404 * Don't plug for HIPRI/polled IO, as those should go straight
408 blk_start_plug(&plug);
411 bio_set_dev(bio, bdev);
412 bio->bi_iter.bi_sector = pos >> 9;
413 bio->bi_write_hint = iocb->ki_hint;
414 bio->bi_private = dio;
415 bio->bi_end_io = blkdev_bio_end_io;
416 bio->bi_ioprio = iocb->ki_ioprio;
418 ret = bio_iov_iter_get_pages(bio, iter);
420 bio->bi_status = BLK_STS_IOERR;
426 bio->bi_opf = REQ_OP_READ;
427 if (dio->should_dirty)
428 bio_set_pages_dirty(bio);
430 bio->bi_opf = dio_bio_write_op(iocb);
431 task_io_account_write(bio->bi_iter.bi_size);
433 if (iocb->ki_flags & IOCB_NOWAIT)
434 bio->bi_opf |= REQ_NOWAIT;
436 dio->size += bio->bi_iter.bi_size;
437 pos += bio->bi_iter.bi_size;
439 nr_pages = bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS);
443 if (iocb->ki_flags & IOCB_HIPRI) {
444 bio_set_polled(bio, iocb);
448 qc = submit_bio(bio);
451 WRITE_ONCE(iocb->ki_cookie, qc);
455 if (!dio->multi_bio) {
457 * AIO needs an extra reference to ensure the dio
458 * structure which is embedded into the first bio
463 dio->multi_bio = true;
464 atomic_set(&dio->ref, 2);
466 atomic_inc(&dio->ref);
470 bio = bio_alloc(GFP_KERNEL, nr_pages);
474 blk_finish_plug(&plug);
480 set_current_state(TASK_UNINTERRUPTIBLE);
481 if (!READ_ONCE(dio->waiter))
484 if (!(iocb->ki_flags & IOCB_HIPRI) ||
485 !blk_poll(bdev_get_queue(bdev), qc, true))
488 __set_current_state(TASK_RUNNING);
491 ret = blk_status_to_errno(dio->bio.bi_status);
500 blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
502 unsigned int nr_pages;
504 if (!iov_iter_count(iter))
507 nr_pages = bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS + 1);
508 if (is_sync_kiocb(iocb) && nr_pages <= BIO_MAX_VECS)
509 return __blkdev_direct_IO_simple(iocb, iter, nr_pages);
511 return __blkdev_direct_IO(iocb, iter, bio_max_segs(nr_pages));
514 static __init int blkdev_init(void)
516 return bioset_init(&blkdev_dio_pool, 4,
517 offsetof(struct blkdev_dio, bio),
518 BIOSET_NEED_BVECS|BIOSET_PERCPU_CACHE);
520 module_init(blkdev_init);
522 int __sync_blockdev(struct block_device *bdev, int wait)
527 return filemap_flush(bdev->bd_inode->i_mapping);
528 return filemap_write_and_wait(bdev->bd_inode->i_mapping);
532 * Write out and wait upon all the dirty data associated with a block
533 * device via its mapping. Does not take the superblock lock.
535 int sync_blockdev(struct block_device *bdev)
537 return __sync_blockdev(bdev, 1);
539 EXPORT_SYMBOL(sync_blockdev);
542 * Write out and wait upon all dirty data associated with this
543 * device. Filesystem data as well as the underlying block
544 * device. Takes the superblock lock.
546 int fsync_bdev(struct block_device *bdev)
548 struct super_block *sb = get_super(bdev);
550 int res = sync_filesystem(sb);
554 return sync_blockdev(bdev);
556 EXPORT_SYMBOL(fsync_bdev);
559 * freeze_bdev -- lock a filesystem and force it into a consistent state
560 * @bdev: blockdevice to lock
562 * If a superblock is found on this device, we take the s_umount semaphore
563 * on it to make sure nobody unmounts until the snapshot creation is done.
564 * The reference counter (bd_fsfreeze_count) guarantees that only the last
565 * unfreeze process can unfreeze the frozen filesystem actually when multiple
566 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
567 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
570 int freeze_bdev(struct block_device *bdev)
572 struct super_block *sb;
575 mutex_lock(&bdev->bd_fsfreeze_mutex);
576 if (++bdev->bd_fsfreeze_count > 1)
579 sb = get_active_super(bdev);
582 if (sb->s_op->freeze_super)
583 error = sb->s_op->freeze_super(sb);
585 error = freeze_super(sb);
586 deactivate_super(sb);
589 bdev->bd_fsfreeze_count--;
592 bdev->bd_fsfreeze_sb = sb;
597 mutex_unlock(&bdev->bd_fsfreeze_mutex);
600 EXPORT_SYMBOL(freeze_bdev);
603 * thaw_bdev -- unlock filesystem
604 * @bdev: blockdevice to unlock
606 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
608 int thaw_bdev(struct block_device *bdev)
610 struct super_block *sb;
613 mutex_lock(&bdev->bd_fsfreeze_mutex);
614 if (!bdev->bd_fsfreeze_count)
618 if (--bdev->bd_fsfreeze_count > 0)
621 sb = bdev->bd_fsfreeze_sb;
625 if (sb->s_op->thaw_super)
626 error = sb->s_op->thaw_super(sb);
628 error = thaw_super(sb);
630 bdev->bd_fsfreeze_count++;
632 bdev->bd_fsfreeze_sb = NULL;
634 mutex_unlock(&bdev->bd_fsfreeze_mutex);
637 EXPORT_SYMBOL(thaw_bdev);
639 static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
641 return block_write_full_page(page, blkdev_get_block, wbc);
644 static int blkdev_readpage(struct file * file, struct page * page)
646 return block_read_full_page(page, blkdev_get_block);
649 static void blkdev_readahead(struct readahead_control *rac)
651 mpage_readahead(rac, blkdev_get_block);
654 static int blkdev_write_begin(struct file *file, struct address_space *mapping,
655 loff_t pos, unsigned len, unsigned flags,
656 struct page **pagep, void **fsdata)
658 return block_write_begin(mapping, pos, len, flags, pagep,
662 static int blkdev_write_end(struct file *file, struct address_space *mapping,
663 loff_t pos, unsigned len, unsigned copied,
664 struct page *page, void *fsdata)
667 ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
677 * for a block special file file_inode(file)->i_size is zero
678 * so we compute the size by hand (just as in block_read/write above)
680 static loff_t block_llseek(struct file *file, loff_t offset, int whence)
682 struct inode *bd_inode = bdev_file_inode(file);
685 inode_lock(bd_inode);
686 retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode));
687 inode_unlock(bd_inode);
691 int blkdev_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
693 struct inode *bd_inode = bdev_file_inode(filp);
694 struct block_device *bdev = I_BDEV(bd_inode);
697 error = file_write_and_wait_range(filp, start, end);
702 * There is no need to serialise calls to blkdev_issue_flush with
703 * i_mutex and doing so causes performance issues with concurrent
704 * O_SYNC writers to a block device.
706 error = blkdev_issue_flush(bdev);
707 if (error == -EOPNOTSUPP)
712 EXPORT_SYMBOL(blkdev_fsync);
715 * bdev_read_page() - Start reading a page from a block device
716 * @bdev: The device to read the page from
717 * @sector: The offset on the device to read the page to (need not be aligned)
718 * @page: The page to read
720 * On entry, the page should be locked. It will be unlocked when the page
721 * has been read. If the block driver implements rw_page synchronously,
722 * that will be true on exit from this function, but it need not be.
724 * Errors returned by this function are usually "soft", eg out of memory, or
725 * queue full; callers should try a different route to read this page rather
726 * than propagate an error back up the stack.
728 * Return: negative errno if an error occurs, 0 if submission was successful.
730 int bdev_read_page(struct block_device *bdev, sector_t sector,
733 const struct block_device_operations *ops = bdev->bd_disk->fops;
734 int result = -EOPNOTSUPP;
736 if (!ops->rw_page || bdev_get_integrity(bdev))
739 result = blk_queue_enter(bdev->bd_disk->queue, 0);
742 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
744 blk_queue_exit(bdev->bd_disk->queue);
749 * bdev_write_page() - Start writing a page to a block device
750 * @bdev: The device to write the page to
751 * @sector: The offset on the device to write the page to (need not be aligned)
752 * @page: The page to write
753 * @wbc: The writeback_control for the write
755 * On entry, the page should be locked and not currently under writeback.
756 * On exit, if the write started successfully, the page will be unlocked and
757 * under writeback. If the write failed already (eg the driver failed to
758 * queue the page to the device), the page will still be locked. If the
759 * caller is a ->writepage implementation, it will need to unlock the page.
761 * Errors returned by this function are usually "soft", eg out of memory, or
762 * queue full; callers should try a different route to write this page rather
763 * than propagate an error back up the stack.
765 * Return: negative errno if an error occurs, 0 if submission was successful.
767 int bdev_write_page(struct block_device *bdev, sector_t sector,
768 struct page *page, struct writeback_control *wbc)
771 const struct block_device_operations *ops = bdev->bd_disk->fops;
773 if (!ops->rw_page || bdev_get_integrity(bdev))
775 result = blk_queue_enter(bdev->bd_disk->queue, 0);
779 set_page_writeback(page);
780 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
783 end_page_writeback(page);
785 clean_page_buffers(page);
788 blk_queue_exit(bdev->bd_disk->queue);
796 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
797 static struct kmem_cache * bdev_cachep __read_mostly;
799 static struct inode *bdev_alloc_inode(struct super_block *sb)
801 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
805 memset(&ei->bdev, 0, sizeof(ei->bdev));
806 ei->bdev.bd_bdi = &noop_backing_dev_info;
807 return &ei->vfs_inode;
810 static void bdev_free_inode(struct inode *inode)
812 struct block_device *bdev = I_BDEV(inode);
814 free_percpu(bdev->bd_stats);
815 kfree(bdev->bd_meta_info);
817 if (!bdev_is_partition(bdev))
818 kfree(bdev->bd_disk);
819 kmem_cache_free(bdev_cachep, BDEV_I(inode));
822 static void init_once(void *data)
824 struct bdev_inode *ei = data;
826 inode_init_once(&ei->vfs_inode);
829 static void bdev_evict_inode(struct inode *inode)
831 struct block_device *bdev = &BDEV_I(inode)->bdev;
832 truncate_inode_pages_final(&inode->i_data);
833 invalidate_inode_buffers(inode); /* is it needed here? */
835 /* Detach inode from wb early as bdi_put() may free bdi->wb */
836 inode_detach_wb(inode);
837 if (bdev->bd_bdi != &noop_backing_dev_info) {
838 bdi_put(bdev->bd_bdi);
839 bdev->bd_bdi = &noop_backing_dev_info;
843 static const struct super_operations bdev_sops = {
844 .statfs = simple_statfs,
845 .alloc_inode = bdev_alloc_inode,
846 .free_inode = bdev_free_inode,
847 .drop_inode = generic_delete_inode,
848 .evict_inode = bdev_evict_inode,
851 static int bd_init_fs_context(struct fs_context *fc)
853 struct pseudo_fs_context *ctx = init_pseudo(fc, BDEVFS_MAGIC);
856 fc->s_iflags |= SB_I_CGROUPWB;
857 ctx->ops = &bdev_sops;
861 static struct file_system_type bd_type = {
863 .init_fs_context = bd_init_fs_context,
864 .kill_sb = kill_anon_super,
867 struct super_block *blockdev_superblock __read_mostly;
868 EXPORT_SYMBOL_GPL(blockdev_superblock);
870 void __init bdev_cache_init(void)
873 static struct vfsmount *bd_mnt;
875 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
876 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
877 SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC),
879 err = register_filesystem(&bd_type);
881 panic("Cannot register bdev pseudo-fs");
882 bd_mnt = kern_mount(&bd_type);
884 panic("Cannot create bdev pseudo-fs");
885 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
888 struct block_device *bdev_alloc(struct gendisk *disk, u8 partno)
890 struct block_device *bdev;
893 inode = new_inode(blockdev_superblock);
896 inode->i_mode = S_IFBLK;
898 inode->i_data.a_ops = &def_blk_aops;
899 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
901 bdev = I_BDEV(inode);
902 mutex_init(&bdev->bd_fsfreeze_mutex);
903 spin_lock_init(&bdev->bd_size_lock);
904 bdev->bd_disk = disk;
905 bdev->bd_partno = partno;
906 bdev->bd_inode = inode;
908 INIT_LIST_HEAD(&bdev->bd_holder_disks);
910 bdev->bd_stats = alloc_percpu(struct disk_stats);
911 if (!bdev->bd_stats) {
918 void bdev_add(struct block_device *bdev, dev_t dev)
921 bdev->bd_inode->i_rdev = dev;
922 bdev->bd_inode->i_ino = dev;
923 insert_inode_hash(bdev->bd_inode);
926 static struct block_device *bdget(dev_t dev)
930 inode = ilookup(blockdev_superblock, dev);
933 return &BDEV_I(inode)->bdev;
937 * bdgrab -- Grab a reference to an already referenced block device
938 * @bdev: Block device to grab a reference to.
940 * Returns the block_device with an additional reference when successful,
941 * or NULL if the inode is already beeing freed.
943 struct block_device *bdgrab(struct block_device *bdev)
945 if (!igrab(bdev->bd_inode))
949 EXPORT_SYMBOL(bdgrab);
951 long nr_blockdev_pages(void)
956 spin_lock(&blockdev_superblock->s_inode_list_lock);
957 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list)
958 ret += inode->i_mapping->nrpages;
959 spin_unlock(&blockdev_superblock->s_inode_list_lock);
964 void bdput(struct block_device *bdev)
966 iput(bdev->bd_inode);
968 EXPORT_SYMBOL(bdput);
971 * bd_may_claim - test whether a block device can be claimed
972 * @bdev: block device of interest
973 * @whole: whole block device containing @bdev, may equal @bdev
974 * @holder: holder trying to claim @bdev
976 * Test whether @bdev can be claimed by @holder.
979 * spin_lock(&bdev_lock).
982 * %true if @bdev can be claimed, %false otherwise.
984 static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
987 if (bdev->bd_holder == holder)
988 return true; /* already a holder */
989 else if (bdev->bd_holder != NULL)
990 return false; /* held by someone else */
991 else if (whole == bdev)
992 return true; /* is a whole device which isn't held */
994 else if (whole->bd_holder == bd_may_claim)
995 return true; /* is a partition of a device that is being partitioned */
996 else if (whole->bd_holder != NULL)
997 return false; /* is a partition of a held device */
999 return true; /* is a partition of an un-held device */
1003 * bd_prepare_to_claim - claim a block device
1004 * @bdev: block device of interest
1005 * @holder: holder trying to claim @bdev
1007 * Claim @bdev. This function fails if @bdev is already claimed by another
1008 * holder and waits if another claiming is in progress. return, the caller
1009 * has ownership of bd_claiming and bd_holder[s].
1012 * 0 if @bdev can be claimed, -EBUSY otherwise.
1014 int bd_prepare_to_claim(struct block_device *bdev, void *holder)
1016 struct block_device *whole = bdev_whole(bdev);
1018 if (WARN_ON_ONCE(!holder))
1021 spin_lock(&bdev_lock);
1022 /* if someone else claimed, fail */
1023 if (!bd_may_claim(bdev, whole, holder)) {
1024 spin_unlock(&bdev_lock);
1028 /* if claiming is already in progress, wait for it to finish */
1029 if (whole->bd_claiming) {
1030 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
1033 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
1034 spin_unlock(&bdev_lock);
1036 finish_wait(wq, &wait);
1041 whole->bd_claiming = holder;
1042 spin_unlock(&bdev_lock);
1045 EXPORT_SYMBOL_GPL(bd_prepare_to_claim); /* only for the loop driver */
1047 static void bd_clear_claiming(struct block_device *whole, void *holder)
1049 lockdep_assert_held(&bdev_lock);
1050 /* tell others that we're done */
1051 BUG_ON(whole->bd_claiming != holder);
1052 whole->bd_claiming = NULL;
1053 wake_up_bit(&whole->bd_claiming, 0);
1057 * bd_finish_claiming - finish claiming of a block device
1058 * @bdev: block device of interest
1059 * @holder: holder that has claimed @bdev
1061 * Finish exclusive open of a block device. Mark the device as exlusively
1062 * open by the holder and wake up all waiters for exclusive open to finish.
1064 static void bd_finish_claiming(struct block_device *bdev, void *holder)
1066 struct block_device *whole = bdev_whole(bdev);
1068 spin_lock(&bdev_lock);
1069 BUG_ON(!bd_may_claim(bdev, whole, holder));
1071 * Note that for a whole device bd_holders will be incremented twice,
1072 * and bd_holder will be set to bd_may_claim before being set to holder
1074 whole->bd_holders++;
1075 whole->bd_holder = bd_may_claim;
1077 bdev->bd_holder = holder;
1078 bd_clear_claiming(whole, holder);
1079 spin_unlock(&bdev_lock);
1083 * bd_abort_claiming - abort claiming of a block device
1084 * @bdev: block device of interest
1085 * @holder: holder that has claimed @bdev
1087 * Abort claiming of a block device when the exclusive open failed. This can be
1088 * also used when exclusive open is not actually desired and we just needed
1089 * to block other exclusive openers for a while.
1091 void bd_abort_claiming(struct block_device *bdev, void *holder)
1093 spin_lock(&bdev_lock);
1094 bd_clear_claiming(bdev_whole(bdev), holder);
1095 spin_unlock(&bdev_lock);
1097 EXPORT_SYMBOL(bd_abort_claiming);
1100 struct bd_holder_disk {
1101 struct list_head list;
1102 struct gendisk *disk;
1106 static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev,
1107 struct gendisk *disk)
1109 struct bd_holder_disk *holder;
1111 list_for_each_entry(holder, &bdev->bd_holder_disks, list)
1112 if (holder->disk == disk)
1117 static int add_symlink(struct kobject *from, struct kobject *to)
1119 return sysfs_create_link(from, to, kobject_name(to));
1122 static void del_symlink(struct kobject *from, struct kobject *to)
1124 sysfs_remove_link(from, kobject_name(to));
1128 * bd_link_disk_holder - create symlinks between holding disk and slave bdev
1129 * @bdev: the claimed slave bdev
1130 * @disk: the holding disk
1132 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
1134 * This functions creates the following sysfs symlinks.
1136 * - from "slaves" directory of the holder @disk to the claimed @bdev
1137 * - from "holders" directory of the @bdev to the holder @disk
1139 * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
1140 * passed to bd_link_disk_holder(), then:
1142 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
1143 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
1145 * The caller must have claimed @bdev before calling this function and
1146 * ensure that both @bdev and @disk are valid during the creation and
1147 * lifetime of these symlinks.
1153 * 0 on success, -errno on failure.
1155 int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
1157 struct bd_holder_disk *holder;
1160 mutex_lock(&bdev->bd_disk->open_mutex);
1162 WARN_ON_ONCE(!bdev->bd_holder);
1164 /* FIXME: remove the following once add_disk() handles errors */
1165 if (WARN_ON(!disk->slave_dir || !bdev->bd_holder_dir))
1168 holder = bd_find_holder_disk(bdev, disk);
1174 holder = kzalloc(sizeof(*holder), GFP_KERNEL);
1180 INIT_LIST_HEAD(&holder->list);
1181 holder->disk = disk;
1184 ret = add_symlink(disk->slave_dir, bdev_kobj(bdev));
1188 ret = add_symlink(bdev->bd_holder_dir, &disk_to_dev(disk)->kobj);
1192 * bdev could be deleted beneath us which would implicitly destroy
1193 * the holder directory. Hold on to it.
1195 kobject_get(bdev->bd_holder_dir);
1197 list_add(&holder->list, &bdev->bd_holder_disks);
1201 del_symlink(disk->slave_dir, bdev_kobj(bdev));
1205 mutex_unlock(&bdev->bd_disk->open_mutex);
1208 EXPORT_SYMBOL_GPL(bd_link_disk_holder);
1211 * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
1212 * @bdev: the calimed slave bdev
1213 * @disk: the holding disk
1215 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
1220 void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
1222 struct bd_holder_disk *holder;
1224 mutex_lock(&bdev->bd_disk->open_mutex);
1226 holder = bd_find_holder_disk(bdev, disk);
1228 if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
1229 del_symlink(disk->slave_dir, bdev_kobj(bdev));
1230 del_symlink(bdev->bd_holder_dir, &disk_to_dev(disk)->kobj);
1231 kobject_put(bdev->bd_holder_dir);
1232 list_del_init(&holder->list);
1236 mutex_unlock(&bdev->bd_disk->open_mutex);
1238 EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
1241 static void blkdev_flush_mapping(struct block_device *bdev)
1243 WARN_ON_ONCE(bdev->bd_holders);
1244 sync_blockdev(bdev);
1246 bdev_write_inode(bdev);
1249 static int blkdev_get_whole(struct block_device *bdev, fmode_t mode)
1251 struct gendisk *disk = bdev->bd_disk;
1254 if (disk->fops->open) {
1255 ret = disk->fops->open(bdev, mode);
1257 /* avoid ghost partitions on a removed medium */
1258 if (ret == -ENOMEDIUM &&
1259 test_bit(GD_NEED_PART_SCAN, &disk->state))
1260 bdev_disk_changed(disk, true);
1265 if (!bdev->bd_openers) {
1266 set_init_blocksize(bdev);
1267 if (bdev->bd_bdi == &noop_backing_dev_info)
1268 bdev->bd_bdi = bdi_get(disk->queue->backing_dev_info);
1270 if (test_bit(GD_NEED_PART_SCAN, &disk->state))
1271 bdev_disk_changed(disk, false);
1276 static void blkdev_put_whole(struct block_device *bdev, fmode_t mode)
1278 if (!--bdev->bd_openers)
1279 blkdev_flush_mapping(bdev);
1280 if (bdev->bd_disk->fops->release)
1281 bdev->bd_disk->fops->release(bdev->bd_disk, mode);
1284 static int blkdev_get_part(struct block_device *part, fmode_t mode)
1286 struct gendisk *disk = part->bd_disk;
1287 struct block_device *whole;
1290 if (part->bd_openers)
1293 whole = bdgrab(disk->part0);
1294 ret = blkdev_get_whole(whole, mode);
1299 if (!bdev_nr_sectors(part))
1300 goto out_blkdev_put;
1302 disk->open_partitions++;
1303 set_init_blocksize(part);
1304 if (part->bd_bdi == &noop_backing_dev_info)
1305 part->bd_bdi = bdi_get(disk->queue->backing_dev_info);
1311 blkdev_put_whole(whole, mode);
1317 static void blkdev_put_part(struct block_device *part, fmode_t mode)
1319 struct block_device *whole = bdev_whole(part);
1321 if (--part->bd_openers)
1323 blkdev_flush_mapping(part);
1324 whole->bd_disk->open_partitions--;
1325 blkdev_put_whole(whole, mode);
1329 struct block_device *blkdev_get_no_open(dev_t dev)
1331 struct block_device *bdev;
1332 struct gendisk *disk;
1336 blk_request_module(dev);
1342 disk = bdev->bd_disk;
1343 if (!kobject_get_unless_zero(&disk_to_dev(disk)->kobj))
1345 if ((disk->flags & (GENHD_FL_UP | GENHD_FL_HIDDEN)) != GENHD_FL_UP)
1347 if (!try_module_get(bdev->bd_disk->fops->owner))
1357 void blkdev_put_no_open(struct block_device *bdev)
1359 module_put(bdev->bd_disk->fops->owner);
1360 put_disk(bdev->bd_disk);
1365 * blkdev_get_by_dev - open a block device by device number
1366 * @dev: device number of block device to open
1367 * @mode: FMODE_* mask
1368 * @holder: exclusive holder identifier
1370 * Open the block device described by device number @dev. If @mode includes
1371 * %FMODE_EXCL, the block device is opened with exclusive access. Specifying
1372 * %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may nest for
1375 * Use this interface ONLY if you really do not have anything better - i.e. when
1376 * you are behind a truly sucky interface and all you are given is a device
1377 * number. Everything else should use blkdev_get_by_path().
1383 * Reference to the block_device on success, ERR_PTR(-errno) on failure.
1385 struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
1387 bool unblock_events = true;
1388 struct block_device *bdev;
1389 struct gendisk *disk;
1392 ret = devcgroup_check_permission(DEVCG_DEV_BLOCK,
1393 MAJOR(dev), MINOR(dev),
1394 ((mode & FMODE_READ) ? DEVCG_ACC_READ : 0) |
1395 ((mode & FMODE_WRITE) ? DEVCG_ACC_WRITE : 0));
1397 return ERR_PTR(ret);
1399 bdev = blkdev_get_no_open(dev);
1401 return ERR_PTR(-ENXIO);
1402 disk = bdev->bd_disk;
1404 if (mode & FMODE_EXCL) {
1405 ret = bd_prepare_to_claim(bdev, holder);
1410 disk_block_events(disk);
1412 mutex_lock(&disk->open_mutex);
1414 if (!(disk->flags & GENHD_FL_UP))
1415 goto abort_claiming;
1416 if (bdev_is_partition(bdev))
1417 ret = blkdev_get_part(bdev, mode);
1419 ret = blkdev_get_whole(bdev, mode);
1421 goto abort_claiming;
1422 if (mode & FMODE_EXCL) {
1423 bd_finish_claiming(bdev, holder);
1426 * Block event polling for write claims if requested. Any write
1427 * holder makes the write_holder state stick until all are
1428 * released. This is good enough and tracking individual
1429 * writeable reference is too fragile given the way @mode is
1430 * used in blkdev_get/put().
1432 if ((mode & FMODE_WRITE) && !bdev->bd_write_holder &&
1433 (disk->flags & GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE)) {
1434 bdev->bd_write_holder = true;
1435 unblock_events = false;
1438 mutex_unlock(&disk->open_mutex);
1441 disk_unblock_events(disk);
1445 if (mode & FMODE_EXCL)
1446 bd_abort_claiming(bdev, holder);
1447 mutex_unlock(&disk->open_mutex);
1448 disk_unblock_events(disk);
1450 blkdev_put_no_open(bdev);
1451 return ERR_PTR(ret);
1453 EXPORT_SYMBOL(blkdev_get_by_dev);
1456 * blkdev_get_by_path - open a block device by name
1457 * @path: path to the block device to open
1458 * @mode: FMODE_* mask
1459 * @holder: exclusive holder identifier
1461 * Open the block device described by the device file at @path. If @mode
1462 * includes %FMODE_EXCL, the block device is opened with exclusive access.
1463 * Specifying %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may
1464 * nest for the same @holder.
1470 * Reference to the block_device on success, ERR_PTR(-errno) on failure.
1472 struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
1475 struct block_device *bdev;
1479 error = lookup_bdev(path, &dev);
1481 return ERR_PTR(error);
1483 bdev = blkdev_get_by_dev(dev, mode, holder);
1484 if (!IS_ERR(bdev) && (mode & FMODE_WRITE) && bdev_read_only(bdev)) {
1485 blkdev_put(bdev, mode);
1486 return ERR_PTR(-EACCES);
1491 EXPORT_SYMBOL(blkdev_get_by_path);
1493 static int blkdev_open(struct inode * inode, struct file * filp)
1495 struct block_device *bdev;
1498 * Preserve backwards compatibility and allow large file access
1499 * even if userspace doesn't ask for it explicitly. Some mkfs
1500 * binary needs it. We might want to drop this workaround
1501 * during an unstable branch.
1503 filp->f_flags |= O_LARGEFILE;
1505 filp->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC;
1507 if (filp->f_flags & O_NDELAY)
1508 filp->f_mode |= FMODE_NDELAY;
1509 if (filp->f_flags & O_EXCL)
1510 filp->f_mode |= FMODE_EXCL;
1511 if ((filp->f_flags & O_ACCMODE) == 3)
1512 filp->f_mode |= FMODE_WRITE_IOCTL;
1514 bdev = blkdev_get_by_dev(inode->i_rdev, filp->f_mode, filp);
1516 return PTR_ERR(bdev);
1517 filp->f_mapping = bdev->bd_inode->i_mapping;
1518 filp->f_wb_err = filemap_sample_wb_err(filp->f_mapping);
1522 void blkdev_put(struct block_device *bdev, fmode_t mode)
1524 struct gendisk *disk = bdev->bd_disk;
1527 * Sync early if it looks like we're the last one. If someone else
1528 * opens the block device between now and the decrement of bd_openers
1529 * then we did a sync that we didn't need to, but that's not the end
1530 * of the world and we want to avoid long (could be several minute)
1531 * syncs while holding the mutex.
1533 if (bdev->bd_openers == 1)
1534 sync_blockdev(bdev);
1536 mutex_lock(&disk->open_mutex);
1537 if (mode & FMODE_EXCL) {
1538 struct block_device *whole = bdev_whole(bdev);
1542 * Release a claim on the device. The holder fields
1543 * are protected with bdev_lock. open_mutex is to
1544 * synchronize disk_holder unlinking.
1546 spin_lock(&bdev_lock);
1548 WARN_ON_ONCE(--bdev->bd_holders < 0);
1549 WARN_ON_ONCE(--whole->bd_holders < 0);
1551 if ((bdev_free = !bdev->bd_holders))
1552 bdev->bd_holder = NULL;
1553 if (!whole->bd_holders)
1554 whole->bd_holder = NULL;
1556 spin_unlock(&bdev_lock);
1559 * If this was the last claim, remove holder link and
1560 * unblock evpoll if it was a write holder.
1562 if (bdev_free && bdev->bd_write_holder) {
1563 disk_unblock_events(disk);
1564 bdev->bd_write_holder = false;
1569 * Trigger event checking and tell drivers to flush MEDIA_CHANGE
1570 * event. This is to ensure detection of media removal commanded
1571 * from userland - e.g. eject(1).
1573 disk_flush_events(disk, DISK_EVENT_MEDIA_CHANGE);
1575 if (bdev_is_partition(bdev))
1576 blkdev_put_part(bdev, mode);
1578 blkdev_put_whole(bdev, mode);
1579 mutex_unlock(&disk->open_mutex);
1581 blkdev_put_no_open(bdev);
1583 EXPORT_SYMBOL(blkdev_put);
1585 static int blkdev_close(struct inode * inode, struct file * filp)
1587 struct block_device *bdev = I_BDEV(bdev_file_inode(filp));
1588 blkdev_put(bdev, filp->f_mode);
1592 static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1594 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
1595 fmode_t mode = file->f_mode;
1598 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
1599 * to updated it before every ioctl.
1601 if (file->f_flags & O_NDELAY)
1602 mode |= FMODE_NDELAY;
1604 mode &= ~FMODE_NDELAY;
1606 return blkdev_ioctl(bdev, mode, cmd, arg);
1610 * Write data to the block device. Only intended for the block device itself
1611 * and the raw driver which basically is a fake block device.
1613 * Does not take i_mutex for the write and thus is not for general purpose
1616 static ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
1618 struct file *file = iocb->ki_filp;
1619 struct inode *bd_inode = bdev_file_inode(file);
1620 loff_t size = i_size_read(bd_inode);
1621 struct blk_plug plug;
1625 if (bdev_read_only(I_BDEV(bd_inode)))
1628 if (IS_SWAPFILE(bd_inode) && !is_hibernate_resume_dev(bd_inode->i_rdev))
1631 if (!iov_iter_count(from))
1634 if (iocb->ki_pos >= size)
1637 if ((iocb->ki_flags & (IOCB_NOWAIT | IOCB_DIRECT)) == IOCB_NOWAIT)
1640 size -= iocb->ki_pos;
1641 if (iov_iter_count(from) > size) {
1642 shorted = iov_iter_count(from) - size;
1643 iov_iter_truncate(from, size);
1646 blk_start_plug(&plug);
1647 ret = __generic_file_write_iter(iocb, from);
1649 ret = generic_write_sync(iocb, ret);
1650 iov_iter_reexpand(from, iov_iter_count(from) + shorted);
1651 blk_finish_plug(&plug);
1655 static ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
1657 struct file *file = iocb->ki_filp;
1658 struct inode *bd_inode = bdev_file_inode(file);
1659 loff_t size = i_size_read(bd_inode);
1660 loff_t pos = iocb->ki_pos;
1668 if (iov_iter_count(to) > size) {
1669 shorted = iov_iter_count(to) - size;
1670 iov_iter_truncate(to, size);
1673 ret = generic_file_read_iter(iocb, to);
1674 iov_iter_reexpand(to, iov_iter_count(to) + shorted);
1678 static int blkdev_writepages(struct address_space *mapping,
1679 struct writeback_control *wbc)
1681 return generic_writepages(mapping, wbc);
1684 static const struct address_space_operations def_blk_aops = {
1685 .set_page_dirty = __set_page_dirty_buffers,
1686 .readpage = blkdev_readpage,
1687 .readahead = blkdev_readahead,
1688 .writepage = blkdev_writepage,
1689 .write_begin = blkdev_write_begin,
1690 .write_end = blkdev_write_end,
1691 .writepages = blkdev_writepages,
1692 .direct_IO = blkdev_direct_IO,
1693 .migratepage = buffer_migrate_page_norefs,
1694 .is_dirty_writeback = buffer_check_dirty_writeback,
1697 #define BLKDEV_FALLOC_FL_SUPPORTED \
1698 (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
1699 FALLOC_FL_ZERO_RANGE | FALLOC_FL_NO_HIDE_STALE)
1701 static long blkdev_fallocate(struct file *file, int mode, loff_t start,
1704 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
1705 loff_t end = start + len - 1;
1709 /* Fail if we don't recognize the flags. */
1710 if (mode & ~BLKDEV_FALLOC_FL_SUPPORTED)
1713 /* Don't go off the end of the device. */
1714 isize = i_size_read(bdev->bd_inode);
1718 if (mode & FALLOC_FL_KEEP_SIZE) {
1719 len = isize - start;
1720 end = start + len - 1;
1726 * Don't allow IO that isn't aligned to logical block size.
1728 if ((start | len) & (bdev_logical_block_size(bdev) - 1))
1731 /* Invalidate the page cache, including dirty pages. */
1732 error = truncate_bdev_range(bdev, file->f_mode, start, end);
1737 case FALLOC_FL_ZERO_RANGE:
1738 case FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE:
1739 error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
1740 GFP_KERNEL, BLKDEV_ZERO_NOUNMAP);
1742 case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE:
1743 error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
1744 GFP_KERNEL, BLKDEV_ZERO_NOFALLBACK);
1746 case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE | FALLOC_FL_NO_HIDE_STALE:
1747 error = blkdev_issue_discard(bdev, start >> 9, len >> 9,
1757 * Invalidate the page cache again; if someone wandered in and dirtied
1758 * a page, we just discard it - userspace has no way of knowing whether
1759 * the write happened before or after discard completing...
1761 return truncate_bdev_range(bdev, file->f_mode, start, end);
1764 const struct file_operations def_blk_fops = {
1765 .open = blkdev_open,
1766 .release = blkdev_close,
1767 .llseek = block_llseek,
1768 .read_iter = blkdev_read_iter,
1769 .write_iter = blkdev_write_iter,
1770 .iopoll = blkdev_iopoll,
1771 .mmap = generic_file_mmap,
1772 .fsync = blkdev_fsync,
1773 .unlocked_ioctl = block_ioctl,
1774 #ifdef CONFIG_COMPAT
1775 .compat_ioctl = compat_blkdev_ioctl,
1777 .splice_read = generic_file_splice_read,
1778 .splice_write = iter_file_splice_write,
1779 .fallocate = blkdev_fallocate,
1783 * lookup_bdev - lookup a struct block_device by name
1784 * @pathname: special file representing the block device
1785 * @dev: return value of the block device's dev_t
1787 * Get a reference to the blockdevice at @pathname in the current
1788 * namespace if possible and return it. Return ERR_PTR(error)
1791 int lookup_bdev(const char *pathname, dev_t *dev)
1793 struct inode *inode;
1797 if (!pathname || !*pathname)
1800 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
1804 inode = d_backing_inode(path.dentry);
1806 if (!S_ISBLK(inode->i_mode))
1809 if (!may_open_dev(&path))
1812 *dev = inode->i_rdev;
1818 EXPORT_SYMBOL(lookup_bdev);
1820 int __invalidate_device(struct block_device *bdev, bool kill_dirty)
1822 struct super_block *sb = get_super(bdev);
1827 * no need to lock the super, get_super holds the
1828 * read mutex so the filesystem cannot go away
1829 * under us (->put_super runs with the write lock
1832 shrink_dcache_sb(sb);
1833 res = invalidate_inodes(sb, kill_dirty);
1836 invalidate_bdev(bdev);
1839 EXPORT_SYMBOL(__invalidate_device);
1841 void iterate_bdevs(void (*func)(struct block_device *, void *), void *arg)
1843 struct inode *inode, *old_inode = NULL;
1845 spin_lock(&blockdev_superblock->s_inode_list_lock);
1846 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
1847 struct address_space *mapping = inode->i_mapping;
1848 struct block_device *bdev;
1850 spin_lock(&inode->i_lock);
1851 if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) ||
1852 mapping->nrpages == 0) {
1853 spin_unlock(&inode->i_lock);
1857 spin_unlock(&inode->i_lock);
1858 spin_unlock(&blockdev_superblock->s_inode_list_lock);
1860 * We hold a reference to 'inode' so it couldn't have been
1861 * removed from s_inodes list while we dropped the
1862 * s_inode_list_lock We cannot iput the inode now as we can
1863 * be holding the last reference and we cannot iput it under
1864 * s_inode_list_lock. So we keep the reference and iput it
1869 bdev = I_BDEV(inode);
1871 mutex_lock(&bdev->bd_disk->open_mutex);
1872 if (bdev->bd_openers)
1874 mutex_unlock(&bdev->bd_disk->open_mutex);
1876 spin_lock(&blockdev_superblock->s_inode_list_lock);
1878 spin_unlock(&blockdev_superblock->s_inode_list_lock);