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