blob: 5147bdd3b8e1cc0c86ae9ac4f32d1bcaa596d4b7 [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>
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -070014#include <linux/device_cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/highmem.h>
16#include <linux/blkdev.h>
17#include <linux/module.h>
18#include <linux/blkpg.h>
19#include <linux/buffer_head.h>
Nick Piggin585d3bc2009-02-25 10:44:19 +010020#include <linux/pagevec.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>
Catalin Marinas2e1483c2009-06-11 13:24:13 +010027#include <linux/kmemleak.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/uaccess.h>
David Howells07f3f052006-09-30 20:52:18 +020029#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31struct bdev_inode {
32 struct block_device bdev;
33 struct inode vfs_inode;
34};
35
Adrian Bunk4c54ac62008-02-18 13:48:31 +010036static const struct address_space_operations def_blk_aops;
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static inline struct bdev_inode *BDEV_I(struct inode *inode)
39{
40 return container_of(inode, struct bdev_inode, vfs_inode);
41}
42
43inline struct block_device *I_BDEV(struct inode *inode)
44{
45 return &BDEV_I(inode)->bdev;
46}
47
48EXPORT_SYMBOL(I_BDEV);
49
Dave Chinnera5491e02010-10-21 11:49:26 +110050/*
51 * move the inode from it's current bdi to the a new bdi. if the inode is dirty
52 * we need to move it onto the dirty list of @dst so that the inode is always
53 * on the right list.
54 */
55static void bdev_inode_switch_bdi(struct inode *inode,
56 struct backing_dev_info *dst)
57{
Dave Chinnera66979a2011-03-22 22:23:41 +110058 spin_lock(&inode_wb_list_lock);
Dave Chinner250df6e2011-03-22 22:23:36 +110059 spin_lock(&inode->i_lock);
Dave Chinnera5491e02010-10-21 11:49:26 +110060 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 Chinner250df6e2011-03-22 22:23:36 +110063 spin_unlock(&inode->i_lock);
Dave Chinnera66979a2011-03-22 22:23:41 +110064 spin_unlock(&inode_wb_list_lock);
Dave Chinnera5491e02010-10-21 11:49:26 +110065}
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static sector_t max_block(struct block_device *bdev)
68{
69 sector_t retval = ~((sector_t)0);
70 loff_t sz = i_size_read(bdev->bd_inode);
71
72 if (sz) {
73 unsigned int size = block_size(bdev);
74 unsigned int sizebits = blksize_bits(size);
75 retval = (sz >> sizebits);
76 }
77 return retval;
78}
79
Peter Zijlstraf9a14392007-05-06 14:49:55 -070080/* Kill _all_ buffers and pagecache , dirty or not.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static void kill_bdev(struct block_device *bdev)
82{
Peter Zijlstraf9a14392007-05-06 14:49:55 -070083 if (bdev->bd_inode->i_mapping->nrpages == 0)
84 return;
85 invalidate_bh_lrus();
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
87}
88
89int set_blocksize(struct block_device *bdev, int size)
90{
91 /* Size must be a power of two, and between 512 and PAGE_SIZE */
Vignesh Babu BM1368c4f2007-05-08 00:24:32 -070092 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return -EINVAL;
94
95 /* Size cannot be smaller than the size supported by the device */
Martin K. Petersene1defc42009-05-22 17:17:49 -040096 if (size < bdev_logical_block_size(bdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return -EINVAL;
98
99 /* Don't change the size if it is same as current */
100 if (bdev->bd_block_size != size) {
101 sync_blockdev(bdev);
102 bdev->bd_block_size = size;
103 bdev->bd_inode->i_blkbits = blksize_bits(size);
104 kill_bdev(bdev);
105 }
106 return 0;
107}
108
109EXPORT_SYMBOL(set_blocksize);
110
111int sb_set_blocksize(struct super_block *sb, int size)
112{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if (set_blocksize(sb->s_bdev, size))
114 return 0;
115 /* If we get here, we know size is power of two
116 * and it's value is between 512 and PAGE_SIZE */
117 sb->s_blocksize = size;
Coywolf Qi Hunt38885bd2006-03-24 03:18:05 -0800118 sb->s_blocksize_bits = blksize_bits(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return sb->s_blocksize;
120}
121
122EXPORT_SYMBOL(sb_set_blocksize);
123
124int sb_min_blocksize(struct super_block *sb, int size)
125{
Martin K. Petersene1defc42009-05-22 17:17:49 -0400126 int minsize = bdev_logical_block_size(sb->s_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (size < minsize)
128 size = minsize;
129 return sb_set_blocksize(sb, size);
130}
131
132EXPORT_SYMBOL(sb_min_blocksize);
133
134static int
135blkdev_get_block(struct inode *inode, sector_t iblock,
136 struct buffer_head *bh, int create)
137{
138 if (iblock >= max_block(I_BDEV(inode))) {
139 if (create)
140 return -EIO;
141
142 /*
143 * for reads, we're just trying to fill a partial page.
144 * return a hole, they will have to call get_block again
145 * before they can fill it, and they will get -EIO at that
146 * time
147 */
148 return 0;
149 }
150 bh->b_bdev = I_BDEV(inode);
151 bh->b_blocknr = iblock;
152 set_buffer_mapped(bh);
153 return 0;
154}
155
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800156static int
157blkdev_get_blocks(struct inode *inode, sector_t iblock,
158 struct buffer_head *bh, int create)
159{
160 sector_t end_block = max_block(I_BDEV(inode));
161 unsigned long max_blocks = bh->b_size >> inode->i_blkbits;
162
163 if ((iblock + max_blocks) > end_block) {
164 max_blocks = end_block - iblock;
165 if ((long)max_blocks <= 0) {
166 if (create)
167 return -EIO; /* write fully beyond EOF */
168 /*
169 * It is a read which is fully beyond EOF. We return
170 * a !buffer_mapped buffer
171 */
172 max_blocks = 0;
173 }
174 }
175
176 bh->b_bdev = I_BDEV(inode);
177 bh->b_blocknr = iblock;
178 bh->b_size = max_blocks << inode->i_blkbits;
179 if (max_blocks)
180 set_buffer_mapped(bh);
181 return 0;
182}
183
184static ssize_t
185blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
186 loff_t offset, unsigned long nr_segs)
187{
188 struct file *file = iocb->ki_filp;
189 struct inode *inode = file->f_mapping->host;
190
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +0200191 return __blockdev_direct_IO(rw, iocb, inode, I_BDEV(inode), iov, offset,
192 nr_segs, blkdev_get_blocks, NULL, NULL, 0);
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800193}
194
Jan Kara5cee5812009-04-27 16:43:51 +0200195int __sync_blockdev(struct block_device *bdev, int wait)
196{
197 if (!bdev)
198 return 0;
199 if (!wait)
200 return filemap_flush(bdev->bd_inode->i_mapping);
201 return filemap_write_and_wait(bdev->bd_inode->i_mapping);
202}
203
Nick Piggin585d3bc2009-02-25 10:44:19 +0100204/*
205 * Write out and wait upon all the dirty data associated with a block
206 * device via its mapping. Does not take the superblock lock.
207 */
208int sync_blockdev(struct block_device *bdev)
209{
Jan Kara5cee5812009-04-27 16:43:51 +0200210 return __sync_blockdev(bdev, 1);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100211}
212EXPORT_SYMBOL(sync_blockdev);
213
214/*
215 * Write out and wait upon all dirty data associated with this
216 * device. Filesystem data as well as the underlying block
217 * device. Takes the superblock lock.
218 */
219int fsync_bdev(struct block_device *bdev)
220{
221 struct super_block *sb = get_super(bdev);
222 if (sb) {
Jan Kara60b06802009-04-27 16:43:53 +0200223 int res = sync_filesystem(sb);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100224 drop_super(sb);
225 return res;
226 }
227 return sync_blockdev(bdev);
228}
Al Viro47e44912009-04-01 07:07:16 -0400229EXPORT_SYMBOL(fsync_bdev);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100230
231/**
232 * freeze_bdev -- lock a filesystem and force it into a consistent state
233 * @bdev: blockdevice to lock
234 *
Nick Piggin585d3bc2009-02-25 10:44:19 +0100235 * If a superblock is found on this device, we take the s_umount semaphore
236 * on it to make sure nobody unmounts until the snapshot creation is done.
237 * The reference counter (bd_fsfreeze_count) guarantees that only the last
238 * unfreeze process can unfreeze the frozen filesystem actually when multiple
239 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
240 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
241 * actually.
242 */
243struct super_block *freeze_bdev(struct block_device *bdev)
244{
245 struct super_block *sb;
246 int error = 0;
247
248 mutex_lock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig45042302009-08-03 23:28:35 +0200249 if (++bdev->bd_fsfreeze_count > 1) {
250 /*
251 * We don't even need to grab a reference - the first call
252 * to freeze_bdev grab an active reference and only the last
253 * thaw_bdev drops it.
254 */
Nick Piggin585d3bc2009-02-25 10:44:19 +0100255 sb = get_super(bdev);
Christoph Hellwig45042302009-08-03 23:28:35 +0200256 drop_super(sb);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100257 mutex_unlock(&bdev->bd_fsfreeze_mutex);
258 return sb;
259 }
Nick Piggin585d3bc2009-02-25 10:44:19 +0100260
Christoph Hellwig45042302009-08-03 23:28:35 +0200261 sb = get_active_super(bdev);
262 if (!sb)
263 goto out;
Josef Bacik18e9e512010-03-23 10:34:56 -0400264 error = freeze_super(sb);
265 if (error) {
266 deactivate_super(sb);
267 bdev->bd_fsfreeze_count--;
Christoph Hellwig45042302009-08-03 23:28:35 +0200268 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Josef Bacik18e9e512010-03-23 10:34:56 -0400269 return ERR_PTR(error);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100270 }
Josef Bacik18e9e512010-03-23 10:34:56 -0400271 deactivate_super(sb);
Christoph Hellwig45042302009-08-03 23:28:35 +0200272 out:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100273 sync_blockdev(bdev);
274 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig4fadd7b2009-08-03 23:28:06 +0200275 return sb; /* thaw_bdev releases s->s_umount */
Nick Piggin585d3bc2009-02-25 10:44:19 +0100276}
277EXPORT_SYMBOL(freeze_bdev);
278
279/**
280 * thaw_bdev -- unlock filesystem
281 * @bdev: blockdevice to unlock
282 * @sb: associated superblock
283 *
284 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
285 */
286int thaw_bdev(struct block_device *bdev, struct super_block *sb)
287{
Christoph Hellwig45042302009-08-03 23:28:35 +0200288 int error = -EINVAL;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100289
290 mutex_lock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig45042302009-08-03 23:28:35 +0200291 if (!bdev->bd_fsfreeze_count)
Josef Bacik18e9e512010-03-23 10:34:56 -0400292 goto out;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100293
Christoph Hellwig45042302009-08-03 23:28:35 +0200294 error = 0;
295 if (--bdev->bd_fsfreeze_count > 0)
Josef Bacik18e9e512010-03-23 10:34:56 -0400296 goto out;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100297
Christoph Hellwig45042302009-08-03 23:28:35 +0200298 if (!sb)
Josef Bacik18e9e512010-03-23 10:34:56 -0400299 goto out;
Christoph Hellwig45042302009-08-03 23:28:35 +0200300
Josef Bacik18e9e512010-03-23 10:34:56 -0400301 error = thaw_super(sb);
302 if (error) {
303 bdev->bd_fsfreeze_count++;
304 mutex_unlock(&bdev->bd_fsfreeze_mutex);
305 return error;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100306 }
Josef Bacik18e9e512010-03-23 10:34:56 -0400307out:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100308 mutex_unlock(&bdev->bd_fsfreeze_mutex);
309 return 0;
310}
311EXPORT_SYMBOL(thaw_bdev);
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
314{
315 return block_write_full_page(page, blkdev_get_block, wbc);
316}
317
318static int blkdev_readpage(struct file * file, struct page * page)
319{
320 return block_read_full_page(page, blkdev_get_block);
321}
322
Nick Piggin6272b5a2007-10-16 01:25:04 -0700323static int blkdev_write_begin(struct file *file, struct address_space *mapping,
324 loff_t pos, unsigned len, unsigned flags,
325 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Christoph Hellwig155130a2010-06-04 11:29:58 +0200327 return block_write_begin(mapping, pos, len, flags, pagep,
328 blkdev_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Nick Piggin6272b5a2007-10-16 01:25:04 -0700331static int blkdev_write_end(struct file *file, struct address_space *mapping,
332 loff_t pos, unsigned len, unsigned copied,
333 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Nick Piggin6272b5a2007-10-16 01:25:04 -0700335 int ret;
336 ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
337
338 unlock_page(page);
339 page_cache_release(page);
340
341 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
344/*
345 * private llseek:
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800346 * for a block special file file->f_path.dentry->d_inode->i_size is zero
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 * so we compute the size by hand (just as in block_read/write above)
348 */
349static loff_t block_llseek(struct file *file, loff_t offset, int origin)
350{
351 struct inode *bd_inode = file->f_mapping->host;
352 loff_t size;
353 loff_t retval;
354
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800355 mutex_lock(&bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 size = i_size_read(bd_inode);
357
358 switch (origin) {
359 case 2:
360 offset += size;
361 break;
362 case 1:
363 offset += file->f_pos;
364 }
365 retval = -EINVAL;
366 if (offset >= 0 && offset <= size) {
367 if (offset != file->f_pos) {
368 file->f_pos = offset;
369 }
370 retval = offset;
371 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800372 mutex_unlock(&bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return retval;
374}
375
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200376int blkdev_fsync(struct file *filp, int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400378 struct inode *bd_inode = filp->f_mapping->host;
379 struct block_device *bdev = I_BDEV(bd_inode);
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100380 int error;
381
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400382 /*
383 * There is no need to serialise calls to blkdev_issue_flush with
384 * i_mutex and doing so causes performance issues with concurrent
385 * O_SYNC writers to a block device.
386 */
387 mutex_unlock(&bd_inode->i_mutex);
388
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200389 error = blkdev_issue_flush(bdev, GFP_KERNEL, NULL);
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100390 if (error == -EOPNOTSUPP)
391 error = 0;
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400392
393 mutex_lock(&bd_inode->i_mutex);
394
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100395 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
Andrew Mortonb1dd3b22010-04-06 14:35:00 -0700397EXPORT_SYMBOL(blkdev_fsync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399/*
400 * pseudo-fs
401 */
402
403static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
Christoph Lametere18b8902006-12-06 20:33:20 -0800404static struct kmem_cache * bdev_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406static struct inode *bdev_alloc_inode(struct super_block *sb)
407{
Christoph Lametere94b1762006-12-06 20:33:17 -0800408 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (!ei)
410 return NULL;
411 return &ei->vfs_inode;
412}
413
Nick Pigginfa0d7e32011-01-07 17:49:49 +1100414static void bdev_i_callback(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Nick Pigginfa0d7e32011-01-07 17:49:49 +1100416 struct inode *inode = container_of(head, struct inode, i_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 struct bdev_inode *bdi = BDEV_I(inode);
418
Nick Pigginfa0d7e32011-01-07 17:49:49 +1100419 INIT_LIST_HEAD(&inode->i_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 kmem_cache_free(bdev_cachep, bdi);
421}
422
Nick Pigginfa0d7e32011-01-07 17:49:49 +1100423static void bdev_destroy_inode(struct inode *inode)
424{
425 call_rcu(&inode->i_rcu, bdev_i_callback);
426}
427
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700428static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
430 struct bdev_inode *ei = (struct bdev_inode *) foo;
431 struct block_device *bdev = &ei->bdev;
432
Christoph Lametera35afb82007-05-16 22:10:57 -0700433 memset(bdev, 0, sizeof(*bdev));
434 mutex_init(&bdev->bd_mutex);
Christoph Lametera35afb82007-05-16 22:10:57 -0700435 INIT_LIST_HEAD(&bdev->bd_inodes);
436 INIT_LIST_HEAD(&bdev->bd_list);
Tejun Heo49731ba2011-01-14 18:43:57 +0100437#ifdef CONFIG_SYSFS
438 INIT_LIST_HEAD(&bdev->bd_holder_disks);
439#endif
Christoph Lametera35afb82007-05-16 22:10:57 -0700440 inode_init_once(&ei->vfs_inode);
Takashi Satofcccf502009-01-09 16:40:59 -0800441 /* Initialize mutex for freeze. */
442 mutex_init(&bdev->bd_fsfreeze_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
445static inline void __bd_forget(struct inode *inode)
446{
447 list_del_init(&inode->i_devices);
448 inode->i_bdev = NULL;
449 inode->i_mapping = &inode->i_data;
450}
451
Al Virob57922d2010-06-07 14:34:48 -0400452static void bdev_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 struct block_device *bdev = &BDEV_I(inode)->bdev;
455 struct list_head *p;
Al Virob57922d2010-06-07 14:34:48 -0400456 truncate_inode_pages(&inode->i_data, 0);
457 invalidate_inode_buffers(inode); /* is it needed here? */
458 end_writeback(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 spin_lock(&bdev_lock);
460 while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
461 __bd_forget(list_entry(p, struct inode, i_devices));
462 }
463 list_del_init(&bdev->bd_list);
464 spin_unlock(&bdev_lock);
465}
466
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800467static const struct super_operations bdev_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 .statfs = simple_statfs,
469 .alloc_inode = bdev_alloc_inode,
470 .destroy_inode = bdev_destroy_inode,
471 .drop_inode = generic_delete_inode,
Al Virob57922d2010-06-07 14:34:48 -0400472 .evict_inode = bdev_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473};
474
Al Viro51139ad2010-07-25 23:47:46 +0400475static struct dentry *bd_mount(struct file_system_type *fs_type,
476 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Al Viroc74a1cb2011-01-12 16:59:34 -0500478 return mount_pseudo(fs_type, "bdev:", &bdev_sops, NULL, 0x62646576);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
481static struct file_system_type bd_type = {
482 .name = "bdev",
Al Viro51139ad2010-07-25 23:47:46 +0400483 .mount = bd_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 .kill_sb = kill_anon_super,
485};
486
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800487struct super_block *blockdev_superblock __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489void __init bdev_cache_init(void)
490{
491 int err;
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800492 struct vfsmount *bd_mnt;
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800495 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
496 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +0900497 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 err = register_filesystem(&bd_type);
499 if (err)
500 panic("Cannot register bdev pseudo-fs");
501 bd_mnt = kern_mount(&bd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (IS_ERR(bd_mnt))
503 panic("Cannot create bdev pseudo-fs");
Catalin Marinas2e1483c2009-06-11 13:24:13 +0100504 /*
505 * This vfsmount structure is only used to obtain the
506 * blockdev_superblock, so tell kmemleak not to report it.
507 */
508 kmemleak_not_leak(bd_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
510}
511
512/*
513 * Most likely _very_ bad one - but then it's hardly critical for small
514 * /dev and can be fixed when somebody will need really large one.
515 * Keep in mind that it will be fed through icache hash function too.
516 */
517static inline unsigned long hash(dev_t dev)
518{
519 return MAJOR(dev)+MINOR(dev);
520}
521
522static int bdev_test(struct inode *inode, void *data)
523{
524 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
525}
526
527static int bdev_set(struct inode *inode, void *data)
528{
529 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
530 return 0;
531}
532
533static LIST_HEAD(all_bdevs);
534
535struct block_device *bdget(dev_t dev)
536{
537 struct block_device *bdev;
538 struct inode *inode;
539
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800540 inode = iget5_locked(blockdev_superblock, hash(dev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 bdev_test, bdev_set, &dev);
542
543 if (!inode)
544 return NULL;
545
546 bdev = &BDEV_I(inode)->bdev;
547
548 if (inode->i_state & I_NEW) {
549 bdev->bd_contains = NULL;
550 bdev->bd_inode = inode;
551 bdev->bd_block_size = (1 << inode->i_blkbits);
552 bdev->bd_part_count = 0;
553 bdev->bd_invalidated = 0;
554 inode->i_mode = S_IFBLK;
555 inode->i_rdev = dev;
556 inode->i_bdev = bdev;
557 inode->i_data.a_ops = &def_blk_aops;
558 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
559 inode->i_data.backing_dev_info = &default_backing_dev_info;
560 spin_lock(&bdev_lock);
561 list_add(&bdev->bd_list, &all_bdevs);
562 spin_unlock(&bdev_lock);
563 unlock_new_inode(inode);
564 }
565 return bdev;
566}
567
568EXPORT_SYMBOL(bdget);
569
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200570/**
571 * bdgrab -- Grab a reference to an already referenced block device
572 * @bdev: Block device to grab a reference to.
573 */
574struct block_device *bdgrab(struct block_device *bdev)
575{
Al Viro7de9c6e2010-10-23 11:11:40 -0400576 ihold(bdev->bd_inode);
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200577 return bdev;
578}
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580long nr_blockdev_pages(void)
581{
Matthias Kaehlcke203a2932007-07-15 23:40:23 -0700582 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 long ret = 0;
584 spin_lock(&bdev_lock);
Matthias Kaehlcke203a2932007-07-15 23:40:23 -0700585 list_for_each_entry(bdev, &all_bdevs, bd_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 ret += bdev->bd_inode->i_mapping->nrpages;
587 }
588 spin_unlock(&bdev_lock);
589 return ret;
590}
591
592void bdput(struct block_device *bdev)
593{
594 iput(bdev->bd_inode);
595}
596
597EXPORT_SYMBOL(bdput);
598
599static struct block_device *bd_acquire(struct inode *inode)
600{
601 struct block_device *bdev;
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 spin_lock(&bdev_lock);
604 bdev = inode->i_bdev;
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700605 if (bdev) {
Al Viro7de9c6e2010-10-23 11:11:40 -0400606 ihold(bdev->bd_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 spin_unlock(&bdev_lock);
608 return bdev;
609 }
610 spin_unlock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 bdev = bdget(inode->i_rdev);
613 if (bdev) {
614 spin_lock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700615 if (!inode->i_bdev) {
616 /*
Al Viro7de9c6e2010-10-23 11:11:40 -0400617 * We take an additional reference to bd_inode,
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700618 * and it's released in clear_inode() of inode.
619 * So, we can access it via ->i_mapping always
620 * without igrab().
621 */
Al Viro7de9c6e2010-10-23 11:11:40 -0400622 ihold(bdev->bd_inode);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700623 inode->i_bdev = bdev;
624 inode->i_mapping = bdev->bd_inode->i_mapping;
625 list_add(&inode->i_devices, &bdev->bd_inodes);
626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 spin_unlock(&bdev_lock);
628 }
629 return bdev;
630}
631
632/* Call when you free inode */
633
634void bd_forget(struct inode *inode)
635{
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700636 struct block_device *bdev = NULL;
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 spin_lock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700639 if (inode->i_bdev) {
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800640 if (!sb_is_blkdev_sb(inode->i_sb))
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700641 bdev = inode->i_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 __bd_forget(inode);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 spin_unlock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700645
646 if (bdev)
647 iput(bdev->bd_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900650/**
651 * bd_may_claim - test whether a block device can be claimed
652 * @bdev: block device of interest
653 * @whole: whole block device containing @bdev, may equal @bdev
654 * @holder: holder trying to claim @bdev
655 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300656 * Test whether @bdev can be claimed by @holder.
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900657 *
658 * CONTEXT:
659 * spin_lock(&bdev_lock).
660 *
661 * RETURNS:
662 * %true if @bdev can be claimed, %false otherwise.
663 */
664static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
665 void *holder)
666{
667 if (bdev->bd_holder == holder)
668 return true; /* already a holder */
669 else if (bdev->bd_holder != NULL)
670 return false; /* held by someone else */
671 else if (bdev->bd_contains == bdev)
672 return true; /* is a whole device which isn't held */
673
Tejun Heoe525fd82010-11-13 11:55:17 +0100674 else if (whole->bd_holder == bd_may_claim)
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900675 return true; /* is a partition of a device that is being partitioned */
676 else if (whole->bd_holder != NULL)
677 return false; /* is a partition of a held device */
678 else
679 return true; /* is a partition of an un-held device */
680}
681
682/**
Tejun Heo6b4517a2010-04-07 18:53:59 +0900683 * bd_prepare_to_claim - prepare to claim a block device
684 * @bdev: block device of interest
685 * @whole: the whole device containing @bdev, may equal @bdev
686 * @holder: holder trying to claim @bdev
687 *
688 * Prepare to claim @bdev. This function fails if @bdev is already
689 * claimed by another holder and waits if another claiming is in
690 * progress. This function doesn't actually claim. On successful
691 * return, the caller has ownership of bd_claiming and bd_holder[s].
692 *
693 * CONTEXT:
694 * spin_lock(&bdev_lock). Might release bdev_lock, sleep and regrab
695 * it multiple times.
696 *
697 * RETURNS:
698 * 0 if @bdev can be claimed, -EBUSY otherwise.
699 */
700static int bd_prepare_to_claim(struct block_device *bdev,
701 struct block_device *whole, void *holder)
702{
703retry:
704 /* if someone else claimed, fail */
705 if (!bd_may_claim(bdev, whole, holder))
706 return -EBUSY;
707
Tejun Heoe75aa852010-08-04 17:59:39 +0200708 /* if claiming is already in progress, wait for it to finish */
709 if (whole->bd_claiming) {
Tejun Heo6b4517a2010-04-07 18:53:59 +0900710 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
711 DEFINE_WAIT(wait);
712
713 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
714 spin_unlock(&bdev_lock);
715 schedule();
716 finish_wait(wq, &wait);
717 spin_lock(&bdev_lock);
718 goto retry;
719 }
720
721 /* yay, all mine */
722 return 0;
723}
724
725/**
726 * bd_start_claiming - start claiming a block device
727 * @bdev: block device of interest
728 * @holder: holder trying to claim @bdev
729 *
730 * @bdev is about to be opened exclusively. Check @bdev can be opened
731 * exclusively and mark that an exclusive open is in progress. Each
732 * successful call to this function must be matched with a call to
Nick Pigginb0018362010-05-26 01:51:19 +1000733 * either bd_finish_claiming() or bd_abort_claiming() (which do not
734 * fail).
735 *
736 * This function is used to gain exclusive access to the block device
737 * without actually causing other exclusive open attempts to fail. It
738 * should be used when the open sequence itself requires exclusive
739 * access but may subsequently fail.
Tejun Heo6b4517a2010-04-07 18:53:59 +0900740 *
741 * CONTEXT:
742 * Might sleep.
743 *
744 * RETURNS:
745 * Pointer to the block device containing @bdev on success, ERR_PTR()
746 * value on failure.
747 */
748static struct block_device *bd_start_claiming(struct block_device *bdev,
749 void *holder)
750{
751 struct gendisk *disk;
752 struct block_device *whole;
753 int partno, err;
754
755 might_sleep();
756
757 /*
758 * @bdev might not have been initialized properly yet, look up
759 * and grab the outer block device the hard way.
760 */
761 disk = get_gendisk(bdev->bd_dev, &partno);
762 if (!disk)
763 return ERR_PTR(-ENXIO);
764
765 whole = bdget_disk(disk, 0);
Nick Piggincf342572010-05-26 01:50:21 +1000766 module_put(disk->fops->owner);
Tejun Heo6b4517a2010-04-07 18:53:59 +0900767 put_disk(disk);
768 if (!whole)
769 return ERR_PTR(-ENOMEM);
770
771 /* prepare to claim, if successful, mark claiming in progress */
772 spin_lock(&bdev_lock);
773
774 err = bd_prepare_to_claim(bdev, whole, holder);
775 if (err == 0) {
776 whole->bd_claiming = holder;
777 spin_unlock(&bdev_lock);
778 return whole;
779 } else {
780 spin_unlock(&bdev_lock);
781 bdput(whole);
782 return ERR_PTR(err);
783 }
784}
785
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800786#ifdef CONFIG_SYSFS
Tejun Heo49731ba2011-01-14 18:43:57 +0100787struct bd_holder_disk {
788 struct list_head list;
789 struct gendisk *disk;
790 int refcnt;
791};
792
793static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev,
794 struct gendisk *disk)
795{
796 struct bd_holder_disk *holder;
797
798 list_for_each_entry(holder, &bdev->bd_holder_disks, list)
799 if (holder->disk == disk)
800 return holder;
801 return NULL;
802}
803
Andrew Morton4d7dd8f2006-09-29 01:58:56 -0700804static int add_symlink(struct kobject *from, struct kobject *to)
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800805{
Andrew Morton4d7dd8f2006-09-29 01:58:56 -0700806 return sysfs_create_link(from, to, kobject_name(to));
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800807}
808
809static void del_symlink(struct kobject *from, struct kobject *to)
810{
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800811 sysfs_remove_link(from, kobject_name(to));
812}
813
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800814/**
Tejun Heoe09b4572010-11-13 11:55:17 +0100815 * bd_link_disk_holder - create symlinks between holding disk and slave bdev
816 * @bdev: the claimed slave bdev
817 * @disk: the holding disk
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500818 *
Tejun Heo49731ba2011-01-14 18:43:57 +0100819 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
820 *
Tejun Heoe09b4572010-11-13 11:55:17 +0100821 * This functions creates the following sysfs symlinks.
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500822 *
Tejun Heoe09b4572010-11-13 11:55:17 +0100823 * - from "slaves" directory of the holder @disk to the claimed @bdev
824 * - from "holders" directory of the @bdev to the holder @disk
825 *
826 * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
827 * passed to bd_link_disk_holder(), then:
828 *
829 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
830 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
831 *
832 * The caller must have claimed @bdev before calling this function and
833 * ensure that both @bdev and @disk are valid during the creation and
834 * lifetime of these symlinks.
835 *
836 * CONTEXT:
837 * Might sleep.
838 *
839 * RETURNS:
840 * 0 on success, -errno on failure.
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500841 */
Tejun Heoe09b4572010-11-13 11:55:17 +0100842int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500843{
Tejun Heo49731ba2011-01-14 18:43:57 +0100844 struct bd_holder_disk *holder;
Tejun Heoe09b4572010-11-13 11:55:17 +0100845 int ret = 0;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800846
Peter Zijlstra2e7b6512006-12-08 02:36:13 -0800847 mutex_lock(&bdev->bd_mutex);
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500848
Tejun Heo49731ba2011-01-14 18:43:57 +0100849 WARN_ON_ONCE(!bdev->bd_holder);
Johannes Weiner4e916722007-07-15 23:41:25 -0700850
Tejun Heoe09b4572010-11-13 11:55:17 +0100851 /* FIXME: remove the following once add_disk() handles errors */
852 if (WARN_ON(!disk->slave_dir || !bdev->bd_part->holder_dir))
853 goto out_unlock;
Johannes Weiner4e916722007-07-15 23:41:25 -0700854
Tejun Heo49731ba2011-01-14 18:43:57 +0100855 holder = bd_find_holder_disk(bdev, disk);
856 if (holder) {
857 holder->refcnt++;
Tejun Heoe09b4572010-11-13 11:55:17 +0100858 goto out_unlock;
859 }
860
Tejun Heo49731ba2011-01-14 18:43:57 +0100861 holder = kzalloc(sizeof(*holder), GFP_KERNEL);
862 if (!holder) {
863 ret = -ENOMEM;
864 goto out_unlock;
865 }
866
867 INIT_LIST_HEAD(&holder->list);
868 holder->disk = disk;
869 holder->refcnt = 1;
870
871 ret = add_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
872 if (ret)
873 goto out_free;
874
875 ret = add_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj);
876 if (ret)
877 goto out_del;
Tejun Heoe7407d12011-02-24 09:56:32 +0100878 /*
879 * bdev could be deleted beneath us which would implicitly destroy
880 * the holder directory. Hold on to it.
881 */
882 kobject_get(bdev->bd_part->holder_dir);
Tejun Heo49731ba2011-01-14 18:43:57 +0100883
884 list_add(&holder->list, &bdev->bd_holder_disks);
885 goto out_unlock;
886
887out_del:
888 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
889out_free:
890 kfree(holder);
Tejun Heoe09b4572010-11-13 11:55:17 +0100891out_unlock:
Jun'ichi Nomurab4cf1b72006-03-27 01:18:00 -0800892 mutex_unlock(&bdev->bd_mutex);
Tejun Heoe09b4572010-11-13 11:55:17 +0100893 return ret;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800894}
Tejun Heoe09b4572010-11-13 11:55:17 +0100895EXPORT_SYMBOL_GPL(bd_link_disk_holder);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800896
Tejun Heo49731ba2011-01-14 18:43:57 +0100897/**
898 * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
899 * @bdev: the calimed slave bdev
900 * @disk: the holding disk
901 *
902 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
903 *
904 * CONTEXT:
905 * Might sleep.
906 */
907void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800908{
Tejun Heo49731ba2011-01-14 18:43:57 +0100909 struct bd_holder_disk *holder;
Tejun Heoe09b4572010-11-13 11:55:17 +0100910
Tejun Heo49731ba2011-01-14 18:43:57 +0100911 mutex_lock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800912
Tejun Heo49731ba2011-01-14 18:43:57 +0100913 holder = bd_find_holder_disk(bdev, disk);
914
915 if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
916 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
917 del_symlink(bdev->bd_part->holder_dir,
918 &disk_to_dev(disk)->kobj);
Tejun Heoe7407d12011-02-24 09:56:32 +0100919 kobject_put(bdev->bd_part->holder_dir);
Tejun Heo49731ba2011-01-14 18:43:57 +0100920 list_del_init(&holder->list);
921 kfree(holder);
922 }
923
924 mutex_unlock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800925}
Tejun Heo49731ba2011-01-14 18:43:57 +0100926EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800927#endif
928
Andrew Patterson0c002c22008-09-04 14:27:20 -0600929/**
Andrew Patterson56ade442008-09-04 14:27:40 -0600930 * flush_disk - invalidates all buffer-cache entries on a disk
931 *
932 * @bdev: struct block device to be flushed
Randy Dunlape6eb5ce2011-02-26 10:54:00 -0800933 * @kill_dirty: flag to guide handling of dirty inodes
Andrew Patterson56ade442008-09-04 14:27:40 -0600934 *
935 * Invalidates all buffer-cache entries on a disk. It should be called
936 * when a disk has been changed -- either by a media change or online
937 * resize.
938 */
NeilBrown93b270f2011-02-24 17:25:47 +1100939static void flush_disk(struct block_device *bdev, bool kill_dirty)
Andrew Patterson56ade442008-09-04 14:27:40 -0600940{
NeilBrown93b270f2011-02-24 17:25:47 +1100941 if (__invalidate_device(bdev, kill_dirty)) {
Andrew Patterson56ade442008-09-04 14:27:40 -0600942 char name[BDEVNAME_SIZE] = "";
943
944 if (bdev->bd_disk)
945 disk_name(bdev->bd_disk, 0, name);
946 printk(KERN_WARNING "VFS: busy inodes on changed media or "
947 "resized disk %s\n", name);
948 }
949
950 if (!bdev->bd_disk)
951 return;
952 if (disk_partitionable(bdev->bd_disk))
953 bdev->bd_invalidated = 1;
954}
955
956/**
Randy Dunlap57d1b532008-10-09 10:42:38 +0200957 * check_disk_size_change - checks for disk size change and adjusts bdev size.
Andrew Pattersonc3279d12008-09-04 14:27:25 -0600958 * @disk: struct gendisk to check
959 * @bdev: struct bdev to adjust.
960 *
961 * This routine checks to see if the bdev size does not match the disk size
962 * and adjusts it if it differs.
963 */
964void check_disk_size_change(struct gendisk *disk, struct block_device *bdev)
965{
966 loff_t disk_size, bdev_size;
967
968 disk_size = (loff_t)get_capacity(disk) << 9;
969 bdev_size = i_size_read(bdev->bd_inode);
970 if (disk_size != bdev_size) {
971 char name[BDEVNAME_SIZE];
972
973 disk_name(disk, 0, name);
974 printk(KERN_INFO
975 "%s: detected capacity change from %lld to %lld\n",
976 name, bdev_size, disk_size);
977 i_size_write(bdev->bd_inode, disk_size);
NeilBrown93b270f2011-02-24 17:25:47 +1100978 flush_disk(bdev, false);
Andrew Pattersonc3279d12008-09-04 14:27:25 -0600979 }
980}
981EXPORT_SYMBOL(check_disk_size_change);
982
983/**
Randy Dunlap57d1b532008-10-09 10:42:38 +0200984 * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back
Andrew Patterson0c002c22008-09-04 14:27:20 -0600985 * @disk: struct gendisk to be revalidated
986 *
987 * This routine is a wrapper for lower-level driver's revalidate_disk
988 * call-backs. It is used to do common pre and post operations needed
989 * for all revalidate_disk operations.
990 */
991int revalidate_disk(struct gendisk *disk)
992{
Andrew Pattersonc3279d12008-09-04 14:27:25 -0600993 struct block_device *bdev;
Andrew Patterson0c002c22008-09-04 14:27:20 -0600994 int ret = 0;
995
996 if (disk->fops->revalidate_disk)
997 ret = disk->fops->revalidate_disk(disk);
998
Andrew Pattersonc3279d12008-09-04 14:27:25 -0600999 bdev = bdget_disk(disk, 0);
1000 if (!bdev)
1001 return ret;
1002
1003 mutex_lock(&bdev->bd_mutex);
1004 check_disk_size_change(disk, bdev);
1005 mutex_unlock(&bdev->bd_mutex);
1006 bdput(bdev);
Andrew Patterson0c002c22008-09-04 14:27:20 -06001007 return ret;
1008}
1009EXPORT_SYMBOL(revalidate_disk);
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011/*
1012 * This routine checks whether a removable media has been changed,
1013 * and invalidates all buffer-cache-entries in that case. This
1014 * is a relatively slow routine, so we have to try to minimize using
1015 * it. Thus it is called only upon a 'mount' or 'open'. This
1016 * is the best way of combining speed and utility, I think.
1017 * People changing diskettes in the middle of an operation deserve
1018 * to lose :-)
1019 */
1020int check_disk_change(struct block_device *bdev)
1021{
1022 struct gendisk *disk = bdev->bd_disk;
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001023 const struct block_device_operations *bdops = disk->fops;
Tejun Heo77ea8872010-12-08 20:57:37 +01001024 unsigned int events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Tejun Heo77ea8872010-12-08 20:57:37 +01001026 events = disk_clear_events(disk, DISK_EVENT_MEDIA_CHANGE |
1027 DISK_EVENT_EJECT_REQUEST);
1028 if (!(events & DISK_EVENT_MEDIA_CHANGE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return 0;
1030
NeilBrown93b270f2011-02-24 17:25:47 +11001031 flush_disk(bdev, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (bdops->revalidate_disk)
1033 bdops->revalidate_disk(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return 1;
1035}
1036
1037EXPORT_SYMBOL(check_disk_change);
1038
1039void bd_set_size(struct block_device *bdev, loff_t size)
1040{
Martin K. Petersene1defc42009-05-22 17:17:49 -04001041 unsigned bsize = bdev_logical_block_size(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 bdev->bd_inode->i_size = size;
1044 while (bsize < PAGE_CACHE_SIZE) {
1045 if (size & bsize)
1046 break;
1047 bsize <<= 1;
1048 }
1049 bdev->bd_block_size = bsize;
1050 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
1051}
1052EXPORT_SYMBOL(bd_set_size);
1053
Al Viro9a1c3542008-02-22 20:40:24 -05001054static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
NeilBrown37be4122006-12-08 02:36:16 -08001055
Peter Zijlstra6d740cd2007-02-20 13:58:18 -08001056/*
1057 * bd_mutex locking:
1058 *
1059 * mutex_lock(part->bd_mutex)
1060 * mutex_lock_nested(whole->bd_mutex, 1)
1061 */
1062
Al Viro572c4892007-10-08 13:24:05 -04001063static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 struct gendisk *disk;
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -07001066 int ret;
Tejun Heocf771cb2008-09-03 09:01:09 +02001067 int partno;
Al Virofe6e9c12008-06-23 08:30:55 -04001068 int perm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Al Viro572c4892007-10-08 13:24:05 -04001070 if (mode & FMODE_READ)
Al Virofe6e9c12008-06-23 08:30:55 -04001071 perm |= MAY_READ;
Al Viro572c4892007-10-08 13:24:05 -04001072 if (mode & FMODE_WRITE)
Al Virofe6e9c12008-06-23 08:30:55 -04001073 perm |= MAY_WRITE;
1074 /*
1075 * hooks: /n/, see "layering violations".
1076 */
Chris Wrightb7300b72010-08-10 18:02:55 -07001077 if (!for_part) {
1078 ret = devcgroup_inode_permission(bdev->bd_inode, perm);
1079 if (ret != 0) {
1080 bdput(bdev);
1081 return ret;
1082 }
Al Viro82666022008-08-01 05:32:04 -04001083 }
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -07001084
NeilBrownd3374822009-01-09 08:31:10 +11001085 restart:
Tejun Heo0762b8b2008-08-25 19:56:12 +09001086
Tejun Heo89f97492008-11-05 10:21:06 +01001087 ret = -ENXIO;
Tejun Heocf771cb2008-09-03 09:01:09 +02001088 disk = get_gendisk(bdev->bd_dev, &partno);
Tejun Heo0762b8b2008-08-25 19:56:12 +09001089 if (!disk)
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001090 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Tejun Heo69e02c52011-03-09 19:54:27 +01001092 disk_block_events(disk);
NeilBrown6796bf52006-12-08 02:36:16 -08001093 mutex_lock_nested(&bdev->bd_mutex, for_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if (!bdev->bd_openers) {
1095 bdev->bd_disk = disk;
1096 bdev->bd_contains = bdev;
Tejun Heocf771cb2008-09-03 09:01:09 +02001097 if (!partno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 struct backing_dev_info *bdi;
Tejun Heo89f97492008-11-05 10:21:06 +01001099
1100 ret = -ENXIO;
1101 bdev->bd_part = disk_get_part(disk, partno);
1102 if (!bdev->bd_part)
1103 goto out_clear;
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 if (disk->fops->open) {
Al Viro572c4892007-10-08 13:24:05 -04001106 ret = disk->fops->open(bdev, mode);
NeilBrownd3374822009-01-09 08:31:10 +11001107 if (ret == -ERESTARTSYS) {
1108 /* Lost a race with 'disk' being
1109 * deleted, try again.
1110 * See md.c
1111 */
1112 disk_put_part(bdev->bd_part);
1113 bdev->bd_part = NULL;
NeilBrownd3374822009-01-09 08:31:10 +11001114 bdev->bd_disk = NULL;
1115 mutex_unlock(&bdev->bd_mutex);
Tejun Heo69e02c52011-03-09 19:54:27 +01001116 disk_unblock_events(disk);
1117 module_put(disk->fops->owner);
1118 put_disk(disk);
NeilBrownd3374822009-01-09 08:31:10 +11001119 goto restart;
1120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (ret)
Tejun Heo0762b8b2008-08-25 19:56:12 +09001122 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
1124 if (!bdev->bd_openers) {
1125 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
1126 bdi = blk_get_backing_dev_info(bdev);
1127 if (bdi == NULL)
1128 bdi = &default_backing_dev_info;
Dave Chinnera5491e02010-10-21 11:49:26 +11001129 bdev_inode_switch_bdi(bdev->bd_inode, bdi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
1131 if (bdev->bd_invalidated)
1132 rescan_partitions(disk, bdev);
1133 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 struct block_device *whole;
1135 whole = bdget_disk(disk, 0);
1136 ret = -ENOMEM;
1137 if (!whole)
Tejun Heo0762b8b2008-08-25 19:56:12 +09001138 goto out_clear;
NeilBrown37be4122006-12-08 02:36:16 -08001139 BUG_ON(for_part);
Al Viro572c4892007-10-08 13:24:05 -04001140 ret = __blkdev_get(whole, mode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (ret)
Tejun Heo0762b8b2008-08-25 19:56:12 +09001142 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 bdev->bd_contains = whole;
Dave Chinnera5491e02010-10-21 11:49:26 +11001144 bdev_inode_switch_bdi(bdev->bd_inode,
1145 whole->bd_inode->i_data.backing_dev_info);
Tejun Heo89f97492008-11-05 10:21:06 +01001146 bdev->bd_part = disk_get_part(disk, partno);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001147 if (!(disk->flags & GENHD_FL_UP) ||
Tejun Heo89f97492008-11-05 10:21:06 +01001148 !bdev->bd_part || !bdev->bd_part->nr_sects) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 ret = -ENXIO;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001150 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
Tejun Heo89f97492008-11-05 10:21:06 +01001152 bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
1154 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (bdev->bd_contains == bdev) {
1156 if (bdev->bd_disk->fops->open) {
Al Viro572c4892007-10-08 13:24:05 -04001157 ret = bdev->bd_disk->fops->open(bdev, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 if (ret)
Tejun Heo0762b8b2008-08-25 19:56:12 +09001159 goto out_unlock_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 }
1161 if (bdev->bd_invalidated)
1162 rescan_partitions(bdev->bd_disk, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
Tejun Heo69e02c52011-03-09 19:54:27 +01001164 /* only one opener holds refs to the module and disk */
1165 module_put(disk->fops->owner);
1166 put_disk(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
1168 bdev->bd_openers++;
NeilBrown37be4122006-12-08 02:36:16 -08001169 if (for_part)
1170 bdev->bd_part_count++;
Arjan van de Venc039e312006-03-23 03:00:28 -08001171 mutex_unlock(&bdev->bd_mutex);
Tejun Heo69e02c52011-03-09 19:54:27 +01001172 disk_unblock_events(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 return 0;
1174
Tejun Heo0762b8b2008-08-25 19:56:12 +09001175 out_clear:
Tejun Heo89f97492008-11-05 10:21:06 +01001176 disk_put_part(bdev->bd_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 bdev->bd_disk = NULL;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001178 bdev->bd_part = NULL;
Dave Chinnera5491e02010-10-21 11:49:26 +11001179 bdev_inode_switch_bdi(bdev->bd_inode, &default_backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (bdev != bdev->bd_contains)
Al Viro572c4892007-10-08 13:24:05 -04001181 __blkdev_put(bdev->bd_contains, mode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 bdev->bd_contains = NULL;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001183 out_unlock_bdev:
Arjan van de Venc039e312006-03-23 03:00:28 -08001184 mutex_unlock(&bdev->bd_mutex);
Tejun Heo69e02c52011-03-09 19:54:27 +01001185 disk_unblock_events(disk);
Tejun Heo69e02c52011-03-09 19:54:27 +01001186 module_put(disk->fops->owner);
Tejun Heo0762b8b2008-08-25 19:56:12 +09001187 put_disk(disk);
Dan Carpenter4345cab2011-03-19 13:53:31 +01001188 out:
Tejun Heo0762b8b2008-08-25 19:56:12 +09001189 bdput(bdev);
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return ret;
1192}
1193
Tejun Heod4d77622010-11-13 11:55:18 +01001194/**
1195 * blkdev_get - open a block device
1196 * @bdev: block_device to open
1197 * @mode: FMODE_* mask
1198 * @holder: exclusive holder identifier
1199 *
1200 * Open @bdev with @mode. If @mode includes %FMODE_EXCL, @bdev is
1201 * open with exclusive access. Specifying %FMODE_EXCL with %NULL
1202 * @holder is invalid. Exclusive opens may nest for the same @holder.
1203 *
1204 * On success, the reference count of @bdev is unchanged. On failure,
1205 * @bdev is put.
1206 *
1207 * CONTEXT:
1208 * Might sleep.
1209 *
1210 * RETURNS:
1211 * 0 on success, -errno on failure.
1212 */
Tejun Heoe525fd82010-11-13 11:55:17 +01001213int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Tejun Heoe525fd82010-11-13 11:55:17 +01001215 struct block_device *whole = NULL;
1216 int res;
1217
1218 WARN_ON_ONCE((mode & FMODE_EXCL) && !holder);
1219
1220 if ((mode & FMODE_EXCL) && holder) {
1221 whole = bd_start_claiming(bdev, holder);
1222 if (IS_ERR(whole)) {
1223 bdput(bdev);
1224 return PTR_ERR(whole);
1225 }
1226 }
1227
1228 res = __blkdev_get(bdev, mode, 0);
1229
1230 if (whole) {
Tejun Heo6a027ef2010-11-13 11:55:17 +01001231 /* finish claiming */
Tejun Heo77ea8872010-12-08 20:57:37 +01001232 mutex_lock(&bdev->bd_mutex);
Tejun Heo6a027ef2010-11-13 11:55:17 +01001233 spin_lock(&bdev_lock);
1234
Tejun Heo77ea8872010-12-08 20:57:37 +01001235 if (!res) {
Tejun Heo6a027ef2010-11-13 11:55:17 +01001236 BUG_ON(!bd_may_claim(bdev, whole, holder));
1237 /*
1238 * Note that for a whole device bd_holders
1239 * will be incremented twice, and bd_holder
1240 * will be set to bd_may_claim before being
1241 * set to holder
1242 */
1243 whole->bd_holders++;
1244 whole->bd_holder = bd_may_claim;
1245 bdev->bd_holders++;
1246 bdev->bd_holder = holder;
1247 }
1248
1249 /* tell others that we're done */
1250 BUG_ON(whole->bd_claiming != holder);
1251 whole->bd_claiming = NULL;
1252 wake_up_bit(&whole->bd_claiming, 0);
1253
1254 spin_unlock(&bdev_lock);
Tejun Heo77ea8872010-12-08 20:57:37 +01001255
1256 /*
1257 * Block event polling for write claims. Any write
1258 * holder makes the write_holder state stick until all
1259 * are released. This is good enough and tracking
1260 * individual writeable reference is too fragile given
1261 * the way @mode is used in blkdev_get/put().
1262 */
1263 if (!res && (mode & FMODE_WRITE) && !bdev->bd_write_holder) {
1264 bdev->bd_write_holder = true;
1265 disk_block_events(bdev->bd_disk);
1266 }
1267
1268 mutex_unlock(&bdev->bd_mutex);
Tejun Heo6a027ef2010-11-13 11:55:17 +01001269 bdput(whole);
Tejun Heoe525fd82010-11-13 11:55:17 +01001270 }
1271
1272 return res;
NeilBrown37be4122006-12-08 02:36:16 -08001273}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274EXPORT_SYMBOL(blkdev_get);
1275
Tejun Heod4d77622010-11-13 11:55:18 +01001276/**
1277 * blkdev_get_by_path - open a block device by name
1278 * @path: path to the block device to open
1279 * @mode: FMODE_* mask
1280 * @holder: exclusive holder identifier
1281 *
1282 * Open the blockdevice described by the device file at @path. @mode
1283 * and @holder are identical to blkdev_get().
1284 *
1285 * On success, the returned block_device has reference count of one.
1286 *
1287 * CONTEXT:
1288 * Might sleep.
1289 *
1290 * RETURNS:
1291 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1292 */
1293struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
1294 void *holder)
1295{
1296 struct block_device *bdev;
1297 int err;
1298
1299 bdev = lookup_bdev(path);
1300 if (IS_ERR(bdev))
1301 return bdev;
1302
1303 err = blkdev_get(bdev, mode, holder);
1304 if (err)
1305 return ERR_PTR(err);
1306
Chuck Ebberte51900f2011-02-16 18:11:53 -05001307 if ((mode & FMODE_WRITE) && bdev_read_only(bdev)) {
1308 blkdev_put(bdev, mode);
1309 return ERR_PTR(-EACCES);
1310 }
1311
Tejun Heod4d77622010-11-13 11:55:18 +01001312 return bdev;
1313}
1314EXPORT_SYMBOL(blkdev_get_by_path);
1315
1316/**
1317 * blkdev_get_by_dev - open a block device by device number
1318 * @dev: device number of block device to open
1319 * @mode: FMODE_* mask
1320 * @holder: exclusive holder identifier
1321 *
1322 * Open the blockdevice described by device number @dev. @mode and
1323 * @holder are identical to blkdev_get().
1324 *
1325 * Use it ONLY if you really do not have anything better - i.e. when
1326 * you are behind a truly sucky interface and all you are given is a
1327 * device number. _Never_ to be used for internal purposes. If you
1328 * ever need it - reconsider your API.
1329 *
1330 * On success, the returned block_device has reference count of one.
1331 *
1332 * CONTEXT:
1333 * Might sleep.
1334 *
1335 * RETURNS:
1336 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1337 */
1338struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
1339{
1340 struct block_device *bdev;
1341 int err;
1342
1343 bdev = bdget(dev);
1344 if (!bdev)
1345 return ERR_PTR(-ENOMEM);
1346
1347 err = blkdev_get(bdev, mode, holder);
1348 if (err)
1349 return ERR_PTR(err);
1350
1351 return bdev;
1352}
1353EXPORT_SYMBOL(blkdev_get_by_dev);
1354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355static int blkdev_open(struct inode * inode, struct file * filp)
1356{
1357 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 /*
1360 * Preserve backwards compatibility and allow large file access
1361 * even if userspace doesn't ask for it explicitly. Some mkfs
1362 * binary needs it. We might want to drop this workaround
1363 * during an unstable branch.
1364 */
1365 filp->f_flags |= O_LARGEFILE;
1366
Al Viro572c4892007-10-08 13:24:05 -04001367 if (filp->f_flags & O_NDELAY)
1368 filp->f_mode |= FMODE_NDELAY;
1369 if (filp->f_flags & O_EXCL)
1370 filp->f_mode |= FMODE_EXCL;
1371 if ((filp->f_flags & O_ACCMODE) == 3)
1372 filp->f_mode |= FMODE_WRITE_IOCTL;
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 bdev = bd_acquire(inode);
Pavel Emelianov6a2aae02006-10-28 10:38:33 -07001375 if (bdev == NULL)
1376 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Al Viro572c4892007-10-08 13:24:05 -04001378 filp->f_mapping = bdev->bd_inode->i_mapping;
1379
Tejun Heoe525fd82010-11-13 11:55:17 +01001380 return blkdev_get(bdev, filp->f_mode, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381}
1382
Al Viro9a1c3542008-02-22 20:40:24 -05001383static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001384{
1385 int ret = 0;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001386 struct gendisk *disk = bdev->bd_disk;
NeilBrown37be4122006-12-08 02:36:16 -08001387 struct block_device *victim = NULL;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001388
NeilBrown6796bf52006-12-08 02:36:16 -08001389 mutex_lock_nested(&bdev->bd_mutex, for_part);
NeilBrown37be4122006-12-08 02:36:16 -08001390 if (for_part)
1391 bdev->bd_part_count--;
1392
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001393 if (!--bdev->bd_openers) {
Tejun Heo6a027ef2010-11-13 11:55:17 +01001394 WARN_ON_ONCE(bdev->bd_holders);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001395 sync_blockdev(bdev);
1396 kill_bdev(bdev);
1397 }
1398 if (bdev->bd_contains == bdev) {
1399 if (disk->fops->release)
Al Viro9a1c3542008-02-22 20:40:24 -05001400 ret = disk->fops->release(disk, mode);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001401 }
1402 if (!bdev->bd_openers) {
1403 struct module *owner = disk->fops->owner;
1404
1405 put_disk(disk);
1406 module_put(owner);
Tejun Heo0762b8b2008-08-25 19:56:12 +09001407 disk_put_part(bdev->bd_part);
1408 bdev->bd_part = NULL;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001409 bdev->bd_disk = NULL;
Dave Chinnera5491e02010-10-21 11:49:26 +11001410 bdev_inode_switch_bdi(bdev->bd_inode,
1411 &default_backing_dev_info);
NeilBrown37be4122006-12-08 02:36:16 -08001412 if (bdev != bdev->bd_contains)
1413 victim = bdev->bd_contains;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001414 bdev->bd_contains = NULL;
1415 }
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001416 mutex_unlock(&bdev->bd_mutex);
1417 bdput(bdev);
NeilBrown37be4122006-12-08 02:36:16 -08001418 if (victim)
Al Viro9a1c3542008-02-22 20:40:24 -05001419 __blkdev_put(victim, mode, 1);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001420 return ret;
1421}
1422
Al Viro9a1c3542008-02-22 20:40:24 -05001423int blkdev_put(struct block_device *bdev, fmode_t mode)
NeilBrown37be4122006-12-08 02:36:16 -08001424{
Tejun Heoe525fd82010-11-13 11:55:17 +01001425 if (mode & FMODE_EXCL) {
Tejun Heo6a027ef2010-11-13 11:55:17 +01001426 bool bdev_free;
1427
1428 /*
1429 * Release a claim on the device. The holder fields
1430 * are protected with bdev_lock. bd_mutex is to
1431 * synchronize disk_holder unlinking.
1432 */
Tejun Heoe525fd82010-11-13 11:55:17 +01001433 mutex_lock(&bdev->bd_mutex);
Tejun Heo6a027ef2010-11-13 11:55:17 +01001434 spin_lock(&bdev_lock);
1435
1436 WARN_ON_ONCE(--bdev->bd_holders < 0);
1437 WARN_ON_ONCE(--bdev->bd_contains->bd_holders < 0);
1438
1439 /* bd_contains might point to self, check in a separate step */
1440 if ((bdev_free = !bdev->bd_holders))
1441 bdev->bd_holder = NULL;
1442 if (!bdev->bd_contains->bd_holders)
1443 bdev->bd_contains->bd_holder = NULL;
1444
1445 spin_unlock(&bdev_lock);
1446
Tejun Heo77ea8872010-12-08 20:57:37 +01001447 /*
1448 * If this was the last claim, remove holder link and
1449 * unblock evpoll if it was a write holder.
1450 */
1451 if (bdev_free) {
Tejun Heo77ea8872010-12-08 20:57:37 +01001452 if (bdev->bd_write_holder) {
1453 disk_unblock_events(bdev->bd_disk);
Tejun Heofacc31d2011-03-09 19:54:27 +01001454 disk_check_events(bdev->bd_disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01001455 bdev->bd_write_holder = false;
Tejun Heo69362172011-03-09 19:54:27 +01001456 }
Tejun Heo77ea8872010-12-08 20:57:37 +01001457 }
Tejun Heo6a027ef2010-11-13 11:55:17 +01001458
Tejun Heoe525fd82010-11-13 11:55:17 +01001459 mutex_unlock(&bdev->bd_mutex);
Tejun Heo69362172011-03-09 19:54:27 +01001460 }
Tejun Heo77ea8872010-12-08 20:57:37 +01001461
Al Viro9a1c3542008-02-22 20:40:24 -05001462 return __blkdev_put(bdev, mode, 0);
NeilBrown37be4122006-12-08 02:36:16 -08001463}
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001464EXPORT_SYMBOL(blkdev_put);
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466static int blkdev_close(struct inode * inode, struct file * filp)
1467{
1468 struct block_device *bdev = I_BDEV(filp->f_mapping->host);
Tejun Heoe525fd82010-11-13 11:55:17 +01001469
Al Viro9a1c3542008-02-22 20:40:24 -05001470 return blkdev_put(bdev, filp->f_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471}
1472
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07001473static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Al Viro56b26ad2008-09-19 03:17:36 -04001475 struct block_device *bdev = I_BDEV(file->f_mapping->host);
1476 fmode_t mode = file->f_mode;
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +01001477
1478 /*
1479 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
1480 * to updated it before every ioctl.
1481 */
Al Viro56b26ad2008-09-19 03:17:36 -04001482 if (file->f_flags & O_NDELAY)
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +01001483 mode |= FMODE_NDELAY;
1484 else
1485 mode &= ~FMODE_NDELAY;
1486
Al Viro56b26ad2008-09-19 03:17:36 -04001487 return blkdev_ioctl(bdev, mode, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488}
1489
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05001490/*
Christoph Hellwigeef99382009-08-20 17:43:41 +02001491 * Write data to the block device. Only intended for the block device itself
1492 * and the raw driver which basically is a fake block device.
1493 *
1494 * Does not take i_mutex for the write and thus is not for general purpose
1495 * use.
1496 */
1497ssize_t blkdev_aio_write(struct kiocb *iocb, const struct iovec *iov,
1498 unsigned long nr_segs, loff_t pos)
1499{
1500 struct file *file = iocb->ki_filp;
1501 ssize_t ret;
1502
1503 BUG_ON(iocb->ki_pos != pos);
1504
1505 ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos);
1506 if (ret > 0 || ret == -EIOCBQUEUED) {
1507 ssize_t err;
1508
1509 err = generic_write_sync(file, pos, ret);
1510 if (err < 0 && ret > 0)
1511 ret = err;
1512 }
1513 return ret;
1514}
1515EXPORT_SYMBOL_GPL(blkdev_aio_write);
1516
1517/*
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05001518 * Try to release a page associated with block device when the system
1519 * is under memory pressure.
1520 */
1521static int blkdev_releasepage(struct page *page, gfp_t wait)
1522{
1523 struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
1524
1525 if (super && super->s_op->bdev_try_to_free_page)
1526 return super->s_op->bdev_try_to_free_page(super, page, wait);
1527
1528 return try_to_free_buffers(page);
1529}
1530
Adrian Bunk4c54ac62008-02-18 13:48:31 +01001531static const struct address_space_operations def_blk_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 .readpage = blkdev_readpage,
1533 .writepage = blkdev_writepage,
Nick Piggin6272b5a2007-10-16 01:25:04 -07001534 .write_begin = blkdev_write_begin,
1535 .write_end = blkdev_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 .writepages = generic_writepages,
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05001537 .releasepage = blkdev_releasepage,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 .direct_IO = blkdev_direct_IO,
1539};
1540
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001541const struct file_operations def_blk_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 .open = blkdev_open,
1543 .release = blkdev_close,
1544 .llseek = block_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -07001545 .read = do_sync_read,
1546 .write = do_sync_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 .aio_read = generic_file_aio_read,
Christoph Hellwigeef99382009-08-20 17:43:41 +02001548 .aio_write = blkdev_aio_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 .mmap = generic_file_mmap,
Andrew Mortonb1dd3b22010-04-06 14:35:00 -07001550 .fsync = blkdev_fsync,
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07001551 .unlocked_ioctl = block_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552#ifdef CONFIG_COMPAT
1553 .compat_ioctl = compat_blkdev_ioctl,
1554#endif
Jens Axboe7f9c51f2006-05-01 19:59:32 +02001555 .splice_read = generic_file_splice_read,
1556 .splice_write = generic_file_splice_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557};
1558
1559int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
1560{
1561 int res;
1562 mm_segment_t old_fs = get_fs();
1563 set_fs(KERNEL_DS);
Al Viro56b26ad2008-09-19 03:17:36 -04001564 res = blkdev_ioctl(bdev, 0, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 set_fs(old_fs);
1566 return res;
1567}
1568
1569EXPORT_SYMBOL(ioctl_by_bdev);
1570
1571/**
1572 * lookup_bdev - lookup a struct block_device by name
Randy Dunlap94e29592009-01-06 14:41:15 -08001573 * @pathname: special file representing the block device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 *
Randy Dunlap57d1b532008-10-09 10:42:38 +02001575 * Get a reference to the blockdevice at @pathname in the current
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 * namespace if possible and return it. Return ERR_PTR(error)
1577 * otherwise.
1578 */
Al Viro421748e2008-08-02 01:04:36 -04001579struct block_device *lookup_bdev(const char *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
1581 struct block_device *bdev;
1582 struct inode *inode;
Al Viro421748e2008-08-02 01:04:36 -04001583 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 int error;
1585
Al Viro421748e2008-08-02 01:04:36 -04001586 if (!pathname || !*pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 return ERR_PTR(-EINVAL);
1588
Al Viro421748e2008-08-02 01:04:36 -04001589 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 if (error)
1591 return ERR_PTR(error);
1592
Al Viro421748e2008-08-02 01:04:36 -04001593 inode = path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 error = -ENOTBLK;
1595 if (!S_ISBLK(inode->i_mode))
1596 goto fail;
1597 error = -EACCES;
Al Viro421748e2008-08-02 01:04:36 -04001598 if (path.mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 goto fail;
1600 error = -ENOMEM;
1601 bdev = bd_acquire(inode);
1602 if (!bdev)
1603 goto fail;
1604out:
Al Viro421748e2008-08-02 01:04:36 -04001605 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 return bdev;
1607fail:
1608 bdev = ERR_PTR(error);
1609 goto out;
1610}
Al Virod5686b42008-08-01 05:00:11 -04001611EXPORT_SYMBOL(lookup_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
NeilBrown93b270f2011-02-24 17:25:47 +11001613int __invalidate_device(struct block_device *bdev, bool kill_dirty)
David Howellsb71e8a42006-08-29 19:06:11 +01001614{
1615 struct super_block *sb = get_super(bdev);
1616 int res = 0;
1617
1618 if (sb) {
1619 /*
1620 * no need to lock the super, get_super holds the
1621 * read mutex so the filesystem cannot go away
1622 * under us (->put_super runs with the write lock
1623 * hold).
1624 */
1625 shrink_dcache_sb(sb);
NeilBrown93b270f2011-02-24 17:25:47 +11001626 res = invalidate_inodes(sb, kill_dirty);
David Howellsb71e8a42006-08-29 19:06:11 +01001627 drop_super(sb);
1628 }
Peter Zijlstraf98393a2007-05-06 14:49:54 -07001629 invalidate_bdev(bdev);
David Howellsb71e8a42006-08-29 19:06:11 +01001630 return res;
1631}
1632EXPORT_SYMBOL(__invalidate_device);