1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
9 #include <linux/init.h>
11 #include <linux/fcntl.h>
12 #include <linux/slab.h>
13 #include <linux/kmod.h>
14 #include <linux/major.h>
15 #include <linux/device_cgroup.h>
16 #include <linux/highmem.h>
17 #include <linux/blkdev.h>
18 #include <linux/backing-dev.h>
19 #include <linux/module.h>
20 #include <linux/blkpg.h>
21 #include <linux/magic.h>
22 #include <linux/buffer_head.h>
23 #include <linux/swap.h>
24 #include <linux/pagevec.h>
25 #include <linux/writeback.h>
26 #include <linux/mpage.h>
27 #include <linux/mount.h>
28 #include <linux/pseudo_fs.h>
29 #include <linux/uio.h>
30 #include <linux/namei.h>
31 #include <linux/log2.h>
32 #include <linux/cleancache.h>
33 #include <linux/task_io_accounting_ops.h>
34 #include <linux/falloc.h>
35 #include <linux/part_stat.h>
36 #include <linux/uaccess.h>
37 #include <linux/suspend.h>
41 struct block_device bdev;
42 struct inode vfs_inode;
45 static const struct address_space_operations def_blk_aops;
47 static inline struct bdev_inode *BDEV_I(struct inode *inode)
49 return container_of(inode, struct bdev_inode, vfs_inode);
52 struct block_device *I_BDEV(struct inode *inode)
54 return &BDEV_I(inode)->bdev;
56 EXPORT_SYMBOL(I_BDEV);
58 static void bdev_write_inode(struct block_device *bdev)
60 struct inode *inode = bdev->bd_inode;
63 spin_lock(&inode->i_lock);
64 while (inode->i_state & I_DIRTY) {
65 spin_unlock(&inode->i_lock);
66 ret = write_inode_now(inode, true);
68 char name[BDEVNAME_SIZE];
69 pr_warn_ratelimited("VFS: Dirty inode writeback failed "
70 "for block device %s (err=%d).\n",
71 bdevname(bdev, name), ret);
73 spin_lock(&inode->i_lock);
75 spin_unlock(&inode->i_lock);
78 /* Kill _all_ buffers and pagecache , dirty or not.. */
79 static void kill_bdev(struct block_device *bdev)
81 struct address_space *mapping = bdev->bd_inode->i_mapping;
83 if (mapping->nrpages == 0 && mapping->nrexceptional == 0)
87 truncate_inode_pages(mapping, 0);
90 /* Invalidate clean unused buffers and pagecache. */
91 void invalidate_bdev(struct block_device *bdev)
93 struct address_space *mapping = bdev->bd_inode->i_mapping;
95 if (mapping->nrpages) {
97 lru_add_drain_all(); /* make sure all lru add caches are flushed */
98 invalidate_mapping_pages(mapping, 0, -1);
100 /* 99% of the time, we don't need to flush the cleancache on the bdev.
101 * But, for the strange corners, lets be cautious
103 cleancache_invalidate_inode(mapping);
105 EXPORT_SYMBOL(invalidate_bdev);
108 * Drop all buffers & page cache for given bdev range. This function bails
109 * with error if bdev has other exclusive owner (such as filesystem).
111 int truncate_bdev_range(struct block_device *bdev, fmode_t mode,
112 loff_t lstart, loff_t lend)
115 * If we don't hold exclusive handle for the device, upgrade to it
116 * while we discard the buffer cache to avoid discarding buffers
117 * under live filesystem.
119 if (!(mode & FMODE_EXCL)) {
120 int err = bd_prepare_to_claim(bdev, truncate_bdev_range);
125 truncate_inode_pages_range(bdev->bd_inode->i_mapping, lstart, lend);
126 if (!(mode & FMODE_EXCL))
127 bd_abort_claiming(bdev, truncate_bdev_range);
130 EXPORT_SYMBOL(truncate_bdev_range);
132 static void set_init_blocksize(struct block_device *bdev)
134 bdev->bd_inode->i_blkbits = blksize_bits(bdev_logical_block_size(bdev));
137 int set_blocksize(struct block_device *bdev, int size)
139 /* Size must be a power of two, and between 512 and PAGE_SIZE */
140 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
143 /* Size cannot be smaller than the size supported by the device */
144 if (size < bdev_logical_block_size(bdev))
147 /* Don't change the size if it is same as current */
148 if (bdev->bd_inode->i_blkbits != blksize_bits(size)) {
150 bdev->bd_inode->i_blkbits = blksize_bits(size);
156 EXPORT_SYMBOL(set_blocksize);
158 int sb_set_blocksize(struct super_block *sb, int size)
160 if (set_blocksize(sb->s_bdev, size))
162 /* If we get here, we know size is power of two
163 * and it's value is between 512 and PAGE_SIZE */
164 sb->s_blocksize = size;
165 sb->s_blocksize_bits = blksize_bits(size);
166 return sb->s_blocksize;
169 EXPORT_SYMBOL(sb_set_blocksize);
171 int sb_min_blocksize(struct super_block *sb, int size)
173 int minsize = bdev_logical_block_size(sb->s_bdev);
176 return sb_set_blocksize(sb, size);
179 EXPORT_SYMBOL(sb_min_blocksize);
182 blkdev_get_block(struct inode *inode, sector_t iblock,
183 struct buffer_head *bh, int create)
185 bh->b_bdev = I_BDEV(inode);
186 bh->b_blocknr = iblock;
187 set_buffer_mapped(bh);
191 static struct inode *bdev_file_inode(struct file *file)
193 return file->f_mapping->host;
196 static unsigned int dio_bio_write_op(struct kiocb *iocb)
198 unsigned int op = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
200 /* avoid the need for a I/O completion work item */
201 if (iocb->ki_flags & IOCB_DSYNC)
206 #define DIO_INLINE_BIO_VECS 4
208 static void blkdev_bio_end_io_simple(struct bio *bio)
210 struct task_struct *waiter = bio->bi_private;
212 WRITE_ONCE(bio->bi_private, NULL);
213 blk_wake_io_task(waiter);
217 __blkdev_direct_IO_simple(struct kiocb *iocb, struct iov_iter *iter,
220 struct file *file = iocb->ki_filp;
221 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
222 struct bio_vec inline_vecs[DIO_INLINE_BIO_VECS], *vecs;
223 loff_t pos = iocb->ki_pos;
224 bool should_dirty = false;
229 if ((pos | iov_iter_alignment(iter)) &
230 (bdev_logical_block_size(bdev) - 1))
233 if (nr_pages <= DIO_INLINE_BIO_VECS)
236 vecs = kmalloc_array(nr_pages, sizeof(struct bio_vec),
242 bio_init(&bio, vecs, nr_pages);
243 bio_set_dev(&bio, bdev);
244 bio.bi_iter.bi_sector = pos >> 9;
245 bio.bi_write_hint = iocb->ki_hint;
246 bio.bi_private = current;
247 bio.bi_end_io = blkdev_bio_end_io_simple;
248 bio.bi_ioprio = iocb->ki_ioprio;
250 ret = bio_iov_iter_get_pages(&bio, iter);
253 ret = bio.bi_iter.bi_size;
255 if (iov_iter_rw(iter) == READ) {
256 bio.bi_opf = REQ_OP_READ;
257 if (iter_is_iovec(iter))
260 bio.bi_opf = dio_bio_write_op(iocb);
261 task_io_account_write(ret);
263 if (iocb->ki_flags & IOCB_HIPRI)
264 bio_set_polled(&bio, iocb);
266 qc = submit_bio(&bio);
268 set_current_state(TASK_UNINTERRUPTIBLE);
269 if (!READ_ONCE(bio.bi_private))
271 if (!(iocb->ki_flags & IOCB_HIPRI) ||
272 !blk_poll(bdev_get_queue(bdev), qc, true))
275 __set_current_state(TASK_RUNNING);
277 bio_release_pages(&bio, should_dirty);
278 if (unlikely(bio.bi_status))
279 ret = blk_status_to_errno(bio.bi_status);
282 if (vecs != inline_vecs)
293 struct task_struct *waiter;
298 bool should_dirty : 1;
303 static struct bio_set blkdev_dio_pool;
305 static int blkdev_iopoll(struct kiocb *kiocb, bool wait)
307 struct block_device *bdev = I_BDEV(kiocb->ki_filp->f_mapping->host);
308 struct request_queue *q = bdev_get_queue(bdev);
310 return blk_poll(q, READ_ONCE(kiocb->ki_cookie), wait);
313 static void blkdev_bio_end_io(struct bio *bio)
315 struct blkdev_dio *dio = bio->bi_private;
316 bool should_dirty = dio->should_dirty;
318 if (bio->bi_status && !dio->bio.bi_status)
319 dio->bio.bi_status = bio->bi_status;
321 if (!dio->multi_bio || atomic_dec_and_test(&dio->ref)) {
323 struct kiocb *iocb = dio->iocb;
326 if (likely(!dio->bio.bi_status)) {
330 ret = blk_status_to_errno(dio->bio.bi_status);
333 dio->iocb->ki_complete(iocb, ret, 0);
337 struct task_struct *waiter = dio->waiter;
339 WRITE_ONCE(dio->waiter, NULL);
340 blk_wake_io_task(waiter);
345 bio_check_pages_dirty(bio);
347 bio_release_pages(bio, false);
353 __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter, int nr_pages)
355 struct file *file = iocb->ki_filp;
356 struct inode *inode = bdev_file_inode(file);
357 struct block_device *bdev = I_BDEV(inode);
358 struct blk_plug plug;
359 struct blkdev_dio *dio;
361 bool is_poll = (iocb->ki_flags & IOCB_HIPRI) != 0;
362 bool is_read = (iov_iter_rw(iter) == READ), is_sync;
363 loff_t pos = iocb->ki_pos;
364 blk_qc_t qc = BLK_QC_T_NONE;
367 if ((pos | iov_iter_alignment(iter)) &
368 (bdev_logical_block_size(bdev) - 1))
371 bio = bio_alloc_bioset(GFP_KERNEL, nr_pages, &blkdev_dio_pool);
373 dio = container_of(bio, struct blkdev_dio, bio);
374 dio->is_sync = is_sync = is_sync_kiocb(iocb);
376 dio->waiter = current;
383 dio->multi_bio = false;
384 dio->should_dirty = is_read && iter_is_iovec(iter);
387 * Don't plug for HIPRI/polled IO, as those should go straight
391 blk_start_plug(&plug);
394 bio_set_dev(bio, bdev);
395 bio->bi_iter.bi_sector = pos >> 9;
396 bio->bi_write_hint = iocb->ki_hint;
397 bio->bi_private = dio;
398 bio->bi_end_io = blkdev_bio_end_io;
399 bio->bi_ioprio = iocb->ki_ioprio;
401 ret = bio_iov_iter_get_pages(bio, iter);
403 bio->bi_status = BLK_STS_IOERR;
409 bio->bi_opf = REQ_OP_READ;
410 if (dio->should_dirty)
411 bio_set_pages_dirty(bio);
413 bio->bi_opf = dio_bio_write_op(iocb);
414 task_io_account_write(bio->bi_iter.bi_size);
417 dio->size += bio->bi_iter.bi_size;
418 pos += bio->bi_iter.bi_size;
420 nr_pages = iov_iter_npages(iter, BIO_MAX_PAGES);
424 if (iocb->ki_flags & IOCB_HIPRI) {
425 bio_set_polled(bio, iocb);
429 qc = submit_bio(bio);
432 WRITE_ONCE(iocb->ki_cookie, qc);
436 if (!dio->multi_bio) {
438 * AIO needs an extra reference to ensure the dio
439 * structure which is embedded into the first bio
444 dio->multi_bio = true;
445 atomic_set(&dio->ref, 2);
447 atomic_inc(&dio->ref);
451 bio = bio_alloc(GFP_KERNEL, nr_pages);
455 blk_finish_plug(&plug);
461 set_current_state(TASK_UNINTERRUPTIBLE);
462 if (!READ_ONCE(dio->waiter))
465 if (!(iocb->ki_flags & IOCB_HIPRI) ||
466 !blk_poll(bdev_get_queue(bdev), qc, true))
469 __set_current_state(TASK_RUNNING);
472 ret = blk_status_to_errno(dio->bio.bi_status);
481 blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
485 nr_pages = iov_iter_npages(iter, BIO_MAX_PAGES + 1);
488 if (is_sync_kiocb(iocb) && nr_pages <= BIO_MAX_PAGES)
489 return __blkdev_direct_IO_simple(iocb, iter, nr_pages);
491 return __blkdev_direct_IO(iocb, iter, min(nr_pages, BIO_MAX_PAGES));
494 static __init int blkdev_init(void)
496 return bioset_init(&blkdev_dio_pool, 4, offsetof(struct blkdev_dio, bio), BIOSET_NEED_BVECS);
498 module_init(blkdev_init);
500 int __sync_blockdev(struct block_device *bdev, int wait)
505 return filemap_flush(bdev->bd_inode->i_mapping);
506 return filemap_write_and_wait(bdev->bd_inode->i_mapping);
510 * Write out and wait upon all the dirty data associated with a block
511 * device via its mapping. Does not take the superblock lock.
513 int sync_blockdev(struct block_device *bdev)
515 return __sync_blockdev(bdev, 1);
517 EXPORT_SYMBOL(sync_blockdev);
520 * Write out and wait upon all dirty data associated with this
521 * device. Filesystem data as well as the underlying block
522 * device. Takes the superblock lock.
524 int fsync_bdev(struct block_device *bdev)
526 struct super_block *sb = get_super(bdev);
528 int res = sync_filesystem(sb);
532 return sync_blockdev(bdev);
534 EXPORT_SYMBOL(fsync_bdev);
537 * freeze_bdev -- lock a filesystem and force it into a consistent state
538 * @bdev: blockdevice to lock
540 * If a superblock is found on this device, we take the s_umount semaphore
541 * on it to make sure nobody unmounts until the snapshot creation is done.
542 * The reference counter (bd_fsfreeze_count) guarantees that only the last
543 * unfreeze process can unfreeze the frozen filesystem actually when multiple
544 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
545 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
548 int freeze_bdev(struct block_device *bdev)
550 struct super_block *sb;
553 mutex_lock(&bdev->bd_fsfreeze_mutex);
554 if (++bdev->bd_fsfreeze_count > 1)
557 sb = get_active_super(bdev);
560 if (sb->s_op->freeze_super)
561 error = sb->s_op->freeze_super(sb);
563 error = freeze_super(sb);
564 deactivate_super(sb);
567 bdev->bd_fsfreeze_count--;
570 bdev->bd_fsfreeze_sb = sb;
575 mutex_unlock(&bdev->bd_fsfreeze_mutex);
578 EXPORT_SYMBOL(freeze_bdev);
581 * thaw_bdev -- unlock filesystem
582 * @bdev: blockdevice to unlock
584 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
586 int thaw_bdev(struct block_device *bdev)
588 struct super_block *sb;
591 mutex_lock(&bdev->bd_fsfreeze_mutex);
592 if (!bdev->bd_fsfreeze_count)
596 if (--bdev->bd_fsfreeze_count > 0)
599 sb = bdev->bd_fsfreeze_sb;
603 if (sb->s_op->thaw_super)
604 error = sb->s_op->thaw_super(sb);
606 error = thaw_super(sb);
608 bdev->bd_fsfreeze_count++;
610 mutex_unlock(&bdev->bd_fsfreeze_mutex);
613 EXPORT_SYMBOL(thaw_bdev);
615 static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
617 return block_write_full_page(page, blkdev_get_block, wbc);
620 static int blkdev_readpage(struct file * file, struct page * page)
622 return block_read_full_page(page, blkdev_get_block);
625 static void blkdev_readahead(struct readahead_control *rac)
627 mpage_readahead(rac, blkdev_get_block);
630 static int blkdev_write_begin(struct file *file, struct address_space *mapping,
631 loff_t pos, unsigned len, unsigned flags,
632 struct page **pagep, void **fsdata)
634 return block_write_begin(mapping, pos, len, flags, pagep,
638 static int blkdev_write_end(struct file *file, struct address_space *mapping,
639 loff_t pos, unsigned len, unsigned copied,
640 struct page *page, void *fsdata)
643 ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
653 * for a block special file file_inode(file)->i_size is zero
654 * so we compute the size by hand (just as in block_read/write above)
656 static loff_t block_llseek(struct file *file, loff_t offset, int whence)
658 struct inode *bd_inode = bdev_file_inode(file);
661 inode_lock(bd_inode);
662 retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode));
663 inode_unlock(bd_inode);
667 int blkdev_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
669 struct inode *bd_inode = bdev_file_inode(filp);
670 struct block_device *bdev = I_BDEV(bd_inode);
673 error = file_write_and_wait_range(filp, start, end);
678 * There is no need to serialise calls to blkdev_issue_flush with
679 * i_mutex and doing so causes performance issues with concurrent
680 * O_SYNC writers to a block device.
682 error = blkdev_issue_flush(bdev, GFP_KERNEL);
683 if (error == -EOPNOTSUPP)
688 EXPORT_SYMBOL(blkdev_fsync);
691 * bdev_read_page() - Start reading a page from a block device
692 * @bdev: The device to read the page from
693 * @sector: The offset on the device to read the page to (need not be aligned)
694 * @page: The page to read
696 * On entry, the page should be locked. It will be unlocked when the page
697 * has been read. If the block driver implements rw_page synchronously,
698 * that will be true on exit from this function, but it need not be.
700 * Errors returned by this function are usually "soft", eg out of memory, or
701 * queue full; callers should try a different route to read this page rather
702 * than propagate an error back up the stack.
704 * Return: negative errno if an error occurs, 0 if submission was successful.
706 int bdev_read_page(struct block_device *bdev, sector_t sector,
709 const struct block_device_operations *ops = bdev->bd_disk->fops;
710 int result = -EOPNOTSUPP;
712 if (!ops->rw_page || bdev_get_integrity(bdev))
715 result = blk_queue_enter(bdev->bd_disk->queue, 0);
718 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
720 blk_queue_exit(bdev->bd_disk->queue);
725 * bdev_write_page() - Start writing a page to a block device
726 * @bdev: The device to write the page to
727 * @sector: The offset on the device to write the page to (need not be aligned)
728 * @page: The page to write
729 * @wbc: The writeback_control for the write
731 * On entry, the page should be locked and not currently under writeback.
732 * On exit, if the write started successfully, the page will be unlocked and
733 * under writeback. If the write failed already (eg the driver failed to
734 * queue the page to the device), the page will still be locked. If the
735 * caller is a ->writepage implementation, it will need to unlock the page.
737 * Errors returned by this function are usually "soft", eg out of memory, or
738 * queue full; callers should try a different route to write this page rather
739 * than propagate an error back up the stack.
741 * Return: negative errno if an error occurs, 0 if submission was successful.
743 int bdev_write_page(struct block_device *bdev, sector_t sector,
744 struct page *page, struct writeback_control *wbc)
747 const struct block_device_operations *ops = bdev->bd_disk->fops;
749 if (!ops->rw_page || bdev_get_integrity(bdev))
751 result = blk_queue_enter(bdev->bd_disk->queue, 0);
755 set_page_writeback(page);
756 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
759 end_page_writeback(page);
761 clean_page_buffers(page);
764 blk_queue_exit(bdev->bd_disk->queue);
772 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
773 static struct kmem_cache * bdev_cachep __read_mostly;
775 static struct inode *bdev_alloc_inode(struct super_block *sb)
777 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
780 return &ei->vfs_inode;
783 static void bdev_free_inode(struct inode *inode)
785 struct block_device *bdev = I_BDEV(inode);
787 free_percpu(bdev->bd_stats);
789 kmem_cache_free(bdev_cachep, BDEV_I(inode));
792 static void init_once(void *data)
794 struct bdev_inode *ei = data;
796 inode_init_once(&ei->vfs_inode);
799 static void bdev_evict_inode(struct inode *inode)
801 struct block_device *bdev = &BDEV_I(inode)->bdev;
802 truncate_inode_pages_final(&inode->i_data);
803 invalidate_inode_buffers(inode); /* is it needed here? */
805 /* Detach inode from wb early as bdi_put() may free bdi->wb */
806 inode_detach_wb(inode);
807 if (bdev->bd_bdi != &noop_backing_dev_info) {
808 bdi_put(bdev->bd_bdi);
809 bdev->bd_bdi = &noop_backing_dev_info;
813 static const struct super_operations bdev_sops = {
814 .statfs = simple_statfs,
815 .alloc_inode = bdev_alloc_inode,
816 .free_inode = bdev_free_inode,
817 .drop_inode = generic_delete_inode,
818 .evict_inode = bdev_evict_inode,
821 static int bd_init_fs_context(struct fs_context *fc)
823 struct pseudo_fs_context *ctx = init_pseudo(fc, BDEVFS_MAGIC);
826 fc->s_iflags |= SB_I_CGROUPWB;
827 ctx->ops = &bdev_sops;
831 static struct file_system_type bd_type = {
833 .init_fs_context = bd_init_fs_context,
834 .kill_sb = kill_anon_super,
837 struct super_block *blockdev_superblock __read_mostly;
838 EXPORT_SYMBOL_GPL(blockdev_superblock);
840 void __init bdev_cache_init(void)
843 static struct vfsmount *bd_mnt;
845 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
846 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
847 SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC),
849 err = register_filesystem(&bd_type);
851 panic("Cannot register bdev pseudo-fs");
852 bd_mnt = kern_mount(&bd_type);
854 panic("Cannot create bdev pseudo-fs");
855 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
858 struct block_device *bdev_alloc(struct gendisk *disk, u8 partno)
860 struct block_device *bdev;
863 inode = new_inode(blockdev_superblock);
866 inode->i_mode = S_IFBLK;
868 inode->i_data.a_ops = &def_blk_aops;
869 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
871 bdev = I_BDEV(inode);
872 memset(bdev, 0, sizeof(*bdev));
873 mutex_init(&bdev->bd_mutex);
874 mutex_init(&bdev->bd_fsfreeze_mutex);
875 spin_lock_init(&bdev->bd_size_lock);
876 bdev->bd_disk = disk;
877 bdev->bd_partno = partno;
878 bdev->bd_inode = inode;
879 bdev->bd_bdi = &noop_backing_dev_info;
881 INIT_LIST_HEAD(&bdev->bd_holder_disks);
883 bdev->bd_stats = alloc_percpu(struct disk_stats);
884 if (!bdev->bd_stats) {
891 void bdev_add(struct block_device *bdev, dev_t dev)
894 bdev->bd_inode->i_rdev = dev;
895 bdev->bd_inode->i_ino = dev;
896 insert_inode_hash(bdev->bd_inode);
899 static struct block_device *bdget(dev_t dev)
903 inode = ilookup(blockdev_superblock, dev);
906 return &BDEV_I(inode)->bdev;
910 * bdgrab -- Grab a reference to an already referenced block device
911 * @bdev: Block device to grab a reference to.
913 * Returns the block_device with an additional reference when successful,
914 * or NULL if the inode is already beeing freed.
916 struct block_device *bdgrab(struct block_device *bdev)
918 if (!igrab(bdev->bd_inode))
922 EXPORT_SYMBOL(bdgrab);
924 struct block_device *bdget_part(struct hd_struct *part)
926 return bdget(part_devt(part));
929 long nr_blockdev_pages(void)
934 spin_lock(&blockdev_superblock->s_inode_list_lock);
935 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list)
936 ret += inode->i_mapping->nrpages;
937 spin_unlock(&blockdev_superblock->s_inode_list_lock);
942 void bdput(struct block_device *bdev)
944 iput(bdev->bd_inode);
946 EXPORT_SYMBOL(bdput);
949 * bd_may_claim - test whether a block device can be claimed
950 * @bdev: block device of interest
951 * @whole: whole block device containing @bdev, may equal @bdev
952 * @holder: holder trying to claim @bdev
954 * Test whether @bdev can be claimed by @holder.
957 * spin_lock(&bdev_lock).
960 * %true if @bdev can be claimed, %false otherwise.
962 static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
965 if (bdev->bd_holder == holder)
966 return true; /* already a holder */
967 else if (bdev->bd_holder != NULL)
968 return false; /* held by someone else */
969 else if (whole == bdev)
970 return true; /* is a whole device which isn't held */
972 else if (whole->bd_holder == bd_may_claim)
973 return true; /* is a partition of a device that is being partitioned */
974 else if (whole->bd_holder != NULL)
975 return false; /* is a partition of a held device */
977 return true; /* is a partition of an un-held device */
981 * bd_prepare_to_claim - claim a block device
982 * @bdev: block device of interest
983 * @holder: holder trying to claim @bdev
985 * Claim @bdev. This function fails if @bdev is already claimed by another
986 * holder and waits if another claiming is in progress. return, the caller
987 * has ownership of bd_claiming and bd_holder[s].
990 * 0 if @bdev can be claimed, -EBUSY otherwise.
992 int bd_prepare_to_claim(struct block_device *bdev, void *holder)
994 struct block_device *whole = bdev_whole(bdev);
996 if (WARN_ON_ONCE(!holder))
999 spin_lock(&bdev_lock);
1000 /* if someone else claimed, fail */
1001 if (!bd_may_claim(bdev, whole, holder)) {
1002 spin_unlock(&bdev_lock);
1006 /* if claiming is already in progress, wait for it to finish */
1007 if (whole->bd_claiming) {
1008 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
1011 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
1012 spin_unlock(&bdev_lock);
1014 finish_wait(wq, &wait);
1019 whole->bd_claiming = holder;
1020 spin_unlock(&bdev_lock);
1023 EXPORT_SYMBOL_GPL(bd_prepare_to_claim); /* only for the loop driver */
1025 static void bd_clear_claiming(struct block_device *whole, void *holder)
1027 lockdep_assert_held(&bdev_lock);
1028 /* tell others that we're done */
1029 BUG_ON(whole->bd_claiming != holder);
1030 whole->bd_claiming = NULL;
1031 wake_up_bit(&whole->bd_claiming, 0);
1035 * bd_finish_claiming - finish claiming of a block device
1036 * @bdev: block device of interest
1037 * @holder: holder that has claimed @bdev
1039 * Finish exclusive open of a block device. Mark the device as exlusively
1040 * open by the holder and wake up all waiters for exclusive open to finish.
1042 static void bd_finish_claiming(struct block_device *bdev, void *holder)
1044 struct block_device *whole = bdev_whole(bdev);
1046 spin_lock(&bdev_lock);
1047 BUG_ON(!bd_may_claim(bdev, whole, holder));
1049 * Note that for a whole device bd_holders will be incremented twice,
1050 * and bd_holder will be set to bd_may_claim before being set to holder
1052 whole->bd_holders++;
1053 whole->bd_holder = bd_may_claim;
1055 bdev->bd_holder = holder;
1056 bd_clear_claiming(whole, holder);
1057 spin_unlock(&bdev_lock);
1061 * bd_abort_claiming - abort claiming of a block device
1062 * @bdev: block device of interest
1063 * @whole: whole block device
1064 * @holder: holder that has claimed @bdev
1066 * Abort claiming of a block device when the exclusive open failed. This can be
1067 * also used when exclusive open is not actually desired and we just needed
1068 * to block other exclusive openers for a while.
1070 void bd_abort_claiming(struct block_device *bdev, void *holder)
1072 spin_lock(&bdev_lock);
1073 bd_clear_claiming(bdev_whole(bdev), holder);
1074 spin_unlock(&bdev_lock);
1076 EXPORT_SYMBOL(bd_abort_claiming);
1079 struct bd_holder_disk {
1080 struct list_head list;
1081 struct gendisk *disk;
1085 static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev,
1086 struct gendisk *disk)
1088 struct bd_holder_disk *holder;
1090 list_for_each_entry(holder, &bdev->bd_holder_disks, list)
1091 if (holder->disk == disk)
1096 static int add_symlink(struct kobject *from, struct kobject *to)
1098 return sysfs_create_link(from, to, kobject_name(to));
1101 static void del_symlink(struct kobject *from, struct kobject *to)
1103 sysfs_remove_link(from, kobject_name(to));
1107 * bd_link_disk_holder - create symlinks between holding disk and slave bdev
1108 * @bdev: the claimed slave bdev
1109 * @disk: the holding disk
1111 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
1113 * This functions creates the following sysfs symlinks.
1115 * - from "slaves" directory of the holder @disk to the claimed @bdev
1116 * - from "holders" directory of the @bdev to the holder @disk
1118 * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
1119 * passed to bd_link_disk_holder(), then:
1121 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
1122 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
1124 * The caller must have claimed @bdev before calling this function and
1125 * ensure that both @bdev and @disk are valid during the creation and
1126 * lifetime of these symlinks.
1132 * 0 on success, -errno on failure.
1134 int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
1136 struct bd_holder_disk *holder;
1139 mutex_lock(&bdev->bd_mutex);
1141 WARN_ON_ONCE(!bdev->bd_holder);
1143 /* FIXME: remove the following once add_disk() handles errors */
1144 if (WARN_ON(!disk->slave_dir || !bdev->bd_part->holder_dir))
1147 holder = bd_find_holder_disk(bdev, disk);
1153 holder = kzalloc(sizeof(*holder), GFP_KERNEL);
1159 INIT_LIST_HEAD(&holder->list);
1160 holder->disk = disk;
1163 ret = add_symlink(disk->slave_dir, bdev_kobj(bdev));
1167 ret = add_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj);
1171 * bdev could be deleted beneath us which would implicitly destroy
1172 * the holder directory. Hold on to it.
1174 kobject_get(bdev->bd_part->holder_dir);
1176 list_add(&holder->list, &bdev->bd_holder_disks);
1180 del_symlink(disk->slave_dir, bdev_kobj(bdev));
1184 mutex_unlock(&bdev->bd_mutex);
1187 EXPORT_SYMBOL_GPL(bd_link_disk_holder);
1190 * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
1191 * @bdev: the calimed slave bdev
1192 * @disk: the holding disk
1194 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
1199 void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
1201 struct bd_holder_disk *holder;
1203 mutex_lock(&bdev->bd_mutex);
1205 holder = bd_find_holder_disk(bdev, disk);
1207 if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
1208 del_symlink(disk->slave_dir, bdev_kobj(bdev));
1209 del_symlink(bdev->bd_part->holder_dir,
1210 &disk_to_dev(disk)->kobj);
1211 kobject_put(bdev->bd_part->holder_dir);
1212 list_del_init(&holder->list);
1216 mutex_unlock(&bdev->bd_mutex);
1218 EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
1221 static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
1223 int bdev_disk_changed(struct block_device *bdev, bool invalidate)
1225 struct gendisk *disk = bdev->bd_disk;
1228 lockdep_assert_held(&bdev->bd_mutex);
1230 clear_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
1233 ret = blk_drop_partitions(bdev);
1238 * Historically we only set the capacity to zero for devices that
1239 * support partitions (independ of actually having partitions created).
1240 * Doing that is rather inconsistent, but changing it broke legacy
1241 * udisks polling for legacy ide-cdrom devices. Use the crude check
1242 * below to get the sane behavior for most device while not breaking
1243 * userspace for this particular setup.
1246 if (disk_part_scan_enabled(disk) ||
1247 !(disk->flags & GENHD_FL_REMOVABLE))
1248 set_capacity(disk, 0);
1250 if (disk->fops->revalidate_disk)
1251 disk->fops->revalidate_disk(disk);
1254 if (get_capacity(disk)) {
1255 ret = blk_add_partitions(disk, bdev);
1258 } else if (invalidate) {
1260 * Tell userspace that the media / partition table may have
1263 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
1269 * Only exported for for loop and dasd for historic reasons. Don't use in new
1272 EXPORT_SYMBOL_GPL(bdev_disk_changed);
1277 * mutex_lock(part->bd_mutex)
1278 * mutex_lock_nested(whole->bd_mutex, 1)
1280 static int __blkdev_get(struct block_device *bdev, fmode_t mode)
1282 struct gendisk *disk = bdev->bd_disk;
1285 if (!bdev->bd_openers) {
1286 if (!bdev_is_partition(bdev)) {
1288 bdev->bd_part = disk_get_part(disk, 0);
1293 if (disk->fops->open)
1294 ret = disk->fops->open(bdev, mode);
1297 set_init_blocksize(bdev);
1300 * If the device is invalidated, rescan partition
1301 * if open succeeded or failed with -ENOMEDIUM.
1302 * The latter is necessary to prevent ghost
1303 * partitions on a removed medium.
1305 if (test_bit(GD_NEED_PART_SCAN, &disk->state) &&
1306 (!ret || ret == -ENOMEDIUM))
1307 bdev_disk_changed(bdev, ret == -ENOMEDIUM);
1312 struct block_device *whole = bdget_disk(disk, 0);
1314 mutex_lock_nested(&whole->bd_mutex, 1);
1315 ret = __blkdev_get(whole, mode);
1317 mutex_unlock(&whole->bd_mutex);
1321 whole->bd_part_count++;
1322 mutex_unlock(&whole->bd_mutex);
1324 bdev->bd_part = disk_get_part(disk, bdev->bd_partno);
1325 if (!(disk->flags & GENHD_FL_UP) ||
1326 !bdev->bd_part || !bdev_nr_sectors(bdev)) {
1327 __blkdev_put(whole, mode, 1);
1332 set_init_blocksize(bdev);
1335 if (bdev->bd_bdi == &noop_backing_dev_info)
1336 bdev->bd_bdi = bdi_get(disk->queue->backing_dev_info);
1338 if (!bdev_is_partition(bdev)) {
1340 if (bdev->bd_disk->fops->open)
1341 ret = bdev->bd_disk->fops->open(bdev, mode);
1342 /* the same as first opener case, read comment there */
1343 if (test_bit(GD_NEED_PART_SCAN, &disk->state) &&
1344 (!ret || ret == -ENOMEDIUM))
1345 bdev_disk_changed(bdev, ret == -ENOMEDIUM);
1354 disk_put_part(bdev->bd_part);
1355 bdev->bd_part = NULL;
1359 struct block_device *blkdev_get_no_open(dev_t dev)
1361 struct block_device *bdev;
1362 struct gendisk *disk;
1364 down_read(&bdev_lookup_sem);
1367 up_read(&bdev_lookup_sem);
1368 blk_request_module(dev);
1369 down_read(&bdev_lookup_sem);
1376 disk = bdev->bd_disk;
1377 if (!kobject_get_unless_zero(&disk_to_dev(disk)->kobj))
1379 if ((disk->flags & (GENHD_FL_UP | GENHD_FL_HIDDEN)) != GENHD_FL_UP)
1381 if (!try_module_get(bdev->bd_disk->fops->owner))
1383 up_read(&bdev_lookup_sem);
1390 up_read(&bdev_lookup_sem);
1394 void blkdev_put_no_open(struct block_device *bdev)
1396 module_put(bdev->bd_disk->fops->owner);
1397 put_disk(bdev->bd_disk);
1402 * blkdev_get_by_dev - open a block device by device number
1403 * @dev: device number of block device to open
1404 * @mode: FMODE_* mask
1405 * @holder: exclusive holder identifier
1407 * Open the block device described by device number @dev. If @mode includes
1408 * %FMODE_EXCL, the block device is opened with exclusive access. Specifying
1409 * %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may nest for
1412 * Use this interface ONLY if you really do not have anything better - i.e. when
1413 * you are behind a truly sucky interface and all you are given is a device
1414 * number. Everything else should use blkdev_get_by_path().
1420 * Reference to the block_device on success, ERR_PTR(-errno) on failure.
1422 struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
1424 bool unblock_events = true;
1425 struct block_device *bdev;
1426 struct gendisk *disk;
1429 ret = devcgroup_check_permission(DEVCG_DEV_BLOCK,
1430 MAJOR(dev), MINOR(dev),
1431 ((mode & FMODE_READ) ? DEVCG_ACC_READ : 0) |
1432 ((mode & FMODE_WRITE) ? DEVCG_ACC_WRITE : 0));
1434 return ERR_PTR(ret);
1437 * If we lost a race with 'disk' being deleted, try again. See md.c.
1440 bdev = blkdev_get_no_open(dev);
1442 return ERR_PTR(-ENXIO);
1443 disk = bdev->bd_disk;
1445 if (mode & FMODE_EXCL) {
1446 ret = bd_prepare_to_claim(bdev, holder);
1451 disk_block_events(disk);
1453 mutex_lock(&bdev->bd_mutex);
1454 ret =__blkdev_get(bdev, mode);
1456 goto abort_claiming;
1457 if (mode & FMODE_EXCL) {
1458 bd_finish_claiming(bdev, holder);
1461 * Block event polling for write claims if requested. Any write
1462 * holder makes the write_holder state stick until all are
1463 * released. This is good enough and tracking individual
1464 * writeable reference is too fragile given the way @mode is
1465 * used in blkdev_get/put().
1467 if ((mode & FMODE_WRITE) && !bdev->bd_write_holder &&
1468 (disk->flags & GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE)) {
1469 bdev->bd_write_holder = true;
1470 unblock_events = false;
1473 mutex_unlock(&bdev->bd_mutex);
1476 disk_unblock_events(disk);
1480 if (mode & FMODE_EXCL)
1481 bd_abort_claiming(bdev, holder);
1482 mutex_unlock(&bdev->bd_mutex);
1483 disk_unblock_events(disk);
1485 blkdev_put_no_open(bdev);
1486 if (ret == -ERESTARTSYS)
1488 return ERR_PTR(ret);
1490 EXPORT_SYMBOL(blkdev_get_by_dev);
1493 * blkdev_get_by_path - open a block device by name
1494 * @path: path to the block device to open
1495 * @mode: FMODE_* mask
1496 * @holder: exclusive holder identifier
1498 * Open the block device described by the device file at @path. If @mode
1499 * includes %FMODE_EXCL, the block device is opened with exclusive access.
1500 * Specifying %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may
1501 * nest for the same @holder.
1507 * Reference to the block_device on success, ERR_PTR(-errno) on failure.
1509 struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
1512 struct block_device *bdev;
1516 error = lookup_bdev(path, &dev);
1518 return ERR_PTR(error);
1520 bdev = blkdev_get_by_dev(dev, mode, holder);
1521 if (!IS_ERR(bdev) && (mode & FMODE_WRITE) && bdev_read_only(bdev)) {
1522 blkdev_put(bdev, mode);
1523 return ERR_PTR(-EACCES);
1528 EXPORT_SYMBOL(blkdev_get_by_path);
1530 static int blkdev_open(struct inode * inode, struct file * filp)
1532 struct block_device *bdev;
1535 * Preserve backwards compatibility and allow large file access
1536 * even if userspace doesn't ask for it explicitly. Some mkfs
1537 * binary needs it. We might want to drop this workaround
1538 * during an unstable branch.
1540 filp->f_flags |= O_LARGEFILE;
1542 filp->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC;
1544 if (filp->f_flags & O_NDELAY)
1545 filp->f_mode |= FMODE_NDELAY;
1546 if (filp->f_flags & O_EXCL)
1547 filp->f_mode |= FMODE_EXCL;
1548 if ((filp->f_flags & O_ACCMODE) == 3)
1549 filp->f_mode |= FMODE_WRITE_IOCTL;
1551 bdev = blkdev_get_by_dev(inode->i_rdev, filp->f_mode, filp);
1553 return PTR_ERR(bdev);
1554 filp->f_mapping = bdev->bd_inode->i_mapping;
1555 filp->f_wb_err = filemap_sample_wb_err(filp->f_mapping);
1559 static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
1561 struct gendisk *disk = bdev->bd_disk;
1562 struct block_device *victim = NULL;
1565 * Sync early if it looks like we're the last one. If someone else
1566 * opens the block device between now and the decrement of bd_openers
1567 * then we did a sync that we didn't need to, but that's not the end
1568 * of the world and we want to avoid long (could be several minute)
1569 * syncs while holding the mutex.
1571 if (bdev->bd_openers == 1)
1572 sync_blockdev(bdev);
1574 mutex_lock_nested(&bdev->bd_mutex, for_part);
1576 bdev->bd_part_count--;
1578 if (!--bdev->bd_openers) {
1579 WARN_ON_ONCE(bdev->bd_holders);
1580 sync_blockdev(bdev);
1582 bdev_write_inode(bdev);
1584 if (!bdev_is_partition(bdev) && disk->fops->release)
1585 disk->fops->release(disk, mode);
1587 disk_put_part(bdev->bd_part);
1588 bdev->bd_part = NULL;
1589 if (bdev_is_partition(bdev))
1590 victim = bdev_whole(bdev);
1592 if (!bdev_is_partition(bdev) && disk->fops->release)
1593 disk->fops->release(disk, mode);
1595 mutex_unlock(&bdev->bd_mutex);
1597 __blkdev_put(victim, mode, 1);
1602 void blkdev_put(struct block_device *bdev, fmode_t mode)
1604 struct gendisk *disk = bdev->bd_disk;
1606 mutex_lock(&bdev->bd_mutex);
1608 if (mode & FMODE_EXCL) {
1609 struct block_device *whole = bdev_whole(bdev);
1613 * Release a claim on the device. The holder fields
1614 * are protected with bdev_lock. bd_mutex is to
1615 * synchronize disk_holder unlinking.
1617 spin_lock(&bdev_lock);
1619 WARN_ON_ONCE(--bdev->bd_holders < 0);
1620 WARN_ON_ONCE(--whole->bd_holders < 0);
1622 if ((bdev_free = !bdev->bd_holders))
1623 bdev->bd_holder = NULL;
1624 if (!whole->bd_holders)
1625 whole->bd_holder = NULL;
1627 spin_unlock(&bdev_lock);
1630 * If this was the last claim, remove holder link and
1631 * unblock evpoll if it was a write holder.
1633 if (bdev_free && bdev->bd_write_holder) {
1634 disk_unblock_events(disk);
1635 bdev->bd_write_holder = false;
1640 * Trigger event checking and tell drivers to flush MEDIA_CHANGE
1641 * event. This is to ensure detection of media removal commanded
1642 * from userland - e.g. eject(1).
1644 disk_flush_events(disk, DISK_EVENT_MEDIA_CHANGE);
1645 mutex_unlock(&bdev->bd_mutex);
1647 __blkdev_put(bdev, mode, 0);
1648 blkdev_put_no_open(bdev);
1650 EXPORT_SYMBOL(blkdev_put);
1652 static int blkdev_close(struct inode * inode, struct file * filp)
1654 struct block_device *bdev = I_BDEV(bdev_file_inode(filp));
1655 blkdev_put(bdev, filp->f_mode);
1659 static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1661 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
1662 fmode_t mode = file->f_mode;
1665 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
1666 * to updated it before every ioctl.
1668 if (file->f_flags & O_NDELAY)
1669 mode |= FMODE_NDELAY;
1671 mode &= ~FMODE_NDELAY;
1673 return blkdev_ioctl(bdev, mode, cmd, arg);
1677 * Write data to the block device. Only intended for the block device itself
1678 * and the raw driver which basically is a fake block device.
1680 * Does not take i_mutex for the write and thus is not for general purpose
1683 ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
1685 struct file *file = iocb->ki_filp;
1686 struct inode *bd_inode = bdev_file_inode(file);
1687 loff_t size = i_size_read(bd_inode);
1688 struct blk_plug plug;
1691 if (bdev_read_only(I_BDEV(bd_inode)))
1694 if (IS_SWAPFILE(bd_inode) && !is_hibernate_resume_dev(bd_inode->i_rdev))
1697 if (!iov_iter_count(from))
1700 if (iocb->ki_pos >= size)
1703 if ((iocb->ki_flags & (IOCB_NOWAIT | IOCB_DIRECT)) == IOCB_NOWAIT)
1706 iov_iter_truncate(from, size - iocb->ki_pos);
1708 blk_start_plug(&plug);
1709 ret = __generic_file_write_iter(iocb, from);
1711 ret = generic_write_sync(iocb, ret);
1712 blk_finish_plug(&plug);
1715 EXPORT_SYMBOL_GPL(blkdev_write_iter);
1717 ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
1719 struct file *file = iocb->ki_filp;
1720 struct inode *bd_inode = bdev_file_inode(file);
1721 loff_t size = i_size_read(bd_inode);
1722 loff_t pos = iocb->ki_pos;
1728 iov_iter_truncate(to, size);
1729 return generic_file_read_iter(iocb, to);
1731 EXPORT_SYMBOL_GPL(blkdev_read_iter);
1734 * Try to release a page associated with block device when the system
1735 * is under memory pressure.
1737 static int blkdev_releasepage(struct page *page, gfp_t wait)
1739 struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
1741 if (super && super->s_op->bdev_try_to_free_page)
1742 return super->s_op->bdev_try_to_free_page(super, page, wait);
1744 return try_to_free_buffers(page);
1747 static int blkdev_writepages(struct address_space *mapping,
1748 struct writeback_control *wbc)
1750 return generic_writepages(mapping, wbc);
1753 static const struct address_space_operations def_blk_aops = {
1754 .readpage = blkdev_readpage,
1755 .readahead = blkdev_readahead,
1756 .writepage = blkdev_writepage,
1757 .write_begin = blkdev_write_begin,
1758 .write_end = blkdev_write_end,
1759 .writepages = blkdev_writepages,
1760 .releasepage = blkdev_releasepage,
1761 .direct_IO = blkdev_direct_IO,
1762 .migratepage = buffer_migrate_page_norefs,
1763 .is_dirty_writeback = buffer_check_dirty_writeback,
1766 #define BLKDEV_FALLOC_FL_SUPPORTED \
1767 (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
1768 FALLOC_FL_ZERO_RANGE | FALLOC_FL_NO_HIDE_STALE)
1770 static long blkdev_fallocate(struct file *file, int mode, loff_t start,
1773 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
1774 loff_t end = start + len - 1;
1778 /* Fail if we don't recognize the flags. */
1779 if (mode & ~BLKDEV_FALLOC_FL_SUPPORTED)
1782 /* Don't go off the end of the device. */
1783 isize = i_size_read(bdev->bd_inode);
1787 if (mode & FALLOC_FL_KEEP_SIZE) {
1788 len = isize - start;
1789 end = start + len - 1;
1795 * Don't allow IO that isn't aligned to logical block size.
1797 if ((start | len) & (bdev_logical_block_size(bdev) - 1))
1800 /* Invalidate the page cache, including dirty pages. */
1801 error = truncate_bdev_range(bdev, file->f_mode, start, end);
1806 case FALLOC_FL_ZERO_RANGE:
1807 case FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE:
1808 error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
1809 GFP_KERNEL, BLKDEV_ZERO_NOUNMAP);
1811 case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE:
1812 error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
1813 GFP_KERNEL, BLKDEV_ZERO_NOFALLBACK);
1815 case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE | FALLOC_FL_NO_HIDE_STALE:
1816 error = blkdev_issue_discard(bdev, start >> 9, len >> 9,
1826 * Invalidate again; if someone wandered in and dirtied a page,
1827 * the caller will be given -EBUSY. The third argument is
1828 * inclusive, so the rounding here is safe.
1830 return invalidate_inode_pages2_range(bdev->bd_inode->i_mapping,
1831 start >> PAGE_SHIFT,
1835 const struct file_operations def_blk_fops = {
1836 .open = blkdev_open,
1837 .release = blkdev_close,
1838 .llseek = block_llseek,
1839 .read_iter = blkdev_read_iter,
1840 .write_iter = blkdev_write_iter,
1841 .iopoll = blkdev_iopoll,
1842 .mmap = generic_file_mmap,
1843 .fsync = blkdev_fsync,
1844 .unlocked_ioctl = block_ioctl,
1845 #ifdef CONFIG_COMPAT
1846 .compat_ioctl = compat_blkdev_ioctl,
1848 .splice_read = generic_file_splice_read,
1849 .splice_write = iter_file_splice_write,
1850 .fallocate = blkdev_fallocate,
1854 * lookup_bdev - lookup a struct block_device by name
1855 * @pathname: special file representing the block device
1857 * Get a reference to the blockdevice at @pathname in the current
1858 * namespace if possible and return it. Return ERR_PTR(error)
1861 int lookup_bdev(const char *pathname, dev_t *dev)
1863 struct inode *inode;
1867 if (!pathname || !*pathname)
1870 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
1874 inode = d_backing_inode(path.dentry);
1876 if (!S_ISBLK(inode->i_mode))
1879 if (!may_open_dev(&path))
1882 *dev = inode->i_rdev;
1888 EXPORT_SYMBOL(lookup_bdev);
1890 int __invalidate_device(struct block_device *bdev, bool kill_dirty)
1892 struct super_block *sb = get_super(bdev);
1897 * no need to lock the super, get_super holds the
1898 * read mutex so the filesystem cannot go away
1899 * under us (->put_super runs with the write lock
1902 shrink_dcache_sb(sb);
1903 res = invalidate_inodes(sb, kill_dirty);
1906 invalidate_bdev(bdev);
1909 EXPORT_SYMBOL(__invalidate_device);
1911 void iterate_bdevs(void (*func)(struct block_device *, void *), void *arg)
1913 struct inode *inode, *old_inode = NULL;
1915 spin_lock(&blockdev_superblock->s_inode_list_lock);
1916 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
1917 struct address_space *mapping = inode->i_mapping;
1918 struct block_device *bdev;
1920 spin_lock(&inode->i_lock);
1921 if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) ||
1922 mapping->nrpages == 0) {
1923 spin_unlock(&inode->i_lock);
1927 spin_unlock(&inode->i_lock);
1928 spin_unlock(&blockdev_superblock->s_inode_list_lock);
1930 * We hold a reference to 'inode' so it couldn't have been
1931 * removed from s_inodes list while we dropped the
1932 * s_inode_list_lock We cannot iput the inode now as we can
1933 * be holding the last reference and we cannot iput it under
1934 * s_inode_list_lock. So we keep the reference and iput it
1939 bdev = I_BDEV(inode);
1941 mutex_lock(&bdev->bd_mutex);
1942 if (bdev->bd_openers)
1944 mutex_unlock(&bdev->bd_mutex);
1946 spin_lock(&blockdev_superblock->s_inode_list_lock);
1948 spin_unlock(&blockdev_superblock->s_inode_list_lock);