Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #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 Emelyanov | 7db9cfd | 2008-06-05 22:46:27 -0700 | [diff] [blame] | 14 | #include <linux/device_cgroup.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #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 Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 20 | #include <linux/pagevec.h> |
David Howells | 811d736 | 2006-08-29 19:06:09 +0100 | [diff] [blame] | 21 | #include <linux/writeback.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/mpage.h> |
| 23 | #include <linux/mount.h> |
| 24 | #include <linux/uio.h> |
| 25 | #include <linux/namei.h> |
Vignesh Babu BM | 1368c4f | 2007-05-08 00:24:32 -0700 | [diff] [blame] | 26 | #include <linux/log2.h> |
Catalin Marinas | 2e1483c | 2009-06-11 13:24:13 +0100 | [diff] [blame] | 27 | #include <linux/kmemleak.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <asm/uaccess.h> |
David Howells | 07f3f05 | 2006-09-30 20:52:18 +0200 | [diff] [blame] | 29 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | struct bdev_inode { |
| 32 | struct block_device bdev; |
| 33 | struct inode vfs_inode; |
| 34 | }; |
| 35 | |
Adrian Bunk | 4c54ac6 | 2008-02-18 13:48:31 +0100 | [diff] [blame] | 36 | static const struct address_space_operations def_blk_aops; |
| 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | static inline struct bdev_inode *BDEV_I(struct inode *inode) |
| 39 | { |
| 40 | return container_of(inode, struct bdev_inode, vfs_inode); |
| 41 | } |
| 42 | |
| 43 | inline struct block_device *I_BDEV(struct inode *inode) |
| 44 | { |
| 45 | return &BDEV_I(inode)->bdev; |
| 46 | } |
| 47 | |
| 48 | EXPORT_SYMBOL(I_BDEV); |
| 49 | |
Dave Chinner | a5491e0 | 2010-10-21 11:49:26 +1100 | [diff] [blame] | 50 | /* |
| 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 | */ |
| 55 | static 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 Piggin | 7ccf19a | 2010-10-21 11:49:30 +1100 | [diff] [blame] | 61 | list_move(&inode->i_wb_list, &dst->wb.b_dirty); |
Dave Chinner | a5491e0 | 2010-10-21 11:49:26 +1100 | [diff] [blame] | 62 | spin_unlock(&inode_lock); |
| 63 | } |
| 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | static 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 Zijlstra | f9a1439 | 2007-05-06 14:49:55 -0700 | [diff] [blame] | 78 | /* Kill _all_ buffers and pagecache , dirty or not.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | static void kill_bdev(struct block_device *bdev) |
| 80 | { |
Peter Zijlstra | f9a1439 | 2007-05-06 14:49:55 -0700 | [diff] [blame] | 81 | if (bdev->bd_inode->i_mapping->nrpages == 0) |
| 82 | return; |
| 83 | invalidate_bh_lrus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | truncate_inode_pages(bdev->bd_inode->i_mapping, 0); |
| 85 | } |
| 86 | |
| 87 | int 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 BM | 1368c4f | 2007-05-08 00:24:32 -0700 | [diff] [blame] | 90 | if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | return -EINVAL; |
| 92 | |
| 93 | /* Size cannot be smaller than the size supported by the device */ |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 94 | if (size < bdev_logical_block_size(bdev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | 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 | |
| 107 | EXPORT_SYMBOL(set_blocksize); |
| 108 | |
| 109 | int sb_set_blocksize(struct super_block *sb, int size) |
| 110 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | 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 Hunt | 38885bd | 2006-03-24 03:18:05 -0800 | [diff] [blame] | 116 | sb->s_blocksize_bits = blksize_bits(size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | return sb->s_blocksize; |
| 118 | } |
| 119 | |
| 120 | EXPORT_SYMBOL(sb_set_blocksize); |
| 121 | |
| 122 | int sb_min_blocksize(struct super_block *sb, int size) |
| 123 | { |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 124 | int minsize = bdev_logical_block_size(sb->s_bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | if (size < minsize) |
| 126 | size = minsize; |
| 127 | return sb_set_blocksize(sb, size); |
| 128 | } |
| 129 | |
| 130 | EXPORT_SYMBOL(sb_min_blocksize); |
| 131 | |
| 132 | static int |
| 133 | blkdev_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 Morton | b2e895d | 2007-02-03 01:14:01 -0800 | [diff] [blame] | 154 | static int |
| 155 | blkdev_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 | |
| 182 | static ssize_t |
| 183 | blkdev_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 Hellwig | eafdc7d | 2010-06-04 11:29:53 +0200 | [diff] [blame] | 189 | return __blockdev_direct_IO(rw, iocb, inode, I_BDEV(inode), iov, offset, |
| 190 | nr_segs, blkdev_get_blocks, NULL, NULL, 0); |
Andrew Morton | b2e895d | 2007-02-03 01:14:01 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Jan Kara | 5cee581 | 2009-04-27 16:43:51 +0200 | [diff] [blame] | 193 | int __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 Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 202 | /* |
| 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 | */ |
| 206 | int sync_blockdev(struct block_device *bdev) |
| 207 | { |
Jan Kara | 5cee581 | 2009-04-27 16:43:51 +0200 | [diff] [blame] | 208 | return __sync_blockdev(bdev, 1); |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 209 | } |
| 210 | EXPORT_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 | */ |
| 217 | int fsync_bdev(struct block_device *bdev) |
| 218 | { |
| 219 | struct super_block *sb = get_super(bdev); |
| 220 | if (sb) { |
Jan Kara | 60b0680 | 2009-04-27 16:43:53 +0200 | [diff] [blame] | 221 | int res = sync_filesystem(sb); |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 222 | drop_super(sb); |
| 223 | return res; |
| 224 | } |
| 225 | return sync_blockdev(bdev); |
| 226 | } |
Al Viro | 47e4491 | 2009-04-01 07:07:16 -0400 | [diff] [blame] | 227 | EXPORT_SYMBOL(fsync_bdev); |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 228 | |
| 229 | /** |
| 230 | * freeze_bdev -- lock a filesystem and force it into a consistent state |
| 231 | * @bdev: blockdevice to lock |
| 232 | * |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 233 | * 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 | */ |
| 241 | struct 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 Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 247 | 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 Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 253 | sb = get_super(bdev); |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 254 | drop_super(sb); |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 255 | mutex_unlock(&bdev->bd_fsfreeze_mutex); |
| 256 | return sb; |
| 257 | } |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 258 | |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 259 | sb = get_active_super(bdev); |
| 260 | if (!sb) |
| 261 | goto out; |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 262 | error = freeze_super(sb); |
| 263 | if (error) { |
| 264 | deactivate_super(sb); |
| 265 | bdev->bd_fsfreeze_count--; |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 266 | mutex_unlock(&bdev->bd_fsfreeze_mutex); |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 267 | return ERR_PTR(error); |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 268 | } |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 269 | deactivate_super(sb); |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 270 | out: |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 271 | sync_blockdev(bdev); |
| 272 | mutex_unlock(&bdev->bd_fsfreeze_mutex); |
Christoph Hellwig | 4fadd7b | 2009-08-03 23:28:06 +0200 | [diff] [blame] | 273 | return sb; /* thaw_bdev releases s->s_umount */ |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 274 | } |
| 275 | EXPORT_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 | */ |
| 284 | int thaw_bdev(struct block_device *bdev, struct super_block *sb) |
| 285 | { |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 286 | int error = -EINVAL; |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 287 | |
| 288 | mutex_lock(&bdev->bd_fsfreeze_mutex); |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 289 | if (!bdev->bd_fsfreeze_count) |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 290 | goto out; |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 291 | |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 292 | error = 0; |
| 293 | if (--bdev->bd_fsfreeze_count > 0) |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 294 | goto out; |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 295 | |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 296 | if (!sb) |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 297 | goto out; |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 298 | |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 299 | error = thaw_super(sb); |
| 300 | if (error) { |
| 301 | bdev->bd_fsfreeze_count++; |
| 302 | mutex_unlock(&bdev->bd_fsfreeze_mutex); |
| 303 | return error; |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 304 | } |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 305 | out: |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 306 | mutex_unlock(&bdev->bd_fsfreeze_mutex); |
| 307 | return 0; |
| 308 | } |
| 309 | EXPORT_SYMBOL(thaw_bdev); |
| 310 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | static int blkdev_writepage(struct page *page, struct writeback_control *wbc) |
| 312 | { |
| 313 | return block_write_full_page(page, blkdev_get_block, wbc); |
| 314 | } |
| 315 | |
| 316 | static int blkdev_readpage(struct file * file, struct page * page) |
| 317 | { |
| 318 | return block_read_full_page(page, blkdev_get_block); |
| 319 | } |
| 320 | |
Nick Piggin | 6272b5a | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 321 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | { |
Christoph Hellwig | 155130a | 2010-06-04 11:29:58 +0200 | [diff] [blame] | 325 | return block_write_begin(mapping, pos, len, flags, pagep, |
| 326 | blkdev_get_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Nick Piggin | 6272b5a | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 329 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | { |
Nick Piggin | 6272b5a | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 333 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /* |
| 343 | * private llseek: |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 344 | * for a block special file file->f_path.dentry->d_inode->i_size is zero |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | * so we compute the size by hand (just as in block_read/write above) |
| 346 | */ |
| 347 | static 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 Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 353 | mutex_lock(&bd_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | 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 Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 370 | mutex_unlock(&bd_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | return retval; |
| 372 | } |
| 373 | |
Christoph Hellwig | 7ea8085 | 2010-05-26 17:53:25 +0200 | [diff] [blame] | 374 | int blkdev_fsync(struct file *filp, int datasync) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | { |
Anton Blanchard | b8af67e | 2010-04-23 13:18:06 -0400 | [diff] [blame] | 376 | struct inode *bd_inode = filp->f_mapping->host; |
| 377 | struct block_device *bdev = I_BDEV(bd_inode); |
Christoph Hellwig | ab0a973 | 2009-10-29 14:14:04 +0100 | [diff] [blame] | 378 | int error; |
| 379 | |
Anton Blanchard | b8af67e | 2010-04-23 13:18:06 -0400 | [diff] [blame] | 380 | /* |
| 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 Hellwig | dd3932e | 2010-09-16 20:51:46 +0200 | [diff] [blame] | 387 | error = blkdev_issue_flush(bdev, GFP_KERNEL, NULL); |
Christoph Hellwig | ab0a973 | 2009-10-29 14:14:04 +0100 | [diff] [blame] | 388 | if (error == -EOPNOTSUPP) |
| 389 | error = 0; |
Anton Blanchard | b8af67e | 2010-04-23 13:18:06 -0400 | [diff] [blame] | 390 | |
| 391 | mutex_lock(&bd_inode->i_mutex); |
| 392 | |
Christoph Hellwig | ab0a973 | 2009-10-29 14:14:04 +0100 | [diff] [blame] | 393 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | } |
Andrew Morton | b1dd3b2 | 2010-04-06 14:35:00 -0700 | [diff] [blame] | 395 | EXPORT_SYMBOL(blkdev_fsync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | |
| 397 | /* |
| 398 | * pseudo-fs |
| 399 | */ |
| 400 | |
| 401 | static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock); |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 402 | static struct kmem_cache * bdev_cachep __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
| 404 | static struct inode *bdev_alloc_inode(struct super_block *sb) |
| 405 | { |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 406 | struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | if (!ei) |
| 408 | return NULL; |
| 409 | return &ei->vfs_inode; |
| 410 | } |
| 411 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 412 | static void bdev_i_callback(struct rcu_head *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | { |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 414 | struct inode *inode = container_of(head, struct inode, i_rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | struct bdev_inode *bdi = BDEV_I(inode); |
| 416 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 417 | INIT_LIST_HEAD(&inode->i_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | kmem_cache_free(bdev_cachep, bdi); |
| 419 | } |
| 420 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 421 | static void bdev_destroy_inode(struct inode *inode) |
| 422 | { |
| 423 | call_rcu(&inode->i_rcu, bdev_i_callback); |
| 424 | } |
| 425 | |
Alexey Dobriyan | 51cc506 | 2008-07-25 19:45:34 -0700 | [diff] [blame] | 426 | static void init_once(void *foo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | { |
| 428 | struct bdev_inode *ei = (struct bdev_inode *) foo; |
| 429 | struct block_device *bdev = &ei->bdev; |
| 430 | |
Christoph Lameter | a35afb8 | 2007-05-16 22:10:57 -0700 | [diff] [blame] | 431 | memset(bdev, 0, sizeof(*bdev)); |
| 432 | mutex_init(&bdev->bd_mutex); |
Christoph Lameter | a35afb8 | 2007-05-16 22:10:57 -0700 | [diff] [blame] | 433 | INIT_LIST_HEAD(&bdev->bd_inodes); |
| 434 | INIT_LIST_HEAD(&bdev->bd_list); |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 435 | #ifdef CONFIG_SYSFS |
| 436 | INIT_LIST_HEAD(&bdev->bd_holder_disks); |
| 437 | #endif |
Christoph Lameter | a35afb8 | 2007-05-16 22:10:57 -0700 | [diff] [blame] | 438 | inode_init_once(&ei->vfs_inode); |
Takashi Sato | fcccf50 | 2009-01-09 16:40:59 -0800 | [diff] [blame] | 439 | /* Initialize mutex for freeze. */ |
| 440 | mutex_init(&bdev->bd_fsfreeze_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | static 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 Viro | b57922d | 2010-06-07 14:34:48 -0400 | [diff] [blame] | 450 | static void bdev_evict_inode(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | { |
| 452 | struct block_device *bdev = &BDEV_I(inode)->bdev; |
| 453 | struct list_head *p; |
Al Viro | b57922d | 2010-06-07 14:34:48 -0400 | [diff] [blame] | 454 | truncate_inode_pages(&inode->i_data, 0); |
| 455 | invalidate_inode_buffers(inode); /* is it needed here? */ |
| 456 | end_writeback(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | 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' Sipek | ee9b6d6 | 2007-02-12 00:55:41 -0800 | [diff] [blame] | 465 | static const struct super_operations bdev_sops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | .statfs = simple_statfs, |
| 467 | .alloc_inode = bdev_alloc_inode, |
| 468 | .destroy_inode = bdev_destroy_inode, |
| 469 | .drop_inode = generic_delete_inode, |
Al Viro | b57922d | 2010-06-07 14:34:48 -0400 | [diff] [blame] | 470 | .evict_inode = bdev_evict_inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | }; |
| 472 | |
Al Viro | 51139ad | 2010-07-25 23:47:46 +0400 | [diff] [blame] | 473 | static struct dentry *bd_mount(struct file_system_type *fs_type, |
| 474 | int flags, const char *dev_name, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | { |
Al Viro | c74a1cb | 2011-01-12 16:59:34 -0500 | [diff] [blame] | 476 | return mount_pseudo(fs_type, "bdev:", &bdev_sops, NULL, 0x62646576); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | static struct file_system_type bd_type = { |
| 480 | .name = "bdev", |
Al Viro | 51139ad | 2010-07-25 23:47:46 +0400 | [diff] [blame] | 481 | .mount = bd_mount, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | .kill_sb = kill_anon_super, |
| 483 | }; |
| 484 | |
Denis ChengRq | c2acf7b | 2008-12-01 14:34:56 -0800 | [diff] [blame] | 485 | struct super_block *blockdev_superblock __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | |
| 487 | void __init bdev_cache_init(void) |
| 488 | { |
| 489 | int err; |
Denis ChengRq | c2acf7b | 2008-12-01 14:34:56 -0800 | [diff] [blame] | 490 | struct vfsmount *bd_mnt; |
| 491 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode), |
Paul Jackson | fffb60f | 2006-03-24 03:16:06 -0800 | [diff] [blame] | 493 | 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT| |
| 494 | SLAB_MEM_SPREAD|SLAB_PANIC), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 495 | init_once); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | err = register_filesystem(&bd_type); |
| 497 | if (err) |
| 498 | panic("Cannot register bdev pseudo-fs"); |
| 499 | bd_mnt = kern_mount(&bd_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | if (IS_ERR(bd_mnt)) |
| 501 | panic("Cannot create bdev pseudo-fs"); |
Catalin Marinas | 2e1483c | 2009-06-11 13:24:13 +0100 | [diff] [blame] | 502 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | 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 | */ |
| 515 | static inline unsigned long hash(dev_t dev) |
| 516 | { |
| 517 | return MAJOR(dev)+MINOR(dev); |
| 518 | } |
| 519 | |
| 520 | static int bdev_test(struct inode *inode, void *data) |
| 521 | { |
| 522 | return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data; |
| 523 | } |
| 524 | |
| 525 | static int bdev_set(struct inode *inode, void *data) |
| 526 | { |
| 527 | BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data; |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | static LIST_HEAD(all_bdevs); |
| 532 | |
| 533 | struct block_device *bdget(dev_t dev) |
| 534 | { |
| 535 | struct block_device *bdev; |
| 536 | struct inode *inode; |
| 537 | |
Denis ChengRq | c2acf7b | 2008-12-01 14:34:56 -0800 | [diff] [blame] | 538 | inode = iget5_locked(blockdev_superblock, hash(dev), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | 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 | |
| 566 | EXPORT_SYMBOL(bdget); |
| 567 | |
Alan Jenkins | dddac6a | 2009-07-29 21:07:55 +0200 | [diff] [blame] | 568 | /** |
| 569 | * bdgrab -- Grab a reference to an already referenced block device |
| 570 | * @bdev: Block device to grab a reference to. |
| 571 | */ |
| 572 | struct block_device *bdgrab(struct block_device *bdev) |
| 573 | { |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 574 | ihold(bdev->bd_inode); |
Alan Jenkins | dddac6a | 2009-07-29 21:07:55 +0200 | [diff] [blame] | 575 | return bdev; |
| 576 | } |
| 577 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | long nr_blockdev_pages(void) |
| 579 | { |
Matthias Kaehlcke | 203a293 | 2007-07-15 23:40:23 -0700 | [diff] [blame] | 580 | struct block_device *bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | long ret = 0; |
| 582 | spin_lock(&bdev_lock); |
Matthias Kaehlcke | 203a293 | 2007-07-15 23:40:23 -0700 | [diff] [blame] | 583 | list_for_each_entry(bdev, &all_bdevs, bd_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | ret += bdev->bd_inode->i_mapping->nrpages; |
| 585 | } |
| 586 | spin_unlock(&bdev_lock); |
| 587 | return ret; |
| 588 | } |
| 589 | |
| 590 | void bdput(struct block_device *bdev) |
| 591 | { |
| 592 | iput(bdev->bd_inode); |
| 593 | } |
| 594 | |
| 595 | EXPORT_SYMBOL(bdput); |
| 596 | |
| 597 | static struct block_device *bd_acquire(struct inode *inode) |
| 598 | { |
| 599 | struct block_device *bdev; |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 600 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | spin_lock(&bdev_lock); |
| 602 | bdev = inode->i_bdev; |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 603 | if (bdev) { |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 604 | ihold(bdev->bd_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | spin_unlock(&bdev_lock); |
| 606 | return bdev; |
| 607 | } |
| 608 | spin_unlock(&bdev_lock); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 609 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | bdev = bdget(inode->i_rdev); |
| 611 | if (bdev) { |
| 612 | spin_lock(&bdev_lock); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 613 | if (!inode->i_bdev) { |
| 614 | /* |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 615 | * We take an additional reference to bd_inode, |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 616 | * and it's released in clear_inode() of inode. |
| 617 | * So, we can access it via ->i_mapping always |
| 618 | * without igrab(). |
| 619 | */ |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 620 | ihold(bdev->bd_inode); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 621 | inode->i_bdev = bdev; |
| 622 | inode->i_mapping = bdev->bd_inode->i_mapping; |
| 623 | list_add(&inode->i_devices, &bdev->bd_inodes); |
| 624 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | spin_unlock(&bdev_lock); |
| 626 | } |
| 627 | return bdev; |
| 628 | } |
| 629 | |
| 630 | /* Call when you free inode */ |
| 631 | |
| 632 | void bd_forget(struct inode *inode) |
| 633 | { |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 634 | struct block_device *bdev = NULL; |
| 635 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | spin_lock(&bdev_lock); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 637 | if (inode->i_bdev) { |
Denis ChengRq | c2acf7b | 2008-12-01 14:34:56 -0800 | [diff] [blame] | 638 | if (!sb_is_blkdev_sb(inode->i_sb)) |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 639 | bdev = inode->i_bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | __bd_forget(inode); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 641 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | spin_unlock(&bdev_lock); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 643 | |
| 644 | if (bdev) |
| 645 | iput(bdev->bd_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Tejun Heo | 1a3cbbc | 2010-04-07 18:52:29 +0900 | [diff] [blame] | 648 | /** |
| 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 | */ |
| 662 | static 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 Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 672 | else if (whole->bd_holder == bd_may_claim) |
Tejun Heo | 1a3cbbc | 2010-04-07 18:52:29 +0900 | [diff] [blame] | 673 | 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 Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 681 | * 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 | */ |
| 698 | static int bd_prepare_to_claim(struct block_device *bdev, |
| 699 | struct block_device *whole, void *holder) |
| 700 | { |
| 701 | retry: |
| 702 | /* if someone else claimed, fail */ |
| 703 | if (!bd_may_claim(bdev, whole, holder)) |
| 704 | return -EBUSY; |
| 705 | |
Tejun Heo | e75aa85 | 2010-08-04 17:59:39 +0200 | [diff] [blame] | 706 | /* if claiming is already in progress, wait for it to finish */ |
| 707 | if (whole->bd_claiming) { |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 708 | 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 Piggin | b001836 | 2010-05-26 01:51:19 +1000 | [diff] [blame] | 731 | * 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 Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 738 | * |
| 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 | */ |
| 746 | static 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 Piggin | cf34257 | 2010-05-26 01:50:21 +1000 | [diff] [blame] | 764 | module_put(disk->fops->owner); |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 765 | 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 Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 784 | #ifdef CONFIG_SYSFS |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 785 | struct bd_holder_disk { |
| 786 | struct list_head list; |
| 787 | struct gendisk *disk; |
| 788 | int refcnt; |
| 789 | }; |
| 790 | |
| 791 | static 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 Morton | 4d7dd8f | 2006-09-29 01:58:56 -0700 | [diff] [blame] | 802 | static int add_symlink(struct kobject *from, struct kobject *to) |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 803 | { |
Andrew Morton | 4d7dd8f | 2006-09-29 01:58:56 -0700 | [diff] [blame] | 804 | return sysfs_create_link(from, to, kobject_name(to)); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | static void del_symlink(struct kobject *from, struct kobject *to) |
| 808 | { |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 809 | sysfs_remove_link(from, kobject_name(to)); |
| 810 | } |
| 811 | |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 812 | /** |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 813 | * 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 Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 816 | * |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 817 | * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT. |
| 818 | * |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 819 | * This functions creates the following sysfs symlinks. |
Jun'ichi Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 820 | * |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 821 | * - 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 Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 839 | */ |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 840 | int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk) |
Jun'ichi Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 841 | { |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 842 | struct bd_holder_disk *holder; |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 843 | int ret = 0; |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 844 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 845 | mutex_lock(&bdev->bd_mutex); |
Jun'ichi Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 846 | |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 847 | WARN_ON_ONCE(!bdev->bd_holder); |
Johannes Weiner | 4e91672 | 2007-07-15 23:41:25 -0700 | [diff] [blame] | 848 | |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 849 | /* 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 Weiner | 4e91672 | 2007-07-15 23:41:25 -0700 | [diff] [blame] | 852 | |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 853 | holder = bd_find_holder_disk(bdev, disk); |
| 854 | if (holder) { |
| 855 | holder->refcnt++; |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 856 | goto out_unlock; |
| 857 | } |
| 858 | |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 859 | 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 Heo | e7407d1 | 2011-02-24 09:56:32 +0100 | [diff] [blame] | 876 | /* |
| 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 Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 881 | |
| 882 | list_add(&holder->list, &bdev->bd_holder_disks); |
| 883 | goto out_unlock; |
| 884 | |
| 885 | out_del: |
| 886 | del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj); |
| 887 | out_free: |
| 888 | kfree(holder); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 889 | out_unlock: |
Jun'ichi Nomura | b4cf1b7 | 2006-03-27 01:18:00 -0800 | [diff] [blame] | 890 | mutex_unlock(&bdev->bd_mutex); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 891 | return ret; |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 892 | } |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 893 | EXPORT_SYMBOL_GPL(bd_link_disk_holder); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 894 | |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 895 | /** |
| 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 | */ |
| 905 | void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk) |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 906 | { |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 907 | struct bd_holder_disk *holder; |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 908 | |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 909 | mutex_lock(&bdev->bd_mutex); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 910 | |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 911 | 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 Heo | e7407d1 | 2011-02-24 09:56:32 +0100 | [diff] [blame] | 917 | kobject_put(bdev->bd_part->holder_dir); |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 918 | list_del_init(&holder->list); |
| 919 | kfree(holder); |
| 920 | } |
| 921 | |
| 922 | mutex_unlock(&bdev->bd_mutex); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 923 | } |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 924 | EXPORT_SYMBOL_GPL(bd_unlink_disk_holder); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 925 | #endif |
| 926 | |
Andrew Patterson | 0c002c2 | 2008-09-04 14:27:20 -0600 | [diff] [blame] | 927 | /** |
Andrew Patterson | 56ade44 | 2008-09-04 14:27:40 -0600 | [diff] [blame] | 928 | * flush_disk - invalidates all buffer-cache entries on a disk |
| 929 | * |
| 930 | * @bdev: struct block device to be flushed |
Randy Dunlap | e6eb5ce | 2011-02-26 10:54:00 -0800 | [diff] [blame] | 931 | * @kill_dirty: flag to guide handling of dirty inodes |
Andrew Patterson | 56ade44 | 2008-09-04 14:27:40 -0600 | [diff] [blame] | 932 | * |
| 933 | * Invalidates all buffer-cache entries on a disk. It should be called |
| 934 | * when a disk has been changed -- either by a media change or online |
| 935 | * resize. |
| 936 | */ |
NeilBrown | 93b270f | 2011-02-24 17:25:47 +1100 | [diff] [blame] | 937 | static void flush_disk(struct block_device *bdev, bool kill_dirty) |
Andrew Patterson | 56ade44 | 2008-09-04 14:27:40 -0600 | [diff] [blame] | 938 | { |
NeilBrown | 93b270f | 2011-02-24 17:25:47 +1100 | [diff] [blame] | 939 | if (__invalidate_device(bdev, kill_dirty)) { |
Andrew Patterson | 56ade44 | 2008-09-04 14:27:40 -0600 | [diff] [blame] | 940 | char name[BDEVNAME_SIZE] = ""; |
| 941 | |
| 942 | if (bdev->bd_disk) |
| 943 | disk_name(bdev->bd_disk, 0, name); |
| 944 | printk(KERN_WARNING "VFS: busy inodes on changed media or " |
| 945 | "resized disk %s\n", name); |
| 946 | } |
| 947 | |
| 948 | if (!bdev->bd_disk) |
| 949 | return; |
| 950 | if (disk_partitionable(bdev->bd_disk)) |
| 951 | bdev->bd_invalidated = 1; |
| 952 | } |
| 953 | |
| 954 | /** |
Randy Dunlap | 57d1b53 | 2008-10-09 10:42:38 +0200 | [diff] [blame] | 955 | * check_disk_size_change - checks for disk size change and adjusts bdev size. |
Andrew Patterson | c3279d1 | 2008-09-04 14:27:25 -0600 | [diff] [blame] | 956 | * @disk: struct gendisk to check |
| 957 | * @bdev: struct bdev to adjust. |
| 958 | * |
| 959 | * This routine checks to see if the bdev size does not match the disk size |
| 960 | * and adjusts it if it differs. |
| 961 | */ |
| 962 | void check_disk_size_change(struct gendisk *disk, struct block_device *bdev) |
| 963 | { |
| 964 | loff_t disk_size, bdev_size; |
| 965 | |
| 966 | disk_size = (loff_t)get_capacity(disk) << 9; |
| 967 | bdev_size = i_size_read(bdev->bd_inode); |
| 968 | if (disk_size != bdev_size) { |
| 969 | char name[BDEVNAME_SIZE]; |
| 970 | |
| 971 | disk_name(disk, 0, name); |
| 972 | printk(KERN_INFO |
| 973 | "%s: detected capacity change from %lld to %lld\n", |
| 974 | name, bdev_size, disk_size); |
| 975 | i_size_write(bdev->bd_inode, disk_size); |
NeilBrown | 93b270f | 2011-02-24 17:25:47 +1100 | [diff] [blame] | 976 | flush_disk(bdev, false); |
Andrew Patterson | c3279d1 | 2008-09-04 14:27:25 -0600 | [diff] [blame] | 977 | } |
| 978 | } |
| 979 | EXPORT_SYMBOL(check_disk_size_change); |
| 980 | |
| 981 | /** |
Randy Dunlap | 57d1b53 | 2008-10-09 10:42:38 +0200 | [diff] [blame] | 982 | * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back |
Andrew Patterson | 0c002c2 | 2008-09-04 14:27:20 -0600 | [diff] [blame] | 983 | * @disk: struct gendisk to be revalidated |
| 984 | * |
| 985 | * This routine is a wrapper for lower-level driver's revalidate_disk |
| 986 | * call-backs. It is used to do common pre and post operations needed |
| 987 | * for all revalidate_disk operations. |
| 988 | */ |
| 989 | int revalidate_disk(struct gendisk *disk) |
| 990 | { |
Andrew Patterson | c3279d1 | 2008-09-04 14:27:25 -0600 | [diff] [blame] | 991 | struct block_device *bdev; |
Andrew Patterson | 0c002c2 | 2008-09-04 14:27:20 -0600 | [diff] [blame] | 992 | int ret = 0; |
| 993 | |
| 994 | if (disk->fops->revalidate_disk) |
| 995 | ret = disk->fops->revalidate_disk(disk); |
| 996 | |
Andrew Patterson | c3279d1 | 2008-09-04 14:27:25 -0600 | [diff] [blame] | 997 | bdev = bdget_disk(disk, 0); |
| 998 | if (!bdev) |
| 999 | return ret; |
| 1000 | |
| 1001 | mutex_lock(&bdev->bd_mutex); |
| 1002 | check_disk_size_change(disk, bdev); |
| 1003 | mutex_unlock(&bdev->bd_mutex); |
| 1004 | bdput(bdev); |
Andrew Patterson | 0c002c2 | 2008-09-04 14:27:20 -0600 | [diff] [blame] | 1005 | return ret; |
| 1006 | } |
| 1007 | EXPORT_SYMBOL(revalidate_disk); |
| 1008 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | /* |
| 1010 | * This routine checks whether a removable media has been changed, |
| 1011 | * and invalidates all buffer-cache-entries in that case. This |
| 1012 | * is a relatively slow routine, so we have to try to minimize using |
| 1013 | * it. Thus it is called only upon a 'mount' or 'open'. This |
| 1014 | * is the best way of combining speed and utility, I think. |
| 1015 | * People changing diskettes in the middle of an operation deserve |
| 1016 | * to lose :-) |
| 1017 | */ |
| 1018 | int check_disk_change(struct block_device *bdev) |
| 1019 | { |
| 1020 | struct gendisk *disk = bdev->bd_disk; |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 1021 | const struct block_device_operations *bdops = disk->fops; |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1022 | unsigned int events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1024 | events = disk_clear_events(disk, DISK_EVENT_MEDIA_CHANGE | |
| 1025 | DISK_EVENT_EJECT_REQUEST); |
| 1026 | if (!(events & DISK_EVENT_MEDIA_CHANGE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | return 0; |
| 1028 | |
NeilBrown | 93b270f | 2011-02-24 17:25:47 +1100 | [diff] [blame] | 1029 | flush_disk(bdev, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | if (bdops->revalidate_disk) |
| 1031 | bdops->revalidate_disk(bdev->bd_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | return 1; |
| 1033 | } |
| 1034 | |
| 1035 | EXPORT_SYMBOL(check_disk_change); |
| 1036 | |
| 1037 | void bd_set_size(struct block_device *bdev, loff_t size) |
| 1038 | { |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1039 | unsigned bsize = bdev_logical_block_size(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | |
| 1041 | bdev->bd_inode->i_size = size; |
| 1042 | while (bsize < PAGE_CACHE_SIZE) { |
| 1043 | if (size & bsize) |
| 1044 | break; |
| 1045 | bsize <<= 1; |
| 1046 | } |
| 1047 | bdev->bd_block_size = bsize; |
| 1048 | bdev->bd_inode->i_blkbits = blksize_bits(bsize); |
| 1049 | } |
| 1050 | EXPORT_SYMBOL(bd_set_size); |
| 1051 | |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1052 | static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part); |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1053 | |
Peter Zijlstra | 6d740cd | 2007-02-20 13:58:18 -0800 | [diff] [blame] | 1054 | /* |
| 1055 | * bd_mutex locking: |
| 1056 | * |
| 1057 | * mutex_lock(part->bd_mutex) |
| 1058 | * mutex_lock_nested(whole->bd_mutex, 1) |
| 1059 | */ |
| 1060 | |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1061 | static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | struct gendisk *disk; |
Pavel Emelyanov | 7db9cfd | 2008-06-05 22:46:27 -0700 | [diff] [blame] | 1064 | int ret; |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 1065 | int partno; |
Al Viro | fe6e9c1 | 2008-06-23 08:30:55 -0400 | [diff] [blame] | 1066 | int perm = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1068 | if (mode & FMODE_READ) |
Al Viro | fe6e9c1 | 2008-06-23 08:30:55 -0400 | [diff] [blame] | 1069 | perm |= MAY_READ; |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1070 | if (mode & FMODE_WRITE) |
Al Viro | fe6e9c1 | 2008-06-23 08:30:55 -0400 | [diff] [blame] | 1071 | perm |= MAY_WRITE; |
| 1072 | /* |
| 1073 | * hooks: /n/, see "layering violations". |
| 1074 | */ |
Chris Wright | b7300b7 | 2010-08-10 18:02:55 -0700 | [diff] [blame] | 1075 | if (!for_part) { |
| 1076 | ret = devcgroup_inode_permission(bdev->bd_inode, perm); |
| 1077 | if (ret != 0) { |
| 1078 | bdput(bdev); |
| 1079 | return ret; |
| 1080 | } |
Al Viro | 8266602 | 2008-08-01 05:32:04 -0400 | [diff] [blame] | 1081 | } |
Pavel Emelyanov | 7db9cfd | 2008-06-05 22:46:27 -0700 | [diff] [blame] | 1082 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 1083 | restart: |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1084 | |
Tejun Heo | 89f9749 | 2008-11-05 10:21:06 +0100 | [diff] [blame] | 1085 | ret = -ENXIO; |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 1086 | disk = get_gendisk(bdev->bd_dev, &partno); |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1087 | if (!disk) |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 1088 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | |
NeilBrown | 6796bf5 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1090 | mutex_lock_nested(&bdev->bd_mutex, for_part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | if (!bdev->bd_openers) { |
| 1092 | bdev->bd_disk = disk; |
| 1093 | bdev->bd_contains = bdev; |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 1094 | if (!partno) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | struct backing_dev_info *bdi; |
Tejun Heo | 89f9749 | 2008-11-05 10:21:06 +0100 | [diff] [blame] | 1096 | |
| 1097 | ret = -ENXIO; |
| 1098 | bdev->bd_part = disk_get_part(disk, partno); |
| 1099 | if (!bdev->bd_part) |
| 1100 | goto out_clear; |
| 1101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | if (disk->fops->open) { |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1103 | ret = disk->fops->open(bdev, mode); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 1104 | if (ret == -ERESTARTSYS) { |
| 1105 | /* Lost a race with 'disk' being |
| 1106 | * deleted, try again. |
| 1107 | * See md.c |
| 1108 | */ |
| 1109 | disk_put_part(bdev->bd_part); |
| 1110 | bdev->bd_part = NULL; |
| 1111 | module_put(disk->fops->owner); |
| 1112 | put_disk(disk); |
| 1113 | bdev->bd_disk = NULL; |
| 1114 | mutex_unlock(&bdev->bd_mutex); |
| 1115 | goto restart; |
| 1116 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | if (ret) |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1118 | goto out_clear; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | } |
| 1120 | if (!bdev->bd_openers) { |
| 1121 | bd_set_size(bdev,(loff_t)get_capacity(disk)<<9); |
| 1122 | bdi = blk_get_backing_dev_info(bdev); |
| 1123 | if (bdi == NULL) |
| 1124 | bdi = &default_backing_dev_info; |
Dave Chinner | a5491e0 | 2010-10-21 11:49:26 +1100 | [diff] [blame] | 1125 | bdev_inode_switch_bdi(bdev->bd_inode, bdi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | } |
| 1127 | if (bdev->bd_invalidated) |
| 1128 | rescan_partitions(disk, bdev); |
| 1129 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | struct block_device *whole; |
| 1131 | whole = bdget_disk(disk, 0); |
| 1132 | ret = -ENOMEM; |
| 1133 | if (!whole) |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1134 | goto out_clear; |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1135 | BUG_ON(for_part); |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1136 | ret = __blkdev_get(whole, mode, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | if (ret) |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1138 | goto out_clear; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | bdev->bd_contains = whole; |
Dave Chinner | a5491e0 | 2010-10-21 11:49:26 +1100 | [diff] [blame] | 1140 | bdev_inode_switch_bdi(bdev->bd_inode, |
| 1141 | whole->bd_inode->i_data.backing_dev_info); |
Tejun Heo | 89f9749 | 2008-11-05 10:21:06 +0100 | [diff] [blame] | 1142 | bdev->bd_part = disk_get_part(disk, partno); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 1143 | if (!(disk->flags & GENHD_FL_UP) || |
Tejun Heo | 89f9749 | 2008-11-05 10:21:06 +0100 | [diff] [blame] | 1144 | !bdev->bd_part || !bdev->bd_part->nr_sects) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | ret = -ENXIO; |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1146 | goto out_clear; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | } |
Tejun Heo | 89f9749 | 2008-11-05 10:21:06 +0100 | [diff] [blame] | 1148 | bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | } |
| 1150 | } else { |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1151 | module_put(disk->fops->owner); |
Neil Brown | 960cc0f | 2009-10-26 08:59:17 +0100 | [diff] [blame] | 1152 | put_disk(disk); |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1153 | disk = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | if (bdev->bd_contains == bdev) { |
| 1155 | if (bdev->bd_disk->fops->open) { |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1156 | ret = bdev->bd_disk->fops->open(bdev, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | if (ret) |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1158 | goto out_unlock_bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | } |
| 1160 | if (bdev->bd_invalidated) |
| 1161 | rescan_partitions(bdev->bd_disk, bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | } |
| 1163 | } |
| 1164 | bdev->bd_openers++; |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1165 | if (for_part) |
| 1166 | bdev->bd_part_count++; |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 1167 | mutex_unlock(&bdev->bd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | return 0; |
| 1169 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1170 | out_clear: |
Tejun Heo | 89f9749 | 2008-11-05 10:21:06 +0100 | [diff] [blame] | 1171 | disk_put_part(bdev->bd_part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | bdev->bd_disk = NULL; |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1173 | bdev->bd_part = NULL; |
Dave Chinner | a5491e0 | 2010-10-21 11:49:26 +1100 | [diff] [blame] | 1174 | bdev_inode_switch_bdi(bdev->bd_inode, &default_backing_dev_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | if (bdev != bdev->bd_contains) |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1176 | __blkdev_put(bdev->bd_contains, mode, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | bdev->bd_contains = NULL; |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1178 | out_unlock_bdev: |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 1179 | mutex_unlock(&bdev->bd_mutex); |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 1180 | out: |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1181 | if (disk) |
| 1182 | module_put(disk->fops->owner); |
| 1183 | put_disk(disk); |
| 1184 | bdput(bdev); |
| 1185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | return ret; |
| 1187 | } |
| 1188 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1189 | /** |
| 1190 | * blkdev_get - open a block device |
| 1191 | * @bdev: block_device to open |
| 1192 | * @mode: FMODE_* mask |
| 1193 | * @holder: exclusive holder identifier |
| 1194 | * |
| 1195 | * Open @bdev with @mode. If @mode includes %FMODE_EXCL, @bdev is |
| 1196 | * open with exclusive access. Specifying %FMODE_EXCL with %NULL |
| 1197 | * @holder is invalid. Exclusive opens may nest for the same @holder. |
| 1198 | * |
| 1199 | * On success, the reference count of @bdev is unchanged. On failure, |
| 1200 | * @bdev is put. |
| 1201 | * |
| 1202 | * CONTEXT: |
| 1203 | * Might sleep. |
| 1204 | * |
| 1205 | * RETURNS: |
| 1206 | * 0 on success, -errno on failure. |
| 1207 | */ |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1208 | int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | { |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1210 | struct block_device *whole = NULL; |
| 1211 | int res; |
| 1212 | |
| 1213 | WARN_ON_ONCE((mode & FMODE_EXCL) && !holder); |
| 1214 | |
| 1215 | if ((mode & FMODE_EXCL) && holder) { |
| 1216 | whole = bd_start_claiming(bdev, holder); |
| 1217 | if (IS_ERR(whole)) { |
| 1218 | bdput(bdev); |
| 1219 | return PTR_ERR(whole); |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | res = __blkdev_get(bdev, mode, 0); |
| 1224 | |
| 1225 | if (whole) { |
Tejun Heo | 6a027ef | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1226 | /* finish claiming */ |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1227 | mutex_lock(&bdev->bd_mutex); |
Tejun Heo | 6a027ef | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1228 | spin_lock(&bdev_lock); |
| 1229 | |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1230 | if (!res) { |
Tejun Heo | 6a027ef | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1231 | BUG_ON(!bd_may_claim(bdev, whole, holder)); |
| 1232 | /* |
| 1233 | * Note that for a whole device bd_holders |
| 1234 | * will be incremented twice, and bd_holder |
| 1235 | * will be set to bd_may_claim before being |
| 1236 | * set to holder |
| 1237 | */ |
| 1238 | whole->bd_holders++; |
| 1239 | whole->bd_holder = bd_may_claim; |
| 1240 | bdev->bd_holders++; |
| 1241 | bdev->bd_holder = holder; |
| 1242 | } |
| 1243 | |
| 1244 | /* tell others that we're done */ |
| 1245 | BUG_ON(whole->bd_claiming != holder); |
| 1246 | whole->bd_claiming = NULL; |
| 1247 | wake_up_bit(&whole->bd_claiming, 0); |
| 1248 | |
| 1249 | spin_unlock(&bdev_lock); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1250 | |
| 1251 | /* |
| 1252 | * Block event polling for write claims. Any write |
| 1253 | * holder makes the write_holder state stick until all |
| 1254 | * are released. This is good enough and tracking |
| 1255 | * individual writeable reference is too fragile given |
| 1256 | * the way @mode is used in blkdev_get/put(). |
| 1257 | */ |
| 1258 | if (!res && (mode & FMODE_WRITE) && !bdev->bd_write_holder) { |
| 1259 | bdev->bd_write_holder = true; |
| 1260 | disk_block_events(bdev->bd_disk); |
| 1261 | } |
| 1262 | |
| 1263 | mutex_unlock(&bdev->bd_mutex); |
Tejun Heo | 6a027ef | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1264 | bdput(whole); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | return res; |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1268 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | EXPORT_SYMBOL(blkdev_get); |
| 1270 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1271 | /** |
| 1272 | * blkdev_get_by_path - open a block device by name |
| 1273 | * @path: path to the block device to open |
| 1274 | * @mode: FMODE_* mask |
| 1275 | * @holder: exclusive holder identifier |
| 1276 | * |
| 1277 | * Open the blockdevice described by the device file at @path. @mode |
| 1278 | * and @holder are identical to blkdev_get(). |
| 1279 | * |
| 1280 | * On success, the returned block_device has reference count of one. |
| 1281 | * |
| 1282 | * CONTEXT: |
| 1283 | * Might sleep. |
| 1284 | * |
| 1285 | * RETURNS: |
| 1286 | * Pointer to block_device on success, ERR_PTR(-errno) on failure. |
| 1287 | */ |
| 1288 | struct block_device *blkdev_get_by_path(const char *path, fmode_t mode, |
| 1289 | void *holder) |
| 1290 | { |
| 1291 | struct block_device *bdev; |
| 1292 | int err; |
| 1293 | |
| 1294 | bdev = lookup_bdev(path); |
| 1295 | if (IS_ERR(bdev)) |
| 1296 | return bdev; |
| 1297 | |
| 1298 | err = blkdev_get(bdev, mode, holder); |
| 1299 | if (err) |
| 1300 | return ERR_PTR(err); |
| 1301 | |
Chuck Ebbert | e51900f | 2011-02-16 18:11:53 -0500 | [diff] [blame] | 1302 | if ((mode & FMODE_WRITE) && bdev_read_only(bdev)) { |
| 1303 | blkdev_put(bdev, mode); |
| 1304 | return ERR_PTR(-EACCES); |
| 1305 | } |
| 1306 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1307 | return bdev; |
| 1308 | } |
| 1309 | EXPORT_SYMBOL(blkdev_get_by_path); |
| 1310 | |
| 1311 | /** |
| 1312 | * blkdev_get_by_dev - open a block device by device number |
| 1313 | * @dev: device number of block device to open |
| 1314 | * @mode: FMODE_* mask |
| 1315 | * @holder: exclusive holder identifier |
| 1316 | * |
| 1317 | * Open the blockdevice described by device number @dev. @mode and |
| 1318 | * @holder are identical to blkdev_get(). |
| 1319 | * |
| 1320 | * Use it ONLY if you really do not have anything better - i.e. when |
| 1321 | * you are behind a truly sucky interface and all you are given is a |
| 1322 | * device number. _Never_ to be used for internal purposes. If you |
| 1323 | * ever need it - reconsider your API. |
| 1324 | * |
| 1325 | * On success, the returned block_device has reference count of one. |
| 1326 | * |
| 1327 | * CONTEXT: |
| 1328 | * Might sleep. |
| 1329 | * |
| 1330 | * RETURNS: |
| 1331 | * Pointer to block_device on success, ERR_PTR(-errno) on failure. |
| 1332 | */ |
| 1333 | struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder) |
| 1334 | { |
| 1335 | struct block_device *bdev; |
| 1336 | int err; |
| 1337 | |
| 1338 | bdev = bdget(dev); |
| 1339 | if (!bdev) |
| 1340 | return ERR_PTR(-ENOMEM); |
| 1341 | |
| 1342 | err = blkdev_get(bdev, mode, holder); |
| 1343 | if (err) |
| 1344 | return ERR_PTR(err); |
| 1345 | |
| 1346 | return bdev; |
| 1347 | } |
| 1348 | EXPORT_SYMBOL(blkdev_get_by_dev); |
| 1349 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | static int blkdev_open(struct inode * inode, struct file * filp) |
| 1351 | { |
| 1352 | struct block_device *bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | |
| 1354 | /* |
| 1355 | * Preserve backwards compatibility and allow large file access |
| 1356 | * even if userspace doesn't ask for it explicitly. Some mkfs |
| 1357 | * binary needs it. We might want to drop this workaround |
| 1358 | * during an unstable branch. |
| 1359 | */ |
| 1360 | filp->f_flags |= O_LARGEFILE; |
| 1361 | |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1362 | if (filp->f_flags & O_NDELAY) |
| 1363 | filp->f_mode |= FMODE_NDELAY; |
| 1364 | if (filp->f_flags & O_EXCL) |
| 1365 | filp->f_mode |= FMODE_EXCL; |
| 1366 | if ((filp->f_flags & O_ACCMODE) == 3) |
| 1367 | filp->f_mode |= FMODE_WRITE_IOCTL; |
| 1368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | bdev = bd_acquire(inode); |
Pavel Emelianov | 6a2aae0 | 2006-10-28 10:38:33 -0700 | [diff] [blame] | 1370 | if (bdev == NULL) |
| 1371 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1373 | filp->f_mapping = bdev->bd_inode->i_mapping; |
| 1374 | |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1375 | return blkdev_get(bdev, filp->f_mode, filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | } |
| 1377 | |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1378 | static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part) |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1379 | { |
| 1380 | int ret = 0; |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1381 | struct gendisk *disk = bdev->bd_disk; |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1382 | struct block_device *victim = NULL; |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1383 | |
NeilBrown | 6796bf5 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1384 | mutex_lock_nested(&bdev->bd_mutex, for_part); |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1385 | if (for_part) |
| 1386 | bdev->bd_part_count--; |
| 1387 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1388 | if (!--bdev->bd_openers) { |
Tejun Heo | 6a027ef | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1389 | WARN_ON_ONCE(bdev->bd_holders); |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1390 | sync_blockdev(bdev); |
| 1391 | kill_bdev(bdev); |
| 1392 | } |
| 1393 | if (bdev->bd_contains == bdev) { |
| 1394 | if (disk->fops->release) |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1395 | ret = disk->fops->release(disk, mode); |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1396 | } |
| 1397 | if (!bdev->bd_openers) { |
| 1398 | struct module *owner = disk->fops->owner; |
| 1399 | |
| 1400 | put_disk(disk); |
| 1401 | module_put(owner); |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1402 | disk_put_part(bdev->bd_part); |
| 1403 | bdev->bd_part = NULL; |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1404 | bdev->bd_disk = NULL; |
Dave Chinner | a5491e0 | 2010-10-21 11:49:26 +1100 | [diff] [blame] | 1405 | bdev_inode_switch_bdi(bdev->bd_inode, |
| 1406 | &default_backing_dev_info); |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1407 | if (bdev != bdev->bd_contains) |
| 1408 | victim = bdev->bd_contains; |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1409 | bdev->bd_contains = NULL; |
| 1410 | } |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1411 | mutex_unlock(&bdev->bd_mutex); |
| 1412 | bdput(bdev); |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1413 | if (victim) |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1414 | __blkdev_put(victim, mode, 1); |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1415 | return ret; |
| 1416 | } |
| 1417 | |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1418 | int blkdev_put(struct block_device *bdev, fmode_t mode) |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1419 | { |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1420 | if (mode & FMODE_EXCL) { |
Tejun Heo | 6a027ef | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1421 | bool bdev_free; |
| 1422 | |
| 1423 | /* |
| 1424 | * Release a claim on the device. The holder fields |
| 1425 | * are protected with bdev_lock. bd_mutex is to |
| 1426 | * synchronize disk_holder unlinking. |
| 1427 | */ |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1428 | mutex_lock(&bdev->bd_mutex); |
Tejun Heo | 6a027ef | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1429 | spin_lock(&bdev_lock); |
| 1430 | |
| 1431 | WARN_ON_ONCE(--bdev->bd_holders < 0); |
| 1432 | WARN_ON_ONCE(--bdev->bd_contains->bd_holders < 0); |
| 1433 | |
| 1434 | /* bd_contains might point to self, check in a separate step */ |
| 1435 | if ((bdev_free = !bdev->bd_holders)) |
| 1436 | bdev->bd_holder = NULL; |
| 1437 | if (!bdev->bd_contains->bd_holders) |
| 1438 | bdev->bd_contains->bd_holder = NULL; |
| 1439 | |
| 1440 | spin_unlock(&bdev_lock); |
| 1441 | |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1442 | /* |
| 1443 | * If this was the last claim, remove holder link and |
| 1444 | * unblock evpoll if it was a write holder. |
| 1445 | */ |
| 1446 | if (bdev_free) { |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1447 | if (bdev->bd_write_holder) { |
| 1448 | disk_unblock_events(bdev->bd_disk); |
| 1449 | bdev->bd_write_holder = false; |
| 1450 | } else |
| 1451 | disk_check_events(bdev->bd_disk); |
| 1452 | } |
Tejun Heo | 6a027ef | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1453 | |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1454 | mutex_unlock(&bdev->bd_mutex); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1455 | } else |
| 1456 | disk_check_events(bdev->bd_disk); |
| 1457 | |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1458 | return __blkdev_put(bdev, mode, 0); |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1459 | } |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1460 | EXPORT_SYMBOL(blkdev_put); |
| 1461 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | static int blkdev_close(struct inode * inode, struct file * filp) |
| 1463 | { |
| 1464 | struct block_device *bdev = I_BDEV(filp->f_mapping->host); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1465 | |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1466 | return blkdev_put(bdev, filp->f_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | } |
| 1468 | |
Arnd Bergmann | bb93e3a | 2005-06-23 00:10:15 -0700 | [diff] [blame] | 1469 | static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | { |
Al Viro | 56b26ad | 2008-09-19 03:17:36 -0400 | [diff] [blame] | 1471 | struct block_device *bdev = I_BDEV(file->f_mapping->host); |
| 1472 | fmode_t mode = file->f_mode; |
Christoph Hellwig | fd4ce1a | 2008-11-05 14:58:42 +0100 | [diff] [blame] | 1473 | |
| 1474 | /* |
| 1475 | * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have |
| 1476 | * to updated it before every ioctl. |
| 1477 | */ |
Al Viro | 56b26ad | 2008-09-19 03:17:36 -0400 | [diff] [blame] | 1478 | if (file->f_flags & O_NDELAY) |
Christoph Hellwig | fd4ce1a | 2008-11-05 14:58:42 +0100 | [diff] [blame] | 1479 | mode |= FMODE_NDELAY; |
| 1480 | else |
| 1481 | mode &= ~FMODE_NDELAY; |
| 1482 | |
Al Viro | 56b26ad | 2008-09-19 03:17:36 -0400 | [diff] [blame] | 1483 | return blkdev_ioctl(bdev, mode, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | } |
| 1485 | |
Theodore Ts'o | 87d8fe1 | 2009-01-03 09:47:09 -0500 | [diff] [blame] | 1486 | /* |
Christoph Hellwig | eef9938 | 2009-08-20 17:43:41 +0200 | [diff] [blame] | 1487 | * Write data to the block device. Only intended for the block device itself |
| 1488 | * and the raw driver which basically is a fake block device. |
| 1489 | * |
| 1490 | * Does not take i_mutex for the write and thus is not for general purpose |
| 1491 | * use. |
| 1492 | */ |
| 1493 | ssize_t blkdev_aio_write(struct kiocb *iocb, const struct iovec *iov, |
| 1494 | unsigned long nr_segs, loff_t pos) |
| 1495 | { |
| 1496 | struct file *file = iocb->ki_filp; |
| 1497 | ssize_t ret; |
| 1498 | |
| 1499 | BUG_ON(iocb->ki_pos != pos); |
| 1500 | |
| 1501 | ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos); |
| 1502 | if (ret > 0 || ret == -EIOCBQUEUED) { |
| 1503 | ssize_t err; |
| 1504 | |
| 1505 | err = generic_write_sync(file, pos, ret); |
| 1506 | if (err < 0 && ret > 0) |
| 1507 | ret = err; |
| 1508 | } |
| 1509 | return ret; |
| 1510 | } |
| 1511 | EXPORT_SYMBOL_GPL(blkdev_aio_write); |
| 1512 | |
| 1513 | /* |
Theodore Ts'o | 87d8fe1 | 2009-01-03 09:47:09 -0500 | [diff] [blame] | 1514 | * Try to release a page associated with block device when the system |
| 1515 | * is under memory pressure. |
| 1516 | */ |
| 1517 | static int blkdev_releasepage(struct page *page, gfp_t wait) |
| 1518 | { |
| 1519 | struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super; |
| 1520 | |
| 1521 | if (super && super->s_op->bdev_try_to_free_page) |
| 1522 | return super->s_op->bdev_try_to_free_page(super, page, wait); |
| 1523 | |
| 1524 | return try_to_free_buffers(page); |
| 1525 | } |
| 1526 | |
Adrian Bunk | 4c54ac6 | 2008-02-18 13:48:31 +0100 | [diff] [blame] | 1527 | static const struct address_space_operations def_blk_aops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | .readpage = blkdev_readpage, |
| 1529 | .writepage = blkdev_writepage, |
| 1530 | .sync_page = block_sync_page, |
Nick Piggin | 6272b5a | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 1531 | .write_begin = blkdev_write_begin, |
| 1532 | .write_end = blkdev_write_end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | .writepages = generic_writepages, |
Theodore Ts'o | 87d8fe1 | 2009-01-03 09:47:09 -0500 | [diff] [blame] | 1534 | .releasepage = blkdev_releasepage, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | .direct_IO = blkdev_direct_IO, |
| 1536 | }; |
| 1537 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 1538 | const struct file_operations def_blk_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | .open = blkdev_open, |
| 1540 | .release = blkdev_close, |
| 1541 | .llseek = block_llseek, |
Badari Pulavarty | 543ade1 | 2006-09-30 23:28:48 -0700 | [diff] [blame] | 1542 | .read = do_sync_read, |
| 1543 | .write = do_sync_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | .aio_read = generic_file_aio_read, |
Christoph Hellwig | eef9938 | 2009-08-20 17:43:41 +0200 | [diff] [blame] | 1545 | .aio_write = blkdev_aio_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | .mmap = generic_file_mmap, |
Andrew Morton | b1dd3b2 | 2010-04-06 14:35:00 -0700 | [diff] [blame] | 1547 | .fsync = blkdev_fsync, |
Arnd Bergmann | bb93e3a | 2005-06-23 00:10:15 -0700 | [diff] [blame] | 1548 | .unlocked_ioctl = block_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | #ifdef CONFIG_COMPAT |
| 1550 | .compat_ioctl = compat_blkdev_ioctl, |
| 1551 | #endif |
Jens Axboe | 7f9c51f | 2006-05-01 19:59:32 +0200 | [diff] [blame] | 1552 | .splice_read = generic_file_splice_read, |
| 1553 | .splice_write = generic_file_splice_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | }; |
| 1555 | |
| 1556 | int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg) |
| 1557 | { |
| 1558 | int res; |
| 1559 | mm_segment_t old_fs = get_fs(); |
| 1560 | set_fs(KERNEL_DS); |
Al Viro | 56b26ad | 2008-09-19 03:17:36 -0400 | [diff] [blame] | 1561 | res = blkdev_ioctl(bdev, 0, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | set_fs(old_fs); |
| 1563 | return res; |
| 1564 | } |
| 1565 | |
| 1566 | EXPORT_SYMBOL(ioctl_by_bdev); |
| 1567 | |
| 1568 | /** |
| 1569 | * lookup_bdev - lookup a struct block_device by name |
Randy Dunlap | 94e2959 | 2009-01-06 14:41:15 -0800 | [diff] [blame] | 1570 | * @pathname: special file representing the block device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | * |
Randy Dunlap | 57d1b53 | 2008-10-09 10:42:38 +0200 | [diff] [blame] | 1572 | * Get a reference to the blockdevice at @pathname in the current |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | * namespace if possible and return it. Return ERR_PTR(error) |
| 1574 | * otherwise. |
| 1575 | */ |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1576 | struct block_device *lookup_bdev(const char *pathname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | { |
| 1578 | struct block_device *bdev; |
| 1579 | struct inode *inode; |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1580 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | int error; |
| 1582 | |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1583 | if (!pathname || !*pathname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | return ERR_PTR(-EINVAL); |
| 1585 | |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1586 | error = kern_path(pathname, LOOKUP_FOLLOW, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | if (error) |
| 1588 | return ERR_PTR(error); |
| 1589 | |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1590 | inode = path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | error = -ENOTBLK; |
| 1592 | if (!S_ISBLK(inode->i_mode)) |
| 1593 | goto fail; |
| 1594 | error = -EACCES; |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1595 | if (path.mnt->mnt_flags & MNT_NODEV) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | goto fail; |
| 1597 | error = -ENOMEM; |
| 1598 | bdev = bd_acquire(inode); |
| 1599 | if (!bdev) |
| 1600 | goto fail; |
| 1601 | out: |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1602 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | return bdev; |
| 1604 | fail: |
| 1605 | bdev = ERR_PTR(error); |
| 1606 | goto out; |
| 1607 | } |
Al Viro | d5686b4 | 2008-08-01 05:00:11 -0400 | [diff] [blame] | 1608 | EXPORT_SYMBOL(lookup_bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | |
NeilBrown | 93b270f | 2011-02-24 17:25:47 +1100 | [diff] [blame] | 1610 | int __invalidate_device(struct block_device *bdev, bool kill_dirty) |
David Howells | b71e8a4 | 2006-08-29 19:06:11 +0100 | [diff] [blame] | 1611 | { |
| 1612 | struct super_block *sb = get_super(bdev); |
| 1613 | int res = 0; |
| 1614 | |
| 1615 | if (sb) { |
| 1616 | /* |
| 1617 | * no need to lock the super, get_super holds the |
| 1618 | * read mutex so the filesystem cannot go away |
| 1619 | * under us (->put_super runs with the write lock |
| 1620 | * hold). |
| 1621 | */ |
| 1622 | shrink_dcache_sb(sb); |
NeilBrown | 93b270f | 2011-02-24 17:25:47 +1100 | [diff] [blame] | 1623 | res = invalidate_inodes(sb, kill_dirty); |
David Howells | b71e8a4 | 2006-08-29 19:06:11 +0100 | [diff] [blame] | 1624 | drop_super(sb); |
| 1625 | } |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 1626 | invalidate_bdev(bdev); |
David Howells | b71e8a4 | 2006-08-29 19:06:11 +0100 | [diff] [blame] | 1627 | return res; |
| 1628 | } |
| 1629 | EXPORT_SYMBOL(__invalidate_device); |