blob: 94d41db62004bd30edaa714b4d82b566ab663919 [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{
58 spin_lock(&inode_lock);
59 inode->i_data.backing_dev_info = dst;
60 if (inode->i_state & I_DIRTY)
Nick Piggin7ccf19a2010-10-21 11:49:30 +110061 list_move(&inode->i_wb_list, &dst->wb.b_dirty);
Dave Chinnera5491e02010-10-21 11:49:26 +110062 spin_unlock(&inode_lock);
63}
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static sector_t max_block(struct block_device *bdev)
66{
67 sector_t retval = ~((sector_t)0);
68 loff_t sz = i_size_read(bdev->bd_inode);
69
70 if (sz) {
71 unsigned int size = block_size(bdev);
72 unsigned int sizebits = blksize_bits(size);
73 retval = (sz >> sizebits);
74 }
75 return retval;
76}
77
Peter Zijlstraf9a14392007-05-06 14:49:55 -070078/* Kill _all_ buffers and pagecache , dirty or not.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static void kill_bdev(struct block_device *bdev)
80{
Peter Zijlstraf9a14392007-05-06 14:49:55 -070081 if (bdev->bd_inode->i_mapping->nrpages == 0)
82 return;
83 invalidate_bh_lrus();
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
85}
86
87int set_blocksize(struct block_device *bdev, int size)
88{
89 /* Size must be a power of two, and between 512 and PAGE_SIZE */
Vignesh Babu BM1368c4f2007-05-08 00:24:32 -070090 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return -EINVAL;
92
93 /* Size cannot be smaller than the size supported by the device */
Martin K. Petersene1defc42009-05-22 17:17:49 -040094 if (size < bdev_logical_block_size(bdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return -EINVAL;
96
97 /* Don't change the size if it is same as current */
98 if (bdev->bd_block_size != size) {
99 sync_blockdev(bdev);
100 bdev->bd_block_size = size;
101 bdev->bd_inode->i_blkbits = blksize_bits(size);
102 kill_bdev(bdev);
103 }
104 return 0;
105}
106
107EXPORT_SYMBOL(set_blocksize);
108
109int sb_set_blocksize(struct super_block *sb, int size)
110{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if (set_blocksize(sb->s_bdev, size))
112 return 0;
113 /* If we get here, we know size is power of two
114 * and it's value is between 512 and PAGE_SIZE */
115 sb->s_blocksize = size;
Coywolf Qi Hunt38885bd2006-03-24 03:18:05 -0800116 sb->s_blocksize_bits = blksize_bits(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return sb->s_blocksize;
118}
119
120EXPORT_SYMBOL(sb_set_blocksize);
121
122int sb_min_blocksize(struct super_block *sb, int size)
123{
Martin K. Petersene1defc42009-05-22 17:17:49 -0400124 int minsize = bdev_logical_block_size(sb->s_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 if (size < minsize)
126 size = minsize;
127 return sb_set_blocksize(sb, size);
128}
129
130EXPORT_SYMBOL(sb_min_blocksize);
131
132static int
133blkdev_get_block(struct inode *inode, sector_t iblock,
134 struct buffer_head *bh, int create)
135{
136 if (iblock >= max_block(I_BDEV(inode))) {
137 if (create)
138 return -EIO;
139
140 /*
141 * for reads, we're just trying to fill a partial page.
142 * return a hole, they will have to call get_block again
143 * before they can fill it, and they will get -EIO at that
144 * time
145 */
146 return 0;
147 }
148 bh->b_bdev = I_BDEV(inode);
149 bh->b_blocknr = iblock;
150 set_buffer_mapped(bh);
151 return 0;
152}
153
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800154static int
155blkdev_get_blocks(struct inode *inode, sector_t iblock,
156 struct buffer_head *bh, int create)
157{
158 sector_t end_block = max_block(I_BDEV(inode));
159 unsigned long max_blocks = bh->b_size >> inode->i_blkbits;
160
161 if ((iblock + max_blocks) > end_block) {
162 max_blocks = end_block - iblock;
163 if ((long)max_blocks <= 0) {
164 if (create)
165 return -EIO; /* write fully beyond EOF */
166 /*
167 * It is a read which is fully beyond EOF. We return
168 * a !buffer_mapped buffer
169 */
170 max_blocks = 0;
171 }
172 }
173
174 bh->b_bdev = I_BDEV(inode);
175 bh->b_blocknr = iblock;
176 bh->b_size = max_blocks << inode->i_blkbits;
177 if (max_blocks)
178 set_buffer_mapped(bh);
179 return 0;
180}
181
182static ssize_t
183blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
184 loff_t offset, unsigned long nr_segs)
185{
186 struct file *file = iocb->ki_filp;
187 struct inode *inode = file->f_mapping->host;
188
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +0200189 return __blockdev_direct_IO(rw, iocb, inode, I_BDEV(inode), iov, offset,
190 nr_segs, blkdev_get_blocks, NULL, NULL, 0);
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800191}
192
Jan Kara5cee5812009-04-27 16:43:51 +0200193int __sync_blockdev(struct block_device *bdev, int wait)
194{
195 if (!bdev)
196 return 0;
197 if (!wait)
198 return filemap_flush(bdev->bd_inode->i_mapping);
199 return filemap_write_and_wait(bdev->bd_inode->i_mapping);
200}
201
Nick Piggin585d3bc2009-02-25 10:44:19 +0100202/*
203 * Write out and wait upon all the dirty data associated with a block
204 * device via its mapping. Does not take the superblock lock.
205 */
206int sync_blockdev(struct block_device *bdev)
207{
Jan Kara5cee5812009-04-27 16:43:51 +0200208 return __sync_blockdev(bdev, 1);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100209}
210EXPORT_SYMBOL(sync_blockdev);
211
212/*
213 * Write out and wait upon all dirty data associated with this
214 * device. Filesystem data as well as the underlying block
215 * device. Takes the superblock lock.
216 */
217int fsync_bdev(struct block_device *bdev)
218{
219 struct super_block *sb = get_super(bdev);
220 if (sb) {
Jan Kara60b06802009-04-27 16:43:53 +0200221 int res = sync_filesystem(sb);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100222 drop_super(sb);
223 return res;
224 }
225 return sync_blockdev(bdev);
226}
Al Viro47e44912009-04-01 07:07:16 -0400227EXPORT_SYMBOL(fsync_bdev);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100228
229/**
230 * freeze_bdev -- lock a filesystem and force it into a consistent state
231 * @bdev: blockdevice to lock
232 *
Nick Piggin585d3bc2009-02-25 10:44:19 +0100233 * If a superblock is found on this device, we take the s_umount semaphore
234 * on it to make sure nobody unmounts until the snapshot creation is done.
235 * The reference counter (bd_fsfreeze_count) guarantees that only the last
236 * unfreeze process can unfreeze the frozen filesystem actually when multiple
237 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
238 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
239 * actually.
240 */
241struct super_block *freeze_bdev(struct block_device *bdev)
242{
243 struct super_block *sb;
244 int error = 0;
245
246 mutex_lock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig45042302009-08-03 23:28:35 +0200247 if (++bdev->bd_fsfreeze_count > 1) {
248 /*
249 * We don't even need to grab a reference - the first call
250 * to freeze_bdev grab an active reference and only the last
251 * thaw_bdev drops it.
252 */
Nick Piggin585d3bc2009-02-25 10:44:19 +0100253 sb = get_super(bdev);
Christoph Hellwig45042302009-08-03 23:28:35 +0200254 drop_super(sb);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100255 mutex_unlock(&bdev->bd_fsfreeze_mutex);
256 return sb;
257 }
Nick Piggin585d3bc2009-02-25 10:44:19 +0100258
Christoph Hellwig45042302009-08-03 23:28:35 +0200259 sb = get_active_super(bdev);
260 if (!sb)
261 goto out;
Josef Bacik18e9e512010-03-23 10:34:56 -0400262 error = freeze_super(sb);
263 if (error) {
264 deactivate_super(sb);
265 bdev->bd_fsfreeze_count--;
Christoph Hellwig45042302009-08-03 23:28:35 +0200266 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Josef Bacik18e9e512010-03-23 10:34:56 -0400267 return ERR_PTR(error);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100268 }
Josef Bacik18e9e512010-03-23 10:34:56 -0400269 deactivate_super(sb);
Christoph Hellwig45042302009-08-03 23:28:35 +0200270 out:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100271 sync_blockdev(bdev);
272 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig4fadd7b2009-08-03 23:28:06 +0200273 return sb; /* thaw_bdev releases s->s_umount */
Nick Piggin585d3bc2009-02-25 10:44:19 +0100274}
275EXPORT_SYMBOL(freeze_bdev);
276
277/**
278 * thaw_bdev -- unlock filesystem
279 * @bdev: blockdevice to unlock
280 * @sb: associated superblock
281 *
282 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
283 */
284int thaw_bdev(struct block_device *bdev, struct super_block *sb)
285{
Christoph Hellwig45042302009-08-03 23:28:35 +0200286 int error = -EINVAL;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100287
288 mutex_lock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig45042302009-08-03 23:28:35 +0200289 if (!bdev->bd_fsfreeze_count)
Josef Bacik18e9e512010-03-23 10:34:56 -0400290 goto out;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100291
Christoph Hellwig45042302009-08-03 23:28:35 +0200292 error = 0;
293 if (--bdev->bd_fsfreeze_count > 0)
Josef Bacik18e9e512010-03-23 10:34:56 -0400294 goto out;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100295
Christoph Hellwig45042302009-08-03 23:28:35 +0200296 if (!sb)
Josef Bacik18e9e512010-03-23 10:34:56 -0400297 goto out;
Christoph Hellwig45042302009-08-03 23:28:35 +0200298
Josef Bacik18e9e512010-03-23 10:34:56 -0400299 error = thaw_super(sb);
300 if (error) {
301 bdev->bd_fsfreeze_count++;
302 mutex_unlock(&bdev->bd_fsfreeze_mutex);
303 return error;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100304 }
Josef Bacik18e9e512010-03-23 10:34:56 -0400305out:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100306 mutex_unlock(&bdev->bd_fsfreeze_mutex);
307 return 0;
308}
309EXPORT_SYMBOL(thaw_bdev);
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
312{
313 return block_write_full_page(page, blkdev_get_block, wbc);
314}
315
316static int blkdev_readpage(struct file * file, struct page * page)
317{
318 return block_read_full_page(page, blkdev_get_block);
319}
320
Nick Piggin6272b5a2007-10-16 01:25:04 -0700321static int blkdev_write_begin(struct file *file, struct address_space *mapping,
322 loff_t pos, unsigned len, unsigned flags,
323 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Christoph Hellwig155130a2010-06-04 11:29:58 +0200325 return block_write_begin(mapping, pos, len, flags, pagep,
326 blkdev_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Nick Piggin6272b5a2007-10-16 01:25:04 -0700329static int blkdev_write_end(struct file *file, struct address_space *mapping,
330 loff_t pos, unsigned len, unsigned copied,
331 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Nick Piggin6272b5a2007-10-16 01:25:04 -0700333 int ret;
334 ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
335
336 unlock_page(page);
337 page_cache_release(page);
338
339 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
342/*
343 * private llseek:
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800344 * for a block special file file->f_path.dentry->d_inode->i_size is zero
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 * so we compute the size by hand (just as in block_read/write above)
346 */
347static loff_t block_llseek(struct file *file, loff_t offset, int origin)
348{
349 struct inode *bd_inode = file->f_mapping->host;
350 loff_t size;
351 loff_t retval;
352
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800353 mutex_lock(&bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 size = i_size_read(bd_inode);
355
356 switch (origin) {
357 case 2:
358 offset += size;
359 break;
360 case 1:
361 offset += file->f_pos;
362 }
363 retval = -EINVAL;
364 if (offset >= 0 && offset <= size) {
365 if (offset != file->f_pos) {
366 file->f_pos = offset;
367 }
368 retval = offset;
369 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800370 mutex_unlock(&bd_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return retval;
372}
373
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200374int blkdev_fsync(struct file *filp, int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400376 struct inode *bd_inode = filp->f_mapping->host;
377 struct block_device *bdev = I_BDEV(bd_inode);
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100378 int error;
379
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400380 /*
381 * There is no need to serialise calls to blkdev_issue_flush with
382 * i_mutex and doing so causes performance issues with concurrent
383 * O_SYNC writers to a block device.
384 */
385 mutex_unlock(&bd_inode->i_mutex);
386
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200387 error = blkdev_issue_flush(bdev, GFP_KERNEL, NULL);
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100388 if (error == -EOPNOTSUPP)
389 error = 0;
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400390
391 mutex_lock(&bd_inode->i_mutex);
392
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100393 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
Andrew Mortonb1dd3b22010-04-06 14:35:00 -0700395EXPORT_SYMBOL(blkdev_fsync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397/*
398 * pseudo-fs
399 */
400
401static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
Christoph Lametere18b8902006-12-06 20:33:20 -0800402static struct kmem_cache * bdev_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404static struct inode *bdev_alloc_inode(struct super_block *sb)
405{
Christoph Lametere94b1762006-12-06 20:33:17 -0800406 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (!ei)
408 return NULL;
409 return &ei->vfs_inode;
410}
411
Nick Pigginfa0d7e32011-01-07 17:49:49 +1100412static void bdev_i_callback(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Nick Pigginfa0d7e32011-01-07 17:49:49 +1100414 struct inode *inode = container_of(head, struct inode, i_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 struct bdev_inode *bdi = BDEV_I(inode);
416
Nick Pigginfa0d7e32011-01-07 17:49:49 +1100417 INIT_LIST_HEAD(&inode->i_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 kmem_cache_free(bdev_cachep, bdi);
419}
420
Nick Pigginfa0d7e32011-01-07 17:49:49 +1100421static void bdev_destroy_inode(struct inode *inode)
422{
423 call_rcu(&inode->i_rcu, bdev_i_callback);
424}
425
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700426static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
428 struct bdev_inode *ei = (struct bdev_inode *) foo;
429 struct block_device *bdev = &ei->bdev;
430
Christoph Lametera35afb82007-05-16 22:10:57 -0700431 memset(bdev, 0, sizeof(*bdev));
432 mutex_init(&bdev->bd_mutex);
Christoph Lametera35afb82007-05-16 22:10:57 -0700433 INIT_LIST_HEAD(&bdev->bd_inodes);
434 INIT_LIST_HEAD(&bdev->bd_list);
Tejun Heo49731ba2011-01-14 18:43:57 +0100435#ifdef CONFIG_SYSFS
436 INIT_LIST_HEAD(&bdev->bd_holder_disks);
437#endif
Christoph Lametera35afb82007-05-16 22:10:57 -0700438 inode_init_once(&ei->vfs_inode);
Takashi Satofcccf502009-01-09 16:40:59 -0800439 /* Initialize mutex for freeze. */
440 mutex_init(&bdev->bd_fsfreeze_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
443static inline void __bd_forget(struct inode *inode)
444{
445 list_del_init(&inode->i_devices);
446 inode->i_bdev = NULL;
447 inode->i_mapping = &inode->i_data;
448}
449
Al Virob57922d2010-06-07 14:34:48 -0400450static void bdev_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 struct block_device *bdev = &BDEV_I(inode)->bdev;
453 struct list_head *p;
Al Virob57922d2010-06-07 14:34:48 -0400454 truncate_inode_pages(&inode->i_data, 0);
455 invalidate_inode_buffers(inode); /* is it needed here? */
456 end_writeback(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 spin_lock(&bdev_lock);
458 while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
459 __bd_forget(list_entry(p, struct inode, i_devices));
460 }
461 list_del_init(&bdev->bd_list);
462 spin_unlock(&bdev_lock);
463}
464
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800465static const struct super_operations bdev_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 .statfs = simple_statfs,
467 .alloc_inode = bdev_alloc_inode,
468 .destroy_inode = bdev_destroy_inode,
469 .drop_inode = generic_delete_inode,
Al Virob57922d2010-06-07 14:34:48 -0400470 .evict_inode = bdev_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471};
472
Al Viro51139ad2010-07-25 23:47:46 +0400473static struct dentry *bd_mount(struct file_system_type *fs_type,
474 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Al Viroc74a1cb2011-01-12 16:59:34 -0500476 return mount_pseudo(fs_type, "bdev:", &bdev_sops, NULL, 0x62646576);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
479static struct file_system_type bd_type = {
480 .name = "bdev",
Al Viro51139ad2010-07-25 23:47:46 +0400481 .mount = bd_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 .kill_sb = kill_anon_super,
483};
484
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800485struct super_block *blockdev_superblock __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487void __init bdev_cache_init(void)
488{
489 int err;
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800490 struct vfsmount *bd_mnt;
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800493 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
494 SLAB_MEM_SPREAD|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +0900495 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 err = register_filesystem(&bd_type);
497 if (err)
498 panic("Cannot register bdev pseudo-fs");
499 bd_mnt = kern_mount(&bd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (IS_ERR(bd_mnt))
501 panic("Cannot create bdev pseudo-fs");
Catalin Marinas2e1483c2009-06-11 13:24:13 +0100502 /*
503 * This vfsmount structure is only used to obtain the
504 * blockdev_superblock, so tell kmemleak not to report it.
505 */
506 kmemleak_not_leak(bd_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
508}
509
510/*
511 * Most likely _very_ bad one - but then it's hardly critical for small
512 * /dev and can be fixed when somebody will need really large one.
513 * Keep in mind that it will be fed through icache hash function too.
514 */
515static inline unsigned long hash(dev_t dev)
516{
517 return MAJOR(dev)+MINOR(dev);
518}
519
520static int bdev_test(struct inode *inode, void *data)
521{
522 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
523}
524
525static int bdev_set(struct inode *inode, void *data)
526{
527 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
528 return 0;
529}
530
531static LIST_HEAD(all_bdevs);
532
533struct block_device *bdget(dev_t dev)
534{
535 struct block_device *bdev;
536 struct inode *inode;
537
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800538 inode = iget5_locked(blockdev_superblock, hash(dev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 bdev_test, bdev_set, &dev);
540
541 if (!inode)
542 return NULL;
543
544 bdev = &BDEV_I(inode)->bdev;
545
546 if (inode->i_state & I_NEW) {
547 bdev->bd_contains = NULL;
548 bdev->bd_inode = inode;
549 bdev->bd_block_size = (1 << inode->i_blkbits);
550 bdev->bd_part_count = 0;
551 bdev->bd_invalidated = 0;
552 inode->i_mode = S_IFBLK;
553 inode->i_rdev = dev;
554 inode->i_bdev = bdev;
555 inode->i_data.a_ops = &def_blk_aops;
556 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
557 inode->i_data.backing_dev_info = &default_backing_dev_info;
558 spin_lock(&bdev_lock);
559 list_add(&bdev->bd_list, &all_bdevs);
560 spin_unlock(&bdev_lock);
561 unlock_new_inode(inode);
562 }
563 return bdev;
564}
565
566EXPORT_SYMBOL(bdget);
567
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200568/**
569 * bdgrab -- Grab a reference to an already referenced block device
570 * @bdev: Block device to grab a reference to.
571 */
572struct block_device *bdgrab(struct block_device *bdev)
573{
Al Viro7de9c6e2010-10-23 11:11:40 -0400574 ihold(bdev->bd_inode);
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200575 return bdev;
576}
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578long nr_blockdev_pages(void)
579{
Matthias Kaehlcke203a2932007-07-15 23:40:23 -0700580 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 long ret = 0;
582 spin_lock(&bdev_lock);
Matthias Kaehlcke203a2932007-07-15 23:40:23 -0700583 list_for_each_entry(bdev, &all_bdevs, bd_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 ret += bdev->bd_inode->i_mapping->nrpages;
585 }
586 spin_unlock(&bdev_lock);
587 return ret;
588}
589
590void bdput(struct block_device *bdev)
591{
592 iput(bdev->bd_inode);
593}
594
595EXPORT_SYMBOL(bdput);
596
597static struct block_device *bd_acquire(struct inode *inode)
598{
599 struct block_device *bdev;
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 spin_lock(&bdev_lock);
602 bdev = inode->i_bdev;
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700603 if (bdev) {
Al Viro7de9c6e2010-10-23 11:11:40 -0400604 ihold(bdev->bd_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 spin_unlock(&bdev_lock);
606 return bdev;
607 }
608 spin_unlock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 bdev = bdget(inode->i_rdev);
611 if (bdev) {
612 spin_lock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700613 if (!inode->i_bdev) {
614 /*
Al Viro7de9c6e2010-10-23 11:11:40 -0400615 * We take an additional reference to bd_inode,
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700616 * and it's released in clear_inode() of inode.
617 * So, we can access it via ->i_mapping always
618 * without igrab().
619 */
Al Viro7de9c6e2010-10-23 11:11:40 -0400620 ihold(bdev->bd_inode);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700621 inode->i_bdev = bdev;
622 inode->i_mapping = bdev->bd_inode->i_mapping;
623 list_add(&inode->i_devices, &bdev->bd_inodes);
624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 spin_unlock(&bdev_lock);
626 }
627 return bdev;
628}
629
630/* Call when you free inode */
631
632void bd_forget(struct inode *inode)
633{
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700634 struct block_device *bdev = NULL;
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 spin_lock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700637 if (inode->i_bdev) {
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800638 if (!sb_is_blkdev_sb(inode->i_sb))
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700639 bdev = inode->i_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 __bd_forget(inode);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 spin_unlock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700643
644 if (bdev)
645 iput(bdev->bd_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900648/**
649 * bd_may_claim - test whether a block device can be claimed
650 * @bdev: block device of interest
651 * @whole: whole block device containing @bdev, may equal @bdev
652 * @holder: holder trying to claim @bdev
653 *
654 * Test whther @bdev can be claimed by @holder.
655 *
656 * CONTEXT:
657 * spin_lock(&bdev_lock).
658 *
659 * RETURNS:
660 * %true if @bdev can be claimed, %false otherwise.
661 */
662static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
663 void *holder)
664{
665 if (bdev->bd_holder == holder)
666 return true; /* already a holder */
667 else if (bdev->bd_holder != NULL)
668 return false; /* held by someone else */
669 else if (bdev->bd_contains == bdev)
670 return true; /* is a whole device which isn't held */
671
Tejun Heoe525fd82010-11-13 11:55:17 +0100672 else if (whole->bd_holder == bd_may_claim)
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900673 return true; /* is a partition of a device that is being partitioned */
674 else if (whole->bd_holder != NULL)
675 return false; /* is a partition of a held device */
676 else
677 return true; /* is a partition of an un-held device */
678}
679
680/**
Tejun Heo6b4517a2010-04-07 18:53:59 +0900681 * bd_prepare_to_claim - prepare to claim a block device
682 * @bdev: block device of interest
683 * @whole: the whole device containing @bdev, may equal @bdev
684 * @holder: holder trying to claim @bdev
685 *
686 * Prepare to claim @bdev. This function fails if @bdev is already
687 * claimed by another holder and waits if another claiming is in
688 * progress. This function doesn't actually claim. On successful
689 * return, the caller has ownership of bd_claiming and bd_holder[s].
690 *
691 * CONTEXT:
692 * spin_lock(&bdev_lock). Might release bdev_lock, sleep and regrab
693 * it multiple times.
694 *
695 * RETURNS:
696 * 0 if @bdev can be claimed, -EBUSY otherwise.
697 */
698static int bd_prepare_to_claim(struct block_device *bdev,
699 struct block_device *whole, void *holder)
700{
701retry:
702 /* if someone else claimed, fail */
703 if (!bd_may_claim(bdev, whole, holder))
704 return -EBUSY;
705
Tejun Heoe75aa852010-08-04 17:59:39 +0200706 /* if claiming is already in progress, wait for it to finish */
707 if (whole->bd_claiming) {
Tejun Heo6b4517a2010-04-07 18:53:59 +0900708 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
709 DEFINE_WAIT(wait);
710
711 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
712 spin_unlock(&bdev_lock);
713 schedule();
714 finish_wait(wq, &wait);
715 spin_lock(&bdev_lock);
716 goto retry;
717 }
718
719 /* yay, all mine */
720 return 0;
721}
722
723/**
724 * bd_start_claiming - start claiming a block device
725 * @bdev: block device of interest
726 * @holder: holder trying to claim @bdev
727 *
728 * @bdev is about to be opened exclusively. Check @bdev can be opened
729 * exclusively and mark that an exclusive open is in progress. Each
730 * successful call to this function must be matched with a call to
Nick Pigginb0018362010-05-26 01:51:19 +1000731 * either bd_finish_claiming() or bd_abort_claiming() (which do not
732 * fail).
733 *
734 * This function is used to gain exclusive access to the block device
735 * without actually causing other exclusive open attempts to fail. It
736 * should be used when the open sequence itself requires exclusive
737 * access but may subsequently fail.
Tejun Heo6b4517a2010-04-07 18:53:59 +0900738 *
739 * CONTEXT:
740 * Might sleep.
741 *
742 * RETURNS:
743 * Pointer to the block device containing @bdev on success, ERR_PTR()
744 * value on failure.
745 */
746static struct block_device *bd_start_claiming(struct block_device *bdev,
747 void *holder)
748{
749 struct gendisk *disk;
750 struct block_device *whole;
751 int partno, err;
752
753 might_sleep();
754
755 /*
756 * @bdev might not have been initialized properly yet, look up
757 * and grab the outer block device the hard way.
758 */
759 disk = get_gendisk(bdev->bd_dev, &partno);
760 if (!disk)
761 return ERR_PTR(-ENXIO);
762
763 whole = bdget_disk(disk, 0);
Nick Piggincf342572010-05-26 01:50:21 +1000764 module_put(disk->fops->owner);
Tejun Heo6b4517a2010-04-07 18:53:59 +0900765 put_disk(disk);
766 if (!whole)
767 return ERR_PTR(-ENOMEM);
768
769 /* prepare to claim, if successful, mark claiming in progress */
770 spin_lock(&bdev_lock);
771
772 err = bd_prepare_to_claim(bdev, whole, holder);
773 if (err == 0) {
774 whole->bd_claiming = holder;
775 spin_unlock(&bdev_lock);
776 return whole;
777 } else {
778 spin_unlock(&bdev_lock);
779 bdput(whole);
780 return ERR_PTR(err);
781 }
782}
783
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800784#ifdef CONFIG_SYSFS
Tejun Heo49731ba2011-01-14 18:43:57 +0100785struct bd_holder_disk {
786 struct list_head list;
787 struct gendisk *disk;
788 int refcnt;
789};
790
791static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev,
792 struct gendisk *disk)
793{
794 struct bd_holder_disk *holder;
795
796 list_for_each_entry(holder, &bdev->bd_holder_disks, list)
797 if (holder->disk == disk)
798 return holder;
799 return NULL;
800}
801
Andrew Morton4d7dd8f2006-09-29 01:58:56 -0700802static int add_symlink(struct kobject *from, struct kobject *to)
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800803{
Andrew Morton4d7dd8f2006-09-29 01:58:56 -0700804 return sysfs_create_link(from, to, kobject_name(to));
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800805}
806
807static void del_symlink(struct kobject *from, struct kobject *to)
808{
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800809 sysfs_remove_link(from, kobject_name(to));
810}
811
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800812/**
Tejun Heoe09b4572010-11-13 11:55:17 +0100813 * bd_link_disk_holder - create symlinks between holding disk and slave bdev
814 * @bdev: the claimed slave bdev
815 * @disk: the holding disk
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500816 *
Tejun Heo49731ba2011-01-14 18:43:57 +0100817 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
818 *
Tejun Heoe09b4572010-11-13 11:55:17 +0100819 * This functions creates the following sysfs symlinks.
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500820 *
Tejun Heoe09b4572010-11-13 11:55:17 +0100821 * - from "slaves" directory of the holder @disk to the claimed @bdev
822 * - from "holders" directory of the @bdev to the holder @disk
823 *
824 * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
825 * passed to bd_link_disk_holder(), then:
826 *
827 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
828 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
829 *
830 * The caller must have claimed @bdev before calling this function and
831 * ensure that both @bdev and @disk are valid during the creation and
832 * lifetime of these symlinks.
833 *
834 * CONTEXT:
835 * Might sleep.
836 *
837 * RETURNS:
838 * 0 on success, -errno on failure.
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500839 */
Tejun Heoe09b4572010-11-13 11:55:17 +0100840int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500841{
Tejun Heo49731ba2011-01-14 18:43:57 +0100842 struct bd_holder_disk *holder;
Tejun Heoe09b4572010-11-13 11:55:17 +0100843 int ret = 0;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800844
Peter Zijlstra2e7b6512006-12-08 02:36:13 -0800845 mutex_lock(&bdev->bd_mutex);
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -0500846
Tejun Heo49731ba2011-01-14 18:43:57 +0100847 WARN_ON_ONCE(!bdev->bd_holder);
Johannes Weiner4e916722007-07-15 23:41:25 -0700848
Tejun Heoe09b4572010-11-13 11:55:17 +0100849 /* FIXME: remove the following once add_disk() handles errors */
850 if (WARN_ON(!disk->slave_dir || !bdev->bd_part->holder_dir))
851 goto out_unlock;
Johannes Weiner4e916722007-07-15 23:41:25 -0700852
Tejun Heo49731ba2011-01-14 18:43:57 +0100853 holder = bd_find_holder_disk(bdev, disk);
854 if (holder) {
855 holder->refcnt++;
Tejun Heoe09b4572010-11-13 11:55:17 +0100856 goto out_unlock;
857 }
858
Tejun Heo49731ba2011-01-14 18:43:57 +0100859 holder = kzalloc(sizeof(*holder), GFP_KERNEL);
860 if (!holder) {
861 ret = -ENOMEM;
862 goto out_unlock;
863 }
864
865 INIT_LIST_HEAD(&holder->list);
866 holder->disk = disk;
867 holder->refcnt = 1;
868
869 ret = add_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
870 if (ret)
871 goto out_free;
872
873 ret = add_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj);
874 if (ret)
875 goto out_del;
Tejun Heoe7407d12011-02-24 09:56:32 +0100876 /*
877 * bdev could be deleted beneath us which would implicitly destroy
878 * the holder directory. Hold on to it.
879 */
880 kobject_get(bdev->bd_part->holder_dir);
Tejun Heo49731ba2011-01-14 18:43:57 +0100881
882 list_add(&holder->list, &bdev->bd_holder_disks);
883 goto out_unlock;
884
885out_del:
886 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
887out_free:
888 kfree(holder);
Tejun Heoe09b4572010-11-13 11:55:17 +0100889out_unlock:
Jun'ichi Nomurab4cf1b72006-03-27 01:18:00 -0800890 mutex_unlock(&bdev->bd_mutex);
Tejun Heoe09b4572010-11-13 11:55:17 +0100891 return ret;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800892}
Tejun Heoe09b4572010-11-13 11:55:17 +0100893EXPORT_SYMBOL_GPL(bd_link_disk_holder);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800894
Tejun Heo49731ba2011-01-14 18:43:57 +0100895/**
896 * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
897 * @bdev: the calimed slave bdev
898 * @disk: the holding disk
899 *
900 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
901 *
902 * CONTEXT:
903 * Might sleep.
904 */
905void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800906{
Tejun Heo49731ba2011-01-14 18:43:57 +0100907 struct bd_holder_disk *holder;
Tejun Heoe09b4572010-11-13 11:55:17 +0100908
Tejun Heo49731ba2011-01-14 18:43:57 +0100909 mutex_lock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800910
Tejun Heo49731ba2011-01-14 18:43:57 +0100911 holder = bd_find_holder_disk(bdev, disk);
912
913 if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
914 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
915 del_symlink(bdev->bd_part->holder_dir,
916 &disk_to_dev(disk)->kobj);
Tejun Heoe7407d12011-02-24 09:56:32 +0100917 kobject_put(bdev->bd_part->holder_dir);
Tejun Heo49731ba2011-01-14 18:43:57 +0100918 list_del_init(&holder->list);
919 kfree(holder);
920 }
921
922 mutex_unlock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800923}
Tejun Heo49731ba2011-01-14 18:43:57 +0100924EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -0800925#endif
926
Andrew Patterson0c002c22008-09-04 14:27:20 -0600927/**
Andrew Patterson56ade442008-09-04 14:27:40 -0600928 * flush_disk - invalidates all buffer-cache entries on a disk
929 *
930 * @bdev: struct block device to be flushed
931 *
932 * Invalidates all buffer-cache entries on a disk. It should be called
933 * when a disk has been changed -- either by a media change or online
934 * resize.
935 */
936static void flush_disk(struct block_device *bdev)
937{
938 if (__invalidate_device(bdev)) {
939 char name[BDEVNAME_SIZE] = "";
940
941 if (bdev->bd_disk)
942 disk_name(bdev->bd_disk, 0, name);
943 printk(KERN_WARNING "VFS: busy inodes on changed media or "
944 "resized disk %s\n", name);
945 }
946
947 if (!bdev->bd_disk)
948 return;
949 if (disk_partitionable(bdev->bd_disk))
950 bdev->bd_invalidated = 1;
951}
952
953/**
Randy Dunlap57d1b532008-10-09 10:42:38 +0200954 * check_disk_size_change - checks for disk size change and adjusts bdev size.
Andrew Pattersonc3279d12008-09-04 14:27:25 -0600955 * @disk: struct gendisk to check
956 * @bdev: struct bdev to adjust.
957 *
958 * This routine checks to see if the bdev size does not match the disk size
959 * and adjusts it if it differs.
960 */
961void check_disk_size_change(struct gendisk *disk, struct block_device *bdev)
962{
963 loff_t disk_size, bdev_size;
964
965 disk_size = (loff_t)get_capacity(disk) << 9;
966 bdev_size = i_size_read(bdev->bd_inode);
967 if (disk_size != bdev_size) {
968 char name[BDEVNAME_SIZE];
969
970 disk_name(disk, 0, name);
971 printk(KERN_INFO
972 "%s: detected capacity change from %lld to %lld\n",
973 name, bdev_size, disk_size);
974 i_size_write(bdev->bd_inode, disk_size);
Andrew Patterson608aeef2008-09-04 14:27:45 -0600975 flush_disk(bdev);
Andrew Pattersonc3279d12008-09-04 14:27:25 -0600976 }
977}
978EXPORT_SYMBOL(check_disk_size_change);
979
980/**
Randy Dunlap57d1b532008-10-09 10:42:38 +0200981 * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back
Andrew Patterson0c002c22008-09-04 14:27:20 -0600982 * @disk: struct gendisk to be revalidated
983 *
984 * This routine is a wrapper for lower-level driver's revalidate_disk
985 * call-backs. It is used to do common pre and post operations needed
986 * for all revalidate_disk operations.
987 */
988int revalidate_disk(struct gendisk *disk)
989{
Andrew Pattersonc3279d12008-09-04 14:27:25 -0600990 struct block_device *bdev;
Andrew Patterson0c002c22008-09-04 14:27:20 -0600991 int ret = 0;
992
993 if (disk->fops->revalidate_disk)
994 ret = disk->fops->revalidate_disk(disk);
995
Andrew Pattersonc3279d12008-09-04 14:27:25 -0600996 bdev = bdget_disk(disk, 0);
997 if (!bdev)
998 return ret;
999
1000 mutex_lock(&bdev->bd_mutex);
1001 check_disk_size_change(disk, bdev);
1002 mutex_unlock(&bdev->bd_mutex);
1003 bdput(bdev);
Andrew Patterson0c002c22008-09-04 14:27:20 -06001004 return ret;
1005}
1006EXPORT_SYMBOL(revalidate_disk);
1007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008/*
1009 * This routine checks whether a removable media has been changed,
1010 * and invalidates all buffer-cache-entries in that case. This
1011 * is a relatively slow routine, so we have to try to minimize using
1012 * it. Thus it is called only upon a 'mount' or 'open'. This
1013 * is the best way of combining speed and utility, I think.
1014 * People changing diskettes in the middle of an operation deserve
1015 * to lose :-)
1016 */
1017int check_disk_change(struct block_device *bdev)
1018{
1019 struct gendisk *disk = bdev->bd_disk;
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001020 const struct block_device_operations *bdops = disk->fops;
Tejun Heo77ea8872010-12-08 20:57:37 +01001021 unsigned int events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Tejun Heo77ea8872010-12-08 20:57:37 +01001023 events = disk_clear_events(disk, DISK_EVENT_MEDIA_CHANGE |
1024 DISK_EVENT_EJECT_REQUEST);
1025 if (!(events & DISK_EVENT_MEDIA_CHANGE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return 0;
1027
Andrew Patterson56ade442008-09-04 14:27:40 -06001028 flush_disk(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (bdops->revalidate_disk)
1030 bdops->revalidate_disk(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return 1;
1032}
1033
1034EXPORT_SYMBOL(check_disk_change);
1035
1036void bd_set_size(struct block_device *bdev, loff_t size)
1037{
Martin K. Petersene1defc42009-05-22 17:17:49 -04001038 unsigned bsize = bdev_logical_block_size(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 bdev->bd_inode->i_size = size;
1041 while (bsize < PAGE_CACHE_SIZE) {
1042 if (size & bsize)
1043 break;
1044 bsize <<= 1;
1045 }
1046 bdev->bd_block_size = bsize;
1047 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
1048}
1049EXPORT_SYMBOL(bd_set_size);
1050
Al Viro9a1c3542008-02-22 20:40:24 -05001051static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
NeilBrown37be4122006-12-08 02:36:16 -08001052
Peter Zijlstra6d740cd2007-02-20 13:58:18 -08001053/*
1054 * bd_mutex locking:
1055 *
1056 * mutex_lock(part->bd_mutex)
1057 * mutex_lock_nested(whole->bd_mutex, 1)
1058 */
1059
Al Viro572c4892007-10-08 13:24:05 -04001060static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 struct gendisk *disk;
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -07001063 int ret;
Tejun Heocf771cb2008-09-03 09:01:09 +02001064 int partno;
Al Virofe6e9c12008-06-23 08:30:55 -04001065 int perm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Al Viro572c4892007-10-08 13:24:05 -04001067 if (mode & FMODE_READ)
Al Virofe6e9c12008-06-23 08:30:55 -04001068 perm |= MAY_READ;
Al Viro572c4892007-10-08 13:24:05 -04001069 if (mode & FMODE_WRITE)
Al Virofe6e9c12008-06-23 08:30:55 -04001070 perm |= MAY_WRITE;
1071 /*
1072 * hooks: /n/, see "layering violations".
1073 */
Chris Wrightb7300b72010-08-10 18:02:55 -07001074 if (!for_part) {
1075 ret = devcgroup_inode_permission(bdev->bd_inode, perm);
1076 if (ret != 0) {
1077 bdput(bdev);
1078 return ret;
1079 }
Al Viro82666022008-08-01 05:32:04 -04001080 }
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -07001081
NeilBrownd3374822009-01-09 08:31:10 +11001082 restart:
Tejun Heo0762b8b2008-08-25 19:56:12 +09001083
Tejun Heo89f97492008-11-05 10:21:06 +01001084 ret = -ENXIO;
Tejun Heocf771cb2008-09-03 09:01:09 +02001085 disk = get_gendisk(bdev->bd_dev, &partno);
Tejun Heo0762b8b2008-08-25 19:56:12 +09001086 if (!disk)
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001087 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
NeilBrown6796bf52006-12-08 02:36:16 -08001089 mutex_lock_nested(&bdev->bd_mutex, for_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 if (!bdev->bd_openers) {
1091 bdev->bd_disk = disk;
1092 bdev->bd_contains = bdev;
Tejun Heocf771cb2008-09-03 09:01:09 +02001093 if (!partno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 struct backing_dev_info *bdi;
Tejun Heo89f97492008-11-05 10:21:06 +01001095
1096 ret = -ENXIO;
1097 bdev->bd_part = disk_get_part(disk, partno);
1098 if (!bdev->bd_part)
1099 goto out_clear;
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (disk->fops->open) {
Al Viro572c4892007-10-08 13:24:05 -04001102 ret = disk->fops->open(bdev, mode);
NeilBrownd3374822009-01-09 08:31:10 +11001103 if (ret == -ERESTARTSYS) {
1104 /* Lost a race with 'disk' being
1105 * deleted, try again.
1106 * See md.c
1107 */
1108 disk_put_part(bdev->bd_part);
1109 bdev->bd_part = NULL;
1110 module_put(disk->fops->owner);
1111 put_disk(disk);
1112 bdev->bd_disk = NULL;
1113 mutex_unlock(&bdev->bd_mutex);
1114 goto restart;
1115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if (ret)
Tejun Heo0762b8b2008-08-25 19:56:12 +09001117 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
1119 if (!bdev->bd_openers) {
1120 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
1121 bdi = blk_get_backing_dev_info(bdev);
1122 if (bdi == NULL)
1123 bdi = &default_backing_dev_info;
Dave Chinnera5491e02010-10-21 11:49:26 +11001124 bdev_inode_switch_bdi(bdev->bd_inode, bdi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
1126 if (bdev->bd_invalidated)
1127 rescan_partitions(disk, bdev);
1128 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 struct block_device *whole;
1130 whole = bdget_disk(disk, 0);
1131 ret = -ENOMEM;
1132 if (!whole)
Tejun Heo0762b8b2008-08-25 19:56:12 +09001133 goto out_clear;
NeilBrown37be4122006-12-08 02:36:16 -08001134 BUG_ON(for_part);
Al Viro572c4892007-10-08 13:24:05 -04001135 ret = __blkdev_get(whole, mode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 if (ret)
Tejun Heo0762b8b2008-08-25 19:56:12 +09001137 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 bdev->bd_contains = whole;
Dave Chinnera5491e02010-10-21 11:49:26 +11001139 bdev_inode_switch_bdi(bdev->bd_inode,
1140 whole->bd_inode->i_data.backing_dev_info);
Tejun Heo89f97492008-11-05 10:21:06 +01001141 bdev->bd_part = disk_get_part(disk, partno);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001142 if (!(disk->flags & GENHD_FL_UP) ||
Tejun Heo89f97492008-11-05 10:21:06 +01001143 !bdev->bd_part || !bdev->bd_part->nr_sects) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 ret = -ENXIO;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001145 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
Tejun Heo89f97492008-11-05 10:21:06 +01001147 bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
1149 } else {
Tejun Heo0762b8b2008-08-25 19:56:12 +09001150 module_put(disk->fops->owner);
Neil Brown960cc0f2009-10-26 08:59:17 +01001151 put_disk(disk);
Tejun Heo0762b8b2008-08-25 19:56:12 +09001152 disk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (bdev->bd_contains == bdev) {
1154 if (bdev->bd_disk->fops->open) {
Al Viro572c4892007-10-08 13:24:05 -04001155 ret = bdev->bd_disk->fops->open(bdev, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (ret)
Tejun Heo0762b8b2008-08-25 19:56:12 +09001157 goto out_unlock_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
1159 if (bdev->bd_invalidated)
1160 rescan_partitions(bdev->bd_disk, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
1162 }
1163 bdev->bd_openers++;
NeilBrown37be4122006-12-08 02:36:16 -08001164 if (for_part)
1165 bdev->bd_part_count++;
Arjan van de Venc039e312006-03-23 03:00:28 -08001166 mutex_unlock(&bdev->bd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 return 0;
1168
Tejun Heo0762b8b2008-08-25 19:56:12 +09001169 out_clear:
Tejun Heo89f97492008-11-05 10:21:06 +01001170 disk_put_part(bdev->bd_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 bdev->bd_disk = NULL;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001172 bdev->bd_part = NULL;
Dave Chinnera5491e02010-10-21 11:49:26 +11001173 bdev_inode_switch_bdi(bdev->bd_inode, &default_backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if (bdev != bdev->bd_contains)
Al Viro572c4892007-10-08 13:24:05 -04001175 __blkdev_put(bdev->bd_contains, mode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 bdev->bd_contains = NULL;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001177 out_unlock_bdev:
Arjan van de Venc039e312006-03-23 03:00:28 -08001178 mutex_unlock(&bdev->bd_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001179 out:
Tejun Heo0762b8b2008-08-25 19:56:12 +09001180 if (disk)
1181 module_put(disk->fops->owner);
1182 put_disk(disk);
1183 bdput(bdev);
1184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return ret;
1186}
1187
Tejun Heod4d77622010-11-13 11:55:18 +01001188/**
1189 * blkdev_get - open a block device
1190 * @bdev: block_device to open
1191 * @mode: FMODE_* mask
1192 * @holder: exclusive holder identifier
1193 *
1194 * Open @bdev with @mode. If @mode includes %FMODE_EXCL, @bdev is
1195 * open with exclusive access. Specifying %FMODE_EXCL with %NULL
1196 * @holder is invalid. Exclusive opens may nest for the same @holder.
1197 *
1198 * On success, the reference count of @bdev is unchanged. On failure,
1199 * @bdev is put.
1200 *
1201 * CONTEXT:
1202 * Might sleep.
1203 *
1204 * RETURNS:
1205 * 0 on success, -errno on failure.
1206 */
Tejun Heoe525fd82010-11-13 11:55:17 +01001207int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Tejun Heoe525fd82010-11-13 11:55:17 +01001209 struct block_device *whole = NULL;
1210 int res;
1211
1212 WARN_ON_ONCE((mode & FMODE_EXCL) && !holder);
1213
1214 if ((mode & FMODE_EXCL) && holder) {
1215 whole = bd_start_claiming(bdev, holder);
1216 if (IS_ERR(whole)) {
1217 bdput(bdev);
1218 return PTR_ERR(whole);
1219 }
1220 }
1221
1222 res = __blkdev_get(bdev, mode, 0);
1223
1224 if (whole) {
Tejun Heo6a027ef2010-11-13 11:55:17 +01001225 /* finish claiming */
Tejun Heo77ea8872010-12-08 20:57:37 +01001226 mutex_lock(&bdev->bd_mutex);
Tejun Heo6a027ef2010-11-13 11:55:17 +01001227 spin_lock(&bdev_lock);
1228
Tejun Heo77ea8872010-12-08 20:57:37 +01001229 if (!res) {
Tejun Heo6a027ef2010-11-13 11:55:17 +01001230 BUG_ON(!bd_may_claim(bdev, whole, holder));
1231 /*
1232 * Note that for a whole device bd_holders
1233 * will be incremented twice, and bd_holder
1234 * will be set to bd_may_claim before being
1235 * set to holder
1236 */
1237 whole->bd_holders++;
1238 whole->bd_holder = bd_may_claim;
1239 bdev->bd_holders++;
1240 bdev->bd_holder = holder;
1241 }
1242
1243 /* tell others that we're done */
1244 BUG_ON(whole->bd_claiming != holder);
1245 whole->bd_claiming = NULL;
1246 wake_up_bit(&whole->bd_claiming, 0);
1247
1248 spin_unlock(&bdev_lock);
Tejun Heo77ea8872010-12-08 20:57:37 +01001249
1250 /*
1251 * Block event polling for write claims. Any write
1252 * holder makes the write_holder state stick until all
1253 * are released. This is good enough and tracking
1254 * individual writeable reference is too fragile given
1255 * the way @mode is used in blkdev_get/put().
1256 */
1257 if (!res && (mode & FMODE_WRITE) && !bdev->bd_write_holder) {
1258 bdev->bd_write_holder = true;
1259 disk_block_events(bdev->bd_disk);
1260 }
1261
1262 mutex_unlock(&bdev->bd_mutex);
Tejun Heo6a027ef2010-11-13 11:55:17 +01001263 bdput(whole);
Tejun Heoe525fd82010-11-13 11:55:17 +01001264 }
1265
1266 return res;
NeilBrown37be4122006-12-08 02:36:16 -08001267}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268EXPORT_SYMBOL(blkdev_get);
1269
Tejun Heod4d77622010-11-13 11:55:18 +01001270/**
1271 * blkdev_get_by_path - open a block device by name
1272 * @path: path to the block device to open
1273 * @mode: FMODE_* mask
1274 * @holder: exclusive holder identifier
1275 *
1276 * Open the blockdevice described by the device file at @path. @mode
1277 * and @holder are identical to blkdev_get().
1278 *
1279 * On success, the returned block_device has reference count of one.
1280 *
1281 * CONTEXT:
1282 * Might sleep.
1283 *
1284 * RETURNS:
1285 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1286 */
1287struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
1288 void *holder)
1289{
1290 struct block_device *bdev;
1291 int err;
1292
1293 bdev = lookup_bdev(path);
1294 if (IS_ERR(bdev))
1295 return bdev;
1296
1297 err = blkdev_get(bdev, mode, holder);
1298 if (err)
1299 return ERR_PTR(err);
1300
Chuck Ebberte51900f2011-02-16 18:11:53 -05001301 if ((mode & FMODE_WRITE) && bdev_read_only(bdev)) {
1302 blkdev_put(bdev, mode);
1303 return ERR_PTR(-EACCES);
1304 }
1305
Tejun Heod4d77622010-11-13 11:55:18 +01001306 return bdev;
1307}
1308EXPORT_SYMBOL(blkdev_get_by_path);
1309
1310/**
1311 * blkdev_get_by_dev - open a block device by device number
1312 * @dev: device number of block device to open
1313 * @mode: FMODE_* mask
1314 * @holder: exclusive holder identifier
1315 *
1316 * Open the blockdevice described by device number @dev. @mode and
1317 * @holder are identical to blkdev_get().
1318 *
1319 * Use it ONLY if you really do not have anything better - i.e. when
1320 * you are behind a truly sucky interface and all you are given is a
1321 * device number. _Never_ to be used for internal purposes. If you
1322 * ever need it - reconsider your API.
1323 *
1324 * On success, the returned block_device has reference count of one.
1325 *
1326 * CONTEXT:
1327 * Might sleep.
1328 *
1329 * RETURNS:
1330 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1331 */
1332struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
1333{
1334 struct block_device *bdev;
1335 int err;
1336
1337 bdev = bdget(dev);
1338 if (!bdev)
1339 return ERR_PTR(-ENOMEM);
1340
1341 err = blkdev_get(bdev, mode, holder);
1342 if (err)
1343 return ERR_PTR(err);
1344
1345 return bdev;
1346}
1347EXPORT_SYMBOL(blkdev_get_by_dev);
1348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349static int blkdev_open(struct inode * inode, struct file * filp)
1350{
1351 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 /*
1354 * Preserve backwards compatibility and allow large file access
1355 * even if userspace doesn't ask for it explicitly. Some mkfs
1356 * binary needs it. We might want to drop this workaround
1357 * during an unstable branch.
1358 */
1359 filp->f_flags |= O_LARGEFILE;
1360
Al Viro572c4892007-10-08 13:24:05 -04001361 if (filp->f_flags & O_NDELAY)
1362 filp->f_mode |= FMODE_NDELAY;
1363 if (filp->f_flags & O_EXCL)
1364 filp->f_mode |= FMODE_EXCL;
1365 if ((filp->f_flags & O_ACCMODE) == 3)
1366 filp->f_mode |= FMODE_WRITE_IOCTL;
1367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 bdev = bd_acquire(inode);
Pavel Emelianov6a2aae02006-10-28 10:38:33 -07001369 if (bdev == NULL)
1370 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Al Viro572c4892007-10-08 13:24:05 -04001372 filp->f_mapping = bdev->bd_inode->i_mapping;
1373
Tejun Heoe525fd82010-11-13 11:55:17 +01001374 return blkdev_get(bdev, filp->f_mode, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375}
1376
Al Viro9a1c3542008-02-22 20:40:24 -05001377static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001378{
1379 int ret = 0;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001380 struct gendisk *disk = bdev->bd_disk;
NeilBrown37be4122006-12-08 02:36:16 -08001381 struct block_device *victim = NULL;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001382
NeilBrown6796bf52006-12-08 02:36:16 -08001383 mutex_lock_nested(&bdev->bd_mutex, for_part);
NeilBrown37be4122006-12-08 02:36:16 -08001384 if (for_part)
1385 bdev->bd_part_count--;
1386
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001387 if (!--bdev->bd_openers) {
Tejun Heo6a027ef2010-11-13 11:55:17 +01001388 WARN_ON_ONCE(bdev->bd_holders);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001389 sync_blockdev(bdev);
1390 kill_bdev(bdev);
1391 }
1392 if (bdev->bd_contains == bdev) {
1393 if (disk->fops->release)
Al Viro9a1c3542008-02-22 20:40:24 -05001394 ret = disk->fops->release(disk, mode);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001395 }
1396 if (!bdev->bd_openers) {
1397 struct module *owner = disk->fops->owner;
1398
1399 put_disk(disk);
1400 module_put(owner);
Tejun Heo0762b8b2008-08-25 19:56:12 +09001401 disk_put_part(bdev->bd_part);
1402 bdev->bd_part = NULL;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001403 bdev->bd_disk = NULL;
Dave Chinnera5491e02010-10-21 11:49:26 +11001404 bdev_inode_switch_bdi(bdev->bd_inode,
1405 &default_backing_dev_info);
NeilBrown37be4122006-12-08 02:36:16 -08001406 if (bdev != bdev->bd_contains)
1407 victim = bdev->bd_contains;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001408 bdev->bd_contains = NULL;
1409 }
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001410 mutex_unlock(&bdev->bd_mutex);
1411 bdput(bdev);
NeilBrown37be4122006-12-08 02:36:16 -08001412 if (victim)
Al Viro9a1c3542008-02-22 20:40:24 -05001413 __blkdev_put(victim, mode, 1);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001414 return ret;
1415}
1416
Al Viro9a1c3542008-02-22 20:40:24 -05001417int blkdev_put(struct block_device *bdev, fmode_t mode)
NeilBrown37be4122006-12-08 02:36:16 -08001418{
Tejun Heoe525fd82010-11-13 11:55:17 +01001419 if (mode & FMODE_EXCL) {
Tejun Heo6a027ef2010-11-13 11:55:17 +01001420 bool bdev_free;
1421
1422 /*
1423 * Release a claim on the device. The holder fields
1424 * are protected with bdev_lock. bd_mutex is to
1425 * synchronize disk_holder unlinking.
1426 */
Tejun Heoe525fd82010-11-13 11:55:17 +01001427 mutex_lock(&bdev->bd_mutex);
Tejun Heo6a027ef2010-11-13 11:55:17 +01001428 spin_lock(&bdev_lock);
1429
1430 WARN_ON_ONCE(--bdev->bd_holders < 0);
1431 WARN_ON_ONCE(--bdev->bd_contains->bd_holders < 0);
1432
1433 /* bd_contains might point to self, check in a separate step */
1434 if ((bdev_free = !bdev->bd_holders))
1435 bdev->bd_holder = NULL;
1436 if (!bdev->bd_contains->bd_holders)
1437 bdev->bd_contains->bd_holder = NULL;
1438
1439 spin_unlock(&bdev_lock);
1440
Tejun Heo77ea8872010-12-08 20:57:37 +01001441 /*
1442 * If this was the last claim, remove holder link and
1443 * unblock evpoll if it was a write holder.
1444 */
1445 if (bdev_free) {
Tejun Heo77ea8872010-12-08 20:57:37 +01001446 if (bdev->bd_write_holder) {
1447 disk_unblock_events(bdev->bd_disk);
1448 bdev->bd_write_holder = false;
1449 } else
1450 disk_check_events(bdev->bd_disk);
1451 }
Tejun Heo6a027ef2010-11-13 11:55:17 +01001452
Tejun Heoe525fd82010-11-13 11:55:17 +01001453 mutex_unlock(&bdev->bd_mutex);
Tejun Heo77ea8872010-12-08 20:57:37 +01001454 } else
1455 disk_check_events(bdev->bd_disk);
1456
Al Viro9a1c3542008-02-22 20:40:24 -05001457 return __blkdev_put(bdev, mode, 0);
NeilBrown37be4122006-12-08 02:36:16 -08001458}
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001459EXPORT_SYMBOL(blkdev_put);
1460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461static int blkdev_close(struct inode * inode, struct file * filp)
1462{
1463 struct block_device *bdev = I_BDEV(filp->f_mapping->host);
Tejun Heoe525fd82010-11-13 11:55:17 +01001464
Al Viro9a1c3542008-02-22 20:40:24 -05001465 return blkdev_put(bdev, filp->f_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466}
1467
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07001468static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
Al Viro56b26ad2008-09-19 03:17:36 -04001470 struct block_device *bdev = I_BDEV(file->f_mapping->host);
1471 fmode_t mode = file->f_mode;
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +01001472
1473 /*
1474 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
1475 * to updated it before every ioctl.
1476 */
Al Viro56b26ad2008-09-19 03:17:36 -04001477 if (file->f_flags & O_NDELAY)
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +01001478 mode |= FMODE_NDELAY;
1479 else
1480 mode &= ~FMODE_NDELAY;
1481
Al Viro56b26ad2008-09-19 03:17:36 -04001482 return blkdev_ioctl(bdev, mode, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483}
1484
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05001485/*
Christoph Hellwigeef99382009-08-20 17:43:41 +02001486 * Write data to the block device. Only intended for the block device itself
1487 * and the raw driver which basically is a fake block device.
1488 *
1489 * Does not take i_mutex for the write and thus is not for general purpose
1490 * use.
1491 */
1492ssize_t blkdev_aio_write(struct kiocb *iocb, const struct iovec *iov,
1493 unsigned long nr_segs, loff_t pos)
1494{
1495 struct file *file = iocb->ki_filp;
1496 ssize_t ret;
1497
1498 BUG_ON(iocb->ki_pos != pos);
1499
1500 ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos);
1501 if (ret > 0 || ret == -EIOCBQUEUED) {
1502 ssize_t err;
1503
1504 err = generic_write_sync(file, pos, ret);
1505 if (err < 0 && ret > 0)
1506 ret = err;
1507 }
1508 return ret;
1509}
1510EXPORT_SYMBOL_GPL(blkdev_aio_write);
1511
1512/*
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05001513 * Try to release a page associated with block device when the system
1514 * is under memory pressure.
1515 */
1516static int blkdev_releasepage(struct page *page, gfp_t wait)
1517{
1518 struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
1519
1520 if (super && super->s_op->bdev_try_to_free_page)
1521 return super->s_op->bdev_try_to_free_page(super, page, wait);
1522
1523 return try_to_free_buffers(page);
1524}
1525
Adrian Bunk4c54ac62008-02-18 13:48:31 +01001526static const struct address_space_operations def_blk_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 .readpage = blkdev_readpage,
1528 .writepage = blkdev_writepage,
1529 .sync_page = block_sync_page,
Nick Piggin6272b5a2007-10-16 01:25:04 -07001530 .write_begin = blkdev_write_begin,
1531 .write_end = blkdev_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 .writepages = generic_writepages,
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05001533 .releasepage = blkdev_releasepage,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 .direct_IO = blkdev_direct_IO,
1535};
1536
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001537const struct file_operations def_blk_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 .open = blkdev_open,
1539 .release = blkdev_close,
1540 .llseek = block_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -07001541 .read = do_sync_read,
1542 .write = do_sync_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 .aio_read = generic_file_aio_read,
Christoph Hellwigeef99382009-08-20 17:43:41 +02001544 .aio_write = blkdev_aio_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 .mmap = generic_file_mmap,
Andrew Mortonb1dd3b22010-04-06 14:35:00 -07001546 .fsync = blkdev_fsync,
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07001547 .unlocked_ioctl = block_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548#ifdef CONFIG_COMPAT
1549 .compat_ioctl = compat_blkdev_ioctl,
1550#endif
Jens Axboe7f9c51f2006-05-01 19:59:32 +02001551 .splice_read = generic_file_splice_read,
1552 .splice_write = generic_file_splice_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553};
1554
1555int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
1556{
1557 int res;
1558 mm_segment_t old_fs = get_fs();
1559 set_fs(KERNEL_DS);
Al Viro56b26ad2008-09-19 03:17:36 -04001560 res = blkdev_ioctl(bdev, 0, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 set_fs(old_fs);
1562 return res;
1563}
1564
1565EXPORT_SYMBOL(ioctl_by_bdev);
1566
1567/**
1568 * lookup_bdev - lookup a struct block_device by name
Randy Dunlap94e29592009-01-06 14:41:15 -08001569 * @pathname: special file representing the block device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 *
Randy Dunlap57d1b532008-10-09 10:42:38 +02001571 * Get a reference to the blockdevice at @pathname in the current
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 * namespace if possible and return it. Return ERR_PTR(error)
1573 * otherwise.
1574 */
Al Viro421748e2008-08-02 01:04:36 -04001575struct block_device *lookup_bdev(const char *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
1577 struct block_device *bdev;
1578 struct inode *inode;
Al Viro421748e2008-08-02 01:04:36 -04001579 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 int error;
1581
Al Viro421748e2008-08-02 01:04:36 -04001582 if (!pathname || !*pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 return ERR_PTR(-EINVAL);
1584
Al Viro421748e2008-08-02 01:04:36 -04001585 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 if (error)
1587 return ERR_PTR(error);
1588
Al Viro421748e2008-08-02 01:04:36 -04001589 inode = path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 error = -ENOTBLK;
1591 if (!S_ISBLK(inode->i_mode))
1592 goto fail;
1593 error = -EACCES;
Al Viro421748e2008-08-02 01:04:36 -04001594 if (path.mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 goto fail;
1596 error = -ENOMEM;
1597 bdev = bd_acquire(inode);
1598 if (!bdev)
1599 goto fail;
1600out:
Al Viro421748e2008-08-02 01:04:36 -04001601 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 return bdev;
1603fail:
1604 bdev = ERR_PTR(error);
1605 goto out;
1606}
Al Virod5686b42008-08-01 05:00:11 -04001607EXPORT_SYMBOL(lookup_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
David Howellsb71e8a42006-08-29 19:06:11 +01001609int __invalidate_device(struct block_device *bdev)
1610{
1611 struct super_block *sb = get_super(bdev);
1612 int res = 0;
1613
1614 if (sb) {
1615 /*
1616 * no need to lock the super, get_super holds the
1617 * read mutex so the filesystem cannot go away
1618 * under us (->put_super runs with the write lock
1619 * hold).
1620 */
1621 shrink_dcache_sb(sb);
1622 res = invalidate_inodes(sb);
1623 drop_super(sb);
1624 }
Peter Zijlstraf98393a2007-05-06 14:49:54 -07001625 invalidate_bdev(bdev);
David Howellsb71e8a42006-08-29 19:06:11 +01001626 return res;
1627}
1628EXPORT_SYMBOL(__invalidate_device);