]>
Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 | 2 | /* |
1da177e4 LT |
3 | * Copyright (C) 1991, 1992 Linus Torvalds |
4 | * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE | |
7b51e703 | 5 | * Copyright (C) 2016 - 2020 Christoph Hellwig |
1da177e4 LT |
6 | */ |
7 | ||
1da177e4 LT |
8 | #include <linux/init.h> |
9 | #include <linux/mm.h> | |
1da177e4 LT |
10 | #include <linux/slab.h> |
11 | #include <linux/kmod.h> | |
12 | #include <linux/major.h> | |
7db9cfd3 | 13 | #include <linux/device_cgroup.h> |
1da177e4 | 14 | #include <linux/blkdev.h> |
fe45e630 | 15 | #include <linux/blk-integrity.h> |
66114cad | 16 | #include <linux/backing-dev.h> |
1da177e4 LT |
17 | #include <linux/module.h> |
18 | #include <linux/blkpg.h> | |
b502bd11 | 19 | #include <linux/magic.h> |
1da177e4 | 20 | #include <linux/buffer_head.h> |
ff01bb48 | 21 | #include <linux/swap.h> |
811d736f | 22 | #include <linux/writeback.h> |
1da177e4 | 23 | #include <linux/mount.h> |
9030d16e | 24 | #include <linux/pseudo_fs.h> |
1da177e4 LT |
25 | #include <linux/uio.h> |
26 | #include <linux/namei.h> | |
b55d26bd | 27 | #include <linux/security.h> |
15e3d2c5 | 28 | #include <linux/part_stat.h> |
7c0f6ba6 | 29 | #include <linux/uaccess.h> |
2d985f8c | 30 | #include <linux/stat.h> |
0dca4462 CH |
31 | #include "../fs/internal.h" |
32 | #include "blk.h" | |
1da177e4 | 33 | |
ed5cc702 JK |
34 | /* Should we allow writing to mounted block devices? */ |
35 | static bool bdev_allow_write_mounted = IS_ENABLED(CONFIG_BLK_DEV_WRITE_MOUNTED); | |
36 | ||
1da177e4 LT |
37 | struct bdev_inode { |
38 | struct block_device bdev; | |
39 | struct inode vfs_inode; | |
40 | }; | |
41 | ||
42 | static inline struct bdev_inode *BDEV_I(struct inode *inode) | |
43 | { | |
44 | return container_of(inode, struct bdev_inode, vfs_inode); | |
45 | } | |
46 | ||
df65f166 AV |
47 | static inline struct inode *BD_INODE(struct block_device *bdev) |
48 | { | |
49 | return &container_of(bdev, struct bdev_inode, bdev)->vfs_inode; | |
50 | } | |
51 | ||
ff5053f6 | 52 | struct block_device *I_BDEV(struct inode *inode) |
1da177e4 LT |
53 | { |
54 | return &BDEV_I(inode)->bdev; | |
55 | } | |
1da177e4 LT |
56 | EXPORT_SYMBOL(I_BDEV); |
57 | ||
f3a60882 CB |
58 | struct block_device *file_bdev(struct file *bdev_file) |
59 | { | |
7c09a4ed | 60 | return I_BDEV(bdev_file->f_mapping->host); |
f3a60882 CB |
61 | } |
62 | EXPORT_SYMBOL(file_bdev); | |
63 | ||
dbd3ca50 | 64 | static void bdev_write_inode(struct block_device *bdev) |
564f00f6 | 65 | { |
df65f166 | 66 | struct inode *inode = BD_INODE(bdev); |
dbd3ca50 VG |
67 | int ret; |
68 | ||
564f00f6 CH |
69 | spin_lock(&inode->i_lock); |
70 | while (inode->i_state & I_DIRTY) { | |
71 | spin_unlock(&inode->i_lock); | |
dbd3ca50 | 72 | ret = write_inode_now(inode, true); |
5bf83e9a CH |
73 | if (ret) |
74 | pr_warn_ratelimited( | |
75 | "VFS: Dirty inode writeback failed for block device %pg (err=%d).\n", | |
76 | bdev, ret); | |
564f00f6 CH |
77 | spin_lock(&inode->i_lock); |
78 | } | |
79 | spin_unlock(&inode->i_lock); | |
80 | } | |
81 | ||
f9a14399 | 82 | /* Kill _all_ buffers and pagecache , dirty or not.. */ |
3373a346 | 83 | static void kill_bdev(struct block_device *bdev) |
1da177e4 | 84 | { |
224941e8 | 85 | struct address_space *mapping = bdev->bd_mapping; |
ff01bb48 | 86 | |
7716506a | 87 | if (mapping_empty(mapping)) |
f9a14399 | 88 | return; |
ff01bb48 | 89 | |
f9a14399 | 90 | invalidate_bh_lrus(); |
ff01bb48 | 91 | truncate_inode_pages(mapping, 0); |
3373a346 | 92 | } |
ff01bb48 AV |
93 | |
94 | /* Invalidate clean unused buffers and pagecache. */ | |
95 | void invalidate_bdev(struct block_device *bdev) | |
96 | { | |
224941e8 | 97 | struct address_space *mapping = bdev->bd_mapping; |
ff01bb48 | 98 | |
a5f6a6a9 AR |
99 | if (mapping->nrpages) { |
100 | invalidate_bh_lrus(); | |
101 | lru_add_drain_all(); /* make sure all lru add caches are flushed */ | |
102 | invalidate_mapping_pages(mapping, 0, -1); | |
103 | } | |
ff01bb48 AV |
104 | } |
105 | EXPORT_SYMBOL(invalidate_bdev); | |
1da177e4 | 106 | |
384d87ef JK |
107 | /* |
108 | * Drop all buffers & page cache for given bdev range. This function bails | |
109 | * with error if bdev has other exclusive owner (such as filesystem). | |
110 | */ | |
05bdb996 | 111 | int truncate_bdev_range(struct block_device *bdev, blk_mode_t mode, |
384d87ef JK |
112 | loff_t lstart, loff_t lend) |
113 | { | |
384d87ef JK |
114 | /* |
115 | * If we don't hold exclusive handle for the device, upgrade to it | |
116 | * while we discard the buffer cache to avoid discarding buffers | |
117 | * under live filesystem. | |
118 | */ | |
05bdb996 | 119 | if (!(mode & BLK_OPEN_EXCL)) { |
0718afd4 | 120 | int err = bd_prepare_to_claim(bdev, truncate_bdev_range, NULL); |
384d87ef | 121 | if (err) |
56887cff | 122 | goto invalidate; |
384d87ef | 123 | } |
37c3fc9a | 124 | |
224941e8 | 125 | truncate_inode_pages_range(bdev->bd_mapping, lstart, lend); |
05bdb996 | 126 | if (!(mode & BLK_OPEN_EXCL)) |
37c3fc9a | 127 | bd_abort_claiming(bdev, truncate_bdev_range); |
384d87ef | 128 | return 0; |
56887cff JK |
129 | |
130 | invalidate: | |
131 | /* | |
132 | * Someone else has handle exclusively open. Try invalidating instead. | |
133 | * The 'end' argument is inclusive so the rounding is safe. | |
134 | */ | |
224941e8 | 135 | return invalidate_inode_pages2_range(bdev->bd_mapping, |
56887cff JK |
136 | lstart >> PAGE_SHIFT, |
137 | lend >> PAGE_SHIFT); | |
384d87ef | 138 | } |
384d87ef | 139 | |
04906b2f JK |
140 | static void set_init_blocksize(struct block_device *bdev) |
141 | { | |
8dc932d3 | 142 | unsigned int bsize = bdev_logical_block_size(bdev); |
df65f166 | 143 | loff_t size = i_size_read(BD_INODE(bdev)); |
8dc932d3 MM |
144 | |
145 | while (bsize < PAGE_SIZE) { | |
146 | if (size & bsize) | |
147 | break; | |
148 | bsize <<= 1; | |
149 | } | |
df65f166 | 150 | BD_INODE(bdev)->i_blkbits = blksize_bits(bsize); |
3c209171 HR |
151 | mapping_set_folio_min_order(BD_INODE(bdev)->i_mapping, |
152 | get_order(bsize)); | |
04906b2f JK |
153 | } |
154 | ||
e03463d2 DW |
155 | /** |
156 | * bdev_validate_blocksize - check that this block size is acceptable | |
157 | * @bdev: blockdevice to check | |
158 | * @block_size: block size to check | |
159 | * | |
160 | * For block device users that do not use buffer heads or the block device | |
161 | * page cache, make sure that this block size can be used with the device. | |
162 | * | |
163 | * Return: On success zero is returned, negative error code on failure. | |
164 | */ | |
165 | int bdev_validate_blocksize(struct block_device *bdev, int block_size) | |
1da177e4 | 166 | { |
e03463d2 | 167 | if (blk_validate_block_size(block_size)) |
1da177e4 LT |
168 | return -EINVAL; |
169 | ||
170 | /* Size cannot be smaller than the size supported by the device */ | |
e03463d2 | 171 | if (block_size < bdev_logical_block_size(bdev)) |
1da177e4 LT |
172 | return -EINVAL; |
173 | ||
e03463d2 DW |
174 | return 0; |
175 | } | |
176 | EXPORT_SYMBOL_GPL(bdev_validate_blocksize); | |
177 | ||
178 | int set_blocksize(struct file *file, int size) | |
179 | { | |
180 | struct inode *inode = file->f_mapping->host; | |
181 | struct block_device *bdev = I_BDEV(inode); | |
182 | int ret; | |
183 | ||
184 | ret = bdev_validate_blocksize(bdev, size); | |
185 | if (ret) | |
186 | return ret; | |
187 | ||
d18a8679 AV |
188 | if (!file->private_data) |
189 | return -EINVAL; | |
190 | ||
1da177e4 | 191 | /* Don't change the size if it is same as current */ |
ead083ae | 192 | if (inode->i_blkbits != blksize_bits(size)) { |
c0e473a0 DW |
193 | /* |
194 | * Flush and truncate the pagecache before we reconfigure the | |
195 | * mapping geometry because folio sizes are variable now. If a | |
196 | * reader has already allocated a folio whose size is smaller | |
197 | * than the new min_order but invokes readahead after the new | |
198 | * min_order becomes visible, readahead will think there are | |
199 | * "zero" blocks per folio and crash. Take the inode and | |
200 | * invalidation locks to avoid racing with | |
201 | * read/write/fallocate. | |
202 | */ | |
203 | inode_lock(inode); | |
204 | filemap_invalidate_lock(inode->i_mapping); | |
205 | ||
1da177e4 | 206 | sync_blockdev(bdev); |
c0e473a0 DW |
207 | kill_bdev(bdev); |
208 | ||
ead083ae | 209 | inode->i_blkbits = blksize_bits(size); |
3c209171 | 210 | mapping_set_folio_min_order(inode->i_mapping, get_order(size)); |
1da177e4 | 211 | kill_bdev(bdev); |
c0e473a0 DW |
212 | filemap_invalidate_unlock(inode->i_mapping); |
213 | inode_unlock(inode); | |
1da177e4 LT |
214 | } |
215 | return 0; | |
216 | } | |
217 | ||
218 | EXPORT_SYMBOL(set_blocksize); | |
219 | ||
220 | int sb_set_blocksize(struct super_block *sb, int size) | |
221 | { | |
a64e5a59 LC |
222 | if (!(sb->s_type->fs_flags & FS_LBS) && size > PAGE_SIZE) |
223 | return 0; | |
ead083ae | 224 | if (set_blocksize(sb->s_bdev_file, size)) |
1da177e4 | 225 | return 0; |
47dd6753 | 226 | /* If we get here, we know size is validated */ |
1da177e4 | 227 | sb->s_blocksize = size; |
38885bd4 | 228 | sb->s_blocksize_bits = blksize_bits(size); |
1da177e4 LT |
229 | return sb->s_blocksize; |
230 | } | |
231 | ||
232 | EXPORT_SYMBOL(sb_set_blocksize); | |
233 | ||
234 | int sb_min_blocksize(struct super_block *sb, int size) | |
235 | { | |
e1defc4f | 236 | int minsize = bdev_logical_block_size(sb->s_bdev); |
1da177e4 LT |
237 | if (size < minsize) |
238 | size = minsize; | |
239 | return sb_set_blocksize(sb, size); | |
240 | } | |
241 | ||
242 | EXPORT_SYMBOL(sb_min_blocksize); | |
243 | ||
70164eb6 | 244 | int sync_blockdev_nowait(struct block_device *bdev) |
5cee5815 JK |
245 | { |
246 | if (!bdev) | |
247 | return 0; | |
224941e8 | 248 | return filemap_flush(bdev->bd_mapping); |
5cee5815 | 249 | } |
70164eb6 | 250 | EXPORT_SYMBOL_GPL(sync_blockdev_nowait); |
5cee5815 | 251 | |
585d3bc0 NP |
252 | /* |
253 | * Write out and wait upon all the dirty data associated with a block | |
254 | * device via its mapping. Does not take the superblock lock. | |
255 | */ | |
256 | int sync_blockdev(struct block_device *bdev) | |
257 | { | |
70164eb6 CH |
258 | if (!bdev) |
259 | return 0; | |
224941e8 | 260 | return filemap_write_and_wait(bdev->bd_mapping); |
585d3bc0 NP |
261 | } |
262 | EXPORT_SYMBOL(sync_blockdev); | |
263 | ||
97d6fb1b YM |
264 | int sync_blockdev_range(struct block_device *bdev, loff_t lstart, loff_t lend) |
265 | { | |
224941e8 | 266 | return filemap_write_and_wait_range(bdev->bd_mapping, |
97d6fb1b YM |
267 | lstart, lend); |
268 | } | |
269 | EXPORT_SYMBOL(sync_blockdev_range); | |
270 | ||
585d3bc0 | 271 | /** |
982c3b30 | 272 | * bdev_freeze - lock a filesystem and force it into a consistent state |
585d3bc0 NP |
273 | * @bdev: blockdevice to lock |
274 | * | |
585d3bc0 NP |
275 | * If a superblock is found on this device, we take the s_umount semaphore |
276 | * on it to make sure nobody unmounts until the snapshot creation is done. | |
277 | * The reference counter (bd_fsfreeze_count) guarantees that only the last | |
278 | * unfreeze process can unfreeze the frozen filesystem actually when multiple | |
982c3b30 CB |
279 | * freeze requests arrive simultaneously. It counts up in bdev_freeze() and |
280 | * count down in bdev_thaw(). When it becomes 0, thaw_bdev() will unfreeze | |
585d3bc0 | 281 | * actually. |
982c3b30 CB |
282 | * |
283 | * Return: On success zero is returned, negative error code on failure. | |
585d3bc0 | 284 | */ |
982c3b30 | 285 | int bdev_freeze(struct block_device *bdev) |
585d3bc0 | 286 | { |
585d3bc0 NP |
287 | int error = 0; |
288 | ||
289 | mutex_lock(&bdev->bd_fsfreeze_mutex); | |
040f04bd | 290 | |
49ef8832 CB |
291 | if (atomic_inc_return(&bdev->bd_fsfreeze_count) > 1) { |
292 | mutex_unlock(&bdev->bd_fsfreeze_mutex); | |
293 | return 0; | |
585d3bc0 | 294 | } |
040f04bd | 295 | |
49ef8832 CB |
296 | mutex_lock(&bdev->bd_holder_lock); |
297 | if (bdev->bd_holder_ops && bdev->bd_holder_ops->freeze) { | |
298 | error = bdev->bd_holder_ops->freeze(bdev); | |
299 | lockdep_assert_not_held(&bdev->bd_holder_lock); | |
300 | } else { | |
301 | mutex_unlock(&bdev->bd_holder_lock); | |
302 | error = sync_blockdev(bdev); | |
585d3bc0 | 303 | } |
040f04bd | 304 | |
49ef8832 CB |
305 | if (error) |
306 | atomic_dec(&bdev->bd_fsfreeze_count); | |
307 | ||
585d3bc0 | 308 | mutex_unlock(&bdev->bd_fsfreeze_mutex); |
040f04bd | 309 | return error; |
585d3bc0 | 310 | } |
982c3b30 | 311 | EXPORT_SYMBOL(bdev_freeze); |
585d3bc0 NP |
312 | |
313 | /** | |
982c3b30 | 314 | * bdev_thaw - unlock filesystem |
585d3bc0 | 315 | * @bdev: blockdevice to unlock |
585d3bc0 | 316 | * |
982c3b30 CB |
317 | * Unlocks the filesystem and marks it writeable again after bdev_freeze(). |
318 | * | |
319 | * Return: On success zero is returned, negative error code on failure. | |
585d3bc0 | 320 | */ |
982c3b30 | 321 | int bdev_thaw(struct block_device *bdev) |
585d3bc0 | 322 | { |
49ef8832 | 323 | int error = -EINVAL, nr_freeze; |
585d3bc0 NP |
324 | |
325 | mutex_lock(&bdev->bd_fsfreeze_mutex); | |
49ef8832 CB |
326 | |
327 | /* | |
328 | * If this returns < 0 it means that @bd_fsfreeze_count was | |
329 | * already 0 and no decrement was performed. | |
330 | */ | |
331 | nr_freeze = atomic_dec_if_positive(&bdev->bd_fsfreeze_count); | |
332 | if (nr_freeze < 0) | |
18e9e510 | 333 | goto out; |
4504230a CH |
334 | |
335 | error = 0; | |
49ef8832 | 336 | if (nr_freeze > 0) |
18e9e510 | 337 | goto out; |
4504230a | 338 | |
49ef8832 CB |
339 | mutex_lock(&bdev->bd_holder_lock); |
340 | if (bdev->bd_holder_ops && bdev->bd_holder_ops->thaw) { | |
341 | error = bdev->bd_holder_ops->thaw(bdev); | |
342 | lockdep_assert_not_held(&bdev->bd_holder_lock); | |
343 | } else { | |
344 | mutex_unlock(&bdev->bd_holder_lock); | |
345 | } | |
4504230a | 346 | |
997198ba | 347 | if (error) |
49ef8832 | 348 | atomic_inc(&bdev->bd_fsfreeze_count); |
18e9e510 | 349 | out: |
585d3bc0 | 350 | mutex_unlock(&bdev->bd_fsfreeze_mutex); |
997198ba | 351 | return error; |
585d3bc0 | 352 | } |
982c3b30 | 353 | EXPORT_SYMBOL(bdev_thaw); |
585d3bc0 | 354 | |
1da177e4 LT |
355 | /* |
356 | * pseudo-fs | |
357 | */ | |
358 | ||
74e6464a | 359 | static __cacheline_aligned_in_smp DEFINE_MUTEX(bdev_lock); |
68279f9c | 360 | static struct kmem_cache *bdev_cachep __ro_after_init; |
1da177e4 LT |
361 | |
362 | static struct inode *bdev_alloc_inode(struct super_block *sb) | |
363 | { | |
fd60b288 | 364 | struct bdev_inode *ei = alloc_inode_sb(sb, bdev_cachep, GFP_KERNEL); |
2d2f6f1b | 365 | |
1da177e4 LT |
366 | if (!ei) |
367 | return NULL; | |
2d2f6f1b | 368 | memset(&ei->bdev, 0, sizeof(ei->bdev)); |
b55d26bd DB |
369 | |
370 | if (security_bdev_alloc(&ei->bdev)) { | |
371 | kmem_cache_free(bdev_cachep, ei); | |
372 | return NULL; | |
373 | } | |
1da177e4 LT |
374 | return &ei->vfs_inode; |
375 | } | |
376 | ||
41149cb0 | 377 | static void bdev_free_inode(struct inode *inode) |
1da177e4 | 378 | { |
15e3d2c5 CH |
379 | struct block_device *bdev = I_BDEV(inode); |
380 | ||
381 | free_percpu(bdev->bd_stats); | |
231926db | 382 | kfree(bdev->bd_meta_info); |
b55d26bd | 383 | security_bdev_free(bdev); |
15e3d2c5 | 384 | |
889c05cc CH |
385 | if (!bdev_is_partition(bdev)) { |
386 | if (bdev->bd_disk && bdev->bd_disk->bdi) | |
387 | bdi_put(bdev->bd_disk->bdi); | |
340e8457 | 388 | kfree(bdev->bd_disk); |
889c05cc | 389 | } |
9451aa0a CH |
390 | |
391 | if (MAJOR(bdev->bd_dev) == BLOCK_EXT_MAJOR) | |
392 | blk_free_ext_minor(MINOR(bdev->bd_dev)); | |
393 | ||
41149cb0 | 394 | kmem_cache_free(bdev_cachep, BDEV_I(inode)); |
fa0d7e3d NP |
395 | } |
396 | ||
e6cb5382 | 397 | static void init_once(void *data) |
1da177e4 | 398 | { |
e6cb5382 | 399 | struct bdev_inode *ei = data; |
1da177e4 | 400 | |
a35afb83 | 401 | inode_init_once(&ei->vfs_inode); |
1da177e4 LT |
402 | } |
403 | ||
b57922d9 | 404 | static void bdev_evict_inode(struct inode *inode) |
1da177e4 | 405 | { |
91b0abe3 | 406 | truncate_inode_pages_final(&inode->i_data); |
b57922d9 | 407 | invalidate_inode_buffers(inode); /* is it needed here? */ |
dbd5768f | 408 | clear_inode(inode); |
1da177e4 LT |
409 | } |
410 | ||
ee9b6d61 | 411 | static const struct super_operations bdev_sops = { |
1da177e4 LT |
412 | .statfs = simple_statfs, |
413 | .alloc_inode = bdev_alloc_inode, | |
41149cb0 | 414 | .free_inode = bdev_free_inode, |
1da177e4 | 415 | .drop_inode = generic_delete_inode, |
b57922d9 | 416 | .evict_inode = bdev_evict_inode, |
1da177e4 LT |
417 | }; |
418 | ||
9030d16e | 419 | static int bd_init_fs_context(struct fs_context *fc) |
1da177e4 | 420 | { |
9030d16e DH |
421 | struct pseudo_fs_context *ctx = init_pseudo(fc, BDEVFS_MAGIC); |
422 | if (!ctx) | |
423 | return -ENOMEM; | |
424 | fc->s_iflags |= SB_I_CGROUPWB; | |
425 | ctx->ops = &bdev_sops; | |
426 | return 0; | |
1da177e4 LT |
427 | } |
428 | ||
429 | static struct file_system_type bd_type = { | |
430 | .name = "bdev", | |
9030d16e | 431 | .init_fs_context = bd_init_fs_context, |
1da177e4 LT |
432 | .kill_sb = kill_anon_super, |
433 | }; | |
434 | ||
68279f9c | 435 | struct super_block *blockdev_superblock __ro_after_init; |
d9c23321 | 436 | static struct vfsmount *blockdev_mnt __ro_after_init; |
a212b105 | 437 | EXPORT_SYMBOL_GPL(blockdev_superblock); |
1da177e4 LT |
438 | |
439 | void __init bdev_cache_init(void) | |
440 | { | |
441 | int err; | |
c2acf7b9 | 442 | |
1da177e4 | 443 | bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode), |
fffb60f9 | 444 | 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT| |
82c6515d | 445 | SLAB_ACCOUNT|SLAB_PANIC), |
20c2df83 | 446 | init_once); |
1da177e4 LT |
447 | err = register_filesystem(&bd_type); |
448 | if (err) | |
449 | panic("Cannot register bdev pseudo-fs"); | |
f3a60882 CB |
450 | blockdev_mnt = kern_mount(&bd_type); |
451 | if (IS_ERR(blockdev_mnt)) | |
1da177e4 | 452 | panic("Cannot create bdev pseudo-fs"); |
f3a60882 | 453 | blockdev_superblock = blockdev_mnt->mnt_sb; /* For writeback */ |
1da177e4 LT |
454 | } |
455 | ||
22ae8ce8 | 456 | struct block_device *bdev_alloc(struct gendisk *disk, u8 partno) |
1da177e4 LT |
457 | { |
458 | struct block_device *bdev; | |
459 | struct inode *inode; | |
460 | ||
22ae8ce8 | 461 | inode = new_inode(blockdev_superblock); |
1da177e4 LT |
462 | if (!inode) |
463 | return NULL; | |
22ae8ce8 CH |
464 | inode->i_mode = S_IFBLK; |
465 | inode->i_rdev = 0; | |
466 | inode->i_data.a_ops = &def_blk_aops; | |
467 | mapping_set_gfp_mask(&inode->i_data, GFP_USER); | |
468 | ||
469 | bdev = I_BDEV(inode); | |
e6cb5382 | 470 | mutex_init(&bdev->bd_fsfreeze_mutex); |
22ae8ce8 | 471 | spin_lock_init(&bdev->bd_size_lock); |
0718afd4 | 472 | mutex_init(&bdev->bd_holder_lock); |
1116b9fa | 473 | atomic_set(&bdev->__bd_flags, partno); |
e33aef2c | 474 | bdev->bd_mapping = &inode->i_data; |
17220ca5 | 475 | bdev->bd_queue = disk->queue; |
ac2b6f9d AV |
476 | if (partno && bdev_test_flag(disk->part0, BD_HAS_SUBMIT_BIO)) |
477 | bdev_set_flag(bdev, BD_HAS_SUBMIT_BIO); | |
15e3d2c5 CH |
478 | bdev->bd_stats = alloc_percpu(struct disk_stats); |
479 | if (!bdev->bd_stats) { | |
480 | iput(inode); | |
481 | return NULL; | |
482 | } | |
06cc978d | 483 | bdev->bd_disk = disk; |
22ae8ce8 CH |
484 | return bdev; |
485 | } | |
1da177e4 | 486 | |
83794367 DLM |
487 | void bdev_set_nr_sectors(struct block_device *bdev, sector_t sectors) |
488 | { | |
489 | spin_lock(&bdev->bd_size_lock); | |
df65f166 | 490 | i_size_write(BD_INODE(bdev), (loff_t)sectors << SECTOR_SHIFT); |
83794367 DLM |
491 | bdev->bd_nr_sectors = sectors; |
492 | spin_unlock(&bdev->bd_size_lock); | |
493 | } | |
494 | ||
22ae8ce8 CH |
495 | void bdev_add(struct block_device *bdev, dev_t dev) |
496 | { | |
df65f166 | 497 | struct inode *inode = BD_INODE(bdev); |
1898efcd | 498 | if (bdev_stable_writes(bdev)) |
224941e8 | 499 | mapping_set_stable_writes(bdev->bd_mapping); |
22ae8ce8 | 500 | bdev->bd_dev = dev; |
df65f166 AV |
501 | inode->i_rdev = dev; |
502 | inode->i_ino = dev; | |
503 | insert_inode_hash(inode); | |
22ae8ce8 | 504 | } |
1da177e4 | 505 | |
2638c208 AV |
506 | void bdev_unhash(struct block_device *bdev) |
507 | { | |
df65f166 | 508 | remove_inode_hash(BD_INODE(bdev)); |
2638c208 AV |
509 | } |
510 | ||
511 | void bdev_drop(struct block_device *bdev) | |
512 | { | |
df65f166 | 513 | iput(BD_INODE(bdev)); |
22ae8ce8 | 514 | } |
1da177e4 | 515 | |
1da177e4 LT |
516 | long nr_blockdev_pages(void) |
517 | { | |
1008fe6d | 518 | struct inode *inode; |
1da177e4 | 519 | long ret = 0; |
1008fe6d CH |
520 | |
521 | spin_lock(&blockdev_superblock->s_inode_list_lock); | |
522 | list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) | |
523 | ret += inode->i_mapping->nrpages; | |
524 | spin_unlock(&blockdev_superblock->s_inode_list_lock); | |
525 | ||
1da177e4 LT |
526 | return ret; |
527 | } | |
528 | ||
1a3cbbc5 TH |
529 | /** |
530 | * bd_may_claim - test whether a block device can be claimed | |
531 | * @bdev: block device of interest | |
1a3cbbc5 | 532 | * @holder: holder trying to claim @bdev |
0718afd4 | 533 | * @hops: holder ops |
1a3cbbc5 | 534 | * |
25985edc | 535 | * Test whether @bdev can be claimed by @holder. |
1a3cbbc5 | 536 | * |
1a3cbbc5 TH |
537 | * RETURNS: |
538 | * %true if @bdev can be claimed, %false otherwise. | |
539 | */ | |
0718afd4 CH |
540 | static bool bd_may_claim(struct block_device *bdev, void *holder, |
541 | const struct blk_holder_ops *hops) | |
ae5f855e CH |
542 | { |
543 | struct block_device *whole = bdev_whole(bdev); | |
544 | ||
74e6464a CH |
545 | lockdep_assert_held(&bdev_lock); |
546 | ||
ae5f855e CH |
547 | if (bdev->bd_holder) { |
548 | /* | |
549 | * The same holder can always re-claim. | |
550 | */ | |
0718afd4 CH |
551 | if (bdev->bd_holder == holder) { |
552 | if (WARN_ON_ONCE(bdev->bd_holder_ops != hops)) | |
553 | return false; | |
ae5f855e | 554 | return true; |
0718afd4 | 555 | } |
ae5f855e CH |
556 | return false; |
557 | } | |
558 | ||
559 | /* | |
560 | * If the whole devices holder is set to bd_may_claim, a partition on | |
561 | * the device is claimed, but not the whole device. | |
562 | */ | |
563 | if (whole != bdev && | |
564 | whole->bd_holder && whole->bd_holder != bd_may_claim) | |
565 | return false; | |
566 | return true; | |
1a3cbbc5 TH |
567 | } |
568 | ||
6b4517a7 | 569 | /** |
58e46ed9 | 570 | * bd_prepare_to_claim - claim a block device |
6b4517a7 | 571 | * @bdev: block device of interest |
6b4517a7 | 572 | * @holder: holder trying to claim @bdev |
0718afd4 | 573 | * @hops: holder ops. |
6b4517a7 | 574 | * |
58e46ed9 CH |
575 | * Claim @bdev. This function fails if @bdev is already claimed by another |
576 | * holder and waits if another claiming is in progress. return, the caller | |
577 | * has ownership of bd_claiming and bd_holder[s]. | |
6b4517a7 TH |
578 | * |
579 | * RETURNS: | |
580 | * 0 if @bdev can be claimed, -EBUSY otherwise. | |
581 | */ | |
0718afd4 CH |
582 | int bd_prepare_to_claim(struct block_device *bdev, void *holder, |
583 | const struct blk_holder_ops *hops) | |
6b4517a7 | 584 | { |
37c3fc9a CH |
585 | struct block_device *whole = bdev_whole(bdev); |
586 | ||
587 | if (WARN_ON_ONCE(!holder)) | |
588 | return -EINVAL; | |
6b4517a7 | 589 | retry: |
74e6464a | 590 | mutex_lock(&bdev_lock); |
6b4517a7 | 591 | /* if someone else claimed, fail */ |
0718afd4 | 592 | if (!bd_may_claim(bdev, holder, hops)) { |
74e6464a | 593 | mutex_unlock(&bdev_lock); |
6b4517a7 | 594 | return -EBUSY; |
58e46ed9 | 595 | } |
6b4517a7 | 596 | |
e75aa858 TH |
597 | /* if claiming is already in progress, wait for it to finish */ |
598 | if (whole->bd_claiming) { | |
aa3d8a36 | 599 | wait_queue_head_t *wq = __var_waitqueue(&whole->bd_claiming); |
6b4517a7 TH |
600 | DEFINE_WAIT(wait); |
601 | ||
602 | prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE); | |
74e6464a | 603 | mutex_unlock(&bdev_lock); |
6b4517a7 TH |
604 | schedule(); |
605 | finish_wait(wq, &wait); | |
6b4517a7 TH |
606 | goto retry; |
607 | } | |
608 | ||
609 | /* yay, all mine */ | |
58e46ed9 | 610 | whole->bd_claiming = holder; |
74e6464a | 611 | mutex_unlock(&bdev_lock); |
6b4517a7 TH |
612 | return 0; |
613 | } | |
ecbe6bc0 | 614 | EXPORT_SYMBOL_GPL(bd_prepare_to_claim); /* only for the loop driver */ |
6b4517a7 | 615 | |
89e524c0 JK |
616 | static void bd_clear_claiming(struct block_device *whole, void *holder) |
617 | { | |
618 | lockdep_assert_held(&bdev_lock); | |
619 | /* tell others that we're done */ | |
620 | BUG_ON(whole->bd_claiming != holder); | |
621 | whole->bd_claiming = NULL; | |
aa3d8a36 | 622 | wake_up_var(&whole->bd_claiming); |
89e524c0 JK |
623 | } |
624 | ||
625 | /** | |
626 | * bd_finish_claiming - finish claiming of a block device | |
627 | * @bdev: block device of interest | |
89e524c0 | 628 | * @holder: holder that has claimed @bdev |
017fb83e | 629 | * @hops: block device holder operations |
89e524c0 JK |
630 | * |
631 | * Finish exclusive open of a block device. Mark the device as exlusively | |
632 | * open by the holder and wake up all waiters for exclusive open to finish. | |
633 | */ | |
0718afd4 CH |
634 | static void bd_finish_claiming(struct block_device *bdev, void *holder, |
635 | const struct blk_holder_ops *hops) | |
89e524c0 | 636 | { |
37c3fc9a CH |
637 | struct block_device *whole = bdev_whole(bdev); |
638 | ||
74e6464a | 639 | mutex_lock(&bdev_lock); |
0718afd4 | 640 | BUG_ON(!bd_may_claim(bdev, holder, hops)); |
89e524c0 JK |
641 | /* |
642 | * Note that for a whole device bd_holders will be incremented twice, | |
643 | * and bd_holder will be set to bd_may_claim before being set to holder | |
644 | */ | |
645 | whole->bd_holders++; | |
646 | whole->bd_holder = bd_may_claim; | |
647 | bdev->bd_holders++; | |
0718afd4 | 648 | mutex_lock(&bdev->bd_holder_lock); |
89e524c0 | 649 | bdev->bd_holder = holder; |
0718afd4 CH |
650 | bdev->bd_holder_ops = hops; |
651 | mutex_unlock(&bdev->bd_holder_lock); | |
89e524c0 | 652 | bd_clear_claiming(whole, holder); |
74e6464a | 653 | mutex_unlock(&bdev_lock); |
89e524c0 | 654 | } |
89e524c0 JK |
655 | |
656 | /** | |
657 | * bd_abort_claiming - abort claiming of a block device | |
658 | * @bdev: block device of interest | |
89e524c0 JK |
659 | * @holder: holder that has claimed @bdev |
660 | * | |
661 | * Abort claiming of a block device when the exclusive open failed. This can be | |
662 | * also used when exclusive open is not actually desired and we just needed | |
663 | * to block other exclusive openers for a while. | |
664 | */ | |
37c3fc9a | 665 | void bd_abort_claiming(struct block_device *bdev, void *holder) |
89e524c0 | 666 | { |
74e6464a | 667 | mutex_lock(&bdev_lock); |
37c3fc9a | 668 | bd_clear_claiming(bdev_whole(bdev), holder); |
74e6464a | 669 | mutex_unlock(&bdev_lock); |
89e524c0 JK |
670 | } |
671 | EXPORT_SYMBOL(bd_abort_claiming); | |
6b4517a7 | 672 | |
2736e8ee | 673 | static void bd_end_claim(struct block_device *bdev, void *holder) |
0783b1a7 CH |
674 | { |
675 | struct block_device *whole = bdev_whole(bdev); | |
676 | bool unblock = false; | |
677 | ||
678 | /* | |
679 | * Release a claim on the device. The holder fields are protected with | |
680 | * bdev_lock. open_mutex is used to synchronize disk_holder unlinking. | |
681 | */ | |
74e6464a | 682 | mutex_lock(&bdev_lock); |
2736e8ee | 683 | WARN_ON_ONCE(bdev->bd_holder != holder); |
0783b1a7 CH |
684 | WARN_ON_ONCE(--bdev->bd_holders < 0); |
685 | WARN_ON_ONCE(--whole->bd_holders < 0); | |
686 | if (!bdev->bd_holders) { | |
0718afd4 | 687 | mutex_lock(&bdev->bd_holder_lock); |
0783b1a7 | 688 | bdev->bd_holder = NULL; |
0718afd4 CH |
689 | bdev->bd_holder_ops = NULL; |
690 | mutex_unlock(&bdev->bd_holder_lock); | |
4c80105e | 691 | if (bdev_test_flag(bdev, BD_WRITE_HOLDER)) |
0783b1a7 CH |
692 | unblock = true; |
693 | } | |
694 | if (!whole->bd_holders) | |
695 | whole->bd_holder = NULL; | |
74e6464a | 696 | mutex_unlock(&bdev_lock); |
0783b1a7 CH |
697 | |
698 | /* | |
699 | * If this was the last claim, remove holder link and unblock evpoll if | |
700 | * it was a write holder. | |
701 | */ | |
702 | if (unblock) { | |
703 | disk_unblock_events(bdev->bd_disk); | |
4c80105e | 704 | bdev_clear_flag(bdev, BD_WRITE_HOLDER); |
0783b1a7 CH |
705 | } |
706 | } | |
707 | ||
c8276b95 CH |
708 | static void blkdev_flush_mapping(struct block_device *bdev) |
709 | { | |
710 | WARN_ON_ONCE(bdev->bd_holders); | |
711 | sync_blockdev(bdev); | |
712 | kill_bdev(bdev); | |
713 | bdev_write_inode(bdev); | |
714 | } | |
37be4124 | 715 | |
752863bd CH |
716 | static void blkdev_put_whole(struct block_device *bdev) |
717 | { | |
718 | if (atomic_dec_and_test(&bdev->bd_openers)) | |
719 | blkdev_flush_mapping(bdev); | |
720 | if (bdev->bd_disk->fops->release) | |
721 | bdev->bd_disk->fops->release(bdev->bd_disk); | |
722 | } | |
723 | ||
05bdb996 | 724 | static int blkdev_get_whole(struct block_device *bdev, blk_mode_t mode) |
a1548b67 | 725 | { |
142fe8f4 | 726 | struct gendisk *disk = bdev->bd_disk; |
af22fef3 | 727 | int ret; |
a1548b67 | 728 | |
362529d9 | 729 | if (disk->fops->open) { |
d32e2bf8 | 730 | ret = disk->fops->open(disk, mode); |
362529d9 CH |
731 | if (ret) { |
732 | /* avoid ghost partitions on a removed medium */ | |
733 | if (ret == -ENOMEDIUM && | |
734 | test_bit(GD_NEED_PART_SCAN, &disk->state)) | |
0384264e | 735 | bdev_disk_changed(disk, true); |
362529d9 CH |
736 | return ret; |
737 | } | |
d981cb5b | 738 | } |
a1548b67 | 739 | |
9acf381f | 740 | if (!atomic_read(&bdev->bd_openers)) |
362529d9 | 741 | set_init_blocksize(bdev); |
9acf381f | 742 | atomic_inc(&bdev->bd_openers); |
752863bd CH |
743 | if (test_bit(GD_NEED_PART_SCAN, &disk->state)) { |
744 | /* | |
745 | * Only return scanning errors if we are called from contexts | |
746 | * that explicitly want them, e.g. the BLKRRPART ioctl. | |
747 | */ | |
748 | ret = bdev_disk_changed(disk, false); | |
749 | if (ret && (mode & BLK_OPEN_STRICT_SCAN)) { | |
750 | blkdev_put_whole(bdev); | |
751 | return ret; | |
752 | } | |
753 | } | |
483546c1 | 754 | return 0; |
362529d9 | 755 | } |
a1548b67 | 756 | |
05bdb996 | 757 | static int blkdev_get_part(struct block_device *part, blk_mode_t mode) |
1da177e4 | 758 | { |
362529d9 | 759 | struct gendisk *disk = part->bd_disk; |
362529d9 | 760 | int ret; |
5b642d8b | 761 | |
9d3b8813 | 762 | ret = blkdev_get_whole(bdev_whole(part), mode); |
a8698707 | 763 | if (ret) |
9d3b8813 | 764 | return ret; |
7e69723f | 765 | |
362529d9 CH |
766 | ret = -ENXIO; |
767 | if (!bdev_nr_sectors(part)) | |
768 | goto out_blkdev_put; | |
7e69723f | 769 | |
9d1c9287 CH |
770 | if (!atomic_read(&part->bd_openers)) { |
771 | disk->open_partitions++; | |
772 | set_init_blocksize(part); | |
773 | } | |
9acf381f | 774 | atomic_inc(&part->bd_openers); |
1da177e4 | 775 | return 0; |
5a023cdb | 776 | |
362529d9 | 777 | out_blkdev_put: |
ae220766 | 778 | blkdev_put_whole(bdev_whole(part)); |
362529d9 | 779 | return ret; |
1da177e4 | 780 | } |
5b56b6ed | 781 | |
a56aefca CB |
782 | int bdev_permission(dev_t dev, blk_mode_t mode, void *holder) |
783 | { | |
784 | int ret; | |
785 | ||
786 | ret = devcgroup_check_permission(DEVCG_DEV_BLOCK, | |
787 | MAJOR(dev), MINOR(dev), | |
788 | ((mode & BLK_OPEN_READ) ? DEVCG_ACC_READ : 0) | | |
789 | ((mode & BLK_OPEN_WRITE) ? DEVCG_ACC_WRITE : 0)); | |
790 | if (ret) | |
791 | return ret; | |
792 | ||
793 | /* Blocking writes requires exclusive opener */ | |
794 | if (mode & BLK_OPEN_RESTRICT_WRITES && !holder) | |
795 | return -EINVAL; | |
796 | ||
ab838b3f CB |
797 | /* |
798 | * We're using error pointers to indicate to ->release() when we | |
799 | * failed to open that block device. Also this doesn't make sense. | |
800 | */ | |
801 | if (WARN_ON_ONCE(IS_ERR(holder))) | |
802 | return -EINVAL; | |
803 | ||
a56aefca CB |
804 | return 0; |
805 | } | |
806 | ||
ae220766 | 807 | static void blkdev_put_part(struct block_device *part) |
c8276b95 CH |
808 | { |
809 | struct block_device *whole = bdev_whole(part); | |
03e26279 | 810 | |
9d1c9287 CH |
811 | if (atomic_dec_and_test(&part->bd_openers)) { |
812 | blkdev_flush_mapping(part); | |
813 | whole->bd_disk->open_partitions--; | |
814 | } | |
ae220766 | 815 | blkdev_put_whole(whole); |
1da177e4 LT |
816 | } |
817 | ||
5f33b522 | 818 | struct block_device *blkdev_get_no_open(dev_t dev, bool autoload) |
22ae8ce8 CH |
819 | { |
820 | struct block_device *bdev; | |
9d3b8813 | 821 | struct inode *inode; |
22ae8ce8 | 822 | |
9d3b8813 | 823 | inode = ilookup(blockdev_superblock, dev); |
5f33b522 | 824 | if (!inode && autoload && IS_ENABLED(CONFIG_BLOCK_LEGACY_AUTOLOAD)) { |
22ae8ce8 | 825 | blk_request_module(dev); |
9d3b8813 | 826 | inode = ilookup(blockdev_superblock, dev); |
fbdee71b CH |
827 | if (inode) |
828 | pr_warn_ratelimited( | |
451f0b6f | 829 | "block device autoloading is deprecated and will be removed.\n"); |
22ae8ce8 | 830 | } |
fbdee71b CH |
831 | if (!inode) |
832 | return NULL; | |
22ae8ce8 | 833 | |
9d3b8813 CH |
834 | /* switch from the inode reference to a device mode one: */ |
835 | bdev = &BDEV_I(inode)->bdev; | |
836 | if (!kobject_get_unless_zero(&bdev->bd_device.kobj)) | |
837 | bdev = NULL; | |
838 | iput(inode); | |
22ae8ce8 | 839 | return bdev; |
22ae8ce8 CH |
840 | } |
841 | ||
842 | void blkdev_put_no_open(struct block_device *bdev) | |
843 | { | |
9d3b8813 | 844 | put_device(&bdev->bd_device); |
22ae8ce8 | 845 | } |
ed5cc702 JK |
846 | |
847 | static bool bdev_writes_blocked(struct block_device *bdev) | |
848 | { | |
3ff56e28 | 849 | return bdev->bd_writers < 0; |
ed5cc702 JK |
850 | } |
851 | ||
852 | static void bdev_block_writes(struct block_device *bdev) | |
853 | { | |
3ff56e28 | 854 | bdev->bd_writers--; |
ed5cc702 JK |
855 | } |
856 | ||
857 | static void bdev_unblock_writes(struct block_device *bdev) | |
858 | { | |
3ff56e28 | 859 | bdev->bd_writers++; |
ed5cc702 JK |
860 | } |
861 | ||
862 | static bool bdev_may_open(struct block_device *bdev, blk_mode_t mode) | |
863 | { | |
864 | if (bdev_allow_write_mounted) | |
865 | return true; | |
866 | /* Writes blocked? */ | |
867 | if (mode & BLK_OPEN_WRITE && bdev_writes_blocked(bdev)) | |
868 | return false; | |
869 | if (mode & BLK_OPEN_RESTRICT_WRITES && bdev->bd_writers > 0) | |
870 | return false; | |
871 | return true; | |
872 | } | |
873 | ||
874 | static void bdev_claim_write_access(struct block_device *bdev, blk_mode_t mode) | |
875 | { | |
876 | if (bdev_allow_write_mounted) | |
877 | return; | |
878 | ||
879 | /* Claim exclusive or shared write access. */ | |
880 | if (mode & BLK_OPEN_RESTRICT_WRITES) | |
881 | bdev_block_writes(bdev); | |
882 | else if (mode & BLK_OPEN_WRITE) | |
883 | bdev->bd_writers++; | |
884 | } | |
885 | ||
22650a99 CB |
886 | static inline bool bdev_unclaimed(const struct file *bdev_file) |
887 | { | |
888 | return bdev_file->private_data == BDEV_I(bdev_file->f_mapping->host); | |
889 | } | |
890 | ||
ab838b3f | 891 | static void bdev_yield_write_access(struct file *bdev_file) |
ed5cc702 | 892 | { |
321de651 CB |
893 | struct block_device *bdev; |
894 | ||
ed5cc702 JK |
895 | if (bdev_allow_write_mounted) |
896 | return; | |
897 | ||
22650a99 CB |
898 | if (bdev_unclaimed(bdev_file)) |
899 | return; | |
900 | ||
321de651 | 901 | bdev = file_bdev(bdev_file); |
ddd65e19 CB |
902 | |
903 | if (bdev_file->f_mode & FMODE_WRITE_RESTRICTED) | |
904 | bdev_unblock_writes(bdev); | |
905 | else if (bdev_file->f_mode & FMODE_WRITE) | |
906 | bdev->bd_writers--; | |
ed5cc702 JK |
907 | } |
908 | ||
d4d77629 | 909 | /** |
a56aefca CB |
910 | * bdev_open - open a block device |
911 | * @bdev: block device to open | |
05bdb996 | 912 | * @mode: open mode (BLK_OPEN_*) |
d4d77629 | 913 | * @holder: exclusive holder identifier |
0718afd4 | 914 | * @hops: holder operations |
a56aefca | 915 | * @bdev_file: file for the block device |
d4d77629 | 916 | * |
a56aefca CB |
917 | * Open the block device. If @holder is not %NULL, the block device is opened |
918 | * with exclusive access. Exclusive opens may nest for the same @holder. | |
d4d77629 TH |
919 | * |
920 | * CONTEXT: | |
921 | * Might sleep. | |
922 | * | |
923 | * RETURNS: | |
a56aefca | 924 | * zero on success, -errno on failure. |
d4d77629 | 925 | */ |
a56aefca CB |
926 | int bdev_open(struct block_device *bdev, blk_mode_t mode, void *holder, |
927 | const struct blk_holder_ops *hops, struct file *bdev_file) | |
1da177e4 | 928 | { |
cd34758c | 929 | bool unblock_events = true; |
a56aefca | 930 | struct gendisk *disk = bdev->bd_disk; |
5b56b6ed | 931 | int ret; |
e525fd89 | 932 | |
2736e8ee | 933 | if (holder) { |
05bdb996 | 934 | mode |= BLK_OPEN_EXCL; |
0718afd4 | 935 | ret = bd_prepare_to_claim(bdev, holder, hops); |
5b56b6ed | 936 | if (ret) |
ab838b3f | 937 | return ret; |
2736e8ee | 938 | } else { |
ab838b3f CB |
939 | if (WARN_ON_ONCE(mode & BLK_OPEN_EXCL)) |
940 | return -EIO; | |
5b56b6ed CH |
941 | } |
942 | ||
943 | disk_block_events(disk); | |
944 | ||
a8698707 | 945 | mutex_lock(&disk->open_mutex); |
362529d9 | 946 | ret = -ENXIO; |
50b4aecf | 947 | if (!disk_live(disk)) |
362529d9 | 948 | goto abort_claiming; |
efcf5932 ML |
949 | if (!try_module_get(disk->fops->owner)) |
950 | goto abort_claiming; | |
ed5cc702 JK |
951 | ret = -EBUSY; |
952 | if (!bdev_may_open(bdev, mode)) | |
9617cd6f | 953 | goto put_module; |
362529d9 CH |
954 | if (bdev_is_partition(bdev)) |
955 | ret = blkdev_get_part(bdev, mode); | |
956 | else | |
957 | ret = blkdev_get_whole(bdev, mode); | |
22ae8ce8 | 958 | if (ret) |
efcf5932 | 959 | goto put_module; |
ed5cc702 | 960 | bdev_claim_write_access(bdev, mode); |
2736e8ee | 961 | if (holder) { |
0718afd4 | 962 | bd_finish_claiming(bdev, holder, hops); |
5b56b6ed CH |
963 | |
964 | /* | |
965 | * Block event polling for write claims if requested. Any write | |
966 | * holder makes the write_holder state stick until all are | |
967 | * released. This is good enough and tracking individual | |
968 | * writeable reference is too fragile given the way @mode is | |
969 | * used in blkdev_get/put(). | |
970 | */ | |
4c80105e AV |
971 | if ((mode & BLK_OPEN_WRITE) && |
972 | !bdev_test_flag(bdev, BD_WRITE_HOLDER) && | |
1545e0b4 | 973 | (disk->event_flags & DISK_EVENT_FLAG_BLOCK_ON_EXCL_WRITE)) { |
4c80105e | 974 | bdev_set_flag(bdev, BD_WRITE_HOLDER); |
5b56b6ed CH |
975 | unblock_events = false; |
976 | } | |
977 | } | |
a8698707 | 978 | mutex_unlock(&disk->open_mutex); |
5b56b6ed CH |
979 | |
980 | if (unblock_events) | |
981 | disk_unblock_events(disk); | |
a56aefca CB |
982 | |
983 | bdev_file->f_flags |= O_LARGEFILE; | |
210a03c9 | 984 | bdev_file->f_mode |= FMODE_CAN_ODIRECT; |
a56aefca CB |
985 | if (bdev_nowait(bdev)) |
986 | bdev_file->f_mode |= FMODE_NOWAIT; | |
ddd65e19 CB |
987 | if (mode & BLK_OPEN_RESTRICT_WRITES) |
988 | bdev_file->f_mode |= FMODE_WRITE_RESTRICTED; | |
224941e8 | 989 | bdev_file->f_mapping = bdev->bd_mapping; |
a56aefca | 990 | bdev_file->f_wb_err = filemap_sample_wb_err(bdev_file->f_mapping); |
ab838b3f | 991 | bdev_file->private_data = holder; |
a56aefca CB |
992 | |
993 | return 0; | |
efcf5932 ML |
994 | put_module: |
995 | module_put(disk->fops->owner); | |
22ae8ce8 | 996 | abort_claiming: |
2736e8ee | 997 | if (holder) |
37c3fc9a | 998 | bd_abort_claiming(bdev, holder); |
a8698707 | 999 | mutex_unlock(&disk->open_mutex); |
22ae8ce8 | 1000 | disk_unblock_events(disk); |
a56aefca | 1001 | return ret; |
37be4124 | 1002 | } |
e719b4d1 | 1003 | |
f3a60882 CB |
1004 | /* |
1005 | * If BLK_OPEN_WRITE_IOCTL is set then this is a historical quirk | |
1006 | * associated with the floppy driver where it has allowed ioctls if the | |
1007 | * file was opened for writing, but does not allow reads or writes. | |
1008 | * Make sure that this quirk is reflected in @f_flags. | |
1009 | * | |
1010 | * It can also happen if a block device is opened as O_RDWR | O_WRONLY. | |
1011 | */ | |
1012 | static unsigned blk_to_file_flags(blk_mode_t mode) | |
1013 | { | |
1014 | unsigned int flags = 0; | |
1015 | ||
1016 | if ((mode & (BLK_OPEN_READ | BLK_OPEN_WRITE)) == | |
1017 | (BLK_OPEN_READ | BLK_OPEN_WRITE)) | |
1018 | flags |= O_RDWR; | |
1019 | else if (mode & BLK_OPEN_WRITE_IOCTL) | |
1020 | flags |= O_RDWR | O_WRONLY; | |
1021 | else if (mode & BLK_OPEN_WRITE) | |
1022 | flags |= O_WRONLY; | |
1023 | else if (mode & BLK_OPEN_READ) | |
1024 | flags |= O_RDONLY; /* homeopathic, because O_RDONLY is 0 */ | |
1025 | else | |
1026 | WARN_ON_ONCE(true); | |
1027 | ||
1028 | if (mode & BLK_OPEN_NDELAY) | |
1029 | flags |= O_NDELAY; | |
1030 | ||
1031 | return flags; | |
1032 | } | |
1033 | ||
1034 | struct file *bdev_file_open_by_dev(dev_t dev, blk_mode_t mode, void *holder, | |
1035 | const struct blk_holder_ops *hops) | |
1036 | { | |
1037 | struct file *bdev_file; | |
a56aefca | 1038 | struct block_device *bdev; |
f3a60882 | 1039 | unsigned int flags; |
a56aefca | 1040 | int ret; |
f3a60882 | 1041 | |
a56aefca CB |
1042 | ret = bdev_permission(dev, mode, holder); |
1043 | if (ret) | |
1044 | return ERR_PTR(ret); | |
1045 | ||
5f33b522 | 1046 | bdev = blkdev_get_no_open(dev, true); |
a56aefca CB |
1047 | if (!bdev) |
1048 | return ERR_PTR(-ENXIO); | |
f3a60882 CB |
1049 | |
1050 | flags = blk_to_file_flags(mode); | |
df65f166 | 1051 | bdev_file = alloc_file_pseudo_noaccount(BD_INODE(bdev), |
f3a60882 CB |
1052 | blockdev_mnt, "", flags | O_LARGEFILE, &def_blk_fops); |
1053 | if (IS_ERR(bdev_file)) { | |
a56aefca | 1054 | blkdev_put_no_open(bdev); |
f3a60882 CB |
1055 | return bdev_file; |
1056 | } | |
df65f166 | 1057 | ihold(BD_INODE(bdev)); |
f3a60882 | 1058 | |
a56aefca CB |
1059 | ret = bdev_open(bdev, mode, holder, hops, bdev_file); |
1060 | if (ret) { | |
ab838b3f CB |
1061 | /* We failed to open the block device. Let ->release() know. */ |
1062 | bdev_file->private_data = ERR_PTR(ret); | |
a56aefca CB |
1063 | fput(bdev_file); |
1064 | return ERR_PTR(ret); | |
1065 | } | |
f3a60882 CB |
1066 | return bdev_file; |
1067 | } | |
1068 | EXPORT_SYMBOL(bdev_file_open_by_dev); | |
1069 | ||
1070 | struct file *bdev_file_open_by_path(const char *path, blk_mode_t mode, | |
1071 | void *holder, | |
1072 | const struct blk_holder_ops *hops) | |
1073 | { | |
7c09a4ed | 1074 | struct file *file; |
f3a60882 CB |
1075 | dev_t dev; |
1076 | int error; | |
1077 | ||
1078 | error = lookup_bdev(path, &dev); | |
1079 | if (error) | |
1080 | return ERR_PTR(error); | |
1081 | ||
7c09a4ed CB |
1082 | file = bdev_file_open_by_dev(dev, mode, holder, hops); |
1083 | if (!IS_ERR(file) && (mode & BLK_OPEN_WRITE)) { | |
1084 | if (bdev_read_only(file_bdev(file))) { | |
1085 | fput(file); | |
1086 | file = ERR_PTR(-EACCES); | |
f3a60882 CB |
1087 | } |
1088 | } | |
1089 | ||
7c09a4ed | 1090 | return file; |
f3a60882 CB |
1091 | } |
1092 | EXPORT_SYMBOL(bdev_file_open_by_path); | |
1093 | ||
22650a99 CB |
1094 | static inline void bd_yield_claim(struct file *bdev_file) |
1095 | { | |
1096 | struct block_device *bdev = file_bdev(bdev_file); | |
1097 | void *holder = bdev_file->private_data; | |
1098 | ||
1099 | lockdep_assert_held(&bdev->bd_disk->open_mutex); | |
1100 | ||
1101 | if (WARN_ON_ONCE(IS_ERR_OR_NULL(holder))) | |
1102 | return; | |
1103 | ||
1104 | if (!bdev_unclaimed(bdev_file)) | |
1105 | bd_end_claim(bdev, holder); | |
1106 | } | |
1107 | ||
7c09a4ed | 1108 | void bdev_release(struct file *bdev_file) |
2e7b651d | 1109 | { |
7c09a4ed | 1110 | struct block_device *bdev = file_bdev(bdev_file); |
ab838b3f | 1111 | void *holder = bdev_file->private_data; |
2e7b651d PZ |
1112 | struct gendisk *disk = bdev->bd_disk; |
1113 | ||
ab838b3f CB |
1114 | /* We failed to open that block device. */ |
1115 | if (IS_ERR(holder)) | |
1116 | goto put_no_open; | |
1117 | ||
b849dd84 DA |
1118 | /* |
1119 | * Sync early if it looks like we're the last one. If someone else | |
1120 | * opens the block device between now and the decrement of bd_openers | |
1121 | * then we did a sync that we didn't need to, but that's not the end | |
1122 | * of the world and we want to avoid long (could be several minute) | |
1123 | * syncs while holding the mutex. | |
1124 | */ | |
9acf381f | 1125 | if (atomic_read(&bdev->bd_openers) == 1) |
b849dd84 DA |
1126 | sync_blockdev(bdev); |
1127 | ||
a8698707 | 1128 | mutex_lock(&disk->open_mutex); |
ab838b3f | 1129 | bdev_yield_write_access(bdev_file); |
ed5cc702 | 1130 | |
ab838b3f | 1131 | if (holder) |
22650a99 | 1132 | bd_yield_claim(bdev_file); |
77ea887e | 1133 | |
85ef06d1 TH |
1134 | /* |
1135 | * Trigger event checking and tell drivers to flush MEDIA_CHANGE | |
1136 | * event. This is to ensure detection of media removal commanded | |
1137 | * from userland - e.g. eject(1). | |
1138 | */ | |
5b56b6ed | 1139 | disk_flush_events(disk, DISK_EVENT_MEDIA_CHANGE); |
85ef06d1 | 1140 | |
c8276b95 | 1141 | if (bdev_is_partition(bdev)) |
ae220766 | 1142 | blkdev_put_part(bdev); |
c8276b95 | 1143 | else |
ae220766 | 1144 | blkdev_put_whole(bdev); |
a8698707 CH |
1145 | mutex_unlock(&disk->open_mutex); |
1146 | ||
efcf5932 | 1147 | module_put(disk->fops->owner); |
ab838b3f | 1148 | put_no_open: |
22ae8ce8 | 1149 | blkdev_put_no_open(bdev); |
e719b4d1 | 1150 | } |
e719b4d1 | 1151 | |
22650a99 CB |
1152 | /** |
1153 | * bdev_fput - yield claim to the block device and put the file | |
1154 | * @bdev_file: open block device | |
1155 | * | |
1156 | * Yield claim on the block device and put the file. Ensure that the | |
1157 | * block device can be reclaimed before the file is closed which is a | |
1158 | * deferred operation. | |
1159 | */ | |
1160 | void bdev_fput(struct file *bdev_file) | |
1161 | { | |
1162 | if (WARN_ON_ONCE(bdev_file->f_op != &def_blk_fops)) | |
1163 | return; | |
1164 | ||
1165 | if (bdev_file->private_data) { | |
1166 | struct block_device *bdev = file_bdev(bdev_file); | |
1167 | struct gendisk *disk = bdev->bd_disk; | |
1168 | ||
1169 | mutex_lock(&disk->open_mutex); | |
1170 | bdev_yield_write_access(bdev_file); | |
1171 | bd_yield_claim(bdev_file); | |
1172 | /* | |
1173 | * Tell release we already gave up our hold on the | |
1174 | * device and if write restrictions are available that | |
1175 | * we already gave up write access to the device. | |
1176 | */ | |
1177 | bdev_file->private_data = BDEV_I(bdev_file->f_mapping->host); | |
1178 | mutex_unlock(&disk->open_mutex); | |
1179 | } | |
1180 | ||
1181 | fput(bdev_file); | |
1182 | } | |
1183 | EXPORT_SYMBOL(bdev_fput); | |
1184 | ||
1da177e4 | 1185 | /** |
0ba4566c MWO |
1186 | * lookup_bdev() - Look up a struct block_device by name. |
1187 | * @pathname: Name of the block device in the filesystem. | |
1188 | * @dev: Pointer to the block device's dev_t, if found. | |
1da177e4 | 1189 | * |
057178cf | 1190 | * Lookup the block device's dev_t at @pathname in the current |
0ba4566c | 1191 | * namespace if possible and return it in @dev. |
057178cf | 1192 | * |
0ba4566c MWO |
1193 | * Context: May sleep. |
1194 | * Return: 0 if succeeded, negative errno otherwise. | |
1da177e4 | 1195 | */ |
4e7b5671 | 1196 | int lookup_bdev(const char *pathname, dev_t *dev) |
1da177e4 | 1197 | { |
1da177e4 | 1198 | struct inode *inode; |
421748ec | 1199 | struct path path; |
1da177e4 LT |
1200 | int error; |
1201 | ||
421748ec | 1202 | if (!pathname || !*pathname) |
4e7b5671 | 1203 | return -EINVAL; |
1da177e4 | 1204 | |
421748ec | 1205 | error = kern_path(pathname, LOOKUP_FOLLOW, &path); |
1da177e4 | 1206 | if (error) |
4e7b5671 | 1207 | return error; |
1da177e4 | 1208 | |
bb668734 | 1209 | inode = d_backing_inode(path.dentry); |
1da177e4 LT |
1210 | error = -ENOTBLK; |
1211 | if (!S_ISBLK(inode->i_mode)) | |
4e7b5671 | 1212 | goto out_path_put; |
1da177e4 | 1213 | error = -EACCES; |
a2982cc9 | 1214 | if (!may_open_dev(&path)) |
4e7b5671 CH |
1215 | goto out_path_put; |
1216 | ||
1217 | *dev = inode->i_rdev; | |
1218 | error = 0; | |
1219 | out_path_put: | |
421748ec | 1220 | path_put(&path); |
4e7b5671 | 1221 | return error; |
1da177e4 | 1222 | } |
d5686b44 | 1223 | EXPORT_SYMBOL(lookup_bdev); |
1da177e4 | 1224 | |
560e20e4 CH |
1225 | /** |
1226 | * bdev_mark_dead - mark a block device as dead | |
1227 | * @bdev: block device to operate on | |
1228 | * @surprise: indicate a surprise removal | |
1229 | * | |
1230 | * Tell the file system that this devices or media is dead. If @surprise is set | |
1231 | * to %true the device or media is already gone, if not we are preparing for an | |
1232 | * orderly removal. | |
1233 | * | |
d8530de5 CH |
1234 | * This calls into the file system, which then typicall syncs out all dirty data |
1235 | * and writes back inodes and then invalidates any cached data in the inodes on | |
1236 | * the file system. In addition we also invalidate the block device mapping. | |
560e20e4 CH |
1237 | */ |
1238 | void bdev_mark_dead(struct block_device *bdev, bool surprise) | |
b71e8a4c | 1239 | { |
d8530de5 CH |
1240 | mutex_lock(&bdev->bd_holder_lock); |
1241 | if (bdev->bd_holder_ops && bdev->bd_holder_ops->mark_dead) | |
1242 | bdev->bd_holder_ops->mark_dead(bdev, surprise); | |
fd146410 JK |
1243 | else { |
1244 | mutex_unlock(&bdev->bd_holder_lock); | |
d8530de5 | 1245 | sync_blockdev(bdev); |
fd146410 | 1246 | } |
b71e8a4c | 1247 | |
f98393a6 | 1248 | invalidate_bdev(bdev); |
b71e8a4c | 1249 | } |
560e20e4 | 1250 | /* |
6e57236e CH |
1251 | * New drivers should not use this directly. There are some drivers however |
1252 | * that needs this for historical reasons. For example, the DASD driver has | |
1253 | * historically had a shutdown to offline mode that doesn't actually remove the | |
1254 | * gendisk that otherwise looks a lot like a safe device removal. | |
560e20e4 CH |
1255 | */ |
1256 | EXPORT_SYMBOL_GPL(bdev_mark_dead); | |
5c0d6b60 | 1257 | |
1e03a36b | 1258 | void sync_bdevs(bool wait) |
5c0d6b60 JK |
1259 | { |
1260 | struct inode *inode, *old_inode = NULL; | |
1261 | ||
74278da9 | 1262 | spin_lock(&blockdev_superblock->s_inode_list_lock); |
5c0d6b60 JK |
1263 | list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) { |
1264 | struct address_space *mapping = inode->i_mapping; | |
af309226 | 1265 | struct block_device *bdev; |
5c0d6b60 JK |
1266 | |
1267 | spin_lock(&inode->i_lock); | |
1268 | if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) || | |
1269 | mapping->nrpages == 0) { | |
1270 | spin_unlock(&inode->i_lock); | |
1271 | continue; | |
1272 | } | |
1273 | __iget(inode); | |
1274 | spin_unlock(&inode->i_lock); | |
74278da9 | 1275 | spin_unlock(&blockdev_superblock->s_inode_list_lock); |
5c0d6b60 JK |
1276 | /* |
1277 | * We hold a reference to 'inode' so it couldn't have been | |
1278 | * removed from s_inodes list while we dropped the | |
74278da9 | 1279 | * s_inode_list_lock We cannot iput the inode now as we can |
5c0d6b60 | 1280 | * be holding the last reference and we cannot iput it under |
74278da9 | 1281 | * s_inode_list_lock. So we keep the reference and iput it |
5c0d6b60 JK |
1282 | * later. |
1283 | */ | |
1284 | iput(old_inode); | |
1285 | old_inode = inode; | |
af309226 | 1286 | bdev = I_BDEV(inode); |
5c0d6b60 | 1287 | |
a8698707 | 1288 | mutex_lock(&bdev->bd_disk->open_mutex); |
9acf381f | 1289 | if (!atomic_read(&bdev->bd_openers)) { |
1e03a36b CH |
1290 | ; /* skip */ |
1291 | } else if (wait) { | |
1292 | /* | |
1293 | * We keep the error status of individual mapping so | |
1294 | * that applications can catch the writeback error using | |
1295 | * fsync(2). See filemap_fdatawait_keep_errors() for | |
1296 | * details. | |
1297 | */ | |
1298 | filemap_fdatawait_keep_errors(inode->i_mapping); | |
1299 | } else { | |
1300 | filemap_fdatawrite(inode->i_mapping); | |
1301 | } | |
a8698707 | 1302 | mutex_unlock(&bdev->bd_disk->open_mutex); |
5c0d6b60 | 1303 | |
74278da9 | 1304 | spin_lock(&blockdev_superblock->s_inode_list_lock); |
5c0d6b60 | 1305 | } |
74278da9 | 1306 | spin_unlock(&blockdev_superblock->s_inode_list_lock); |
5c0d6b60 JK |
1307 | iput(old_inode); |
1308 | } | |
2d985f8c EB |
1309 | |
1310 | /* | |
9abcfbd2 | 1311 | * Handle STATX_{DIOALIGN, WRITE_ATOMIC} for block devices. |
2d985f8c | 1312 | */ |
777d0961 | 1313 | void bdev_statx(const struct path *path, struct kstat *stat, u32 request_mask) |
2d985f8c EB |
1314 | { |
1315 | struct block_device *bdev; | |
1316 | ||
9abcfbd2 | 1317 | /* |
d13b7090 CH |
1318 | * Note that d_backing_inode() returns the block device node inode, not |
1319 | * the block device's internal inode. Therefore it is *not* valid to | |
1320 | * use I_BDEV() here; the block device has to be looked up by i_rdev | |
9abcfbd2 PS |
1321 | * instead. |
1322 | */ | |
5f33b522 | 1323 | bdev = blkdev_get_no_open(d_backing_inode(path->dentry)->i_rdev, false); |
2d985f8c EB |
1324 | if (!bdev) |
1325 | return; | |
1326 | ||
9abcfbd2 PS |
1327 | if (request_mask & STATX_DIOALIGN) { |
1328 | stat->dio_mem_align = bdev_dma_alignment(bdev) + 1; | |
1329 | stat->dio_offset_align = bdev_logical_block_size(bdev); | |
1330 | stat->result_mask |= STATX_DIOALIGN; | |
1331 | } | |
1332 | ||
1333 | if (request_mask & STATX_WRITE_ATOMIC && bdev_can_atomic_write(bdev)) { | |
1334 | struct request_queue *bd_queue = bdev->bd_queue; | |
1335 | ||
1336 | generic_fill_statx_atomic_writes(stat, | |
1337 | queue_atomic_write_unit_min_bytes(bd_queue), | |
5d894321 JG |
1338 | queue_atomic_write_unit_max_bytes(bd_queue), |
1339 | 0); | |
9abcfbd2 | 1340 | } |
2d985f8c | 1341 | |
425fbcd6 LC |
1342 | stat->blksize = bdev_io_min(bdev); |
1343 | ||
2d985f8c EB |
1344 | blkdev_put_no_open(bdev); |
1345 | } | |
ed5cc702 | 1346 | |
186ddac2 YK |
1347 | bool disk_live(struct gendisk *disk) |
1348 | { | |
df65f166 | 1349 | return !inode_unhashed(BD_INODE(disk->part0)); |
186ddac2 YK |
1350 | } |
1351 | EXPORT_SYMBOL_GPL(disk_live); | |
1352 | ||
1353 | unsigned int block_size(struct block_device *bdev) | |
1354 | { | |
df65f166 | 1355 | return 1 << BD_INODE(bdev)->i_blkbits; |
186ddac2 YK |
1356 | } |
1357 | EXPORT_SYMBOL_GPL(block_size); | |
1358 | ||
ed5cc702 JK |
1359 | static int __init setup_bdev_allow_write_mounted(char *str) |
1360 | { | |
1361 | if (kstrtobool(str, &bdev_allow_write_mounted)) | |
1362 | pr_warn("Invalid option string for bdev_allow_write_mounted:" | |
1363 | " '%s'\n", str); | |
1364 | return 1; | |
1365 | } | |
1366 | __setup("bdev_allow_write_mounted=", setup_bdev_allow_write_mounted); |