blob: 8ebbfdf708c24c9d70bc4cc88e8266202d56ec06 [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
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800329struct super_block *blockdev_superblock __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331void __init bdev_cache_init(void)
332{
333 int err;
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800334 struct vfsmount *bd_mnt;
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800337 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
338 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +0900339 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 err = register_filesystem(&bd_type);
341 if (err)
342 panic("Cannot register bdev pseudo-fs");
343 bd_mnt = kern_mount(&bd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (IS_ERR(bd_mnt))
345 panic("Cannot create bdev pseudo-fs");
346 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
347}
348
349/*
350 * Most likely _very_ bad one - but then it's hardly critical for small
351 * /dev and can be fixed when somebody will need really large one.
352 * Keep in mind that it will be fed through icache hash function too.
353 */
354static inline unsigned long hash(dev_t dev)
355{
356 return MAJOR(dev)+MINOR(dev);
357}
358
359static int bdev_test(struct inode *inode, void *data)
360{
361 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
362}
363
364static int bdev_set(struct inode *inode, void *data)
365{
366 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
367 return 0;
368}
369
370static LIST_HEAD(all_bdevs);
371
372struct block_device *bdget(dev_t dev)
373{
374 struct block_device *bdev;
375 struct inode *inode;
376
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800377 inode = iget5_locked(blockdev_superblock, hash(dev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 bdev_test, bdev_set, &dev);
379
380 if (!inode)
381 return NULL;
382
383 bdev = &BDEV_I(inode)->bdev;
384
385 if (inode->i_state & I_NEW) {
386 bdev->bd_contains = NULL;
387 bdev->bd_inode = inode;
388 bdev->bd_block_size = (1 << inode->i_blkbits);
389 bdev->bd_part_count = 0;
390 bdev->bd_invalidated = 0;
391 inode->i_mode = S_IFBLK;
392 inode->i_rdev = dev;
393 inode->i_bdev = bdev;
394 inode->i_data.a_ops = &def_blk_aops;
395 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
396 inode->i_data.backing_dev_info = &default_backing_dev_info;
397 spin_lock(&bdev_lock);
398 list_add(&bdev->bd_list, &all_bdevs);
399 spin_unlock(&bdev_lock);
400 unlock_new_inode(inode);
401 }
402 return bdev;
403}
404
405EXPORT_SYMBOL(bdget);
406
407long nr_blockdev_pages(void)
408{
Matthias Kaehlcke203a2932007-07-15 23:40:23 -0700409 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 long ret = 0;
411 spin_lock(&bdev_lock);
Matthias Kaehlcke203a2932007-07-15 23:40:23 -0700412 list_for_each_entry(bdev, &all_bdevs, bd_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 ret += bdev->bd_inode->i_mapping->nrpages;
414 }
415 spin_unlock(&bdev_lock);
416 return ret;
417}
418
419void bdput(struct block_device *bdev)
420{
421 iput(bdev->bd_inode);
422}
423
424EXPORT_SYMBOL(bdput);
425
426static struct block_device *bd_acquire(struct inode *inode)
427{
428 struct block_device *bdev;
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 spin_lock(&bdev_lock);
431 bdev = inode->i_bdev;
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700432 if (bdev) {
433 atomic_inc(&bdev->bd_inode->i_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 spin_unlock(&bdev_lock);
435 return bdev;
436 }
437 spin_unlock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 bdev = bdget(inode->i_rdev);
440 if (bdev) {
441 spin_lock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700442 if (!inode->i_bdev) {
443 /*
444 * We take an additional bd_inode->i_count for inode,
445 * and it's released in clear_inode() of inode.
446 * So, we can access it via ->i_mapping always
447 * without igrab().
448 */
449 atomic_inc(&bdev->bd_inode->i_count);
450 inode->i_bdev = bdev;
451 inode->i_mapping = bdev->bd_inode->i_mapping;
452 list_add(&inode->i_devices, &bdev->bd_inodes);
453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 spin_unlock(&bdev_lock);
455 }
456 return bdev;
457}
458
459/* Call when you free inode */
460
461void bd_forget(struct inode *inode)
462{
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700463 struct block_device *bdev = NULL;
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 spin_lock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700466 if (inode->i_bdev) {
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800467 if (!sb_is_blkdev_sb(inode->i_sb))
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700468 bdev = inode->i_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 __bd_forget(inode);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 spin_unlock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700472
473 if (bdev)
474 iput(bdev->bd_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
477int bd_claim(struct block_device *bdev, void *holder)
478{
479 int res;
480 spin_lock(&bdev_lock);
481
482 /* first decide result */
483 if (bdev->bd_holder == holder)
484 res = 0; /* already a holder */
485 else if (bdev->bd_holder != NULL)
486 res = -EBUSY; /* held by someone else */
487 else if (bdev->bd_contains == bdev)
488 res = 0; /* is a whole device which isn't held */
489
490 else if (bdev->bd_contains->bd_holder == bd_claim)
491 res = 0; /* is a partition of a device that is being partitioned */
492 else if (bdev->bd_contains->bd_holder != NULL)
493 res = -EBUSY; /* is a partition of a held device */
494 else
495 res = 0; /* is a partition of an un-held device */
496
497 /* now impose change */
498 if (res==0) {
499 /* note that for a whole device bd_holders
500 * will be incremented twice, and bd_holder will
501 * be set to bd_claim before being set to holder
502 */
503 bdev->bd_contains->bd_holders ++;
504 bdev->bd_contains->bd_holder = bd_claim;
505 bdev->bd_holders++;
506 bdev->bd_holder = holder;
507 }
508 spin_unlock(&bdev_lock);
509 return res;
510}
511
512EXPORT_SYMBOL(bd_claim);
513
514void bd_release(struct block_device *bdev)
515{
516 spin_lock(&bdev_lock);
517 if (!--bdev->bd_contains->bd_holders)
518 bdev->bd_contains->bd_holder = NULL;
519 if (!--bdev->bd_holders)
520 bdev->bd_holder = NULL;
521 spin_unlock(&bdev_lock);
522}
523
524EXPORT_SYMBOL(bd_release);
525
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800526#ifdef CONFIG_SYSFS
527/*
528 * Functions for bd_claim_by_kobject / bd_release_from_kobject
529 *
530 * If a kobject is passed to bd_claim_by_kobject()
531 * and the kobject has a parent directory,
532 * following symlinks are created:
533 * o from the kobject to the claimed bdev
534 * o from "holders" directory of the bdev to the parent of the kobject
535 * bd_release_from_kobject() removes these symlinks.
536 *
537 * Example:
538 * If /dev/dm-0 maps to /dev/sda, kobject corresponding to
539 * /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then:
540 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
541 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
542 */
543
Andrew Morton4d7dd8f2006-09-29 01:58:56 -0700544static int add_symlink(struct kobject *from, struct kobject *to)
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800545{
546 if (!from || !to)
Andrew Morton4d7dd8f2006-09-29 01:58:56 -0700547 return 0;
548 return sysfs_create_link(from, to, kobject_name(to));
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800549}
550
551static void del_symlink(struct kobject *from, struct kobject *to)
552{
553 if (!from || !to)
554 return;
555 sysfs_remove_link(from, kobject_name(to));
556}
557
558/*
559 * 'struct bd_holder' contains pointers to kobjects symlinked by
560 * bd_claim_by_kobject.
561 * It's connected to bd_holder_list which is protected by bdev->bd_sem.
562 */
563struct bd_holder {
564 struct list_head list; /* chain of holders of the bdev */
565 int count; /* references from the holder */
566 struct kobject *sdir; /* holder object, e.g. "/block/dm-0/slaves" */
567 struct kobject *hdev; /* e.g. "/block/dm-0" */
568 struct kobject *hdir; /* e.g. "/block/sda/holders" */
569 struct kobject *sdev; /* e.g. "/block/sda" */
570};
571
572/*
573 * Get references of related kobjects at once.
574 * Returns 1 on success. 0 on failure.
575 *
576 * Should call bd_holder_release_dirs() after successful use.
577 */
578static int bd_holder_grab_dirs(struct block_device *bdev,
579 struct bd_holder *bo)
580{
581 if (!bdev || !bo)
582 return 0;
583
584 bo->sdir = kobject_get(bo->sdir);
585 if (!bo->sdir)
586 return 0;
587
588 bo->hdev = kobject_get(bo->sdir->parent);
589 if (!bo->hdev)
590 goto fail_put_sdir;
591
Tejun Heo0762b8b2008-08-25 19:56:12 +0900592 bo->sdev = kobject_get(&part_to_dev(bdev->bd_part)->kobj);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800593 if (!bo->sdev)
594 goto fail_put_hdev;
595
Tejun Heo4c465012008-08-25 19:56:11 +0900596 bo->hdir = kobject_get(bdev->bd_part->holder_dir);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800597 if (!bo->hdir)
598 goto fail_put_sdev;
599
600 return 1;
601
602fail_put_sdev:
603 kobject_put(bo->sdev);
604fail_put_hdev:
605 kobject_put(bo->hdev);
606fail_put_sdir:
607 kobject_put(bo->sdir);
608
609 return 0;
610}
611
612/* Put references of related kobjects at once. */
613static void bd_holder_release_dirs(struct bd_holder *bo)
614{
615 kobject_put(bo->hdir);
616 kobject_put(bo->sdev);
617 kobject_put(bo->hdev);
618 kobject_put(bo->sdir);
619}
620
621static struct bd_holder *alloc_bd_holder(struct kobject *kobj)
622{
623 struct bd_holder *bo;
624
625 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
626 if (!bo)
627 return NULL;
628
629 bo->count = 1;
630 bo->sdir = kobj;
631
632 return bo;
633}
634
635static void free_bd_holder(struct bd_holder *bo)
636{
637 kfree(bo);
638}
639
640/**
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500641 * find_bd_holder - find matching struct bd_holder from the block device
642 *
643 * @bdev: struct block device to be searched
644 * @bo: target struct bd_holder
645 *
646 * Returns matching entry with @bo in @bdev->bd_holder_list.
647 * If found, increment the reference count and return the pointer.
648 * If not found, returns NULL.
649 */
Andrew Morton36a561d2006-10-30 22:07:03 -0800650static struct bd_holder *find_bd_holder(struct block_device *bdev,
651 struct bd_holder *bo)
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500652{
653 struct bd_holder *tmp;
654
655 list_for_each_entry(tmp, &bdev->bd_holder_list, list)
656 if (tmp->sdir == bo->sdir) {
657 tmp->count++;
658 return tmp;
659 }
660
661 return NULL;
662}
663
664/**
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800665 * add_bd_holder - create sysfs symlinks for bd_claim() relationship
666 *
667 * @bdev: block device to be bd_claimed
668 * @bo: preallocated and initialized by alloc_bd_holder()
669 *
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500670 * Add @bo to @bdev->bd_holder_list, create symlinks.
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800671 *
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500672 * Returns 0 if symlinks are created.
673 * Returns -ve if something fails.
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800674 */
675static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
676{
Johannes Weiner4e916722007-07-15 23:41:25 -0700677 int err;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800678
679 if (!bo)
Andrew Morton4d7dd8f2006-09-29 01:58:56 -0700680 return -EINVAL;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800681
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800682 if (!bd_holder_grab_dirs(bdev, bo))
Andrew Morton4d7dd8f2006-09-29 01:58:56 -0700683 return -EBUSY;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800684
Johannes Weiner4e916722007-07-15 23:41:25 -0700685 err = add_symlink(bo->sdir, bo->sdev);
686 if (err)
687 return err;
688
689 err = add_symlink(bo->hdir, bo->hdev);
690 if (err) {
691 del_symlink(bo->sdir, bo->sdev);
692 return err;
Andrew Morton4d7dd8f2006-09-29 01:58:56 -0700693 }
Johannes Weiner4e916722007-07-15 23:41:25 -0700694
695 list_add_tail(&bo->list, &bdev->bd_holder_list);
696 return 0;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800697}
698
699/**
700 * del_bd_holder - delete sysfs symlinks for bd_claim() relationship
701 *
702 * @bdev: block device to be bd_claimed
703 * @kobj: holder's kobject
704 *
705 * If there is matching entry with @kobj in @bdev->bd_holder_list
706 * and no other bd_claim() from the same kobject,
707 * remove the struct bd_holder from the list, delete symlinks for it.
708 *
709 * Returns a pointer to the struct bd_holder when it's removed from the list
710 * and ready to be freed.
711 * Returns NULL if matching claim isn't found or there is other bd_claim()
712 * by the same kobject.
713 */
714static struct bd_holder *del_bd_holder(struct block_device *bdev,
715 struct kobject *kobj)
716{
717 struct bd_holder *bo;
718
719 list_for_each_entry(bo, &bdev->bd_holder_list, list) {
720 if (bo->sdir == kobj) {
721 bo->count--;
722 BUG_ON(bo->count < 0);
723 if (!bo->count) {
724 list_del(&bo->list);
725 del_symlink(bo->sdir, bo->sdev);
726 del_symlink(bo->hdir, bo->hdev);
727 bd_holder_release_dirs(bo);
728 return bo;
729 }
730 break;
731 }
732 }
733
734 return NULL;
735}
736
737/**
738 * bd_claim_by_kobject - bd_claim() with additional kobject signature
739 *
740 * @bdev: block device to be claimed
741 * @holder: holder's signature
742 * @kobj: holder's kobject
743 *
744 * Do bd_claim() and if it succeeds, create sysfs symlinks between
745 * the bdev and the holder's kobject.
746 * Use bd_release_from_kobject() when relesing the claimed bdev.
747 *
748 * Returns 0 on success. (same as bd_claim())
749 * Returns errno on failure.
750 */
751static int bd_claim_by_kobject(struct block_device *bdev, void *holder,
752 struct kobject *kobj)
753{
Johannes Weiner4e916722007-07-15 23:41:25 -0700754 int err;
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500755 struct bd_holder *bo, *found;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800756
757 if (!kobj)
758 return -EINVAL;
759
760 bo = alloc_bd_holder(kobj);
761 if (!bo)
762 return -ENOMEM;
763
Peter Zijlstra2e7b6512006-12-08 02:36:13 -0800764 mutex_lock(&bdev->bd_mutex);
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500765
Johannes Weiner4e916722007-07-15 23:41:25 -0700766 err = bd_claim(bdev, holder);
767 if (err)
Andrew Morton4210df22007-07-15 23:41:28 -0700768 goto fail;
Johannes Weiner4e916722007-07-15 23:41:25 -0700769
770 found = find_bd_holder(bdev, bo);
771 if (found)
Andrew Morton4210df22007-07-15 23:41:28 -0700772 goto fail;
Johannes Weiner4e916722007-07-15 23:41:25 -0700773
774 err = add_bd_holder(bdev, bo);
775 if (err)
776 bd_release(bdev);
Andrew Morton4210df22007-07-15 23:41:28 -0700777 else
778 bo = NULL;
779fail:
Jun'ichi Nomurab4cf1b72006-03-27 01:18:00 -0800780 mutex_unlock(&bdev->bd_mutex);
Andrew Morton4210df22007-07-15 23:41:28 -0700781 free_bd_holder(bo);
Johannes Weiner4e916722007-07-15 23:41:25 -0700782 return err;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800783}
784
785/**
786 * bd_release_from_kobject - bd_release() with additional kobject signature
787 *
788 * @bdev: block device to be released
789 * @kobj: holder's kobject
790 *
791 * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject().
792 */
793static void bd_release_from_kobject(struct block_device *bdev,
794 struct kobject *kobj)
795{
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800796 if (!kobj)
797 return;
798
Peter Zijlstra2e7b6512006-12-08 02:36:13 -0800799 mutex_lock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800800 bd_release(bdev);
Andrew Morton4210df22007-07-15 23:41:28 -0700801 free_bd_holder(del_bd_holder(bdev, kobj));
Jun'ichi Nomurab4cf1b72006-03-27 01:18:00 -0800802 mutex_unlock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800803}
804
805/**
806 * bd_claim_by_disk - wrapper function for bd_claim_by_kobject()
807 *
808 * @bdev: block device to be claimed
809 * @holder: holder's signature
810 * @disk: holder's gendisk
811 *
812 * Call bd_claim_by_kobject() with getting @disk->slave_dir.
813 */
814int bd_claim_by_disk(struct block_device *bdev, void *holder,
815 struct gendisk *disk)
816{
817 return bd_claim_by_kobject(bdev, holder, kobject_get(disk->slave_dir));
818}
819EXPORT_SYMBOL_GPL(bd_claim_by_disk);
820
821/**
822 * bd_release_from_disk - wrapper function for bd_release_from_kobject()
823 *
824 * @bdev: block device to be claimed
825 * @disk: holder's gendisk
826 *
827 * Call bd_release_from_kobject() and put @disk->slave_dir.
828 */
829void bd_release_from_disk(struct block_device *bdev, struct gendisk *disk)
830{
831 bd_release_from_kobject(bdev, disk->slave_dir);
832 kobject_put(disk->slave_dir);
833}
834EXPORT_SYMBOL_GPL(bd_release_from_disk);
835#endif
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837/*
838 * Tries to open block device by device number. Use it ONLY if you
839 * really do not have anything better - i.e. when you are behind a
840 * truly sucky interface and all you are given is a device number. _Never_
841 * to be used for internal purposes. If you ever need it - reconsider
842 * your API.
843 */
Al Viroaeb5d722008-09-02 15:28:45 -0400844struct block_device *open_by_devnum(dev_t dev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
846 struct block_device *bdev = bdget(dev);
847 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (bdev)
Al Viro572c4892007-10-08 13:24:05 -0400849 err = blkdev_get(bdev, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return err ? ERR_PTR(err) : bdev;
851}
852
853EXPORT_SYMBOL(open_by_devnum);
854
Andrew Patterson0c002c22008-09-04 14:27:20 -0600855/**
Andrew Patterson56ade442008-09-04 14:27:40 -0600856 * flush_disk - invalidates all buffer-cache entries on a disk
857 *
858 * @bdev: struct block device to be flushed
859 *
860 * Invalidates all buffer-cache entries on a disk. It should be called
861 * when a disk has been changed -- either by a media change or online
862 * resize.
863 */
864static void flush_disk(struct block_device *bdev)
865{
866 if (__invalidate_device(bdev)) {
867 char name[BDEVNAME_SIZE] = "";
868
869 if (bdev->bd_disk)
870 disk_name(bdev->bd_disk, 0, name);
871 printk(KERN_WARNING "VFS: busy inodes on changed media or "
872 "resized disk %s\n", name);
873 }
874
875 if (!bdev->bd_disk)
876 return;
877 if (disk_partitionable(bdev->bd_disk))
878 bdev->bd_invalidated = 1;
879}
880
881/**
Randy Dunlap57d1b532008-10-09 10:42:38 +0200882 * check_disk_size_change - checks for disk size change and adjusts bdev size.
Andrew Pattersonc3279d12008-09-04 14:27:25 -0600883 * @disk: struct gendisk to check
884 * @bdev: struct bdev to adjust.
885 *
886 * This routine checks to see if the bdev size does not match the disk size
887 * and adjusts it if it differs.
888 */
889void check_disk_size_change(struct gendisk *disk, struct block_device *bdev)
890{
891 loff_t disk_size, bdev_size;
892
893 disk_size = (loff_t)get_capacity(disk) << 9;
894 bdev_size = i_size_read(bdev->bd_inode);
895 if (disk_size != bdev_size) {
896 char name[BDEVNAME_SIZE];
897
898 disk_name(disk, 0, name);
899 printk(KERN_INFO
900 "%s: detected capacity change from %lld to %lld\n",
901 name, bdev_size, disk_size);
902 i_size_write(bdev->bd_inode, disk_size);
Andrew Patterson608aeef2008-09-04 14:27:45 -0600903 flush_disk(bdev);
Andrew Pattersonc3279d12008-09-04 14:27:25 -0600904 }
905}
906EXPORT_SYMBOL(check_disk_size_change);
907
908/**
Randy Dunlap57d1b532008-10-09 10:42:38 +0200909 * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back
Andrew Patterson0c002c22008-09-04 14:27:20 -0600910 * @disk: struct gendisk to be revalidated
911 *
912 * This routine is a wrapper for lower-level driver's revalidate_disk
913 * call-backs. It is used to do common pre and post operations needed
914 * for all revalidate_disk operations.
915 */
916int revalidate_disk(struct gendisk *disk)
917{
Andrew Pattersonc3279d12008-09-04 14:27:25 -0600918 struct block_device *bdev;
Andrew Patterson0c002c22008-09-04 14:27:20 -0600919 int ret = 0;
920
921 if (disk->fops->revalidate_disk)
922 ret = disk->fops->revalidate_disk(disk);
923
Andrew Pattersonc3279d12008-09-04 14:27:25 -0600924 bdev = bdget_disk(disk, 0);
925 if (!bdev)
926 return ret;
927
928 mutex_lock(&bdev->bd_mutex);
929 check_disk_size_change(disk, bdev);
930 mutex_unlock(&bdev->bd_mutex);
931 bdput(bdev);
Andrew Patterson0c002c22008-09-04 14:27:20 -0600932 return ret;
933}
934EXPORT_SYMBOL(revalidate_disk);
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936/*
937 * This routine checks whether a removable media has been changed,
938 * and invalidates all buffer-cache-entries in that case. This
939 * is a relatively slow routine, so we have to try to minimize using
940 * it. Thus it is called only upon a 'mount' or 'open'. This
941 * is the best way of combining speed and utility, I think.
942 * People changing diskettes in the middle of an operation deserve
943 * to lose :-)
944 */
945int check_disk_change(struct block_device *bdev)
946{
947 struct gendisk *disk = bdev->bd_disk;
948 struct block_device_operations * bdops = disk->fops;
949
950 if (!bdops->media_changed)
951 return 0;
952 if (!bdops->media_changed(bdev->bd_disk))
953 return 0;
954
Andrew Patterson56ade442008-09-04 14:27:40 -0600955 flush_disk(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (bdops->revalidate_disk)
957 bdops->revalidate_disk(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return 1;
959}
960
961EXPORT_SYMBOL(check_disk_change);
962
963void bd_set_size(struct block_device *bdev, loff_t size)
964{
965 unsigned bsize = bdev_hardsect_size(bdev);
966
967 bdev->bd_inode->i_size = size;
968 while (bsize < PAGE_CACHE_SIZE) {
969 if (size & bsize)
970 break;
971 bsize <<= 1;
972 }
973 bdev->bd_block_size = bsize;
974 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
975}
976EXPORT_SYMBOL(bd_set_size);
977
Al Viro9a1c3542008-02-22 20:40:24 -0500978static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
NeilBrown37be4122006-12-08 02:36:16 -0800979
Peter Zijlstra6d740cd2007-02-20 13:58:18 -0800980/*
981 * bd_mutex locking:
982 *
983 * mutex_lock(part->bd_mutex)
984 * mutex_lock_nested(whole->bd_mutex, 1)
985 */
986
Al Viro572c4892007-10-08 13:24:05 -0400987static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 struct gendisk *disk;
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -0700990 int ret;
Tejun Heocf771cb2008-09-03 09:01:09 +0200991 int partno;
Al Virofe6e9c12008-06-23 08:30:55 -0400992 int perm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Al Viro572c4892007-10-08 13:24:05 -0400994 if (mode & FMODE_READ)
Al Virofe6e9c12008-06-23 08:30:55 -0400995 perm |= MAY_READ;
Al Viro572c4892007-10-08 13:24:05 -0400996 if (mode & FMODE_WRITE)
Al Virofe6e9c12008-06-23 08:30:55 -0400997 perm |= MAY_WRITE;
998 /*
999 * hooks: /n/, see "layering violations".
1000 */
1001 ret = devcgroup_inode_permission(bdev->bd_inode, perm);
Al Viro82666022008-08-01 05:32:04 -04001002 if (ret != 0) {
1003 bdput(bdev);
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -07001004 return ret;
Al Viro82666022008-08-01 05:32:04 -04001005 }
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -07001006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 lock_kernel();
NeilBrownd3374822009-01-09 08:31:10 +11001008 restart:
Tejun Heo0762b8b2008-08-25 19:56:12 +09001009
Tejun Heo89f97492008-11-05 10:21:06 +01001010 ret = -ENXIO;
Tejun Heocf771cb2008-09-03 09:01:09 +02001011 disk = get_gendisk(bdev->bd_dev, &partno);
Tejun Heo0762b8b2008-08-25 19:56:12 +09001012 if (!disk)
1013 goto out_unlock_kernel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
NeilBrown6796bf52006-12-08 02:36:16 -08001015 mutex_lock_nested(&bdev->bd_mutex, for_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (!bdev->bd_openers) {
1017 bdev->bd_disk = disk;
1018 bdev->bd_contains = bdev;
Tejun Heocf771cb2008-09-03 09:01:09 +02001019 if (!partno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 struct backing_dev_info *bdi;
Tejun Heo89f97492008-11-05 10:21:06 +01001021
1022 ret = -ENXIO;
1023 bdev->bd_part = disk_get_part(disk, partno);
1024 if (!bdev->bd_part)
1025 goto out_clear;
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (disk->fops->open) {
Al Viro572c4892007-10-08 13:24:05 -04001028 ret = disk->fops->open(bdev, mode);
NeilBrownd3374822009-01-09 08:31:10 +11001029 if (ret == -ERESTARTSYS) {
1030 /* Lost a race with 'disk' being
1031 * deleted, try again.
1032 * See md.c
1033 */
1034 disk_put_part(bdev->bd_part);
1035 bdev->bd_part = NULL;
1036 module_put(disk->fops->owner);
1037 put_disk(disk);
1038 bdev->bd_disk = NULL;
1039 mutex_unlock(&bdev->bd_mutex);
1040 goto restart;
1041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (ret)
Tejun Heo0762b8b2008-08-25 19:56:12 +09001043 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
1045 if (!bdev->bd_openers) {
1046 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
1047 bdi = blk_get_backing_dev_info(bdev);
1048 if (bdi == NULL)
1049 bdi = &default_backing_dev_info;
1050 bdev->bd_inode->i_data.backing_dev_info = bdi;
1051 }
1052 if (bdev->bd_invalidated)
1053 rescan_partitions(disk, bdev);
1054 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 struct block_device *whole;
1056 whole = bdget_disk(disk, 0);
1057 ret = -ENOMEM;
1058 if (!whole)
Tejun Heo0762b8b2008-08-25 19:56:12 +09001059 goto out_clear;
NeilBrown37be4122006-12-08 02:36:16 -08001060 BUG_ON(for_part);
Al Viro572c4892007-10-08 13:24:05 -04001061 ret = __blkdev_get(whole, mode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (ret)
Tejun Heo0762b8b2008-08-25 19:56:12 +09001063 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 bdev->bd_contains = whole;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 bdev->bd_inode->i_data.backing_dev_info =
1066 whole->bd_inode->i_data.backing_dev_info;
Tejun Heo89f97492008-11-05 10:21:06 +01001067 bdev->bd_part = disk_get_part(disk, partno);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001068 if (!(disk->flags & GENHD_FL_UP) ||
Tejun Heo89f97492008-11-05 10:21:06 +01001069 !bdev->bd_part || !bdev->bd_part->nr_sects) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 ret = -ENXIO;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001071 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
Tejun Heo89f97492008-11-05 10:21:06 +01001073 bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
1075 } else {
1076 put_disk(disk);
Tejun Heo0762b8b2008-08-25 19:56:12 +09001077 module_put(disk->fops->owner);
Tejun Heo0762b8b2008-08-25 19:56:12 +09001078 disk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (bdev->bd_contains == bdev) {
1080 if (bdev->bd_disk->fops->open) {
Al Viro572c4892007-10-08 13:24:05 -04001081 ret = bdev->bd_disk->fops->open(bdev, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (ret)
Tejun Heo0762b8b2008-08-25 19:56:12 +09001083 goto out_unlock_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
1085 if (bdev->bd_invalidated)
1086 rescan_partitions(bdev->bd_disk, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
1088 }
1089 bdev->bd_openers++;
NeilBrown37be4122006-12-08 02:36:16 -08001090 if (for_part)
1091 bdev->bd_part_count++;
Arjan van de Venc039e312006-03-23 03:00:28 -08001092 mutex_unlock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 unlock_kernel();
1094 return 0;
1095
Tejun Heo0762b8b2008-08-25 19:56:12 +09001096 out_clear:
Tejun Heo89f97492008-11-05 10:21:06 +01001097 disk_put_part(bdev->bd_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 bdev->bd_disk = NULL;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001099 bdev->bd_part = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
1101 if (bdev != bdev->bd_contains)
Al Viro572c4892007-10-08 13:24:05 -04001102 __blkdev_put(bdev->bd_contains, mode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 bdev->bd_contains = NULL;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001104 out_unlock_bdev:
Arjan van de Venc039e312006-03-23 03:00:28 -08001105 mutex_unlock(&bdev->bd_mutex);
Tejun Heo0762b8b2008-08-25 19:56:12 +09001106 out_unlock_kernel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 unlock_kernel();
Tejun Heo0762b8b2008-08-25 19:56:12 +09001108
Tejun Heo0762b8b2008-08-25 19:56:12 +09001109 if (disk)
1110 module_put(disk->fops->owner);
1111 put_disk(disk);
1112 bdput(bdev);
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return ret;
1115}
1116
Al Viro572c4892007-10-08 13:24:05 -04001117int blkdev_get(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Al Viro572c4892007-10-08 13:24:05 -04001119 return __blkdev_get(bdev, mode, 0);
NeilBrown37be4122006-12-08 02:36:16 -08001120}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121EXPORT_SYMBOL(blkdev_get);
1122
1123static int blkdev_open(struct inode * inode, struct file * filp)
1124{
1125 struct block_device *bdev;
1126 int res;
1127
1128 /*
1129 * Preserve backwards compatibility and allow large file access
1130 * even if userspace doesn't ask for it explicitly. Some mkfs
1131 * binary needs it. We might want to drop this workaround
1132 * during an unstable branch.
1133 */
1134 filp->f_flags |= O_LARGEFILE;
1135
Al Viro572c4892007-10-08 13:24:05 -04001136 if (filp->f_flags & O_NDELAY)
1137 filp->f_mode |= FMODE_NDELAY;
1138 if (filp->f_flags & O_EXCL)
1139 filp->f_mode |= FMODE_EXCL;
1140 if ((filp->f_flags & O_ACCMODE) == 3)
1141 filp->f_mode |= FMODE_WRITE_IOCTL;
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 bdev = bd_acquire(inode);
Pavel Emelianov6a2aae02006-10-28 10:38:33 -07001144 if (bdev == NULL)
1145 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Al Viro572c4892007-10-08 13:24:05 -04001147 filp->f_mapping = bdev->bd_inode->i_mapping;
1148
1149 res = blkdev_get(bdev, filp->f_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (res)
1151 return res;
1152
Christoph Hellwigebbefc02008-11-05 14:54:41 +01001153 if (filp->f_mode & FMODE_EXCL) {
1154 res = bd_claim(bdev, filp);
1155 if (res)
1156 goto out_blkdev_put;
1157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Christoph Hellwigebbefc02008-11-05 14:54:41 +01001159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Christoph Hellwigebbefc02008-11-05 14:54:41 +01001161 out_blkdev_put:
Al Viro9a1c3542008-02-22 20:40:24 -05001162 blkdev_put(bdev, filp->f_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return res;
1164}
1165
Al Viro9a1c3542008-02-22 20:40:24 -05001166static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001167{
1168 int ret = 0;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001169 struct gendisk *disk = bdev->bd_disk;
NeilBrown37be4122006-12-08 02:36:16 -08001170 struct block_device *victim = NULL;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001171
NeilBrown6796bf52006-12-08 02:36:16 -08001172 mutex_lock_nested(&bdev->bd_mutex, for_part);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001173 lock_kernel();
NeilBrown37be4122006-12-08 02:36:16 -08001174 if (for_part)
1175 bdev->bd_part_count--;
1176
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001177 if (!--bdev->bd_openers) {
1178 sync_blockdev(bdev);
1179 kill_bdev(bdev);
1180 }
1181 if (bdev->bd_contains == bdev) {
1182 if (disk->fops->release)
Al Viro9a1c3542008-02-22 20:40:24 -05001183 ret = disk->fops->release(disk, mode);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001184 }
1185 if (!bdev->bd_openers) {
1186 struct module *owner = disk->fops->owner;
1187
1188 put_disk(disk);
1189 module_put(owner);
Tejun Heo0762b8b2008-08-25 19:56:12 +09001190 disk_put_part(bdev->bd_part);
1191 bdev->bd_part = NULL;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001192 bdev->bd_disk = NULL;
1193 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
NeilBrown37be4122006-12-08 02:36:16 -08001194 if (bdev != bdev->bd_contains)
1195 victim = bdev->bd_contains;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001196 bdev->bd_contains = NULL;
1197 }
1198 unlock_kernel();
1199 mutex_unlock(&bdev->bd_mutex);
1200 bdput(bdev);
NeilBrown37be4122006-12-08 02:36:16 -08001201 if (victim)
Al Viro9a1c3542008-02-22 20:40:24 -05001202 __blkdev_put(victim, mode, 1);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001203 return ret;
1204}
1205
Al Viro9a1c3542008-02-22 20:40:24 -05001206int blkdev_put(struct block_device *bdev, fmode_t mode)
NeilBrown37be4122006-12-08 02:36:16 -08001207{
Al Viro9a1c3542008-02-22 20:40:24 -05001208 return __blkdev_put(bdev, mode, 0);
NeilBrown37be4122006-12-08 02:36:16 -08001209}
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001210EXPORT_SYMBOL(blkdev_put);
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212static int blkdev_close(struct inode * inode, struct file * filp)
1213{
1214 struct block_device *bdev = I_BDEV(filp->f_mapping->host);
1215 if (bdev->bd_holder == filp)
1216 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001217 return blkdev_put(bdev, filp->f_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07001220static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
Al Viro56b26ad2008-09-19 03:17:36 -04001222 struct block_device *bdev = I_BDEV(file->f_mapping->host);
1223 fmode_t mode = file->f_mode;
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +01001224
1225 /*
1226 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
1227 * to updated it before every ioctl.
1228 */
Al Viro56b26ad2008-09-19 03:17:36 -04001229 if (file->f_flags & O_NDELAY)
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +01001230 mode |= FMODE_NDELAY;
1231 else
1232 mode &= ~FMODE_NDELAY;
1233
Al Viro56b26ad2008-09-19 03:17:36 -04001234 return blkdev_ioctl(bdev, mode, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
Adrian Bunk4c54ac62008-02-18 13:48:31 +01001237static const struct address_space_operations def_blk_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 .readpage = blkdev_readpage,
1239 .writepage = blkdev_writepage,
1240 .sync_page = block_sync_page,
Nick Piggin6272b5a2007-10-16 01:25:04 -07001241 .write_begin = blkdev_write_begin,
1242 .write_end = blkdev_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 .writepages = generic_writepages,
1244 .direct_IO = blkdev_direct_IO,
1245};
1246
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001247const struct file_operations def_blk_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 .open = blkdev_open,
1249 .release = blkdev_close,
1250 .llseek = block_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -07001251 .read = do_sync_read,
1252 .write = do_sync_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 .aio_read = generic_file_aio_read,
Badari Pulavarty027445c2006-09-30 23:28:46 -07001254 .aio_write = generic_file_aio_write_nolock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 .mmap = generic_file_mmap,
1256 .fsync = block_fsync,
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07001257 .unlocked_ioctl = block_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258#ifdef CONFIG_COMPAT
1259 .compat_ioctl = compat_blkdev_ioctl,
1260#endif
Jens Axboe7f9c51f2006-05-01 19:59:32 +02001261 .splice_read = generic_file_splice_read,
1262 .splice_write = generic_file_splice_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263};
1264
1265int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
1266{
1267 int res;
1268 mm_segment_t old_fs = get_fs();
1269 set_fs(KERNEL_DS);
Al Viro56b26ad2008-09-19 03:17:36 -04001270 res = blkdev_ioctl(bdev, 0, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 set_fs(old_fs);
1272 return res;
1273}
1274
1275EXPORT_SYMBOL(ioctl_by_bdev);
1276
1277/**
1278 * lookup_bdev - lookup a struct block_device by name
Randy Dunlap94e29592009-01-06 14:41:15 -08001279 * @pathname: special file representing the block device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 *
Randy Dunlap57d1b532008-10-09 10:42:38 +02001281 * Get a reference to the blockdevice at @pathname in the current
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 * namespace if possible and return it. Return ERR_PTR(error)
1283 * otherwise.
1284 */
Al Viro421748e2008-08-02 01:04:36 -04001285struct block_device *lookup_bdev(const char *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
1287 struct block_device *bdev;
1288 struct inode *inode;
Al Viro421748e2008-08-02 01:04:36 -04001289 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 int error;
1291
Al Viro421748e2008-08-02 01:04:36 -04001292 if (!pathname || !*pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 return ERR_PTR(-EINVAL);
1294
Al Viro421748e2008-08-02 01:04:36 -04001295 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (error)
1297 return ERR_PTR(error);
1298
Al Viro421748e2008-08-02 01:04:36 -04001299 inode = path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 error = -ENOTBLK;
1301 if (!S_ISBLK(inode->i_mode))
1302 goto fail;
1303 error = -EACCES;
Al Viro421748e2008-08-02 01:04:36 -04001304 if (path.mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 goto fail;
1306 error = -ENOMEM;
1307 bdev = bd_acquire(inode);
1308 if (!bdev)
1309 goto fail;
1310out:
Al Viro421748e2008-08-02 01:04:36 -04001311 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 return bdev;
1313fail:
1314 bdev = ERR_PTR(error);
1315 goto out;
1316}
Al Virod5686b42008-08-01 05:00:11 -04001317EXPORT_SYMBOL(lookup_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319/**
Al Viro30c40d22008-02-22 19:50:45 -05001320 * open_bdev_exclusive - open a block device by name and set it up for use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 *
1322 * @path: special file representing the block device
Al Viro30c40d22008-02-22 19:50:45 -05001323 * @mode: FMODE_... combination to pass be used
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 * @holder: owner for exclusion
1325 *
1326 * Open the blockdevice described by the special file at @path, claim it
1327 * for the @holder.
1328 */
Al Viro30c40d22008-02-22 19:50:45 -05001329struct block_device *open_bdev_exclusive(const char *path, fmode_t mode, void *holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
1331 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 int error = 0;
1333
1334 bdev = lookup_bdev(path);
1335 if (IS_ERR(bdev))
1336 return bdev;
1337
Al Viro572c4892007-10-08 13:24:05 -04001338 error = blkdev_get(bdev, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if (error)
1340 return ERR_PTR(error);
1341 error = -EACCES;
Al Viro30c40d22008-02-22 19:50:45 -05001342 if ((mode & FMODE_WRITE) && bdev_read_only(bdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 goto blkdev_put;
1344 error = bd_claim(bdev, holder);
1345 if (error)
1346 goto blkdev_put;
1347
1348 return bdev;
1349
1350blkdev_put:
Al Viro9a1c3542008-02-22 20:40:24 -05001351 blkdev_put(bdev, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 return ERR_PTR(error);
1353}
1354
Al Viro30c40d22008-02-22 19:50:45 -05001355EXPORT_SYMBOL(open_bdev_exclusive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357/**
Al Viro30c40d22008-02-22 19:50:45 -05001358 * close_bdev_exclusive - close a blockdevice opened by open_bdev_exclusive()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 *
1360 * @bdev: blockdevice to close
Al Viro30c40d22008-02-22 19:50:45 -05001361 * @mode: mode, must match that used to open.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 *
Al Viro30c40d22008-02-22 19:50:45 -05001363 * This is the counterpart to open_bdev_exclusive().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 */
Al Viro30c40d22008-02-22 19:50:45 -05001365void close_bdev_exclusive(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
1367 bd_release(bdev);
Al Viro30c40d22008-02-22 19:50:45 -05001368 blkdev_put(bdev, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369}
1370
Al Viro30c40d22008-02-22 19:50:45 -05001371EXPORT_SYMBOL(close_bdev_exclusive);
David Howellsb71e8a42006-08-29 19:06:11 +01001372
1373int __invalidate_device(struct block_device *bdev)
1374{
1375 struct super_block *sb = get_super(bdev);
1376 int res = 0;
1377
1378 if (sb) {
1379 /*
1380 * no need to lock the super, get_super holds the
1381 * read mutex so the filesystem cannot go away
1382 * under us (->put_super runs with the write lock
1383 * hold).
1384 */
1385 shrink_dcache_sb(sb);
1386 res = invalidate_inodes(sb);
1387 drop_super(sb);
1388 }
Peter Zijlstraf98393a2007-05-06 14:49:54 -07001389 invalidate_bdev(bdev);
David Howellsb71e8a42006-08-29 19:06:11 +01001390 return res;
1391}
1392EXPORT_SYMBOL(__invalidate_device);