blob: 8335f0e1b0fba4ff98e7933b6b557910b53efbce [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
Adrian Bunk4c54ac62008-02-18 13:48:31 +010034static const struct address_space_operations def_blk_aops;
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static inline struct bdev_inode *BDEV_I(struct inode *inode)
37{
38 return container_of(inode, struct bdev_inode, vfs_inode);
39}
40
41inline struct block_device *I_BDEV(struct inode *inode)
42{
43 return &BDEV_I(inode)->bdev;
44}
45
46EXPORT_SYMBOL(I_BDEV);
47
48static sector_t max_block(struct block_device *bdev)
49{
50 sector_t retval = ~((sector_t)0);
51 loff_t sz = i_size_read(bdev->bd_inode);
52
53 if (sz) {
54 unsigned int size = block_size(bdev);
55 unsigned int sizebits = blksize_bits(size);
56 retval = (sz >> sizebits);
57 }
58 return retval;
59}
60
Peter Zijlstraf9a14392007-05-06 14:49:55 -070061/* Kill _all_ buffers and pagecache , dirty or not.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static void kill_bdev(struct block_device *bdev)
63{
Peter Zijlstraf9a14392007-05-06 14:49:55 -070064 if (bdev->bd_inode->i_mapping->nrpages == 0)
65 return;
66 invalidate_bh_lrus();
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
68}
69
70int set_blocksize(struct block_device *bdev, int size)
71{
72 /* Size must be a power of two, and between 512 and PAGE_SIZE */
Vignesh Babu BM1368c4f2007-05-08 00:24:32 -070073 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return -EINVAL;
75
76 /* Size cannot be smaller than the size supported by the device */
77 if (size < bdev_hardsect_size(bdev))
78 return -EINVAL;
79
80 /* Don't change the size if it is same as current */
81 if (bdev->bd_block_size != size) {
82 sync_blockdev(bdev);
83 bdev->bd_block_size = size;
84 bdev->bd_inode->i_blkbits = blksize_bits(size);
85 kill_bdev(bdev);
86 }
87 return 0;
88}
89
90EXPORT_SYMBOL(set_blocksize);
91
92int sb_set_blocksize(struct super_block *sb, int size)
93{
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 if (set_blocksize(sb->s_bdev, size))
95 return 0;
96 /* If we get here, we know size is power of two
97 * and it's value is between 512 and PAGE_SIZE */
98 sb->s_blocksize = size;
Coywolf Qi Hunt38885bd2006-03-24 03:18:05 -080099 sb->s_blocksize_bits = blksize_bits(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return sb->s_blocksize;
101}
102
103EXPORT_SYMBOL(sb_set_blocksize);
104
105int sb_min_blocksize(struct super_block *sb, int size)
106{
107 int minsize = bdev_hardsect_size(sb->s_bdev);
108 if (size < minsize)
109 size = minsize;
110 return sb_set_blocksize(sb, size);
111}
112
113EXPORT_SYMBOL(sb_min_blocksize);
114
115static int
116blkdev_get_block(struct inode *inode, sector_t iblock,
117 struct buffer_head *bh, int create)
118{
119 if (iblock >= max_block(I_BDEV(inode))) {
120 if (create)
121 return -EIO;
122
123 /*
124 * for reads, we're just trying to fill a partial page.
125 * return a hole, they will have to call get_block again
126 * before they can fill it, and they will get -EIO at that
127 * time
128 */
129 return 0;
130 }
131 bh->b_bdev = I_BDEV(inode);
132 bh->b_blocknr = iblock;
133 set_buffer_mapped(bh);
134 return 0;
135}
136
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800137static int
138blkdev_get_blocks(struct inode *inode, sector_t iblock,
139 struct buffer_head *bh, int create)
140{
141 sector_t end_block = max_block(I_BDEV(inode));
142 unsigned long max_blocks = bh->b_size >> inode->i_blkbits;
143
144 if ((iblock + max_blocks) > end_block) {
145 max_blocks = end_block - iblock;
146 if ((long)max_blocks <= 0) {
147 if (create)
148 return -EIO; /* write fully beyond EOF */
149 /*
150 * It is a read which is fully beyond EOF. We return
151 * a !buffer_mapped buffer
152 */
153 max_blocks = 0;
154 }
155 }
156
157 bh->b_bdev = I_BDEV(inode);
158 bh->b_blocknr = iblock;
159 bh->b_size = max_blocks << inode->i_blkbits;
160 if (max_blocks)
161 set_buffer_mapped(bh);
162 return 0;
163}
164
165static ssize_t
166blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
167 loff_t offset, unsigned long nr_segs)
168{
169 struct file *file = iocb->ki_filp;
170 struct inode *inode = file->f_mapping->host;
171
172 return blockdev_direct_IO_no_locking(rw, iocb, inode, I_BDEV(inode),
173 iov, offset, nr_segs, blkdev_get_blocks, NULL);
174}
175
176#if 0
NeilBrown6712ecf2007-09-27 12:47:43 +0200177static void blk_end_aio(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Chen, Kenneth We61c9012006-12-13 00:34:36 -0800179 struct kiocb *iocb = bio->bi_private;
180 atomic_t *bio_count = &iocb->ki_bio_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Chen, Kenneth We61c9012006-12-13 00:34:36 -0800182 if (bio_data_dir(bio) == READ)
183 bio_check_pages_dirty(bio);
184 else {
185 bio_release_pages(bio);
186 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188
Chen, Kenneth We61c9012006-12-13 00:34:36 -0800189 /* iocb->ki_nbytes stores error code from LLDD */
190 if (error)
191 iocb->ki_nbytes = -EIO;
192
193 if (atomic_dec_and_test(bio_count)) {
Andrew Morton790816d2007-01-22 20:40:35 -0800194 if ((long)iocb->ki_nbytes < 0)
Chen, Kenneth We61c9012006-12-13 00:34:36 -0800195 aio_complete(iocb, iocb->ki_nbytes, 0);
196 else
197 aio_complete(iocb, iocb->ki_left, 0);
198 }
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return 0;
201}
202
Chen, Kenneth We61c9012006-12-13 00:34:36 -0800203#define VEC_SIZE 16
204struct pvec {
205 unsigned short nr;
206 unsigned short idx;
207 struct page *page[VEC_SIZE];
208};
209
210#define PAGES_SPANNED(addr, len) \
211 (DIV_ROUND_UP((addr) + (len), PAGE_SIZE) - (addr) / PAGE_SIZE);
212
213/*
214 * get page pointer for user addr, we internally cache struct page array for
215 * (addr, count) range in pvec to avoid frequent call to get_user_pages. If
216 * internal page list is exhausted, a batch count of up to VEC_SIZE is used
217 * to get next set of page struct.
218 */
219static struct page *blk_get_page(unsigned long addr, size_t count, int rw,
220 struct pvec *pvec)
221{
222 int ret, nr_pages;
223 if (pvec->idx == pvec->nr) {
224 nr_pages = PAGES_SPANNED(addr, count);
225 nr_pages = min(nr_pages, VEC_SIZE);
226 down_read(&current->mm->mmap_sem);
227 ret = get_user_pages(current, current->mm, addr, nr_pages,
228 rw == READ, 0, pvec->page, NULL);
229 up_read(&current->mm->mmap_sem);
230 if (ret < 0)
231 return ERR_PTR(ret);
232 pvec->nr = ret;
233 pvec->idx = 0;
234 }
235 return pvec->page[pvec->idx++];
236}
237
Chen, Kenneth Wcda92052007-01-22 20:40:43 -0800238/* return a page back to pvec array */
239static void blk_unget_page(struct page *page, struct pvec *pvec)
240{
241 pvec->page[--pvec->idx] = page;
242}
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244static ssize_t
245blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
Chen, Kenneth We61c9012006-12-13 00:34:36 -0800246 loff_t pos, unsigned long nr_segs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Chen, Kenneth We61c9012006-12-13 00:34:36 -0800248 struct inode *inode = iocb->ki_filp->f_mapping->host;
249 unsigned blkbits = blksize_bits(bdev_hardsect_size(I_BDEV(inode)));
250 unsigned blocksize_mask = (1 << blkbits) - 1;
251 unsigned long seg = 0; /* iov segment iterator */
252 unsigned long nvec; /* number of bio vec needed */
253 unsigned long cur_off; /* offset into current page */
254 unsigned long cur_len; /* I/O len of current page, up to PAGE_SIZE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Chen, Kenneth We61c9012006-12-13 00:34:36 -0800256 unsigned long addr; /* user iovec address */
257 size_t count; /* user iovec len */
258 size_t nbytes = iocb->ki_nbytes = iocb->ki_left; /* total xfer size */
259 loff_t size; /* size of block device */
260 struct bio *bio;
261 atomic_t *bio_count = &iocb->ki_bio_count;
262 struct page *page;
263 struct pvec pvec;
264
265 pvec.nr = 0;
266 pvec.idx = 0;
267
268 if (pos & blocksize_mask)
269 return -EINVAL;
270
271 size = i_size_read(inode);
272 if (pos + nbytes > size) {
273 nbytes = size - pos;
274 iocb->ki_left = nbytes;
275 }
276
277 /*
278 * check first non-zero iov alignment, the remaining
279 * iov alignment is checked inside bio loop below.
280 */
281 do {
282 addr = (unsigned long) iov[seg].iov_base;
283 count = min(iov[seg].iov_len, nbytes);
284 if (addr & blocksize_mask || count & blocksize_mask)
285 return -EINVAL;
286 } while (!count && ++seg < nr_segs);
287 atomic_set(bio_count, 1);
288
289 while (nbytes) {
290 /* roughly estimate number of bio vec needed */
291 nvec = (nbytes + PAGE_SIZE - 1) / PAGE_SIZE;
292 nvec = max(nvec, nr_segs - seg);
293 nvec = min(nvec, (unsigned long) BIO_MAX_PAGES);
294
295 /* bio_alloc should not fail with GFP_KERNEL flag */
296 bio = bio_alloc(GFP_KERNEL, nvec);
297 bio->bi_bdev = I_BDEV(inode);
298 bio->bi_end_io = blk_end_aio;
299 bio->bi_private = iocb;
300 bio->bi_sector = pos >> blkbits;
301same_bio:
302 cur_off = addr & ~PAGE_MASK;
303 cur_len = PAGE_SIZE - cur_off;
304 if (count < cur_len)
305 cur_len = count;
306
307 page = blk_get_page(addr, count, rw, &pvec);
308 if (unlikely(IS_ERR(page)))
309 goto backout;
310
311 if (bio_add_page(bio, page, cur_len, cur_off)) {
312 pos += cur_len;
313 addr += cur_len;
314 count -= cur_len;
315 nbytes -= cur_len;
316
317 if (count)
318 goto same_bio;
319 while (++seg < nr_segs) {
320 addr = (unsigned long) iov[seg].iov_base;
321 count = iov[seg].iov_len;
322 if (!count)
323 continue;
324 if (unlikely(addr & blocksize_mask ||
325 count & blocksize_mask)) {
326 page = ERR_PTR(-EINVAL);
327 goto backout;
328 }
329 count = min(count, nbytes);
330 goto same_bio;
331 }
Chen, Kenneth Wcda92052007-01-22 20:40:43 -0800332 } else {
333 blk_unget_page(page, &pvec);
Chen, Kenneth We61c9012006-12-13 00:34:36 -0800334 }
335
336 /* bio is ready, submit it */
337 if (rw == READ)
338 bio_set_pages_dirty(bio);
339 atomic_inc(bio_count);
340 submit_bio(rw, bio);
341 }
342
343completion:
344 iocb->ki_left -= nbytes;
345 nbytes = iocb->ki_left;
346 iocb->ki_pos += nbytes;
347
348 blk_run_address_space(inode->i_mapping);
349 if (atomic_dec_and_test(bio_count))
350 aio_complete(iocb, nbytes, 0);
351
352 return -EIOCBQUEUED;
353
354backout:
355 /*
356 * back out nbytes count constructed so far for this bio,
357 * we will throw away current bio.
358 */
359 nbytes += bio->bi_size;
360 bio_release_pages(bio);
361 bio_put(bio);
362
363 /*
364 * if no bio was submmitted, return the error code.
365 * otherwise, proceed with pending I/O completion.
366 */
367 if (atomic_read(bio_count) == 1)
368 return PTR_ERR(page);
369 goto completion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800371#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
374{
375 return block_write_full_page(page, blkdev_get_block, wbc);
376}
377
378static int blkdev_readpage(struct file * file, struct page * page)
379{
380 return block_read_full_page(page, blkdev_get_block);
381}
382
Nick Piggin6272b5a2007-10-16 01:25:04 -0700383static int blkdev_write_begin(struct file *file, struct address_space *mapping,
384 loff_t pos, unsigned len, unsigned flags,
385 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Nick Piggin6272b5a2007-10-16 01:25:04 -0700387 *pagep = NULL;
388 return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
389 blkdev_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
Nick Piggin6272b5a2007-10-16 01:25:04 -0700392static int blkdev_write_end(struct file *file, struct address_space *mapping,
393 loff_t pos, unsigned len, unsigned copied,
394 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Nick Piggin6272b5a2007-10-16 01:25:04 -0700396 int ret;
397 ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
398
399 unlock_page(page);
400 page_cache_release(page);
401
402 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
405/*
406 * private llseek:
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800407 * for a block special file file->f_path.dentry->d_inode->i_size is zero
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 * so we compute the size by hand (just as in block_read/write above)
409 */
410static loff_t block_llseek(struct file *file, loff_t offset, int origin)
411{
412 struct inode *bd_inode = file->f_mapping->host;
413 loff_t size;
414 loff_t retval;
415
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800416 mutex_lock(&bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 size = i_size_read(bd_inode);
418
419 switch (origin) {
420 case 2:
421 offset += size;
422 break;
423 case 1:
424 offset += file->f_pos;
425 }
426 retval = -EINVAL;
427 if (offset >= 0 && offset <= size) {
428 if (offset != file->f_pos) {
429 file->f_pos = offset;
430 }
431 retval = offset;
432 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800433 mutex_unlock(&bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return retval;
435}
436
437/*
438 * Filp is never NULL; the only case when ->fsync() is called with
439 * NULL first argument is nfsd_sync_dir() and that's not a directory.
440 */
441
442static int block_fsync(struct file *filp, struct dentry *dentry, int datasync)
443{
444 return sync_blockdev(I_BDEV(filp->f_mapping->host));
445}
446
447/*
448 * pseudo-fs
449 */
450
451static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
Christoph Lametere18b8902006-12-06 20:33:20 -0800452static struct kmem_cache * bdev_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454static struct inode *bdev_alloc_inode(struct super_block *sb)
455{
Christoph Lametere94b1762006-12-06 20:33:17 -0800456 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (!ei)
458 return NULL;
459 return &ei->vfs_inode;
460}
461
462static void bdev_destroy_inode(struct inode *inode)
463{
464 struct bdev_inode *bdi = BDEV_I(inode);
465
466 bdi->bdev.bd_inode_backing_dev_info = NULL;
467 kmem_cache_free(bdev_cachep, bdi);
468}
469
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -0700470static void init_once(struct kmem_cache * cachep, void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 struct bdev_inode *ei = (struct bdev_inode *) foo;
473 struct block_device *bdev = &ei->bdev;
474
Christoph Lametera35afb82007-05-16 22:10:57 -0700475 memset(bdev, 0, sizeof(*bdev));
476 mutex_init(&bdev->bd_mutex);
477 sema_init(&bdev->bd_mount_sem, 1);
478 INIT_LIST_HEAD(&bdev->bd_inodes);
479 INIT_LIST_HEAD(&bdev->bd_list);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800480#ifdef CONFIG_SYSFS
Christoph Lametera35afb82007-05-16 22:10:57 -0700481 INIT_LIST_HEAD(&bdev->bd_holder_list);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800482#endif
Christoph Lametera35afb82007-05-16 22:10:57 -0700483 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
486static inline void __bd_forget(struct inode *inode)
487{
488 list_del_init(&inode->i_devices);
489 inode->i_bdev = NULL;
490 inode->i_mapping = &inode->i_data;
491}
492
493static void bdev_clear_inode(struct inode *inode)
494{
495 struct block_device *bdev = &BDEV_I(inode)->bdev;
496 struct list_head *p;
497 spin_lock(&bdev_lock);
498 while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
499 __bd_forget(list_entry(p, struct inode, i_devices));
500 }
501 list_del_init(&bdev->bd_list);
502 spin_unlock(&bdev_lock);
503}
504
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800505static const struct super_operations bdev_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 .statfs = simple_statfs,
507 .alloc_inode = bdev_alloc_inode,
508 .destroy_inode = bdev_destroy_inode,
509 .drop_inode = generic_delete_inode,
510 .clear_inode = bdev_clear_inode,
511};
512
David Howells454e2392006-06-23 02:02:57 -0700513static int bd_get_sb(struct file_system_type *fs_type,
514 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
David Howells454e2392006-06-23 02:02:57 -0700516 return get_sb_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
519static struct file_system_type bd_type = {
520 .name = "bdev",
521 .get_sb = bd_get_sb,
522 .kill_sb = kill_anon_super,
523};
524
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800525static struct vfsmount *bd_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526struct super_block *blockdev_superblock;
527
528void __init bdev_cache_init(void)
529{
530 int err;
531 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800532 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
533 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +0900534 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 err = register_filesystem(&bd_type);
536 if (err)
537 panic("Cannot register bdev pseudo-fs");
538 bd_mnt = kern_mount(&bd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (IS_ERR(bd_mnt))
540 panic("Cannot create bdev pseudo-fs");
541 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
542}
543
544/*
545 * Most likely _very_ bad one - but then it's hardly critical for small
546 * /dev and can be fixed when somebody will need really large one.
547 * Keep in mind that it will be fed through icache hash function too.
548 */
549static inline unsigned long hash(dev_t dev)
550{
551 return MAJOR(dev)+MINOR(dev);
552}
553
554static int bdev_test(struct inode *inode, void *data)
555{
556 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
557}
558
559static int bdev_set(struct inode *inode, void *data)
560{
561 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
562 return 0;
563}
564
565static LIST_HEAD(all_bdevs);
566
567struct block_device *bdget(dev_t dev)
568{
569 struct block_device *bdev;
570 struct inode *inode;
571
572 inode = iget5_locked(bd_mnt->mnt_sb, hash(dev),
573 bdev_test, bdev_set, &dev);
574
575 if (!inode)
576 return NULL;
577
578 bdev = &BDEV_I(inode)->bdev;
579
580 if (inode->i_state & I_NEW) {
581 bdev->bd_contains = NULL;
582 bdev->bd_inode = inode;
583 bdev->bd_block_size = (1 << inode->i_blkbits);
584 bdev->bd_part_count = 0;
585 bdev->bd_invalidated = 0;
586 inode->i_mode = S_IFBLK;
587 inode->i_rdev = dev;
588 inode->i_bdev = bdev;
589 inode->i_data.a_ops = &def_blk_aops;
590 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
591 inode->i_data.backing_dev_info = &default_backing_dev_info;
592 spin_lock(&bdev_lock);
593 list_add(&bdev->bd_list, &all_bdevs);
594 spin_unlock(&bdev_lock);
595 unlock_new_inode(inode);
596 }
597 return bdev;
598}
599
600EXPORT_SYMBOL(bdget);
601
602long nr_blockdev_pages(void)
603{
Matthias Kaehlcke203a2932007-07-15 23:40:23 -0700604 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 long ret = 0;
606 spin_lock(&bdev_lock);
Matthias Kaehlcke203a2932007-07-15 23:40:23 -0700607 list_for_each_entry(bdev, &all_bdevs, bd_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 ret += bdev->bd_inode->i_mapping->nrpages;
609 }
610 spin_unlock(&bdev_lock);
611 return ret;
612}
613
614void bdput(struct block_device *bdev)
615{
616 iput(bdev->bd_inode);
617}
618
619EXPORT_SYMBOL(bdput);
620
621static struct block_device *bd_acquire(struct inode *inode)
622{
623 struct block_device *bdev;
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 spin_lock(&bdev_lock);
626 bdev = inode->i_bdev;
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700627 if (bdev) {
628 atomic_inc(&bdev->bd_inode->i_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 spin_unlock(&bdev_lock);
630 return bdev;
631 }
632 spin_unlock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 bdev = bdget(inode->i_rdev);
635 if (bdev) {
636 spin_lock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700637 if (!inode->i_bdev) {
638 /*
639 * We take an additional bd_inode->i_count for inode,
640 * and it's released in clear_inode() of inode.
641 * So, we can access it via ->i_mapping always
642 * without igrab().
643 */
644 atomic_inc(&bdev->bd_inode->i_count);
645 inode->i_bdev = bdev;
646 inode->i_mapping = bdev->bd_inode->i_mapping;
647 list_add(&inode->i_devices, &bdev->bd_inodes);
648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 spin_unlock(&bdev_lock);
650 }
651 return bdev;
652}
653
654/* Call when you free inode */
655
656void bd_forget(struct inode *inode)
657{
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700658 struct block_device *bdev = NULL;
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 spin_lock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700661 if (inode->i_bdev) {
662 if (inode->i_sb != blockdev_superblock)
663 bdev = inode->i_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 __bd_forget(inode);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 spin_unlock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700667
668 if (bdev)
669 iput(bdev->bd_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
672int bd_claim(struct block_device *bdev, void *holder)
673{
674 int res;
675 spin_lock(&bdev_lock);
676
677 /* first decide result */
678 if (bdev->bd_holder == holder)
679 res = 0; /* already a holder */
680 else if (bdev->bd_holder != NULL)
681 res = -EBUSY; /* held by someone else */
682 else if (bdev->bd_contains == bdev)
683 res = 0; /* is a whole device which isn't held */
684
685 else if (bdev->bd_contains->bd_holder == bd_claim)
686 res = 0; /* is a partition of a device that is being partitioned */
687 else if (bdev->bd_contains->bd_holder != NULL)
688 res = -EBUSY; /* is a partition of a held device */
689 else
690 res = 0; /* is a partition of an un-held device */
691
692 /* now impose change */
693 if (res==0) {
694 /* note that for a whole device bd_holders
695 * will be incremented twice, and bd_holder will
696 * be set to bd_claim before being set to holder
697 */
698 bdev->bd_contains->bd_holders ++;
699 bdev->bd_contains->bd_holder = bd_claim;
700 bdev->bd_holders++;
701 bdev->bd_holder = holder;
702 }
703 spin_unlock(&bdev_lock);
704 return res;
705}
706
707EXPORT_SYMBOL(bd_claim);
708
709void bd_release(struct block_device *bdev)
710{
711 spin_lock(&bdev_lock);
712 if (!--bdev->bd_contains->bd_holders)
713 bdev->bd_contains->bd_holder = NULL;
714 if (!--bdev->bd_holders)
715 bdev->bd_holder = NULL;
716 spin_unlock(&bdev_lock);
717}
718
719EXPORT_SYMBOL(bd_release);
720
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800721#ifdef CONFIG_SYSFS
722/*
723 * Functions for bd_claim_by_kobject / bd_release_from_kobject
724 *
725 * If a kobject is passed to bd_claim_by_kobject()
726 * and the kobject has a parent directory,
727 * following symlinks are created:
728 * o from the kobject to the claimed bdev
729 * o from "holders" directory of the bdev to the parent of the kobject
730 * bd_release_from_kobject() removes these symlinks.
731 *
732 * Example:
733 * If /dev/dm-0 maps to /dev/sda, kobject corresponding to
734 * /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then:
735 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
736 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
737 */
738
739static struct kobject *bdev_get_kobj(struct block_device *bdev)
740{
741 if (bdev->bd_contains != bdev)
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200742 return kobject_get(&bdev->bd_part->dev.kobj);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800743 else
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200744 return kobject_get(&bdev->bd_disk->dev.kobj);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800745}
746
747static struct kobject *bdev_get_holder(struct block_device *bdev)
748{
749 if (bdev->bd_contains != bdev)
750 return kobject_get(bdev->bd_part->holder_dir);
751 else
752 return kobject_get(bdev->bd_disk->holder_dir);
753}
754
Andrew Morton4d7dd8fd2006-09-29 01:58:56 -0700755static int add_symlink(struct kobject *from, struct kobject *to)
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800756{
757 if (!from || !to)
Andrew Morton4d7dd8fd2006-09-29 01:58:56 -0700758 return 0;
759 return sysfs_create_link(from, to, kobject_name(to));
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800760}
761
762static void del_symlink(struct kobject *from, struct kobject *to)
763{
764 if (!from || !to)
765 return;
766 sysfs_remove_link(from, kobject_name(to));
767}
768
769/*
770 * 'struct bd_holder' contains pointers to kobjects symlinked by
771 * bd_claim_by_kobject.
772 * It's connected to bd_holder_list which is protected by bdev->bd_sem.
773 */
774struct bd_holder {
775 struct list_head list; /* chain of holders of the bdev */
776 int count; /* references from the holder */
777 struct kobject *sdir; /* holder object, e.g. "/block/dm-0/slaves" */
778 struct kobject *hdev; /* e.g. "/block/dm-0" */
779 struct kobject *hdir; /* e.g. "/block/sda/holders" */
780 struct kobject *sdev; /* e.g. "/block/sda" */
781};
782
783/*
784 * Get references of related kobjects at once.
785 * Returns 1 on success. 0 on failure.
786 *
787 * Should call bd_holder_release_dirs() after successful use.
788 */
789static int bd_holder_grab_dirs(struct block_device *bdev,
790 struct bd_holder *bo)
791{
792 if (!bdev || !bo)
793 return 0;
794
795 bo->sdir = kobject_get(bo->sdir);
796 if (!bo->sdir)
797 return 0;
798
799 bo->hdev = kobject_get(bo->sdir->parent);
800 if (!bo->hdev)
801 goto fail_put_sdir;
802
803 bo->sdev = bdev_get_kobj(bdev);
804 if (!bo->sdev)
805 goto fail_put_hdev;
806
807 bo->hdir = bdev_get_holder(bdev);
808 if (!bo->hdir)
809 goto fail_put_sdev;
810
811 return 1;
812
813fail_put_sdev:
814 kobject_put(bo->sdev);
815fail_put_hdev:
816 kobject_put(bo->hdev);
817fail_put_sdir:
818 kobject_put(bo->sdir);
819
820 return 0;
821}
822
823/* Put references of related kobjects at once. */
824static void bd_holder_release_dirs(struct bd_holder *bo)
825{
826 kobject_put(bo->hdir);
827 kobject_put(bo->sdev);
828 kobject_put(bo->hdev);
829 kobject_put(bo->sdir);
830}
831
832static struct bd_holder *alloc_bd_holder(struct kobject *kobj)
833{
834 struct bd_holder *bo;
835
836 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
837 if (!bo)
838 return NULL;
839
840 bo->count = 1;
841 bo->sdir = kobj;
842
843 return bo;
844}
845
846static void free_bd_holder(struct bd_holder *bo)
847{
848 kfree(bo);
849}
850
851/**
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500852 * find_bd_holder - find matching struct bd_holder from the block device
853 *
854 * @bdev: struct block device to be searched
855 * @bo: target struct bd_holder
856 *
857 * Returns matching entry with @bo in @bdev->bd_holder_list.
858 * If found, increment the reference count and return the pointer.
859 * If not found, returns NULL.
860 */
Andrew Morton36a561d2006-10-30 22:07:03 -0800861static struct bd_holder *find_bd_holder(struct block_device *bdev,
862 struct bd_holder *bo)
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500863{
864 struct bd_holder *tmp;
865
866 list_for_each_entry(tmp, &bdev->bd_holder_list, list)
867 if (tmp->sdir == bo->sdir) {
868 tmp->count++;
869 return tmp;
870 }
871
872 return NULL;
873}
874
875/**
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800876 * add_bd_holder - create sysfs symlinks for bd_claim() relationship
877 *
878 * @bdev: block device to be bd_claimed
879 * @bo: preallocated and initialized by alloc_bd_holder()
880 *
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500881 * Add @bo to @bdev->bd_holder_list, create symlinks.
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800882 *
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500883 * Returns 0 if symlinks are created.
884 * Returns -ve if something fails.
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800885 */
886static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
887{
Johannes Weiner4e916722007-07-15 23:41:25 -0700888 int err;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800889
890 if (!bo)
Andrew Morton4d7dd8fd2006-09-29 01:58:56 -0700891 return -EINVAL;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800892
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800893 if (!bd_holder_grab_dirs(bdev, bo))
Andrew Morton4d7dd8fd2006-09-29 01:58:56 -0700894 return -EBUSY;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800895
Johannes Weiner4e916722007-07-15 23:41:25 -0700896 err = add_symlink(bo->sdir, bo->sdev);
897 if (err)
898 return err;
899
900 err = add_symlink(bo->hdir, bo->hdev);
901 if (err) {
902 del_symlink(bo->sdir, bo->sdev);
903 return err;
Andrew Morton4d7dd8fd2006-09-29 01:58:56 -0700904 }
Johannes Weiner4e916722007-07-15 23:41:25 -0700905
906 list_add_tail(&bo->list, &bdev->bd_holder_list);
907 return 0;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800908}
909
910/**
911 * del_bd_holder - delete sysfs symlinks for bd_claim() relationship
912 *
913 * @bdev: block device to be bd_claimed
914 * @kobj: holder's kobject
915 *
916 * If there is matching entry with @kobj in @bdev->bd_holder_list
917 * and no other bd_claim() from the same kobject,
918 * remove the struct bd_holder from the list, delete symlinks for it.
919 *
920 * Returns a pointer to the struct bd_holder when it's removed from the list
921 * and ready to be freed.
922 * Returns NULL if matching claim isn't found or there is other bd_claim()
923 * by the same kobject.
924 */
925static struct bd_holder *del_bd_holder(struct block_device *bdev,
926 struct kobject *kobj)
927{
928 struct bd_holder *bo;
929
930 list_for_each_entry(bo, &bdev->bd_holder_list, list) {
931 if (bo->sdir == kobj) {
932 bo->count--;
933 BUG_ON(bo->count < 0);
934 if (!bo->count) {
935 list_del(&bo->list);
936 del_symlink(bo->sdir, bo->sdev);
937 del_symlink(bo->hdir, bo->hdev);
938 bd_holder_release_dirs(bo);
939 return bo;
940 }
941 break;
942 }
943 }
944
945 return NULL;
946}
947
948/**
949 * bd_claim_by_kobject - bd_claim() with additional kobject signature
950 *
951 * @bdev: block device to be claimed
952 * @holder: holder's signature
953 * @kobj: holder's kobject
954 *
955 * Do bd_claim() and if it succeeds, create sysfs symlinks between
956 * the bdev and the holder's kobject.
957 * Use bd_release_from_kobject() when relesing the claimed bdev.
958 *
959 * Returns 0 on success. (same as bd_claim())
960 * Returns errno on failure.
961 */
962static int bd_claim_by_kobject(struct block_device *bdev, void *holder,
963 struct kobject *kobj)
964{
Johannes Weiner4e916722007-07-15 23:41:25 -0700965 int err;
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500966 struct bd_holder *bo, *found;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800967
968 if (!kobj)
969 return -EINVAL;
970
971 bo = alloc_bd_holder(kobj);
972 if (!bo)
973 return -ENOMEM;
974
Peter Zijlstra2e7b6512006-12-08 02:36:13 -0800975 mutex_lock(&bdev->bd_mutex);
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500976
Johannes Weiner4e916722007-07-15 23:41:25 -0700977 err = bd_claim(bdev, holder);
978 if (err)
Andrew Morton4210df22007-07-15 23:41:28 -0700979 goto fail;
Johannes Weiner4e916722007-07-15 23:41:25 -0700980
981 found = find_bd_holder(bdev, bo);
982 if (found)
Andrew Morton4210df22007-07-15 23:41:28 -0700983 goto fail;
Johannes Weiner4e916722007-07-15 23:41:25 -0700984
985 err = add_bd_holder(bdev, bo);
986 if (err)
987 bd_release(bdev);
Andrew Morton4210df22007-07-15 23:41:28 -0700988 else
989 bo = NULL;
990fail:
Jun'ichi Nomurab4cf1b72006-03-27 01:18:00 -0800991 mutex_unlock(&bdev->bd_mutex);
Andrew Morton4210df22007-07-15 23:41:28 -0700992 free_bd_holder(bo);
Johannes Weiner4e916722007-07-15 23:41:25 -0700993 return err;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800994}
995
996/**
997 * bd_release_from_kobject - bd_release() with additional kobject signature
998 *
999 * @bdev: block device to be released
1000 * @kobj: holder's kobject
1001 *
1002 * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject().
1003 */
1004static void bd_release_from_kobject(struct block_device *bdev,
1005 struct kobject *kobj)
1006{
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001007 if (!kobj)
1008 return;
1009
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001010 mutex_lock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001011 bd_release(bdev);
Andrew Morton4210df22007-07-15 23:41:28 -07001012 free_bd_holder(del_bd_holder(bdev, kobj));
Jun'ichi Nomurab4cf1b72006-03-27 01:18:00 -08001013 mutex_unlock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001014}
1015
1016/**
1017 * bd_claim_by_disk - wrapper function for bd_claim_by_kobject()
1018 *
1019 * @bdev: block device to be claimed
1020 * @holder: holder's signature
1021 * @disk: holder's gendisk
1022 *
1023 * Call bd_claim_by_kobject() with getting @disk->slave_dir.
1024 */
1025int bd_claim_by_disk(struct block_device *bdev, void *holder,
1026 struct gendisk *disk)
1027{
1028 return bd_claim_by_kobject(bdev, holder, kobject_get(disk->slave_dir));
1029}
1030EXPORT_SYMBOL_GPL(bd_claim_by_disk);
1031
1032/**
1033 * bd_release_from_disk - wrapper function for bd_release_from_kobject()
1034 *
1035 * @bdev: block device to be claimed
1036 * @disk: holder's gendisk
1037 *
1038 * Call bd_release_from_kobject() and put @disk->slave_dir.
1039 */
1040void bd_release_from_disk(struct block_device *bdev, struct gendisk *disk)
1041{
1042 bd_release_from_kobject(bdev, disk->slave_dir);
1043 kobject_put(disk->slave_dir);
1044}
1045EXPORT_SYMBOL_GPL(bd_release_from_disk);
1046#endif
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048/*
1049 * Tries to open block device by device number. Use it ONLY if you
1050 * really do not have anything better - i.e. when you are behind a
1051 * truly sucky interface and all you are given is a device number. _Never_
1052 * to be used for internal purposes. If you ever need it - reconsider
1053 * your API.
1054 */
1055struct block_device *open_by_devnum(dev_t dev, unsigned mode)
1056{
1057 struct block_device *bdev = bdget(dev);
1058 int err = -ENOMEM;
1059 int flags = mode & FMODE_WRITE ? O_RDWR : O_RDONLY;
1060 if (bdev)
1061 err = blkdev_get(bdev, mode, flags);
1062 return err ? ERR_PTR(err) : bdev;
1063}
1064
1065EXPORT_SYMBOL(open_by_devnum);
1066
1067/*
1068 * This routine checks whether a removable media has been changed,
1069 * and invalidates all buffer-cache-entries in that case. This
1070 * is a relatively slow routine, so we have to try to minimize using
1071 * it. Thus it is called only upon a 'mount' or 'open'. This
1072 * is the best way of combining speed and utility, I think.
1073 * People changing diskettes in the middle of an operation deserve
1074 * to lose :-)
1075 */
1076int check_disk_change(struct block_device *bdev)
1077{
1078 struct gendisk *disk = bdev->bd_disk;
1079 struct block_device_operations * bdops = disk->fops;
1080
1081 if (!bdops->media_changed)
1082 return 0;
1083 if (!bdops->media_changed(bdev->bd_disk))
1084 return 0;
1085
Christoph Hellwig2ef41632005-05-05 16:15:59 -07001086 if (__invalidate_device(bdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 printk("VFS: busy inodes on changed media.\n");
1088
1089 if (bdops->revalidate_disk)
1090 bdops->revalidate_disk(bdev->bd_disk);
1091 if (bdev->bd_disk->minors > 1)
1092 bdev->bd_invalidated = 1;
1093 return 1;
1094}
1095
1096EXPORT_SYMBOL(check_disk_change);
1097
1098void bd_set_size(struct block_device *bdev, loff_t size)
1099{
1100 unsigned bsize = bdev_hardsect_size(bdev);
1101
1102 bdev->bd_inode->i_size = size;
1103 while (bsize < PAGE_CACHE_SIZE) {
1104 if (size & bsize)
1105 break;
1106 bsize <<= 1;
1107 }
1108 bdev->bd_block_size = bsize;
1109 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
1110}
1111EXPORT_SYMBOL(bd_set_size);
1112
NeilBrown37be4122006-12-08 02:36:16 -08001113static int __blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags,
1114 int for_part);
Peter Zijlstrac48f70c2006-12-08 02:36:18 -08001115static int __blkdev_put(struct block_device *bdev, int for_part);
NeilBrown37be4122006-12-08 02:36:16 -08001116
Peter Zijlstra6d740cd2007-02-20 13:58:18 -08001117/*
1118 * bd_mutex locking:
1119 *
1120 * mutex_lock(part->bd_mutex)
1121 * mutex_lock_nested(whole->bd_mutex, 1)
1122 */
1123
NeilBrown37be4122006-12-08 02:36:16 -08001124static int do_open(struct block_device *bdev, struct file *file, int for_part)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
1126 struct module *owner = NULL;
1127 struct gendisk *disk;
1128 int ret = -ENXIO;
1129 int part;
1130
1131 file->f_mapping = bdev->bd_inode->i_mapping;
1132 lock_kernel();
1133 disk = get_gendisk(bdev->bd_dev, &part);
1134 if (!disk) {
1135 unlock_kernel();
1136 bdput(bdev);
1137 return ret;
1138 }
1139 owner = disk->fops->owner;
1140
NeilBrown6796bf52006-12-08 02:36:16 -08001141 mutex_lock_nested(&bdev->bd_mutex, for_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (!bdev->bd_openers) {
1143 bdev->bd_disk = disk;
1144 bdev->bd_contains = bdev;
1145 if (!part) {
1146 struct backing_dev_info *bdi;
1147 if (disk->fops->open) {
1148 ret = disk->fops->open(bdev->bd_inode, file);
1149 if (ret)
1150 goto out_first;
1151 }
1152 if (!bdev->bd_openers) {
1153 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
1154 bdi = blk_get_backing_dev_info(bdev);
1155 if (bdi == NULL)
1156 bdi = &default_backing_dev_info;
1157 bdev->bd_inode->i_data.backing_dev_info = bdi;
1158 }
1159 if (bdev->bd_invalidated)
1160 rescan_partitions(disk, bdev);
1161 } else {
1162 struct hd_struct *p;
1163 struct block_device *whole;
1164 whole = bdget_disk(disk, 0);
1165 ret = -ENOMEM;
1166 if (!whole)
1167 goto out_first;
NeilBrown37be4122006-12-08 02:36:16 -08001168 BUG_ON(for_part);
1169 ret = __blkdev_get(whole, file->f_mode, file->f_flags, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (ret)
1171 goto out_first;
1172 bdev->bd_contains = whole;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 p = disk->part[part - 1];
1174 bdev->bd_inode->i_data.backing_dev_info =
1175 whole->bd_inode->i_data.backing_dev_info;
1176 if (!(disk->flags & GENHD_FL_UP) || !p || !p->nr_sects) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 ret = -ENXIO;
1178 goto out_first;
1179 }
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001180 kobject_get(&p->dev.kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 bdev->bd_part = p;
1182 bd_set_size(bdev, (loff_t) p->nr_sects << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
1184 } else {
1185 put_disk(disk);
1186 module_put(owner);
1187 if (bdev->bd_contains == bdev) {
1188 if (bdev->bd_disk->fops->open) {
1189 ret = bdev->bd_disk->fops->open(bdev->bd_inode, file);
1190 if (ret)
1191 goto out;
1192 }
1193 if (bdev->bd_invalidated)
1194 rescan_partitions(bdev->bd_disk, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
1196 }
1197 bdev->bd_openers++;
NeilBrown37be4122006-12-08 02:36:16 -08001198 if (for_part)
1199 bdev->bd_part_count++;
Arjan van de Venc039e312006-03-23 03:00:28 -08001200 mutex_unlock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 unlock_kernel();
1202 return 0;
1203
1204out_first:
1205 bdev->bd_disk = NULL;
1206 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
1207 if (bdev != bdev->bd_contains)
Peter Zijlstrac48f70c2006-12-08 02:36:18 -08001208 __blkdev_put(bdev->bd_contains, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 bdev->bd_contains = NULL;
1210 put_disk(disk);
1211 module_put(owner);
1212out:
Arjan van de Venc039e312006-03-23 03:00:28 -08001213 mutex_unlock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 unlock_kernel();
1215 if (ret)
1216 bdput(bdev);
1217 return ret;
1218}
1219
NeilBrown37be4122006-12-08 02:36:16 -08001220static int __blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags,
1221 int for_part)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
1223 /*
1224 * This crockload is due to bad choice of ->open() type.
1225 * It will go away.
1226 * For now, block device ->open() routine must _not_
1227 * examine anything in 'inode' argument except ->i_rdev.
1228 */
1229 struct file fake_file = {};
1230 struct dentry fake_dentry = {};
1231 fake_file.f_mode = mode;
1232 fake_file.f_flags = flags;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001233 fake_file.f_path.dentry = &fake_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 fake_dentry.d_inode = bdev->bd_inode;
1235
NeilBrown37be4122006-12-08 02:36:16 -08001236 return do_open(bdev, &fake_file, for_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
NeilBrown37be4122006-12-08 02:36:16 -08001239int blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags)
1240{
1241 return __blkdev_get(bdev, mode, flags, 0);
1242}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243EXPORT_SYMBOL(blkdev_get);
1244
1245static int blkdev_open(struct inode * inode, struct file * filp)
1246{
1247 struct block_device *bdev;
1248 int res;
1249
1250 /*
1251 * Preserve backwards compatibility and allow large file access
1252 * even if userspace doesn't ask for it explicitly. Some mkfs
1253 * binary needs it. We might want to drop this workaround
1254 * during an unstable branch.
1255 */
1256 filp->f_flags |= O_LARGEFILE;
1257
1258 bdev = bd_acquire(inode);
Pavel Emelianov6a2aae02006-10-28 10:38:33 -07001259 if (bdev == NULL)
1260 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
NeilBrown37be4122006-12-08 02:36:16 -08001262 res = do_open(bdev, filp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 if (res)
1264 return res;
1265
1266 if (!(filp->f_flags & O_EXCL) )
1267 return 0;
1268
1269 if (!(res = bd_claim(bdev, filp)))
1270 return 0;
1271
1272 blkdev_put(bdev);
1273 return res;
1274}
1275
NeilBrown37be4122006-12-08 02:36:16 -08001276static int __blkdev_put(struct block_device *bdev, int for_part)
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001277{
1278 int ret = 0;
1279 struct inode *bd_inode = bdev->bd_inode;
1280 struct gendisk *disk = bdev->bd_disk;
NeilBrown37be4122006-12-08 02:36:16 -08001281 struct block_device *victim = NULL;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001282
NeilBrown6796bf52006-12-08 02:36:16 -08001283 mutex_lock_nested(&bdev->bd_mutex, for_part);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001284 lock_kernel();
NeilBrown37be4122006-12-08 02:36:16 -08001285 if (for_part)
1286 bdev->bd_part_count--;
1287
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001288 if (!--bdev->bd_openers) {
1289 sync_blockdev(bdev);
1290 kill_bdev(bdev);
1291 }
1292 if (bdev->bd_contains == bdev) {
1293 if (disk->fops->release)
1294 ret = disk->fops->release(bd_inode, NULL);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001295 }
1296 if (!bdev->bd_openers) {
1297 struct module *owner = disk->fops->owner;
1298
1299 put_disk(disk);
1300 module_put(owner);
1301
1302 if (bdev->bd_contains != bdev) {
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001303 kobject_put(&bdev->bd_part->dev.kobj);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001304 bdev->bd_part = NULL;
1305 }
1306 bdev->bd_disk = NULL;
1307 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
NeilBrown37be4122006-12-08 02:36:16 -08001308 if (bdev != bdev->bd_contains)
1309 victim = bdev->bd_contains;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001310 bdev->bd_contains = NULL;
1311 }
1312 unlock_kernel();
1313 mutex_unlock(&bdev->bd_mutex);
1314 bdput(bdev);
NeilBrown37be4122006-12-08 02:36:16 -08001315 if (victim)
1316 __blkdev_put(victim, 1);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001317 return ret;
1318}
1319
NeilBrown37be4122006-12-08 02:36:16 -08001320int blkdev_put(struct block_device *bdev)
1321{
1322 return __blkdev_put(bdev, 0);
1323}
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001324EXPORT_SYMBOL(blkdev_put);
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326static int blkdev_close(struct inode * inode, struct file * filp)
1327{
1328 struct block_device *bdev = I_BDEV(filp->f_mapping->host);
1329 if (bdev->bd_holder == filp)
1330 bd_release(bdev);
1331 return blkdev_put(bdev);
1332}
1333
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07001334static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
1336 return blkdev_ioctl(file->f_mapping->host, file, cmd, arg);
1337}
1338
Adrian Bunk4c54ac62008-02-18 13:48:31 +01001339static const struct address_space_operations def_blk_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 .readpage = blkdev_readpage,
1341 .writepage = blkdev_writepage,
1342 .sync_page = block_sync_page,
Nick Piggin6272b5a2007-10-16 01:25:04 -07001343 .write_begin = blkdev_write_begin,
1344 .write_end = blkdev_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 .writepages = generic_writepages,
1346 .direct_IO = blkdev_direct_IO,
1347};
1348
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001349const struct file_operations def_blk_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 .open = blkdev_open,
1351 .release = blkdev_close,
1352 .llseek = block_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -07001353 .read = do_sync_read,
1354 .write = do_sync_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 .aio_read = generic_file_aio_read,
Badari Pulavarty027445c2006-09-30 23:28:46 -07001356 .aio_write = generic_file_aio_write_nolock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 .mmap = generic_file_mmap,
1358 .fsync = block_fsync,
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07001359 .unlocked_ioctl = block_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360#ifdef CONFIG_COMPAT
1361 .compat_ioctl = compat_blkdev_ioctl,
1362#endif
Jens Axboe7f9c51f2006-05-01 19:59:32 +02001363 .splice_read = generic_file_splice_read,
1364 .splice_write = generic_file_splice_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365};
1366
1367int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
1368{
1369 int res;
1370 mm_segment_t old_fs = get_fs();
1371 set_fs(KERNEL_DS);
1372 res = blkdev_ioctl(bdev->bd_inode, NULL, cmd, arg);
1373 set_fs(old_fs);
1374 return res;
1375}
1376
1377EXPORT_SYMBOL(ioctl_by_bdev);
1378
1379/**
1380 * lookup_bdev - lookup a struct block_device by name
1381 *
1382 * @path: special file representing the block device
1383 *
1384 * Get a reference to the blockdevice at @path in the current
1385 * namespace if possible and return it. Return ERR_PTR(error)
1386 * otherwise.
1387 */
1388struct block_device *lookup_bdev(const char *path)
1389{
1390 struct block_device *bdev;
1391 struct inode *inode;
1392 struct nameidata nd;
1393 int error;
1394
1395 if (!path || !*path)
1396 return ERR_PTR(-EINVAL);
1397
1398 error = path_lookup(path, LOOKUP_FOLLOW, &nd);
1399 if (error)
1400 return ERR_PTR(error);
1401
Jan Blunck4ac91372008-02-14 19:34:32 -08001402 inode = nd.path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 error = -ENOTBLK;
1404 if (!S_ISBLK(inode->i_mode))
1405 goto fail;
1406 error = -EACCES;
Jan Blunck4ac91372008-02-14 19:34:32 -08001407 if (nd.path.mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 goto fail;
1409 error = -ENOMEM;
1410 bdev = bd_acquire(inode);
1411 if (!bdev)
1412 goto fail;
1413out:
Jan Blunck1d957f92008-02-14 19:34:35 -08001414 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return bdev;
1416fail:
1417 bdev = ERR_PTR(error);
1418 goto out;
1419}
1420
1421/**
1422 * open_bdev_excl - open a block device by name and set it up for use
1423 *
1424 * @path: special file representing the block device
1425 * @flags: %MS_RDONLY for opening read-only
1426 * @holder: owner for exclusion
1427 *
1428 * Open the blockdevice described by the special file at @path, claim it
1429 * for the @holder.
1430 */
1431struct block_device *open_bdev_excl(const char *path, int flags, void *holder)
1432{
1433 struct block_device *bdev;
1434 mode_t mode = FMODE_READ;
1435 int error = 0;
1436
1437 bdev = lookup_bdev(path);
1438 if (IS_ERR(bdev))
1439 return bdev;
1440
1441 if (!(flags & MS_RDONLY))
1442 mode |= FMODE_WRITE;
1443 error = blkdev_get(bdev, mode, 0);
1444 if (error)
1445 return ERR_PTR(error);
1446 error = -EACCES;
1447 if (!(flags & MS_RDONLY) && bdev_read_only(bdev))
1448 goto blkdev_put;
1449 error = bd_claim(bdev, holder);
1450 if (error)
1451 goto blkdev_put;
1452
1453 return bdev;
1454
1455blkdev_put:
1456 blkdev_put(bdev);
1457 return ERR_PTR(error);
1458}
1459
1460EXPORT_SYMBOL(open_bdev_excl);
1461
1462/**
1463 * close_bdev_excl - release a blockdevice openen by open_bdev_excl()
1464 *
1465 * @bdev: blockdevice to close
1466 *
1467 * This is the counterpart to open_bdev_excl().
1468 */
1469void close_bdev_excl(struct block_device *bdev)
1470{
1471 bd_release(bdev);
1472 blkdev_put(bdev);
1473}
1474
1475EXPORT_SYMBOL(close_bdev_excl);
David Howellsb71e8a42006-08-29 19:06:11 +01001476
1477int __invalidate_device(struct block_device *bdev)
1478{
1479 struct super_block *sb = get_super(bdev);
1480 int res = 0;
1481
1482 if (sb) {
1483 /*
1484 * no need to lock the super, get_super holds the
1485 * read mutex so the filesystem cannot go away
1486 * under us (->put_super runs with the write lock
1487 * hold).
1488 */
1489 shrink_dcache_sb(sb);
1490 res = invalidate_inodes(sb);
1491 drop_super(sb);
1492 }
Peter Zijlstraf98393a2007-05-06 14:49:54 -07001493 invalidate_bdev(bdev);
David Howellsb71e8a42006-08-29 19:06:11 +01001494 return res;
1495}
1496EXPORT_SYMBOL(__invalidate_device);