PCI: rework Documentation/pci.txt
[linux-2.6.22.y-op.git] / fs / block_dev.c
blob8b18e43b82fe8e156bab431070a882ea5cb9a5bf
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 static int blk_end_aio(struct bio *bio, unsigned int bytes_done, int error)
134 struct kiocb *iocb = bio->bi_private;
135 atomic_t *bio_count = &iocb->ki_bio_count;
137 if (bio_data_dir(bio) == READ)
138 bio_check_pages_dirty(bio);
139 else {
140 bio_release_pages(bio);
141 bio_put(bio);
144 /* iocb->ki_nbytes stores error code from LLDD */
145 if (error)
146 iocb->ki_nbytes = -EIO;
148 if (atomic_dec_and_test(bio_count)) {
149 if (iocb->ki_nbytes < 0)
150 aio_complete(iocb, iocb->ki_nbytes, 0);
151 else
152 aio_complete(iocb, iocb->ki_left, 0);
155 return 0;
158 #define VEC_SIZE 16
159 struct pvec {
160 unsigned short nr;
161 unsigned short idx;
162 struct page *page[VEC_SIZE];
165 #define PAGES_SPANNED(addr, len) \
166 (DIV_ROUND_UP((addr) + (len), PAGE_SIZE) - (addr) / PAGE_SIZE);
169 * get page pointer for user addr, we internally cache struct page array for
170 * (addr, count) range in pvec to avoid frequent call to get_user_pages. If
171 * internal page list is exhausted, a batch count of up to VEC_SIZE is used
172 * to get next set of page struct.
174 static struct page *blk_get_page(unsigned long addr, size_t count, int rw,
175 struct pvec *pvec)
177 int ret, nr_pages;
178 if (pvec->idx == pvec->nr) {
179 nr_pages = PAGES_SPANNED(addr, count);
180 nr_pages = min(nr_pages, VEC_SIZE);
181 down_read(&current->mm->mmap_sem);
182 ret = get_user_pages(current, current->mm, addr, nr_pages,
183 rw == READ, 0, pvec->page, NULL);
184 up_read(&current->mm->mmap_sem);
185 if (ret < 0)
186 return ERR_PTR(ret);
187 pvec->nr = ret;
188 pvec->idx = 0;
190 return pvec->page[pvec->idx++];
193 static ssize_t
194 blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
195 loff_t pos, unsigned long nr_segs)
197 struct inode *inode = iocb->ki_filp->f_mapping->host;
198 unsigned blkbits = blksize_bits(bdev_hardsect_size(I_BDEV(inode)));
199 unsigned blocksize_mask = (1 << blkbits) - 1;
200 unsigned long seg = 0; /* iov segment iterator */
201 unsigned long nvec; /* number of bio vec needed */
202 unsigned long cur_off; /* offset into current page */
203 unsigned long cur_len; /* I/O len of current page, up to PAGE_SIZE */
205 unsigned long addr; /* user iovec address */
206 size_t count; /* user iovec len */
207 size_t nbytes = iocb->ki_nbytes = iocb->ki_left; /* total xfer size */
208 loff_t size; /* size of block device */
209 struct bio *bio;
210 atomic_t *bio_count = &iocb->ki_bio_count;
211 struct page *page;
212 struct pvec pvec;
214 pvec.nr = 0;
215 pvec.idx = 0;
217 if (pos & blocksize_mask)
218 return -EINVAL;
220 size = i_size_read(inode);
221 if (pos + nbytes > size) {
222 nbytes = size - pos;
223 iocb->ki_left = nbytes;
227 * check first non-zero iov alignment, the remaining
228 * iov alignment is checked inside bio loop below.
230 do {
231 addr = (unsigned long) iov[seg].iov_base;
232 count = min(iov[seg].iov_len, nbytes);
233 if (addr & blocksize_mask || count & blocksize_mask)
234 return -EINVAL;
235 } while (!count && ++seg < nr_segs);
236 atomic_set(bio_count, 1);
238 while (nbytes) {
239 /* roughly estimate number of bio vec needed */
240 nvec = (nbytes + PAGE_SIZE - 1) / PAGE_SIZE;
241 nvec = max(nvec, nr_segs - seg);
242 nvec = min(nvec, (unsigned long) BIO_MAX_PAGES);
244 /* bio_alloc should not fail with GFP_KERNEL flag */
245 bio = bio_alloc(GFP_KERNEL, nvec);
246 bio->bi_bdev = I_BDEV(inode);
247 bio->bi_end_io = blk_end_aio;
248 bio->bi_private = iocb;
249 bio->bi_sector = pos >> blkbits;
250 same_bio:
251 cur_off = addr & ~PAGE_MASK;
252 cur_len = PAGE_SIZE - cur_off;
253 if (count < cur_len)
254 cur_len = count;
256 page = blk_get_page(addr, count, rw, &pvec);
257 if (unlikely(IS_ERR(page)))
258 goto backout;
260 if (bio_add_page(bio, page, cur_len, cur_off)) {
261 pos += cur_len;
262 addr += cur_len;
263 count -= cur_len;
264 nbytes -= cur_len;
266 if (count)
267 goto same_bio;
268 while (++seg < nr_segs) {
269 addr = (unsigned long) iov[seg].iov_base;
270 count = iov[seg].iov_len;
271 if (!count)
272 continue;
273 if (unlikely(addr & blocksize_mask ||
274 count & blocksize_mask)) {
275 page = ERR_PTR(-EINVAL);
276 goto backout;
278 count = min(count, nbytes);
279 goto same_bio;
283 /* bio is ready, submit it */
284 if (rw == READ)
285 bio_set_pages_dirty(bio);
286 atomic_inc(bio_count);
287 submit_bio(rw, bio);
290 completion:
291 iocb->ki_left -= nbytes;
292 nbytes = iocb->ki_left;
293 iocb->ki_pos += nbytes;
295 blk_run_address_space(inode->i_mapping);
296 if (atomic_dec_and_test(bio_count))
297 aio_complete(iocb, nbytes, 0);
299 return -EIOCBQUEUED;
301 backout:
303 * back out nbytes count constructed so far for this bio,
304 * we will throw away current bio.
306 nbytes += bio->bi_size;
307 bio_release_pages(bio);
308 bio_put(bio);
311 * if no bio was submmitted, return the error code.
312 * otherwise, proceed with pending I/O completion.
314 if (atomic_read(bio_count) == 1)
315 return PTR_ERR(page);
316 goto completion;
319 static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
321 return block_write_full_page(page, blkdev_get_block, wbc);
324 static int blkdev_readpage(struct file * file, struct page * page)
326 return block_read_full_page(page, blkdev_get_block);
329 static int blkdev_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
331 return block_prepare_write(page, from, to, blkdev_get_block);
334 static int blkdev_commit_write(struct file *file, struct page *page, unsigned from, unsigned to)
336 return block_commit_write(page, from, to);
340 * private llseek:
341 * for a block special file file->f_path.dentry->d_inode->i_size is zero
342 * so we compute the size by hand (just as in block_read/write above)
344 static loff_t block_llseek(struct file *file, loff_t offset, int origin)
346 struct inode *bd_inode = file->f_mapping->host;
347 loff_t size;
348 loff_t retval;
350 mutex_lock(&bd_inode->i_mutex);
351 size = i_size_read(bd_inode);
353 switch (origin) {
354 case 2:
355 offset += size;
356 break;
357 case 1:
358 offset += file->f_pos;
360 retval = -EINVAL;
361 if (offset >= 0 && offset <= size) {
362 if (offset != file->f_pos) {
363 file->f_pos = offset;
365 retval = offset;
367 mutex_unlock(&bd_inode->i_mutex);
368 return retval;
372 * Filp is never NULL; the only case when ->fsync() is called with
373 * NULL first argument is nfsd_sync_dir() and that's not a directory.
376 static int block_fsync(struct file *filp, struct dentry *dentry, int datasync)
378 return sync_blockdev(I_BDEV(filp->f_mapping->host));
382 * pseudo-fs
385 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
386 static struct kmem_cache * bdev_cachep __read_mostly;
388 static struct inode *bdev_alloc_inode(struct super_block *sb)
390 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
391 if (!ei)
392 return NULL;
393 return &ei->vfs_inode;
396 static void bdev_destroy_inode(struct inode *inode)
398 struct bdev_inode *bdi = BDEV_I(inode);
400 bdi->bdev.bd_inode_backing_dev_info = NULL;
401 kmem_cache_free(bdev_cachep, bdi);
404 static void init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
406 struct bdev_inode *ei = (struct bdev_inode *) foo;
407 struct block_device *bdev = &ei->bdev;
409 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
410 SLAB_CTOR_CONSTRUCTOR)
412 memset(bdev, 0, sizeof(*bdev));
413 mutex_init(&bdev->bd_mutex);
414 sema_init(&bdev->bd_mount_sem, 1);
415 INIT_LIST_HEAD(&bdev->bd_inodes);
416 INIT_LIST_HEAD(&bdev->bd_list);
417 #ifdef CONFIG_SYSFS
418 INIT_LIST_HEAD(&bdev->bd_holder_list);
419 #endif
420 inode_init_once(&ei->vfs_inode);
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_clear_inode(struct inode *inode)
433 struct block_device *bdev = &BDEV_I(inode)->bdev;
434 struct list_head *p;
435 spin_lock(&bdev_lock);
436 while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
437 __bd_forget(list_entry(p, struct inode, i_devices));
439 list_del_init(&bdev->bd_list);
440 spin_unlock(&bdev_lock);
443 static struct super_operations bdev_sops = {
444 .statfs = simple_statfs,
445 .alloc_inode = bdev_alloc_inode,
446 .destroy_inode = bdev_destroy_inode,
447 .drop_inode = generic_delete_inode,
448 .clear_inode = bdev_clear_inode,
451 static int bd_get_sb(struct file_system_type *fs_type,
452 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
454 return get_sb_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576, mnt);
457 static struct file_system_type bd_type = {
458 .name = "bdev",
459 .get_sb = bd_get_sb,
460 .kill_sb = kill_anon_super,
463 static struct vfsmount *bd_mnt __read_mostly;
464 struct super_block *blockdev_superblock;
466 void __init bdev_cache_init(void)
468 int err;
469 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
470 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
471 SLAB_MEM_SPREAD|SLAB_PANIC),
472 init_once, NULL);
473 err = register_filesystem(&bd_type);
474 if (err)
475 panic("Cannot register bdev pseudo-fs");
476 bd_mnt = kern_mount(&bd_type);
477 err = PTR_ERR(bd_mnt);
478 if (IS_ERR(bd_mnt))
479 panic("Cannot create bdev pseudo-fs");
480 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
484 * Most likely _very_ bad one - but then it's hardly critical for small
485 * /dev and can be fixed when somebody will need really large one.
486 * Keep in mind that it will be fed through icache hash function too.
488 static inline unsigned long hash(dev_t dev)
490 return MAJOR(dev)+MINOR(dev);
493 static int bdev_test(struct inode *inode, void *data)
495 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
498 static int bdev_set(struct inode *inode, void *data)
500 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
501 return 0;
504 static LIST_HEAD(all_bdevs);
506 struct block_device *bdget(dev_t dev)
508 struct block_device *bdev;
509 struct inode *inode;
511 inode = iget5_locked(bd_mnt->mnt_sb, hash(dev),
512 bdev_test, bdev_set, &dev);
514 if (!inode)
515 return NULL;
517 bdev = &BDEV_I(inode)->bdev;
519 if (inode->i_state & I_NEW) {
520 bdev->bd_contains = NULL;
521 bdev->bd_inode = inode;
522 bdev->bd_block_size = (1 << inode->i_blkbits);
523 bdev->bd_part_count = 0;
524 bdev->bd_invalidated = 0;
525 inode->i_mode = S_IFBLK;
526 inode->i_rdev = dev;
527 inode->i_bdev = bdev;
528 inode->i_data.a_ops = &def_blk_aops;
529 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
530 inode->i_data.backing_dev_info = &default_backing_dev_info;
531 spin_lock(&bdev_lock);
532 list_add(&bdev->bd_list, &all_bdevs);
533 spin_unlock(&bdev_lock);
534 unlock_new_inode(inode);
536 return bdev;
539 EXPORT_SYMBOL(bdget);
541 long nr_blockdev_pages(void)
543 struct list_head *p;
544 long ret = 0;
545 spin_lock(&bdev_lock);
546 list_for_each(p, &all_bdevs) {
547 struct block_device *bdev;
548 bdev = list_entry(p, struct block_device, bd_list);
549 ret += bdev->bd_inode->i_mapping->nrpages;
551 spin_unlock(&bdev_lock);
552 return ret;
555 void bdput(struct block_device *bdev)
557 iput(bdev->bd_inode);
560 EXPORT_SYMBOL(bdput);
562 static struct block_device *bd_acquire(struct inode *inode)
564 struct block_device *bdev;
566 spin_lock(&bdev_lock);
567 bdev = inode->i_bdev;
568 if (bdev) {
569 atomic_inc(&bdev->bd_inode->i_count);
570 spin_unlock(&bdev_lock);
571 return bdev;
573 spin_unlock(&bdev_lock);
575 bdev = bdget(inode->i_rdev);
576 if (bdev) {
577 spin_lock(&bdev_lock);
578 if (!inode->i_bdev) {
580 * We take an additional bd_inode->i_count for inode,
581 * and it's released in clear_inode() of inode.
582 * So, we can access it via ->i_mapping always
583 * without igrab().
585 atomic_inc(&bdev->bd_inode->i_count);
586 inode->i_bdev = bdev;
587 inode->i_mapping = bdev->bd_inode->i_mapping;
588 list_add(&inode->i_devices, &bdev->bd_inodes);
590 spin_unlock(&bdev_lock);
592 return bdev;
595 /* Call when you free inode */
597 void bd_forget(struct inode *inode)
599 struct block_device *bdev = NULL;
601 spin_lock(&bdev_lock);
602 if (inode->i_bdev) {
603 if (inode->i_sb != blockdev_superblock)
604 bdev = inode->i_bdev;
605 __bd_forget(inode);
607 spin_unlock(&bdev_lock);
609 if (bdev)
610 iput(bdev->bd_inode);
613 int bd_claim(struct block_device *bdev, void *holder)
615 int res;
616 spin_lock(&bdev_lock);
618 /* first decide result */
619 if (bdev->bd_holder == holder)
620 res = 0; /* already a holder */
621 else if (bdev->bd_holder != NULL)
622 res = -EBUSY; /* held by someone else */
623 else if (bdev->bd_contains == bdev)
624 res = 0; /* is a whole device which isn't held */
626 else if (bdev->bd_contains->bd_holder == bd_claim)
627 res = 0; /* is a partition of a device that is being partitioned */
628 else if (bdev->bd_contains->bd_holder != NULL)
629 res = -EBUSY; /* is a partition of a held device */
630 else
631 res = 0; /* is a partition of an un-held device */
633 /* now impose change */
634 if (res==0) {
635 /* note that for a whole device bd_holders
636 * will be incremented twice, and bd_holder will
637 * be set to bd_claim before being set to holder
639 bdev->bd_contains->bd_holders ++;
640 bdev->bd_contains->bd_holder = bd_claim;
641 bdev->bd_holders++;
642 bdev->bd_holder = holder;
644 spin_unlock(&bdev_lock);
645 return res;
648 EXPORT_SYMBOL(bd_claim);
650 void bd_release(struct block_device *bdev)
652 spin_lock(&bdev_lock);
653 if (!--bdev->bd_contains->bd_holders)
654 bdev->bd_contains->bd_holder = NULL;
655 if (!--bdev->bd_holders)
656 bdev->bd_holder = NULL;
657 spin_unlock(&bdev_lock);
660 EXPORT_SYMBOL(bd_release);
662 #ifdef CONFIG_SYSFS
664 * Functions for bd_claim_by_kobject / bd_release_from_kobject
666 * If a kobject is passed to bd_claim_by_kobject()
667 * and the kobject has a parent directory,
668 * following symlinks are created:
669 * o from the kobject to the claimed bdev
670 * o from "holders" directory of the bdev to the parent of the kobject
671 * bd_release_from_kobject() removes these symlinks.
673 * Example:
674 * If /dev/dm-0 maps to /dev/sda, kobject corresponding to
675 * /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then:
676 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
677 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
680 static struct kobject *bdev_get_kobj(struct block_device *bdev)
682 if (bdev->bd_contains != bdev)
683 return kobject_get(&bdev->bd_part->kobj);
684 else
685 return kobject_get(&bdev->bd_disk->kobj);
688 static struct kobject *bdev_get_holder(struct block_device *bdev)
690 if (bdev->bd_contains != bdev)
691 return kobject_get(bdev->bd_part->holder_dir);
692 else
693 return kobject_get(bdev->bd_disk->holder_dir);
696 static int add_symlink(struct kobject *from, struct kobject *to)
698 if (!from || !to)
699 return 0;
700 return sysfs_create_link(from, to, kobject_name(to));
703 static void del_symlink(struct kobject *from, struct kobject *to)
705 if (!from || !to)
706 return;
707 sysfs_remove_link(from, kobject_name(to));
711 * 'struct bd_holder' contains pointers to kobjects symlinked by
712 * bd_claim_by_kobject.
713 * It's connected to bd_holder_list which is protected by bdev->bd_sem.
715 struct bd_holder {
716 struct list_head list; /* chain of holders of the bdev */
717 int count; /* references from the holder */
718 struct kobject *sdir; /* holder object, e.g. "/block/dm-0/slaves" */
719 struct kobject *hdev; /* e.g. "/block/dm-0" */
720 struct kobject *hdir; /* e.g. "/block/sda/holders" */
721 struct kobject *sdev; /* e.g. "/block/sda" */
725 * Get references of related kobjects at once.
726 * Returns 1 on success. 0 on failure.
728 * Should call bd_holder_release_dirs() after successful use.
730 static int bd_holder_grab_dirs(struct block_device *bdev,
731 struct bd_holder *bo)
733 if (!bdev || !bo)
734 return 0;
736 bo->sdir = kobject_get(bo->sdir);
737 if (!bo->sdir)
738 return 0;
740 bo->hdev = kobject_get(bo->sdir->parent);
741 if (!bo->hdev)
742 goto fail_put_sdir;
744 bo->sdev = bdev_get_kobj(bdev);
745 if (!bo->sdev)
746 goto fail_put_hdev;
748 bo->hdir = bdev_get_holder(bdev);
749 if (!bo->hdir)
750 goto fail_put_sdev;
752 return 1;
754 fail_put_sdev:
755 kobject_put(bo->sdev);
756 fail_put_hdev:
757 kobject_put(bo->hdev);
758 fail_put_sdir:
759 kobject_put(bo->sdir);
761 return 0;
764 /* Put references of related kobjects at once. */
765 static void bd_holder_release_dirs(struct bd_holder *bo)
767 kobject_put(bo->hdir);
768 kobject_put(bo->sdev);
769 kobject_put(bo->hdev);
770 kobject_put(bo->sdir);
773 static struct bd_holder *alloc_bd_holder(struct kobject *kobj)
775 struct bd_holder *bo;
777 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
778 if (!bo)
779 return NULL;
781 bo->count = 1;
782 bo->sdir = kobj;
784 return bo;
787 static void free_bd_holder(struct bd_holder *bo)
789 kfree(bo);
793 * find_bd_holder - find matching struct bd_holder from the block device
795 * @bdev: struct block device to be searched
796 * @bo: target struct bd_holder
798 * Returns matching entry with @bo in @bdev->bd_holder_list.
799 * If found, increment the reference count and return the pointer.
800 * If not found, returns NULL.
802 static struct bd_holder *find_bd_holder(struct block_device *bdev,
803 struct bd_holder *bo)
805 struct bd_holder *tmp;
807 list_for_each_entry(tmp, &bdev->bd_holder_list, list)
808 if (tmp->sdir == bo->sdir) {
809 tmp->count++;
810 return tmp;
813 return NULL;
817 * add_bd_holder - create sysfs symlinks for bd_claim() relationship
819 * @bdev: block device to be bd_claimed
820 * @bo: preallocated and initialized by alloc_bd_holder()
822 * Add @bo to @bdev->bd_holder_list, create symlinks.
824 * Returns 0 if symlinks are created.
825 * Returns -ve if something fails.
827 static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
829 int ret;
831 if (!bo)
832 return -EINVAL;
834 if (!bd_holder_grab_dirs(bdev, bo))
835 return -EBUSY;
837 ret = add_symlink(bo->sdir, bo->sdev);
838 if (ret == 0) {
839 ret = add_symlink(bo->hdir, bo->hdev);
840 if (ret)
841 del_symlink(bo->sdir, bo->sdev);
843 if (ret == 0)
844 list_add_tail(&bo->list, &bdev->bd_holder_list);
845 return ret;
849 * del_bd_holder - delete sysfs symlinks for bd_claim() relationship
851 * @bdev: block device to be bd_claimed
852 * @kobj: holder's kobject
854 * If there is matching entry with @kobj in @bdev->bd_holder_list
855 * and no other bd_claim() from the same kobject,
856 * remove the struct bd_holder from the list, delete symlinks for it.
858 * Returns a pointer to the struct bd_holder when it's removed from the list
859 * and ready to be freed.
860 * Returns NULL if matching claim isn't found or there is other bd_claim()
861 * by the same kobject.
863 static struct bd_holder *del_bd_holder(struct block_device *bdev,
864 struct kobject *kobj)
866 struct bd_holder *bo;
868 list_for_each_entry(bo, &bdev->bd_holder_list, list) {
869 if (bo->sdir == kobj) {
870 bo->count--;
871 BUG_ON(bo->count < 0);
872 if (!bo->count) {
873 list_del(&bo->list);
874 del_symlink(bo->sdir, bo->sdev);
875 del_symlink(bo->hdir, bo->hdev);
876 bd_holder_release_dirs(bo);
877 return bo;
879 break;
883 return NULL;
887 * bd_claim_by_kobject - bd_claim() with additional kobject signature
889 * @bdev: block device to be claimed
890 * @holder: holder's signature
891 * @kobj: holder's kobject
893 * Do bd_claim() and if it succeeds, create sysfs symlinks between
894 * the bdev and the holder's kobject.
895 * Use bd_release_from_kobject() when relesing the claimed bdev.
897 * Returns 0 on success. (same as bd_claim())
898 * Returns errno on failure.
900 static int bd_claim_by_kobject(struct block_device *bdev, void *holder,
901 struct kobject *kobj)
903 int res;
904 struct bd_holder *bo, *found;
906 if (!kobj)
907 return -EINVAL;
909 bo = alloc_bd_holder(kobj);
910 if (!bo)
911 return -ENOMEM;
913 mutex_lock(&bdev->bd_mutex);
914 res = bd_claim(bdev, holder);
915 if (res == 0) {
916 found = find_bd_holder(bdev, bo);
917 if (found == NULL) {
918 res = add_bd_holder(bdev, bo);
919 if (res)
920 bd_release(bdev);
924 if (res || found)
925 free_bd_holder(bo);
926 mutex_unlock(&bdev->bd_mutex);
928 return res;
932 * bd_release_from_kobject - bd_release() with additional kobject signature
934 * @bdev: block device to be released
935 * @kobj: holder's kobject
937 * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject().
939 static void bd_release_from_kobject(struct block_device *bdev,
940 struct kobject *kobj)
942 struct bd_holder *bo;
944 if (!kobj)
945 return;
947 mutex_lock(&bdev->bd_mutex);
948 bd_release(bdev);
949 if ((bo = del_bd_holder(bdev, kobj)))
950 free_bd_holder(bo);
951 mutex_unlock(&bdev->bd_mutex);
955 * bd_claim_by_disk - wrapper function for bd_claim_by_kobject()
957 * @bdev: block device to be claimed
958 * @holder: holder's signature
959 * @disk: holder's gendisk
961 * Call bd_claim_by_kobject() with getting @disk->slave_dir.
963 int bd_claim_by_disk(struct block_device *bdev, void *holder,
964 struct gendisk *disk)
966 return bd_claim_by_kobject(bdev, holder, kobject_get(disk->slave_dir));
968 EXPORT_SYMBOL_GPL(bd_claim_by_disk);
971 * bd_release_from_disk - wrapper function for bd_release_from_kobject()
973 * @bdev: block device to be claimed
974 * @disk: holder's gendisk
976 * Call bd_release_from_kobject() and put @disk->slave_dir.
978 void bd_release_from_disk(struct block_device *bdev, struct gendisk *disk)
980 bd_release_from_kobject(bdev, disk->slave_dir);
981 kobject_put(disk->slave_dir);
983 EXPORT_SYMBOL_GPL(bd_release_from_disk);
984 #endif
987 * Tries to open block device by device number. Use it ONLY if you
988 * really do not have anything better - i.e. when you are behind a
989 * truly sucky interface and all you are given is a device number. _Never_
990 * to be used for internal purposes. If you ever need it - reconsider
991 * your API.
993 struct block_device *open_by_devnum(dev_t dev, unsigned mode)
995 struct block_device *bdev = bdget(dev);
996 int err = -ENOMEM;
997 int flags = mode & FMODE_WRITE ? O_RDWR : O_RDONLY;
998 if (bdev)
999 err = blkdev_get(bdev, mode, flags);
1000 return err ? ERR_PTR(err) : bdev;
1003 EXPORT_SYMBOL(open_by_devnum);
1006 * This routine checks whether a removable media has been changed,
1007 * and invalidates all buffer-cache-entries in that case. This
1008 * is a relatively slow routine, so we have to try to minimize using
1009 * it. Thus it is called only upon a 'mount' or 'open'. This
1010 * is the best way of combining speed and utility, I think.
1011 * People changing diskettes in the middle of an operation deserve
1012 * to lose :-)
1014 int check_disk_change(struct block_device *bdev)
1016 struct gendisk *disk = bdev->bd_disk;
1017 struct block_device_operations * bdops = disk->fops;
1019 if (!bdops->media_changed)
1020 return 0;
1021 if (!bdops->media_changed(bdev->bd_disk))
1022 return 0;
1024 if (__invalidate_device(bdev))
1025 printk("VFS: busy inodes on changed media.\n");
1027 if (bdops->revalidate_disk)
1028 bdops->revalidate_disk(bdev->bd_disk);
1029 if (bdev->bd_disk->minors > 1)
1030 bdev->bd_invalidated = 1;
1031 return 1;
1034 EXPORT_SYMBOL(check_disk_change);
1036 void bd_set_size(struct block_device *bdev, loff_t size)
1038 unsigned bsize = bdev_hardsect_size(bdev);
1040 bdev->bd_inode->i_size = size;
1041 while (bsize < PAGE_CACHE_SIZE) {
1042 if (size & bsize)
1043 break;
1044 bsize <<= 1;
1046 bdev->bd_block_size = bsize;
1047 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
1049 EXPORT_SYMBOL(bd_set_size);
1051 static int __blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags,
1052 int for_part);
1053 static int __blkdev_put(struct block_device *bdev, int for_part);
1055 static int do_open(struct block_device *bdev, struct file *file, int for_part)
1057 struct module *owner = NULL;
1058 struct gendisk *disk;
1059 int ret = -ENXIO;
1060 int part;
1062 file->f_mapping = bdev->bd_inode->i_mapping;
1063 lock_kernel();
1064 disk = get_gendisk(bdev->bd_dev, &part);
1065 if (!disk) {
1066 unlock_kernel();
1067 bdput(bdev);
1068 return ret;
1070 owner = disk->fops->owner;
1072 mutex_lock_nested(&bdev->bd_mutex, for_part);
1073 if (!bdev->bd_openers) {
1074 bdev->bd_disk = disk;
1075 bdev->bd_contains = bdev;
1076 if (!part) {
1077 struct backing_dev_info *bdi;
1078 if (disk->fops->open) {
1079 ret = disk->fops->open(bdev->bd_inode, file);
1080 if (ret)
1081 goto out_first;
1083 if (!bdev->bd_openers) {
1084 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
1085 bdi = blk_get_backing_dev_info(bdev);
1086 if (bdi == NULL)
1087 bdi = &default_backing_dev_info;
1088 bdev->bd_inode->i_data.backing_dev_info = bdi;
1090 if (bdev->bd_invalidated)
1091 rescan_partitions(disk, bdev);
1092 } else {
1093 struct hd_struct *p;
1094 struct block_device *whole;
1095 whole = bdget_disk(disk, 0);
1096 ret = -ENOMEM;
1097 if (!whole)
1098 goto out_first;
1099 BUG_ON(for_part);
1100 ret = __blkdev_get(whole, file->f_mode, file->f_flags, 1);
1101 if (ret)
1102 goto out_first;
1103 bdev->bd_contains = whole;
1104 p = disk->part[part - 1];
1105 bdev->bd_inode->i_data.backing_dev_info =
1106 whole->bd_inode->i_data.backing_dev_info;
1107 if (!(disk->flags & GENHD_FL_UP) || !p || !p->nr_sects) {
1108 ret = -ENXIO;
1109 goto out_first;
1111 kobject_get(&p->kobj);
1112 bdev->bd_part = p;
1113 bd_set_size(bdev, (loff_t) p->nr_sects << 9);
1115 } else {
1116 put_disk(disk);
1117 module_put(owner);
1118 if (bdev->bd_contains == bdev) {
1119 if (bdev->bd_disk->fops->open) {
1120 ret = bdev->bd_disk->fops->open(bdev->bd_inode, file);
1121 if (ret)
1122 goto out;
1124 if (bdev->bd_invalidated)
1125 rescan_partitions(bdev->bd_disk, bdev);
1128 bdev->bd_openers++;
1129 if (for_part)
1130 bdev->bd_part_count++;
1131 mutex_unlock(&bdev->bd_mutex);
1132 unlock_kernel();
1133 return 0;
1135 out_first:
1136 bdev->bd_disk = NULL;
1137 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
1138 if (bdev != bdev->bd_contains)
1139 __blkdev_put(bdev->bd_contains, 1);
1140 bdev->bd_contains = NULL;
1141 put_disk(disk);
1142 module_put(owner);
1143 out:
1144 mutex_unlock(&bdev->bd_mutex);
1145 unlock_kernel();
1146 if (ret)
1147 bdput(bdev);
1148 return ret;
1151 static int __blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags,
1152 int for_part)
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_path.dentry = &fake_dentry;
1165 fake_dentry.d_inode = bdev->bd_inode;
1167 return do_open(bdev, &fake_file, for_part);
1170 int blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags)
1172 return __blkdev_get(bdev, mode, flags, 0);
1174 EXPORT_SYMBOL(blkdev_get);
1176 static int blkdev_open(struct inode * inode, struct file * filp)
1178 struct block_device *bdev;
1179 int res;
1182 * Preserve backwards compatibility and allow large file access
1183 * even if userspace doesn't ask for it explicitly. Some mkfs
1184 * binary needs it. We might want to drop this workaround
1185 * during an unstable branch.
1187 filp->f_flags |= O_LARGEFILE;
1189 bdev = bd_acquire(inode);
1190 if (bdev == NULL)
1191 return -ENOMEM;
1193 res = do_open(bdev, filp, 0);
1194 if (res)
1195 return res;
1197 if (!(filp->f_flags & O_EXCL) )
1198 return 0;
1200 if (!(res = bd_claim(bdev, filp)))
1201 return 0;
1203 blkdev_put(bdev);
1204 return res;
1207 static int __blkdev_put(struct block_device *bdev, int for_part)
1209 int ret = 0;
1210 struct inode *bd_inode = bdev->bd_inode;
1211 struct gendisk *disk = bdev->bd_disk;
1212 struct block_device *victim = NULL;
1214 mutex_lock_nested(&bdev->bd_mutex, for_part);
1215 lock_kernel();
1216 if (for_part)
1217 bdev->bd_part_count--;
1219 if (!--bdev->bd_openers) {
1220 sync_blockdev(bdev);
1221 kill_bdev(bdev);
1223 if (bdev->bd_contains == bdev) {
1224 if (disk->fops->release)
1225 ret = disk->fops->release(bd_inode, NULL);
1227 if (!bdev->bd_openers) {
1228 struct module *owner = disk->fops->owner;
1230 put_disk(disk);
1231 module_put(owner);
1233 if (bdev->bd_contains != bdev) {
1234 kobject_put(&bdev->bd_part->kobj);
1235 bdev->bd_part = NULL;
1237 bdev->bd_disk = NULL;
1238 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
1239 if (bdev != bdev->bd_contains)
1240 victim = bdev->bd_contains;
1241 bdev->bd_contains = NULL;
1243 unlock_kernel();
1244 mutex_unlock(&bdev->bd_mutex);
1245 bdput(bdev);
1246 if (victim)
1247 __blkdev_put(victim, 1);
1248 return ret;
1251 int blkdev_put(struct block_device *bdev)
1253 return __blkdev_put(bdev, 0);
1255 EXPORT_SYMBOL(blkdev_put);
1257 static int blkdev_close(struct inode * inode, struct file * filp)
1259 struct block_device *bdev = I_BDEV(filp->f_mapping->host);
1260 if (bdev->bd_holder == filp)
1261 bd_release(bdev);
1262 return blkdev_put(bdev);
1265 static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1267 return blkdev_ioctl(file->f_mapping->host, file, cmd, arg);
1270 const struct address_space_operations def_blk_aops = {
1271 .readpage = blkdev_readpage,
1272 .writepage = blkdev_writepage,
1273 .sync_page = block_sync_page,
1274 .prepare_write = blkdev_prepare_write,
1275 .commit_write = blkdev_commit_write,
1276 .writepages = generic_writepages,
1277 .direct_IO = blkdev_direct_IO,
1280 const struct file_operations def_blk_fops = {
1281 .open = blkdev_open,
1282 .release = blkdev_close,
1283 .llseek = block_llseek,
1284 .read = do_sync_read,
1285 .write = do_sync_write,
1286 .aio_read = generic_file_aio_read,
1287 .aio_write = generic_file_aio_write_nolock,
1288 .mmap = generic_file_mmap,
1289 .fsync = block_fsync,
1290 .unlocked_ioctl = block_ioctl,
1291 #ifdef CONFIG_COMPAT
1292 .compat_ioctl = compat_blkdev_ioctl,
1293 #endif
1294 .sendfile = generic_file_sendfile,
1295 .splice_read = generic_file_splice_read,
1296 .splice_write = generic_file_splice_write,
1299 int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
1301 int res;
1302 mm_segment_t old_fs = get_fs();
1303 set_fs(KERNEL_DS);
1304 res = blkdev_ioctl(bdev->bd_inode, NULL, cmd, arg);
1305 set_fs(old_fs);
1306 return res;
1309 EXPORT_SYMBOL(ioctl_by_bdev);
1312 * lookup_bdev - lookup a struct block_device by name
1314 * @path: special file representing the block device
1316 * Get a reference to the blockdevice at @path in the current
1317 * namespace if possible and return it. Return ERR_PTR(error)
1318 * otherwise.
1320 struct block_device *lookup_bdev(const char *path)
1322 struct block_device *bdev;
1323 struct inode *inode;
1324 struct nameidata nd;
1325 int error;
1327 if (!path || !*path)
1328 return ERR_PTR(-EINVAL);
1330 error = path_lookup(path, LOOKUP_FOLLOW, &nd);
1331 if (error)
1332 return ERR_PTR(error);
1334 inode = nd.dentry->d_inode;
1335 error = -ENOTBLK;
1336 if (!S_ISBLK(inode->i_mode))
1337 goto fail;
1338 error = -EACCES;
1339 if (nd.mnt->mnt_flags & MNT_NODEV)
1340 goto fail;
1341 error = -ENOMEM;
1342 bdev = bd_acquire(inode);
1343 if (!bdev)
1344 goto fail;
1345 out:
1346 path_release(&nd);
1347 return bdev;
1348 fail:
1349 bdev = ERR_PTR(error);
1350 goto out;
1354 * open_bdev_excl - open a block device by name and set it up for use
1356 * @path: special file representing the block device
1357 * @flags: %MS_RDONLY for opening read-only
1358 * @holder: owner for exclusion
1360 * Open the blockdevice described by the special file at @path, claim it
1361 * for the @holder.
1363 struct block_device *open_bdev_excl(const char *path, int flags, void *holder)
1365 struct block_device *bdev;
1366 mode_t mode = FMODE_READ;
1367 int error = 0;
1369 bdev = lookup_bdev(path);
1370 if (IS_ERR(bdev))
1371 return bdev;
1373 if (!(flags & MS_RDONLY))
1374 mode |= FMODE_WRITE;
1375 error = blkdev_get(bdev, mode, 0);
1376 if (error)
1377 return ERR_PTR(error);
1378 error = -EACCES;
1379 if (!(flags & MS_RDONLY) && bdev_read_only(bdev))
1380 goto blkdev_put;
1381 error = bd_claim(bdev, holder);
1382 if (error)
1383 goto blkdev_put;
1385 return bdev;
1387 blkdev_put:
1388 blkdev_put(bdev);
1389 return ERR_PTR(error);
1392 EXPORT_SYMBOL(open_bdev_excl);
1395 * close_bdev_excl - release a blockdevice openen by open_bdev_excl()
1397 * @bdev: blockdevice to close
1399 * This is the counterpart to open_bdev_excl().
1401 void close_bdev_excl(struct block_device *bdev)
1403 bd_release(bdev);
1404 blkdev_put(bdev);
1407 EXPORT_SYMBOL(close_bdev_excl);
1409 int __invalidate_device(struct block_device *bdev)
1411 struct super_block *sb = get_super(bdev);
1412 int res = 0;
1414 if (sb) {
1416 * no need to lock the super, get_super holds the
1417 * read mutex so the filesystem cannot go away
1418 * under us (->put_super runs with the write lock
1419 * hold).
1421 shrink_dcache_sb(sb);
1422 res = invalidate_inodes(sb);
1423 drop_super(sb);
1425 invalidate_bdev(bdev, 0);
1426 return res;
1428 EXPORT_SYMBOL(__invalidate_device);