1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
5 * Copyright (C) 2016 - 2020 Christoph Hellwig
6 */
7
8 #include <linux/init.h>
9 #include <linux/mm.h>
10 #include <linux/slab.h>
11 #include <linux/kmod.h>
12 #include <linux/major.h>
13 #include <linux/device_cgroup.h>
14 #include <linux/blkdev.h>
15 #include <linux/blk-integrity.h>
16 #include <linux/backing-dev.h>
17 #include <linux/module.h>
18 #include <linux/blkpg.h>
19 #include <linux/magic.h>
20 #include <linux/buffer_head.h>
21 #include <linux/swap.h>
22 #include <linux/writeback.h>
23 #include <linux/mount.h>
24 #include <linux/pseudo_fs.h>
25 #include <linux/uio.h>
26 #include <linux/namei.h>
27 #include <linux/cleancache.h>
28 #include <linux/part_stat.h>
29 #include <linux/uaccess.h>
30 #include <linux/stat.h>
31 #include "../fs/internal.h"
32 #include "blk.h"
33
34 struct bdev_inode {
35 struct block_device bdev;
36 struct inode vfs_inode;
37 };
38
BDEV_I(struct inode * inode)39 static inline struct bdev_inode *BDEV_I(struct inode *inode)
40 {
41 return container_of(inode, struct bdev_inode, vfs_inode);
42 }
43
I_BDEV(struct inode * inode)44 struct block_device *I_BDEV(struct inode *inode)
45 {
46 return &BDEV_I(inode)->bdev;
47 }
48 EXPORT_SYMBOL(I_BDEV);
49
bdev_write_inode(struct block_device * bdev)50 static void bdev_write_inode(struct block_device *bdev)
51 {
52 struct inode *inode = bdev->bd_inode;
53 int ret;
54
55 spin_lock(&inode->i_lock);
56 while (inode->i_state & I_DIRTY) {
57 spin_unlock(&inode->i_lock);
58 ret = write_inode_now(inode, true);
59 if (ret)
60 pr_warn_ratelimited(
61 "VFS: Dirty inode writeback failed for block device %pg (err=%d).\n",
62 bdev, ret);
63 spin_lock(&inode->i_lock);
64 }
65 spin_unlock(&inode->i_lock);
66 }
67
68 /* Kill _all_ buffers and pagecache , dirty or not.. */
kill_bdev(struct block_device * bdev)69 static void kill_bdev(struct block_device *bdev)
70 {
71 struct address_space *mapping = bdev->bd_inode->i_mapping;
72
73 if (mapping_empty(mapping))
74 return;
75
76 invalidate_bh_lrus();
77 truncate_inode_pages(mapping, 0);
78 }
79
80 /* Invalidate clean unused buffers and pagecache. */
invalidate_bdev(struct block_device * bdev)81 void invalidate_bdev(struct block_device *bdev)
82 {
83 struct address_space *mapping = bdev->bd_inode->i_mapping;
84
85 if (mapping->nrpages) {
86 invalidate_bh_lrus();
87 lru_add_drain_all(); /* make sure all lru add caches are flushed */
88 invalidate_mapping_pages(mapping, 0, -1);
89 }
90 /* 99% of the time, we don't need to flush the cleancache on the bdev.
91 * But, for the strange corners, lets be cautious
92 */
93 cleancache_invalidate_inode(mapping);
94 }
95 EXPORT_SYMBOL(invalidate_bdev);
96
97 /*
98 * Drop all buffers & page cache for given bdev range. This function bails
99 * with error if bdev has other exclusive owner (such as filesystem).
100 */
truncate_bdev_range(struct block_device * bdev,blk_mode_t mode,loff_t lstart,loff_t lend)101 int truncate_bdev_range(struct block_device *bdev, blk_mode_t mode,
102 loff_t lstart, loff_t lend)
103 {
104 /*
105 * If we don't hold exclusive handle for the device, upgrade to it
106 * while we discard the buffer cache to avoid discarding buffers
107 * under live filesystem.
108 */
109 if (!(mode & BLK_OPEN_EXCL)) {
110 int err = bd_prepare_to_claim(bdev, truncate_bdev_range, NULL);
111 if (err)
112 goto invalidate;
113 }
114
115 truncate_inode_pages_range(bdev->bd_inode->i_mapping, lstart, lend);
116 if (!(mode & BLK_OPEN_EXCL))
117 bd_abort_claiming(bdev, truncate_bdev_range);
118 return 0;
119
120 invalidate:
121 /*
122 * Someone else has handle exclusively open. Try invalidating instead.
123 * The 'end' argument is inclusive so the rounding is safe.
124 */
125 return invalidate_inode_pages2_range(bdev->bd_inode->i_mapping,
126 lstart >> PAGE_SHIFT,
127 lend >> PAGE_SHIFT);
128 }
129
set_init_blocksize(struct block_device * bdev)130 static void set_init_blocksize(struct block_device *bdev)
131 {
132 unsigned int bsize = bdev_logical_block_size(bdev);
133 loff_t size = i_size_read(bdev->bd_inode);
134
135 while (bsize < PAGE_SIZE) {
136 if (size & bsize)
137 break;
138 bsize <<= 1;
139 }
140 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
141 }
142
set_blocksize(struct block_device * bdev,int size)143 int set_blocksize(struct block_device *bdev, int size)
144 {
145 /* Size must be a power of two, and between 512 and PAGE_SIZE */
146 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
147 return -EINVAL;
148
149 /* Size cannot be smaller than the size supported by the device */
150 if (size < bdev_logical_block_size(bdev))
151 return -EINVAL;
152
153 /* Don't change the size if it is same as current */
154 if (bdev->bd_inode->i_blkbits != blksize_bits(size)) {
155 sync_blockdev(bdev);
156 bdev->bd_inode->i_blkbits = blksize_bits(size);
157 kill_bdev(bdev);
158 }
159 return 0;
160 }
161
162 EXPORT_SYMBOL(set_blocksize);
163
sb_set_blocksize(struct super_block * sb,int size)164 int sb_set_blocksize(struct super_block *sb, int size)
165 {
166 if (set_blocksize(sb->s_bdev, size))
167 return 0;
168 /* If we get here, we know size is power of two
169 * and it's value is between 512 and PAGE_SIZE */
170 sb->s_blocksize = size;
171 sb->s_blocksize_bits = blksize_bits(size);
172 return sb->s_blocksize;
173 }
174
175 EXPORT_SYMBOL(sb_set_blocksize);
176
sb_min_blocksize(struct super_block * sb,int size)177 int sb_min_blocksize(struct super_block *sb, int size)
178 {
179 int minsize = bdev_logical_block_size(sb->s_bdev);
180 if (size < minsize)
181 size = minsize;
182 return sb_set_blocksize(sb, size);
183 }
184
185 EXPORT_SYMBOL(sb_min_blocksize);
186
sync_blockdev_nowait(struct block_device * bdev)187 int sync_blockdev_nowait(struct block_device *bdev)
188 {
189 if (!bdev)
190 return 0;
191 return filemap_flush(bdev->bd_inode->i_mapping);
192 }
193 EXPORT_SYMBOL_GPL(sync_blockdev_nowait);
194
195 /*
196 * Write out and wait upon all the dirty data associated with a block
197 * device via its mapping. Does not take the superblock lock.
198 */
sync_blockdev(struct block_device * bdev)199 int sync_blockdev(struct block_device *bdev)
200 {
201 if (!bdev)
202 return 0;
203 return filemap_write_and_wait(bdev->bd_inode->i_mapping);
204 }
205 EXPORT_SYMBOL(sync_blockdev);
206
sync_blockdev_range(struct block_device * bdev,loff_t lstart,loff_t lend)207 int sync_blockdev_range(struct block_device *bdev, loff_t lstart, loff_t lend)
208 {
209 return filemap_write_and_wait_range(bdev->bd_inode->i_mapping,
210 lstart, lend);
211 }
212 EXPORT_SYMBOL(sync_blockdev_range);
213
214 /**
215 * freeze_bdev - lock a filesystem and force it into a consistent state
216 * @bdev: blockdevice to lock
217 *
218 * If a superblock is found on this device, we take the s_umount semaphore
219 * on it to make sure nobody unmounts until the snapshot creation is done.
220 * The reference counter (bd_fsfreeze_count) guarantees that only the last
221 * unfreeze process can unfreeze the frozen filesystem actually when multiple
222 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
223 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
224 * actually.
225 */
freeze_bdev(struct block_device * bdev)226 int freeze_bdev(struct block_device *bdev)
227 {
228 struct super_block *sb;
229 int error = 0;
230
231 mutex_lock(&bdev->bd_fsfreeze_mutex);
232 if (++bdev->bd_fsfreeze_count > 1)
233 goto done;
234
235 sb = get_active_super(bdev);
236 if (!sb)
237 goto sync;
238 if (sb->s_op->freeze_super)
239 error = sb->s_op->freeze_super(sb, FREEZE_HOLDER_USERSPACE);
240 else
241 error = freeze_super(sb, FREEZE_HOLDER_USERSPACE);
242 deactivate_super(sb);
243
244 if (error) {
245 bdev->bd_fsfreeze_count--;
246 goto done;
247 }
248 bdev->bd_fsfreeze_sb = sb;
249
250 sync:
251 sync_blockdev(bdev);
252 done:
253 mutex_unlock(&bdev->bd_fsfreeze_mutex);
254 return error;
255 }
256 EXPORT_SYMBOL(freeze_bdev);
257
258 /**
259 * thaw_bdev - unlock filesystem
260 * @bdev: blockdevice to unlock
261 *
262 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
263 */
thaw_bdev(struct block_device * bdev)264 int thaw_bdev(struct block_device *bdev)
265 {
266 struct super_block *sb;
267 int error = -EINVAL;
268
269 mutex_lock(&bdev->bd_fsfreeze_mutex);
270 if (!bdev->bd_fsfreeze_count)
271 goto out;
272
273 error = 0;
274 if (--bdev->bd_fsfreeze_count > 0)
275 goto out;
276
277 sb = bdev->bd_fsfreeze_sb;
278 if (!sb)
279 goto out;
280
281 if (sb->s_op->thaw_super)
282 error = sb->s_op->thaw_super(sb, FREEZE_HOLDER_USERSPACE);
283 else
284 error = thaw_super(sb, FREEZE_HOLDER_USERSPACE);
285 if (error)
286 bdev->bd_fsfreeze_count++;
287 else
288 bdev->bd_fsfreeze_sb = NULL;
289 out:
290 mutex_unlock(&bdev->bd_fsfreeze_mutex);
291 return error;
292 }
293 EXPORT_SYMBOL(thaw_bdev);
294
295 /*
296 * pseudo-fs
297 */
298
299 static __cacheline_aligned_in_smp DEFINE_MUTEX(bdev_lock);
300 static struct kmem_cache * bdev_cachep __read_mostly;
301
bdev_alloc_inode(struct super_block * sb)302 static struct inode *bdev_alloc_inode(struct super_block *sb)
303 {
304 struct bdev_inode *ei = alloc_inode_sb(sb, bdev_cachep, GFP_KERNEL);
305
306 if (!ei)
307 return NULL;
308 memset(&ei->bdev, 0, sizeof(ei->bdev));
309 return &ei->vfs_inode;
310 }
311
bdev_free_inode(struct inode * inode)312 static void bdev_free_inode(struct inode *inode)
313 {
314 struct block_device *bdev = I_BDEV(inode);
315
316 free_percpu(bdev->bd_stats);
317 kfree(bdev->bd_meta_info);
318
319 if (!bdev_is_partition(bdev)) {
320 if (bdev->bd_disk && bdev->bd_disk->bdi)
321 bdi_put(bdev->bd_disk->bdi);
322 kfree(bdev->bd_disk);
323 }
324
325 if (MAJOR(bdev->bd_dev) == BLOCK_EXT_MAJOR)
326 blk_free_ext_minor(MINOR(bdev->bd_dev));
327
328 kmem_cache_free(bdev_cachep, BDEV_I(inode));
329 }
330
init_once(void * data)331 static void init_once(void *data)
332 {
333 struct bdev_inode *ei = data;
334
335 inode_init_once(&ei->vfs_inode);
336 }
337
bdev_evict_inode(struct inode * inode)338 static void bdev_evict_inode(struct inode *inode)
339 {
340 truncate_inode_pages_final(&inode->i_data);
341 invalidate_inode_buffers(inode); /* is it needed here? */
342 clear_inode(inode);
343 }
344
345 static const struct super_operations bdev_sops = {
346 .statfs = simple_statfs,
347 .alloc_inode = bdev_alloc_inode,
348 .free_inode = bdev_free_inode,
349 .drop_inode = generic_delete_inode,
350 .evict_inode = bdev_evict_inode,
351 };
352
bd_init_fs_context(struct fs_context * fc)353 static int bd_init_fs_context(struct fs_context *fc)
354 {
355 struct pseudo_fs_context *ctx = init_pseudo(fc, BDEVFS_MAGIC);
356 if (!ctx)
357 return -ENOMEM;
358 fc->s_iflags |= SB_I_CGROUPWB;
359 ctx->ops = &bdev_sops;
360 return 0;
361 }
362
363 static struct file_system_type bd_type = {
364 .name = "bdev",
365 .init_fs_context = bd_init_fs_context,
366 .kill_sb = kill_anon_super,
367 };
368
369 struct super_block *blockdev_superblock __read_mostly;
370 EXPORT_SYMBOL_GPL(blockdev_superblock);
371
bdev_cache_init(void)372 void __init bdev_cache_init(void)
373 {
374 int err;
375 static struct vfsmount *bd_mnt;
376
377 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
378 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
379 SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC),
380 init_once);
381 err = register_filesystem(&bd_type);
382 if (err)
383 panic("Cannot register bdev pseudo-fs");
384 bd_mnt = kern_mount(&bd_type);
385 if (IS_ERR(bd_mnt))
386 panic("Cannot create bdev pseudo-fs");
387 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
388 }
389
bdev_alloc(struct gendisk * disk,u8 partno)390 struct block_device *bdev_alloc(struct gendisk *disk, u8 partno)
391 {
392 struct block_device *bdev;
393 struct inode *inode;
394
395 inode = new_inode(blockdev_superblock);
396 if (!inode)
397 return NULL;
398 inode->i_mode = S_IFBLK;
399 inode->i_rdev = 0;
400 inode->i_data.a_ops = &def_blk_aops;
401 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
402
403 bdev = I_BDEV(inode);
404 mutex_init(&bdev->bd_fsfreeze_mutex);
405 spin_lock_init(&bdev->bd_size_lock);
406 mutex_init(&bdev->bd_holder_lock);
407 bdev->bd_partno = partno;
408 bdev->bd_inode = inode;
409 bdev->bd_queue = disk->queue;
410 if (partno)
411 bdev->bd_has_submit_bio = disk->part0->bd_has_submit_bio;
412 else
413 bdev->bd_has_submit_bio = false;
414 bdev->bd_stats = alloc_percpu(struct disk_stats);
415 if (!bdev->bd_stats) {
416 iput(inode);
417 return NULL;
418 }
419 bdev->bd_disk = disk;
420 return bdev;
421 }
422
bdev_set_nr_sectors(struct block_device * bdev,sector_t sectors)423 void bdev_set_nr_sectors(struct block_device *bdev, sector_t sectors)
424 {
425 spin_lock(&bdev->bd_size_lock);
426 i_size_write(bdev->bd_inode, (loff_t)sectors << SECTOR_SHIFT);
427 bdev->bd_nr_sectors = sectors;
428 spin_unlock(&bdev->bd_size_lock);
429 }
430
bdev_add(struct block_device * bdev,dev_t dev)431 void bdev_add(struct block_device *bdev, dev_t dev)
432 {
433 if (bdev_stable_writes(bdev))
434 mapping_set_stable_writes(bdev->bd_inode->i_mapping);
435 bdev->bd_dev = dev;
436 bdev->bd_inode->i_rdev = dev;
437 bdev->bd_inode->i_ino = dev;
438 insert_inode_hash(bdev->bd_inode);
439 }
440
nr_blockdev_pages(void)441 long nr_blockdev_pages(void)
442 {
443 struct inode *inode;
444 long ret = 0;
445
446 spin_lock(&blockdev_superblock->s_inode_list_lock);
447 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list)
448 ret += inode->i_mapping->nrpages;
449 spin_unlock(&blockdev_superblock->s_inode_list_lock);
450
451 return ret;
452 }
453
454 /**
455 * bd_may_claim - test whether a block device can be claimed
456 * @bdev: block device of interest
457 * @holder: holder trying to claim @bdev
458 * @hops: holder ops
459 *
460 * Test whether @bdev can be claimed by @holder.
461 *
462 * RETURNS:
463 * %true if @bdev can be claimed, %false otherwise.
464 */
bd_may_claim(struct block_device * bdev,void * holder,const struct blk_holder_ops * hops)465 static bool bd_may_claim(struct block_device *bdev, void *holder,
466 const struct blk_holder_ops *hops)
467 {
468 struct block_device *whole = bdev_whole(bdev);
469
470 lockdep_assert_held(&bdev_lock);
471
472 if (bdev->bd_holder) {
473 /*
474 * The same holder can always re-claim.
475 */
476 if (bdev->bd_holder == holder) {
477 if (WARN_ON_ONCE(bdev->bd_holder_ops != hops))
478 return false;
479 return true;
480 }
481 return false;
482 }
483
484 /*
485 * If the whole devices holder is set to bd_may_claim, a partition on
486 * the device is claimed, but not the whole device.
487 */
488 if (whole != bdev &&
489 whole->bd_holder && whole->bd_holder != bd_may_claim)
490 return false;
491 return true;
492 }
493
494 /**
495 * bd_prepare_to_claim - claim a block device
496 * @bdev: block device of interest
497 * @holder: holder trying to claim @bdev
498 * @hops: holder ops.
499 *
500 * Claim @bdev. This function fails if @bdev is already claimed by another
501 * holder and waits if another claiming is in progress. return, the caller
502 * has ownership of bd_claiming and bd_holder[s].
503 *
504 * RETURNS:
505 * 0 if @bdev can be claimed, -EBUSY otherwise.
506 */
bd_prepare_to_claim(struct block_device * bdev,void * holder,const struct blk_holder_ops * hops)507 int bd_prepare_to_claim(struct block_device *bdev, void *holder,
508 const struct blk_holder_ops *hops)
509 {
510 struct block_device *whole = bdev_whole(bdev);
511
512 if (WARN_ON_ONCE(!holder))
513 return -EINVAL;
514 retry:
515 mutex_lock(&bdev_lock);
516 /* if someone else claimed, fail */
517 if (!bd_may_claim(bdev, holder, hops)) {
518 mutex_unlock(&bdev_lock);
519 return -EBUSY;
520 }
521
522 /* if claiming is already in progress, wait for it to finish */
523 if (whole->bd_claiming) {
524 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
525 DEFINE_WAIT(wait);
526
527 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
528 mutex_unlock(&bdev_lock);
529 schedule();
530 finish_wait(wq, &wait);
531 goto retry;
532 }
533
534 /* yay, all mine */
535 whole->bd_claiming = holder;
536 mutex_unlock(&bdev_lock);
537 return 0;
538 }
539 EXPORT_SYMBOL_GPL(bd_prepare_to_claim); /* only for the loop driver */
540
bd_clear_claiming(struct block_device * whole,void * holder)541 static void bd_clear_claiming(struct block_device *whole, void *holder)
542 {
543 lockdep_assert_held(&bdev_lock);
544 /* tell others that we're done */
545 BUG_ON(whole->bd_claiming != holder);
546 whole->bd_claiming = NULL;
547 wake_up_bit(&whole->bd_claiming, 0);
548 }
549
550 /**
551 * bd_finish_claiming - finish claiming of a block device
552 * @bdev: block device of interest
553 * @holder: holder that has claimed @bdev
554 * @hops: block device holder operations
555 *
556 * Finish exclusive open of a block device. Mark the device as exlusively
557 * open by the holder and wake up all waiters for exclusive open to finish.
558 */
bd_finish_claiming(struct block_device * bdev,void * holder,const struct blk_holder_ops * hops)559 static void bd_finish_claiming(struct block_device *bdev, void *holder,
560 const struct blk_holder_ops *hops)
561 {
562 struct block_device *whole = bdev_whole(bdev);
563
564 mutex_lock(&bdev_lock);
565 BUG_ON(!bd_may_claim(bdev, holder, hops));
566 /*
567 * Note that for a whole device bd_holders will be incremented twice,
568 * and bd_holder will be set to bd_may_claim before being set to holder
569 */
570 whole->bd_holders++;
571 whole->bd_holder = bd_may_claim;
572 bdev->bd_holders++;
573 mutex_lock(&bdev->bd_holder_lock);
574 bdev->bd_holder = holder;
575 bdev->bd_holder_ops = hops;
576 mutex_unlock(&bdev->bd_holder_lock);
577 bd_clear_claiming(whole, holder);
578 mutex_unlock(&bdev_lock);
579 }
580
581 /**
582 * bd_abort_claiming - abort claiming of a block device
583 * @bdev: block device of interest
584 * @holder: holder that has claimed @bdev
585 *
586 * Abort claiming of a block device when the exclusive open failed. This can be
587 * also used when exclusive open is not actually desired and we just needed
588 * to block other exclusive openers for a while.
589 */
bd_abort_claiming(struct block_device * bdev,void * holder)590 void bd_abort_claiming(struct block_device *bdev, void *holder)
591 {
592 mutex_lock(&bdev_lock);
593 bd_clear_claiming(bdev_whole(bdev), holder);
594 mutex_unlock(&bdev_lock);
595 }
596 EXPORT_SYMBOL(bd_abort_claiming);
597
bd_end_claim(struct block_device * bdev,void * holder)598 static void bd_end_claim(struct block_device *bdev, void *holder)
599 {
600 struct block_device *whole = bdev_whole(bdev);
601 bool unblock = false;
602
603 /*
604 * Release a claim on the device. The holder fields are protected with
605 * bdev_lock. open_mutex is used to synchronize disk_holder unlinking.
606 */
607 mutex_lock(&bdev_lock);
608 WARN_ON_ONCE(bdev->bd_holder != holder);
609 WARN_ON_ONCE(--bdev->bd_holders < 0);
610 WARN_ON_ONCE(--whole->bd_holders < 0);
611 if (!bdev->bd_holders) {
612 mutex_lock(&bdev->bd_holder_lock);
613 bdev->bd_holder = NULL;
614 bdev->bd_holder_ops = NULL;
615 mutex_unlock(&bdev->bd_holder_lock);
616 if (bdev->bd_write_holder)
617 unblock = true;
618 }
619 if (!whole->bd_holders)
620 whole->bd_holder = NULL;
621 mutex_unlock(&bdev_lock);
622
623 /*
624 * If this was the last claim, remove holder link and unblock evpoll if
625 * it was a write holder.
626 */
627 if (unblock) {
628 disk_unblock_events(bdev->bd_disk);
629 bdev->bd_write_holder = false;
630 }
631 }
632
blkdev_flush_mapping(struct block_device * bdev)633 static void blkdev_flush_mapping(struct block_device *bdev)
634 {
635 WARN_ON_ONCE(bdev->bd_holders);
636 sync_blockdev(bdev);
637 kill_bdev(bdev);
638 bdev_write_inode(bdev);
639 }
640
blkdev_get_whole(struct block_device * bdev,blk_mode_t mode)641 static int blkdev_get_whole(struct block_device *bdev, blk_mode_t mode)
642 {
643 struct gendisk *disk = bdev->bd_disk;
644 int ret;
645
646 if (disk->fops->open) {
647 ret = disk->fops->open(disk, mode);
648 if (ret) {
649 /* avoid ghost partitions on a removed medium */
650 if (ret == -ENOMEDIUM &&
651 test_bit(GD_NEED_PART_SCAN, &disk->state))
652 bdev_disk_changed(disk, true);
653 return ret;
654 }
655 }
656
657 if (!atomic_read(&bdev->bd_openers))
658 set_init_blocksize(bdev);
659 if (test_bit(GD_NEED_PART_SCAN, &disk->state))
660 bdev_disk_changed(disk, false);
661 atomic_inc(&bdev->bd_openers);
662 return 0;
663 }
664
blkdev_put_whole(struct block_device * bdev)665 static void blkdev_put_whole(struct block_device *bdev)
666 {
667 if (atomic_dec_and_test(&bdev->bd_openers))
668 blkdev_flush_mapping(bdev);
669 if (bdev->bd_disk->fops->release)
670 bdev->bd_disk->fops->release(bdev->bd_disk);
671 }
672
blkdev_get_part(struct block_device * part,blk_mode_t mode)673 static int blkdev_get_part(struct block_device *part, blk_mode_t mode)
674 {
675 struct gendisk *disk = part->bd_disk;
676 int ret;
677
678 ret = blkdev_get_whole(bdev_whole(part), mode);
679 if (ret)
680 return ret;
681
682 ret = -ENXIO;
683 if (!bdev_nr_sectors(part))
684 goto out_blkdev_put;
685
686 if (!atomic_read(&part->bd_openers)) {
687 disk->open_partitions++;
688 set_init_blocksize(part);
689 }
690 atomic_inc(&part->bd_openers);
691 return 0;
692
693 out_blkdev_put:
694 blkdev_put_whole(bdev_whole(part));
695 return ret;
696 }
697
blkdev_put_part(struct block_device * part)698 static void blkdev_put_part(struct block_device *part)
699 {
700 struct block_device *whole = bdev_whole(part);
701
702 if (atomic_dec_and_test(&part->bd_openers)) {
703 blkdev_flush_mapping(part);
704 whole->bd_disk->open_partitions--;
705 }
706 blkdev_put_whole(whole);
707 }
708
blkdev_get_no_open(dev_t dev)709 struct block_device *blkdev_get_no_open(dev_t dev)
710 {
711 struct block_device *bdev;
712 struct inode *inode;
713
714 inode = ilookup(blockdev_superblock, dev);
715 if (!inode && IS_ENABLED(CONFIG_BLOCK_LEGACY_AUTOLOAD)) {
716 blk_request_module(dev);
717 inode = ilookup(blockdev_superblock, dev);
718 if (inode)
719 pr_warn_ratelimited(
720 "block device autoloading is deprecated and will be removed.\n");
721 }
722 if (!inode)
723 return NULL;
724
725 /* switch from the inode reference to a device mode one: */
726 bdev = &BDEV_I(inode)->bdev;
727 if (!kobject_get_unless_zero(&bdev->bd_device.kobj))
728 bdev = NULL;
729 iput(inode);
730 return bdev;
731 }
732
blkdev_put_no_open(struct block_device * bdev)733 void blkdev_put_no_open(struct block_device *bdev)
734 {
735 put_device(&bdev->bd_device);
736 }
737
738 /**
739 * blkdev_get_by_dev - open a block device by device number
740 * @dev: device number of block device to open
741 * @mode: open mode (BLK_OPEN_*)
742 * @holder: exclusive holder identifier
743 * @hops: holder operations
744 *
745 * Open the block device described by device number @dev. If @holder is not
746 * %NULL, the block device is opened with exclusive access. Exclusive opens may
747 * nest for the same @holder.
748 *
749 * Use this interface ONLY if you really do not have anything better - i.e. when
750 * you are behind a truly sucky interface and all you are given is a device
751 * number. Everything else should use blkdev_get_by_path().
752 *
753 * CONTEXT:
754 * Might sleep.
755 *
756 * RETURNS:
757 * Reference to the block_device on success, ERR_PTR(-errno) on failure.
758 */
blkdev_get_by_dev(dev_t dev,blk_mode_t mode,void * holder,const struct blk_holder_ops * hops)759 struct block_device *blkdev_get_by_dev(dev_t dev, blk_mode_t mode, void *holder,
760 const struct blk_holder_ops *hops)
761 {
762 bool unblock_events = true;
763 struct block_device *bdev;
764 struct gendisk *disk;
765 int ret;
766
767 ret = devcgroup_check_permission(DEVCG_DEV_BLOCK,
768 MAJOR(dev), MINOR(dev),
769 ((mode & BLK_OPEN_READ) ? DEVCG_ACC_READ : 0) |
770 ((mode & BLK_OPEN_WRITE) ? DEVCG_ACC_WRITE : 0));
771 if (ret)
772 return ERR_PTR(ret);
773
774 bdev = blkdev_get_no_open(dev);
775 if (!bdev)
776 return ERR_PTR(-ENXIO);
777 disk = bdev->bd_disk;
778
779 if (holder) {
780 mode |= BLK_OPEN_EXCL;
781 ret = bd_prepare_to_claim(bdev, holder, hops);
782 if (ret)
783 goto put_blkdev;
784 } else {
785 if (WARN_ON_ONCE(mode & BLK_OPEN_EXCL)) {
786 ret = -EIO;
787 goto put_blkdev;
788 }
789 }
790
791 disk_block_events(disk);
792
793 mutex_lock(&disk->open_mutex);
794 ret = -ENXIO;
795 if (!disk_live(disk))
796 goto abort_claiming;
797 if (!try_module_get(disk->fops->owner))
798 goto abort_claiming;
799 if (bdev_is_partition(bdev))
800 ret = blkdev_get_part(bdev, mode);
801 else
802 ret = blkdev_get_whole(bdev, mode);
803 if (ret)
804 goto put_module;
805 if (holder) {
806 bd_finish_claiming(bdev, holder, hops);
807
808 /*
809 * Block event polling for write claims if requested. Any write
810 * holder makes the write_holder state stick until all are
811 * released. This is good enough and tracking individual
812 * writeable reference is too fragile given the way @mode is
813 * used in blkdev_get/put().
814 */
815 if ((mode & BLK_OPEN_WRITE) && !bdev->bd_write_holder &&
816 (disk->event_flags & DISK_EVENT_FLAG_BLOCK_ON_EXCL_WRITE)) {
817 bdev->bd_write_holder = true;
818 unblock_events = false;
819 }
820 }
821 mutex_unlock(&disk->open_mutex);
822
823 if (unblock_events)
824 disk_unblock_events(disk);
825 return bdev;
826 put_module:
827 module_put(disk->fops->owner);
828 abort_claiming:
829 if (holder)
830 bd_abort_claiming(bdev, holder);
831 mutex_unlock(&disk->open_mutex);
832 disk_unblock_events(disk);
833 put_blkdev:
834 blkdev_put_no_open(bdev);
835 return ERR_PTR(ret);
836 }
837 EXPORT_SYMBOL(blkdev_get_by_dev);
838
bdev_open_by_dev(dev_t dev,blk_mode_t mode,void * holder,const struct blk_holder_ops * hops)839 struct bdev_handle *bdev_open_by_dev(dev_t dev, blk_mode_t mode, void *holder,
840 const struct blk_holder_ops *hops)
841 {
842 struct bdev_handle *handle = kmalloc(sizeof(*handle), GFP_KERNEL);
843 struct block_device *bdev;
844
845 if (!handle)
846 return ERR_PTR(-ENOMEM);
847 bdev = blkdev_get_by_dev(dev, mode, holder, hops);
848 if (IS_ERR(bdev)) {
849 kfree(handle);
850 return ERR_CAST(bdev);
851 }
852 handle->bdev = bdev;
853 handle->holder = holder;
854 return handle;
855 }
856 EXPORT_SYMBOL(bdev_open_by_dev);
857
858 /**
859 * blkdev_get_by_path - open a block device by name
860 * @path: path to the block device to open
861 * @mode: open mode (BLK_OPEN_*)
862 * @holder: exclusive holder identifier
863 * @hops: holder operations
864 *
865 * Open the block device described by the device file at @path. If @holder is
866 * not %NULL, the block device is opened with exclusive access. Exclusive opens
867 * may nest for the same @holder.
868 *
869 * CONTEXT:
870 * Might sleep.
871 *
872 * RETURNS:
873 * Reference to the block_device on success, ERR_PTR(-errno) on failure.
874 */
blkdev_get_by_path(const char * path,blk_mode_t mode,void * holder,const struct blk_holder_ops * hops)875 struct block_device *blkdev_get_by_path(const char *path, blk_mode_t mode,
876 void *holder, const struct blk_holder_ops *hops)
877 {
878 struct block_device *bdev;
879 dev_t dev;
880 int error;
881
882 error = lookup_bdev(path, &dev);
883 if (error)
884 return ERR_PTR(error);
885
886 bdev = blkdev_get_by_dev(dev, mode, holder, hops);
887 if (!IS_ERR(bdev) && (mode & BLK_OPEN_WRITE) && bdev_read_only(bdev)) {
888 blkdev_put(bdev, holder);
889 return ERR_PTR(-EACCES);
890 }
891
892 return bdev;
893 }
894 EXPORT_SYMBOL(blkdev_get_by_path);
895
bdev_open_by_path(const char * path,blk_mode_t mode,void * holder,const struct blk_holder_ops * hops)896 struct bdev_handle *bdev_open_by_path(const char *path, blk_mode_t mode,
897 void *holder, const struct blk_holder_ops *hops)
898 {
899 struct bdev_handle *handle;
900 dev_t dev;
901 int error;
902
903 error = lookup_bdev(path, &dev);
904 if (error)
905 return ERR_PTR(error);
906
907 handle = bdev_open_by_dev(dev, mode, holder, hops);
908 if (!IS_ERR(handle) && (mode & BLK_OPEN_WRITE) &&
909 bdev_read_only(handle->bdev)) {
910 bdev_release(handle);
911 return ERR_PTR(-EACCES);
912 }
913
914 return handle;
915 }
916 EXPORT_SYMBOL(bdev_open_by_path);
917
blkdev_put(struct block_device * bdev,void * holder)918 void blkdev_put(struct block_device *bdev, void *holder)
919 {
920 struct gendisk *disk = bdev->bd_disk;
921
922 /*
923 * Sync early if it looks like we're the last one. If someone else
924 * opens the block device between now and the decrement of bd_openers
925 * then we did a sync that we didn't need to, but that's not the end
926 * of the world and we want to avoid long (could be several minute)
927 * syncs while holding the mutex.
928 */
929 if (atomic_read(&bdev->bd_openers) == 1)
930 sync_blockdev(bdev);
931
932 mutex_lock(&disk->open_mutex);
933 if (holder)
934 bd_end_claim(bdev, holder);
935
936 /*
937 * Trigger event checking and tell drivers to flush MEDIA_CHANGE
938 * event. This is to ensure detection of media removal commanded
939 * from userland - e.g. eject(1).
940 */
941 disk_flush_events(disk, DISK_EVENT_MEDIA_CHANGE);
942
943 if (bdev_is_partition(bdev))
944 blkdev_put_part(bdev);
945 else
946 blkdev_put_whole(bdev);
947 mutex_unlock(&disk->open_mutex);
948
949 module_put(disk->fops->owner);
950 blkdev_put_no_open(bdev);
951 }
952 EXPORT_SYMBOL(blkdev_put);
953
bdev_release(struct bdev_handle * handle)954 void bdev_release(struct bdev_handle *handle)
955 {
956 blkdev_put(handle->bdev, handle->holder);
957 kfree(handle);
958 }
959 EXPORT_SYMBOL(bdev_release);
960
961 /**
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.
965 *
966 * Lookup the block device's dev_t at @pathname in the current
967 * namespace if possible and return it in @dev.
968 *
969 * Context: May sleep.
970 * Return: 0 if succeeded, negative errno otherwise.
971 */
lookup_bdev(const char * pathname,dev_t * dev)972 int lookup_bdev(const char *pathname, dev_t *dev)
973 {
974 struct inode *inode;
975 struct path path;
976 int error;
977
978 if (!pathname || !*pathname)
979 return -EINVAL;
980
981 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
982 if (error)
983 return error;
984
985 inode = d_backing_inode(path.dentry);
986 error = -ENOTBLK;
987 if (!S_ISBLK(inode->i_mode))
988 goto out_path_put;
989 error = -EACCES;
990 if (!may_open_dev(&path))
991 goto out_path_put;
992
993 *dev = inode->i_rdev;
994 error = 0;
995 out_path_put:
996 path_put(&path);
997 return error;
998 }
999 EXPORT_SYMBOL(lookup_bdev);
1000
1001 /**
1002 * bdev_mark_dead - mark a block device as dead
1003 * @bdev: block device to operate on
1004 * @surprise: indicate a surprise removal
1005 *
1006 * Tell the file system that this devices or media is dead. If @surprise is set
1007 * to %true the device or media is already gone, if not we are preparing for an
1008 * orderly removal.
1009 *
1010 * This calls into the file system, which then typicall syncs out all dirty data
1011 * and writes back inodes and then invalidates any cached data in the inodes on
1012 * the file system. In addition we also invalidate the block device mapping.
1013 */
bdev_mark_dead(struct block_device * bdev,bool surprise)1014 void bdev_mark_dead(struct block_device *bdev, bool surprise)
1015 {
1016 mutex_lock(&bdev->bd_holder_lock);
1017 if (bdev->bd_holder_ops && bdev->bd_holder_ops->mark_dead)
1018 bdev->bd_holder_ops->mark_dead(bdev, surprise);
1019 else
1020 sync_blockdev(bdev);
1021 mutex_unlock(&bdev->bd_holder_lock);
1022
1023 invalidate_bdev(bdev);
1024 }
1025 #ifdef CONFIG_DASD_MODULE
1026 /*
1027 * Drivers should not use this directly, but the DASD driver has historically
1028 * had a shutdown to offline mode that doesn't actually remove the gendisk
1029 * that otherwise looks a lot like a safe device removal.
1030 */
1031 EXPORT_SYMBOL_GPL(bdev_mark_dead);
1032 #endif
1033
sync_bdevs(bool wait)1034 void sync_bdevs(bool wait)
1035 {
1036 struct inode *inode, *old_inode = NULL;
1037
1038 spin_lock(&blockdev_superblock->s_inode_list_lock);
1039 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
1040 struct address_space *mapping = inode->i_mapping;
1041 struct block_device *bdev;
1042
1043 spin_lock(&inode->i_lock);
1044 if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) ||
1045 mapping->nrpages == 0) {
1046 spin_unlock(&inode->i_lock);
1047 continue;
1048 }
1049 __iget(inode);
1050 spin_unlock(&inode->i_lock);
1051 spin_unlock(&blockdev_superblock->s_inode_list_lock);
1052 /*
1053 * We hold a reference to 'inode' so it couldn't have been
1054 * removed from s_inodes list while we dropped the
1055 * s_inode_list_lock We cannot iput the inode now as we can
1056 * be holding the last reference and we cannot iput it under
1057 * s_inode_list_lock. So we keep the reference and iput it
1058 * later.
1059 */
1060 iput(old_inode);
1061 old_inode = inode;
1062 bdev = I_BDEV(inode);
1063
1064 mutex_lock(&bdev->bd_disk->open_mutex);
1065 if (!atomic_read(&bdev->bd_openers)) {
1066 ; /* skip */
1067 } else if (wait) {
1068 /*
1069 * We keep the error status of individual mapping so
1070 * that applications can catch the writeback error using
1071 * fsync(2). See filemap_fdatawait_keep_errors() for
1072 * details.
1073 */
1074 filemap_fdatawait_keep_errors(inode->i_mapping);
1075 } else {
1076 filemap_fdatawrite(inode->i_mapping);
1077 }
1078 mutex_unlock(&bdev->bd_disk->open_mutex);
1079
1080 spin_lock(&blockdev_superblock->s_inode_list_lock);
1081 }
1082 spin_unlock(&blockdev_superblock->s_inode_list_lock);
1083 iput(old_inode);
1084 }
1085
1086 /*
1087 * Handle STATX_DIOALIGN for block devices.
1088 *
1089 * Note that the inode passed to this is the inode of a block device node file,
1090 * not the block device's internal inode. Therefore it is *not* valid to use
1091 * I_BDEV() here; the block device has to be looked up by i_rdev instead.
1092 */
bdev_statx_dioalign(struct inode * inode,struct kstat * stat)1093 void bdev_statx_dioalign(struct inode *inode, struct kstat *stat)
1094 {
1095 struct block_device *bdev;
1096
1097 bdev = blkdev_get_no_open(inode->i_rdev);
1098 if (!bdev)
1099 return;
1100
1101 stat->dio_mem_align = bdev_dma_alignment(bdev) + 1;
1102 stat->dio_offset_align = bdev_logical_block_size(bdev);
1103 stat->result_mask |= STATX_DIOALIGN;
1104
1105 blkdev_put_no_open(bdev);
1106 }
1107