Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / fs / block_dev.c
1 /*
2  *  linux/fs/block_dev.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  *  Copyright (C) 2001  Andrea Arcangeli <andrea@suse.de> SuSE
6  */
7
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/mpage.h>
21 #include <linux/mount.h>
22 #include <linux/uio.h>
23 #include <linux/namei.h>
24 #include <asm/uaccess.h>
25
26 struct bdev_inode {
27         struct block_device bdev;
28         struct inode vfs_inode;
29 };
30
31 static inline struct bdev_inode *BDEV_I(struct inode *inode)
32 {
33         return container_of(inode, struct bdev_inode, vfs_inode);
34 }
35
36 inline struct block_device *I_BDEV(struct inode *inode)
37 {
38         return &BDEV_I(inode)->bdev;
39 }
40
41 EXPORT_SYMBOL(I_BDEV);
42
43 static sector_t max_block(struct block_device *bdev)
44 {
45         sector_t retval = ~((sector_t)0);
46         loff_t sz = i_size_read(bdev->bd_inode);
47
48         if (sz) {
49                 unsigned int size = block_size(bdev);
50                 unsigned int sizebits = blksize_bits(size);
51                 retval = (sz >> sizebits);
52         }
53         return retval;
54 }
55
56 /* Kill _all_ buffers, dirty or not.. */
57 static void kill_bdev(struct block_device *bdev)
58 {
59         invalidate_bdev(bdev, 1);
60         truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
61 }       
62
63 int set_blocksize(struct block_device *bdev, int size)
64 {
65         /* Size must be a power of two, and between 512 and PAGE_SIZE */
66         if (size > PAGE_SIZE || size < 512 || (size & (size-1)))
67                 return -EINVAL;
68
69         /* Size cannot be smaller than the size supported by the device */
70         if (size < bdev_hardsect_size(bdev))
71                 return -EINVAL;
72
73         /* Don't change the size if it is same as current */
74         if (bdev->bd_block_size != size) {
75                 sync_blockdev(bdev);
76                 bdev->bd_block_size = size;
77                 bdev->bd_inode->i_blkbits = blksize_bits(size);
78                 kill_bdev(bdev);
79         }
80         return 0;
81 }
82
83 EXPORT_SYMBOL(set_blocksize);
84
85 int sb_set_blocksize(struct super_block *sb, int size)
86 {
87         if (set_blocksize(sb->s_bdev, size))
88                 return 0;
89         /* If we get here, we know size is power of two
90          * and it's value is between 512 and PAGE_SIZE */
91         sb->s_blocksize = size;
92         sb->s_blocksize_bits = blksize_bits(size);
93         return sb->s_blocksize;
94 }
95
96 EXPORT_SYMBOL(sb_set_blocksize);
97
98 int sb_min_blocksize(struct super_block *sb, int size)
99 {
100         int minsize = bdev_hardsect_size(sb->s_bdev);
101         if (size < minsize)
102                 size = minsize;
103         return sb_set_blocksize(sb, size);
104 }
105
106 EXPORT_SYMBOL(sb_min_blocksize);
107
108 static int
109 blkdev_get_block(struct inode *inode, sector_t iblock,
110                 struct buffer_head *bh, int create)
111 {
112         if (iblock >= max_block(I_BDEV(inode))) {
113                 if (create)
114                         return -EIO;
115
116                 /*
117                  * for reads, we're just trying to fill a partial page.
118                  * return a hole, they will have to call get_block again
119                  * before they can fill it, and they will get -EIO at that
120                  * time
121                  */
122                 return 0;
123         }
124         bh->b_bdev = I_BDEV(inode);
125         bh->b_blocknr = iblock;
126         set_buffer_mapped(bh);
127         return 0;
128 }
129
130 static int
131 blkdev_get_blocks(struct inode *inode, sector_t iblock,
132                 struct buffer_head *bh, int create)
133 {
134         sector_t end_block = max_block(I_BDEV(inode));
135         unsigned long max_blocks = bh->b_size >> inode->i_blkbits;
136
137         if ((iblock + max_blocks) > end_block) {
138                 max_blocks = end_block - iblock;
139                 if ((long)max_blocks <= 0) {
140                         if (create)
141                                 return -EIO;    /* write fully beyond EOF */
142                         /*
143                          * It is a read which is fully beyond EOF.  We return
144                          * a !buffer_mapped buffer
145                          */
146                         max_blocks = 0;
147                 }
148         }
149
150         bh->b_bdev = I_BDEV(inode);
151         bh->b_blocknr = iblock;
152         bh->b_size = max_blocks << inode->i_blkbits;
153         if (max_blocks)
154                 set_buffer_mapped(bh);
155         return 0;
156 }
157
158 static ssize_t
159 blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
160                         loff_t offset, unsigned long nr_segs)
161 {
162         struct file *file = iocb->ki_filp;
163         struct inode *inode = file->f_mapping->host;
164
165         return blockdev_direct_IO_no_locking(rw, iocb, inode, I_BDEV(inode),
166                                 iov, offset, nr_segs, blkdev_get_blocks, NULL);
167 }
168
169 static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
170 {
171         return block_write_full_page(page, blkdev_get_block, wbc);
172 }
173
174 static int blkdev_readpage(struct file * file, struct page * page)
175 {
176         return block_read_full_page(page, blkdev_get_block);
177 }
178
179 static int blkdev_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
180 {
181         return block_prepare_write(page, from, to, blkdev_get_block);
182 }
183
184 static int blkdev_commit_write(struct file *file, struct page *page, unsigned from, unsigned to)
185 {
186         return block_commit_write(page, from, to);
187 }
188
189 /*
190  * private llseek:
191  * for a block special file file->f_dentry->d_inode->i_size is zero
192  * so we compute the size by hand (just as in block_read/write above)
193  */
194 static loff_t block_llseek(struct file *file, loff_t offset, int origin)
195 {
196         struct inode *bd_inode = file->f_mapping->host;
197         loff_t size;
198         loff_t retval;
199
200         mutex_lock(&bd_inode->i_mutex);
201         size = i_size_read(bd_inode);
202
203         switch (origin) {
204                 case 2:
205                         offset += size;
206                         break;
207                 case 1:
208                         offset += file->f_pos;
209         }
210         retval = -EINVAL;
211         if (offset >= 0 && offset <= size) {
212                 if (offset != file->f_pos) {
213                         file->f_pos = offset;
214                 }
215                 retval = offset;
216         }
217         mutex_unlock(&bd_inode->i_mutex);
218         return retval;
219 }
220         
221 /*
222  *      Filp is never NULL; the only case when ->fsync() is called with
223  *      NULL first argument is nfsd_sync_dir() and that's not a directory.
224  */
225  
226 static int block_fsync(struct file *filp, struct dentry *dentry, int datasync)
227 {
228         return sync_blockdev(I_BDEV(filp->f_mapping->host));
229 }
230
231 /*
232  * pseudo-fs
233  */
234
235 static  __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
236 static kmem_cache_t * bdev_cachep __read_mostly;
237
238 static struct inode *bdev_alloc_inode(struct super_block *sb)
239 {
240         struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, SLAB_KERNEL);
241         if (!ei)
242                 return NULL;
243         return &ei->vfs_inode;
244 }
245
246 static void bdev_destroy_inode(struct inode *inode)
247 {
248         struct bdev_inode *bdi = BDEV_I(inode);
249
250         bdi->bdev.bd_inode_backing_dev_info = NULL;
251         kmem_cache_free(bdev_cachep, bdi);
252 }
253
254 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
255 {
256         struct bdev_inode *ei = (struct bdev_inode *) foo;
257         struct block_device *bdev = &ei->bdev;
258
259         if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
260             SLAB_CTOR_CONSTRUCTOR)
261         {
262                 memset(bdev, 0, sizeof(*bdev));
263                 mutex_init(&bdev->bd_mount_mutex);
264                 INIT_LIST_HEAD(&bdev->bd_inodes);
265                 INIT_LIST_HEAD(&bdev->bd_list);
266 #ifdef CONFIG_SYSFS
267                 INIT_LIST_HEAD(&bdev->bd_holder_list);
268 #endif
269                 inode_init_once(&ei->vfs_inode);
270         }
271 }
272
273 static inline void __bd_forget(struct inode *inode)
274 {
275         list_del_init(&inode->i_devices);
276         inode->i_bdev = NULL;
277         inode->i_mapping = &inode->i_data;
278 }
279
280 static void bdev_clear_inode(struct inode *inode)
281 {
282         struct block_device *bdev = &BDEV_I(inode)->bdev;
283         struct list_head *p;
284         spin_lock(&bdev_lock);
285         while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
286                 __bd_forget(list_entry(p, struct inode, i_devices));
287         }
288         list_del_init(&bdev->bd_list);
289         spin_unlock(&bdev_lock);
290 }
291
292 static struct super_operations bdev_sops = {
293         .statfs = simple_statfs,
294         .alloc_inode = bdev_alloc_inode,
295         .destroy_inode = bdev_destroy_inode,
296         .drop_inode = generic_delete_inode,
297         .clear_inode = bdev_clear_inode,
298 };
299
300 static int bd_get_sb(struct file_system_type *fs_type,
301         int flags, const char *dev_name, void *data, struct vfsmount *mnt)
302 {
303         return get_sb_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576, mnt);
304 }
305
306 static struct file_system_type bd_type = {
307         .name           = "bdev",
308         .get_sb         = bd_get_sb,
309         .kill_sb        = kill_anon_super,
310 };
311
312 static struct vfsmount *bd_mnt __read_mostly;
313 struct super_block *blockdev_superblock;
314
315 void __init bdev_cache_init(void)
316 {
317         int err;
318         bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
319                         0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
320                                 SLAB_MEM_SPREAD|SLAB_PANIC),
321                         init_once, NULL);
322         err = register_filesystem(&bd_type);
323         if (err)
324                 panic("Cannot register bdev pseudo-fs");
325         bd_mnt = kern_mount(&bd_type);
326         err = PTR_ERR(bd_mnt);
327         if (IS_ERR(bd_mnt))
328                 panic("Cannot create bdev pseudo-fs");
329         blockdev_superblock = bd_mnt->mnt_sb;   /* For writeback */
330 }
331
332 /*
333  * Most likely _very_ bad one - but then it's hardly critical for small
334  * /dev and can be fixed when somebody will need really large one.
335  * Keep in mind that it will be fed through icache hash function too.
336  */
337 static inline unsigned long hash(dev_t dev)
338 {
339         return MAJOR(dev)+MINOR(dev);
340 }
341
342 static int bdev_test(struct inode *inode, void *data)
343 {
344         return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
345 }
346
347 static int bdev_set(struct inode *inode, void *data)
348 {
349         BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
350         return 0;
351 }
352
353 static LIST_HEAD(all_bdevs);
354
355 static struct lock_class_key bdev_part_lock_key;
356
357 struct block_device *bdget(dev_t dev)
358 {
359         struct block_device *bdev;
360         struct inode *inode;
361         struct gendisk *disk;
362         int part = 0;
363
364         inode = iget5_locked(bd_mnt->mnt_sb, hash(dev),
365                         bdev_test, bdev_set, &dev);
366
367         if (!inode)
368                 return NULL;
369
370         bdev = &BDEV_I(inode)->bdev;
371
372         if (inode->i_state & I_NEW) {
373                 bdev->bd_contains = NULL;
374                 bdev->bd_inode = inode;
375                 bdev->bd_block_size = (1 << inode->i_blkbits);
376                 bdev->bd_part_count = 0;
377                 bdev->bd_invalidated = 0;
378                 inode->i_mode = S_IFBLK;
379                 inode->i_rdev = dev;
380                 inode->i_bdev = bdev;
381                 inode->i_data.a_ops = &def_blk_aops;
382                 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
383                 inode->i_data.backing_dev_info = &default_backing_dev_info;
384                 spin_lock(&bdev_lock);
385                 list_add(&bdev->bd_list, &all_bdevs);
386                 spin_unlock(&bdev_lock);
387                 unlock_new_inode(inode);
388                 mutex_init(&bdev->bd_mutex);
389                 disk = get_gendisk(dev, &part);
390                 if (disk && part)
391                         lockdep_set_class(&bdev->bd_mutex, &bdev_part_lock_key);
392                 put_disk(disk);
393         }
394         return bdev;
395 }
396
397 EXPORT_SYMBOL(bdget);
398
399 long nr_blockdev_pages(void)
400 {
401         struct list_head *p;
402         long ret = 0;
403         spin_lock(&bdev_lock);
404         list_for_each(p, &all_bdevs) {
405                 struct block_device *bdev;
406                 bdev = list_entry(p, struct block_device, bd_list);
407                 ret += bdev->bd_inode->i_mapping->nrpages;
408         }
409         spin_unlock(&bdev_lock);
410         return ret;
411 }
412
413 void bdput(struct block_device *bdev)
414 {
415         iput(bdev->bd_inode);
416 }
417
418 EXPORT_SYMBOL(bdput);
419  
420 static struct block_device *bd_acquire(struct inode *inode)
421 {
422         struct block_device *bdev;
423
424         spin_lock(&bdev_lock);
425         bdev = inode->i_bdev;
426         if (bdev) {
427                 atomic_inc(&bdev->bd_inode->i_count);
428                 spin_unlock(&bdev_lock);
429                 return bdev;
430         }
431         spin_unlock(&bdev_lock);
432
433         bdev = bdget(inode->i_rdev);
434         if (bdev) {
435                 spin_lock(&bdev_lock);
436                 if (!inode->i_bdev) {
437                         /*
438                          * We take an additional bd_inode->i_count for inode,
439                          * and it's released in clear_inode() of inode.
440                          * So, we can access it via ->i_mapping always
441                          * without igrab().
442                          */
443                         atomic_inc(&bdev->bd_inode->i_count);
444                         inode->i_bdev = bdev;
445                         inode->i_mapping = bdev->bd_inode->i_mapping;
446                         list_add(&inode->i_devices, &bdev->bd_inodes);
447                 }
448                 spin_unlock(&bdev_lock);
449         }
450         return bdev;
451 }
452
453 /* Call when you free inode */
454
455 void bd_forget(struct inode *inode)
456 {
457         struct block_device *bdev = NULL;
458
459         spin_lock(&bdev_lock);
460         if (inode->i_bdev) {
461                 if (inode->i_sb != blockdev_superblock)
462                         bdev = inode->i_bdev;
463                 __bd_forget(inode);
464         }
465         spin_unlock(&bdev_lock);
466
467         if (bdev)
468                 iput(bdev->bd_inode);
469 }
470
471 int bd_claim(struct block_device *bdev, void *holder)
472 {
473         int res;
474         spin_lock(&bdev_lock);
475
476         /* first decide result */
477         if (bdev->bd_holder == holder)
478                 res = 0;         /* already a holder */
479         else if (bdev->bd_holder != NULL)
480                 res = -EBUSY;    /* held by someone else */
481         else if (bdev->bd_contains == bdev)
482                 res = 0;         /* is a whole device which isn't held */
483
484         else if (bdev->bd_contains->bd_holder == bd_claim)
485                 res = 0;         /* is a partition of a device that is being partitioned */
486         else if (bdev->bd_contains->bd_holder != NULL)
487                 res = -EBUSY;    /* is a partition of a held device */
488         else
489                 res = 0;         /* is a partition of an un-held device */
490
491         /* now impose change */
492         if (res==0) {
493                 /* note that for a whole device bd_holders
494                  * will be incremented twice, and bd_holder will
495                  * be set to bd_claim before being set to holder
496                  */
497                 bdev->bd_contains->bd_holders ++;
498                 bdev->bd_contains->bd_holder = bd_claim;
499                 bdev->bd_holders++;
500                 bdev->bd_holder = holder;
501         }
502         spin_unlock(&bdev_lock);
503         return res;
504 }
505
506 EXPORT_SYMBOL(bd_claim);
507
508 void bd_release(struct block_device *bdev)
509 {
510         spin_lock(&bdev_lock);
511         if (!--bdev->bd_contains->bd_holders)
512                 bdev->bd_contains->bd_holder = NULL;
513         if (!--bdev->bd_holders)
514                 bdev->bd_holder = NULL;
515         spin_unlock(&bdev_lock);
516 }
517
518 EXPORT_SYMBOL(bd_release);
519
520 #ifdef CONFIG_SYSFS
521 /*
522  * Functions for bd_claim_by_kobject / bd_release_from_kobject
523  *
524  *     If a kobject is passed to bd_claim_by_kobject()
525  *     and the kobject has a parent directory,
526  *     following symlinks are created:
527  *        o from the kobject to the claimed bdev
528  *        o from "holders" directory of the bdev to the parent of the kobject
529  *     bd_release_from_kobject() removes these symlinks.
530  *
531  *     Example:
532  *        If /dev/dm-0 maps to /dev/sda, kobject corresponding to
533  *        /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then:
534  *           /sys/block/dm-0/slaves/sda --> /sys/block/sda
535  *           /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
536  */
537
538 static struct kobject *bdev_get_kobj(struct block_device *bdev)
539 {
540         if (bdev->bd_contains != bdev)
541                 return kobject_get(&bdev->bd_part->kobj);
542         else
543                 return kobject_get(&bdev->bd_disk->kobj);
544 }
545
546 static struct kobject *bdev_get_holder(struct block_device *bdev)
547 {
548         if (bdev->bd_contains != bdev)
549                 return kobject_get(bdev->bd_part->holder_dir);
550         else
551                 return kobject_get(bdev->bd_disk->holder_dir);
552 }
553
554 static void add_symlink(struct kobject *from, struct kobject *to)
555 {
556         if (!from || !to)
557                 return;
558         sysfs_create_link(from, to, kobject_name(to));
559 }
560
561 static void del_symlink(struct kobject *from, struct kobject *to)
562 {
563         if (!from || !to)
564                 return;
565         sysfs_remove_link(from, kobject_name(to));
566 }
567
568 /*
569  * 'struct bd_holder' contains pointers to kobjects symlinked by
570  * bd_claim_by_kobject.
571  * It's connected to bd_holder_list which is protected by bdev->bd_sem.
572  */
573 struct bd_holder {
574         struct list_head list;  /* chain of holders of the bdev */
575         int count;              /* references from the holder */
576         struct kobject *sdir;   /* holder object, e.g. "/block/dm-0/slaves" */
577         struct kobject *hdev;   /* e.g. "/block/dm-0" */
578         struct kobject *hdir;   /* e.g. "/block/sda/holders" */
579         struct kobject *sdev;   /* e.g. "/block/sda" */
580 };
581
582 /*
583  * Get references of related kobjects at once.
584  * Returns 1 on success. 0 on failure.
585  *
586  * Should call bd_holder_release_dirs() after successful use.
587  */
588 static int bd_holder_grab_dirs(struct block_device *bdev,
589                         struct bd_holder *bo)
590 {
591         if (!bdev || !bo)
592                 return 0;
593
594         bo->sdir = kobject_get(bo->sdir);
595         if (!bo->sdir)
596                 return 0;
597
598         bo->hdev = kobject_get(bo->sdir->parent);
599         if (!bo->hdev)
600                 goto fail_put_sdir;
601
602         bo->sdev = bdev_get_kobj(bdev);
603         if (!bo->sdev)
604                 goto fail_put_hdev;
605
606         bo->hdir = bdev_get_holder(bdev);
607         if (!bo->hdir)
608                 goto fail_put_sdev;
609
610         return 1;
611
612 fail_put_sdev:
613         kobject_put(bo->sdev);
614 fail_put_hdev:
615         kobject_put(bo->hdev);
616 fail_put_sdir:
617         kobject_put(bo->sdir);
618
619         return 0;
620 }
621
622 /* Put references of related kobjects at once. */
623 static void bd_holder_release_dirs(struct bd_holder *bo)
624 {
625         kobject_put(bo->hdir);
626         kobject_put(bo->sdev);
627         kobject_put(bo->hdev);
628         kobject_put(bo->sdir);
629 }
630
631 static struct bd_holder *alloc_bd_holder(struct kobject *kobj)
632 {
633         struct bd_holder *bo;
634
635         bo = kzalloc(sizeof(*bo), GFP_KERNEL);
636         if (!bo)
637                 return NULL;
638
639         bo->count = 1;
640         bo->sdir = kobj;
641
642         return bo;
643 }
644
645 static void free_bd_holder(struct bd_holder *bo)
646 {
647         kfree(bo);
648 }
649
650 /**
651  * add_bd_holder - create sysfs symlinks for bd_claim() relationship
652  *
653  * @bdev:       block device to be bd_claimed
654  * @bo:         preallocated and initialized by alloc_bd_holder()
655  *
656  * If there is no matching entry with @bo in @bdev->bd_holder_list,
657  * add @bo to the list, create symlinks.
658  *
659  * Returns 1 if @bo was added to the list.
660  * Returns 0 if @bo wasn't used by any reason and should be freed.
661  */
662 static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
663 {
664         struct bd_holder *tmp;
665
666         if (!bo)
667                 return 0;
668
669         list_for_each_entry(tmp, &bdev->bd_holder_list, list) {
670                 if (tmp->sdir == bo->sdir) {
671                         tmp->count++;
672                         return 0;
673                 }
674         }
675
676         if (!bd_holder_grab_dirs(bdev, bo))
677                 return 0;
678
679         add_symlink(bo->sdir, bo->sdev);
680         add_symlink(bo->hdir, bo->hdev);
681         list_add_tail(&bo->list, &bdev->bd_holder_list);
682         return 1;
683 }
684
685 /**
686  * del_bd_holder - delete sysfs symlinks for bd_claim() relationship
687  *
688  * @bdev:       block device to be bd_claimed
689  * @kobj:       holder's kobject
690  *
691  * If there is matching entry with @kobj in @bdev->bd_holder_list
692  * and no other bd_claim() from the same kobject,
693  * remove the struct bd_holder from the list, delete symlinks for it.
694  *
695  * Returns a pointer to the struct bd_holder when it's removed from the list
696  * and ready to be freed.
697  * Returns NULL if matching claim isn't found or there is other bd_claim()
698  * by the same kobject.
699  */
700 static struct bd_holder *del_bd_holder(struct block_device *bdev,
701                                         struct kobject *kobj)
702 {
703         struct bd_holder *bo;
704
705         list_for_each_entry(bo, &bdev->bd_holder_list, list) {
706                 if (bo->sdir == kobj) {
707                         bo->count--;
708                         BUG_ON(bo->count < 0);
709                         if (!bo->count) {
710                                 list_del(&bo->list);
711                                 del_symlink(bo->sdir, bo->sdev);
712                                 del_symlink(bo->hdir, bo->hdev);
713                                 bd_holder_release_dirs(bo);
714                                 return bo;
715                         }
716                         break;
717                 }
718         }
719
720         return NULL;
721 }
722
723 /**
724  * bd_claim_by_kobject - bd_claim() with additional kobject signature
725  *
726  * @bdev:       block device to be claimed
727  * @holder:     holder's signature
728  * @kobj:       holder's kobject
729  *
730  * Do bd_claim() and if it succeeds, create sysfs symlinks between
731  * the bdev and the holder's kobject.
732  * Use bd_release_from_kobject() when relesing the claimed bdev.
733  *
734  * Returns 0 on success. (same as bd_claim())
735  * Returns errno on failure.
736  */
737 static int bd_claim_by_kobject(struct block_device *bdev, void *holder,
738                                 struct kobject *kobj)
739 {
740         int res;
741         struct bd_holder *bo;
742
743         if (!kobj)
744                 return -EINVAL;
745
746         bo = alloc_bd_holder(kobj);
747         if (!bo)
748                 return -ENOMEM;
749
750         mutex_lock(&bdev->bd_mutex);
751         res = bd_claim(bdev, holder);
752         if (res || !add_bd_holder(bdev, bo))
753                 free_bd_holder(bo);
754         mutex_unlock(&bdev->bd_mutex);
755
756         return res;
757 }
758
759 /**
760  * bd_release_from_kobject - bd_release() with additional kobject signature
761  *
762  * @bdev:       block device to be released
763  * @kobj:       holder's kobject
764  *
765  * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject().
766  */
767 static void bd_release_from_kobject(struct block_device *bdev,
768                                         struct kobject *kobj)
769 {
770         struct bd_holder *bo;
771
772         if (!kobj)
773                 return;
774
775         mutex_lock(&bdev->bd_mutex);
776         bd_release(bdev);
777         if ((bo = del_bd_holder(bdev, kobj)))
778                 free_bd_holder(bo);
779         mutex_unlock(&bdev->bd_mutex);
780 }
781
782 /**
783  * bd_claim_by_disk - wrapper function for bd_claim_by_kobject()
784  *
785  * @bdev:       block device to be claimed
786  * @holder:     holder's signature
787  * @disk:       holder's gendisk
788  *
789  * Call bd_claim_by_kobject() with getting @disk->slave_dir.
790  */
791 int bd_claim_by_disk(struct block_device *bdev, void *holder,
792                         struct gendisk *disk)
793 {
794         return bd_claim_by_kobject(bdev, holder, kobject_get(disk->slave_dir));
795 }
796 EXPORT_SYMBOL_GPL(bd_claim_by_disk);
797
798 /**
799  * bd_release_from_disk - wrapper function for bd_release_from_kobject()
800  *
801  * @bdev:       block device to be claimed
802  * @disk:       holder's gendisk
803  *
804  * Call bd_release_from_kobject() and put @disk->slave_dir.
805  */
806 void bd_release_from_disk(struct block_device *bdev, struct gendisk *disk)
807 {
808         bd_release_from_kobject(bdev, disk->slave_dir);
809         kobject_put(disk->slave_dir);
810 }
811 EXPORT_SYMBOL_GPL(bd_release_from_disk);
812 #endif
813
814 /*
815  * Tries to open block device by device number.  Use it ONLY if you
816  * really do not have anything better - i.e. when you are behind a
817  * truly sucky interface and all you are given is a device number.  _Never_
818  * to be used for internal purposes.  If you ever need it - reconsider
819  * your API.
820  */
821 struct block_device *open_by_devnum(dev_t dev, unsigned mode)
822 {
823         struct block_device *bdev = bdget(dev);
824         int err = -ENOMEM;
825         int flags = mode & FMODE_WRITE ? O_RDWR : O_RDONLY;
826         if (bdev)
827                 err = blkdev_get(bdev, mode, flags);
828         return err ? ERR_PTR(err) : bdev;
829 }
830
831 EXPORT_SYMBOL(open_by_devnum);
832
833 /*
834  * This routine checks whether a removable media has been changed,
835  * and invalidates all buffer-cache-entries in that case. This
836  * is a relatively slow routine, so we have to try to minimize using
837  * it. Thus it is called only upon a 'mount' or 'open'. This
838  * is the best way of combining speed and utility, I think.
839  * People changing diskettes in the middle of an operation deserve
840  * to lose :-)
841  */
842 int check_disk_change(struct block_device *bdev)
843 {
844         struct gendisk *disk = bdev->bd_disk;
845         struct block_device_operations * bdops = disk->fops;
846
847         if (!bdops->media_changed)
848                 return 0;
849         if (!bdops->media_changed(bdev->bd_disk))
850                 return 0;
851
852         if (__invalidate_device(bdev))
853                 printk("VFS: busy inodes on changed media.\n");
854
855         if (bdops->revalidate_disk)
856                 bdops->revalidate_disk(bdev->bd_disk);
857         if (bdev->bd_disk->minors > 1)
858                 bdev->bd_invalidated = 1;
859         return 1;
860 }
861
862 EXPORT_SYMBOL(check_disk_change);
863
864 void bd_set_size(struct block_device *bdev, loff_t size)
865 {
866         unsigned bsize = bdev_hardsect_size(bdev);
867
868         bdev->bd_inode->i_size = size;
869         while (bsize < PAGE_CACHE_SIZE) {
870                 if (size & bsize)
871                         break;
872                 bsize <<= 1;
873         }
874         bdev->bd_block_size = bsize;
875         bdev->bd_inode->i_blkbits = blksize_bits(bsize);
876 }
877 EXPORT_SYMBOL(bd_set_size);
878
879 static int do_open(struct block_device *bdev, struct file *file)
880 {
881         struct module *owner = NULL;
882         struct gendisk *disk;
883         int ret = -ENXIO;
884         int part;
885
886         file->f_mapping = bdev->bd_inode->i_mapping;
887         lock_kernel();
888         disk = get_gendisk(bdev->bd_dev, &part);
889         if (!disk) {
890                 unlock_kernel();
891                 bdput(bdev);
892                 return ret;
893         }
894         owner = disk->fops->owner;
895
896         mutex_lock(&bdev->bd_mutex);
897         if (!bdev->bd_openers) {
898                 bdev->bd_disk = disk;
899                 bdev->bd_contains = bdev;
900                 if (!part) {
901                         struct backing_dev_info *bdi;
902                         if (disk->fops->open) {
903                                 ret = disk->fops->open(bdev->bd_inode, file);
904                                 if (ret)
905                                         goto out_first;
906                         }
907                         if (!bdev->bd_openers) {
908                                 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
909                                 bdi = blk_get_backing_dev_info(bdev);
910                                 if (bdi == NULL)
911                                         bdi = &default_backing_dev_info;
912                                 bdev->bd_inode->i_data.backing_dev_info = bdi;
913                         }
914                         if (bdev->bd_invalidated)
915                                 rescan_partitions(disk, bdev);
916                 } else {
917                         struct hd_struct *p;
918                         struct block_device *whole;
919                         whole = bdget_disk(disk, 0);
920                         ret = -ENOMEM;
921                         if (!whole)
922                                 goto out_first;
923                         ret = blkdev_get(whole, file->f_mode, file->f_flags);
924                         if (ret)
925                                 goto out_first;
926                         bdev->bd_contains = whole;
927                         mutex_lock(&whole->bd_mutex);
928                         whole->bd_part_count++;
929                         p = disk->part[part - 1];
930                         bdev->bd_inode->i_data.backing_dev_info =
931                            whole->bd_inode->i_data.backing_dev_info;
932                         if (!(disk->flags & GENHD_FL_UP) || !p || !p->nr_sects) {
933                                 whole->bd_part_count--;
934                                 mutex_unlock(&whole->bd_mutex);
935                                 ret = -ENXIO;
936                                 goto out_first;
937                         }
938                         kobject_get(&p->kobj);
939                         bdev->bd_part = p;
940                         bd_set_size(bdev, (loff_t) p->nr_sects << 9);
941                         mutex_unlock(&whole->bd_mutex);
942                 }
943         } else {
944                 put_disk(disk);
945                 module_put(owner);
946                 if (bdev->bd_contains == bdev) {
947                         if (bdev->bd_disk->fops->open) {
948                                 ret = bdev->bd_disk->fops->open(bdev->bd_inode, file);
949                                 if (ret)
950                                         goto out;
951                         }
952                         if (bdev->bd_invalidated)
953                                 rescan_partitions(bdev->bd_disk, bdev);
954                 } else {
955                         mutex_lock(&bdev->bd_contains->bd_mutex);
956                         bdev->bd_contains->bd_part_count++;
957                         mutex_unlock(&bdev->bd_contains->bd_mutex);
958                 }
959         }
960         bdev->bd_openers++;
961         mutex_unlock(&bdev->bd_mutex);
962         unlock_kernel();
963         return 0;
964
965 out_first:
966         bdev->bd_disk = NULL;
967         bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
968         if (bdev != bdev->bd_contains)
969                 blkdev_put(bdev->bd_contains);
970         bdev->bd_contains = NULL;
971         put_disk(disk);
972         module_put(owner);
973 out:
974         mutex_unlock(&bdev->bd_mutex);
975         unlock_kernel();
976         if (ret)
977                 bdput(bdev);
978         return ret;
979 }
980
981 int blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags)
982 {
983         /*
984          * This crockload is due to bad choice of ->open() type.
985          * It will go away.
986          * For now, block device ->open() routine must _not_
987          * examine anything in 'inode' argument except ->i_rdev.
988          */
989         struct file fake_file = {};
990         struct dentry fake_dentry = {};
991         fake_file.f_mode = mode;
992         fake_file.f_flags = flags;
993         fake_file.f_dentry = &fake_dentry;
994         fake_dentry.d_inode = bdev->bd_inode;
995
996         return do_open(bdev, &fake_file);
997 }
998
999 EXPORT_SYMBOL(blkdev_get);
1000
1001 static int blkdev_open(struct inode * inode, struct file * filp)
1002 {
1003         struct block_device *bdev;
1004         int res;
1005
1006         /*
1007          * Preserve backwards compatibility and allow large file access
1008          * even if userspace doesn't ask for it explicitly. Some mkfs
1009          * binary needs it. We might want to drop this workaround
1010          * during an unstable branch.
1011          */
1012         filp->f_flags |= O_LARGEFILE;
1013
1014         bdev = bd_acquire(inode);
1015
1016         res = do_open(bdev, filp);
1017         if (res)
1018                 return res;
1019
1020         if (!(filp->f_flags & O_EXCL) )
1021                 return 0;
1022
1023         if (!(res = bd_claim(bdev, filp)))
1024                 return 0;
1025
1026         blkdev_put(bdev);
1027         return res;
1028 }
1029
1030 int blkdev_put(struct block_device *bdev)
1031 {
1032         int ret = 0;
1033         struct inode *bd_inode = bdev->bd_inode;
1034         struct gendisk *disk = bdev->bd_disk;
1035
1036         mutex_lock(&bdev->bd_mutex);
1037         lock_kernel();
1038         if (!--bdev->bd_openers) {
1039                 sync_blockdev(bdev);
1040                 kill_bdev(bdev);
1041         }
1042         if (bdev->bd_contains == bdev) {
1043                 if (disk->fops->release)
1044                         ret = disk->fops->release(bd_inode, NULL);
1045         } else {
1046                 mutex_lock(&bdev->bd_contains->bd_mutex);
1047                 bdev->bd_contains->bd_part_count--;
1048                 mutex_unlock(&bdev->bd_contains->bd_mutex);
1049         }
1050         if (!bdev->bd_openers) {
1051                 struct module *owner = disk->fops->owner;
1052
1053                 put_disk(disk);
1054                 module_put(owner);
1055
1056                 if (bdev->bd_contains != bdev) {
1057                         kobject_put(&bdev->bd_part->kobj);
1058                         bdev->bd_part = NULL;
1059                 }
1060                 bdev->bd_disk = NULL;
1061                 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
1062                 if (bdev != bdev->bd_contains) {
1063                         blkdev_put(bdev->bd_contains);
1064                 }
1065                 bdev->bd_contains = NULL;
1066         }
1067         unlock_kernel();
1068         mutex_unlock(&bdev->bd_mutex);
1069         bdput(bdev);
1070         return ret;
1071 }
1072
1073 EXPORT_SYMBOL(blkdev_put);
1074
1075 static int blkdev_close(struct inode * inode, struct file * filp)
1076 {
1077         struct block_device *bdev = I_BDEV(filp->f_mapping->host);
1078         if (bdev->bd_holder == filp)
1079                 bd_release(bdev);
1080         return blkdev_put(bdev);
1081 }
1082
1083 static ssize_t blkdev_file_write(struct file *file, const char __user *buf,
1084                                    size_t count, loff_t *ppos)
1085 {
1086         struct iovec local_iov = { .iov_base = (void __user *)buf, .iov_len = count };
1087
1088         return generic_file_write_nolock(file, &local_iov, 1, ppos);
1089 }
1090
1091 static ssize_t blkdev_file_aio_write(struct kiocb *iocb, const char __user *buf,
1092                                    size_t count, loff_t pos)
1093 {
1094         struct iovec local_iov = { .iov_base = (void __user *)buf, .iov_len = count };
1095
1096         return generic_file_aio_write_nolock(iocb, &local_iov, 1, &iocb->ki_pos);
1097 }
1098
1099 static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1100 {
1101         return blkdev_ioctl(file->f_mapping->host, file, cmd, arg);
1102 }
1103
1104 const struct address_space_operations def_blk_aops = {
1105         .readpage       = blkdev_readpage,
1106         .writepage      = blkdev_writepage,
1107         .sync_page      = block_sync_page,
1108         .prepare_write  = blkdev_prepare_write,
1109         .commit_write   = blkdev_commit_write,
1110         .writepages     = generic_writepages,
1111         .direct_IO      = blkdev_direct_IO,
1112 };
1113
1114 const struct file_operations def_blk_fops = {
1115         .open           = blkdev_open,
1116         .release        = blkdev_close,
1117         .llseek         = block_llseek,
1118         .read           = generic_file_read,
1119         .write          = blkdev_file_write,
1120         .aio_read       = generic_file_aio_read,
1121         .aio_write      = blkdev_file_aio_write, 
1122         .mmap           = generic_file_mmap,
1123         .fsync          = block_fsync,
1124         .unlocked_ioctl = block_ioctl,
1125 #ifdef CONFIG_COMPAT
1126         .compat_ioctl   = compat_blkdev_ioctl,
1127 #endif
1128         .readv          = generic_file_readv,
1129         .writev         = generic_file_write_nolock,
1130         .sendfile       = generic_file_sendfile,
1131         .splice_read    = generic_file_splice_read,
1132         .splice_write   = generic_file_splice_write,
1133 };
1134
1135 int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
1136 {
1137         int res;
1138         mm_segment_t old_fs = get_fs();
1139         set_fs(KERNEL_DS);
1140         res = blkdev_ioctl(bdev->bd_inode, NULL, cmd, arg);
1141         set_fs(old_fs);
1142         return res;
1143 }
1144
1145 EXPORT_SYMBOL(ioctl_by_bdev);
1146
1147 /**
1148  * lookup_bdev  - lookup a struct block_device by name
1149  *
1150  * @path:       special file representing the block device
1151  *
1152  * Get a reference to the blockdevice at @path in the current
1153  * namespace if possible and return it.  Return ERR_PTR(error)
1154  * otherwise.
1155  */
1156 struct block_device *lookup_bdev(const char *path)
1157 {
1158         struct block_device *bdev;
1159         struct inode *inode;
1160         struct nameidata nd;
1161         int error;
1162
1163         if (!path || !*path)
1164                 return ERR_PTR(-EINVAL);
1165
1166         error = path_lookup(path, LOOKUP_FOLLOW, &nd);
1167         if (error)
1168                 return ERR_PTR(error);
1169
1170         inode = nd.dentry->d_inode;
1171         error = -ENOTBLK;
1172         if (!S_ISBLK(inode->i_mode))
1173                 goto fail;
1174         error = -EACCES;
1175         if (nd.mnt->mnt_flags & MNT_NODEV)
1176                 goto fail;
1177         error = -ENOMEM;
1178         bdev = bd_acquire(inode);
1179         if (!bdev)
1180                 goto fail;
1181 out:
1182         path_release(&nd);
1183         return bdev;
1184 fail:
1185         bdev = ERR_PTR(error);
1186         goto out;
1187 }
1188
1189 /**
1190  * open_bdev_excl  -  open a block device by name and set it up for use
1191  *
1192  * @path:       special file representing the block device
1193  * @flags:      %MS_RDONLY for opening read-only
1194  * @holder:     owner for exclusion
1195  *
1196  * Open the blockdevice described by the special file at @path, claim it
1197  * for the @holder.
1198  */
1199 struct block_device *open_bdev_excl(const char *path, int flags, void *holder)
1200 {
1201         struct block_device *bdev;
1202         mode_t mode = FMODE_READ;
1203         int error = 0;
1204
1205         bdev = lookup_bdev(path);
1206         if (IS_ERR(bdev))
1207                 return bdev;
1208
1209         if (!(flags & MS_RDONLY))
1210                 mode |= FMODE_WRITE;
1211         error = blkdev_get(bdev, mode, 0);
1212         if (error)
1213                 return ERR_PTR(error);
1214         error = -EACCES;
1215         if (!(flags & MS_RDONLY) && bdev_read_only(bdev))
1216                 goto blkdev_put;
1217         error = bd_claim(bdev, holder);
1218         if (error)
1219                 goto blkdev_put;
1220
1221         return bdev;
1222         
1223 blkdev_put:
1224         blkdev_put(bdev);
1225         return ERR_PTR(error);
1226 }
1227
1228 EXPORT_SYMBOL(open_bdev_excl);
1229
1230 /**
1231  * close_bdev_excl  -  release a blockdevice openen by open_bdev_excl()
1232  *
1233  * @bdev:       blockdevice to close
1234  *
1235  * This is the counterpart to open_bdev_excl().
1236  */
1237 void close_bdev_excl(struct block_device *bdev)
1238 {
1239         bd_release(bdev);
1240         blkdev_put(bdev);
1241 }
1242
1243 EXPORT_SYMBOL(close_bdev_excl);