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