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(rw, iocb, inode, I_BDEV(inode), iov, offset,
178 nr_segs, blkdev_get_blocks, NULL, NULL, 0);
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 error = freeze_super(sb);
252 deactivate_super(sb);
253 bdev->bd_fsfreeze_count--;
254 mutex_unlock(&bdev->bd_fsfreeze_mutex);
255 return ERR_PTR(error);
257 deactivate_super(sb);
260 mutex_unlock(&bdev->bd_fsfreeze_mutex);
261 return sb; /* thaw_bdev releases s->s_umount */
263 EXPORT_SYMBOL(freeze_bdev);
266 * thaw_bdev -- unlock filesystem
267 * @bdev: blockdevice to unlock
268 * @sb: associated superblock
270 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
272 int thaw_bdev(struct block_device *bdev, struct super_block *sb)
276 mutex_lock(&bdev->bd_fsfreeze_mutex);
277 if (!bdev->bd_fsfreeze_count)
281 if (--bdev->bd_fsfreeze_count > 0)
287 error = thaw_super(sb);
289 bdev->bd_fsfreeze_count++;
290 mutex_unlock(&bdev->bd_fsfreeze_mutex);
294 mutex_unlock(&bdev->bd_fsfreeze_mutex);
297 EXPORT_SYMBOL(thaw_bdev);
299 static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
301 return block_write_full_page(page, blkdev_get_block, wbc);
304 static int blkdev_readpage(struct file * file, struct page * page)
306 return block_read_full_page(page, blkdev_get_block);
309 static int blkdev_write_begin(struct file *file, struct address_space *mapping,
310 loff_t pos, unsigned len, unsigned flags,
311 struct page **pagep, void **fsdata)
313 return block_write_begin(mapping, pos, len, flags, pagep,
317 static int blkdev_write_end(struct file *file, struct address_space *mapping,
318 loff_t pos, unsigned len, unsigned copied,
319 struct page *page, void *fsdata)
322 ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
325 page_cache_release(page);
332 * for a block special file file->f_path.dentry->d_inode->i_size is zero
333 * so we compute the size by hand (just as in block_read/write above)
335 static loff_t block_llseek(struct file *file, loff_t offset, int origin)
337 struct inode *bd_inode = file->f_mapping->host;
341 mutex_lock(&bd_inode->i_mutex);
342 size = i_size_read(bd_inode);
349 offset += file->f_pos;
352 if (offset >= 0 && offset <= size) {
353 if (offset != file->f_pos) {
354 file->f_pos = offset;
358 mutex_unlock(&bd_inode->i_mutex);
362 int blkdev_fsync(struct file *filp, int datasync)
364 struct inode *bd_inode = filp->f_mapping->host;
365 struct block_device *bdev = I_BDEV(bd_inode);
369 * There is no need to serialise calls to blkdev_issue_flush with
370 * i_mutex and doing so causes performance issues with concurrent
371 * O_SYNC writers to a block device.
373 mutex_unlock(&bd_inode->i_mutex);
375 error = blkdev_issue_flush(bdev, GFP_KERNEL, NULL, BLKDEV_IFL_WAIT);
376 if (error == -EOPNOTSUPP)
379 mutex_lock(&bd_inode->i_mutex);
383 EXPORT_SYMBOL(blkdev_fsync);
389 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
390 static struct kmem_cache * bdev_cachep __read_mostly;
392 static struct inode *bdev_alloc_inode(struct super_block *sb)
394 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
397 return &ei->vfs_inode;
400 static void bdev_destroy_inode(struct inode *inode)
402 struct bdev_inode *bdi = BDEV_I(inode);
404 kmem_cache_free(bdev_cachep, bdi);
407 static void init_once(void *foo)
409 struct bdev_inode *ei = (struct bdev_inode *) foo;
410 struct block_device *bdev = &ei->bdev;
412 memset(bdev, 0, sizeof(*bdev));
413 mutex_init(&bdev->bd_mutex);
414 INIT_LIST_HEAD(&bdev->bd_inodes);
415 INIT_LIST_HEAD(&bdev->bd_list);
417 INIT_LIST_HEAD(&bdev->bd_holder_list);
419 inode_init_once(&ei->vfs_inode);
420 /* Initialize mutex for freeze. */
421 mutex_init(&bdev->bd_fsfreeze_mutex);
424 static inline void __bd_forget(struct inode *inode)
426 list_del_init(&inode->i_devices);
427 inode->i_bdev = NULL;
428 inode->i_mapping = &inode->i_data;
431 static void bdev_evict_inode(struct inode *inode)
433 struct block_device *bdev = &BDEV_I(inode)->bdev;
435 truncate_inode_pages(&inode->i_data, 0);
436 invalidate_inode_buffers(inode); /* is it needed here? */
437 end_writeback(inode);
438 spin_lock(&bdev_lock);
439 while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
440 __bd_forget(list_entry(p, struct inode, i_devices));
442 list_del_init(&bdev->bd_list);
443 spin_unlock(&bdev_lock);
446 static const struct super_operations bdev_sops = {
447 .statfs = simple_statfs,
448 .alloc_inode = bdev_alloc_inode,
449 .destroy_inode = bdev_destroy_inode,
450 .drop_inode = generic_delete_inode,
451 .evict_inode = bdev_evict_inode,
454 static int bd_get_sb(struct file_system_type *fs_type,
455 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
457 return get_sb_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576, mnt);
460 static struct file_system_type bd_type = {
463 .kill_sb = kill_anon_super,
466 struct super_block *blockdev_superblock __read_mostly;
468 void __init bdev_cache_init(void)
471 struct vfsmount *bd_mnt;
473 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
474 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
475 SLAB_MEM_SPREAD|SLAB_PANIC),
477 err = register_filesystem(&bd_type);
479 panic("Cannot register bdev pseudo-fs");
480 bd_mnt = kern_mount(&bd_type);
482 panic("Cannot create bdev pseudo-fs");
484 * This vfsmount structure is only used to obtain the
485 * blockdev_superblock, so tell kmemleak not to report it.
487 kmemleak_not_leak(bd_mnt);
488 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
492 * Most likely _very_ bad one - but then it's hardly critical for small
493 * /dev and can be fixed when somebody will need really large one.
494 * Keep in mind that it will be fed through icache hash function too.
496 static inline unsigned long hash(dev_t dev)
498 return MAJOR(dev)+MINOR(dev);
501 static int bdev_test(struct inode *inode, void *data)
503 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
506 static int bdev_set(struct inode *inode, void *data)
508 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
512 static LIST_HEAD(all_bdevs);
514 struct block_device *bdget(dev_t dev)
516 struct block_device *bdev;
519 inode = iget5_locked(blockdev_superblock, hash(dev),
520 bdev_test, bdev_set, &dev);
525 bdev = &BDEV_I(inode)->bdev;
527 if (inode->i_state & I_NEW) {
528 bdev->bd_contains = NULL;
529 bdev->bd_inode = inode;
530 bdev->bd_block_size = (1 << inode->i_blkbits);
531 bdev->bd_part_count = 0;
532 bdev->bd_invalidated = 0;
533 inode->i_mode = S_IFBLK;
535 inode->i_bdev = bdev;
536 inode->i_data.a_ops = &def_blk_aops;
537 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
538 inode->i_data.backing_dev_info = &default_backing_dev_info;
539 spin_lock(&bdev_lock);
540 list_add(&bdev->bd_list, &all_bdevs);
541 spin_unlock(&bdev_lock);
542 unlock_new_inode(inode);
547 EXPORT_SYMBOL(bdget);
550 * bdgrab -- Grab a reference to an already referenced block device
551 * @bdev: Block device to grab a reference to.
553 struct block_device *bdgrab(struct block_device *bdev)
555 atomic_inc(&bdev->bd_inode->i_count);
559 long nr_blockdev_pages(void)
561 struct block_device *bdev;
563 spin_lock(&bdev_lock);
564 list_for_each_entry(bdev, &all_bdevs, bd_list) {
565 ret += bdev->bd_inode->i_mapping->nrpages;
567 spin_unlock(&bdev_lock);
571 void bdput(struct block_device *bdev)
573 iput(bdev->bd_inode);
576 EXPORT_SYMBOL(bdput);
578 static struct block_device *bd_acquire(struct inode *inode)
580 struct block_device *bdev;
582 spin_lock(&bdev_lock);
583 bdev = inode->i_bdev;
585 atomic_inc(&bdev->bd_inode->i_count);
586 spin_unlock(&bdev_lock);
589 spin_unlock(&bdev_lock);
591 bdev = bdget(inode->i_rdev);
593 spin_lock(&bdev_lock);
594 if (!inode->i_bdev) {
596 * We take an additional bd_inode->i_count for inode,
597 * and it's released in clear_inode() of inode.
598 * So, we can access it via ->i_mapping always
601 atomic_inc(&bdev->bd_inode->i_count);
602 inode->i_bdev = bdev;
603 inode->i_mapping = bdev->bd_inode->i_mapping;
604 list_add(&inode->i_devices, &bdev->bd_inodes);
606 spin_unlock(&bdev_lock);
611 /* Call when you free inode */
613 void bd_forget(struct inode *inode)
615 struct block_device *bdev = NULL;
617 spin_lock(&bdev_lock);
619 if (!sb_is_blkdev_sb(inode->i_sb))
620 bdev = inode->i_bdev;
623 spin_unlock(&bdev_lock);
626 iput(bdev->bd_inode);
630 * bd_may_claim - test whether a block device can be claimed
631 * @bdev: block device of interest
632 * @whole: whole block device containing @bdev, may equal @bdev
633 * @holder: holder trying to claim @bdev
635 * Test whther @bdev can be claimed by @holder.
638 * spin_lock(&bdev_lock).
641 * %true if @bdev can be claimed, %false otherwise.
643 static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
646 if (bdev->bd_holder == holder)
647 return true; /* already a holder */
648 else if (bdev->bd_holder != NULL)
649 return false; /* held by someone else */
650 else if (bdev->bd_contains == bdev)
651 return true; /* is a whole device which isn't held */
653 else if (whole->bd_holder == bd_claim)
654 return true; /* is a partition of a device that is being partitioned */
655 else if (whole->bd_holder != NULL)
656 return false; /* is a partition of a held device */
658 return true; /* is a partition of an un-held device */
662 * bd_prepare_to_claim - prepare to claim a block device
663 * @bdev: block device of interest
664 * @whole: the whole device containing @bdev, may equal @bdev
665 * @holder: holder trying to claim @bdev
667 * Prepare to claim @bdev. This function fails if @bdev is already
668 * claimed by another holder and waits if another claiming is in
669 * progress. This function doesn't actually claim. On successful
670 * return, the caller has ownership of bd_claiming and bd_holder[s].
673 * spin_lock(&bdev_lock). Might release bdev_lock, sleep and regrab
677 * 0 if @bdev can be claimed, -EBUSY otherwise.
679 static int bd_prepare_to_claim(struct block_device *bdev,
680 struct block_device *whole, void *holder)
683 /* if someone else claimed, fail */
684 if (!bd_may_claim(bdev, whole, holder))
687 /* if claiming is already in progress, wait for it to finish */
688 if (whole->bd_claiming) {
689 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
692 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
693 spin_unlock(&bdev_lock);
695 finish_wait(wq, &wait);
696 spin_lock(&bdev_lock);
705 * bd_start_claiming - start claiming a block device
706 * @bdev: block device of interest
707 * @holder: holder trying to claim @bdev
709 * @bdev is about to be opened exclusively. Check @bdev can be opened
710 * exclusively and mark that an exclusive open is in progress. Each
711 * successful call to this function must be matched with a call to
712 * either bd_finish_claiming() or bd_abort_claiming() (which do not
715 * This function is used to gain exclusive access to the block device
716 * without actually causing other exclusive open attempts to fail. It
717 * should be used when the open sequence itself requires exclusive
718 * access but may subsequently fail.
724 * Pointer to the block device containing @bdev on success, ERR_PTR()
727 static struct block_device *bd_start_claiming(struct block_device *bdev,
730 struct gendisk *disk;
731 struct block_device *whole;
737 * @bdev might not have been initialized properly yet, look up
738 * and grab the outer block device the hard way.
740 disk = get_gendisk(bdev->bd_dev, &partno);
742 return ERR_PTR(-ENXIO);
744 whole = bdget_disk(disk, 0);
745 module_put(disk->fops->owner);
748 return ERR_PTR(-ENOMEM);
750 /* prepare to claim, if successful, mark claiming in progress */
751 spin_lock(&bdev_lock);
753 err = bd_prepare_to_claim(bdev, whole, holder);
755 whole->bd_claiming = holder;
756 spin_unlock(&bdev_lock);
759 spin_unlock(&bdev_lock);
765 /* releases bdev_lock */
766 static void __bd_abort_claiming(struct block_device *whole, void *holder)
768 BUG_ON(whole->bd_claiming != holder);
769 whole->bd_claiming = NULL;
770 wake_up_bit(&whole->bd_claiming, 0);
772 spin_unlock(&bdev_lock);
777 * bd_abort_claiming - abort claiming a block device
778 * @whole: whole block device returned by bd_start_claiming()
779 * @holder: holder trying to claim @bdev
781 * Abort a claiming block started by bd_start_claiming(). Note that
782 * @whole is not the block device to be claimed but the whole device
783 * returned by bd_start_claiming().
786 * Grabs and releases bdev_lock.
788 static void bd_abort_claiming(struct block_device *whole, void *holder)
790 spin_lock(&bdev_lock);
791 __bd_abort_claiming(whole, holder); /* releases bdev_lock */
794 /* increment holders when we have a legitimate claim. requires bdev_lock */
795 static void __bd_claim(struct block_device *bdev, struct block_device *whole,
798 /* note that for a whole device bd_holders
799 * will be incremented twice, and bd_holder will
800 * be set to bd_claim before being set to holder
803 whole->bd_holder = bd_claim;
805 bdev->bd_holder = holder;
809 * bd_finish_claiming - finish claiming a block device
810 * @bdev: block device of interest (passed to bd_start_claiming())
811 * @whole: whole block device returned by bd_start_claiming()
812 * @holder: holder trying to claim @bdev
814 * Finish a claiming block started by bd_start_claiming().
817 * Grabs and releases bdev_lock.
819 static void bd_finish_claiming(struct block_device *bdev,
820 struct block_device *whole, void *holder)
822 spin_lock(&bdev_lock);
823 BUG_ON(!bd_may_claim(bdev, whole, holder));
824 __bd_claim(bdev, whole, holder);
825 __bd_abort_claiming(whole, holder); /* not actually an abort */
829 * bd_claim - claim a block device
830 * @bdev: block device to claim
831 * @holder: holder trying to claim @bdev
833 * Try to claim @bdev which must have been opened successfully.
839 * 0 if successful, -EBUSY if @bdev is already claimed.
841 int bd_claim(struct block_device *bdev, void *holder)
843 struct block_device *whole = bdev->bd_contains;
848 spin_lock(&bdev_lock);
849 res = bd_prepare_to_claim(bdev, whole, holder);
851 __bd_claim(bdev, whole, holder);
852 spin_unlock(&bdev_lock);
856 EXPORT_SYMBOL(bd_claim);
858 void bd_release(struct block_device *bdev)
860 spin_lock(&bdev_lock);
861 if (!--bdev->bd_contains->bd_holders)
862 bdev->bd_contains->bd_holder = NULL;
863 if (!--bdev->bd_holders)
864 bdev->bd_holder = NULL;
865 spin_unlock(&bdev_lock);
868 EXPORT_SYMBOL(bd_release);
872 * Functions for bd_claim_by_kobject / bd_release_from_kobject
874 * If a kobject is passed to bd_claim_by_kobject()
875 * and the kobject has a parent directory,
876 * following symlinks are created:
877 * o from the kobject to the claimed bdev
878 * o from "holders" directory of the bdev to the parent of the kobject
879 * bd_release_from_kobject() removes these symlinks.
882 * If /dev/dm-0 maps to /dev/sda, kobject corresponding to
883 * /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then:
884 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
885 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
888 static int add_symlink(struct kobject *from, struct kobject *to)
892 return sysfs_create_link(from, to, kobject_name(to));
895 static void del_symlink(struct kobject *from, struct kobject *to)
899 sysfs_remove_link(from, kobject_name(to));
903 * 'struct bd_holder' contains pointers to kobjects symlinked by
904 * bd_claim_by_kobject.
905 * It's connected to bd_holder_list which is protected by bdev->bd_sem.
908 struct list_head list; /* chain of holders of the bdev */
909 int count; /* references from the holder */
910 struct kobject *sdir; /* holder object, e.g. "/block/dm-0/slaves" */
911 struct kobject *hdev; /* e.g. "/block/dm-0" */
912 struct kobject *hdir; /* e.g. "/block/sda/holders" */
913 struct kobject *sdev; /* e.g. "/block/sda" */
917 * Get references of related kobjects at once.
918 * Returns 1 on success. 0 on failure.
920 * Should call bd_holder_release_dirs() after successful use.
922 static int bd_holder_grab_dirs(struct block_device *bdev,
923 struct bd_holder *bo)
928 bo->sdir = kobject_get(bo->sdir);
932 bo->hdev = kobject_get(bo->sdir->parent);
936 bo->sdev = kobject_get(&part_to_dev(bdev->bd_part)->kobj);
940 bo->hdir = kobject_get(bdev->bd_part->holder_dir);
947 kobject_put(bo->sdev);
949 kobject_put(bo->hdev);
951 kobject_put(bo->sdir);
956 /* Put references of related kobjects at once. */
957 static void bd_holder_release_dirs(struct bd_holder *bo)
959 kobject_put(bo->hdir);
960 kobject_put(bo->sdev);
961 kobject_put(bo->hdev);
962 kobject_put(bo->sdir);
965 static struct bd_holder *alloc_bd_holder(struct kobject *kobj)
967 struct bd_holder *bo;
969 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
979 static void free_bd_holder(struct bd_holder *bo)
985 * find_bd_holder - find matching struct bd_holder from the block device
987 * @bdev: struct block device to be searched
988 * @bo: target struct bd_holder
990 * Returns matching entry with @bo in @bdev->bd_holder_list.
991 * If found, increment the reference count and return the pointer.
992 * If not found, returns NULL.
994 static struct bd_holder *find_bd_holder(struct block_device *bdev,
995 struct bd_holder *bo)
997 struct bd_holder *tmp;
999 list_for_each_entry(tmp, &bdev->bd_holder_list, list)
1000 if (tmp->sdir == bo->sdir) {
1009 * add_bd_holder - create sysfs symlinks for bd_claim() relationship
1011 * @bdev: block device to be bd_claimed
1012 * @bo: preallocated and initialized by alloc_bd_holder()
1014 * Add @bo to @bdev->bd_holder_list, create symlinks.
1016 * Returns 0 if symlinks are created.
1017 * Returns -ve if something fails.
1019 static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
1026 if (!bd_holder_grab_dirs(bdev, bo))
1029 err = add_symlink(bo->sdir, bo->sdev);
1033 err = add_symlink(bo->hdir, bo->hdev);
1035 del_symlink(bo->sdir, bo->sdev);
1039 list_add_tail(&bo->list, &bdev->bd_holder_list);
1044 * del_bd_holder - delete sysfs symlinks for bd_claim() relationship
1046 * @bdev: block device to be bd_claimed
1047 * @kobj: holder's kobject
1049 * If there is matching entry with @kobj in @bdev->bd_holder_list
1050 * and no other bd_claim() from the same kobject,
1051 * remove the struct bd_holder from the list, delete symlinks for it.
1053 * Returns a pointer to the struct bd_holder when it's removed from the list
1054 * and ready to be freed.
1055 * Returns NULL if matching claim isn't found or there is other bd_claim()
1056 * by the same kobject.
1058 static struct bd_holder *del_bd_holder(struct block_device *bdev,
1059 struct kobject *kobj)
1061 struct bd_holder *bo;
1063 list_for_each_entry(bo, &bdev->bd_holder_list, list) {
1064 if (bo->sdir == kobj) {
1066 BUG_ON(bo->count < 0);
1068 list_del(&bo->list);
1069 del_symlink(bo->sdir, bo->sdev);
1070 del_symlink(bo->hdir, bo->hdev);
1071 bd_holder_release_dirs(bo);
1082 * bd_claim_by_kobject - bd_claim() with additional kobject signature
1084 * @bdev: block device to be claimed
1085 * @holder: holder's signature
1086 * @kobj: holder's kobject
1088 * Do bd_claim() and if it succeeds, create sysfs symlinks between
1089 * the bdev and the holder's kobject.
1090 * Use bd_release_from_kobject() when relesing the claimed bdev.
1092 * Returns 0 on success. (same as bd_claim())
1093 * Returns errno on failure.
1095 static int bd_claim_by_kobject(struct block_device *bdev, void *holder,
1096 struct kobject *kobj)
1099 struct bd_holder *bo, *found;
1104 bo = alloc_bd_holder(kobj);
1108 mutex_lock(&bdev->bd_mutex);
1110 err = bd_claim(bdev, holder);
1114 found = find_bd_holder(bdev, bo);
1118 err = add_bd_holder(bdev, bo);
1124 mutex_unlock(&bdev->bd_mutex);
1130 * bd_release_from_kobject - bd_release() with additional kobject signature
1132 * @bdev: block device to be released
1133 * @kobj: holder's kobject
1135 * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject().
1137 static void bd_release_from_kobject(struct block_device *bdev,
1138 struct kobject *kobj)
1143 mutex_lock(&bdev->bd_mutex);
1145 free_bd_holder(del_bd_holder(bdev, kobj));
1146 mutex_unlock(&bdev->bd_mutex);
1150 * bd_claim_by_disk - wrapper function for bd_claim_by_kobject()
1152 * @bdev: block device to be claimed
1153 * @holder: holder's signature
1154 * @disk: holder's gendisk
1156 * Call bd_claim_by_kobject() with getting @disk->slave_dir.
1158 int bd_claim_by_disk(struct block_device *bdev, void *holder,
1159 struct gendisk *disk)
1161 return bd_claim_by_kobject(bdev, holder, kobject_get(disk->slave_dir));
1163 EXPORT_SYMBOL_GPL(bd_claim_by_disk);
1166 * bd_release_from_disk - wrapper function for bd_release_from_kobject()
1168 * @bdev: block device to be claimed
1169 * @disk: holder's gendisk
1171 * Call bd_release_from_kobject() and put @disk->slave_dir.
1173 void bd_release_from_disk(struct block_device *bdev, struct gendisk *disk)
1175 bd_release_from_kobject(bdev, disk->slave_dir);
1176 kobject_put(disk->slave_dir);
1178 EXPORT_SYMBOL_GPL(bd_release_from_disk);
1182 * Tries to open block device by device number. Use it ONLY if you
1183 * really do not have anything better - i.e. when you are behind a
1184 * truly sucky interface and all you are given is a device number. _Never_
1185 * to be used for internal purposes. If you ever need it - reconsider
1188 struct block_device *open_by_devnum(dev_t dev, fmode_t mode)
1190 struct block_device *bdev = bdget(dev);
1193 err = blkdev_get(bdev, mode);
1194 return err ? ERR_PTR(err) : bdev;
1197 EXPORT_SYMBOL(open_by_devnum);
1200 * flush_disk - invalidates all buffer-cache entries on a disk
1202 * @bdev: struct block device to be flushed
1204 * Invalidates all buffer-cache entries on a disk. It should be called
1205 * when a disk has been changed -- either by a media change or online
1208 static void flush_disk(struct block_device *bdev)
1210 if (__invalidate_device(bdev)) {
1211 char name[BDEVNAME_SIZE] = "";
1214 disk_name(bdev->bd_disk, 0, name);
1215 printk(KERN_WARNING "VFS: busy inodes on changed media or "
1216 "resized disk %s\n", name);
1221 if (disk_partitionable(bdev->bd_disk))
1222 bdev->bd_invalidated = 1;
1226 * check_disk_size_change - checks for disk size change and adjusts bdev size.
1227 * @disk: struct gendisk to check
1228 * @bdev: struct bdev to adjust.
1230 * This routine checks to see if the bdev size does not match the disk size
1231 * and adjusts it if it differs.
1233 void check_disk_size_change(struct gendisk *disk, struct block_device *bdev)
1235 loff_t disk_size, bdev_size;
1237 disk_size = (loff_t)get_capacity(disk) << 9;
1238 bdev_size = i_size_read(bdev->bd_inode);
1239 if (disk_size != bdev_size) {
1240 char name[BDEVNAME_SIZE];
1242 disk_name(disk, 0, name);
1244 "%s: detected capacity change from %lld to %lld\n",
1245 name, bdev_size, disk_size);
1246 i_size_write(bdev->bd_inode, disk_size);
1250 EXPORT_SYMBOL(check_disk_size_change);
1253 * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back
1254 * @disk: struct gendisk to be revalidated
1256 * This routine is a wrapper for lower-level driver's revalidate_disk
1257 * call-backs. It is used to do common pre and post operations needed
1258 * for all revalidate_disk operations.
1260 int revalidate_disk(struct gendisk *disk)
1262 struct block_device *bdev;
1265 if (disk->fops->revalidate_disk)
1266 ret = disk->fops->revalidate_disk(disk);
1268 bdev = bdget_disk(disk, 0);
1272 mutex_lock(&bdev->bd_mutex);
1273 check_disk_size_change(disk, bdev);
1274 mutex_unlock(&bdev->bd_mutex);
1278 EXPORT_SYMBOL(revalidate_disk);
1281 * This routine checks whether a removable media has been changed,
1282 * and invalidates all buffer-cache-entries in that case. This
1283 * is a relatively slow routine, so we have to try to minimize using
1284 * it. Thus it is called only upon a 'mount' or 'open'. This
1285 * is the best way of combining speed and utility, I think.
1286 * People changing diskettes in the middle of an operation deserve
1289 int check_disk_change(struct block_device *bdev)
1291 struct gendisk *disk = bdev->bd_disk;
1292 const struct block_device_operations *bdops = disk->fops;
1294 if (!bdops->media_changed)
1296 if (!bdops->media_changed(bdev->bd_disk))
1300 if (bdops->revalidate_disk)
1301 bdops->revalidate_disk(bdev->bd_disk);
1305 EXPORT_SYMBOL(check_disk_change);
1307 void bd_set_size(struct block_device *bdev, loff_t size)
1309 unsigned bsize = bdev_logical_block_size(bdev);
1311 bdev->bd_inode->i_size = size;
1312 while (bsize < PAGE_CACHE_SIZE) {
1317 bdev->bd_block_size = bsize;
1318 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
1320 EXPORT_SYMBOL(bd_set_size);
1322 static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
1327 * mutex_lock(part->bd_mutex)
1328 * mutex_lock_nested(whole->bd_mutex, 1)
1331 static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
1333 struct gendisk *disk;
1338 if (mode & FMODE_READ)
1340 if (mode & FMODE_WRITE)
1343 * hooks: /n/, see "layering violations".
1346 ret = devcgroup_inode_permission(bdev->bd_inode, perm);
1356 disk = get_gendisk(bdev->bd_dev, &partno);
1360 mutex_lock_nested(&bdev->bd_mutex, for_part);
1361 if (!bdev->bd_openers) {
1362 bdev->bd_disk = disk;
1363 bdev->bd_contains = bdev;
1365 struct backing_dev_info *bdi;
1368 bdev->bd_part = disk_get_part(disk, partno);
1372 if (disk->fops->open) {
1373 ret = disk->fops->open(bdev, mode);
1374 if (ret == -ERESTARTSYS) {
1375 /* Lost a race with 'disk' being
1376 * deleted, try again.
1379 disk_put_part(bdev->bd_part);
1380 bdev->bd_part = NULL;
1381 module_put(disk->fops->owner);
1383 bdev->bd_disk = NULL;
1384 mutex_unlock(&bdev->bd_mutex);
1390 if (!bdev->bd_openers) {
1391 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
1392 bdi = blk_get_backing_dev_info(bdev);
1394 bdi = &default_backing_dev_info;
1395 bdev->bd_inode->i_data.backing_dev_info = bdi;
1397 if (bdev->bd_invalidated)
1398 rescan_partitions(disk, bdev);
1400 struct block_device *whole;
1401 whole = bdget_disk(disk, 0);
1406 ret = __blkdev_get(whole, mode, 1);
1409 bdev->bd_contains = whole;
1410 bdev->bd_inode->i_data.backing_dev_info =
1411 whole->bd_inode->i_data.backing_dev_info;
1412 bdev->bd_part = disk_get_part(disk, partno);
1413 if (!(disk->flags & GENHD_FL_UP) ||
1414 !bdev->bd_part || !bdev->bd_part->nr_sects) {
1418 bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9);
1421 module_put(disk->fops->owner);
1424 if (bdev->bd_contains == bdev) {
1425 if (bdev->bd_disk->fops->open) {
1426 ret = bdev->bd_disk->fops->open(bdev, mode);
1428 goto out_unlock_bdev;
1430 if (bdev->bd_invalidated)
1431 rescan_partitions(bdev->bd_disk, bdev);
1436 bdev->bd_part_count++;
1437 mutex_unlock(&bdev->bd_mutex);
1441 disk_put_part(bdev->bd_part);
1442 bdev->bd_disk = NULL;
1443 bdev->bd_part = NULL;
1444 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
1445 if (bdev != bdev->bd_contains)
1446 __blkdev_put(bdev->bd_contains, mode, 1);
1447 bdev->bd_contains = NULL;
1449 mutex_unlock(&bdev->bd_mutex);
1452 module_put(disk->fops->owner);
1459 int blkdev_get(struct block_device *bdev, fmode_t mode)
1461 return __blkdev_get(bdev, mode, 0);
1463 EXPORT_SYMBOL(blkdev_get);
1465 static int blkdev_open(struct inode * inode, struct file * filp)
1467 struct block_device *whole = NULL;
1468 struct block_device *bdev;
1472 * Preserve backwards compatibility and allow large file access
1473 * even if userspace doesn't ask for it explicitly. Some mkfs
1474 * binary needs it. We might want to drop this workaround
1475 * during an unstable branch.
1477 filp->f_flags |= O_LARGEFILE;
1479 if (filp->f_flags & O_NDELAY)
1480 filp->f_mode |= FMODE_NDELAY;
1481 if (filp->f_flags & O_EXCL)
1482 filp->f_mode |= FMODE_EXCL;
1483 if ((filp->f_flags & O_ACCMODE) == 3)
1484 filp->f_mode |= FMODE_WRITE_IOCTL;
1486 bdev = bd_acquire(inode);
1490 if (filp->f_mode & FMODE_EXCL) {
1491 whole = bd_start_claiming(bdev, filp);
1492 if (IS_ERR(whole)) {
1494 return PTR_ERR(whole);
1498 filp->f_mapping = bdev->bd_inode->i_mapping;
1500 res = blkdev_get(bdev, filp->f_mode);
1504 bd_finish_claiming(bdev, whole, filp);
1506 bd_abort_claiming(whole, filp);
1512 static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
1515 struct gendisk *disk = bdev->bd_disk;
1516 struct block_device *victim = NULL;
1518 mutex_lock_nested(&bdev->bd_mutex, for_part);
1520 bdev->bd_part_count--;
1522 if (!--bdev->bd_openers) {
1523 sync_blockdev(bdev);
1526 if (bdev->bd_contains == bdev) {
1527 if (disk->fops->release)
1528 ret = disk->fops->release(disk, mode);
1530 if (!bdev->bd_openers) {
1531 struct module *owner = disk->fops->owner;
1535 disk_put_part(bdev->bd_part);
1536 bdev->bd_part = NULL;
1537 bdev->bd_disk = NULL;
1538 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
1539 if (bdev != bdev->bd_contains)
1540 victim = bdev->bd_contains;
1541 bdev->bd_contains = NULL;
1543 mutex_unlock(&bdev->bd_mutex);
1546 __blkdev_put(victim, mode, 1);
1550 int blkdev_put(struct block_device *bdev, fmode_t mode)
1552 return __blkdev_put(bdev, mode, 0);
1554 EXPORT_SYMBOL(blkdev_put);
1556 static int blkdev_close(struct inode * inode, struct file * filp)
1558 struct block_device *bdev = I_BDEV(filp->f_mapping->host);
1559 if (bdev->bd_holder == filp)
1561 return blkdev_put(bdev, filp->f_mode);
1564 static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1566 struct block_device *bdev = I_BDEV(file->f_mapping->host);
1567 fmode_t mode = file->f_mode;
1570 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
1571 * to updated it before every ioctl.
1573 if (file->f_flags & O_NDELAY)
1574 mode |= FMODE_NDELAY;
1576 mode &= ~FMODE_NDELAY;
1578 return blkdev_ioctl(bdev, mode, cmd, arg);
1582 * Write data to the block device. Only intended for the block device itself
1583 * and the raw driver which basically is a fake block device.
1585 * Does not take i_mutex for the write and thus is not for general purpose
1588 ssize_t blkdev_aio_write(struct kiocb *iocb, const struct iovec *iov,
1589 unsigned long nr_segs, loff_t pos)
1591 struct file *file = iocb->ki_filp;
1594 BUG_ON(iocb->ki_pos != pos);
1596 ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos);
1597 if (ret > 0 || ret == -EIOCBQUEUED) {
1600 err = generic_write_sync(file, pos, ret);
1601 if (err < 0 && ret > 0)
1606 EXPORT_SYMBOL_GPL(blkdev_aio_write);
1609 * Try to release a page associated with block device when the system
1610 * is under memory pressure.
1612 static int blkdev_releasepage(struct page *page, gfp_t wait)
1614 struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
1616 if (super && super->s_op->bdev_try_to_free_page)
1617 return super->s_op->bdev_try_to_free_page(super, page, wait);
1619 return try_to_free_buffers(page);
1622 static const struct address_space_operations def_blk_aops = {
1623 .readpage = blkdev_readpage,
1624 .writepage = blkdev_writepage,
1625 .sync_page = block_sync_page,
1626 .write_begin = blkdev_write_begin,
1627 .write_end = blkdev_write_end,
1628 .writepages = generic_writepages,
1629 .releasepage = blkdev_releasepage,
1630 .direct_IO = blkdev_direct_IO,
1634 ssize_t mydo_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
1636 unsigned long buf_addr = (unsigned long)buf;
1637 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)
1639 struct mtd_blktrans_dev *dev;
1640 struct mtd_blktrans_ops *tr;
1641 struct mtd_info *mtd;
1643 dev = (filp->f_mapping->host->i_bdev->bd_disk->private_data);
1645 /*if((buf_addr < 0xc0000000)&&(mtd->name[0]=='u' &&mtd->name[3]=='r' && mtd->name[4]==0)) // user part
1647 return(do_sync_read(filp, buf,len,ppos));
1652 return(do_sync_read(filp, buf,len,ppos));
1654 //printk("mydo_sync_read buf = 0x%lx LBA = 0x%lx len = 0x%x \n",buf, (unsigned long)(*ppos>>9),len);
1655 if(tr->readsect(dev, (unsigned long)(*ppos>>9), len>>9, buf))
1665 return(do_sync_read(filp, buf,len,ppos));
1669 ssize_t mydo_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
1671 unsigned long buf_addr = (unsigned long)buf;
1672 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)
1674 struct mtd_blktrans_dev *dev;
1675 struct mtd_blktrans_ops *tr;
1676 struct mtd_info *mtd;
1678 dev = (filp->f_mapping->host->i_bdev->bd_disk->private_data);
1681 /*if((buf_addr < 0xc0000000)&&(mtd->name[0]=='u' &&mtd->name[3]=='r' && mtd->name[4]==0))
1683 return(do_sync_write(filp, buf,len,ppos));
1690 //printk("mydo_sync_write buf = 0x%lx LBA = 0x%lx len = 0x%x \n",buf, (unsigned long)(*ppos>>9),len);
1691 if(tr->writesect(dev, (unsigned long)(*ppos>>9), len>>9, buf))
1701 return(do_sync_write(filp, buf,len,ppos));
1706 const struct file_operations def_blk_fops = {
1707 .open = blkdev_open,
1708 .release = blkdev_close,
1709 .llseek = block_llseek,
1710 .read = mydo_sync_read,
1711 .write = mydo_sync_write,
1712 .aio_read = generic_file_aio_read,
1713 .aio_write = blkdev_aio_write,
1714 .mmap = generic_file_mmap,
1715 .fsync = blkdev_fsync,
1716 .unlocked_ioctl = block_ioctl,
1717 #ifdef CONFIG_COMPAT
1718 .compat_ioctl = compat_blkdev_ioctl,
1720 .splice_read = generic_file_splice_read,
1721 .splice_write = generic_file_splice_write,
1724 int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
1727 mm_segment_t old_fs = get_fs();
1729 res = blkdev_ioctl(bdev, 0, cmd, arg);
1734 EXPORT_SYMBOL(ioctl_by_bdev);
1737 * lookup_bdev - lookup a struct block_device by name
1738 * @pathname: special file representing the block device
1740 * Get a reference to the blockdevice at @pathname in the current
1741 * namespace if possible and return it. Return ERR_PTR(error)
1744 struct block_device *lookup_bdev(const char *pathname)
1746 struct block_device *bdev;
1747 struct inode *inode;
1751 if (!pathname || !*pathname)
1752 return ERR_PTR(-EINVAL);
1754 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
1756 return ERR_PTR(error);
1758 inode = path.dentry->d_inode;
1760 if (!S_ISBLK(inode->i_mode))
1763 if (path.mnt->mnt_flags & MNT_NODEV)
1766 bdev = bd_acquire(inode);
1773 bdev = ERR_PTR(error);
1776 EXPORT_SYMBOL(lookup_bdev);
1779 * open_bdev_exclusive - open a block device by name and set it up for use
1781 * @path: special file representing the block device
1782 * @mode: FMODE_... combination to pass be used
1783 * @holder: owner for exclusion
1785 * Open the blockdevice described by the special file at @path, claim it
1788 struct block_device *open_bdev_exclusive(const char *path, fmode_t mode, void *holder)
1790 struct block_device *bdev, *whole;
1793 bdev = lookup_bdev(path);
1797 whole = bd_start_claiming(bdev, holder);
1798 if (IS_ERR(whole)) {
1803 error = blkdev_get(bdev, mode);
1805 goto out_abort_claiming;
1808 if ((mode & FMODE_WRITE) && bdev_read_only(bdev))
1809 goto out_blkdev_put;
1811 bd_finish_claiming(bdev, whole, holder);
1815 blkdev_put(bdev, mode);
1817 bd_abort_claiming(whole, holder);
1818 return ERR_PTR(error);
1821 EXPORT_SYMBOL(open_bdev_exclusive);
1824 * close_bdev_exclusive - close a blockdevice opened by open_bdev_exclusive()
1826 * @bdev: blockdevice to close
1827 * @mode: mode, must match that used to open.
1829 * This is the counterpart to open_bdev_exclusive().
1831 void close_bdev_exclusive(struct block_device *bdev, fmode_t mode)
1834 blkdev_put(bdev, mode);
1837 EXPORT_SYMBOL(close_bdev_exclusive);
1839 int __invalidate_device(struct block_device *bdev)
1841 struct super_block *sb = get_super(bdev);
1846 * no need to lock the super, get_super holds the
1847 * read mutex so the filesystem cannot go away
1848 * under us (->put_super runs with the write lock
1851 shrink_dcache_sb(sb);
1852 res = invalidate_inodes(sb);
1855 invalidate_bdev(bdev);
1858 EXPORT_SYMBOL(__invalidate_device);