Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / fs / block_dev.c
blob07bc3c05d25edc3fc7a18c6fbd326acd029a88ee
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 <<<<<<< HEAD:fs/block_dev.c
35 =======
36 static const struct address_space_operations def_blk_aops;
38 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/block_dev.c
39 static inline struct bdev_inode *BDEV_I(struct inode *inode)
41 return container_of(inode, struct bdev_inode, vfs_inode);
44 inline struct block_device *I_BDEV(struct inode *inode)
46 return &BDEV_I(inode)->bdev;
49 EXPORT_SYMBOL(I_BDEV);
51 static sector_t max_block(struct block_device *bdev)
53 sector_t retval = ~((sector_t)0);
54 loff_t sz = i_size_read(bdev->bd_inode);
56 if (sz) {
57 unsigned int size = block_size(bdev);
58 unsigned int sizebits = blksize_bits(size);
59 retval = (sz >> sizebits);
61 return retval;
64 /* Kill _all_ buffers and pagecache , dirty or not.. */
65 static void kill_bdev(struct block_device *bdev)
67 if (bdev->bd_inode->i_mapping->nrpages == 0)
68 return;
69 invalidate_bh_lrus();
70 truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
73 int set_blocksize(struct block_device *bdev, int size)
75 /* Size must be a power of two, and between 512 and PAGE_SIZE */
76 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
77 return -EINVAL;
79 /* Size cannot be smaller than the size supported by the device */
80 if (size < bdev_hardsect_size(bdev))
81 return -EINVAL;
83 /* Don't change the size if it is same as current */
84 if (bdev->bd_block_size != size) {
85 sync_blockdev(bdev);
86 bdev->bd_block_size = size;
87 bdev->bd_inode->i_blkbits = blksize_bits(size);
88 kill_bdev(bdev);
90 return 0;
93 EXPORT_SYMBOL(set_blocksize);
95 int sb_set_blocksize(struct super_block *sb, int size)
97 if (set_blocksize(sb->s_bdev, size))
98 return 0;
99 /* If we get here, we know size is power of two
100 * and it's value is between 512 and PAGE_SIZE */
101 sb->s_blocksize = size;
102 sb->s_blocksize_bits = blksize_bits(size);
103 return sb->s_blocksize;
106 EXPORT_SYMBOL(sb_set_blocksize);
108 int sb_min_blocksize(struct super_block *sb, int size)
110 int minsize = bdev_hardsect_size(sb->s_bdev);
111 if (size < minsize)
112 size = minsize;
113 return sb_set_blocksize(sb, size);
116 EXPORT_SYMBOL(sb_min_blocksize);
118 static int
119 blkdev_get_block(struct inode *inode, sector_t iblock,
120 struct buffer_head *bh, int create)
122 if (iblock >= max_block(I_BDEV(inode))) {
123 if (create)
124 return -EIO;
127 * for reads, we're just trying to fill a partial page.
128 * return a hole, they will have to call get_block again
129 * before they can fill it, and they will get -EIO at that
130 * time
132 return 0;
134 bh->b_bdev = I_BDEV(inode);
135 bh->b_blocknr = iblock;
136 set_buffer_mapped(bh);
137 return 0;
140 static int
141 blkdev_get_blocks(struct inode *inode, sector_t iblock,
142 struct buffer_head *bh, int create)
144 sector_t end_block = max_block(I_BDEV(inode));
145 unsigned long max_blocks = bh->b_size >> inode->i_blkbits;
147 if ((iblock + max_blocks) > end_block) {
148 max_blocks = end_block - iblock;
149 if ((long)max_blocks <= 0) {
150 if (create)
151 return -EIO; /* write fully beyond EOF */
153 * It is a read which is fully beyond EOF. We return
154 * a !buffer_mapped buffer
156 max_blocks = 0;
160 bh->b_bdev = I_BDEV(inode);
161 bh->b_blocknr = iblock;
162 bh->b_size = max_blocks << inode->i_blkbits;
163 if (max_blocks)
164 set_buffer_mapped(bh);
165 return 0;
168 static ssize_t
169 blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
170 loff_t offset, unsigned long nr_segs)
172 struct file *file = iocb->ki_filp;
173 struct inode *inode = file->f_mapping->host;
175 return blockdev_direct_IO_no_locking(rw, iocb, inode, I_BDEV(inode),
176 iov, offset, nr_segs, blkdev_get_blocks, NULL);
179 <<<<<<< HEAD:fs/block_dev.c
180 #if 0
181 static void blk_end_aio(struct bio *bio, int error)
183 struct kiocb *iocb = bio->bi_private;
184 atomic_t *bio_count = &iocb->ki_bio_count;
186 if (bio_data_dir(bio) == READ)
187 bio_check_pages_dirty(bio);
188 else {
189 bio_release_pages(bio);
190 bio_put(bio);
193 /* iocb->ki_nbytes stores error code from LLDD */
194 if (error)
195 iocb->ki_nbytes = -EIO;
197 if (atomic_dec_and_test(bio_count)) {
198 if ((long)iocb->ki_nbytes < 0)
199 aio_complete(iocb, iocb->ki_nbytes, 0);
200 else
201 aio_complete(iocb, iocb->ki_left, 0);
204 return 0;
207 #define VEC_SIZE 16
208 struct pvec {
209 unsigned short nr;
210 unsigned short idx;
211 struct page *page[VEC_SIZE];
214 #define PAGES_SPANNED(addr, len) \
215 (DIV_ROUND_UP((addr) + (len), PAGE_SIZE) - (addr) / PAGE_SIZE);
218 * get page pointer for user addr, we internally cache struct page array for
219 * (addr, count) range in pvec to avoid frequent call to get_user_pages. If
220 * internal page list is exhausted, a batch count of up to VEC_SIZE is used
221 * to get next set of page struct.
223 static struct page *blk_get_page(unsigned long addr, size_t count, int rw,
224 struct pvec *pvec)
226 int ret, nr_pages;
227 if (pvec->idx == pvec->nr) {
228 nr_pages = PAGES_SPANNED(addr, count);
229 nr_pages = min(nr_pages, VEC_SIZE);
230 down_read(&current->mm->mmap_sem);
231 ret = get_user_pages(current, current->mm, addr, nr_pages,
232 rw == READ, 0, pvec->page, NULL);
233 up_read(&current->mm->mmap_sem);
234 if (ret < 0)
235 return ERR_PTR(ret);
236 pvec->nr = ret;
237 pvec->idx = 0;
239 return pvec->page[pvec->idx++];
242 /* return a page back to pvec array */
243 static void blk_unget_page(struct page *page, struct pvec *pvec)
245 pvec->page[--pvec->idx] = page;
248 static ssize_t
249 blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
250 loff_t pos, unsigned long nr_segs)
252 struct inode *inode = iocb->ki_filp->f_mapping->host;
253 unsigned blkbits = blksize_bits(bdev_hardsect_size(I_BDEV(inode)));
254 unsigned blocksize_mask = (1 << blkbits) - 1;
255 unsigned long seg = 0; /* iov segment iterator */
256 unsigned long nvec; /* number of bio vec needed */
257 unsigned long cur_off; /* offset into current page */
258 unsigned long cur_len; /* I/O len of current page, up to PAGE_SIZE */
260 unsigned long addr; /* user iovec address */
261 size_t count; /* user iovec len */
262 size_t nbytes = iocb->ki_nbytes = iocb->ki_left; /* total xfer size */
263 loff_t size; /* size of block device */
264 struct bio *bio;
265 atomic_t *bio_count = &iocb->ki_bio_count;
266 struct page *page;
267 struct pvec pvec;
269 pvec.nr = 0;
270 pvec.idx = 0;
272 if (pos & blocksize_mask)
273 return -EINVAL;
275 size = i_size_read(inode);
276 if (pos + nbytes > size) {
277 nbytes = size - pos;
278 iocb->ki_left = nbytes;
282 * check first non-zero iov alignment, the remaining
283 * iov alignment is checked inside bio loop below.
285 do {
286 addr = (unsigned long) iov[seg].iov_base;
287 count = min(iov[seg].iov_len, nbytes);
288 if (addr & blocksize_mask || count & blocksize_mask)
289 return -EINVAL;
290 } while (!count && ++seg < nr_segs);
291 atomic_set(bio_count, 1);
293 while (nbytes) {
294 /* roughly estimate number of bio vec needed */
295 nvec = (nbytes + PAGE_SIZE - 1) / PAGE_SIZE;
296 nvec = max(nvec, nr_segs - seg);
297 nvec = min(nvec, (unsigned long) BIO_MAX_PAGES);
299 /* bio_alloc should not fail with GFP_KERNEL flag */
300 bio = bio_alloc(GFP_KERNEL, nvec);
301 bio->bi_bdev = I_BDEV(inode);
302 bio->bi_end_io = blk_end_aio;
303 bio->bi_private = iocb;
304 bio->bi_sector = pos >> blkbits;
305 same_bio:
306 cur_off = addr & ~PAGE_MASK;
307 cur_len = PAGE_SIZE - cur_off;
308 if (count < cur_len)
309 cur_len = count;
311 page = blk_get_page(addr, count, rw, &pvec);
312 if (unlikely(IS_ERR(page)))
313 goto backout;
315 if (bio_add_page(bio, page, cur_len, cur_off)) {
316 pos += cur_len;
317 addr += cur_len;
318 count -= cur_len;
319 nbytes -= cur_len;
321 if (count)
322 goto same_bio;
323 while (++seg < nr_segs) {
324 addr = (unsigned long) iov[seg].iov_base;
325 count = iov[seg].iov_len;
326 if (!count)
327 continue;
328 if (unlikely(addr & blocksize_mask ||
329 count & blocksize_mask)) {
330 page = ERR_PTR(-EINVAL);
331 goto backout;
333 count = min(count, nbytes);
334 goto same_bio;
336 } else {
337 blk_unget_page(page, &pvec);
340 /* bio is ready, submit it */
341 if (rw == READ)
342 bio_set_pages_dirty(bio);
343 atomic_inc(bio_count);
344 submit_bio(rw, bio);
347 completion:
348 iocb->ki_left -= nbytes;
349 nbytes = iocb->ki_left;
350 iocb->ki_pos += nbytes;
352 blk_run_address_space(inode->i_mapping);
353 if (atomic_dec_and_test(bio_count))
354 aio_complete(iocb, nbytes, 0);
356 return -EIOCBQUEUED;
358 backout:
360 * back out nbytes count constructed so far for this bio,
361 * we will throw away current bio.
363 nbytes += bio->bi_size;
364 bio_release_pages(bio);
365 bio_put(bio);
368 * if no bio was submmitted, return the error code.
369 * otherwise, proceed with pending I/O completion.
371 if (atomic_read(bio_count) == 1)
372 return PTR_ERR(page);
373 goto completion;
375 #endif
377 =======
378 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/block_dev.c
379 static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
381 return block_write_full_page(page, blkdev_get_block, wbc);
384 static int blkdev_readpage(struct file * file, struct page * page)
386 return block_read_full_page(page, blkdev_get_block);
389 static int blkdev_write_begin(struct file *file, struct address_space *mapping,
390 loff_t pos, unsigned len, unsigned flags,
391 struct page **pagep, void **fsdata)
393 *pagep = NULL;
394 return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
395 blkdev_get_block);
398 static int blkdev_write_end(struct file *file, struct address_space *mapping,
399 loff_t pos, unsigned len, unsigned copied,
400 struct page *page, void *fsdata)
402 int ret;
403 ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
405 unlock_page(page);
406 page_cache_release(page);
408 return ret;
412 * private llseek:
413 * for a block special file file->f_path.dentry->d_inode->i_size is zero
414 * so we compute the size by hand (just as in block_read/write above)
416 static loff_t block_llseek(struct file *file, loff_t offset, int origin)
418 struct inode *bd_inode = file->f_mapping->host;
419 loff_t size;
420 loff_t retval;
422 mutex_lock(&bd_inode->i_mutex);
423 size = i_size_read(bd_inode);
425 switch (origin) {
426 case 2:
427 offset += size;
428 break;
429 case 1:
430 offset += file->f_pos;
432 retval = -EINVAL;
433 if (offset >= 0 && offset <= size) {
434 if (offset != file->f_pos) {
435 file->f_pos = offset;
437 retval = offset;
439 mutex_unlock(&bd_inode->i_mutex);
440 return retval;
444 * Filp is never NULL; the only case when ->fsync() is called with
445 * NULL first argument is nfsd_sync_dir() and that's not a directory.
448 static int block_fsync(struct file *filp, struct dentry *dentry, int datasync)
450 return sync_blockdev(I_BDEV(filp->f_mapping->host));
454 * pseudo-fs
457 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
458 static struct kmem_cache * bdev_cachep __read_mostly;
460 static struct inode *bdev_alloc_inode(struct super_block *sb)
462 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
463 if (!ei)
464 return NULL;
465 return &ei->vfs_inode;
468 static void bdev_destroy_inode(struct inode *inode)
470 struct bdev_inode *bdi = BDEV_I(inode);
472 bdi->bdev.bd_inode_backing_dev_info = NULL;
473 kmem_cache_free(bdev_cachep, bdi);
476 static void init_once(struct kmem_cache * cachep, void *foo)
478 struct bdev_inode *ei = (struct bdev_inode *) foo;
479 struct block_device *bdev = &ei->bdev;
481 memset(bdev, 0, sizeof(*bdev));
482 mutex_init(&bdev->bd_mutex);
483 sema_init(&bdev->bd_mount_sem, 1);
484 INIT_LIST_HEAD(&bdev->bd_inodes);
485 INIT_LIST_HEAD(&bdev->bd_list);
486 #ifdef CONFIG_SYSFS
487 INIT_LIST_HEAD(&bdev->bd_holder_list);
488 #endif
489 inode_init_once(&ei->vfs_inode);
492 static inline void __bd_forget(struct inode *inode)
494 list_del_init(&inode->i_devices);
495 inode->i_bdev = NULL;
496 inode->i_mapping = &inode->i_data;
499 static void bdev_clear_inode(struct inode *inode)
501 struct block_device *bdev = &BDEV_I(inode)->bdev;
502 struct list_head *p;
503 spin_lock(&bdev_lock);
504 while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
505 __bd_forget(list_entry(p, struct inode, i_devices));
507 list_del_init(&bdev->bd_list);
508 spin_unlock(&bdev_lock);
511 static const struct super_operations bdev_sops = {
512 .statfs = simple_statfs,
513 .alloc_inode = bdev_alloc_inode,
514 .destroy_inode = bdev_destroy_inode,
515 .drop_inode = generic_delete_inode,
516 .clear_inode = bdev_clear_inode,
519 static int bd_get_sb(struct file_system_type *fs_type,
520 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
522 return get_sb_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576, mnt);
525 static struct file_system_type bd_type = {
526 .name = "bdev",
527 .get_sb = bd_get_sb,
528 .kill_sb = kill_anon_super,
531 static struct vfsmount *bd_mnt __read_mostly;
532 struct super_block *blockdev_superblock;
534 void __init bdev_cache_init(void)
536 int err;
537 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
538 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
539 SLAB_MEM_SPREAD|SLAB_PANIC),
540 init_once);
541 err = register_filesystem(&bd_type);
542 if (err)
543 panic("Cannot register bdev pseudo-fs");
544 bd_mnt = kern_mount(&bd_type);
545 if (IS_ERR(bd_mnt))
546 panic("Cannot create bdev pseudo-fs");
547 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
551 * Most likely _very_ bad one - but then it's hardly critical for small
552 * /dev and can be fixed when somebody will need really large one.
553 * Keep in mind that it will be fed through icache hash function too.
555 static inline unsigned long hash(dev_t dev)
557 return MAJOR(dev)+MINOR(dev);
560 static int bdev_test(struct inode *inode, void *data)
562 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
565 static int bdev_set(struct inode *inode, void *data)
567 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
568 return 0;
571 static LIST_HEAD(all_bdevs);
573 struct block_device *bdget(dev_t dev)
575 struct block_device *bdev;
576 struct inode *inode;
578 inode = iget5_locked(bd_mnt->mnt_sb, hash(dev),
579 bdev_test, bdev_set, &dev);
581 if (!inode)
582 return NULL;
584 bdev = &BDEV_I(inode)->bdev;
586 if (inode->i_state & I_NEW) {
587 bdev->bd_contains = NULL;
588 bdev->bd_inode = inode;
589 bdev->bd_block_size = (1 << inode->i_blkbits);
590 bdev->bd_part_count = 0;
591 bdev->bd_invalidated = 0;
592 inode->i_mode = S_IFBLK;
593 inode->i_rdev = dev;
594 inode->i_bdev = bdev;
595 inode->i_data.a_ops = &def_blk_aops;
596 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
597 inode->i_data.backing_dev_info = &default_backing_dev_info;
598 spin_lock(&bdev_lock);
599 list_add(&bdev->bd_list, &all_bdevs);
600 spin_unlock(&bdev_lock);
601 unlock_new_inode(inode);
603 return bdev;
606 EXPORT_SYMBOL(bdget);
608 long nr_blockdev_pages(void)
610 struct block_device *bdev;
611 long ret = 0;
612 spin_lock(&bdev_lock);
613 list_for_each_entry(bdev, &all_bdevs, bd_list) {
614 ret += bdev->bd_inode->i_mapping->nrpages;
616 spin_unlock(&bdev_lock);
617 return ret;
620 void bdput(struct block_device *bdev)
622 iput(bdev->bd_inode);
625 EXPORT_SYMBOL(bdput);
627 static struct block_device *bd_acquire(struct inode *inode)
629 struct block_device *bdev;
631 spin_lock(&bdev_lock);
632 bdev = inode->i_bdev;
633 if (bdev) {
634 atomic_inc(&bdev->bd_inode->i_count);
635 spin_unlock(&bdev_lock);
636 return bdev;
638 spin_unlock(&bdev_lock);
640 bdev = bdget(inode->i_rdev);
641 if (bdev) {
642 spin_lock(&bdev_lock);
643 if (!inode->i_bdev) {
645 * We take an additional bd_inode->i_count for inode,
646 * and it's released in clear_inode() of inode.
647 * So, we can access it via ->i_mapping always
648 * without igrab().
650 atomic_inc(&bdev->bd_inode->i_count);
651 inode->i_bdev = bdev;
652 inode->i_mapping = bdev->bd_inode->i_mapping;
653 list_add(&inode->i_devices, &bdev->bd_inodes);
655 spin_unlock(&bdev_lock);
657 return bdev;
660 /* Call when you free inode */
662 void bd_forget(struct inode *inode)
664 struct block_device *bdev = NULL;
666 spin_lock(&bdev_lock);
667 if (inode->i_bdev) {
668 if (inode->i_sb != blockdev_superblock)
669 bdev = inode->i_bdev;
670 __bd_forget(inode);
672 spin_unlock(&bdev_lock);
674 if (bdev)
675 iput(bdev->bd_inode);
678 int bd_claim(struct block_device *bdev, void *holder)
680 int res;
681 spin_lock(&bdev_lock);
683 /* first decide result */
684 if (bdev->bd_holder == holder)
685 res = 0; /* already a holder */
686 else if (bdev->bd_holder != NULL)
687 res = -EBUSY; /* held by someone else */
688 else if (bdev->bd_contains == bdev)
689 res = 0; /* is a whole device which isn't held */
691 else if (bdev->bd_contains->bd_holder == bd_claim)
692 res = 0; /* is a partition of a device that is being partitioned */
693 else if (bdev->bd_contains->bd_holder != NULL)
694 res = -EBUSY; /* is a partition of a held device */
695 else
696 res = 0; /* is a partition of an un-held device */
698 /* now impose change */
699 if (res==0) {
700 /* note that for a whole device bd_holders
701 * will be incremented twice, and bd_holder will
702 * be set to bd_claim before being set to holder
704 bdev->bd_contains->bd_holders ++;
705 bdev->bd_contains->bd_holder = bd_claim;
706 bdev->bd_holders++;
707 bdev->bd_holder = holder;
709 spin_unlock(&bdev_lock);
710 return res;
713 EXPORT_SYMBOL(bd_claim);
715 void bd_release(struct block_device *bdev)
717 spin_lock(&bdev_lock);
718 if (!--bdev->bd_contains->bd_holders)
719 bdev->bd_contains->bd_holder = NULL;
720 if (!--bdev->bd_holders)
721 bdev->bd_holder = NULL;
722 spin_unlock(&bdev_lock);
725 EXPORT_SYMBOL(bd_release);
727 #ifdef CONFIG_SYSFS
729 * Functions for bd_claim_by_kobject / bd_release_from_kobject
731 * If a kobject is passed to bd_claim_by_kobject()
732 * and the kobject has a parent directory,
733 * following symlinks are created:
734 * o from the kobject to the claimed bdev
735 * o from "holders" directory of the bdev to the parent of the kobject
736 * bd_release_from_kobject() removes these symlinks.
738 * Example:
739 * If /dev/dm-0 maps to /dev/sda, kobject corresponding to
740 * /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then:
741 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
742 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
745 static struct kobject *bdev_get_kobj(struct block_device *bdev)
747 if (bdev->bd_contains != bdev)
748 return kobject_get(&bdev->bd_part->dev.kobj);
749 else
750 return kobject_get(&bdev->bd_disk->dev.kobj);
753 static struct kobject *bdev_get_holder(struct block_device *bdev)
755 if (bdev->bd_contains != bdev)
756 return kobject_get(bdev->bd_part->holder_dir);
757 else
758 return kobject_get(bdev->bd_disk->holder_dir);
761 static int add_symlink(struct kobject *from, struct kobject *to)
763 if (!from || !to)
764 return 0;
765 return sysfs_create_link(from, to, kobject_name(to));
768 static void del_symlink(struct kobject *from, struct kobject *to)
770 if (!from || !to)
771 return;
772 sysfs_remove_link(from, kobject_name(to));
776 * 'struct bd_holder' contains pointers to kobjects symlinked by
777 * bd_claim_by_kobject.
778 * It's connected to bd_holder_list which is protected by bdev->bd_sem.
780 struct bd_holder {
781 struct list_head list; /* chain of holders of the bdev */
782 int count; /* references from the holder */
783 struct kobject *sdir; /* holder object, e.g. "/block/dm-0/slaves" */
784 struct kobject *hdev; /* e.g. "/block/dm-0" */
785 struct kobject *hdir; /* e.g. "/block/sda/holders" */
786 struct kobject *sdev; /* e.g. "/block/sda" */
790 * Get references of related kobjects at once.
791 * Returns 1 on success. 0 on failure.
793 * Should call bd_holder_release_dirs() after successful use.
795 static int bd_holder_grab_dirs(struct block_device *bdev,
796 struct bd_holder *bo)
798 if (!bdev || !bo)
799 return 0;
801 bo->sdir = kobject_get(bo->sdir);
802 if (!bo->sdir)
803 return 0;
805 bo->hdev = kobject_get(bo->sdir->parent);
806 if (!bo->hdev)
807 goto fail_put_sdir;
809 bo->sdev = bdev_get_kobj(bdev);
810 if (!bo->sdev)
811 goto fail_put_hdev;
813 bo->hdir = bdev_get_holder(bdev);
814 if (!bo->hdir)
815 goto fail_put_sdev;
817 return 1;
819 fail_put_sdev:
820 kobject_put(bo->sdev);
821 fail_put_hdev:
822 kobject_put(bo->hdev);
823 fail_put_sdir:
824 kobject_put(bo->sdir);
826 return 0;
829 /* Put references of related kobjects at once. */
830 static void bd_holder_release_dirs(struct bd_holder *bo)
832 kobject_put(bo->hdir);
833 kobject_put(bo->sdev);
834 kobject_put(bo->hdev);
835 kobject_put(bo->sdir);
838 static struct bd_holder *alloc_bd_holder(struct kobject *kobj)
840 struct bd_holder *bo;
842 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
843 if (!bo)
844 return NULL;
846 bo->count = 1;
847 bo->sdir = kobj;
849 return bo;
852 static void free_bd_holder(struct bd_holder *bo)
854 kfree(bo);
858 * find_bd_holder - find matching struct bd_holder from the block device
860 * @bdev: struct block device to be searched
861 * @bo: target struct bd_holder
863 * Returns matching entry with @bo in @bdev->bd_holder_list.
864 * If found, increment the reference count and return the pointer.
865 * If not found, returns NULL.
867 static struct bd_holder *find_bd_holder(struct block_device *bdev,
868 struct bd_holder *bo)
870 struct bd_holder *tmp;
872 list_for_each_entry(tmp, &bdev->bd_holder_list, list)
873 if (tmp->sdir == bo->sdir) {
874 tmp->count++;
875 return tmp;
878 return NULL;
882 * add_bd_holder - create sysfs symlinks for bd_claim() relationship
884 * @bdev: block device to be bd_claimed
885 * @bo: preallocated and initialized by alloc_bd_holder()
887 * Add @bo to @bdev->bd_holder_list, create symlinks.
889 * Returns 0 if symlinks are created.
890 * Returns -ve if something fails.
892 static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
894 int err;
896 if (!bo)
897 return -EINVAL;
899 if (!bd_holder_grab_dirs(bdev, bo))
900 return -EBUSY;
902 err = add_symlink(bo->sdir, bo->sdev);
903 if (err)
904 return err;
906 err = add_symlink(bo->hdir, bo->hdev);
907 if (err) {
908 del_symlink(bo->sdir, bo->sdev);
909 return err;
912 list_add_tail(&bo->list, &bdev->bd_holder_list);
913 return 0;
917 * del_bd_holder - delete sysfs symlinks for bd_claim() relationship
919 * @bdev: block device to be bd_claimed
920 * @kobj: holder's kobject
922 * If there is matching entry with @kobj in @bdev->bd_holder_list
923 * and no other bd_claim() from the same kobject,
924 * remove the struct bd_holder from the list, delete symlinks for it.
926 * Returns a pointer to the struct bd_holder when it's removed from the list
927 * and ready to be freed.
928 * Returns NULL if matching claim isn't found or there is other bd_claim()
929 * by the same kobject.
931 static struct bd_holder *del_bd_holder(struct block_device *bdev,
932 struct kobject *kobj)
934 struct bd_holder *bo;
936 list_for_each_entry(bo, &bdev->bd_holder_list, list) {
937 if (bo->sdir == kobj) {
938 bo->count--;
939 BUG_ON(bo->count < 0);
940 if (!bo->count) {
941 list_del(&bo->list);
942 del_symlink(bo->sdir, bo->sdev);
943 del_symlink(bo->hdir, bo->hdev);
944 bd_holder_release_dirs(bo);
945 return bo;
947 break;
951 return NULL;
955 * bd_claim_by_kobject - bd_claim() with additional kobject signature
957 * @bdev: block device to be claimed
958 * @holder: holder's signature
959 * @kobj: holder's kobject
961 * Do bd_claim() and if it succeeds, create sysfs symlinks between
962 * the bdev and the holder's kobject.
963 * Use bd_release_from_kobject() when relesing the claimed bdev.
965 * Returns 0 on success. (same as bd_claim())
966 * Returns errno on failure.
968 static int bd_claim_by_kobject(struct block_device *bdev, void *holder,
969 struct kobject *kobj)
971 int err;
972 struct bd_holder *bo, *found;
974 if (!kobj)
975 return -EINVAL;
977 bo = alloc_bd_holder(kobj);
978 if (!bo)
979 return -ENOMEM;
981 mutex_lock(&bdev->bd_mutex);
983 err = bd_claim(bdev, holder);
984 if (err)
985 goto fail;
987 found = find_bd_holder(bdev, bo);
988 if (found)
989 goto fail;
991 err = add_bd_holder(bdev, bo);
992 if (err)
993 bd_release(bdev);
994 else
995 bo = NULL;
996 fail:
997 mutex_unlock(&bdev->bd_mutex);
998 free_bd_holder(bo);
999 return err;
1003 * bd_release_from_kobject - bd_release() with additional kobject signature
1005 * @bdev: block device to be released
1006 * @kobj: holder's kobject
1008 * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject().
1010 static void bd_release_from_kobject(struct block_device *bdev,
1011 struct kobject *kobj)
1013 if (!kobj)
1014 return;
1016 mutex_lock(&bdev->bd_mutex);
1017 bd_release(bdev);
1018 free_bd_holder(del_bd_holder(bdev, kobj));
1019 mutex_unlock(&bdev->bd_mutex);
1023 * bd_claim_by_disk - wrapper function for bd_claim_by_kobject()
1025 * @bdev: block device to be claimed
1026 * @holder: holder's signature
1027 * @disk: holder's gendisk
1029 * Call bd_claim_by_kobject() with getting @disk->slave_dir.
1031 int bd_claim_by_disk(struct block_device *bdev, void *holder,
1032 struct gendisk *disk)
1034 return bd_claim_by_kobject(bdev, holder, kobject_get(disk->slave_dir));
1036 EXPORT_SYMBOL_GPL(bd_claim_by_disk);
1039 * bd_release_from_disk - wrapper function for bd_release_from_kobject()
1041 * @bdev: block device to be claimed
1042 * @disk: holder's gendisk
1044 * Call bd_release_from_kobject() and put @disk->slave_dir.
1046 void bd_release_from_disk(struct block_device *bdev, struct gendisk *disk)
1048 bd_release_from_kobject(bdev, disk->slave_dir);
1049 kobject_put(disk->slave_dir);
1051 EXPORT_SYMBOL_GPL(bd_release_from_disk);
1052 #endif
1055 * Tries to open block device by device number. Use it ONLY if you
1056 * really do not have anything better - i.e. when you are behind a
1057 * truly sucky interface and all you are given is a device number. _Never_
1058 * to be used for internal purposes. If you ever need it - reconsider
1059 * your API.
1061 struct block_device *open_by_devnum(dev_t dev, unsigned mode)
1063 struct block_device *bdev = bdget(dev);
1064 int err = -ENOMEM;
1065 int flags = mode & FMODE_WRITE ? O_RDWR : O_RDONLY;
1066 if (bdev)
1067 err = blkdev_get(bdev, mode, flags);
1068 return err ? ERR_PTR(err) : bdev;
1071 EXPORT_SYMBOL(open_by_devnum);
1074 * This routine checks whether a removable media has been changed,
1075 * and invalidates all buffer-cache-entries in that case. This
1076 * is a relatively slow routine, so we have to try to minimize using
1077 * it. Thus it is called only upon a 'mount' or 'open'. This
1078 * is the best way of combining speed and utility, I think.
1079 * People changing diskettes in the middle of an operation deserve
1080 * to lose :-)
1082 int check_disk_change(struct block_device *bdev)
1084 struct gendisk *disk = bdev->bd_disk;
1085 struct block_device_operations * bdops = disk->fops;
1087 if (!bdops->media_changed)
1088 return 0;
1089 if (!bdops->media_changed(bdev->bd_disk))
1090 return 0;
1092 if (__invalidate_device(bdev))
1093 printk("VFS: busy inodes on changed media.\n");
1095 if (bdops->revalidate_disk)
1096 bdops->revalidate_disk(bdev->bd_disk);
1097 if (bdev->bd_disk->minors > 1)
1098 bdev->bd_invalidated = 1;
1099 return 1;
1102 EXPORT_SYMBOL(check_disk_change);
1104 void bd_set_size(struct block_device *bdev, loff_t size)
1106 unsigned bsize = bdev_hardsect_size(bdev);
1108 bdev->bd_inode->i_size = size;
1109 while (bsize < PAGE_CACHE_SIZE) {
1110 if (size & bsize)
1111 break;
1112 bsize <<= 1;
1114 bdev->bd_block_size = bsize;
1115 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
1117 EXPORT_SYMBOL(bd_set_size);
1119 static int __blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags,
1120 int for_part);
1121 static int __blkdev_put(struct block_device *bdev, int for_part);
1124 * bd_mutex locking:
1126 * mutex_lock(part->bd_mutex)
1127 * mutex_lock_nested(whole->bd_mutex, 1)
1130 static int do_open(struct block_device *bdev, struct file *file, int for_part)
1132 struct module *owner = NULL;
1133 struct gendisk *disk;
1134 int ret = -ENXIO;
1135 int part;
1137 file->f_mapping = bdev->bd_inode->i_mapping;
1138 lock_kernel();
1139 disk = get_gendisk(bdev->bd_dev, &part);
1140 if (!disk) {
1141 unlock_kernel();
1142 bdput(bdev);
1143 return ret;
1145 owner = disk->fops->owner;
1147 mutex_lock_nested(&bdev->bd_mutex, for_part);
1148 if (!bdev->bd_openers) {
1149 bdev->bd_disk = disk;
1150 bdev->bd_contains = bdev;
1151 if (!part) {
1152 struct backing_dev_info *bdi;
1153 if (disk->fops->open) {
1154 ret = disk->fops->open(bdev->bd_inode, file);
1155 if (ret)
1156 goto out_first;
1158 if (!bdev->bd_openers) {
1159 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
1160 bdi = blk_get_backing_dev_info(bdev);
1161 if (bdi == NULL)
1162 bdi = &default_backing_dev_info;
1163 bdev->bd_inode->i_data.backing_dev_info = bdi;
1165 if (bdev->bd_invalidated)
1166 rescan_partitions(disk, bdev);
1167 } else {
1168 struct hd_struct *p;
1169 struct block_device *whole;
1170 whole = bdget_disk(disk, 0);
1171 ret = -ENOMEM;
1172 if (!whole)
1173 goto out_first;
1174 BUG_ON(for_part);
1175 ret = __blkdev_get(whole, file->f_mode, file->f_flags, 1);
1176 if (ret)
1177 goto out_first;
1178 bdev->bd_contains = whole;
1179 p = disk->part[part - 1];
1180 bdev->bd_inode->i_data.backing_dev_info =
1181 whole->bd_inode->i_data.backing_dev_info;
1182 if (!(disk->flags & GENHD_FL_UP) || !p || !p->nr_sects) {
1183 ret = -ENXIO;
1184 goto out_first;
1186 kobject_get(&p->dev.kobj);
1187 bdev->bd_part = p;
1188 bd_set_size(bdev, (loff_t) p->nr_sects << 9);
1190 } else {
1191 put_disk(disk);
1192 module_put(owner);
1193 if (bdev->bd_contains == bdev) {
1194 if (bdev->bd_disk->fops->open) {
1195 ret = bdev->bd_disk->fops->open(bdev->bd_inode, file);
1196 if (ret)
1197 goto out;
1199 if (bdev->bd_invalidated)
1200 rescan_partitions(bdev->bd_disk, bdev);
1203 bdev->bd_openers++;
1204 if (for_part)
1205 bdev->bd_part_count++;
1206 mutex_unlock(&bdev->bd_mutex);
1207 unlock_kernel();
1208 return 0;
1210 out_first:
1211 bdev->bd_disk = NULL;
1212 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
1213 if (bdev != bdev->bd_contains)
1214 __blkdev_put(bdev->bd_contains, 1);
1215 bdev->bd_contains = NULL;
1216 put_disk(disk);
1217 module_put(owner);
1218 out:
1219 mutex_unlock(&bdev->bd_mutex);
1220 unlock_kernel();
1221 if (ret)
1222 bdput(bdev);
1223 return ret;
1226 static int __blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags,
1227 int for_part)
1230 * This crockload is due to bad choice of ->open() type.
1231 * It will go away.
1232 * For now, block device ->open() routine must _not_
1233 * examine anything in 'inode' argument except ->i_rdev.
1235 struct file fake_file = {};
1236 struct dentry fake_dentry = {};
1237 fake_file.f_mode = mode;
1238 fake_file.f_flags = flags;
1239 fake_file.f_path.dentry = &fake_dentry;
1240 fake_dentry.d_inode = bdev->bd_inode;
1242 return do_open(bdev, &fake_file, for_part);
1245 int blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags)
1247 return __blkdev_get(bdev, mode, flags, 0);
1249 EXPORT_SYMBOL(blkdev_get);
1251 static int blkdev_open(struct inode * inode, struct file * filp)
1253 struct block_device *bdev;
1254 int res;
1257 * Preserve backwards compatibility and allow large file access
1258 * even if userspace doesn't ask for it explicitly. Some mkfs
1259 * binary needs it. We might want to drop this workaround
1260 * during an unstable branch.
1262 filp->f_flags |= O_LARGEFILE;
1264 bdev = bd_acquire(inode);
1265 if (bdev == NULL)
1266 return -ENOMEM;
1268 res = do_open(bdev, filp, 0);
1269 if (res)
1270 return res;
1272 if (!(filp->f_flags & O_EXCL) )
1273 return 0;
1275 if (!(res = bd_claim(bdev, filp)))
1276 return 0;
1278 blkdev_put(bdev);
1279 return res;
1282 static int __blkdev_put(struct block_device *bdev, int for_part)
1284 int ret = 0;
1285 struct inode *bd_inode = bdev->bd_inode;
1286 struct gendisk *disk = bdev->bd_disk;
1287 struct block_device *victim = NULL;
1289 mutex_lock_nested(&bdev->bd_mutex, for_part);
1290 lock_kernel();
1291 if (for_part)
1292 bdev->bd_part_count--;
1294 if (!--bdev->bd_openers) {
1295 sync_blockdev(bdev);
1296 kill_bdev(bdev);
1298 if (bdev->bd_contains == bdev) {
1299 if (disk->fops->release)
1300 ret = disk->fops->release(bd_inode, NULL);
1302 if (!bdev->bd_openers) {
1303 struct module *owner = disk->fops->owner;
1305 put_disk(disk);
1306 module_put(owner);
1308 if (bdev->bd_contains != bdev) {
1309 kobject_put(&bdev->bd_part->dev.kobj);
1310 bdev->bd_part = NULL;
1312 bdev->bd_disk = NULL;
1313 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
1314 if (bdev != bdev->bd_contains)
1315 victim = bdev->bd_contains;
1316 bdev->bd_contains = NULL;
1318 unlock_kernel();
1319 mutex_unlock(&bdev->bd_mutex);
1320 bdput(bdev);
1321 if (victim)
1322 __blkdev_put(victim, 1);
1323 return ret;
1326 int blkdev_put(struct block_device *bdev)
1328 return __blkdev_put(bdev, 0);
1330 EXPORT_SYMBOL(blkdev_put);
1332 static int blkdev_close(struct inode * inode, struct file * filp)
1334 struct block_device *bdev = I_BDEV(filp->f_mapping->host);
1335 if (bdev->bd_holder == filp)
1336 bd_release(bdev);
1337 return blkdev_put(bdev);
1340 static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1342 return blkdev_ioctl(file->f_mapping->host, file, cmd, arg);
1345 <<<<<<< HEAD:fs/block_dev.c
1346 const struct address_space_operations def_blk_aops = {
1347 =======
1348 static const struct address_space_operations def_blk_aops = {
1349 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/block_dev.c
1350 .readpage = blkdev_readpage,
1351 .writepage = blkdev_writepage,
1352 .sync_page = block_sync_page,
1353 .write_begin = blkdev_write_begin,
1354 .write_end = blkdev_write_end,
1355 .writepages = generic_writepages,
1356 .direct_IO = blkdev_direct_IO,
1359 const struct file_operations def_blk_fops = {
1360 .open = blkdev_open,
1361 .release = blkdev_close,
1362 .llseek = block_llseek,
1363 .read = do_sync_read,
1364 .write = do_sync_write,
1365 .aio_read = generic_file_aio_read,
1366 .aio_write = generic_file_aio_write_nolock,
1367 .mmap = generic_file_mmap,
1368 .fsync = block_fsync,
1369 .unlocked_ioctl = block_ioctl,
1370 #ifdef CONFIG_COMPAT
1371 .compat_ioctl = compat_blkdev_ioctl,
1372 #endif
1373 .splice_read = generic_file_splice_read,
1374 .splice_write = generic_file_splice_write,
1377 int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
1379 int res;
1380 mm_segment_t old_fs = get_fs();
1381 set_fs(KERNEL_DS);
1382 res = blkdev_ioctl(bdev->bd_inode, NULL, cmd, arg);
1383 set_fs(old_fs);
1384 return res;
1387 EXPORT_SYMBOL(ioctl_by_bdev);
1390 * lookup_bdev - lookup a struct block_device by name
1392 * @path: special file representing the block device
1394 * Get a reference to the blockdevice at @path in the current
1395 * namespace if possible and return it. Return ERR_PTR(error)
1396 * otherwise.
1398 struct block_device *lookup_bdev(const char *path)
1400 struct block_device *bdev;
1401 struct inode *inode;
1402 struct nameidata nd;
1403 int error;
1405 if (!path || !*path)
1406 return ERR_PTR(-EINVAL);
1408 error = path_lookup(path, LOOKUP_FOLLOW, &nd);
1409 if (error)
1410 return ERR_PTR(error);
1412 inode = nd.path.dentry->d_inode;
1413 error = -ENOTBLK;
1414 if (!S_ISBLK(inode->i_mode))
1415 goto fail;
1416 error = -EACCES;
1417 if (nd.path.mnt->mnt_flags & MNT_NODEV)
1418 goto fail;
1419 error = -ENOMEM;
1420 bdev = bd_acquire(inode);
1421 if (!bdev)
1422 goto fail;
1423 out:
1424 path_put(&nd.path);
1425 return bdev;
1426 fail:
1427 bdev = ERR_PTR(error);
1428 goto out;
1432 * open_bdev_excl - open a block device by name and set it up for use
1434 * @path: special file representing the block device
1435 * @flags: %MS_RDONLY for opening read-only
1436 * @holder: owner for exclusion
1438 * Open the blockdevice described by the special file at @path, claim it
1439 * for the @holder.
1441 struct block_device *open_bdev_excl(const char *path, int flags, void *holder)
1443 struct block_device *bdev;
1444 mode_t mode = FMODE_READ;
1445 int error = 0;
1447 bdev = lookup_bdev(path);
1448 if (IS_ERR(bdev))
1449 return bdev;
1451 if (!(flags & MS_RDONLY))
1452 mode |= FMODE_WRITE;
1453 error = blkdev_get(bdev, mode, 0);
1454 if (error)
1455 return ERR_PTR(error);
1456 error = -EACCES;
1457 if (!(flags & MS_RDONLY) && bdev_read_only(bdev))
1458 goto blkdev_put;
1459 error = bd_claim(bdev, holder);
1460 if (error)
1461 goto blkdev_put;
1463 return bdev;
1465 blkdev_put:
1466 blkdev_put(bdev);
1467 return ERR_PTR(error);
1470 EXPORT_SYMBOL(open_bdev_excl);
1473 * close_bdev_excl - release a blockdevice openen by open_bdev_excl()
1475 * @bdev: blockdevice to close
1477 * This is the counterpart to open_bdev_excl().
1479 void close_bdev_excl(struct block_device *bdev)
1481 bd_release(bdev);
1482 blkdev_put(bdev);
1485 EXPORT_SYMBOL(close_bdev_excl);
1487 int __invalidate_device(struct block_device *bdev)
1489 struct super_block *sb = get_super(bdev);
1490 int res = 0;
1492 if (sb) {
1494 * no need to lock the super, get_super holds the
1495 * read mutex so the filesystem cannot go away
1496 * under us (->put_super runs with the write lock
1497 * hold).
1499 shrink_dcache_sb(sb);
1500 res = invalidate_inodes(sb);
1501 drop_super(sb);
1503 invalidate_bdev(bdev);
1504 return res;
1506 EXPORT_SYMBOL(__invalidate_device);