xref: /linux/block/bdev.c (revision 7effbd18e496a954540fb6319db0d87d28a70496)
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/part_stat.h>
28 #include <linux/uaccess.h>
29 #include <linux/stat.h>
30 #include "../fs/internal.h"
31 #include "blk.h"
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 
43 struct block_device *I_BDEV(struct inode *inode)
44 {
45 	return &BDEV_I(inode)->bdev;
46 }
47 EXPORT_SYMBOL(I_BDEV);
48 
49 static void bdev_write_inode(struct block_device *bdev)
50 {
51 	struct inode *inode = bdev->bd_inode;
52 	int ret;
53 
54 	spin_lock(&inode->i_lock);
55 	while (inode->i_state & I_DIRTY) {
56 		spin_unlock(&inode->i_lock);
57 		ret = write_inode_now(inode, true);
58 		if (ret)
59 			pr_warn_ratelimited(
60 	"VFS: Dirty inode writeback failed for block device %pg (err=%d).\n",
61 				bdev, ret);
62 		spin_lock(&inode->i_lock);
63 	}
64 	spin_unlock(&inode->i_lock);
65 }
66 
67 /* Kill _all_ buffers and pagecache , dirty or not.. */
68 static void kill_bdev(struct block_device *bdev)
69 {
70 	struct address_space *mapping = bdev->bd_inode->i_mapping;
71 
72 	if (mapping_empty(mapping))
73 		return;
74 
75 	invalidate_bh_lrus();
76 	truncate_inode_pages(mapping, 0);
77 }
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 
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 	}
89 }
90 EXPORT_SYMBOL(invalidate_bdev);
91 
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 {
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)) {
105 		int err = bd_prepare_to_claim(bdev, truncate_bdev_range);
106 		if (err)
107 			goto invalidate;
108 	}
109 
110 	truncate_inode_pages_range(bdev->bd_inode->i_mapping, lstart, lend);
111 	if (!(mode & FMODE_EXCL))
112 		bd_abort_claiming(bdev, truncate_bdev_range);
113 	return 0;
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);
123 }
124 
125 static void set_init_blocksize(struct block_device *bdev)
126 {
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);
136 }
137 
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 */
141 	if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
142 		return -EINVAL;
143 
144 	/* Size cannot be smaller than the size supported by the device */
145 	if (size < bdev_logical_block_size(bdev))
146 		return -EINVAL;
147 
148 	/* Don't change the size if it is same as current */
149 	if (bdev->bd_inode->i_blkbits != blksize_bits(size)) {
150 		sync_blockdev(bdev);
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 {
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;
166 	sb->s_blocksize_bits = blksize_bits(size);
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 {
174 	int minsize = bdev_logical_block_size(sb->s_bdev);
175 	if (size < minsize)
176 		size = minsize;
177 	return sb_set_blocksize(sb, size);
178 }
179 
180 EXPORT_SYMBOL(sb_min_blocksize);
181 
182 int sync_blockdev_nowait(struct block_device *bdev)
183 {
184 	if (!bdev)
185 		return 0;
186 	return filemap_flush(bdev->bd_inode->i_mapping);
187 }
188 EXPORT_SYMBOL_GPL(sync_blockdev_nowait);
189 
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 {
196 	if (!bdev)
197 		return 0;
198 	return filemap_write_and_wait(bdev->bd_inode->i_mapping);
199 }
200 EXPORT_SYMBOL(sync_blockdev);
201 
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 
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) {
218 		int res = sync_filesystem(sb);
219 		drop_super(sb);
220 		return res;
221 	}
222 	return sync_blockdev(bdev);
223 }
224 EXPORT_SYMBOL(fsync_bdev);
225 
226 /**
227  * freeze_bdev - lock a filesystem and force it into a consistent state
228  * @bdev:	blockdevice to lock
229  *
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  */
238 int freeze_bdev(struct block_device *bdev)
239 {
240 	struct super_block *sb;
241 	int error = 0;
242 
243 	mutex_lock(&bdev->bd_fsfreeze_mutex);
244 	if (++bdev->bd_fsfreeze_count > 1)
245 		goto done;
246 
247 	sb = get_active_super(bdev);
248 	if (!sb)
249 		goto sync;
250 	if (sb->s_op->freeze_super)
251 		error = sb->s_op->freeze_super(sb);
252 	else
253 		error = freeze_super(sb);
254 	deactivate_super(sb);
255 
256 	if (error) {
257 		bdev->bd_fsfreeze_count--;
258 		goto done;
259 	}
260 	bdev->bd_fsfreeze_sb = sb;
261 
262 sync:
263 	sync_blockdev(bdev);
264 done:
265 	mutex_unlock(&bdev->bd_fsfreeze_mutex);
266 	return error;
267 }
268 EXPORT_SYMBOL(freeze_bdev);
269 
270 /**
271  * thaw_bdev - unlock filesystem
272  * @bdev:	blockdevice to unlock
273  *
274  * Unlocks the filesystem and marks it writeable again after freeze_bdev().
275  */
276 int thaw_bdev(struct block_device *bdev)
277 {
278 	struct super_block *sb;
279 	int error = -EINVAL;
280 
281 	mutex_lock(&bdev->bd_fsfreeze_mutex);
282 	if (!bdev->bd_fsfreeze_count)
283 		goto out;
284 
285 	error = 0;
286 	if (--bdev->bd_fsfreeze_count > 0)
287 		goto out;
288 
289 	sb = bdev->bd_fsfreeze_sb;
290 	if (!sb)
291 		goto out;
292 
293 	if (sb->s_op->thaw_super)
294 		error = sb->s_op->thaw_super(sb);
295 	else
296 		error = thaw_super(sb);
297 	if (error)
298 		bdev->bd_fsfreeze_count++;
299 	else
300 		bdev->bd_fsfreeze_sb = NULL;
301 out:
302 	mutex_unlock(&bdev->bd_fsfreeze_mutex);
303 	return error;
304 }
305 EXPORT_SYMBOL(thaw_bdev);
306 
307 /*
308  * pseudo-fs
309  */
310 
311 static  __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
312 static struct kmem_cache * bdev_cachep __read_mostly;
313 
314 static struct inode *bdev_alloc_inode(struct super_block *sb)
315 {
316 	struct bdev_inode *ei = alloc_inode_sb(sb, bdev_cachep, GFP_KERNEL);
317 
318 	if (!ei)
319 		return NULL;
320 	memset(&ei->bdev, 0, sizeof(ei->bdev));
321 	return &ei->vfs_inode;
322 }
323 
324 static void bdev_free_inode(struct inode *inode)
325 {
326 	struct block_device *bdev = I_BDEV(inode);
327 
328 	free_percpu(bdev->bd_stats);
329 	kfree(bdev->bd_meta_info);
330 
331 	if (!bdev_is_partition(bdev)) {
332 		if (bdev->bd_disk && bdev->bd_disk->bdi)
333 			bdi_put(bdev->bd_disk->bdi);
334 		kfree(bdev->bd_disk);
335 	}
336 
337 	if (MAJOR(bdev->bd_dev) == BLOCK_EXT_MAJOR)
338 		blk_free_ext_minor(MINOR(bdev->bd_dev));
339 
340 	kmem_cache_free(bdev_cachep, BDEV_I(inode));
341 }
342 
343 static void init_once(void *data)
344 {
345 	struct bdev_inode *ei = data;
346 
347 	inode_init_once(&ei->vfs_inode);
348 }
349 
350 static void bdev_evict_inode(struct inode *inode)
351 {
352 	truncate_inode_pages_final(&inode->i_data);
353 	invalidate_inode_buffers(inode); /* is it needed here? */
354 	clear_inode(inode);
355 }
356 
357 static const struct super_operations bdev_sops = {
358 	.statfs = simple_statfs,
359 	.alloc_inode = bdev_alloc_inode,
360 	.free_inode = bdev_free_inode,
361 	.drop_inode = generic_delete_inode,
362 	.evict_inode = bdev_evict_inode,
363 };
364 
365 static int bd_init_fs_context(struct fs_context *fc)
366 {
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;
373 }
374 
375 static struct file_system_type bd_type = {
376 	.name		= "bdev",
377 	.init_fs_context = bd_init_fs_context,
378 	.kill_sb	= kill_anon_super,
379 };
380 
381 struct super_block *blockdev_superblock __read_mostly;
382 EXPORT_SYMBOL_GPL(blockdev_superblock);
383 
384 void __init bdev_cache_init(void)
385 {
386 	int err;
387 	static struct vfsmount *bd_mnt;
388 
389 	bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
390 			0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
391 				SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC),
392 			init_once);
393 	err = register_filesystem(&bd_type);
394 	if (err)
395 		panic("Cannot register bdev pseudo-fs");
396 	bd_mnt = kern_mount(&bd_type);
397 	if (IS_ERR(bd_mnt))
398 		panic("Cannot create bdev pseudo-fs");
399 	blockdev_superblock = bd_mnt->mnt_sb;   /* For writeback */
400 }
401 
402 struct block_device *bdev_alloc(struct gendisk *disk, u8 partno)
403 {
404 	struct block_device *bdev;
405 	struct inode *inode;
406 
407 	inode = new_inode(blockdev_superblock);
408 	if (!inode)
409 		return NULL;
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);
416 	mutex_init(&bdev->bd_fsfreeze_mutex);
417 	spin_lock_init(&bdev->bd_size_lock);
418 	bdev->bd_partno = partno;
419 	bdev->bd_inode = inode;
420 	bdev->bd_queue = disk->queue;
421 	bdev->bd_stats = alloc_percpu(struct disk_stats);
422 	if (!bdev->bd_stats) {
423 		iput(inode);
424 		return NULL;
425 	}
426 	bdev->bd_disk = disk;
427 	return bdev;
428 }
429 
430 void bdev_add(struct block_device *bdev, dev_t dev)
431 {
432 	bdev->bd_dev = dev;
433 	bdev->bd_inode->i_rdev = dev;
434 	bdev->bd_inode->i_ino = dev;
435 	insert_inode_hash(bdev->bd_inode);
436 }
437 
438 long nr_blockdev_pages(void)
439 {
440 	struct inode *inode;
441 	long ret = 0;
442 
443 	spin_lock(&blockdev_superblock->s_inode_list_lock);
444 	list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list)
445 		ret += inode->i_mapping->nrpages;
446 	spin_unlock(&blockdev_superblock->s_inode_list_lock);
447 
448 	return ret;
449 }
450 
451 /**
452  * bd_may_claim - test whether a block device can be claimed
453  * @bdev: block device of interest
454  * @whole: whole block device containing @bdev, may equal @bdev
455  * @holder: holder trying to claim @bdev
456  *
457  * Test whether @bdev can be claimed by @holder.
458  *
459  * CONTEXT:
460  * spin_lock(&bdev_lock).
461  *
462  * RETURNS:
463  * %true if @bdev can be claimed, %false otherwise.
464  */
465 static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
466 			 void *holder)
467 {
468 	if (bdev->bd_holder == holder)
469 		return true;	 /* already a holder */
470 	else if (bdev->bd_holder != NULL)
471 		return false; 	 /* held by someone else */
472 	else if (whole == bdev)
473 		return true;  	 /* is a whole device which isn't held */
474 
475 	else if (whole->bd_holder == bd_may_claim)
476 		return true; 	 /* is a partition of a device that is being partitioned */
477 	else if (whole->bd_holder != NULL)
478 		return false;	 /* is a partition of a held device */
479 	else
480 		return true;	 /* is a partition of an un-held device */
481 }
482 
483 /**
484  * bd_prepare_to_claim - claim a block device
485  * @bdev: block device of interest
486  * @holder: holder trying to claim @bdev
487  *
488  * Claim @bdev.  This function fails if @bdev is already claimed by another
489  * holder and waits if another claiming is in progress. return, the caller
490  * has ownership of bd_claiming and bd_holder[s].
491  *
492  * RETURNS:
493  * 0 if @bdev can be claimed, -EBUSY otherwise.
494  */
495 int bd_prepare_to_claim(struct block_device *bdev, void *holder)
496 {
497 	struct block_device *whole = bdev_whole(bdev);
498 
499 	if (WARN_ON_ONCE(!holder))
500 		return -EINVAL;
501 retry:
502 	spin_lock(&bdev_lock);
503 	/* if someone else claimed, fail */
504 	if (!bd_may_claim(bdev, whole, holder)) {
505 		spin_unlock(&bdev_lock);
506 		return -EBUSY;
507 	}
508 
509 	/* if claiming is already in progress, wait for it to finish */
510 	if (whole->bd_claiming) {
511 		wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
512 		DEFINE_WAIT(wait);
513 
514 		prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
515 		spin_unlock(&bdev_lock);
516 		schedule();
517 		finish_wait(wq, &wait);
518 		goto retry;
519 	}
520 
521 	/* yay, all mine */
522 	whole->bd_claiming = holder;
523 	spin_unlock(&bdev_lock);
524 	return 0;
525 }
526 EXPORT_SYMBOL_GPL(bd_prepare_to_claim); /* only for the loop driver */
527 
528 static void bd_clear_claiming(struct block_device *whole, void *holder)
529 {
530 	lockdep_assert_held(&bdev_lock);
531 	/* tell others that we're done */
532 	BUG_ON(whole->bd_claiming != holder);
533 	whole->bd_claiming = NULL;
534 	wake_up_bit(&whole->bd_claiming, 0);
535 }
536 
537 /**
538  * bd_finish_claiming - finish claiming of a block device
539  * @bdev: block device of interest
540  * @holder: holder that has claimed @bdev
541  *
542  * Finish exclusive open of a block device. Mark the device as exlusively
543  * open by the holder and wake up all waiters for exclusive open to finish.
544  */
545 static void bd_finish_claiming(struct block_device *bdev, void *holder)
546 {
547 	struct block_device *whole = bdev_whole(bdev);
548 
549 	spin_lock(&bdev_lock);
550 	BUG_ON(!bd_may_claim(bdev, whole, holder));
551 	/*
552 	 * Note that for a whole device bd_holders will be incremented twice,
553 	 * and bd_holder will be set to bd_may_claim before being set to holder
554 	 */
555 	whole->bd_holders++;
556 	whole->bd_holder = bd_may_claim;
557 	bdev->bd_holders++;
558 	bdev->bd_holder = holder;
559 	bd_clear_claiming(whole, holder);
560 	spin_unlock(&bdev_lock);
561 }
562 
563 /**
564  * bd_abort_claiming - abort claiming of a block device
565  * @bdev: block device of interest
566  * @holder: holder that has claimed @bdev
567  *
568  * Abort claiming of a block device when the exclusive open failed. This can be
569  * also used when exclusive open is not actually desired and we just needed
570  * to block other exclusive openers for a while.
571  */
572 void bd_abort_claiming(struct block_device *bdev, void *holder)
573 {
574 	spin_lock(&bdev_lock);
575 	bd_clear_claiming(bdev_whole(bdev), holder);
576 	spin_unlock(&bdev_lock);
577 }
578 EXPORT_SYMBOL(bd_abort_claiming);
579 
580 static void blkdev_flush_mapping(struct block_device *bdev)
581 {
582 	WARN_ON_ONCE(bdev->bd_holders);
583 	sync_blockdev(bdev);
584 	kill_bdev(bdev);
585 	bdev_write_inode(bdev);
586 }
587 
588 static int blkdev_get_whole(struct block_device *bdev, fmode_t mode)
589 {
590 	struct gendisk *disk = bdev->bd_disk;
591 	int ret;
592 
593 	if (disk->fops->open) {
594 		ret = disk->fops->open(bdev, mode);
595 		if (ret) {
596 			/* avoid ghost partitions on a removed medium */
597 			if (ret == -ENOMEDIUM &&
598 			     test_bit(GD_NEED_PART_SCAN, &disk->state))
599 				bdev_disk_changed(disk, true);
600 			return ret;
601 		}
602 	}
603 
604 	if (!atomic_read(&bdev->bd_openers))
605 		set_init_blocksize(bdev);
606 	if (test_bit(GD_NEED_PART_SCAN, &disk->state))
607 		bdev_disk_changed(disk, false);
608 	atomic_inc(&bdev->bd_openers);
609 	return 0;
610 }
611 
612 static void blkdev_put_whole(struct block_device *bdev, fmode_t mode)
613 {
614 	if (atomic_dec_and_test(&bdev->bd_openers))
615 		blkdev_flush_mapping(bdev);
616 	if (bdev->bd_disk->fops->release)
617 		bdev->bd_disk->fops->release(bdev->bd_disk, mode);
618 }
619 
620 static int blkdev_get_part(struct block_device *part, fmode_t mode)
621 {
622 	struct gendisk *disk = part->bd_disk;
623 	int ret;
624 
625 	if (atomic_read(&part->bd_openers))
626 		goto done;
627 
628 	ret = blkdev_get_whole(bdev_whole(part), mode);
629 	if (ret)
630 		return ret;
631 
632 	ret = -ENXIO;
633 	if (!bdev_nr_sectors(part))
634 		goto out_blkdev_put;
635 
636 	disk->open_partitions++;
637 	set_init_blocksize(part);
638 done:
639 	atomic_inc(&part->bd_openers);
640 	return 0;
641 
642 out_blkdev_put:
643 	blkdev_put_whole(bdev_whole(part), mode);
644 	return ret;
645 }
646 
647 static void blkdev_put_part(struct block_device *part, fmode_t mode)
648 {
649 	struct block_device *whole = bdev_whole(part);
650 
651 	if (!atomic_dec_and_test(&part->bd_openers))
652 		return;
653 	blkdev_flush_mapping(part);
654 	whole->bd_disk->open_partitions--;
655 	blkdev_put_whole(whole, mode);
656 }
657 
658 struct block_device *blkdev_get_no_open(dev_t dev)
659 {
660 	struct block_device *bdev;
661 	struct inode *inode;
662 
663 	inode = ilookup(blockdev_superblock, dev);
664 	if (!inode && IS_ENABLED(CONFIG_BLOCK_LEGACY_AUTOLOAD)) {
665 		blk_request_module(dev);
666 		inode = ilookup(blockdev_superblock, dev);
667 		if (inode)
668 			pr_warn_ratelimited(
669 "block device autoloading is deprecated and will be removed.\n");
670 	}
671 	if (!inode)
672 		return NULL;
673 
674 	/* switch from the inode reference to a device mode one: */
675 	bdev = &BDEV_I(inode)->bdev;
676 	if (!kobject_get_unless_zero(&bdev->bd_device.kobj))
677 		bdev = NULL;
678 	iput(inode);
679 	return bdev;
680 }
681 
682 void blkdev_put_no_open(struct block_device *bdev)
683 {
684 	put_device(&bdev->bd_device);
685 }
686 
687 /**
688  * blkdev_get_by_dev - open a block device by device number
689  * @dev: device number of block device to open
690  * @mode: FMODE_* mask
691  * @holder: exclusive holder identifier
692  *
693  * Open the block device described by device number @dev. If @mode includes
694  * %FMODE_EXCL, the block device is opened with exclusive access.  Specifying
695  * %FMODE_EXCL with a %NULL @holder is invalid.  Exclusive opens may nest for
696  * the same @holder.
697  *
698  * Use this interface ONLY if you really do not have anything better - i.e. when
699  * you are behind a truly sucky interface and all you are given is a device
700  * number.  Everything else should use blkdev_get_by_path().
701  *
702  * CONTEXT:
703  * Might sleep.
704  *
705  * RETURNS:
706  * Reference to the block_device on success, ERR_PTR(-errno) on failure.
707  */
708 struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
709 {
710 	bool unblock_events = true;
711 	struct block_device *bdev;
712 	struct gendisk *disk;
713 	int ret;
714 
715 	ret = devcgroup_check_permission(DEVCG_DEV_BLOCK,
716 			MAJOR(dev), MINOR(dev),
717 			((mode & FMODE_READ) ? DEVCG_ACC_READ : 0) |
718 			((mode & FMODE_WRITE) ? DEVCG_ACC_WRITE : 0));
719 	if (ret)
720 		return ERR_PTR(ret);
721 
722 	bdev = blkdev_get_no_open(dev);
723 	if (!bdev)
724 		return ERR_PTR(-ENXIO);
725 	disk = bdev->bd_disk;
726 
727 	if (mode & FMODE_EXCL) {
728 		ret = bd_prepare_to_claim(bdev, holder);
729 		if (ret)
730 			goto put_blkdev;
731 	}
732 
733 	disk_block_events(disk);
734 
735 	mutex_lock(&disk->open_mutex);
736 	ret = -ENXIO;
737 	if (!disk_live(disk))
738 		goto abort_claiming;
739 	if (!try_module_get(disk->fops->owner))
740 		goto abort_claiming;
741 	if (bdev_is_partition(bdev))
742 		ret = blkdev_get_part(bdev, mode);
743 	else
744 		ret = blkdev_get_whole(bdev, mode);
745 	if (ret)
746 		goto put_module;
747 	if (mode & FMODE_EXCL) {
748 		bd_finish_claiming(bdev, holder);
749 
750 		/*
751 		 * Block event polling for write claims if requested.  Any write
752 		 * holder makes the write_holder state stick until all are
753 		 * released.  This is good enough and tracking individual
754 		 * writeable reference is too fragile given the way @mode is
755 		 * used in blkdev_get/put().
756 		 */
757 		if ((mode & FMODE_WRITE) && !bdev->bd_write_holder &&
758 		    (disk->event_flags & DISK_EVENT_FLAG_BLOCK_ON_EXCL_WRITE)) {
759 			bdev->bd_write_holder = true;
760 			unblock_events = false;
761 		}
762 	}
763 	mutex_unlock(&disk->open_mutex);
764 
765 	if (unblock_events)
766 		disk_unblock_events(disk);
767 	return bdev;
768 put_module:
769 	module_put(disk->fops->owner);
770 abort_claiming:
771 	if (mode & FMODE_EXCL)
772 		bd_abort_claiming(bdev, holder);
773 	mutex_unlock(&disk->open_mutex);
774 	disk_unblock_events(disk);
775 put_blkdev:
776 	blkdev_put_no_open(bdev);
777 	return ERR_PTR(ret);
778 }
779 EXPORT_SYMBOL(blkdev_get_by_dev);
780 
781 /**
782  * blkdev_get_by_path - open a block device by name
783  * @path: path to the block device to open
784  * @mode: FMODE_* mask
785  * @holder: exclusive holder identifier
786  *
787  * Open the block device described by the device file at @path.  If @mode
788  * includes %FMODE_EXCL, the block device is opened with exclusive access.
789  * Specifying %FMODE_EXCL with a %NULL @holder is invalid.  Exclusive opens may
790  * nest for the same @holder.
791  *
792  * CONTEXT:
793  * Might sleep.
794  *
795  * RETURNS:
796  * Reference to the block_device on success, ERR_PTR(-errno) on failure.
797  */
798 struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
799 					void *holder)
800 {
801 	struct block_device *bdev;
802 	dev_t dev;
803 	int error;
804 
805 	error = lookup_bdev(path, &dev);
806 	if (error)
807 		return ERR_PTR(error);
808 
809 	bdev = blkdev_get_by_dev(dev, mode, holder);
810 	if (!IS_ERR(bdev) && (mode & FMODE_WRITE) && bdev_read_only(bdev)) {
811 		blkdev_put(bdev, mode);
812 		return ERR_PTR(-EACCES);
813 	}
814 
815 	return bdev;
816 }
817 EXPORT_SYMBOL(blkdev_get_by_path);
818 
819 void blkdev_put(struct block_device *bdev, fmode_t mode)
820 {
821 	struct gendisk *disk = bdev->bd_disk;
822 
823 	/*
824 	 * Sync early if it looks like we're the last one.  If someone else
825 	 * opens the block device between now and the decrement of bd_openers
826 	 * then we did a sync that we didn't need to, but that's not the end
827 	 * of the world and we want to avoid long (could be several minute)
828 	 * syncs while holding the mutex.
829 	 */
830 	if (atomic_read(&bdev->bd_openers) == 1)
831 		sync_blockdev(bdev);
832 
833 	mutex_lock(&disk->open_mutex);
834 	if (mode & FMODE_EXCL) {
835 		struct block_device *whole = bdev_whole(bdev);
836 		bool bdev_free;
837 
838 		/*
839 		 * Release a claim on the device.  The holder fields
840 		 * are protected with bdev_lock.  open_mutex is to
841 		 * synchronize disk_holder unlinking.
842 		 */
843 		spin_lock(&bdev_lock);
844 
845 		WARN_ON_ONCE(--bdev->bd_holders < 0);
846 		WARN_ON_ONCE(--whole->bd_holders < 0);
847 
848 		if ((bdev_free = !bdev->bd_holders))
849 			bdev->bd_holder = NULL;
850 		if (!whole->bd_holders)
851 			whole->bd_holder = NULL;
852 
853 		spin_unlock(&bdev_lock);
854 
855 		/*
856 		 * If this was the last claim, remove holder link and
857 		 * unblock evpoll if it was a write holder.
858 		 */
859 		if (bdev_free && bdev->bd_write_holder) {
860 			disk_unblock_events(disk);
861 			bdev->bd_write_holder = false;
862 		}
863 	}
864 
865 	/*
866 	 * Trigger event checking and tell drivers to flush MEDIA_CHANGE
867 	 * event.  This is to ensure detection of media removal commanded
868 	 * from userland - e.g. eject(1).
869 	 */
870 	disk_flush_events(disk, DISK_EVENT_MEDIA_CHANGE);
871 
872 	if (bdev_is_partition(bdev))
873 		blkdev_put_part(bdev, mode);
874 	else
875 		blkdev_put_whole(bdev, mode);
876 	mutex_unlock(&disk->open_mutex);
877 
878 	module_put(disk->fops->owner);
879 	blkdev_put_no_open(bdev);
880 }
881 EXPORT_SYMBOL(blkdev_put);
882 
883 /**
884  * lookup_bdev() - Look up a struct block_device by name.
885  * @pathname: Name of the block device in the filesystem.
886  * @dev: Pointer to the block device's dev_t, if found.
887  *
888  * Lookup the block device's dev_t at @pathname in the current
889  * namespace if possible and return it in @dev.
890  *
891  * Context: May sleep.
892  * Return: 0 if succeeded, negative errno otherwise.
893  */
894 int lookup_bdev(const char *pathname, dev_t *dev)
895 {
896 	struct inode *inode;
897 	struct path path;
898 	int error;
899 
900 	if (!pathname || !*pathname)
901 		return -EINVAL;
902 
903 	error = kern_path(pathname, LOOKUP_FOLLOW, &path);
904 	if (error)
905 		return error;
906 
907 	inode = d_backing_inode(path.dentry);
908 	error = -ENOTBLK;
909 	if (!S_ISBLK(inode->i_mode))
910 		goto out_path_put;
911 	error = -EACCES;
912 	if (!may_open_dev(&path))
913 		goto out_path_put;
914 
915 	*dev = inode->i_rdev;
916 	error = 0;
917 out_path_put:
918 	path_put(&path);
919 	return error;
920 }
921 EXPORT_SYMBOL(lookup_bdev);
922 
923 int __invalidate_device(struct block_device *bdev, bool kill_dirty)
924 {
925 	struct super_block *sb = get_super(bdev);
926 	int res = 0;
927 
928 	if (sb) {
929 		/*
930 		 * no need to lock the super, get_super holds the
931 		 * read mutex so the filesystem cannot go away
932 		 * under us (->put_super runs with the write lock
933 		 * hold).
934 		 */
935 		shrink_dcache_sb(sb);
936 		res = invalidate_inodes(sb, kill_dirty);
937 		drop_super(sb);
938 	}
939 	invalidate_bdev(bdev);
940 	return res;
941 }
942 EXPORT_SYMBOL(__invalidate_device);
943 
944 void sync_bdevs(bool wait)
945 {
946 	struct inode *inode, *old_inode = NULL;
947 
948 	spin_lock(&blockdev_superblock->s_inode_list_lock);
949 	list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
950 		struct address_space *mapping = inode->i_mapping;
951 		struct block_device *bdev;
952 
953 		spin_lock(&inode->i_lock);
954 		if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) ||
955 		    mapping->nrpages == 0) {
956 			spin_unlock(&inode->i_lock);
957 			continue;
958 		}
959 		__iget(inode);
960 		spin_unlock(&inode->i_lock);
961 		spin_unlock(&blockdev_superblock->s_inode_list_lock);
962 		/*
963 		 * We hold a reference to 'inode' so it couldn't have been
964 		 * removed from s_inodes list while we dropped the
965 		 * s_inode_list_lock  We cannot iput the inode now as we can
966 		 * be holding the last reference and we cannot iput it under
967 		 * s_inode_list_lock. So we keep the reference and iput it
968 		 * later.
969 		 */
970 		iput(old_inode);
971 		old_inode = inode;
972 		bdev = I_BDEV(inode);
973 
974 		mutex_lock(&bdev->bd_disk->open_mutex);
975 		if (!atomic_read(&bdev->bd_openers)) {
976 			; /* skip */
977 		} else if (wait) {
978 			/*
979 			 * We keep the error status of individual mapping so
980 			 * that applications can catch the writeback error using
981 			 * fsync(2). See filemap_fdatawait_keep_errors() for
982 			 * details.
983 			 */
984 			filemap_fdatawait_keep_errors(inode->i_mapping);
985 		} else {
986 			filemap_fdatawrite(inode->i_mapping);
987 		}
988 		mutex_unlock(&bdev->bd_disk->open_mutex);
989 
990 		spin_lock(&blockdev_superblock->s_inode_list_lock);
991 	}
992 	spin_unlock(&blockdev_superblock->s_inode_list_lock);
993 	iput(old_inode);
994 }
995 
996 /*
997  * Handle STATX_DIOALIGN for block devices.
998  *
999  * Note that the inode passed to this is the inode of a block device node file,
1000  * not the block device's internal inode.  Therefore it is *not* valid to use
1001  * I_BDEV() here; the block device has to be looked up by i_rdev instead.
1002  */
1003 void bdev_statx_dioalign(struct inode *inode, struct kstat *stat)
1004 {
1005 	struct block_device *bdev;
1006 
1007 	bdev = blkdev_get_no_open(inode->i_rdev);
1008 	if (!bdev)
1009 		return;
1010 
1011 	stat->dio_mem_align = bdev_dma_alignment(bdev) + 1;
1012 	stat->dio_offset_align = bdev_logical_block_size(bdev);
1013 	stat->result_mask |= STATX_DIOALIGN;
1014 
1015 	blkdev_put_no_open(bdev);
1016 }
1017