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); |
Christoph Lameter | a35afb8 | 2007-05-16 22:10:57 -0700 | [diff] [blame] | 429 | inode_init_once(&ei->vfs_inode); |
Takashi Sato | fcccf50 | 2009-01-09 16:40:59 -0800 | [diff] [blame] | 430 | /* Initialize mutex for freeze. */ |
| 431 | mutex_init(&bdev->bd_fsfreeze_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | static inline void __bd_forget(struct inode *inode) |
| 435 | { |
| 436 | list_del_init(&inode->i_devices); |
| 437 | inode->i_bdev = NULL; |
| 438 | inode->i_mapping = &inode->i_data; |
| 439 | } |
| 440 | |
Al Viro | b57922d | 2010-06-07 14:34:48 -0400 | [diff] [blame] | 441 | static void bdev_evict_inode(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | { |
| 443 | struct block_device *bdev = &BDEV_I(inode)->bdev; |
| 444 | struct list_head *p; |
Al Viro | b57922d | 2010-06-07 14:34:48 -0400 | [diff] [blame] | 445 | truncate_inode_pages(&inode->i_data, 0); |
| 446 | invalidate_inode_buffers(inode); /* is it needed here? */ |
| 447 | end_writeback(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | spin_lock(&bdev_lock); |
| 449 | while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) { |
| 450 | __bd_forget(list_entry(p, struct inode, i_devices)); |
| 451 | } |
| 452 | list_del_init(&bdev->bd_list); |
| 453 | spin_unlock(&bdev_lock); |
| 454 | } |
| 455 | |
Josef 'Jeff' Sipek | ee9b6d6 | 2007-02-12 00:55:41 -0800 | [diff] [blame] | 456 | static const struct super_operations bdev_sops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | .statfs = simple_statfs, |
| 458 | .alloc_inode = bdev_alloc_inode, |
| 459 | .destroy_inode = bdev_destroy_inode, |
| 460 | .drop_inode = generic_delete_inode, |
Al Viro | b57922d | 2010-06-07 14:34:48 -0400 | [diff] [blame] | 461 | .evict_inode = bdev_evict_inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | }; |
| 463 | |
Al Viro | 51139ad | 2010-07-25 23:47:46 +0400 | [diff] [blame] | 464 | static struct dentry *bd_mount(struct file_system_type *fs_type, |
| 465 | int flags, const char *dev_name, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | { |
Al Viro | 51139ad | 2010-07-25 23:47:46 +0400 | [diff] [blame] | 467 | return mount_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | static struct file_system_type bd_type = { |
| 471 | .name = "bdev", |
Al Viro | 51139ad | 2010-07-25 23:47:46 +0400 | [diff] [blame] | 472 | .mount = bd_mount, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | .kill_sb = kill_anon_super, |
| 474 | }; |
| 475 | |
Denis ChengRq | c2acf7b | 2008-12-01 14:34:56 -0800 | [diff] [blame] | 476 | struct super_block *blockdev_superblock __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | |
| 478 | void __init bdev_cache_init(void) |
| 479 | { |
| 480 | int err; |
Denis ChengRq | c2acf7b | 2008-12-01 14:34:56 -0800 | [diff] [blame] | 481 | struct vfsmount *bd_mnt; |
| 482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode), |
Paul Jackson | fffb60f | 2006-03-24 03:16:06 -0800 | [diff] [blame] | 484 | 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT| |
| 485 | SLAB_MEM_SPREAD|SLAB_PANIC), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 486 | init_once); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | err = register_filesystem(&bd_type); |
| 488 | if (err) |
| 489 | panic("Cannot register bdev pseudo-fs"); |
| 490 | bd_mnt = kern_mount(&bd_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | if (IS_ERR(bd_mnt)) |
| 492 | panic("Cannot create bdev pseudo-fs"); |
Catalin Marinas | 2e1483c | 2009-06-11 13:24:13 +0100 | [diff] [blame] | 493 | /* |
| 494 | * This vfsmount structure is only used to obtain the |
| 495 | * blockdev_superblock, so tell kmemleak not to report it. |
| 496 | */ |
| 497 | kmemleak_not_leak(bd_mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */ |
| 499 | } |
| 500 | |
| 501 | /* |
| 502 | * Most likely _very_ bad one - but then it's hardly critical for small |
| 503 | * /dev and can be fixed when somebody will need really large one. |
| 504 | * Keep in mind that it will be fed through icache hash function too. |
| 505 | */ |
| 506 | static inline unsigned long hash(dev_t dev) |
| 507 | { |
| 508 | return MAJOR(dev)+MINOR(dev); |
| 509 | } |
| 510 | |
| 511 | static int bdev_test(struct inode *inode, void *data) |
| 512 | { |
| 513 | return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data; |
| 514 | } |
| 515 | |
| 516 | static int bdev_set(struct inode *inode, void *data) |
| 517 | { |
| 518 | BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data; |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | static LIST_HEAD(all_bdevs); |
| 523 | |
| 524 | struct block_device *bdget(dev_t dev) |
| 525 | { |
| 526 | struct block_device *bdev; |
| 527 | struct inode *inode; |
| 528 | |
Denis ChengRq | c2acf7b | 2008-12-01 14:34:56 -0800 | [diff] [blame] | 529 | inode = iget5_locked(blockdev_superblock, hash(dev), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | bdev_test, bdev_set, &dev); |
| 531 | |
| 532 | if (!inode) |
| 533 | return NULL; |
| 534 | |
| 535 | bdev = &BDEV_I(inode)->bdev; |
| 536 | |
| 537 | if (inode->i_state & I_NEW) { |
| 538 | bdev->bd_contains = NULL; |
| 539 | bdev->bd_inode = inode; |
| 540 | bdev->bd_block_size = (1 << inode->i_blkbits); |
| 541 | bdev->bd_part_count = 0; |
| 542 | bdev->bd_invalidated = 0; |
| 543 | inode->i_mode = S_IFBLK; |
| 544 | inode->i_rdev = dev; |
| 545 | inode->i_bdev = bdev; |
| 546 | inode->i_data.a_ops = &def_blk_aops; |
| 547 | mapping_set_gfp_mask(&inode->i_data, GFP_USER); |
| 548 | inode->i_data.backing_dev_info = &default_backing_dev_info; |
| 549 | spin_lock(&bdev_lock); |
| 550 | list_add(&bdev->bd_list, &all_bdevs); |
| 551 | spin_unlock(&bdev_lock); |
| 552 | unlock_new_inode(inode); |
| 553 | } |
| 554 | return bdev; |
| 555 | } |
| 556 | |
| 557 | EXPORT_SYMBOL(bdget); |
| 558 | |
Alan Jenkins | dddac6a | 2009-07-29 21:07:55 +0200 | [diff] [blame] | 559 | /** |
| 560 | * bdgrab -- Grab a reference to an already referenced block device |
| 561 | * @bdev: Block device to grab a reference to. |
| 562 | */ |
| 563 | struct block_device *bdgrab(struct block_device *bdev) |
| 564 | { |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 565 | ihold(bdev->bd_inode); |
Alan Jenkins | dddac6a | 2009-07-29 21:07:55 +0200 | [diff] [blame] | 566 | return bdev; |
| 567 | } |
| 568 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | long nr_blockdev_pages(void) |
| 570 | { |
Matthias Kaehlcke | 203a293 | 2007-07-15 23:40:23 -0700 | [diff] [blame] | 571 | struct block_device *bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | long ret = 0; |
| 573 | spin_lock(&bdev_lock); |
Matthias Kaehlcke | 203a293 | 2007-07-15 23:40:23 -0700 | [diff] [blame] | 574 | list_for_each_entry(bdev, &all_bdevs, bd_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | ret += bdev->bd_inode->i_mapping->nrpages; |
| 576 | } |
| 577 | spin_unlock(&bdev_lock); |
| 578 | return ret; |
| 579 | } |
| 580 | |
| 581 | void bdput(struct block_device *bdev) |
| 582 | { |
| 583 | iput(bdev->bd_inode); |
| 584 | } |
| 585 | |
| 586 | EXPORT_SYMBOL(bdput); |
| 587 | |
| 588 | static struct block_device *bd_acquire(struct inode *inode) |
| 589 | { |
| 590 | struct block_device *bdev; |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 591 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | spin_lock(&bdev_lock); |
| 593 | bdev = inode->i_bdev; |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 594 | if (bdev) { |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 595 | ihold(bdev->bd_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | spin_unlock(&bdev_lock); |
| 597 | return bdev; |
| 598 | } |
| 599 | spin_unlock(&bdev_lock); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 600 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | bdev = bdget(inode->i_rdev); |
| 602 | if (bdev) { |
| 603 | spin_lock(&bdev_lock); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 604 | if (!inode->i_bdev) { |
| 605 | /* |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 606 | * We take an additional reference to bd_inode, |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 607 | * and it's released in clear_inode() of inode. |
| 608 | * So, we can access it via ->i_mapping always |
| 609 | * without igrab(). |
| 610 | */ |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 611 | ihold(bdev->bd_inode); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 612 | inode->i_bdev = bdev; |
| 613 | inode->i_mapping = bdev->bd_inode->i_mapping; |
| 614 | list_add(&inode->i_devices, &bdev->bd_inodes); |
| 615 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | spin_unlock(&bdev_lock); |
| 617 | } |
| 618 | return bdev; |
| 619 | } |
| 620 | |
| 621 | /* Call when you free inode */ |
| 622 | |
| 623 | void bd_forget(struct inode *inode) |
| 624 | { |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 625 | struct block_device *bdev = NULL; |
| 626 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | spin_lock(&bdev_lock); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 628 | if (inode->i_bdev) { |
Denis ChengRq | c2acf7b | 2008-12-01 14:34:56 -0800 | [diff] [blame] | 629 | if (!sb_is_blkdev_sb(inode->i_sb)) |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 630 | bdev = inode->i_bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | __bd_forget(inode); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 632 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | spin_unlock(&bdev_lock); |
OGAWA Hirofumi | 09d967c | 2006-06-22 14:47:21 -0700 | [diff] [blame] | 634 | |
| 635 | if (bdev) |
| 636 | iput(bdev->bd_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | } |
| 638 | |
Tejun Heo | 1a3cbbc | 2010-04-07 18:52:29 +0900 | [diff] [blame] | 639 | /** |
| 640 | * bd_may_claim - test whether a block device can be claimed |
| 641 | * @bdev: block device of interest |
| 642 | * @whole: whole block device containing @bdev, may equal @bdev |
| 643 | * @holder: holder trying to claim @bdev |
| 644 | * |
| 645 | * Test whther @bdev can be claimed by @holder. |
| 646 | * |
| 647 | * CONTEXT: |
| 648 | * spin_lock(&bdev_lock). |
| 649 | * |
| 650 | * RETURNS: |
| 651 | * %true if @bdev can be claimed, %false otherwise. |
| 652 | */ |
| 653 | static bool bd_may_claim(struct block_device *bdev, struct block_device *whole, |
| 654 | void *holder) |
| 655 | { |
| 656 | if (bdev->bd_holder == holder) |
| 657 | return true; /* already a holder */ |
| 658 | else if (bdev->bd_holder != NULL) |
| 659 | return false; /* held by someone else */ |
| 660 | else if (bdev->bd_contains == bdev) |
| 661 | return true; /* is a whole device which isn't held */ |
| 662 | |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 663 | else if (whole->bd_holder == bd_may_claim) |
Tejun Heo | 1a3cbbc | 2010-04-07 18:52:29 +0900 | [diff] [blame] | 664 | return true; /* is a partition of a device that is being partitioned */ |
| 665 | else if (whole->bd_holder != NULL) |
| 666 | return false; /* is a partition of a held device */ |
| 667 | else |
| 668 | return true; /* is a partition of an un-held device */ |
| 669 | } |
| 670 | |
| 671 | /** |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 672 | * bd_prepare_to_claim - prepare to claim a block device |
| 673 | * @bdev: block device of interest |
| 674 | * @whole: the whole device containing @bdev, may equal @bdev |
| 675 | * @holder: holder trying to claim @bdev |
| 676 | * |
| 677 | * Prepare to claim @bdev. This function fails if @bdev is already |
| 678 | * claimed by another holder and waits if another claiming is in |
| 679 | * progress. This function doesn't actually claim. On successful |
| 680 | * return, the caller has ownership of bd_claiming and bd_holder[s]. |
| 681 | * |
| 682 | * CONTEXT: |
| 683 | * spin_lock(&bdev_lock). Might release bdev_lock, sleep and regrab |
| 684 | * it multiple times. |
| 685 | * |
| 686 | * RETURNS: |
| 687 | * 0 if @bdev can be claimed, -EBUSY otherwise. |
| 688 | */ |
| 689 | static int bd_prepare_to_claim(struct block_device *bdev, |
| 690 | struct block_device *whole, void *holder) |
| 691 | { |
| 692 | retry: |
| 693 | /* if someone else claimed, fail */ |
| 694 | if (!bd_may_claim(bdev, whole, holder)) |
| 695 | return -EBUSY; |
| 696 | |
Tejun Heo | e75aa85 | 2010-08-04 17:59:39 +0200 | [diff] [blame] | 697 | /* if claiming is already in progress, wait for it to finish */ |
| 698 | if (whole->bd_claiming) { |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 699 | wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0); |
| 700 | DEFINE_WAIT(wait); |
| 701 | |
| 702 | prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE); |
| 703 | spin_unlock(&bdev_lock); |
| 704 | schedule(); |
| 705 | finish_wait(wq, &wait); |
| 706 | spin_lock(&bdev_lock); |
| 707 | goto retry; |
| 708 | } |
| 709 | |
| 710 | /* yay, all mine */ |
| 711 | return 0; |
| 712 | } |
| 713 | |
| 714 | /** |
| 715 | * bd_start_claiming - start claiming a block device |
| 716 | * @bdev: block device of interest |
| 717 | * @holder: holder trying to claim @bdev |
| 718 | * |
| 719 | * @bdev is about to be opened exclusively. Check @bdev can be opened |
| 720 | * exclusively and mark that an exclusive open is in progress. Each |
| 721 | * successful call to this function must be matched with a call to |
Nick Piggin | b001836 | 2010-05-26 01:51:19 +1000 | [diff] [blame] | 722 | * either bd_finish_claiming() or bd_abort_claiming() (which do not |
| 723 | * fail). |
| 724 | * |
| 725 | * This function is used to gain exclusive access to the block device |
| 726 | * without actually causing other exclusive open attempts to fail. It |
| 727 | * should be used when the open sequence itself requires exclusive |
| 728 | * access but may subsequently fail. |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 729 | * |
| 730 | * CONTEXT: |
| 731 | * Might sleep. |
| 732 | * |
| 733 | * RETURNS: |
| 734 | * Pointer to the block device containing @bdev on success, ERR_PTR() |
| 735 | * value on failure. |
| 736 | */ |
| 737 | static struct block_device *bd_start_claiming(struct block_device *bdev, |
| 738 | void *holder) |
| 739 | { |
| 740 | struct gendisk *disk; |
| 741 | struct block_device *whole; |
| 742 | int partno, err; |
| 743 | |
| 744 | might_sleep(); |
| 745 | |
| 746 | /* |
| 747 | * @bdev might not have been initialized properly yet, look up |
| 748 | * and grab the outer block device the hard way. |
| 749 | */ |
| 750 | disk = get_gendisk(bdev->bd_dev, &partno); |
| 751 | if (!disk) |
| 752 | return ERR_PTR(-ENXIO); |
| 753 | |
| 754 | whole = bdget_disk(disk, 0); |
Nick Piggin | cf34257 | 2010-05-26 01:50:21 +1000 | [diff] [blame] | 755 | module_put(disk->fops->owner); |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 756 | put_disk(disk); |
| 757 | if (!whole) |
| 758 | return ERR_PTR(-ENOMEM); |
| 759 | |
| 760 | /* prepare to claim, if successful, mark claiming in progress */ |
| 761 | spin_lock(&bdev_lock); |
| 762 | |
| 763 | err = bd_prepare_to_claim(bdev, whole, holder); |
| 764 | if (err == 0) { |
| 765 | whole->bd_claiming = holder; |
| 766 | spin_unlock(&bdev_lock); |
| 767 | return whole; |
| 768 | } else { |
| 769 | spin_unlock(&bdev_lock); |
| 770 | bdput(whole); |
| 771 | return ERR_PTR(err); |
| 772 | } |
| 773 | } |
| 774 | |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 775 | #ifdef CONFIG_SYSFS |
Andrew Morton | 4d7dd8fd | 2006-09-29 01:58:56 -0700 | [diff] [blame] | 776 | static int add_symlink(struct kobject *from, struct kobject *to) |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 777 | { |
Andrew Morton | 4d7dd8fd | 2006-09-29 01:58:56 -0700 | [diff] [blame] | 778 | return sysfs_create_link(from, to, kobject_name(to)); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | static void del_symlink(struct kobject *from, struct kobject *to) |
| 782 | { |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 783 | sysfs_remove_link(from, kobject_name(to)); |
| 784 | } |
| 785 | |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 786 | /** |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 787 | * bd_link_disk_holder - create symlinks between holding disk and slave bdev |
| 788 | * @bdev: the claimed slave bdev |
| 789 | * @disk: the holding disk |
Jun'ichi Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 790 | * |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 791 | * This functions creates the following sysfs symlinks. |
Jun'ichi Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 792 | * |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 793 | * - from "slaves" directory of the holder @disk to the claimed @bdev |
| 794 | * - from "holders" directory of the @bdev to the holder @disk |
| 795 | * |
| 796 | * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is |
| 797 | * passed to bd_link_disk_holder(), then: |
| 798 | * |
| 799 | * /sys/block/dm-0/slaves/sda --> /sys/block/sda |
| 800 | * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0 |
| 801 | * |
| 802 | * The caller must have claimed @bdev before calling this function and |
| 803 | * ensure that both @bdev and @disk are valid during the creation and |
| 804 | * lifetime of these symlinks. |
| 805 | * |
| 806 | * CONTEXT: |
| 807 | * Might sleep. |
| 808 | * |
| 809 | * RETURNS: |
| 810 | * 0 on success, -errno on failure. |
Jun'ichi Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 811 | */ |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 812 | int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk) |
Jun'ichi Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 813 | { |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 814 | int ret = 0; |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 815 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 816 | mutex_lock(&bdev->bd_mutex); |
Jun'ichi Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 817 | |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 818 | WARN_ON_ONCE(!bdev->bd_holder || bdev->bd_holder_disk); |
Johannes Weiner | 4e91672 | 2007-07-15 23:41:25 -0700 | [diff] [blame] | 819 | |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 820 | /* FIXME: remove the following once add_disk() handles errors */ |
| 821 | if (WARN_ON(!disk->slave_dir || !bdev->bd_part->holder_dir)) |
| 822 | goto out_unlock; |
Johannes Weiner | 4e91672 | 2007-07-15 23:41:25 -0700 | [diff] [blame] | 823 | |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 824 | ret = add_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj); |
| 825 | if (ret) |
| 826 | goto out_unlock; |
| 827 | |
| 828 | ret = add_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj); |
| 829 | if (ret) { |
| 830 | del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj); |
| 831 | goto out_unlock; |
| 832 | } |
| 833 | |
| 834 | bdev->bd_holder_disk = disk; |
| 835 | out_unlock: |
Jun'ichi Nomura | b4cf1b7 | 2006-03-27 01:18:00 -0800 | [diff] [blame] | 836 | mutex_unlock(&bdev->bd_mutex); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 837 | return ret; |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 838 | } |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 839 | EXPORT_SYMBOL_GPL(bd_link_disk_holder); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 840 | |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 841 | static void bd_unlink_disk_holder(struct block_device *bdev) |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 842 | { |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 843 | struct gendisk *disk = bdev->bd_holder_disk; |
| 844 | |
| 845 | bdev->bd_holder_disk = NULL; |
| 846 | if (!disk) |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 847 | return; |
| 848 | |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 849 | del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj); |
| 850 | del_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 851 | } |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 852 | #else |
| 853 | static inline void bd_unlink_disk_holder(struct block_device *bdev) |
| 854 | { } |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 855 | #endif |
| 856 | |
Andrew Patterson | 0c002c2 | 2008-09-04 14:27:20 -0600 | [diff] [blame] | 857 | /** |
Andrew Patterson | 56ade44 | 2008-09-04 14:27:40 -0600 | [diff] [blame] | 858 | * flush_disk - invalidates all buffer-cache entries on a disk |
| 859 | * |
| 860 | * @bdev: struct block device to be flushed |
| 861 | * |
| 862 | * Invalidates all buffer-cache entries on a disk. It should be called |
| 863 | * when a disk has been changed -- either by a media change or online |
| 864 | * resize. |
| 865 | */ |
| 866 | static void flush_disk(struct block_device *bdev) |
| 867 | { |
| 868 | if (__invalidate_device(bdev)) { |
| 869 | char name[BDEVNAME_SIZE] = ""; |
| 870 | |
| 871 | if (bdev->bd_disk) |
| 872 | disk_name(bdev->bd_disk, 0, name); |
| 873 | printk(KERN_WARNING "VFS: busy inodes on changed media or " |
| 874 | "resized disk %s\n", name); |
| 875 | } |
| 876 | |
| 877 | if (!bdev->bd_disk) |
| 878 | return; |
| 879 | if (disk_partitionable(bdev->bd_disk)) |
| 880 | bdev->bd_invalidated = 1; |
| 881 | } |
| 882 | |
| 883 | /** |
Randy Dunlap | 57d1b53 | 2008-10-09 10:42:38 +0200 | [diff] [blame] | 884 | * 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] | 885 | * @disk: struct gendisk to check |
| 886 | * @bdev: struct bdev to adjust. |
| 887 | * |
| 888 | * This routine checks to see if the bdev size does not match the disk size |
| 889 | * and adjusts it if it differs. |
| 890 | */ |
| 891 | void check_disk_size_change(struct gendisk *disk, struct block_device *bdev) |
| 892 | { |
| 893 | loff_t disk_size, bdev_size; |
| 894 | |
| 895 | disk_size = (loff_t)get_capacity(disk) << 9; |
| 896 | bdev_size = i_size_read(bdev->bd_inode); |
| 897 | if (disk_size != bdev_size) { |
| 898 | char name[BDEVNAME_SIZE]; |
| 899 | |
| 900 | disk_name(disk, 0, name); |
| 901 | printk(KERN_INFO |
| 902 | "%s: detected capacity change from %lld to %lld\n", |
| 903 | name, bdev_size, disk_size); |
| 904 | i_size_write(bdev->bd_inode, disk_size); |
Andrew Patterson | 608aeef | 2008-09-04 14:27:45 -0600 | [diff] [blame] | 905 | flush_disk(bdev); |
Andrew Patterson | c3279d1 | 2008-09-04 14:27:25 -0600 | [diff] [blame] | 906 | } |
| 907 | } |
| 908 | EXPORT_SYMBOL(check_disk_size_change); |
| 909 | |
| 910 | /** |
Randy Dunlap | 57d1b53 | 2008-10-09 10:42:38 +0200 | [diff] [blame] | 911 | * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back |
Andrew Patterson | 0c002c2 | 2008-09-04 14:27:20 -0600 | [diff] [blame] | 912 | * @disk: struct gendisk to be revalidated |
| 913 | * |
| 914 | * This routine is a wrapper for lower-level driver's revalidate_disk |
| 915 | * call-backs. It is used to do common pre and post operations needed |
| 916 | * for all revalidate_disk operations. |
| 917 | */ |
| 918 | int revalidate_disk(struct gendisk *disk) |
| 919 | { |
Andrew Patterson | c3279d1 | 2008-09-04 14:27:25 -0600 | [diff] [blame] | 920 | struct block_device *bdev; |
Andrew Patterson | 0c002c2 | 2008-09-04 14:27:20 -0600 | [diff] [blame] | 921 | int ret = 0; |
| 922 | |
| 923 | if (disk->fops->revalidate_disk) |
| 924 | ret = disk->fops->revalidate_disk(disk); |
| 925 | |
Andrew Patterson | c3279d1 | 2008-09-04 14:27:25 -0600 | [diff] [blame] | 926 | bdev = bdget_disk(disk, 0); |
| 927 | if (!bdev) |
| 928 | return ret; |
| 929 | |
| 930 | mutex_lock(&bdev->bd_mutex); |
| 931 | check_disk_size_change(disk, bdev); |
| 932 | mutex_unlock(&bdev->bd_mutex); |
| 933 | bdput(bdev); |
Andrew Patterson | 0c002c2 | 2008-09-04 14:27:20 -0600 | [diff] [blame] | 934 | return ret; |
| 935 | } |
| 936 | EXPORT_SYMBOL(revalidate_disk); |
| 937 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | /* |
| 939 | * This routine checks whether a removable media has been changed, |
| 940 | * and invalidates all buffer-cache-entries in that case. This |
| 941 | * is a relatively slow routine, so we have to try to minimize using |
| 942 | * it. Thus it is called only upon a 'mount' or 'open'. This |
| 943 | * is the best way of combining speed and utility, I think. |
| 944 | * People changing diskettes in the middle of an operation deserve |
| 945 | * to lose :-) |
| 946 | */ |
| 947 | int check_disk_change(struct block_device *bdev) |
| 948 | { |
| 949 | struct gendisk *disk = bdev->bd_disk; |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 950 | const struct block_device_operations *bdops = disk->fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | |
| 952 | if (!bdops->media_changed) |
| 953 | return 0; |
| 954 | if (!bdops->media_changed(bdev->bd_disk)) |
| 955 | return 0; |
| 956 | |
Andrew Patterson | 56ade44 | 2008-09-04 14:27:40 -0600 | [diff] [blame] | 957 | flush_disk(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | if (bdops->revalidate_disk) |
| 959 | bdops->revalidate_disk(bdev->bd_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | return 1; |
| 961 | } |
| 962 | |
| 963 | EXPORT_SYMBOL(check_disk_change); |
| 964 | |
| 965 | void bd_set_size(struct block_device *bdev, loff_t size) |
| 966 | { |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 967 | unsigned bsize = bdev_logical_block_size(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | |
| 969 | bdev->bd_inode->i_size = size; |
| 970 | while (bsize < PAGE_CACHE_SIZE) { |
| 971 | if (size & bsize) |
| 972 | break; |
| 973 | bsize <<= 1; |
| 974 | } |
| 975 | bdev->bd_block_size = bsize; |
| 976 | bdev->bd_inode->i_blkbits = blksize_bits(bsize); |
| 977 | } |
| 978 | EXPORT_SYMBOL(bd_set_size); |
| 979 | |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 980 | 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] | 981 | |
Peter Zijlstra | 6d740cd | 2007-02-20 13:58:18 -0800 | [diff] [blame] | 982 | /* |
| 983 | * bd_mutex locking: |
| 984 | * |
| 985 | * mutex_lock(part->bd_mutex) |
| 986 | * mutex_lock_nested(whole->bd_mutex, 1) |
| 987 | */ |
| 988 | |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 989 | 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] | 990 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | struct gendisk *disk; |
Pavel Emelyanov | 7db9cfd | 2008-06-05 22:46:27 -0700 | [diff] [blame] | 992 | int ret; |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 993 | int partno; |
Al Viro | fe6e9c1 | 2008-06-23 08:30:55 -0400 | [diff] [blame] | 994 | int perm = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 996 | if (mode & FMODE_READ) |
Al Viro | fe6e9c1 | 2008-06-23 08:30:55 -0400 | [diff] [blame] | 997 | perm |= MAY_READ; |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 998 | if (mode & FMODE_WRITE) |
Al Viro | fe6e9c1 | 2008-06-23 08:30:55 -0400 | [diff] [blame] | 999 | perm |= MAY_WRITE; |
| 1000 | /* |
| 1001 | * hooks: /n/, see "layering violations". |
| 1002 | */ |
Chris Wright | b7300b7 | 2010-08-10 18:02:55 -0700 | [diff] [blame] | 1003 | if (!for_part) { |
| 1004 | ret = devcgroup_inode_permission(bdev->bd_inode, perm); |
| 1005 | if (ret != 0) { |
| 1006 | bdput(bdev); |
| 1007 | return ret; |
| 1008 | } |
Al Viro | 8266602 | 2008-08-01 05:32:04 -0400 | [diff] [blame] | 1009 | } |
Pavel Emelyanov | 7db9cfd | 2008-06-05 22:46:27 -0700 | [diff] [blame] | 1010 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 1011 | restart: |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1012 | |
Tejun Heo | 89f9749 | 2008-11-05 10:21:06 +0100 | [diff] [blame] | 1013 | ret = -ENXIO; |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 1014 | disk = get_gendisk(bdev->bd_dev, &partno); |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1015 | if (!disk) |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 1016 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | |
NeilBrown | 6796bf5 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1018 | mutex_lock_nested(&bdev->bd_mutex, for_part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | if (!bdev->bd_openers) { |
| 1020 | bdev->bd_disk = disk; |
| 1021 | bdev->bd_contains = bdev; |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 1022 | if (!partno) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | struct backing_dev_info *bdi; |
Tejun Heo | 89f9749 | 2008-11-05 10:21:06 +0100 | [diff] [blame] | 1024 | |
| 1025 | ret = -ENXIO; |
| 1026 | bdev->bd_part = disk_get_part(disk, partno); |
| 1027 | if (!bdev->bd_part) |
| 1028 | goto out_clear; |
| 1029 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | if (disk->fops->open) { |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1031 | ret = disk->fops->open(bdev, mode); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 1032 | if (ret == -ERESTARTSYS) { |
| 1033 | /* Lost a race with 'disk' being |
| 1034 | * deleted, try again. |
| 1035 | * See md.c |
| 1036 | */ |
| 1037 | disk_put_part(bdev->bd_part); |
| 1038 | bdev->bd_part = NULL; |
| 1039 | module_put(disk->fops->owner); |
| 1040 | put_disk(disk); |
| 1041 | bdev->bd_disk = NULL; |
| 1042 | mutex_unlock(&bdev->bd_mutex); |
| 1043 | goto restart; |
| 1044 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | if (ret) |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1046 | goto out_clear; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | } |
| 1048 | if (!bdev->bd_openers) { |
| 1049 | bd_set_size(bdev,(loff_t)get_capacity(disk)<<9); |
| 1050 | bdi = blk_get_backing_dev_info(bdev); |
| 1051 | if (bdi == NULL) |
| 1052 | bdi = &default_backing_dev_info; |
Dave Chinner | a5491e0 | 2010-10-21 11:49:26 +1100 | [diff] [blame] | 1053 | bdev_inode_switch_bdi(bdev->bd_inode, bdi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | } |
| 1055 | if (bdev->bd_invalidated) |
| 1056 | rescan_partitions(disk, bdev); |
| 1057 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | struct block_device *whole; |
| 1059 | whole = bdget_disk(disk, 0); |
| 1060 | ret = -ENOMEM; |
| 1061 | if (!whole) |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1062 | goto out_clear; |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1063 | BUG_ON(for_part); |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1064 | ret = __blkdev_get(whole, mode, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | if (ret) |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1066 | goto out_clear; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | bdev->bd_contains = whole; |
Dave Chinner | a5491e0 | 2010-10-21 11:49:26 +1100 | [diff] [blame] | 1068 | bdev_inode_switch_bdi(bdev->bd_inode, |
| 1069 | whole->bd_inode->i_data.backing_dev_info); |
Tejun Heo | 89f9749 | 2008-11-05 10:21:06 +0100 | [diff] [blame] | 1070 | bdev->bd_part = disk_get_part(disk, partno); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 1071 | if (!(disk->flags & GENHD_FL_UP) || |
Tejun Heo | 89f9749 | 2008-11-05 10:21:06 +0100 | [diff] [blame] | 1072 | !bdev->bd_part || !bdev->bd_part->nr_sects) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | ret = -ENXIO; |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1074 | goto out_clear; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | } |
Tejun Heo | 89f9749 | 2008-11-05 10:21:06 +0100 | [diff] [blame] | 1076 | bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | } |
| 1078 | } else { |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1079 | module_put(disk->fops->owner); |
Neil Brown | 960cc0f | 2009-10-26 08:59:17 +0100 | [diff] [blame] | 1080 | put_disk(disk); |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1081 | disk = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | if (bdev->bd_contains == bdev) { |
| 1083 | if (bdev->bd_disk->fops->open) { |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1084 | ret = bdev->bd_disk->fops->open(bdev, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | if (ret) |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1086 | goto out_unlock_bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | } |
| 1088 | if (bdev->bd_invalidated) |
| 1089 | rescan_partitions(bdev->bd_disk, bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | } |
| 1091 | } |
| 1092 | bdev->bd_openers++; |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1093 | if (for_part) |
| 1094 | bdev->bd_part_count++; |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 1095 | mutex_unlock(&bdev->bd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | return 0; |
| 1097 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1098 | out_clear: |
Tejun Heo | 89f9749 | 2008-11-05 10:21:06 +0100 | [diff] [blame] | 1099 | disk_put_part(bdev->bd_part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | bdev->bd_disk = NULL; |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1101 | bdev->bd_part = NULL; |
Dave Chinner | a5491e0 | 2010-10-21 11:49:26 +1100 | [diff] [blame] | 1102 | bdev_inode_switch_bdi(bdev->bd_inode, &default_backing_dev_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | if (bdev != bdev->bd_contains) |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1104 | __blkdev_put(bdev->bd_contains, mode, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | bdev->bd_contains = NULL; |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1106 | out_unlock_bdev: |
Arjan van de Ven | c039e31 | 2006-03-23 03:00:28 -0800 | [diff] [blame] | 1107 | mutex_unlock(&bdev->bd_mutex); |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 1108 | out: |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1109 | if (disk) |
| 1110 | module_put(disk->fops->owner); |
| 1111 | put_disk(disk); |
| 1112 | bdput(bdev); |
| 1113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | return ret; |
| 1115 | } |
| 1116 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame^] | 1117 | /** |
| 1118 | * blkdev_get - open a block device |
| 1119 | * @bdev: block_device to open |
| 1120 | * @mode: FMODE_* mask |
| 1121 | * @holder: exclusive holder identifier |
| 1122 | * |
| 1123 | * Open @bdev with @mode. If @mode includes %FMODE_EXCL, @bdev is |
| 1124 | * open with exclusive access. Specifying %FMODE_EXCL with %NULL |
| 1125 | * @holder is invalid. Exclusive opens may nest for the same @holder. |
| 1126 | * |
| 1127 | * On success, the reference count of @bdev is unchanged. On failure, |
| 1128 | * @bdev is put. |
| 1129 | * |
| 1130 | * CONTEXT: |
| 1131 | * Might sleep. |
| 1132 | * |
| 1133 | * RETURNS: |
| 1134 | * 0 on success, -errno on failure. |
| 1135 | */ |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1136 | int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | { |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1138 | struct block_device *whole = NULL; |
| 1139 | int res; |
| 1140 | |
| 1141 | WARN_ON_ONCE((mode & FMODE_EXCL) && !holder); |
| 1142 | |
| 1143 | if ((mode & FMODE_EXCL) && holder) { |
| 1144 | whole = bd_start_claiming(bdev, holder); |
| 1145 | if (IS_ERR(whole)) { |
| 1146 | bdput(bdev); |
| 1147 | return PTR_ERR(whole); |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | res = __blkdev_get(bdev, mode, 0); |
| 1152 | |
Tejun Heo | 75f1dc0 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1153 | /* __blkdev_get() may alter read only status, check it afterwards */ |
| 1154 | if (!res && (mode & FMODE_WRITE) && bdev_read_only(bdev)) { |
| 1155 | __blkdev_put(bdev, mode, 0); |
| 1156 | res = -EACCES; |
| 1157 | } |
| 1158 | |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1159 | if (whole) { |
Tejun Heo | 6a027ef | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1160 | /* finish claiming */ |
| 1161 | spin_lock(&bdev_lock); |
| 1162 | |
| 1163 | if (res == 0) { |
| 1164 | BUG_ON(!bd_may_claim(bdev, whole, holder)); |
| 1165 | /* |
| 1166 | * Note that for a whole device bd_holders |
| 1167 | * will be incremented twice, and bd_holder |
| 1168 | * will be set to bd_may_claim before being |
| 1169 | * set to holder |
| 1170 | */ |
| 1171 | whole->bd_holders++; |
| 1172 | whole->bd_holder = bd_may_claim; |
| 1173 | bdev->bd_holders++; |
| 1174 | bdev->bd_holder = holder; |
| 1175 | } |
| 1176 | |
| 1177 | /* tell others that we're done */ |
| 1178 | BUG_ON(whole->bd_claiming != holder); |
| 1179 | whole->bd_claiming = NULL; |
| 1180 | wake_up_bit(&whole->bd_claiming, 0); |
| 1181 | |
| 1182 | spin_unlock(&bdev_lock); |
| 1183 | bdput(whole); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | return res; |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1187 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | EXPORT_SYMBOL(blkdev_get); |
| 1189 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame^] | 1190 | /** |
| 1191 | * blkdev_get_by_path - open a block device by name |
| 1192 | * @path: path to the block device to open |
| 1193 | * @mode: FMODE_* mask |
| 1194 | * @holder: exclusive holder identifier |
| 1195 | * |
| 1196 | * Open the blockdevice described by the device file at @path. @mode |
| 1197 | * and @holder are identical to blkdev_get(). |
| 1198 | * |
| 1199 | * On success, the returned block_device has reference count of one. |
| 1200 | * |
| 1201 | * CONTEXT: |
| 1202 | * Might sleep. |
| 1203 | * |
| 1204 | * RETURNS: |
| 1205 | * Pointer to block_device on success, ERR_PTR(-errno) on failure. |
| 1206 | */ |
| 1207 | struct block_device *blkdev_get_by_path(const char *path, fmode_t mode, |
| 1208 | void *holder) |
| 1209 | { |
| 1210 | struct block_device *bdev; |
| 1211 | int err; |
| 1212 | |
| 1213 | bdev = lookup_bdev(path); |
| 1214 | if (IS_ERR(bdev)) |
| 1215 | return bdev; |
| 1216 | |
| 1217 | err = blkdev_get(bdev, mode, holder); |
| 1218 | if (err) |
| 1219 | return ERR_PTR(err); |
| 1220 | |
| 1221 | return bdev; |
| 1222 | } |
| 1223 | EXPORT_SYMBOL(blkdev_get_by_path); |
| 1224 | |
| 1225 | /** |
| 1226 | * blkdev_get_by_dev - open a block device by device number |
| 1227 | * @dev: device number of block device to open |
| 1228 | * @mode: FMODE_* mask |
| 1229 | * @holder: exclusive holder identifier |
| 1230 | * |
| 1231 | * Open the blockdevice described by device number @dev. @mode and |
| 1232 | * @holder are identical to blkdev_get(). |
| 1233 | * |
| 1234 | * Use it ONLY if you really do not have anything better - i.e. when |
| 1235 | * you are behind a truly sucky interface and all you are given is a |
| 1236 | * device number. _Never_ to be used for internal purposes. If you |
| 1237 | * ever need it - reconsider your API. |
| 1238 | * |
| 1239 | * On success, the returned block_device has reference count of one. |
| 1240 | * |
| 1241 | * CONTEXT: |
| 1242 | * Might sleep. |
| 1243 | * |
| 1244 | * RETURNS: |
| 1245 | * Pointer to block_device on success, ERR_PTR(-errno) on failure. |
| 1246 | */ |
| 1247 | struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder) |
| 1248 | { |
| 1249 | struct block_device *bdev; |
| 1250 | int err; |
| 1251 | |
| 1252 | bdev = bdget(dev); |
| 1253 | if (!bdev) |
| 1254 | return ERR_PTR(-ENOMEM); |
| 1255 | |
| 1256 | err = blkdev_get(bdev, mode, holder); |
| 1257 | if (err) |
| 1258 | return ERR_PTR(err); |
| 1259 | |
| 1260 | return bdev; |
| 1261 | } |
| 1262 | EXPORT_SYMBOL(blkdev_get_by_dev); |
| 1263 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | static int blkdev_open(struct inode * inode, struct file * filp) |
| 1265 | { |
| 1266 | struct block_device *bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | |
| 1268 | /* |
| 1269 | * Preserve backwards compatibility and allow large file access |
| 1270 | * even if userspace doesn't ask for it explicitly. Some mkfs |
| 1271 | * binary needs it. We might want to drop this workaround |
| 1272 | * during an unstable branch. |
| 1273 | */ |
| 1274 | filp->f_flags |= O_LARGEFILE; |
| 1275 | |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1276 | if (filp->f_flags & O_NDELAY) |
| 1277 | filp->f_mode |= FMODE_NDELAY; |
| 1278 | if (filp->f_flags & O_EXCL) |
| 1279 | filp->f_mode |= FMODE_EXCL; |
| 1280 | if ((filp->f_flags & O_ACCMODE) == 3) |
| 1281 | filp->f_mode |= FMODE_WRITE_IOCTL; |
| 1282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | bdev = bd_acquire(inode); |
Pavel Emelianov | 6a2aae0 | 2006-10-28 10:38:33 -0700 | [diff] [blame] | 1284 | if (bdev == NULL) |
| 1285 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1287 | filp->f_mapping = bdev->bd_inode->i_mapping; |
| 1288 | |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1289 | return blkdev_get(bdev, filp->f_mode, filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | } |
| 1291 | |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1292 | 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] | 1293 | { |
| 1294 | int ret = 0; |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1295 | struct gendisk *disk = bdev->bd_disk; |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1296 | struct block_device *victim = NULL; |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1297 | |
NeilBrown | 6796bf5 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1298 | mutex_lock_nested(&bdev->bd_mutex, for_part); |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1299 | if (for_part) |
| 1300 | bdev->bd_part_count--; |
| 1301 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1302 | if (!--bdev->bd_openers) { |
Tejun Heo | 6a027ef | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1303 | WARN_ON_ONCE(bdev->bd_holders); |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1304 | sync_blockdev(bdev); |
| 1305 | kill_bdev(bdev); |
| 1306 | } |
| 1307 | if (bdev->bd_contains == bdev) { |
| 1308 | if (disk->fops->release) |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1309 | ret = disk->fops->release(disk, mode); |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1310 | } |
| 1311 | if (!bdev->bd_openers) { |
| 1312 | struct module *owner = disk->fops->owner; |
| 1313 | |
| 1314 | put_disk(disk); |
| 1315 | module_put(owner); |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1316 | disk_put_part(bdev->bd_part); |
| 1317 | bdev->bd_part = NULL; |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1318 | bdev->bd_disk = NULL; |
Dave Chinner | a5491e0 | 2010-10-21 11:49:26 +1100 | [diff] [blame] | 1319 | bdev_inode_switch_bdi(bdev->bd_inode, |
| 1320 | &default_backing_dev_info); |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1321 | if (bdev != bdev->bd_contains) |
| 1322 | victim = bdev->bd_contains; |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1323 | bdev->bd_contains = NULL; |
| 1324 | } |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1325 | mutex_unlock(&bdev->bd_mutex); |
| 1326 | bdput(bdev); |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1327 | if (victim) |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1328 | __blkdev_put(victim, mode, 1); |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1329 | return ret; |
| 1330 | } |
| 1331 | |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1332 | int blkdev_put(struct block_device *bdev, fmode_t mode) |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1333 | { |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1334 | if (mode & FMODE_EXCL) { |
Tejun Heo | 6a027ef | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1335 | bool bdev_free; |
| 1336 | |
| 1337 | /* |
| 1338 | * Release a claim on the device. The holder fields |
| 1339 | * are protected with bdev_lock. bd_mutex is to |
| 1340 | * synchronize disk_holder unlinking. |
| 1341 | */ |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1342 | mutex_lock(&bdev->bd_mutex); |
Tejun Heo | 6a027ef | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1343 | spin_lock(&bdev_lock); |
| 1344 | |
| 1345 | WARN_ON_ONCE(--bdev->bd_holders < 0); |
| 1346 | WARN_ON_ONCE(--bdev->bd_contains->bd_holders < 0); |
| 1347 | |
| 1348 | /* bd_contains might point to self, check in a separate step */ |
| 1349 | if ((bdev_free = !bdev->bd_holders)) |
| 1350 | bdev->bd_holder = NULL; |
| 1351 | if (!bdev->bd_contains->bd_holders) |
| 1352 | bdev->bd_contains->bd_holder = NULL; |
| 1353 | |
| 1354 | spin_unlock(&bdev_lock); |
| 1355 | |
| 1356 | /* if this was the last claim, holder link should go too */ |
| 1357 | if (bdev_free) |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1358 | bd_unlink_disk_holder(bdev); |
Tejun Heo | 6a027ef | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1359 | |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1360 | mutex_unlock(&bdev->bd_mutex); |
| 1361 | } |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1362 | return __blkdev_put(bdev, mode, 0); |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1363 | } |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1364 | EXPORT_SYMBOL(blkdev_put); |
| 1365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | static int blkdev_close(struct inode * inode, struct file * filp) |
| 1367 | { |
| 1368 | struct block_device *bdev = I_BDEV(filp->f_mapping->host); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1369 | |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1370 | return blkdev_put(bdev, filp->f_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | } |
| 1372 | |
Arnd Bergmann | bb93e3a | 2005-06-23 00:10:15 -0700 | [diff] [blame] | 1373 | static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | { |
Al Viro | 56b26ad | 2008-09-19 03:17:36 -0400 | [diff] [blame] | 1375 | struct block_device *bdev = I_BDEV(file->f_mapping->host); |
| 1376 | fmode_t mode = file->f_mode; |
Christoph Hellwig | fd4ce1a | 2008-11-05 14:58:42 +0100 | [diff] [blame] | 1377 | |
| 1378 | /* |
| 1379 | * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have |
| 1380 | * to updated it before every ioctl. |
| 1381 | */ |
Al Viro | 56b26ad | 2008-09-19 03:17:36 -0400 | [diff] [blame] | 1382 | if (file->f_flags & O_NDELAY) |
Christoph Hellwig | fd4ce1a | 2008-11-05 14:58:42 +0100 | [diff] [blame] | 1383 | mode |= FMODE_NDELAY; |
| 1384 | else |
| 1385 | mode &= ~FMODE_NDELAY; |
| 1386 | |
Al Viro | 56b26ad | 2008-09-19 03:17:36 -0400 | [diff] [blame] | 1387 | return blkdev_ioctl(bdev, mode, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | } |
| 1389 | |
Theodore Ts'o | 87d8fe1 | 2009-01-03 09:47:09 -0500 | [diff] [blame] | 1390 | /* |
Christoph Hellwig | eef9938 | 2009-08-20 17:43:41 +0200 | [diff] [blame] | 1391 | * Write data to the block device. Only intended for the block device itself |
| 1392 | * and the raw driver which basically is a fake block device. |
| 1393 | * |
| 1394 | * Does not take i_mutex for the write and thus is not for general purpose |
| 1395 | * use. |
| 1396 | */ |
| 1397 | ssize_t blkdev_aio_write(struct kiocb *iocb, const struct iovec *iov, |
| 1398 | unsigned long nr_segs, loff_t pos) |
| 1399 | { |
| 1400 | struct file *file = iocb->ki_filp; |
| 1401 | ssize_t ret; |
| 1402 | |
| 1403 | BUG_ON(iocb->ki_pos != pos); |
| 1404 | |
| 1405 | ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos); |
| 1406 | if (ret > 0 || ret == -EIOCBQUEUED) { |
| 1407 | ssize_t err; |
| 1408 | |
| 1409 | err = generic_write_sync(file, pos, ret); |
| 1410 | if (err < 0 && ret > 0) |
| 1411 | ret = err; |
| 1412 | } |
| 1413 | return ret; |
| 1414 | } |
| 1415 | EXPORT_SYMBOL_GPL(blkdev_aio_write); |
| 1416 | |
| 1417 | /* |
Theodore Ts'o | 87d8fe1 | 2009-01-03 09:47:09 -0500 | [diff] [blame] | 1418 | * Try to release a page associated with block device when the system |
| 1419 | * is under memory pressure. |
| 1420 | */ |
| 1421 | static int blkdev_releasepage(struct page *page, gfp_t wait) |
| 1422 | { |
| 1423 | struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super; |
| 1424 | |
| 1425 | if (super && super->s_op->bdev_try_to_free_page) |
| 1426 | return super->s_op->bdev_try_to_free_page(super, page, wait); |
| 1427 | |
| 1428 | return try_to_free_buffers(page); |
| 1429 | } |
| 1430 | |
Adrian Bunk | 4c54ac6 | 2008-02-18 13:48:31 +0100 | [diff] [blame] | 1431 | static const struct address_space_operations def_blk_aops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | .readpage = blkdev_readpage, |
| 1433 | .writepage = blkdev_writepage, |
| 1434 | .sync_page = block_sync_page, |
Nick Piggin | 6272b5a | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 1435 | .write_begin = blkdev_write_begin, |
| 1436 | .write_end = blkdev_write_end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | .writepages = generic_writepages, |
Theodore Ts'o | 87d8fe1 | 2009-01-03 09:47:09 -0500 | [diff] [blame] | 1438 | .releasepage = blkdev_releasepage, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | .direct_IO = blkdev_direct_IO, |
| 1440 | }; |
| 1441 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 1442 | const struct file_operations def_blk_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | .open = blkdev_open, |
| 1444 | .release = blkdev_close, |
| 1445 | .llseek = block_llseek, |
Badari Pulavarty | 543ade1 | 2006-09-30 23:28:48 -0700 | [diff] [blame] | 1446 | .read = do_sync_read, |
| 1447 | .write = do_sync_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | .aio_read = generic_file_aio_read, |
Christoph Hellwig | eef9938 | 2009-08-20 17:43:41 +0200 | [diff] [blame] | 1449 | .aio_write = blkdev_aio_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | .mmap = generic_file_mmap, |
Andrew Morton | b1dd3b2 | 2010-04-06 14:35:00 -0700 | [diff] [blame] | 1451 | .fsync = blkdev_fsync, |
Arnd Bergmann | bb93e3a | 2005-06-23 00:10:15 -0700 | [diff] [blame] | 1452 | .unlocked_ioctl = block_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | #ifdef CONFIG_COMPAT |
| 1454 | .compat_ioctl = compat_blkdev_ioctl, |
| 1455 | #endif |
Jens Axboe | 7f9c51f | 2006-05-01 19:59:32 +0200 | [diff] [blame] | 1456 | .splice_read = generic_file_splice_read, |
| 1457 | .splice_write = generic_file_splice_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | }; |
| 1459 | |
| 1460 | int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg) |
| 1461 | { |
| 1462 | int res; |
| 1463 | mm_segment_t old_fs = get_fs(); |
| 1464 | set_fs(KERNEL_DS); |
Al Viro | 56b26ad | 2008-09-19 03:17:36 -0400 | [diff] [blame] | 1465 | res = blkdev_ioctl(bdev, 0, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | set_fs(old_fs); |
| 1467 | return res; |
| 1468 | } |
| 1469 | |
| 1470 | EXPORT_SYMBOL(ioctl_by_bdev); |
| 1471 | |
| 1472 | /** |
| 1473 | * lookup_bdev - lookup a struct block_device by name |
Randy Dunlap | 94e2959 | 2009-01-06 14:41:15 -0800 | [diff] [blame] | 1474 | * @pathname: special file representing the block device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | * |
Randy Dunlap | 57d1b53 | 2008-10-09 10:42:38 +0200 | [diff] [blame] | 1476 | * Get a reference to the blockdevice at @pathname in the current |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | * namespace if possible and return it. Return ERR_PTR(error) |
| 1478 | * otherwise. |
| 1479 | */ |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1480 | struct block_device *lookup_bdev(const char *pathname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | { |
| 1482 | struct block_device *bdev; |
| 1483 | struct inode *inode; |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1484 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | int error; |
| 1486 | |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1487 | if (!pathname || !*pathname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | return ERR_PTR(-EINVAL); |
| 1489 | |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1490 | error = kern_path(pathname, LOOKUP_FOLLOW, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | if (error) |
| 1492 | return ERR_PTR(error); |
| 1493 | |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1494 | inode = path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | error = -ENOTBLK; |
| 1496 | if (!S_ISBLK(inode->i_mode)) |
| 1497 | goto fail; |
| 1498 | error = -EACCES; |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1499 | if (path.mnt->mnt_flags & MNT_NODEV) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | goto fail; |
| 1501 | error = -ENOMEM; |
| 1502 | bdev = bd_acquire(inode); |
| 1503 | if (!bdev) |
| 1504 | goto fail; |
| 1505 | out: |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1506 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | return bdev; |
| 1508 | fail: |
| 1509 | bdev = ERR_PTR(error); |
| 1510 | goto out; |
| 1511 | } |
Al Viro | d5686b4 | 2008-08-01 05:00:11 -0400 | [diff] [blame] | 1512 | EXPORT_SYMBOL(lookup_bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | |
David Howells | b71e8a4 | 2006-08-29 19:06:11 +0100 | [diff] [blame] | 1514 | int __invalidate_device(struct block_device *bdev) |
| 1515 | { |
| 1516 | struct super_block *sb = get_super(bdev); |
| 1517 | int res = 0; |
| 1518 | |
| 1519 | if (sb) { |
| 1520 | /* |
| 1521 | * no need to lock the super, get_super holds the |
| 1522 | * read mutex so the filesystem cannot go away |
| 1523 | * under us (->put_super runs with the write lock |
| 1524 | * hold). |
| 1525 | */ |
| 1526 | shrink_dcache_sb(sb); |
| 1527 | res = invalidate_inodes(sb); |
| 1528 | drop_super(sb); |
| 1529 | } |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 1530 | invalidate_bdev(bdev); |
David Howells | b71e8a4 | 2006-08-29 19:06:11 +0100 | [diff] [blame] | 1531 | return res; |
| 1532 | } |
| 1533 | EXPORT_SYMBOL(__invalidate_device); |