MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / fs / block_dev.c
blob9801fed932f45aae993c6a0bc75bffcf2b2e15f9
1 /*
2 * linux/fs/block_dev.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
6 */
8 #include <linux/init.h>
9 #include <linux/mm.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/writeback.h>
21 #include <linux/mpage.h>
22 #include <linux/mount.h>
23 #include <linux/uio.h>
24 #include <linux/namei.h>
25 #include <asm/uaccess.h>
26 #include "internal.h"
28 struct bdev_inode {
29 struct block_device bdev;
30 struct inode vfs_inode;
33 static inline struct bdev_inode *BDEV_I(struct inode *inode)
35 return container_of(inode, struct bdev_inode, vfs_inode);
38 inline struct block_device *I_BDEV(struct inode *inode)
40 return &BDEV_I(inode)->bdev;
43 EXPORT_SYMBOL(I_BDEV);
45 static sector_t max_block(struct block_device *bdev)
47 sector_t retval = ~((sector_t)0);
48 loff_t sz = i_size_read(bdev->bd_inode);
50 if (sz) {
51 unsigned int size = block_size(bdev);
52 unsigned int sizebits = blksize_bits(size);
53 retval = (sz >> sizebits);
55 return retval;
58 /* Kill _all_ buffers, dirty or not.. */
59 static void kill_bdev(struct block_device *bdev)
61 invalidate_bdev(bdev, 1);
62 truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
65 int set_blocksize(struct block_device *bdev, int size)
67 /* Size must be a power of two, and between 512 and PAGE_SIZE */
68 if (size > PAGE_SIZE || size < 512 || (size & (size-1)))
69 return -EINVAL;
71 /* Size cannot be smaller than the size supported by the device */
72 if (size < bdev_hardsect_size(bdev))
73 return -EINVAL;
75 /* Don't change the size if it is same as current */
76 if (bdev->bd_block_size != size) {
77 sync_blockdev(bdev);
78 bdev->bd_block_size = size;
79 bdev->bd_inode->i_blkbits = blksize_bits(size);
80 kill_bdev(bdev);
82 return 0;
85 EXPORT_SYMBOL(set_blocksize);
87 int sb_set_blocksize(struct super_block *sb, int size)
89 if (set_blocksize(sb->s_bdev, size))
90 return 0;
91 /* If we get here, we know size is power of two
92 * and it's value is between 512 and PAGE_SIZE */
93 sb->s_blocksize = size;
94 sb->s_blocksize_bits = blksize_bits(size);
95 return sb->s_blocksize;
98 EXPORT_SYMBOL(sb_set_blocksize);
100 int sb_min_blocksize(struct super_block *sb, int size)
102 int minsize = bdev_hardsect_size(sb->s_bdev);
103 if (size < minsize)
104 size = minsize;
105 return sb_set_blocksize(sb, size);
108 EXPORT_SYMBOL(sb_min_blocksize);
110 static int
111 blkdev_get_block(struct inode *inode, sector_t iblock,
112 struct buffer_head *bh, int create)
114 if (iblock >= max_block(I_BDEV(inode))) {
115 if (create)
116 return -EIO;
119 * for reads, we're just trying to fill a partial page.
120 * return a hole, they will have to call get_block again
121 * before they can fill it, and they will get -EIO at that
122 * time
124 return 0;
126 bh->b_bdev = I_BDEV(inode);
127 bh->b_blocknr = iblock;
128 set_buffer_mapped(bh);
129 return 0;
132 #ifdef CONFIG_DIRECTIO
133 static int
134 blkdev_get_blocks(struct inode *inode, sector_t iblock,
135 struct buffer_head *bh, int create)
137 sector_t end_block = max_block(I_BDEV(inode));
138 unsigned long max_blocks = bh->b_size >> inode->i_blkbits;
140 if ((iblock + max_blocks) > end_block) {
141 max_blocks = end_block - iblock;
142 if ((long)max_blocks <= 0) {
143 if (create)
144 return -EIO; /* write fully beyond EOF */
146 * It is a read which is fully beyond EOF. We return
147 * a !buffer_mapped buffer
149 max_blocks = 0;
153 bh->b_bdev = I_BDEV(inode);
154 bh->b_blocknr = iblock;
155 bh->b_size = max_blocks << inode->i_blkbits;
156 if (max_blocks)
157 set_buffer_mapped(bh);
158 return 0;
161 static ssize_t
162 blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
163 loff_t offset, unsigned long nr_segs)
165 struct file *file = iocb->ki_filp;
166 struct inode *inode = file->f_mapping->host;
168 return blockdev_direct_IO_no_locking(rw, iocb, inode, I_BDEV(inode),
169 iov, offset, nr_segs, blkdev_get_blocks, NULL);
171 #endif
173 static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
175 return block_write_full_page(page, blkdev_get_block, wbc);
178 static int blkdev_readpage(struct file * file, struct page * page)
180 return block_read_full_page(page, blkdev_get_block);
183 static int blkdev_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
185 return block_prepare_write(page, from, to, blkdev_get_block);
188 static int blkdev_commit_write(struct file *file, struct page *page, unsigned from, unsigned to)
190 return block_commit_write(page, from, to);
194 * private llseek:
195 * for a block special file file->f_dentry->d_inode->i_size is zero
196 * so we compute the size by hand (just as in block_read/write above)
198 static loff_t block_llseek(struct file *file, loff_t offset, int origin)
200 struct inode *bd_inode = file->f_mapping->host;
201 loff_t size;
202 loff_t retval;
204 mutex_lock(&bd_inode->i_mutex);
205 size = i_size_read(bd_inode);
207 switch (origin) {
208 case 2:
209 offset += size;
210 break;
211 case 1:
212 offset += file->f_pos;
214 retval = -EINVAL;
215 if (offset >= 0 && offset <= size) {
216 if (offset != file->f_pos) {
217 file->f_pos = offset;
219 retval = offset;
221 mutex_unlock(&bd_inode->i_mutex);
222 return retval;
226 * Filp is never NULL; the only case when ->fsync() is called with
227 * NULL first argument is nfsd_sync_dir() and that's not a directory.
230 static int block_fsync(struct file *filp, struct dentry *dentry, int datasync)
232 return sync_blockdev(I_BDEV(filp->f_mapping->host));
236 * pseudo-fs
239 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
240 static kmem_cache_t * bdev_cachep __read_mostly;
242 static struct inode *bdev_alloc_inode(struct super_block *sb)
244 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, SLAB_KERNEL);
245 if (!ei)
246 return NULL;
247 return &ei->vfs_inode;
250 static void bdev_destroy_inode(struct inode *inode)
252 struct bdev_inode *bdi = BDEV_I(inode);
254 bdi->bdev.bd_inode_backing_dev_info = NULL;
255 kmem_cache_free(bdev_cachep, bdi);
258 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
260 struct bdev_inode *ei = (struct bdev_inode *) foo;
261 struct block_device *bdev = &ei->bdev;
263 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
264 SLAB_CTOR_CONSTRUCTOR)
266 memset(bdev, 0, sizeof(*bdev));
267 mutex_init(&bdev->bd_mutex);
268 mutex_init(&bdev->bd_mount_mutex);
269 INIT_LIST_HEAD(&bdev->bd_inodes);
270 INIT_LIST_HEAD(&bdev->bd_list);
271 #ifdef CONFIG_SYSFS
272 INIT_LIST_HEAD(&bdev->bd_holder_list);
273 #endif
274 inode_init_once(&ei->vfs_inode);
278 static inline void __bd_forget(struct inode *inode)
280 list_del_init(&inode->i_devices);
281 #if 0 // mask by Victor Yu. 02-12-2007
282 inode->i_bdev = NULL;
283 #else
284 inode->u.i_bdev = NULL;
285 #endif
286 inode->i_mapping = &inode->i_data;
289 static void bdev_clear_inode(struct inode *inode)
291 struct block_device *bdev = &BDEV_I(inode)->bdev;
292 struct list_head *p;
293 spin_lock(&bdev_lock);
294 while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
295 __bd_forget(list_entry(p, struct inode, i_devices));
297 list_del_init(&bdev->bd_list);
298 spin_unlock(&bdev_lock);
301 static struct super_operations bdev_sops = {
302 .statfs = simple_statfs,
303 .alloc_inode = bdev_alloc_inode,
304 .destroy_inode = bdev_destroy_inode,
305 .drop_inode = generic_delete_inode,
306 .clear_inode = bdev_clear_inode,
309 static int bd_get_sb(struct file_system_type *fs_type,
310 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
312 return get_sb_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576, mnt);
315 static struct file_system_type bd_type = {
316 .name = "bdev",
317 .get_sb = bd_get_sb,
318 .kill_sb = kill_anon_super,
321 static struct vfsmount *bd_mnt __read_mostly;
322 struct super_block *blockdev_superblock;
324 void __init bdev_cache_init(void)
326 int err;
327 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
328 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
329 SLAB_MEM_SPREAD|SLAB_PANIC),
330 init_once, NULL);
331 err = register_filesystem(&bd_type);
332 if (err)
333 panic("Cannot register bdev pseudo-fs");
334 bd_mnt = kern_mount(&bd_type);
335 err = PTR_ERR(bd_mnt);
336 if (IS_ERR(bd_mnt))
337 panic("Cannot create bdev pseudo-fs");
338 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
342 * Most likely _very_ bad one - but then it's hardly critical for small
343 * /dev and can be fixed when somebody will need really large one.
344 * Keep in mind that it will be fed through icache hash function too.
346 static inline unsigned long hash(dev_t dev)
348 return MAJOR(dev)+MINOR(dev);
351 static int bdev_test(struct inode *inode, void *data)
353 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
356 static int bdev_set(struct inode *inode, void *data)
358 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
359 return 0;
362 static LIST_HEAD(all_bdevs);
364 struct block_device *bdget(dev_t dev)
366 struct block_device *bdev;
367 struct inode *inode;
369 inode = iget5_locked(bd_mnt->mnt_sb, hash(dev),
370 bdev_test, bdev_set, &dev);
372 if (!inode)
373 return NULL;
375 bdev = &BDEV_I(inode)->bdev;
377 if (inode->i_state & I_NEW) {
378 bdev->bd_contains = NULL;
379 bdev->bd_inode = inode;
380 bdev->bd_block_size = (1 << inode->i_blkbits);
381 bdev->bd_part_count = 0;
382 bdev->bd_invalidated = 0;
383 inode->i_mode = S_IFBLK;
384 inode->i_rdev = dev;
385 #if 0 // mask by Victor Yu. 02-12-2007
386 inode->i_bdev = bdev;
387 #else
388 inode->u.i_bdev = bdev;
389 #endif
390 inode->i_data.a_ops = &def_blk_aops;
391 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
392 inode->i_data.backing_dev_info = &default_backing_dev_info;
393 spin_lock(&bdev_lock);
394 list_add(&bdev->bd_list, &all_bdevs);
395 spin_unlock(&bdev_lock);
396 unlock_new_inode(inode);
398 return bdev;
401 EXPORT_SYMBOL(bdget);
403 long nr_blockdev_pages(void)
405 struct list_head *p;
406 long ret = 0;
407 spin_lock(&bdev_lock);
408 list_for_each(p, &all_bdevs) {
409 struct block_device *bdev;
410 bdev = list_entry(p, struct block_device, bd_list);
411 ret += bdev->bd_inode->i_mapping->nrpages;
413 spin_unlock(&bdev_lock);
414 return ret;
417 void bdput(struct block_device *bdev)
419 iput(bdev->bd_inode);
422 EXPORT_SYMBOL(bdput);
424 static struct block_device *bd_acquire(struct inode *inode)
426 struct block_device *bdev;
428 spin_lock(&bdev_lock);
429 #if 0 // mask by Victor Yu. 02-12-2007
430 bdev = inode->i_bdev;
431 #else
432 bdev = inode->u.i_bdev;
433 #endif
434 if (bdev) {
435 atomic_inc(&bdev->bd_inode->i_count);
436 spin_unlock(&bdev_lock);
437 return bdev;
439 spin_unlock(&bdev_lock);
441 bdev = bdget(inode->i_rdev);
442 if (bdev) {
443 spin_lock(&bdev_lock);
444 #if 0 // mask by Victor Yu. 02-12-2007
445 if (!inode->i_bdev) {
446 #else
447 if (!inode->u.i_bdev) {
448 #endif
450 * We take an additional bd_inode->i_count for inode,
451 * and it's released in clear_inode() of inode.
452 * So, we can access it via ->i_mapping always
453 * without igrab().
455 atomic_inc(&bdev->bd_inode->i_count);
456 #if 0 // mask by Viccctor Yu. 02-12-2007
457 inode->i_bdev = bdev;
458 #else
459 inode->u.i_bdev = bdev;
460 #endif
461 inode->i_mapping = bdev->bd_inode->i_mapping;
462 list_add(&inode->i_devices, &bdev->bd_inodes);
464 spin_unlock(&bdev_lock);
466 return bdev;
469 /* Call when you free inode */
471 void bd_forget(struct inode *inode)
473 struct block_device *bdev = NULL;
475 spin_lock(&bdev_lock);
476 #if 0 // mask by Victor Yu. 02-12-2007
477 if (inode->i_bdev) {
478 #else
479 if (inode->u.i_bdev) {
480 #endif
481 if (inode->i_sb != blockdev_superblock)
482 #if 0 // mask by Victor Yu. 02-12-2007
483 bdev = inode->i_bdev;
484 #else
485 bdev = inode->u.i_bdev;
486 #endif
487 __bd_forget(inode);
489 spin_unlock(&bdev_lock);
491 if (bdev)
492 iput(bdev->bd_inode);
495 int bd_claim(struct block_device *bdev, void *holder)
497 int res;
498 spin_lock(&bdev_lock);
500 /* first decide result */
501 if (bdev->bd_holder == holder)
502 res = 0; /* already a holder */
503 else if (bdev->bd_holder != NULL)
504 res = -EBUSY; /* held by someone else */
505 else if (bdev->bd_contains == bdev)
506 res = 0; /* is a whole device which isn't held */
508 else if (bdev->bd_contains->bd_holder == bd_claim)
509 res = 0; /* is a partition of a device that is being partitioned */
510 else if (bdev->bd_contains->bd_holder != NULL)
511 res = -EBUSY; /* is a partition of a held device */
512 else
513 res = 0; /* is a partition of an un-held device */
515 /* now impose change */
516 if (res==0) {
517 /* note that for a whole device bd_holders
518 * will be incremented twice, and bd_holder will
519 * be set to bd_claim before being set to holder
521 bdev->bd_contains->bd_holders ++;
522 bdev->bd_contains->bd_holder = bd_claim;
523 bdev->bd_holders++;
524 bdev->bd_holder = holder;
526 spin_unlock(&bdev_lock);
527 return res;
530 EXPORT_SYMBOL(bd_claim);
532 void bd_release(struct block_device *bdev)
534 spin_lock(&bdev_lock);
535 if (!--bdev->bd_contains->bd_holders)
536 bdev->bd_contains->bd_holder = NULL;
537 if (!--bdev->bd_holders)
538 bdev->bd_holder = NULL;
539 spin_unlock(&bdev_lock);
542 EXPORT_SYMBOL(bd_release);
544 #ifdef CONFIG_SYSFS
546 * Functions for bd_claim_by_kobject / bd_release_from_kobject
548 * If a kobject is passed to bd_claim_by_kobject()
549 * and the kobject has a parent directory,
550 * following symlinks are created:
551 * o from the kobject to the claimed bdev
552 * o from "holders" directory of the bdev to the parent of the kobject
553 * bd_release_from_kobject() removes these symlinks.
555 * Example:
556 * If /dev/dm-0 maps to /dev/sda, kobject corresponding to
557 * /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then:
558 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
559 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
562 static struct kobject *bdev_get_kobj(struct block_device *bdev)
564 if (bdev->bd_contains != bdev)
565 return kobject_get(&bdev->bd_part->kobj);
566 else
567 return kobject_get(&bdev->bd_disk->kobj);
570 static struct kobject *bdev_get_holder(struct block_device *bdev)
572 if (bdev->bd_contains != bdev)
573 return kobject_get(bdev->bd_part->holder_dir);
574 else
575 return kobject_get(bdev->bd_disk->holder_dir);
578 static int add_symlink(struct kobject *from, struct kobject *to)
580 if (!from || !to)
581 return 0;
582 return sysfs_create_link(from, to, kobject_name(to));
585 static void del_symlink(struct kobject *from, struct kobject *to)
587 if (!from || !to)
588 return;
589 sysfs_remove_link(from, kobject_name(to));
593 * 'struct bd_holder' contains pointers to kobjects symlinked by
594 * bd_claim_by_kobject.
595 * It's connected to bd_holder_list which is protected by bdev->bd_sem.
597 struct bd_holder {
598 struct list_head list; /* chain of holders of the bdev */
599 int count; /* references from the holder */
600 struct kobject *sdir; /* holder object, e.g. "/block/dm-0/slaves" */
601 struct kobject *hdev; /* e.g. "/block/dm-0" */
602 struct kobject *hdir; /* e.g. "/block/sda/holders" */
603 struct kobject *sdev; /* e.g. "/block/sda" */
607 * Get references of related kobjects at once.
608 * Returns 1 on success. 0 on failure.
610 * Should call bd_holder_release_dirs() after successful use.
612 static int bd_holder_grab_dirs(struct block_device *bdev,
613 struct bd_holder *bo)
615 if (!bdev || !bo)
616 return 0;
618 bo->sdir = kobject_get(bo->sdir);
619 if (!bo->sdir)
620 return 0;
622 bo->hdev = kobject_get(bo->sdir->parent);
623 if (!bo->hdev)
624 goto fail_put_sdir;
626 bo->sdev = bdev_get_kobj(bdev);
627 if (!bo->sdev)
628 goto fail_put_hdev;
630 bo->hdir = bdev_get_holder(bdev);
631 if (!bo->hdir)
632 goto fail_put_sdev;
634 return 1;
636 fail_put_sdev:
637 kobject_put(bo->sdev);
638 fail_put_hdev:
639 kobject_put(bo->hdev);
640 fail_put_sdir:
641 kobject_put(bo->sdir);
643 return 0;
646 /* Put references of related kobjects at once. */
647 static void bd_holder_release_dirs(struct bd_holder *bo)
649 kobject_put(bo->hdir);
650 kobject_put(bo->sdev);
651 kobject_put(bo->hdev);
652 kobject_put(bo->sdir);
655 static struct bd_holder *alloc_bd_holder(struct kobject *kobj)
657 struct bd_holder *bo;
659 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
660 if (!bo)
661 return NULL;
663 bo->count = 1;
664 bo->sdir = kobj;
666 return bo;
669 static void free_bd_holder(struct bd_holder *bo)
671 kfree(bo);
675 * find_bd_holder - find matching struct bd_holder from the block device
677 * @bdev: struct block device to be searched
678 * @bo: target struct bd_holder
680 * Returns matching entry with @bo in @bdev->bd_holder_list.
681 * If found, increment the reference count and return the pointer.
682 * If not found, returns NULL.
684 static struct bd_holder *find_bd_holder(struct block_device *bdev,
685 struct bd_holder *bo)
687 struct bd_holder *tmp;
689 list_for_each_entry(tmp, &bdev->bd_holder_list, list)
690 if (tmp->sdir == bo->sdir) {
691 tmp->count++;
692 return tmp;
695 return NULL;
699 * add_bd_holder - create sysfs symlinks for bd_claim() relationship
701 * @bdev: block device to be bd_claimed
702 * @bo: preallocated and initialized by alloc_bd_holder()
704 * Add @bo to @bdev->bd_holder_list, create symlinks.
706 * Returns 0 if symlinks are created.
707 * Returns -ve if something fails.
709 static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
711 int ret;
713 if (!bo)
714 return -EINVAL;
716 if (!bd_holder_grab_dirs(bdev, bo))
717 return -EBUSY;
719 ret = add_symlink(bo->sdir, bo->sdev);
720 if (ret == 0) {
721 ret = add_symlink(bo->hdir, bo->hdev);
722 if (ret)
723 del_symlink(bo->sdir, bo->sdev);
725 if (ret == 0)
726 list_add_tail(&bo->list, &bdev->bd_holder_list);
727 return ret;
731 * del_bd_holder - delete sysfs symlinks for bd_claim() relationship
733 * @bdev: block device to be bd_claimed
734 * @kobj: holder's kobject
736 * If there is matching entry with @kobj in @bdev->bd_holder_list
737 * and no other bd_claim() from the same kobject,
738 * remove the struct bd_holder from the list, delete symlinks for it.
740 * Returns a pointer to the struct bd_holder when it's removed from the list
741 * and ready to be freed.
742 * Returns NULL if matching claim isn't found or there is other bd_claim()
743 * by the same kobject.
745 static struct bd_holder *del_bd_holder(struct block_device *bdev,
746 struct kobject *kobj)
748 struct bd_holder *bo;
750 list_for_each_entry(bo, &bdev->bd_holder_list, list) {
751 if (bo->sdir == kobj) {
752 bo->count--;
753 BUG_ON(bo->count < 0);
754 if (!bo->count) {
755 list_del(&bo->list);
756 del_symlink(bo->sdir, bo->sdev);
757 del_symlink(bo->hdir, bo->hdev);
758 bd_holder_release_dirs(bo);
759 return bo;
761 break;
765 return NULL;
769 * bd_claim_by_kobject - bd_claim() with additional kobject signature
771 * @bdev: block device to be claimed
772 * @holder: holder's signature
773 * @kobj: holder's kobject
775 * Do bd_claim() and if it succeeds, create sysfs symlinks between
776 * the bdev and the holder's kobject.
777 * Use bd_release_from_kobject() when relesing the claimed bdev.
779 * Returns 0 on success. (same as bd_claim())
780 * Returns errno on failure.
782 static int bd_claim_by_kobject(struct block_device *bdev, void *holder,
783 struct kobject *kobj)
785 int res;
786 struct bd_holder *bo, *found;
788 if (!kobj)
789 return -EINVAL;
791 bo = alloc_bd_holder(kobj);
792 if (!bo)
793 return -ENOMEM;
795 mutex_lock_nested(&bdev->bd_mutex, BD_MUTEX_PARTITION);
796 res = bd_claim(bdev, holder);
797 if (res == 0) {
798 found = find_bd_holder(bdev, bo);
799 if (found == NULL) {
800 res = add_bd_holder(bdev, bo);
801 if (res)
802 bd_release(bdev);
806 if (res || found)
807 free_bd_holder(bo);
808 mutex_unlock(&bdev->bd_mutex);
810 return res;
814 * bd_release_from_kobject - bd_release() with additional kobject signature
816 * @bdev: block device to be released
817 * @kobj: holder's kobject
819 * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject().
821 static void bd_release_from_kobject(struct block_device *bdev,
822 struct kobject *kobj)
824 struct bd_holder *bo;
826 if (!kobj)
827 return;
829 mutex_lock_nested(&bdev->bd_mutex, BD_MUTEX_PARTITION);
830 bd_release(bdev);
831 if ((bo = del_bd_holder(bdev, kobj)))
832 free_bd_holder(bo);
833 mutex_unlock(&bdev->bd_mutex);
837 * bd_claim_by_disk - wrapper function for bd_claim_by_kobject()
839 * @bdev: block device to be claimed
840 * @holder: holder's signature
841 * @disk: holder's gendisk
843 * Call bd_claim_by_kobject() with getting @disk->slave_dir.
845 int bd_claim_by_disk(struct block_device *bdev, void *holder,
846 struct gendisk *disk)
848 return bd_claim_by_kobject(bdev, holder, kobject_get(disk->slave_dir));
850 EXPORT_SYMBOL_GPL(bd_claim_by_disk);
853 * bd_release_from_disk - wrapper function for bd_release_from_kobject()
855 * @bdev: block device to be claimed
856 * @disk: holder's gendisk
858 * Call bd_release_from_kobject() and put @disk->slave_dir.
860 void bd_release_from_disk(struct block_device *bdev, struct gendisk *disk)
862 bd_release_from_kobject(bdev, disk->slave_dir);
863 kobject_put(disk->slave_dir);
865 EXPORT_SYMBOL_GPL(bd_release_from_disk);
866 #endif
869 * Tries to open block device by device number. Use it ONLY if you
870 * really do not have anything better - i.e. when you are behind a
871 * truly sucky interface and all you are given is a device number. _Never_
872 * to be used for internal purposes. If you ever need it - reconsider
873 * your API.
875 struct block_device *open_by_devnum(dev_t dev, unsigned mode)
877 struct block_device *bdev = bdget(dev);
878 int err = -ENOMEM;
879 int flags = mode & FMODE_WRITE ? O_RDWR : O_RDONLY;
880 if (bdev)
881 err = blkdev_get(bdev, mode, flags);
882 return err ? ERR_PTR(err) : bdev;
885 EXPORT_SYMBOL(open_by_devnum);
887 static int
888 blkdev_get_partition(struct block_device *bdev, mode_t mode, unsigned flags);
890 struct block_device *open_partition_by_devnum(dev_t dev, unsigned mode)
892 struct block_device *bdev = bdget(dev);
893 int err = -ENOMEM;
894 int flags = mode & FMODE_WRITE ? O_RDWR : O_RDONLY;
895 if (bdev)
896 err = blkdev_get_partition(bdev, mode, flags);
897 return err ? ERR_PTR(err) : bdev;
900 EXPORT_SYMBOL(open_partition_by_devnum);
904 * This routine checks whether a removable media has been changed,
905 * and invalidates all buffer-cache-entries in that case. This
906 * is a relatively slow routine, so we have to try to minimize using
907 * it. Thus it is called only upon a 'mount' or 'open'. This
908 * is the best way of combining speed and utility, I think.
909 * People changing diskettes in the middle of an operation deserve
910 * to lose :-)
912 int check_disk_change(struct block_device *bdev)
914 struct gendisk *disk = bdev->bd_disk;
915 struct block_device_operations * bdops = disk->fops;
917 if (!bdops->media_changed)
918 return 0;
919 if (!bdops->media_changed(bdev->bd_disk))
920 return 0;
922 if (__invalidate_device(bdev))
923 printk("VFS: busy inodes on changed media.\n");
925 if (bdops->revalidate_disk)
926 bdops->revalidate_disk(bdev->bd_disk);
927 if (bdev->bd_disk->minors > 1)
928 bdev->bd_invalidated = 1;
929 return 1;
932 EXPORT_SYMBOL(check_disk_change);
934 void bd_set_size(struct block_device *bdev, loff_t size)
936 unsigned bsize = bdev_hardsect_size(bdev);
938 bdev->bd_inode->i_size = size;
939 while (bsize < PAGE_CACHE_SIZE) {
940 if (size & bsize)
941 break;
942 bsize <<= 1;
944 bdev->bd_block_size = bsize;
945 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
947 EXPORT_SYMBOL(bd_set_size);
949 static int __blkdev_put(struct block_device *bdev, unsigned int subclass)
951 int ret = 0;
952 struct inode *bd_inode = bdev->bd_inode;
953 struct gendisk *disk = bdev->bd_disk;
955 mutex_lock_nested(&bdev->bd_mutex, subclass);
956 lock_kernel();
957 if (!--bdev->bd_openers) {
958 sync_blockdev(bdev);
959 kill_bdev(bdev);
961 if (bdev->bd_contains == bdev) {
962 if (disk->fops->release)
963 ret = disk->fops->release(bd_inode, NULL);
964 } else {
965 mutex_lock_nested(&bdev->bd_contains->bd_mutex,
966 subclass + 1);
967 bdev->bd_contains->bd_part_count--;
968 mutex_unlock(&bdev->bd_contains->bd_mutex);
970 if (!bdev->bd_openers) {
971 struct module *owner = disk->fops->owner;
973 put_disk(disk);
974 module_put(owner);
976 if (bdev->bd_contains != bdev) {
977 kobject_put(&bdev->bd_part->kobj);
978 bdev->bd_part = NULL;
980 bdev->bd_disk = NULL;
981 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
982 if (bdev != bdev->bd_contains)
983 __blkdev_put(bdev->bd_contains, subclass + 1);
984 bdev->bd_contains = NULL;
986 unlock_kernel();
987 mutex_unlock(&bdev->bd_mutex);
988 bdput(bdev);
989 return ret;
992 int blkdev_put(struct block_device *bdev)
994 return __blkdev_put(bdev, BD_MUTEX_NORMAL);
996 EXPORT_SYMBOL(blkdev_put);
998 int blkdev_put_partition(struct block_device *bdev)
1000 return __blkdev_put(bdev, BD_MUTEX_PARTITION);
1002 EXPORT_SYMBOL(blkdev_put_partition);
1004 static int
1005 blkdev_get_whole(struct block_device *bdev, mode_t mode, unsigned flags);
1007 static int
1008 do_open(struct block_device *bdev, struct file *file, unsigned int subclass)
1010 struct module *owner = NULL;
1011 struct gendisk *disk;
1012 int ret = -ENXIO;
1013 int part;
1015 file->f_mapping = bdev->bd_inode->i_mapping;
1016 lock_kernel();
1017 disk = get_gendisk(bdev->bd_dev, &part);
1018 if (!disk) {
1019 unlock_kernel();
1020 bdput(bdev);
1021 return ret;
1023 owner = disk->fops->owner;
1025 mutex_lock_nested(&bdev->bd_mutex, subclass);
1027 if (!bdev->bd_openers) {
1028 bdev->bd_disk = disk;
1029 bdev->bd_contains = bdev;
1030 if (!part) {
1031 struct backing_dev_info *bdi;
1032 if (disk->fops->open) {
1033 ret = disk->fops->open(bdev->bd_inode, file);
1034 if (ret)
1035 goto out_first;
1037 if (!bdev->bd_openers) {
1038 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
1039 bdi = blk_get_backing_dev_info(bdev);
1040 if (bdi == NULL)
1041 bdi = &default_backing_dev_info;
1042 bdev->bd_inode->i_data.backing_dev_info = bdi;
1044 if (bdev->bd_invalidated)
1045 rescan_partitions(disk, bdev);
1046 } else {
1047 struct hd_struct *p;
1048 struct block_device *whole;
1049 whole = bdget_disk(disk, 0);
1050 ret = -ENOMEM;
1051 if (!whole)
1052 goto out_first;
1053 ret = blkdev_get_whole(whole, file->f_mode, file->f_flags);
1054 if (ret)
1055 goto out_first;
1056 bdev->bd_contains = whole;
1057 mutex_lock_nested(&whole->bd_mutex, BD_MUTEX_WHOLE);
1058 whole->bd_part_count++;
1059 p = disk->part[part - 1];
1060 bdev->bd_inode->i_data.backing_dev_info =
1061 whole->bd_inode->i_data.backing_dev_info;
1062 if (!(disk->flags & GENHD_FL_UP) || !p || !p->nr_sects) {
1063 whole->bd_part_count--;
1064 mutex_unlock(&whole->bd_mutex);
1065 ret = -ENXIO;
1066 goto out_first;
1068 kobject_get(&p->kobj);
1069 bdev->bd_part = p;
1070 bd_set_size(bdev, (loff_t) p->nr_sects << 9);
1071 mutex_unlock(&whole->bd_mutex);
1073 } else {
1074 put_disk(disk);
1075 module_put(owner);
1076 if (bdev->bd_contains == bdev) {
1077 if (bdev->bd_disk->fops->open) {
1078 ret = bdev->bd_disk->fops->open(bdev->bd_inode, file);
1079 if (ret)
1080 goto out;
1082 if (bdev->bd_invalidated)
1083 rescan_partitions(bdev->bd_disk, bdev);
1084 } else {
1085 mutex_lock_nested(&bdev->bd_contains->bd_mutex,
1086 BD_MUTEX_WHOLE);
1087 bdev->bd_contains->bd_part_count++;
1088 mutex_unlock(&bdev->bd_contains->bd_mutex);
1091 bdev->bd_openers++;
1092 mutex_unlock(&bdev->bd_mutex);
1093 unlock_kernel();
1094 return 0;
1096 out_first:
1097 bdev->bd_disk = NULL;
1098 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
1099 if (bdev != bdev->bd_contains)
1100 __blkdev_put(bdev->bd_contains, BD_MUTEX_WHOLE);
1101 bdev->bd_contains = NULL;
1102 put_disk(disk);
1103 module_put(owner);
1104 out:
1105 mutex_unlock(&bdev->bd_mutex);
1106 unlock_kernel();
1107 if (ret)
1108 bdput(bdev);
1109 return ret;
1112 int blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags)
1115 * This crockload is due to bad choice of ->open() type.
1116 * It will go away.
1117 * For now, block device ->open() routine must _not_
1118 * examine anything in 'inode' argument except ->i_rdev.
1120 struct file fake_file = {};
1121 struct dentry fake_dentry = {};
1122 fake_file.f_mode = mode;
1123 fake_file.f_flags = flags;
1124 fake_file.f_dentry = &fake_dentry;
1125 fake_dentry.d_inode = bdev->bd_inode;
1127 return do_open(bdev, &fake_file, BD_MUTEX_NORMAL);
1130 EXPORT_SYMBOL(blkdev_get);
1132 static int
1133 blkdev_get_whole(struct block_device *bdev, mode_t mode, unsigned flags)
1136 * This crockload is due to bad choice of ->open() type.
1137 * It will go away.
1138 * For now, block device ->open() routine must _not_
1139 * examine anything in 'inode' argument except ->i_rdev.
1141 struct file fake_file = {};
1142 struct dentry fake_dentry = {};
1143 fake_file.f_mode = mode;
1144 fake_file.f_flags = flags;
1145 fake_file.f_dentry = &fake_dentry;
1146 fake_dentry.d_inode = bdev->bd_inode;
1148 return do_open(bdev, &fake_file, BD_MUTEX_WHOLE);
1151 static int
1152 blkdev_get_partition(struct block_device *bdev, mode_t mode, unsigned flags)
1155 * This crockload is due to bad choice of ->open() type.
1156 * It will go away.
1157 * For now, block device ->open() routine must _not_
1158 * examine anything in 'inode' argument except ->i_rdev.
1160 struct file fake_file = {};
1161 struct dentry fake_dentry = {};
1162 fake_file.f_mode = mode;
1163 fake_file.f_flags = flags;
1164 fake_file.f_dentry = &fake_dentry;
1165 fake_dentry.d_inode = bdev->bd_inode;
1167 return do_open(bdev, &fake_file, BD_MUTEX_PARTITION);
1170 static int blkdev_open(struct inode * inode, struct file * filp)
1172 struct block_device *bdev;
1173 int res;
1176 * Preserve backwards compatibility and allow large file access
1177 * even if userspace doesn't ask for it explicitly. Some mkfs
1178 * binary needs it. We might want to drop this workaround
1179 * during an unstable branch.
1181 filp->f_flags |= O_LARGEFILE;
1183 bdev = bd_acquire(inode);
1184 if (bdev == NULL)
1185 return -ENOMEM;
1187 res = do_open(bdev, filp, BD_MUTEX_NORMAL);
1188 if (res)
1189 return res;
1191 if (!(filp->f_flags & O_EXCL) )
1192 return 0;
1194 if (!(res = bd_claim(bdev, filp)))
1195 return 0;
1197 blkdev_put(bdev);
1198 return res;
1201 static int blkdev_close(struct inode * inode, struct file * filp)
1203 struct block_device *bdev = I_BDEV(filp->f_mapping->host);
1204 if (bdev->bd_holder == filp)
1205 bd_release(bdev);
1206 return blkdev_put(bdev);
1209 static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1211 return blkdev_ioctl(file->f_mapping->host, file, cmd, arg);
1214 const struct address_space_operations def_blk_aops = {
1215 .readpage = blkdev_readpage,
1216 .writepage = blkdev_writepage,
1217 .sync_page = block_sync_page,
1218 .prepare_write = blkdev_prepare_write,
1219 .commit_write = blkdev_commit_write,
1220 .writepages = generic_writepages,
1221 #ifdef CONFIG_DIRECTIO
1222 .direct_IO = blkdev_direct_IO,
1223 #endif
1226 const struct file_operations def_blk_fops = {
1227 .open = blkdev_open,
1228 .release = blkdev_close,
1229 .llseek = block_llseek,
1230 .read = do_sync_read,
1231 .write = do_sync_write,
1232 .aio_read = generic_file_aio_read,
1233 .aio_write = generic_file_aio_write_nolock,
1234 .mmap = generic_file_mmap,
1235 .fsync = block_fsync,
1236 .unlocked_ioctl = block_ioctl,
1237 #ifdef CONFIG_COMPAT
1238 .compat_ioctl = compat_blkdev_ioctl,
1239 #endif
1240 .sendfile = generic_file_sendfile,
1241 .splice_read = generic_file_splice_read,
1242 .splice_write = generic_file_splice_write,
1245 int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
1247 int res;
1248 mm_segment_t old_fs = get_fs();
1249 set_fs(KERNEL_DS);
1250 res = blkdev_ioctl(bdev->bd_inode, NULL, cmd, arg);
1251 set_fs(old_fs);
1252 return res;
1255 EXPORT_SYMBOL(ioctl_by_bdev);
1258 * lookup_bdev - lookup a struct block_device by name
1260 * @path: special file representing the block device
1262 * Get a reference to the blockdevice at @path in the current
1263 * namespace if possible and return it. Return ERR_PTR(error)
1264 * otherwise.
1266 struct block_device *lookup_bdev(const char *path)
1268 struct block_device *bdev;
1269 struct inode *inode;
1270 struct nameidata nd;
1271 int error;
1273 if (!path || !*path)
1274 return ERR_PTR(-EINVAL);
1276 error = path_lookup(path, LOOKUP_FOLLOW, &nd);
1277 if (error)
1278 return ERR_PTR(error);
1280 inode = nd.dentry->d_inode;
1281 error = -ENOTBLK;
1282 if (!S_ISBLK(inode->i_mode))
1283 goto fail;
1284 error = -EACCES;
1285 if (nd.mnt->mnt_flags & MNT_NODEV)
1286 goto fail;
1287 error = -ENOMEM;
1288 bdev = bd_acquire(inode);
1289 if (!bdev)
1290 goto fail;
1291 out:
1292 path_release(&nd);
1293 return bdev;
1294 fail:
1295 bdev = ERR_PTR(error);
1296 goto out;
1300 * open_bdev_excl - open a block device by name and set it up for use
1302 * @path: special file representing the block device
1303 * @flags: %MS_RDONLY for opening read-only
1304 * @holder: owner for exclusion
1306 * Open the blockdevice described by the special file at @path, claim it
1307 * for the @holder.
1309 struct block_device *open_bdev_excl(const char *path, int flags, void *holder)
1311 struct block_device *bdev;
1312 mode_t mode = FMODE_READ;
1313 int error = 0;
1315 bdev = lookup_bdev(path);
1316 if (IS_ERR(bdev))
1317 return bdev;
1319 if (!(flags & MS_RDONLY))
1320 mode |= FMODE_WRITE;
1321 error = blkdev_get(bdev, mode, 0);
1322 if (error)
1323 return ERR_PTR(error);
1324 error = -EACCES;
1325 if (!(flags & MS_RDONLY) && bdev_read_only(bdev))
1326 goto blkdev_put;
1327 error = bd_claim(bdev, holder);
1328 if (error)
1329 goto blkdev_put;
1331 return bdev;
1333 blkdev_put:
1334 blkdev_put(bdev);
1335 return ERR_PTR(error);
1338 EXPORT_SYMBOL(open_bdev_excl);
1341 * close_bdev_excl - release a blockdevice openen by open_bdev_excl()
1343 * @bdev: blockdevice to close
1345 * This is the counterpart to open_bdev_excl().
1347 void close_bdev_excl(struct block_device *bdev)
1349 bd_release(bdev);
1350 blkdev_put(bdev);
1353 EXPORT_SYMBOL(close_bdev_excl);
1355 int __invalidate_device(struct block_device *bdev)
1357 struct super_block *sb = get_super(bdev);
1358 int res = 0;
1360 if (sb) {
1362 * no need to lock the super, get_super holds the
1363 * read mutex so the filesystem cannot go away
1364 * under us (->put_super runs with the write lock
1365 * hold).
1367 shrink_dcache_sb(sb);
1368 res = invalidate_inodes(sb);
1369 drop_super(sb);
1371 invalidate_bdev(bdev, 0);
1372 return res;
1374 EXPORT_SYMBOL(__invalidate_device);