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>
27 struct block_device bdev;
28 struct inode vfs_inode;
31 static inline struct bdev_inode *BDEV_I(struct inode *inode)
33 return container_of(inode, struct bdev_inode, vfs_inode);
36 inline struct block_device *I_BDEV(struct inode *inode)
38 return &BDEV_I(inode)->bdev;
41 EXPORT_SYMBOL(I_BDEV);
43 static sector_t max_block(struct block_device *bdev)
45 sector_t retval = ~((sector_t)0);
46 loff_t sz = i_size_read(bdev->bd_inode);
49 unsigned int size = block_size(bdev);
50 unsigned int sizebits = blksize_bits(size);
51 retval = (sz >> sizebits);
56 /* Kill _all_ buffers, dirty or not.. */
57 static void kill_bdev(struct block_device *bdev)
59 invalidate_bdev(bdev, 1);
60 truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
63 int set_blocksize(struct block_device *bdev, int size)
65 /* Size must be a power of two, and between 512 and PAGE_SIZE */
66 if (size > PAGE_SIZE || size < 512 || (size & (size-1)))
69 /* Size cannot be smaller than the size supported by the device */
70 if (size < bdev_hardsect_size(bdev))
73 /* Don't change the size if it is same as current */
74 if (bdev->bd_block_size != size) {
76 bdev->bd_block_size = size;
77 bdev->bd_inode->i_blkbits = blksize_bits(size);
83 EXPORT_SYMBOL(set_blocksize);
85 int sb_set_blocksize(struct super_block *sb, int size)
87 if (set_blocksize(sb->s_bdev, size))
89 /* If we get here, we know size is power of two
90 * and it's value is between 512 and PAGE_SIZE */
91 sb->s_blocksize = size;
92 sb->s_blocksize_bits = blksize_bits(size);
93 return sb->s_blocksize;
96 EXPORT_SYMBOL(sb_set_blocksize);
98 int sb_min_blocksize(struct super_block *sb, int size)
100 int minsize = bdev_hardsect_size(sb->s_bdev);
103 return sb_set_blocksize(sb, size);
106 EXPORT_SYMBOL(sb_min_blocksize);
109 blkdev_get_block(struct inode *inode, sector_t iblock,
110 struct buffer_head *bh, int create)
112 if (iblock >= max_block(I_BDEV(inode))) {
117 * for reads, we're just trying to fill a partial page.
118 * return a hole, they will have to call get_block again
119 * before they can fill it, and they will get -EIO at that
124 bh->b_bdev = I_BDEV(inode);
125 bh->b_blocknr = iblock;
126 set_buffer_mapped(bh);
131 blkdev_get_blocks(struct inode *inode, sector_t iblock,
132 struct buffer_head *bh, int create)
134 sector_t end_block = max_block(I_BDEV(inode));
135 unsigned long max_blocks = bh->b_size >> inode->i_blkbits;
137 if ((iblock + max_blocks) > end_block) {
138 max_blocks = end_block - iblock;
139 if ((long)max_blocks <= 0) {
141 return -EIO; /* write fully beyond EOF */
143 * It is a read which is fully beyond EOF. We return
144 * a !buffer_mapped buffer
150 bh->b_bdev = I_BDEV(inode);
151 bh->b_blocknr = iblock;
152 bh->b_size = max_blocks << inode->i_blkbits;
154 set_buffer_mapped(bh);
159 blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
160 loff_t offset, unsigned long nr_segs)
162 struct file *file = iocb->ki_filp;
163 struct inode *inode = file->f_mapping->host;
165 return blockdev_direct_IO_no_locking(rw, iocb, inode, I_BDEV(inode),
166 iov, offset, nr_segs, blkdev_get_blocks, NULL);
169 static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
171 return block_write_full_page(page, blkdev_get_block, wbc);
174 static int blkdev_readpage(struct file * file, struct page * page)
176 return block_read_full_page(page, blkdev_get_block);
179 static int blkdev_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
181 return block_prepare_write(page, from, to, blkdev_get_block);
184 static int blkdev_commit_write(struct file *file, struct page *page, unsigned from, unsigned to)
186 return block_commit_write(page, from, to);
191 * for a block special file file->f_dentry->d_inode->i_size is zero
192 * so we compute the size by hand (just as in block_read/write above)
194 static loff_t block_llseek(struct file *file, loff_t offset, int origin)
196 struct inode *bd_inode = file->f_mapping->host;
200 mutex_lock(&bd_inode->i_mutex);
201 size = i_size_read(bd_inode);
208 offset += file->f_pos;
211 if (offset >= 0 && offset <= size) {
212 if (offset != file->f_pos) {
213 file->f_pos = offset;
217 mutex_unlock(&bd_inode->i_mutex);
222 * Filp is never NULL; the only case when ->fsync() is called with
223 * NULL first argument is nfsd_sync_dir() and that's not a directory.
226 static int block_fsync(struct file *filp, struct dentry *dentry, int datasync)
228 return sync_blockdev(I_BDEV(filp->f_mapping->host));
235 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
236 static kmem_cache_t * bdev_cachep __read_mostly;
238 static struct inode *bdev_alloc_inode(struct super_block *sb)
240 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, SLAB_KERNEL);
243 return &ei->vfs_inode;
246 static void bdev_destroy_inode(struct inode *inode)
248 struct bdev_inode *bdi = BDEV_I(inode);
250 bdi->bdev.bd_inode_backing_dev_info = NULL;
251 kmem_cache_free(bdev_cachep, bdi);
254 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
256 struct bdev_inode *ei = (struct bdev_inode *) foo;
257 struct block_device *bdev = &ei->bdev;
259 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
260 SLAB_CTOR_CONSTRUCTOR)
262 memset(bdev, 0, sizeof(*bdev));
263 mutex_init(&bdev->bd_mutex);
264 mutex_init(&bdev->bd_mount_mutex);
265 INIT_LIST_HEAD(&bdev->bd_inodes);
266 INIT_LIST_HEAD(&bdev->bd_list);
268 INIT_LIST_HEAD(&bdev->bd_holder_list);
270 inode_init_once(&ei->vfs_inode);
274 static inline void __bd_forget(struct inode *inode)
276 list_del_init(&inode->i_devices);
277 inode->i_bdev = NULL;
278 inode->i_mapping = &inode->i_data;
281 static void bdev_clear_inode(struct inode *inode)
283 struct block_device *bdev = &BDEV_I(inode)->bdev;
285 spin_lock(&bdev_lock);
286 while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
287 __bd_forget(list_entry(p, struct inode, i_devices));
289 list_del_init(&bdev->bd_list);
290 spin_unlock(&bdev_lock);
293 static struct super_operations bdev_sops = {
294 .statfs = simple_statfs,
295 .alloc_inode = bdev_alloc_inode,
296 .destroy_inode = bdev_destroy_inode,
297 .drop_inode = generic_delete_inode,
298 .clear_inode = bdev_clear_inode,
301 static int bd_get_sb(struct file_system_type *fs_type,
302 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
304 return get_sb_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576, mnt);
307 static struct file_system_type bd_type = {
310 .kill_sb = kill_anon_super,
313 static struct vfsmount *bd_mnt __read_mostly;
314 struct super_block *blockdev_superblock;
316 void __init bdev_cache_init(void)
319 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
320 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
321 SLAB_MEM_SPREAD|SLAB_PANIC),
323 err = register_filesystem(&bd_type);
325 panic("Cannot register bdev pseudo-fs");
326 bd_mnt = kern_mount(&bd_type);
327 err = PTR_ERR(bd_mnt);
329 panic("Cannot create bdev pseudo-fs");
330 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
334 * Most likely _very_ bad one - but then it's hardly critical for small
335 * /dev and can be fixed when somebody will need really large one.
336 * Keep in mind that it will be fed through icache hash function too.
338 static inline unsigned long hash(dev_t dev)
340 return MAJOR(dev)+MINOR(dev);
343 static int bdev_test(struct inode *inode, void *data)
345 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
348 static int bdev_set(struct inode *inode, void *data)
350 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
354 static LIST_HEAD(all_bdevs);
356 struct block_device *bdget(dev_t dev)
358 struct block_device *bdev;
361 inode = iget5_locked(bd_mnt->mnt_sb, hash(dev),
362 bdev_test, bdev_set, &dev);
367 bdev = &BDEV_I(inode)->bdev;
369 if (inode->i_state & I_NEW) {
370 bdev->bd_contains = NULL;
371 bdev->bd_inode = inode;
372 bdev->bd_block_size = (1 << inode->i_blkbits);
373 bdev->bd_part_count = 0;
374 bdev->bd_invalidated = 0;
375 inode->i_mode = S_IFBLK;
377 inode->i_bdev = bdev;
378 inode->i_data.a_ops = &def_blk_aops;
379 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
380 inode->i_data.backing_dev_info = &default_backing_dev_info;
381 spin_lock(&bdev_lock);
382 list_add(&bdev->bd_list, &all_bdevs);
383 spin_unlock(&bdev_lock);
384 unlock_new_inode(inode);
389 EXPORT_SYMBOL(bdget);
391 long nr_blockdev_pages(void)
395 spin_lock(&bdev_lock);
396 list_for_each(p, &all_bdevs) {
397 struct block_device *bdev;
398 bdev = list_entry(p, struct block_device, bd_list);
399 ret += bdev->bd_inode->i_mapping->nrpages;
401 spin_unlock(&bdev_lock);
405 void bdput(struct block_device *bdev)
407 iput(bdev->bd_inode);
410 EXPORT_SYMBOL(bdput);
412 static struct block_device *bd_acquire(struct inode *inode)
414 struct block_device *bdev;
416 spin_lock(&bdev_lock);
417 bdev = inode->i_bdev;
419 atomic_inc(&bdev->bd_inode->i_count);
420 spin_unlock(&bdev_lock);
423 spin_unlock(&bdev_lock);
425 bdev = bdget(inode->i_rdev);
427 spin_lock(&bdev_lock);
428 if (!inode->i_bdev) {
430 * We take an additional bd_inode->i_count for inode,
431 * and it's released in clear_inode() of inode.
432 * So, we can access it via ->i_mapping always
435 atomic_inc(&bdev->bd_inode->i_count);
436 inode->i_bdev = bdev;
437 inode->i_mapping = bdev->bd_inode->i_mapping;
438 list_add(&inode->i_devices, &bdev->bd_inodes);
440 spin_unlock(&bdev_lock);
445 /* Call when you free inode */
447 void bd_forget(struct inode *inode)
449 struct block_device *bdev = NULL;
451 spin_lock(&bdev_lock);
453 if (inode->i_sb != blockdev_superblock)
454 bdev = inode->i_bdev;
457 spin_unlock(&bdev_lock);
460 iput(bdev->bd_inode);
463 int bd_claim(struct block_device *bdev, void *holder)
466 spin_lock(&bdev_lock);
468 /* first decide result */
469 if (bdev->bd_holder == holder)
470 res = 0; /* already a holder */
471 else if (bdev->bd_holder != NULL)
472 res = -EBUSY; /* held by someone else */
473 else if (bdev->bd_contains == bdev)
474 res = 0; /* is a whole device which isn't held */
476 else if (bdev->bd_contains->bd_holder == bd_claim)
477 res = 0; /* is a partition of a device that is being partitioned */
478 else if (bdev->bd_contains->bd_holder != NULL)
479 res = -EBUSY; /* is a partition of a held device */
481 res = 0; /* is a partition of an un-held device */
483 /* now impose change */
485 /* note that for a whole device bd_holders
486 * will be incremented twice, and bd_holder will
487 * be set to bd_claim before being set to holder
489 bdev->bd_contains->bd_holders ++;
490 bdev->bd_contains->bd_holder = bd_claim;
492 bdev->bd_holder = holder;
494 spin_unlock(&bdev_lock);
498 EXPORT_SYMBOL(bd_claim);
500 void bd_release(struct block_device *bdev)
502 spin_lock(&bdev_lock);
503 if (!--bdev->bd_contains->bd_holders)
504 bdev->bd_contains->bd_holder = NULL;
505 if (!--bdev->bd_holders)
506 bdev->bd_holder = NULL;
507 spin_unlock(&bdev_lock);
510 EXPORT_SYMBOL(bd_release);
514 * Functions for bd_claim_by_kobject / bd_release_from_kobject
516 * If a kobject is passed to bd_claim_by_kobject()
517 * and the kobject has a parent directory,
518 * following symlinks are created:
519 * o from the kobject to the claimed bdev
520 * o from "holders" directory of the bdev to the parent of the kobject
521 * bd_release_from_kobject() removes these symlinks.
524 * If /dev/dm-0 maps to /dev/sda, kobject corresponding to
525 * /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then:
526 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
527 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
530 static struct kobject *bdev_get_kobj(struct block_device *bdev)
532 if (bdev->bd_contains != bdev)
533 return kobject_get(&bdev->bd_part->kobj);
535 return kobject_get(&bdev->bd_disk->kobj);
538 static struct kobject *bdev_get_holder(struct block_device *bdev)
540 if (bdev->bd_contains != bdev)
541 return kobject_get(bdev->bd_part->holder_dir);
543 return kobject_get(bdev->bd_disk->holder_dir);
546 static void add_symlink(struct kobject *from, struct kobject *to)
550 sysfs_create_link(from, to, kobject_name(to));
553 static void del_symlink(struct kobject *from, struct kobject *to)
557 sysfs_remove_link(from, kobject_name(to));
561 * 'struct bd_holder' contains pointers to kobjects symlinked by
562 * bd_claim_by_kobject.
563 * It's connected to bd_holder_list which is protected by bdev->bd_sem.
566 struct list_head list; /* chain of holders of the bdev */
567 int count; /* references from the holder */
568 struct kobject *sdir; /* holder object, e.g. "/block/dm-0/slaves" */
569 struct kobject *hdev; /* e.g. "/block/dm-0" */
570 struct kobject *hdir; /* e.g. "/block/sda/holders" */
571 struct kobject *sdev; /* e.g. "/block/sda" */
575 * Get references of related kobjects at once.
576 * Returns 1 on success. 0 on failure.
578 * Should call bd_holder_release_dirs() after successful use.
580 static int bd_holder_grab_dirs(struct block_device *bdev,
581 struct bd_holder *bo)
586 bo->sdir = kobject_get(bo->sdir);
590 bo->hdev = kobject_get(bo->sdir->parent);
594 bo->sdev = bdev_get_kobj(bdev);
598 bo->hdir = bdev_get_holder(bdev);
605 kobject_put(bo->sdev);
607 kobject_put(bo->hdev);
609 kobject_put(bo->sdir);
614 /* Put references of related kobjects at once. */
615 static void bd_holder_release_dirs(struct bd_holder *bo)
617 kobject_put(bo->hdir);
618 kobject_put(bo->sdev);
619 kobject_put(bo->hdev);
620 kobject_put(bo->sdir);
623 static struct bd_holder *alloc_bd_holder(struct kobject *kobj)
625 struct bd_holder *bo;
627 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
637 static void free_bd_holder(struct bd_holder *bo)
643 * add_bd_holder - create sysfs symlinks for bd_claim() relationship
645 * @bdev: block device to be bd_claimed
646 * @bo: preallocated and initialized by alloc_bd_holder()
648 * If there is no matching entry with @bo in @bdev->bd_holder_list,
649 * add @bo to the list, create symlinks.
651 * Returns 1 if @bo was added to the list.
652 * Returns 0 if @bo wasn't used by any reason and should be freed.
654 static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
656 struct bd_holder *tmp;
661 list_for_each_entry(tmp, &bdev->bd_holder_list, list) {
662 if (tmp->sdir == bo->sdir) {
668 if (!bd_holder_grab_dirs(bdev, bo))
671 add_symlink(bo->sdir, bo->sdev);
672 add_symlink(bo->hdir, bo->hdev);
673 list_add_tail(&bo->list, &bdev->bd_holder_list);
678 * del_bd_holder - delete sysfs symlinks for bd_claim() relationship
680 * @bdev: block device to be bd_claimed
681 * @kobj: holder's kobject
683 * If there is matching entry with @kobj in @bdev->bd_holder_list
684 * and no other bd_claim() from the same kobject,
685 * remove the struct bd_holder from the list, delete symlinks for it.
687 * Returns a pointer to the struct bd_holder when it's removed from the list
688 * and ready to be freed.
689 * Returns NULL if matching claim isn't found or there is other bd_claim()
690 * by the same kobject.
692 static struct bd_holder *del_bd_holder(struct block_device *bdev,
693 struct kobject *kobj)
695 struct bd_holder *bo;
697 list_for_each_entry(bo, &bdev->bd_holder_list, list) {
698 if (bo->sdir == kobj) {
700 BUG_ON(bo->count < 0);
703 del_symlink(bo->sdir, bo->sdev);
704 del_symlink(bo->hdir, bo->hdev);
705 bd_holder_release_dirs(bo);
716 * bd_claim_by_kobject - bd_claim() with additional kobject signature
718 * @bdev: block device to be claimed
719 * @holder: holder's signature
720 * @kobj: holder's kobject
722 * Do bd_claim() and if it succeeds, create sysfs symlinks between
723 * the bdev and the holder's kobject.
724 * Use bd_release_from_kobject() when relesing the claimed bdev.
726 * Returns 0 on success. (same as bd_claim())
727 * Returns errno on failure.
729 static int bd_claim_by_kobject(struct block_device *bdev, void *holder,
730 struct kobject *kobj)
733 struct bd_holder *bo;
738 bo = alloc_bd_holder(kobj);
742 mutex_lock(&bdev->bd_mutex);
743 res = bd_claim(bdev, holder);
744 if (res || !add_bd_holder(bdev, bo))
746 mutex_unlock(&bdev->bd_mutex);
752 * bd_release_from_kobject - bd_release() with additional kobject signature
754 * @bdev: block device to be released
755 * @kobj: holder's kobject
757 * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject().
759 static void bd_release_from_kobject(struct block_device *bdev,
760 struct kobject *kobj)
762 struct bd_holder *bo;
767 mutex_lock(&bdev->bd_mutex);
769 if ((bo = del_bd_holder(bdev, kobj)))
771 mutex_unlock(&bdev->bd_mutex);
775 * bd_claim_by_disk - wrapper function for bd_claim_by_kobject()
777 * @bdev: block device to be claimed
778 * @holder: holder's signature
779 * @disk: holder's gendisk
781 * Call bd_claim_by_kobject() with getting @disk->slave_dir.
783 int bd_claim_by_disk(struct block_device *bdev, void *holder,
784 struct gendisk *disk)
786 return bd_claim_by_kobject(bdev, holder, kobject_get(disk->slave_dir));
788 EXPORT_SYMBOL_GPL(bd_claim_by_disk);
791 * bd_release_from_disk - wrapper function for bd_release_from_kobject()
793 * @bdev: block device to be claimed
794 * @disk: holder's gendisk
796 * Call bd_release_from_kobject() and put @disk->slave_dir.
798 void bd_release_from_disk(struct block_device *bdev, struct gendisk *disk)
800 bd_release_from_kobject(bdev, disk->slave_dir);
801 kobject_put(disk->slave_dir);
803 EXPORT_SYMBOL_GPL(bd_release_from_disk);
807 * Tries to open block device by device number. Use it ONLY if you
808 * really do not have anything better - i.e. when you are behind a
809 * truly sucky interface and all you are given is a device number. _Never_
810 * to be used for internal purposes. If you ever need it - reconsider
813 struct block_device *open_by_devnum(dev_t dev, unsigned mode)
815 struct block_device *bdev = bdget(dev);
817 int flags = mode & FMODE_WRITE ? O_RDWR : O_RDONLY;
819 err = blkdev_get(bdev, mode, flags);
820 return err ? ERR_PTR(err) : bdev;
823 EXPORT_SYMBOL(open_by_devnum);
826 * This routine checks whether a removable media has been changed,
827 * and invalidates all buffer-cache-entries in that case. This
828 * is a relatively slow routine, so we have to try to minimize using
829 * it. Thus it is called only upon a 'mount' or 'open'. This
830 * is the best way of combining speed and utility, I think.
831 * People changing diskettes in the middle of an operation deserve
834 int check_disk_change(struct block_device *bdev)
836 struct gendisk *disk = bdev->bd_disk;
837 struct block_device_operations * bdops = disk->fops;
839 if (!bdops->media_changed)
841 if (!bdops->media_changed(bdev->bd_disk))
844 if (__invalidate_device(bdev))
845 printk("VFS: busy inodes on changed media.\n");
847 if (bdops->revalidate_disk)
848 bdops->revalidate_disk(bdev->bd_disk);
849 if (bdev->bd_disk->minors > 1)
850 bdev->bd_invalidated = 1;
854 EXPORT_SYMBOL(check_disk_change);
856 void bd_set_size(struct block_device *bdev, loff_t size)
858 unsigned bsize = bdev_hardsect_size(bdev);
860 bdev->bd_inode->i_size = size;
861 while (bsize < PAGE_CACHE_SIZE) {
866 bdev->bd_block_size = bsize;
867 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
869 EXPORT_SYMBOL(bd_set_size);
871 static int do_open(struct block_device *bdev, struct file *file)
873 struct module *owner = NULL;
874 struct gendisk *disk;
878 file->f_mapping = bdev->bd_inode->i_mapping;
880 disk = get_gendisk(bdev->bd_dev, &part);
886 owner = disk->fops->owner;
888 mutex_lock(&bdev->bd_mutex);
889 if (!bdev->bd_openers) {
890 bdev->bd_disk = disk;
891 bdev->bd_contains = bdev;
893 struct backing_dev_info *bdi;
894 if (disk->fops->open) {
895 ret = disk->fops->open(bdev->bd_inode, file);
899 if (!bdev->bd_openers) {
900 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
901 bdi = blk_get_backing_dev_info(bdev);
903 bdi = &default_backing_dev_info;
904 bdev->bd_inode->i_data.backing_dev_info = bdi;
906 if (bdev->bd_invalidated)
907 rescan_partitions(disk, bdev);
910 struct block_device *whole;
911 whole = bdget_disk(disk, 0);
915 ret = blkdev_get(whole, file->f_mode, file->f_flags);
918 bdev->bd_contains = whole;
919 mutex_lock(&whole->bd_mutex);
920 whole->bd_part_count++;
921 p = disk->part[part - 1];
922 bdev->bd_inode->i_data.backing_dev_info =
923 whole->bd_inode->i_data.backing_dev_info;
924 if (!(disk->flags & GENHD_FL_UP) || !p || !p->nr_sects) {
925 whole->bd_part_count--;
926 mutex_unlock(&whole->bd_mutex);
930 kobject_get(&p->kobj);
932 bd_set_size(bdev, (loff_t) p->nr_sects << 9);
933 mutex_unlock(&whole->bd_mutex);
938 if (bdev->bd_contains == bdev) {
939 if (bdev->bd_disk->fops->open) {
940 ret = bdev->bd_disk->fops->open(bdev->bd_inode, file);
944 if (bdev->bd_invalidated)
945 rescan_partitions(bdev->bd_disk, bdev);
947 mutex_lock(&bdev->bd_contains->bd_mutex);
948 bdev->bd_contains->bd_part_count++;
949 mutex_unlock(&bdev->bd_contains->bd_mutex);
953 mutex_unlock(&bdev->bd_mutex);
958 bdev->bd_disk = NULL;
959 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
960 if (bdev != bdev->bd_contains)
961 blkdev_put(bdev->bd_contains);
962 bdev->bd_contains = NULL;
966 mutex_unlock(&bdev->bd_mutex);
973 int blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags)
976 * This crockload is due to bad choice of ->open() type.
978 * For now, block device ->open() routine must _not_
979 * examine anything in 'inode' argument except ->i_rdev.
981 struct file fake_file = {};
982 struct dentry fake_dentry = {};
983 fake_file.f_mode = mode;
984 fake_file.f_flags = flags;
985 fake_file.f_dentry = &fake_dentry;
986 fake_dentry.d_inode = bdev->bd_inode;
988 return do_open(bdev, &fake_file);
991 EXPORT_SYMBOL(blkdev_get);
993 static int blkdev_open(struct inode * inode, struct file * filp)
995 struct block_device *bdev;
999 * Preserve backwards compatibility and allow large file access
1000 * even if userspace doesn't ask for it explicitly. Some mkfs
1001 * binary needs it. We might want to drop this workaround
1002 * during an unstable branch.
1004 filp->f_flags |= O_LARGEFILE;
1006 bdev = bd_acquire(inode);
1008 res = do_open(bdev, filp);
1012 if (!(filp->f_flags & O_EXCL) )
1015 if (!(res = bd_claim(bdev, filp)))
1022 int blkdev_put(struct block_device *bdev)
1025 struct inode *bd_inode = bdev->bd_inode;
1026 struct gendisk *disk = bdev->bd_disk;
1028 mutex_lock(&bdev->bd_mutex);
1030 if (!--bdev->bd_openers) {
1031 sync_blockdev(bdev);
1034 if (bdev->bd_contains == bdev) {
1035 if (disk->fops->release)
1036 ret = disk->fops->release(bd_inode, NULL);
1038 mutex_lock(&bdev->bd_contains->bd_mutex);
1039 bdev->bd_contains->bd_part_count--;
1040 mutex_unlock(&bdev->bd_contains->bd_mutex);
1042 if (!bdev->bd_openers) {
1043 struct module *owner = disk->fops->owner;
1048 if (bdev->bd_contains != bdev) {
1049 kobject_put(&bdev->bd_part->kobj);
1050 bdev->bd_part = NULL;
1052 bdev->bd_disk = NULL;
1053 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
1054 if (bdev != bdev->bd_contains) {
1055 blkdev_put(bdev->bd_contains);
1057 bdev->bd_contains = NULL;
1060 mutex_unlock(&bdev->bd_mutex);
1065 EXPORT_SYMBOL(blkdev_put);
1067 static int blkdev_close(struct inode * inode, struct file * filp)
1069 struct block_device *bdev = I_BDEV(filp->f_mapping->host);
1070 if (bdev->bd_holder == filp)
1072 return blkdev_put(bdev);
1075 static ssize_t blkdev_file_write(struct file *file, const char __user *buf,
1076 size_t count, loff_t *ppos)
1078 struct iovec local_iov = { .iov_base = (void __user *)buf, .iov_len = count };
1080 return generic_file_write_nolock(file, &local_iov, 1, ppos);
1083 static ssize_t blkdev_file_aio_write(struct kiocb *iocb, const char __user *buf,
1084 size_t count, loff_t pos)
1086 struct iovec local_iov = { .iov_base = (void __user *)buf, .iov_len = count };
1088 return generic_file_aio_write_nolock(iocb, &local_iov, 1, &iocb->ki_pos);
1091 static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1093 return blkdev_ioctl(file->f_mapping->host, file, cmd, arg);
1096 const struct address_space_operations def_blk_aops = {
1097 .readpage = blkdev_readpage,
1098 .writepage = blkdev_writepage,
1099 .sync_page = block_sync_page,
1100 .prepare_write = blkdev_prepare_write,
1101 .commit_write = blkdev_commit_write,
1102 .writepages = generic_writepages,
1103 .direct_IO = blkdev_direct_IO,
1106 const struct file_operations def_blk_fops = {
1107 .open = blkdev_open,
1108 .release = blkdev_close,
1109 .llseek = block_llseek,
1110 .read = generic_file_read,
1111 .write = blkdev_file_write,
1112 .aio_read = generic_file_aio_read,
1113 .aio_write = blkdev_file_aio_write,
1114 .mmap = generic_file_mmap,
1115 .fsync = block_fsync,
1116 .unlocked_ioctl = block_ioctl,
1117 #ifdef CONFIG_COMPAT
1118 .compat_ioctl = compat_blkdev_ioctl,
1120 .readv = generic_file_readv,
1121 .writev = generic_file_write_nolock,
1122 .sendfile = generic_file_sendfile,
1123 .splice_read = generic_file_splice_read,
1124 .splice_write = generic_file_splice_write,
1127 int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
1130 mm_segment_t old_fs = get_fs();
1132 res = blkdev_ioctl(bdev->bd_inode, NULL, cmd, arg);
1137 EXPORT_SYMBOL(ioctl_by_bdev);
1140 * lookup_bdev - lookup a struct block_device by name
1142 * @path: special file representing the block device
1144 * Get a reference to the blockdevice at @path in the current
1145 * namespace if possible and return it. Return ERR_PTR(error)
1148 struct block_device *lookup_bdev(const char *path)
1150 struct block_device *bdev;
1151 struct inode *inode;
1152 struct nameidata nd;
1155 if (!path || !*path)
1156 return ERR_PTR(-EINVAL);
1158 error = path_lookup(path, LOOKUP_FOLLOW, &nd);
1160 return ERR_PTR(error);
1162 inode = nd.dentry->d_inode;
1164 if (!S_ISBLK(inode->i_mode))
1167 if (nd.mnt->mnt_flags & MNT_NODEV)
1170 bdev = bd_acquire(inode);
1177 bdev = ERR_PTR(error);
1182 * open_bdev_excl - open a block device by name and set it up for use
1184 * @path: special file representing the block device
1185 * @flags: %MS_RDONLY for opening read-only
1186 * @holder: owner for exclusion
1188 * Open the blockdevice described by the special file at @path, claim it
1191 struct block_device *open_bdev_excl(const char *path, int flags, void *holder)
1193 struct block_device *bdev;
1194 mode_t mode = FMODE_READ;
1197 bdev = lookup_bdev(path);
1201 if (!(flags & MS_RDONLY))
1202 mode |= FMODE_WRITE;
1203 error = blkdev_get(bdev, mode, 0);
1205 return ERR_PTR(error);
1207 if (!(flags & MS_RDONLY) && bdev_read_only(bdev))
1209 error = bd_claim(bdev, holder);
1217 return ERR_PTR(error);
1220 EXPORT_SYMBOL(open_bdev_excl);
1223 * close_bdev_excl - release a blockdevice openen by open_bdev_excl()
1225 * @bdev: blockdevice to close
1227 * This is the counterpart to open_bdev_excl().
1229 void close_bdev_excl(struct block_device *bdev)
1235 EXPORT_SYMBOL(close_bdev_excl);