x86: fix prefetch workaround
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / block_dev.c
blob7d822fae7765b2bfdf0978e8ecf1296875073e85
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 <linux/log2.h>
26 #include <asm/uaccess.h>
27 #include "internal.h"
29 struct bdev_inode {
30 struct block_device bdev;
31 struct inode vfs_inode;
34 static const struct address_space_operations def_blk_aops;
36 static inline struct bdev_inode *BDEV_I(struct inode *inode)
38 return container_of(inode, struct bdev_inode, vfs_inode);
41 inline struct block_device *I_BDEV(struct inode *inode)
43 return &BDEV_I(inode)->bdev;
46 EXPORT_SYMBOL(I_BDEV);
48 static sector_t max_block(struct block_device *bdev)
50 sector_t retval = ~((sector_t)0);
51 loff_t sz = i_size_read(bdev->bd_inode);
53 if (sz) {
54 unsigned int size = block_size(bdev);
55 unsigned int sizebits = blksize_bits(size);
56 retval = (sz >> sizebits);
58 return retval;
61 /* Kill _all_ buffers and pagecache , dirty or not.. */
62 static void kill_bdev(struct block_device *bdev)
64 if (bdev->bd_inode->i_mapping->nrpages == 0)
65 return;
66 invalidate_bh_lrus();
67 truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
70 int set_blocksize(struct block_device *bdev, int size)
72 /* Size must be a power of two, and between 512 and PAGE_SIZE */
73 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
74 return -EINVAL;
76 /* Size cannot be smaller than the size supported by the device */
77 if (size < bdev_hardsect_size(bdev))
78 return -EINVAL;
80 /* Don't change the size if it is same as current */
81 if (bdev->bd_block_size != size) {
82 sync_blockdev(bdev);
83 bdev->bd_block_size = size;
84 bdev->bd_inode->i_blkbits = blksize_bits(size);
85 kill_bdev(bdev);
87 return 0;
90 EXPORT_SYMBOL(set_blocksize);
92 int sb_set_blocksize(struct super_block *sb, int size)
94 if (set_blocksize(sb->s_bdev, size))
95 return 0;
96 /* If we get here, we know size is power of two
97 * and it's value is between 512 and PAGE_SIZE */
98 sb->s_blocksize = size;
99 sb->s_blocksize_bits = blksize_bits(size);
100 return sb->s_blocksize;
103 EXPORT_SYMBOL(sb_set_blocksize);
105 int sb_min_blocksize(struct super_block *sb, int size)
107 int minsize = bdev_hardsect_size(sb->s_bdev);
108 if (size < minsize)
109 size = minsize;
110 return sb_set_blocksize(sb, size);
113 EXPORT_SYMBOL(sb_min_blocksize);
115 static int
116 blkdev_get_block(struct inode *inode, sector_t iblock,
117 struct buffer_head *bh, int create)
119 if (iblock >= max_block(I_BDEV(inode))) {
120 if (create)
121 return -EIO;
124 * for reads, we're just trying to fill a partial page.
125 * return a hole, they will have to call get_block again
126 * before they can fill it, and they will get -EIO at that
127 * time
129 return 0;
131 bh->b_bdev = I_BDEV(inode);
132 bh->b_blocknr = iblock;
133 set_buffer_mapped(bh);
134 return 0;
137 static int
138 blkdev_get_blocks(struct inode *inode, sector_t iblock,
139 struct buffer_head *bh, int create)
141 sector_t end_block = max_block(I_BDEV(inode));
142 unsigned long max_blocks = bh->b_size >> inode->i_blkbits;
144 if ((iblock + max_blocks) > end_block) {
145 max_blocks = end_block - iblock;
146 if ((long)max_blocks <= 0) {
147 if (create)
148 return -EIO; /* write fully beyond EOF */
150 * It is a read which is fully beyond EOF. We return
151 * a !buffer_mapped buffer
153 max_blocks = 0;
157 bh->b_bdev = I_BDEV(inode);
158 bh->b_blocknr = iblock;
159 bh->b_size = max_blocks << inode->i_blkbits;
160 if (max_blocks)
161 set_buffer_mapped(bh);
162 return 0;
165 static ssize_t
166 blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
167 loff_t offset, unsigned long nr_segs)
169 struct file *file = iocb->ki_filp;
170 struct inode *inode = file->f_mapping->host;
172 return blockdev_direct_IO_no_locking(rw, iocb, inode, I_BDEV(inode),
173 iov, offset, nr_segs, blkdev_get_blocks, NULL);
176 static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
178 return block_write_full_page(page, blkdev_get_block, wbc);
181 static int blkdev_readpage(struct file * file, struct page * page)
183 return block_read_full_page(page, blkdev_get_block);
186 static int blkdev_write_begin(struct file *file, struct address_space *mapping,
187 loff_t pos, unsigned len, unsigned flags,
188 struct page **pagep, void **fsdata)
190 *pagep = NULL;
191 return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
192 blkdev_get_block);
195 static int blkdev_write_end(struct file *file, struct address_space *mapping,
196 loff_t pos, unsigned len, unsigned copied,
197 struct page *page, void *fsdata)
199 int ret;
200 ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
202 unlock_page(page);
203 page_cache_release(page);
205 return ret;
209 * private llseek:
210 * for a block special file file->f_path.dentry->d_inode->i_size is zero
211 * so we compute the size by hand (just as in block_read/write above)
213 static loff_t block_llseek(struct file *file, loff_t offset, int origin)
215 struct inode *bd_inode = file->f_mapping->host;
216 loff_t size;
217 loff_t retval;
219 mutex_lock(&bd_inode->i_mutex);
220 size = i_size_read(bd_inode);
222 switch (origin) {
223 case 2:
224 offset += size;
225 break;
226 case 1:
227 offset += file->f_pos;
229 retval = -EINVAL;
230 if (offset >= 0 && offset <= size) {
231 if (offset != file->f_pos) {
232 file->f_pos = offset;
234 retval = offset;
236 mutex_unlock(&bd_inode->i_mutex);
237 return retval;
241 * Filp is never NULL; the only case when ->fsync() is called with
242 * NULL first argument is nfsd_sync_dir() and that's not a directory.
245 static int block_fsync(struct file *filp, struct dentry *dentry, int datasync)
247 return sync_blockdev(I_BDEV(filp->f_mapping->host));
251 * pseudo-fs
254 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
255 static struct kmem_cache * bdev_cachep __read_mostly;
257 static struct inode *bdev_alloc_inode(struct super_block *sb)
259 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
260 if (!ei)
261 return NULL;
262 return &ei->vfs_inode;
265 static void bdev_destroy_inode(struct inode *inode)
267 struct bdev_inode *bdi = BDEV_I(inode);
269 bdi->bdev.bd_inode_backing_dev_info = NULL;
270 kmem_cache_free(bdev_cachep, bdi);
273 static void init_once(struct kmem_cache * cachep, void *foo)
275 struct bdev_inode *ei = (struct bdev_inode *) foo;
276 struct block_device *bdev = &ei->bdev;
278 memset(bdev, 0, sizeof(*bdev));
279 mutex_init(&bdev->bd_mutex);
280 sema_init(&bdev->bd_mount_sem, 1);
281 INIT_LIST_HEAD(&bdev->bd_inodes);
282 INIT_LIST_HEAD(&bdev->bd_list);
283 #ifdef CONFIG_SYSFS
284 INIT_LIST_HEAD(&bdev->bd_holder_list);
285 #endif
286 inode_init_once(&ei->vfs_inode);
289 static inline void __bd_forget(struct inode *inode)
291 list_del_init(&inode->i_devices);
292 inode->i_bdev = NULL;
293 inode->i_mapping = &inode->i_data;
296 static void bdev_clear_inode(struct inode *inode)
298 struct block_device *bdev = &BDEV_I(inode)->bdev;
299 struct list_head *p;
300 spin_lock(&bdev_lock);
301 while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
302 __bd_forget(list_entry(p, struct inode, i_devices));
304 list_del_init(&bdev->bd_list);
305 spin_unlock(&bdev_lock);
308 static const struct super_operations bdev_sops = {
309 .statfs = simple_statfs,
310 .alloc_inode = bdev_alloc_inode,
311 .destroy_inode = bdev_destroy_inode,
312 .drop_inode = generic_delete_inode,
313 .clear_inode = bdev_clear_inode,
316 static int bd_get_sb(struct file_system_type *fs_type,
317 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
319 return get_sb_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576, mnt);
322 static struct file_system_type bd_type = {
323 .name = "bdev",
324 .get_sb = bd_get_sb,
325 .kill_sb = kill_anon_super,
328 static struct vfsmount *bd_mnt __read_mostly;
329 struct super_block *blockdev_superblock;
331 void __init bdev_cache_init(void)
333 int err;
334 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
335 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
336 SLAB_MEM_SPREAD|SLAB_PANIC),
337 init_once);
338 err = register_filesystem(&bd_type);
339 if (err)
340 panic("Cannot register bdev pseudo-fs");
341 bd_mnt = kern_mount(&bd_type);
342 if (IS_ERR(bd_mnt))
343 panic("Cannot create bdev pseudo-fs");
344 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
348 * Most likely _very_ bad one - but then it's hardly critical for small
349 * /dev and can be fixed when somebody will need really large one.
350 * Keep in mind that it will be fed through icache hash function too.
352 static inline unsigned long hash(dev_t dev)
354 return MAJOR(dev)+MINOR(dev);
357 static int bdev_test(struct inode *inode, void *data)
359 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
362 static int bdev_set(struct inode *inode, void *data)
364 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
365 return 0;
368 static LIST_HEAD(all_bdevs);
370 struct block_device *bdget(dev_t dev)
372 struct block_device *bdev;
373 struct inode *inode;
375 inode = iget5_locked(bd_mnt->mnt_sb, hash(dev),
376 bdev_test, bdev_set, &dev);
378 if (!inode)
379 return NULL;
381 bdev = &BDEV_I(inode)->bdev;
383 if (inode->i_state & I_NEW) {
384 bdev->bd_contains = NULL;
385 bdev->bd_inode = inode;
386 bdev->bd_block_size = (1 << inode->i_blkbits);
387 bdev->bd_part_count = 0;
388 bdev->bd_invalidated = 0;
389 inode->i_mode = S_IFBLK;
390 inode->i_rdev = dev;
391 inode->i_bdev = bdev;
392 inode->i_data.a_ops = &def_blk_aops;
393 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
394 inode->i_data.backing_dev_info = &default_backing_dev_info;
395 spin_lock(&bdev_lock);
396 list_add(&bdev->bd_list, &all_bdevs);
397 spin_unlock(&bdev_lock);
398 unlock_new_inode(inode);
400 return bdev;
403 EXPORT_SYMBOL(bdget);
405 long nr_blockdev_pages(void)
407 struct block_device *bdev;
408 long ret = 0;
409 spin_lock(&bdev_lock);
410 list_for_each_entry(bdev, &all_bdevs, 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 bdev = inode->i_bdev;
430 if (bdev) {
431 atomic_inc(&bdev->bd_inode->i_count);
432 spin_unlock(&bdev_lock);
433 return bdev;
435 spin_unlock(&bdev_lock);
437 bdev = bdget(inode->i_rdev);
438 if (bdev) {
439 spin_lock(&bdev_lock);
440 if (!inode->i_bdev) {
442 * We take an additional bd_inode->i_count for inode,
443 * and it's released in clear_inode() of inode.
444 * So, we can access it via ->i_mapping always
445 * without igrab().
447 atomic_inc(&bdev->bd_inode->i_count);
448 inode->i_bdev = bdev;
449 inode->i_mapping = bdev->bd_inode->i_mapping;
450 list_add(&inode->i_devices, &bdev->bd_inodes);
452 spin_unlock(&bdev_lock);
454 return bdev;
457 /* Call when you free inode */
459 void bd_forget(struct inode *inode)
461 struct block_device *bdev = NULL;
463 spin_lock(&bdev_lock);
464 if (inode->i_bdev) {
465 if (inode->i_sb != blockdev_superblock)
466 bdev = inode->i_bdev;
467 __bd_forget(inode);
469 spin_unlock(&bdev_lock);
471 if (bdev)
472 iput(bdev->bd_inode);
475 int bd_claim(struct block_device *bdev, void *holder)
477 int res;
478 spin_lock(&bdev_lock);
480 /* first decide result */
481 if (bdev->bd_holder == holder)
482 res = 0; /* already a holder */
483 else if (bdev->bd_holder != NULL)
484 res = -EBUSY; /* held by someone else */
485 else if (bdev->bd_contains == bdev)
486 res = 0; /* is a whole device which isn't held */
488 else if (bdev->bd_contains->bd_holder == bd_claim)
489 res = 0; /* is a partition of a device that is being partitioned */
490 else if (bdev->bd_contains->bd_holder != NULL)
491 res = -EBUSY; /* is a partition of a held device */
492 else
493 res = 0; /* is a partition of an un-held device */
495 /* now impose change */
496 if (res==0) {
497 /* note that for a whole device bd_holders
498 * will be incremented twice, and bd_holder will
499 * be set to bd_claim before being set to holder
501 bdev->bd_contains->bd_holders ++;
502 bdev->bd_contains->bd_holder = bd_claim;
503 bdev->bd_holders++;
504 bdev->bd_holder = holder;
506 spin_unlock(&bdev_lock);
507 return res;
510 EXPORT_SYMBOL(bd_claim);
512 void bd_release(struct block_device *bdev)
514 spin_lock(&bdev_lock);
515 if (!--bdev->bd_contains->bd_holders)
516 bdev->bd_contains->bd_holder = NULL;
517 if (!--bdev->bd_holders)
518 bdev->bd_holder = NULL;
519 spin_unlock(&bdev_lock);
522 EXPORT_SYMBOL(bd_release);
524 #ifdef CONFIG_SYSFS
526 * Functions for bd_claim_by_kobject / bd_release_from_kobject
528 * If a kobject is passed to bd_claim_by_kobject()
529 * and the kobject has a parent directory,
530 * following symlinks are created:
531 * o from the kobject to the claimed bdev
532 * o from "holders" directory of the bdev to the parent of the kobject
533 * bd_release_from_kobject() removes these symlinks.
535 * Example:
536 * If /dev/dm-0 maps to /dev/sda, kobject corresponding to
537 * /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then:
538 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
539 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
542 static struct kobject *bdev_get_kobj(struct block_device *bdev)
544 if (bdev->bd_contains != bdev)
545 return kobject_get(&bdev->bd_part->dev.kobj);
546 else
547 return kobject_get(&bdev->bd_disk->dev.kobj);
550 static struct kobject *bdev_get_holder(struct block_device *bdev)
552 if (bdev->bd_contains != bdev)
553 return kobject_get(bdev->bd_part->holder_dir);
554 else
555 return kobject_get(bdev->bd_disk->holder_dir);
558 static int add_symlink(struct kobject *from, struct kobject *to)
560 if (!from || !to)
561 return 0;
562 return sysfs_create_link(from, to, kobject_name(to));
565 static void del_symlink(struct kobject *from, struct kobject *to)
567 if (!from || !to)
568 return;
569 sysfs_remove_link(from, kobject_name(to));
573 * 'struct bd_holder' contains pointers to kobjects symlinked by
574 * bd_claim_by_kobject.
575 * It's connected to bd_holder_list which is protected by bdev->bd_sem.
577 struct bd_holder {
578 struct list_head list; /* chain of holders of the bdev */
579 int count; /* references from the holder */
580 struct kobject *sdir; /* holder object, e.g. "/block/dm-0/slaves" */
581 struct kobject *hdev; /* e.g. "/block/dm-0" */
582 struct kobject *hdir; /* e.g. "/block/sda/holders" */
583 struct kobject *sdev; /* e.g. "/block/sda" */
587 * Get references of related kobjects at once.
588 * Returns 1 on success. 0 on failure.
590 * Should call bd_holder_release_dirs() after successful use.
592 static int bd_holder_grab_dirs(struct block_device *bdev,
593 struct bd_holder *bo)
595 if (!bdev || !bo)
596 return 0;
598 bo->sdir = kobject_get(bo->sdir);
599 if (!bo->sdir)
600 return 0;
602 bo->hdev = kobject_get(bo->sdir->parent);
603 if (!bo->hdev)
604 goto fail_put_sdir;
606 bo->sdev = bdev_get_kobj(bdev);
607 if (!bo->sdev)
608 goto fail_put_hdev;
610 bo->hdir = bdev_get_holder(bdev);
611 if (!bo->hdir)
612 goto fail_put_sdev;
614 return 1;
616 fail_put_sdev:
617 kobject_put(bo->sdev);
618 fail_put_hdev:
619 kobject_put(bo->hdev);
620 fail_put_sdir:
621 kobject_put(bo->sdir);
623 return 0;
626 /* Put references of related kobjects at once. */
627 static void bd_holder_release_dirs(struct bd_holder *bo)
629 kobject_put(bo->hdir);
630 kobject_put(bo->sdev);
631 kobject_put(bo->hdev);
632 kobject_put(bo->sdir);
635 static struct bd_holder *alloc_bd_holder(struct kobject *kobj)
637 struct bd_holder *bo;
639 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
640 if (!bo)
641 return NULL;
643 bo->count = 1;
644 bo->sdir = kobj;
646 return bo;
649 static void free_bd_holder(struct bd_holder *bo)
651 kfree(bo);
655 * find_bd_holder - find matching struct bd_holder from the block device
657 * @bdev: struct block device to be searched
658 * @bo: target struct bd_holder
660 * Returns matching entry with @bo in @bdev->bd_holder_list.
661 * If found, increment the reference count and return the pointer.
662 * If not found, returns NULL.
664 static struct bd_holder *find_bd_holder(struct block_device *bdev,
665 struct bd_holder *bo)
667 struct bd_holder *tmp;
669 list_for_each_entry(tmp, &bdev->bd_holder_list, list)
670 if (tmp->sdir == bo->sdir) {
671 tmp->count++;
672 return tmp;
675 return NULL;
679 * add_bd_holder - create sysfs symlinks for bd_claim() relationship
681 * @bdev: block device to be bd_claimed
682 * @bo: preallocated and initialized by alloc_bd_holder()
684 * Add @bo to @bdev->bd_holder_list, create symlinks.
686 * Returns 0 if symlinks are created.
687 * Returns -ve if something fails.
689 static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
691 int err;
693 if (!bo)
694 return -EINVAL;
696 if (!bd_holder_grab_dirs(bdev, bo))
697 return -EBUSY;
699 err = add_symlink(bo->sdir, bo->sdev);
700 if (err)
701 return err;
703 err = add_symlink(bo->hdir, bo->hdev);
704 if (err) {
705 del_symlink(bo->sdir, bo->sdev);
706 return err;
709 list_add_tail(&bo->list, &bdev->bd_holder_list);
710 return 0;
714 * del_bd_holder - delete sysfs symlinks for bd_claim() relationship
716 * @bdev: block device to be bd_claimed
717 * @kobj: holder's kobject
719 * If there is matching entry with @kobj in @bdev->bd_holder_list
720 * and no other bd_claim() from the same kobject,
721 * remove the struct bd_holder from the list, delete symlinks for it.
723 * Returns a pointer to the struct bd_holder when it's removed from the list
724 * and ready to be freed.
725 * Returns NULL if matching claim isn't found or there is other bd_claim()
726 * by the same kobject.
728 static struct bd_holder *del_bd_holder(struct block_device *bdev,
729 struct kobject *kobj)
731 struct bd_holder *bo;
733 list_for_each_entry(bo, &bdev->bd_holder_list, list) {
734 if (bo->sdir == kobj) {
735 bo->count--;
736 BUG_ON(bo->count < 0);
737 if (!bo->count) {
738 list_del(&bo->list);
739 del_symlink(bo->sdir, bo->sdev);
740 del_symlink(bo->hdir, bo->hdev);
741 bd_holder_release_dirs(bo);
742 return bo;
744 break;
748 return NULL;
752 * bd_claim_by_kobject - bd_claim() with additional kobject signature
754 * @bdev: block device to be claimed
755 * @holder: holder's signature
756 * @kobj: holder's kobject
758 * Do bd_claim() and if it succeeds, create sysfs symlinks between
759 * the bdev and the holder's kobject.
760 * Use bd_release_from_kobject() when relesing the claimed bdev.
762 * Returns 0 on success. (same as bd_claim())
763 * Returns errno on failure.
765 static int bd_claim_by_kobject(struct block_device *bdev, void *holder,
766 struct kobject *kobj)
768 int err;
769 struct bd_holder *bo, *found;
771 if (!kobj)
772 return -EINVAL;
774 bo = alloc_bd_holder(kobj);
775 if (!bo)
776 return -ENOMEM;
778 mutex_lock(&bdev->bd_mutex);
780 err = bd_claim(bdev, holder);
781 if (err)
782 goto fail;
784 found = find_bd_holder(bdev, bo);
785 if (found)
786 goto fail;
788 err = add_bd_holder(bdev, bo);
789 if (err)
790 bd_release(bdev);
791 else
792 bo = NULL;
793 fail:
794 mutex_unlock(&bdev->bd_mutex);
795 free_bd_holder(bo);
796 return err;
800 * bd_release_from_kobject - bd_release() with additional kobject signature
802 * @bdev: block device to be released
803 * @kobj: holder's kobject
805 * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject().
807 static void bd_release_from_kobject(struct block_device *bdev,
808 struct kobject *kobj)
810 if (!kobj)
811 return;
813 mutex_lock(&bdev->bd_mutex);
814 bd_release(bdev);
815 free_bd_holder(del_bd_holder(bdev, kobj));
816 mutex_unlock(&bdev->bd_mutex);
820 * bd_claim_by_disk - wrapper function for bd_claim_by_kobject()
822 * @bdev: block device to be claimed
823 * @holder: holder's signature
824 * @disk: holder's gendisk
826 * Call bd_claim_by_kobject() with getting @disk->slave_dir.
828 int bd_claim_by_disk(struct block_device *bdev, void *holder,
829 struct gendisk *disk)
831 return bd_claim_by_kobject(bdev, holder, kobject_get(disk->slave_dir));
833 EXPORT_SYMBOL_GPL(bd_claim_by_disk);
836 * bd_release_from_disk - wrapper function for bd_release_from_kobject()
838 * @bdev: block device to be claimed
839 * @disk: holder's gendisk
841 * Call bd_release_from_kobject() and put @disk->slave_dir.
843 void bd_release_from_disk(struct block_device *bdev, struct gendisk *disk)
845 bd_release_from_kobject(bdev, disk->slave_dir);
846 kobject_put(disk->slave_dir);
848 EXPORT_SYMBOL_GPL(bd_release_from_disk);
849 #endif
852 * Tries to open block device by device number. Use it ONLY if you
853 * really do not have anything better - i.e. when you are behind a
854 * truly sucky interface and all you are given is a device number. _Never_
855 * to be used for internal purposes. If you ever need it - reconsider
856 * your API.
858 struct block_device *open_by_devnum(dev_t dev, unsigned mode)
860 struct block_device *bdev = bdget(dev);
861 int err = -ENOMEM;
862 int flags = mode & FMODE_WRITE ? O_RDWR : O_RDONLY;
863 if (bdev)
864 err = blkdev_get(bdev, mode, flags);
865 return err ? ERR_PTR(err) : bdev;
868 EXPORT_SYMBOL(open_by_devnum);
871 * This routine checks whether a removable media has been changed,
872 * and invalidates all buffer-cache-entries in that case. This
873 * is a relatively slow routine, so we have to try to minimize using
874 * it. Thus it is called only upon a 'mount' or 'open'. This
875 * is the best way of combining speed and utility, I think.
876 * People changing diskettes in the middle of an operation deserve
877 * to lose :-)
879 int check_disk_change(struct block_device *bdev)
881 struct gendisk *disk = bdev->bd_disk;
882 struct block_device_operations * bdops = disk->fops;
884 if (!bdops->media_changed)
885 return 0;
886 if (!bdops->media_changed(bdev->bd_disk))
887 return 0;
889 if (__invalidate_device(bdev))
890 printk("VFS: busy inodes on changed media.\n");
892 if (bdops->revalidate_disk)
893 bdops->revalidate_disk(bdev->bd_disk);
894 if (bdev->bd_disk->minors > 1)
895 bdev->bd_invalidated = 1;
896 return 1;
899 EXPORT_SYMBOL(check_disk_change);
901 void bd_set_size(struct block_device *bdev, loff_t size)
903 unsigned bsize = bdev_hardsect_size(bdev);
905 bdev->bd_inode->i_size = size;
906 while (bsize < PAGE_CACHE_SIZE) {
907 if (size & bsize)
908 break;
909 bsize <<= 1;
911 bdev->bd_block_size = bsize;
912 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
914 EXPORT_SYMBOL(bd_set_size);
916 static int __blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags,
917 int for_part);
918 static int __blkdev_put(struct block_device *bdev, int for_part);
921 * bd_mutex locking:
923 * mutex_lock(part->bd_mutex)
924 * mutex_lock_nested(whole->bd_mutex, 1)
927 static int do_open(struct block_device *bdev, struct file *file, int for_part)
929 struct module *owner = NULL;
930 struct gendisk *disk;
931 int ret = -ENXIO;
932 int part;
934 file->f_mapping = bdev->bd_inode->i_mapping;
935 lock_kernel();
936 disk = get_gendisk(bdev->bd_dev, &part);
937 if (!disk) {
938 unlock_kernel();
939 bdput(bdev);
940 return ret;
942 owner = disk->fops->owner;
944 mutex_lock_nested(&bdev->bd_mutex, for_part);
945 if (!bdev->bd_openers) {
946 bdev->bd_disk = disk;
947 bdev->bd_contains = bdev;
948 if (!part) {
949 struct backing_dev_info *bdi;
950 if (disk->fops->open) {
951 ret = disk->fops->open(bdev->bd_inode, file);
952 if (ret)
953 goto out_first;
955 if (!bdev->bd_openers) {
956 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
957 bdi = blk_get_backing_dev_info(bdev);
958 if (bdi == NULL)
959 bdi = &default_backing_dev_info;
960 bdev->bd_inode->i_data.backing_dev_info = bdi;
962 if (bdev->bd_invalidated)
963 rescan_partitions(disk, bdev);
964 } else {
965 struct hd_struct *p;
966 struct block_device *whole;
967 whole = bdget_disk(disk, 0);
968 ret = -ENOMEM;
969 if (!whole)
970 goto out_first;
971 BUG_ON(for_part);
972 ret = __blkdev_get(whole, file->f_mode, file->f_flags, 1);
973 if (ret)
974 goto out_first;
975 bdev->bd_contains = whole;
976 p = disk->part[part - 1];
977 bdev->bd_inode->i_data.backing_dev_info =
978 whole->bd_inode->i_data.backing_dev_info;
979 if (!(disk->flags & GENHD_FL_UP) || !p || !p->nr_sects) {
980 ret = -ENXIO;
981 goto out_first;
983 kobject_get(&p->dev.kobj);
984 bdev->bd_part = p;
985 bd_set_size(bdev, (loff_t) p->nr_sects << 9);
987 } else {
988 put_disk(disk);
989 module_put(owner);
990 if (bdev->bd_contains == bdev) {
991 if (bdev->bd_disk->fops->open) {
992 ret = bdev->bd_disk->fops->open(bdev->bd_inode, file);
993 if (ret)
994 goto out;
996 if (bdev->bd_invalidated)
997 rescan_partitions(bdev->bd_disk, bdev);
1000 bdev->bd_openers++;
1001 if (for_part)
1002 bdev->bd_part_count++;
1003 mutex_unlock(&bdev->bd_mutex);
1004 unlock_kernel();
1005 return 0;
1007 out_first:
1008 bdev->bd_disk = NULL;
1009 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
1010 if (bdev != bdev->bd_contains)
1011 __blkdev_put(bdev->bd_contains, 1);
1012 bdev->bd_contains = NULL;
1013 put_disk(disk);
1014 module_put(owner);
1015 out:
1016 mutex_unlock(&bdev->bd_mutex);
1017 unlock_kernel();
1018 if (ret)
1019 bdput(bdev);
1020 return ret;
1023 static int __blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags,
1024 int for_part)
1027 * This crockload is due to bad choice of ->open() type.
1028 * It will go away.
1029 * For now, block device ->open() routine must _not_
1030 * examine anything in 'inode' argument except ->i_rdev.
1032 struct file fake_file = {};
1033 struct dentry fake_dentry = {};
1034 fake_file.f_mode = mode;
1035 fake_file.f_flags = flags;
1036 fake_file.f_path.dentry = &fake_dentry;
1037 fake_dentry.d_inode = bdev->bd_inode;
1039 return do_open(bdev, &fake_file, for_part);
1042 int blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags)
1044 return __blkdev_get(bdev, mode, flags, 0);
1046 EXPORT_SYMBOL(blkdev_get);
1048 static int blkdev_open(struct inode * inode, struct file * filp)
1050 struct block_device *bdev;
1051 int res;
1054 * Preserve backwards compatibility and allow large file access
1055 * even if userspace doesn't ask for it explicitly. Some mkfs
1056 * binary needs it. We might want to drop this workaround
1057 * during an unstable branch.
1059 filp->f_flags |= O_LARGEFILE;
1061 bdev = bd_acquire(inode);
1062 if (bdev == NULL)
1063 return -ENOMEM;
1065 res = do_open(bdev, filp, 0);
1066 if (res)
1067 return res;
1069 if (!(filp->f_flags & O_EXCL) )
1070 return 0;
1072 if (!(res = bd_claim(bdev, filp)))
1073 return 0;
1075 blkdev_put(bdev);
1076 return res;
1079 static int __blkdev_put(struct block_device *bdev, int for_part)
1081 int ret = 0;
1082 struct inode *bd_inode = bdev->bd_inode;
1083 struct gendisk *disk = bdev->bd_disk;
1084 struct block_device *victim = NULL;
1086 mutex_lock_nested(&bdev->bd_mutex, for_part);
1087 lock_kernel();
1088 if (for_part)
1089 bdev->bd_part_count--;
1091 if (!--bdev->bd_openers) {
1092 sync_blockdev(bdev);
1093 kill_bdev(bdev);
1095 if (bdev->bd_contains == bdev) {
1096 if (disk->fops->release)
1097 ret = disk->fops->release(bd_inode, NULL);
1099 if (!bdev->bd_openers) {
1100 struct module *owner = disk->fops->owner;
1102 put_disk(disk);
1103 module_put(owner);
1105 if (bdev->bd_contains != bdev) {
1106 kobject_put(&bdev->bd_part->dev.kobj);
1107 bdev->bd_part = NULL;
1109 bdev->bd_disk = NULL;
1110 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
1111 if (bdev != bdev->bd_contains)
1112 victim = bdev->bd_contains;
1113 bdev->bd_contains = NULL;
1115 unlock_kernel();
1116 mutex_unlock(&bdev->bd_mutex);
1117 bdput(bdev);
1118 if (victim)
1119 __blkdev_put(victim, 1);
1120 return ret;
1123 int blkdev_put(struct block_device *bdev)
1125 return __blkdev_put(bdev, 0);
1127 EXPORT_SYMBOL(blkdev_put);
1129 static int blkdev_close(struct inode * inode, struct file * filp)
1131 struct block_device *bdev = I_BDEV(filp->f_mapping->host);
1132 if (bdev->bd_holder == filp)
1133 bd_release(bdev);
1134 return blkdev_put(bdev);
1137 static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1139 return blkdev_ioctl(file->f_mapping->host, file, cmd, arg);
1142 static const struct address_space_operations def_blk_aops = {
1143 .readpage = blkdev_readpage,
1144 .writepage = blkdev_writepage,
1145 .sync_page = block_sync_page,
1146 .write_begin = blkdev_write_begin,
1147 .write_end = blkdev_write_end,
1148 .writepages = generic_writepages,
1149 .direct_IO = blkdev_direct_IO,
1152 const struct file_operations def_blk_fops = {
1153 .open = blkdev_open,
1154 .release = blkdev_close,
1155 .llseek = block_llseek,
1156 .read = do_sync_read,
1157 .write = do_sync_write,
1158 .aio_read = generic_file_aio_read,
1159 .aio_write = generic_file_aio_write_nolock,
1160 .mmap = generic_file_mmap,
1161 .fsync = block_fsync,
1162 .unlocked_ioctl = block_ioctl,
1163 #ifdef CONFIG_COMPAT
1164 .compat_ioctl = compat_blkdev_ioctl,
1165 #endif
1166 .splice_read = generic_file_splice_read,
1167 .splice_write = generic_file_splice_write,
1170 int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
1172 int res;
1173 mm_segment_t old_fs = get_fs();
1174 set_fs(KERNEL_DS);
1175 res = blkdev_ioctl(bdev->bd_inode, NULL, cmd, arg);
1176 set_fs(old_fs);
1177 return res;
1180 EXPORT_SYMBOL(ioctl_by_bdev);
1183 * lookup_bdev - lookup a struct block_device by name
1185 * @path: special file representing the block device
1187 * Get a reference to the blockdevice at @path in the current
1188 * namespace if possible and return it. Return ERR_PTR(error)
1189 * otherwise.
1191 struct block_device *lookup_bdev(const char *path)
1193 struct block_device *bdev;
1194 struct inode *inode;
1195 struct nameidata nd;
1196 int error;
1198 if (!path || !*path)
1199 return ERR_PTR(-EINVAL);
1201 error = path_lookup(path, LOOKUP_FOLLOW, &nd);
1202 if (error)
1203 return ERR_PTR(error);
1205 inode = nd.path.dentry->d_inode;
1206 error = -ENOTBLK;
1207 if (!S_ISBLK(inode->i_mode))
1208 goto fail;
1209 error = -EACCES;
1210 if (nd.path.mnt->mnt_flags & MNT_NODEV)
1211 goto fail;
1212 error = -ENOMEM;
1213 bdev = bd_acquire(inode);
1214 if (!bdev)
1215 goto fail;
1216 out:
1217 path_put(&nd.path);
1218 return bdev;
1219 fail:
1220 bdev = ERR_PTR(error);
1221 goto out;
1225 * open_bdev_excl - open a block device by name and set it up for use
1227 * @path: special file representing the block device
1228 * @flags: %MS_RDONLY for opening read-only
1229 * @holder: owner for exclusion
1231 * Open the blockdevice described by the special file at @path, claim it
1232 * for the @holder.
1234 struct block_device *open_bdev_excl(const char *path, int flags, void *holder)
1236 struct block_device *bdev;
1237 mode_t mode = FMODE_READ;
1238 int error = 0;
1240 bdev = lookup_bdev(path);
1241 if (IS_ERR(bdev))
1242 return bdev;
1244 if (!(flags & MS_RDONLY))
1245 mode |= FMODE_WRITE;
1246 error = blkdev_get(bdev, mode, 0);
1247 if (error)
1248 return ERR_PTR(error);
1249 error = -EACCES;
1250 if (!(flags & MS_RDONLY) && bdev_read_only(bdev))
1251 goto blkdev_put;
1252 error = bd_claim(bdev, holder);
1253 if (error)
1254 goto blkdev_put;
1256 return bdev;
1258 blkdev_put:
1259 blkdev_put(bdev);
1260 return ERR_PTR(error);
1263 EXPORT_SYMBOL(open_bdev_excl);
1266 * close_bdev_excl - release a blockdevice openen by open_bdev_excl()
1268 * @bdev: blockdevice to close
1270 * This is the counterpart to open_bdev_excl().
1272 void close_bdev_excl(struct block_device *bdev)
1274 bd_release(bdev);
1275 blkdev_put(bdev);
1278 EXPORT_SYMBOL(close_bdev_excl);
1280 int __invalidate_device(struct block_device *bdev)
1282 struct super_block *sb = get_super(bdev);
1283 int res = 0;
1285 if (sb) {
1287 * no need to lock the super, get_super holds the
1288 * read mutex so the filesystem cannot go away
1289 * under us (->put_super runs with the write lock
1290 * hold).
1292 shrink_dcache_sb(sb);
1293 res = invalidate_inodes(sb);
1294 drop_super(sb);
1296 invalidate_bdev(bdev);
1297 return res;
1299 EXPORT_SYMBOL(__invalidate_device);