4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
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/module.h>
18 #include <linux/blkpg.h>
19 #include <linux/buffer_head.h>
20 #include <linux/pagevec.h>
21 #include <linux/writeback.h>
22 #include <linux/mpage.h>
23 #include <linux/mount.h>
24 #include <linux/uio.h>
25 #include <linux/namei.h>
26 #include <linux/log2.h>
27 #include <linux/kmemleak.h>
28 #include <asm/uaccess.h>
32 struct block_device bdev;
33 struct inode vfs_inode;
36 static const struct address_space_operations def_blk_aops;
38 static inline struct bdev_inode *BDEV_I(struct inode *inode)
40 return container_of(inode, struct bdev_inode, vfs_inode);
43 inline struct block_device *I_BDEV(struct inode *inode)
45 return &BDEV_I(inode)->bdev;
47 EXPORT_SYMBOL(I_BDEV);
50 * Move the inode from its current bdi to a new bdi. If the inode is dirty we
51 * need to move it onto the dirty list of @dst so that the inode is always on
54 static void bdev_inode_switch_bdi(struct inode *inode,
55 struct backing_dev_info *dst)
57 struct backing_dev_info *old = inode->i_data.backing_dev_info;
58 bool wakeup_bdi = false;
60 if (unlikely(dst == old)) /* deadlock avoidance */
62 bdi_lock_two(&old->wb, &dst->wb);
63 spin_lock(&inode->i_lock);
64 inode->i_data.backing_dev_info = dst;
65 if (inode->i_state & I_DIRTY) {
66 if (bdi_cap_writeback_dirty(dst) && !wb_has_dirty_io(&dst->wb))
68 list_move(&inode->i_wb_list, &dst->wb.b_dirty);
70 spin_unlock(&inode->i_lock);
71 spin_unlock(&old->wb.list_lock);
72 spin_unlock(&dst->wb.list_lock);
75 bdi_wakeup_thread_delayed(dst);
78 sector_t blkdev_max_block(struct block_device *bdev)
80 sector_t retval = ~((sector_t)0);
81 loff_t sz = i_size_read(bdev->bd_inode);
84 unsigned int size = block_size(bdev);
85 unsigned int sizebits = blksize_bits(size);
86 retval = (sz >> sizebits);
91 /* Kill _all_ buffers and pagecache , dirty or not.. */
92 void kill_bdev(struct block_device *bdev)
94 if (bdev->bd_inode->i_mapping->nrpages == 0)
97 truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
99 EXPORT_SYMBOL(kill_bdev);
101 int set_blocksize(struct block_device *bdev, int size)
103 /* Size must be a power of two, and between 512 and PAGE_SIZE */
104 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
107 /* Size cannot be smaller than the size supported by the device */
108 if (size < bdev_logical_block_size(bdev))
111 /* Don't change the size if it is same as current */
112 if (bdev->bd_block_size != size) {
114 bdev->bd_block_size = size;
115 bdev->bd_inode->i_blkbits = blksize_bits(size);
121 EXPORT_SYMBOL(set_blocksize);
123 int sb_set_blocksize(struct super_block *sb, int size)
125 if (set_blocksize(sb->s_bdev, size))
127 /* If we get here, we know size is power of two
128 * and it's value is between 512 and PAGE_SIZE */
129 sb->s_blocksize = size;
130 sb->s_blocksize_bits = blksize_bits(size);
131 return sb->s_blocksize;
134 EXPORT_SYMBOL(sb_set_blocksize);
136 int sb_min_blocksize(struct super_block *sb, int size)
138 int minsize = bdev_logical_block_size(sb->s_bdev);
141 return sb_set_blocksize(sb, size);
144 EXPORT_SYMBOL(sb_min_blocksize);
147 blkdev_get_block(struct inode *inode, sector_t iblock,
148 struct buffer_head *bh, int create)
150 if (iblock >= blkdev_max_block(I_BDEV(inode))) {
155 * for reads, we're just trying to fill a partial page.
156 * return a hole, they will have to call get_block again
157 * before they can fill it, and they will get -EIO at that
162 bh->b_bdev = I_BDEV(inode);
163 bh->b_blocknr = iblock;
164 set_buffer_mapped(bh);
169 blkdev_get_blocks(struct inode *inode, sector_t iblock,
170 struct buffer_head *bh, int create)
172 sector_t end_block = blkdev_max_block(I_BDEV(inode));
173 unsigned long max_blocks = bh->b_size >> inode->i_blkbits;
175 if ((iblock + max_blocks) > end_block) {
176 max_blocks = end_block - iblock;
177 if ((long)max_blocks <= 0) {
179 return -EIO; /* write fully beyond EOF */
181 * It is a read which is fully beyond EOF. We return
182 * a !buffer_mapped buffer
188 bh->b_bdev = I_BDEV(inode);
189 bh->b_blocknr = iblock;
190 bh->b_size = max_blocks << inode->i_blkbits;
192 set_buffer_mapped(bh);
197 blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
198 loff_t offset, unsigned long nr_segs)
200 struct file *file = iocb->ki_filp;
201 struct inode *inode = file->f_mapping->host;
203 return __blockdev_direct_IO(rw, iocb, inode, I_BDEV(inode), iov, offset,
204 nr_segs, blkdev_get_blocks, NULL, NULL, 0);
207 int __sync_blockdev(struct block_device *bdev, int wait)
212 return filemap_flush(bdev->bd_inode->i_mapping);
213 return filemap_write_and_wait(bdev->bd_inode->i_mapping);
217 * Write out and wait upon all the dirty data associated with a block
218 * device via its mapping. Does not take the superblock lock.
220 int sync_blockdev(struct block_device *bdev)
222 return __sync_blockdev(bdev, 1);
224 EXPORT_SYMBOL(sync_blockdev);
227 * Write out and wait upon all dirty data associated with this
228 * device. Filesystem data as well as the underlying block
229 * device. Takes the superblock lock.
231 int fsync_bdev(struct block_device *bdev)
233 struct super_block *sb = get_super(bdev);
235 int res = sync_filesystem(sb);
239 return sync_blockdev(bdev);
241 EXPORT_SYMBOL(fsync_bdev);
244 * freeze_bdev -- lock a filesystem and force it into a consistent state
245 * @bdev: blockdevice to lock
247 * If a superblock is found on this device, we take the s_umount semaphore
248 * on it to make sure nobody unmounts until the snapshot creation is done.
249 * The reference counter (bd_fsfreeze_count) guarantees that only the last
250 * unfreeze process can unfreeze the frozen filesystem actually when multiple
251 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
252 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
255 struct super_block *freeze_bdev(struct block_device *bdev)
257 struct super_block *sb;
260 mutex_lock(&bdev->bd_fsfreeze_mutex);
261 if (++bdev->bd_fsfreeze_count > 1) {
263 * We don't even need to grab a reference - the first call
264 * to freeze_bdev grab an active reference and only the last
265 * thaw_bdev drops it.
267 sb = get_super(bdev);
269 mutex_unlock(&bdev->bd_fsfreeze_mutex);
273 sb = get_active_super(bdev);
276 error = freeze_super(sb);
278 deactivate_super(sb);
279 bdev->bd_fsfreeze_count--;
280 mutex_unlock(&bdev->bd_fsfreeze_mutex);
281 return ERR_PTR(error);
283 deactivate_super(sb);
286 mutex_unlock(&bdev->bd_fsfreeze_mutex);
287 return sb; /* thaw_bdev releases s->s_umount */
289 EXPORT_SYMBOL(freeze_bdev);
292 * thaw_bdev -- unlock filesystem
293 * @bdev: blockdevice to unlock
294 * @sb: associated superblock
296 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
298 int thaw_bdev(struct block_device *bdev, struct super_block *sb)
302 mutex_lock(&bdev->bd_fsfreeze_mutex);
303 if (!bdev->bd_fsfreeze_count)
307 if (--bdev->bd_fsfreeze_count > 0)
313 error = thaw_super(sb);
315 bdev->bd_fsfreeze_count++;
316 mutex_unlock(&bdev->bd_fsfreeze_mutex);
320 mutex_unlock(&bdev->bd_fsfreeze_mutex);
323 EXPORT_SYMBOL(thaw_bdev);
325 static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
327 return block_write_full_page(page, blkdev_get_block, wbc);
330 static int blkdev_readpage(struct file * file, struct page * page)
332 return block_read_full_page(page, blkdev_get_block);
335 static int blkdev_write_begin(struct file *file, struct address_space *mapping,
336 loff_t pos, unsigned len, unsigned flags,
337 struct page **pagep, void **fsdata)
339 return block_write_begin(mapping, pos, len, flags, pagep,
343 static int blkdev_write_end(struct file *file, struct address_space *mapping,
344 loff_t pos, unsigned len, unsigned copied,
345 struct page *page, void *fsdata)
348 ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
351 page_cache_release(page);
358 * for a block special file file->f_path.dentry->d_inode->i_size is zero
359 * so we compute the size by hand (just as in block_read/write above)
361 static loff_t block_llseek(struct file *file, loff_t offset, int origin)
363 struct inode *bd_inode = file->f_mapping->host;
367 mutex_lock(&bd_inode->i_mutex);
368 size = i_size_read(bd_inode);
376 offset += file->f_pos;
382 if (offset >= 0 && offset <= size) {
383 if (offset != file->f_pos) {
384 file->f_pos = offset;
389 mutex_unlock(&bd_inode->i_mutex);
393 int blkdev_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
395 struct inode *bd_inode = filp->f_mapping->host;
396 struct block_device *bdev = I_BDEV(bd_inode);
399 error = filemap_write_and_wait_range(filp->f_mapping, start, end);
404 * There is no need to serialise calls to blkdev_issue_flush with
405 * i_mutex and doing so causes performance issues with concurrent
406 * O_SYNC writers to a block device.
408 error = blkdev_issue_flush(bdev, GFP_KERNEL, NULL);
409 if (error == -EOPNOTSUPP)
414 EXPORT_SYMBOL(blkdev_fsync);
420 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
421 static struct kmem_cache * bdev_cachep __read_mostly;
423 static struct inode *bdev_alloc_inode(struct super_block *sb)
425 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
428 return &ei->vfs_inode;
431 static void bdev_i_callback(struct rcu_head *head)
433 struct inode *inode = container_of(head, struct inode, i_rcu);
434 struct bdev_inode *bdi = BDEV_I(inode);
436 INIT_LIST_HEAD(&inode->i_dentry);
437 kmem_cache_free(bdev_cachep, bdi);
440 static void bdev_destroy_inode(struct inode *inode)
442 call_rcu(&inode->i_rcu, bdev_i_callback);
445 static void init_once(void *foo)
447 struct bdev_inode *ei = (struct bdev_inode *) foo;
448 struct block_device *bdev = &ei->bdev;
450 memset(bdev, 0, sizeof(*bdev));
451 mutex_init(&bdev->bd_mutex);
452 INIT_LIST_HEAD(&bdev->bd_inodes);
453 INIT_LIST_HEAD(&bdev->bd_list);
455 INIT_LIST_HEAD(&bdev->bd_holder_disks);
457 inode_init_once(&ei->vfs_inode);
458 /* Initialize mutex for freeze. */
459 mutex_init(&bdev->bd_fsfreeze_mutex);
462 static inline void __bd_forget(struct inode *inode)
464 list_del_init(&inode->i_devices);
465 inode->i_bdev = NULL;
466 inode->i_mapping = &inode->i_data;
469 static void bdev_evict_inode(struct inode *inode)
471 struct block_device *bdev = &BDEV_I(inode)->bdev;
473 truncate_inode_pages(&inode->i_data, 0);
474 invalidate_inode_buffers(inode); /* is it needed here? */
475 end_writeback(inode);
476 spin_lock(&bdev_lock);
477 while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
478 __bd_forget(list_entry(p, struct inode, i_devices));
480 list_del_init(&bdev->bd_list);
481 spin_unlock(&bdev_lock);
484 static const struct super_operations bdev_sops = {
485 .statfs = simple_statfs,
486 .alloc_inode = bdev_alloc_inode,
487 .destroy_inode = bdev_destroy_inode,
488 .drop_inode = generic_delete_inode,
489 .evict_inode = bdev_evict_inode,
492 static struct dentry *bd_mount(struct file_system_type *fs_type,
493 int flags, const char *dev_name, void *data)
495 return mount_pseudo(fs_type, "bdev:", &bdev_sops, NULL, 0x62646576);
498 static struct file_system_type bd_type = {
501 .kill_sb = kill_anon_super,
504 struct super_block *blockdev_superblock __read_mostly;
506 void __init bdev_cache_init(void)
509 struct vfsmount *bd_mnt;
511 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
512 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
513 SLAB_MEM_SPREAD|SLAB_PANIC),
515 err = register_filesystem(&bd_type);
517 panic("Cannot register bdev pseudo-fs");
518 bd_mnt = kern_mount(&bd_type);
520 panic("Cannot create bdev pseudo-fs");
522 * This vfsmount structure is only used to obtain the
523 * blockdev_superblock, so tell kmemleak not to report it.
525 kmemleak_not_leak(bd_mnt);
526 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
530 * Most likely _very_ bad one - but then it's hardly critical for small
531 * /dev and can be fixed when somebody will need really large one.
532 * Keep in mind that it will be fed through icache hash function too.
534 static inline unsigned long hash(dev_t dev)
536 return MAJOR(dev)+MINOR(dev);
539 static int bdev_test(struct inode *inode, void *data)
541 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
544 static int bdev_set(struct inode *inode, void *data)
546 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
550 static LIST_HEAD(all_bdevs);
552 struct block_device *bdget(dev_t dev)
554 struct block_device *bdev;
557 inode = iget5_locked(blockdev_superblock, hash(dev),
558 bdev_test, bdev_set, &dev);
563 bdev = &BDEV_I(inode)->bdev;
565 if (inode->i_state & I_NEW) {
566 bdev->bd_contains = NULL;
567 bdev->bd_super = NULL;
568 bdev->bd_inode = inode;
569 bdev->bd_block_size = (1 << inode->i_blkbits);
570 bdev->bd_part_count = 0;
571 bdev->bd_invalidated = 0;
572 inode->i_mode = S_IFBLK;
574 inode->i_bdev = bdev;
575 inode->i_data.a_ops = &def_blk_aops;
576 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
577 inode->i_data.backing_dev_info = &default_backing_dev_info;
578 spin_lock(&bdev_lock);
579 list_add(&bdev->bd_list, &all_bdevs);
580 spin_unlock(&bdev_lock);
581 unlock_new_inode(inode);
586 EXPORT_SYMBOL(bdget);
589 * bdgrab -- Grab a reference to an already referenced block device
590 * @bdev: Block device to grab a reference to.
592 struct block_device *bdgrab(struct block_device *bdev)
594 ihold(bdev->bd_inode);
597 EXPORT_SYMBOL(bdgrab);
599 long nr_blockdev_pages(void)
601 struct block_device *bdev;
603 spin_lock(&bdev_lock);
604 list_for_each_entry(bdev, &all_bdevs, bd_list) {
605 ret += bdev->bd_inode->i_mapping->nrpages;
607 spin_unlock(&bdev_lock);
611 void bdput(struct block_device *bdev)
613 iput(bdev->bd_inode);
616 EXPORT_SYMBOL(bdput);
618 static struct block_device *bd_acquire(struct inode *inode)
620 struct block_device *bdev;
622 spin_lock(&bdev_lock);
623 bdev = inode->i_bdev;
625 ihold(bdev->bd_inode);
626 spin_unlock(&bdev_lock);
629 spin_unlock(&bdev_lock);
631 bdev = bdget(inode->i_rdev);
633 spin_lock(&bdev_lock);
634 if (!inode->i_bdev) {
636 * We take an additional reference to bd_inode,
637 * and it's released in clear_inode() of inode.
638 * So, we can access it via ->i_mapping always
641 ihold(bdev->bd_inode);
642 inode->i_bdev = bdev;
643 inode->i_mapping = bdev->bd_inode->i_mapping;
644 list_add(&inode->i_devices, &bdev->bd_inodes);
646 spin_unlock(&bdev_lock);
651 /* Call when you free inode */
653 void bd_forget(struct inode *inode)
655 struct block_device *bdev = NULL;
657 spin_lock(&bdev_lock);
659 if (!sb_is_blkdev_sb(inode->i_sb))
660 bdev = inode->i_bdev;
663 spin_unlock(&bdev_lock);
666 iput(bdev->bd_inode);
670 * bd_may_claim - test whether a block device can be claimed
671 * @bdev: block device of interest
672 * @whole: whole block device containing @bdev, may equal @bdev
673 * @holder: holder trying to claim @bdev
675 * Test whether @bdev can be claimed by @holder.
678 * spin_lock(&bdev_lock).
681 * %true if @bdev can be claimed, %false otherwise.
683 static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
686 if (bdev->bd_holder == holder)
687 return true; /* already a holder */
688 else if (bdev->bd_holder != NULL)
689 return false; /* held by someone else */
690 else if (whole == bdev)
691 return true; /* is a whole device which isn't held */
693 else if (whole->bd_holder == bd_may_claim)
694 return true; /* is a partition of a device that is being partitioned */
695 else if (whole->bd_holder != NULL)
696 return false; /* is a partition of a held device */
698 return true; /* is a partition of an un-held device */
702 * bd_prepare_to_claim - prepare to claim a block device
703 * @bdev: block device of interest
704 * @whole: the whole device containing @bdev, may equal @bdev
705 * @holder: holder trying to claim @bdev
707 * Prepare to claim @bdev. This function fails if @bdev is already
708 * claimed by another holder and waits if another claiming is in
709 * progress. This function doesn't actually claim. On successful
710 * return, the caller has ownership of bd_claiming and bd_holder[s].
713 * spin_lock(&bdev_lock). Might release bdev_lock, sleep and regrab
717 * 0 if @bdev can be claimed, -EBUSY otherwise.
719 static int bd_prepare_to_claim(struct block_device *bdev,
720 struct block_device *whole, void *holder)
723 /* if someone else claimed, fail */
724 if (!bd_may_claim(bdev, whole, holder))
727 /* if claiming is already in progress, wait for it to finish */
728 if (whole->bd_claiming) {
729 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
732 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
733 spin_unlock(&bdev_lock);
735 finish_wait(wq, &wait);
736 spin_lock(&bdev_lock);
745 * bd_start_claiming - start claiming a block device
746 * @bdev: block device of interest
747 * @holder: holder trying to claim @bdev
749 * @bdev is about to be opened exclusively. Check @bdev can be opened
750 * exclusively and mark that an exclusive open is in progress. Each
751 * successful call to this function must be matched with a call to
752 * either bd_finish_claiming() or bd_abort_claiming() (which do not
755 * This function is used to gain exclusive access to the block device
756 * without actually causing other exclusive open attempts to fail. It
757 * should be used when the open sequence itself requires exclusive
758 * access but may subsequently fail.
764 * Pointer to the block device containing @bdev on success, ERR_PTR()
767 static struct block_device *bd_start_claiming(struct block_device *bdev,
770 struct gendisk *disk;
771 struct block_device *whole;
777 * @bdev might not have been initialized properly yet, look up
778 * and grab the outer block device the hard way.
780 disk = get_gendisk(bdev->bd_dev, &partno);
782 return ERR_PTR(-ENXIO);
785 * Normally, @bdev should equal what's returned from bdget_disk()
786 * if partno is 0; however, some drivers (floppy) use multiple
787 * bdev's for the same physical device and @bdev may be one of the
788 * aliases. Keep @bdev if partno is 0. This means claimer
789 * tracking is broken for those devices but it has always been that
793 whole = bdget_disk(disk, 0);
795 whole = bdgrab(bdev);
797 module_put(disk->fops->owner);
800 return ERR_PTR(-ENOMEM);
802 /* prepare to claim, if successful, mark claiming in progress */
803 spin_lock(&bdev_lock);
805 err = bd_prepare_to_claim(bdev, whole, holder);
807 whole->bd_claiming = holder;
808 spin_unlock(&bdev_lock);
811 spin_unlock(&bdev_lock);
818 struct bd_holder_disk {
819 struct list_head list;
820 struct gendisk *disk;
824 static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev,
825 struct gendisk *disk)
827 struct bd_holder_disk *holder;
829 list_for_each_entry(holder, &bdev->bd_holder_disks, list)
830 if (holder->disk == disk)
835 static int add_symlink(struct kobject *from, struct kobject *to)
837 return sysfs_create_link(from, to, kobject_name(to));
840 static void del_symlink(struct kobject *from, struct kobject *to)
842 sysfs_remove_link(from, kobject_name(to));
846 * bd_link_disk_holder - create symlinks between holding disk and slave bdev
847 * @bdev: the claimed slave bdev
848 * @disk: the holding disk
850 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
852 * This functions creates the following sysfs symlinks.
854 * - from "slaves" directory of the holder @disk to the claimed @bdev
855 * - from "holders" directory of the @bdev to the holder @disk
857 * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
858 * passed to bd_link_disk_holder(), then:
860 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
861 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
863 * The caller must have claimed @bdev before calling this function and
864 * ensure that both @bdev and @disk are valid during the creation and
865 * lifetime of these symlinks.
871 * 0 on success, -errno on failure.
873 int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
875 struct bd_holder_disk *holder;
878 mutex_lock(&bdev->bd_mutex);
880 WARN_ON_ONCE(!bdev->bd_holder);
882 /* FIXME: remove the following once add_disk() handles errors */
883 if (WARN_ON(!disk->slave_dir || !bdev->bd_part->holder_dir))
886 holder = bd_find_holder_disk(bdev, disk);
892 holder = kzalloc(sizeof(*holder), GFP_KERNEL);
898 INIT_LIST_HEAD(&holder->list);
902 ret = add_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
906 ret = add_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj);
910 * bdev could be deleted beneath us which would implicitly destroy
911 * the holder directory. Hold on to it.
913 kobject_get(bdev->bd_part->holder_dir);
915 list_add(&holder->list, &bdev->bd_holder_disks);
919 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
923 mutex_unlock(&bdev->bd_mutex);
926 EXPORT_SYMBOL_GPL(bd_link_disk_holder);
929 * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
930 * @bdev: the calimed slave bdev
931 * @disk: the holding disk
933 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
938 void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
940 struct bd_holder_disk *holder;
942 mutex_lock(&bdev->bd_mutex);
944 holder = bd_find_holder_disk(bdev, disk);
946 if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
947 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
948 del_symlink(bdev->bd_part->holder_dir,
949 &disk_to_dev(disk)->kobj);
950 kobject_put(bdev->bd_part->holder_dir);
951 list_del_init(&holder->list);
955 mutex_unlock(&bdev->bd_mutex);
957 EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
961 * flush_disk - invalidates all buffer-cache entries on a disk
963 * @bdev: struct block device to be flushed
964 * @kill_dirty: flag to guide handling of dirty inodes
966 * Invalidates all buffer-cache entries on a disk. It should be called
967 * when a disk has been changed -- either by a media change or online
970 static void flush_disk(struct block_device *bdev, bool kill_dirty)
972 if (__invalidate_device(bdev, kill_dirty)) {
973 char name[BDEVNAME_SIZE] = "";
976 disk_name(bdev->bd_disk, 0, name);
977 printk(KERN_WARNING "VFS: busy inodes on changed media or "
978 "resized disk %s\n", name);
983 if (disk_part_scan_enabled(bdev->bd_disk))
984 bdev->bd_invalidated = 1;
988 * check_disk_size_change - checks for disk size change and adjusts bdev size.
989 * @disk: struct gendisk to check
990 * @bdev: struct bdev to adjust.
992 * This routine checks to see if the bdev size does not match the disk size
993 * and adjusts it if it differs.
995 void check_disk_size_change(struct gendisk *disk, struct block_device *bdev)
997 loff_t disk_size, bdev_size;
999 disk_size = (loff_t)get_capacity(disk) << 9;
1000 bdev_size = i_size_read(bdev->bd_inode);
1001 if (disk_size != bdev_size) {
1002 char name[BDEVNAME_SIZE];
1004 disk_name(disk, 0, name);
1006 "%s: detected capacity change from %lld to %lld\n",
1007 name, bdev_size, disk_size);
1008 i_size_write(bdev->bd_inode, disk_size);
1009 flush_disk(bdev, false);
1012 EXPORT_SYMBOL(check_disk_size_change);
1015 * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back
1016 * @disk: struct gendisk to be revalidated
1018 * This routine is a wrapper for lower-level driver's revalidate_disk
1019 * call-backs. It is used to do common pre and post operations needed
1020 * for all revalidate_disk operations.
1022 int revalidate_disk(struct gendisk *disk)
1024 struct block_device *bdev;
1027 if (disk->fops->revalidate_disk)
1028 ret = disk->fops->revalidate_disk(disk);
1030 bdev = bdget_disk(disk, 0);
1034 mutex_lock(&bdev->bd_mutex);
1035 check_disk_size_change(disk, bdev);
1036 bdev->bd_invalidated = 0;
1037 mutex_unlock(&bdev->bd_mutex);
1041 EXPORT_SYMBOL(revalidate_disk);
1044 * This routine checks whether a removable media has been changed,
1045 * and invalidates all buffer-cache-entries in that case. This
1046 * is a relatively slow routine, so we have to try to minimize using
1047 * it. Thus it is called only upon a 'mount' or 'open'. This
1048 * is the best way of combining speed and utility, I think.
1049 * People changing diskettes in the middle of an operation deserve
1052 int check_disk_change(struct block_device *bdev)
1054 struct gendisk *disk = bdev->bd_disk;
1055 const struct block_device_operations *bdops = disk->fops;
1056 unsigned int events;
1058 events = disk_clear_events(disk, DISK_EVENT_MEDIA_CHANGE |
1059 DISK_EVENT_EJECT_REQUEST);
1060 if (!(events & DISK_EVENT_MEDIA_CHANGE))
1063 flush_disk(bdev, true);
1064 if (bdops->revalidate_disk)
1065 bdops->revalidate_disk(bdev->bd_disk);
1069 EXPORT_SYMBOL(check_disk_change);
1071 void bd_set_size(struct block_device *bdev, loff_t size)
1073 unsigned bsize = bdev_logical_block_size(bdev);
1075 mutex_lock(&bdev->bd_inode->i_mutex);
1076 i_size_write(bdev->bd_inode, size);
1077 mutex_unlock(&bdev->bd_inode->i_mutex);
1078 while (bsize < PAGE_CACHE_SIZE) {
1083 bdev->bd_block_size = bsize;
1084 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
1086 EXPORT_SYMBOL(bd_set_size);
1088 static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
1093 * mutex_lock(part->bd_mutex)
1094 * mutex_lock_nested(whole->bd_mutex, 1)
1097 static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
1099 struct gendisk *disk;
1100 struct module *owner;
1105 if (mode & FMODE_READ)
1107 if (mode & FMODE_WRITE)
1110 * hooks: /n/, see "layering violations".
1113 ret = devcgroup_inode_permission(bdev->bd_inode, perm);
1123 disk = get_gendisk(bdev->bd_dev, &partno);
1126 owner = disk->fops->owner;
1128 disk_block_events(disk);
1129 mutex_lock_nested(&bdev->bd_mutex, for_part);
1130 if (!bdev->bd_openers) {
1131 bdev->bd_disk = disk;
1132 bdev->bd_contains = bdev;
1134 struct backing_dev_info *bdi;
1137 bdev->bd_part = disk_get_part(disk, partno);
1142 if (disk->fops->open) {
1143 ret = disk->fops->open(bdev, mode);
1144 if (ret == -ERESTARTSYS) {
1145 /* Lost a race with 'disk' being
1146 * deleted, try again.
1149 disk_put_part(bdev->bd_part);
1150 bdev->bd_part = NULL;
1151 bdev->bd_disk = NULL;
1152 mutex_unlock(&bdev->bd_mutex);
1153 disk_unblock_events(disk);
1160 if (!ret && !bdev->bd_openers) {
1161 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
1162 bdi = blk_get_backing_dev_info(bdev);
1164 bdi = &default_backing_dev_info;
1165 bdev_inode_switch_bdi(bdev->bd_inode, bdi);
1169 * If the device is invalidated, rescan partition
1170 * if open succeeded or failed with -ENOMEDIUM.
1171 * The latter is necessary to prevent ghost
1172 * partitions on a removed medium.
1174 if (bdev->bd_invalidated) {
1176 rescan_partitions(disk, bdev);
1177 else if (ret == -ENOMEDIUM)
1178 invalidate_partitions(disk, bdev);
1183 struct block_device *whole;
1184 whole = bdget_disk(disk, 0);
1189 ret = __blkdev_get(whole, mode, 1);
1192 bdev->bd_contains = whole;
1193 bdev_inode_switch_bdi(bdev->bd_inode,
1194 whole->bd_inode->i_data.backing_dev_info);
1195 bdev->bd_part = disk_get_part(disk, partno);
1196 if (!(disk->flags & GENHD_FL_UP) ||
1197 !bdev->bd_part || !bdev->bd_part->nr_sects) {
1201 bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9);
1204 if (bdev->bd_contains == bdev) {
1206 if (bdev->bd_disk->fops->open)
1207 ret = bdev->bd_disk->fops->open(bdev, mode);
1208 /* the same as first opener case, read comment there */
1209 if (bdev->bd_invalidated) {
1211 rescan_partitions(bdev->bd_disk, bdev);
1212 else if (ret == -ENOMEDIUM)
1213 invalidate_partitions(bdev->bd_disk, bdev);
1216 goto out_unlock_bdev;
1218 /* only one opener holds refs to the module and disk */
1224 bdev->bd_part_count++;
1225 mutex_unlock(&bdev->bd_mutex);
1226 disk_unblock_events(disk);
1230 disk_put_part(bdev->bd_part);
1231 bdev->bd_disk = NULL;
1232 bdev->bd_part = NULL;
1233 bdev_inode_switch_bdi(bdev->bd_inode, &default_backing_dev_info);
1234 if (bdev != bdev->bd_contains)
1235 __blkdev_put(bdev->bd_contains, mode, 1);
1236 bdev->bd_contains = NULL;
1238 mutex_unlock(&bdev->bd_mutex);
1239 disk_unblock_events(disk);
1249 * blkdev_get - open a block device
1250 * @bdev: block_device to open
1251 * @mode: FMODE_* mask
1252 * @holder: exclusive holder identifier
1254 * Open @bdev with @mode. If @mode includes %FMODE_EXCL, @bdev is
1255 * open with exclusive access. Specifying %FMODE_EXCL with %NULL
1256 * @holder is invalid. Exclusive opens may nest for the same @holder.
1258 * On success, the reference count of @bdev is unchanged. On failure,
1265 * 0 on success, -errno on failure.
1267 int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder)
1269 struct block_device *whole = NULL;
1272 WARN_ON_ONCE((mode & FMODE_EXCL) && !holder);
1274 if ((mode & FMODE_EXCL) && holder) {
1275 whole = bd_start_claiming(bdev, holder);
1276 if (IS_ERR(whole)) {
1278 return PTR_ERR(whole);
1282 res = __blkdev_get(bdev, mode, 0);
1285 struct gendisk *disk = whole->bd_disk;
1287 /* finish claiming */
1288 mutex_lock(&bdev->bd_mutex);
1289 spin_lock(&bdev_lock);
1292 BUG_ON(!bd_may_claim(bdev, whole, holder));
1294 * Note that for a whole device bd_holders
1295 * will be incremented twice, and bd_holder
1296 * will be set to bd_may_claim before being
1299 whole->bd_holders++;
1300 whole->bd_holder = bd_may_claim;
1302 bdev->bd_holder = holder;
1305 /* tell others that we're done */
1306 BUG_ON(whole->bd_claiming != holder);
1307 whole->bd_claiming = NULL;
1308 wake_up_bit(&whole->bd_claiming, 0);
1310 spin_unlock(&bdev_lock);
1313 * Block event polling for write claims if requested. Any
1314 * write holder makes the write_holder state stick until
1315 * all are released. This is good enough and tracking
1316 * individual writeable reference is too fragile given the
1317 * way @mode is used in blkdev_get/put().
1319 if (!res && (mode & FMODE_WRITE) && !bdev->bd_write_holder &&
1320 (disk->flags & GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE)) {
1321 bdev->bd_write_holder = true;
1322 disk_block_events(disk);
1325 mutex_unlock(&bdev->bd_mutex);
1331 EXPORT_SYMBOL(blkdev_get);
1334 * blkdev_get_by_path - open a block device by name
1335 * @path: path to the block device to open
1336 * @mode: FMODE_* mask
1337 * @holder: exclusive holder identifier
1339 * Open the blockdevice described by the device file at @path. @mode
1340 * and @holder are identical to blkdev_get().
1342 * On success, the returned block_device has reference count of one.
1348 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1350 struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
1353 struct block_device *bdev;
1356 bdev = lookup_bdev(path);
1360 err = blkdev_get(bdev, mode, holder);
1362 return ERR_PTR(err);
1364 if ((mode & FMODE_WRITE) && bdev_read_only(bdev)) {
1365 blkdev_put(bdev, mode);
1366 return ERR_PTR(-EACCES);
1371 EXPORT_SYMBOL(blkdev_get_by_path);
1374 * blkdev_get_by_dev - open a block device by device number
1375 * @dev: device number of block device to open
1376 * @mode: FMODE_* mask
1377 * @holder: exclusive holder identifier
1379 * Open the blockdevice described by device number @dev. @mode and
1380 * @holder are identical to blkdev_get().
1382 * Use it ONLY if you really do not have anything better - i.e. when
1383 * you are behind a truly sucky interface and all you are given is a
1384 * device number. _Never_ to be used for internal purposes. If you
1385 * ever need it - reconsider your API.
1387 * On success, the returned block_device has reference count of one.
1393 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1395 struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
1397 struct block_device *bdev;
1402 return ERR_PTR(-ENOMEM);
1404 err = blkdev_get(bdev, mode, holder);
1406 return ERR_PTR(err);
1410 EXPORT_SYMBOL(blkdev_get_by_dev);
1412 static int blkdev_open(struct inode * inode, struct file * filp)
1414 struct block_device *bdev;
1417 * Preserve backwards compatibility and allow large file access
1418 * even if userspace doesn't ask for it explicitly. Some mkfs
1419 * binary needs it. We might want to drop this workaround
1420 * during an unstable branch.
1422 filp->f_flags |= O_LARGEFILE;
1424 if (filp->f_flags & O_NDELAY)
1425 filp->f_mode |= FMODE_NDELAY;
1426 if (filp->f_flags & O_EXCL)
1427 filp->f_mode |= FMODE_EXCL;
1428 if ((filp->f_flags & O_ACCMODE) == 3)
1429 filp->f_mode |= FMODE_WRITE_IOCTL;
1431 bdev = bd_acquire(inode);
1435 filp->f_mapping = bdev->bd_inode->i_mapping;
1437 return blkdev_get(bdev, filp->f_mode, filp);
1440 static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
1443 struct gendisk *disk = bdev->bd_disk;
1444 struct block_device *victim = NULL;
1446 mutex_lock_nested(&bdev->bd_mutex, for_part);
1448 bdev->bd_part_count--;
1450 if (!--bdev->bd_openers) {
1451 WARN_ON_ONCE(bdev->bd_holders);
1452 sync_blockdev(bdev);
1454 /* ->release can cause the old bdi to disappear,
1455 * so must switch it out first
1457 bdev_inode_switch_bdi(bdev->bd_inode,
1458 &default_backing_dev_info);
1460 if (bdev->bd_contains == bdev) {
1461 if (disk->fops->release)
1462 ret = disk->fops->release(disk, mode);
1464 if (!bdev->bd_openers) {
1465 struct module *owner = disk->fops->owner;
1467 disk_put_part(bdev->bd_part);
1468 bdev->bd_part = NULL;
1469 bdev->bd_disk = NULL;
1470 if (bdev != bdev->bd_contains)
1471 victim = bdev->bd_contains;
1472 bdev->bd_contains = NULL;
1477 mutex_unlock(&bdev->bd_mutex);
1480 __blkdev_put(victim, mode, 1);
1484 int blkdev_put(struct block_device *bdev, fmode_t mode)
1486 mutex_lock(&bdev->bd_mutex);
1488 if (mode & FMODE_EXCL) {
1492 * Release a claim on the device. The holder fields
1493 * are protected with bdev_lock. bd_mutex is to
1494 * synchronize disk_holder unlinking.
1496 spin_lock(&bdev_lock);
1498 WARN_ON_ONCE(--bdev->bd_holders < 0);
1499 WARN_ON_ONCE(--bdev->bd_contains->bd_holders < 0);
1501 /* bd_contains might point to self, check in a separate step */
1502 if ((bdev_free = !bdev->bd_holders))
1503 bdev->bd_holder = NULL;
1504 if (!bdev->bd_contains->bd_holders)
1505 bdev->bd_contains->bd_holder = NULL;
1507 spin_unlock(&bdev_lock);
1510 * If this was the last claim, remove holder link and
1511 * unblock evpoll if it was a write holder.
1513 if (bdev_free && bdev->bd_write_holder) {
1514 disk_unblock_events(bdev->bd_disk);
1515 bdev->bd_write_holder = false;
1520 * Trigger event checking and tell drivers to flush MEDIA_CHANGE
1521 * event. This is to ensure detection of media removal commanded
1522 * from userland - e.g. eject(1).
1524 disk_flush_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE);
1526 mutex_unlock(&bdev->bd_mutex);
1528 return __blkdev_put(bdev, mode, 0);
1530 EXPORT_SYMBOL(blkdev_put);
1532 static int blkdev_close(struct inode * inode, struct file * filp)
1534 struct block_device *bdev = I_BDEV(filp->f_mapping->host);
1536 return blkdev_put(bdev, filp->f_mode);
1539 static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1541 struct block_device *bdev = I_BDEV(file->f_mapping->host);
1542 fmode_t mode = file->f_mode;
1545 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
1546 * to updated it before every ioctl.
1548 if (file->f_flags & O_NDELAY)
1549 mode |= FMODE_NDELAY;
1551 mode &= ~FMODE_NDELAY;
1553 return blkdev_ioctl(bdev, mode, cmd, arg);
1557 * Write data to the block device. Only intended for the block device itself
1558 * and the raw driver which basically is a fake block device.
1560 * Does not take i_mutex for the write and thus is not for general purpose
1563 ssize_t blkdev_aio_write(struct kiocb *iocb, const struct iovec *iov,
1564 unsigned long nr_segs, loff_t pos)
1566 struct file *file = iocb->ki_filp;
1569 BUG_ON(iocb->ki_pos != pos);
1571 ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos);
1572 if (ret > 0 || ret == -EIOCBQUEUED) {
1575 err = generic_write_sync(file, pos, ret);
1576 if (err < 0 && ret > 0)
1581 EXPORT_SYMBOL_GPL(blkdev_aio_write);
1584 * Try to release a page associated with block device when the system
1585 * is under memory pressure.
1587 static int blkdev_releasepage(struct page *page, gfp_t wait)
1589 struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
1591 if (super && super->s_op->bdev_try_to_free_page)
1592 return super->s_op->bdev_try_to_free_page(super, page, wait);
1594 return try_to_free_buffers(page);
1597 static const struct address_space_operations def_blk_aops = {
1598 .readpage = blkdev_readpage,
1599 .writepage = blkdev_writepage,
1600 .write_begin = blkdev_write_begin,
1601 .write_end = blkdev_write_end,
1602 .writepages = generic_writepages,
1603 .releasepage = blkdev_releasepage,
1604 .direct_IO = blkdev_direct_IO,
1607 const struct file_operations def_blk_fops = {
1608 .open = blkdev_open,
1609 .release = blkdev_close,
1610 .llseek = block_llseek,
1611 .read = do_sync_read,
1612 .write = do_sync_write,
1613 .aio_read = generic_file_aio_read,
1614 .aio_write = blkdev_aio_write,
1615 .mmap = generic_file_mmap,
1616 .fsync = blkdev_fsync,
1617 .unlocked_ioctl = block_ioctl,
1618 #ifdef CONFIG_COMPAT
1619 .compat_ioctl = compat_blkdev_ioctl,
1621 .splice_read = generic_file_splice_read,
1622 .splice_write = generic_file_splice_write,
1625 int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
1628 mm_segment_t old_fs = get_fs();
1630 res = blkdev_ioctl(bdev, 0, cmd, arg);
1635 EXPORT_SYMBOL(ioctl_by_bdev);
1638 * lookup_bdev - lookup a struct block_device by name
1639 * @pathname: special file representing the block device
1641 * Get a reference to the blockdevice at @pathname in the current
1642 * namespace if possible and return it. Return ERR_PTR(error)
1645 struct block_device *lookup_bdev(const char *pathname)
1647 struct block_device *bdev;
1648 struct inode *inode;
1652 if (!pathname || !*pathname)
1653 return ERR_PTR(-EINVAL);
1655 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
1657 return ERR_PTR(error);
1659 inode = path.dentry->d_inode;
1661 if (!S_ISBLK(inode->i_mode))
1664 if (path.mnt->mnt_flags & MNT_NODEV)
1667 bdev = bd_acquire(inode);
1674 bdev = ERR_PTR(error);
1677 EXPORT_SYMBOL(lookup_bdev);
1679 int __invalidate_device(struct block_device *bdev, bool kill_dirty)
1681 struct super_block *sb = get_super(bdev);
1686 * no need to lock the super, get_super holds the
1687 * read mutex so the filesystem cannot go away
1688 * under us (->put_super runs with the write lock
1691 shrink_dcache_sb(sb);
1692 res = invalidate_inodes(sb, kill_dirty);
1695 invalidate_bdev(bdev);
1698 EXPORT_SYMBOL(__invalidate_device);