blob: 8b18e43b82fe8e156bab431070a882ea5cb9a5bf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#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>
David Howells811d7362006-08-29 19:06:09 +010020#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/mpage.h>
22#include <linux/mount.h>
23#include <linux/uio.h>
24#include <linux/namei.h>
25#include <asm/uaccess.h>
David Howells07f3f052006-09-30 20:52:18 +020026#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
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{
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 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;
Coywolf Qi Hunt38885bd2006-03-24 03:18:05 -080094 sb->s_blocksize_bits = blksize_bits(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 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
Chen, Kenneth We61c9012006-12-13 00:34:36 -0800132static int blk_end_aio(struct bio *bio, unsigned int bytes_done, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Chen, Kenneth We61c9012006-12-13 00:34:36 -0800134 struct kiocb *iocb = bio->bi_private;
135 atomic_t *bio_count = &iocb->ki_bio_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Chen, Kenneth We61c9012006-12-13 00:34:36 -0800137 if (bio_data_dir(bio) == READ)
138 bio_check_pages_dirty(bio);
139 else {
140 bio_release_pages(bio);
141 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
143
Chen, Kenneth We61c9012006-12-13 00:34:36 -0800144 /* 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)) {
149 if (iocb->ki_nbytes < 0)
150 aio_complete(iocb, iocb->ki_nbytes, 0);
151 else
152 aio_complete(iocb, iocb->ki_left, 0);
153 }
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return 0;
156}
157
Chen, Kenneth We61c9012006-12-13 00:34:36 -0800158#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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193static ssize_t
194blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
Chen, Kenneth We61c9012006-12-13 00:34:36 -0800195 loff_t pos, unsigned long nr_segs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Chen, Kenneth We61c9012006-12-13 00:34:36 -0800197 struct inode *inode = iocb->ki_filp->f_mapping->host;
198 unsigned blkbits = blksize_bits(bdev_hardsect_size(I_BDEV(inode)));
199 unsigned blocksize_mask = (1 << blkbits) - 1;
200 unsigned long seg = 0; /* iov segment iterator */
201 unsigned long nvec; /* number of bio vec needed */
202 unsigned long cur_off; /* offset into current page */
203 unsigned long cur_len; /* I/O len of current page, up to PAGE_SIZE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Chen, Kenneth We61c9012006-12-13 00:34:36 -0800205 unsigned long addr; /* user iovec address */
206 size_t count; /* user iovec len */
207 size_t nbytes = iocb->ki_nbytes = iocb->ki_left; /* total xfer size */
208 loff_t size; /* size of block device */
209 struct bio *bio;
210 atomic_t *bio_count = &iocb->ki_bio_count;
211 struct page *page;
212 struct pvec pvec;
213
214 pvec.nr = 0;
215 pvec.idx = 0;
216
217 if (pos & blocksize_mask)
218 return -EINVAL;
219
220 size = i_size_read(inode);
221 if (pos + nbytes > size) {
222 nbytes = size - pos;
223 iocb->ki_left = nbytes;
224 }
225
226 /*
227 * check first non-zero iov alignment, the remaining
228 * iov alignment is checked inside bio loop below.
229 */
230 do {
231 addr = (unsigned long) iov[seg].iov_base;
232 count = min(iov[seg].iov_len, nbytes);
233 if (addr & blocksize_mask || count & blocksize_mask)
234 return -EINVAL;
235 } while (!count && ++seg < nr_segs);
236 atomic_set(bio_count, 1);
237
238 while (nbytes) {
239 /* roughly estimate number of bio vec needed */
240 nvec = (nbytes + PAGE_SIZE - 1) / PAGE_SIZE;
241 nvec = max(nvec, nr_segs - seg);
242 nvec = min(nvec, (unsigned long) BIO_MAX_PAGES);
243
244 /* bio_alloc should not fail with GFP_KERNEL flag */
245 bio = bio_alloc(GFP_KERNEL, nvec);
246 bio->bi_bdev = I_BDEV(inode);
247 bio->bi_end_io = blk_end_aio;
248 bio->bi_private = iocb;
249 bio->bi_sector = pos >> blkbits;
250same_bio:
251 cur_off = addr & ~PAGE_MASK;
252 cur_len = PAGE_SIZE - cur_off;
253 if (count < cur_len)
254 cur_len = count;
255
256 page = blk_get_page(addr, count, rw, &pvec);
257 if (unlikely(IS_ERR(page)))
258 goto backout;
259
260 if (bio_add_page(bio, page, cur_len, cur_off)) {
261 pos += cur_len;
262 addr += cur_len;
263 count -= cur_len;
264 nbytes -= cur_len;
265
266 if (count)
267 goto same_bio;
268 while (++seg < nr_segs) {
269 addr = (unsigned long) iov[seg].iov_base;
270 count = iov[seg].iov_len;
271 if (!count)
272 continue;
273 if (unlikely(addr & blocksize_mask ||
274 count & blocksize_mask)) {
275 page = ERR_PTR(-EINVAL);
276 goto backout;
277 }
278 count = min(count, nbytes);
279 goto same_bio;
280 }
281 }
282
283 /* bio is ready, submit it */
284 if (rw == READ)
285 bio_set_pages_dirty(bio);
286 atomic_inc(bio_count);
287 submit_bio(rw, bio);
288 }
289
290completion:
291 iocb->ki_left -= nbytes;
292 nbytes = iocb->ki_left;
293 iocb->ki_pos += nbytes;
294
295 blk_run_address_space(inode->i_mapping);
296 if (atomic_dec_and_test(bio_count))
297 aio_complete(iocb, nbytes, 0);
298
299 return -EIOCBQUEUED;
300
301backout:
302 /*
303 * back out nbytes count constructed so far for this bio,
304 * we will throw away current bio.
305 */
306 nbytes += bio->bi_size;
307 bio_release_pages(bio);
308 bio_put(bio);
309
310 /*
311 * if no bio was submmitted, return the error code.
312 * otherwise, proceed with pending I/O completion.
313 */
314 if (atomic_read(bio_count) == 1)
315 return PTR_ERR(page);
316 goto completion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
319static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
320{
321 return block_write_full_page(page, blkdev_get_block, wbc);
322}
323
324static int blkdev_readpage(struct file * file, struct page * page)
325{
326 return block_read_full_page(page, blkdev_get_block);
327}
328
329static int blkdev_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
330{
331 return block_prepare_write(page, from, to, blkdev_get_block);
332}
333
334static int blkdev_commit_write(struct file *file, struct page *page, unsigned from, unsigned to)
335{
336 return block_commit_write(page, from, to);
337}
338
339/*
340 * private llseek:
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800341 * for a block special file file->f_path.dentry->d_inode->i_size is zero
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 * so we compute the size by hand (just as in block_read/write above)
343 */
344static loff_t block_llseek(struct file *file, loff_t offset, int origin)
345{
346 struct inode *bd_inode = file->f_mapping->host;
347 loff_t size;
348 loff_t retval;
349
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800350 mutex_lock(&bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 size = i_size_read(bd_inode);
352
353 switch (origin) {
354 case 2:
355 offset += size;
356 break;
357 case 1:
358 offset += file->f_pos;
359 }
360 retval = -EINVAL;
361 if (offset >= 0 && offset <= size) {
362 if (offset != file->f_pos) {
363 file->f_pos = offset;
364 }
365 retval = offset;
366 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800367 mutex_unlock(&bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return retval;
369}
370
371/*
372 * Filp is never NULL; the only case when ->fsync() is called with
373 * NULL first argument is nfsd_sync_dir() and that's not a directory.
374 */
375
376static int block_fsync(struct file *filp, struct dentry *dentry, int datasync)
377{
378 return sync_blockdev(I_BDEV(filp->f_mapping->host));
379}
380
381/*
382 * pseudo-fs
383 */
384
385static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
Christoph Lametere18b8902006-12-06 20:33:20 -0800386static struct kmem_cache * bdev_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388static struct inode *bdev_alloc_inode(struct super_block *sb)
389{
Christoph Lametere94b1762006-12-06 20:33:17 -0800390 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (!ei)
392 return NULL;
393 return &ei->vfs_inode;
394}
395
396static void bdev_destroy_inode(struct inode *inode)
397{
398 struct bdev_inode *bdi = BDEV_I(inode);
399
400 bdi->bdev.bd_inode_backing_dev_info = NULL;
401 kmem_cache_free(bdev_cachep, bdi);
402}
403
Christoph Lametere18b8902006-12-06 20:33:20 -0800404static void init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 struct bdev_inode *ei = (struct bdev_inode *) foo;
407 struct block_device *bdev = &ei->bdev;
408
409 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
410 SLAB_CTOR_CONSTRUCTOR)
411 {
412 memset(bdev, 0, sizeof(*bdev));
Arjan van de Venc039e312006-03-23 03:00:28 -0800413 mutex_init(&bdev->bd_mutex);
David Chinnerf73ca1b2007-01-10 23:15:41 -0800414 sema_init(&bdev->bd_mount_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 INIT_LIST_HEAD(&bdev->bd_inodes);
416 INIT_LIST_HEAD(&bdev->bd_list);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800417#ifdef CONFIG_SYSFS
418 INIT_LIST_HEAD(&bdev->bd_holder_list);
419#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 inode_init_once(&ei->vfs_inode);
421 }
422}
423
424static inline void __bd_forget(struct inode *inode)
425{
426 list_del_init(&inode->i_devices);
427 inode->i_bdev = NULL;
428 inode->i_mapping = &inode->i_data;
429}
430
431static void bdev_clear_inode(struct inode *inode)
432{
433 struct block_device *bdev = &BDEV_I(inode)->bdev;
434 struct list_head *p;
435 spin_lock(&bdev_lock);
436 while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
437 __bd_forget(list_entry(p, struct inode, i_devices));
438 }
439 list_del_init(&bdev->bd_list);
440 spin_unlock(&bdev_lock);
441}
442
443static struct super_operations bdev_sops = {
444 .statfs = simple_statfs,
445 .alloc_inode = bdev_alloc_inode,
446 .destroy_inode = bdev_destroy_inode,
447 .drop_inode = generic_delete_inode,
448 .clear_inode = bdev_clear_inode,
449};
450
David Howells454e2392006-06-23 02:02:57 -0700451static int bd_get_sb(struct file_system_type *fs_type,
452 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
David Howells454e2392006-06-23 02:02:57 -0700454 return get_sb_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457static struct file_system_type bd_type = {
458 .name = "bdev",
459 .get_sb = bd_get_sb,
460 .kill_sb = kill_anon_super,
461};
462
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800463static struct vfsmount *bd_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464struct super_block *blockdev_superblock;
465
466void __init bdev_cache_init(void)
467{
468 int err;
469 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800470 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
471 SLAB_MEM_SPREAD|SLAB_PANIC),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 init_once, NULL);
473 err = register_filesystem(&bd_type);
474 if (err)
475 panic("Cannot register bdev pseudo-fs");
476 bd_mnt = kern_mount(&bd_type);
477 err = PTR_ERR(bd_mnt);
478 if (IS_ERR(bd_mnt))
479 panic("Cannot create bdev pseudo-fs");
480 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
481}
482
483/*
484 * Most likely _very_ bad one - but then it's hardly critical for small
485 * /dev and can be fixed when somebody will need really large one.
486 * Keep in mind that it will be fed through icache hash function too.
487 */
488static inline unsigned long hash(dev_t dev)
489{
490 return MAJOR(dev)+MINOR(dev);
491}
492
493static int bdev_test(struct inode *inode, void *data)
494{
495 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
496}
497
498static int bdev_set(struct inode *inode, void *data)
499{
500 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
501 return 0;
502}
503
504static LIST_HEAD(all_bdevs);
505
506struct block_device *bdget(dev_t dev)
507{
508 struct block_device *bdev;
509 struct inode *inode;
510
511 inode = iget5_locked(bd_mnt->mnt_sb, hash(dev),
512 bdev_test, bdev_set, &dev);
513
514 if (!inode)
515 return NULL;
516
517 bdev = &BDEV_I(inode)->bdev;
518
519 if (inode->i_state & I_NEW) {
520 bdev->bd_contains = NULL;
521 bdev->bd_inode = inode;
522 bdev->bd_block_size = (1 << inode->i_blkbits);
523 bdev->bd_part_count = 0;
524 bdev->bd_invalidated = 0;
525 inode->i_mode = S_IFBLK;
526 inode->i_rdev = dev;
527 inode->i_bdev = bdev;
528 inode->i_data.a_ops = &def_blk_aops;
529 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
530 inode->i_data.backing_dev_info = &default_backing_dev_info;
531 spin_lock(&bdev_lock);
532 list_add(&bdev->bd_list, &all_bdevs);
533 spin_unlock(&bdev_lock);
534 unlock_new_inode(inode);
535 }
536 return bdev;
537}
538
539EXPORT_SYMBOL(bdget);
540
541long nr_blockdev_pages(void)
542{
543 struct list_head *p;
544 long ret = 0;
545 spin_lock(&bdev_lock);
546 list_for_each(p, &all_bdevs) {
547 struct block_device *bdev;
548 bdev = list_entry(p, struct block_device, bd_list);
549 ret += bdev->bd_inode->i_mapping->nrpages;
550 }
551 spin_unlock(&bdev_lock);
552 return ret;
553}
554
555void bdput(struct block_device *bdev)
556{
557 iput(bdev->bd_inode);
558}
559
560EXPORT_SYMBOL(bdput);
561
562static struct block_device *bd_acquire(struct inode *inode)
563{
564 struct block_device *bdev;
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 spin_lock(&bdev_lock);
567 bdev = inode->i_bdev;
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700568 if (bdev) {
569 atomic_inc(&bdev->bd_inode->i_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 spin_unlock(&bdev_lock);
571 return bdev;
572 }
573 spin_unlock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 bdev = bdget(inode->i_rdev);
576 if (bdev) {
577 spin_lock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700578 if (!inode->i_bdev) {
579 /*
580 * We take an additional bd_inode->i_count for inode,
581 * and it's released in clear_inode() of inode.
582 * So, we can access it via ->i_mapping always
583 * without igrab().
584 */
585 atomic_inc(&bdev->bd_inode->i_count);
586 inode->i_bdev = bdev;
587 inode->i_mapping = bdev->bd_inode->i_mapping;
588 list_add(&inode->i_devices, &bdev->bd_inodes);
589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 spin_unlock(&bdev_lock);
591 }
592 return bdev;
593}
594
595/* Call when you free inode */
596
597void bd_forget(struct inode *inode)
598{
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700599 struct block_device *bdev = NULL;
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 spin_lock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700602 if (inode->i_bdev) {
603 if (inode->i_sb != blockdev_superblock)
604 bdev = inode->i_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 __bd_forget(inode);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 spin_unlock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700608
609 if (bdev)
610 iput(bdev->bd_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
613int bd_claim(struct block_device *bdev, void *holder)
614{
615 int res;
616 spin_lock(&bdev_lock);
617
618 /* first decide result */
619 if (bdev->bd_holder == holder)
620 res = 0; /* already a holder */
621 else if (bdev->bd_holder != NULL)
622 res = -EBUSY; /* held by someone else */
623 else if (bdev->bd_contains == bdev)
624 res = 0; /* is a whole device which isn't held */
625
626 else if (bdev->bd_contains->bd_holder == bd_claim)
627 res = 0; /* is a partition of a device that is being partitioned */
628 else if (bdev->bd_contains->bd_holder != NULL)
629 res = -EBUSY; /* is a partition of a held device */
630 else
631 res = 0; /* is a partition of an un-held device */
632
633 /* now impose change */
634 if (res==0) {
635 /* note that for a whole device bd_holders
636 * will be incremented twice, and bd_holder will
637 * be set to bd_claim before being set to holder
638 */
639 bdev->bd_contains->bd_holders ++;
640 bdev->bd_contains->bd_holder = bd_claim;
641 bdev->bd_holders++;
642 bdev->bd_holder = holder;
643 }
644 spin_unlock(&bdev_lock);
645 return res;
646}
647
648EXPORT_SYMBOL(bd_claim);
649
650void bd_release(struct block_device *bdev)
651{
652 spin_lock(&bdev_lock);
653 if (!--bdev->bd_contains->bd_holders)
654 bdev->bd_contains->bd_holder = NULL;
655 if (!--bdev->bd_holders)
656 bdev->bd_holder = NULL;
657 spin_unlock(&bdev_lock);
658}
659
660EXPORT_SYMBOL(bd_release);
661
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800662#ifdef CONFIG_SYSFS
663/*
664 * Functions for bd_claim_by_kobject / bd_release_from_kobject
665 *
666 * If a kobject is passed to bd_claim_by_kobject()
667 * and the kobject has a parent directory,
668 * following symlinks are created:
669 * o from the kobject to the claimed bdev
670 * o from "holders" directory of the bdev to the parent of the kobject
671 * bd_release_from_kobject() removes these symlinks.
672 *
673 * Example:
674 * If /dev/dm-0 maps to /dev/sda, kobject corresponding to
675 * /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then:
676 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
677 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
678 */
679
680static struct kobject *bdev_get_kobj(struct block_device *bdev)
681{
682 if (bdev->bd_contains != bdev)
683 return kobject_get(&bdev->bd_part->kobj);
684 else
685 return kobject_get(&bdev->bd_disk->kobj);
686}
687
688static struct kobject *bdev_get_holder(struct block_device *bdev)
689{
690 if (bdev->bd_contains != bdev)
691 return kobject_get(bdev->bd_part->holder_dir);
692 else
693 return kobject_get(bdev->bd_disk->holder_dir);
694}
695
Andrew Morton4d7dd8f2006-09-29 01:58:56 -0700696static int add_symlink(struct kobject *from, struct kobject *to)
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800697{
698 if (!from || !to)
Andrew Morton4d7dd8f2006-09-29 01:58:56 -0700699 return 0;
700 return sysfs_create_link(from, to, kobject_name(to));
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800701}
702
703static void del_symlink(struct kobject *from, struct kobject *to)
704{
705 if (!from || !to)
706 return;
707 sysfs_remove_link(from, kobject_name(to));
708}
709
710/*
711 * 'struct bd_holder' contains pointers to kobjects symlinked by
712 * bd_claim_by_kobject.
713 * It's connected to bd_holder_list which is protected by bdev->bd_sem.
714 */
715struct bd_holder {
716 struct list_head list; /* chain of holders of the bdev */
717 int count; /* references from the holder */
718 struct kobject *sdir; /* holder object, e.g. "/block/dm-0/slaves" */
719 struct kobject *hdev; /* e.g. "/block/dm-0" */
720 struct kobject *hdir; /* e.g. "/block/sda/holders" */
721 struct kobject *sdev; /* e.g. "/block/sda" */
722};
723
724/*
725 * Get references of related kobjects at once.
726 * Returns 1 on success. 0 on failure.
727 *
728 * Should call bd_holder_release_dirs() after successful use.
729 */
730static int bd_holder_grab_dirs(struct block_device *bdev,
731 struct bd_holder *bo)
732{
733 if (!bdev || !bo)
734 return 0;
735
736 bo->sdir = kobject_get(bo->sdir);
737 if (!bo->sdir)
738 return 0;
739
740 bo->hdev = kobject_get(bo->sdir->parent);
741 if (!bo->hdev)
742 goto fail_put_sdir;
743
744 bo->sdev = bdev_get_kobj(bdev);
745 if (!bo->sdev)
746 goto fail_put_hdev;
747
748 bo->hdir = bdev_get_holder(bdev);
749 if (!bo->hdir)
750 goto fail_put_sdev;
751
752 return 1;
753
754fail_put_sdev:
755 kobject_put(bo->sdev);
756fail_put_hdev:
757 kobject_put(bo->hdev);
758fail_put_sdir:
759 kobject_put(bo->sdir);
760
761 return 0;
762}
763
764/* Put references of related kobjects at once. */
765static void bd_holder_release_dirs(struct bd_holder *bo)
766{
767 kobject_put(bo->hdir);
768 kobject_put(bo->sdev);
769 kobject_put(bo->hdev);
770 kobject_put(bo->sdir);
771}
772
773static struct bd_holder *alloc_bd_holder(struct kobject *kobj)
774{
775 struct bd_holder *bo;
776
777 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
778 if (!bo)
779 return NULL;
780
781 bo->count = 1;
782 bo->sdir = kobj;
783
784 return bo;
785}
786
787static void free_bd_holder(struct bd_holder *bo)
788{
789 kfree(bo);
790}
791
792/**
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500793 * find_bd_holder - find matching struct bd_holder from the block device
794 *
795 * @bdev: struct block device to be searched
796 * @bo: target struct bd_holder
797 *
798 * Returns matching entry with @bo in @bdev->bd_holder_list.
799 * If found, increment the reference count and return the pointer.
800 * If not found, returns NULL.
801 */
Andrew Morton36a561d2006-10-30 22:07:03 -0800802static struct bd_holder *find_bd_holder(struct block_device *bdev,
803 struct bd_holder *bo)
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500804{
805 struct bd_holder *tmp;
806
807 list_for_each_entry(tmp, &bdev->bd_holder_list, list)
808 if (tmp->sdir == bo->sdir) {
809 tmp->count++;
810 return tmp;
811 }
812
813 return NULL;
814}
815
816/**
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800817 * add_bd_holder - create sysfs symlinks for bd_claim() relationship
818 *
819 * @bdev: block device to be bd_claimed
820 * @bo: preallocated and initialized by alloc_bd_holder()
821 *
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500822 * Add @bo to @bdev->bd_holder_list, create symlinks.
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800823 *
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500824 * Returns 0 if symlinks are created.
825 * Returns -ve if something fails.
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800826 */
827static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
828{
Andrew Morton4d7dd8f2006-09-29 01:58:56 -0700829 int ret;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800830
831 if (!bo)
Andrew Morton4d7dd8f2006-09-29 01:58:56 -0700832 return -EINVAL;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800833
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800834 if (!bd_holder_grab_dirs(bdev, bo))
Andrew Morton4d7dd8f2006-09-29 01:58:56 -0700835 return -EBUSY;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800836
Andrew Morton4d7dd8f2006-09-29 01:58:56 -0700837 ret = add_symlink(bo->sdir, bo->sdev);
838 if (ret == 0) {
839 ret = add_symlink(bo->hdir, bo->hdev);
840 if (ret)
841 del_symlink(bo->sdir, bo->sdev);
842 }
843 if (ret == 0)
844 list_add_tail(&bo->list, &bdev->bd_holder_list);
845 return ret;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800846}
847
848/**
849 * del_bd_holder - delete sysfs symlinks for bd_claim() relationship
850 *
851 * @bdev: block device to be bd_claimed
852 * @kobj: holder's kobject
853 *
854 * If there is matching entry with @kobj in @bdev->bd_holder_list
855 * and no other bd_claim() from the same kobject,
856 * remove the struct bd_holder from the list, delete symlinks for it.
857 *
858 * Returns a pointer to the struct bd_holder when it's removed from the list
859 * and ready to be freed.
860 * Returns NULL if matching claim isn't found or there is other bd_claim()
861 * by the same kobject.
862 */
863static struct bd_holder *del_bd_holder(struct block_device *bdev,
864 struct kobject *kobj)
865{
866 struct bd_holder *bo;
867
868 list_for_each_entry(bo, &bdev->bd_holder_list, list) {
869 if (bo->sdir == kobj) {
870 bo->count--;
871 BUG_ON(bo->count < 0);
872 if (!bo->count) {
873 list_del(&bo->list);
874 del_symlink(bo->sdir, bo->sdev);
875 del_symlink(bo->hdir, bo->hdev);
876 bd_holder_release_dirs(bo);
877 return bo;
878 }
879 break;
880 }
881 }
882
883 return NULL;
884}
885
886/**
887 * bd_claim_by_kobject - bd_claim() with additional kobject signature
888 *
889 * @bdev: block device to be claimed
890 * @holder: holder's signature
891 * @kobj: holder's kobject
892 *
893 * Do bd_claim() and if it succeeds, create sysfs symlinks between
894 * the bdev and the holder's kobject.
895 * Use bd_release_from_kobject() when relesing the claimed bdev.
896 *
897 * Returns 0 on success. (same as bd_claim())
898 * Returns errno on failure.
899 */
900static int bd_claim_by_kobject(struct block_device *bdev, void *holder,
901 struct kobject *kobj)
902{
903 int res;
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500904 struct bd_holder *bo, *found;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800905
906 if (!kobj)
907 return -EINVAL;
908
909 bo = alloc_bd_holder(kobj);
910 if (!bo)
911 return -ENOMEM;
912
Peter Zijlstra2e7b6512006-12-08 02:36:13 -0800913 mutex_lock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800914 res = bd_claim(bdev, holder);
Jun'ichi Nomurabcb55162006-10-30 16:23:45 -0500915 if (res == 0) {
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500916 found = find_bd_holder(bdev, bo);
917 if (found == NULL) {
918 res = add_bd_holder(bdev, bo);
919 if (res)
920 bd_release(bdev);
921 }
Jun'ichi Nomurabcb55162006-10-30 16:23:45 -0500922 }
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500923
924 if (res || found)
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800925 free_bd_holder(bo);
Jun'ichi Nomurab4cf1b72006-03-27 01:18:00 -0800926 mutex_unlock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800927
928 return res;
929}
930
931/**
932 * bd_release_from_kobject - bd_release() with additional kobject signature
933 *
934 * @bdev: block device to be released
935 * @kobj: holder's kobject
936 *
937 * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject().
938 */
939static void bd_release_from_kobject(struct block_device *bdev,
940 struct kobject *kobj)
941{
942 struct bd_holder *bo;
943
944 if (!kobj)
945 return;
946
Peter Zijlstra2e7b6512006-12-08 02:36:13 -0800947 mutex_lock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800948 bd_release(bdev);
949 if ((bo = del_bd_holder(bdev, kobj)))
950 free_bd_holder(bo);
Jun'ichi Nomurab4cf1b72006-03-27 01:18:00 -0800951 mutex_unlock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800952}
953
954/**
955 * bd_claim_by_disk - wrapper function for bd_claim_by_kobject()
956 *
957 * @bdev: block device to be claimed
958 * @holder: holder's signature
959 * @disk: holder's gendisk
960 *
961 * Call bd_claim_by_kobject() with getting @disk->slave_dir.
962 */
963int bd_claim_by_disk(struct block_device *bdev, void *holder,
964 struct gendisk *disk)
965{
966 return bd_claim_by_kobject(bdev, holder, kobject_get(disk->slave_dir));
967}
968EXPORT_SYMBOL_GPL(bd_claim_by_disk);
969
970/**
971 * bd_release_from_disk - wrapper function for bd_release_from_kobject()
972 *
973 * @bdev: block device to be claimed
974 * @disk: holder's gendisk
975 *
976 * Call bd_release_from_kobject() and put @disk->slave_dir.
977 */
978void bd_release_from_disk(struct block_device *bdev, struct gendisk *disk)
979{
980 bd_release_from_kobject(bdev, disk->slave_dir);
981 kobject_put(disk->slave_dir);
982}
983EXPORT_SYMBOL_GPL(bd_release_from_disk);
984#endif
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986/*
987 * Tries to open block device by device number. Use it ONLY if you
988 * really do not have anything better - i.e. when you are behind a
989 * truly sucky interface and all you are given is a device number. _Never_
990 * to be used for internal purposes. If you ever need it - reconsider
991 * your API.
992 */
993struct block_device *open_by_devnum(dev_t dev, unsigned mode)
994{
995 struct block_device *bdev = bdget(dev);
996 int err = -ENOMEM;
997 int flags = mode & FMODE_WRITE ? O_RDWR : O_RDONLY;
998 if (bdev)
999 err = blkdev_get(bdev, mode, flags);
1000 return err ? ERR_PTR(err) : bdev;
1001}
1002
1003EXPORT_SYMBOL(open_by_devnum);
1004
1005/*
1006 * This routine checks whether a removable media has been changed,
1007 * and invalidates all buffer-cache-entries in that case. This
1008 * is a relatively slow routine, so we have to try to minimize using
1009 * it. Thus it is called only upon a 'mount' or 'open'. This
1010 * is the best way of combining speed and utility, I think.
1011 * People changing diskettes in the middle of an operation deserve
1012 * to lose :-)
1013 */
1014int check_disk_change(struct block_device *bdev)
1015{
1016 struct gendisk *disk = bdev->bd_disk;
1017 struct block_device_operations * bdops = disk->fops;
1018
1019 if (!bdops->media_changed)
1020 return 0;
1021 if (!bdops->media_changed(bdev->bd_disk))
1022 return 0;
1023
Christoph Hellwig2ef41632005-05-05 16:15:59 -07001024 if (__invalidate_device(bdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 printk("VFS: busy inodes on changed media.\n");
1026
1027 if (bdops->revalidate_disk)
1028 bdops->revalidate_disk(bdev->bd_disk);
1029 if (bdev->bd_disk->minors > 1)
1030 bdev->bd_invalidated = 1;
1031 return 1;
1032}
1033
1034EXPORT_SYMBOL(check_disk_change);
1035
1036void bd_set_size(struct block_device *bdev, loff_t size)
1037{
1038 unsigned bsize = bdev_hardsect_size(bdev);
1039
1040 bdev->bd_inode->i_size = size;
1041 while (bsize < PAGE_CACHE_SIZE) {
1042 if (size & bsize)
1043 break;
1044 bsize <<= 1;
1045 }
1046 bdev->bd_block_size = bsize;
1047 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
1048}
1049EXPORT_SYMBOL(bd_set_size);
1050
NeilBrown37be4122006-12-08 02:36:16 -08001051static int __blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags,
1052 int for_part);
Peter Zijlstrac48f70c2006-12-08 02:36:18 -08001053static int __blkdev_put(struct block_device *bdev, int for_part);
NeilBrown37be4122006-12-08 02:36:16 -08001054
1055static int do_open(struct block_device *bdev, struct file *file, int for_part)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
1057 struct module *owner = NULL;
1058 struct gendisk *disk;
1059 int ret = -ENXIO;
1060 int part;
1061
1062 file->f_mapping = bdev->bd_inode->i_mapping;
1063 lock_kernel();
1064 disk = get_gendisk(bdev->bd_dev, &part);
1065 if (!disk) {
1066 unlock_kernel();
1067 bdput(bdev);
1068 return ret;
1069 }
1070 owner = disk->fops->owner;
1071
NeilBrown6796bf52006-12-08 02:36:16 -08001072 mutex_lock_nested(&bdev->bd_mutex, for_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (!bdev->bd_openers) {
1074 bdev->bd_disk = disk;
1075 bdev->bd_contains = bdev;
1076 if (!part) {
1077 struct backing_dev_info *bdi;
1078 if (disk->fops->open) {
1079 ret = disk->fops->open(bdev->bd_inode, file);
1080 if (ret)
1081 goto out_first;
1082 }
1083 if (!bdev->bd_openers) {
1084 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
1085 bdi = blk_get_backing_dev_info(bdev);
1086 if (bdi == NULL)
1087 bdi = &default_backing_dev_info;
1088 bdev->bd_inode->i_data.backing_dev_info = bdi;
1089 }
1090 if (bdev->bd_invalidated)
1091 rescan_partitions(disk, bdev);
1092 } else {
1093 struct hd_struct *p;
1094 struct block_device *whole;
1095 whole = bdget_disk(disk, 0);
1096 ret = -ENOMEM;
1097 if (!whole)
1098 goto out_first;
NeilBrown37be4122006-12-08 02:36:16 -08001099 BUG_ON(for_part);
1100 ret = __blkdev_get(whole, file->f_mode, file->f_flags, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (ret)
1102 goto out_first;
1103 bdev->bd_contains = whole;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 p = disk->part[part - 1];
1105 bdev->bd_inode->i_data.backing_dev_info =
1106 whole->bd_inode->i_data.backing_dev_info;
1107 if (!(disk->flags & GENHD_FL_UP) || !p || !p->nr_sects) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 ret = -ENXIO;
1109 goto out_first;
1110 }
1111 kobject_get(&p->kobj);
1112 bdev->bd_part = p;
1113 bd_set_size(bdev, (loff_t) p->nr_sects << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 }
1115 } else {
1116 put_disk(disk);
1117 module_put(owner);
1118 if (bdev->bd_contains == bdev) {
1119 if (bdev->bd_disk->fops->open) {
1120 ret = bdev->bd_disk->fops->open(bdev->bd_inode, file);
1121 if (ret)
1122 goto out;
1123 }
1124 if (bdev->bd_invalidated)
1125 rescan_partitions(bdev->bd_disk, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
1127 }
1128 bdev->bd_openers++;
NeilBrown37be4122006-12-08 02:36:16 -08001129 if (for_part)
1130 bdev->bd_part_count++;
Arjan van de Venc039e312006-03-23 03:00:28 -08001131 mutex_unlock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 unlock_kernel();
1133 return 0;
1134
1135out_first:
1136 bdev->bd_disk = NULL;
1137 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
1138 if (bdev != bdev->bd_contains)
Peter Zijlstrac48f70c2006-12-08 02:36:18 -08001139 __blkdev_put(bdev->bd_contains, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 bdev->bd_contains = NULL;
1141 put_disk(disk);
1142 module_put(owner);
1143out:
Arjan van de Venc039e312006-03-23 03:00:28 -08001144 mutex_unlock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 unlock_kernel();
1146 if (ret)
1147 bdput(bdev);
1148 return ret;
1149}
1150
NeilBrown37be4122006-12-08 02:36:16 -08001151static int __blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags,
1152 int for_part)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
1154 /*
1155 * This crockload is due to bad choice of ->open() type.
1156 * It will go away.
1157 * For now, block device ->open() routine must _not_
1158 * examine anything in 'inode' argument except ->i_rdev.
1159 */
1160 struct file fake_file = {};
1161 struct dentry fake_dentry = {};
1162 fake_file.f_mode = mode;
1163 fake_file.f_flags = flags;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001164 fake_file.f_path.dentry = &fake_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 fake_dentry.d_inode = bdev->bd_inode;
1166
NeilBrown37be4122006-12-08 02:36:16 -08001167 return do_open(bdev, &fake_file, for_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
1169
NeilBrown37be4122006-12-08 02:36:16 -08001170int blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags)
1171{
1172 return __blkdev_get(bdev, mode, flags, 0);
1173}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174EXPORT_SYMBOL(blkdev_get);
1175
1176static int blkdev_open(struct inode * inode, struct file * filp)
1177{
1178 struct block_device *bdev;
1179 int res;
1180
1181 /*
1182 * Preserve backwards compatibility and allow large file access
1183 * even if userspace doesn't ask for it explicitly. Some mkfs
1184 * binary needs it. We might want to drop this workaround
1185 * during an unstable branch.
1186 */
1187 filp->f_flags |= O_LARGEFILE;
1188
1189 bdev = bd_acquire(inode);
Pavel Emelianov6a2aae02006-10-28 10:38:33 -07001190 if (bdev == NULL)
1191 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
NeilBrown37be4122006-12-08 02:36:16 -08001193 res = do_open(bdev, filp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if (res)
1195 return res;
1196
1197 if (!(filp->f_flags & O_EXCL) )
1198 return 0;
1199
1200 if (!(res = bd_claim(bdev, filp)))
1201 return 0;
1202
1203 blkdev_put(bdev);
1204 return res;
1205}
1206
NeilBrown37be4122006-12-08 02:36:16 -08001207static int __blkdev_put(struct block_device *bdev, int for_part)
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001208{
1209 int ret = 0;
1210 struct inode *bd_inode = bdev->bd_inode;
1211 struct gendisk *disk = bdev->bd_disk;
NeilBrown37be4122006-12-08 02:36:16 -08001212 struct block_device *victim = NULL;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001213
NeilBrown6796bf52006-12-08 02:36:16 -08001214 mutex_lock_nested(&bdev->bd_mutex, for_part);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001215 lock_kernel();
NeilBrown37be4122006-12-08 02:36:16 -08001216 if (for_part)
1217 bdev->bd_part_count--;
1218
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001219 if (!--bdev->bd_openers) {
1220 sync_blockdev(bdev);
1221 kill_bdev(bdev);
1222 }
1223 if (bdev->bd_contains == bdev) {
1224 if (disk->fops->release)
1225 ret = disk->fops->release(bd_inode, NULL);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001226 }
1227 if (!bdev->bd_openers) {
1228 struct module *owner = disk->fops->owner;
1229
1230 put_disk(disk);
1231 module_put(owner);
1232
1233 if (bdev->bd_contains != bdev) {
1234 kobject_put(&bdev->bd_part->kobj);
1235 bdev->bd_part = NULL;
1236 }
1237 bdev->bd_disk = NULL;
1238 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
NeilBrown37be4122006-12-08 02:36:16 -08001239 if (bdev != bdev->bd_contains)
1240 victim = bdev->bd_contains;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001241 bdev->bd_contains = NULL;
1242 }
1243 unlock_kernel();
1244 mutex_unlock(&bdev->bd_mutex);
1245 bdput(bdev);
NeilBrown37be4122006-12-08 02:36:16 -08001246 if (victim)
1247 __blkdev_put(victim, 1);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001248 return ret;
1249}
1250
NeilBrown37be4122006-12-08 02:36:16 -08001251int blkdev_put(struct block_device *bdev)
1252{
1253 return __blkdev_put(bdev, 0);
1254}
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001255EXPORT_SYMBOL(blkdev_put);
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257static int blkdev_close(struct inode * inode, struct file * filp)
1258{
1259 struct block_device *bdev = I_BDEV(filp->f_mapping->host);
1260 if (bdev->bd_holder == filp)
1261 bd_release(bdev);
1262 return blkdev_put(bdev);
1263}
1264
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07001265static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
1267 return blkdev_ioctl(file->f_mapping->host, file, cmd, arg);
1268}
1269
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001270const struct address_space_operations def_blk_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 .readpage = blkdev_readpage,
1272 .writepage = blkdev_writepage,
1273 .sync_page = block_sync_page,
1274 .prepare_write = blkdev_prepare_write,
1275 .commit_write = blkdev_commit_write,
1276 .writepages = generic_writepages,
1277 .direct_IO = blkdev_direct_IO,
1278};
1279
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001280const struct file_operations def_blk_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 .open = blkdev_open,
1282 .release = blkdev_close,
1283 .llseek = block_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -07001284 .read = do_sync_read,
1285 .write = do_sync_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 .aio_read = generic_file_aio_read,
Badari Pulavarty027445c2006-09-30 23:28:46 -07001287 .aio_write = generic_file_aio_write_nolock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 .mmap = generic_file_mmap,
1289 .fsync = block_fsync,
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07001290 .unlocked_ioctl = block_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291#ifdef CONFIG_COMPAT
1292 .compat_ioctl = compat_blkdev_ioctl,
1293#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 .sendfile = generic_file_sendfile,
Jens Axboe7f9c51f2006-05-01 19:59:32 +02001295 .splice_read = generic_file_splice_read,
1296 .splice_write = generic_file_splice_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297};
1298
1299int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
1300{
1301 int res;
1302 mm_segment_t old_fs = get_fs();
1303 set_fs(KERNEL_DS);
1304 res = blkdev_ioctl(bdev->bd_inode, NULL, cmd, arg);
1305 set_fs(old_fs);
1306 return res;
1307}
1308
1309EXPORT_SYMBOL(ioctl_by_bdev);
1310
1311/**
1312 * lookup_bdev - lookup a struct block_device by name
1313 *
1314 * @path: special file representing the block device
1315 *
1316 * Get a reference to the blockdevice at @path in the current
1317 * namespace if possible and return it. Return ERR_PTR(error)
1318 * otherwise.
1319 */
1320struct block_device *lookup_bdev(const char *path)
1321{
1322 struct block_device *bdev;
1323 struct inode *inode;
1324 struct nameidata nd;
1325 int error;
1326
1327 if (!path || !*path)
1328 return ERR_PTR(-EINVAL);
1329
1330 error = path_lookup(path, LOOKUP_FOLLOW, &nd);
1331 if (error)
1332 return ERR_PTR(error);
1333
1334 inode = nd.dentry->d_inode;
1335 error = -ENOTBLK;
1336 if (!S_ISBLK(inode->i_mode))
1337 goto fail;
1338 error = -EACCES;
1339 if (nd.mnt->mnt_flags & MNT_NODEV)
1340 goto fail;
1341 error = -ENOMEM;
1342 bdev = bd_acquire(inode);
1343 if (!bdev)
1344 goto fail;
1345out:
1346 path_release(&nd);
1347 return bdev;
1348fail:
1349 bdev = ERR_PTR(error);
1350 goto out;
1351}
1352
1353/**
1354 * open_bdev_excl - open a block device by name and set it up for use
1355 *
1356 * @path: special file representing the block device
1357 * @flags: %MS_RDONLY for opening read-only
1358 * @holder: owner for exclusion
1359 *
1360 * Open the blockdevice described by the special file at @path, claim it
1361 * for the @holder.
1362 */
1363struct block_device *open_bdev_excl(const char *path, int flags, void *holder)
1364{
1365 struct block_device *bdev;
1366 mode_t mode = FMODE_READ;
1367 int error = 0;
1368
1369 bdev = lookup_bdev(path);
1370 if (IS_ERR(bdev))
1371 return bdev;
1372
1373 if (!(flags & MS_RDONLY))
1374 mode |= FMODE_WRITE;
1375 error = blkdev_get(bdev, mode, 0);
1376 if (error)
1377 return ERR_PTR(error);
1378 error = -EACCES;
1379 if (!(flags & MS_RDONLY) && bdev_read_only(bdev))
1380 goto blkdev_put;
1381 error = bd_claim(bdev, holder);
1382 if (error)
1383 goto blkdev_put;
1384
1385 return bdev;
1386
1387blkdev_put:
1388 blkdev_put(bdev);
1389 return ERR_PTR(error);
1390}
1391
1392EXPORT_SYMBOL(open_bdev_excl);
1393
1394/**
1395 * close_bdev_excl - release a blockdevice openen by open_bdev_excl()
1396 *
1397 * @bdev: blockdevice to close
1398 *
1399 * This is the counterpart to open_bdev_excl().
1400 */
1401void close_bdev_excl(struct block_device *bdev)
1402{
1403 bd_release(bdev);
1404 blkdev_put(bdev);
1405}
1406
1407EXPORT_SYMBOL(close_bdev_excl);
David Howellsb71e8a42006-08-29 19:06:11 +01001408
1409int __invalidate_device(struct block_device *bdev)
1410{
1411 struct super_block *sb = get_super(bdev);
1412 int res = 0;
1413
1414 if (sb) {
1415 /*
1416 * no need to lock the super, get_super holds the
1417 * read mutex so the filesystem cannot go away
1418 * under us (->put_super runs with the write lock
1419 * hold).
1420 */
1421 shrink_dcache_sb(sb);
1422 res = invalidate_inodes(sb);
1423 drop_super(sb);
1424 }
1425 invalidate_bdev(bdev, 0);
1426 return res;
1427}
1428EXPORT_SYMBOL(__invalidate_device);