]> git.karo-electronics.de Git - mv-sheeva.git/blob - fs/block_dev.c
block: simplify holder symlink handling
[mv-sheeva.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/device_cgroup.h>
16 #include <linux/highmem.h>
17 #include <linux/blkdev.h>
18 #include <linux/module.h>
19 #include <linux/blkpg.h>
20 #include <linux/buffer_head.h>
21 #include <linux/pagevec.h>
22 #include <linux/writeback.h>
23 #include <linux/mpage.h>
24 #include <linux/mount.h>
25 #include <linux/uio.h>
26 #include <linux/namei.h>
27 #include <linux/log2.h>
28 #include <linux/kmemleak.h>
29 #include <asm/uaccess.h>
30 #include "internal.h"
31
32 struct bdev_inode {
33         struct block_device bdev;
34         struct inode vfs_inode;
35 };
36
37 static const struct address_space_operations def_blk_aops;
38
39 static inline struct bdev_inode *BDEV_I(struct inode *inode)
40 {
41         return container_of(inode, struct bdev_inode, vfs_inode);
42 }
43
44 inline struct block_device *I_BDEV(struct inode *inode)
45 {
46         return &BDEV_I(inode)->bdev;
47 }
48
49 EXPORT_SYMBOL(I_BDEV);
50
51 /*
52  * move the inode from it's current bdi to the a new bdi. if the inode is dirty
53  * we need to move it onto the dirty list of @dst so that the inode is always
54  * on the right list.
55  */
56 static void bdev_inode_switch_bdi(struct inode *inode,
57                         struct backing_dev_info *dst)
58 {
59         spin_lock(&inode_lock);
60         inode->i_data.backing_dev_info = dst;
61         if (inode->i_state & I_DIRTY)
62                 list_move(&inode->i_wb_list, &dst->wb.b_dirty);
63         spin_unlock(&inode_lock);
64 }
65
66 static sector_t max_block(struct block_device *bdev)
67 {
68         sector_t retval = ~((sector_t)0);
69         loff_t sz = i_size_read(bdev->bd_inode);
70
71         if (sz) {
72                 unsigned int size = block_size(bdev);
73                 unsigned int sizebits = blksize_bits(size);
74                 retval = (sz >> sizebits);
75         }
76         return retval;
77 }
78
79 /* Kill _all_ buffers and pagecache , dirty or not.. */
80 static void kill_bdev(struct block_device *bdev)
81 {
82         if (bdev->bd_inode->i_mapping->nrpages == 0)
83                 return;
84         invalidate_bh_lrus();
85         truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
86 }       
87
88 int set_blocksize(struct block_device *bdev, int size)
89 {
90         /* Size must be a power of two, and between 512 and PAGE_SIZE */
91         if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
92                 return -EINVAL;
93
94         /* Size cannot be smaller than the size supported by the device */
95         if (size < bdev_logical_block_size(bdev))
96                 return -EINVAL;
97
98         /* Don't change the size if it is same as current */
99         if (bdev->bd_block_size != size) {
100                 sync_blockdev(bdev);
101                 bdev->bd_block_size = size;
102                 bdev->bd_inode->i_blkbits = blksize_bits(size);
103                 kill_bdev(bdev);
104         }
105         return 0;
106 }
107
108 EXPORT_SYMBOL(set_blocksize);
109
110 int sb_set_blocksize(struct super_block *sb, int size)
111 {
112         if (set_blocksize(sb->s_bdev, size))
113                 return 0;
114         /* If we get here, we know size is power of two
115          * and it's value is between 512 and PAGE_SIZE */
116         sb->s_blocksize = size;
117         sb->s_blocksize_bits = blksize_bits(size);
118         return sb->s_blocksize;
119 }
120
121 EXPORT_SYMBOL(sb_set_blocksize);
122
123 int sb_min_blocksize(struct super_block *sb, int size)
124 {
125         int minsize = bdev_logical_block_size(sb->s_bdev);
126         if (size < minsize)
127                 size = minsize;
128         return sb_set_blocksize(sb, size);
129 }
130
131 EXPORT_SYMBOL(sb_min_blocksize);
132
133 static int
134 blkdev_get_block(struct inode *inode, sector_t iblock,
135                 struct buffer_head *bh, int create)
136 {
137         if (iblock >= max_block(I_BDEV(inode))) {
138                 if (create)
139                         return -EIO;
140
141                 /*
142                  * for reads, we're just trying to fill a partial page.
143                  * return a hole, they will have to call get_block again
144                  * before they can fill it, and they will get -EIO at that
145                  * time
146                  */
147                 return 0;
148         }
149         bh->b_bdev = I_BDEV(inode);
150         bh->b_blocknr = iblock;
151         set_buffer_mapped(bh);
152         return 0;
153 }
154
155 static int
156 blkdev_get_blocks(struct inode *inode, sector_t iblock,
157                 struct buffer_head *bh, int create)
158 {
159         sector_t end_block = max_block(I_BDEV(inode));
160         unsigned long max_blocks = bh->b_size >> inode->i_blkbits;
161
162         if ((iblock + max_blocks) > end_block) {
163                 max_blocks = end_block - iblock;
164                 if ((long)max_blocks <= 0) {
165                         if (create)
166                                 return -EIO;    /* write fully beyond EOF */
167                         /*
168                          * It is a read which is fully beyond EOF.  We return
169                          * a !buffer_mapped buffer
170                          */
171                         max_blocks = 0;
172                 }
173         }
174
175         bh->b_bdev = I_BDEV(inode);
176         bh->b_blocknr = iblock;
177         bh->b_size = max_blocks << inode->i_blkbits;
178         if (max_blocks)
179                 set_buffer_mapped(bh);
180         return 0;
181 }
182
183 static ssize_t
184 blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
185                         loff_t offset, unsigned long nr_segs)
186 {
187         struct file *file = iocb->ki_filp;
188         struct inode *inode = file->f_mapping->host;
189
190         return __blockdev_direct_IO(rw, iocb, inode, I_BDEV(inode), iov, offset,
191                                     nr_segs, blkdev_get_blocks, NULL, NULL, 0);
192 }
193
194 int __sync_blockdev(struct block_device *bdev, int wait)
195 {
196         if (!bdev)
197                 return 0;
198         if (!wait)
199                 return filemap_flush(bdev->bd_inode->i_mapping);
200         return filemap_write_and_wait(bdev->bd_inode->i_mapping);
201 }
202
203 /*
204  * Write out and wait upon all the dirty data associated with a block
205  * device via its mapping.  Does not take the superblock lock.
206  */
207 int sync_blockdev(struct block_device *bdev)
208 {
209         return __sync_blockdev(bdev, 1);
210 }
211 EXPORT_SYMBOL(sync_blockdev);
212
213 /*
214  * Write out and wait upon all dirty data associated with this
215  * device.   Filesystem data as well as the underlying block
216  * device.  Takes the superblock lock.
217  */
218 int fsync_bdev(struct block_device *bdev)
219 {
220         struct super_block *sb = get_super(bdev);
221         if (sb) {
222                 int res = sync_filesystem(sb);
223                 drop_super(sb);
224                 return res;
225         }
226         return sync_blockdev(bdev);
227 }
228 EXPORT_SYMBOL(fsync_bdev);
229
230 /**
231  * freeze_bdev  --  lock a filesystem and force it into a consistent state
232  * @bdev:       blockdevice to lock
233  *
234  * If a superblock is found on this device, we take the s_umount semaphore
235  * on it to make sure nobody unmounts until the snapshot creation is done.
236  * The reference counter (bd_fsfreeze_count) guarantees that only the last
237  * unfreeze process can unfreeze the frozen filesystem actually when multiple
238  * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
239  * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
240  * actually.
241  */
242 struct super_block *freeze_bdev(struct block_device *bdev)
243 {
244         struct super_block *sb;
245         int error = 0;
246
247         mutex_lock(&bdev->bd_fsfreeze_mutex);
248         if (++bdev->bd_fsfreeze_count > 1) {
249                 /*
250                  * We don't even need to grab a reference - the first call
251                  * to freeze_bdev grab an active reference and only the last
252                  * thaw_bdev drops it.
253                  */
254                 sb = get_super(bdev);
255                 drop_super(sb);
256                 mutex_unlock(&bdev->bd_fsfreeze_mutex);
257                 return sb;
258         }
259
260         sb = get_active_super(bdev);
261         if (!sb)
262                 goto out;
263         error = freeze_super(sb);
264         if (error) {
265                 deactivate_super(sb);
266                 bdev->bd_fsfreeze_count--;
267                 mutex_unlock(&bdev->bd_fsfreeze_mutex);
268                 return ERR_PTR(error);
269         }
270         deactivate_super(sb);
271  out:
272         sync_blockdev(bdev);
273         mutex_unlock(&bdev->bd_fsfreeze_mutex);
274         return sb;      /* thaw_bdev releases s->s_umount */
275 }
276 EXPORT_SYMBOL(freeze_bdev);
277
278 /**
279  * thaw_bdev  -- unlock filesystem
280  * @bdev:       blockdevice to unlock
281  * @sb:         associated superblock
282  *
283  * Unlocks the filesystem and marks it writeable again after freeze_bdev().
284  */
285 int thaw_bdev(struct block_device *bdev, struct super_block *sb)
286 {
287         int error = -EINVAL;
288
289         mutex_lock(&bdev->bd_fsfreeze_mutex);
290         if (!bdev->bd_fsfreeze_count)
291                 goto out;
292
293         error = 0;
294         if (--bdev->bd_fsfreeze_count > 0)
295                 goto out;
296
297         if (!sb)
298                 goto out;
299
300         error = thaw_super(sb);
301         if (error) {
302                 bdev->bd_fsfreeze_count++;
303                 mutex_unlock(&bdev->bd_fsfreeze_mutex);
304                 return error;
305         }
306 out:
307         mutex_unlock(&bdev->bd_fsfreeze_mutex);
308         return 0;
309 }
310 EXPORT_SYMBOL(thaw_bdev);
311
312 static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
313 {
314         return block_write_full_page(page, blkdev_get_block, wbc);
315 }
316
317 static int blkdev_readpage(struct file * file, struct page * page)
318 {
319         return block_read_full_page(page, blkdev_get_block);
320 }
321
322 static int blkdev_write_begin(struct file *file, struct address_space *mapping,
323                         loff_t pos, unsigned len, unsigned flags,
324                         struct page **pagep, void **fsdata)
325 {
326         return block_write_begin(mapping, pos, len, flags, pagep,
327                                  blkdev_get_block);
328 }
329
330 static int blkdev_write_end(struct file *file, struct address_space *mapping,
331                         loff_t pos, unsigned len, unsigned copied,
332                         struct page *page, void *fsdata)
333 {
334         int ret;
335         ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
336
337         unlock_page(page);
338         page_cache_release(page);
339
340         return ret;
341 }
342
343 /*
344  * private llseek:
345  * for a block special file file->f_path.dentry->d_inode->i_size is zero
346  * so we compute the size by hand (just as in block_read/write above)
347  */
348 static loff_t block_llseek(struct file *file, loff_t offset, int origin)
349 {
350         struct inode *bd_inode = file->f_mapping->host;
351         loff_t size;
352         loff_t retval;
353
354         mutex_lock(&bd_inode->i_mutex);
355         size = i_size_read(bd_inode);
356
357         switch (origin) {
358                 case 2:
359                         offset += size;
360                         break;
361                 case 1:
362                         offset += file->f_pos;
363         }
364         retval = -EINVAL;
365         if (offset >= 0 && offset <= size) {
366                 if (offset != file->f_pos) {
367                         file->f_pos = offset;
368                 }
369                 retval = offset;
370         }
371         mutex_unlock(&bd_inode->i_mutex);
372         return retval;
373 }
374         
375 int blkdev_fsync(struct file *filp, int datasync)
376 {
377         struct inode *bd_inode = filp->f_mapping->host;
378         struct block_device *bdev = I_BDEV(bd_inode);
379         int error;
380
381         /*
382          * There is no need to serialise calls to blkdev_issue_flush with
383          * i_mutex and doing so causes performance issues with concurrent
384          * O_SYNC writers to a block device.
385          */
386         mutex_unlock(&bd_inode->i_mutex);
387
388         error = blkdev_issue_flush(bdev, GFP_KERNEL, NULL);
389         if (error == -EOPNOTSUPP)
390                 error = 0;
391
392         mutex_lock(&bd_inode->i_mutex);
393
394         return error;
395 }
396 EXPORT_SYMBOL(blkdev_fsync);
397
398 /*
399  * pseudo-fs
400  */
401
402 static  __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
403 static struct kmem_cache * bdev_cachep __read_mostly;
404
405 static struct inode *bdev_alloc_inode(struct super_block *sb)
406 {
407         struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
408         if (!ei)
409                 return NULL;
410         return &ei->vfs_inode;
411 }
412
413 static void bdev_destroy_inode(struct inode *inode)
414 {
415         struct bdev_inode *bdi = BDEV_I(inode);
416
417         kmem_cache_free(bdev_cachep, bdi);
418 }
419
420 static void init_once(void *foo)
421 {
422         struct bdev_inode *ei = (struct bdev_inode *) foo;
423         struct block_device *bdev = &ei->bdev;
424
425         memset(bdev, 0, sizeof(*bdev));
426         mutex_init(&bdev->bd_mutex);
427         INIT_LIST_HEAD(&bdev->bd_inodes);
428         INIT_LIST_HEAD(&bdev->bd_list);
429         inode_init_once(&ei->vfs_inode);
430         /* Initialize mutex for freeze. */
431         mutex_init(&bdev->bd_fsfreeze_mutex);
432 }
433
434 static inline void __bd_forget(struct inode *inode)
435 {
436         list_del_init(&inode->i_devices);
437         inode->i_bdev = NULL;
438         inode->i_mapping = &inode->i_data;
439 }
440
441 static void bdev_evict_inode(struct inode *inode)
442 {
443         struct block_device *bdev = &BDEV_I(inode)->bdev;
444         struct list_head *p;
445         truncate_inode_pages(&inode->i_data, 0);
446         invalidate_inode_buffers(inode); /* is it needed here? */
447         end_writeback(inode);
448         spin_lock(&bdev_lock);
449         while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
450                 __bd_forget(list_entry(p, struct inode, i_devices));
451         }
452         list_del_init(&bdev->bd_list);
453         spin_unlock(&bdev_lock);
454 }
455
456 static const struct super_operations bdev_sops = {
457         .statfs = simple_statfs,
458         .alloc_inode = bdev_alloc_inode,
459         .destroy_inode = bdev_destroy_inode,
460         .drop_inode = generic_delete_inode,
461         .evict_inode = bdev_evict_inode,
462 };
463
464 static struct dentry *bd_mount(struct file_system_type *fs_type,
465         int flags, const char *dev_name, void *data)
466 {
467         return mount_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576);
468 }
469
470 static struct file_system_type bd_type = {
471         .name           = "bdev",
472         .mount          = bd_mount,
473         .kill_sb        = kill_anon_super,
474 };
475
476 struct super_block *blockdev_superblock __read_mostly;
477
478 void __init bdev_cache_init(void)
479 {
480         int err;
481         struct vfsmount *bd_mnt;
482
483         bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
484                         0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
485                                 SLAB_MEM_SPREAD|SLAB_PANIC),
486                         init_once);
487         err = register_filesystem(&bd_type);
488         if (err)
489                 panic("Cannot register bdev pseudo-fs");
490         bd_mnt = kern_mount(&bd_type);
491         if (IS_ERR(bd_mnt))
492                 panic("Cannot create bdev pseudo-fs");
493         /*
494          * This vfsmount structure is only used to obtain the
495          * blockdev_superblock, so tell kmemleak not to report it.
496          */
497         kmemleak_not_leak(bd_mnt);
498         blockdev_superblock = bd_mnt->mnt_sb;   /* For writeback */
499 }
500
501 /*
502  * Most likely _very_ bad one - but then it's hardly critical for small
503  * /dev and can be fixed when somebody will need really large one.
504  * Keep in mind that it will be fed through icache hash function too.
505  */
506 static inline unsigned long hash(dev_t dev)
507 {
508         return MAJOR(dev)+MINOR(dev);
509 }
510
511 static int bdev_test(struct inode *inode, void *data)
512 {
513         return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
514 }
515
516 static int bdev_set(struct inode *inode, void *data)
517 {
518         BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
519         return 0;
520 }
521
522 static LIST_HEAD(all_bdevs);
523
524 struct block_device *bdget(dev_t dev)
525 {
526         struct block_device *bdev;
527         struct inode *inode;
528
529         inode = iget5_locked(blockdev_superblock, hash(dev),
530                         bdev_test, bdev_set, &dev);
531
532         if (!inode)
533                 return NULL;
534
535         bdev = &BDEV_I(inode)->bdev;
536
537         if (inode->i_state & I_NEW) {
538                 bdev->bd_contains = NULL;
539                 bdev->bd_inode = inode;
540                 bdev->bd_block_size = (1 << inode->i_blkbits);
541                 bdev->bd_part_count = 0;
542                 bdev->bd_invalidated = 0;
543                 inode->i_mode = S_IFBLK;
544                 inode->i_rdev = dev;
545                 inode->i_bdev = bdev;
546                 inode->i_data.a_ops = &def_blk_aops;
547                 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
548                 inode->i_data.backing_dev_info = &default_backing_dev_info;
549                 spin_lock(&bdev_lock);
550                 list_add(&bdev->bd_list, &all_bdevs);
551                 spin_unlock(&bdev_lock);
552                 unlock_new_inode(inode);
553         }
554         return bdev;
555 }
556
557 EXPORT_SYMBOL(bdget);
558
559 /**
560  * bdgrab -- Grab a reference to an already referenced block device
561  * @bdev:       Block device to grab a reference to.
562  */
563 struct block_device *bdgrab(struct block_device *bdev)
564 {
565         ihold(bdev->bd_inode);
566         return bdev;
567 }
568
569 long nr_blockdev_pages(void)
570 {
571         struct block_device *bdev;
572         long ret = 0;
573         spin_lock(&bdev_lock);
574         list_for_each_entry(bdev, &all_bdevs, bd_list) {
575                 ret += bdev->bd_inode->i_mapping->nrpages;
576         }
577         spin_unlock(&bdev_lock);
578         return ret;
579 }
580
581 void bdput(struct block_device *bdev)
582 {
583         iput(bdev->bd_inode);
584 }
585
586 EXPORT_SYMBOL(bdput);
587  
588 static struct block_device *bd_acquire(struct inode *inode)
589 {
590         struct block_device *bdev;
591
592         spin_lock(&bdev_lock);
593         bdev = inode->i_bdev;
594         if (bdev) {
595                 ihold(bdev->bd_inode);
596                 spin_unlock(&bdev_lock);
597                 return bdev;
598         }
599         spin_unlock(&bdev_lock);
600
601         bdev = bdget(inode->i_rdev);
602         if (bdev) {
603                 spin_lock(&bdev_lock);
604                 if (!inode->i_bdev) {
605                         /*
606                          * We take an additional reference to bd_inode,
607                          * and it's released in clear_inode() of inode.
608                          * So, we can access it via ->i_mapping always
609                          * without igrab().
610                          */
611                         ihold(bdev->bd_inode);
612                         inode->i_bdev = bdev;
613                         inode->i_mapping = bdev->bd_inode->i_mapping;
614                         list_add(&inode->i_devices, &bdev->bd_inodes);
615                 }
616                 spin_unlock(&bdev_lock);
617         }
618         return bdev;
619 }
620
621 /* Call when you free inode */
622
623 void bd_forget(struct inode *inode)
624 {
625         struct block_device *bdev = NULL;
626
627         spin_lock(&bdev_lock);
628         if (inode->i_bdev) {
629                 if (!sb_is_blkdev_sb(inode->i_sb))
630                         bdev = inode->i_bdev;
631                 __bd_forget(inode);
632         }
633         spin_unlock(&bdev_lock);
634
635         if (bdev)
636                 iput(bdev->bd_inode);
637 }
638
639 /**
640  * bd_may_claim - test whether a block device can be claimed
641  * @bdev: block device of interest
642  * @whole: whole block device containing @bdev, may equal @bdev
643  * @holder: holder trying to claim @bdev
644  *
645  * Test whther @bdev can be claimed by @holder.
646  *
647  * CONTEXT:
648  * spin_lock(&bdev_lock).
649  *
650  * RETURNS:
651  * %true if @bdev can be claimed, %false otherwise.
652  */
653 static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
654                          void *holder)
655 {
656         if (bdev->bd_holder == holder)
657                 return true;     /* already a holder */
658         else if (bdev->bd_holder != NULL)
659                 return false;    /* held by someone else */
660         else if (bdev->bd_contains == bdev)
661                 return true;     /* is a whole device which isn't held */
662
663         else if (whole->bd_holder == bd_claim)
664                 return true;     /* is a partition of a device that is being partitioned */
665         else if (whole->bd_holder != NULL)
666                 return false;    /* is a partition of a held device */
667         else
668                 return true;     /* is a partition of an un-held device */
669 }
670
671 /**
672  * bd_prepare_to_claim - prepare to claim a block device
673  * @bdev: block device of interest
674  * @whole: the whole device containing @bdev, may equal @bdev
675  * @holder: holder trying to claim @bdev
676  *
677  * Prepare to claim @bdev.  This function fails if @bdev is already
678  * claimed by another holder and waits if another claiming is in
679  * progress.  This function doesn't actually claim.  On successful
680  * return, the caller has ownership of bd_claiming and bd_holder[s].
681  *
682  * CONTEXT:
683  * spin_lock(&bdev_lock).  Might release bdev_lock, sleep and regrab
684  * it multiple times.
685  *
686  * RETURNS:
687  * 0 if @bdev can be claimed, -EBUSY otherwise.
688  */
689 static int bd_prepare_to_claim(struct block_device *bdev,
690                                struct block_device *whole, void *holder)
691 {
692 retry:
693         /* if someone else claimed, fail */
694         if (!bd_may_claim(bdev, whole, holder))
695                 return -EBUSY;
696
697         /* if claiming is already in progress, wait for it to finish */
698         if (whole->bd_claiming) {
699                 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
700                 DEFINE_WAIT(wait);
701
702                 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
703                 spin_unlock(&bdev_lock);
704                 schedule();
705                 finish_wait(wq, &wait);
706                 spin_lock(&bdev_lock);
707                 goto retry;
708         }
709
710         /* yay, all mine */
711         return 0;
712 }
713
714 /**
715  * bd_start_claiming - start claiming a block device
716  * @bdev: block device of interest
717  * @holder: holder trying to claim @bdev
718  *
719  * @bdev is about to be opened exclusively.  Check @bdev can be opened
720  * exclusively and mark that an exclusive open is in progress.  Each
721  * successful call to this function must be matched with a call to
722  * either bd_finish_claiming() or bd_abort_claiming() (which do not
723  * fail).
724  *
725  * This function is used to gain exclusive access to the block device
726  * without actually causing other exclusive open attempts to fail. It
727  * should be used when the open sequence itself requires exclusive
728  * access but may subsequently fail.
729  *
730  * CONTEXT:
731  * Might sleep.
732  *
733  * RETURNS:
734  * Pointer to the block device containing @bdev on success, ERR_PTR()
735  * value on failure.
736  */
737 static struct block_device *bd_start_claiming(struct block_device *bdev,
738                                               void *holder)
739 {
740         struct gendisk *disk;
741         struct block_device *whole;
742         int partno, err;
743
744         might_sleep();
745
746         /*
747          * @bdev might not have been initialized properly yet, look up
748          * and grab the outer block device the hard way.
749          */
750         disk = get_gendisk(bdev->bd_dev, &partno);
751         if (!disk)
752                 return ERR_PTR(-ENXIO);
753
754         whole = bdget_disk(disk, 0);
755         module_put(disk->fops->owner);
756         put_disk(disk);
757         if (!whole)
758                 return ERR_PTR(-ENOMEM);
759
760         /* prepare to claim, if successful, mark claiming in progress */
761         spin_lock(&bdev_lock);
762
763         err = bd_prepare_to_claim(bdev, whole, holder);
764         if (err == 0) {
765                 whole->bd_claiming = holder;
766                 spin_unlock(&bdev_lock);
767                 return whole;
768         } else {
769                 spin_unlock(&bdev_lock);
770                 bdput(whole);
771                 return ERR_PTR(err);
772         }
773 }
774
775 /* releases bdev_lock */
776 static void __bd_abort_claiming(struct block_device *whole, void *holder)
777 {
778         BUG_ON(whole->bd_claiming != holder);
779         whole->bd_claiming = NULL;
780         wake_up_bit(&whole->bd_claiming, 0);
781
782         spin_unlock(&bdev_lock);
783         bdput(whole);
784 }
785
786 /**
787  * bd_abort_claiming - abort claiming a block device
788  * @whole: whole block device returned by bd_start_claiming()
789  * @holder: holder trying to claim @bdev
790  *
791  * Abort a claiming block started by bd_start_claiming().  Note that
792  * @whole is not the block device to be claimed but the whole device
793  * returned by bd_start_claiming().
794  *
795  * CONTEXT:
796  * Grabs and releases bdev_lock.
797  */
798 static void bd_abort_claiming(struct block_device *whole, void *holder)
799 {
800         spin_lock(&bdev_lock);
801         __bd_abort_claiming(whole, holder);             /* releases bdev_lock */
802 }
803
804 /* increment holders when we have a legitimate claim. requires bdev_lock */
805 static void __bd_claim(struct block_device *bdev, struct block_device *whole,
806                                         void *holder)
807 {
808         /* note that for a whole device bd_holders
809          * will be incremented twice, and bd_holder will
810          * be set to bd_claim before being set to holder
811          */
812         whole->bd_holders++;
813         whole->bd_holder = bd_claim;
814         bdev->bd_holders++;
815         bdev->bd_holder = holder;
816 }
817
818 /**
819  * bd_finish_claiming - finish claiming a block device
820  * @bdev: block device of interest (passed to bd_start_claiming())
821  * @whole: whole block device returned by bd_start_claiming()
822  * @holder: holder trying to claim @bdev
823  *
824  * Finish a claiming block started by bd_start_claiming().
825  *
826  * CONTEXT:
827  * Grabs and releases bdev_lock.
828  */
829 static void bd_finish_claiming(struct block_device *bdev,
830                                 struct block_device *whole, void *holder)
831 {
832         spin_lock(&bdev_lock);
833         BUG_ON(!bd_may_claim(bdev, whole, holder));
834         __bd_claim(bdev, whole, holder);
835         __bd_abort_claiming(whole, holder); /* not actually an abort */
836 }
837
838 /**
839  * bd_claim - claim a block device
840  * @bdev: block device to claim
841  * @holder: holder trying to claim @bdev
842  *
843  * Try to claim @bdev which must have been opened successfully.
844  *
845  * CONTEXT:
846  * Might sleep.
847  *
848  * RETURNS:
849  * 0 if successful, -EBUSY if @bdev is already claimed.
850  */
851 int bd_claim(struct block_device *bdev, void *holder)
852 {
853         struct block_device *whole = bdev->bd_contains;
854         int res;
855
856         might_sleep();
857
858         spin_lock(&bdev_lock);
859         res = bd_prepare_to_claim(bdev, whole, holder);
860         if (res == 0)
861                 __bd_claim(bdev, whole, holder);
862         spin_unlock(&bdev_lock);
863
864         return res;
865 }
866 EXPORT_SYMBOL(bd_claim);
867
868 void bd_release(struct block_device *bdev)
869 {
870         spin_lock(&bdev_lock);
871         if (!--bdev->bd_contains->bd_holders)
872                 bdev->bd_contains->bd_holder = NULL;
873         if (!--bdev->bd_holders)
874                 bdev->bd_holder = NULL;
875         spin_unlock(&bdev_lock);
876 }
877
878 EXPORT_SYMBOL(bd_release);
879
880 #ifdef CONFIG_SYSFS
881 static int add_symlink(struct kobject *from, struct kobject *to)
882 {
883         return sysfs_create_link(from, to, kobject_name(to));
884 }
885
886 static void del_symlink(struct kobject *from, struct kobject *to)
887 {
888         sysfs_remove_link(from, kobject_name(to));
889 }
890
891 /**
892  * bd_link_disk_holder - create symlinks between holding disk and slave bdev
893  * @bdev: the claimed slave bdev
894  * @disk: the holding disk
895  *
896  * This functions creates the following sysfs symlinks.
897  *
898  * - from "slaves" directory of the holder @disk to the claimed @bdev
899  * - from "holders" directory of the @bdev to the holder @disk
900  *
901  * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
902  * passed to bd_link_disk_holder(), then:
903  *
904  *   /sys/block/dm-0/slaves/sda --> /sys/block/sda
905  *   /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
906  *
907  * The caller must have claimed @bdev before calling this function and
908  * ensure that both @bdev and @disk are valid during the creation and
909  * lifetime of these symlinks.
910  *
911  * CONTEXT:
912  * Might sleep.
913  *
914  * RETURNS:
915  * 0 on success, -errno on failure.
916  */
917 int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
918 {
919         int ret = 0;
920
921         mutex_lock(&bdev->bd_mutex);
922
923         WARN_ON_ONCE(!bdev->bd_holder || bdev->bd_holder_disk);
924
925         /* FIXME: remove the following once add_disk() handles errors */
926         if (WARN_ON(!disk->slave_dir || !bdev->bd_part->holder_dir))
927                 goto out_unlock;
928
929         ret = add_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
930         if (ret)
931                 goto out_unlock;
932
933         ret = add_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj);
934         if (ret) {
935                 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
936                 goto out_unlock;
937         }
938
939         bdev->bd_holder_disk = disk;
940 out_unlock:
941         mutex_unlock(&bdev->bd_mutex);
942         return ret;
943 }
944 EXPORT_SYMBOL_GPL(bd_link_disk_holder);
945
946 void bd_unlink_disk_holder(struct block_device *bdev)
947 {
948         struct gendisk *disk = bdev->bd_holder_disk;
949
950         bdev->bd_holder_disk = NULL;
951         if (!disk)
952                 return;
953
954         del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
955         del_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj);
956 }
957 EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
958 #endif
959
960 /*
961  * Tries to open block device by device number.  Use it ONLY if you
962  * really do not have anything better - i.e. when you are behind a
963  * truly sucky interface and all you are given is a device number.  _Never_
964  * to be used for internal purposes.  If you ever need it - reconsider
965  * your API.
966  */
967 struct block_device *open_by_devnum(dev_t dev, fmode_t mode)
968 {
969         struct block_device *bdev = bdget(dev);
970         int err = -ENOMEM;
971         if (bdev)
972                 err = blkdev_get(bdev, mode);
973         return err ? ERR_PTR(err) : bdev;
974 }
975
976 EXPORT_SYMBOL(open_by_devnum);
977
978 /**
979  * flush_disk - invalidates all buffer-cache entries on a disk
980  *
981  * @bdev:      struct block device to be flushed
982  *
983  * Invalidates all buffer-cache entries on a disk. It should be called
984  * when a disk has been changed -- either by a media change or online
985  * resize.
986  */
987 static void flush_disk(struct block_device *bdev)
988 {
989         if (__invalidate_device(bdev)) {
990                 char name[BDEVNAME_SIZE] = "";
991
992                 if (bdev->bd_disk)
993                         disk_name(bdev->bd_disk, 0, name);
994                 printk(KERN_WARNING "VFS: busy inodes on changed media or "
995                        "resized disk %s\n", name);
996         }
997
998         if (!bdev->bd_disk)
999                 return;
1000         if (disk_partitionable(bdev->bd_disk))
1001                 bdev->bd_invalidated = 1;
1002 }
1003
1004 /**
1005  * check_disk_size_change - checks for disk size change and adjusts bdev size.
1006  * @disk: struct gendisk to check
1007  * @bdev: struct bdev to adjust.
1008  *
1009  * This routine checks to see if the bdev size does not match the disk size
1010  * and adjusts it if it differs.
1011  */
1012 void check_disk_size_change(struct gendisk *disk, struct block_device *bdev)
1013 {
1014         loff_t disk_size, bdev_size;
1015
1016         disk_size = (loff_t)get_capacity(disk) << 9;
1017         bdev_size = i_size_read(bdev->bd_inode);
1018         if (disk_size != bdev_size) {
1019                 char name[BDEVNAME_SIZE];
1020
1021                 disk_name(disk, 0, name);
1022                 printk(KERN_INFO
1023                        "%s: detected capacity change from %lld to %lld\n",
1024                        name, bdev_size, disk_size);
1025                 i_size_write(bdev->bd_inode, disk_size);
1026                 flush_disk(bdev);
1027         }
1028 }
1029 EXPORT_SYMBOL(check_disk_size_change);
1030
1031 /**
1032  * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back
1033  * @disk: struct gendisk to be revalidated
1034  *
1035  * This routine is a wrapper for lower-level driver's revalidate_disk
1036  * call-backs.  It is used to do common pre and post operations needed
1037  * for all revalidate_disk operations.
1038  */
1039 int revalidate_disk(struct gendisk *disk)
1040 {
1041         struct block_device *bdev;
1042         int ret = 0;
1043
1044         if (disk->fops->revalidate_disk)
1045                 ret = disk->fops->revalidate_disk(disk);
1046
1047         bdev = bdget_disk(disk, 0);
1048         if (!bdev)
1049                 return ret;
1050
1051         mutex_lock(&bdev->bd_mutex);
1052         check_disk_size_change(disk, bdev);
1053         mutex_unlock(&bdev->bd_mutex);
1054         bdput(bdev);
1055         return ret;
1056 }
1057 EXPORT_SYMBOL(revalidate_disk);
1058
1059 /*
1060  * This routine checks whether a removable media has been changed,
1061  * and invalidates all buffer-cache-entries in that case. This
1062  * is a relatively slow routine, so we have to try to minimize using
1063  * it. Thus it is called only upon a 'mount' or 'open'. This
1064  * is the best way of combining speed and utility, I think.
1065  * People changing diskettes in the middle of an operation deserve
1066  * to lose :-)
1067  */
1068 int check_disk_change(struct block_device *bdev)
1069 {
1070         struct gendisk *disk = bdev->bd_disk;
1071         const struct block_device_operations *bdops = disk->fops;
1072
1073         if (!bdops->media_changed)
1074                 return 0;
1075         if (!bdops->media_changed(bdev->bd_disk))
1076                 return 0;
1077
1078         flush_disk(bdev);
1079         if (bdops->revalidate_disk)
1080                 bdops->revalidate_disk(bdev->bd_disk);
1081         return 1;
1082 }
1083
1084 EXPORT_SYMBOL(check_disk_change);
1085
1086 void bd_set_size(struct block_device *bdev, loff_t size)
1087 {
1088         unsigned bsize = bdev_logical_block_size(bdev);
1089
1090         bdev->bd_inode->i_size = size;
1091         while (bsize < PAGE_CACHE_SIZE) {
1092                 if (size & bsize)
1093                         break;
1094                 bsize <<= 1;
1095         }
1096         bdev->bd_block_size = bsize;
1097         bdev->bd_inode->i_blkbits = blksize_bits(bsize);
1098 }
1099 EXPORT_SYMBOL(bd_set_size);
1100
1101 static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
1102
1103 /*
1104  * bd_mutex locking:
1105  *
1106  *  mutex_lock(part->bd_mutex)
1107  *    mutex_lock_nested(whole->bd_mutex, 1)
1108  */
1109
1110 static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
1111 {
1112         struct gendisk *disk;
1113         int ret;
1114         int partno;
1115         int perm = 0;
1116
1117         if (mode & FMODE_READ)
1118                 perm |= MAY_READ;
1119         if (mode & FMODE_WRITE)
1120                 perm |= MAY_WRITE;
1121         /*
1122          * hooks: /n/, see "layering violations".
1123          */
1124         if (!for_part) {
1125                 ret = devcgroup_inode_permission(bdev->bd_inode, perm);
1126                 if (ret != 0) {
1127                         bdput(bdev);
1128                         return ret;
1129                 }
1130         }
1131
1132  restart:
1133
1134         ret = -ENXIO;
1135         disk = get_gendisk(bdev->bd_dev, &partno);
1136         if (!disk)
1137                 goto out;
1138
1139         mutex_lock_nested(&bdev->bd_mutex, for_part);
1140         if (!bdev->bd_openers) {
1141                 bdev->bd_disk = disk;
1142                 bdev->bd_contains = bdev;
1143                 if (!partno) {
1144                         struct backing_dev_info *bdi;
1145
1146                         ret = -ENXIO;
1147                         bdev->bd_part = disk_get_part(disk, partno);
1148                         if (!bdev->bd_part)
1149                                 goto out_clear;
1150
1151                         if (disk->fops->open) {
1152                                 ret = disk->fops->open(bdev, mode);
1153                                 if (ret == -ERESTARTSYS) {
1154                                         /* Lost a race with 'disk' being
1155                                          * deleted, try again.
1156                                          * See md.c
1157                                          */
1158                                         disk_put_part(bdev->bd_part);
1159                                         bdev->bd_part = NULL;
1160                                         module_put(disk->fops->owner);
1161                                         put_disk(disk);
1162                                         bdev->bd_disk = NULL;
1163                                         mutex_unlock(&bdev->bd_mutex);
1164                                         goto restart;
1165                                 }
1166                                 if (ret)
1167                                         goto out_clear;
1168                         }
1169                         if (!bdev->bd_openers) {
1170                                 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
1171                                 bdi = blk_get_backing_dev_info(bdev);
1172                                 if (bdi == NULL)
1173                                         bdi = &default_backing_dev_info;
1174                                 bdev_inode_switch_bdi(bdev->bd_inode, bdi);
1175                         }
1176                         if (bdev->bd_invalidated)
1177                                 rescan_partitions(disk, bdev);
1178                 } else {
1179                         struct block_device *whole;
1180                         whole = bdget_disk(disk, 0);
1181                         ret = -ENOMEM;
1182                         if (!whole)
1183                                 goto out_clear;
1184                         BUG_ON(for_part);
1185                         ret = __blkdev_get(whole, mode, 1);
1186                         if (ret)
1187                                 goto out_clear;
1188                         bdev->bd_contains = whole;
1189                         bdev_inode_switch_bdi(bdev->bd_inode,
1190                                 whole->bd_inode->i_data.backing_dev_info);
1191                         bdev->bd_part = disk_get_part(disk, partno);
1192                         if (!(disk->flags & GENHD_FL_UP) ||
1193                             !bdev->bd_part || !bdev->bd_part->nr_sects) {
1194                                 ret = -ENXIO;
1195                                 goto out_clear;
1196                         }
1197                         bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9);
1198                 }
1199         } else {
1200                 module_put(disk->fops->owner);
1201                 put_disk(disk);
1202                 disk = NULL;
1203                 if (bdev->bd_contains == bdev) {
1204                         if (bdev->bd_disk->fops->open) {
1205                                 ret = bdev->bd_disk->fops->open(bdev, mode);
1206                                 if (ret)
1207                                         goto out_unlock_bdev;
1208                         }
1209                         if (bdev->bd_invalidated)
1210                                 rescan_partitions(bdev->bd_disk, bdev);
1211                 }
1212         }
1213         bdev->bd_openers++;
1214         if (for_part)
1215                 bdev->bd_part_count++;
1216         mutex_unlock(&bdev->bd_mutex);
1217         return 0;
1218
1219  out_clear:
1220         disk_put_part(bdev->bd_part);
1221         bdev->bd_disk = NULL;
1222         bdev->bd_part = NULL;
1223         bdev_inode_switch_bdi(bdev->bd_inode, &default_backing_dev_info);
1224         if (bdev != bdev->bd_contains)
1225                 __blkdev_put(bdev->bd_contains, mode, 1);
1226         bdev->bd_contains = NULL;
1227  out_unlock_bdev:
1228         mutex_unlock(&bdev->bd_mutex);
1229  out:
1230         if (disk)
1231                 module_put(disk->fops->owner);
1232         put_disk(disk);
1233         bdput(bdev);
1234
1235         return ret;
1236 }
1237
1238 int blkdev_get(struct block_device *bdev, fmode_t mode)
1239 {
1240         return __blkdev_get(bdev, mode, 0);
1241 }
1242 EXPORT_SYMBOL(blkdev_get);
1243
1244 static int blkdev_open(struct inode * inode, struct file * filp)
1245 {
1246         struct block_device *whole = NULL;
1247         struct block_device *bdev;
1248         int res;
1249
1250         /*
1251          * Preserve backwards compatibility and allow large file access
1252          * even if userspace doesn't ask for it explicitly. Some mkfs
1253          * binary needs it. We might want to drop this workaround
1254          * during an unstable branch.
1255          */
1256         filp->f_flags |= O_LARGEFILE;
1257
1258         if (filp->f_flags & O_NDELAY)
1259                 filp->f_mode |= FMODE_NDELAY;
1260         if (filp->f_flags & O_EXCL)
1261                 filp->f_mode |= FMODE_EXCL;
1262         if ((filp->f_flags & O_ACCMODE) == 3)
1263                 filp->f_mode |= FMODE_WRITE_IOCTL;
1264
1265         bdev = bd_acquire(inode);
1266         if (bdev == NULL)
1267                 return -ENOMEM;
1268
1269         if (filp->f_mode & FMODE_EXCL) {
1270                 whole = bd_start_claiming(bdev, filp);
1271                 if (IS_ERR(whole)) {
1272                         bdput(bdev);
1273                         return PTR_ERR(whole);
1274                 }
1275         }
1276
1277         filp->f_mapping = bdev->bd_inode->i_mapping;
1278
1279         res = blkdev_get(bdev, filp->f_mode);
1280
1281         if (whole) {
1282                 if (res == 0)
1283                         bd_finish_claiming(bdev, whole, filp);
1284                 else
1285                         bd_abort_claiming(whole, filp);
1286         }
1287
1288         return res;
1289 }
1290
1291 static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
1292 {
1293         int ret = 0;
1294         struct gendisk *disk = bdev->bd_disk;
1295         struct block_device *victim = NULL;
1296
1297         mutex_lock_nested(&bdev->bd_mutex, for_part);
1298         if (for_part)
1299                 bdev->bd_part_count--;
1300
1301         if (!--bdev->bd_openers) {
1302                 sync_blockdev(bdev);
1303                 kill_bdev(bdev);
1304         }
1305         if (bdev->bd_contains == bdev) {
1306                 if (disk->fops->release)
1307                         ret = disk->fops->release(disk, mode);
1308         }
1309         if (!bdev->bd_openers) {
1310                 struct module *owner = disk->fops->owner;
1311
1312                 put_disk(disk);
1313                 module_put(owner);
1314                 disk_put_part(bdev->bd_part);
1315                 bdev->bd_part = NULL;
1316                 bdev->bd_disk = NULL;
1317                 bdev_inode_switch_bdi(bdev->bd_inode,
1318                                         &default_backing_dev_info);
1319                 if (bdev != bdev->bd_contains)
1320                         victim = bdev->bd_contains;
1321                 bdev->bd_contains = NULL;
1322         }
1323         mutex_unlock(&bdev->bd_mutex);
1324         bdput(bdev);
1325         if (victim)
1326                 __blkdev_put(victim, mode, 1);
1327         return ret;
1328 }
1329
1330 int blkdev_put(struct block_device *bdev, fmode_t mode)
1331 {
1332         return __blkdev_put(bdev, mode, 0);
1333 }
1334 EXPORT_SYMBOL(blkdev_put);
1335
1336 static int blkdev_close(struct inode * inode, struct file * filp)
1337 {
1338         struct block_device *bdev = I_BDEV(filp->f_mapping->host);
1339         if (bdev->bd_holder == filp)
1340                 bd_release(bdev);
1341         return blkdev_put(bdev, filp->f_mode);
1342 }
1343
1344 static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1345 {
1346         struct block_device *bdev = I_BDEV(file->f_mapping->host);
1347         fmode_t mode = file->f_mode;
1348
1349         /*
1350          * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
1351          * to updated it before every ioctl.
1352          */
1353         if (file->f_flags & O_NDELAY)
1354                 mode |= FMODE_NDELAY;
1355         else
1356                 mode &= ~FMODE_NDELAY;
1357
1358         return blkdev_ioctl(bdev, mode, cmd, arg);
1359 }
1360
1361 /*
1362  * Write data to the block device.  Only intended for the block device itself
1363  * and the raw driver which basically is a fake block device.
1364  *
1365  * Does not take i_mutex for the write and thus is not for general purpose
1366  * use.
1367  */
1368 ssize_t blkdev_aio_write(struct kiocb *iocb, const struct iovec *iov,
1369                          unsigned long nr_segs, loff_t pos)
1370 {
1371         struct file *file = iocb->ki_filp;
1372         ssize_t ret;
1373
1374         BUG_ON(iocb->ki_pos != pos);
1375
1376         ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos);
1377         if (ret > 0 || ret == -EIOCBQUEUED) {
1378                 ssize_t err;
1379
1380                 err = generic_write_sync(file, pos, ret);
1381                 if (err < 0 && ret > 0)
1382                         ret = err;
1383         }
1384         return ret;
1385 }
1386 EXPORT_SYMBOL_GPL(blkdev_aio_write);
1387
1388 /*
1389  * Try to release a page associated with block device when the system
1390  * is under memory pressure.
1391  */
1392 static int blkdev_releasepage(struct page *page, gfp_t wait)
1393 {
1394         struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
1395
1396         if (super && super->s_op->bdev_try_to_free_page)
1397                 return super->s_op->bdev_try_to_free_page(super, page, wait);
1398
1399         return try_to_free_buffers(page);
1400 }
1401
1402 static const struct address_space_operations def_blk_aops = {
1403         .readpage       = blkdev_readpage,
1404         .writepage      = blkdev_writepage,
1405         .sync_page      = block_sync_page,
1406         .write_begin    = blkdev_write_begin,
1407         .write_end      = blkdev_write_end,
1408         .writepages     = generic_writepages,
1409         .releasepage    = blkdev_releasepage,
1410         .direct_IO      = blkdev_direct_IO,
1411 };
1412
1413 const struct file_operations def_blk_fops = {
1414         .open           = blkdev_open,
1415         .release        = blkdev_close,
1416         .llseek         = block_llseek,
1417         .read           = do_sync_read,
1418         .write          = do_sync_write,
1419         .aio_read       = generic_file_aio_read,
1420         .aio_write      = blkdev_aio_write,
1421         .mmap           = generic_file_mmap,
1422         .fsync          = blkdev_fsync,
1423         .unlocked_ioctl = block_ioctl,
1424 #ifdef CONFIG_COMPAT
1425         .compat_ioctl   = compat_blkdev_ioctl,
1426 #endif
1427         .splice_read    = generic_file_splice_read,
1428         .splice_write   = generic_file_splice_write,
1429 };
1430
1431 int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
1432 {
1433         int res;
1434         mm_segment_t old_fs = get_fs();
1435         set_fs(KERNEL_DS);
1436         res = blkdev_ioctl(bdev, 0, cmd, arg);
1437         set_fs(old_fs);
1438         return res;
1439 }
1440
1441 EXPORT_SYMBOL(ioctl_by_bdev);
1442
1443 /**
1444  * lookup_bdev  - lookup a struct block_device by name
1445  * @pathname:   special file representing the block device
1446  *
1447  * Get a reference to the blockdevice at @pathname in the current
1448  * namespace if possible and return it.  Return ERR_PTR(error)
1449  * otherwise.
1450  */
1451 struct block_device *lookup_bdev(const char *pathname)
1452 {
1453         struct block_device *bdev;
1454         struct inode *inode;
1455         struct path path;
1456         int error;
1457
1458         if (!pathname || !*pathname)
1459                 return ERR_PTR(-EINVAL);
1460
1461         error = kern_path(pathname, LOOKUP_FOLLOW, &path);
1462         if (error)
1463                 return ERR_PTR(error);
1464
1465         inode = path.dentry->d_inode;
1466         error = -ENOTBLK;
1467         if (!S_ISBLK(inode->i_mode))
1468                 goto fail;
1469         error = -EACCES;
1470         if (path.mnt->mnt_flags & MNT_NODEV)
1471                 goto fail;
1472         error = -ENOMEM;
1473         bdev = bd_acquire(inode);
1474         if (!bdev)
1475                 goto fail;
1476 out:
1477         path_put(&path);
1478         return bdev;
1479 fail:
1480         bdev = ERR_PTR(error);
1481         goto out;
1482 }
1483 EXPORT_SYMBOL(lookup_bdev);
1484
1485 /**
1486  * open_bdev_exclusive  -  open a block device by name and set it up for use
1487  *
1488  * @path:       special file representing the block device
1489  * @mode:       FMODE_... combination to pass be used
1490  * @holder:     owner for exclusion
1491  *
1492  * Open the blockdevice described by the special file at @path, claim it
1493  * for the @holder.
1494  */
1495 struct block_device *open_bdev_exclusive(const char *path, fmode_t mode, void *holder)
1496 {
1497         struct block_device *bdev, *whole;
1498         int error;
1499
1500         bdev = lookup_bdev(path);
1501         if (IS_ERR(bdev))
1502                 return bdev;
1503
1504         whole = bd_start_claiming(bdev, holder);
1505         if (IS_ERR(whole)) {
1506                 bdput(bdev);
1507                 return whole;
1508         }
1509
1510         error = blkdev_get(bdev, mode);
1511         if (error)
1512                 goto out_abort_claiming;
1513
1514         error = -EACCES;
1515         if ((mode & FMODE_WRITE) && bdev_read_only(bdev))
1516                 goto out_blkdev_put;
1517
1518         bd_finish_claiming(bdev, whole, holder);
1519         return bdev;
1520
1521 out_blkdev_put:
1522         blkdev_put(bdev, mode);
1523 out_abort_claiming:
1524         bd_abort_claiming(whole, holder);
1525         return ERR_PTR(error);
1526 }
1527
1528 EXPORT_SYMBOL(open_bdev_exclusive);
1529
1530 /**
1531  * close_bdev_exclusive  -  close a blockdevice opened by open_bdev_exclusive()
1532  *
1533  * @bdev:       blockdevice to close
1534  * @mode:       mode, must match that used to open.
1535  *
1536  * This is the counterpart to open_bdev_exclusive().
1537  */
1538 void close_bdev_exclusive(struct block_device *bdev, fmode_t mode)
1539 {
1540         bd_release(bdev);
1541         blkdev_put(bdev, mode);
1542 }
1543
1544 EXPORT_SYMBOL(close_bdev_exclusive);
1545
1546 int __invalidate_device(struct block_device *bdev)
1547 {
1548         struct super_block *sb = get_super(bdev);
1549         int res = 0;
1550
1551         if (sb) {
1552                 /*
1553                  * no need to lock the super, get_super holds the
1554                  * read mutex so the filesystem cannot go away
1555                  * under us (->put_super runs with the write lock
1556                  * hold).
1557                  */
1558                 shrink_dcache_sb(sb);
1559                 res = invalidate_inodes(sb);
1560                 drop_super(sb);
1561         }
1562         invalidate_bdev(bdev);
1563         return res;
1564 }
1565 EXPORT_SYMBOL(__invalidate_device);