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/device_cgroup.h>
16 #include <linux/highmem.h>
17 #include <linux/blkdev.h>
18 #include <linux/module.h>
19 #include <linux/blkpg.h>
20 #include <linux/buffer_head.h>
21 #include <linux/pagevec.h>
22 #include <linux/writeback.h>
23 #include <linux/mpage.h>
24 #include <linux/mount.h>
25 #include <linux/uio.h>
26 #include <linux/namei.h>
27 #include <linux/log2.h>
28 #include <linux/kmemleak.h>
29 #include <asm/uaccess.h>
31 #include <linux/mtd/blktrans.h>
32 #include <linux/mtd/mtd.h>
35 struct block_device bdev;
36 struct inode vfs_inode;
39 static const struct address_space_operations def_blk_aops;
41 static inline struct bdev_inode *BDEV_I(struct inode *inode)
43 return container_of(inode, struct bdev_inode, vfs_inode);
46 inline struct block_device *I_BDEV(struct inode *inode)
48 return &BDEV_I(inode)->bdev;
51 EXPORT_SYMBOL(I_BDEV);
53 static sector_t max_block(struct block_device *bdev)
55 sector_t retval = ~((sector_t)0);
56 loff_t sz = i_size_read(bdev->bd_inode);
59 unsigned int size = block_size(bdev);
60 unsigned int sizebits = blksize_bits(size);
61 retval = (sz >> sizebits);
66 /* Kill _all_ buffers and pagecache , dirty or not.. */
67 static void kill_bdev(struct block_device *bdev)
69 if (bdev->bd_inode->i_mapping->nrpages == 0)
72 truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
75 int set_blocksize(struct block_device *bdev, int size)
77 /* Size must be a power of two, and between 512 and PAGE_SIZE */
78 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
81 /* Size cannot be smaller than the size supported by the device */
82 if (size < bdev_logical_block_size(bdev))
85 /* Don't change the size if it is same as current */
86 if (bdev->bd_block_size != size) {
88 bdev->bd_block_size = size;
89 bdev->bd_inode->i_blkbits = blksize_bits(size);
95 EXPORT_SYMBOL(set_blocksize);
97 int sb_set_blocksize(struct super_block *sb, int size)
99 if (set_blocksize(sb->s_bdev, size))
101 /* If we get here, we know size is power of two
102 * and it's value is between 512 and PAGE_SIZE */
103 sb->s_blocksize = size;
104 sb->s_blocksize_bits = blksize_bits(size);
105 return sb->s_blocksize;
108 EXPORT_SYMBOL(sb_set_blocksize);
110 int sb_min_blocksize(struct super_block *sb, int size)
112 int minsize = bdev_logical_block_size(sb->s_bdev);
115 return sb_set_blocksize(sb, size);
118 EXPORT_SYMBOL(sb_min_blocksize);
121 blkdev_get_block(struct inode *inode, sector_t iblock,
122 struct buffer_head *bh, int create)
124 if (iblock >= max_block(I_BDEV(inode))) {
129 * for reads, we're just trying to fill a partial page.
130 * return a hole, they will have to call get_block again
131 * before they can fill it, and they will get -EIO at that
136 bh->b_bdev = I_BDEV(inode);
137 bh->b_blocknr = iblock;
138 set_buffer_mapped(bh);
143 blkdev_get_blocks(struct inode *inode, sector_t iblock,
144 struct buffer_head *bh, int create)
146 sector_t end_block = max_block(I_BDEV(inode));
147 unsigned long max_blocks = bh->b_size >> inode->i_blkbits;
149 if ((iblock + max_blocks) > end_block) {
150 max_blocks = end_block - iblock;
151 if ((long)max_blocks <= 0) {
153 return -EIO; /* write fully beyond EOF */
155 * It is a read which is fully beyond EOF. We return
156 * a !buffer_mapped buffer
162 bh->b_bdev = I_BDEV(inode);
163 bh->b_blocknr = iblock;
164 bh->b_size = max_blocks << inode->i_blkbits;
166 set_buffer_mapped(bh);
171 blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
172 loff_t offset, unsigned long nr_segs)
174 struct file *file = iocb->ki_filp;
175 struct inode *inode = file->f_mapping->host;
177 return blockdev_direct_IO_no_locking(rw, iocb, inode, I_BDEV(inode),
178 iov, offset, nr_segs, blkdev_get_blocks, NULL);
181 int __sync_blockdev(struct block_device *bdev, int wait)
186 return filemap_flush(bdev->bd_inode->i_mapping);
187 return filemap_write_and_wait(bdev->bd_inode->i_mapping);
191 * Write out and wait upon all the dirty data associated with a block
192 * device via its mapping. Does not take the superblock lock.
194 int sync_blockdev(struct block_device *bdev)
196 return __sync_blockdev(bdev, 1);
198 EXPORT_SYMBOL(sync_blockdev);
201 * Write out and wait upon all dirty data associated with this
202 * device. Filesystem data as well as the underlying block
203 * device. Takes the superblock lock.
205 int fsync_bdev(struct block_device *bdev)
207 struct super_block *sb = get_super(bdev);
209 int res = sync_filesystem(sb);
213 return sync_blockdev(bdev);
215 EXPORT_SYMBOL(fsync_bdev);
218 * freeze_bdev -- lock a filesystem and force it into a consistent state
219 * @bdev: blockdevice to lock
221 * If a superblock is found on this device, we take the s_umount semaphore
222 * on it to make sure nobody unmounts until the snapshot creation is done.
223 * The reference counter (bd_fsfreeze_count) guarantees that only the last
224 * unfreeze process can unfreeze the frozen filesystem actually when multiple
225 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
226 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
229 struct super_block *freeze_bdev(struct block_device *bdev)
231 struct super_block *sb;
234 mutex_lock(&bdev->bd_fsfreeze_mutex);
235 if (++bdev->bd_fsfreeze_count > 1) {
237 * We don't even need to grab a reference - the first call
238 * to freeze_bdev grab an active reference and only the last
239 * thaw_bdev drops it.
241 sb = get_super(bdev);
243 mutex_unlock(&bdev->bd_fsfreeze_mutex);
247 sb = get_active_super(bdev);
250 if (sb->s_flags & MS_RDONLY) {
251 sb->s_frozen = SB_FREEZE_TRANS;
252 up_write(&sb->s_umount);
253 mutex_unlock(&bdev->bd_fsfreeze_mutex);
257 sb->s_frozen = SB_FREEZE_WRITE;
262 sb->s_frozen = SB_FREEZE_TRANS;
265 sync_blockdev(sb->s_bdev);
267 if (sb->s_op->freeze_fs) {
268 error = sb->s_op->freeze_fs(sb);
271 "VFS:Filesystem freeze failed\n");
272 sb->s_frozen = SB_UNFROZEN;
273 deactivate_locked_super(sb);
274 bdev->bd_fsfreeze_count--;
275 mutex_unlock(&bdev->bd_fsfreeze_mutex);
276 return ERR_PTR(error);
279 up_write(&sb->s_umount);
283 mutex_unlock(&bdev->bd_fsfreeze_mutex);
284 return sb; /* thaw_bdev releases s->s_umount */
286 EXPORT_SYMBOL(freeze_bdev);
289 * thaw_bdev -- unlock filesystem
290 * @bdev: blockdevice to unlock
291 * @sb: associated superblock
293 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
295 int thaw_bdev(struct block_device *bdev, struct super_block *sb)
299 mutex_lock(&bdev->bd_fsfreeze_mutex);
300 if (!bdev->bd_fsfreeze_count)
304 if (--bdev->bd_fsfreeze_count > 0)
310 BUG_ON(sb->s_bdev != bdev);
311 down_write(&sb->s_umount);
312 if (sb->s_flags & MS_RDONLY)
315 if (sb->s_op->unfreeze_fs) {
316 error = sb->s_op->unfreeze_fs(sb);
319 "VFS:Filesystem thaw failed\n");
320 sb->s_frozen = SB_FREEZE_TRANS;
321 bdev->bd_fsfreeze_count++;
322 mutex_unlock(&bdev->bd_fsfreeze_mutex);
328 sb->s_frozen = SB_UNFROZEN;
330 wake_up(&sb->s_wait_unfrozen);
333 deactivate_locked_super(sb);
335 mutex_unlock(&bdev->bd_fsfreeze_mutex);
338 EXPORT_SYMBOL(thaw_bdev);
340 static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
342 return block_write_full_page(page, blkdev_get_block, wbc);
345 static int blkdev_readpage(struct file * file, struct page * page)
347 return block_read_full_page(page, blkdev_get_block);
350 static int blkdev_write_begin(struct file *file, struct address_space *mapping,
351 loff_t pos, unsigned len, unsigned flags,
352 struct page **pagep, void **fsdata)
355 return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
359 static int blkdev_write_end(struct file *file, struct address_space *mapping,
360 loff_t pos, unsigned len, unsigned copied,
361 struct page *page, void *fsdata)
364 ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
367 page_cache_release(page);
374 * for a block special file file->f_path.dentry->d_inode->i_size is zero
375 * so we compute the size by hand (just as in block_read/write above)
377 static loff_t block_llseek(struct file *file, loff_t offset, int origin)
379 struct inode *bd_inode = file->f_mapping->host;
383 mutex_lock(&bd_inode->i_mutex);
384 size = i_size_read(bd_inode);
391 offset += file->f_pos;
394 if (offset >= 0 && offset <= size) {
395 if (offset != file->f_pos) {
396 file->f_pos = offset;
400 mutex_unlock(&bd_inode->i_mutex);
405 * Filp is never NULL; the only case when ->fsync() is called with
406 * NULL first argument is nfsd_sync_dir() and that's not a directory.
409 int block_fsync(struct file *filp, struct dentry *dentry, int datasync)
411 return sync_blockdev(I_BDEV(filp->f_mapping->host));
418 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
419 static struct kmem_cache * bdev_cachep __read_mostly;
421 static struct inode *bdev_alloc_inode(struct super_block *sb)
423 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
426 return &ei->vfs_inode;
428 EXPORT_SYMBOL(block_fsync);
430 static void bdev_destroy_inode(struct inode *inode)
432 struct bdev_inode *bdi = BDEV_I(inode);
434 kmem_cache_free(bdev_cachep, bdi);
437 static void init_once(void *foo)
439 struct bdev_inode *ei = (struct bdev_inode *) foo;
440 struct block_device *bdev = &ei->bdev;
442 memset(bdev, 0, sizeof(*bdev));
443 mutex_init(&bdev->bd_mutex);
444 INIT_LIST_HEAD(&bdev->bd_inodes);
445 INIT_LIST_HEAD(&bdev->bd_list);
447 INIT_LIST_HEAD(&bdev->bd_holder_list);
449 inode_init_once(&ei->vfs_inode);
450 /* Initialize mutex for freeze. */
451 mutex_init(&bdev->bd_fsfreeze_mutex);
454 static inline void __bd_forget(struct inode *inode)
456 list_del_init(&inode->i_devices);
457 inode->i_bdev = NULL;
458 inode->i_mapping = &inode->i_data;
461 static void bdev_clear_inode(struct inode *inode)
463 struct block_device *bdev = &BDEV_I(inode)->bdev;
465 spin_lock(&bdev_lock);
466 while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
467 __bd_forget(list_entry(p, struct inode, i_devices));
469 list_del_init(&bdev->bd_list);
470 spin_unlock(&bdev_lock);
473 static const struct super_operations bdev_sops = {
474 .statfs = simple_statfs,
475 .alloc_inode = bdev_alloc_inode,
476 .destroy_inode = bdev_destroy_inode,
477 .drop_inode = generic_delete_inode,
478 .clear_inode = bdev_clear_inode,
481 static int bd_get_sb(struct file_system_type *fs_type,
482 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
484 return get_sb_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576, mnt);
487 static struct file_system_type bd_type = {
490 .kill_sb = kill_anon_super,
493 struct super_block *blockdev_superblock __read_mostly;
495 void __init bdev_cache_init(void)
498 struct vfsmount *bd_mnt;
500 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
501 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
502 SLAB_MEM_SPREAD|SLAB_PANIC),
504 err = register_filesystem(&bd_type);
506 panic("Cannot register bdev pseudo-fs");
507 bd_mnt = kern_mount(&bd_type);
509 panic("Cannot create bdev pseudo-fs");
511 * This vfsmount structure is only used to obtain the
512 * blockdev_superblock, so tell kmemleak not to report it.
514 kmemleak_not_leak(bd_mnt);
515 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
519 * Most likely _very_ bad one - but then it's hardly critical for small
520 * /dev and can be fixed when somebody will need really large one.
521 * Keep in mind that it will be fed through icache hash function too.
523 static inline unsigned long hash(dev_t dev)
525 return MAJOR(dev)+MINOR(dev);
528 static int bdev_test(struct inode *inode, void *data)
530 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
533 static int bdev_set(struct inode *inode, void *data)
535 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
539 static LIST_HEAD(all_bdevs);
541 struct block_device *bdget(dev_t dev)
543 struct block_device *bdev;
546 inode = iget5_locked(blockdev_superblock, hash(dev),
547 bdev_test, bdev_set, &dev);
552 bdev = &BDEV_I(inode)->bdev;
554 if (inode->i_state & I_NEW) {
555 bdev->bd_contains = NULL;
556 bdev->bd_inode = inode;
557 bdev->bd_block_size = (1 << inode->i_blkbits);
558 bdev->bd_part_count = 0;
559 bdev->bd_invalidated = 0;
560 inode->i_mode = S_IFBLK;
562 inode->i_bdev = bdev;
563 inode->i_data.a_ops = &def_blk_aops;
564 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
565 inode->i_data.backing_dev_info = &default_backing_dev_info;
566 spin_lock(&bdev_lock);
567 list_add(&bdev->bd_list, &all_bdevs);
568 spin_unlock(&bdev_lock);
569 unlock_new_inode(inode);
574 EXPORT_SYMBOL(bdget);
577 * bdgrab -- Grab a reference to an already referenced block device
578 * @bdev: Block device to grab a reference to.
580 struct block_device *bdgrab(struct block_device *bdev)
582 atomic_inc(&bdev->bd_inode->i_count);
586 long nr_blockdev_pages(void)
588 struct block_device *bdev;
590 spin_lock(&bdev_lock);
591 list_for_each_entry(bdev, &all_bdevs, bd_list) {
592 ret += bdev->bd_inode->i_mapping->nrpages;
594 spin_unlock(&bdev_lock);
598 void bdput(struct block_device *bdev)
600 iput(bdev->bd_inode);
603 EXPORT_SYMBOL(bdput);
605 static struct block_device *bd_acquire(struct inode *inode)
607 struct block_device *bdev;
609 spin_lock(&bdev_lock);
610 bdev = inode->i_bdev;
612 atomic_inc(&bdev->bd_inode->i_count);
613 spin_unlock(&bdev_lock);
616 spin_unlock(&bdev_lock);
618 bdev = bdget(inode->i_rdev);
620 spin_lock(&bdev_lock);
621 if (!inode->i_bdev) {
623 * We take an additional bd_inode->i_count for inode,
624 * and it's released in clear_inode() of inode.
625 * So, we can access it via ->i_mapping always
628 atomic_inc(&bdev->bd_inode->i_count);
629 inode->i_bdev = bdev;
630 inode->i_mapping = bdev->bd_inode->i_mapping;
631 list_add(&inode->i_devices, &bdev->bd_inodes);
633 spin_unlock(&bdev_lock);
638 /* Call when you free inode */
640 void bd_forget(struct inode *inode)
642 struct block_device *bdev = NULL;
644 spin_lock(&bdev_lock);
646 if (!sb_is_blkdev_sb(inode->i_sb))
647 bdev = inode->i_bdev;
650 spin_unlock(&bdev_lock);
653 iput(bdev->bd_inode);
656 int bd_claim(struct block_device *bdev, void *holder)
659 spin_lock(&bdev_lock);
661 /* first decide result */
662 if (bdev->bd_holder == holder)
663 res = 0; /* already a holder */
664 else if (bdev->bd_holder != NULL)
665 res = -EBUSY; /* held by someone else */
666 else if (bdev->bd_contains == bdev)
667 res = 0; /* is a whole device which isn't held */
669 else if (bdev->bd_contains->bd_holder == bd_claim)
670 res = 0; /* is a partition of a device that is being partitioned */
671 else if (bdev->bd_contains->bd_holder != NULL)
672 res = -EBUSY; /* is a partition of a held device */
674 res = 0; /* is a partition of an un-held device */
676 /* now impose change */
678 /* note that for a whole device bd_holders
679 * will be incremented twice, and bd_holder will
680 * be set to bd_claim before being set to holder
682 bdev->bd_contains->bd_holders ++;
683 bdev->bd_contains->bd_holder = bd_claim;
685 bdev->bd_holder = holder;
687 spin_unlock(&bdev_lock);
691 EXPORT_SYMBOL(bd_claim);
693 void bd_release(struct block_device *bdev)
695 spin_lock(&bdev_lock);
696 if (!--bdev->bd_contains->bd_holders)
697 bdev->bd_contains->bd_holder = NULL;
698 if (!--bdev->bd_holders)
699 bdev->bd_holder = NULL;
700 spin_unlock(&bdev_lock);
703 EXPORT_SYMBOL(bd_release);
707 * Functions for bd_claim_by_kobject / bd_release_from_kobject
709 * If a kobject is passed to bd_claim_by_kobject()
710 * and the kobject has a parent directory,
711 * following symlinks are created:
712 * o from the kobject to the claimed bdev
713 * o from "holders" directory of the bdev to the parent of the kobject
714 * bd_release_from_kobject() removes these symlinks.
717 * If /dev/dm-0 maps to /dev/sda, kobject corresponding to
718 * /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then:
719 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
720 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
723 static int add_symlink(struct kobject *from, struct kobject *to)
727 return sysfs_create_link(from, to, kobject_name(to));
730 static void del_symlink(struct kobject *from, struct kobject *to)
734 sysfs_remove_link(from, kobject_name(to));
738 * 'struct bd_holder' contains pointers to kobjects symlinked by
739 * bd_claim_by_kobject.
740 * It's connected to bd_holder_list which is protected by bdev->bd_sem.
743 struct list_head list; /* chain of holders of the bdev */
744 int count; /* references from the holder */
745 struct kobject *sdir; /* holder object, e.g. "/block/dm-0/slaves" */
746 struct kobject *hdev; /* e.g. "/block/dm-0" */
747 struct kobject *hdir; /* e.g. "/block/sda/holders" */
748 struct kobject *sdev; /* e.g. "/block/sda" */
752 * Get references of related kobjects at once.
753 * Returns 1 on success. 0 on failure.
755 * Should call bd_holder_release_dirs() after successful use.
757 static int bd_holder_grab_dirs(struct block_device *bdev,
758 struct bd_holder *bo)
763 bo->sdir = kobject_get(bo->sdir);
767 bo->hdev = kobject_get(bo->sdir->parent);
771 bo->sdev = kobject_get(&part_to_dev(bdev->bd_part)->kobj);
775 bo->hdir = kobject_get(bdev->bd_part->holder_dir);
782 kobject_put(bo->sdev);
784 kobject_put(bo->hdev);
786 kobject_put(bo->sdir);
791 /* Put references of related kobjects at once. */
792 static void bd_holder_release_dirs(struct bd_holder *bo)
794 kobject_put(bo->hdir);
795 kobject_put(bo->sdev);
796 kobject_put(bo->hdev);
797 kobject_put(bo->sdir);
800 static struct bd_holder *alloc_bd_holder(struct kobject *kobj)
802 struct bd_holder *bo;
804 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
814 static void free_bd_holder(struct bd_holder *bo)
820 * find_bd_holder - find matching struct bd_holder from the block device
822 * @bdev: struct block device to be searched
823 * @bo: target struct bd_holder
825 * Returns matching entry with @bo in @bdev->bd_holder_list.
826 * If found, increment the reference count and return the pointer.
827 * If not found, returns NULL.
829 static struct bd_holder *find_bd_holder(struct block_device *bdev,
830 struct bd_holder *bo)
832 struct bd_holder *tmp;
834 list_for_each_entry(tmp, &bdev->bd_holder_list, list)
835 if (tmp->sdir == bo->sdir) {
844 * add_bd_holder - create sysfs symlinks for bd_claim() relationship
846 * @bdev: block device to be bd_claimed
847 * @bo: preallocated and initialized by alloc_bd_holder()
849 * Add @bo to @bdev->bd_holder_list, create symlinks.
851 * Returns 0 if symlinks are created.
852 * Returns -ve if something fails.
854 static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
861 if (!bd_holder_grab_dirs(bdev, bo))
864 err = add_symlink(bo->sdir, bo->sdev);
868 err = add_symlink(bo->hdir, bo->hdev);
870 del_symlink(bo->sdir, bo->sdev);
874 list_add_tail(&bo->list, &bdev->bd_holder_list);
879 * del_bd_holder - delete sysfs symlinks for bd_claim() relationship
881 * @bdev: block device to be bd_claimed
882 * @kobj: holder's kobject
884 * If there is matching entry with @kobj in @bdev->bd_holder_list
885 * and no other bd_claim() from the same kobject,
886 * remove the struct bd_holder from the list, delete symlinks for it.
888 * Returns a pointer to the struct bd_holder when it's removed from the list
889 * and ready to be freed.
890 * Returns NULL if matching claim isn't found or there is other bd_claim()
891 * by the same kobject.
893 static struct bd_holder *del_bd_holder(struct block_device *bdev,
894 struct kobject *kobj)
896 struct bd_holder *bo;
898 list_for_each_entry(bo, &bdev->bd_holder_list, list) {
899 if (bo->sdir == kobj) {
901 BUG_ON(bo->count < 0);
904 del_symlink(bo->sdir, bo->sdev);
905 del_symlink(bo->hdir, bo->hdev);
906 bd_holder_release_dirs(bo);
917 * bd_claim_by_kobject - bd_claim() with additional kobject signature
919 * @bdev: block device to be claimed
920 * @holder: holder's signature
921 * @kobj: holder's kobject
923 * Do bd_claim() and if it succeeds, create sysfs symlinks between
924 * the bdev and the holder's kobject.
925 * Use bd_release_from_kobject() when relesing the claimed bdev.
927 * Returns 0 on success. (same as bd_claim())
928 * Returns errno on failure.
930 static int bd_claim_by_kobject(struct block_device *bdev, void *holder,
931 struct kobject *kobj)
934 struct bd_holder *bo, *found;
939 bo = alloc_bd_holder(kobj);
943 mutex_lock(&bdev->bd_mutex);
945 err = bd_claim(bdev, holder);
949 found = find_bd_holder(bdev, bo);
953 err = add_bd_holder(bdev, bo);
959 mutex_unlock(&bdev->bd_mutex);
965 * bd_release_from_kobject - bd_release() with additional kobject signature
967 * @bdev: block device to be released
968 * @kobj: holder's kobject
970 * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject().
972 static void bd_release_from_kobject(struct block_device *bdev,
973 struct kobject *kobj)
978 mutex_lock(&bdev->bd_mutex);
980 free_bd_holder(del_bd_holder(bdev, kobj));
981 mutex_unlock(&bdev->bd_mutex);
985 * bd_claim_by_disk - wrapper function for bd_claim_by_kobject()
987 * @bdev: block device to be claimed
988 * @holder: holder's signature
989 * @disk: holder's gendisk
991 * Call bd_claim_by_kobject() with getting @disk->slave_dir.
993 int bd_claim_by_disk(struct block_device *bdev, void *holder,
994 struct gendisk *disk)
996 return bd_claim_by_kobject(bdev, holder, kobject_get(disk->slave_dir));
998 EXPORT_SYMBOL_GPL(bd_claim_by_disk);
1001 * bd_release_from_disk - wrapper function for bd_release_from_kobject()
1003 * @bdev: block device to be claimed
1004 * @disk: holder's gendisk
1006 * Call bd_release_from_kobject() and put @disk->slave_dir.
1008 void bd_release_from_disk(struct block_device *bdev, struct gendisk *disk)
1010 bd_release_from_kobject(bdev, disk->slave_dir);
1011 kobject_put(disk->slave_dir);
1013 EXPORT_SYMBOL_GPL(bd_release_from_disk);
1017 * Tries to open block device by device number. Use it ONLY if you
1018 * really do not have anything better - i.e. when you are behind a
1019 * truly sucky interface and all you are given is a device number. _Never_
1020 * to be used for internal purposes. If you ever need it - reconsider
1023 struct block_device *open_by_devnum(dev_t dev, fmode_t mode)
1025 struct block_device *bdev = bdget(dev);
1028 err = blkdev_get(bdev, mode);
1029 return err ? ERR_PTR(err) : bdev;
1032 EXPORT_SYMBOL(open_by_devnum);
1035 * flush_disk - invalidates all buffer-cache entries on a disk
1037 * @bdev: struct block device to be flushed
1039 * Invalidates all buffer-cache entries on a disk. It should be called
1040 * when a disk has been changed -- either by a media change or online
1043 static void flush_disk(struct block_device *bdev)
1045 if (__invalidate_device(bdev)) {
1046 char name[BDEVNAME_SIZE] = "";
1049 disk_name(bdev->bd_disk, 0, name);
1050 printk(KERN_WARNING "VFS: busy inodes on changed media or "
1051 "resized disk %s\n", name);
1056 if (disk_partitionable(bdev->bd_disk))
1057 bdev->bd_invalidated = 1;
1061 * check_disk_size_change - checks for disk size change and adjusts bdev size.
1062 * @disk: struct gendisk to check
1063 * @bdev: struct bdev to adjust.
1065 * This routine checks to see if the bdev size does not match the disk size
1066 * and adjusts it if it differs.
1068 void check_disk_size_change(struct gendisk *disk, struct block_device *bdev)
1070 loff_t disk_size, bdev_size;
1072 disk_size = (loff_t)get_capacity(disk) << 9;
1073 bdev_size = i_size_read(bdev->bd_inode);
1074 if (disk_size != bdev_size) {
1075 char name[BDEVNAME_SIZE];
1077 disk_name(disk, 0, name);
1079 "%s: detected capacity change from %lld to %lld\n",
1080 name, bdev_size, disk_size);
1081 i_size_write(bdev->bd_inode, disk_size);
1085 EXPORT_SYMBOL(check_disk_size_change);
1088 * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back
1089 * @disk: struct gendisk to be revalidated
1091 * This routine is a wrapper for lower-level driver's revalidate_disk
1092 * call-backs. It is used to do common pre and post operations needed
1093 * for all revalidate_disk operations.
1095 int revalidate_disk(struct gendisk *disk)
1097 struct block_device *bdev;
1100 if (disk->fops->revalidate_disk)
1101 ret = disk->fops->revalidate_disk(disk);
1103 bdev = bdget_disk(disk, 0);
1107 mutex_lock(&bdev->bd_mutex);
1108 check_disk_size_change(disk, bdev);
1109 mutex_unlock(&bdev->bd_mutex);
1113 EXPORT_SYMBOL(revalidate_disk);
1116 * This routine checks whether a removable media has been changed,
1117 * and invalidates all buffer-cache-entries in that case. This
1118 * is a relatively slow routine, so we have to try to minimize using
1119 * it. Thus it is called only upon a 'mount' or 'open'. This
1120 * is the best way of combining speed and utility, I think.
1121 * People changing diskettes in the middle of an operation deserve
1124 int check_disk_change(struct block_device *bdev)
1126 struct gendisk *disk = bdev->bd_disk;
1127 const struct block_device_operations *bdops = disk->fops;
1129 if (!bdops->media_changed)
1131 if (!bdops->media_changed(bdev->bd_disk))
1135 if (bdops->revalidate_disk)
1136 bdops->revalidate_disk(bdev->bd_disk);
1140 EXPORT_SYMBOL(check_disk_change);
1142 void bd_set_size(struct block_device *bdev, loff_t size)
1144 unsigned bsize = bdev_logical_block_size(bdev);
1146 bdev->bd_inode->i_size = size;
1147 while (bsize < PAGE_CACHE_SIZE) {
1152 bdev->bd_block_size = bsize;
1153 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
1155 EXPORT_SYMBOL(bd_set_size);
1157 static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
1162 * mutex_lock(part->bd_mutex)
1163 * mutex_lock_nested(whole->bd_mutex, 1)
1166 static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
1168 struct gendisk *disk;
1173 if (mode & FMODE_READ)
1175 if (mode & FMODE_WRITE)
1178 * hooks: /n/, see "layering violations".
1181 ret = devcgroup_inode_permission(bdev->bd_inode, perm);
1192 disk = get_gendisk(bdev->bd_dev, &partno);
1194 goto out_unlock_kernel;
1196 mutex_lock_nested(&bdev->bd_mutex, for_part);
1197 if (!bdev->bd_openers) {
1198 bdev->bd_disk = disk;
1199 bdev->bd_contains = bdev;
1201 struct backing_dev_info *bdi;
1204 bdev->bd_part = disk_get_part(disk, partno);
1209 if (disk->fops->open) {
1210 ret = disk->fops->open(bdev, mode);
1211 if (ret == -ERESTARTSYS) {
1212 /* Lost a race with 'disk' being
1213 * deleted, try again.
1216 disk_put_part(bdev->bd_part);
1217 bdev->bd_part = NULL;
1218 module_put(disk->fops->owner);
1220 bdev->bd_disk = NULL;
1221 mutex_unlock(&bdev->bd_mutex);
1226 * If the device is invalidated, rescan partition
1227 * if open succeeded or failed with -ENOMEDIUM.
1228 * The latter is necessary to prevent ghost
1229 * partitions on a removed medium.
1231 if (bdev->bd_invalidated && (!ret || ret == -ENOMEDIUM))
1232 rescan_partitions(disk, bdev);
1236 if (!bdev->bd_openers) {
1237 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
1238 bdi = blk_get_backing_dev_info(bdev);
1240 bdi = &default_backing_dev_info;
1241 bdev->bd_inode->i_data.backing_dev_info = bdi;
1244 struct block_device *whole;
1245 whole = bdget_disk(disk, 0);
1250 ret = __blkdev_get(whole, mode, 1);
1253 bdev->bd_contains = whole;
1254 bdev->bd_inode->i_data.backing_dev_info =
1255 whole->bd_inode->i_data.backing_dev_info;
1256 bdev->bd_part = disk_get_part(disk, partno);
1257 if (!(disk->flags & GENHD_FL_UP) ||
1258 !bdev->bd_part || !bdev->bd_part->nr_sects) {
1262 bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9);
1265 module_put(disk->fops->owner);
1268 if (bdev->bd_contains == bdev) {
1270 if (bdev->bd_disk->fops->open)
1271 ret = bdev->bd_disk->fops->open(bdev, mode);
1272 /* the same as first opener case, read comment there */
1273 if (bdev->bd_invalidated && (!ret || ret == -ENOMEDIUM))
1274 rescan_partitions(bdev->bd_disk, bdev);
1276 goto out_unlock_bdev;
1281 bdev->bd_part_count++;
1282 mutex_unlock(&bdev->bd_mutex);
1287 disk_put_part(bdev->bd_part);
1288 bdev->bd_disk = NULL;
1289 bdev->bd_part = NULL;
1290 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
1291 if (bdev != bdev->bd_contains)
1292 __blkdev_put(bdev->bd_contains, mode, 1);
1293 bdev->bd_contains = NULL;
1295 mutex_unlock(&bdev->bd_mutex);
1300 module_put(disk->fops->owner);
1307 int blkdev_get(struct block_device *bdev, fmode_t mode)
1309 return __blkdev_get(bdev, mode, 0);
1311 EXPORT_SYMBOL(blkdev_get);
1313 static int blkdev_open(struct inode * inode, struct file * filp)
1315 struct block_device *bdev;
1319 * Preserve backwards compatibility and allow large file access
1320 * even if userspace doesn't ask for it explicitly. Some mkfs
1321 * binary needs it. We might want to drop this workaround
1322 * during an unstable branch.
1324 filp->f_flags |= O_LARGEFILE;
1326 if (filp->f_flags & O_NDELAY)
1327 filp->f_mode |= FMODE_NDELAY;
1328 if (filp->f_flags & O_EXCL)
1329 filp->f_mode |= FMODE_EXCL;
1330 if ((filp->f_flags & O_ACCMODE) == 3)
1331 filp->f_mode |= FMODE_WRITE_IOCTL;
1333 bdev = bd_acquire(inode);
1337 filp->f_mapping = bdev->bd_inode->i_mapping;
1339 res = blkdev_get(bdev, filp->f_mode);
1343 if (filp->f_mode & FMODE_EXCL) {
1344 res = bd_claim(bdev, filp);
1346 goto out_blkdev_put;
1352 blkdev_put(bdev, filp->f_mode);
1356 static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
1359 struct gendisk *disk = bdev->bd_disk;
1360 struct block_device *victim = NULL;
1362 mutex_lock_nested(&bdev->bd_mutex, for_part);
1365 bdev->bd_part_count--;
1367 if (!--bdev->bd_openers) {
1368 sync_blockdev(bdev);
1371 if (bdev->bd_contains == bdev) {
1372 if (disk->fops->release)
1373 ret = disk->fops->release(disk, mode);
1375 if (!bdev->bd_openers) {
1376 struct module *owner = disk->fops->owner;
1380 disk_put_part(bdev->bd_part);
1381 bdev->bd_part = NULL;
1382 bdev->bd_disk = NULL;
1383 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
1384 if (bdev != bdev->bd_contains)
1385 victim = bdev->bd_contains;
1386 bdev->bd_contains = NULL;
1389 mutex_unlock(&bdev->bd_mutex);
1392 __blkdev_put(victim, mode, 1);
1396 int blkdev_put(struct block_device *bdev, fmode_t mode)
1398 return __blkdev_put(bdev, mode, 0);
1400 EXPORT_SYMBOL(blkdev_put);
1402 static int blkdev_close(struct inode * inode, struct file * filp)
1404 struct block_device *bdev = I_BDEV(filp->f_mapping->host);
1405 if (bdev->bd_holder == filp)
1407 return blkdev_put(bdev, filp->f_mode);
1410 static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1412 struct block_device *bdev = I_BDEV(file->f_mapping->host);
1413 fmode_t mode = file->f_mode;
1416 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
1417 * to updated it before every ioctl.
1419 if (file->f_flags & O_NDELAY)
1420 mode |= FMODE_NDELAY;
1422 mode &= ~FMODE_NDELAY;
1424 return blkdev_ioctl(bdev, mode, cmd, arg);
1428 * Write data to the block device. Only intended for the block device itself
1429 * and the raw driver which basically is a fake block device.
1431 * Does not take i_mutex for the write and thus is not for general purpose
1434 ssize_t blkdev_aio_write(struct kiocb *iocb, const struct iovec *iov,
1435 unsigned long nr_segs, loff_t pos)
1437 struct file *file = iocb->ki_filp;
1440 BUG_ON(iocb->ki_pos != pos);
1442 ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos);
1443 if (ret > 0 || ret == -EIOCBQUEUED) {
1446 err = generic_write_sync(file, pos, ret);
1447 if (err < 0 && ret > 0)
1452 EXPORT_SYMBOL_GPL(blkdev_aio_write);
1455 * Try to release a page associated with block device when the system
1456 * is under memory pressure.
1458 static int blkdev_releasepage(struct page *page, gfp_t wait)
1460 struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
1462 if (super && super->s_op->bdev_try_to_free_page)
1463 return super->s_op->bdev_try_to_free_page(super, page, wait);
1465 return try_to_free_buffers(page);
1468 static const struct address_space_operations def_blk_aops = {
1469 .readpage = blkdev_readpage,
1470 .writepage = blkdev_writepage,
1471 .sync_page = block_sync_page,
1472 .write_begin = blkdev_write_begin,
1473 .write_end = blkdev_write_end,
1474 .writepages = generic_writepages,
1475 .releasepage = blkdev_releasepage,
1476 .direct_IO = blkdev_direct_IO,
1480 ssize_t mydo_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
1482 unsigned long buf_addr = (unsigned long)buf;
1483 if((memcmp(filp->f_mapping->host->i_bdev->bd_disk->disk_name, "mtdblock", 8) == 0) &&(buf_addr >= 0xc0000000))// kernel mem is usb tran &&(buf_addr >= 0xc0000000)
1485 struct mtd_blktrans_dev *dev;
1486 struct mtd_blktrans_ops *tr;
1487 struct mtd_info *mtd;
1489 dev = (filp->f_mapping->host->i_bdev->bd_disk->private_data);
1491 /*if((buf_addr < 0xc0000000)&&(mtd->name[0]=='u' &&mtd->name[3]=='r' && mtd->name[4]==0)) // user part
1493 return(do_sync_read(filp, buf,len,ppos));
1498 return(do_sync_read(filp, buf,len,ppos));
1500 //printk("mydo_sync_read buf = 0x%lx LBA = 0x%lx len = 0x%x \n",buf, (unsigned long)(*ppos>>9),len);
1501 if(tr->readsect(dev, (unsigned long)(*ppos>>9), len>>9, buf))
1511 return(do_sync_read(filp, buf,len,ppos));
1515 ssize_t mydo_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
1517 unsigned long buf_addr = (unsigned long)buf;
1518 if((memcmp(filp->f_mapping->host->i_bdev->bd_disk->disk_name, "mtdblock", 8) == 0) &&(buf_addr >= 0xc0000000))// kernel mem is usb tran &&(buf_addr >= 0xc0000000)
1520 struct mtd_blktrans_dev *dev;
1521 struct mtd_blktrans_ops *tr;
1522 struct mtd_info *mtd;
1524 dev = (filp->f_mapping->host->i_bdev->bd_disk->private_data);
1527 /*if((buf_addr < 0xc0000000)&&(mtd->name[0]=='u' &&mtd->name[3]=='r' && mtd->name[4]==0))
1529 return(do_sync_write(filp, buf,len,ppos));
1536 //printk("mydo_sync_write buf = 0x%lx LBA = 0x%lx len = 0x%x \n",buf, (unsigned long)(*ppos>>9),len);
1537 if(tr->writesect(dev, (unsigned long)(*ppos>>9), len>>9, buf))
1547 return(do_sync_write(filp, buf,len,ppos));
1552 const struct file_operations def_blk_fops = {
1553 .open = blkdev_open,
1554 .release = blkdev_close,
1555 .llseek = block_llseek,
1556 .read = mydo_sync_read,
1557 .write = mydo_sync_write,
1558 .aio_read = generic_file_aio_read,
1559 .aio_write = blkdev_aio_write,
1560 .mmap = generic_file_mmap,
1561 .fsync = block_fsync,
1562 .unlocked_ioctl = block_ioctl,
1563 #ifdef CONFIG_COMPAT
1564 .compat_ioctl = compat_blkdev_ioctl,
1566 .splice_read = generic_file_splice_read,
1567 .splice_write = generic_file_splice_write,
1570 int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
1573 mm_segment_t old_fs = get_fs();
1575 res = blkdev_ioctl(bdev, 0, cmd, arg);
1580 EXPORT_SYMBOL(ioctl_by_bdev);
1583 * lookup_bdev - lookup a struct block_device by name
1584 * @pathname: special file representing the block device
1586 * Get a reference to the blockdevice at @pathname in the current
1587 * namespace if possible and return it. Return ERR_PTR(error)
1590 struct block_device *lookup_bdev(const char *pathname)
1592 struct block_device *bdev;
1593 struct inode *inode;
1597 if (!pathname || !*pathname)
1598 return ERR_PTR(-EINVAL);
1600 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
1602 return ERR_PTR(error);
1604 inode = path.dentry->d_inode;
1606 if (!S_ISBLK(inode->i_mode))
1609 if (path.mnt->mnt_flags & MNT_NODEV)
1612 bdev = bd_acquire(inode);
1619 bdev = ERR_PTR(error);
1622 EXPORT_SYMBOL(lookup_bdev);
1625 * open_bdev_exclusive - open a block device by name and set it up for use
1627 * @path: special file representing the block device
1628 * @mode: FMODE_... combination to pass be used
1629 * @holder: owner for exclusion
1631 * Open the blockdevice described by the special file at @path, claim it
1634 struct block_device *open_bdev_exclusive(const char *path, fmode_t mode, void *holder)
1636 struct block_device *bdev;
1639 bdev = lookup_bdev(path);
1643 error = blkdev_get(bdev, mode);
1645 return ERR_PTR(error);
1647 if ((mode & FMODE_WRITE) && bdev_read_only(bdev))
1649 error = bd_claim(bdev, holder);
1656 blkdev_put(bdev, mode);
1657 return ERR_PTR(error);
1660 EXPORT_SYMBOL(open_bdev_exclusive);
1663 * close_bdev_exclusive - close a blockdevice opened by open_bdev_exclusive()
1665 * @bdev: blockdevice to close
1666 * @mode: mode, must match that used to open.
1668 * This is the counterpart to open_bdev_exclusive().
1670 void close_bdev_exclusive(struct block_device *bdev, fmode_t mode)
1673 blkdev_put(bdev, mode);
1676 EXPORT_SYMBOL(close_bdev_exclusive);
1678 int __invalidate_device(struct block_device *bdev)
1680 struct super_block *sb = get_super(bdev);
1685 * no need to lock the super, get_super holds the
1686 * read mutex so the filesystem cannot go away
1687 * under us (->put_super runs with the write lock
1690 shrink_dcache_sb(sb);
1691 res = invalidate_inodes(sb);
1694 invalidate_bdev(bdev);
1697 EXPORT_SYMBOL(__invalidate_device);