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