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 | /** | |
227 | * freeze_bdev -- lock a filesystem and force it into a consistent state | |
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 | /** | |
271 | * thaw_bdev -- unlock filesystem | |
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 | ||
47a191fd MW |
307 | /** |
308 | * bdev_read_page() - Start reading a page from a block device | |
309 | * @bdev: The device to read the page from | |
310 | * @sector: The offset on the device to read the page to (need not be aligned) | |
311 | * @page: The page to read | |
312 | * | |
313 | * On entry, the page should be locked. It will be unlocked when the page | |
314 | * has been read. If the block driver implements rw_page synchronously, | |
315 | * that will be true on exit from this function, but it need not be. | |
316 | * | |
317 | * Errors returned by this function are usually "soft", eg out of memory, or | |
318 | * queue full; callers should try a different route to read this page rather | |
319 | * than propagate an error back up the stack. | |
320 | * | |
321 | * Return: negative errno if an error occurs, 0 if submission was successful. | |
322 | */ | |
323 | int bdev_read_page(struct block_device *bdev, sector_t sector, | |
324 | struct page *page) | |
325 | { | |
326 | const struct block_device_operations *ops = bdev->bd_disk->fops; | |
2e6edc95 DW |
327 | int result = -EOPNOTSUPP; |
328 | ||
f68eb1e7 | 329 | if (!ops->rw_page || bdev_get_integrity(bdev)) |
2e6edc95 DW |
330 | return result; |
331 | ||
025a3865 | 332 | result = blk_queue_enter(bdev_get_queue(bdev), 0); |
2e6edc95 DW |
333 | if (result) |
334 | return result; | |
3f289dcb TH |
335 | result = ops->rw_page(bdev, sector + get_start_sect(bdev), page, |
336 | REQ_OP_READ); | |
025a3865 | 337 | blk_queue_exit(bdev_get_queue(bdev)); |
2e6edc95 | 338 | return result; |
47a191fd | 339 | } |
47a191fd MW |
340 | |
341 | /** | |
342 | * bdev_write_page() - Start writing a page to a block device | |
343 | * @bdev: The device to write the page to | |
344 | * @sector: The offset on the device to write the page to (need not be aligned) | |
345 | * @page: The page to write | |
346 | * @wbc: The writeback_control for the write | |
347 | * | |
348 | * On entry, the page should be locked and not currently under writeback. | |
349 | * On exit, if the write started successfully, the page will be unlocked and | |
350 | * under writeback. If the write failed already (eg the driver failed to | |
351 | * queue the page to the device), the page will still be locked. If the | |
352 | * caller is a ->writepage implementation, it will need to unlock the page. | |
353 | * | |
354 | * Errors returned by this function are usually "soft", eg out of memory, or | |
355 | * queue full; callers should try a different route to write this page rather | |
356 | * than propagate an error back up the stack. | |
357 | * | |
358 | * Return: negative errno if an error occurs, 0 if submission was successful. | |
359 | */ | |
360 | int bdev_write_page(struct block_device *bdev, sector_t sector, | |
361 | struct page *page, struct writeback_control *wbc) | |
362 | { | |
363 | int result; | |
47a191fd | 364 | const struct block_device_operations *ops = bdev->bd_disk->fops; |
2e6edc95 | 365 | |
f68eb1e7 | 366 | if (!ops->rw_page || bdev_get_integrity(bdev)) |
47a191fd | 367 | return -EOPNOTSUPP; |
025a3865 | 368 | result = blk_queue_enter(bdev_get_queue(bdev), 0); |
2e6edc95 DW |
369 | if (result) |
370 | return result; | |
371 | ||
47a191fd | 372 | set_page_writeback(page); |
3f289dcb TH |
373 | result = ops->rw_page(bdev, sector + get_start_sect(bdev), page, |
374 | REQ_OP_WRITE); | |
f892760a | 375 | if (result) { |
47a191fd | 376 | end_page_writeback(page); |
f892760a MW |
377 | } else { |
378 | clean_page_buffers(page); | |
47a191fd | 379 | unlock_page(page); |
f892760a | 380 | } |
025a3865 | 381 | blk_queue_exit(bdev_get_queue(bdev)); |
47a191fd MW |
382 | return result; |
383 | } | |
47a191fd | 384 | |
1da177e4 LT |
385 | /* |
386 | * pseudo-fs | |
387 | */ | |
388 | ||
389 | static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock); | |
e18b890b | 390 | static struct kmem_cache * bdev_cachep __read_mostly; |
1da177e4 LT |
391 | |
392 | static struct inode *bdev_alloc_inode(struct super_block *sb) | |
393 | { | |
fd60b288 | 394 | struct bdev_inode *ei = alloc_inode_sb(sb, bdev_cachep, GFP_KERNEL); |
2d2f6f1b | 395 | |
1da177e4 LT |
396 | if (!ei) |
397 | return NULL; | |
2d2f6f1b | 398 | memset(&ei->bdev, 0, sizeof(ei->bdev)); |
1da177e4 LT |
399 | return &ei->vfs_inode; |
400 | } | |
401 | ||
41149cb0 | 402 | static void bdev_free_inode(struct inode *inode) |
1da177e4 | 403 | { |
15e3d2c5 CH |
404 | struct block_device *bdev = I_BDEV(inode); |
405 | ||
406 | free_percpu(bdev->bd_stats); | |
231926db | 407 | kfree(bdev->bd_meta_info); |
15e3d2c5 | 408 | |
889c05cc CH |
409 | if (!bdev_is_partition(bdev)) { |
410 | if (bdev->bd_disk && bdev->bd_disk->bdi) | |
411 | bdi_put(bdev->bd_disk->bdi); | |
340e8457 | 412 | kfree(bdev->bd_disk); |
889c05cc | 413 | } |
9451aa0a CH |
414 | |
415 | if (MAJOR(bdev->bd_dev) == BLOCK_EXT_MAJOR) | |
416 | blk_free_ext_minor(MINOR(bdev->bd_dev)); | |
417 | ||
41149cb0 | 418 | kmem_cache_free(bdev_cachep, BDEV_I(inode)); |
fa0d7e3d NP |
419 | } |
420 | ||
e6cb5382 | 421 | static void init_once(void *data) |
1da177e4 | 422 | { |
e6cb5382 | 423 | struct bdev_inode *ei = data; |
1da177e4 | 424 | |
a35afb83 | 425 | inode_init_once(&ei->vfs_inode); |
1da177e4 LT |
426 | } |
427 | ||
b57922d9 | 428 | static void bdev_evict_inode(struct inode *inode) |
1da177e4 | 429 | { |
91b0abe3 | 430 | truncate_inode_pages_final(&inode->i_data); |
b57922d9 | 431 | invalidate_inode_buffers(inode); /* is it needed here? */ |
dbd5768f | 432 | clear_inode(inode); |
1da177e4 LT |
433 | } |
434 | ||
ee9b6d61 | 435 | static const struct super_operations bdev_sops = { |
1da177e4 LT |
436 | .statfs = simple_statfs, |
437 | .alloc_inode = bdev_alloc_inode, | |
41149cb0 | 438 | .free_inode = bdev_free_inode, |
1da177e4 | 439 | .drop_inode = generic_delete_inode, |
b57922d9 | 440 | .evict_inode = bdev_evict_inode, |
1da177e4 LT |
441 | }; |
442 | ||
9030d16e | 443 | static int bd_init_fs_context(struct fs_context *fc) |
1da177e4 | 444 | { |
9030d16e DH |
445 | struct pseudo_fs_context *ctx = init_pseudo(fc, BDEVFS_MAGIC); |
446 | if (!ctx) | |
447 | return -ENOMEM; | |
448 | fc->s_iflags |= SB_I_CGROUPWB; | |
449 | ctx->ops = &bdev_sops; | |
450 | return 0; | |
1da177e4 LT |
451 | } |
452 | ||
453 | static struct file_system_type bd_type = { | |
454 | .name = "bdev", | |
9030d16e | 455 | .init_fs_context = bd_init_fs_context, |
1da177e4 LT |
456 | .kill_sb = kill_anon_super, |
457 | }; | |
458 | ||
a212b105 TH |
459 | struct super_block *blockdev_superblock __read_mostly; |
460 | EXPORT_SYMBOL_GPL(blockdev_superblock); | |
1da177e4 LT |
461 | |
462 | void __init bdev_cache_init(void) | |
463 | { | |
464 | int err; | |
ace8577a | 465 | static struct vfsmount *bd_mnt; |
c2acf7b9 | 466 | |
1da177e4 | 467 | bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode), |
fffb60f9 | 468 | 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT| |
5d097056 | 469 | SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC), |
20c2df83 | 470 | init_once); |
1da177e4 LT |
471 | err = register_filesystem(&bd_type); |
472 | if (err) | |
473 | panic("Cannot register bdev pseudo-fs"); | |
474 | bd_mnt = kern_mount(&bd_type); | |
1da177e4 LT |
475 | if (IS_ERR(bd_mnt)) |
476 | panic("Cannot create bdev pseudo-fs"); | |
ace8577a | 477 | blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */ |
1da177e4 LT |
478 | } |
479 | ||
22ae8ce8 | 480 | struct block_device *bdev_alloc(struct gendisk *disk, u8 partno) |
1da177e4 LT |
481 | { |
482 | struct block_device *bdev; | |
483 | struct inode *inode; | |
484 | ||
22ae8ce8 | 485 | inode = new_inode(blockdev_superblock); |
1da177e4 LT |
486 | if (!inode) |
487 | return NULL; | |
22ae8ce8 CH |
488 | inode->i_mode = S_IFBLK; |
489 | inode->i_rdev = 0; | |
490 | inode->i_data.a_ops = &def_blk_aops; | |
491 | mapping_set_gfp_mask(&inode->i_data, GFP_USER); | |
492 | ||
493 | bdev = I_BDEV(inode); | |
e6cb5382 | 494 | mutex_init(&bdev->bd_fsfreeze_mutex); |
22ae8ce8 | 495 | spin_lock_init(&bdev->bd_size_lock); |
22ae8ce8 | 496 | bdev->bd_partno = partno; |
22ae8ce8 | 497 | bdev->bd_inode = inode; |
17220ca5 | 498 | bdev->bd_queue = disk->queue; |
15e3d2c5 CH |
499 | bdev->bd_stats = alloc_percpu(struct disk_stats); |
500 | if (!bdev->bd_stats) { | |
501 | iput(inode); | |
502 | return NULL; | |
503 | } | |
06cc978d | 504 | bdev->bd_disk = disk; |
22ae8ce8 CH |
505 | return bdev; |
506 | } | |
1da177e4 | 507 | |
22ae8ce8 CH |
508 | void bdev_add(struct block_device *bdev, dev_t dev) |
509 | { | |
510 | bdev->bd_dev = dev; | |
511 | bdev->bd_inode->i_rdev = dev; | |
512 | bdev->bd_inode->i_ino = dev; | |
513 | insert_inode_hash(bdev->bd_inode); | |
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 | |
532 | * @whole: whole block device containing @bdev, may equal @bdev | |
533 | * @holder: holder trying to claim @bdev | |
534 | * | |
25985edc | 535 | * Test whether @bdev can be claimed by @holder. |
1a3cbbc5 TH |
536 | * |
537 | * CONTEXT: | |
538 | * spin_lock(&bdev_lock). | |
539 | * | |
540 | * RETURNS: | |
541 | * %true if @bdev can be claimed, %false otherwise. | |
542 | */ | |
543 | static bool bd_may_claim(struct block_device *bdev, struct block_device *whole, | |
544 | void *holder) | |
1da177e4 | 545 | { |
1da177e4 | 546 | if (bdev->bd_holder == holder) |
1a3cbbc5 | 547 | return true; /* already a holder */ |
1da177e4 | 548 | else if (bdev->bd_holder != NULL) |
1a3cbbc5 | 549 | return false; /* held by someone else */ |
bcc7f5b4 | 550 | else if (whole == bdev) |
1a3cbbc5 | 551 | return true; /* is a whole device which isn't held */ |
1da177e4 | 552 | |
e525fd89 | 553 | else if (whole->bd_holder == bd_may_claim) |
1a3cbbc5 TH |
554 | return true; /* is a partition of a device that is being partitioned */ |
555 | else if (whole->bd_holder != NULL) | |
556 | return false; /* is a partition of a held device */ | |
1da177e4 | 557 | else |
1a3cbbc5 TH |
558 | return true; /* is a partition of an un-held device */ |
559 | } | |
560 | ||
6b4517a7 | 561 | /** |
58e46ed9 | 562 | * bd_prepare_to_claim - claim a block device |
6b4517a7 | 563 | * @bdev: block device of interest |
6b4517a7 TH |
564 | * @holder: holder trying to claim @bdev |
565 | * | |
58e46ed9 CH |
566 | * Claim @bdev. This function fails if @bdev is already claimed by another |
567 | * holder and waits if another claiming is in progress. return, the caller | |
568 | * has ownership of bd_claiming and bd_holder[s]. | |
6b4517a7 TH |
569 | * |
570 | * RETURNS: | |
571 | * 0 if @bdev can be claimed, -EBUSY otherwise. | |
572 | */ | |
37c3fc9a | 573 | int bd_prepare_to_claim(struct block_device *bdev, void *holder) |
6b4517a7 | 574 | { |
37c3fc9a CH |
575 | struct block_device *whole = bdev_whole(bdev); |
576 | ||
577 | if (WARN_ON_ONCE(!holder)) | |
578 | return -EINVAL; | |
6b4517a7 | 579 | retry: |
58e46ed9 | 580 | spin_lock(&bdev_lock); |
6b4517a7 | 581 | /* if someone else claimed, fail */ |
58e46ed9 CH |
582 | if (!bd_may_claim(bdev, whole, holder)) { |
583 | spin_unlock(&bdev_lock); | |
6b4517a7 | 584 | return -EBUSY; |
58e46ed9 | 585 | } |
6b4517a7 | 586 | |
e75aa858 TH |
587 | /* if claiming is already in progress, wait for it to finish */ |
588 | if (whole->bd_claiming) { | |
6b4517a7 TH |
589 | wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0); |
590 | DEFINE_WAIT(wait); | |
591 | ||
592 | prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE); | |
593 | spin_unlock(&bdev_lock); | |
594 | schedule(); | |
595 | finish_wait(wq, &wait); | |
6b4517a7 TH |
596 | goto retry; |
597 | } | |
598 | ||
599 | /* yay, all mine */ | |
58e46ed9 CH |
600 | whole->bd_claiming = holder; |
601 | spin_unlock(&bdev_lock); | |
6b4517a7 TH |
602 | return 0; |
603 | } | |
ecbe6bc0 | 604 | EXPORT_SYMBOL_GPL(bd_prepare_to_claim); /* only for the loop driver */ |
6b4517a7 | 605 | |
89e524c0 JK |
606 | static void bd_clear_claiming(struct block_device *whole, void *holder) |
607 | { | |
608 | lockdep_assert_held(&bdev_lock); | |
609 | /* tell others that we're done */ | |
610 | BUG_ON(whole->bd_claiming != holder); | |
611 | whole->bd_claiming = NULL; | |
612 | wake_up_bit(&whole->bd_claiming, 0); | |
613 | } | |
614 | ||
615 | /** | |
616 | * bd_finish_claiming - finish claiming of a block device | |
617 | * @bdev: block device of interest | |
89e524c0 JK |
618 | * @holder: holder that has claimed @bdev |
619 | * | |
620 | * Finish exclusive open of a block device. Mark the device as exlusively | |
621 | * open by the holder and wake up all waiters for exclusive open to finish. | |
622 | */ | |
37c3fc9a | 623 | static void bd_finish_claiming(struct block_device *bdev, void *holder) |
89e524c0 | 624 | { |
37c3fc9a CH |
625 | struct block_device *whole = bdev_whole(bdev); |
626 | ||
89e524c0 JK |
627 | spin_lock(&bdev_lock); |
628 | BUG_ON(!bd_may_claim(bdev, whole, holder)); | |
629 | /* | |
630 | * Note that for a whole device bd_holders will be incremented twice, | |
631 | * and bd_holder will be set to bd_may_claim before being set to holder | |
632 | */ | |
633 | whole->bd_holders++; | |
634 | whole->bd_holder = bd_may_claim; | |
635 | bdev->bd_holders++; | |
636 | bdev->bd_holder = holder; | |
637 | bd_clear_claiming(whole, holder); | |
638 | spin_unlock(&bdev_lock); | |
639 | } | |
89e524c0 JK |
640 | |
641 | /** | |
642 | * bd_abort_claiming - abort claiming of a block device | |
643 | * @bdev: block device of interest | |
89e524c0 JK |
644 | * @holder: holder that has claimed @bdev |
645 | * | |
646 | * Abort claiming of a block device when the exclusive open failed. This can be | |
647 | * also used when exclusive open is not actually desired and we just needed | |
648 | * to block other exclusive openers for a while. | |
649 | */ | |
37c3fc9a | 650 | void bd_abort_claiming(struct block_device *bdev, void *holder) |
89e524c0 JK |
651 | { |
652 | spin_lock(&bdev_lock); | |
37c3fc9a | 653 | bd_clear_claiming(bdev_whole(bdev), holder); |
89e524c0 JK |
654 | spin_unlock(&bdev_lock); |
655 | } | |
656 | EXPORT_SYMBOL(bd_abort_claiming); | |
6b4517a7 | 657 | |
c8276b95 CH |
658 | static void blkdev_flush_mapping(struct block_device *bdev) |
659 | { | |
660 | WARN_ON_ONCE(bdev->bd_holders); | |
661 | sync_blockdev(bdev); | |
662 | kill_bdev(bdev); | |
663 | bdev_write_inode(bdev); | |
664 | } | |
37be4124 | 665 | |
362529d9 | 666 | static int blkdev_get_whole(struct block_device *bdev, fmode_t mode) |
a1548b67 | 667 | { |
142fe8f4 | 668 | struct gendisk *disk = bdev->bd_disk; |
af22fef3 | 669 | int ret; |
a1548b67 | 670 | |
362529d9 CH |
671 | if (disk->fops->open) { |
672 | ret = disk->fops->open(bdev, mode); | |
673 | if (ret) { | |
674 | /* avoid ghost partitions on a removed medium */ | |
675 | if (ret == -ENOMEDIUM && | |
676 | test_bit(GD_NEED_PART_SCAN, &disk->state)) | |
0384264e | 677 | bdev_disk_changed(disk, true); |
362529d9 CH |
678 | return ret; |
679 | } | |
d981cb5b | 680 | } |
a1548b67 | 681 | |
9acf381f | 682 | if (!atomic_read(&bdev->bd_openers)) |
362529d9 | 683 | set_init_blocksize(bdev); |
362529d9 | 684 | if (test_bit(GD_NEED_PART_SCAN, &disk->state)) |
0384264e | 685 | bdev_disk_changed(disk, false); |
9acf381f | 686 | atomic_inc(&bdev->bd_openers); |
483546c1 | 687 | return 0; |
362529d9 | 688 | } |
a1548b67 | 689 | |
c8276b95 CH |
690 | static void blkdev_put_whole(struct block_device *bdev, fmode_t mode) |
691 | { | |
9acf381f | 692 | if (atomic_dec_and_test(&bdev->bd_openers)) |
c8276b95 CH |
693 | blkdev_flush_mapping(bdev); |
694 | if (bdev->bd_disk->fops->release) | |
695 | bdev->bd_disk->fops->release(bdev->bd_disk, mode); | |
a1548b67 CH |
696 | } |
697 | ||
362529d9 | 698 | static int blkdev_get_part(struct block_device *part, fmode_t mode) |
1da177e4 | 699 | { |
362529d9 | 700 | struct gendisk *disk = part->bd_disk; |
362529d9 | 701 | int ret; |
5b642d8b | 702 | |
9acf381f | 703 | if (atomic_read(&part->bd_openers)) |
362529d9 | 704 | goto done; |
6c60ff04 | 705 | |
9d3b8813 | 706 | ret = blkdev_get_whole(bdev_whole(part), mode); |
a8698707 | 707 | if (ret) |
9d3b8813 | 708 | return ret; |
7e69723f | 709 | |
362529d9 CH |
710 | ret = -ENXIO; |
711 | if (!bdev_nr_sectors(part)) | |
712 | goto out_blkdev_put; | |
7e69723f | 713 | |
ab4b5705 | 714 | disk->open_partitions++; |
362529d9 | 715 | set_init_blocksize(part); |
362529d9 | 716 | done: |
9acf381f | 717 | atomic_inc(&part->bd_openers); |
1da177e4 | 718 | return 0; |
5a023cdb | 719 | |
362529d9 | 720 | out_blkdev_put: |
9d3b8813 | 721 | blkdev_put_whole(bdev_whole(part), mode); |
362529d9 | 722 | return ret; |
1da177e4 | 723 | } |
5b56b6ed | 724 | |
c8276b95 CH |
725 | static void blkdev_put_part(struct block_device *part, fmode_t mode) |
726 | { | |
727 | struct block_device *whole = bdev_whole(part); | |
03e26279 | 728 | |
9acf381f | 729 | if (!atomic_dec_and_test(&part->bd_openers)) |
c8276b95 CH |
730 | return; |
731 | blkdev_flush_mapping(part); | |
ab4b5705 | 732 | whole->bd_disk->open_partitions--; |
c8276b95 | 733 | blkdev_put_whole(whole, mode); |
1da177e4 LT |
734 | } |
735 | ||
22ae8ce8 CH |
736 | struct block_device *blkdev_get_no_open(dev_t dev) |
737 | { | |
738 | struct block_device *bdev; | |
9d3b8813 | 739 | struct inode *inode; |
22ae8ce8 | 740 | |
9d3b8813 | 741 | inode = ilookup(blockdev_superblock, dev); |
fbdee71b | 742 | if (!inode && IS_ENABLED(CONFIG_BLOCK_LEGACY_AUTOLOAD)) { |
22ae8ce8 | 743 | blk_request_module(dev); |
9d3b8813 | 744 | inode = ilookup(blockdev_superblock, dev); |
fbdee71b CH |
745 | if (inode) |
746 | pr_warn_ratelimited( | |
451f0b6f | 747 | "block device autoloading is deprecated and will be removed.\n"); |
22ae8ce8 | 748 | } |
fbdee71b CH |
749 | if (!inode) |
750 | return NULL; | |
22ae8ce8 | 751 | |
9d3b8813 CH |
752 | /* switch from the inode reference to a device mode one: */ |
753 | bdev = &BDEV_I(inode)->bdev; | |
754 | if (!kobject_get_unless_zero(&bdev->bd_device.kobj)) | |
755 | bdev = NULL; | |
756 | iput(inode); | |
22ae8ce8 | 757 | return bdev; |
22ae8ce8 CH |
758 | } |
759 | ||
760 | void blkdev_put_no_open(struct block_device *bdev) | |
761 | { | |
9d3b8813 | 762 | put_device(&bdev->bd_device); |
22ae8ce8 CH |
763 | } |
764 | ||
d4d77629 | 765 | /** |
4e7b5671 CH |
766 | * blkdev_get_by_dev - open a block device by device number |
767 | * @dev: device number of block device to open | |
d4d77629 TH |
768 | * @mode: FMODE_* mask |
769 | * @holder: exclusive holder identifier | |
770 | * | |
4e7b5671 CH |
771 | * Open the block device described by device number @dev. If @mode includes |
772 | * %FMODE_EXCL, the block device is opened with exclusive access. Specifying | |
773 | * %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may nest for | |
774 | * the same @holder. | |
d4d77629 | 775 | * |
4e7b5671 CH |
776 | * Use this interface ONLY if you really do not have anything better - i.e. when |
777 | * you are behind a truly sucky interface and all you are given is a device | |
778 | * number. Everything else should use blkdev_get_by_path(). | |
d4d77629 TH |
779 | * |
780 | * CONTEXT: | |
781 | * Might sleep. | |
782 | * | |
783 | * RETURNS: | |
4e7b5671 | 784 | * Reference to the block_device on success, ERR_PTR(-errno) on failure. |
d4d77629 | 785 | */ |
4e7b5671 | 786 | struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder) |
1da177e4 | 787 | { |
5b56b6ed | 788 | bool unblock_events = true; |
4e7b5671 | 789 | struct block_device *bdev; |
5b56b6ed | 790 | struct gendisk *disk; |
5b56b6ed | 791 | int ret; |
e525fd89 | 792 | |
7918f0f6 | 793 | ret = devcgroup_check_permission(DEVCG_DEV_BLOCK, |
4e7b5671 | 794 | MAJOR(dev), MINOR(dev), |
7918f0f6 CH |
795 | ((mode & FMODE_READ) ? DEVCG_ACC_READ : 0) | |
796 | ((mode & FMODE_WRITE) ? DEVCG_ACC_WRITE : 0)); | |
e5c7fb40 | 797 | if (ret) |
4e7b5671 CH |
798 | return ERR_PTR(ret); |
799 | ||
22ae8ce8 CH |
800 | bdev = blkdev_get_no_open(dev); |
801 | if (!bdev) | |
802 | return ERR_PTR(-ENXIO); | |
803 | disk = bdev->bd_disk; | |
e5c7fb40 | 804 | |
5b56b6ed | 805 | if (mode & FMODE_EXCL) { |
37c3fc9a | 806 | ret = bd_prepare_to_claim(bdev, holder); |
5b56b6ed | 807 | if (ret) |
37c3fc9a | 808 | goto put_blkdev; |
5b56b6ed CH |
809 | } |
810 | ||
811 | disk_block_events(disk); | |
812 | ||
a8698707 | 813 | mutex_lock(&disk->open_mutex); |
362529d9 | 814 | ret = -ENXIO; |
50b4aecf | 815 | if (!disk_live(disk)) |
362529d9 | 816 | goto abort_claiming; |
efcf5932 ML |
817 | if (!try_module_get(disk->fops->owner)) |
818 | goto abort_claiming; | |
362529d9 CH |
819 | if (bdev_is_partition(bdev)) |
820 | ret = blkdev_get_part(bdev, mode); | |
821 | else | |
822 | ret = blkdev_get_whole(bdev, mode); | |
22ae8ce8 | 823 | if (ret) |
efcf5932 | 824 | goto put_module; |
22ae8ce8 | 825 | if (mode & FMODE_EXCL) { |
37c3fc9a | 826 | bd_finish_claiming(bdev, holder); |
5b56b6ed CH |
827 | |
828 | /* | |
829 | * Block event polling for write claims if requested. Any write | |
830 | * holder makes the write_holder state stick until all are | |
831 | * released. This is good enough and tracking individual | |
832 | * writeable reference is too fragile given the way @mode is | |
833 | * used in blkdev_get/put(). | |
834 | */ | |
835 | if ((mode & FMODE_WRITE) && !bdev->bd_write_holder && | |
1545e0b4 | 836 | (disk->event_flags & DISK_EVENT_FLAG_BLOCK_ON_EXCL_WRITE)) { |
5b56b6ed CH |
837 | bdev->bd_write_holder = true; |
838 | unblock_events = false; | |
839 | } | |
840 | } | |
a8698707 | 841 | mutex_unlock(&disk->open_mutex); |
5b56b6ed CH |
842 | |
843 | if (unblock_events) | |
844 | disk_unblock_events(disk); | |
22ae8ce8 | 845 | return bdev; |
efcf5932 ML |
846 | put_module: |
847 | module_put(disk->fops->owner); | |
22ae8ce8 CH |
848 | abort_claiming: |
849 | if (mode & FMODE_EXCL) | |
37c3fc9a | 850 | bd_abort_claiming(bdev, holder); |
a8698707 | 851 | mutex_unlock(&disk->open_mutex); |
22ae8ce8 | 852 | disk_unblock_events(disk); |
22ae8ce8 CH |
853 | put_blkdev: |
854 | blkdev_put_no_open(bdev); | |
22ae8ce8 | 855 | return ERR_PTR(ret); |
37be4124 | 856 | } |
4e7b5671 | 857 | EXPORT_SYMBOL(blkdev_get_by_dev); |
1da177e4 | 858 | |
d4d77629 TH |
859 | /** |
860 | * blkdev_get_by_path - open a block device by name | |
861 | * @path: path to the block device to open | |
862 | * @mode: FMODE_* mask | |
863 | * @holder: exclusive holder identifier | |
864 | * | |
4e7b5671 CH |
865 | * Open the block device described by the device file at @path. If @mode |
866 | * includes %FMODE_EXCL, the block device is opened with exclusive access. | |
867 | * Specifying %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may | |
868 | * nest for the same @holder. | |
d4d77629 TH |
869 | * |
870 | * CONTEXT: | |
871 | * Might sleep. | |
872 | * | |
873 | * RETURNS: | |
4e7b5671 | 874 | * Reference to the block_device on success, ERR_PTR(-errno) on failure. |
d4d77629 TH |
875 | */ |
876 | struct block_device *blkdev_get_by_path(const char *path, fmode_t mode, | |
877 | void *holder) | |
878 | { | |
879 | struct block_device *bdev; | |
4e7b5671 CH |
880 | dev_t dev; |
881 | int error; | |
d4d77629 | 882 | |
4e7b5671 CH |
883 | error = lookup_bdev(path, &dev); |
884 | if (error) | |
885 | return ERR_PTR(error); | |
d4d77629 | 886 | |
4e7b5671 CH |
887 | bdev = blkdev_get_by_dev(dev, mode, holder); |
888 | if (!IS_ERR(bdev) && (mode & FMODE_WRITE) && bdev_read_only(bdev)) { | |
e51900f7 CE |
889 | blkdev_put(bdev, mode); |
890 | return ERR_PTR(-EACCES); | |
891 | } | |
892 | ||
d4d77629 TH |
893 | return bdev; |
894 | } | |
895 | EXPORT_SYMBOL(blkdev_get_by_path); | |
896 | ||
4385bab1 | 897 | void blkdev_put(struct block_device *bdev, fmode_t mode) |
2e7b651d | 898 | { |
2e7b651d PZ |
899 | struct gendisk *disk = bdev->bd_disk; |
900 | ||
b849dd84 DA |
901 | /* |
902 | * Sync early if it looks like we're the last one. If someone else | |
903 | * opens the block device between now and the decrement of bd_openers | |
904 | * then we did a sync that we didn't need to, but that's not the end | |
905 | * of the world and we want to avoid long (could be several minute) | |
906 | * syncs while holding the mutex. | |
907 | */ | |
9acf381f | 908 | if (atomic_read(&bdev->bd_openers) == 1) |
b849dd84 DA |
909 | sync_blockdev(bdev); |
910 | ||
a8698707 | 911 | mutex_lock(&disk->open_mutex); |
e525fd89 | 912 | if (mode & FMODE_EXCL) { |
a954ea81 | 913 | struct block_device *whole = bdev_whole(bdev); |
6a027eff TH |
914 | bool bdev_free; |
915 | ||
916 | /* | |
917 | * Release a claim on the device. The holder fields | |
a8698707 | 918 | * are protected with bdev_lock. open_mutex is to |
6a027eff TH |
919 | * synchronize disk_holder unlinking. |
920 | */ | |
6a027eff TH |
921 | spin_lock(&bdev_lock); |
922 | ||
923 | WARN_ON_ONCE(--bdev->bd_holders < 0); | |
a954ea81 | 924 | WARN_ON_ONCE(--whole->bd_holders < 0); |
6a027eff | 925 | |
6a027eff TH |
926 | if ((bdev_free = !bdev->bd_holders)) |
927 | bdev->bd_holder = NULL; | |
a954ea81 CH |
928 | if (!whole->bd_holders) |
929 | whole->bd_holder = NULL; | |
6a027eff TH |
930 | |
931 | spin_unlock(&bdev_lock); | |
932 | ||
77ea887e TH |
933 | /* |
934 | * If this was the last claim, remove holder link and | |
935 | * unblock evpoll if it was a write holder. | |
936 | */ | |
85ef06d1 | 937 | if (bdev_free && bdev->bd_write_holder) { |
5b56b6ed | 938 | disk_unblock_events(disk); |
85ef06d1 | 939 | bdev->bd_write_holder = false; |
77ea887e | 940 | } |
6936217c | 941 | } |
77ea887e | 942 | |
85ef06d1 TH |
943 | /* |
944 | * Trigger event checking and tell drivers to flush MEDIA_CHANGE | |
945 | * event. This is to ensure detection of media removal commanded | |
946 | * from userland - e.g. eject(1). | |
947 | */ | |
5b56b6ed | 948 | disk_flush_events(disk, DISK_EVENT_MEDIA_CHANGE); |
85ef06d1 | 949 | |
c8276b95 CH |
950 | if (bdev_is_partition(bdev)) |
951 | blkdev_put_part(bdev, mode); | |
952 | else | |
953 | blkdev_put_whole(bdev, mode); | |
a8698707 CH |
954 | mutex_unlock(&disk->open_mutex); |
955 | ||
efcf5932 | 956 | module_put(disk->fops->owner); |
22ae8ce8 | 957 | blkdev_put_no_open(bdev); |
37be4124 | 958 | } |
2e7b651d PZ |
959 | EXPORT_SYMBOL(blkdev_put); |
960 | ||
1da177e4 | 961 | /** |
0ba4566c MWO |
962 | * lookup_bdev() - Look up a struct block_device by name. |
963 | * @pathname: Name of the block device in the filesystem. | |
964 | * @dev: Pointer to the block device's dev_t, if found. | |
1da177e4 | 965 | * |
057178cf | 966 | * Lookup the block device's dev_t at @pathname in the current |
0ba4566c | 967 | * namespace if possible and return it in @dev. |
057178cf | 968 | * |
0ba4566c MWO |
969 | * Context: May sleep. |
970 | * Return: 0 if succeeded, negative errno otherwise. | |
1da177e4 | 971 | */ |
4e7b5671 | 972 | int lookup_bdev(const char *pathname, dev_t *dev) |
1da177e4 | 973 | { |
1da177e4 | 974 | struct inode *inode; |
421748ec | 975 | struct path path; |
1da177e4 LT |
976 | int error; |
977 | ||
421748ec | 978 | if (!pathname || !*pathname) |
4e7b5671 | 979 | return -EINVAL; |
1da177e4 | 980 | |
421748ec | 981 | error = kern_path(pathname, LOOKUP_FOLLOW, &path); |
1da177e4 | 982 | if (error) |
4e7b5671 | 983 | return error; |
1da177e4 | 984 | |
bb668734 | 985 | inode = d_backing_inode(path.dentry); |
1da177e4 LT |
986 | error = -ENOTBLK; |
987 | if (!S_ISBLK(inode->i_mode)) | |
4e7b5671 | 988 | goto out_path_put; |
1da177e4 | 989 | error = -EACCES; |
a2982cc9 | 990 | if (!may_open_dev(&path)) |
4e7b5671 CH |
991 | goto out_path_put; |
992 | ||
993 | *dev = inode->i_rdev; | |
994 | error = 0; | |
995 | out_path_put: | |
421748ec | 996 | path_put(&path); |
4e7b5671 | 997 | return error; |
1da177e4 | 998 | } |
d5686b44 | 999 | EXPORT_SYMBOL(lookup_bdev); |
1da177e4 | 1000 | |
93b270f7 | 1001 | int __invalidate_device(struct block_device *bdev, bool kill_dirty) |
b71e8a4c DH |
1002 | { |
1003 | struct super_block *sb = get_super(bdev); | |
1004 | int res = 0; | |
1005 | ||
1006 | if (sb) { | |
1007 | /* | |
1008 | * no need to lock the super, get_super holds the | |
1009 | * read mutex so the filesystem cannot go away | |
1010 | * under us (->put_super runs with the write lock | |
1011 | * hold). | |
1012 | */ | |
1013 | shrink_dcache_sb(sb); | |
93b270f7 | 1014 | res = invalidate_inodes(sb, kill_dirty); |
b71e8a4c DH |
1015 | drop_super(sb); |
1016 | } | |
f98393a6 | 1017 | invalidate_bdev(bdev); |
b71e8a4c DH |
1018 | return res; |
1019 | } | |
1020 | EXPORT_SYMBOL(__invalidate_device); | |
5c0d6b60 | 1021 | |
1e03a36b | 1022 | void sync_bdevs(bool wait) |
5c0d6b60 JK |
1023 | { |
1024 | struct inode *inode, *old_inode = NULL; | |
1025 | ||
74278da9 | 1026 | spin_lock(&blockdev_superblock->s_inode_list_lock); |
5c0d6b60 JK |
1027 | list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) { |
1028 | struct address_space *mapping = inode->i_mapping; | |
af309226 | 1029 | struct block_device *bdev; |
5c0d6b60 JK |
1030 | |
1031 | spin_lock(&inode->i_lock); | |
1032 | if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) || | |
1033 | mapping->nrpages == 0) { | |
1034 | spin_unlock(&inode->i_lock); | |
1035 | continue; | |
1036 | } | |
1037 | __iget(inode); | |
1038 | spin_unlock(&inode->i_lock); | |
74278da9 | 1039 | spin_unlock(&blockdev_superblock->s_inode_list_lock); |
5c0d6b60 JK |
1040 | /* |
1041 | * We hold a reference to 'inode' so it couldn't have been | |
1042 | * removed from s_inodes list while we dropped the | |
74278da9 | 1043 | * s_inode_list_lock We cannot iput the inode now as we can |
5c0d6b60 | 1044 | * be holding the last reference and we cannot iput it under |
74278da9 | 1045 | * s_inode_list_lock. So we keep the reference and iput it |
5c0d6b60 JK |
1046 | * later. |
1047 | */ | |
1048 | iput(old_inode); | |
1049 | old_inode = inode; | |
af309226 | 1050 | bdev = I_BDEV(inode); |
5c0d6b60 | 1051 | |
a8698707 | 1052 | mutex_lock(&bdev->bd_disk->open_mutex); |
9acf381f | 1053 | if (!atomic_read(&bdev->bd_openers)) { |
1e03a36b CH |
1054 | ; /* skip */ |
1055 | } else if (wait) { | |
1056 | /* | |
1057 | * We keep the error status of individual mapping so | |
1058 | * that applications can catch the writeback error using | |
1059 | * fsync(2). See filemap_fdatawait_keep_errors() for | |
1060 | * details. | |
1061 | */ | |
1062 | filemap_fdatawait_keep_errors(inode->i_mapping); | |
1063 | } else { | |
1064 | filemap_fdatawrite(inode->i_mapping); | |
1065 | } | |
a8698707 | 1066 | mutex_unlock(&bdev->bd_disk->open_mutex); |
5c0d6b60 | 1067 | |
74278da9 | 1068 | spin_lock(&blockdev_superblock->s_inode_list_lock); |
5c0d6b60 | 1069 | } |
74278da9 | 1070 | spin_unlock(&blockdev_superblock->s_inode_list_lock); |
5c0d6b60 JK |
1071 | iput(old_inode); |
1072 | } | |
2d985f8c EB |
1073 | |
1074 | /* | |
1075 | * Handle STATX_DIOALIGN for block devices. | |
1076 | * | |
1077 | * Note that the inode passed to this is the inode of a block device node file, | |
1078 | * not the block device's internal inode. Therefore it is *not* valid to use | |
1079 | * I_BDEV() here; the block device has to be looked up by i_rdev instead. | |
1080 | */ | |
1081 | void bdev_statx_dioalign(struct inode *inode, struct kstat *stat) | |
1082 | { | |
1083 | struct block_device *bdev; | |
1084 | ||
1085 | bdev = blkdev_get_no_open(inode->i_rdev); | |
1086 | if (!bdev) | |
1087 | return; | |
1088 | ||
1089 | stat->dio_mem_align = bdev_dma_alignment(bdev) + 1; | |
1090 | stat->dio_offset_align = bdev_logical_block_size(bdev); | |
1091 | stat->result_mask |= STATX_DIOALIGN; | |
1092 | ||
1093 | blkdev_put_no_open(bdev); | |
1094 | } |