blob: c62fb84944d51a18570b960a1b603f80b72d1afb [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
Josef Bacik06222e42011-07-18 13:21:38 -0400358 retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 switch (origin) {
Josef Bacik06222e42011-07-18 13:21:38 -0400360 case SEEK_END:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 offset += size;
362 break;
Josef Bacik06222e42011-07-18 13:21:38 -0400363 case SEEK_CUR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 offset += file->f_pos;
Josef Bacik06222e42011-07-18 13:21:38 -0400365 case SEEK_SET:
366 break;
367 default:
368 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (offset >= 0 && offset <= size) {
371 if (offset != file->f_pos) {
372 file->f_pos = offset;
373 }
374 retval = offset;
375 }
Josef Bacik06222e42011-07-18 13:21:38 -0400376out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800377 mutex_unlock(&bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return retval;
379}
380
Josef Bacik02c24a82011-07-16 20:44:56 -0400381int blkdev_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400383 struct inode *bd_inode = filp->f_mapping->host;
384 struct block_device *bdev = I_BDEV(bd_inode);
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100385 int error;
386
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400387 /*
388 * There is no need to serialise calls to blkdev_issue_flush with
389 * i_mutex and doing so causes performance issues with concurrent
390 * O_SYNC writers to a block device.
391 */
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200392 error = blkdev_issue_flush(bdev, GFP_KERNEL, NULL);
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100393 if (error == -EOPNOTSUPP)
394 error = 0;
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400395
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100396 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
Andrew Mortonb1dd3b22010-04-06 14:35:00 -0700398EXPORT_SYMBOL(blkdev_fsync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400/*
401 * pseudo-fs
402 */
403
404static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
Christoph Lametere18b8902006-12-06 20:33:20 -0800405static struct kmem_cache * bdev_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407static struct inode *bdev_alloc_inode(struct super_block *sb)
408{
Christoph Lametere94b1762006-12-06 20:33:17 -0800409 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (!ei)
411 return NULL;
412 return &ei->vfs_inode;
413}
414
Nick Pigginfa0d7e32011-01-07 17:49:49 +1100415static void bdev_i_callback(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Nick Pigginfa0d7e32011-01-07 17:49:49 +1100417 struct inode *inode = container_of(head, struct inode, i_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 struct bdev_inode *bdi = BDEV_I(inode);
419
Nick Pigginfa0d7e32011-01-07 17:49:49 +1100420 INIT_LIST_HEAD(&inode->i_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 kmem_cache_free(bdev_cachep, bdi);
422}
423
Nick Pigginfa0d7e32011-01-07 17:49:49 +1100424static void bdev_destroy_inode(struct inode *inode)
425{
426 call_rcu(&inode->i_rcu, bdev_i_callback);
427}
428
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700429static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 struct bdev_inode *ei = (struct bdev_inode *) foo;
432 struct block_device *bdev = &ei->bdev;
433
Christoph Lametera35afb82007-05-16 22:10:57 -0700434 memset(bdev, 0, sizeof(*bdev));
435 mutex_init(&bdev->bd_mutex);
Christoph Lametera35afb82007-05-16 22:10:57 -0700436 INIT_LIST_HEAD(&bdev->bd_inodes);
437 INIT_LIST_HEAD(&bdev->bd_list);
Tejun Heo49731ba2011-01-14 18:43:57 +0100438#ifdef CONFIG_SYSFS
439 INIT_LIST_HEAD(&bdev->bd_holder_disks);
440#endif
Christoph Lametera35afb82007-05-16 22:10:57 -0700441 inode_init_once(&ei->vfs_inode);
Takashi Satofcccf502009-01-09 16:40:59 -0800442 /* Initialize mutex for freeze. */
443 mutex_init(&bdev->bd_fsfreeze_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
446static inline void __bd_forget(struct inode *inode)
447{
448 list_del_init(&inode->i_devices);
449 inode->i_bdev = NULL;
450 inode->i_mapping = &inode->i_data;
451}
452
Al Virob57922d2010-06-07 14:34:48 -0400453static void bdev_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 struct block_device *bdev = &BDEV_I(inode)->bdev;
456 struct list_head *p;
Al Virob57922d2010-06-07 14:34:48 -0400457 truncate_inode_pages(&inode->i_data, 0);
458 invalidate_inode_buffers(inode); /* is it needed here? */
459 end_writeback(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 spin_lock(&bdev_lock);
461 while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
462 __bd_forget(list_entry(p, struct inode, i_devices));
463 }
464 list_del_init(&bdev->bd_list);
465 spin_unlock(&bdev_lock);
466}
467
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800468static const struct super_operations bdev_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 .statfs = simple_statfs,
470 .alloc_inode = bdev_alloc_inode,
471 .destroy_inode = bdev_destroy_inode,
472 .drop_inode = generic_delete_inode,
Al Virob57922d2010-06-07 14:34:48 -0400473 .evict_inode = bdev_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474};
475
Al Viro51139ad2010-07-25 23:47:46 +0400476static struct dentry *bd_mount(struct file_system_type *fs_type,
477 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Al Viroc74a1cb2011-01-12 16:59:34 -0500479 return mount_pseudo(fs_type, "bdev:", &bdev_sops, NULL, 0x62646576);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
482static struct file_system_type bd_type = {
483 .name = "bdev",
Al Viro51139ad2010-07-25 23:47:46 +0400484 .mount = bd_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 .kill_sb = kill_anon_super,
486};
487
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800488struct super_block *blockdev_superblock __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490void __init bdev_cache_init(void)
491{
492 int err;
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800493 struct vfsmount *bd_mnt;
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800496 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
497 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +0900498 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 err = register_filesystem(&bd_type);
500 if (err)
501 panic("Cannot register bdev pseudo-fs");
502 bd_mnt = kern_mount(&bd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (IS_ERR(bd_mnt))
504 panic("Cannot create bdev pseudo-fs");
Catalin Marinas2e1483c2009-06-11 13:24:13 +0100505 /*
506 * This vfsmount structure is only used to obtain the
507 * blockdev_superblock, so tell kmemleak not to report it.
508 */
509 kmemleak_not_leak(bd_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
511}
512
513/*
514 * Most likely _very_ bad one - but then it's hardly critical for small
515 * /dev and can be fixed when somebody will need really large one.
516 * Keep in mind that it will be fed through icache hash function too.
517 */
518static inline unsigned long hash(dev_t dev)
519{
520 return MAJOR(dev)+MINOR(dev);
521}
522
523static int bdev_test(struct inode *inode, void *data)
524{
525 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
526}
527
528static int bdev_set(struct inode *inode, void *data)
529{
530 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
531 return 0;
532}
533
534static LIST_HEAD(all_bdevs);
535
536struct block_device *bdget(dev_t dev)
537{
538 struct block_device *bdev;
539 struct inode *inode;
540
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800541 inode = iget5_locked(blockdev_superblock, hash(dev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 bdev_test, bdev_set, &dev);
543
544 if (!inode)
545 return NULL;
546
547 bdev = &BDEV_I(inode)->bdev;
548
549 if (inode->i_state & I_NEW) {
550 bdev->bd_contains = NULL;
551 bdev->bd_inode = inode;
552 bdev->bd_block_size = (1 << inode->i_blkbits);
553 bdev->bd_part_count = 0;
554 bdev->bd_invalidated = 0;
555 inode->i_mode = S_IFBLK;
556 inode->i_rdev = dev;
557 inode->i_bdev = bdev;
558 inode->i_data.a_ops = &def_blk_aops;
559 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
560 inode->i_data.backing_dev_info = &default_backing_dev_info;
561 spin_lock(&bdev_lock);
562 list_add(&bdev->bd_list, &all_bdevs);
563 spin_unlock(&bdev_lock);
564 unlock_new_inode(inode);
565 }
566 return bdev;
567}
568
569EXPORT_SYMBOL(bdget);
570
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200571/**
572 * bdgrab -- Grab a reference to an already referenced block device
573 * @bdev: Block device to grab a reference to.
574 */
575struct block_device *bdgrab(struct block_device *bdev)
576{
Al Viro7de9c6e2010-10-23 11:11:40 -0400577 ihold(bdev->bd_inode);
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200578 return bdev;
579}
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581long nr_blockdev_pages(void)
582{
Matthias Kaehlcke203a2932007-07-15 23:40:23 -0700583 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 long ret = 0;
585 spin_lock(&bdev_lock);
Matthias Kaehlcke203a2932007-07-15 23:40:23 -0700586 list_for_each_entry(bdev, &all_bdevs, bd_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 ret += bdev->bd_inode->i_mapping->nrpages;
588 }
589 spin_unlock(&bdev_lock);
590 return ret;
591}
592
593void bdput(struct block_device *bdev)
594{
595 iput(bdev->bd_inode);
596}
597
598EXPORT_SYMBOL(bdput);
599
600static struct block_device *bd_acquire(struct inode *inode)
601{
602 struct block_device *bdev;
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 spin_lock(&bdev_lock);
605 bdev = inode->i_bdev;
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700606 if (bdev) {
Al Viro7de9c6e2010-10-23 11:11:40 -0400607 ihold(bdev->bd_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 spin_unlock(&bdev_lock);
609 return bdev;
610 }
611 spin_unlock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 bdev = bdget(inode->i_rdev);
614 if (bdev) {
615 spin_lock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700616 if (!inode->i_bdev) {
617 /*
Al Viro7de9c6e2010-10-23 11:11:40 -0400618 * We take an additional reference to bd_inode,
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700619 * and it's released in clear_inode() of inode.
620 * So, we can access it via ->i_mapping always
621 * without igrab().
622 */
Al Viro7de9c6e2010-10-23 11:11:40 -0400623 ihold(bdev->bd_inode);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700624 inode->i_bdev = bdev;
625 inode->i_mapping = bdev->bd_inode->i_mapping;
626 list_add(&inode->i_devices, &bdev->bd_inodes);
627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 spin_unlock(&bdev_lock);
629 }
630 return bdev;
631}
632
633/* Call when you free inode */
634
635void bd_forget(struct inode *inode)
636{
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700637 struct block_device *bdev = NULL;
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 spin_lock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700640 if (inode->i_bdev) {
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800641 if (!sb_is_blkdev_sb(inode->i_sb))
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700642 bdev = inode->i_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 __bd_forget(inode);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 spin_unlock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700646
647 if (bdev)
648 iput(bdev->bd_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900651/**
652 * bd_may_claim - test whether a block device can be claimed
653 * @bdev: block device of interest
654 * @whole: whole block device containing @bdev, may equal @bdev
655 * @holder: holder trying to claim @bdev
656 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300657 * Test whether @bdev can be claimed by @holder.
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900658 *
659 * CONTEXT:
660 * spin_lock(&bdev_lock).
661 *
662 * RETURNS:
663 * %true if @bdev can be claimed, %false otherwise.
664 */
665static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
666 void *holder)
667{
668 if (bdev->bd_holder == holder)
669 return true; /* already a holder */
670 else if (bdev->bd_holder != NULL)
671 return false; /* held by someone else */
672 else if (bdev->bd_contains == bdev)
673 return true; /* is a whole device which isn't held */
674
Tejun Heoe525fd82010-11-13 11:55:17 +0100675 else if (whole->bd_holder == bd_may_claim)
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900676 return true; /* is a partition of a device that is being partitioned */
677 else if (whole->bd_holder != NULL)
678 return false; /* is a partition of a held device */
679 else
680 return true; /* is a partition of an un-held device */
681}
682
683/**
Tejun Heo6b4517a2010-04-07 18:53:59 +0900684 * bd_prepare_to_claim - prepare to claim a block device
685 * @bdev: block device of interest
686 * @whole: the whole device containing @bdev, may equal @bdev
687 * @holder: holder trying to claim @bdev
688 *
689 * Prepare to claim @bdev. This function fails if @bdev is already
690 * claimed by another holder and waits if another claiming is in
691 * progress. This function doesn't actually claim. On successful
692 * return, the caller has ownership of bd_claiming and bd_holder[s].
693 *
694 * CONTEXT:
695 * spin_lock(&bdev_lock). Might release bdev_lock, sleep and regrab
696 * it multiple times.
697 *
698 * RETURNS:
699 * 0 if @bdev can be claimed, -EBUSY otherwise.
700 */
701static int bd_prepare_to_claim(struct block_device *bdev,
702 struct block_device *whole, void *holder)
703{
704retry:
705 /* if someone else claimed, fail */
706 if (!bd_may_claim(bdev, whole, holder))
707 return -EBUSY;
708
Tejun Heoe75aa852010-08-04 17:59:39 +0200709 /* if claiming is already in progress, wait for it to finish */
710 if (whole->bd_claiming) {
Tejun Heo6b4517a2010-04-07 18:53:59 +0900711 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
712 DEFINE_WAIT(wait);
713
714 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
715 spin_unlock(&bdev_lock);
716 schedule();
717 finish_wait(wq, &wait);
718 spin_lock(&bdev_lock);
719 goto retry;
720 }
721
722 /* yay, all mine */
723 return 0;
724}
725
726/**
727 * bd_start_claiming - start claiming a block device
728 * @bdev: block device of interest
729 * @holder: holder trying to claim @bdev
730 *
731 * @bdev is about to be opened exclusively. Check @bdev can be opened
732 * exclusively and mark that an exclusive open is in progress. Each
733 * successful call to this function must be matched with a call to
Nick Pigginb0018362010-05-26 01:51:19 +1000734 * either bd_finish_claiming() or bd_abort_claiming() (which do not
735 * fail).
736 *
737 * This function is used to gain exclusive access to the block device
738 * without actually causing other exclusive open attempts to fail. It
739 * should be used when the open sequence itself requires exclusive
740 * access but may subsequently fail.
Tejun Heo6b4517a2010-04-07 18:53:59 +0900741 *
742 * CONTEXT:
743 * Might sleep.
744 *
745 * RETURNS:
746 * Pointer to the block device containing @bdev on success, ERR_PTR()
747 * value on failure.
748 */
749static struct block_device *bd_start_claiming(struct block_device *bdev,
750 void *holder)
751{
752 struct gendisk *disk;
753 struct block_device *whole;
754 int partno, err;
755
756 might_sleep();
757
758 /*
759 * @bdev might not have been initialized properly yet, look up
760 * and grab the outer block device the hard way.
761 */
762 disk = get_gendisk(bdev->bd_dev, &partno);
763 if (!disk)
764 return ERR_PTR(-ENXIO);
765
Tejun Heod4c208b2011-06-13 12:45:48 +0200766 /*
767 * Normally, @bdev should equal what's returned from bdget_disk()
768 * if partno is 0; however, some drivers (floppy) use multiple
769 * bdev's for the same physical device and @bdev may be one of the
770 * aliases. Keep @bdev if partno is 0. This means claimer
771 * tracking is broken for those devices but it has always been that
772 * way.
773 */
774 if (partno)
775 whole = bdget_disk(disk, 0);
776 else
777 whole = bdgrab(bdev);
778
Nick Piggincf342572010-05-26 01:50:21 +1000779 module_put(disk->fops->owner);
Tejun Heo6b4517a2010-04-07 18:53:59 +0900780 put_disk(disk);
781 if (!whole)
782 return ERR_PTR(-ENOMEM);
783
784 /* prepare to claim, if successful, mark claiming in progress */
785 spin_lock(&bdev_lock);
786
787 err = bd_prepare_to_claim(bdev, whole, holder);
788 if (err == 0) {
789 whole->bd_claiming = holder;
790 spin_unlock(&bdev_lock);
791 return whole;
792 } else {
793 spin_unlock(&bdev_lock);
794 bdput(whole);
795 return ERR_PTR(err);
796 }
797}
798
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800799#ifdef CONFIG_SYSFS
Tejun Heo49731ba2011-01-14 18:43:57 +0100800struct bd_holder_disk {
801 struct list_head list;
802 struct gendisk *disk;
803 int refcnt;
804};
805
806static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev,
807 struct gendisk *disk)
808{
809 struct bd_holder_disk *holder;
810
811 list_for_each_entry(holder, &bdev->bd_holder_disks, list)
812 if (holder->disk == disk)
813 return holder;
814 return NULL;
815}
816
Andrew Morton4d7dd8f2006-09-29 01:58:56 -0700817static int add_symlink(struct kobject *from, struct kobject *to)
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800818{
Andrew Morton4d7dd8f2006-09-29 01:58:56 -0700819 return sysfs_create_link(from, to, kobject_name(to));
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800820}
821
822static void del_symlink(struct kobject *from, struct kobject *to)
823{
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800824 sysfs_remove_link(from, kobject_name(to));
825}
826
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800827/**
Tejun Heoe09b4572010-11-13 11:55:17 +0100828 * bd_link_disk_holder - create symlinks between holding disk and slave bdev
829 * @bdev: the claimed slave bdev
830 * @disk: the holding disk
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500831 *
Tejun Heo49731ba2011-01-14 18:43:57 +0100832 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
833 *
Tejun Heoe09b4572010-11-13 11:55:17 +0100834 * This functions creates the following sysfs symlinks.
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500835 *
Tejun Heoe09b4572010-11-13 11:55:17 +0100836 * - from "slaves" directory of the holder @disk to the claimed @bdev
837 * - from "holders" directory of the @bdev to the holder @disk
838 *
839 * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
840 * passed to bd_link_disk_holder(), then:
841 *
842 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
843 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
844 *
845 * The caller must have claimed @bdev before calling this function and
846 * ensure that both @bdev and @disk are valid during the creation and
847 * lifetime of these symlinks.
848 *
849 * CONTEXT:
850 * Might sleep.
851 *
852 * RETURNS:
853 * 0 on success, -errno on failure.
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500854 */
Tejun Heoe09b4572010-11-13 11:55:17 +0100855int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500856{
Tejun Heo49731ba2011-01-14 18:43:57 +0100857 struct bd_holder_disk *holder;
Tejun Heoe09b4572010-11-13 11:55:17 +0100858 int ret = 0;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800859
Peter Zijlstra2e7b6512006-12-08 02:36:13 -0800860 mutex_lock(&bdev->bd_mutex);
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500861
Tejun Heo49731ba2011-01-14 18:43:57 +0100862 WARN_ON_ONCE(!bdev->bd_holder);
Johannes Weiner4e916722007-07-15 23:41:25 -0700863
Tejun Heoe09b4572010-11-13 11:55:17 +0100864 /* FIXME: remove the following once add_disk() handles errors */
865 if (WARN_ON(!disk->slave_dir || !bdev->bd_part->holder_dir))
866 goto out_unlock;
Johannes Weiner4e916722007-07-15 23:41:25 -0700867
Tejun Heo49731ba2011-01-14 18:43:57 +0100868 holder = bd_find_holder_disk(bdev, disk);
869 if (holder) {
870 holder->refcnt++;
Tejun Heoe09b4572010-11-13 11:55:17 +0100871 goto out_unlock;
872 }
873
Tejun Heo49731ba2011-01-14 18:43:57 +0100874 holder = kzalloc(sizeof(*holder), GFP_KERNEL);
875 if (!holder) {
876 ret = -ENOMEM;
877 goto out_unlock;
878 }
879
880 INIT_LIST_HEAD(&holder->list);
881 holder->disk = disk;
882 holder->refcnt = 1;
883
884 ret = add_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
885 if (ret)
886 goto out_free;
887
888 ret = add_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj);
889 if (ret)
890 goto out_del;
Tejun Heoe7407d12011-02-24 09:56:32 +0100891 /*
892 * bdev could be deleted beneath us which would implicitly destroy
893 * the holder directory. Hold on to it.
894 */
895 kobject_get(bdev->bd_part->holder_dir);
Tejun Heo49731ba2011-01-14 18:43:57 +0100896
897 list_add(&holder->list, &bdev->bd_holder_disks);
898 goto out_unlock;
899
900out_del:
901 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
902out_free:
903 kfree(holder);
Tejun Heoe09b4572010-11-13 11:55:17 +0100904out_unlock:
Jun'ichi Nomurab4cf1b72006-03-27 01:18:00 -0800905 mutex_unlock(&bdev->bd_mutex);
Tejun Heoe09b4572010-11-13 11:55:17 +0100906 return ret;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800907}
Tejun Heoe09b4572010-11-13 11:55:17 +0100908EXPORT_SYMBOL_GPL(bd_link_disk_holder);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800909
Tejun Heo49731ba2011-01-14 18:43:57 +0100910/**
911 * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
912 * @bdev: the calimed slave bdev
913 * @disk: the holding disk
914 *
915 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
916 *
917 * CONTEXT:
918 * Might sleep.
919 */
920void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800921{
Tejun Heo49731ba2011-01-14 18:43:57 +0100922 struct bd_holder_disk *holder;
Tejun Heoe09b4572010-11-13 11:55:17 +0100923
Tejun Heo49731ba2011-01-14 18:43:57 +0100924 mutex_lock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800925
Tejun Heo49731ba2011-01-14 18:43:57 +0100926 holder = bd_find_holder_disk(bdev, disk);
927
928 if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
929 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
930 del_symlink(bdev->bd_part->holder_dir,
931 &disk_to_dev(disk)->kobj);
Tejun Heoe7407d12011-02-24 09:56:32 +0100932 kobject_put(bdev->bd_part->holder_dir);
Tejun Heo49731ba2011-01-14 18:43:57 +0100933 list_del_init(&holder->list);
934 kfree(holder);
935 }
936
937 mutex_unlock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800938}
Tejun Heo49731ba2011-01-14 18:43:57 +0100939EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800940#endif
941
Andrew Patterson0c002c22008-09-04 14:27:20 -0600942/**
Andrew Patterson56ade442008-09-04 14:27:40 -0600943 * flush_disk - invalidates all buffer-cache entries on a disk
944 *
945 * @bdev: struct block device to be flushed
Randy Dunlape6eb5ce2011-02-26 10:54:00 -0800946 * @kill_dirty: flag to guide handling of dirty inodes
Andrew Patterson56ade442008-09-04 14:27:40 -0600947 *
948 * Invalidates all buffer-cache entries on a disk. It should be called
949 * when a disk has been changed -- either by a media change or online
950 * resize.
951 */
NeilBrown93b270f2011-02-24 17:25:47 +1100952static void flush_disk(struct block_device *bdev, bool kill_dirty)
Andrew Patterson56ade442008-09-04 14:27:40 -0600953{
NeilBrown93b270f2011-02-24 17:25:47 +1100954 if (__invalidate_device(bdev, kill_dirty)) {
Andrew Patterson56ade442008-09-04 14:27:40 -0600955 char name[BDEVNAME_SIZE] = "";
956
957 if (bdev->bd_disk)
958 disk_name(bdev->bd_disk, 0, name);
959 printk(KERN_WARNING "VFS: busy inodes on changed media or "
960 "resized disk %s\n", name);
961 }
962
963 if (!bdev->bd_disk)
964 return;
965 if (disk_partitionable(bdev->bd_disk))
966 bdev->bd_invalidated = 1;
967}
968
969/**
Randy Dunlap57d1b532008-10-09 10:42:38 +0200970 * check_disk_size_change - checks for disk size change and adjusts bdev size.
Andrew Pattersonc3279d12008-09-04 14:27:25 -0600971 * @disk: struct gendisk to check
972 * @bdev: struct bdev to adjust.
973 *
974 * This routine checks to see if the bdev size does not match the disk size
975 * and adjusts it if it differs.
976 */
977void check_disk_size_change(struct gendisk *disk, struct block_device *bdev)
978{
979 loff_t disk_size, bdev_size;
980
981 disk_size = (loff_t)get_capacity(disk) << 9;
982 bdev_size = i_size_read(bdev->bd_inode);
983 if (disk_size != bdev_size) {
984 char name[BDEVNAME_SIZE];
985
986 disk_name(disk, 0, name);
987 printk(KERN_INFO
988 "%s: detected capacity change from %lld to %lld\n",
989 name, bdev_size, disk_size);
990 i_size_write(bdev->bd_inode, disk_size);
NeilBrown93b270f2011-02-24 17:25:47 +1100991 flush_disk(bdev, false);
Andrew Pattersonc3279d12008-09-04 14:27:25 -0600992 }
993}
994EXPORT_SYMBOL(check_disk_size_change);
995
996/**
Randy Dunlap57d1b532008-10-09 10:42:38 +0200997 * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back
Andrew Patterson0c002c22008-09-04 14:27:20 -0600998 * @disk: struct gendisk to be revalidated
999 *
1000 * This routine is a wrapper for lower-level driver's revalidate_disk
1001 * call-backs. It is used to do common pre and post operations needed
1002 * for all revalidate_disk operations.
1003 */
1004int revalidate_disk(struct gendisk *disk)
1005{
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001006 struct block_device *bdev;
Andrew Patterson0c002c22008-09-04 14:27:20 -06001007 int ret = 0;
1008
1009 if (disk->fops->revalidate_disk)
1010 ret = disk->fops->revalidate_disk(disk);
1011
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001012 bdev = bdget_disk(disk, 0);
1013 if (!bdev)
1014 return ret;
1015
1016 mutex_lock(&bdev->bd_mutex);
1017 check_disk_size_change(disk, bdev);
1018 mutex_unlock(&bdev->bd_mutex);
1019 bdput(bdev);
Andrew Patterson0c002c22008-09-04 14:27:20 -06001020 return ret;
1021}
1022EXPORT_SYMBOL(revalidate_disk);
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024/*
1025 * This routine checks whether a removable media has been changed,
1026 * and invalidates all buffer-cache-entries in that case. This
1027 * is a relatively slow routine, so we have to try to minimize using
1028 * it. Thus it is called only upon a 'mount' or 'open'. This
1029 * is the best way of combining speed and utility, I think.
1030 * People changing diskettes in the middle of an operation deserve
1031 * to lose :-)
1032 */
1033int check_disk_change(struct block_device *bdev)
1034{
1035 struct gendisk *disk = bdev->bd_disk;
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001036 const struct block_device_operations *bdops = disk->fops;
Tejun Heo77ea8872010-12-08 20:57:37 +01001037 unsigned int events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Tejun Heo77ea8872010-12-08 20:57:37 +01001039 events = disk_clear_events(disk, DISK_EVENT_MEDIA_CHANGE |
1040 DISK_EVENT_EJECT_REQUEST);
1041 if (!(events & DISK_EVENT_MEDIA_CHANGE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 return 0;
1043
NeilBrown93b270f2011-02-24 17:25:47 +11001044 flush_disk(bdev, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (bdops->revalidate_disk)
1046 bdops->revalidate_disk(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return 1;
1048}
1049
1050EXPORT_SYMBOL(check_disk_change);
1051
1052void bd_set_size(struct block_device *bdev, loff_t size)
1053{
Martin K. Petersene1defc42009-05-22 17:17:49 -04001054 unsigned bsize = bdev_logical_block_size(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 bdev->bd_inode->i_size = size;
1057 while (bsize < PAGE_CACHE_SIZE) {
1058 if (size & bsize)
1059 break;
1060 bsize <<= 1;
1061 }
1062 bdev->bd_block_size = bsize;
1063 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
1064}
1065EXPORT_SYMBOL(bd_set_size);
1066
Al Viro9a1c3542008-02-22 20:40:24 -05001067static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
NeilBrown37be4122006-12-08 02:36:16 -08001068
Peter Zijlstra6d740cd2007-02-20 13:58:18 -08001069/*
1070 * bd_mutex locking:
1071 *
1072 * mutex_lock(part->bd_mutex)
1073 * mutex_lock_nested(whole->bd_mutex, 1)
1074 */
1075
Al Viro572c4892007-10-08 13:24:05 -04001076static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 struct gendisk *disk;
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -07001079 int ret;
Tejun Heocf771cb2008-09-03 09:01:09 +02001080 int partno;
Al Virofe6e9c12008-06-23 08:30:55 -04001081 int perm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Al Viro572c4892007-10-08 13:24:05 -04001083 if (mode & FMODE_READ)
Al Virofe6e9c12008-06-23 08:30:55 -04001084 perm |= MAY_READ;
Al Viro572c4892007-10-08 13:24:05 -04001085 if (mode & FMODE_WRITE)
Al Virofe6e9c12008-06-23 08:30:55 -04001086 perm |= MAY_WRITE;
1087 /*
1088 * hooks: /n/, see "layering violations".
1089 */
Chris Wrightb7300b72010-08-10 18:02:55 -07001090 if (!for_part) {
1091 ret = devcgroup_inode_permission(bdev->bd_inode, perm);
1092 if (ret != 0) {
1093 bdput(bdev);
1094 return ret;
1095 }
Al Viro82666022008-08-01 05:32:04 -04001096 }
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -07001097
NeilBrownd3374822009-01-09 08:31:10 +11001098 restart:
Tejun Heo0762b8b2008-08-25 19:56:12 +09001099
Tejun Heo89f97492008-11-05 10:21:06 +01001100 ret = -ENXIO;
Tejun Heocf771cb2008-09-03 09:01:09 +02001101 disk = get_gendisk(bdev->bd_dev, &partno);
Tejun Heo0762b8b2008-08-25 19:56:12 +09001102 if (!disk)
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001103 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Tejun Heo69e02c52011-03-09 19:54:27 +01001105 disk_block_events(disk);
NeilBrown6796bf52006-12-08 02:36:16 -08001106 mutex_lock_nested(&bdev->bd_mutex, for_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (!bdev->bd_openers) {
1108 bdev->bd_disk = disk;
1109 bdev->bd_contains = bdev;
Tejun Heocf771cb2008-09-03 09:01:09 +02001110 if (!partno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 struct backing_dev_info *bdi;
Tejun Heo89f97492008-11-05 10:21:06 +01001112
1113 ret = -ENXIO;
1114 bdev->bd_part = disk_get_part(disk, partno);
1115 if (!bdev->bd_part)
1116 goto out_clear;
1117
Tejun Heo1196f8b2011-04-21 20:54:45 +02001118 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (disk->fops->open) {
Al Viro572c4892007-10-08 13:24:05 -04001120 ret = disk->fops->open(bdev, mode);
NeilBrownd3374822009-01-09 08:31:10 +11001121 if (ret == -ERESTARTSYS) {
1122 /* Lost a race with 'disk' being
1123 * deleted, try again.
1124 * See md.c
1125 */
1126 disk_put_part(bdev->bd_part);
1127 bdev->bd_part = NULL;
NeilBrownd3374822009-01-09 08:31:10 +11001128 bdev->bd_disk = NULL;
1129 mutex_unlock(&bdev->bd_mutex);
Tejun Heo69e02c52011-03-09 19:54:27 +01001130 disk_unblock_events(disk);
1131 module_put(disk->fops->owner);
1132 put_disk(disk);
NeilBrownd3374822009-01-09 08:31:10 +11001133 goto restart;
1134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
Tejun Heo7e697232011-05-23 13:26:07 +02001136
1137 if (!ret && !bdev->bd_openers) {
1138 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
1139 bdi = blk_get_backing_dev_info(bdev);
1140 if (bdi == NULL)
1141 bdi = &default_backing_dev_info;
1142 bdev_inode_switch_bdi(bdev->bd_inode, bdi);
1143 }
1144
Tejun Heo1196f8b2011-04-21 20:54:45 +02001145 /*
1146 * If the device is invalidated, rescan partition
1147 * if open succeeded or failed with -ENOMEDIUM.
1148 * The latter is necessary to prevent ghost
1149 * partitions on a removed medium.
1150 */
1151 if (bdev->bd_invalidated && (!ret || ret == -ENOMEDIUM))
1152 rescan_partitions(disk, bdev);
1153 if (ret)
1154 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 struct block_device *whole;
1157 whole = bdget_disk(disk, 0);
1158 ret = -ENOMEM;
1159 if (!whole)
Tejun Heo0762b8b2008-08-25 19:56:12 +09001160 goto out_clear;
NeilBrown37be4122006-12-08 02:36:16 -08001161 BUG_ON(for_part);
Al Viro572c4892007-10-08 13:24:05 -04001162 ret = __blkdev_get(whole, mode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 if (ret)
Tejun Heo0762b8b2008-08-25 19:56:12 +09001164 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 bdev->bd_contains = whole;
Dave Chinnera5491e02010-10-21 11:49:26 +11001166 bdev_inode_switch_bdi(bdev->bd_inode,
1167 whole->bd_inode->i_data.backing_dev_info);
Tejun Heo89f97492008-11-05 10:21:06 +01001168 bdev->bd_part = disk_get_part(disk, partno);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001169 if (!(disk->flags & GENHD_FL_UP) ||
Tejun Heo89f97492008-11-05 10:21:06 +01001170 !bdev->bd_part || !bdev->bd_part->nr_sects) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 ret = -ENXIO;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001172 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
Tejun Heo89f97492008-11-05 10:21:06 +01001174 bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
1176 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (bdev->bd_contains == bdev) {
Tejun Heo1196f8b2011-04-21 20:54:45 +02001178 ret = 0;
1179 if (bdev->bd_disk->fops->open)
Al Viro572c4892007-10-08 13:24:05 -04001180 ret = bdev->bd_disk->fops->open(bdev, mode);
Tejun Heo1196f8b2011-04-21 20:54:45 +02001181 /* the same as first opener case, read comment there */
1182 if (bdev->bd_invalidated && (!ret || ret == -ENOMEDIUM))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 rescan_partitions(bdev->bd_disk, bdev);
Tejun Heo1196f8b2011-04-21 20:54:45 +02001184 if (ret)
1185 goto out_unlock_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
Tejun Heo69e02c52011-03-09 19:54:27 +01001187 /* only one opener holds refs to the module and disk */
1188 module_put(disk->fops->owner);
1189 put_disk(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 }
1191 bdev->bd_openers++;
NeilBrown37be4122006-12-08 02:36:16 -08001192 if (for_part)
1193 bdev->bd_part_count++;
Arjan van de Venc039e312006-03-23 03:00:28 -08001194 mutex_unlock(&bdev->bd_mutex);
Tejun Heo69e02c52011-03-09 19:54:27 +01001195 disk_unblock_events(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return 0;
1197
Tejun Heo0762b8b2008-08-25 19:56:12 +09001198 out_clear:
Tejun Heo89f97492008-11-05 10:21:06 +01001199 disk_put_part(bdev->bd_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 bdev->bd_disk = NULL;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001201 bdev->bd_part = NULL;
Dave Chinnera5491e02010-10-21 11:49:26 +11001202 bdev_inode_switch_bdi(bdev->bd_inode, &default_backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 if (bdev != bdev->bd_contains)
Al Viro572c4892007-10-08 13:24:05 -04001204 __blkdev_put(bdev->bd_contains, mode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 bdev->bd_contains = NULL;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001206 out_unlock_bdev:
Arjan van de Venc039e312006-03-23 03:00:28 -08001207 mutex_unlock(&bdev->bd_mutex);
Tejun Heo69e02c52011-03-09 19:54:27 +01001208 disk_unblock_events(disk);
Tejun Heo69e02c52011-03-09 19:54:27 +01001209 module_put(disk->fops->owner);
Tejun Heo0762b8b2008-08-25 19:56:12 +09001210 put_disk(disk);
Dan Carpenter4345cab2011-03-19 13:53:31 +01001211 out:
Tejun Heo0762b8b2008-08-25 19:56:12 +09001212 bdput(bdev);
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 return ret;
1215}
1216
Tejun Heod4d77622010-11-13 11:55:18 +01001217/**
1218 * blkdev_get - open a block device
1219 * @bdev: block_device to open
1220 * @mode: FMODE_* mask
1221 * @holder: exclusive holder identifier
1222 *
1223 * Open @bdev with @mode. If @mode includes %FMODE_EXCL, @bdev is
1224 * open with exclusive access. Specifying %FMODE_EXCL with %NULL
1225 * @holder is invalid. Exclusive opens may nest for the same @holder.
1226 *
1227 * On success, the reference count of @bdev is unchanged. On failure,
1228 * @bdev is put.
1229 *
1230 * CONTEXT:
1231 * Might sleep.
1232 *
1233 * RETURNS:
1234 * 0 on success, -errno on failure.
1235 */
Tejun Heoe525fd82010-11-13 11:55:17 +01001236int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
Tejun Heoe525fd82010-11-13 11:55:17 +01001238 struct block_device *whole = NULL;
1239 int res;
1240
1241 WARN_ON_ONCE((mode & FMODE_EXCL) && !holder);
1242
1243 if ((mode & FMODE_EXCL) && holder) {
1244 whole = bd_start_claiming(bdev, holder);
1245 if (IS_ERR(whole)) {
1246 bdput(bdev);
1247 return PTR_ERR(whole);
1248 }
1249 }
1250
1251 res = __blkdev_get(bdev, mode, 0);
1252
1253 if (whole) {
Tejun Heod4dc2102011-04-21 20:54:46 +02001254 struct gendisk *disk = whole->bd_disk;
1255
Tejun Heo6a027ef2010-11-13 11:55:17 +01001256 /* finish claiming */
Tejun Heo77ea8872010-12-08 20:57:37 +01001257 mutex_lock(&bdev->bd_mutex);
Tejun Heo6a027ef2010-11-13 11:55:17 +01001258 spin_lock(&bdev_lock);
1259
Tejun Heo77ea8872010-12-08 20:57:37 +01001260 if (!res) {
Tejun Heo6a027ef2010-11-13 11:55:17 +01001261 BUG_ON(!bd_may_claim(bdev, whole, holder));
1262 /*
1263 * Note that for a whole device bd_holders
1264 * will be incremented twice, and bd_holder
1265 * will be set to bd_may_claim before being
1266 * set to holder
1267 */
1268 whole->bd_holders++;
1269 whole->bd_holder = bd_may_claim;
1270 bdev->bd_holders++;
1271 bdev->bd_holder = holder;
1272 }
1273
1274 /* tell others that we're done */
1275 BUG_ON(whole->bd_claiming != holder);
1276 whole->bd_claiming = NULL;
1277 wake_up_bit(&whole->bd_claiming, 0);
1278
1279 spin_unlock(&bdev_lock);
Tejun Heo77ea8872010-12-08 20:57:37 +01001280
1281 /*
Tejun Heod4dc2102011-04-21 20:54:46 +02001282 * Block event polling for write claims if requested. Any
1283 * write holder makes the write_holder state stick until
1284 * all are released. This is good enough and tracking
1285 * individual writeable reference is too fragile given the
1286 * way @mode is used in blkdev_get/put().
Tejun Heo77ea8872010-12-08 20:57:37 +01001287 */
Tejun Heo4c49ff32011-06-01 08:27:41 +02001288 if (!res && (mode & FMODE_WRITE) && !bdev->bd_write_holder &&
1289 (disk->flags & GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE)) {
Tejun Heo77ea8872010-12-08 20:57:37 +01001290 bdev->bd_write_holder = true;
Tejun Heod4dc2102011-04-21 20:54:46 +02001291 disk_block_events(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01001292 }
1293
1294 mutex_unlock(&bdev->bd_mutex);
Tejun Heo6a027ef2010-11-13 11:55:17 +01001295 bdput(whole);
Tejun Heoe525fd82010-11-13 11:55:17 +01001296 }
1297
1298 return res;
NeilBrown37be4122006-12-08 02:36:16 -08001299}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300EXPORT_SYMBOL(blkdev_get);
1301
Tejun Heod4d77622010-11-13 11:55:18 +01001302/**
1303 * blkdev_get_by_path - open a block device by name
1304 * @path: path to the block device to open
1305 * @mode: FMODE_* mask
1306 * @holder: exclusive holder identifier
1307 *
1308 * Open the blockdevice described by the device file at @path. @mode
1309 * and @holder are identical to blkdev_get().
1310 *
1311 * On success, the returned block_device has reference count of one.
1312 *
1313 * CONTEXT:
1314 * Might sleep.
1315 *
1316 * RETURNS:
1317 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1318 */
1319struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
1320 void *holder)
1321{
1322 struct block_device *bdev;
1323 int err;
1324
1325 bdev = lookup_bdev(path);
1326 if (IS_ERR(bdev))
1327 return bdev;
1328
1329 err = blkdev_get(bdev, mode, holder);
1330 if (err)
1331 return ERR_PTR(err);
1332
Chuck Ebberte51900f2011-02-16 18:11:53 -05001333 if ((mode & FMODE_WRITE) && bdev_read_only(bdev)) {
1334 blkdev_put(bdev, mode);
1335 return ERR_PTR(-EACCES);
1336 }
1337
Tejun Heod4d77622010-11-13 11:55:18 +01001338 return bdev;
1339}
1340EXPORT_SYMBOL(blkdev_get_by_path);
1341
1342/**
1343 * blkdev_get_by_dev - open a block device by device number
1344 * @dev: device number of block device to open
1345 * @mode: FMODE_* mask
1346 * @holder: exclusive holder identifier
1347 *
1348 * Open the blockdevice described by device number @dev. @mode and
1349 * @holder are identical to blkdev_get().
1350 *
1351 * Use it ONLY if you really do not have anything better - i.e. when
1352 * you are behind a truly sucky interface and all you are given is a
1353 * device number. _Never_ to be used for internal purposes. If you
1354 * ever need it - reconsider your API.
1355 *
1356 * On success, the returned block_device has reference count of one.
1357 *
1358 * CONTEXT:
1359 * Might sleep.
1360 *
1361 * RETURNS:
1362 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1363 */
1364struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
1365{
1366 struct block_device *bdev;
1367 int err;
1368
1369 bdev = bdget(dev);
1370 if (!bdev)
1371 return ERR_PTR(-ENOMEM);
1372
1373 err = blkdev_get(bdev, mode, holder);
1374 if (err)
1375 return ERR_PTR(err);
1376
1377 return bdev;
1378}
1379EXPORT_SYMBOL(blkdev_get_by_dev);
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381static int blkdev_open(struct inode * inode, struct file * filp)
1382{
1383 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 /*
1386 * Preserve backwards compatibility and allow large file access
1387 * even if userspace doesn't ask for it explicitly. Some mkfs
1388 * binary needs it. We might want to drop this workaround
1389 * during an unstable branch.
1390 */
1391 filp->f_flags |= O_LARGEFILE;
1392
Al Viro572c4892007-10-08 13:24:05 -04001393 if (filp->f_flags & O_NDELAY)
1394 filp->f_mode |= FMODE_NDELAY;
1395 if (filp->f_flags & O_EXCL)
1396 filp->f_mode |= FMODE_EXCL;
1397 if ((filp->f_flags & O_ACCMODE) == 3)
1398 filp->f_mode |= FMODE_WRITE_IOCTL;
1399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 bdev = bd_acquire(inode);
Pavel Emelianov6a2aae02006-10-28 10:38:33 -07001401 if (bdev == NULL)
1402 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Al Viro572c4892007-10-08 13:24:05 -04001404 filp->f_mapping = bdev->bd_inode->i_mapping;
1405
Tejun Heoe525fd82010-11-13 11:55:17 +01001406 return blkdev_get(bdev, filp->f_mode, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407}
1408
Al Viro9a1c3542008-02-22 20:40:24 -05001409static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001410{
1411 int ret = 0;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001412 struct gendisk *disk = bdev->bd_disk;
NeilBrown37be4122006-12-08 02:36:16 -08001413 struct block_device *victim = NULL;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001414
NeilBrown6796bf52006-12-08 02:36:16 -08001415 mutex_lock_nested(&bdev->bd_mutex, for_part);
NeilBrown37be4122006-12-08 02:36:16 -08001416 if (for_part)
1417 bdev->bd_part_count--;
1418
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001419 if (!--bdev->bd_openers) {
Tejun Heo6a027ef2010-11-13 11:55:17 +01001420 WARN_ON_ONCE(bdev->bd_holders);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001421 sync_blockdev(bdev);
1422 kill_bdev(bdev);
1423 }
1424 if (bdev->bd_contains == bdev) {
1425 if (disk->fops->release)
Al Viro9a1c3542008-02-22 20:40:24 -05001426 ret = disk->fops->release(disk, mode);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001427 }
1428 if (!bdev->bd_openers) {
1429 struct module *owner = disk->fops->owner;
1430
1431 put_disk(disk);
1432 module_put(owner);
Tejun Heo0762b8b2008-08-25 19:56:12 +09001433 disk_put_part(bdev->bd_part);
1434 bdev->bd_part = NULL;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001435 bdev->bd_disk = NULL;
Dave Chinnera5491e02010-10-21 11:49:26 +11001436 bdev_inode_switch_bdi(bdev->bd_inode,
1437 &default_backing_dev_info);
NeilBrown37be4122006-12-08 02:36:16 -08001438 if (bdev != bdev->bd_contains)
1439 victim = bdev->bd_contains;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001440 bdev->bd_contains = NULL;
1441 }
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001442 mutex_unlock(&bdev->bd_mutex);
1443 bdput(bdev);
NeilBrown37be4122006-12-08 02:36:16 -08001444 if (victim)
Al Viro9a1c3542008-02-22 20:40:24 -05001445 __blkdev_put(victim, mode, 1);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001446 return ret;
1447}
1448
Al Viro9a1c3542008-02-22 20:40:24 -05001449int blkdev_put(struct block_device *bdev, fmode_t mode)
NeilBrown37be4122006-12-08 02:36:16 -08001450{
Tejun Heo85ef06d2011-07-01 16:17:47 +02001451 mutex_lock(&bdev->bd_mutex);
1452
Tejun Heoe525fd82010-11-13 11:55:17 +01001453 if (mode & FMODE_EXCL) {
Tejun Heo6a027ef2010-11-13 11:55:17 +01001454 bool bdev_free;
1455
1456 /*
1457 * Release a claim on the device. The holder fields
1458 * are protected with bdev_lock. bd_mutex is to
1459 * synchronize disk_holder unlinking.
1460 */
Tejun Heo6a027ef2010-11-13 11:55:17 +01001461 spin_lock(&bdev_lock);
1462
1463 WARN_ON_ONCE(--bdev->bd_holders < 0);
1464 WARN_ON_ONCE(--bdev->bd_contains->bd_holders < 0);
1465
1466 /* bd_contains might point to self, check in a separate step */
1467 if ((bdev_free = !bdev->bd_holders))
1468 bdev->bd_holder = NULL;
1469 if (!bdev->bd_contains->bd_holders)
1470 bdev->bd_contains->bd_holder = NULL;
1471
1472 spin_unlock(&bdev_lock);
1473
Tejun Heo77ea8872010-12-08 20:57:37 +01001474 /*
1475 * If this was the last claim, remove holder link and
1476 * unblock evpoll if it was a write holder.
1477 */
Tejun Heo85ef06d2011-07-01 16:17:47 +02001478 if (bdev_free && bdev->bd_write_holder) {
1479 disk_unblock_events(bdev->bd_disk);
1480 bdev->bd_write_holder = false;
Tejun Heo77ea8872010-12-08 20:57:37 +01001481 }
Tejun Heo69362172011-03-09 19:54:27 +01001482 }
Tejun Heo77ea8872010-12-08 20:57:37 +01001483
Tejun Heo85ef06d2011-07-01 16:17:47 +02001484 /*
1485 * Trigger event checking and tell drivers to flush MEDIA_CHANGE
1486 * event. This is to ensure detection of media removal commanded
1487 * from userland - e.g. eject(1).
1488 */
1489 disk_flush_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE);
1490
1491 mutex_unlock(&bdev->bd_mutex);
1492
Al Viro9a1c3542008-02-22 20:40:24 -05001493 return __blkdev_put(bdev, mode, 0);
NeilBrown37be4122006-12-08 02:36:16 -08001494}
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001495EXPORT_SYMBOL(blkdev_put);
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497static int blkdev_close(struct inode * inode, struct file * filp)
1498{
1499 struct block_device *bdev = I_BDEV(filp->f_mapping->host);
Tejun Heoe525fd82010-11-13 11:55:17 +01001500
Al Viro9a1c3542008-02-22 20:40:24 -05001501 return blkdev_put(bdev, filp->f_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
1503
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07001504static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
Al Viro56b26ad2008-09-19 03:17:36 -04001506 struct block_device *bdev = I_BDEV(file->f_mapping->host);
1507 fmode_t mode = file->f_mode;
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +01001508
1509 /*
1510 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
1511 * to updated it before every ioctl.
1512 */
Al Viro56b26ad2008-09-19 03:17:36 -04001513 if (file->f_flags & O_NDELAY)
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +01001514 mode |= FMODE_NDELAY;
1515 else
1516 mode &= ~FMODE_NDELAY;
1517
Al Viro56b26ad2008-09-19 03:17:36 -04001518 return blkdev_ioctl(bdev, mode, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519}
1520
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05001521/*
Christoph Hellwigeef99382009-08-20 17:43:41 +02001522 * Write data to the block device. Only intended for the block device itself
1523 * and the raw driver which basically is a fake block device.
1524 *
1525 * Does not take i_mutex for the write and thus is not for general purpose
1526 * use.
1527 */
1528ssize_t blkdev_aio_write(struct kiocb *iocb, const struct iovec *iov,
1529 unsigned long nr_segs, loff_t pos)
1530{
1531 struct file *file = iocb->ki_filp;
1532 ssize_t ret;
1533
1534 BUG_ON(iocb->ki_pos != pos);
1535
1536 ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos);
1537 if (ret > 0 || ret == -EIOCBQUEUED) {
1538 ssize_t err;
1539
1540 err = generic_write_sync(file, pos, ret);
1541 if (err < 0 && ret > 0)
1542 ret = err;
1543 }
1544 return ret;
1545}
1546EXPORT_SYMBOL_GPL(blkdev_aio_write);
1547
1548/*
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05001549 * Try to release a page associated with block device when the system
1550 * is under memory pressure.
1551 */
1552static int blkdev_releasepage(struct page *page, gfp_t wait)
1553{
1554 struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
1555
1556 if (super && super->s_op->bdev_try_to_free_page)
1557 return super->s_op->bdev_try_to_free_page(super, page, wait);
1558
1559 return try_to_free_buffers(page);
1560}
1561
Adrian Bunk4c54ac62008-02-18 13:48:31 +01001562static const struct address_space_operations def_blk_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 .readpage = blkdev_readpage,
1564 .writepage = blkdev_writepage,
Nick Piggin6272b5a2007-10-16 01:25:04 -07001565 .write_begin = blkdev_write_begin,
1566 .write_end = blkdev_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 .writepages = generic_writepages,
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05001568 .releasepage = blkdev_releasepage,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 .direct_IO = blkdev_direct_IO,
1570};
1571
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001572const struct file_operations def_blk_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 .open = blkdev_open,
1574 .release = blkdev_close,
1575 .llseek = block_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -07001576 .read = do_sync_read,
1577 .write = do_sync_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 .aio_read = generic_file_aio_read,
Christoph Hellwigeef99382009-08-20 17:43:41 +02001579 .aio_write = blkdev_aio_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 .mmap = generic_file_mmap,
Andrew Mortonb1dd3b22010-04-06 14:35:00 -07001581 .fsync = blkdev_fsync,
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07001582 .unlocked_ioctl = block_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583#ifdef CONFIG_COMPAT
1584 .compat_ioctl = compat_blkdev_ioctl,
1585#endif
Jens Axboe7f9c51f2006-05-01 19:59:32 +02001586 .splice_read = generic_file_splice_read,
1587 .splice_write = generic_file_splice_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588};
1589
1590int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
1591{
1592 int res;
1593 mm_segment_t old_fs = get_fs();
1594 set_fs(KERNEL_DS);
Al Viro56b26ad2008-09-19 03:17:36 -04001595 res = blkdev_ioctl(bdev, 0, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 set_fs(old_fs);
1597 return res;
1598}
1599
1600EXPORT_SYMBOL(ioctl_by_bdev);
1601
1602/**
1603 * lookup_bdev - lookup a struct block_device by name
Randy Dunlap94e29592009-01-06 14:41:15 -08001604 * @pathname: special file representing the block device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 *
Randy Dunlap57d1b532008-10-09 10:42:38 +02001606 * Get a reference to the blockdevice at @pathname in the current
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 * namespace if possible and return it. Return ERR_PTR(error)
1608 * otherwise.
1609 */
Al Viro421748e2008-08-02 01:04:36 -04001610struct block_device *lookup_bdev(const char *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
1612 struct block_device *bdev;
1613 struct inode *inode;
Al Viro421748e2008-08-02 01:04:36 -04001614 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 int error;
1616
Al Viro421748e2008-08-02 01:04:36 -04001617 if (!pathname || !*pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 return ERR_PTR(-EINVAL);
1619
Al Viro421748e2008-08-02 01:04:36 -04001620 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 if (error)
1622 return ERR_PTR(error);
1623
Al Viro421748e2008-08-02 01:04:36 -04001624 inode = path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 error = -ENOTBLK;
1626 if (!S_ISBLK(inode->i_mode))
1627 goto fail;
1628 error = -EACCES;
Al Viro421748e2008-08-02 01:04:36 -04001629 if (path.mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 goto fail;
1631 error = -ENOMEM;
1632 bdev = bd_acquire(inode);
1633 if (!bdev)
1634 goto fail;
1635out:
Al Viro421748e2008-08-02 01:04:36 -04001636 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 return bdev;
1638fail:
1639 bdev = ERR_PTR(error);
1640 goto out;
1641}
Al Virod5686b42008-08-01 05:00:11 -04001642EXPORT_SYMBOL(lookup_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
NeilBrown93b270f2011-02-24 17:25:47 +11001644int __invalidate_device(struct block_device *bdev, bool kill_dirty)
David Howellsb71e8a42006-08-29 19:06:11 +01001645{
1646 struct super_block *sb = get_super(bdev);
1647 int res = 0;
1648
1649 if (sb) {
1650 /*
1651 * no need to lock the super, get_super holds the
1652 * read mutex so the filesystem cannot go away
1653 * under us (->put_super runs with the write lock
1654 * hold).
1655 */
1656 shrink_dcache_sb(sb);
NeilBrown93b270f2011-02-24 17:25:47 +11001657 res = invalidate_inodes(sb, kill_dirty);
David Howellsb71e8a42006-08-29 19:06:11 +01001658 drop_super(sb);
1659 }
Peter Zijlstraf98393a2007-05-06 14:49:54 -07001660 invalidate_bdev(bdev);
David Howellsb71e8a42006-08-29 19:06:11 +01001661 return res;
1662}
1663EXPORT_SYMBOL(__invalidate_device);