blob: e63067d25cdb9b839bae496e2f1110177b234e97 [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>
Vignesh Babu BM1368c4f2007-05-08 00:24:32 -070025#include <linux/log2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/uaccess.h>
David Howells07f3f052006-09-30 20:52:18 +020027#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29struct bdev_inode {
30 struct block_device bdev;
31 struct inode vfs_inode;
32};
33
34static inline struct bdev_inode *BDEV_I(struct inode *inode)
35{
36 return container_of(inode, struct bdev_inode, vfs_inode);
37}
38
39inline struct block_device *I_BDEV(struct inode *inode)
40{
41 return &BDEV_I(inode)->bdev;
42}
43
44EXPORT_SYMBOL(I_BDEV);
45
46static 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
Peter Zijlstraf9a14392007-05-06 14:49:55 -070059/* Kill _all_ buffers and pagecache , dirty or not.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static void kill_bdev(struct block_device *bdev)
61{
Peter Zijlstraf9a14392007-05-06 14:49:55 -070062 if (bdev->bd_inode->i_mapping->nrpages == 0)
63 return;
64 invalidate_bh_lrus();
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
66}
67
68int set_blocksize(struct block_device *bdev, int size)
69{
70 /* Size must be a power of two, and between 512 and PAGE_SIZE */
Vignesh Babu BM1368c4f2007-05-08 00:24:32 -070071 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 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
88EXPORT_SYMBOL(set_blocksize);
89
90int sb_set_blocksize(struct super_block *sb, int size)
91{
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 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;
Coywolf Qi Hunt38885bd2006-03-24 03:18:05 -080097 sb->s_blocksize_bits = blksize_bits(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return sb->s_blocksize;
99}
100
101EXPORT_SYMBOL(sb_set_blocksize);
102
103int 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
111EXPORT_SYMBOL(sb_min_blocksize);
112
113static int
114blkdev_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
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800135static int
136blkdev_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
163static ssize_t
164blkdev_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
NeilBrown6712ecf2007-09-27 12:47:43 +0200175static void blk_end_aio(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Chen, Kenneth We61c9012006-12-13 00:34:36 -0800177 struct kiocb *iocb = bio->bi_private;
178 atomic_t *bio_count = &iocb->ki_bio_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Chen, Kenneth We61c9012006-12-13 00:34:36 -0800180 if (bio_data_dir(bio) == READ)
181 bio_check_pages_dirty(bio);
182 else {
183 bio_release_pages(bio);
184 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
186
Chen, Kenneth We61c9012006-12-13 00:34:36 -0800187 /* 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)) {
Andrew Morton790816d2007-01-22 20:40:35 -0800192 if ((long)iocb->ki_nbytes < 0)
Chen, Kenneth We61c9012006-12-13 00:34:36 -0800193 aio_complete(iocb, iocb->ki_nbytes, 0);
194 else
195 aio_complete(iocb, iocb->ki_left, 0);
196 }
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return 0;
199}
200
Chen, Kenneth We61c9012006-12-13 00:34:36 -0800201#define VEC_SIZE 16
202struct 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 */
217static 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
Chen, Kenneth Wcda92052007-01-22 20:40:43 -0800236/* return a page back to pvec array */
237static void blk_unget_page(struct page *page, struct pvec *pvec)
238{
239 pvec->page[--pvec->idx] = page;
240}
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242static ssize_t
243blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
Chen, Kenneth We61c9012006-12-13 00:34:36 -0800244 loff_t pos, unsigned long nr_segs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Chen, Kenneth We61c9012006-12-13 00:34:36 -0800246 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Chen, Kenneth We61c9012006-12-13 00:34:36 -0800254 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;
299same_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 }
Chen, Kenneth Wcda92052007-01-22 20:40:43 -0800330 } else {
331 blk_unget_page(page, &pvec);
Chen, Kenneth We61c9012006-12-13 00:34:36 -0800332 }
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
341completion:
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
352backout:
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800369#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
372{
373 return block_write_full_page(page, blkdev_get_block, wbc);
374}
375
376static int blkdev_readpage(struct file * file, struct page * page)
377{
378 return block_read_full_page(page, blkdev_get_block);
379}
380
Nick Piggin6272b5a2007-10-16 01:25:04 -0700381static int blkdev_write_begin(struct file *file, struct address_space *mapping,
382 loff_t pos, unsigned len, unsigned flags,
383 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Nick Piggin6272b5a2007-10-16 01:25:04 -0700385 *pagep = NULL;
386 return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
387 blkdev_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
Nick Piggin6272b5a2007-10-16 01:25:04 -0700390static int blkdev_write_end(struct file *file, struct address_space *mapping,
391 loff_t pos, unsigned len, unsigned copied,
392 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Nick Piggin6272b5a2007-10-16 01:25:04 -0700394 int ret;
395 ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
396
397 unlock_page(page);
398 page_cache_release(page);
399
400 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
403/*
404 * private llseek:
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800405 * for a block special file file->f_path.dentry->d_inode->i_size is zero
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 * so we compute the size by hand (just as in block_read/write above)
407 */
408static loff_t block_llseek(struct file *file, loff_t offset, int origin)
409{
410 struct inode *bd_inode = file->f_mapping->host;
411 loff_t size;
412 loff_t retval;
413
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800414 mutex_lock(&bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 size = i_size_read(bd_inode);
416
417 switch (origin) {
418 case 2:
419 offset += size;
420 break;
421 case 1:
422 offset += file->f_pos;
423 }
424 retval = -EINVAL;
425 if (offset >= 0 && offset <= size) {
426 if (offset != file->f_pos) {
427 file->f_pos = offset;
428 }
429 retval = offset;
430 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800431 mutex_unlock(&bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return retval;
433}
434
435/*
436 * Filp is never NULL; the only case when ->fsync() is called with
437 * NULL first argument is nfsd_sync_dir() and that's not a directory.
438 */
439
440static int block_fsync(struct file *filp, struct dentry *dentry, int datasync)
441{
442 return sync_blockdev(I_BDEV(filp->f_mapping->host));
443}
444
445/*
446 * pseudo-fs
447 */
448
449static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
Christoph Lametere18b8902006-12-06 20:33:20 -0800450static struct kmem_cache * bdev_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452static struct inode *bdev_alloc_inode(struct super_block *sb)
453{
Christoph Lametere94b1762006-12-06 20:33:17 -0800454 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (!ei)
456 return NULL;
457 return &ei->vfs_inode;
458}
459
460static void bdev_destroy_inode(struct inode *inode)
461{
462 struct bdev_inode *bdi = BDEV_I(inode);
463
464 bdi->bdev.bd_inode_backing_dev_info = NULL;
465 kmem_cache_free(bdev_cachep, bdi);
466}
467
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -0700468static void init_once(struct kmem_cache * cachep, void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 struct bdev_inode *ei = (struct bdev_inode *) foo;
471 struct block_device *bdev = &ei->bdev;
472
Christoph Lametera35afb82007-05-16 22:10:57 -0700473 memset(bdev, 0, sizeof(*bdev));
474 mutex_init(&bdev->bd_mutex);
475 sema_init(&bdev->bd_mount_sem, 1);
476 INIT_LIST_HEAD(&bdev->bd_inodes);
477 INIT_LIST_HEAD(&bdev->bd_list);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800478#ifdef CONFIG_SYSFS
Christoph Lametera35afb82007-05-16 22:10:57 -0700479 INIT_LIST_HEAD(&bdev->bd_holder_list);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800480#endif
Christoph Lametera35afb82007-05-16 22:10:57 -0700481 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
484static inline void __bd_forget(struct inode *inode)
485{
486 list_del_init(&inode->i_devices);
487 inode->i_bdev = NULL;
488 inode->i_mapping = &inode->i_data;
489}
490
491static void bdev_clear_inode(struct inode *inode)
492{
493 struct block_device *bdev = &BDEV_I(inode)->bdev;
494 struct list_head *p;
495 spin_lock(&bdev_lock);
496 while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
497 __bd_forget(list_entry(p, struct inode, i_devices));
498 }
499 list_del_init(&bdev->bd_list);
500 spin_unlock(&bdev_lock);
501}
502
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800503static const struct super_operations bdev_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 .statfs = simple_statfs,
505 .alloc_inode = bdev_alloc_inode,
506 .destroy_inode = bdev_destroy_inode,
507 .drop_inode = generic_delete_inode,
508 .clear_inode = bdev_clear_inode,
509};
510
David Howells454e2392006-06-23 02:02:57 -0700511static int bd_get_sb(struct file_system_type *fs_type,
512 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
David Howells454e2392006-06-23 02:02:57 -0700514 return get_sb_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
517static struct file_system_type bd_type = {
518 .name = "bdev",
519 .get_sb = bd_get_sb,
520 .kill_sb = kill_anon_super,
521};
522
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800523static struct vfsmount *bd_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524struct super_block *blockdev_superblock;
525
526void __init bdev_cache_init(void)
527{
528 int err;
529 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800530 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
531 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +0900532 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 err = register_filesystem(&bd_type);
534 if (err)
535 panic("Cannot register bdev pseudo-fs");
536 bd_mnt = kern_mount(&bd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (IS_ERR(bd_mnt))
538 panic("Cannot create bdev pseudo-fs");
539 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
540}
541
542/*
543 * Most likely _very_ bad one - but then it's hardly critical for small
544 * /dev and can be fixed when somebody will need really large one.
545 * Keep in mind that it will be fed through icache hash function too.
546 */
547static inline unsigned long hash(dev_t dev)
548{
549 return MAJOR(dev)+MINOR(dev);
550}
551
552static int bdev_test(struct inode *inode, void *data)
553{
554 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
555}
556
557static int bdev_set(struct inode *inode, void *data)
558{
559 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
560 return 0;
561}
562
563static LIST_HEAD(all_bdevs);
564
565struct block_device *bdget(dev_t dev)
566{
567 struct block_device *bdev;
568 struct inode *inode;
569
570 inode = iget5_locked(bd_mnt->mnt_sb, hash(dev),
571 bdev_test, bdev_set, &dev);
572
573 if (!inode)
574 return NULL;
575
576 bdev = &BDEV_I(inode)->bdev;
577
578 if (inode->i_state & I_NEW) {
579 bdev->bd_contains = NULL;
580 bdev->bd_inode = inode;
581 bdev->bd_block_size = (1 << inode->i_blkbits);
582 bdev->bd_part_count = 0;
583 bdev->bd_invalidated = 0;
584 inode->i_mode = S_IFBLK;
585 inode->i_rdev = dev;
586 inode->i_bdev = bdev;
587 inode->i_data.a_ops = &def_blk_aops;
588 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
589 inode->i_data.backing_dev_info = &default_backing_dev_info;
590 spin_lock(&bdev_lock);
591 list_add(&bdev->bd_list, &all_bdevs);
592 spin_unlock(&bdev_lock);
593 unlock_new_inode(inode);
594 }
595 return bdev;
596}
597
598EXPORT_SYMBOL(bdget);
599
600long nr_blockdev_pages(void)
601{
Matthias Kaehlcke203a2932007-07-15 23:40:23 -0700602 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 long ret = 0;
604 spin_lock(&bdev_lock);
Matthias Kaehlcke203a2932007-07-15 23:40:23 -0700605 list_for_each_entry(bdev, &all_bdevs, bd_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 ret += bdev->bd_inode->i_mapping->nrpages;
607 }
608 spin_unlock(&bdev_lock);
609 return ret;
610}
611
612void bdput(struct block_device *bdev)
613{
614 iput(bdev->bd_inode);
615}
616
617EXPORT_SYMBOL(bdput);
618
619static struct block_device *bd_acquire(struct inode *inode)
620{
621 struct block_device *bdev;
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 spin_lock(&bdev_lock);
624 bdev = inode->i_bdev;
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700625 if (bdev) {
626 atomic_inc(&bdev->bd_inode->i_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 spin_unlock(&bdev_lock);
628 return bdev;
629 }
630 spin_unlock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 bdev = bdget(inode->i_rdev);
633 if (bdev) {
634 spin_lock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700635 if (!inode->i_bdev) {
636 /*
637 * We take an additional bd_inode->i_count for inode,
638 * and it's released in clear_inode() of inode.
639 * So, we can access it via ->i_mapping always
640 * without igrab().
641 */
642 atomic_inc(&bdev->bd_inode->i_count);
643 inode->i_bdev = bdev;
644 inode->i_mapping = bdev->bd_inode->i_mapping;
645 list_add(&inode->i_devices, &bdev->bd_inodes);
646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 spin_unlock(&bdev_lock);
648 }
649 return bdev;
650}
651
652/* Call when you free inode */
653
654void bd_forget(struct inode *inode)
655{
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700656 struct block_device *bdev = NULL;
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 spin_lock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700659 if (inode->i_bdev) {
660 if (inode->i_sb != blockdev_superblock)
661 bdev = inode->i_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 __bd_forget(inode);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 spin_unlock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700665
666 if (bdev)
667 iput(bdev->bd_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
670int bd_claim(struct block_device *bdev, void *holder)
671{
672 int res;
673 spin_lock(&bdev_lock);
674
675 /* first decide result */
676 if (bdev->bd_holder == holder)
677 res = 0; /* already a holder */
678 else if (bdev->bd_holder != NULL)
679 res = -EBUSY; /* held by someone else */
680 else if (bdev->bd_contains == bdev)
681 res = 0; /* is a whole device which isn't held */
682
683 else if (bdev->bd_contains->bd_holder == bd_claim)
684 res = 0; /* is a partition of a device that is being partitioned */
685 else if (bdev->bd_contains->bd_holder != NULL)
686 res = -EBUSY; /* is a partition of a held device */
687 else
688 res = 0; /* is a partition of an un-held device */
689
690 /* now impose change */
691 if (res==0) {
692 /* note that for a whole device bd_holders
693 * will be incremented twice, and bd_holder will
694 * be set to bd_claim before being set to holder
695 */
696 bdev->bd_contains->bd_holders ++;
697 bdev->bd_contains->bd_holder = bd_claim;
698 bdev->bd_holders++;
699 bdev->bd_holder = holder;
700 }
701 spin_unlock(&bdev_lock);
702 return res;
703}
704
705EXPORT_SYMBOL(bd_claim);
706
707void bd_release(struct block_device *bdev)
708{
709 spin_lock(&bdev_lock);
710 if (!--bdev->bd_contains->bd_holders)
711 bdev->bd_contains->bd_holder = NULL;
712 if (!--bdev->bd_holders)
713 bdev->bd_holder = NULL;
714 spin_unlock(&bdev_lock);
715}
716
717EXPORT_SYMBOL(bd_release);
718
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800719#ifdef CONFIG_SYSFS
720/*
721 * Functions for bd_claim_by_kobject / bd_release_from_kobject
722 *
723 * If a kobject is passed to bd_claim_by_kobject()
724 * and the kobject has a parent directory,
725 * following symlinks are created:
726 * o from the kobject to the claimed bdev
727 * o from "holders" directory of the bdev to the parent of the kobject
728 * bd_release_from_kobject() removes these symlinks.
729 *
730 * Example:
731 * If /dev/dm-0 maps to /dev/sda, kobject corresponding to
732 * /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then:
733 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
734 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
735 */
736
737static struct kobject *bdev_get_kobj(struct block_device *bdev)
738{
739 if (bdev->bd_contains != bdev)
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200740 return kobject_get(&bdev->bd_part->dev.kobj);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800741 else
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200742 return kobject_get(&bdev->bd_disk->dev.kobj);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800743}
744
745static struct kobject *bdev_get_holder(struct block_device *bdev)
746{
747 if (bdev->bd_contains != bdev)
748 return kobject_get(bdev->bd_part->holder_dir);
749 else
750 return kobject_get(bdev->bd_disk->holder_dir);
751}
752
Andrew Morton4d7dd8f2006-09-29 01:58:56 -0700753static int add_symlink(struct kobject *from, struct kobject *to)
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800754{
755 if (!from || !to)
Andrew Morton4d7dd8f2006-09-29 01:58:56 -0700756 return 0;
757 return sysfs_create_link(from, to, kobject_name(to));
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800758}
759
760static void del_symlink(struct kobject *from, struct kobject *to)
761{
762 if (!from || !to)
763 return;
764 sysfs_remove_link(from, kobject_name(to));
765}
766
767/*
768 * 'struct bd_holder' contains pointers to kobjects symlinked by
769 * bd_claim_by_kobject.
770 * It's connected to bd_holder_list which is protected by bdev->bd_sem.
771 */
772struct bd_holder {
773 struct list_head list; /* chain of holders of the bdev */
774 int count; /* references from the holder */
775 struct kobject *sdir; /* holder object, e.g. "/block/dm-0/slaves" */
776 struct kobject *hdev; /* e.g. "/block/dm-0" */
777 struct kobject *hdir; /* e.g. "/block/sda/holders" */
778 struct kobject *sdev; /* e.g. "/block/sda" */
779};
780
781/*
782 * Get references of related kobjects at once.
783 * Returns 1 on success. 0 on failure.
784 *
785 * Should call bd_holder_release_dirs() after successful use.
786 */
787static int bd_holder_grab_dirs(struct block_device *bdev,
788 struct bd_holder *bo)
789{
790 if (!bdev || !bo)
791 return 0;
792
793 bo->sdir = kobject_get(bo->sdir);
794 if (!bo->sdir)
795 return 0;
796
797 bo->hdev = kobject_get(bo->sdir->parent);
798 if (!bo->hdev)
799 goto fail_put_sdir;
800
801 bo->sdev = bdev_get_kobj(bdev);
802 if (!bo->sdev)
803 goto fail_put_hdev;
804
805 bo->hdir = bdev_get_holder(bdev);
806 if (!bo->hdir)
807 goto fail_put_sdev;
808
809 return 1;
810
811fail_put_sdev:
812 kobject_put(bo->sdev);
813fail_put_hdev:
814 kobject_put(bo->hdev);
815fail_put_sdir:
816 kobject_put(bo->sdir);
817
818 return 0;
819}
820
821/* Put references of related kobjects at once. */
822static void bd_holder_release_dirs(struct bd_holder *bo)
823{
824 kobject_put(bo->hdir);
825 kobject_put(bo->sdev);
826 kobject_put(bo->hdev);
827 kobject_put(bo->sdir);
828}
829
830static struct bd_holder *alloc_bd_holder(struct kobject *kobj)
831{
832 struct bd_holder *bo;
833
834 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
835 if (!bo)
836 return NULL;
837
838 bo->count = 1;
839 bo->sdir = kobj;
840
841 return bo;
842}
843
844static void free_bd_holder(struct bd_holder *bo)
845{
846 kfree(bo);
847}
848
849/**
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500850 * find_bd_holder - find matching struct bd_holder from the block device
851 *
852 * @bdev: struct block device to be searched
853 * @bo: target struct bd_holder
854 *
855 * Returns matching entry with @bo in @bdev->bd_holder_list.
856 * If found, increment the reference count and return the pointer.
857 * If not found, returns NULL.
858 */
Andrew Morton36a561d2006-10-30 22:07:03 -0800859static struct bd_holder *find_bd_holder(struct block_device *bdev,
860 struct bd_holder *bo)
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500861{
862 struct bd_holder *tmp;
863
864 list_for_each_entry(tmp, &bdev->bd_holder_list, list)
865 if (tmp->sdir == bo->sdir) {
866 tmp->count++;
867 return tmp;
868 }
869
870 return NULL;
871}
872
873/**
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800874 * add_bd_holder - create sysfs symlinks for bd_claim() relationship
875 *
876 * @bdev: block device to be bd_claimed
877 * @bo: preallocated and initialized by alloc_bd_holder()
878 *
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500879 * Add @bo to @bdev->bd_holder_list, create symlinks.
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800880 *
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500881 * Returns 0 if symlinks are created.
882 * Returns -ve if something fails.
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800883 */
884static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
885{
Johannes Weiner4e916722007-07-15 23:41:25 -0700886 int err;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800887
888 if (!bo)
Andrew Morton4d7dd8f2006-09-29 01:58:56 -0700889 return -EINVAL;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800890
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800891 if (!bd_holder_grab_dirs(bdev, bo))
Andrew Morton4d7dd8f2006-09-29 01:58:56 -0700892 return -EBUSY;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800893
Johannes Weiner4e916722007-07-15 23:41:25 -0700894 err = add_symlink(bo->sdir, bo->sdev);
895 if (err)
896 return err;
897
898 err = add_symlink(bo->hdir, bo->hdev);
899 if (err) {
900 del_symlink(bo->sdir, bo->sdev);
901 return err;
Andrew Morton4d7dd8f2006-09-29 01:58:56 -0700902 }
Johannes Weiner4e916722007-07-15 23:41:25 -0700903
904 list_add_tail(&bo->list, &bdev->bd_holder_list);
905 return 0;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800906}
907
908/**
909 * del_bd_holder - delete sysfs symlinks for bd_claim() relationship
910 *
911 * @bdev: block device to be bd_claimed
912 * @kobj: holder's kobject
913 *
914 * If there is matching entry with @kobj in @bdev->bd_holder_list
915 * and no other bd_claim() from the same kobject,
916 * remove the struct bd_holder from the list, delete symlinks for it.
917 *
918 * Returns a pointer to the struct bd_holder when it's removed from the list
919 * and ready to be freed.
920 * Returns NULL if matching claim isn't found or there is other bd_claim()
921 * by the same kobject.
922 */
923static struct bd_holder *del_bd_holder(struct block_device *bdev,
924 struct kobject *kobj)
925{
926 struct bd_holder *bo;
927
928 list_for_each_entry(bo, &bdev->bd_holder_list, list) {
929 if (bo->sdir == kobj) {
930 bo->count--;
931 BUG_ON(bo->count < 0);
932 if (!bo->count) {
933 list_del(&bo->list);
934 del_symlink(bo->sdir, bo->sdev);
935 del_symlink(bo->hdir, bo->hdev);
936 bd_holder_release_dirs(bo);
937 return bo;
938 }
939 break;
940 }
941 }
942
943 return NULL;
944}
945
946/**
947 * bd_claim_by_kobject - bd_claim() with additional kobject signature
948 *
949 * @bdev: block device to be claimed
950 * @holder: holder's signature
951 * @kobj: holder's kobject
952 *
953 * Do bd_claim() and if it succeeds, create sysfs symlinks between
954 * the bdev and the holder's kobject.
955 * Use bd_release_from_kobject() when relesing the claimed bdev.
956 *
957 * Returns 0 on success. (same as bd_claim())
958 * Returns errno on failure.
959 */
960static int bd_claim_by_kobject(struct block_device *bdev, void *holder,
961 struct kobject *kobj)
962{
Johannes Weiner4e916722007-07-15 23:41:25 -0700963 int err;
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500964 struct bd_holder *bo, *found;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800965
966 if (!kobj)
967 return -EINVAL;
968
969 bo = alloc_bd_holder(kobj);
970 if (!bo)
971 return -ENOMEM;
972
Peter Zijlstra2e7b6512006-12-08 02:36:13 -0800973 mutex_lock(&bdev->bd_mutex);
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500974
Johannes Weiner4e916722007-07-15 23:41:25 -0700975 err = bd_claim(bdev, holder);
976 if (err)
Andrew Morton4210df22007-07-15 23:41:28 -0700977 goto fail;
Johannes Weiner4e916722007-07-15 23:41:25 -0700978
979 found = find_bd_holder(bdev, bo);
980 if (found)
Andrew Morton4210df22007-07-15 23:41:28 -0700981 goto fail;
Johannes Weiner4e916722007-07-15 23:41:25 -0700982
983 err = add_bd_holder(bdev, bo);
984 if (err)
985 bd_release(bdev);
Andrew Morton4210df22007-07-15 23:41:28 -0700986 else
987 bo = NULL;
988fail:
Jun'ichi Nomurab4cf1b72006-03-27 01:18:00 -0800989 mutex_unlock(&bdev->bd_mutex);
Andrew Morton4210df22007-07-15 23:41:28 -0700990 free_bd_holder(bo);
Johannes Weiner4e916722007-07-15 23:41:25 -0700991 return err;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800992}
993
994/**
995 * bd_release_from_kobject - bd_release() with additional kobject signature
996 *
997 * @bdev: block device to be released
998 * @kobj: holder's kobject
999 *
1000 * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject().
1001 */
1002static void bd_release_from_kobject(struct block_device *bdev,
1003 struct kobject *kobj)
1004{
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001005 if (!kobj)
1006 return;
1007
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001008 mutex_lock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001009 bd_release(bdev);
Andrew Morton4210df22007-07-15 23:41:28 -07001010 free_bd_holder(del_bd_holder(bdev, kobj));
Jun'ichi Nomurab4cf1b72006-03-27 01:18:00 -08001011 mutex_unlock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001012}
1013
1014/**
1015 * bd_claim_by_disk - wrapper function for bd_claim_by_kobject()
1016 *
1017 * @bdev: block device to be claimed
1018 * @holder: holder's signature
1019 * @disk: holder's gendisk
1020 *
1021 * Call bd_claim_by_kobject() with getting @disk->slave_dir.
1022 */
1023int bd_claim_by_disk(struct block_device *bdev, void *holder,
1024 struct gendisk *disk)
1025{
1026 return bd_claim_by_kobject(bdev, holder, kobject_get(disk->slave_dir));
1027}
1028EXPORT_SYMBOL_GPL(bd_claim_by_disk);
1029
1030/**
1031 * bd_release_from_disk - wrapper function for bd_release_from_kobject()
1032 *
1033 * @bdev: block device to be claimed
1034 * @disk: holder's gendisk
1035 *
1036 * Call bd_release_from_kobject() and put @disk->slave_dir.
1037 */
1038void bd_release_from_disk(struct block_device *bdev, struct gendisk *disk)
1039{
1040 bd_release_from_kobject(bdev, disk->slave_dir);
1041 kobject_put(disk->slave_dir);
1042}
1043EXPORT_SYMBOL_GPL(bd_release_from_disk);
1044#endif
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046/*
1047 * Tries to open block device by device number. Use it ONLY if you
1048 * really do not have anything better - i.e. when you are behind a
1049 * truly sucky interface and all you are given is a device number. _Never_
1050 * to be used for internal purposes. If you ever need it - reconsider
1051 * your API.
1052 */
1053struct block_device *open_by_devnum(dev_t dev, unsigned mode)
1054{
1055 struct block_device *bdev = bdget(dev);
1056 int err = -ENOMEM;
1057 int flags = mode & FMODE_WRITE ? O_RDWR : O_RDONLY;
1058 if (bdev)
1059 err = blkdev_get(bdev, mode, flags);
1060 return err ? ERR_PTR(err) : bdev;
1061}
1062
1063EXPORT_SYMBOL(open_by_devnum);
1064
1065/*
1066 * This routine checks whether a removable media has been changed,
1067 * and invalidates all buffer-cache-entries in that case. This
1068 * is a relatively slow routine, so we have to try to minimize using
1069 * it. Thus it is called only upon a 'mount' or 'open'. This
1070 * is the best way of combining speed and utility, I think.
1071 * People changing diskettes in the middle of an operation deserve
1072 * to lose :-)
1073 */
1074int check_disk_change(struct block_device *bdev)
1075{
1076 struct gendisk *disk = bdev->bd_disk;
1077 struct block_device_operations * bdops = disk->fops;
1078
1079 if (!bdops->media_changed)
1080 return 0;
1081 if (!bdops->media_changed(bdev->bd_disk))
1082 return 0;
1083
Christoph Hellwig2ef41632005-05-05 16:15:59 -07001084 if (__invalidate_device(bdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 printk("VFS: busy inodes on changed media.\n");
1086
1087 if (bdops->revalidate_disk)
1088 bdops->revalidate_disk(bdev->bd_disk);
1089 if (bdev->bd_disk->minors > 1)
1090 bdev->bd_invalidated = 1;
1091 return 1;
1092}
1093
1094EXPORT_SYMBOL(check_disk_change);
1095
1096void bd_set_size(struct block_device *bdev, loff_t size)
1097{
1098 unsigned bsize = bdev_hardsect_size(bdev);
1099
1100 bdev->bd_inode->i_size = size;
1101 while (bsize < PAGE_CACHE_SIZE) {
1102 if (size & bsize)
1103 break;
1104 bsize <<= 1;
1105 }
1106 bdev->bd_block_size = bsize;
1107 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
1108}
1109EXPORT_SYMBOL(bd_set_size);
1110
NeilBrown37be4122006-12-08 02:36:16 -08001111static int __blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags,
1112 int for_part);
Peter Zijlstrac48f70c2006-12-08 02:36:18 -08001113static int __blkdev_put(struct block_device *bdev, int for_part);
NeilBrown37be4122006-12-08 02:36:16 -08001114
Peter Zijlstra6d740cd2007-02-20 13:58:18 -08001115/*
1116 * bd_mutex locking:
1117 *
1118 * mutex_lock(part->bd_mutex)
1119 * mutex_lock_nested(whole->bd_mutex, 1)
1120 */
1121
NeilBrown37be4122006-12-08 02:36:16 -08001122static int do_open(struct block_device *bdev, struct file *file, int for_part)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
1124 struct module *owner = NULL;
1125 struct gendisk *disk;
1126 int ret = -ENXIO;
1127 int part;
1128
1129 file->f_mapping = bdev->bd_inode->i_mapping;
1130 lock_kernel();
1131 disk = get_gendisk(bdev->bd_dev, &part);
1132 if (!disk) {
1133 unlock_kernel();
1134 bdput(bdev);
1135 return ret;
1136 }
1137 owner = disk->fops->owner;
1138
NeilBrown6796bf52006-12-08 02:36:16 -08001139 mutex_lock_nested(&bdev->bd_mutex, for_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (!bdev->bd_openers) {
1141 bdev->bd_disk = disk;
1142 bdev->bd_contains = bdev;
1143 if (!part) {
1144 struct backing_dev_info *bdi;
1145 if (disk->fops->open) {
1146 ret = disk->fops->open(bdev->bd_inode, file);
1147 if (ret)
1148 goto out_first;
1149 }
1150 if (!bdev->bd_openers) {
1151 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
1152 bdi = blk_get_backing_dev_info(bdev);
1153 if (bdi == NULL)
1154 bdi = &default_backing_dev_info;
1155 bdev->bd_inode->i_data.backing_dev_info = bdi;
1156 }
1157 if (bdev->bd_invalidated)
1158 rescan_partitions(disk, bdev);
1159 } else {
1160 struct hd_struct *p;
1161 struct block_device *whole;
1162 whole = bdget_disk(disk, 0);
1163 ret = -ENOMEM;
1164 if (!whole)
1165 goto out_first;
NeilBrown37be4122006-12-08 02:36:16 -08001166 BUG_ON(for_part);
1167 ret = __blkdev_get(whole, file->f_mode, file->f_flags, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (ret)
1169 goto out_first;
1170 bdev->bd_contains = whole;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 p = disk->part[part - 1];
1172 bdev->bd_inode->i_data.backing_dev_info =
1173 whole->bd_inode->i_data.backing_dev_info;
1174 if (!(disk->flags & GENHD_FL_UP) || !p || !p->nr_sects) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 ret = -ENXIO;
1176 goto out_first;
1177 }
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001178 kobject_get(&p->dev.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 bdev->bd_part = p;
1180 bd_set_size(bdev, (loff_t) p->nr_sects << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
1182 } else {
1183 put_disk(disk);
1184 module_put(owner);
1185 if (bdev->bd_contains == bdev) {
1186 if (bdev->bd_disk->fops->open) {
1187 ret = bdev->bd_disk->fops->open(bdev->bd_inode, file);
1188 if (ret)
1189 goto out;
1190 }
1191 if (bdev->bd_invalidated)
1192 rescan_partitions(bdev->bd_disk, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
1194 }
1195 bdev->bd_openers++;
NeilBrown37be4122006-12-08 02:36:16 -08001196 if (for_part)
1197 bdev->bd_part_count++;
Arjan van de Venc039e312006-03-23 03:00:28 -08001198 mutex_unlock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 unlock_kernel();
1200 return 0;
1201
1202out_first:
1203 bdev->bd_disk = NULL;
1204 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
1205 if (bdev != bdev->bd_contains)
Peter Zijlstrac48f70c2006-12-08 02:36:18 -08001206 __blkdev_put(bdev->bd_contains, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 bdev->bd_contains = NULL;
1208 put_disk(disk);
1209 module_put(owner);
1210out:
Arjan van de Venc039e312006-03-23 03:00:28 -08001211 mutex_unlock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 unlock_kernel();
1213 if (ret)
1214 bdput(bdev);
1215 return ret;
1216}
1217
NeilBrown37be4122006-12-08 02:36:16 -08001218static int __blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags,
1219 int for_part)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
1221 /*
1222 * This crockload is due to bad choice of ->open() type.
1223 * It will go away.
1224 * For now, block device ->open() routine must _not_
1225 * examine anything in 'inode' argument except ->i_rdev.
1226 */
1227 struct file fake_file = {};
1228 struct dentry fake_dentry = {};
1229 fake_file.f_mode = mode;
1230 fake_file.f_flags = flags;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001231 fake_file.f_path.dentry = &fake_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 fake_dentry.d_inode = bdev->bd_inode;
1233
NeilBrown37be4122006-12-08 02:36:16 -08001234 return do_open(bdev, &fake_file, for_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
NeilBrown37be4122006-12-08 02:36:16 -08001237int blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags)
1238{
1239 return __blkdev_get(bdev, mode, flags, 0);
1240}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241EXPORT_SYMBOL(blkdev_get);
1242
1243static int blkdev_open(struct inode * inode, struct file * filp)
1244{
1245 struct block_device *bdev;
1246 int res;
1247
1248 /*
1249 * Preserve backwards compatibility and allow large file access
1250 * even if userspace doesn't ask for it explicitly. Some mkfs
1251 * binary needs it. We might want to drop this workaround
1252 * during an unstable branch.
1253 */
1254 filp->f_flags |= O_LARGEFILE;
1255
1256 bdev = bd_acquire(inode);
Pavel Emelianov6a2aae02006-10-28 10:38:33 -07001257 if (bdev == NULL)
1258 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
NeilBrown37be4122006-12-08 02:36:16 -08001260 res = do_open(bdev, filp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 if (res)
1262 return res;
1263
1264 if (!(filp->f_flags & O_EXCL) )
1265 return 0;
1266
1267 if (!(res = bd_claim(bdev, filp)))
1268 return 0;
1269
1270 blkdev_put(bdev);
1271 return res;
1272}
1273
NeilBrown37be4122006-12-08 02:36:16 -08001274static int __blkdev_put(struct block_device *bdev, int for_part)
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001275{
1276 int ret = 0;
1277 struct inode *bd_inode = bdev->bd_inode;
1278 struct gendisk *disk = bdev->bd_disk;
NeilBrown37be4122006-12-08 02:36:16 -08001279 struct block_device *victim = NULL;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001280
NeilBrown6796bf52006-12-08 02:36:16 -08001281 mutex_lock_nested(&bdev->bd_mutex, for_part);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001282 lock_kernel();
NeilBrown37be4122006-12-08 02:36:16 -08001283 if (for_part)
1284 bdev->bd_part_count--;
1285
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001286 if (!--bdev->bd_openers) {
1287 sync_blockdev(bdev);
1288 kill_bdev(bdev);
1289 }
1290 if (bdev->bd_contains == bdev) {
1291 if (disk->fops->release)
1292 ret = disk->fops->release(bd_inode, NULL);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001293 }
1294 if (!bdev->bd_openers) {
1295 struct module *owner = disk->fops->owner;
1296
1297 put_disk(disk);
1298 module_put(owner);
1299
1300 if (bdev->bd_contains != bdev) {
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001301 kobject_put(&bdev->bd_part->dev.kobj);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001302 bdev->bd_part = NULL;
1303 }
1304 bdev->bd_disk = NULL;
1305 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
NeilBrown37be4122006-12-08 02:36:16 -08001306 if (bdev != bdev->bd_contains)
1307 victim = bdev->bd_contains;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001308 bdev->bd_contains = NULL;
1309 }
1310 unlock_kernel();
1311 mutex_unlock(&bdev->bd_mutex);
1312 bdput(bdev);
NeilBrown37be4122006-12-08 02:36:16 -08001313 if (victim)
1314 __blkdev_put(victim, 1);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001315 return ret;
1316}
1317
NeilBrown37be4122006-12-08 02:36:16 -08001318int blkdev_put(struct block_device *bdev)
1319{
1320 return __blkdev_put(bdev, 0);
1321}
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001322EXPORT_SYMBOL(blkdev_put);
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324static int blkdev_close(struct inode * inode, struct file * filp)
1325{
1326 struct block_device *bdev = I_BDEV(filp->f_mapping->host);
1327 if (bdev->bd_holder == filp)
1328 bd_release(bdev);
1329 return blkdev_put(bdev);
1330}
1331
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07001332static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
1334 return blkdev_ioctl(file->f_mapping->host, file, cmd, arg);
1335}
1336
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001337const struct address_space_operations def_blk_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 .readpage = blkdev_readpage,
1339 .writepage = blkdev_writepage,
1340 .sync_page = block_sync_page,
Nick Piggin6272b5a2007-10-16 01:25:04 -07001341 .write_begin = blkdev_write_begin,
1342 .write_end = blkdev_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 .writepages = generic_writepages,
1344 .direct_IO = blkdev_direct_IO,
1345};
1346
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001347const struct file_operations def_blk_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 .open = blkdev_open,
1349 .release = blkdev_close,
1350 .llseek = block_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -07001351 .read = do_sync_read,
1352 .write = do_sync_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 .aio_read = generic_file_aio_read,
Badari Pulavarty027445c2006-09-30 23:28:46 -07001354 .aio_write = generic_file_aio_write_nolock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 .mmap = generic_file_mmap,
1356 .fsync = block_fsync,
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07001357 .unlocked_ioctl = block_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358#ifdef CONFIG_COMPAT
1359 .compat_ioctl = compat_blkdev_ioctl,
1360#endif
Jens Axboe7f9c51f2006-05-01 19:59:32 +02001361 .splice_read = generic_file_splice_read,
1362 .splice_write = generic_file_splice_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363};
1364
1365int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
1366{
1367 int res;
1368 mm_segment_t old_fs = get_fs();
1369 set_fs(KERNEL_DS);
1370 res = blkdev_ioctl(bdev->bd_inode, NULL, cmd, arg);
1371 set_fs(old_fs);
1372 return res;
1373}
1374
1375EXPORT_SYMBOL(ioctl_by_bdev);
1376
1377/**
1378 * lookup_bdev - lookup a struct block_device by name
1379 *
1380 * @path: special file representing the block device
1381 *
1382 * Get a reference to the blockdevice at @path in the current
1383 * namespace if possible and return it. Return ERR_PTR(error)
1384 * otherwise.
1385 */
1386struct block_device *lookup_bdev(const char *path)
1387{
1388 struct block_device *bdev;
1389 struct inode *inode;
1390 struct nameidata nd;
1391 int error;
1392
1393 if (!path || !*path)
1394 return ERR_PTR(-EINVAL);
1395
1396 error = path_lookup(path, LOOKUP_FOLLOW, &nd);
1397 if (error)
1398 return ERR_PTR(error);
1399
1400 inode = nd.dentry->d_inode;
1401 error = -ENOTBLK;
1402 if (!S_ISBLK(inode->i_mode))
1403 goto fail;
1404 error = -EACCES;
1405 if (nd.mnt->mnt_flags & MNT_NODEV)
1406 goto fail;
1407 error = -ENOMEM;
1408 bdev = bd_acquire(inode);
1409 if (!bdev)
1410 goto fail;
1411out:
1412 path_release(&nd);
1413 return bdev;
1414fail:
1415 bdev = ERR_PTR(error);
1416 goto out;
1417}
1418
1419/**
1420 * open_bdev_excl - open a block device by name and set it up for use
1421 *
1422 * @path: special file representing the block device
1423 * @flags: %MS_RDONLY for opening read-only
1424 * @holder: owner for exclusion
1425 *
1426 * Open the blockdevice described by the special file at @path, claim it
1427 * for the @holder.
1428 */
1429struct block_device *open_bdev_excl(const char *path, int flags, void *holder)
1430{
1431 struct block_device *bdev;
1432 mode_t mode = FMODE_READ;
1433 int error = 0;
1434
1435 bdev = lookup_bdev(path);
1436 if (IS_ERR(bdev))
1437 return bdev;
1438
1439 if (!(flags & MS_RDONLY))
1440 mode |= FMODE_WRITE;
1441 error = blkdev_get(bdev, mode, 0);
1442 if (error)
1443 return ERR_PTR(error);
1444 error = -EACCES;
1445 if (!(flags & MS_RDONLY) && bdev_read_only(bdev))
1446 goto blkdev_put;
1447 error = bd_claim(bdev, holder);
1448 if (error)
1449 goto blkdev_put;
1450
1451 return bdev;
1452
1453blkdev_put:
1454 blkdev_put(bdev);
1455 return ERR_PTR(error);
1456}
1457
1458EXPORT_SYMBOL(open_bdev_excl);
1459
1460/**
1461 * close_bdev_excl - release a blockdevice openen by open_bdev_excl()
1462 *
1463 * @bdev: blockdevice to close
1464 *
1465 * This is the counterpart to open_bdev_excl().
1466 */
1467void close_bdev_excl(struct block_device *bdev)
1468{
1469 bd_release(bdev);
1470 blkdev_put(bdev);
1471}
1472
1473EXPORT_SYMBOL(close_bdev_excl);
David Howellsb71e8a42006-08-29 19:06:11 +01001474
1475int __invalidate_device(struct block_device *bdev)
1476{
1477 struct super_block *sb = get_super(bdev);
1478 int res = 0;
1479
1480 if (sb) {
1481 /*
1482 * no need to lock the super, get_super holds the
1483 * read mutex so the filesystem cannot go away
1484 * under us (->put_super runs with the write lock
1485 * hold).
1486 */
1487 shrink_dcache_sb(sb);
1488 res = invalidate_inodes(sb);
1489 drop_super(sb);
1490 }
Peter Zijlstraf98393a2007-05-06 14:49:54 -07001491 invalidate_bdev(bdev);
David Howellsb71e8a42006-08-29 19:06:11 +01001492 return res;
1493}
1494EXPORT_SYMBOL(__invalidate_device);