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 LT |
32 | |
33 | struct bdev_inode { | |
34 | struct block_device bdev; | |
35 | struct inode vfs_inode; | |
36 | }; | |
37 | ||
38 | static inline struct bdev_inode *BDEV_I(struct inode *inode) | |
39 | { | |
40 | return container_of(inode, struct bdev_inode, vfs_inode); | |
41 | } | |
42 | ||
ff5053f6 | 43 | struct block_device *I_BDEV(struct inode *inode) |
1da177e4 LT |
44 | { |
45 | return &BDEV_I(inode)->bdev; | |
46 | } | |
1da177e4 LT |
47 | EXPORT_SYMBOL(I_BDEV); |
48 | ||
dbd3ca50 | 49 | static void bdev_write_inode(struct block_device *bdev) |
564f00f6 | 50 | { |
dbd3ca50 VG |
51 | struct inode *inode = bdev->bd_inode; |
52 | int ret; | |
53 | ||
564f00f6 CH |
54 | spin_lock(&inode->i_lock); |
55 | while (inode->i_state & I_DIRTY) { | |
56 | spin_unlock(&inode->i_lock); | |
dbd3ca50 | 57 | ret = write_inode_now(inode, true); |
5bf83e9a CH |
58 | if (ret) |
59 | pr_warn_ratelimited( | |
60 | "VFS: Dirty inode writeback failed for block device %pg (err=%d).\n", | |
61 | bdev, ret); | |
564f00f6 CH |
62 | spin_lock(&inode->i_lock); |
63 | } | |
64 | spin_unlock(&inode->i_lock); | |
65 | } | |
66 | ||
f9a14399 | 67 | /* Kill _all_ buffers and pagecache , dirty or not.. */ |
3373a346 | 68 | static void kill_bdev(struct block_device *bdev) |
1da177e4 | 69 | { |
ff01bb48 AV |
70 | struct address_space *mapping = bdev->bd_inode->i_mapping; |
71 | ||
7716506a | 72 | if (mapping_empty(mapping)) |
f9a14399 | 73 | return; |
ff01bb48 | 74 | |
f9a14399 | 75 | invalidate_bh_lrus(); |
ff01bb48 | 76 | truncate_inode_pages(mapping, 0); |
3373a346 | 77 | } |
ff01bb48 AV |
78 | |
79 | /* Invalidate clean unused buffers and pagecache. */ | |
80 | void invalidate_bdev(struct block_device *bdev) | |
81 | { | |
82 | struct address_space *mapping = bdev->bd_inode->i_mapping; | |
83 | ||
a5f6a6a9 AR |
84 | if (mapping->nrpages) { |
85 | invalidate_bh_lrus(); | |
86 | lru_add_drain_all(); /* make sure all lru add caches are flushed */ | |
87 | invalidate_mapping_pages(mapping, 0, -1); | |
88 | } | |
ff01bb48 AV |
89 | } |
90 | EXPORT_SYMBOL(invalidate_bdev); | |
1da177e4 | 91 | |
384d87ef JK |
92 | /* |
93 | * Drop all buffers & page cache for given bdev range. This function bails | |
94 | * with error if bdev has other exclusive owner (such as filesystem). | |
95 | */ | |
96 | int truncate_bdev_range(struct block_device *bdev, fmode_t mode, | |
97 | loff_t lstart, loff_t lend) | |
98 | { | |
384d87ef JK |
99 | /* |
100 | * If we don't hold exclusive handle for the device, upgrade to it | |
101 | * while we discard the buffer cache to avoid discarding buffers | |
102 | * under live filesystem. | |
103 | */ | |
104 | if (!(mode & FMODE_EXCL)) { | |
37c3fc9a | 105 | int err = bd_prepare_to_claim(bdev, truncate_bdev_range); |
384d87ef | 106 | if (err) |
56887cff | 107 | goto invalidate; |
384d87ef | 108 | } |
37c3fc9a | 109 | |
384d87ef | 110 | truncate_inode_pages_range(bdev->bd_inode->i_mapping, lstart, lend); |
37c3fc9a CH |
111 | if (!(mode & FMODE_EXCL)) |
112 | bd_abort_claiming(bdev, truncate_bdev_range); | |
384d87ef | 113 | return 0; |
56887cff JK |
114 | |
115 | invalidate: | |
116 | /* | |
117 | * Someone else has handle exclusively open. Try invalidating instead. | |
118 | * The 'end' argument is inclusive so the rounding is safe. | |
119 | */ | |
120 | return invalidate_inode_pages2_range(bdev->bd_inode->i_mapping, | |
121 | lstart >> PAGE_SHIFT, | |
122 | lend >> PAGE_SHIFT); | |
384d87ef | 123 | } |
384d87ef | 124 | |
04906b2f JK |
125 | static void set_init_blocksize(struct block_device *bdev) |
126 | { | |
8dc932d3 MM |
127 | unsigned int bsize = bdev_logical_block_size(bdev); |
128 | loff_t size = i_size_read(bdev->bd_inode); | |
129 | ||
130 | while (bsize < PAGE_SIZE) { | |
131 | if (size & bsize) | |
132 | break; | |
133 | bsize <<= 1; | |
134 | } | |
135 | bdev->bd_inode->i_blkbits = blksize_bits(bsize); | |
04906b2f JK |
136 | } |
137 | ||
1da177e4 LT |
138 | int set_blocksize(struct block_device *bdev, int size) |
139 | { | |
140 | /* Size must be a power of two, and between 512 and PAGE_SIZE */ | |
1368c4f2 | 141 | if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size)) |
1da177e4 LT |
142 | return -EINVAL; |
143 | ||
144 | /* Size cannot be smaller than the size supported by the device */ | |
e1defc4f | 145 | if (size < bdev_logical_block_size(bdev)) |
1da177e4 LT |
146 | return -EINVAL; |
147 | ||
148 | /* Don't change the size if it is same as current */ | |
6b7b181b | 149 | if (bdev->bd_inode->i_blkbits != blksize_bits(size)) { |
1da177e4 | 150 | sync_blockdev(bdev); |
1da177e4 LT |
151 | bdev->bd_inode->i_blkbits = blksize_bits(size); |
152 | kill_bdev(bdev); | |
153 | } | |
154 | return 0; | |
155 | } | |
156 | ||
157 | EXPORT_SYMBOL(set_blocksize); | |
158 | ||
159 | int sb_set_blocksize(struct super_block *sb, int size) | |
160 | { | |
1da177e4 LT |
161 | if (set_blocksize(sb->s_bdev, size)) |
162 | return 0; | |
163 | /* If we get here, we know size is power of two | |
164 | * and it's value is between 512 and PAGE_SIZE */ | |
165 | sb->s_blocksize = size; | |
38885bd4 | 166 | sb->s_blocksize_bits = blksize_bits(size); |
1da177e4 LT |
167 | return sb->s_blocksize; |
168 | } | |
169 | ||
170 | EXPORT_SYMBOL(sb_set_blocksize); | |
171 | ||
172 | int sb_min_blocksize(struct super_block *sb, int size) | |
173 | { | |
e1defc4f | 174 | int minsize = bdev_logical_block_size(sb->s_bdev); |
1da177e4 LT |
175 | if (size < minsize) |
176 | size = minsize; | |
177 | return sb_set_blocksize(sb, size); | |
178 | } | |
179 | ||
180 | EXPORT_SYMBOL(sb_min_blocksize); | |
181 | ||
70164eb6 | 182 | int sync_blockdev_nowait(struct block_device *bdev) |
5cee5815 JK |
183 | { |
184 | if (!bdev) | |
185 | return 0; | |
70164eb6 | 186 | return filemap_flush(bdev->bd_inode->i_mapping); |
5cee5815 | 187 | } |
70164eb6 | 188 | EXPORT_SYMBOL_GPL(sync_blockdev_nowait); |
5cee5815 | 189 | |
585d3bc0 NP |
190 | /* |
191 | * Write out and wait upon all the dirty data associated with a block | |
192 | * device via its mapping. Does not take the superblock lock. | |
193 | */ | |
194 | int sync_blockdev(struct block_device *bdev) | |
195 | { | |
70164eb6 CH |
196 | if (!bdev) |
197 | return 0; | |
198 | return filemap_write_and_wait(bdev->bd_inode->i_mapping); | |
585d3bc0 NP |
199 | } |
200 | EXPORT_SYMBOL(sync_blockdev); | |
201 | ||
97d6fb1b YM |
202 | int sync_blockdev_range(struct block_device *bdev, loff_t lstart, loff_t lend) |
203 | { | |
204 | return filemap_write_and_wait_range(bdev->bd_inode->i_mapping, | |
205 | lstart, lend); | |
206 | } | |
207 | EXPORT_SYMBOL(sync_blockdev_range); | |
208 | ||
585d3bc0 NP |
209 | /* |
210 | * Write out and wait upon all dirty data associated with this | |
211 | * device. Filesystem data as well as the underlying block | |
212 | * device. Takes the superblock lock. | |
213 | */ | |
214 | int fsync_bdev(struct block_device *bdev) | |
215 | { | |
216 | struct super_block *sb = get_super(bdev); | |
217 | if (sb) { | |
60b0680f | 218 | int res = sync_filesystem(sb); |
585d3bc0 NP |
219 | drop_super(sb); |
220 | return res; | |
221 | } | |
222 | return sync_blockdev(bdev); | |
223 | } | |
47e4491b | 224 | EXPORT_SYMBOL(fsync_bdev); |
585d3bc0 NP |
225 | |
226 | /** | |
2e833c8c | 227 | * freeze_bdev - lock a filesystem and force it into a consistent state |
585d3bc0 NP |
228 | * @bdev: blockdevice to lock |
229 | * | |
585d3bc0 NP |
230 | * If a superblock is found on this device, we take the s_umount semaphore |
231 | * on it to make sure nobody unmounts until the snapshot creation is done. | |
232 | * The reference counter (bd_fsfreeze_count) guarantees that only the last | |
233 | * unfreeze process can unfreeze the frozen filesystem actually when multiple | |
234 | * freeze requests arrive simultaneously. It counts up in freeze_bdev() and | |
235 | * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze | |
236 | * actually. | |
237 | */ | |
040f04bd | 238 | int freeze_bdev(struct block_device *bdev) |
585d3bc0 NP |
239 | { |
240 | struct super_block *sb; | |
241 | int error = 0; | |
242 | ||
243 | mutex_lock(&bdev->bd_fsfreeze_mutex); | |
040f04bd CH |
244 | if (++bdev->bd_fsfreeze_count > 1) |
245 | goto done; | |
4504230a CH |
246 | |
247 | sb = get_active_super(bdev); | |
248 | if (!sb) | |
040f04bd | 249 | goto sync; |
48b6bca6 BM |
250 | if (sb->s_op->freeze_super) |
251 | error = sb->s_op->freeze_super(sb); | |
252 | else | |
253 | error = freeze_super(sb); | |
040f04bd CH |
254 | deactivate_super(sb); |
255 | ||
18e9e510 | 256 | if (error) { |
18e9e510 | 257 | bdev->bd_fsfreeze_count--; |
040f04bd | 258 | goto done; |
585d3bc0 | 259 | } |
040f04bd CH |
260 | bdev->bd_fsfreeze_sb = sb; |
261 | ||
262 | sync: | |
585d3bc0 | 263 | sync_blockdev(bdev); |
040f04bd | 264 | done: |
585d3bc0 | 265 | mutex_unlock(&bdev->bd_fsfreeze_mutex); |
040f04bd | 266 | return error; |
585d3bc0 NP |
267 | } |
268 | EXPORT_SYMBOL(freeze_bdev); | |
269 | ||
270 | /** | |
2e833c8c | 271 | * thaw_bdev - unlock filesystem |
585d3bc0 | 272 | * @bdev: blockdevice to unlock |
585d3bc0 NP |
273 | * |
274 | * Unlocks the filesystem and marks it writeable again after freeze_bdev(). | |
275 | */ | |
040f04bd | 276 | int thaw_bdev(struct block_device *bdev) |
585d3bc0 | 277 | { |
040f04bd | 278 | struct super_block *sb; |
4504230a | 279 | int error = -EINVAL; |
585d3bc0 NP |
280 | |
281 | mutex_lock(&bdev->bd_fsfreeze_mutex); | |
4504230a | 282 | if (!bdev->bd_fsfreeze_count) |
18e9e510 | 283 | goto out; |
4504230a CH |
284 | |
285 | error = 0; | |
286 | if (--bdev->bd_fsfreeze_count > 0) | |
18e9e510 | 287 | goto out; |
4504230a | 288 | |
040f04bd | 289 | sb = bdev->bd_fsfreeze_sb; |
4504230a | 290 | if (!sb) |
18e9e510 | 291 | goto out; |
4504230a | 292 | |
48b6bca6 BM |
293 | if (sb->s_op->thaw_super) |
294 | error = sb->s_op->thaw_super(sb); | |
295 | else | |
296 | error = thaw_super(sb); | |
997198ba | 297 | if (error) |
18e9e510 | 298 | bdev->bd_fsfreeze_count++; |
04a6a536 ST |
299 | else |
300 | bdev->bd_fsfreeze_sb = NULL; | |
18e9e510 | 301 | out: |
585d3bc0 | 302 | mutex_unlock(&bdev->bd_fsfreeze_mutex); |
997198ba | 303 | return error; |
585d3bc0 NP |
304 | } |
305 | EXPORT_SYMBOL(thaw_bdev); | |
306 | ||
1da177e4 LT |
307 | /* |
308 | * pseudo-fs | |
309 | */ | |
310 | ||
311 | static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock); | |
e18b890b | 312 | static struct kmem_cache * bdev_cachep __read_mostly; |
1da177e4 LT |
313 | |
314 | static struct inode *bdev_alloc_inode(struct super_block *sb) | |
315 | { | |
fd60b288 | 316 | struct bdev_inode *ei = alloc_inode_sb(sb, bdev_cachep, GFP_KERNEL); |
2d2f6f1b | 317 | |
1da177e4 LT |
318 | if (!ei) |
319 | return NULL; | |
2d2f6f1b | 320 | memset(&ei->bdev, 0, sizeof(ei->bdev)); |
1da177e4 LT |
321 | return &ei->vfs_inode; |
322 | } | |
323 | ||
41149cb0 | 324 | static void bdev_free_inode(struct inode *inode) |
1da177e4 | 325 | { |
15e3d2c5 CH |
326 | struct block_device *bdev = I_BDEV(inode); |
327 | ||
328 | free_percpu(bdev->bd_stats); | |
231926db | 329 | kfree(bdev->bd_meta_info); |
15e3d2c5 | 330 | |
889c05cc CH |
331 | if (!bdev_is_partition(bdev)) { |
332 | if (bdev->bd_disk && bdev->bd_disk->bdi) | |
333 | bdi_put(bdev->bd_disk->bdi); | |
340e8457 | 334 | kfree(bdev->bd_disk); |
889c05cc | 335 | } |
9451aa0a CH |
336 | |
337 | if (MAJOR(bdev->bd_dev) == BLOCK_EXT_MAJOR) | |
338 | blk_free_ext_minor(MINOR(bdev->bd_dev)); | |
339 | ||
41149cb0 | 340 | kmem_cache_free(bdev_cachep, BDEV_I(inode)); |
fa0d7e3d NP |
341 | } |
342 | ||
e6cb5382 | 343 | static void init_once(void *data) |
1da177e4 | 344 | { |
e6cb5382 | 345 | struct bdev_inode *ei = data; |
1da177e4 | 346 | |
a35afb83 | 347 | inode_init_once(&ei->vfs_inode); |
1da177e4 LT |
348 | } |
349 | ||
b57922d9 | 350 | static void bdev_evict_inode(struct inode *inode) |
1da177e4 | 351 | { |
91b0abe3 | 352 | truncate_inode_pages_final(&inode->i_data); |
b57922d9 | 353 | invalidate_inode_buffers(inode); /* is it needed here? */ |
dbd5768f | 354 | clear_inode(inode); |
1da177e4 LT |
355 | } |
356 | ||
ee9b6d61 | 357 | static const struct super_operations bdev_sops = { |
1da177e4 LT |
358 | .statfs = simple_statfs, |
359 | .alloc_inode = bdev_alloc_inode, | |
41149cb0 | 360 | .free_inode = bdev_free_inode, |
1da177e4 | 361 | .drop_inode = generic_delete_inode, |
b57922d9 | 362 | .evict_inode = bdev_evict_inode, |
1da177e4 LT |
363 | }; |
364 | ||
9030d16e | 365 | static int bd_init_fs_context(struct fs_context *fc) |
1da177e4 | 366 | { |
9030d16e DH |
367 | struct pseudo_fs_context *ctx = init_pseudo(fc, BDEVFS_MAGIC); |
368 | if (!ctx) | |
369 | return -ENOMEM; | |
370 | fc->s_iflags |= SB_I_CGROUPWB; | |
371 | ctx->ops = &bdev_sops; | |
372 | return 0; | |
1da177e4 LT |
373 | } |
374 | ||
375 | static struct file_system_type bd_type = { | |
376 | .name = "bdev", | |
9030d16e | 377 | .init_fs_context = bd_init_fs_context, |
1da177e4 LT |
378 | .kill_sb = kill_anon_super, |
379 | }; | |
380 | ||
a212b105 TH |
381 | struct super_block *blockdev_superblock __read_mostly; |
382 | EXPORT_SYMBOL_GPL(blockdev_superblock); | |
1da177e4 LT |
383 | |
384 | void __init bdev_cache_init(void) | |
385 | { | |
386 | int err; | |
ace8577a | 387 | static struct vfsmount *bd_mnt; |
c2acf7b9 | 388 | |
1da177e4 | 389 | bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode), |
fffb60f9 | 390 | 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT| |
5d097056 | 391 | SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC), |
20c2df83 | 392 | init_once); |
1da177e4 LT |
393 | err = register_filesystem(&bd_type); |
394 | if (err) | |
395 | panic("Cannot register bdev pseudo-fs"); | |
396 | bd_mnt = kern_mount(&bd_type); | |
1da177e4 LT |
397 | if (IS_ERR(bd_mnt)) |
398 | panic("Cannot create bdev pseudo-fs"); | |
ace8577a | 399 | blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */ |
1da177e4 LT |
400 | } |
401 | ||
22ae8ce8 | 402 | struct block_device *bdev_alloc(struct gendisk *disk, u8 partno) |
1da177e4 LT |
403 | { |
404 | struct block_device *bdev; | |
405 | struct inode *inode; | |
406 | ||
22ae8ce8 | 407 | inode = new_inode(blockdev_superblock); |
1da177e4 LT |
408 | if (!inode) |
409 | return NULL; | |
22ae8ce8 CH |
410 | inode->i_mode = S_IFBLK; |
411 | inode->i_rdev = 0; | |
412 | inode->i_data.a_ops = &def_blk_aops; | |
413 | mapping_set_gfp_mask(&inode->i_data, GFP_USER); | |
414 | ||
415 | bdev = I_BDEV(inode); | |
e6cb5382 | 416 | mutex_init(&bdev->bd_fsfreeze_mutex); |
22ae8ce8 | 417 | spin_lock_init(&bdev->bd_size_lock); |
22ae8ce8 | 418 | bdev->bd_partno = partno; |
22ae8ce8 | 419 | bdev->bd_inode = inode; |
17220ca5 | 420 | bdev->bd_queue = disk->queue; |
15e3d2c5 | 421 | bdev->bd_stats = alloc_percpu(struct disk_stats); |
9f4107b0 | 422 | bdev->bd_has_submit_bio = false; |
15e3d2c5 CH |
423 | if (!bdev->bd_stats) { |
424 | iput(inode); | |
425 | return NULL; | |
426 | } | |
06cc978d | 427 | bdev->bd_disk = disk; |
22ae8ce8 CH |
428 | return bdev; |
429 | } | |
1da177e4 | 430 | |
22ae8ce8 CH |
431 | void bdev_add(struct block_device *bdev, dev_t dev) |
432 | { | |
433 | bdev->bd_dev = dev; | |
434 | bdev->bd_inode->i_rdev = dev; | |
435 | bdev->bd_inode->i_ino = dev; | |
436 | insert_inode_hash(bdev->bd_inode); | |
437 | } | |
1da177e4 | 438 | |
1da177e4 LT |
439 | long nr_blockdev_pages(void) |
440 | { | |
1008fe6d | 441 | struct inode *inode; |
1da177e4 | 442 | long ret = 0; |
1008fe6d CH |
443 | |
444 | spin_lock(&blockdev_superblock->s_inode_list_lock); | |
445 | list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) | |
446 | ret += inode->i_mapping->nrpages; | |
447 | spin_unlock(&blockdev_superblock->s_inode_list_lock); | |
448 | ||
1da177e4 LT |
449 | return ret; |
450 | } | |
451 | ||
1a3cbbc5 TH |
452 | /** |
453 | * bd_may_claim - test whether a block device can be claimed | |
454 | * @bdev: block device of interest | |
455 | * @whole: whole block device containing @bdev, may equal @bdev | |
456 | * @holder: holder trying to claim @bdev | |
457 | * | |
25985edc | 458 | * Test whether @bdev can be claimed by @holder. |
1a3cbbc5 TH |
459 | * |
460 | * CONTEXT: | |
461 | * spin_lock(&bdev_lock). | |
462 | * | |
463 | * RETURNS: | |
464 | * %true if @bdev can be claimed, %false otherwise. | |
465 | */ | |
466 | static bool bd_may_claim(struct block_device *bdev, struct block_device *whole, | |
467 | void *holder) | |
1da177e4 | 468 | { |
1da177e4 | 469 | if (bdev->bd_holder == holder) |
1a3cbbc5 | 470 | return true; /* already a holder */ |
1da177e4 | 471 | else if (bdev->bd_holder != NULL) |
1a3cbbc5 | 472 | return false; /* held by someone else */ |
bcc7f5b4 | 473 | else if (whole == bdev) |
1a3cbbc5 | 474 | return true; /* is a whole device which isn't held */ |
1da177e4 | 475 | |
e525fd89 | 476 | else if (whole->bd_holder == bd_may_claim) |
1a3cbbc5 TH |
477 | return true; /* is a partition of a device that is being partitioned */ |
478 | else if (whole->bd_holder != NULL) | |
479 | return false; /* is a partition of a held device */ | |
1da177e4 | 480 | else |
1a3cbbc5 TH |
481 | return true; /* is a partition of an un-held device */ |
482 | } | |
483 | ||
6b4517a7 | 484 | /** |
58e46ed9 | 485 | * bd_prepare_to_claim - claim a block device |
6b4517a7 | 486 | * @bdev: block device of interest |
6b4517a7 TH |
487 | * @holder: holder trying to claim @bdev |
488 | * | |
58e46ed9 CH |
489 | * Claim @bdev. This function fails if @bdev is already claimed by another |
490 | * holder and waits if another claiming is in progress. return, the caller | |
491 | * has ownership of bd_claiming and bd_holder[s]. | |
6b4517a7 TH |
492 | * |
493 | * RETURNS: | |
494 | * 0 if @bdev can be claimed, -EBUSY otherwise. | |
495 | */ | |
37c3fc9a | 496 | int bd_prepare_to_claim(struct block_device *bdev, void *holder) |
6b4517a7 | 497 | { |
37c3fc9a CH |
498 | struct block_device *whole = bdev_whole(bdev); |
499 | ||
500 | if (WARN_ON_ONCE(!holder)) | |
501 | return -EINVAL; | |
6b4517a7 | 502 | retry: |
58e46ed9 | 503 | spin_lock(&bdev_lock); |
6b4517a7 | 504 | /* if someone else claimed, fail */ |
58e46ed9 CH |
505 | if (!bd_may_claim(bdev, whole, holder)) { |
506 | spin_unlock(&bdev_lock); | |
6b4517a7 | 507 | return -EBUSY; |
58e46ed9 | 508 | } |
6b4517a7 | 509 | |
e75aa858 TH |
510 | /* if claiming is already in progress, wait for it to finish */ |
511 | if (whole->bd_claiming) { | |
6b4517a7 TH |
512 | wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0); |
513 | DEFINE_WAIT(wait); | |
514 | ||
515 | prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE); | |
516 | spin_unlock(&bdev_lock); | |
517 | schedule(); | |
518 | finish_wait(wq, &wait); | |
6b4517a7 TH |
519 | goto retry; |
520 | } | |
521 | ||
522 | /* yay, all mine */ | |
58e46ed9 CH |
523 | whole->bd_claiming = holder; |
524 | spin_unlock(&bdev_lock); | |
6b4517a7 TH |
525 | return 0; |
526 | } | |
ecbe6bc0 | 527 | EXPORT_SYMBOL_GPL(bd_prepare_to_claim); /* only for the loop driver */ |
6b4517a7 | 528 | |
89e524c0 JK |
529 | static void bd_clear_claiming(struct block_device *whole, void *holder) |
530 | { | |
531 | lockdep_assert_held(&bdev_lock); | |
532 | /* tell others that we're done */ | |
533 | BUG_ON(whole->bd_claiming != holder); | |
534 | whole->bd_claiming = NULL; | |
535 | wake_up_bit(&whole->bd_claiming, 0); | |
536 | } | |
537 | ||
538 | /** | |
539 | * bd_finish_claiming - finish claiming of a block device | |
540 | * @bdev: block device of interest | |
89e524c0 JK |
541 | * @holder: holder that has claimed @bdev |
542 | * | |
543 | * Finish exclusive open of a block device. Mark the device as exlusively | |
544 | * open by the holder and wake up all waiters for exclusive open to finish. | |
545 | */ | |
37c3fc9a | 546 | static void bd_finish_claiming(struct block_device *bdev, void *holder) |
89e524c0 | 547 | { |
37c3fc9a CH |
548 | struct block_device *whole = bdev_whole(bdev); |
549 | ||
89e524c0 JK |
550 | spin_lock(&bdev_lock); |
551 | BUG_ON(!bd_may_claim(bdev, whole, holder)); | |
552 | /* | |
553 | * Note that for a whole device bd_holders will be incremented twice, | |
554 | * and bd_holder will be set to bd_may_claim before being set to holder | |
555 | */ | |
556 | whole->bd_holders++; | |
557 | whole->bd_holder = bd_may_claim; | |
558 | bdev->bd_holders++; | |
559 | bdev->bd_holder = holder; | |
560 | bd_clear_claiming(whole, holder); | |
561 | spin_unlock(&bdev_lock); | |
562 | } | |
89e524c0 JK |
563 | |
564 | /** | |
565 | * bd_abort_claiming - abort claiming of a block device | |
566 | * @bdev: block device of interest | |
89e524c0 JK |
567 | * @holder: holder that has claimed @bdev |
568 | * | |
569 | * Abort claiming of a block device when the exclusive open failed. This can be | |
570 | * also used when exclusive open is not actually desired and we just needed | |
571 | * to block other exclusive openers for a while. | |
572 | */ | |
37c3fc9a | 573 | void bd_abort_claiming(struct block_device *bdev, void *holder) |
89e524c0 JK |
574 | { |
575 | spin_lock(&bdev_lock); | |
37c3fc9a | 576 | bd_clear_claiming(bdev_whole(bdev), holder); |
89e524c0 JK |
577 | spin_unlock(&bdev_lock); |
578 | } | |
579 | EXPORT_SYMBOL(bd_abort_claiming); | |
6b4517a7 | 580 | |
c8276b95 CH |
581 | static void blkdev_flush_mapping(struct block_device *bdev) |
582 | { | |
583 | WARN_ON_ONCE(bdev->bd_holders); | |
584 | sync_blockdev(bdev); | |
585 | kill_bdev(bdev); | |
586 | bdev_write_inode(bdev); | |
587 | } | |
37be4124 | 588 | |
362529d9 | 589 | static int blkdev_get_whole(struct block_device *bdev, fmode_t mode) |
a1548b67 | 590 | { |
142fe8f4 | 591 | struct gendisk *disk = bdev->bd_disk; |
af22fef3 | 592 | int ret; |
a1548b67 | 593 | |
362529d9 CH |
594 | if (disk->fops->open) { |
595 | ret = disk->fops->open(bdev, mode); | |
596 | if (ret) { | |
597 | /* avoid ghost partitions on a removed medium */ | |
598 | if (ret == -ENOMEDIUM && | |
599 | test_bit(GD_NEED_PART_SCAN, &disk->state)) | |
0384264e | 600 | bdev_disk_changed(disk, true); |
362529d9 CH |
601 | return ret; |
602 | } | |
d981cb5b | 603 | } |
a1548b67 | 604 | |
9acf381f | 605 | if (!atomic_read(&bdev->bd_openers)) |
362529d9 | 606 | set_init_blocksize(bdev); |
362529d9 | 607 | if (test_bit(GD_NEED_PART_SCAN, &disk->state)) |
0384264e | 608 | bdev_disk_changed(disk, false); |
9acf381f | 609 | atomic_inc(&bdev->bd_openers); |
483546c1 | 610 | return 0; |
362529d9 | 611 | } |
a1548b67 | 612 | |
c8276b95 CH |
613 | static void blkdev_put_whole(struct block_device *bdev, fmode_t mode) |
614 | { | |
9acf381f | 615 | if (atomic_dec_and_test(&bdev->bd_openers)) |
c8276b95 CH |
616 | blkdev_flush_mapping(bdev); |
617 | if (bdev->bd_disk->fops->release) | |
618 | bdev->bd_disk->fops->release(bdev->bd_disk, mode); | |
a1548b67 CH |
619 | } |
620 | ||
362529d9 | 621 | static int blkdev_get_part(struct block_device *part, fmode_t mode) |
1da177e4 | 622 | { |
362529d9 | 623 | struct gendisk *disk = part->bd_disk; |
362529d9 | 624 | int ret; |
5b642d8b | 625 | |
9acf381f | 626 | if (atomic_read(&part->bd_openers)) |
362529d9 | 627 | goto done; |
6c60ff04 | 628 | |
9d3b8813 | 629 | ret = blkdev_get_whole(bdev_whole(part), mode); |
a8698707 | 630 | if (ret) |
9d3b8813 | 631 | return ret; |
7e69723f | 632 | |
362529d9 CH |
633 | ret = -ENXIO; |
634 | if (!bdev_nr_sectors(part)) | |
635 | goto out_blkdev_put; | |
7e69723f | 636 | |
ab4b5705 | 637 | disk->open_partitions++; |
362529d9 | 638 | set_init_blocksize(part); |
362529d9 | 639 | done: |
9acf381f | 640 | atomic_inc(&part->bd_openers); |
1da177e4 | 641 | return 0; |
5a023cdb | 642 | |
362529d9 | 643 | out_blkdev_put: |
9d3b8813 | 644 | blkdev_put_whole(bdev_whole(part), mode); |
362529d9 | 645 | return ret; |
1da177e4 | 646 | } |
5b56b6ed | 647 | |
c8276b95 CH |
648 | static void blkdev_put_part(struct block_device *part, fmode_t mode) |
649 | { | |
650 | struct block_device *whole = bdev_whole(part); | |
03e26279 | 651 | |
9acf381f | 652 | if (!atomic_dec_and_test(&part->bd_openers)) |
c8276b95 CH |
653 | return; |
654 | blkdev_flush_mapping(part); | |
ab4b5705 | 655 | whole->bd_disk->open_partitions--; |
c8276b95 | 656 | blkdev_put_whole(whole, mode); |
1da177e4 LT |
657 | } |
658 | ||
22ae8ce8 CH |
659 | struct block_device *blkdev_get_no_open(dev_t dev) |
660 | { | |
661 | struct block_device *bdev; | |
9d3b8813 | 662 | struct inode *inode; |
22ae8ce8 | 663 | |
9d3b8813 | 664 | inode = ilookup(blockdev_superblock, dev); |
fbdee71b | 665 | if (!inode && IS_ENABLED(CONFIG_BLOCK_LEGACY_AUTOLOAD)) { |
22ae8ce8 | 666 | blk_request_module(dev); |
9d3b8813 | 667 | inode = ilookup(blockdev_superblock, dev); |
fbdee71b CH |
668 | if (inode) |
669 | pr_warn_ratelimited( | |
451f0b6f | 670 | "block device autoloading is deprecated and will be removed.\n"); |
22ae8ce8 | 671 | } |
fbdee71b CH |
672 | if (!inode) |
673 | return NULL; | |
22ae8ce8 | 674 | |
9d3b8813 CH |
675 | /* switch from the inode reference to a device mode one: */ |
676 | bdev = &BDEV_I(inode)->bdev; | |
677 | if (!kobject_get_unless_zero(&bdev->bd_device.kobj)) | |
678 | bdev = NULL; | |
679 | iput(inode); | |
22ae8ce8 | 680 | return bdev; |
22ae8ce8 CH |
681 | } |
682 | ||
683 | void blkdev_put_no_open(struct block_device *bdev) | |
684 | { | |
9d3b8813 | 685 | put_device(&bdev->bd_device); |
22ae8ce8 CH |
686 | } |
687 | ||
d4d77629 | 688 | /** |
4e7b5671 CH |
689 | * blkdev_get_by_dev - open a block device by device number |
690 | * @dev: device number of block device to open | |
d4d77629 TH |
691 | * @mode: FMODE_* mask |
692 | * @holder: exclusive holder identifier | |
693 | * | |
4e7b5671 CH |
694 | * Open the block device described by device number @dev. If @mode includes |
695 | * %FMODE_EXCL, the block device is opened with exclusive access. Specifying | |
696 | * %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may nest for | |
697 | * the same @holder. | |
d4d77629 | 698 | * |
4e7b5671 CH |
699 | * Use this interface ONLY if you really do not have anything better - i.e. when |
700 | * you are behind a truly sucky interface and all you are given is a device | |
701 | * number. Everything else should use blkdev_get_by_path(). | |
d4d77629 TH |
702 | * |
703 | * CONTEXT: | |
704 | * Might sleep. | |
705 | * | |
706 | * RETURNS: | |
4e7b5671 | 707 | * Reference to the block_device on success, ERR_PTR(-errno) on failure. |
d4d77629 | 708 | */ |
4e7b5671 | 709 | struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder) |
1da177e4 | 710 | { |
5b56b6ed | 711 | bool unblock_events = true; |
4e7b5671 | 712 | struct block_device *bdev; |
5b56b6ed | 713 | struct gendisk *disk; |
5b56b6ed | 714 | int ret; |
e525fd89 | 715 | |
7918f0f6 | 716 | ret = devcgroup_check_permission(DEVCG_DEV_BLOCK, |
4e7b5671 | 717 | MAJOR(dev), MINOR(dev), |
7918f0f6 CH |
718 | ((mode & FMODE_READ) ? DEVCG_ACC_READ : 0) | |
719 | ((mode & FMODE_WRITE) ? DEVCG_ACC_WRITE : 0)); | |
e5c7fb40 | 720 | if (ret) |
4e7b5671 CH |
721 | return ERR_PTR(ret); |
722 | ||
22ae8ce8 CH |
723 | bdev = blkdev_get_no_open(dev); |
724 | if (!bdev) | |
725 | return ERR_PTR(-ENXIO); | |
726 | disk = bdev->bd_disk; | |
e5c7fb40 | 727 | |
5b56b6ed | 728 | if (mode & FMODE_EXCL) { |
37c3fc9a | 729 | ret = bd_prepare_to_claim(bdev, holder); |
5b56b6ed | 730 | if (ret) |
37c3fc9a | 731 | goto put_blkdev; |
5b56b6ed CH |
732 | } |
733 | ||
734 | disk_block_events(disk); | |
735 | ||
a8698707 | 736 | mutex_lock(&disk->open_mutex); |
362529d9 | 737 | ret = -ENXIO; |
50b4aecf | 738 | if (!disk_live(disk)) |
362529d9 | 739 | goto abort_claiming; |
efcf5932 ML |
740 | if (!try_module_get(disk->fops->owner)) |
741 | goto abort_claiming; | |
362529d9 CH |
742 | if (bdev_is_partition(bdev)) |
743 | ret = blkdev_get_part(bdev, mode); | |
744 | else | |
745 | ret = blkdev_get_whole(bdev, mode); | |
22ae8ce8 | 746 | if (ret) |
efcf5932 | 747 | goto put_module; |
22ae8ce8 | 748 | if (mode & FMODE_EXCL) { |
37c3fc9a | 749 | bd_finish_claiming(bdev, holder); |
5b56b6ed CH |
750 | |
751 | /* | |
752 | * Block event polling for write claims if requested. Any write | |
753 | * holder makes the write_holder state stick until all are | |
754 | * released. This is good enough and tracking individual | |
755 | * writeable reference is too fragile given the way @mode is | |
756 | * used in blkdev_get/put(). | |
757 | */ | |
758 | if ((mode & FMODE_WRITE) && !bdev->bd_write_holder && | |
1545e0b4 | 759 | (disk->event_flags & DISK_EVENT_FLAG_BLOCK_ON_EXCL_WRITE)) { |
5b56b6ed CH |
760 | bdev->bd_write_holder = true; |
761 | unblock_events = false; | |
762 | } | |
763 | } | |
a8698707 | 764 | mutex_unlock(&disk->open_mutex); |
5b56b6ed CH |
765 | |
766 | if (unblock_events) | |
767 | disk_unblock_events(disk); | |
22ae8ce8 | 768 | return bdev; |
efcf5932 ML |
769 | put_module: |
770 | module_put(disk->fops->owner); | |
22ae8ce8 CH |
771 | abort_claiming: |
772 | if (mode & FMODE_EXCL) | |
37c3fc9a | 773 | bd_abort_claiming(bdev, holder); |
a8698707 | 774 | mutex_unlock(&disk->open_mutex); |
22ae8ce8 | 775 | disk_unblock_events(disk); |
22ae8ce8 CH |
776 | put_blkdev: |
777 | blkdev_put_no_open(bdev); | |
22ae8ce8 | 778 | return ERR_PTR(ret); |
37be4124 | 779 | } |
4e7b5671 | 780 | EXPORT_SYMBOL(blkdev_get_by_dev); |
1da177e4 | 781 | |
d4d77629 TH |
782 | /** |
783 | * blkdev_get_by_path - open a block device by name | |
784 | * @path: path to the block device to open | |
785 | * @mode: FMODE_* mask | |
786 | * @holder: exclusive holder identifier | |
787 | * | |
4e7b5671 CH |
788 | * Open the block device described by the device file at @path. If @mode |
789 | * includes %FMODE_EXCL, the block device is opened with exclusive access. | |
790 | * Specifying %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may | |
791 | * nest for the same @holder. | |
d4d77629 TH |
792 | * |
793 | * CONTEXT: | |
794 | * Might sleep. | |
795 | * | |
796 | * RETURNS: | |
4e7b5671 | 797 | * Reference to the block_device on success, ERR_PTR(-errno) on failure. |
d4d77629 TH |
798 | */ |
799 | struct block_device *blkdev_get_by_path(const char *path, fmode_t mode, | |
800 | void *holder) | |
801 | { | |
802 | struct block_device *bdev; | |
4e7b5671 CH |
803 | dev_t dev; |
804 | int error; | |
d4d77629 | 805 | |
4e7b5671 CH |
806 | error = lookup_bdev(path, &dev); |
807 | if (error) | |
808 | return ERR_PTR(error); | |
d4d77629 | 809 | |
4e7b5671 CH |
810 | bdev = blkdev_get_by_dev(dev, mode, holder); |
811 | if (!IS_ERR(bdev) && (mode & FMODE_WRITE) && bdev_read_only(bdev)) { | |
e51900f7 CE |
812 | blkdev_put(bdev, mode); |
813 | return ERR_PTR(-EACCES); | |
814 | } | |
815 | ||
d4d77629 TH |
816 | return bdev; |
817 | } | |
818 | EXPORT_SYMBOL(blkdev_get_by_path); | |
819 | ||
4385bab1 | 820 | void blkdev_put(struct block_device *bdev, fmode_t mode) |
2e7b651d | 821 | { |
2e7b651d PZ |
822 | struct gendisk *disk = bdev->bd_disk; |
823 | ||
b849dd84 DA |
824 | /* |
825 | * Sync early if it looks like we're the last one. If someone else | |
826 | * opens the block device between now and the decrement of bd_openers | |
827 | * then we did a sync that we didn't need to, but that's not the end | |
828 | * of the world and we want to avoid long (could be several minute) | |
829 | * syncs while holding the mutex. | |
830 | */ | |
9acf381f | 831 | if (atomic_read(&bdev->bd_openers) == 1) |
b849dd84 DA |
832 | sync_blockdev(bdev); |
833 | ||
a8698707 | 834 | mutex_lock(&disk->open_mutex); |
e525fd89 | 835 | if (mode & FMODE_EXCL) { |
a954ea81 | 836 | struct block_device *whole = bdev_whole(bdev); |
6a027eff TH |
837 | bool bdev_free; |
838 | ||
839 | /* | |
840 | * Release a claim on the device. The holder fields | |
a8698707 | 841 | * are protected with bdev_lock. open_mutex is to |
6a027eff TH |
842 | * synchronize disk_holder unlinking. |
843 | */ | |
6a027eff TH |
844 | spin_lock(&bdev_lock); |
845 | ||
846 | WARN_ON_ONCE(--bdev->bd_holders < 0); | |
a954ea81 | 847 | WARN_ON_ONCE(--whole->bd_holders < 0); |
6a027eff | 848 | |
6a027eff TH |
849 | if ((bdev_free = !bdev->bd_holders)) |
850 | bdev->bd_holder = NULL; | |
a954ea81 CH |
851 | if (!whole->bd_holders) |
852 | whole->bd_holder = NULL; | |
6a027eff TH |
853 | |
854 | spin_unlock(&bdev_lock); | |
855 | ||
77ea887e TH |
856 | /* |
857 | * If this was the last claim, remove holder link and | |
858 | * unblock evpoll if it was a write holder. | |
859 | */ | |
85ef06d1 | 860 | if (bdev_free && bdev->bd_write_holder) { |
5b56b6ed | 861 | disk_unblock_events(disk); |
85ef06d1 | 862 | bdev->bd_write_holder = false; |
77ea887e | 863 | } |
6936217c | 864 | } |
77ea887e | 865 | |
85ef06d1 TH |
866 | /* |
867 | * Trigger event checking and tell drivers to flush MEDIA_CHANGE | |
868 | * event. This is to ensure detection of media removal commanded | |
869 | * from userland - e.g. eject(1). | |
870 | */ | |
5b56b6ed | 871 | disk_flush_events(disk, DISK_EVENT_MEDIA_CHANGE); |
85ef06d1 | 872 | |
c8276b95 CH |
873 | if (bdev_is_partition(bdev)) |
874 | blkdev_put_part(bdev, mode); | |
875 | else | |
876 | blkdev_put_whole(bdev, mode); | |
a8698707 CH |
877 | mutex_unlock(&disk->open_mutex); |
878 | ||
efcf5932 | 879 | module_put(disk->fops->owner); |
22ae8ce8 | 880 | blkdev_put_no_open(bdev); |
37be4124 | 881 | } |
2e7b651d PZ |
882 | EXPORT_SYMBOL(blkdev_put); |
883 | ||
1da177e4 | 884 | /** |
0ba4566c MWO |
885 | * lookup_bdev() - Look up a struct block_device by name. |
886 | * @pathname: Name of the block device in the filesystem. | |
887 | * @dev: Pointer to the block device's dev_t, if found. | |
1da177e4 | 888 | * |
057178cf | 889 | * Lookup the block device's dev_t at @pathname in the current |
0ba4566c | 890 | * namespace if possible and return it in @dev. |
057178cf | 891 | * |
0ba4566c MWO |
892 | * Context: May sleep. |
893 | * Return: 0 if succeeded, negative errno otherwise. | |
1da177e4 | 894 | */ |
4e7b5671 | 895 | int lookup_bdev(const char *pathname, dev_t *dev) |
1da177e4 | 896 | { |
1da177e4 | 897 | struct inode *inode; |
421748ec | 898 | struct path path; |
1da177e4 LT |
899 | int error; |
900 | ||
421748ec | 901 | if (!pathname || !*pathname) |
4e7b5671 | 902 | return -EINVAL; |
1da177e4 | 903 | |
421748ec | 904 | error = kern_path(pathname, LOOKUP_FOLLOW, &path); |
1da177e4 | 905 | if (error) |
4e7b5671 | 906 | return error; |
1da177e4 | 907 | |
bb668734 | 908 | inode = d_backing_inode(path.dentry); |
1da177e4 LT |
909 | error = -ENOTBLK; |
910 | if (!S_ISBLK(inode->i_mode)) | |
4e7b5671 | 911 | goto out_path_put; |
1da177e4 | 912 | error = -EACCES; |
a2982cc9 | 913 | if (!may_open_dev(&path)) |
4e7b5671 CH |
914 | goto out_path_put; |
915 | ||
916 | *dev = inode->i_rdev; | |
917 | error = 0; | |
918 | out_path_put: | |
421748ec | 919 | path_put(&path); |
4e7b5671 | 920 | return error; |
1da177e4 | 921 | } |
d5686b44 | 922 | EXPORT_SYMBOL(lookup_bdev); |
1da177e4 | 923 | |
93b270f7 | 924 | int __invalidate_device(struct block_device *bdev, bool kill_dirty) |
b71e8a4c DH |
925 | { |
926 | struct super_block *sb = get_super(bdev); | |
927 | int res = 0; | |
928 | ||
929 | if (sb) { | |
930 | /* | |
931 | * no need to lock the super, get_super holds the | |
932 | * read mutex so the filesystem cannot go away | |
933 | * under us (->put_super runs with the write lock | |
934 | * hold). | |
935 | */ | |
936 | shrink_dcache_sb(sb); | |
93b270f7 | 937 | res = invalidate_inodes(sb, kill_dirty); |
b71e8a4c DH |
938 | drop_super(sb); |
939 | } | |
f98393a6 | 940 | invalidate_bdev(bdev); |
b71e8a4c DH |
941 | return res; |
942 | } | |
943 | EXPORT_SYMBOL(__invalidate_device); | |
5c0d6b60 | 944 | |
1e03a36b | 945 | void sync_bdevs(bool wait) |
5c0d6b60 JK |
946 | { |
947 | struct inode *inode, *old_inode = NULL; | |
948 | ||
74278da9 | 949 | spin_lock(&blockdev_superblock->s_inode_list_lock); |
5c0d6b60 JK |
950 | list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) { |
951 | struct address_space *mapping = inode->i_mapping; | |
af309226 | 952 | struct block_device *bdev; |
5c0d6b60 JK |
953 | |
954 | spin_lock(&inode->i_lock); | |
955 | if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) || | |
956 | mapping->nrpages == 0) { | |
957 | spin_unlock(&inode->i_lock); | |
958 | continue; | |
959 | } | |
960 | __iget(inode); | |
961 | spin_unlock(&inode->i_lock); | |
74278da9 | 962 | spin_unlock(&blockdev_superblock->s_inode_list_lock); |
5c0d6b60 JK |
963 | /* |
964 | * We hold a reference to 'inode' so it couldn't have been | |
965 | * removed from s_inodes list while we dropped the | |
74278da9 | 966 | * s_inode_list_lock We cannot iput the inode now as we can |
5c0d6b60 | 967 | * be holding the last reference and we cannot iput it under |
74278da9 | 968 | * s_inode_list_lock. So we keep the reference and iput it |
5c0d6b60 JK |
969 | * later. |
970 | */ | |
971 | iput(old_inode); | |
972 | old_inode = inode; | |
af309226 | 973 | bdev = I_BDEV(inode); |
5c0d6b60 | 974 | |
a8698707 | 975 | mutex_lock(&bdev->bd_disk->open_mutex); |
9acf381f | 976 | if (!atomic_read(&bdev->bd_openers)) { |
1e03a36b CH |
977 | ; /* skip */ |
978 | } else if (wait) { | |
979 | /* | |
980 | * We keep the error status of individual mapping so | |
981 | * that applications can catch the writeback error using | |
982 | * fsync(2). See filemap_fdatawait_keep_errors() for | |
983 | * details. | |
984 | */ | |
985 | filemap_fdatawait_keep_errors(inode->i_mapping); | |
986 | } else { | |
987 | filemap_fdatawrite(inode->i_mapping); | |
988 | } | |
a8698707 | 989 | mutex_unlock(&bdev->bd_disk->open_mutex); |
5c0d6b60 | 990 | |
74278da9 | 991 | spin_lock(&blockdev_superblock->s_inode_list_lock); |
5c0d6b60 | 992 | } |
74278da9 | 993 | spin_unlock(&blockdev_superblock->s_inode_list_lock); |
5c0d6b60 JK |
994 | iput(old_inode); |
995 | } | |
2d985f8c EB |
996 | |
997 | /* | |
998 | * Handle STATX_DIOALIGN for block devices. | |
999 | * | |
1000 | * Note that the inode passed to this is the inode of a block device node file, | |
1001 | * not the block device's internal inode. Therefore it is *not* valid to use | |
1002 | * I_BDEV() here; the block device has to be looked up by i_rdev instead. | |
1003 | */ | |
1004 | void bdev_statx_dioalign(struct inode *inode, struct kstat *stat) | |
1005 | { | |
1006 | struct block_device *bdev; | |
1007 | ||
1008 | bdev = blkdev_get_no_open(inode->i_rdev); | |
1009 | if (!bdev) | |
1010 | return; | |
1011 | ||
1012 | stat->dio_mem_align = bdev_dma_alignment(bdev) + 1; | |
1013 | stat->dio_offset_align = bdev_logical_block_size(bdev); | |
1014 | stat->result_mask |= STATX_DIOALIGN; | |
1015 | ||
1016 | blkdev_put_no_open(bdev); | |
1017 | } |