Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 4 | * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE |
Christoph Hellwig | 7b51e70 | 2020-12-10 08:55:44 +0100 | [diff] [blame] | 5 | * Copyright (C) 2016 - 2020 Christoph Hellwig |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/init.h> |
| 9 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/slab.h> |
| 11 | #include <linux/kmod.h> |
| 12 | #include <linux/major.h> |
Pavel Emelyanov | 7db9cfd | 2008-06-05 22:46:27 -0700 | [diff] [blame] | 13 | #include <linux/device_cgroup.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/blkdev.h> |
Christoph Hellwig | fe45e63 | 2021-09-20 14:33:27 +0200 | [diff] [blame] | 15 | #include <linux/blk-integrity.h> |
Tejun Heo | 66114ca | 2015-05-22 17:13:32 -0400 | [diff] [blame] | 16 | #include <linux/backing-dev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/module.h> |
| 18 | #include <linux/blkpg.h> |
Muthu Kumar | b502bd1 | 2012-03-23 15:01:50 -0700 | [diff] [blame] | 19 | #include <linux/magic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/buffer_head.h> |
Al Viro | ff01bb4 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 21 | #include <linux/swap.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/mount.h> |
David Howells | 9030d16 | 2019-03-25 16:38:23 +0000 | [diff] [blame] | 24 | #include <linux/pseudo_fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/uio.h> |
| 26 | #include <linux/namei.h> |
Christoph Hellwig | 15e3d2c | 2020-11-24 09:34:00 +0100 | [diff] [blame] | 27 | #include <linux/part_stat.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 28 | #include <linux/uaccess.h> |
Christoph Hellwig | 0dca446 | 2021-09-07 16:13:03 +0200 | [diff] [blame] | 29 | #include "../fs/internal.h" |
| 30 | #include "blk.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 | |
| 37 | static inline struct bdev_inode *BDEV_I(struct inode *inode) |
| 38 | { |
| 39 | return container_of(inode, struct bdev_inode, vfs_inode); |
| 40 | } |
| 41 | |
Geert Uytterhoeven | ff5053f | 2015-06-26 13:58:32 +0200 | [diff] [blame] | 42 | struct block_device *I_BDEV(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | { |
| 44 | return &BDEV_I(inode)->bdev; |
| 45 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | EXPORT_SYMBOL(I_BDEV); |
| 47 | |
Vivek Goyal | dbd3ca5 | 2015-11-09 09:23:40 -0700 | [diff] [blame] | 48 | static void bdev_write_inode(struct block_device *bdev) |
Christoph Hellwig | 564f00f | 2015-01-14 10:42:33 +0100 | [diff] [blame] | 49 | { |
Vivek Goyal | dbd3ca5 | 2015-11-09 09:23:40 -0700 | [diff] [blame] | 50 | struct inode *inode = bdev->bd_inode; |
| 51 | int ret; |
| 52 | |
Christoph Hellwig | 564f00f | 2015-01-14 10:42:33 +0100 | [diff] [blame] | 53 | spin_lock(&inode->i_lock); |
| 54 | while (inode->i_state & I_DIRTY) { |
| 55 | spin_unlock(&inode->i_lock); |
Vivek Goyal | dbd3ca5 | 2015-11-09 09:23:40 -0700 | [diff] [blame] | 56 | ret = write_inode_now(inode, true); |
| 57 | if (ret) { |
| 58 | char name[BDEVNAME_SIZE]; |
| 59 | pr_warn_ratelimited("VFS: Dirty inode writeback failed " |
| 60 | "for block device %s (err=%d).\n", |
| 61 | bdevname(bdev, name), ret); |
| 62 | } |
Christoph Hellwig | 564f00f | 2015-01-14 10:42:33 +0100 | [diff] [blame] | 63 | spin_lock(&inode->i_lock); |
| 64 | } |
| 65 | spin_unlock(&inode->i_lock); |
| 66 | } |
| 67 | |
Peter Zijlstra | f9a1439 | 2007-05-06 14:49:55 -0700 | [diff] [blame] | 68 | /* Kill _all_ buffers and pagecache , dirty or not.. */ |
Zheng Bin | 3373a34 | 2020-06-18 12:21:38 +0800 | [diff] [blame] | 69 | static void kill_bdev(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | { |
Al Viro | ff01bb4 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 71 | struct address_space *mapping = bdev->bd_inode->i_mapping; |
| 72 | |
Matthew Wilcox (Oracle) | 7716506 | 2021-05-04 18:32:45 -0700 | [diff] [blame] | 73 | if (mapping_empty(mapping)) |
Peter Zijlstra | f9a1439 | 2007-05-06 14:49:55 -0700 | [diff] [blame] | 74 | return; |
Al Viro | ff01bb4 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 75 | |
Peter Zijlstra | f9a1439 | 2007-05-06 14:49:55 -0700 | [diff] [blame] | 76 | invalidate_bh_lrus(); |
Al Viro | ff01bb4 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 77 | truncate_inode_pages(mapping, 0); |
Zheng Bin | 3373a34 | 2020-06-18 12:21:38 +0800 | [diff] [blame] | 78 | } |
Al Viro | ff01bb4 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 79 | |
| 80 | /* Invalidate clean unused buffers and pagecache. */ |
| 81 | void invalidate_bdev(struct block_device *bdev) |
| 82 | { |
| 83 | struct address_space *mapping = bdev->bd_inode->i_mapping; |
| 84 | |
Andrey Ryabinin | a5f6a6a | 2017-05-03 14:56:02 -0700 | [diff] [blame] | 85 | if (mapping->nrpages) { |
| 86 | invalidate_bh_lrus(); |
| 87 | lru_add_drain_all(); /* make sure all lru add caches are flushed */ |
| 88 | invalidate_mapping_pages(mapping, 0, -1); |
| 89 | } |
Al Viro | ff01bb4 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 90 | } |
| 91 | EXPORT_SYMBOL(invalidate_bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
Jan Kara | 384d87e | 2020-09-04 10:58:52 +0200 | [diff] [blame] | 93 | /* |
| 94 | * Drop all buffers & page cache for given bdev range. This function bails |
| 95 | * with error if bdev has other exclusive owner (such as filesystem). |
| 96 | */ |
| 97 | int truncate_bdev_range(struct block_device *bdev, fmode_t mode, |
| 98 | loff_t lstart, loff_t lend) |
| 99 | { |
Jan Kara | 384d87e | 2020-09-04 10:58:52 +0200 | [diff] [blame] | 100 | /* |
| 101 | * If we don't hold exclusive handle for the device, upgrade to it |
| 102 | * while we discard the buffer cache to avoid discarding buffers |
| 103 | * under live filesystem. |
| 104 | */ |
| 105 | if (!(mode & FMODE_EXCL)) { |
Christoph Hellwig | 37c3fc9 | 2020-11-25 21:20:08 +0100 | [diff] [blame] | 106 | int err = bd_prepare_to_claim(bdev, truncate_bdev_range); |
Jan Kara | 384d87e | 2020-09-04 10:58:52 +0200 | [diff] [blame] | 107 | if (err) |
Jan Kara | 56887cf | 2021-02-22 10:48:09 +0100 | [diff] [blame] | 108 | goto invalidate; |
Jan Kara | 384d87e | 2020-09-04 10:58:52 +0200 | [diff] [blame] | 109 | } |
Christoph Hellwig | 37c3fc9 | 2020-11-25 21:20:08 +0100 | [diff] [blame] | 110 | |
Jan Kara | 384d87e | 2020-09-04 10:58:52 +0200 | [diff] [blame] | 111 | truncate_inode_pages_range(bdev->bd_inode->i_mapping, lstart, lend); |
Christoph Hellwig | 37c3fc9 | 2020-11-25 21:20:08 +0100 | [diff] [blame] | 112 | if (!(mode & FMODE_EXCL)) |
| 113 | bd_abort_claiming(bdev, truncate_bdev_range); |
Jan Kara | 384d87e | 2020-09-04 10:58:52 +0200 | [diff] [blame] | 114 | return 0; |
Jan Kara | 56887cf | 2021-02-22 10:48:09 +0100 | [diff] [blame] | 115 | |
| 116 | invalidate: |
| 117 | /* |
| 118 | * Someone else has handle exclusively open. Try invalidating instead. |
| 119 | * The 'end' argument is inclusive so the rounding is safe. |
| 120 | */ |
| 121 | return invalidate_inode_pages2_range(bdev->bd_inode->i_mapping, |
| 122 | lstart >> PAGE_SHIFT, |
| 123 | lend >> PAGE_SHIFT); |
Jan Kara | 384d87e | 2020-09-04 10:58:52 +0200 | [diff] [blame] | 124 | } |
Jan Kara | 384d87e | 2020-09-04 10:58:52 +0200 | [diff] [blame] | 125 | |
Jan Kara | 04906b2 | 2019-01-14 09:48:10 +0100 | [diff] [blame] | 126 | static void set_init_blocksize(struct block_device *bdev) |
| 127 | { |
Maxim Mikityanskiy | 8dc932d | 2021-01-26 21:59:07 +0200 | [diff] [blame] | 128 | unsigned int bsize = bdev_logical_block_size(bdev); |
| 129 | loff_t size = i_size_read(bdev->bd_inode); |
| 130 | |
| 131 | while (bsize < PAGE_SIZE) { |
| 132 | if (size & bsize) |
| 133 | break; |
| 134 | bsize <<= 1; |
| 135 | } |
| 136 | bdev->bd_inode->i_blkbits = blksize_bits(bsize); |
Jan Kara | 04906b2 | 2019-01-14 09:48:10 +0100 | [diff] [blame] | 137 | } |
| 138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | int set_blocksize(struct block_device *bdev, int size) |
| 140 | { |
| 141 | /* 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] | 142 | if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | return -EINVAL; |
| 144 | |
| 145 | /* Size cannot be smaller than the size supported by the device */ |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 146 | if (size < bdev_logical_block_size(bdev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | return -EINVAL; |
| 148 | |
| 149 | /* Don't change the size if it is same as current */ |
Christoph Hellwig | 6b7b181 | 2020-06-26 10:01:55 +0200 | [diff] [blame] | 150 | if (bdev->bd_inode->i_blkbits != blksize_bits(size)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | sync_blockdev(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | bdev->bd_inode->i_blkbits = blksize_bits(size); |
| 153 | kill_bdev(bdev); |
| 154 | } |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | EXPORT_SYMBOL(set_blocksize); |
| 159 | |
| 160 | int sb_set_blocksize(struct super_block *sb, int size) |
| 161 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | if (set_blocksize(sb->s_bdev, size)) |
| 163 | return 0; |
| 164 | /* If we get here, we know size is power of two |
| 165 | * and it's value is between 512 and PAGE_SIZE */ |
| 166 | sb->s_blocksize = size; |
Coywolf Qi Hunt | 38885bd | 2006-03-24 03:18:05 -0800 | [diff] [blame] | 167 | sb->s_blocksize_bits = blksize_bits(size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | return sb->s_blocksize; |
| 169 | } |
| 170 | |
| 171 | EXPORT_SYMBOL(sb_set_blocksize); |
| 172 | |
| 173 | int sb_min_blocksize(struct super_block *sb, int size) |
| 174 | { |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 175 | int minsize = bdev_logical_block_size(sb->s_bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | if (size < minsize) |
| 177 | size = minsize; |
| 178 | return sb_set_blocksize(sb, size); |
| 179 | } |
| 180 | |
| 181 | EXPORT_SYMBOL(sb_min_blocksize); |
| 182 | |
Christoph Hellwig | 70164eb | 2021-10-19 08:25:25 +0200 | [diff] [blame] | 183 | int sync_blockdev_nowait(struct block_device *bdev) |
Jan Kara | 5cee581 | 2009-04-27 16:43:51 +0200 | [diff] [blame] | 184 | { |
| 185 | if (!bdev) |
| 186 | return 0; |
Christoph Hellwig | 70164eb | 2021-10-19 08:25:25 +0200 | [diff] [blame] | 187 | return filemap_flush(bdev->bd_inode->i_mapping); |
Jan Kara | 5cee581 | 2009-04-27 16:43:51 +0200 | [diff] [blame] | 188 | } |
Christoph Hellwig | 70164eb | 2021-10-19 08:25:25 +0200 | [diff] [blame] | 189 | EXPORT_SYMBOL_GPL(sync_blockdev_nowait); |
Jan Kara | 5cee581 | 2009-04-27 16:43:51 +0200 | [diff] [blame] | 190 | |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 191 | /* |
| 192 | * Write out and wait upon all the dirty data associated with a block |
| 193 | * device via its mapping. Does not take the superblock lock. |
| 194 | */ |
| 195 | int sync_blockdev(struct block_device *bdev) |
| 196 | { |
Christoph Hellwig | 70164eb | 2021-10-19 08:25:25 +0200 | [diff] [blame] | 197 | if (!bdev) |
| 198 | return 0; |
| 199 | return filemap_write_and_wait(bdev->bd_inode->i_mapping); |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 200 | } |
| 201 | EXPORT_SYMBOL(sync_blockdev); |
| 202 | |
| 203 | /* |
| 204 | * Write out and wait upon all dirty data associated with this |
| 205 | * device. Filesystem data as well as the underlying block |
| 206 | * device. Takes the superblock lock. |
| 207 | */ |
| 208 | int fsync_bdev(struct block_device *bdev) |
| 209 | { |
| 210 | struct super_block *sb = get_super(bdev); |
| 211 | if (sb) { |
Jan Kara | 60b0680 | 2009-04-27 16:43:53 +0200 | [diff] [blame] | 212 | int res = sync_filesystem(sb); |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 213 | drop_super(sb); |
| 214 | return res; |
| 215 | } |
| 216 | return sync_blockdev(bdev); |
| 217 | } |
Al Viro | 47e4491 | 2009-04-01 07:07:16 -0400 | [diff] [blame] | 218 | EXPORT_SYMBOL(fsync_bdev); |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 219 | |
| 220 | /** |
| 221 | * freeze_bdev -- lock a filesystem and force it into a consistent state |
| 222 | * @bdev: blockdevice to lock |
| 223 | * |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 224 | * If a superblock is found on this device, we take the s_umount semaphore |
| 225 | * on it to make sure nobody unmounts until the snapshot creation is done. |
| 226 | * The reference counter (bd_fsfreeze_count) guarantees that only the last |
| 227 | * unfreeze process can unfreeze the frozen filesystem actually when multiple |
| 228 | * freeze requests arrive simultaneously. It counts up in freeze_bdev() and |
| 229 | * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze |
| 230 | * actually. |
| 231 | */ |
Christoph Hellwig | 040f04b | 2020-11-24 11:54:06 +0100 | [diff] [blame] | 232 | int freeze_bdev(struct block_device *bdev) |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 233 | { |
| 234 | struct super_block *sb; |
| 235 | int error = 0; |
| 236 | |
| 237 | mutex_lock(&bdev->bd_fsfreeze_mutex); |
Christoph Hellwig | 040f04b | 2020-11-24 11:54:06 +0100 | [diff] [blame] | 238 | if (++bdev->bd_fsfreeze_count > 1) |
| 239 | goto done; |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 240 | |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 241 | sb = get_active_super(bdev); |
| 242 | if (!sb) |
Christoph Hellwig | 040f04b | 2020-11-24 11:54:06 +0100 | [diff] [blame] | 243 | goto sync; |
Benjamin Marzinski | 48b6bca | 2014-11-13 20:42:03 -0600 | [diff] [blame] | 244 | if (sb->s_op->freeze_super) |
| 245 | error = sb->s_op->freeze_super(sb); |
| 246 | else |
| 247 | error = freeze_super(sb); |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 248 | deactivate_super(sb); |
Christoph Hellwig | 040f04b | 2020-11-24 11:54:06 +0100 | [diff] [blame] | 249 | |
| 250 | if (error) { |
| 251 | bdev->bd_fsfreeze_count--; |
| 252 | goto done; |
| 253 | } |
| 254 | bdev->bd_fsfreeze_sb = sb; |
| 255 | |
| 256 | sync: |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 257 | sync_blockdev(bdev); |
Christoph Hellwig | 040f04b | 2020-11-24 11:54:06 +0100 | [diff] [blame] | 258 | done: |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 259 | mutex_unlock(&bdev->bd_fsfreeze_mutex); |
Christoph Hellwig | 040f04b | 2020-11-24 11:54:06 +0100 | [diff] [blame] | 260 | return error; |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 261 | } |
| 262 | EXPORT_SYMBOL(freeze_bdev); |
| 263 | |
| 264 | /** |
| 265 | * thaw_bdev -- unlock filesystem |
| 266 | * @bdev: blockdevice to unlock |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 267 | * |
| 268 | * Unlocks the filesystem and marks it writeable again after freeze_bdev(). |
| 269 | */ |
Christoph Hellwig | 040f04b | 2020-11-24 11:54:06 +0100 | [diff] [blame] | 270 | int thaw_bdev(struct block_device *bdev) |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 271 | { |
Christoph Hellwig | 040f04b | 2020-11-24 11:54:06 +0100 | [diff] [blame] | 272 | struct super_block *sb; |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 273 | int error = -EINVAL; |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 274 | |
| 275 | mutex_lock(&bdev->bd_fsfreeze_mutex); |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 276 | if (!bdev->bd_fsfreeze_count) |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 277 | goto out; |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 278 | |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 279 | error = 0; |
| 280 | if (--bdev->bd_fsfreeze_count > 0) |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 281 | goto out; |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 282 | |
Christoph Hellwig | 040f04b | 2020-11-24 11:54:06 +0100 | [diff] [blame] | 283 | sb = bdev->bd_fsfreeze_sb; |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 284 | if (!sb) |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 285 | goto out; |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 286 | |
Benjamin Marzinski | 48b6bca | 2014-11-13 20:42:03 -0600 | [diff] [blame] | 287 | if (sb->s_op->thaw_super) |
| 288 | error = sb->s_op->thaw_super(sb); |
| 289 | else |
| 290 | error = thaw_super(sb); |
Pierre Morel | 997198b | 2016-10-04 10:53:40 +0200 | [diff] [blame] | 291 | if (error) |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 292 | bdev->bd_fsfreeze_count++; |
Satya Tangirala | 04a6a53 | 2020-12-24 04:49:54 +0000 | [diff] [blame] | 293 | else |
| 294 | bdev->bd_fsfreeze_sb = NULL; |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 295 | out: |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 296 | mutex_unlock(&bdev->bd_fsfreeze_mutex); |
Pierre Morel | 997198b | 2016-10-04 10:53:40 +0200 | [diff] [blame] | 297 | return error; |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 298 | } |
| 299 | EXPORT_SYMBOL(thaw_bdev); |
| 300 | |
Matthew Wilcox | 47a191f | 2014-06-04 16:07:46 -0700 | [diff] [blame] | 301 | /** |
| 302 | * bdev_read_page() - Start reading a page from a block device |
| 303 | * @bdev: The device to read the page from |
| 304 | * @sector: The offset on the device to read the page to (need not be aligned) |
| 305 | * @page: The page to read |
| 306 | * |
| 307 | * On entry, the page should be locked. It will be unlocked when the page |
| 308 | * has been read. If the block driver implements rw_page synchronously, |
| 309 | * that will be true on exit from this function, but it need not be. |
| 310 | * |
| 311 | * Errors returned by this function are usually "soft", eg out of memory, or |
| 312 | * queue full; callers should try a different route to read this page rather |
| 313 | * than propagate an error back up the stack. |
| 314 | * |
| 315 | * Return: negative errno if an error occurs, 0 if submission was successful. |
| 316 | */ |
| 317 | int bdev_read_page(struct block_device *bdev, sector_t sector, |
| 318 | struct page *page) |
| 319 | { |
| 320 | const struct block_device_operations *ops = bdev->bd_disk->fops; |
Dan Williams | 2e6edc95 | 2015-11-19 13:29:28 -0800 | [diff] [blame] | 321 | int result = -EOPNOTSUPP; |
| 322 | |
Vishal Verma | f68eb1e | 2015-05-12 13:48:53 -0400 | [diff] [blame] | 323 | if (!ops->rw_page || bdev_get_integrity(bdev)) |
Dan Williams | 2e6edc95 | 2015-11-19 13:29:28 -0800 | [diff] [blame] | 324 | return result; |
| 325 | |
Pavel Begunkov | 025a386 | 2021-10-14 15:03:27 +0100 | [diff] [blame] | 326 | result = blk_queue_enter(bdev_get_queue(bdev), 0); |
Dan Williams | 2e6edc95 | 2015-11-19 13:29:28 -0800 | [diff] [blame] | 327 | if (result) |
| 328 | return result; |
Tejun Heo | 3f289dc | 2018-07-18 04:47:36 -0700 | [diff] [blame] | 329 | result = ops->rw_page(bdev, sector + get_start_sect(bdev), page, |
| 330 | REQ_OP_READ); |
Pavel Begunkov | 025a386 | 2021-10-14 15:03:27 +0100 | [diff] [blame] | 331 | blk_queue_exit(bdev_get_queue(bdev)); |
Dan Williams | 2e6edc95 | 2015-11-19 13:29:28 -0800 | [diff] [blame] | 332 | return result; |
Matthew Wilcox | 47a191f | 2014-06-04 16:07:46 -0700 | [diff] [blame] | 333 | } |
Matthew Wilcox | 47a191f | 2014-06-04 16:07:46 -0700 | [diff] [blame] | 334 | |
| 335 | /** |
| 336 | * bdev_write_page() - Start writing a page to a block device |
| 337 | * @bdev: The device to write the page to |
| 338 | * @sector: The offset on the device to write the page to (need not be aligned) |
| 339 | * @page: The page to write |
| 340 | * @wbc: The writeback_control for the write |
| 341 | * |
| 342 | * On entry, the page should be locked and not currently under writeback. |
| 343 | * On exit, if the write started successfully, the page will be unlocked and |
| 344 | * under writeback. If the write failed already (eg the driver failed to |
| 345 | * queue the page to the device), the page will still be locked. If the |
| 346 | * caller is a ->writepage implementation, it will need to unlock the page. |
| 347 | * |
| 348 | * Errors returned by this function are usually "soft", eg out of memory, or |
| 349 | * queue full; callers should try a different route to write this page rather |
| 350 | * than propagate an error back up the stack. |
| 351 | * |
| 352 | * Return: negative errno if an error occurs, 0 if submission was successful. |
| 353 | */ |
| 354 | int bdev_write_page(struct block_device *bdev, sector_t sector, |
| 355 | struct page *page, struct writeback_control *wbc) |
| 356 | { |
| 357 | int result; |
Matthew Wilcox | 47a191f | 2014-06-04 16:07:46 -0700 | [diff] [blame] | 358 | const struct block_device_operations *ops = bdev->bd_disk->fops; |
Dan Williams | 2e6edc95 | 2015-11-19 13:29:28 -0800 | [diff] [blame] | 359 | |
Vishal Verma | f68eb1e | 2015-05-12 13:48:53 -0400 | [diff] [blame] | 360 | if (!ops->rw_page || bdev_get_integrity(bdev)) |
Matthew Wilcox | 47a191f | 2014-06-04 16:07:46 -0700 | [diff] [blame] | 361 | return -EOPNOTSUPP; |
Pavel Begunkov | 025a386 | 2021-10-14 15:03:27 +0100 | [diff] [blame] | 362 | result = blk_queue_enter(bdev_get_queue(bdev), 0); |
Dan Williams | 2e6edc95 | 2015-11-19 13:29:28 -0800 | [diff] [blame] | 363 | if (result) |
| 364 | return result; |
| 365 | |
Matthew Wilcox | 47a191f | 2014-06-04 16:07:46 -0700 | [diff] [blame] | 366 | set_page_writeback(page); |
Tejun Heo | 3f289dc | 2018-07-18 04:47:36 -0700 | [diff] [blame] | 367 | result = ops->rw_page(bdev, sector + get_start_sect(bdev), page, |
| 368 | REQ_OP_WRITE); |
Matthew Wilcox | f892760 | 2017-10-13 15:58:15 -0700 | [diff] [blame] | 369 | if (result) { |
Matthew Wilcox | 47a191f | 2014-06-04 16:07:46 -0700 | [diff] [blame] | 370 | end_page_writeback(page); |
Matthew Wilcox | f892760 | 2017-10-13 15:58:15 -0700 | [diff] [blame] | 371 | } else { |
| 372 | clean_page_buffers(page); |
Matthew Wilcox | 47a191f | 2014-06-04 16:07:46 -0700 | [diff] [blame] | 373 | unlock_page(page); |
Matthew Wilcox | f892760 | 2017-10-13 15:58:15 -0700 | [diff] [blame] | 374 | } |
Pavel Begunkov | 025a386 | 2021-10-14 15:03:27 +0100 | [diff] [blame] | 375 | blk_queue_exit(bdev_get_queue(bdev)); |
Matthew Wilcox | 47a191f | 2014-06-04 16:07:46 -0700 | [diff] [blame] | 376 | return result; |
| 377 | } |
Matthew Wilcox | 47a191f | 2014-06-04 16:07:46 -0700 | [diff] [blame] | 378 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | /* |
| 380 | * pseudo-fs |
| 381 | */ |
| 382 | |
| 383 | static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock); |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 384 | static struct kmem_cache * bdev_cachep __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | |
| 386 | static struct inode *bdev_alloc_inode(struct super_block *sb) |
| 387 | { |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 388 | struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL); |
Christoph Hellwig | 2d2f6f1 | 2021-01-07 19:36:40 +0100 | [diff] [blame] | 389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | if (!ei) |
| 391 | return NULL; |
Christoph Hellwig | 2d2f6f1 | 2021-01-07 19:36:40 +0100 | [diff] [blame] | 392 | memset(&ei->bdev, 0, sizeof(ei->bdev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | return &ei->vfs_inode; |
| 394 | } |
| 395 | |
Al Viro | 41149cb | 2019-04-10 15:12:38 -0400 | [diff] [blame] | 396 | static void bdev_free_inode(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | { |
Christoph Hellwig | 15e3d2c | 2020-11-24 09:34:00 +0100 | [diff] [blame] | 398 | struct block_device *bdev = I_BDEV(inode); |
| 399 | |
| 400 | free_percpu(bdev->bd_stats); |
Christoph Hellwig | 231926d | 2020-11-24 12:01:45 +0100 | [diff] [blame] | 401 | kfree(bdev->bd_meta_info); |
Christoph Hellwig | 15e3d2c | 2020-11-24 09:34:00 +0100 | [diff] [blame] | 402 | |
Christoph Hellwig | 889c05c | 2021-08-16 14:26:14 +0200 | [diff] [blame] | 403 | if (!bdev_is_partition(bdev)) { |
| 404 | if (bdev->bd_disk && bdev->bd_disk->bdi) |
| 405 | bdi_put(bdev->bd_disk->bdi); |
Christoph Hellwig | 340e845 | 2021-07-22 09:53:54 +0200 | [diff] [blame] | 406 | kfree(bdev->bd_disk); |
Christoph Hellwig | 889c05c | 2021-08-16 14:26:14 +0200 | [diff] [blame] | 407 | } |
Christoph Hellwig | 9451aa0 | 2021-08-16 14:26:13 +0200 | [diff] [blame] | 408 | |
| 409 | if (MAJOR(bdev->bd_dev) == BLOCK_EXT_MAJOR) |
| 410 | blk_free_ext_minor(MINOR(bdev->bd_dev)); |
| 411 | |
Al Viro | 41149cb | 2019-04-10 15:12:38 -0400 | [diff] [blame] | 412 | kmem_cache_free(bdev_cachep, BDEV_I(inode)); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 413 | } |
| 414 | |
Christoph Hellwig | e6cb538 | 2020-11-23 15:41:40 +0100 | [diff] [blame] | 415 | static void init_once(void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | { |
Christoph Hellwig | e6cb538 | 2020-11-23 15:41:40 +0100 | [diff] [blame] | 417 | struct bdev_inode *ei = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
Christoph Lameter | a35afb8 | 2007-05-16 22:10:57 -0700 | [diff] [blame] | 419 | inode_init_once(&ei->vfs_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Al Viro | b57922d | 2010-06-07 14:34:48 -0400 | [diff] [blame] | 422 | static void bdev_evict_inode(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | { |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 424 | truncate_inode_pages_final(&inode->i_data); |
Al Viro | b57922d | 2010-06-07 14:34:48 -0400 | [diff] [blame] | 425 | invalidate_inode_buffers(inode); /* is it needed here? */ |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 426 | clear_inode(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Josef 'Jeff' Sipek | ee9b6d6 | 2007-02-12 00:55:41 -0800 | [diff] [blame] | 429 | static const struct super_operations bdev_sops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | .statfs = simple_statfs, |
| 431 | .alloc_inode = bdev_alloc_inode, |
Al Viro | 41149cb | 2019-04-10 15:12:38 -0400 | [diff] [blame] | 432 | .free_inode = bdev_free_inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | .drop_inode = generic_delete_inode, |
Al Viro | b57922d | 2010-06-07 14:34:48 -0400 | [diff] [blame] | 434 | .evict_inode = bdev_evict_inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | }; |
| 436 | |
David Howells | 9030d16 | 2019-03-25 16:38:23 +0000 | [diff] [blame] | 437 | static int bd_init_fs_context(struct fs_context *fc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | { |
David Howells | 9030d16 | 2019-03-25 16:38:23 +0000 | [diff] [blame] | 439 | struct pseudo_fs_context *ctx = init_pseudo(fc, BDEVFS_MAGIC); |
| 440 | if (!ctx) |
| 441 | return -ENOMEM; |
| 442 | fc->s_iflags |= SB_I_CGROUPWB; |
| 443 | ctx->ops = &bdev_sops; |
| 444 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | static struct file_system_type bd_type = { |
| 448 | .name = "bdev", |
David Howells | 9030d16 | 2019-03-25 16:38:23 +0000 | [diff] [blame] | 449 | .init_fs_context = bd_init_fs_context, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | .kill_sb = kill_anon_super, |
| 451 | }; |
| 452 | |
Tejun Heo | a212b10 | 2015-05-22 17:13:33 -0400 | [diff] [blame] | 453 | struct super_block *blockdev_superblock __read_mostly; |
| 454 | EXPORT_SYMBOL_GPL(blockdev_superblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
| 456 | void __init bdev_cache_init(void) |
| 457 | { |
| 458 | int err; |
Sergey Senozhatsky | ace8577 | 2012-01-10 02:43:59 +0300 | [diff] [blame] | 459 | static struct vfsmount *bd_mnt; |
Denis ChengRq | c2acf7b | 2008-12-01 14:34:56 -0800 | [diff] [blame] | 460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode), |
Paul Jackson | fffb60f | 2006-03-24 03:16:06 -0800 | [diff] [blame] | 462 | 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT| |
Vladimir Davydov | 5d09705 | 2016-01-14 15:18:21 -0800 | [diff] [blame] | 463 | SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 464 | init_once); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | err = register_filesystem(&bd_type); |
| 466 | if (err) |
| 467 | panic("Cannot register bdev pseudo-fs"); |
| 468 | bd_mnt = kern_mount(&bd_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | if (IS_ERR(bd_mnt)) |
| 470 | panic("Cannot create bdev pseudo-fs"); |
Sergey Senozhatsky | ace8577 | 2012-01-10 02:43:59 +0300 | [diff] [blame] | 471 | blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 474 | struct block_device *bdev_alloc(struct gendisk *disk, u8 partno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | { |
| 476 | struct block_device *bdev; |
| 477 | struct inode *inode; |
| 478 | |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 479 | inode = new_inode(blockdev_superblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | if (!inode) |
| 481 | return NULL; |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 482 | inode->i_mode = S_IFBLK; |
| 483 | inode->i_rdev = 0; |
| 484 | inode->i_data.a_ops = &def_blk_aops; |
| 485 | mapping_set_gfp_mask(&inode->i_data, GFP_USER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 487 | bdev = I_BDEV(inode); |
Christoph Hellwig | e6cb538 | 2020-11-23 15:41:40 +0100 | [diff] [blame] | 488 | mutex_init(&bdev->bd_fsfreeze_mutex); |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 489 | spin_lock_init(&bdev->bd_size_lock); |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 490 | bdev->bd_partno = partno; |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 491 | bdev->bd_inode = inode; |
Pavel Begunkov | 17220ca | 2021-10-14 15:03:26 +0100 | [diff] [blame] | 492 | bdev->bd_queue = disk->queue; |
Christoph Hellwig | 15e3d2c | 2020-11-24 09:34:00 +0100 | [diff] [blame] | 493 | bdev->bd_stats = alloc_percpu(struct disk_stats); |
| 494 | if (!bdev->bd_stats) { |
| 495 | iput(inode); |
| 496 | return NULL; |
| 497 | } |
Tetsuo Handa | 06cc978 | 2021-10-02 18:23:02 +0900 | [diff] [blame] | 498 | bdev->bd_disk = disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | return bdev; |
| 500 | } |
| 501 | |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 502 | void bdev_add(struct block_device *bdev, dev_t dev) |
| 503 | { |
| 504 | bdev->bd_dev = dev; |
| 505 | bdev->bd_inode->i_rdev = dev; |
| 506 | bdev->bd_inode->i_ino = dev; |
| 507 | insert_inode_hash(bdev->bd_inode); |
| 508 | } |
| 509 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | long nr_blockdev_pages(void) |
| 511 | { |
Christoph Hellwig | 1008fe6 | 2020-06-26 10:01:58 +0200 | [diff] [blame] | 512 | struct inode *inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | long ret = 0; |
Christoph Hellwig | 1008fe6 | 2020-06-26 10:01:58 +0200 | [diff] [blame] | 514 | |
| 515 | spin_lock(&blockdev_superblock->s_inode_list_lock); |
| 516 | list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) |
| 517 | ret += inode->i_mapping->nrpages; |
| 518 | spin_unlock(&blockdev_superblock->s_inode_list_lock); |
| 519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | return ret; |
| 521 | } |
| 522 | |
Tejun Heo | 1a3cbbc | 2010-04-07 18:52:29 +0900 | [diff] [blame] | 523 | /** |
| 524 | * bd_may_claim - test whether a block device can be claimed |
| 525 | * @bdev: block device of interest |
| 526 | * @whole: whole block device containing @bdev, may equal @bdev |
| 527 | * @holder: holder trying to claim @bdev |
| 528 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 529 | * Test whether @bdev can be claimed by @holder. |
Tejun Heo | 1a3cbbc | 2010-04-07 18:52:29 +0900 | [diff] [blame] | 530 | * |
| 531 | * CONTEXT: |
| 532 | * spin_lock(&bdev_lock). |
| 533 | * |
| 534 | * RETURNS: |
| 535 | * %true if @bdev can be claimed, %false otherwise. |
| 536 | */ |
| 537 | static bool bd_may_claim(struct block_device *bdev, struct block_device *whole, |
| 538 | void *holder) |
| 539 | { |
| 540 | if (bdev->bd_holder == holder) |
| 541 | return true; /* already a holder */ |
| 542 | else if (bdev->bd_holder != NULL) |
| 543 | return false; /* held by someone else */ |
NeilBrown | bcc7f5b | 2016-12-12 08:21:51 -0700 | [diff] [blame] | 544 | else if (whole == bdev) |
Tejun Heo | 1a3cbbc | 2010-04-07 18:52:29 +0900 | [diff] [blame] | 545 | return true; /* is a whole device which isn't held */ |
| 546 | |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 547 | else if (whole->bd_holder == bd_may_claim) |
Tejun Heo | 1a3cbbc | 2010-04-07 18:52:29 +0900 | [diff] [blame] | 548 | return true; /* is a partition of a device that is being partitioned */ |
| 549 | else if (whole->bd_holder != NULL) |
| 550 | return false; /* is a partition of a held device */ |
| 551 | else |
| 552 | return true; /* is a partition of an un-held device */ |
| 553 | } |
| 554 | |
| 555 | /** |
Christoph Hellwig | 58e46ed | 2020-07-16 16:33:08 +0200 | [diff] [blame] | 556 | * bd_prepare_to_claim - claim a block device |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 557 | * @bdev: block device of interest |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 558 | * @holder: holder trying to claim @bdev |
| 559 | * |
Christoph Hellwig | 58e46ed | 2020-07-16 16:33:08 +0200 | [diff] [blame] | 560 | * Claim @bdev. This function fails if @bdev is already claimed by another |
| 561 | * holder and waits if another claiming is in progress. return, the caller |
| 562 | * has ownership of bd_claiming and bd_holder[s]. |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 563 | * |
| 564 | * RETURNS: |
| 565 | * 0 if @bdev can be claimed, -EBUSY otherwise. |
| 566 | */ |
Christoph Hellwig | 37c3fc9 | 2020-11-25 21:20:08 +0100 | [diff] [blame] | 567 | int bd_prepare_to_claim(struct block_device *bdev, void *holder) |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 568 | { |
Christoph Hellwig | 37c3fc9 | 2020-11-25 21:20:08 +0100 | [diff] [blame] | 569 | struct block_device *whole = bdev_whole(bdev); |
| 570 | |
| 571 | if (WARN_ON_ONCE(!holder)) |
| 572 | return -EINVAL; |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 573 | retry: |
Christoph Hellwig | 58e46ed | 2020-07-16 16:33:08 +0200 | [diff] [blame] | 574 | spin_lock(&bdev_lock); |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 575 | /* if someone else claimed, fail */ |
Christoph Hellwig | 58e46ed | 2020-07-16 16:33:08 +0200 | [diff] [blame] | 576 | if (!bd_may_claim(bdev, whole, holder)) { |
| 577 | spin_unlock(&bdev_lock); |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 578 | return -EBUSY; |
Christoph Hellwig | 58e46ed | 2020-07-16 16:33:08 +0200 | [diff] [blame] | 579 | } |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 580 | |
Tejun Heo | e75aa85 | 2010-08-04 17:59:39 +0200 | [diff] [blame] | 581 | /* if claiming is already in progress, wait for it to finish */ |
| 582 | if (whole->bd_claiming) { |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 583 | wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0); |
| 584 | DEFINE_WAIT(wait); |
| 585 | |
| 586 | prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE); |
| 587 | spin_unlock(&bdev_lock); |
| 588 | schedule(); |
| 589 | finish_wait(wq, &wait); |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 590 | goto retry; |
| 591 | } |
| 592 | |
| 593 | /* yay, all mine */ |
Christoph Hellwig | 58e46ed | 2020-07-16 16:33:08 +0200 | [diff] [blame] | 594 | whole->bd_claiming = holder; |
| 595 | spin_unlock(&bdev_lock); |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 596 | return 0; |
| 597 | } |
Christoph Hellwig | ecbe6bc | 2020-07-16 16:33:09 +0200 | [diff] [blame] | 598 | EXPORT_SYMBOL_GPL(bd_prepare_to_claim); /* only for the loop driver */ |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 599 | |
Jan Kara | 89e524c0 | 2019-07-30 13:10:14 +0200 | [diff] [blame] | 600 | static void bd_clear_claiming(struct block_device *whole, void *holder) |
| 601 | { |
| 602 | lockdep_assert_held(&bdev_lock); |
| 603 | /* tell others that we're done */ |
| 604 | BUG_ON(whole->bd_claiming != holder); |
| 605 | whole->bd_claiming = NULL; |
| 606 | wake_up_bit(&whole->bd_claiming, 0); |
| 607 | } |
| 608 | |
| 609 | /** |
| 610 | * bd_finish_claiming - finish claiming of a block device |
| 611 | * @bdev: block device of interest |
Jan Kara | 89e524c0 | 2019-07-30 13:10:14 +0200 | [diff] [blame] | 612 | * @holder: holder that has claimed @bdev |
| 613 | * |
| 614 | * Finish exclusive open of a block device. Mark the device as exlusively |
| 615 | * open by the holder and wake up all waiters for exclusive open to finish. |
| 616 | */ |
Christoph Hellwig | 37c3fc9 | 2020-11-25 21:20:08 +0100 | [diff] [blame] | 617 | static void bd_finish_claiming(struct block_device *bdev, void *holder) |
Jan Kara | 89e524c0 | 2019-07-30 13:10:14 +0200 | [diff] [blame] | 618 | { |
Christoph Hellwig | 37c3fc9 | 2020-11-25 21:20:08 +0100 | [diff] [blame] | 619 | struct block_device *whole = bdev_whole(bdev); |
| 620 | |
Jan Kara | 89e524c0 | 2019-07-30 13:10:14 +0200 | [diff] [blame] | 621 | spin_lock(&bdev_lock); |
| 622 | BUG_ON(!bd_may_claim(bdev, whole, holder)); |
| 623 | /* |
| 624 | * Note that for a whole device bd_holders will be incremented twice, |
| 625 | * and bd_holder will be set to bd_may_claim before being set to holder |
| 626 | */ |
| 627 | whole->bd_holders++; |
| 628 | whole->bd_holder = bd_may_claim; |
| 629 | bdev->bd_holders++; |
| 630 | bdev->bd_holder = holder; |
| 631 | bd_clear_claiming(whole, holder); |
| 632 | spin_unlock(&bdev_lock); |
| 633 | } |
Jan Kara | 89e524c0 | 2019-07-30 13:10:14 +0200 | [diff] [blame] | 634 | |
| 635 | /** |
| 636 | * bd_abort_claiming - abort claiming of a block device |
| 637 | * @bdev: block device of interest |
Jan Kara | 89e524c0 | 2019-07-30 13:10:14 +0200 | [diff] [blame] | 638 | * @holder: holder that has claimed @bdev |
| 639 | * |
| 640 | * Abort claiming of a block device when the exclusive open failed. This can be |
| 641 | * also used when exclusive open is not actually desired and we just needed |
| 642 | * to block other exclusive openers for a while. |
| 643 | */ |
Christoph Hellwig | 37c3fc9 | 2020-11-25 21:20:08 +0100 | [diff] [blame] | 644 | void bd_abort_claiming(struct block_device *bdev, void *holder) |
Jan Kara | 89e524c0 | 2019-07-30 13:10:14 +0200 | [diff] [blame] | 645 | { |
| 646 | spin_lock(&bdev_lock); |
Christoph Hellwig | 37c3fc9 | 2020-11-25 21:20:08 +0100 | [diff] [blame] | 647 | bd_clear_claiming(bdev_whole(bdev), holder); |
Jan Kara | 89e524c0 | 2019-07-30 13:10:14 +0200 | [diff] [blame] | 648 | spin_unlock(&bdev_lock); |
| 649 | } |
| 650 | EXPORT_SYMBOL(bd_abort_claiming); |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 651 | |
Christoph Hellwig | c8276b9 | 2021-05-25 08:12:58 +0200 | [diff] [blame] | 652 | static void blkdev_flush_mapping(struct block_device *bdev) |
Christoph Hellwig | a1548b6 | 2019-11-14 15:34:34 +0100 | [diff] [blame] | 653 | { |
Christoph Hellwig | c8276b9 | 2021-05-25 08:12:58 +0200 | [diff] [blame] | 654 | WARN_ON_ONCE(bdev->bd_holders); |
Christoph Hellwig | d3c4a43 | 2021-04-06 08:22:55 +0200 | [diff] [blame] | 655 | sync_blockdev(bdev); |
Christoph Hellwig | c8276b9 | 2021-05-25 08:12:58 +0200 | [diff] [blame] | 656 | kill_bdev(bdev); |
| 657 | bdev_write_inode(bdev); |
Christoph Hellwig | a1548b6 | 2019-11-14 15:34:34 +0100 | [diff] [blame] | 658 | } |
| 659 | |
Christoph Hellwig | 362529d | 2021-05-25 08:12:54 +0200 | [diff] [blame] | 660 | static int blkdev_get_whole(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | { |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 662 | struct gendisk *disk = bdev->bd_disk; |
Colin Ian King | af22fef | 2021-11-26 23:06:52 +0000 | [diff] [blame] | 663 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | |
Christoph Hellwig | 362529d | 2021-05-25 08:12:54 +0200 | [diff] [blame] | 665 | if (disk->fops->open) { |
| 666 | ret = disk->fops->open(bdev, mode); |
| 667 | if (ret) { |
| 668 | /* avoid ghost partitions on a removed medium */ |
| 669 | if (ret == -ENOMEDIUM && |
| 670 | test_bit(GD_NEED_PART_SCAN, &disk->state)) |
Christoph Hellwig | 0384264 | 2021-06-24 14:32:40 +0200 | [diff] [blame] | 671 | bdev_disk_changed(disk, true); |
Christoph Hellwig | 362529d | 2021-05-25 08:12:54 +0200 | [diff] [blame] | 672 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | } |
| 674 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | |
Christoph Hellwig | a11d7fc | 2021-08-09 16:17:44 +0200 | [diff] [blame] | 676 | if (!bdev->bd_openers) |
Christoph Hellwig | 362529d | 2021-05-25 08:12:54 +0200 | [diff] [blame] | 677 | set_init_blocksize(bdev); |
Christoph Hellwig | 362529d | 2021-05-25 08:12:54 +0200 | [diff] [blame] | 678 | if (test_bit(GD_NEED_PART_SCAN, &disk->state)) |
Christoph Hellwig | 0384264 | 2021-06-24 14:32:40 +0200 | [diff] [blame] | 679 | bdev_disk_changed(disk, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | bdev->bd_openers++; |
Christoph Hellwig | 362529d | 2021-05-25 08:12:54 +0200 | [diff] [blame] | 681 | return 0;; |
| 682 | } |
| 683 | |
Christoph Hellwig | c8276b9 | 2021-05-25 08:12:58 +0200 | [diff] [blame] | 684 | static void blkdev_put_whole(struct block_device *bdev, fmode_t mode) |
| 685 | { |
| 686 | if (!--bdev->bd_openers) |
| 687 | blkdev_flush_mapping(bdev); |
| 688 | if (bdev->bd_disk->fops->release) |
| 689 | bdev->bd_disk->fops->release(bdev->bd_disk, mode); |
| 690 | } |
| 691 | |
Christoph Hellwig | 362529d | 2021-05-25 08:12:54 +0200 | [diff] [blame] | 692 | static int blkdev_get_part(struct block_device *part, fmode_t mode) |
| 693 | { |
| 694 | struct gendisk *disk = part->bd_disk; |
Christoph Hellwig | 362529d | 2021-05-25 08:12:54 +0200 | [diff] [blame] | 695 | int ret; |
| 696 | |
| 697 | if (part->bd_openers) |
| 698 | goto done; |
| 699 | |
Christoph Hellwig | 9d3b881 | 2021-07-22 09:53:58 +0200 | [diff] [blame] | 700 | ret = blkdev_get_whole(bdev_whole(part), mode); |
Christoph Hellwig | a869870 | 2021-05-25 08:12:56 +0200 | [diff] [blame] | 701 | if (ret) |
Christoph Hellwig | 9d3b881 | 2021-07-22 09:53:58 +0200 | [diff] [blame] | 702 | return ret; |
Christoph Hellwig | 362529d | 2021-05-25 08:12:54 +0200 | [diff] [blame] | 703 | |
| 704 | ret = -ENXIO; |
| 705 | if (!bdev_nr_sectors(part)) |
| 706 | goto out_blkdev_put; |
| 707 | |
Christoph Hellwig | ab4b570 | 2021-05-25 08:12:59 +0200 | [diff] [blame] | 708 | disk->open_partitions++; |
Christoph Hellwig | 362529d | 2021-05-25 08:12:54 +0200 | [diff] [blame] | 709 | set_init_blocksize(part); |
Christoph Hellwig | 362529d | 2021-05-25 08:12:54 +0200 | [diff] [blame] | 710 | done: |
| 711 | part->bd_openers++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | return 0; |
Christoph Hellwig | 362529d | 2021-05-25 08:12:54 +0200 | [diff] [blame] | 713 | |
| 714 | out_blkdev_put: |
Christoph Hellwig | 9d3b881 | 2021-07-22 09:53:58 +0200 | [diff] [blame] | 715 | blkdev_put_whole(bdev_whole(part), mode); |
Christoph Hellwig | 362529d | 2021-05-25 08:12:54 +0200 | [diff] [blame] | 716 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Christoph Hellwig | c8276b9 | 2021-05-25 08:12:58 +0200 | [diff] [blame] | 719 | static void blkdev_put_part(struct block_device *part, fmode_t mode) |
| 720 | { |
| 721 | struct block_device *whole = bdev_whole(part); |
| 722 | |
| 723 | if (--part->bd_openers) |
| 724 | return; |
| 725 | blkdev_flush_mapping(part); |
Christoph Hellwig | ab4b570 | 2021-05-25 08:12:59 +0200 | [diff] [blame] | 726 | whole->bd_disk->open_partitions--; |
Christoph Hellwig | c8276b9 | 2021-05-25 08:12:58 +0200 | [diff] [blame] | 727 | blkdev_put_whole(whole, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | } |
| 729 | |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 730 | struct block_device *blkdev_get_no_open(dev_t dev) |
| 731 | { |
| 732 | struct block_device *bdev; |
Christoph Hellwig | 9d3b881 | 2021-07-22 09:53:58 +0200 | [diff] [blame] | 733 | struct inode *inode; |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 734 | |
Christoph Hellwig | 9d3b881 | 2021-07-22 09:53:58 +0200 | [diff] [blame] | 735 | inode = ilookup(blockdev_superblock, dev); |
| 736 | if (!inode) { |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 737 | blk_request_module(dev); |
Christoph Hellwig | 9d3b881 | 2021-07-22 09:53:58 +0200 | [diff] [blame] | 738 | inode = ilookup(blockdev_superblock, dev); |
| 739 | if (!inode) |
Christoph Hellwig | 6c60ff0 | 2021-05-14 15:18:41 +0200 | [diff] [blame] | 740 | return NULL; |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 741 | } |
| 742 | |
Christoph Hellwig | 9d3b881 | 2021-07-22 09:53:58 +0200 | [diff] [blame] | 743 | /* switch from the inode reference to a device mode one: */ |
| 744 | bdev = &BDEV_I(inode)->bdev; |
| 745 | if (!kobject_get_unless_zero(&bdev->bd_device.kobj)) |
| 746 | bdev = NULL; |
| 747 | iput(inode); |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 748 | return bdev; |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | void blkdev_put_no_open(struct block_device *bdev) |
| 752 | { |
Christoph Hellwig | 9d3b881 | 2021-07-22 09:53:58 +0200 | [diff] [blame] | 753 | put_device(&bdev->bd_device); |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 754 | } |
| 755 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 756 | /** |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 757 | * blkdev_get_by_dev - open a block device by device number |
| 758 | * @dev: device number of block device to open |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 759 | * @mode: FMODE_* mask |
| 760 | * @holder: exclusive holder identifier |
| 761 | * |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 762 | * Open the block device described by device number @dev. If @mode includes |
| 763 | * %FMODE_EXCL, the block device is opened with exclusive access. Specifying |
| 764 | * %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may nest for |
| 765 | * the same @holder. |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 766 | * |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 767 | * Use this interface ONLY if you really do not have anything better - i.e. when |
| 768 | * you are behind a truly sucky interface and all you are given is a device |
| 769 | * number. Everything else should use blkdev_get_by_path(). |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 770 | * |
| 771 | * CONTEXT: |
| 772 | * Might sleep. |
| 773 | * |
| 774 | * RETURNS: |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 775 | * Reference to the block_device on success, ERR_PTR(-errno) on failure. |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 776 | */ |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 777 | struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | { |
Christoph Hellwig | 5b56b6ed | 2020-11-23 10:19:22 +0100 | [diff] [blame] | 779 | bool unblock_events = true; |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 780 | struct block_device *bdev; |
Christoph Hellwig | 5b56b6ed | 2020-11-23 10:19:22 +0100 | [diff] [blame] | 781 | struct gendisk *disk; |
Christoph Hellwig | 5b56b6ed | 2020-11-23 10:19:22 +0100 | [diff] [blame] | 782 | int ret; |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 783 | |
Christoph Hellwig | 7918f0f | 2020-11-23 13:44:44 +0100 | [diff] [blame] | 784 | ret = devcgroup_check_permission(DEVCG_DEV_BLOCK, |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 785 | MAJOR(dev), MINOR(dev), |
Christoph Hellwig | 7918f0f | 2020-11-23 13:44:44 +0100 | [diff] [blame] | 786 | ((mode & FMODE_READ) ? DEVCG_ACC_READ : 0) | |
| 787 | ((mode & FMODE_WRITE) ? DEVCG_ACC_WRITE : 0)); |
Christoph Hellwig | e5c7fb4 | 2020-08-31 20:02:36 +0200 | [diff] [blame] | 788 | if (ret) |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 789 | return ERR_PTR(ret); |
| 790 | |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 791 | bdev = blkdev_get_no_open(dev); |
| 792 | if (!bdev) |
| 793 | return ERR_PTR(-ENXIO); |
| 794 | disk = bdev->bd_disk; |
Christoph Hellwig | e5c7fb4 | 2020-08-31 20:02:36 +0200 | [diff] [blame] | 795 | |
Christoph Hellwig | 5b56b6ed | 2020-11-23 10:19:22 +0100 | [diff] [blame] | 796 | if (mode & FMODE_EXCL) { |
Christoph Hellwig | 37c3fc9 | 2020-11-25 21:20:08 +0100 | [diff] [blame] | 797 | ret = bd_prepare_to_claim(bdev, holder); |
Christoph Hellwig | 5b56b6ed | 2020-11-23 10:19:22 +0100 | [diff] [blame] | 798 | if (ret) |
Christoph Hellwig | 37c3fc9 | 2020-11-25 21:20:08 +0100 | [diff] [blame] | 799 | goto put_blkdev; |
Christoph Hellwig | 5b56b6ed | 2020-11-23 10:19:22 +0100 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | disk_block_events(disk); |
| 803 | |
Christoph Hellwig | a869870 | 2021-05-25 08:12:56 +0200 | [diff] [blame] | 804 | mutex_lock(&disk->open_mutex); |
Christoph Hellwig | 362529d | 2021-05-25 08:12:54 +0200 | [diff] [blame] | 805 | ret = -ENXIO; |
Christoph Hellwig | 50b4aec | 2021-08-09 08:40:28 +0200 | [diff] [blame] | 806 | if (!disk_live(disk)) |
Christoph Hellwig | 362529d | 2021-05-25 08:12:54 +0200 | [diff] [blame] | 807 | goto abort_claiming; |
Ming Lei | efcf593 | 2021-11-11 10:03:43 +0800 | [diff] [blame] | 808 | if (!try_module_get(disk->fops->owner)) |
| 809 | goto abort_claiming; |
Christoph Hellwig | 362529d | 2021-05-25 08:12:54 +0200 | [diff] [blame] | 810 | if (bdev_is_partition(bdev)) |
| 811 | ret = blkdev_get_part(bdev, mode); |
| 812 | else |
| 813 | ret = blkdev_get_whole(bdev, mode); |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 814 | if (ret) |
Ming Lei | efcf593 | 2021-11-11 10:03:43 +0800 | [diff] [blame] | 815 | goto put_module; |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 816 | if (mode & FMODE_EXCL) { |
Christoph Hellwig | 37c3fc9 | 2020-11-25 21:20:08 +0100 | [diff] [blame] | 817 | bd_finish_claiming(bdev, holder); |
Christoph Hellwig | 5b56b6ed | 2020-11-23 10:19:22 +0100 | [diff] [blame] | 818 | |
| 819 | /* |
| 820 | * Block event polling for write claims if requested. Any write |
| 821 | * holder makes the write_holder state stick until all are |
| 822 | * released. This is good enough and tracking individual |
| 823 | * writeable reference is too fragile given the way @mode is |
| 824 | * used in blkdev_get/put(). |
| 825 | */ |
| 826 | if ((mode & FMODE_WRITE) && !bdev->bd_write_holder && |
Christoph Hellwig | 1545e0b | 2021-11-22 14:06:13 +0100 | [diff] [blame] | 827 | (disk->event_flags & DISK_EVENT_FLAG_BLOCK_ON_EXCL_WRITE)) { |
Christoph Hellwig | 5b56b6ed | 2020-11-23 10:19:22 +0100 | [diff] [blame] | 828 | bdev->bd_write_holder = true; |
| 829 | unblock_events = false; |
| 830 | } |
| 831 | } |
Christoph Hellwig | a869870 | 2021-05-25 08:12:56 +0200 | [diff] [blame] | 832 | mutex_unlock(&disk->open_mutex); |
Christoph Hellwig | 5b56b6ed | 2020-11-23 10:19:22 +0100 | [diff] [blame] | 833 | |
| 834 | if (unblock_events) |
| 835 | disk_unblock_events(disk); |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 836 | return bdev; |
Ming Lei | efcf593 | 2021-11-11 10:03:43 +0800 | [diff] [blame] | 837 | put_module: |
| 838 | module_put(disk->fops->owner); |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 839 | abort_claiming: |
| 840 | if (mode & FMODE_EXCL) |
Christoph Hellwig | 37c3fc9 | 2020-11-25 21:20:08 +0100 | [diff] [blame] | 841 | bd_abort_claiming(bdev, holder); |
Christoph Hellwig | a869870 | 2021-05-25 08:12:56 +0200 | [diff] [blame] | 842 | mutex_unlock(&disk->open_mutex); |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 843 | disk_unblock_events(disk); |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 844 | put_blkdev: |
| 845 | blkdev_put_no_open(bdev); |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 846 | return ERR_PTR(ret); |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 847 | } |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 848 | EXPORT_SYMBOL(blkdev_get_by_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 850 | /** |
| 851 | * blkdev_get_by_path - open a block device by name |
| 852 | * @path: path to the block device to open |
| 853 | * @mode: FMODE_* mask |
| 854 | * @holder: exclusive holder identifier |
| 855 | * |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 856 | * Open the block device described by the device file at @path. If @mode |
| 857 | * includes %FMODE_EXCL, the block device is opened with exclusive access. |
| 858 | * Specifying %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may |
| 859 | * nest for the same @holder. |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 860 | * |
| 861 | * CONTEXT: |
| 862 | * Might sleep. |
| 863 | * |
| 864 | * RETURNS: |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 865 | * Reference to the block_device on success, ERR_PTR(-errno) on failure. |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 866 | */ |
| 867 | struct block_device *blkdev_get_by_path(const char *path, fmode_t mode, |
| 868 | void *holder) |
| 869 | { |
| 870 | struct block_device *bdev; |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 871 | dev_t dev; |
| 872 | int error; |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 873 | |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 874 | error = lookup_bdev(path, &dev); |
| 875 | if (error) |
| 876 | return ERR_PTR(error); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 877 | |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 878 | bdev = blkdev_get_by_dev(dev, mode, holder); |
| 879 | if (!IS_ERR(bdev) && (mode & FMODE_WRITE) && bdev_read_only(bdev)) { |
Chuck Ebbert | e51900f | 2011-02-16 18:11:53 -0500 | [diff] [blame] | 880 | blkdev_put(bdev, mode); |
| 881 | return ERR_PTR(-EACCES); |
| 882 | } |
| 883 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 884 | return bdev; |
| 885 | } |
| 886 | EXPORT_SYMBOL(blkdev_get_by_path); |
| 887 | |
Al Viro | 4385bab | 2013-05-05 22:11:03 -0400 | [diff] [blame] | 888 | void blkdev_put(struct block_device *bdev, fmode_t mode) |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 889 | { |
| 890 | struct gendisk *disk = bdev->bd_disk; |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 891 | |
| 892 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | * Sync early if it looks like we're the last one. If someone else |
Pavel Emelianov | 6a2aae0 | 2006-10-28 10:38:33 -0700 | [diff] [blame] | 894 | * opens the block device between now and the decrement of bd_openers |
| 895 | * then we did a sync that we didn't need to, but that's not the end |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | * of the world and we want to avoid long (could be several minute) |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 897 | * syncs while holding the mutex. |
| 898 | */ |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 899 | if (bdev->bd_openers == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | sync_blockdev(bdev); |
| 901 | |
Christoph Hellwig | a869870 | 2021-05-25 08:12:56 +0200 | [diff] [blame] | 902 | mutex_lock(&disk->open_mutex); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 903 | if (mode & FMODE_EXCL) { |
Christoph Hellwig | a954ea8 | 2020-11-23 13:29:55 +0100 | [diff] [blame] | 904 | struct block_device *whole = bdev_whole(bdev); |
Tejun Heo | 6a027ef | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 905 | bool bdev_free; |
| 906 | |
| 907 | /* |
| 908 | * Release a claim on the device. The holder fields |
Christoph Hellwig | a869870 | 2021-05-25 08:12:56 +0200 | [diff] [blame] | 909 | * are protected with bdev_lock. open_mutex is to |
Tejun Heo | 6a027ef | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 910 | * synchronize disk_holder unlinking. |
| 911 | */ |
Tejun Heo | 6a027ef | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 912 | spin_lock(&bdev_lock); |
| 913 | |
| 914 | WARN_ON_ONCE(--bdev->bd_holders < 0); |
Christoph Hellwig | a954ea8 | 2020-11-23 13:29:55 +0100 | [diff] [blame] | 915 | WARN_ON_ONCE(--whole->bd_holders < 0); |
Tejun Heo | 6a027ef | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 916 | |
Tejun Heo | 6a027ef | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 917 | if ((bdev_free = !bdev->bd_holders)) |
| 918 | bdev->bd_holder = NULL; |
Christoph Hellwig | a954ea8 | 2020-11-23 13:29:55 +0100 | [diff] [blame] | 919 | if (!whole->bd_holders) |
| 920 | whole->bd_holder = NULL; |
Tejun Heo | 6a027ef | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 921 | |
| 922 | spin_unlock(&bdev_lock); |
| 923 | |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 924 | /* |
| 925 | * If this was the last claim, remove holder link and |
| 926 | * unblock evpoll if it was a write holder. |
| 927 | */ |
Tejun Heo | 85ef06d | 2011-07-01 16:17:47 +0200 | [diff] [blame] | 928 | if (bdev_free && bdev->bd_write_holder) { |
Christoph Hellwig | 5b56b6ed | 2020-11-23 10:19:22 +0100 | [diff] [blame] | 929 | disk_unblock_events(disk); |
Tejun Heo | 85ef06d | 2011-07-01 16:17:47 +0200 | [diff] [blame] | 930 | bdev->bd_write_holder = false; |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 931 | } |
Tejun Heo | 6936217 | 2011-03-09 19:54:27 +0100 | [diff] [blame] | 932 | } |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 933 | |
Tejun Heo | 85ef06d | 2011-07-01 16:17:47 +0200 | [diff] [blame] | 934 | /* |
| 935 | * Trigger event checking and tell drivers to flush MEDIA_CHANGE |
| 936 | * event. This is to ensure detection of media removal commanded |
| 937 | * from userland - e.g. eject(1). |
| 938 | */ |
Christoph Hellwig | 5b56b6ed | 2020-11-23 10:19:22 +0100 | [diff] [blame] | 939 | disk_flush_events(disk, DISK_EVENT_MEDIA_CHANGE); |
Tejun Heo | 85ef06d | 2011-07-01 16:17:47 +0200 | [diff] [blame] | 940 | |
Christoph Hellwig | c8276b9 | 2021-05-25 08:12:58 +0200 | [diff] [blame] | 941 | if (bdev_is_partition(bdev)) |
| 942 | blkdev_put_part(bdev, mode); |
| 943 | else |
| 944 | blkdev_put_whole(bdev, mode); |
Christoph Hellwig | a869870 | 2021-05-25 08:12:56 +0200 | [diff] [blame] | 945 | mutex_unlock(&disk->open_mutex); |
| 946 | |
Ming Lei | efcf593 | 2021-11-11 10:03:43 +0800 | [diff] [blame] | 947 | module_put(disk->fops->owner); |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 948 | blkdev_put_no_open(bdev); |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 949 | } |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 950 | EXPORT_SYMBOL(blkdev_put); |
| 951 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | /** |
Matthew Wilcox (Oracle) | 0ba4566 | 2021-12-13 17:11:13 +0000 | [diff] [blame] | 953 | * lookup_bdev() - Look up a struct block_device by name. |
| 954 | * @pathname: Name of the block device in the filesystem. |
| 955 | * @dev: Pointer to the block device's dev_t, if found. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | * |
Jackie Liu | 057178c | 2021-10-21 15:13:43 +0800 | [diff] [blame] | 957 | * Lookup the block device's dev_t at @pathname in the current |
Matthew Wilcox (Oracle) | 0ba4566 | 2021-12-13 17:11:13 +0000 | [diff] [blame] | 958 | * namespace if possible and return it in @dev. |
Jackie Liu | 057178c | 2021-10-21 15:13:43 +0800 | [diff] [blame] | 959 | * |
Matthew Wilcox (Oracle) | 0ba4566 | 2021-12-13 17:11:13 +0000 | [diff] [blame] | 960 | * Context: May sleep. |
| 961 | * Return: 0 if succeeded, negative errno otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | */ |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 963 | int lookup_bdev(const char *pathname, dev_t *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | struct inode *inode; |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 966 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | int error; |
| 968 | |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 969 | if (!pathname || !*pathname) |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 970 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 972 | error = kern_path(pathname, LOOKUP_FOLLOW, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | if (error) |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 974 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | |
David Howells | bb668734 | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 976 | inode = d_backing_inode(path.dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | error = -ENOTBLK; |
| 978 | if (!S_ISBLK(inode->i_mode)) |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 979 | goto out_path_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | error = -EACCES; |
Eric W. Biederman | a2982cc | 2016-06-09 15:34:02 -0500 | [diff] [blame] | 981 | if (!may_open_dev(&path)) |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 982 | goto out_path_put; |
| 983 | |
| 984 | *dev = inode->i_rdev; |
| 985 | error = 0; |
| 986 | out_path_put: |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 987 | path_put(&path); |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 988 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | } |
Al Viro | d5686b4 | 2008-08-01 05:00:11 -0400 | [diff] [blame] | 990 | EXPORT_SYMBOL(lookup_bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | |
NeilBrown | 93b270f | 2011-02-24 17:25:47 +1100 | [diff] [blame] | 992 | int __invalidate_device(struct block_device *bdev, bool kill_dirty) |
David Howells | b71e8a4 | 2006-08-29 19:06:11 +0100 | [diff] [blame] | 993 | { |
| 994 | struct super_block *sb = get_super(bdev); |
| 995 | int res = 0; |
| 996 | |
| 997 | if (sb) { |
| 998 | /* |
| 999 | * no need to lock the super, get_super holds the |
| 1000 | * read mutex so the filesystem cannot go away |
| 1001 | * under us (->put_super runs with the write lock |
| 1002 | * hold). |
| 1003 | */ |
| 1004 | shrink_dcache_sb(sb); |
NeilBrown | 93b270f | 2011-02-24 17:25:47 +1100 | [diff] [blame] | 1005 | res = invalidate_inodes(sb, kill_dirty); |
David Howells | b71e8a4 | 2006-08-29 19:06:11 +0100 | [diff] [blame] | 1006 | drop_super(sb); |
| 1007 | } |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 1008 | invalidate_bdev(bdev); |
David Howells | b71e8a4 | 2006-08-29 19:06:11 +0100 | [diff] [blame] | 1009 | return res; |
| 1010 | } |
| 1011 | EXPORT_SYMBOL(__invalidate_device); |
Jan Kara | 5c0d6b6 | 2012-07-03 16:45:31 +0200 | [diff] [blame] | 1012 | |
Christoph Hellwig | 1e03a36 | 2021-10-19 08:25:30 +0200 | [diff] [blame] | 1013 | void sync_bdevs(bool wait) |
Jan Kara | 5c0d6b6 | 2012-07-03 16:45:31 +0200 | [diff] [blame] | 1014 | { |
| 1015 | struct inode *inode, *old_inode = NULL; |
| 1016 | |
Dave Chinner | 74278da | 2015-03-04 12:37:22 -0500 | [diff] [blame] | 1017 | spin_lock(&blockdev_superblock->s_inode_list_lock); |
Jan Kara | 5c0d6b6 | 2012-07-03 16:45:31 +0200 | [diff] [blame] | 1018 | list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) { |
| 1019 | struct address_space *mapping = inode->i_mapping; |
Rabin Vincent | af30922 | 2016-12-01 09:18:28 +0100 | [diff] [blame] | 1020 | struct block_device *bdev; |
Jan Kara | 5c0d6b6 | 2012-07-03 16:45:31 +0200 | [diff] [blame] | 1021 | |
| 1022 | spin_lock(&inode->i_lock); |
| 1023 | if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) || |
| 1024 | mapping->nrpages == 0) { |
| 1025 | spin_unlock(&inode->i_lock); |
| 1026 | continue; |
| 1027 | } |
| 1028 | __iget(inode); |
| 1029 | spin_unlock(&inode->i_lock); |
Dave Chinner | 74278da | 2015-03-04 12:37:22 -0500 | [diff] [blame] | 1030 | spin_unlock(&blockdev_superblock->s_inode_list_lock); |
Jan Kara | 5c0d6b6 | 2012-07-03 16:45:31 +0200 | [diff] [blame] | 1031 | /* |
| 1032 | * We hold a reference to 'inode' so it couldn't have been |
| 1033 | * removed from s_inodes list while we dropped the |
Dave Chinner | 74278da | 2015-03-04 12:37:22 -0500 | [diff] [blame] | 1034 | * s_inode_list_lock We cannot iput the inode now as we can |
Jan Kara | 5c0d6b6 | 2012-07-03 16:45:31 +0200 | [diff] [blame] | 1035 | * be holding the last reference and we cannot iput it under |
Dave Chinner | 74278da | 2015-03-04 12:37:22 -0500 | [diff] [blame] | 1036 | * s_inode_list_lock. So we keep the reference and iput it |
Jan Kara | 5c0d6b6 | 2012-07-03 16:45:31 +0200 | [diff] [blame] | 1037 | * later. |
| 1038 | */ |
| 1039 | iput(old_inode); |
| 1040 | old_inode = inode; |
Rabin Vincent | af30922 | 2016-12-01 09:18:28 +0100 | [diff] [blame] | 1041 | bdev = I_BDEV(inode); |
Jan Kara | 5c0d6b6 | 2012-07-03 16:45:31 +0200 | [diff] [blame] | 1042 | |
Christoph Hellwig | a869870 | 2021-05-25 08:12:56 +0200 | [diff] [blame] | 1043 | mutex_lock(&bdev->bd_disk->open_mutex); |
Christoph Hellwig | 1e03a36 | 2021-10-19 08:25:30 +0200 | [diff] [blame] | 1044 | if (!bdev->bd_openers) { |
| 1045 | ; /* skip */ |
| 1046 | } else if (wait) { |
| 1047 | /* |
| 1048 | * We keep the error status of individual mapping so |
| 1049 | * that applications can catch the writeback error using |
| 1050 | * fsync(2). See filemap_fdatawait_keep_errors() for |
| 1051 | * details. |
| 1052 | */ |
| 1053 | filemap_fdatawait_keep_errors(inode->i_mapping); |
| 1054 | } else { |
| 1055 | filemap_fdatawrite(inode->i_mapping); |
| 1056 | } |
Christoph Hellwig | a869870 | 2021-05-25 08:12:56 +0200 | [diff] [blame] | 1057 | mutex_unlock(&bdev->bd_disk->open_mutex); |
Jan Kara | 5c0d6b6 | 2012-07-03 16:45:31 +0200 | [diff] [blame] | 1058 | |
Dave Chinner | 74278da | 2015-03-04 12:37:22 -0500 | [diff] [blame] | 1059 | spin_lock(&blockdev_superblock->s_inode_list_lock); |
Jan Kara | 5c0d6b6 | 2012-07-03 16:45:31 +0200 | [diff] [blame] | 1060 | } |
Dave Chinner | 74278da | 2015-03-04 12:37:22 -0500 | [diff] [blame] | 1061 | spin_unlock(&blockdev_superblock->s_inode_list_lock); |
Jan Kara | 5c0d6b6 | 2012-07-03 16:45:31 +0200 | [diff] [blame] | 1062 | iput(old_inode); |
| 1063 | } |