blob: fc48912354d164d6304688af8a1fbd51a81f417c [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>
Nick Piggin585d3bc2009-02-25 10:44:19 +010021#include <linux/pagevec.h>
David Howells811d7362006-08-29 19:06:09 +010022#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/mpage.h>
24#include <linux/mount.h>
25#include <linux/uio.h>
26#include <linux/namei.h>
Vignesh Babu BM1368c4f2007-05-08 00:24:32 -070027#include <linux/log2.h>
Catalin Marinas2e1483c2009-06-11 13:24:13 +010028#include <linux/kmemleak.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/uaccess.h>
David Howells07f3f052006-09-30 20:52:18 +020030#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32struct bdev_inode {
33 struct block_device bdev;
34 struct inode vfs_inode;
35};
36
Adrian Bunk4c54ac62008-02-18 13:48:31 +010037static const struct address_space_operations def_blk_aops;
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static inline struct bdev_inode *BDEV_I(struct inode *inode)
40{
41 return container_of(inode, struct bdev_inode, vfs_inode);
42}
43
44inline struct block_device *I_BDEV(struct inode *inode)
45{
46 return &BDEV_I(inode)->bdev;
47}
48
49EXPORT_SYMBOL(I_BDEV);
50
Dave Chinnera5491e02010-10-21 11:49:26 +110051/*
52 * move the inode from it's current bdi to the a new bdi. if the inode is dirty
53 * we need to move it onto the dirty list of @dst so that the inode is always
54 * on the right list.
55 */
56static void bdev_inode_switch_bdi(struct inode *inode,
57 struct backing_dev_info *dst)
58{
59 spin_lock(&inode_lock);
60 inode->i_data.backing_dev_info = dst;
61 if (inode->i_state & I_DIRTY)
Nick Piggin7ccf19a2010-10-21 11:49:30 +110062 list_move(&inode->i_wb_list, &dst->wb.b_dirty);
Dave Chinnera5491e02010-10-21 11:49:26 +110063 spin_unlock(&inode_lock);
64}
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static sector_t max_block(struct block_device *bdev)
67{
68 sector_t retval = ~((sector_t)0);
69 loff_t sz = i_size_read(bdev->bd_inode);
70
71 if (sz) {
72 unsigned int size = block_size(bdev);
73 unsigned int sizebits = blksize_bits(size);
74 retval = (sz >> sizebits);
75 }
76 return retval;
77}
78
Peter Zijlstraf9a14392007-05-06 14:49:55 -070079/* Kill _all_ buffers and pagecache , dirty or not.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static void kill_bdev(struct block_device *bdev)
81{
Peter Zijlstraf9a14392007-05-06 14:49:55 -070082 if (bdev->bd_inode->i_mapping->nrpages == 0)
83 return;
84 invalidate_bh_lrus();
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
86}
87
88int set_blocksize(struct block_device *bdev, int size)
89{
90 /* Size must be a power of two, and between 512 and PAGE_SIZE */
Vignesh Babu BM1368c4f2007-05-08 00:24:32 -070091 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return -EINVAL;
93
94 /* Size cannot be smaller than the size supported by the device */
Martin K. Petersene1defc42009-05-22 17:17:49 -040095 if (size < bdev_logical_block_size(bdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return -EINVAL;
97
98 /* Don't change the size if it is same as current */
99 if (bdev->bd_block_size != size) {
100 sync_blockdev(bdev);
101 bdev->bd_block_size = size;
102 bdev->bd_inode->i_blkbits = blksize_bits(size);
103 kill_bdev(bdev);
104 }
105 return 0;
106}
107
108EXPORT_SYMBOL(set_blocksize);
109
110int sb_set_blocksize(struct super_block *sb, int size)
111{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if (set_blocksize(sb->s_bdev, size))
113 return 0;
114 /* If we get here, we know size is power of two
115 * and it's value is between 512 and PAGE_SIZE */
116 sb->s_blocksize = size;
Coywolf Qi Hunt38885bd2006-03-24 03:18:05 -0800117 sb->s_blocksize_bits = blksize_bits(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return sb->s_blocksize;
119}
120
121EXPORT_SYMBOL(sb_set_blocksize);
122
123int sb_min_blocksize(struct super_block *sb, int size)
124{
Martin K. Petersene1defc42009-05-22 17:17:49 -0400125 int minsize = bdev_logical_block_size(sb->s_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 if (size < minsize)
127 size = minsize;
128 return sb_set_blocksize(sb, size);
129}
130
131EXPORT_SYMBOL(sb_min_blocksize);
132
133static int
134blkdev_get_block(struct inode *inode, sector_t iblock,
135 struct buffer_head *bh, int create)
136{
137 if (iblock >= max_block(I_BDEV(inode))) {
138 if (create)
139 return -EIO;
140
141 /*
142 * for reads, we're just trying to fill a partial page.
143 * return a hole, they will have to call get_block again
144 * before they can fill it, and they will get -EIO at that
145 * time
146 */
147 return 0;
148 }
149 bh->b_bdev = I_BDEV(inode);
150 bh->b_blocknr = iblock;
151 set_buffer_mapped(bh);
152 return 0;
153}
154
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800155static int
156blkdev_get_blocks(struct inode *inode, sector_t iblock,
157 struct buffer_head *bh, int create)
158{
159 sector_t end_block = max_block(I_BDEV(inode));
160 unsigned long max_blocks = bh->b_size >> inode->i_blkbits;
161
162 if ((iblock + max_blocks) > end_block) {
163 max_blocks = end_block - iblock;
164 if ((long)max_blocks <= 0) {
165 if (create)
166 return -EIO; /* write fully beyond EOF */
167 /*
168 * It is a read which is fully beyond EOF. We return
169 * a !buffer_mapped buffer
170 */
171 max_blocks = 0;
172 }
173 }
174
175 bh->b_bdev = I_BDEV(inode);
176 bh->b_blocknr = iblock;
177 bh->b_size = max_blocks << inode->i_blkbits;
178 if (max_blocks)
179 set_buffer_mapped(bh);
180 return 0;
181}
182
183static ssize_t
184blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
185 loff_t offset, unsigned long nr_segs)
186{
187 struct file *file = iocb->ki_filp;
188 struct inode *inode = file->f_mapping->host;
189
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +0200190 return __blockdev_direct_IO(rw, iocb, inode, I_BDEV(inode), iov, offset,
191 nr_segs, blkdev_get_blocks, NULL, NULL, 0);
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800192}
193
Jan Kara5cee5812009-04-27 16:43:51 +0200194int __sync_blockdev(struct block_device *bdev, int wait)
195{
196 if (!bdev)
197 return 0;
198 if (!wait)
199 return filemap_flush(bdev->bd_inode->i_mapping);
200 return filemap_write_and_wait(bdev->bd_inode->i_mapping);
201}
202
Nick Piggin585d3bc2009-02-25 10:44:19 +0100203/*
204 * Write out and wait upon all the dirty data associated with a block
205 * device via its mapping. Does not take the superblock lock.
206 */
207int sync_blockdev(struct block_device *bdev)
208{
Jan Kara5cee5812009-04-27 16:43:51 +0200209 return __sync_blockdev(bdev, 1);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100210}
211EXPORT_SYMBOL(sync_blockdev);
212
213/*
214 * Write out and wait upon all dirty data associated with this
215 * device. Filesystem data as well as the underlying block
216 * device. Takes the superblock lock.
217 */
218int fsync_bdev(struct block_device *bdev)
219{
220 struct super_block *sb = get_super(bdev);
221 if (sb) {
Jan Kara60b06802009-04-27 16:43:53 +0200222 int res = sync_filesystem(sb);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100223 drop_super(sb);
224 return res;
225 }
226 return sync_blockdev(bdev);
227}
Al Viro47e44912009-04-01 07:07:16 -0400228EXPORT_SYMBOL(fsync_bdev);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100229
230/**
231 * freeze_bdev -- lock a filesystem and force it into a consistent state
232 * @bdev: blockdevice to lock
233 *
Nick Piggin585d3bc2009-02-25 10:44:19 +0100234 * If a superblock is found on this device, we take the s_umount semaphore
235 * on it to make sure nobody unmounts until the snapshot creation is done.
236 * The reference counter (bd_fsfreeze_count) guarantees that only the last
237 * unfreeze process can unfreeze the frozen filesystem actually when multiple
238 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
239 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
240 * actually.
241 */
242struct super_block *freeze_bdev(struct block_device *bdev)
243{
244 struct super_block *sb;
245 int error = 0;
246
247 mutex_lock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig45042302009-08-03 23:28:35 +0200248 if (++bdev->bd_fsfreeze_count > 1) {
249 /*
250 * We don't even need to grab a reference - the first call
251 * to freeze_bdev grab an active reference and only the last
252 * thaw_bdev drops it.
253 */
Nick Piggin585d3bc2009-02-25 10:44:19 +0100254 sb = get_super(bdev);
Christoph Hellwig45042302009-08-03 23:28:35 +0200255 drop_super(sb);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100256 mutex_unlock(&bdev->bd_fsfreeze_mutex);
257 return sb;
258 }
Nick Piggin585d3bc2009-02-25 10:44:19 +0100259
Christoph Hellwig45042302009-08-03 23:28:35 +0200260 sb = get_active_super(bdev);
261 if (!sb)
262 goto out;
Josef Bacik18e9e512010-03-23 10:34:56 -0400263 error = freeze_super(sb);
264 if (error) {
265 deactivate_super(sb);
266 bdev->bd_fsfreeze_count--;
Christoph Hellwig45042302009-08-03 23:28:35 +0200267 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Josef Bacik18e9e512010-03-23 10:34:56 -0400268 return ERR_PTR(error);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100269 }
Josef Bacik18e9e512010-03-23 10:34:56 -0400270 deactivate_super(sb);
Christoph Hellwig45042302009-08-03 23:28:35 +0200271 out:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100272 sync_blockdev(bdev);
273 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig4fadd7b2009-08-03 23:28:06 +0200274 return sb; /* thaw_bdev releases s->s_umount */
Nick Piggin585d3bc2009-02-25 10:44:19 +0100275}
276EXPORT_SYMBOL(freeze_bdev);
277
278/**
279 * thaw_bdev -- unlock filesystem
280 * @bdev: blockdevice to unlock
281 * @sb: associated superblock
282 *
283 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
284 */
285int thaw_bdev(struct block_device *bdev, struct super_block *sb)
286{
Christoph Hellwig45042302009-08-03 23:28:35 +0200287 int error = -EINVAL;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100288
289 mutex_lock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig45042302009-08-03 23:28:35 +0200290 if (!bdev->bd_fsfreeze_count)
Josef Bacik18e9e512010-03-23 10:34:56 -0400291 goto out;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100292
Christoph Hellwig45042302009-08-03 23:28:35 +0200293 error = 0;
294 if (--bdev->bd_fsfreeze_count > 0)
Josef Bacik18e9e512010-03-23 10:34:56 -0400295 goto out;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100296
Christoph Hellwig45042302009-08-03 23:28:35 +0200297 if (!sb)
Josef Bacik18e9e512010-03-23 10:34:56 -0400298 goto out;
Christoph Hellwig45042302009-08-03 23:28:35 +0200299
Josef Bacik18e9e512010-03-23 10:34:56 -0400300 error = thaw_super(sb);
301 if (error) {
302 bdev->bd_fsfreeze_count++;
303 mutex_unlock(&bdev->bd_fsfreeze_mutex);
304 return error;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100305 }
Josef Bacik18e9e512010-03-23 10:34:56 -0400306out:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100307 mutex_unlock(&bdev->bd_fsfreeze_mutex);
308 return 0;
309}
310EXPORT_SYMBOL(thaw_bdev);
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
313{
314 return block_write_full_page(page, blkdev_get_block, wbc);
315}
316
317static int blkdev_readpage(struct file * file, struct page * page)
318{
319 return block_read_full_page(page, blkdev_get_block);
320}
321
Nick Piggin6272b5a2007-10-16 01:25:04 -0700322static int blkdev_write_begin(struct file *file, struct address_space *mapping,
323 loff_t pos, unsigned len, unsigned flags,
324 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Christoph Hellwig155130a2010-06-04 11:29:58 +0200326 return block_write_begin(mapping, pos, len, flags, pagep,
327 blkdev_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Nick Piggin6272b5a2007-10-16 01:25:04 -0700330static int blkdev_write_end(struct file *file, struct address_space *mapping,
331 loff_t pos, unsigned len, unsigned copied,
332 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Nick Piggin6272b5a2007-10-16 01:25:04 -0700334 int ret;
335 ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
336
337 unlock_page(page);
338 page_cache_release(page);
339
340 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
343/*
344 * private llseek:
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800345 * for a block special file file->f_path.dentry->d_inode->i_size is zero
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 * so we compute the size by hand (just as in block_read/write above)
347 */
348static loff_t block_llseek(struct file *file, loff_t offset, int origin)
349{
350 struct inode *bd_inode = file->f_mapping->host;
351 loff_t size;
352 loff_t retval;
353
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800354 mutex_lock(&bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 size = i_size_read(bd_inode);
356
357 switch (origin) {
358 case 2:
359 offset += size;
360 break;
361 case 1:
362 offset += file->f_pos;
363 }
364 retval = -EINVAL;
365 if (offset >= 0 && offset <= size) {
366 if (offset != file->f_pos) {
367 file->f_pos = offset;
368 }
369 retval = offset;
370 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800371 mutex_unlock(&bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return retval;
373}
374
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200375int blkdev_fsync(struct file *filp, int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400377 struct inode *bd_inode = filp->f_mapping->host;
378 struct block_device *bdev = I_BDEV(bd_inode);
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100379 int error;
380
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400381 /*
382 * There is no need to serialise calls to blkdev_issue_flush with
383 * i_mutex and doing so causes performance issues with concurrent
384 * O_SYNC writers to a block device.
385 */
386 mutex_unlock(&bd_inode->i_mutex);
387
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200388 error = blkdev_issue_flush(bdev, GFP_KERNEL, NULL);
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100389 if (error == -EOPNOTSUPP)
390 error = 0;
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400391
392 mutex_lock(&bd_inode->i_mutex);
393
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100394 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
Andrew Mortonb1dd3b22010-04-06 14:35:00 -0700396EXPORT_SYMBOL(blkdev_fsync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398/*
399 * pseudo-fs
400 */
401
402static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
Christoph Lametere18b8902006-12-06 20:33:20 -0800403static struct kmem_cache * bdev_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405static struct inode *bdev_alloc_inode(struct super_block *sb)
406{
Christoph Lametere94b1762006-12-06 20:33:17 -0800407 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (!ei)
409 return NULL;
410 return &ei->vfs_inode;
411}
412
413static void bdev_destroy_inode(struct inode *inode)
414{
415 struct bdev_inode *bdi = BDEV_I(inode);
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 kmem_cache_free(bdev_cachep, bdi);
418}
419
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700420static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 struct bdev_inode *ei = (struct bdev_inode *) foo;
423 struct block_device *bdev = &ei->bdev;
424
Christoph Lametera35afb82007-05-16 22:10:57 -0700425 memset(bdev, 0, sizeof(*bdev));
426 mutex_init(&bdev->bd_mutex);
Christoph Lametera35afb82007-05-16 22:10:57 -0700427 INIT_LIST_HEAD(&bdev->bd_inodes);
428 INIT_LIST_HEAD(&bdev->bd_list);
Christoph Lametera35afb82007-05-16 22:10:57 -0700429 inode_init_once(&ei->vfs_inode);
Takashi Satofcccf502009-01-09 16:40:59 -0800430 /* Initialize mutex for freeze. */
431 mutex_init(&bdev->bd_fsfreeze_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
434static inline void __bd_forget(struct inode *inode)
435{
436 list_del_init(&inode->i_devices);
437 inode->i_bdev = NULL;
438 inode->i_mapping = &inode->i_data;
439}
440
Al Virob57922d2010-06-07 14:34:48 -0400441static void bdev_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
443 struct block_device *bdev = &BDEV_I(inode)->bdev;
444 struct list_head *p;
Al Virob57922d2010-06-07 14:34:48 -0400445 truncate_inode_pages(&inode->i_data, 0);
446 invalidate_inode_buffers(inode); /* is it needed here? */
447 end_writeback(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 spin_lock(&bdev_lock);
449 while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
450 __bd_forget(list_entry(p, struct inode, i_devices));
451 }
452 list_del_init(&bdev->bd_list);
453 spin_unlock(&bdev_lock);
454}
455
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800456static const struct super_operations bdev_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 .statfs = simple_statfs,
458 .alloc_inode = bdev_alloc_inode,
459 .destroy_inode = bdev_destroy_inode,
460 .drop_inode = generic_delete_inode,
Al Virob57922d2010-06-07 14:34:48 -0400461 .evict_inode = bdev_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462};
463
Al Viro51139ad2010-07-25 23:47:46 +0400464static struct dentry *bd_mount(struct file_system_type *fs_type,
465 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Al Viro51139ad2010-07-25 23:47:46 +0400467 return mount_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
470static struct file_system_type bd_type = {
471 .name = "bdev",
Al Viro51139ad2010-07-25 23:47:46 +0400472 .mount = bd_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 .kill_sb = kill_anon_super,
474};
475
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800476struct super_block *blockdev_superblock __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478void __init bdev_cache_init(void)
479{
480 int err;
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800481 struct vfsmount *bd_mnt;
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800484 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
485 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +0900486 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 err = register_filesystem(&bd_type);
488 if (err)
489 panic("Cannot register bdev pseudo-fs");
490 bd_mnt = kern_mount(&bd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (IS_ERR(bd_mnt))
492 panic("Cannot create bdev pseudo-fs");
Catalin Marinas2e1483c2009-06-11 13:24:13 +0100493 /*
494 * This vfsmount structure is only used to obtain the
495 * blockdev_superblock, so tell kmemleak not to report it.
496 */
497 kmemleak_not_leak(bd_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
499}
500
501/*
502 * Most likely _very_ bad one - but then it's hardly critical for small
503 * /dev and can be fixed when somebody will need really large one.
504 * Keep in mind that it will be fed through icache hash function too.
505 */
506static inline unsigned long hash(dev_t dev)
507{
508 return MAJOR(dev)+MINOR(dev);
509}
510
511static int bdev_test(struct inode *inode, void *data)
512{
513 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
514}
515
516static int bdev_set(struct inode *inode, void *data)
517{
518 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
519 return 0;
520}
521
522static LIST_HEAD(all_bdevs);
523
524struct block_device *bdget(dev_t dev)
525{
526 struct block_device *bdev;
527 struct inode *inode;
528
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800529 inode = iget5_locked(blockdev_superblock, hash(dev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 bdev_test, bdev_set, &dev);
531
532 if (!inode)
533 return NULL;
534
535 bdev = &BDEV_I(inode)->bdev;
536
537 if (inode->i_state & I_NEW) {
538 bdev->bd_contains = NULL;
539 bdev->bd_inode = inode;
540 bdev->bd_block_size = (1 << inode->i_blkbits);
541 bdev->bd_part_count = 0;
542 bdev->bd_invalidated = 0;
543 inode->i_mode = S_IFBLK;
544 inode->i_rdev = dev;
545 inode->i_bdev = bdev;
546 inode->i_data.a_ops = &def_blk_aops;
547 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
548 inode->i_data.backing_dev_info = &default_backing_dev_info;
549 spin_lock(&bdev_lock);
550 list_add(&bdev->bd_list, &all_bdevs);
551 spin_unlock(&bdev_lock);
552 unlock_new_inode(inode);
553 }
554 return bdev;
555}
556
557EXPORT_SYMBOL(bdget);
558
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200559/**
560 * bdgrab -- Grab a reference to an already referenced block device
561 * @bdev: Block device to grab a reference to.
562 */
563struct block_device *bdgrab(struct block_device *bdev)
564{
Al Viro7de9c6e2010-10-23 11:11:40 -0400565 ihold(bdev->bd_inode);
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200566 return bdev;
567}
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569long nr_blockdev_pages(void)
570{
Matthias Kaehlcke203a2932007-07-15 23:40:23 -0700571 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 long ret = 0;
573 spin_lock(&bdev_lock);
Matthias Kaehlcke203a2932007-07-15 23:40:23 -0700574 list_for_each_entry(bdev, &all_bdevs, bd_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 ret += bdev->bd_inode->i_mapping->nrpages;
576 }
577 spin_unlock(&bdev_lock);
578 return ret;
579}
580
581void bdput(struct block_device *bdev)
582{
583 iput(bdev->bd_inode);
584}
585
586EXPORT_SYMBOL(bdput);
587
588static struct block_device *bd_acquire(struct inode *inode)
589{
590 struct block_device *bdev;
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 spin_lock(&bdev_lock);
593 bdev = inode->i_bdev;
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700594 if (bdev) {
Al Viro7de9c6e2010-10-23 11:11:40 -0400595 ihold(bdev->bd_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 spin_unlock(&bdev_lock);
597 return bdev;
598 }
599 spin_unlock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 bdev = bdget(inode->i_rdev);
602 if (bdev) {
603 spin_lock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700604 if (!inode->i_bdev) {
605 /*
Al Viro7de9c6e2010-10-23 11:11:40 -0400606 * We take an additional reference to bd_inode,
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700607 * and it's released in clear_inode() of inode.
608 * So, we can access it via ->i_mapping always
609 * without igrab().
610 */
Al Viro7de9c6e2010-10-23 11:11:40 -0400611 ihold(bdev->bd_inode);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700612 inode->i_bdev = bdev;
613 inode->i_mapping = bdev->bd_inode->i_mapping;
614 list_add(&inode->i_devices, &bdev->bd_inodes);
615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 spin_unlock(&bdev_lock);
617 }
618 return bdev;
619}
620
621/* Call when you free inode */
622
623void bd_forget(struct inode *inode)
624{
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700625 struct block_device *bdev = NULL;
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 spin_lock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700628 if (inode->i_bdev) {
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800629 if (!sb_is_blkdev_sb(inode->i_sb))
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700630 bdev = inode->i_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 __bd_forget(inode);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 spin_unlock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700634
635 if (bdev)
636 iput(bdev->bd_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900639/**
640 * bd_may_claim - test whether a block device can be claimed
641 * @bdev: block device of interest
642 * @whole: whole block device containing @bdev, may equal @bdev
643 * @holder: holder trying to claim @bdev
644 *
645 * Test whther @bdev can be claimed by @holder.
646 *
647 * CONTEXT:
648 * spin_lock(&bdev_lock).
649 *
650 * RETURNS:
651 * %true if @bdev can be claimed, %false otherwise.
652 */
653static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
654 void *holder)
655{
656 if (bdev->bd_holder == holder)
657 return true; /* already a holder */
658 else if (bdev->bd_holder != NULL)
659 return false; /* held by someone else */
660 else if (bdev->bd_contains == bdev)
661 return true; /* is a whole device which isn't held */
662
Tejun Heoe525fd82010-11-13 11:55:17 +0100663 else if (whole->bd_holder == bd_may_claim)
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900664 return true; /* is a partition of a device that is being partitioned */
665 else if (whole->bd_holder != NULL)
666 return false; /* is a partition of a held device */
667 else
668 return true; /* is a partition of an un-held device */
669}
670
671/**
Tejun Heo6b4517a2010-04-07 18:53:59 +0900672 * bd_prepare_to_claim - prepare to claim a block device
673 * @bdev: block device of interest
674 * @whole: the whole device containing @bdev, may equal @bdev
675 * @holder: holder trying to claim @bdev
676 *
677 * Prepare to claim @bdev. This function fails if @bdev is already
678 * claimed by another holder and waits if another claiming is in
679 * progress. This function doesn't actually claim. On successful
680 * return, the caller has ownership of bd_claiming and bd_holder[s].
681 *
682 * CONTEXT:
683 * spin_lock(&bdev_lock). Might release bdev_lock, sleep and regrab
684 * it multiple times.
685 *
686 * RETURNS:
687 * 0 if @bdev can be claimed, -EBUSY otherwise.
688 */
689static int bd_prepare_to_claim(struct block_device *bdev,
690 struct block_device *whole, void *holder)
691{
692retry:
693 /* if someone else claimed, fail */
694 if (!bd_may_claim(bdev, whole, holder))
695 return -EBUSY;
696
Tejun Heoe75aa852010-08-04 17:59:39 +0200697 /* if claiming is already in progress, wait for it to finish */
698 if (whole->bd_claiming) {
Tejun Heo6b4517a2010-04-07 18:53:59 +0900699 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
700 DEFINE_WAIT(wait);
701
702 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
703 spin_unlock(&bdev_lock);
704 schedule();
705 finish_wait(wq, &wait);
706 spin_lock(&bdev_lock);
707 goto retry;
708 }
709
710 /* yay, all mine */
711 return 0;
712}
713
714/**
715 * bd_start_claiming - start claiming a block device
716 * @bdev: block device of interest
717 * @holder: holder trying to claim @bdev
718 *
719 * @bdev is about to be opened exclusively. Check @bdev can be opened
720 * exclusively and mark that an exclusive open is in progress. Each
721 * successful call to this function must be matched with a call to
Nick Pigginb0018362010-05-26 01:51:19 +1000722 * either bd_finish_claiming() or bd_abort_claiming() (which do not
723 * fail).
724 *
725 * This function is used to gain exclusive access to the block device
726 * without actually causing other exclusive open attempts to fail. It
727 * should be used when the open sequence itself requires exclusive
728 * access but may subsequently fail.
Tejun Heo6b4517a2010-04-07 18:53:59 +0900729 *
730 * CONTEXT:
731 * Might sleep.
732 *
733 * RETURNS:
734 * Pointer to the block device containing @bdev on success, ERR_PTR()
735 * value on failure.
736 */
737static struct block_device *bd_start_claiming(struct block_device *bdev,
738 void *holder)
739{
740 struct gendisk *disk;
741 struct block_device *whole;
742 int partno, err;
743
744 might_sleep();
745
746 /*
747 * @bdev might not have been initialized properly yet, look up
748 * and grab the outer block device the hard way.
749 */
750 disk = get_gendisk(bdev->bd_dev, &partno);
751 if (!disk)
752 return ERR_PTR(-ENXIO);
753
754 whole = bdget_disk(disk, 0);
Nick Piggincf342572010-05-26 01:50:21 +1000755 module_put(disk->fops->owner);
Tejun Heo6b4517a2010-04-07 18:53:59 +0900756 put_disk(disk);
757 if (!whole)
758 return ERR_PTR(-ENOMEM);
759
760 /* prepare to claim, if successful, mark claiming in progress */
761 spin_lock(&bdev_lock);
762
763 err = bd_prepare_to_claim(bdev, whole, holder);
764 if (err == 0) {
765 whole->bd_claiming = holder;
766 spin_unlock(&bdev_lock);
767 return whole;
768 } else {
769 spin_unlock(&bdev_lock);
770 bdput(whole);
771 return ERR_PTR(err);
772 }
773}
774
775/* releases bdev_lock */
776static void __bd_abort_claiming(struct block_device *whole, void *holder)
777{
778 BUG_ON(whole->bd_claiming != holder);
779 whole->bd_claiming = NULL;
780 wake_up_bit(&whole->bd_claiming, 0);
781
782 spin_unlock(&bdev_lock);
783 bdput(whole);
784}
785
786/**
787 * bd_abort_claiming - abort claiming a block device
788 * @whole: whole block device returned by bd_start_claiming()
789 * @holder: holder trying to claim @bdev
790 *
791 * Abort a claiming block started by bd_start_claiming(). Note that
792 * @whole is not the block device to be claimed but the whole device
793 * returned by bd_start_claiming().
794 *
795 * CONTEXT:
796 * Grabs and releases bdev_lock.
797 */
798static void bd_abort_claiming(struct block_device *whole, void *holder)
799{
800 spin_lock(&bdev_lock);
801 __bd_abort_claiming(whole, holder); /* releases bdev_lock */
802}
803
Nick Pigginb0018362010-05-26 01:51:19 +1000804/* increment holders when we have a legitimate claim. requires bdev_lock */
805static void __bd_claim(struct block_device *bdev, struct block_device *whole,
806 void *holder)
807{
808 /* note that for a whole device bd_holders
809 * will be incremented twice, and bd_holder will
Tejun Heoe525fd82010-11-13 11:55:17 +0100810 * be set to bd_may_claim before being set to holder
Nick Pigginb0018362010-05-26 01:51:19 +1000811 */
812 whole->bd_holders++;
Tejun Heoe525fd82010-11-13 11:55:17 +0100813 whole->bd_holder = bd_may_claim;
Nick Pigginb0018362010-05-26 01:51:19 +1000814 bdev->bd_holders++;
815 bdev->bd_holder = holder;
816}
817
818/**
819 * bd_finish_claiming - finish claiming a block device
820 * @bdev: block device of interest (passed to bd_start_claiming())
821 * @whole: whole block device returned by bd_start_claiming()
822 * @holder: holder trying to claim @bdev
823 *
824 * Finish a claiming block started by bd_start_claiming().
825 *
826 * CONTEXT:
827 * Grabs and releases bdev_lock.
828 */
829static void bd_finish_claiming(struct block_device *bdev,
830 struct block_device *whole, void *holder)
831{
832 spin_lock(&bdev_lock);
Nick Pigginb0018362010-05-26 01:51:19 +1000833 BUG_ON(!bd_may_claim(bdev, whole, holder));
834 __bd_claim(bdev, whole, holder);
835 __bd_abort_claiming(whole, holder); /* not actually an abort */
836}
837
Tejun Heoe525fd82010-11-13 11:55:17 +0100838static void bd_release(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
840 spin_lock(&bdev_lock);
841 if (!--bdev->bd_contains->bd_holders)
842 bdev->bd_contains->bd_holder = NULL;
843 if (!--bdev->bd_holders)
844 bdev->bd_holder = NULL;
845 spin_unlock(&bdev_lock);
846}
847
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800848#ifdef CONFIG_SYSFS
Andrew Morton4d7dd8f2006-09-29 01:58:56 -0700849static int add_symlink(struct kobject *from, struct kobject *to)
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800850{
Andrew Morton4d7dd8f2006-09-29 01:58:56 -0700851 return sysfs_create_link(from, to, kobject_name(to));
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800852}
853
854static void del_symlink(struct kobject *from, struct kobject *to)
855{
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800856 sysfs_remove_link(from, kobject_name(to));
857}
858
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800859/**
Tejun Heoe09b4572010-11-13 11:55:17 +0100860 * bd_link_disk_holder - create symlinks between holding disk and slave bdev
861 * @bdev: the claimed slave bdev
862 * @disk: the holding disk
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500863 *
Tejun Heoe09b4572010-11-13 11:55:17 +0100864 * This functions creates the following sysfs symlinks.
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500865 *
Tejun Heoe09b4572010-11-13 11:55:17 +0100866 * - from "slaves" directory of the holder @disk to the claimed @bdev
867 * - from "holders" directory of the @bdev to the holder @disk
868 *
869 * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
870 * passed to bd_link_disk_holder(), then:
871 *
872 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
873 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
874 *
875 * The caller must have claimed @bdev before calling this function and
876 * ensure that both @bdev and @disk are valid during the creation and
877 * lifetime of these symlinks.
878 *
879 * CONTEXT:
880 * Might sleep.
881 *
882 * RETURNS:
883 * 0 on success, -errno on failure.
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500884 */
Tejun Heoe09b4572010-11-13 11:55:17 +0100885int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500886{
Tejun Heoe09b4572010-11-13 11:55:17 +0100887 int ret = 0;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800888
Peter Zijlstra2e7b6512006-12-08 02:36:13 -0800889 mutex_lock(&bdev->bd_mutex);
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500890
Tejun Heoe09b4572010-11-13 11:55:17 +0100891 WARN_ON_ONCE(!bdev->bd_holder || bdev->bd_holder_disk);
Johannes Weiner4e916722007-07-15 23:41:25 -0700892
Tejun Heoe09b4572010-11-13 11:55:17 +0100893 /* FIXME: remove the following once add_disk() handles errors */
894 if (WARN_ON(!disk->slave_dir || !bdev->bd_part->holder_dir))
895 goto out_unlock;
Johannes Weiner4e916722007-07-15 23:41:25 -0700896
Tejun Heoe09b4572010-11-13 11:55:17 +0100897 ret = add_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
898 if (ret)
899 goto out_unlock;
900
901 ret = add_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj);
902 if (ret) {
903 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
904 goto out_unlock;
905 }
906
907 bdev->bd_holder_disk = disk;
908out_unlock:
Jun'ichi Nomurab4cf1b72006-03-27 01:18:00 -0800909 mutex_unlock(&bdev->bd_mutex);
Tejun Heoe09b4572010-11-13 11:55:17 +0100910 return ret;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800911}
Tejun Heoe09b4572010-11-13 11:55:17 +0100912EXPORT_SYMBOL_GPL(bd_link_disk_holder);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800913
Tejun Heoe525fd82010-11-13 11:55:17 +0100914static void bd_unlink_disk_holder(struct block_device *bdev)
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800915{
Tejun Heoe09b4572010-11-13 11:55:17 +0100916 struct gendisk *disk = bdev->bd_holder_disk;
917
918 bdev->bd_holder_disk = NULL;
919 if (!disk)
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800920 return;
921
Tejun Heoe09b4572010-11-13 11:55:17 +0100922 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
923 del_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800924}
Tejun Heoe525fd82010-11-13 11:55:17 +0100925#else
926static inline void bd_unlink_disk_holder(struct block_device *bdev)
927{ }
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800928#endif
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930/*
931 * Tries to open block device by device number. Use it ONLY if you
932 * really do not have anything better - i.e. when you are behind a
933 * truly sucky interface and all you are given is a device number. _Never_
934 * to be used for internal purposes. If you ever need it - reconsider
935 * your API.
936 */
Tejun Heoe525fd82010-11-13 11:55:17 +0100937struct block_device *open_by_devnum(dev_t dev, fmode_t mode, void *holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
939 struct block_device *bdev = bdget(dev);
940 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +0100942 err = blkdev_get(bdev, mode, holder);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return err ? ERR_PTR(err) : bdev;
944}
945
946EXPORT_SYMBOL(open_by_devnum);
947
Andrew Patterson0c002c22008-09-04 14:27:20 -0600948/**
Andrew Patterson56ade442008-09-04 14:27:40 -0600949 * flush_disk - invalidates all buffer-cache entries on a disk
950 *
951 * @bdev: struct block device to be flushed
952 *
953 * Invalidates all buffer-cache entries on a disk. It should be called
954 * when a disk has been changed -- either by a media change or online
955 * resize.
956 */
957static void flush_disk(struct block_device *bdev)
958{
959 if (__invalidate_device(bdev)) {
960 char name[BDEVNAME_SIZE] = "";
961
962 if (bdev->bd_disk)
963 disk_name(bdev->bd_disk, 0, name);
964 printk(KERN_WARNING "VFS: busy inodes on changed media or "
965 "resized disk %s\n", name);
966 }
967
968 if (!bdev->bd_disk)
969 return;
970 if (disk_partitionable(bdev->bd_disk))
971 bdev->bd_invalidated = 1;
972}
973
974/**
Randy Dunlap57d1b532008-10-09 10:42:38 +0200975 * check_disk_size_change - checks for disk size change and adjusts bdev size.
Andrew Pattersonc3279d12008-09-04 14:27:25 -0600976 * @disk: struct gendisk to check
977 * @bdev: struct bdev to adjust.
978 *
979 * This routine checks to see if the bdev size does not match the disk size
980 * and adjusts it if it differs.
981 */
982void check_disk_size_change(struct gendisk *disk, struct block_device *bdev)
983{
984 loff_t disk_size, bdev_size;
985
986 disk_size = (loff_t)get_capacity(disk) << 9;
987 bdev_size = i_size_read(bdev->bd_inode);
988 if (disk_size != bdev_size) {
989 char name[BDEVNAME_SIZE];
990
991 disk_name(disk, 0, name);
992 printk(KERN_INFO
993 "%s: detected capacity change from %lld to %lld\n",
994 name, bdev_size, disk_size);
995 i_size_write(bdev->bd_inode, disk_size);
Andrew Patterson608aeef2008-09-04 14:27:45 -0600996 flush_disk(bdev);
Andrew Pattersonc3279d12008-09-04 14:27:25 -0600997 }
998}
999EXPORT_SYMBOL(check_disk_size_change);
1000
1001/**
Randy Dunlap57d1b532008-10-09 10:42:38 +02001002 * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back
Andrew Patterson0c002c22008-09-04 14:27:20 -06001003 * @disk: struct gendisk to be revalidated
1004 *
1005 * This routine is a wrapper for lower-level driver's revalidate_disk
1006 * call-backs. It is used to do common pre and post operations needed
1007 * for all revalidate_disk operations.
1008 */
1009int revalidate_disk(struct gendisk *disk)
1010{
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001011 struct block_device *bdev;
Andrew Patterson0c002c22008-09-04 14:27:20 -06001012 int ret = 0;
1013
1014 if (disk->fops->revalidate_disk)
1015 ret = disk->fops->revalidate_disk(disk);
1016
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001017 bdev = bdget_disk(disk, 0);
1018 if (!bdev)
1019 return ret;
1020
1021 mutex_lock(&bdev->bd_mutex);
1022 check_disk_size_change(disk, bdev);
1023 mutex_unlock(&bdev->bd_mutex);
1024 bdput(bdev);
Andrew Patterson0c002c22008-09-04 14:27:20 -06001025 return ret;
1026}
1027EXPORT_SYMBOL(revalidate_disk);
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029/*
1030 * This routine checks whether a removable media has been changed,
1031 * and invalidates all buffer-cache-entries in that case. This
1032 * is a relatively slow routine, so we have to try to minimize using
1033 * it. Thus it is called only upon a 'mount' or 'open'. This
1034 * is the best way of combining speed and utility, I think.
1035 * People changing diskettes in the middle of an operation deserve
1036 * to lose :-)
1037 */
1038int check_disk_change(struct block_device *bdev)
1039{
1040 struct gendisk *disk = bdev->bd_disk;
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001041 const struct block_device_operations *bdops = disk->fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 if (!bdops->media_changed)
1044 return 0;
1045 if (!bdops->media_changed(bdev->bd_disk))
1046 return 0;
1047
Andrew Patterson56ade442008-09-04 14:27:40 -06001048 flush_disk(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (bdops->revalidate_disk)
1050 bdops->revalidate_disk(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 return 1;
1052}
1053
1054EXPORT_SYMBOL(check_disk_change);
1055
1056void bd_set_size(struct block_device *bdev, loff_t size)
1057{
Martin K. Petersene1defc42009-05-22 17:17:49 -04001058 unsigned bsize = bdev_logical_block_size(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 bdev->bd_inode->i_size = size;
1061 while (bsize < PAGE_CACHE_SIZE) {
1062 if (size & bsize)
1063 break;
1064 bsize <<= 1;
1065 }
1066 bdev->bd_block_size = bsize;
1067 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
1068}
1069EXPORT_SYMBOL(bd_set_size);
1070
Al Viro9a1c3542008-02-22 20:40:24 -05001071static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
NeilBrown37be4122006-12-08 02:36:16 -08001072
Peter Zijlstra6d740cd2007-02-20 13:58:18 -08001073/*
1074 * bd_mutex locking:
1075 *
1076 * mutex_lock(part->bd_mutex)
1077 * mutex_lock_nested(whole->bd_mutex, 1)
1078 */
1079
Al Viro572c4892007-10-08 13:24:05 -04001080static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 struct gendisk *disk;
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -07001083 int ret;
Tejun Heocf771cb2008-09-03 09:01:09 +02001084 int partno;
Al Virofe6e9c12008-06-23 08:30:55 -04001085 int perm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Al Viro572c4892007-10-08 13:24:05 -04001087 if (mode & FMODE_READ)
Al Virofe6e9c12008-06-23 08:30:55 -04001088 perm |= MAY_READ;
Al Viro572c4892007-10-08 13:24:05 -04001089 if (mode & FMODE_WRITE)
Al Virofe6e9c12008-06-23 08:30:55 -04001090 perm |= MAY_WRITE;
1091 /*
1092 * hooks: /n/, see "layering violations".
1093 */
Chris Wrightb7300b72010-08-10 18:02:55 -07001094 if (!for_part) {
1095 ret = devcgroup_inode_permission(bdev->bd_inode, perm);
1096 if (ret != 0) {
1097 bdput(bdev);
1098 return ret;
1099 }
Al Viro82666022008-08-01 05:32:04 -04001100 }
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -07001101
NeilBrownd3374822009-01-09 08:31:10 +11001102 restart:
Tejun Heo0762b8b2008-08-25 19:56:12 +09001103
Tejun Heo89f97492008-11-05 10:21:06 +01001104 ret = -ENXIO;
Tejun Heocf771cb2008-09-03 09:01:09 +02001105 disk = get_gendisk(bdev->bd_dev, &partno);
Tejun Heo0762b8b2008-08-25 19:56:12 +09001106 if (!disk)
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001107 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
NeilBrown6796bf52006-12-08 02:36:16 -08001109 mutex_lock_nested(&bdev->bd_mutex, for_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (!bdev->bd_openers) {
1111 bdev->bd_disk = disk;
1112 bdev->bd_contains = bdev;
Tejun Heocf771cb2008-09-03 09:01:09 +02001113 if (!partno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 struct backing_dev_info *bdi;
Tejun Heo89f97492008-11-05 10:21:06 +01001115
1116 ret = -ENXIO;
1117 bdev->bd_part = disk_get_part(disk, partno);
1118 if (!bdev->bd_part)
1119 goto out_clear;
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (disk->fops->open) {
Al Viro572c4892007-10-08 13:24:05 -04001122 ret = disk->fops->open(bdev, mode);
NeilBrownd3374822009-01-09 08:31:10 +11001123 if (ret == -ERESTARTSYS) {
1124 /* Lost a race with 'disk' being
1125 * deleted, try again.
1126 * See md.c
1127 */
1128 disk_put_part(bdev->bd_part);
1129 bdev->bd_part = NULL;
1130 module_put(disk->fops->owner);
1131 put_disk(disk);
1132 bdev->bd_disk = NULL;
1133 mutex_unlock(&bdev->bd_mutex);
1134 goto restart;
1135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 if (ret)
Tejun Heo0762b8b2008-08-25 19:56:12 +09001137 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
1139 if (!bdev->bd_openers) {
1140 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
1141 bdi = blk_get_backing_dev_info(bdev);
1142 if (bdi == NULL)
1143 bdi = &default_backing_dev_info;
Dave Chinnera5491e02010-10-21 11:49:26 +11001144 bdev_inode_switch_bdi(bdev->bd_inode, bdi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
1146 if (bdev->bd_invalidated)
1147 rescan_partitions(disk, bdev);
1148 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 struct block_device *whole;
1150 whole = bdget_disk(disk, 0);
1151 ret = -ENOMEM;
1152 if (!whole)
Tejun Heo0762b8b2008-08-25 19:56:12 +09001153 goto out_clear;
NeilBrown37be4122006-12-08 02:36:16 -08001154 BUG_ON(for_part);
Al Viro572c4892007-10-08 13:24:05 -04001155 ret = __blkdev_get(whole, mode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (ret)
Tejun Heo0762b8b2008-08-25 19:56:12 +09001157 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 bdev->bd_contains = whole;
Dave Chinnera5491e02010-10-21 11:49:26 +11001159 bdev_inode_switch_bdi(bdev->bd_inode,
1160 whole->bd_inode->i_data.backing_dev_info);
Tejun Heo89f97492008-11-05 10:21:06 +01001161 bdev->bd_part = disk_get_part(disk, partno);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001162 if (!(disk->flags & GENHD_FL_UP) ||
Tejun Heo89f97492008-11-05 10:21:06 +01001163 !bdev->bd_part || !bdev->bd_part->nr_sects) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 ret = -ENXIO;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001165 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
Tejun Heo89f97492008-11-05 10:21:06 +01001167 bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 }
1169 } else {
Tejun Heo0762b8b2008-08-25 19:56:12 +09001170 module_put(disk->fops->owner);
Neil Brown960cc0f2009-10-26 08:59:17 +01001171 put_disk(disk);
Tejun Heo0762b8b2008-08-25 19:56:12 +09001172 disk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (bdev->bd_contains == bdev) {
1174 if (bdev->bd_disk->fops->open) {
Al Viro572c4892007-10-08 13:24:05 -04001175 ret = bdev->bd_disk->fops->open(bdev, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (ret)
Tejun Heo0762b8b2008-08-25 19:56:12 +09001177 goto out_unlock_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
1179 if (bdev->bd_invalidated)
1180 rescan_partitions(bdev->bd_disk, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
1182 }
1183 bdev->bd_openers++;
NeilBrown37be4122006-12-08 02:36:16 -08001184 if (for_part)
1185 bdev->bd_part_count++;
Arjan van de Venc039e312006-03-23 03:00:28 -08001186 mutex_unlock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return 0;
1188
Tejun Heo0762b8b2008-08-25 19:56:12 +09001189 out_clear:
Tejun Heo89f97492008-11-05 10:21:06 +01001190 disk_put_part(bdev->bd_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 bdev->bd_disk = NULL;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001192 bdev->bd_part = NULL;
Dave Chinnera5491e02010-10-21 11:49:26 +11001193 bdev_inode_switch_bdi(bdev->bd_inode, &default_backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if (bdev != bdev->bd_contains)
Al Viro572c4892007-10-08 13:24:05 -04001195 __blkdev_put(bdev->bd_contains, mode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 bdev->bd_contains = NULL;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001197 out_unlock_bdev:
Arjan van de Venc039e312006-03-23 03:00:28 -08001198 mutex_unlock(&bdev->bd_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001199 out:
Tejun Heo0762b8b2008-08-25 19:56:12 +09001200 if (disk)
1201 module_put(disk->fops->owner);
1202 put_disk(disk);
1203 bdput(bdev);
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return ret;
1206}
1207
Tejun Heoe525fd82010-11-13 11:55:17 +01001208int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
Tejun Heoe525fd82010-11-13 11:55:17 +01001210 struct block_device *whole = NULL;
1211 int res;
1212
1213 WARN_ON_ONCE((mode & FMODE_EXCL) && !holder);
1214
1215 if ((mode & FMODE_EXCL) && holder) {
1216 whole = bd_start_claiming(bdev, holder);
1217 if (IS_ERR(whole)) {
1218 bdput(bdev);
1219 return PTR_ERR(whole);
1220 }
1221 }
1222
1223 res = __blkdev_get(bdev, mode, 0);
1224
1225 if (whole) {
1226 if (res == 0)
1227 bd_finish_claiming(bdev, whole, holder);
1228 else
1229 bd_abort_claiming(whole, holder);
1230 }
1231
1232 return res;
NeilBrown37be4122006-12-08 02:36:16 -08001233}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234EXPORT_SYMBOL(blkdev_get);
1235
1236static int blkdev_open(struct inode * inode, struct file * filp)
1237{
1238 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 /*
1241 * Preserve backwards compatibility and allow large file access
1242 * even if userspace doesn't ask for it explicitly. Some mkfs
1243 * binary needs it. We might want to drop this workaround
1244 * during an unstable branch.
1245 */
1246 filp->f_flags |= O_LARGEFILE;
1247
Al Viro572c4892007-10-08 13:24:05 -04001248 if (filp->f_flags & O_NDELAY)
1249 filp->f_mode |= FMODE_NDELAY;
1250 if (filp->f_flags & O_EXCL)
1251 filp->f_mode |= FMODE_EXCL;
1252 if ((filp->f_flags & O_ACCMODE) == 3)
1253 filp->f_mode |= FMODE_WRITE_IOCTL;
1254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 bdev = bd_acquire(inode);
Pavel Emelianov6a2aae02006-10-28 10:38:33 -07001256 if (bdev == NULL)
1257 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Al Viro572c4892007-10-08 13:24:05 -04001259 filp->f_mapping = bdev->bd_inode->i_mapping;
1260
Tejun Heoe525fd82010-11-13 11:55:17 +01001261 return blkdev_get(bdev, filp->f_mode, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262}
1263
Al Viro9a1c3542008-02-22 20:40:24 -05001264static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001265{
1266 int ret = 0;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001267 struct gendisk *disk = bdev->bd_disk;
NeilBrown37be4122006-12-08 02:36:16 -08001268 struct block_device *victim = NULL;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001269
NeilBrown6796bf52006-12-08 02:36:16 -08001270 mutex_lock_nested(&bdev->bd_mutex, for_part);
NeilBrown37be4122006-12-08 02:36:16 -08001271 if (for_part)
1272 bdev->bd_part_count--;
1273
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001274 if (!--bdev->bd_openers) {
1275 sync_blockdev(bdev);
1276 kill_bdev(bdev);
1277 }
1278 if (bdev->bd_contains == bdev) {
1279 if (disk->fops->release)
Al Viro9a1c3542008-02-22 20:40:24 -05001280 ret = disk->fops->release(disk, mode);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001281 }
1282 if (!bdev->bd_openers) {
1283 struct module *owner = disk->fops->owner;
1284
1285 put_disk(disk);
1286 module_put(owner);
Tejun Heo0762b8b2008-08-25 19:56:12 +09001287 disk_put_part(bdev->bd_part);
1288 bdev->bd_part = NULL;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001289 bdev->bd_disk = NULL;
Dave Chinnera5491e02010-10-21 11:49:26 +11001290 bdev_inode_switch_bdi(bdev->bd_inode,
1291 &default_backing_dev_info);
NeilBrown37be4122006-12-08 02:36:16 -08001292 if (bdev != bdev->bd_contains)
1293 victim = bdev->bd_contains;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001294 bdev->bd_contains = NULL;
1295 }
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001296 mutex_unlock(&bdev->bd_mutex);
1297 bdput(bdev);
NeilBrown37be4122006-12-08 02:36:16 -08001298 if (victim)
Al Viro9a1c3542008-02-22 20:40:24 -05001299 __blkdev_put(victim, mode, 1);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001300 return ret;
1301}
1302
Al Viro9a1c3542008-02-22 20:40:24 -05001303int blkdev_put(struct block_device *bdev, fmode_t mode)
NeilBrown37be4122006-12-08 02:36:16 -08001304{
Tejun Heoe525fd82010-11-13 11:55:17 +01001305 if (mode & FMODE_EXCL) {
1306 mutex_lock(&bdev->bd_mutex);
1307 bd_release(bdev);
1308 if (!bdev->bd_holders)
1309 bd_unlink_disk_holder(bdev);
1310 mutex_unlock(&bdev->bd_mutex);
1311 }
Al Viro9a1c3542008-02-22 20:40:24 -05001312 return __blkdev_put(bdev, mode, 0);
NeilBrown37be4122006-12-08 02:36:16 -08001313}
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001314EXPORT_SYMBOL(blkdev_put);
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316static int blkdev_close(struct inode * inode, struct file * filp)
1317{
1318 struct block_device *bdev = I_BDEV(filp->f_mapping->host);
Tejun Heoe525fd82010-11-13 11:55:17 +01001319
Al Viro9a1c3542008-02-22 20:40:24 -05001320 return blkdev_put(bdev, filp->f_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321}
1322
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07001323static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Al Viro56b26ad2008-09-19 03:17:36 -04001325 struct block_device *bdev = I_BDEV(file->f_mapping->host);
1326 fmode_t mode = file->f_mode;
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +01001327
1328 /*
1329 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
1330 * to updated it before every ioctl.
1331 */
Al Viro56b26ad2008-09-19 03:17:36 -04001332 if (file->f_flags & O_NDELAY)
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +01001333 mode |= FMODE_NDELAY;
1334 else
1335 mode &= ~FMODE_NDELAY;
1336
Al Viro56b26ad2008-09-19 03:17:36 -04001337 return blkdev_ioctl(bdev, mode, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
1339
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05001340/*
Christoph Hellwigeef99382009-08-20 17:43:41 +02001341 * Write data to the block device. Only intended for the block device itself
1342 * and the raw driver which basically is a fake block device.
1343 *
1344 * Does not take i_mutex for the write and thus is not for general purpose
1345 * use.
1346 */
1347ssize_t blkdev_aio_write(struct kiocb *iocb, const struct iovec *iov,
1348 unsigned long nr_segs, loff_t pos)
1349{
1350 struct file *file = iocb->ki_filp;
1351 ssize_t ret;
1352
1353 BUG_ON(iocb->ki_pos != pos);
1354
1355 ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos);
1356 if (ret > 0 || ret == -EIOCBQUEUED) {
1357 ssize_t err;
1358
1359 err = generic_write_sync(file, pos, ret);
1360 if (err < 0 && ret > 0)
1361 ret = err;
1362 }
1363 return ret;
1364}
1365EXPORT_SYMBOL_GPL(blkdev_aio_write);
1366
1367/*
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05001368 * Try to release a page associated with block device when the system
1369 * is under memory pressure.
1370 */
1371static int blkdev_releasepage(struct page *page, gfp_t wait)
1372{
1373 struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
1374
1375 if (super && super->s_op->bdev_try_to_free_page)
1376 return super->s_op->bdev_try_to_free_page(super, page, wait);
1377
1378 return try_to_free_buffers(page);
1379}
1380
Adrian Bunk4c54ac62008-02-18 13:48:31 +01001381static const struct address_space_operations def_blk_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 .readpage = blkdev_readpage,
1383 .writepage = blkdev_writepage,
1384 .sync_page = block_sync_page,
Nick Piggin6272b5a2007-10-16 01:25:04 -07001385 .write_begin = blkdev_write_begin,
1386 .write_end = blkdev_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 .writepages = generic_writepages,
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05001388 .releasepage = blkdev_releasepage,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 .direct_IO = blkdev_direct_IO,
1390};
1391
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001392const struct file_operations def_blk_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 .open = blkdev_open,
1394 .release = blkdev_close,
1395 .llseek = block_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -07001396 .read = do_sync_read,
1397 .write = do_sync_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 .aio_read = generic_file_aio_read,
Christoph Hellwigeef99382009-08-20 17:43:41 +02001399 .aio_write = blkdev_aio_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 .mmap = generic_file_mmap,
Andrew Mortonb1dd3b22010-04-06 14:35:00 -07001401 .fsync = blkdev_fsync,
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07001402 .unlocked_ioctl = block_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403#ifdef CONFIG_COMPAT
1404 .compat_ioctl = compat_blkdev_ioctl,
1405#endif
Jens Axboe7f9c51f2006-05-01 19:59:32 +02001406 .splice_read = generic_file_splice_read,
1407 .splice_write = generic_file_splice_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408};
1409
1410int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
1411{
1412 int res;
1413 mm_segment_t old_fs = get_fs();
1414 set_fs(KERNEL_DS);
Al Viro56b26ad2008-09-19 03:17:36 -04001415 res = blkdev_ioctl(bdev, 0, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 set_fs(old_fs);
1417 return res;
1418}
1419
1420EXPORT_SYMBOL(ioctl_by_bdev);
1421
1422/**
1423 * lookup_bdev - lookup a struct block_device by name
Randy Dunlap94e29592009-01-06 14:41:15 -08001424 * @pathname: special file representing the block device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 *
Randy Dunlap57d1b532008-10-09 10:42:38 +02001426 * Get a reference to the blockdevice at @pathname in the current
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 * namespace if possible and return it. Return ERR_PTR(error)
1428 * otherwise.
1429 */
Al Viro421748e2008-08-02 01:04:36 -04001430struct block_device *lookup_bdev(const char *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
1432 struct block_device *bdev;
1433 struct inode *inode;
Al Viro421748e2008-08-02 01:04:36 -04001434 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 int error;
1436
Al Viro421748e2008-08-02 01:04:36 -04001437 if (!pathname || !*pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 return ERR_PTR(-EINVAL);
1439
Al Viro421748e2008-08-02 01:04:36 -04001440 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (error)
1442 return ERR_PTR(error);
1443
Al Viro421748e2008-08-02 01:04:36 -04001444 inode = path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 error = -ENOTBLK;
1446 if (!S_ISBLK(inode->i_mode))
1447 goto fail;
1448 error = -EACCES;
Al Viro421748e2008-08-02 01:04:36 -04001449 if (path.mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 goto fail;
1451 error = -ENOMEM;
1452 bdev = bd_acquire(inode);
1453 if (!bdev)
1454 goto fail;
1455out:
Al Viro421748e2008-08-02 01:04:36 -04001456 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 return bdev;
1458fail:
1459 bdev = ERR_PTR(error);
1460 goto out;
1461}
Al Virod5686b42008-08-01 05:00:11 -04001462EXPORT_SYMBOL(lookup_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
1464/**
Al Viro30c40d22008-02-22 19:50:45 -05001465 * open_bdev_exclusive - open a block device by name and set it up for use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 *
1467 * @path: special file representing the block device
Al Viro30c40d22008-02-22 19:50:45 -05001468 * @mode: FMODE_... combination to pass be used
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 * @holder: owner for exclusion
1470 *
1471 * Open the blockdevice described by the special file at @path, claim it
1472 * for the @holder.
1473 */
Al Viro30c40d22008-02-22 19:50:45 -05001474struct block_device *open_bdev_exclusive(const char *path, fmode_t mode, void *holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
Tejun Heoe525fd82010-11-13 11:55:17 +01001476 struct block_device *bdev;
Tejun Heo6b4517a2010-04-07 18:53:59 +09001477 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479 bdev = lookup_bdev(path);
1480 if (IS_ERR(bdev))
1481 return bdev;
1482
Tejun Heoe525fd82010-11-13 11:55:17 +01001483 error = blkdev_get(bdev, mode | FMODE_EXCL, holder);
1484 if (error)
1485 return ERR_PTR(error);
1486
1487 if ((mode & FMODE_WRITE) && bdev_read_only(bdev)) {
1488 blkdev_put(bdev, mode);
1489 return ERR_PTR(-EACCES);
Tejun Heo6b4517a2010-04-07 18:53:59 +09001490 }
1491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 return bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
Al Viro30c40d22008-02-22 19:50:45 -05001495EXPORT_SYMBOL(open_bdev_exclusive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
David Howellsb71e8a42006-08-29 19:06:11 +01001497int __invalidate_device(struct block_device *bdev)
1498{
1499 struct super_block *sb = get_super(bdev);
1500 int res = 0;
1501
1502 if (sb) {
1503 /*
1504 * no need to lock the super, get_super holds the
1505 * read mutex so the filesystem cannot go away
1506 * under us (->put_super runs with the write lock
1507 * hold).
1508 */
1509 shrink_dcache_sb(sb);
1510 res = invalidate_inodes(sb);
1511 drop_super(sb);
1512 }
Peter Zijlstraf98393a2007-05-06 14:49:54 -07001513 invalidate_bdev(bdev);
David Howellsb71e8a42006-08-29 19:06:11 +01001514 return res;
1515}
1516EXPORT_SYMBOL(__invalidate_device);