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/smp_lock.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/mpage.h>
21 #include <linux/mount.h>
22 #include <linux/uio.h>
23 #include <linux/namei.h>
24 #include <asm/uaccess.h>
28 struct block_device bdev;
29 struct inode vfs_inode;
32 static inline struct bdev_inode *BDEV_I(struct inode *inode)
34 return container_of(inode, struct bdev_inode, vfs_inode);
37 inline struct block_device *I_BDEV(struct inode *inode)
39 return &BDEV_I(inode)->bdev;
42 EXPORT_SYMBOL(I_BDEV);
44 static sector_t max_block(struct block_device *bdev)
46 sector_t retval = ~((sector_t)0);
47 loff_t sz = i_size_read(bdev->bd_inode);
50 unsigned int size = block_size(bdev);
51 unsigned int sizebits = blksize_bits(size);
52 retval = (sz >> sizebits);
57 /* Kill _all_ buffers, dirty or not.. */
58 static void kill_bdev(struct block_device *bdev)
60 invalidate_bdev(bdev, 1);
61 truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
64 int set_blocksize(struct block_device *bdev, int size)
66 /* Size must be a power of two, and between 512 and PAGE_SIZE */
67 if (size > PAGE_SIZE || size < 512 || (size & (size-1)))
70 /* Size cannot be smaller than the size supported by the device */
71 if (size < bdev_hardsect_size(bdev))
74 /* Don't change the size if it is same as current */
75 if (bdev->bd_block_size != size) {
77 bdev->bd_block_size = size;
78 bdev->bd_inode->i_blkbits = blksize_bits(size);
84 EXPORT_SYMBOL(set_blocksize);
86 int sb_set_blocksize(struct super_block *sb, int size)
88 if (set_blocksize(sb->s_bdev, size))
90 /* If we get here, we know size is power of two
91 * and it's value is between 512 and PAGE_SIZE */
92 sb->s_blocksize = size;
93 sb->s_blocksize_bits = blksize_bits(size);
94 return sb->s_blocksize;
97 EXPORT_SYMBOL(sb_set_blocksize);
99 int sb_min_blocksize(struct super_block *sb, int size)
101 int minsize = bdev_hardsect_size(sb->s_bdev);
104 return sb_set_blocksize(sb, size);
107 EXPORT_SYMBOL(sb_min_blocksize);
110 blkdev_get_block(struct inode *inode, sector_t iblock,
111 struct buffer_head *bh, int create)
113 if (iblock >= max_block(I_BDEV(inode))) {
118 * for reads, we're just trying to fill a partial page.
119 * return a hole, they will have to call get_block again
120 * before they can fill it, and they will get -EIO at that
125 bh->b_bdev = I_BDEV(inode);
126 bh->b_blocknr = iblock;
127 set_buffer_mapped(bh);
132 blkdev_get_blocks(struct inode *inode, sector_t iblock,
133 struct buffer_head *bh, int create)
135 sector_t end_block = max_block(I_BDEV(inode));
136 unsigned long max_blocks = bh->b_size >> inode->i_blkbits;
138 if ((iblock + max_blocks) > end_block) {
139 max_blocks = end_block - iblock;
140 if ((long)max_blocks <= 0) {
142 return -EIO; /* write fully beyond EOF */
144 * It is a read which is fully beyond EOF. We return
145 * a !buffer_mapped buffer
151 bh->b_bdev = I_BDEV(inode);
152 bh->b_blocknr = iblock;
153 bh->b_size = max_blocks << inode->i_blkbits;
155 set_buffer_mapped(bh);
160 blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
161 loff_t offset, unsigned long nr_segs)
163 struct file *file = iocb->ki_filp;
164 struct inode *inode = file->f_mapping->host;
166 return blockdev_direct_IO_no_locking(rw, iocb, inode, I_BDEV(inode),
167 iov, offset, nr_segs, blkdev_get_blocks, NULL);
170 static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
172 return block_write_full_page(page, blkdev_get_block, wbc);
175 static int blkdev_readpage(struct file * file, struct page * page)
177 return block_read_full_page(page, blkdev_get_block);
180 static int blkdev_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
182 return block_prepare_write(page, from, to, blkdev_get_block);
185 static int blkdev_commit_write(struct file *file, struct page *page, unsigned from, unsigned to)
187 return block_commit_write(page, from, to);
192 * for a block special file file->f_dentry->d_inode->i_size is zero
193 * so we compute the size by hand (just as in block_read/write above)
195 static loff_t block_llseek(struct file *file, loff_t offset, int origin)
197 struct inode *bd_inode = file->f_mapping->host;
201 mutex_lock(&bd_inode->i_mutex);
202 size = i_size_read(bd_inode);
209 offset += file->f_pos;
212 if (offset >= 0 && offset <= size) {
213 if (offset != file->f_pos) {
214 file->f_pos = offset;
218 mutex_unlock(&bd_inode->i_mutex);
223 * Filp is never NULL; the only case when ->fsync() is called with
224 * NULL first argument is nfsd_sync_dir() and that's not a directory.
227 static int block_fsync(struct file *filp, struct dentry *dentry, int datasync)
229 return sync_blockdev(I_BDEV(filp->f_mapping->host));
236 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
237 static kmem_cache_t * bdev_cachep __read_mostly;
239 static struct inode *bdev_alloc_inode(struct super_block *sb)
241 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, SLAB_KERNEL);
244 return &ei->vfs_inode;
247 static void bdev_destroy_inode(struct inode *inode)
249 struct bdev_inode *bdi = BDEV_I(inode);
251 bdi->bdev.bd_inode_backing_dev_info = NULL;
252 kmem_cache_free(bdev_cachep, bdi);
255 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
257 struct bdev_inode *ei = (struct bdev_inode *) foo;
258 struct block_device *bdev = &ei->bdev;
260 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
261 SLAB_CTOR_CONSTRUCTOR)
263 memset(bdev, 0, sizeof(*bdev));
264 mutex_init(&bdev->bd_mutex);
265 mutex_init(&bdev->bd_mount_mutex);
266 INIT_LIST_HEAD(&bdev->bd_inodes);
267 INIT_LIST_HEAD(&bdev->bd_list);
269 INIT_LIST_HEAD(&bdev->bd_holder_list);
271 inode_init_once(&ei->vfs_inode);
275 static inline void __bd_forget(struct inode *inode)
277 list_del_init(&inode->i_devices);
278 inode->i_bdev = NULL;
279 inode->i_mapping = &inode->i_data;
282 static void bdev_clear_inode(struct inode *inode)
284 struct block_device *bdev = &BDEV_I(inode)->bdev;
286 spin_lock(&bdev_lock);
287 while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
288 __bd_forget(list_entry(p, struct inode, i_devices));
290 list_del_init(&bdev->bd_list);
291 spin_unlock(&bdev_lock);
294 static struct super_operations bdev_sops = {
295 .statfs = simple_statfs,
296 .alloc_inode = bdev_alloc_inode,
297 .destroy_inode = bdev_destroy_inode,
298 .drop_inode = generic_delete_inode,
299 .clear_inode = bdev_clear_inode,
302 static int bd_get_sb(struct file_system_type *fs_type,
303 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
305 return get_sb_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576, mnt);
308 static struct file_system_type bd_type = {
311 .kill_sb = kill_anon_super,
314 static struct vfsmount *bd_mnt __read_mostly;
315 struct super_block *blockdev_superblock;
317 void __init bdev_cache_init(void)
320 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
321 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
322 SLAB_MEM_SPREAD|SLAB_PANIC),
324 err = register_filesystem(&bd_type);
326 panic("Cannot register bdev pseudo-fs");
327 bd_mnt = kern_mount(&bd_type);
328 err = PTR_ERR(bd_mnt);
330 panic("Cannot create bdev pseudo-fs");
331 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
335 * Most likely _very_ bad one - but then it's hardly critical for small
336 * /dev and can be fixed when somebody will need really large one.
337 * Keep in mind that it will be fed through icache hash function too.
339 static inline unsigned long hash(dev_t dev)
341 return MAJOR(dev)+MINOR(dev);
344 static int bdev_test(struct inode *inode, void *data)
346 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
349 static int bdev_set(struct inode *inode, void *data)
351 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
355 static LIST_HEAD(all_bdevs);
357 struct block_device *bdget(dev_t dev)
359 struct block_device *bdev;
362 inode = iget5_locked(bd_mnt->mnt_sb, hash(dev),
363 bdev_test, bdev_set, &dev);
368 bdev = &BDEV_I(inode)->bdev;
370 if (inode->i_state & I_NEW) {
371 bdev->bd_contains = NULL;
372 bdev->bd_inode = inode;
373 bdev->bd_block_size = (1 << inode->i_blkbits);
374 bdev->bd_part_count = 0;
375 bdev->bd_invalidated = 0;
376 inode->i_mode = S_IFBLK;
378 inode->i_bdev = bdev;
379 inode->i_data.a_ops = &def_blk_aops;
380 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
381 inode->i_data.backing_dev_info = &default_backing_dev_info;
382 spin_lock(&bdev_lock);
383 list_add(&bdev->bd_list, &all_bdevs);
384 spin_unlock(&bdev_lock);
385 unlock_new_inode(inode);
390 EXPORT_SYMBOL(bdget);
392 long nr_blockdev_pages(void)
396 spin_lock(&bdev_lock);
397 list_for_each(p, &all_bdevs) {
398 struct block_device *bdev;
399 bdev = list_entry(p, struct block_device, bd_list);
400 ret += bdev->bd_inode->i_mapping->nrpages;
402 spin_unlock(&bdev_lock);
406 void bdput(struct block_device *bdev)
408 iput(bdev->bd_inode);
411 EXPORT_SYMBOL(bdput);
413 static struct block_device *bd_acquire(struct inode *inode)
415 struct block_device *bdev;
417 spin_lock(&bdev_lock);
418 bdev = inode->i_bdev;
420 atomic_inc(&bdev->bd_inode->i_count);
421 spin_unlock(&bdev_lock);
424 spin_unlock(&bdev_lock);
426 bdev = bdget(inode->i_rdev);
428 spin_lock(&bdev_lock);
429 if (!inode->i_bdev) {
431 * We take an additional bd_inode->i_count for inode,
432 * and it's released in clear_inode() of inode.
433 * So, we can access it via ->i_mapping always
436 atomic_inc(&bdev->bd_inode->i_count);
437 inode->i_bdev = bdev;
438 inode->i_mapping = bdev->bd_inode->i_mapping;
439 list_add(&inode->i_devices, &bdev->bd_inodes);
441 spin_unlock(&bdev_lock);
446 /* Call when you free inode */
448 void bd_forget(struct inode *inode)
450 struct block_device *bdev = NULL;
452 spin_lock(&bdev_lock);
454 if (inode->i_sb != blockdev_superblock)
455 bdev = inode->i_bdev;
458 spin_unlock(&bdev_lock);
461 iput(bdev->bd_inode);
464 int bd_claim(struct block_device *bdev, void *holder)
467 spin_lock(&bdev_lock);
469 /* first decide result */
470 if (bdev->bd_holder == holder)
471 res = 0; /* already a holder */
472 else if (bdev->bd_holder != NULL)
473 res = -EBUSY; /* held by someone else */
474 else if (bdev->bd_contains == bdev)
475 res = 0; /* is a whole device which isn't held */
477 else if (bdev->bd_contains->bd_holder == bd_claim)
478 res = 0; /* is a partition of a device that is being partitioned */
479 else if (bdev->bd_contains->bd_holder != NULL)
480 res = -EBUSY; /* is a partition of a held device */
482 res = 0; /* is a partition of an un-held device */
484 /* now impose change */
486 /* note that for a whole device bd_holders
487 * will be incremented twice, and bd_holder will
488 * be set to bd_claim before being set to holder
490 bdev->bd_contains->bd_holders ++;
491 bdev->bd_contains->bd_holder = bd_claim;
493 bdev->bd_holder = holder;
495 spin_unlock(&bdev_lock);
499 EXPORT_SYMBOL(bd_claim);
501 void bd_release(struct block_device *bdev)
503 spin_lock(&bdev_lock);
504 if (!--bdev->bd_contains->bd_holders)
505 bdev->bd_contains->bd_holder = NULL;
506 if (!--bdev->bd_holders)
507 bdev->bd_holder = NULL;
508 spin_unlock(&bdev_lock);
511 EXPORT_SYMBOL(bd_release);
515 * Functions for bd_claim_by_kobject / bd_release_from_kobject
517 * If a kobject is passed to bd_claim_by_kobject()
518 * and the kobject has a parent directory,
519 * following symlinks are created:
520 * o from the kobject to the claimed bdev
521 * o from "holders" directory of the bdev to the parent of the kobject
522 * bd_release_from_kobject() removes these symlinks.
525 * If /dev/dm-0 maps to /dev/sda, kobject corresponding to
526 * /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then:
527 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
528 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
531 static struct kobject *bdev_get_kobj(struct block_device *bdev)
533 if (bdev->bd_contains != bdev)
534 return kobject_get(&bdev->bd_part->kobj);
536 return kobject_get(&bdev->bd_disk->kobj);
539 static struct kobject *bdev_get_holder(struct block_device *bdev)
541 if (bdev->bd_contains != bdev)
542 return kobject_get(bdev->bd_part->holder_dir);
544 return kobject_get(bdev->bd_disk->holder_dir);
547 static int add_symlink(struct kobject *from, struct kobject *to)
551 return sysfs_create_link(from, to, kobject_name(to));
554 static void del_symlink(struct kobject *from, struct kobject *to)
558 sysfs_remove_link(from, kobject_name(to));
562 * 'struct bd_holder' contains pointers to kobjects symlinked by
563 * bd_claim_by_kobject.
564 * It's connected to bd_holder_list which is protected by bdev->bd_sem.
567 struct list_head list; /* chain of holders of the bdev */
568 int count; /* references from the holder */
569 struct kobject *sdir; /* holder object, e.g. "/block/dm-0/slaves" */
570 struct kobject *hdev; /* e.g. "/block/dm-0" */
571 struct kobject *hdir; /* e.g. "/block/sda/holders" */
572 struct kobject *sdev; /* e.g. "/block/sda" */
576 * Get references of related kobjects at once.
577 * Returns 1 on success. 0 on failure.
579 * Should call bd_holder_release_dirs() after successful use.
581 static int bd_holder_grab_dirs(struct block_device *bdev,
582 struct bd_holder *bo)
587 bo->sdir = kobject_get(bo->sdir);
591 bo->hdev = kobject_get(bo->sdir->parent);
595 bo->sdev = bdev_get_kobj(bdev);
599 bo->hdir = bdev_get_holder(bdev);
606 kobject_put(bo->sdev);
608 kobject_put(bo->hdev);
610 kobject_put(bo->sdir);
615 /* Put references of related kobjects at once. */
616 static void bd_holder_release_dirs(struct bd_holder *bo)
618 kobject_put(bo->hdir);
619 kobject_put(bo->sdev);
620 kobject_put(bo->hdev);
621 kobject_put(bo->sdir);
624 static struct bd_holder *alloc_bd_holder(struct kobject *kobj)
626 struct bd_holder *bo;
628 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
638 static void free_bd_holder(struct bd_holder *bo)
644 * add_bd_holder - create sysfs symlinks for bd_claim() relationship
646 * @bdev: block device to be bd_claimed
647 * @bo: preallocated and initialized by alloc_bd_holder()
649 * If there is no matching entry with @bo in @bdev->bd_holder_list,
650 * add @bo to the list, create symlinks.
652 * Returns 0 if symlinks are created or already there.
653 * Returns -ve if something fails and @bo can be freed.
655 static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
657 struct bd_holder *tmp;
663 list_for_each_entry(tmp, &bdev->bd_holder_list, list) {
664 if (tmp->sdir == bo->sdir) {
666 /* We've already done what we need to do here. */
672 if (!bd_holder_grab_dirs(bdev, bo))
675 ret = add_symlink(bo->sdir, bo->sdev);
677 ret = add_symlink(bo->hdir, bo->hdev);
679 del_symlink(bo->sdir, bo->sdev);
682 list_add_tail(&bo->list, &bdev->bd_holder_list);
687 * del_bd_holder - delete sysfs symlinks for bd_claim() relationship
689 * @bdev: block device to be bd_claimed
690 * @kobj: holder's kobject
692 * If there is matching entry with @kobj in @bdev->bd_holder_list
693 * and no other bd_claim() from the same kobject,
694 * remove the struct bd_holder from the list, delete symlinks for it.
696 * Returns a pointer to the struct bd_holder when it's removed from the list
697 * and ready to be freed.
698 * Returns NULL if matching claim isn't found or there is other bd_claim()
699 * by the same kobject.
701 static struct bd_holder *del_bd_holder(struct block_device *bdev,
702 struct kobject *kobj)
704 struct bd_holder *bo;
706 list_for_each_entry(bo, &bdev->bd_holder_list, list) {
707 if (bo->sdir == kobj) {
709 BUG_ON(bo->count < 0);
712 del_symlink(bo->sdir, bo->sdev);
713 del_symlink(bo->hdir, bo->hdev);
714 bd_holder_release_dirs(bo);
725 * bd_claim_by_kobject - bd_claim() with additional kobject signature
727 * @bdev: block device to be claimed
728 * @holder: holder's signature
729 * @kobj: holder's kobject
731 * Do bd_claim() and if it succeeds, create sysfs symlinks between
732 * the bdev and the holder's kobject.
733 * Use bd_release_from_kobject() when relesing the claimed bdev.
735 * Returns 0 on success. (same as bd_claim())
736 * Returns errno on failure.
738 static int bd_claim_by_kobject(struct block_device *bdev, void *holder,
739 struct kobject *kobj)
742 struct bd_holder *bo;
747 bo = alloc_bd_holder(kobj);
751 mutex_lock_nested(&bdev->bd_mutex, BD_MUTEX_PARTITION);
752 res = bd_claim(bdev, holder);
754 res = add_bd_holder(bdev, bo);
757 mutex_unlock(&bdev->bd_mutex);
763 * bd_release_from_kobject - bd_release() with additional kobject signature
765 * @bdev: block device to be released
766 * @kobj: holder's kobject
768 * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject().
770 static void bd_release_from_kobject(struct block_device *bdev,
771 struct kobject *kobj)
773 struct bd_holder *bo;
778 mutex_lock_nested(&bdev->bd_mutex, BD_MUTEX_PARTITION);
780 if ((bo = del_bd_holder(bdev, kobj)))
782 mutex_unlock(&bdev->bd_mutex);
786 * bd_claim_by_disk - wrapper function for bd_claim_by_kobject()
788 * @bdev: block device to be claimed
789 * @holder: holder's signature
790 * @disk: holder's gendisk
792 * Call bd_claim_by_kobject() with getting @disk->slave_dir.
794 int bd_claim_by_disk(struct block_device *bdev, void *holder,
795 struct gendisk *disk)
797 return bd_claim_by_kobject(bdev, holder, kobject_get(disk->slave_dir));
799 EXPORT_SYMBOL_GPL(bd_claim_by_disk);
802 * bd_release_from_disk - wrapper function for bd_release_from_kobject()
804 * @bdev: block device to be claimed
805 * @disk: holder's gendisk
807 * Call bd_release_from_kobject() and put @disk->slave_dir.
809 void bd_release_from_disk(struct block_device *bdev, struct gendisk *disk)
811 bd_release_from_kobject(bdev, disk->slave_dir);
812 kobject_put(disk->slave_dir);
814 EXPORT_SYMBOL_GPL(bd_release_from_disk);
818 * Tries to open block device by device number. Use it ONLY if you
819 * really do not have anything better - i.e. when you are behind a
820 * truly sucky interface and all you are given is a device number. _Never_
821 * to be used for internal purposes. If you ever need it - reconsider
824 struct block_device *open_by_devnum(dev_t dev, unsigned mode)
826 struct block_device *bdev = bdget(dev);
828 int flags = mode & FMODE_WRITE ? O_RDWR : O_RDONLY;
830 err = blkdev_get(bdev, mode, flags);
831 return err ? ERR_PTR(err) : bdev;
834 EXPORT_SYMBOL(open_by_devnum);
837 blkdev_get_partition(struct block_device *bdev, mode_t mode, unsigned flags);
839 struct block_device *open_partition_by_devnum(dev_t dev, unsigned mode)
841 struct block_device *bdev = bdget(dev);
843 int flags = mode & FMODE_WRITE ? O_RDWR : O_RDONLY;
845 err = blkdev_get_partition(bdev, mode, flags);
846 return err ? ERR_PTR(err) : bdev;
849 EXPORT_SYMBOL(open_partition_by_devnum);
853 * This routine checks whether a removable media has been changed,
854 * and invalidates all buffer-cache-entries in that case. This
855 * is a relatively slow routine, so we have to try to minimize using
856 * it. Thus it is called only upon a 'mount' or 'open'. This
857 * is the best way of combining speed and utility, I think.
858 * People changing diskettes in the middle of an operation deserve
861 int check_disk_change(struct block_device *bdev)
863 struct gendisk *disk = bdev->bd_disk;
864 struct block_device_operations * bdops = disk->fops;
866 if (!bdops->media_changed)
868 if (!bdops->media_changed(bdev->bd_disk))
871 if (__invalidate_device(bdev))
872 printk("VFS: busy inodes on changed media.\n");
874 if (bdops->revalidate_disk)
875 bdops->revalidate_disk(bdev->bd_disk);
876 if (bdev->bd_disk->minors > 1)
877 bdev->bd_invalidated = 1;
881 EXPORT_SYMBOL(check_disk_change);
883 void bd_set_size(struct block_device *bdev, loff_t size)
885 unsigned bsize = bdev_hardsect_size(bdev);
887 bdev->bd_inode->i_size = size;
888 while (bsize < PAGE_CACHE_SIZE) {
893 bdev->bd_block_size = bsize;
894 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
896 EXPORT_SYMBOL(bd_set_size);
898 static int __blkdev_put(struct block_device *bdev, unsigned int subclass)
901 struct inode *bd_inode = bdev->bd_inode;
902 struct gendisk *disk = bdev->bd_disk;
904 mutex_lock_nested(&bdev->bd_mutex, subclass);
906 if (!--bdev->bd_openers) {
910 if (bdev->bd_contains == bdev) {
911 if (disk->fops->release)
912 ret = disk->fops->release(bd_inode, NULL);
914 mutex_lock_nested(&bdev->bd_contains->bd_mutex,
916 bdev->bd_contains->bd_part_count--;
917 mutex_unlock(&bdev->bd_contains->bd_mutex);
919 if (!bdev->bd_openers) {
920 struct module *owner = disk->fops->owner;
925 if (bdev->bd_contains != bdev) {
926 kobject_put(&bdev->bd_part->kobj);
927 bdev->bd_part = NULL;
929 bdev->bd_disk = NULL;
930 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
931 if (bdev != bdev->bd_contains)
932 __blkdev_put(bdev->bd_contains, subclass + 1);
933 bdev->bd_contains = NULL;
936 mutex_unlock(&bdev->bd_mutex);
941 int blkdev_put(struct block_device *bdev)
943 return __blkdev_put(bdev, BD_MUTEX_NORMAL);
945 EXPORT_SYMBOL(blkdev_put);
947 int blkdev_put_partition(struct block_device *bdev)
949 return __blkdev_put(bdev, BD_MUTEX_PARTITION);
951 EXPORT_SYMBOL(blkdev_put_partition);
954 blkdev_get_whole(struct block_device *bdev, mode_t mode, unsigned flags);
957 do_open(struct block_device *bdev, struct file *file, unsigned int subclass)
959 struct module *owner = NULL;
960 struct gendisk *disk;
964 file->f_mapping = bdev->bd_inode->i_mapping;
966 disk = get_gendisk(bdev->bd_dev, &part);
972 owner = disk->fops->owner;
974 mutex_lock_nested(&bdev->bd_mutex, subclass);
976 if (!bdev->bd_openers) {
977 bdev->bd_disk = disk;
978 bdev->bd_contains = bdev;
980 struct backing_dev_info *bdi;
981 if (disk->fops->open) {
982 ret = disk->fops->open(bdev->bd_inode, file);
986 if (!bdev->bd_openers) {
987 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
988 bdi = blk_get_backing_dev_info(bdev);
990 bdi = &default_backing_dev_info;
991 bdev->bd_inode->i_data.backing_dev_info = bdi;
993 if (bdev->bd_invalidated)
994 rescan_partitions(disk, bdev);
997 struct block_device *whole;
998 whole = bdget_disk(disk, 0);
1002 ret = blkdev_get_whole(whole, file->f_mode, file->f_flags);
1005 bdev->bd_contains = whole;
1006 mutex_lock_nested(&whole->bd_mutex, BD_MUTEX_WHOLE);
1007 whole->bd_part_count++;
1008 p = disk->part[part - 1];
1009 bdev->bd_inode->i_data.backing_dev_info =
1010 whole->bd_inode->i_data.backing_dev_info;
1011 if (!(disk->flags & GENHD_FL_UP) || !p || !p->nr_sects) {
1012 whole->bd_part_count--;
1013 mutex_unlock(&whole->bd_mutex);
1017 kobject_get(&p->kobj);
1019 bd_set_size(bdev, (loff_t) p->nr_sects << 9);
1020 mutex_unlock(&whole->bd_mutex);
1025 if (bdev->bd_contains == bdev) {
1026 if (bdev->bd_disk->fops->open) {
1027 ret = bdev->bd_disk->fops->open(bdev->bd_inode, file);
1031 if (bdev->bd_invalidated)
1032 rescan_partitions(bdev->bd_disk, bdev);
1034 mutex_lock_nested(&bdev->bd_contains->bd_mutex,
1036 bdev->bd_contains->bd_part_count++;
1037 mutex_unlock(&bdev->bd_contains->bd_mutex);
1041 mutex_unlock(&bdev->bd_mutex);
1046 bdev->bd_disk = NULL;
1047 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
1048 if (bdev != bdev->bd_contains)
1049 __blkdev_put(bdev->bd_contains, BD_MUTEX_WHOLE);
1050 bdev->bd_contains = NULL;
1054 mutex_unlock(&bdev->bd_mutex);
1061 int blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags)
1064 * This crockload is due to bad choice of ->open() type.
1066 * For now, block device ->open() routine must _not_
1067 * examine anything in 'inode' argument except ->i_rdev.
1069 struct file fake_file = {};
1070 struct dentry fake_dentry = {};
1071 fake_file.f_mode = mode;
1072 fake_file.f_flags = flags;
1073 fake_file.f_dentry = &fake_dentry;
1074 fake_dentry.d_inode = bdev->bd_inode;
1076 return do_open(bdev, &fake_file, BD_MUTEX_NORMAL);
1079 EXPORT_SYMBOL(blkdev_get);
1082 blkdev_get_whole(struct block_device *bdev, mode_t mode, unsigned flags)
1085 * This crockload is due to bad choice of ->open() type.
1087 * For now, block device ->open() routine must _not_
1088 * examine anything in 'inode' argument except ->i_rdev.
1090 struct file fake_file = {};
1091 struct dentry fake_dentry = {};
1092 fake_file.f_mode = mode;
1093 fake_file.f_flags = flags;
1094 fake_file.f_dentry = &fake_dentry;
1095 fake_dentry.d_inode = bdev->bd_inode;
1097 return do_open(bdev, &fake_file, BD_MUTEX_WHOLE);
1101 blkdev_get_partition(struct block_device *bdev, mode_t mode, unsigned flags)
1104 * This crockload is due to bad choice of ->open() type.
1106 * For now, block device ->open() routine must _not_
1107 * examine anything in 'inode' argument except ->i_rdev.
1109 struct file fake_file = {};
1110 struct dentry fake_dentry = {};
1111 fake_file.f_mode = mode;
1112 fake_file.f_flags = flags;
1113 fake_file.f_dentry = &fake_dentry;
1114 fake_dentry.d_inode = bdev->bd_inode;
1116 return do_open(bdev, &fake_file, BD_MUTEX_PARTITION);
1119 static int blkdev_open(struct inode * inode, struct file * filp)
1121 struct block_device *bdev;
1125 * Preserve backwards compatibility and allow large file access
1126 * even if userspace doesn't ask for it explicitly. Some mkfs
1127 * binary needs it. We might want to drop this workaround
1128 * during an unstable branch.
1130 filp->f_flags |= O_LARGEFILE;
1132 bdev = bd_acquire(inode);
1134 res = do_open(bdev, filp, BD_MUTEX_NORMAL);
1138 if (!(filp->f_flags & O_EXCL) )
1141 if (!(res = bd_claim(bdev, filp)))
1148 static int blkdev_close(struct inode * inode, struct file * filp)
1150 struct block_device *bdev = I_BDEV(filp->f_mapping->host);
1151 if (bdev->bd_holder == filp)
1153 return blkdev_put(bdev);
1156 static ssize_t blkdev_file_write(struct file *file, const char __user *buf,
1157 size_t count, loff_t *ppos)
1159 struct iovec local_iov = { .iov_base = (void __user *)buf, .iov_len = count };
1161 return generic_file_write_nolock(file, &local_iov, 1, ppos);
1164 static ssize_t blkdev_file_aio_write(struct kiocb *iocb, const char __user *buf,
1165 size_t count, loff_t pos)
1167 struct iovec local_iov = { .iov_base = (void __user *)buf, .iov_len = count };
1169 return generic_file_aio_write_nolock(iocb, &local_iov, 1, &iocb->ki_pos);
1172 static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1174 return blkdev_ioctl(file->f_mapping->host, file, cmd, arg);
1177 const struct address_space_operations def_blk_aops = {
1178 .readpage = blkdev_readpage,
1179 .writepage = blkdev_writepage,
1180 .sync_page = block_sync_page,
1181 .prepare_write = blkdev_prepare_write,
1182 .commit_write = blkdev_commit_write,
1183 .writepages = generic_writepages,
1184 .direct_IO = blkdev_direct_IO,
1187 const struct file_operations def_blk_fops = {
1188 .open = blkdev_open,
1189 .release = blkdev_close,
1190 .llseek = block_llseek,
1191 .read = generic_file_read,
1192 .write = blkdev_file_write,
1193 .aio_read = generic_file_aio_read,
1194 .aio_write = blkdev_file_aio_write,
1195 .mmap = generic_file_mmap,
1196 .fsync = block_fsync,
1197 .unlocked_ioctl = block_ioctl,
1198 #ifdef CONFIG_COMPAT
1199 .compat_ioctl = compat_blkdev_ioctl,
1201 .readv = generic_file_readv,
1202 .writev = generic_file_write_nolock,
1203 .sendfile = generic_file_sendfile,
1204 .splice_read = generic_file_splice_read,
1205 .splice_write = generic_file_splice_write,
1208 int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
1211 mm_segment_t old_fs = get_fs();
1213 res = blkdev_ioctl(bdev->bd_inode, NULL, cmd, arg);
1218 EXPORT_SYMBOL(ioctl_by_bdev);
1221 * lookup_bdev - lookup a struct block_device by name
1223 * @path: special file representing the block device
1225 * Get a reference to the blockdevice at @path in the current
1226 * namespace if possible and return it. Return ERR_PTR(error)
1229 struct block_device *lookup_bdev(const char *path)
1231 struct block_device *bdev;
1232 struct inode *inode;
1233 struct nameidata nd;
1236 if (!path || !*path)
1237 return ERR_PTR(-EINVAL);
1239 error = path_lookup(path, LOOKUP_FOLLOW, &nd);
1241 return ERR_PTR(error);
1243 inode = nd.dentry->d_inode;
1245 if (!S_ISBLK(inode->i_mode))
1248 if (nd.mnt->mnt_flags & MNT_NODEV)
1251 bdev = bd_acquire(inode);
1258 bdev = ERR_PTR(error);
1263 * open_bdev_excl - open a block device by name and set it up for use
1265 * @path: special file representing the block device
1266 * @flags: %MS_RDONLY for opening read-only
1267 * @holder: owner for exclusion
1269 * Open the blockdevice described by the special file at @path, claim it
1272 struct block_device *open_bdev_excl(const char *path, int flags, void *holder)
1274 struct block_device *bdev;
1275 mode_t mode = FMODE_READ;
1278 bdev = lookup_bdev(path);
1282 if (!(flags & MS_RDONLY))
1283 mode |= FMODE_WRITE;
1284 error = blkdev_get(bdev, mode, 0);
1286 return ERR_PTR(error);
1288 if (!(flags & MS_RDONLY) && bdev_read_only(bdev))
1290 error = bd_claim(bdev, holder);
1298 return ERR_PTR(error);
1301 EXPORT_SYMBOL(open_bdev_excl);
1304 * close_bdev_excl - release a blockdevice openen by open_bdev_excl()
1306 * @bdev: blockdevice to close
1308 * This is the counterpart to open_bdev_excl().
1310 void close_bdev_excl(struct block_device *bdev)
1316 EXPORT_SYMBOL(close_bdev_excl);