blob: 57d5726428544f3f6f80ca7a22245b762c952acd [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>
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -070015#include <linux/device_cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/highmem.h>
17#include <linux/blkdev.h>
18#include <linux/module.h>
19#include <linux/blkpg.h>
20#include <linux/buffer_head.h>
David Howells811d7362006-08-29 19:06:09 +010021#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/mpage.h>
23#include <linux/mount.h>
24#include <linux/uio.h>
25#include <linux/namei.h>
Vignesh Babu BM1368c4f2007-05-08 00:24:32 -070026#include <linux/log2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/uaccess.h>
David Howells07f3f052006-09-30 20:52:18 +020028#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30struct bdev_inode {
31 struct block_device bdev;
32 struct inode vfs_inode;
33};
34
Adrian Bunk4c54ac62008-02-18 13:48:31 +010035static const struct address_space_operations def_blk_aops;
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037static inline struct bdev_inode *BDEV_I(struct inode *inode)
38{
39 return container_of(inode, struct bdev_inode, vfs_inode);
40}
41
42inline struct block_device *I_BDEV(struct inode *inode)
43{
44 return &BDEV_I(inode)->bdev;
45}
46
47EXPORT_SYMBOL(I_BDEV);
48
49static sector_t max_block(struct block_device *bdev)
50{
51 sector_t retval = ~((sector_t)0);
52 loff_t sz = i_size_read(bdev->bd_inode);
53
54 if (sz) {
55 unsigned int size = block_size(bdev);
56 unsigned int sizebits = blksize_bits(size);
57 retval = (sz >> sizebits);
58 }
59 return retval;
60}
61
Peter Zijlstraf9a14392007-05-06 14:49:55 -070062/* Kill _all_ buffers and pagecache , dirty or not.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static void kill_bdev(struct block_device *bdev)
64{
Peter Zijlstraf9a14392007-05-06 14:49:55 -070065 if (bdev->bd_inode->i_mapping->nrpages == 0)
66 return;
67 invalidate_bh_lrus();
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
69}
70
71int set_blocksize(struct block_device *bdev, int size)
72{
73 /* Size must be a power of two, and between 512 and PAGE_SIZE */
Vignesh Babu BM1368c4f2007-05-08 00:24:32 -070074 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return -EINVAL;
76
77 /* Size cannot be smaller than the size supported by the device */
78 if (size < bdev_hardsect_size(bdev))
79 return -EINVAL;
80
81 /* Don't change the size if it is same as current */
82 if (bdev->bd_block_size != size) {
83 sync_blockdev(bdev);
84 bdev->bd_block_size = size;
85 bdev->bd_inode->i_blkbits = blksize_bits(size);
86 kill_bdev(bdev);
87 }
88 return 0;
89}
90
91EXPORT_SYMBOL(set_blocksize);
92
93int sb_set_blocksize(struct super_block *sb, int size)
94{
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 if (set_blocksize(sb->s_bdev, size))
96 return 0;
97 /* If we get here, we know size is power of two
98 * and it's value is between 512 and PAGE_SIZE */
99 sb->s_blocksize = size;
Coywolf Qi Hunt38885bd2006-03-24 03:18:05 -0800100 sb->s_blocksize_bits = blksize_bits(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return sb->s_blocksize;
102}
103
104EXPORT_SYMBOL(sb_set_blocksize);
105
106int sb_min_blocksize(struct super_block *sb, int size)
107{
108 int minsize = bdev_hardsect_size(sb->s_bdev);
109 if (size < minsize)
110 size = minsize;
111 return sb_set_blocksize(sb, size);
112}
113
114EXPORT_SYMBOL(sb_min_blocksize);
115
116static int
117blkdev_get_block(struct inode *inode, sector_t iblock,
118 struct buffer_head *bh, int create)
119{
120 if (iblock >= max_block(I_BDEV(inode))) {
121 if (create)
122 return -EIO;
123
124 /*
125 * for reads, we're just trying to fill a partial page.
126 * return a hole, they will have to call get_block again
127 * before they can fill it, and they will get -EIO at that
128 * time
129 */
130 return 0;
131 }
132 bh->b_bdev = I_BDEV(inode);
133 bh->b_blocknr = iblock;
134 set_buffer_mapped(bh);
135 return 0;
136}
137
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800138static int
139blkdev_get_blocks(struct inode *inode, sector_t iblock,
140 struct buffer_head *bh, int create)
141{
142 sector_t end_block = max_block(I_BDEV(inode));
143 unsigned long max_blocks = bh->b_size >> inode->i_blkbits;
144
145 if ((iblock + max_blocks) > end_block) {
146 max_blocks = end_block - iblock;
147 if ((long)max_blocks <= 0) {
148 if (create)
149 return -EIO; /* write fully beyond EOF */
150 /*
151 * It is a read which is fully beyond EOF. We return
152 * a !buffer_mapped buffer
153 */
154 max_blocks = 0;
155 }
156 }
157
158 bh->b_bdev = I_BDEV(inode);
159 bh->b_blocknr = iblock;
160 bh->b_size = max_blocks << inode->i_blkbits;
161 if (max_blocks)
162 set_buffer_mapped(bh);
163 return 0;
164}
165
166static ssize_t
167blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
168 loff_t offset, unsigned long nr_segs)
169{
170 struct file *file = iocb->ki_filp;
171 struct inode *inode = file->f_mapping->host;
172
173 return blockdev_direct_IO_no_locking(rw, iocb, inode, I_BDEV(inode),
174 iov, offset, nr_segs, blkdev_get_blocks, NULL);
175}
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
178{
179 return block_write_full_page(page, blkdev_get_block, wbc);
180}
181
182static int blkdev_readpage(struct file * file, struct page * page)
183{
184 return block_read_full_page(page, blkdev_get_block);
185}
186
Nick Piggin6272b5a2007-10-16 01:25:04 -0700187static int blkdev_write_begin(struct file *file, struct address_space *mapping,
188 loff_t pos, unsigned len, unsigned flags,
189 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Nick Piggin6272b5a2007-10-16 01:25:04 -0700191 *pagep = NULL;
192 return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
193 blkdev_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Nick Piggin6272b5a2007-10-16 01:25:04 -0700196static int blkdev_write_end(struct file *file, struct address_space *mapping,
197 loff_t pos, unsigned len, unsigned copied,
198 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Nick Piggin6272b5a2007-10-16 01:25:04 -0700200 int ret;
201 ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
202
203 unlock_page(page);
204 page_cache_release(page);
205
206 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
209/*
210 * private llseek:
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800211 * for a block special file file->f_path.dentry->d_inode->i_size is zero
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 * so we compute the size by hand (just as in block_read/write above)
213 */
214static loff_t block_llseek(struct file *file, loff_t offset, int origin)
215{
216 struct inode *bd_inode = file->f_mapping->host;
217 loff_t size;
218 loff_t retval;
219
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800220 mutex_lock(&bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 size = i_size_read(bd_inode);
222
223 switch (origin) {
224 case 2:
225 offset += size;
226 break;
227 case 1:
228 offset += file->f_pos;
229 }
230 retval = -EINVAL;
231 if (offset >= 0 && offset <= size) {
232 if (offset != file->f_pos) {
233 file->f_pos = offset;
234 }
235 retval = offset;
236 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800237 mutex_unlock(&bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return retval;
239}
240
241/*
242 * Filp is never NULL; the only case when ->fsync() is called with
243 * NULL first argument is nfsd_sync_dir() and that's not a directory.
244 */
245
246static int block_fsync(struct file *filp, struct dentry *dentry, int datasync)
247{
248 return sync_blockdev(I_BDEV(filp->f_mapping->host));
249}
250
251/*
252 * pseudo-fs
253 */
254
255static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
Christoph Lametere18b8902006-12-06 20:33:20 -0800256static struct kmem_cache * bdev_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258static struct inode *bdev_alloc_inode(struct super_block *sb)
259{
Christoph Lametere94b1762006-12-06 20:33:17 -0800260 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (!ei)
262 return NULL;
263 return &ei->vfs_inode;
264}
265
266static void bdev_destroy_inode(struct inode *inode)
267{
268 struct bdev_inode *bdi = BDEV_I(inode);
269
270 bdi->bdev.bd_inode_backing_dev_info = NULL;
271 kmem_cache_free(bdev_cachep, bdi);
272}
273
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700274static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 struct bdev_inode *ei = (struct bdev_inode *) foo;
277 struct block_device *bdev = &ei->bdev;
278
Christoph Lametera35afb82007-05-16 22:10:57 -0700279 memset(bdev, 0, sizeof(*bdev));
280 mutex_init(&bdev->bd_mutex);
281 sema_init(&bdev->bd_mount_sem, 1);
282 INIT_LIST_HEAD(&bdev->bd_inodes);
283 INIT_LIST_HEAD(&bdev->bd_list);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800284#ifdef CONFIG_SYSFS
Christoph Lametera35afb82007-05-16 22:10:57 -0700285 INIT_LIST_HEAD(&bdev->bd_holder_list);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800286#endif
Christoph Lametera35afb82007-05-16 22:10:57 -0700287 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
290static inline void __bd_forget(struct inode *inode)
291{
292 list_del_init(&inode->i_devices);
293 inode->i_bdev = NULL;
294 inode->i_mapping = &inode->i_data;
295}
296
297static void bdev_clear_inode(struct inode *inode)
298{
299 struct block_device *bdev = &BDEV_I(inode)->bdev;
300 struct list_head *p;
301 spin_lock(&bdev_lock);
302 while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
303 __bd_forget(list_entry(p, struct inode, i_devices));
304 }
305 list_del_init(&bdev->bd_list);
306 spin_unlock(&bdev_lock);
307}
308
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800309static const struct super_operations bdev_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 .statfs = simple_statfs,
311 .alloc_inode = bdev_alloc_inode,
312 .destroy_inode = bdev_destroy_inode,
313 .drop_inode = generic_delete_inode,
314 .clear_inode = bdev_clear_inode,
315};
316
David Howells454e2392006-06-23 02:02:57 -0700317static int bd_get_sb(struct file_system_type *fs_type,
318 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
David Howells454e2392006-06-23 02:02:57 -0700320 return get_sb_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
323static struct file_system_type bd_type = {
324 .name = "bdev",
325 .get_sb = bd_get_sb,
326 .kill_sb = kill_anon_super,
327};
328
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800329static struct vfsmount *bd_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330struct super_block *blockdev_superblock;
331
332void __init bdev_cache_init(void)
333{
334 int err;
335 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800336 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
337 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +0900338 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 err = register_filesystem(&bd_type);
340 if (err)
341 panic("Cannot register bdev pseudo-fs");
342 bd_mnt = kern_mount(&bd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (IS_ERR(bd_mnt))
344 panic("Cannot create bdev pseudo-fs");
345 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
346}
347
348/*
349 * Most likely _very_ bad one - but then it's hardly critical for small
350 * /dev and can be fixed when somebody will need really large one.
351 * Keep in mind that it will be fed through icache hash function too.
352 */
353static inline unsigned long hash(dev_t dev)
354{
355 return MAJOR(dev)+MINOR(dev);
356}
357
358static int bdev_test(struct inode *inode, void *data)
359{
360 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
361}
362
363static int bdev_set(struct inode *inode, void *data)
364{
365 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
366 return 0;
367}
368
369static LIST_HEAD(all_bdevs);
370
371struct block_device *bdget(dev_t dev)
372{
373 struct block_device *bdev;
374 struct inode *inode;
375
376 inode = iget5_locked(bd_mnt->mnt_sb, hash(dev),
377 bdev_test, bdev_set, &dev);
378
379 if (!inode)
380 return NULL;
381
382 bdev = &BDEV_I(inode)->bdev;
383
384 if (inode->i_state & I_NEW) {
385 bdev->bd_contains = NULL;
386 bdev->bd_inode = inode;
387 bdev->bd_block_size = (1 << inode->i_blkbits);
388 bdev->bd_part_count = 0;
389 bdev->bd_invalidated = 0;
390 inode->i_mode = S_IFBLK;
391 inode->i_rdev = dev;
392 inode->i_bdev = bdev;
393 inode->i_data.a_ops = &def_blk_aops;
394 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
395 inode->i_data.backing_dev_info = &default_backing_dev_info;
396 spin_lock(&bdev_lock);
397 list_add(&bdev->bd_list, &all_bdevs);
398 spin_unlock(&bdev_lock);
399 unlock_new_inode(inode);
400 }
401 return bdev;
402}
403
404EXPORT_SYMBOL(bdget);
405
406long nr_blockdev_pages(void)
407{
Matthias Kaehlcke203a2932007-07-15 23:40:23 -0700408 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 long ret = 0;
410 spin_lock(&bdev_lock);
Matthias Kaehlcke203a2932007-07-15 23:40:23 -0700411 list_for_each_entry(bdev, &all_bdevs, bd_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 ret += bdev->bd_inode->i_mapping->nrpages;
413 }
414 spin_unlock(&bdev_lock);
415 return ret;
416}
417
418void bdput(struct block_device *bdev)
419{
420 iput(bdev->bd_inode);
421}
422
423EXPORT_SYMBOL(bdput);
424
425static struct block_device *bd_acquire(struct inode *inode)
426{
427 struct block_device *bdev;
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 spin_lock(&bdev_lock);
430 bdev = inode->i_bdev;
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700431 if (bdev) {
432 atomic_inc(&bdev->bd_inode->i_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 spin_unlock(&bdev_lock);
434 return bdev;
435 }
436 spin_unlock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 bdev = bdget(inode->i_rdev);
439 if (bdev) {
440 spin_lock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700441 if (!inode->i_bdev) {
442 /*
443 * We take an additional bd_inode->i_count for inode,
444 * and it's released in clear_inode() of inode.
445 * So, we can access it via ->i_mapping always
446 * without igrab().
447 */
448 atomic_inc(&bdev->bd_inode->i_count);
449 inode->i_bdev = bdev;
450 inode->i_mapping = bdev->bd_inode->i_mapping;
451 list_add(&inode->i_devices, &bdev->bd_inodes);
452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 spin_unlock(&bdev_lock);
454 }
455 return bdev;
456}
457
458/* Call when you free inode */
459
460void bd_forget(struct inode *inode)
461{
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700462 struct block_device *bdev = NULL;
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 spin_lock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700465 if (inode->i_bdev) {
466 if (inode->i_sb != blockdev_superblock)
467 bdev = inode->i_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 __bd_forget(inode);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 spin_unlock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700471
472 if (bdev)
473 iput(bdev->bd_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
476int bd_claim(struct block_device *bdev, void *holder)
477{
478 int res;
479 spin_lock(&bdev_lock);
480
481 /* first decide result */
482 if (bdev->bd_holder == holder)
483 res = 0; /* already a holder */
484 else if (bdev->bd_holder != NULL)
485 res = -EBUSY; /* held by someone else */
486 else if (bdev->bd_contains == bdev)
487 res = 0; /* is a whole device which isn't held */
488
489 else if (bdev->bd_contains->bd_holder == bd_claim)
490 res = 0; /* is a partition of a device that is being partitioned */
491 else if (bdev->bd_contains->bd_holder != NULL)
492 res = -EBUSY; /* is a partition of a held device */
493 else
494 res = 0; /* is a partition of an un-held device */
495
496 /* now impose change */
497 if (res==0) {
498 /* note that for a whole device bd_holders
499 * will be incremented twice, and bd_holder will
500 * be set to bd_claim before being set to holder
501 */
502 bdev->bd_contains->bd_holders ++;
503 bdev->bd_contains->bd_holder = bd_claim;
504 bdev->bd_holders++;
505 bdev->bd_holder = holder;
506 }
507 spin_unlock(&bdev_lock);
508 return res;
509}
510
511EXPORT_SYMBOL(bd_claim);
512
513void bd_release(struct block_device *bdev)
514{
515 spin_lock(&bdev_lock);
516 if (!--bdev->bd_contains->bd_holders)
517 bdev->bd_contains->bd_holder = NULL;
518 if (!--bdev->bd_holders)
519 bdev->bd_holder = NULL;
520 spin_unlock(&bdev_lock);
521}
522
523EXPORT_SYMBOL(bd_release);
524
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800525#ifdef CONFIG_SYSFS
526/*
527 * Functions for bd_claim_by_kobject / bd_release_from_kobject
528 *
529 * If a kobject is passed to bd_claim_by_kobject()
530 * and the kobject has a parent directory,
531 * following symlinks are created:
532 * o from the kobject to the claimed bdev
533 * o from "holders" directory of the bdev to the parent of the kobject
534 * bd_release_from_kobject() removes these symlinks.
535 *
536 * Example:
537 * If /dev/dm-0 maps to /dev/sda, kobject corresponding to
538 * /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then:
539 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
540 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
541 */
542
543static struct kobject *bdev_get_kobj(struct block_device *bdev)
544{
545 if (bdev->bd_contains != bdev)
Tejun Heoed9e1982008-08-25 19:56:05 +0900546 return kobject_get(&part_to_dev(bdev->bd_part)->kobj);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800547 else
Tejun Heoed9e1982008-08-25 19:56:05 +0900548 return kobject_get(&disk_to_dev(bdev->bd_disk)->kobj);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800549}
550
Andrew Morton4d7dd8fd2006-09-29 01:58:56 -0700551static int add_symlink(struct kobject *from, struct kobject *to)
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800552{
553 if (!from || !to)
Andrew Morton4d7dd8fd2006-09-29 01:58:56 -0700554 return 0;
555 return sysfs_create_link(from, to, kobject_name(to));
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800556}
557
558static void del_symlink(struct kobject *from, struct kobject *to)
559{
560 if (!from || !to)
561 return;
562 sysfs_remove_link(from, kobject_name(to));
563}
564
565/*
566 * 'struct bd_holder' contains pointers to kobjects symlinked by
567 * bd_claim_by_kobject.
568 * It's connected to bd_holder_list which is protected by bdev->bd_sem.
569 */
570struct bd_holder {
571 struct list_head list; /* chain of holders of the bdev */
572 int count; /* references from the holder */
573 struct kobject *sdir; /* holder object, e.g. "/block/dm-0/slaves" */
574 struct kobject *hdev; /* e.g. "/block/dm-0" */
575 struct kobject *hdir; /* e.g. "/block/sda/holders" */
576 struct kobject *sdev; /* e.g. "/block/sda" */
577};
578
579/*
580 * Get references of related kobjects at once.
581 * Returns 1 on success. 0 on failure.
582 *
583 * Should call bd_holder_release_dirs() after successful use.
584 */
585static int bd_holder_grab_dirs(struct block_device *bdev,
586 struct bd_holder *bo)
587{
588 if (!bdev || !bo)
589 return 0;
590
591 bo->sdir = kobject_get(bo->sdir);
592 if (!bo->sdir)
593 return 0;
594
595 bo->hdev = kobject_get(bo->sdir->parent);
596 if (!bo->hdev)
597 goto fail_put_sdir;
598
599 bo->sdev = bdev_get_kobj(bdev);
600 if (!bo->sdev)
601 goto fail_put_hdev;
602
Tejun Heo4c465012008-08-25 19:56:11 +0900603 bo->hdir = kobject_get(bdev->bd_part->holder_dir);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800604 if (!bo->hdir)
605 goto fail_put_sdev;
606
607 return 1;
608
609fail_put_sdev:
610 kobject_put(bo->sdev);
611fail_put_hdev:
612 kobject_put(bo->hdev);
613fail_put_sdir:
614 kobject_put(bo->sdir);
615
616 return 0;
617}
618
619/* Put references of related kobjects at once. */
620static void bd_holder_release_dirs(struct bd_holder *bo)
621{
622 kobject_put(bo->hdir);
623 kobject_put(bo->sdev);
624 kobject_put(bo->hdev);
625 kobject_put(bo->sdir);
626}
627
628static struct bd_holder *alloc_bd_holder(struct kobject *kobj)
629{
630 struct bd_holder *bo;
631
632 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
633 if (!bo)
634 return NULL;
635
636 bo->count = 1;
637 bo->sdir = kobj;
638
639 return bo;
640}
641
642static void free_bd_holder(struct bd_holder *bo)
643{
644 kfree(bo);
645}
646
647/**
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500648 * find_bd_holder - find matching struct bd_holder from the block device
649 *
650 * @bdev: struct block device to be searched
651 * @bo: target struct bd_holder
652 *
653 * Returns matching entry with @bo in @bdev->bd_holder_list.
654 * If found, increment the reference count and return the pointer.
655 * If not found, returns NULL.
656 */
Andrew Morton36a561d2006-10-30 22:07:03 -0800657static struct bd_holder *find_bd_holder(struct block_device *bdev,
658 struct bd_holder *bo)
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500659{
660 struct bd_holder *tmp;
661
662 list_for_each_entry(tmp, &bdev->bd_holder_list, list)
663 if (tmp->sdir == bo->sdir) {
664 tmp->count++;
665 return tmp;
666 }
667
668 return NULL;
669}
670
671/**
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800672 * add_bd_holder - create sysfs symlinks for bd_claim() relationship
673 *
674 * @bdev: block device to be bd_claimed
675 * @bo: preallocated and initialized by alloc_bd_holder()
676 *
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500677 * Add @bo to @bdev->bd_holder_list, create symlinks.
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800678 *
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500679 * Returns 0 if symlinks are created.
680 * Returns -ve if something fails.
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800681 */
682static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
683{
Johannes Weiner4e916722007-07-15 23:41:25 -0700684 int err;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800685
686 if (!bo)
Andrew Morton4d7dd8fd2006-09-29 01:58:56 -0700687 return -EINVAL;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800688
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800689 if (!bd_holder_grab_dirs(bdev, bo))
Andrew Morton4d7dd8fd2006-09-29 01:58:56 -0700690 return -EBUSY;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800691
Johannes Weiner4e916722007-07-15 23:41:25 -0700692 err = add_symlink(bo->sdir, bo->sdev);
693 if (err)
694 return err;
695
696 err = add_symlink(bo->hdir, bo->hdev);
697 if (err) {
698 del_symlink(bo->sdir, bo->sdev);
699 return err;
Andrew Morton4d7dd8fd2006-09-29 01:58:56 -0700700 }
Johannes Weiner4e916722007-07-15 23:41:25 -0700701
702 list_add_tail(&bo->list, &bdev->bd_holder_list);
703 return 0;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800704}
705
706/**
707 * del_bd_holder - delete sysfs symlinks for bd_claim() relationship
708 *
709 * @bdev: block device to be bd_claimed
710 * @kobj: holder's kobject
711 *
712 * If there is matching entry with @kobj in @bdev->bd_holder_list
713 * and no other bd_claim() from the same kobject,
714 * remove the struct bd_holder from the list, delete symlinks for it.
715 *
716 * Returns a pointer to the struct bd_holder when it's removed from the list
717 * and ready to be freed.
718 * Returns NULL if matching claim isn't found or there is other bd_claim()
719 * by the same kobject.
720 */
721static struct bd_holder *del_bd_holder(struct block_device *bdev,
722 struct kobject *kobj)
723{
724 struct bd_holder *bo;
725
726 list_for_each_entry(bo, &bdev->bd_holder_list, list) {
727 if (bo->sdir == kobj) {
728 bo->count--;
729 BUG_ON(bo->count < 0);
730 if (!bo->count) {
731 list_del(&bo->list);
732 del_symlink(bo->sdir, bo->sdev);
733 del_symlink(bo->hdir, bo->hdev);
734 bd_holder_release_dirs(bo);
735 return bo;
736 }
737 break;
738 }
739 }
740
741 return NULL;
742}
743
744/**
745 * bd_claim_by_kobject - bd_claim() with additional kobject signature
746 *
747 * @bdev: block device to be claimed
748 * @holder: holder's signature
749 * @kobj: holder's kobject
750 *
751 * Do bd_claim() and if it succeeds, create sysfs symlinks between
752 * the bdev and the holder's kobject.
753 * Use bd_release_from_kobject() when relesing the claimed bdev.
754 *
755 * Returns 0 on success. (same as bd_claim())
756 * Returns errno on failure.
757 */
758static int bd_claim_by_kobject(struct block_device *bdev, void *holder,
759 struct kobject *kobj)
760{
Johannes Weiner4e916722007-07-15 23:41:25 -0700761 int err;
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500762 struct bd_holder *bo, *found;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800763
764 if (!kobj)
765 return -EINVAL;
766
767 bo = alloc_bd_holder(kobj);
768 if (!bo)
769 return -ENOMEM;
770
Peter Zijlstra2e7b6512006-12-08 02:36:13 -0800771 mutex_lock(&bdev->bd_mutex);
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500772
Johannes Weiner4e916722007-07-15 23:41:25 -0700773 err = bd_claim(bdev, holder);
774 if (err)
Andrew Morton4210df22007-07-15 23:41:28 -0700775 goto fail;
Johannes Weiner4e916722007-07-15 23:41:25 -0700776
777 found = find_bd_holder(bdev, bo);
778 if (found)
Andrew Morton4210df22007-07-15 23:41:28 -0700779 goto fail;
Johannes Weiner4e916722007-07-15 23:41:25 -0700780
781 err = add_bd_holder(bdev, bo);
782 if (err)
783 bd_release(bdev);
Andrew Morton4210df22007-07-15 23:41:28 -0700784 else
785 bo = NULL;
786fail:
Jun'ichi Nomurab4cf1b72006-03-27 01:18:00 -0800787 mutex_unlock(&bdev->bd_mutex);
Andrew Morton4210df22007-07-15 23:41:28 -0700788 free_bd_holder(bo);
Johannes Weiner4e916722007-07-15 23:41:25 -0700789 return err;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800790}
791
792/**
793 * bd_release_from_kobject - bd_release() with additional kobject signature
794 *
795 * @bdev: block device to be released
796 * @kobj: holder's kobject
797 *
798 * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject().
799 */
800static void bd_release_from_kobject(struct block_device *bdev,
801 struct kobject *kobj)
802{
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800803 if (!kobj)
804 return;
805
Peter Zijlstra2e7b6512006-12-08 02:36:13 -0800806 mutex_lock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800807 bd_release(bdev);
Andrew Morton4210df22007-07-15 23:41:28 -0700808 free_bd_holder(del_bd_holder(bdev, kobj));
Jun'ichi Nomurab4cf1b72006-03-27 01:18:00 -0800809 mutex_unlock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800810}
811
812/**
813 * bd_claim_by_disk - wrapper function for bd_claim_by_kobject()
814 *
815 * @bdev: block device to be claimed
816 * @holder: holder's signature
817 * @disk: holder's gendisk
818 *
819 * Call bd_claim_by_kobject() with getting @disk->slave_dir.
820 */
821int bd_claim_by_disk(struct block_device *bdev, void *holder,
822 struct gendisk *disk)
823{
824 return bd_claim_by_kobject(bdev, holder, kobject_get(disk->slave_dir));
825}
826EXPORT_SYMBOL_GPL(bd_claim_by_disk);
827
828/**
829 * bd_release_from_disk - wrapper function for bd_release_from_kobject()
830 *
831 * @bdev: block device to be claimed
832 * @disk: holder's gendisk
833 *
834 * Call bd_release_from_kobject() and put @disk->slave_dir.
835 */
836void bd_release_from_disk(struct block_device *bdev, struct gendisk *disk)
837{
838 bd_release_from_kobject(bdev, disk->slave_dir);
839 kobject_put(disk->slave_dir);
840}
841EXPORT_SYMBOL_GPL(bd_release_from_disk);
842#endif
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844/*
845 * Tries to open block device by device number. Use it ONLY if you
846 * really do not have anything better - i.e. when you are behind a
847 * truly sucky interface and all you are given is a device number. _Never_
848 * to be used for internal purposes. If you ever need it - reconsider
849 * your API.
850 */
851struct block_device *open_by_devnum(dev_t dev, unsigned mode)
852{
853 struct block_device *bdev = bdget(dev);
854 int err = -ENOMEM;
855 int flags = mode & FMODE_WRITE ? O_RDWR : O_RDONLY;
856 if (bdev)
857 err = blkdev_get(bdev, mode, flags);
858 return err ? ERR_PTR(err) : bdev;
859}
860
861EXPORT_SYMBOL(open_by_devnum);
862
863/*
864 * This routine checks whether a removable media has been changed,
865 * and invalidates all buffer-cache-entries in that case. This
866 * is a relatively slow routine, so we have to try to minimize using
867 * it. Thus it is called only upon a 'mount' or 'open'. This
868 * is the best way of combining speed and utility, I think.
869 * People changing diskettes in the middle of an operation deserve
870 * to lose :-)
871 */
872int check_disk_change(struct block_device *bdev)
873{
874 struct gendisk *disk = bdev->bd_disk;
875 struct block_device_operations * bdops = disk->fops;
876
877 if (!bdops->media_changed)
878 return 0;
879 if (!bdops->media_changed(bdev->bd_disk))
880 return 0;
881
Christoph Hellwig2ef41632005-05-05 16:15:59 -0700882 if (__invalidate_device(bdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 printk("VFS: busy inodes on changed media.\n");
884
885 if (bdops->revalidate_disk)
886 bdops->revalidate_disk(bdev->bd_disk);
Tejun Heob5d0b9d2008-09-03 09:06:42 +0200887 if (disk_partitionable(bdev->bd_disk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 bdev->bd_invalidated = 1;
889 return 1;
890}
891
892EXPORT_SYMBOL(check_disk_change);
893
894void bd_set_size(struct block_device *bdev, loff_t size)
895{
896 unsigned bsize = bdev_hardsect_size(bdev);
897
898 bdev->bd_inode->i_size = size;
899 while (bsize < PAGE_CACHE_SIZE) {
900 if (size & bsize)
901 break;
902 bsize <<= 1;
903 }
904 bdev->bd_block_size = bsize;
905 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
906}
907EXPORT_SYMBOL(bd_set_size);
908
NeilBrown37be4122006-12-08 02:36:16 -0800909static int __blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags,
910 int for_part);
Peter Zijlstrac48f70c2006-12-08 02:36:18 -0800911static int __blkdev_put(struct block_device *bdev, int for_part);
NeilBrown37be4122006-12-08 02:36:16 -0800912
Peter Zijlstra6d740cd2007-02-20 13:58:18 -0800913/*
914 * bd_mutex locking:
915 *
916 * mutex_lock(part->bd_mutex)
917 * mutex_lock_nested(whole->bd_mutex, 1)
918 */
919
NeilBrown37be4122006-12-08 02:36:16 -0800920static int do_open(struct block_device *bdev, struct file *file, int for_part)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
922 struct module *owner = NULL;
923 struct gendisk *disk;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200924 struct hd_struct *part = NULL;
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -0700925 int ret;
Tejun Heocf771cb2008-09-03 09:01:09 +0200926 int partno;
Al Virofe6e9c12008-06-23 08:30:55 -0400927 int perm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Al Virofe6e9c12008-06-23 08:30:55 -0400929 if (file->f_mode & FMODE_READ)
930 perm |= MAY_READ;
931 if (file->f_mode & FMODE_WRITE)
932 perm |= MAY_WRITE;
933 /*
934 * hooks: /n/, see "layering violations".
935 */
936 ret = devcgroup_inode_permission(bdev->bd_inode, perm);
Al Viro82666022008-08-01 05:32:04 -0400937 if (ret != 0) {
938 bdput(bdev);
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -0700939 return ret;
Al Viro82666022008-08-01 05:32:04 -0400940 }
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -0700941
942 ret = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 file->f_mapping = bdev->bd_inode->i_mapping;
944 lock_kernel();
Tejun Heocf771cb2008-09-03 09:01:09 +0200945 disk = get_gendisk(bdev->bd_dev, &partno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if (!disk) {
947 unlock_kernel();
948 bdput(bdev);
949 return ret;
950 }
951 owner = disk->fops->owner;
952
NeilBrown6796bf52006-12-08 02:36:16 -0800953 mutex_lock_nested(&bdev->bd_mutex, for_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 if (!bdev->bd_openers) {
955 bdev->bd_disk = disk;
956 bdev->bd_contains = bdev;
Tejun Heocf771cb2008-09-03 09:01:09 +0200957 if (!partno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 struct backing_dev_info *bdi;
959 if (disk->fops->open) {
960 ret = disk->fops->open(bdev->bd_inode, file);
961 if (ret)
962 goto out_first;
963 }
964 if (!bdev->bd_openers) {
965 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
966 bdi = blk_get_backing_dev_info(bdev);
967 if (bdi == NULL)
968 bdi = &default_backing_dev_info;
969 bdev->bd_inode->i_data.backing_dev_info = bdi;
970 }
971 if (bdev->bd_invalidated)
972 rescan_partitions(disk, bdev);
973 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 struct block_device *whole;
975 whole = bdget_disk(disk, 0);
976 ret = -ENOMEM;
977 if (!whole)
978 goto out_first;
NeilBrown37be4122006-12-08 02:36:16 -0800979 BUG_ON(for_part);
980 ret = __blkdev_get(whole, file->f_mode, file->f_flags, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (ret)
982 goto out_first;
983 bdev->bd_contains = whole;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200984 part = disk_get_part(disk, partno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 bdev->bd_inode->i_data.backing_dev_info =
986 whole->bd_inode->i_data.backing_dev_info;
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200987 if (!(disk->flags & GENHD_FL_UP) ||
988 !part || !part->nr_sects) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 ret = -ENXIO;
990 goto out_first;
991 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +0200992 bdev->bd_part = part;
993 bd_set_size(bdev, (loff_t)part->nr_sects << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
995 } else {
996 put_disk(disk);
997 module_put(owner);
998 if (bdev->bd_contains == bdev) {
999 if (bdev->bd_disk->fops->open) {
1000 ret = bdev->bd_disk->fops->open(bdev->bd_inode, file);
1001 if (ret)
1002 goto out;
1003 }
1004 if (bdev->bd_invalidated)
1005 rescan_partitions(bdev->bd_disk, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
1007 }
1008 bdev->bd_openers++;
NeilBrown37be4122006-12-08 02:36:16 -08001009 if (for_part)
1010 bdev->bd_part_count++;
Arjan van de Venc039e312006-03-23 03:00:28 -08001011 mutex_unlock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 unlock_kernel();
1013 return 0;
1014
1015out_first:
1016 bdev->bd_disk = NULL;
1017 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
1018 if (bdev != bdev->bd_contains)
Peter Zijlstrac48f70c2006-12-08 02:36:18 -08001019 __blkdev_put(bdev->bd_contains, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 bdev->bd_contains = NULL;
1021 put_disk(disk);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001022 disk_put_part(part);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 module_put(owner);
1024out:
Arjan van de Venc039e312006-03-23 03:00:28 -08001025 mutex_unlock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 unlock_kernel();
1027 if (ret)
1028 bdput(bdev);
1029 return ret;
1030}
1031
NeilBrown37be4122006-12-08 02:36:16 -08001032static int __blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags,
1033 int for_part)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
1035 /*
1036 * This crockload is due to bad choice of ->open() type.
1037 * It will go away.
1038 * For now, block device ->open() routine must _not_
1039 * examine anything in 'inode' argument except ->i_rdev.
1040 */
1041 struct file fake_file = {};
1042 struct dentry fake_dentry = {};
1043 fake_file.f_mode = mode;
1044 fake_file.f_flags = flags;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001045 fake_file.f_path.dentry = &fake_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 fake_dentry.d_inode = bdev->bd_inode;
1047
NeilBrown37be4122006-12-08 02:36:16 -08001048 return do_open(bdev, &fake_file, for_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
NeilBrown37be4122006-12-08 02:36:16 -08001051int blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags)
1052{
1053 return __blkdev_get(bdev, mode, flags, 0);
1054}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055EXPORT_SYMBOL(blkdev_get);
1056
1057static int blkdev_open(struct inode * inode, struct file * filp)
1058{
1059 struct block_device *bdev;
1060 int res;
1061
1062 /*
1063 * Preserve backwards compatibility and allow large file access
1064 * even if userspace doesn't ask for it explicitly. Some mkfs
1065 * binary needs it. We might want to drop this workaround
1066 * during an unstable branch.
1067 */
1068 filp->f_flags |= O_LARGEFILE;
1069
1070 bdev = bd_acquire(inode);
Pavel Emelianov6a2aae02006-10-28 10:38:33 -07001071 if (bdev == NULL)
1072 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
NeilBrown37be4122006-12-08 02:36:16 -08001074 res = do_open(bdev, filp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (res)
1076 return res;
1077
1078 if (!(filp->f_flags & O_EXCL) )
1079 return 0;
1080
1081 if (!(res = bd_claim(bdev, filp)))
1082 return 0;
1083
1084 blkdev_put(bdev);
1085 return res;
1086}
1087
NeilBrown37be4122006-12-08 02:36:16 -08001088static int __blkdev_put(struct block_device *bdev, int for_part)
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001089{
1090 int ret = 0;
1091 struct inode *bd_inode = bdev->bd_inode;
1092 struct gendisk *disk = bdev->bd_disk;
NeilBrown37be4122006-12-08 02:36:16 -08001093 struct block_device *victim = NULL;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001094
NeilBrown6796bf52006-12-08 02:36:16 -08001095 mutex_lock_nested(&bdev->bd_mutex, for_part);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001096 lock_kernel();
NeilBrown37be4122006-12-08 02:36:16 -08001097 if (for_part)
1098 bdev->bd_part_count--;
1099
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001100 if (!--bdev->bd_openers) {
1101 sync_blockdev(bdev);
1102 kill_bdev(bdev);
1103 }
1104 if (bdev->bd_contains == bdev) {
1105 if (disk->fops->release)
1106 ret = disk->fops->release(bd_inode, NULL);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001107 }
1108 if (!bdev->bd_openers) {
1109 struct module *owner = disk->fops->owner;
1110
1111 put_disk(disk);
1112 module_put(owner);
1113
1114 if (bdev->bd_contains != bdev) {
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001115 disk_put_part(bdev->bd_part);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001116 bdev->bd_part = NULL;
1117 }
1118 bdev->bd_disk = NULL;
1119 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
NeilBrown37be4122006-12-08 02:36:16 -08001120 if (bdev != bdev->bd_contains)
1121 victim = bdev->bd_contains;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001122 bdev->bd_contains = NULL;
1123 }
1124 unlock_kernel();
1125 mutex_unlock(&bdev->bd_mutex);
1126 bdput(bdev);
NeilBrown37be4122006-12-08 02:36:16 -08001127 if (victim)
1128 __blkdev_put(victim, 1);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001129 return ret;
1130}
1131
NeilBrown37be4122006-12-08 02:36:16 -08001132int blkdev_put(struct block_device *bdev)
1133{
1134 return __blkdev_put(bdev, 0);
1135}
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001136EXPORT_SYMBOL(blkdev_put);
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138static int blkdev_close(struct inode * inode, struct file * filp)
1139{
1140 struct block_device *bdev = I_BDEV(filp->f_mapping->host);
1141 if (bdev->bd_holder == filp)
1142 bd_release(bdev);
1143 return blkdev_put(bdev);
1144}
1145
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07001146static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
1148 return blkdev_ioctl(file->f_mapping->host, file, cmd, arg);
1149}
1150
Adrian Bunk4c54ac62008-02-18 13:48:31 +01001151static const struct address_space_operations def_blk_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 .readpage = blkdev_readpage,
1153 .writepage = blkdev_writepage,
1154 .sync_page = block_sync_page,
Nick Piggin6272b5a2007-10-16 01:25:04 -07001155 .write_begin = blkdev_write_begin,
1156 .write_end = blkdev_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 .writepages = generic_writepages,
1158 .direct_IO = blkdev_direct_IO,
1159};
1160
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001161const struct file_operations def_blk_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 .open = blkdev_open,
1163 .release = blkdev_close,
1164 .llseek = block_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -07001165 .read = do_sync_read,
1166 .write = do_sync_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 .aio_read = generic_file_aio_read,
Badari Pulavarty027445c2006-09-30 23:28:46 -07001168 .aio_write = generic_file_aio_write_nolock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 .mmap = generic_file_mmap,
1170 .fsync = block_fsync,
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07001171 .unlocked_ioctl = block_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172#ifdef CONFIG_COMPAT
1173 .compat_ioctl = compat_blkdev_ioctl,
1174#endif
Jens Axboe7f9c51f2006-05-01 19:59:32 +02001175 .splice_read = generic_file_splice_read,
1176 .splice_write = generic_file_splice_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177};
1178
1179int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
1180{
1181 int res;
1182 mm_segment_t old_fs = get_fs();
1183 set_fs(KERNEL_DS);
1184 res = blkdev_ioctl(bdev->bd_inode, NULL, cmd, arg);
1185 set_fs(old_fs);
1186 return res;
1187}
1188
1189EXPORT_SYMBOL(ioctl_by_bdev);
1190
1191/**
1192 * lookup_bdev - lookup a struct block_device by name
1193 *
1194 * @path: special file representing the block device
1195 *
1196 * Get a reference to the blockdevice at @path in the current
1197 * namespace if possible and return it. Return ERR_PTR(error)
1198 * otherwise.
1199 */
1200struct block_device *lookup_bdev(const char *path)
1201{
1202 struct block_device *bdev;
1203 struct inode *inode;
1204 struct nameidata nd;
1205 int error;
1206
1207 if (!path || !*path)
1208 return ERR_PTR(-EINVAL);
1209
1210 error = path_lookup(path, LOOKUP_FOLLOW, &nd);
1211 if (error)
1212 return ERR_PTR(error);
1213
Jan Blunck4ac91372008-02-14 19:34:32 -08001214 inode = nd.path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 error = -ENOTBLK;
1216 if (!S_ISBLK(inode->i_mode))
1217 goto fail;
1218 error = -EACCES;
Jan Blunck4ac91372008-02-14 19:34:32 -08001219 if (nd.path.mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 goto fail;
1221 error = -ENOMEM;
1222 bdev = bd_acquire(inode);
1223 if (!bdev)
1224 goto fail;
1225out:
Jan Blunck1d957f92008-02-14 19:34:35 -08001226 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return bdev;
1228fail:
1229 bdev = ERR_PTR(error);
1230 goto out;
1231}
Al Virod5686b42008-08-01 05:00:11 -04001232EXPORT_SYMBOL(lookup_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234/**
1235 * open_bdev_excl - open a block device by name and set it up for use
1236 *
1237 * @path: special file representing the block device
1238 * @flags: %MS_RDONLY for opening read-only
1239 * @holder: owner for exclusion
1240 *
1241 * Open the blockdevice described by the special file at @path, claim it
1242 * for the @holder.
1243 */
1244struct block_device *open_bdev_excl(const char *path, int flags, void *holder)
1245{
1246 struct block_device *bdev;
1247 mode_t mode = FMODE_READ;
1248 int error = 0;
1249
1250 bdev = lookup_bdev(path);
1251 if (IS_ERR(bdev))
1252 return bdev;
1253
1254 if (!(flags & MS_RDONLY))
1255 mode |= FMODE_WRITE;
1256 error = blkdev_get(bdev, mode, 0);
1257 if (error)
1258 return ERR_PTR(error);
1259 error = -EACCES;
1260 if (!(flags & MS_RDONLY) && bdev_read_only(bdev))
1261 goto blkdev_put;
1262 error = bd_claim(bdev, holder);
1263 if (error)
1264 goto blkdev_put;
1265
1266 return bdev;
1267
1268blkdev_put:
1269 blkdev_put(bdev);
1270 return ERR_PTR(error);
1271}
1272
1273EXPORT_SYMBOL(open_bdev_excl);
1274
1275/**
1276 * close_bdev_excl - release a blockdevice openen by open_bdev_excl()
1277 *
1278 * @bdev: blockdevice to close
1279 *
1280 * This is the counterpart to open_bdev_excl().
1281 */
1282void close_bdev_excl(struct block_device *bdev)
1283{
1284 bd_release(bdev);
1285 blkdev_put(bdev);
1286}
1287
1288EXPORT_SYMBOL(close_bdev_excl);
David Howellsb71e8a42006-08-29 19:06:11 +01001289
1290int __invalidate_device(struct block_device *bdev)
1291{
1292 struct super_block *sb = get_super(bdev);
1293 int res = 0;
1294
1295 if (sb) {
1296 /*
1297 * no need to lock the super, get_super holds the
1298 * read mutex so the filesystem cannot go away
1299 * under us (->put_super runs with the write lock
1300 * hold).
1301 */
1302 shrink_dcache_sb(sb);
1303 res = invalidate_inodes(sb);
1304 drop_super(sb);
1305 }
Peter Zijlstraf98393a2007-05-06 14:49:54 -07001306 invalidate_bdev(bdev);
David Howellsb71e8a42006-08-29 19:06:11 +01001307 return res;
1308}
1309EXPORT_SYMBOL(__invalidate_device);