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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/smp_lock.h> |
Pavel Emelyanov | 7db9cfd | 2008-06-05 22:46:27 -0700 | [diff] [blame] | 15 | #include <linux/device_cgroup.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/highmem.h> |
| 17 | #include <linux/blkdev.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/blkpg.h> |
| 20 | #include <linux/buffer_head.h> |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 21 | #include <linux/pagevec.h> |
David Howells | 811d736 | 2006-08-29 19:06:09 +0100 | [diff] [blame] | 22 | #include <linux/writeback.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/mpage.h> |
| 24 | #include <linux/mount.h> |
| 25 | #include <linux/uio.h> |
| 26 | #include <linux/namei.h> |
Vignesh Babu BM | 1368c4f | 2007-05-08 00:24:32 -0700 | [diff] [blame] | 27 | #include <linux/log2.h> |
Catalin Marinas | 2e1483c | 2009-06-11 13:24:13 +0100 | [diff] [blame] | 28 | #include <linux/kmemleak.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <asm/uaccess.h> |
David Howells | 07f3f05 | 2006-09-30 20:52:18 +0200 | [diff] [blame] | 30 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | struct bdev_inode { |
| 33 | struct block_device bdev; |
| 34 | struct inode vfs_inode; |
| 35 | }; |
| 36 | |
Adrian Bunk | 4c54ac6 | 2008-02-18 13:48:31 +0100 | [diff] [blame] | 37 | static const struct address_space_operations def_blk_aops; |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | static inline struct bdev_inode *BDEV_I(struct inode *inode) |
| 40 | { |
| 41 | return container_of(inode, struct bdev_inode, vfs_inode); |
| 42 | } |
| 43 | |
| 44 | inline struct block_device *I_BDEV(struct inode *inode) |
| 45 | { |
| 46 | return &BDEV_I(inode)->bdev; |
| 47 | } |
| 48 | |
| 49 | EXPORT_SYMBOL(I_BDEV); |
| 50 | |
Dave Chinner | a5491e0 | 2010-10-21 11:49:26 +1100 | [diff] [blame] | 51 | /* |
| 52 | * move the inode from it's current bdi to the a new bdi. if the inode is dirty |
| 53 | * we need to move it onto the dirty list of @dst so that the inode is always |
| 54 | * on the right list. |
| 55 | */ |
| 56 | static void bdev_inode_switch_bdi(struct inode *inode, |
| 57 | struct backing_dev_info *dst) |
| 58 | { |
| 59 | spin_lock(&inode_lock); |
| 60 | inode->i_data.backing_dev_info = dst; |
| 61 | if (inode->i_state & I_DIRTY) |
Nick Piggin | 7ccf19a | 2010-10-21 11:49:30 +1100 | [diff] [blame] | 62 | list_move(&inode->i_wb_list, &dst->wb.b_dirty); |
Dave Chinner | a5491e0 | 2010-10-21 11:49:26 +1100 | [diff] [blame] | 63 | spin_unlock(&inode_lock); |
| 64 | } |
| 65 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | static sector_t max_block(struct block_device *bdev) |
| 67 | { |
| 68 | sector_t retval = ~((sector_t)0); |
| 69 | loff_t sz = i_size_read(bdev->bd_inode); |
| 70 | |
| 71 | if (sz) { |
| 72 | unsigned int size = block_size(bdev); |
| 73 | unsigned int sizebits = blksize_bits(size); |
| 74 | retval = (sz >> sizebits); |
| 75 | } |
| 76 | return retval; |
| 77 | } |
| 78 | |
Peter Zijlstra | f9a1439 | 2007-05-06 14:49:55 -0700 | [diff] [blame] | 79 | /* Kill _all_ buffers and pagecache , dirty or not.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | static void kill_bdev(struct block_device *bdev) |
| 81 | { |
Peter Zijlstra | f9a1439 | 2007-05-06 14:49:55 -0700 | [diff] [blame] | 82 | if (bdev->bd_inode->i_mapping->nrpages == 0) |
| 83 | return; |
| 84 | invalidate_bh_lrus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | truncate_inode_pages(bdev->bd_inode->i_mapping, 0); |
| 86 | } |
| 87 | |
| 88 | int set_blocksize(struct block_device *bdev, int size) |
| 89 | { |
| 90 | /* Size must be a power of two, and between 512 and PAGE_SIZE */ |
Vignesh Babu BM | 1368c4f | 2007-05-08 00:24:32 -0700 | [diff] [blame] | 91 | if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | return -EINVAL; |
| 93 | |
| 94 | /* Size cannot be smaller than the size supported by the device */ |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 95 | if (size < bdev_logical_block_size(bdev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | return -EINVAL; |
| 97 | |
| 98 | /* Don't change the size if it is same as current */ |
| 99 | if (bdev->bd_block_size != size) { |
| 100 | sync_blockdev(bdev); |
| 101 | bdev->bd_block_size = size; |
| 102 | bdev->bd_inode->i_blkbits = blksize_bits(size); |
| 103 | kill_bdev(bdev); |
| 104 | } |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | EXPORT_SYMBOL(set_blocksize); |
| 109 | |
| 110 | int sb_set_blocksize(struct super_block *sb, int size) |
| 111 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | if (set_blocksize(sb->s_bdev, size)) |
| 113 | return 0; |
| 114 | /* If we get here, we know size is power of two |
| 115 | * and it's value is between 512 and PAGE_SIZE */ |
| 116 | sb->s_blocksize = size; |
Coywolf Qi Hunt | 38885bd | 2006-03-24 03:18:05 -0800 | [diff] [blame] | 117 | sb->s_blocksize_bits = blksize_bits(size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | return sb->s_blocksize; |
| 119 | } |
| 120 | |
| 121 | EXPORT_SYMBOL(sb_set_blocksize); |
| 122 | |
| 123 | int sb_min_blocksize(struct super_block *sb, int size) |
| 124 | { |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 125 | int minsize = bdev_logical_block_size(sb->s_bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | if (size < minsize) |
| 127 | size = minsize; |
| 128 | return sb_set_blocksize(sb, size); |
| 129 | } |
| 130 | |
| 131 | EXPORT_SYMBOL(sb_min_blocksize); |
| 132 | |
| 133 | static int |
| 134 | blkdev_get_block(struct inode *inode, sector_t iblock, |
| 135 | struct buffer_head *bh, int create) |
| 136 | { |
| 137 | if (iblock >= max_block(I_BDEV(inode))) { |
| 138 | if (create) |
| 139 | return -EIO; |
| 140 | |
| 141 | /* |
| 142 | * for reads, we're just trying to fill a partial page. |
| 143 | * return a hole, they will have to call get_block again |
| 144 | * before they can fill it, and they will get -EIO at that |
| 145 | * time |
| 146 | */ |
| 147 | return 0; |
| 148 | } |
| 149 | bh->b_bdev = I_BDEV(inode); |
| 150 | bh->b_blocknr = iblock; |
| 151 | set_buffer_mapped(bh); |
| 152 | return 0; |
| 153 | } |
| 154 | |
Andrew Morton | b2e895d | 2007-02-03 01:14:01 -0800 | [diff] [blame] | 155 | static int |
| 156 | blkdev_get_blocks(struct inode *inode, sector_t iblock, |
| 157 | struct buffer_head *bh, int create) |
| 158 | { |
| 159 | sector_t end_block = max_block(I_BDEV(inode)); |
| 160 | unsigned long max_blocks = bh->b_size >> inode->i_blkbits; |
| 161 | |
| 162 | if ((iblock + max_blocks) > end_block) { |
| 163 | max_blocks = end_block - iblock; |
| 164 | if ((long)max_blocks <= 0) { |
| 165 | if (create) |
| 166 | return -EIO; /* write fully beyond EOF */ |
| 167 | /* |
| 168 | * It is a read which is fully beyond EOF. We return |
| 169 | * a !buffer_mapped buffer |
| 170 | */ |
| 171 | max_blocks = 0; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | bh->b_bdev = I_BDEV(inode); |
| 176 | bh->b_blocknr = iblock; |
| 177 | bh->b_size = max_blocks << inode->i_blkbits; |
| 178 | if (max_blocks) |
| 179 | set_buffer_mapped(bh); |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | static ssize_t |
| 184 | blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, |
| 185 | loff_t offset, unsigned long nr_segs) |
| 186 | { |
| 187 | struct file *file = iocb->ki_filp; |
| 188 | struct inode *inode = file->f_mapping->host; |
| 189 | |
Christoph Hellwig | eafdc7d | 2010-06-04 11:29:53 +0200 | [diff] [blame] | 190 | return __blockdev_direct_IO(rw, iocb, inode, I_BDEV(inode), iov, offset, |
| 191 | nr_segs, blkdev_get_blocks, NULL, NULL, 0); |
Andrew Morton | b2e895d | 2007-02-03 01:14:01 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Jan Kara | 5cee581 | 2009-04-27 16:43:51 +0200 | [diff] [blame] | 194 | int __sync_blockdev(struct block_device *bdev, int wait) |
| 195 | { |
| 196 | if (!bdev) |
| 197 | return 0; |
| 198 | if (!wait) |
| 199 | return filemap_flush(bdev->bd_inode->i_mapping); |
| 200 | return filemap_write_and_wait(bdev->bd_inode->i_mapping); |
| 201 | } |
| 202 | |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 203 | /* |
| 204 | * Write out and wait upon all the dirty data associated with a block |
| 205 | * device via its mapping. Does not take the superblock lock. |
| 206 | */ |
| 207 | int sync_blockdev(struct block_device *bdev) |
| 208 | { |
Jan Kara | 5cee581 | 2009-04-27 16:43:51 +0200 | [diff] [blame] | 209 | return __sync_blockdev(bdev, 1); |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 210 | } |
| 211 | EXPORT_SYMBOL(sync_blockdev); |
| 212 | |
| 213 | /* |
| 214 | * Write out and wait upon all dirty data associated with this |
| 215 | * device. Filesystem data as well as the underlying block |
| 216 | * device. Takes the superblock lock. |
| 217 | */ |
| 218 | int fsync_bdev(struct block_device *bdev) |
| 219 | { |
| 220 | struct super_block *sb = get_super(bdev); |
| 221 | if (sb) { |
Jan Kara | 60b0680 | 2009-04-27 16:43:53 +0200 | [diff] [blame] | 222 | int res = sync_filesystem(sb); |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 223 | drop_super(sb); |
| 224 | return res; |
| 225 | } |
| 226 | return sync_blockdev(bdev); |
| 227 | } |
Al Viro | 47e4491 | 2009-04-01 07:07:16 -0400 | [diff] [blame] | 228 | EXPORT_SYMBOL(fsync_bdev); |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 229 | |
| 230 | /** |
| 231 | * freeze_bdev -- lock a filesystem and force it into a consistent state |
| 232 | * @bdev: blockdevice to lock |
| 233 | * |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 234 | * If a superblock is found on this device, we take the s_umount semaphore |
| 235 | * on it to make sure nobody unmounts until the snapshot creation is done. |
| 236 | * The reference counter (bd_fsfreeze_count) guarantees that only the last |
| 237 | * unfreeze process can unfreeze the frozen filesystem actually when multiple |
| 238 | * freeze requests arrive simultaneously. It counts up in freeze_bdev() and |
| 239 | * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze |
| 240 | * actually. |
| 241 | */ |
| 242 | struct super_block *freeze_bdev(struct block_device *bdev) |
| 243 | { |
| 244 | struct super_block *sb; |
| 245 | int error = 0; |
| 246 | |
| 247 | mutex_lock(&bdev->bd_fsfreeze_mutex); |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 248 | if (++bdev->bd_fsfreeze_count > 1) { |
| 249 | /* |
| 250 | * We don't even need to grab a reference - the first call |
| 251 | * to freeze_bdev grab an active reference and only the last |
| 252 | * thaw_bdev drops it. |
| 253 | */ |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 254 | sb = get_super(bdev); |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 255 | drop_super(sb); |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 256 | mutex_unlock(&bdev->bd_fsfreeze_mutex); |
| 257 | return sb; |
| 258 | } |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 259 | |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 260 | sb = get_active_super(bdev); |
| 261 | if (!sb) |
| 262 | goto out; |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 263 | error = freeze_super(sb); |
| 264 | if (error) { |
| 265 | deactivate_super(sb); |
| 266 | bdev->bd_fsfreeze_count--; |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 267 | mutex_unlock(&bdev->bd_fsfreeze_mutex); |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 268 | return ERR_PTR(error); |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 269 | } |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 270 | deactivate_super(sb); |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 271 | out: |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 272 | sync_blockdev(bdev); |
| 273 | mutex_unlock(&bdev->bd_fsfreeze_mutex); |
Christoph Hellwig | 4fadd7b | 2009-08-03 23:28:06 +0200 | [diff] [blame] | 274 | return sb; /* thaw_bdev releases s->s_umount */ |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 275 | } |
| 276 | EXPORT_SYMBOL(freeze_bdev); |
| 277 | |
| 278 | /** |
| 279 | * thaw_bdev -- unlock filesystem |
| 280 | * @bdev: blockdevice to unlock |
| 281 | * @sb: associated superblock |
| 282 | * |
| 283 | * Unlocks the filesystem and marks it writeable again after freeze_bdev(). |
| 284 | */ |
| 285 | int thaw_bdev(struct block_device *bdev, struct super_block *sb) |
| 286 | { |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 287 | int error = -EINVAL; |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 288 | |
| 289 | mutex_lock(&bdev->bd_fsfreeze_mutex); |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 290 | if (!bdev->bd_fsfreeze_count) |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 291 | goto out; |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 292 | |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 293 | error = 0; |
| 294 | if (--bdev->bd_fsfreeze_count > 0) |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 295 | goto out; |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 296 | |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 297 | if (!sb) |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 298 | goto out; |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 299 | |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 300 | error = thaw_super(sb); |
| 301 | if (error) { |
| 302 | bdev->bd_fsfreeze_count++; |
| 303 | mutex_unlock(&bdev->bd_fsfreeze_mutex); |
| 304 | return error; |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 305 | } |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 306 | out: |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 307 | mutex_unlock(&bdev->bd_fsfreeze_mutex); |
| 308 | return 0; |
| 309 | } |
| 310 | EXPORT_SYMBOL(thaw_bdev); |
| 311 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | static int blkdev_writepage(struct page *page, struct writeback_control *wbc) |
| 313 | { |
| 314 | return block_write_full_page(page, blkdev_get_block, wbc); |
| 315 | } |
| 316 | |
| 317 | static int blkdev_readpage(struct file * file, struct page * page) |
| 318 | { |
| 319 | return block_read_full_page(page, blkdev_get_block); |
| 320 | } |
| 321 | |
Nick Piggin | 6272b5a | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 322 | static int blkdev_write_begin(struct file *file, struct address_space *mapping, |
| 323 | loff_t pos, unsigned len, unsigned flags, |
| 324 | struct page **pagep, void **fsdata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | { |
Christoph Hellwig | 155130a | 2010-06-04 11:29:58 +0200 | [diff] [blame] | 326 | return block_write_begin(mapping, pos, len, flags, pagep, |
| 327 | blkdev_get_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Nick Piggin | 6272b5a | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 330 | static int blkdev_write_end(struct file *file, struct address_space *mapping, |
| 331 | loff_t pos, unsigned len, unsigned copied, |
| 332 | struct page *page, void *fsdata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | { |
Nick Piggin | 6272b5a | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 334 | int ret; |
| 335 | ret = block_write_end(file, mapping, pos, len, copied, page, fsdata); |
| 336 | |
| 337 | unlock_page(page); |
| 338 | page_cache_release(page); |
| 339 | |
| 340 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /* |
| 344 | * private llseek: |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 345 | * 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] | 346 | * so we compute the size by hand (just as in block_read/write above) |
| 347 | */ |
| 348 | static loff_t block_llseek(struct file *file, loff_t offset, int origin) |
| 349 | { |
| 350 | struct inode *bd_inode = file->f_mapping->host; |
| 351 | loff_t size; |
| 352 | loff_t retval; |
| 353 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 354 | mutex_lock(&bd_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | size = i_size_read(bd_inode); |
| 356 | |
| 357 | switch (origin) { |
| 358 | case 2: |
| 359 | offset += size; |
| 360 | break; |
| 361 | case 1: |
| 362 | offset += file->f_pos; |
| 363 | } |
| 364 | retval = -EINVAL; |
| 365 | if (offset >= 0 && offset <= size) { |
| 366 | if (offset != file->f_pos) { |
| 367 | file->f_pos = offset; |
| 368 | } |
| 369 | retval = offset; |
| 370 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 371 | mutex_unlock(&bd_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | return retval; |
| 373 | } |
| 374 | |
Christoph Hellwig | 7ea8085 | 2010-05-26 17:53:25 +0200 | [diff] [blame] | 375 | int blkdev_fsync(struct file *filp, int datasync) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | { |
Anton Blanchard | b8af67e | 2010-04-23 13:18:06 -0400 | [diff] [blame] | 377 | struct inode *bd_inode = filp->f_mapping->host; |
| 378 | struct block_device *bdev = I_BDEV(bd_inode); |
Christoph Hellwig | ab0a973 | 2009-10-29 14:14:04 +0100 | [diff] [blame] | 379 | int error; |
| 380 | |
Anton Blanchard | b8af67e | 2010-04-23 13:18:06 -0400 | [diff] [blame] | 381 | /* |
| 382 | * There is no need to serialise calls to blkdev_issue_flush with |
| 383 | * i_mutex and doing so causes performance issues with concurrent |
| 384 | * O_SYNC writers to a block device. |
| 385 | */ |
| 386 | mutex_unlock(&bd_inode->i_mutex); |
| 387 | |
Christoph Hellwig | dd3932e | 2010-09-16 20:51:46 +0200 | [diff] [blame] | 388 | error = blkdev_issue_flush(bdev, GFP_KERNEL, NULL); |
Christoph Hellwig | ab0a973 | 2009-10-29 14:14:04 +0100 | [diff] [blame] | 389 | if (error == -EOPNOTSUPP) |
| 390 | error = 0; |
Anton Blanchard | b8af67e | 2010-04-23 13:18:06 -0400 | [diff] [blame] | 391 | |
| 392 | mutex_lock(&bd_inode->i_mutex); |
| 393 | |
Christoph Hellwig | ab0a973 | 2009-10-29 14:14:04 +0100 | [diff] [blame] | 394 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | } |
Andrew Morton | b1dd3b2 | 2010-04-06 14:35:00 -0700 | [diff] [blame] | 396 | EXPORT_SYMBOL(blkdev_fsync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | |
| 398 | /* |
| 399 | * pseudo-fs |
| 400 | */ |
| 401 | |
| 402 | static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock); |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 403 | static struct kmem_cache * bdev_cachep __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | |
| 405 | static struct inode *bdev_alloc_inode(struct super_block *sb) |
| 406 | { |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 407 | struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | if (!ei) |
| 409 | return NULL; |
| 410 | return &ei->vfs_inode; |
| 411 | } |
| 412 | |
| 413 | static void bdev_destroy_inode(struct inode *inode) |
| 414 | { |
| 415 | struct bdev_inode *bdi = BDEV_I(inode); |
| 416 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | kmem_cache_free(bdev_cachep, bdi); |
| 418 | } |
| 419 | |
Alexey Dobriyan | 51cc506 | 2008-07-25 19:45:34 -0700 | [diff] [blame] | 420 | static void init_once(void *foo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | { |
| 422 | struct bdev_inode *ei = (struct bdev_inode *) foo; |
| 423 | struct block_device *bdev = &ei->bdev; |
| 424 | |
Christoph Lameter | a35afb8 | 2007-05-16 22:10:57 -0700 | [diff] [blame] | 425 | memset(bdev, 0, sizeof(*bdev)); |
| 426 | mutex_init(&bdev->bd_mutex); |
Christoph Lameter | a35afb8 | 2007-05-16 22:10:57 -0700 | [diff] [blame] | 427 | INIT_LIST_HEAD(&bdev->bd_inodes); |
| 428 | INIT_LIST_HEAD(&bdev->bd_list); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 429 | #ifdef CONFIG_SYSFS |
Christoph Lameter | a35afb8 | 2007-05-16 22:10:57 -0700 | [diff] [blame] | 430 | INIT_LIST_HEAD(&bdev->bd_holder_list); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 431 | #endif |
Christoph Lameter | a35afb8 | 2007-05-16 22:10:57 -0700 | [diff] [blame] | 432 | inode_init_once(&ei->vfs_inode); |
Takashi Sato | fcccf50 | 2009-01-09 16:40:59 -0800 | [diff] [blame] | 433 | /* Initialize mutex for freeze. */ |
| 434 | mutex_init(&bdev->bd_fsfreeze_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | static inline void __bd_forget(struct inode *inode) |
| 438 | { |
| 439 | list_del_init(&inode->i_devices); |
| 440 | inode->i_bdev = NULL; |
| 441 | inode->i_mapping = &inode->i_data; |
| 442 | } |
| 443 | |
Al Viro | b57922d | 2010-06-07 14:34:48 -0400 | [diff] [blame] | 444 | static void bdev_evict_inode(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | { |
| 446 | struct block_device *bdev = &BDEV_I(inode)->bdev; |
| 447 | struct list_head *p; |
Al Viro | b57922d | 2010-06-07 14:34:48 -0400 | [diff] [blame] | 448 | truncate_inode_pages(&inode->i_data, 0); |
| 449 | invalidate_inode_buffers(inode); /* is it needed here? */ |
| 450 | end_writeback(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | spin_lock(&bdev_lock); |
| 452 | while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) { |
| 453 | __bd_forget(list_entry(p, struct inode, i_devices)); |
| 454 | } |
| 455 | list_del_init(&bdev->bd_list); |
| 456 | spin_unlock(&bdev_lock); |
| 457 | } |
| 458 | |
Josef 'Jeff' Sipek | ee9b6d6 | 2007-02-12 00:55:41 -0800 | [diff] [blame] | 459 | static const struct super_operations bdev_sops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | .statfs = simple_statfs, |
| 461 | .alloc_inode = bdev_alloc_inode, |
| 462 | .destroy_inode = bdev_destroy_inode, |
| 463 | .drop_inode = generic_delete_inode, |
Al Viro | b57922d | 2010-06-07 14:34:48 -0400 | [diff] [blame] | 464 | .evict_inode = bdev_evict_inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | }; |
| 466 | |
Al Viro | 51139ad | 2010-07-25 23:47:46 +0400 | [diff] [blame] | 467 | static struct dentry *bd_mount(struct file_system_type *fs_type, |
| 468 | int flags, const char *dev_name, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | { |
Al Viro | 51139ad | 2010-07-25 23:47:46 +0400 | [diff] [blame] | 470 | return mount_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | static struct file_system_type bd_type = { |
| 474 | .name = "bdev", |
Al Viro | 51139ad | 2010-07-25 23:47:46 +0400 | [diff] [blame] | 475 | .mount = bd_mount, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | .kill_sb = kill_anon_super, |
| 477 | }; |
| 478 | |
Denis ChengRq | c2acf7b | 2008-12-01 14:34:56 -0800 | [diff] [blame] | 479 | struct super_block *blockdev_superblock __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
| 481 | void __init bdev_cache_init(void) |
| 482 | { |
| 483 | int err; |
Denis ChengRq | c2acf7b | 2008-12-01 14:34:56 -0800 | [diff] [blame] | 484 | struct vfsmount *bd_mnt; |
| 485 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode), |
Paul Jackson | fffb60f | 2006-03-24 03:16:06 -0800 | [diff] [blame] | 487 | 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT| |
| 488 | SLAB_MEM_SPREAD|SLAB_PANIC), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 489 | init_once); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | err = register_filesystem(&bd_type); |
| 491 | if (err) |
| 492 | panic("Cannot register bdev pseudo-fs"); |
| 493 | bd_mnt = kern_mount(&bd_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | if (IS_ERR(bd_mnt)) |
| 495 | panic("Cannot create bdev pseudo-fs"); |
Catalin Marinas | 2e1483c | 2009-06-11 13:24:13 +0100 | [diff] [blame] | 496 | /* |
| 497 | * This vfsmount structure is only used to obtain the |
| 498 | * blockdev_superblock, so tell kmemleak not to report it. |
| 499 | */ |
| 500 | kmemleak_not_leak(bd_mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */ |
| 502 | } |
| 503 | |
| 504 | /* |
| 505 | * Most likely _very_ bad one - but then it's hardly critical for small |
| 506 | * /dev and can be fixed when somebody will need really large one. |
| 507 | * Keep in mind that it will be fed through icache hash function too. |
| 508 | */ |
| 509 | static inline unsigned long hash(dev_t dev) |
| 510 | { |
| 511 | return MAJOR(dev)+MINOR(dev); |
| 512 | } |
| 513 | |
| 514 | static int bdev_test(struct inode *inode, void *data) |
| 515 | { |
| 516 | return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data; |
| 517 | } |
| 518 | |
| 519 | static int bdev_set(struct inode *inode, void *data) |
| 520 | { |
| 521 | BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data; |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | static LIST_HEAD(all_bdevs); |
| 526 | |
| 527 | struct block_device *bdget(dev_t dev) |
| 528 | { |
| 529 | struct block_device *bdev; |
| 530 | struct inode *inode; |
| 531 | |
Denis ChengRq | c2acf7b | 2008-12-01 14:34:56 -0800 | [diff] [blame] | 532 | inode = iget5_locked(blockdev_superblock, hash(dev), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | bdev_test, bdev_set, &dev); |
| 534 | |
| 535 | if (!inode) |
| 536 | return NULL; |
| 537 | |
| 538 | bdev = &BDEV_I(inode)->bdev; |
| 539 | |
| 540 | if (inode->i_state & I_NEW) { |
| 541 | bdev->bd_contains = NULL; |
| 542 | bdev->bd_inode = inode; |
| 543 | bdev->bd_block_size = (1 << inode->i_blkbits); |
| 544 | bdev->bd_part_count = 0; |
| 545 | bdev->bd_invalidated = 0; |
| 546 | inode->i_mode = S_IFBLK; |
| 547 | inode->i_rdev = dev; |
| 548 | inode->i_bdev = bdev; |
| 549 | inode->i_data.a_ops = &def_blk_aops; |
| 550 | mapping_set_gfp_mask(&inode->i_data, GFP_USER); |
| 551 | inode->i_data.backing_dev_info = &default_backing_dev_info; |
| 552 | spin_lock(&bdev_lock); |
| 553 | list_add(&bdev->bd_list, &all_bdevs); |
| 554 | spin_unlock(&bdev_lock); |
| 555 | unlock_new_inode(inode); |
| 556 | } |
| 557 | return bdev; |
| 558 | } |
| 559 | |
| 560 | EXPORT_SYMBOL(bdget); |
| 561 | |
Alan Jenkins | dddac6a | 2009-07-29 21:07:55 +0200 | [diff] [blame] | 562 | /** |
| 563 | * bdgrab -- Grab a reference to an already referenced block device |
| 564 | * @bdev: Block device to grab a reference to. |
| 565 | */ |
| 566 | struct block_device *bdgrab(struct block_device *bdev) |
| 567 | { |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 568 | ihold(bdev->bd_inode); |
Alan Jenkins | dddac6a | 2009-07-29 21:07:55 +0200 | [diff] [blame] | 569 | return bdev; |
| 570 | } |
| 571 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | long nr_blockdev_pages(void) |
| 573 | { |
Matthias Kaehlcke | 203a293 | 2007-07-15 23:40:23 -0700 | [diff] [blame] | 574 | struct block_device *bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | long ret = 0; |
| 576 | spin_lock(&bdev_lock); |
Matthias Kaehlcke | 203a293 | 2007-07-15 23:40:23 -0700 | [diff] [blame] | 577 | list_for_each_entry(bdev, &all_bdevs, bd_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | ret += bdev->bd_inode->i_mapping->nrpages; |
| 579 | } |
| 580 | spin_unlock(&bdev_lock); |
| 581 | return ret; |
| 582 | } |
| 583 | |
| 584 | void bdput(struct block_device *bdev) |
| 585 | { |
| 586 | iput(bdev->bd_inode); |
| 587 | } |
| 588 | |
| 589 | EXPORT_SYMBOL(bdput); |
| 590 | |
| 591 | static struct block_device *bd_acquire(struct inode *inode) |
| 592 | { |
| 593 | struct block_device *bdev; |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 594 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | spin_lock(&bdev_lock); |
| 596 | bdev = inode->i_bdev; |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 597 | if (bdev) { |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 598 | ihold(bdev->bd_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | spin_unlock(&bdev_lock); |
| 600 | return bdev; |
| 601 | } |
| 602 | spin_unlock(&bdev_lock); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 603 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | bdev = bdget(inode->i_rdev); |
| 605 | if (bdev) { |
| 606 | spin_lock(&bdev_lock); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 607 | if (!inode->i_bdev) { |
| 608 | /* |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 609 | * We take an additional reference to bd_inode, |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 610 | * and it's released in clear_inode() of inode. |
| 611 | * So, we can access it via ->i_mapping always |
| 612 | * without igrab(). |
| 613 | */ |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 614 | ihold(bdev->bd_inode); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 615 | inode->i_bdev = bdev; |
| 616 | inode->i_mapping = bdev->bd_inode->i_mapping; |
| 617 | list_add(&inode->i_devices, &bdev->bd_inodes); |
| 618 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | spin_unlock(&bdev_lock); |
| 620 | } |
| 621 | return bdev; |
| 622 | } |
| 623 | |
| 624 | /* Call when you free inode */ |
| 625 | |
| 626 | void bd_forget(struct inode *inode) |
| 627 | { |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 628 | struct block_device *bdev = NULL; |
| 629 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | spin_lock(&bdev_lock); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 631 | if (inode->i_bdev) { |
Denis ChengRq | c2acf7b | 2008-12-01 14:34:56 -0800 | [diff] [blame] | 632 | if (!sb_is_blkdev_sb(inode->i_sb)) |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 633 | bdev = inode->i_bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | __bd_forget(inode); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 635 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | spin_unlock(&bdev_lock); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 637 | |
| 638 | if (bdev) |
| 639 | iput(bdev->bd_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Tejun Heo | 1a3cbbc | 2010-04-07 18:52:29 +0900 | [diff] [blame] | 642 | /** |
| 643 | * bd_may_claim - test whether a block device can be claimed |
| 644 | * @bdev: block device of interest |
| 645 | * @whole: whole block device containing @bdev, may equal @bdev |
| 646 | * @holder: holder trying to claim @bdev |
| 647 | * |
| 648 | * Test whther @bdev can be claimed by @holder. |
| 649 | * |
| 650 | * CONTEXT: |
| 651 | * spin_lock(&bdev_lock). |
| 652 | * |
| 653 | * RETURNS: |
| 654 | * %true if @bdev can be claimed, %false otherwise. |
| 655 | */ |
| 656 | static bool bd_may_claim(struct block_device *bdev, struct block_device *whole, |
| 657 | void *holder) |
| 658 | { |
| 659 | if (bdev->bd_holder == holder) |
| 660 | return true; /* already a holder */ |
| 661 | else if (bdev->bd_holder != NULL) |
| 662 | return false; /* held by someone else */ |
| 663 | else if (bdev->bd_contains == bdev) |
| 664 | return true; /* is a whole device which isn't held */ |
| 665 | |
| 666 | else if (whole->bd_holder == bd_claim) |
| 667 | return true; /* is a partition of a device that is being partitioned */ |
| 668 | else if (whole->bd_holder != NULL) |
| 669 | return false; /* is a partition of a held device */ |
| 670 | else |
| 671 | return true; /* is a partition of an un-held device */ |
| 672 | } |
| 673 | |
| 674 | /** |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 675 | * bd_prepare_to_claim - prepare to claim a block device |
| 676 | * @bdev: block device of interest |
| 677 | * @whole: the whole device containing @bdev, may equal @bdev |
| 678 | * @holder: holder trying to claim @bdev |
| 679 | * |
| 680 | * Prepare to claim @bdev. This function fails if @bdev is already |
| 681 | * claimed by another holder and waits if another claiming is in |
| 682 | * progress. This function doesn't actually claim. On successful |
| 683 | * return, the caller has ownership of bd_claiming and bd_holder[s]. |
| 684 | * |
| 685 | * CONTEXT: |
| 686 | * spin_lock(&bdev_lock). Might release bdev_lock, sleep and regrab |
| 687 | * it multiple times. |
| 688 | * |
| 689 | * RETURNS: |
| 690 | * 0 if @bdev can be claimed, -EBUSY otherwise. |
| 691 | */ |
| 692 | static int bd_prepare_to_claim(struct block_device *bdev, |
| 693 | struct block_device *whole, void *holder) |
| 694 | { |
| 695 | retry: |
| 696 | /* if someone else claimed, fail */ |
| 697 | if (!bd_may_claim(bdev, whole, holder)) |
| 698 | return -EBUSY; |
| 699 | |
Tejun Heo | e75aa85 | 2010-08-04 17:59:39 +0200 | [diff] [blame] | 700 | /* if claiming is already in progress, wait for it to finish */ |
| 701 | if (whole->bd_claiming) { |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 702 | wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0); |
| 703 | DEFINE_WAIT(wait); |
| 704 | |
| 705 | prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE); |
| 706 | spin_unlock(&bdev_lock); |
| 707 | schedule(); |
| 708 | finish_wait(wq, &wait); |
| 709 | spin_lock(&bdev_lock); |
| 710 | goto retry; |
| 711 | } |
| 712 | |
| 713 | /* yay, all mine */ |
| 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | /** |
| 718 | * bd_start_claiming - start claiming a block device |
| 719 | * @bdev: block device of interest |
| 720 | * @holder: holder trying to claim @bdev |
| 721 | * |
| 722 | * @bdev is about to be opened exclusively. Check @bdev can be opened |
| 723 | * exclusively and mark that an exclusive open is in progress. Each |
| 724 | * successful call to this function must be matched with a call to |
Nick Piggin | b001836 | 2010-05-26 01:51:19 +1000 | [diff] [blame] | 725 | * either bd_finish_claiming() or bd_abort_claiming() (which do not |
| 726 | * fail). |
| 727 | * |
| 728 | * This function is used to gain exclusive access to the block device |
| 729 | * without actually causing other exclusive open attempts to fail. It |
| 730 | * should be used when the open sequence itself requires exclusive |
| 731 | * access but may subsequently fail. |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 732 | * |
| 733 | * CONTEXT: |
| 734 | * Might sleep. |
| 735 | * |
| 736 | * RETURNS: |
| 737 | * Pointer to the block device containing @bdev on success, ERR_PTR() |
| 738 | * value on failure. |
| 739 | */ |
| 740 | static struct block_device *bd_start_claiming(struct block_device *bdev, |
| 741 | void *holder) |
| 742 | { |
| 743 | struct gendisk *disk; |
| 744 | struct block_device *whole; |
| 745 | int partno, err; |
| 746 | |
| 747 | might_sleep(); |
| 748 | |
| 749 | /* |
| 750 | * @bdev might not have been initialized properly yet, look up |
| 751 | * and grab the outer block device the hard way. |
| 752 | */ |
| 753 | disk = get_gendisk(bdev->bd_dev, &partno); |
| 754 | if (!disk) |
| 755 | return ERR_PTR(-ENXIO); |
| 756 | |
| 757 | whole = bdget_disk(disk, 0); |
Nick Piggin | cf34257 | 2010-05-26 01:50:21 +1000 | [diff] [blame] | 758 | module_put(disk->fops->owner); |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 759 | put_disk(disk); |
| 760 | if (!whole) |
| 761 | return ERR_PTR(-ENOMEM); |
| 762 | |
| 763 | /* prepare to claim, if successful, mark claiming in progress */ |
| 764 | spin_lock(&bdev_lock); |
| 765 | |
| 766 | err = bd_prepare_to_claim(bdev, whole, holder); |
| 767 | if (err == 0) { |
| 768 | whole->bd_claiming = holder; |
| 769 | spin_unlock(&bdev_lock); |
| 770 | return whole; |
| 771 | } else { |
| 772 | spin_unlock(&bdev_lock); |
| 773 | bdput(whole); |
| 774 | return ERR_PTR(err); |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | /* releases bdev_lock */ |
| 779 | static void __bd_abort_claiming(struct block_device *whole, void *holder) |
| 780 | { |
| 781 | BUG_ON(whole->bd_claiming != holder); |
| 782 | whole->bd_claiming = NULL; |
| 783 | wake_up_bit(&whole->bd_claiming, 0); |
| 784 | |
| 785 | spin_unlock(&bdev_lock); |
| 786 | bdput(whole); |
| 787 | } |
| 788 | |
| 789 | /** |
| 790 | * bd_abort_claiming - abort claiming a block device |
| 791 | * @whole: whole block device returned by bd_start_claiming() |
| 792 | * @holder: holder trying to claim @bdev |
| 793 | * |
| 794 | * Abort a claiming block started by bd_start_claiming(). Note that |
| 795 | * @whole is not the block device to be claimed but the whole device |
| 796 | * returned by bd_start_claiming(). |
| 797 | * |
| 798 | * CONTEXT: |
| 799 | * Grabs and releases bdev_lock. |
| 800 | */ |
| 801 | static void bd_abort_claiming(struct block_device *whole, void *holder) |
| 802 | { |
| 803 | spin_lock(&bdev_lock); |
| 804 | __bd_abort_claiming(whole, holder); /* releases bdev_lock */ |
| 805 | } |
| 806 | |
Nick Piggin | b001836 | 2010-05-26 01:51:19 +1000 | [diff] [blame] | 807 | /* increment holders when we have a legitimate claim. requires bdev_lock */ |
| 808 | static void __bd_claim(struct block_device *bdev, struct block_device *whole, |
| 809 | void *holder) |
| 810 | { |
| 811 | /* note that for a whole device bd_holders |
| 812 | * will be incremented twice, and bd_holder will |
| 813 | * be set to bd_claim before being set to holder |
| 814 | */ |
| 815 | whole->bd_holders++; |
| 816 | whole->bd_holder = bd_claim; |
| 817 | bdev->bd_holders++; |
| 818 | bdev->bd_holder = holder; |
| 819 | } |
| 820 | |
| 821 | /** |
| 822 | * bd_finish_claiming - finish claiming a block device |
| 823 | * @bdev: block device of interest (passed to bd_start_claiming()) |
| 824 | * @whole: whole block device returned by bd_start_claiming() |
| 825 | * @holder: holder trying to claim @bdev |
| 826 | * |
| 827 | * Finish a claiming block started by bd_start_claiming(). |
| 828 | * |
| 829 | * CONTEXT: |
| 830 | * Grabs and releases bdev_lock. |
| 831 | */ |
| 832 | static void bd_finish_claiming(struct block_device *bdev, |
| 833 | struct block_device *whole, void *holder) |
| 834 | { |
| 835 | spin_lock(&bdev_lock); |
Nick Piggin | b001836 | 2010-05-26 01:51:19 +1000 | [diff] [blame] | 836 | BUG_ON(!bd_may_claim(bdev, whole, holder)); |
| 837 | __bd_claim(bdev, whole, holder); |
| 838 | __bd_abort_claiming(whole, holder); /* not actually an abort */ |
| 839 | } |
| 840 | |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 841 | /** |
Tejun Heo | 1a3cbbc | 2010-04-07 18:52:29 +0900 | [diff] [blame] | 842 | * bd_claim - claim a block device |
| 843 | * @bdev: block device to claim |
| 844 | * @holder: holder trying to claim @bdev |
| 845 | * |
Nick Piggin | b001836 | 2010-05-26 01:51:19 +1000 | [diff] [blame] | 846 | * Try to claim @bdev which must have been opened successfully. |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 847 | * |
| 848 | * CONTEXT: |
| 849 | * Might sleep. |
Tejun Heo | 1a3cbbc | 2010-04-07 18:52:29 +0900 | [diff] [blame] | 850 | * |
| 851 | * RETURNS: |
| 852 | * 0 if successful, -EBUSY if @bdev is already claimed. |
| 853 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | int bd_claim(struct block_device *bdev, void *holder) |
| 855 | { |
Tejun Heo | 1a3cbbc | 2010-04-07 18:52:29 +0900 | [diff] [blame] | 856 | struct block_device *whole = bdev->bd_contains; |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 857 | int res; |
| 858 | |
| 859 | might_sleep(); |
Tejun Heo | 1a3cbbc | 2010-04-07 18:52:29 +0900 | [diff] [blame] | 860 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | spin_lock(&bdev_lock); |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 862 | res = bd_prepare_to_claim(bdev, whole, holder); |
Nick Piggin | b001836 | 2010-05-26 01:51:19 +1000 | [diff] [blame] | 863 | if (res == 0) |
| 864 | __bd_claim(bdev, whole, holder); |
| 865 | spin_unlock(&bdev_lock); |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 866 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | return res; |
| 868 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | EXPORT_SYMBOL(bd_claim); |
| 870 | |
| 871 | void bd_release(struct block_device *bdev) |
| 872 | { |
| 873 | spin_lock(&bdev_lock); |
| 874 | if (!--bdev->bd_contains->bd_holders) |
| 875 | bdev->bd_contains->bd_holder = NULL; |
| 876 | if (!--bdev->bd_holders) |
| 877 | bdev->bd_holder = NULL; |
| 878 | spin_unlock(&bdev_lock); |
| 879 | } |
| 880 | |
| 881 | EXPORT_SYMBOL(bd_release); |
| 882 | |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 883 | #ifdef CONFIG_SYSFS |
| 884 | /* |
| 885 | * Functions for bd_claim_by_kobject / bd_release_from_kobject |
| 886 | * |
| 887 | * If a kobject is passed to bd_claim_by_kobject() |
| 888 | * and the kobject has a parent directory, |
| 889 | * following symlinks are created: |
| 890 | * o from the kobject to the claimed bdev |
| 891 | * o from "holders" directory of the bdev to the parent of the kobject |
| 892 | * bd_release_from_kobject() removes these symlinks. |
| 893 | * |
| 894 | * Example: |
| 895 | * If /dev/dm-0 maps to /dev/sda, kobject corresponding to |
| 896 | * /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then: |
| 897 | * /sys/block/dm-0/slaves/sda --> /sys/block/sda |
| 898 | * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0 |
| 899 | */ |
| 900 | |
Andrew Morton | 4d7dd8fd | 2006-09-29 01:58:56 -0700 | [diff] [blame] | 901 | static int add_symlink(struct kobject *from, struct kobject *to) |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 902 | { |
| 903 | if (!from || !to) |
Andrew Morton | 4d7dd8fd | 2006-09-29 01:58:56 -0700 | [diff] [blame] | 904 | return 0; |
| 905 | return sysfs_create_link(from, to, kobject_name(to)); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | static void del_symlink(struct kobject *from, struct kobject *to) |
| 909 | { |
| 910 | if (!from || !to) |
| 911 | return; |
| 912 | sysfs_remove_link(from, kobject_name(to)); |
| 913 | } |
| 914 | |
| 915 | /* |
| 916 | * 'struct bd_holder' contains pointers to kobjects symlinked by |
| 917 | * bd_claim_by_kobject. |
| 918 | * It's connected to bd_holder_list which is protected by bdev->bd_sem. |
| 919 | */ |
| 920 | struct bd_holder { |
| 921 | struct list_head list; /* chain of holders of the bdev */ |
| 922 | int count; /* references from the holder */ |
| 923 | struct kobject *sdir; /* holder object, e.g. "/block/dm-0/slaves" */ |
| 924 | struct kobject *hdev; /* e.g. "/block/dm-0" */ |
| 925 | struct kobject *hdir; /* e.g. "/block/sda/holders" */ |
| 926 | struct kobject *sdev; /* e.g. "/block/sda" */ |
| 927 | }; |
| 928 | |
| 929 | /* |
| 930 | * Get references of related kobjects at once. |
| 931 | * Returns 1 on success. 0 on failure. |
| 932 | * |
| 933 | * Should call bd_holder_release_dirs() after successful use. |
| 934 | */ |
| 935 | static int bd_holder_grab_dirs(struct block_device *bdev, |
| 936 | struct bd_holder *bo) |
| 937 | { |
| 938 | if (!bdev || !bo) |
| 939 | return 0; |
| 940 | |
| 941 | bo->sdir = kobject_get(bo->sdir); |
| 942 | if (!bo->sdir) |
| 943 | return 0; |
| 944 | |
| 945 | bo->hdev = kobject_get(bo->sdir->parent); |
| 946 | if (!bo->hdev) |
| 947 | goto fail_put_sdir; |
| 948 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 949 | bo->sdev = kobject_get(&part_to_dev(bdev->bd_part)->kobj); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 950 | if (!bo->sdev) |
| 951 | goto fail_put_hdev; |
| 952 | |
Tejun Heo | 4c46501 | 2008-08-25 19:56:11 +0900 | [diff] [blame] | 953 | bo->hdir = kobject_get(bdev->bd_part->holder_dir); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 954 | if (!bo->hdir) |
| 955 | goto fail_put_sdev; |
| 956 | |
| 957 | return 1; |
| 958 | |
| 959 | fail_put_sdev: |
| 960 | kobject_put(bo->sdev); |
| 961 | fail_put_hdev: |
| 962 | kobject_put(bo->hdev); |
| 963 | fail_put_sdir: |
| 964 | kobject_put(bo->sdir); |
| 965 | |
| 966 | return 0; |
| 967 | } |
| 968 | |
| 969 | /* Put references of related kobjects at once. */ |
| 970 | static void bd_holder_release_dirs(struct bd_holder *bo) |
| 971 | { |
| 972 | kobject_put(bo->hdir); |
| 973 | kobject_put(bo->sdev); |
| 974 | kobject_put(bo->hdev); |
| 975 | kobject_put(bo->sdir); |
| 976 | } |
| 977 | |
| 978 | static struct bd_holder *alloc_bd_holder(struct kobject *kobj) |
| 979 | { |
| 980 | struct bd_holder *bo; |
| 981 | |
| 982 | bo = kzalloc(sizeof(*bo), GFP_KERNEL); |
| 983 | if (!bo) |
| 984 | return NULL; |
| 985 | |
| 986 | bo->count = 1; |
| 987 | bo->sdir = kobj; |
| 988 | |
| 989 | return bo; |
| 990 | } |
| 991 | |
| 992 | static void free_bd_holder(struct bd_holder *bo) |
| 993 | { |
| 994 | kfree(bo); |
| 995 | } |
| 996 | |
| 997 | /** |
Jun'ichi Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 998 | * find_bd_holder - find matching struct bd_holder from the block device |
| 999 | * |
| 1000 | * @bdev: struct block device to be searched |
| 1001 | * @bo: target struct bd_holder |
| 1002 | * |
| 1003 | * Returns matching entry with @bo in @bdev->bd_holder_list. |
| 1004 | * If found, increment the reference count and return the pointer. |
| 1005 | * If not found, returns NULL. |
| 1006 | */ |
Andrew Morton | 36a561d | 2006-10-30 22:07:03 -0800 | [diff] [blame] | 1007 | static struct bd_holder *find_bd_holder(struct block_device *bdev, |
| 1008 | struct bd_holder *bo) |
Jun'ichi Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 1009 | { |
| 1010 | struct bd_holder *tmp; |
| 1011 | |
| 1012 | list_for_each_entry(tmp, &bdev->bd_holder_list, list) |
| 1013 | if (tmp->sdir == bo->sdir) { |
| 1014 | tmp->count++; |
| 1015 | return tmp; |
| 1016 | } |
| 1017 | |
| 1018 | return NULL; |
| 1019 | } |
| 1020 | |
| 1021 | /** |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 1022 | * add_bd_holder - create sysfs symlinks for bd_claim() relationship |
| 1023 | * |
| 1024 | * @bdev: block device to be bd_claimed |
| 1025 | * @bo: preallocated and initialized by alloc_bd_holder() |
| 1026 | * |
Jun'ichi Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 1027 | * Add @bo to @bdev->bd_holder_list, create symlinks. |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 1028 | * |
Jun'ichi Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 1029 | * Returns 0 if symlinks are created. |
| 1030 | * Returns -ve if something fails. |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 1031 | */ |
| 1032 | static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo) |
| 1033 | { |
Johannes Weiner | 4e91672 | 2007-07-15 23:41:25 -0700 | [diff] [blame] | 1034 | int err; |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 1035 | |
| 1036 | if (!bo) |
Andrew Morton | 4d7dd8fd | 2006-09-29 01:58:56 -0700 | [diff] [blame] | 1037 | return -EINVAL; |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 1038 | |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 1039 | if (!bd_holder_grab_dirs(bdev, bo)) |
Andrew Morton | 4d7dd8fd | 2006-09-29 01:58:56 -0700 | [diff] [blame] | 1040 | return -EBUSY; |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 1041 | |
Johannes Weiner | 4e91672 | 2007-07-15 23:41:25 -0700 | [diff] [blame] | 1042 | err = add_symlink(bo->sdir, bo->sdev); |
| 1043 | if (err) |
| 1044 | return err; |
| 1045 | |
| 1046 | err = add_symlink(bo->hdir, bo->hdev); |
| 1047 | if (err) { |
| 1048 | del_symlink(bo->sdir, bo->sdev); |
| 1049 | return err; |
Andrew Morton | 4d7dd8fd | 2006-09-29 01:58:56 -0700 | [diff] [blame] | 1050 | } |
Johannes Weiner | 4e91672 | 2007-07-15 23:41:25 -0700 | [diff] [blame] | 1051 | |
| 1052 | list_add_tail(&bo->list, &bdev->bd_holder_list); |
| 1053 | return 0; |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | /** |
| 1057 | * del_bd_holder - delete sysfs symlinks for bd_claim() relationship |
| 1058 | * |
| 1059 | * @bdev: block device to be bd_claimed |
| 1060 | * @kobj: holder's kobject |
| 1061 | * |
| 1062 | * If there is matching entry with @kobj in @bdev->bd_holder_list |
| 1063 | * and no other bd_claim() from the same kobject, |
| 1064 | * remove the struct bd_holder from the list, delete symlinks for it. |
| 1065 | * |
| 1066 | * Returns a pointer to the struct bd_holder when it's removed from the list |
| 1067 | * and ready to be freed. |
| 1068 | * Returns NULL if matching claim isn't found or there is other bd_claim() |
| 1069 | * by the same kobject. |
| 1070 | */ |
| 1071 | static struct bd_holder *del_bd_holder(struct block_device *bdev, |
| 1072 | struct kobject *kobj) |
| 1073 | { |
| 1074 | struct bd_holder *bo; |
| 1075 | |
| 1076 | list_for_each_entry(bo, &bdev->bd_holder_list, list) { |
| 1077 | if (bo->sdir == kobj) { |
| 1078 | bo->count--; |
| 1079 | BUG_ON(bo->count < 0); |
| 1080 | if (!bo->count) { |
| 1081 | list_del(&bo->list); |
| 1082 | del_symlink(bo->sdir, bo->sdev); |
| 1083 | del_symlink(bo->hdir, bo->hdev); |
| 1084 | bd_holder_release_dirs(bo); |
| 1085 | return bo; |
| 1086 | } |
| 1087 | break; |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | return NULL; |
| 1092 | } |
| 1093 | |
| 1094 | /** |
| 1095 | * bd_claim_by_kobject - bd_claim() with additional kobject signature |
| 1096 | * |
| 1097 | * @bdev: block device to be claimed |
| 1098 | * @holder: holder's signature |
| 1099 | * @kobj: holder's kobject |
| 1100 | * |
| 1101 | * Do bd_claim() and if it succeeds, create sysfs symlinks between |
| 1102 | * the bdev and the holder's kobject. |
| 1103 | * Use bd_release_from_kobject() when relesing the claimed bdev. |
| 1104 | * |
| 1105 | * Returns 0 on success. (same as bd_claim()) |
| 1106 | * Returns errno on failure. |
| 1107 | */ |
| 1108 | static int bd_claim_by_kobject(struct block_device *bdev, void *holder, |
| 1109 | struct kobject *kobj) |
| 1110 | { |
Johannes Weiner | 4e91672 | 2007-07-15 23:41:25 -0700 | [diff] [blame] | 1111 | int err; |
Jun'ichi Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 1112 | struct bd_holder *bo, *found; |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 1113 | |
| 1114 | if (!kobj) |
| 1115 | return -EINVAL; |
| 1116 | |
| 1117 | bo = alloc_bd_holder(kobj); |
| 1118 | if (!bo) |
| 1119 | return -ENOMEM; |
| 1120 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1121 | mutex_lock(&bdev->bd_mutex); |
Jun'ichi Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 1122 | |
Johannes Weiner | 4e91672 | 2007-07-15 23:41:25 -0700 | [diff] [blame] | 1123 | err = bd_claim(bdev, holder); |
| 1124 | if (err) |
Andrew Morton | 4210df2 | 2007-07-15 23:41:28 -0700 | [diff] [blame] | 1125 | goto fail; |
Johannes Weiner | 4e91672 | 2007-07-15 23:41:25 -0700 | [diff] [blame] | 1126 | |
| 1127 | found = find_bd_holder(bdev, bo); |
| 1128 | if (found) |
Andrew Morton | 4210df2 | 2007-07-15 23:41:28 -0700 | [diff] [blame] | 1129 | goto fail; |
Johannes Weiner | 4e91672 | 2007-07-15 23:41:25 -0700 | [diff] [blame] | 1130 | |
| 1131 | err = add_bd_holder(bdev, bo); |
| 1132 | if (err) |
| 1133 | bd_release(bdev); |
Andrew Morton | 4210df2 | 2007-07-15 23:41:28 -0700 | [diff] [blame] | 1134 | else |
| 1135 | bo = NULL; |
| 1136 | fail: |
Jun'ichi Nomura | b4cf1b7 | 2006-03-27 01:18:00 -0800 | [diff] [blame] | 1137 | mutex_unlock(&bdev->bd_mutex); |
Andrew Morton | 4210df2 | 2007-07-15 23:41:28 -0700 | [diff] [blame] | 1138 | free_bd_holder(bo); |
Johannes Weiner | 4e91672 | 2007-07-15 23:41:25 -0700 | [diff] [blame] | 1139 | return err; |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | /** |
| 1143 | * bd_release_from_kobject - bd_release() with additional kobject signature |
| 1144 | * |
| 1145 | * @bdev: block device to be released |
| 1146 | * @kobj: holder's kobject |
| 1147 | * |
| 1148 | * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject(). |
| 1149 | */ |
| 1150 | static void bd_release_from_kobject(struct block_device *bdev, |
| 1151 | struct kobject *kobj) |
| 1152 | { |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 1153 | if (!kobj) |
| 1154 | return; |
| 1155 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1156 | mutex_lock(&bdev->bd_mutex); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 1157 | bd_release(bdev); |
Andrew Morton | 4210df2 | 2007-07-15 23:41:28 -0700 | [diff] [blame] | 1158 | free_bd_holder(del_bd_holder(bdev, kobj)); |
Jun'ichi Nomura | b4cf1b7 | 2006-03-27 01:18:00 -0800 | [diff] [blame] | 1159 | mutex_unlock(&bdev->bd_mutex); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | /** |
| 1163 | * bd_claim_by_disk - wrapper function for bd_claim_by_kobject() |
| 1164 | * |
| 1165 | * @bdev: block device to be claimed |
| 1166 | * @holder: holder's signature |
| 1167 | * @disk: holder's gendisk |
| 1168 | * |
| 1169 | * Call bd_claim_by_kobject() with getting @disk->slave_dir. |
| 1170 | */ |
| 1171 | int bd_claim_by_disk(struct block_device *bdev, void *holder, |
| 1172 | struct gendisk *disk) |
| 1173 | { |
| 1174 | return bd_claim_by_kobject(bdev, holder, kobject_get(disk->slave_dir)); |
| 1175 | } |
| 1176 | EXPORT_SYMBOL_GPL(bd_claim_by_disk); |
| 1177 | |
| 1178 | /** |
| 1179 | * bd_release_from_disk - wrapper function for bd_release_from_kobject() |
| 1180 | * |
| 1181 | * @bdev: block device to be claimed |
| 1182 | * @disk: holder's gendisk |
| 1183 | * |
| 1184 | * Call bd_release_from_kobject() and put @disk->slave_dir. |
| 1185 | */ |
| 1186 | void bd_release_from_disk(struct block_device *bdev, struct gendisk *disk) |
| 1187 | { |
| 1188 | bd_release_from_kobject(bdev, disk->slave_dir); |
| 1189 | kobject_put(disk->slave_dir); |
| 1190 | } |
| 1191 | EXPORT_SYMBOL_GPL(bd_release_from_disk); |
| 1192 | #endif |
| 1193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | /* |
| 1195 | * Tries to open block device by device number. Use it ONLY if you |
| 1196 | * really do not have anything better - i.e. when you are behind a |
| 1197 | * truly sucky interface and all you are given is a device number. _Never_ |
| 1198 | * to be used for internal purposes. If you ever need it - reconsider |
| 1199 | * your API. |
| 1200 | */ |
Al Viro | aeb5d72 | 2008-09-02 15:28:45 -0400 | [diff] [blame] | 1201 | struct block_device *open_by_devnum(dev_t dev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | { |
| 1203 | struct block_device *bdev = bdget(dev); |
| 1204 | int err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | if (bdev) |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1206 | err = blkdev_get(bdev, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | return err ? ERR_PTR(err) : bdev; |
| 1208 | } |
| 1209 | |
| 1210 | EXPORT_SYMBOL(open_by_devnum); |
| 1211 | |
Andrew Patterson | 0c002c2 | 2008-09-04 14:27:20 -0600 | [diff] [blame] | 1212 | /** |
Andrew Patterson | 56ade44 | 2008-09-04 14:27:40 -0600 | [diff] [blame] | 1213 | * flush_disk - invalidates all buffer-cache entries on a disk |
| 1214 | * |
| 1215 | * @bdev: struct block device to be flushed |
| 1216 | * |
| 1217 | * Invalidates all buffer-cache entries on a disk. It should be called |
| 1218 | * when a disk has been changed -- either by a media change or online |
| 1219 | * resize. |
| 1220 | */ |
| 1221 | static void flush_disk(struct block_device *bdev) |
| 1222 | { |
| 1223 | if (__invalidate_device(bdev)) { |
| 1224 | char name[BDEVNAME_SIZE] = ""; |
| 1225 | |
| 1226 | if (bdev->bd_disk) |
| 1227 | disk_name(bdev->bd_disk, 0, name); |
| 1228 | printk(KERN_WARNING "VFS: busy inodes on changed media or " |
| 1229 | "resized disk %s\n", name); |
| 1230 | } |
| 1231 | |
| 1232 | if (!bdev->bd_disk) |
| 1233 | return; |
| 1234 | if (disk_partitionable(bdev->bd_disk)) |
| 1235 | bdev->bd_invalidated = 1; |
| 1236 | } |
| 1237 | |
| 1238 | /** |
Randy Dunlap | 57d1b53 | 2008-10-09 10:42:38 +0200 | [diff] [blame] | 1239 | * 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] | 1240 | * @disk: struct gendisk to check |
| 1241 | * @bdev: struct bdev to adjust. |
| 1242 | * |
| 1243 | * This routine checks to see if the bdev size does not match the disk size |
| 1244 | * and adjusts it if it differs. |
| 1245 | */ |
| 1246 | void check_disk_size_change(struct gendisk *disk, struct block_device *bdev) |
| 1247 | { |
| 1248 | loff_t disk_size, bdev_size; |
| 1249 | |
| 1250 | disk_size = (loff_t)get_capacity(disk) << 9; |
| 1251 | bdev_size = i_size_read(bdev->bd_inode); |
| 1252 | if (disk_size != bdev_size) { |
| 1253 | char name[BDEVNAME_SIZE]; |
| 1254 | |
| 1255 | disk_name(disk, 0, name); |
| 1256 | printk(KERN_INFO |
| 1257 | "%s: detected capacity change from %lld to %lld\n", |
| 1258 | name, bdev_size, disk_size); |
| 1259 | i_size_write(bdev->bd_inode, disk_size); |
Andrew Patterson | 608aeef | 2008-09-04 14:27:45 -0600 | [diff] [blame] | 1260 | flush_disk(bdev); |
Andrew Patterson | c3279d1 | 2008-09-04 14:27:25 -0600 | [diff] [blame] | 1261 | } |
| 1262 | } |
| 1263 | EXPORT_SYMBOL(check_disk_size_change); |
| 1264 | |
| 1265 | /** |
Randy Dunlap | 57d1b53 | 2008-10-09 10:42:38 +0200 | [diff] [blame] | 1266 | * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back |
Andrew Patterson | 0c002c2 | 2008-09-04 14:27:20 -0600 | [diff] [blame] | 1267 | * @disk: struct gendisk to be revalidated |
| 1268 | * |
| 1269 | * This routine is a wrapper for lower-level driver's revalidate_disk |
| 1270 | * call-backs. It is used to do common pre and post operations needed |
| 1271 | * for all revalidate_disk operations. |
| 1272 | */ |
| 1273 | int revalidate_disk(struct gendisk *disk) |
| 1274 | { |
Andrew Patterson | c3279d1 | 2008-09-04 14:27:25 -0600 | [diff] [blame] | 1275 | struct block_device *bdev; |
Andrew Patterson | 0c002c2 | 2008-09-04 14:27:20 -0600 | [diff] [blame] | 1276 | int ret = 0; |
| 1277 | |
| 1278 | if (disk->fops->revalidate_disk) |
| 1279 | ret = disk->fops->revalidate_disk(disk); |
| 1280 | |
Andrew Patterson | c3279d1 | 2008-09-04 14:27:25 -0600 | [diff] [blame] | 1281 | bdev = bdget_disk(disk, 0); |
| 1282 | if (!bdev) |
| 1283 | return ret; |
| 1284 | |
| 1285 | mutex_lock(&bdev->bd_mutex); |
| 1286 | check_disk_size_change(disk, bdev); |
| 1287 | mutex_unlock(&bdev->bd_mutex); |
| 1288 | bdput(bdev); |
Andrew Patterson | 0c002c2 | 2008-09-04 14:27:20 -0600 | [diff] [blame] | 1289 | return ret; |
| 1290 | } |
| 1291 | EXPORT_SYMBOL(revalidate_disk); |
| 1292 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | /* |
| 1294 | * This routine checks whether a removable media has been changed, |
| 1295 | * and invalidates all buffer-cache-entries in that case. This |
| 1296 | * is a relatively slow routine, so we have to try to minimize using |
| 1297 | * it. Thus it is called only upon a 'mount' or 'open'. This |
| 1298 | * is the best way of combining speed and utility, I think. |
| 1299 | * People changing diskettes in the middle of an operation deserve |
| 1300 | * to lose :-) |
| 1301 | */ |
| 1302 | int check_disk_change(struct block_device *bdev) |
| 1303 | { |
| 1304 | struct gendisk *disk = bdev->bd_disk; |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 1305 | const struct block_device_operations *bdops = disk->fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | |
| 1307 | if (!bdops->media_changed) |
| 1308 | return 0; |
| 1309 | if (!bdops->media_changed(bdev->bd_disk)) |
| 1310 | return 0; |
| 1311 | |
Andrew Patterson | 56ade44 | 2008-09-04 14:27:40 -0600 | [diff] [blame] | 1312 | flush_disk(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | if (bdops->revalidate_disk) |
| 1314 | bdops->revalidate_disk(bdev->bd_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | return 1; |
| 1316 | } |
| 1317 | |
| 1318 | EXPORT_SYMBOL(check_disk_change); |
| 1319 | |
| 1320 | void bd_set_size(struct block_device *bdev, loff_t size) |
| 1321 | { |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1322 | unsigned bsize = bdev_logical_block_size(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | |
| 1324 | bdev->bd_inode->i_size = size; |
| 1325 | while (bsize < PAGE_CACHE_SIZE) { |
| 1326 | if (size & bsize) |
| 1327 | break; |
| 1328 | bsize <<= 1; |
| 1329 | } |
| 1330 | bdev->bd_block_size = bsize; |
| 1331 | bdev->bd_inode->i_blkbits = blksize_bits(bsize); |
| 1332 | } |
| 1333 | EXPORT_SYMBOL(bd_set_size); |
| 1334 | |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1335 | 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] | 1336 | |
Peter Zijlstra | 6d740cd | 2007-02-20 13:58:18 -0800 | [diff] [blame] | 1337 | /* |
| 1338 | * bd_mutex locking: |
| 1339 | * |
| 1340 | * mutex_lock(part->bd_mutex) |
| 1341 | * mutex_lock_nested(whole->bd_mutex, 1) |
| 1342 | */ |
| 1343 | |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1344 | 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] | 1345 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | struct gendisk *disk; |
Pavel Emelyanov | 7db9cfd | 2008-06-05 22:46:27 -0700 | [diff] [blame] | 1347 | int ret; |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 1348 | int partno; |
Al Viro | fe6e9c1 | 2008-06-23 08:30:55 -0400 | [diff] [blame] | 1349 | int perm = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1351 | if (mode & FMODE_READ) |
Al Viro | fe6e9c1 | 2008-06-23 08:30:55 -0400 | [diff] [blame] | 1352 | perm |= MAY_READ; |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1353 | if (mode & FMODE_WRITE) |
Al Viro | fe6e9c1 | 2008-06-23 08:30:55 -0400 | [diff] [blame] | 1354 | perm |= MAY_WRITE; |
| 1355 | /* |
| 1356 | * hooks: /n/, see "layering violations". |
| 1357 | */ |
Chris Wright | b7300b7 | 2010-08-10 18:02:55 -0700 | [diff] [blame] | 1358 | if (!for_part) { |
| 1359 | ret = devcgroup_inode_permission(bdev->bd_inode, perm); |
| 1360 | if (ret != 0) { |
| 1361 | bdput(bdev); |
| 1362 | return ret; |
| 1363 | } |
Al Viro | 8266602 | 2008-08-01 05:32:04 -0400 | [diff] [blame] | 1364 | } |
Pavel Emelyanov | 7db9cfd | 2008-06-05 22:46:27 -0700 | [diff] [blame] | 1365 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 1366 | restart: |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1367 | |
Tejun Heo | 89f9749 | 2008-11-05 10:21:06 +0100 | [diff] [blame] | 1368 | ret = -ENXIO; |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 1369 | disk = get_gendisk(bdev->bd_dev, &partno); |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1370 | if (!disk) |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 1371 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | |
NeilBrown | 6796bf5 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1373 | mutex_lock_nested(&bdev->bd_mutex, for_part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | if (!bdev->bd_openers) { |
| 1375 | bdev->bd_disk = disk; |
| 1376 | bdev->bd_contains = bdev; |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 1377 | if (!partno) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | struct backing_dev_info *bdi; |
Tejun Heo | 89f9749 | 2008-11-05 10:21:06 +0100 | [diff] [blame] | 1379 | |
| 1380 | ret = -ENXIO; |
| 1381 | bdev->bd_part = disk_get_part(disk, partno); |
| 1382 | if (!bdev->bd_part) |
| 1383 | goto out_clear; |
| 1384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | if (disk->fops->open) { |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1386 | ret = disk->fops->open(bdev, mode); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 1387 | if (ret == -ERESTARTSYS) { |
| 1388 | /* Lost a race with 'disk' being |
| 1389 | * deleted, try again. |
| 1390 | * See md.c |
| 1391 | */ |
| 1392 | disk_put_part(bdev->bd_part); |
| 1393 | bdev->bd_part = NULL; |
| 1394 | module_put(disk->fops->owner); |
| 1395 | put_disk(disk); |
| 1396 | bdev->bd_disk = NULL; |
| 1397 | mutex_unlock(&bdev->bd_mutex); |
| 1398 | goto restart; |
| 1399 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | if (ret) |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1401 | goto out_clear; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | } |
| 1403 | if (!bdev->bd_openers) { |
| 1404 | bd_set_size(bdev,(loff_t)get_capacity(disk)<<9); |
| 1405 | bdi = blk_get_backing_dev_info(bdev); |
| 1406 | if (bdi == NULL) |
| 1407 | bdi = &default_backing_dev_info; |
Dave Chinner | a5491e0 | 2010-10-21 11:49:26 +1100 | [diff] [blame] | 1408 | bdev_inode_switch_bdi(bdev->bd_inode, bdi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | } |
| 1410 | if (bdev->bd_invalidated) |
| 1411 | rescan_partitions(disk, bdev); |
| 1412 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | struct block_device *whole; |
| 1414 | whole = bdget_disk(disk, 0); |
| 1415 | ret = -ENOMEM; |
| 1416 | if (!whole) |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1417 | goto out_clear; |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1418 | BUG_ON(for_part); |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1419 | ret = __blkdev_get(whole, mode, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | if (ret) |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1421 | goto out_clear; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | bdev->bd_contains = whole; |
Dave Chinner | a5491e0 | 2010-10-21 11:49:26 +1100 | [diff] [blame] | 1423 | bdev_inode_switch_bdi(bdev->bd_inode, |
| 1424 | whole->bd_inode->i_data.backing_dev_info); |
Tejun Heo | 89f9749 | 2008-11-05 10:21:06 +0100 | [diff] [blame] | 1425 | bdev->bd_part = disk_get_part(disk, partno); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 1426 | if (!(disk->flags & GENHD_FL_UP) || |
Tejun Heo | 89f9749 | 2008-11-05 10:21:06 +0100 | [diff] [blame] | 1427 | !bdev->bd_part || !bdev->bd_part->nr_sects) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | ret = -ENXIO; |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1429 | goto out_clear; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | } |
Tejun Heo | 89f9749 | 2008-11-05 10:21:06 +0100 | [diff] [blame] | 1431 | bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | } |
| 1433 | } else { |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1434 | module_put(disk->fops->owner); |
Neil Brown | 960cc0f | 2009-10-26 08:59:17 +0100 | [diff] [blame] | 1435 | put_disk(disk); |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1436 | disk = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | if (bdev->bd_contains == bdev) { |
| 1438 | if (bdev->bd_disk->fops->open) { |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1439 | ret = bdev->bd_disk->fops->open(bdev, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | if (ret) |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1441 | goto out_unlock_bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | } |
| 1443 | if (bdev->bd_invalidated) |
| 1444 | rescan_partitions(bdev->bd_disk, bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | } |
| 1446 | } |
| 1447 | bdev->bd_openers++; |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1448 | if (for_part) |
| 1449 | bdev->bd_part_count++; |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 1450 | mutex_unlock(&bdev->bd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | return 0; |
| 1452 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1453 | out_clear: |
Tejun Heo | 89f9749 | 2008-11-05 10:21:06 +0100 | [diff] [blame] | 1454 | disk_put_part(bdev->bd_part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | bdev->bd_disk = NULL; |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1456 | bdev->bd_part = NULL; |
Dave Chinner | a5491e0 | 2010-10-21 11:49:26 +1100 | [diff] [blame] | 1457 | bdev_inode_switch_bdi(bdev->bd_inode, &default_backing_dev_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | if (bdev != bdev->bd_contains) |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1459 | __blkdev_put(bdev->bd_contains, mode, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | bdev->bd_contains = NULL; |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1461 | out_unlock_bdev: |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 1462 | mutex_unlock(&bdev->bd_mutex); |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 1463 | out: |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1464 | if (disk) |
| 1465 | module_put(disk->fops->owner); |
| 1466 | put_disk(disk); |
| 1467 | bdput(bdev); |
| 1468 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | return ret; |
| 1470 | } |
| 1471 | |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1472 | int blkdev_get(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | { |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1474 | return __blkdev_get(bdev, mode, 0); |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1475 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | EXPORT_SYMBOL(blkdev_get); |
| 1477 | |
| 1478 | static int blkdev_open(struct inode * inode, struct file * filp) |
| 1479 | { |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 1480 | struct block_device *whole = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | struct block_device *bdev; |
| 1482 | int res; |
| 1483 | |
| 1484 | /* |
| 1485 | * Preserve backwards compatibility and allow large file access |
| 1486 | * even if userspace doesn't ask for it explicitly. Some mkfs |
| 1487 | * binary needs it. We might want to drop this workaround |
| 1488 | * during an unstable branch. |
| 1489 | */ |
| 1490 | filp->f_flags |= O_LARGEFILE; |
| 1491 | |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1492 | if (filp->f_flags & O_NDELAY) |
| 1493 | filp->f_mode |= FMODE_NDELAY; |
| 1494 | if (filp->f_flags & O_EXCL) |
| 1495 | filp->f_mode |= FMODE_EXCL; |
| 1496 | if ((filp->f_flags & O_ACCMODE) == 3) |
| 1497 | filp->f_mode |= FMODE_WRITE_IOCTL; |
| 1498 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | bdev = bd_acquire(inode); |
Pavel Emelianov | 6a2aae0 | 2006-10-28 10:38:33 -0700 | [diff] [blame] | 1500 | if (bdev == NULL) |
| 1501 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 1503 | if (filp->f_mode & FMODE_EXCL) { |
| 1504 | whole = bd_start_claiming(bdev, filp); |
| 1505 | if (IS_ERR(whole)) { |
| 1506 | bdput(bdev); |
| 1507 | return PTR_ERR(whole); |
| 1508 | } |
| 1509 | } |
| 1510 | |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1511 | filp->f_mapping = bdev->bd_inode->i_mapping; |
| 1512 | |
| 1513 | res = blkdev_get(bdev, filp->f_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 1515 | if (whole) { |
| 1516 | if (res == 0) |
Nick Piggin | b001836 | 2010-05-26 01:51:19 +1000 | [diff] [blame] | 1517 | bd_finish_claiming(bdev, whole, filp); |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 1518 | else |
| 1519 | bd_abort_claiming(whole, filp); |
Christoph Hellwig | ebbefc0 | 2008-11-05 14:54:41 +0100 | [diff] [blame] | 1520 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | return res; |
| 1523 | } |
| 1524 | |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1525 | 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] | 1526 | { |
| 1527 | int ret = 0; |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1528 | struct gendisk *disk = bdev->bd_disk; |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1529 | struct block_device *victim = NULL; |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1530 | |
NeilBrown | 6796bf5 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1531 | mutex_lock_nested(&bdev->bd_mutex, for_part); |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1532 | if (for_part) |
| 1533 | bdev->bd_part_count--; |
| 1534 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1535 | if (!--bdev->bd_openers) { |
| 1536 | sync_blockdev(bdev); |
| 1537 | kill_bdev(bdev); |
| 1538 | } |
| 1539 | if (bdev->bd_contains == bdev) { |
| 1540 | if (disk->fops->release) |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1541 | ret = disk->fops->release(disk, mode); |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1542 | } |
| 1543 | if (!bdev->bd_openers) { |
| 1544 | struct module *owner = disk->fops->owner; |
| 1545 | |
| 1546 | put_disk(disk); |
| 1547 | module_put(owner); |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1548 | disk_put_part(bdev->bd_part); |
| 1549 | bdev->bd_part = NULL; |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1550 | bdev->bd_disk = NULL; |
Dave Chinner | a5491e0 | 2010-10-21 11:49:26 +1100 | [diff] [blame] | 1551 | bdev_inode_switch_bdi(bdev->bd_inode, |
| 1552 | &default_backing_dev_info); |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1553 | if (bdev != bdev->bd_contains) |
| 1554 | victim = bdev->bd_contains; |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1555 | bdev->bd_contains = NULL; |
| 1556 | } |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1557 | mutex_unlock(&bdev->bd_mutex); |
| 1558 | bdput(bdev); |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1559 | if (victim) |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1560 | __blkdev_put(victim, mode, 1); |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1561 | return ret; |
| 1562 | } |
| 1563 | |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1564 | int blkdev_put(struct block_device *bdev, fmode_t mode) |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1565 | { |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1566 | return __blkdev_put(bdev, mode, 0); |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1567 | } |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1568 | EXPORT_SYMBOL(blkdev_put); |
| 1569 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | static int blkdev_close(struct inode * inode, struct file * filp) |
| 1571 | { |
| 1572 | struct block_device *bdev = I_BDEV(filp->f_mapping->host); |
| 1573 | if (bdev->bd_holder == filp) |
| 1574 | bd_release(bdev); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1575 | return blkdev_put(bdev, filp->f_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | } |
| 1577 | |
Arnd Bergmann | bb93e3a | 2005-06-23 00:10:15 -0700 | [diff] [blame] | 1578 | static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | { |
Al Viro | 56b26ad | 2008-09-19 03:17:36 -0400 | [diff] [blame] | 1580 | struct block_device *bdev = I_BDEV(file->f_mapping->host); |
| 1581 | fmode_t mode = file->f_mode; |
Christoph Hellwig | fd4ce1a | 2008-11-05 14:58:42 +0100 | [diff] [blame] | 1582 | |
| 1583 | /* |
| 1584 | * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have |
| 1585 | * to updated it before every ioctl. |
| 1586 | */ |
Al Viro | 56b26ad | 2008-09-19 03:17:36 -0400 | [diff] [blame] | 1587 | if (file->f_flags & O_NDELAY) |
Christoph Hellwig | fd4ce1a | 2008-11-05 14:58:42 +0100 | [diff] [blame] | 1588 | mode |= FMODE_NDELAY; |
| 1589 | else |
| 1590 | mode &= ~FMODE_NDELAY; |
| 1591 | |
Al Viro | 56b26ad | 2008-09-19 03:17:36 -0400 | [diff] [blame] | 1592 | return blkdev_ioctl(bdev, mode, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | } |
| 1594 | |
Theodore Ts'o | 87d8fe1 | 2009-01-03 09:47:09 -0500 | [diff] [blame] | 1595 | /* |
Christoph Hellwig | eef9938 | 2009-08-20 17:43:41 +0200 | [diff] [blame] | 1596 | * Write data to the block device. Only intended for the block device itself |
| 1597 | * and the raw driver which basically is a fake block device. |
| 1598 | * |
| 1599 | * Does not take i_mutex for the write and thus is not for general purpose |
| 1600 | * use. |
| 1601 | */ |
| 1602 | ssize_t blkdev_aio_write(struct kiocb *iocb, const struct iovec *iov, |
| 1603 | unsigned long nr_segs, loff_t pos) |
| 1604 | { |
| 1605 | struct file *file = iocb->ki_filp; |
| 1606 | ssize_t ret; |
| 1607 | |
| 1608 | BUG_ON(iocb->ki_pos != pos); |
| 1609 | |
| 1610 | ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos); |
| 1611 | if (ret > 0 || ret == -EIOCBQUEUED) { |
| 1612 | ssize_t err; |
| 1613 | |
| 1614 | err = generic_write_sync(file, pos, ret); |
| 1615 | if (err < 0 && ret > 0) |
| 1616 | ret = err; |
| 1617 | } |
| 1618 | return ret; |
| 1619 | } |
| 1620 | EXPORT_SYMBOL_GPL(blkdev_aio_write); |
| 1621 | |
| 1622 | /* |
Theodore Ts'o | 87d8fe1 | 2009-01-03 09:47:09 -0500 | [diff] [blame] | 1623 | * Try to release a page associated with block device when the system |
| 1624 | * is under memory pressure. |
| 1625 | */ |
| 1626 | static int blkdev_releasepage(struct page *page, gfp_t wait) |
| 1627 | { |
| 1628 | struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super; |
| 1629 | |
| 1630 | if (super && super->s_op->bdev_try_to_free_page) |
| 1631 | return super->s_op->bdev_try_to_free_page(super, page, wait); |
| 1632 | |
| 1633 | return try_to_free_buffers(page); |
| 1634 | } |
| 1635 | |
Adrian Bunk | 4c54ac6 | 2008-02-18 13:48:31 +0100 | [diff] [blame] | 1636 | static const struct address_space_operations def_blk_aops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | .readpage = blkdev_readpage, |
| 1638 | .writepage = blkdev_writepage, |
| 1639 | .sync_page = block_sync_page, |
Nick Piggin | 6272b5a | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 1640 | .write_begin = blkdev_write_begin, |
| 1641 | .write_end = blkdev_write_end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | .writepages = generic_writepages, |
Theodore Ts'o | 87d8fe1 | 2009-01-03 09:47:09 -0500 | [diff] [blame] | 1643 | .releasepage = blkdev_releasepage, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | .direct_IO = blkdev_direct_IO, |
| 1645 | }; |
| 1646 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 1647 | const struct file_operations def_blk_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | .open = blkdev_open, |
| 1649 | .release = blkdev_close, |
| 1650 | .llseek = block_llseek, |
Badari Pulavarty | 543ade1 | 2006-09-30 23:28:48 -0700 | [diff] [blame] | 1651 | .read = do_sync_read, |
| 1652 | .write = do_sync_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | .aio_read = generic_file_aio_read, |
Christoph Hellwig | eef9938 | 2009-08-20 17:43:41 +0200 | [diff] [blame] | 1654 | .aio_write = blkdev_aio_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | .mmap = generic_file_mmap, |
Andrew Morton | b1dd3b2 | 2010-04-06 14:35:00 -0700 | [diff] [blame] | 1656 | .fsync = blkdev_fsync, |
Arnd Bergmann | bb93e3a | 2005-06-23 00:10:15 -0700 | [diff] [blame] | 1657 | .unlocked_ioctl = block_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | #ifdef CONFIG_COMPAT |
| 1659 | .compat_ioctl = compat_blkdev_ioctl, |
| 1660 | #endif |
Jens Axboe | 7f9c51f | 2006-05-01 19:59:32 +0200 | [diff] [blame] | 1661 | .splice_read = generic_file_splice_read, |
| 1662 | .splice_write = generic_file_splice_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | }; |
| 1664 | |
| 1665 | int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg) |
| 1666 | { |
| 1667 | int res; |
| 1668 | mm_segment_t old_fs = get_fs(); |
| 1669 | set_fs(KERNEL_DS); |
Al Viro | 56b26ad | 2008-09-19 03:17:36 -0400 | [diff] [blame] | 1670 | res = blkdev_ioctl(bdev, 0, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | set_fs(old_fs); |
| 1672 | return res; |
| 1673 | } |
| 1674 | |
| 1675 | EXPORT_SYMBOL(ioctl_by_bdev); |
| 1676 | |
| 1677 | /** |
| 1678 | * lookup_bdev - lookup a struct block_device by name |
Randy Dunlap | 94e2959 | 2009-01-06 14:41:15 -0800 | [diff] [blame] | 1679 | * @pathname: special file representing the block device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | * |
Randy Dunlap | 57d1b53 | 2008-10-09 10:42:38 +0200 | [diff] [blame] | 1681 | * Get a reference to the blockdevice at @pathname in the current |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | * namespace if possible and return it. Return ERR_PTR(error) |
| 1683 | * otherwise. |
| 1684 | */ |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1685 | struct block_device *lookup_bdev(const char *pathname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | { |
| 1687 | struct block_device *bdev; |
| 1688 | struct inode *inode; |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1689 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | int error; |
| 1691 | |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1692 | if (!pathname || !*pathname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | return ERR_PTR(-EINVAL); |
| 1694 | |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1695 | error = kern_path(pathname, LOOKUP_FOLLOW, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | if (error) |
| 1697 | return ERR_PTR(error); |
| 1698 | |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1699 | inode = path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | error = -ENOTBLK; |
| 1701 | if (!S_ISBLK(inode->i_mode)) |
| 1702 | goto fail; |
| 1703 | error = -EACCES; |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1704 | if (path.mnt->mnt_flags & MNT_NODEV) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | goto fail; |
| 1706 | error = -ENOMEM; |
| 1707 | bdev = bd_acquire(inode); |
| 1708 | if (!bdev) |
| 1709 | goto fail; |
| 1710 | out: |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1711 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | return bdev; |
| 1713 | fail: |
| 1714 | bdev = ERR_PTR(error); |
| 1715 | goto out; |
| 1716 | } |
Al Viro | d5686b4 | 2008-08-01 05:00:11 -0400 | [diff] [blame] | 1717 | EXPORT_SYMBOL(lookup_bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | |
| 1719 | /** |
Al Viro | 30c40d2 | 2008-02-22 19:50:45 -0500 | [diff] [blame] | 1720 | * open_bdev_exclusive - open a block device by name and set it up for use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | * |
| 1722 | * @path: special file representing the block device |
Al Viro | 30c40d2 | 2008-02-22 19:50:45 -0500 | [diff] [blame] | 1723 | * @mode: FMODE_... combination to pass be used |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | * @holder: owner for exclusion |
| 1725 | * |
| 1726 | * Open the blockdevice described by the special file at @path, claim it |
| 1727 | * for the @holder. |
| 1728 | */ |
Al Viro | 30c40d2 | 2008-02-22 19:50:45 -0500 | [diff] [blame] | 1729 | struct block_device *open_bdev_exclusive(const char *path, fmode_t mode, void *holder) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | { |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 1731 | struct block_device *bdev, *whole; |
| 1732 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | |
| 1734 | bdev = lookup_bdev(path); |
| 1735 | if (IS_ERR(bdev)) |
| 1736 | return bdev; |
| 1737 | |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 1738 | whole = bd_start_claiming(bdev, holder); |
| 1739 | if (IS_ERR(whole)) { |
| 1740 | bdput(bdev); |
| 1741 | return whole; |
| 1742 | } |
| 1743 | |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1744 | error = blkdev_get(bdev, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | if (error) |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 1746 | goto out_abort_claiming; |
| 1747 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | error = -EACCES; |
Al Viro | 30c40d2 | 2008-02-22 19:50:45 -0500 | [diff] [blame] | 1749 | if ((mode & FMODE_WRITE) && bdev_read_only(bdev)) |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 1750 | goto out_blkdev_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | |
Nick Piggin | b001836 | 2010-05-26 01:51:19 +1000 | [diff] [blame] | 1752 | bd_finish_claiming(bdev, whole, holder); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | return bdev; |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 1754 | |
| 1755 | out_blkdev_put: |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1756 | blkdev_put(bdev, mode); |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 1757 | out_abort_claiming: |
| 1758 | bd_abort_claiming(whole, holder); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | return ERR_PTR(error); |
| 1760 | } |
| 1761 | |
Al Viro | 30c40d2 | 2008-02-22 19:50:45 -0500 | [diff] [blame] | 1762 | EXPORT_SYMBOL(open_bdev_exclusive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | |
| 1764 | /** |
Al Viro | 30c40d2 | 2008-02-22 19:50:45 -0500 | [diff] [blame] | 1765 | * close_bdev_exclusive - close a blockdevice opened by open_bdev_exclusive() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | * |
| 1767 | * @bdev: blockdevice to close |
Al Viro | 30c40d2 | 2008-02-22 19:50:45 -0500 | [diff] [blame] | 1768 | * @mode: mode, must match that used to open. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | * |
Al Viro | 30c40d2 | 2008-02-22 19:50:45 -0500 | [diff] [blame] | 1770 | * This is the counterpart to open_bdev_exclusive(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | */ |
Al Viro | 30c40d2 | 2008-02-22 19:50:45 -0500 | [diff] [blame] | 1772 | void close_bdev_exclusive(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | { |
| 1774 | bd_release(bdev); |
Al Viro | 30c40d2 | 2008-02-22 19:50:45 -0500 | [diff] [blame] | 1775 | blkdev_put(bdev, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | } |
| 1777 | |
Al Viro | 30c40d2 | 2008-02-22 19:50:45 -0500 | [diff] [blame] | 1778 | EXPORT_SYMBOL(close_bdev_exclusive); |
David Howells | b71e8a4 | 2006-08-29 19:06:11 +0100 | [diff] [blame] | 1779 | |
| 1780 | int __invalidate_device(struct block_device *bdev) |
| 1781 | { |
| 1782 | struct super_block *sb = get_super(bdev); |
| 1783 | int res = 0; |
| 1784 | |
| 1785 | if (sb) { |
| 1786 | /* |
| 1787 | * no need to lock the super, get_super holds the |
| 1788 | * read mutex so the filesystem cannot go away |
| 1789 | * under us (->put_super runs with the write lock |
| 1790 | * hold). |
| 1791 | */ |
| 1792 | shrink_dcache_sb(sb); |
| 1793 | res = invalidate_inodes(sb); |
| 1794 | drop_super(sb); |
| 1795 | } |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 1796 | invalidate_bdev(bdev); |
David Howells | b71e8a4 | 2006-08-29 19:06:11 +0100 | [diff] [blame] | 1797 | return res; |
| 1798 | } |
| 1799 | EXPORT_SYMBOL(__invalidate_device); |