Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 4 | * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE |
Christoph Hellwig | 7b51e70 | 2020-12-10 08:55:44 +0100 | [diff] [blame] | 5 | * Copyright (C) 2016 - 2020 Christoph Hellwig |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/init.h> |
| 9 | #include <linux/mm.h> |
| 10 | #include <linux/fcntl.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/kmod.h> |
| 13 | #include <linux/major.h> |
Pavel Emelyanov | 7db9cfd | 2008-06-05 22:46:27 -0700 | [diff] [blame] | 14 | #include <linux/device_cgroup.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/highmem.h> |
| 16 | #include <linux/blkdev.h> |
Tejun Heo | 66114ca | 2015-05-22 17:13:32 -0400 | [diff] [blame] | 17 | #include <linux/backing-dev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/module.h> |
| 19 | #include <linux/blkpg.h> |
Muthu Kumar | b502bd1 | 2012-03-23 15:01:50 -0700 | [diff] [blame] | 20 | #include <linux/magic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/buffer_head.h> |
Al Viro | ff01bb4 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 22 | #include <linux/swap.h> |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 23 | #include <linux/pagevec.h> |
David Howells | 811d736 | 2006-08-29 19:06:09 +0100 | [diff] [blame] | 24 | #include <linux/writeback.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/mpage.h> |
| 26 | #include <linux/mount.h> |
David Howells | 9030d16 | 2019-03-25 16:38:23 +0000 | [diff] [blame] | 27 | #include <linux/pseudo_fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/uio.h> |
| 29 | #include <linux/namei.h> |
Vignesh Babu BM | 1368c4f | 2007-05-08 00:24:32 -0700 | [diff] [blame] | 30 | #include <linux/log2.h> |
Al Viro | ff01bb4 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 31 | #include <linux/cleancache.h> |
Christoph Hellwig | 189ce2b | 2016-10-31 11:59:25 -0600 | [diff] [blame] | 32 | #include <linux/task_io_accounting_ops.h> |
Darrick J. Wong | 25f4c41 | 2016-10-11 13:51:11 -0700 | [diff] [blame] | 33 | #include <linux/falloc.h> |
Christoph Hellwig | 15e3d2c | 2020-11-24 09:34:00 +0100 | [diff] [blame] | 34 | #include <linux/part_stat.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 35 | #include <linux/uaccess.h> |
Domenico Andreoli | 56939e0 | 2020-03-23 08:22:15 -0700 | [diff] [blame] | 36 | #include <linux/suspend.h> |
David Howells | 07f3f05 | 2006-09-30 20:52:18 +0200 | [diff] [blame] | 37 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | struct bdev_inode { |
| 40 | struct block_device bdev; |
| 41 | struct inode vfs_inode; |
| 42 | }; |
| 43 | |
Adrian Bunk | 4c54ac6 | 2008-02-18 13:48:31 +0100 | [diff] [blame] | 44 | static const struct address_space_operations def_blk_aops; |
| 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | static inline struct bdev_inode *BDEV_I(struct inode *inode) |
| 47 | { |
| 48 | return container_of(inode, struct bdev_inode, vfs_inode); |
| 49 | } |
| 50 | |
Geert Uytterhoeven | ff5053f | 2015-06-26 13:58:32 +0200 | [diff] [blame] | 51 | struct block_device *I_BDEV(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { |
| 53 | return &BDEV_I(inode)->bdev; |
| 54 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | EXPORT_SYMBOL(I_BDEV); |
| 56 | |
Vivek Goyal | dbd3ca5 | 2015-11-09 09:23:40 -0700 | [diff] [blame] | 57 | static void bdev_write_inode(struct block_device *bdev) |
Christoph Hellwig | 564f00f | 2015-01-14 10:42:33 +0100 | [diff] [blame] | 58 | { |
Vivek Goyal | dbd3ca5 | 2015-11-09 09:23:40 -0700 | [diff] [blame] | 59 | struct inode *inode = bdev->bd_inode; |
| 60 | int ret; |
| 61 | |
Christoph Hellwig | 564f00f | 2015-01-14 10:42:33 +0100 | [diff] [blame] | 62 | spin_lock(&inode->i_lock); |
| 63 | while (inode->i_state & I_DIRTY) { |
| 64 | spin_unlock(&inode->i_lock); |
Vivek Goyal | dbd3ca5 | 2015-11-09 09:23:40 -0700 | [diff] [blame] | 65 | ret = write_inode_now(inode, true); |
| 66 | if (ret) { |
| 67 | char name[BDEVNAME_SIZE]; |
| 68 | pr_warn_ratelimited("VFS: Dirty inode writeback failed " |
| 69 | "for block device %s (err=%d).\n", |
| 70 | bdevname(bdev, name), ret); |
| 71 | } |
Christoph Hellwig | 564f00f | 2015-01-14 10:42:33 +0100 | [diff] [blame] | 72 | spin_lock(&inode->i_lock); |
| 73 | } |
| 74 | spin_unlock(&inode->i_lock); |
| 75 | } |
| 76 | |
Peter Zijlstra | f9a1439 | 2007-05-06 14:49:55 -0700 | [diff] [blame] | 77 | /* Kill _all_ buffers and pagecache , dirty or not.. */ |
Zheng Bin | 3373a34 | 2020-06-18 12:21:38 +0800 | [diff] [blame] | 78 | static void kill_bdev(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | { |
Al Viro | ff01bb4 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 80 | struct address_space *mapping = bdev->bd_inode->i_mapping; |
| 81 | |
Ross Zwisler | f9fe48b | 2016-01-22 15:10:40 -0800 | [diff] [blame] | 82 | if (mapping->nrpages == 0 && mapping->nrexceptional == 0) |
Peter Zijlstra | f9a1439 | 2007-05-06 14:49:55 -0700 | [diff] [blame] | 83 | return; |
Al Viro | ff01bb4 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 84 | |
Peter Zijlstra | f9a1439 | 2007-05-06 14:49:55 -0700 | [diff] [blame] | 85 | invalidate_bh_lrus(); |
Al Viro | ff01bb4 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 86 | truncate_inode_pages(mapping, 0); |
Zheng Bin | 3373a34 | 2020-06-18 12:21:38 +0800 | [diff] [blame] | 87 | } |
Al Viro | ff01bb4 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 88 | |
| 89 | /* Invalidate clean unused buffers and pagecache. */ |
| 90 | void invalidate_bdev(struct block_device *bdev) |
| 91 | { |
| 92 | struct address_space *mapping = bdev->bd_inode->i_mapping; |
| 93 | |
Andrey Ryabinin | a5f6a6a | 2017-05-03 14:56:02 -0700 | [diff] [blame] | 94 | if (mapping->nrpages) { |
| 95 | invalidate_bh_lrus(); |
| 96 | lru_add_drain_all(); /* make sure all lru add caches are flushed */ |
| 97 | invalidate_mapping_pages(mapping, 0, -1); |
| 98 | } |
Al Viro | ff01bb4 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 99 | /* 99% of the time, we don't need to flush the cleancache on the bdev. |
| 100 | * But, for the strange corners, lets be cautious |
| 101 | */ |
Dan Magenheimer | 3167760 | 2011-09-21 11:56:28 -0400 | [diff] [blame] | 102 | cleancache_invalidate_inode(mapping); |
Al Viro | ff01bb4 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 103 | } |
| 104 | EXPORT_SYMBOL(invalidate_bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
Jan Kara | 384d87e | 2020-09-04 10:58:52 +0200 | [diff] [blame] | 106 | /* |
| 107 | * Drop all buffers & page cache for given bdev range. This function bails |
| 108 | * with error if bdev has other exclusive owner (such as filesystem). |
| 109 | */ |
| 110 | int truncate_bdev_range(struct block_device *bdev, fmode_t mode, |
| 111 | loff_t lstart, loff_t lend) |
| 112 | { |
Jan Kara | 384d87e | 2020-09-04 10:58:52 +0200 | [diff] [blame] | 113 | /* |
| 114 | * If we don't hold exclusive handle for the device, upgrade to it |
| 115 | * while we discard the buffer cache to avoid discarding buffers |
| 116 | * under live filesystem. |
| 117 | */ |
| 118 | if (!(mode & FMODE_EXCL)) { |
Christoph Hellwig | 37c3fc9 | 2020-11-25 21:20:08 +0100 | [diff] [blame] | 119 | int err = bd_prepare_to_claim(bdev, truncate_bdev_range); |
Jan Kara | 384d87e | 2020-09-04 10:58:52 +0200 | [diff] [blame] | 120 | if (err) |
| 121 | return err; |
| 122 | } |
Christoph Hellwig | 37c3fc9 | 2020-11-25 21:20:08 +0100 | [diff] [blame] | 123 | |
Jan Kara | 384d87e | 2020-09-04 10:58:52 +0200 | [diff] [blame] | 124 | truncate_inode_pages_range(bdev->bd_inode->i_mapping, lstart, lend); |
Christoph Hellwig | 37c3fc9 | 2020-11-25 21:20:08 +0100 | [diff] [blame] | 125 | if (!(mode & FMODE_EXCL)) |
| 126 | bd_abort_claiming(bdev, truncate_bdev_range); |
Jan Kara | 384d87e | 2020-09-04 10:58:52 +0200 | [diff] [blame] | 127 | return 0; |
| 128 | } |
| 129 | EXPORT_SYMBOL(truncate_bdev_range); |
| 130 | |
Jan Kara | 04906b2 | 2019-01-14 09:48:10 +0100 | [diff] [blame] | 131 | static void set_init_blocksize(struct block_device *bdev) |
| 132 | { |
Maxim Mikityanskiy | 8dc932d | 2021-01-26 21:59:07 +0200 | [diff] [blame^] | 133 | unsigned int bsize = bdev_logical_block_size(bdev); |
| 134 | loff_t size = i_size_read(bdev->bd_inode); |
| 135 | |
| 136 | while (bsize < PAGE_SIZE) { |
| 137 | if (size & bsize) |
| 138 | break; |
| 139 | bsize <<= 1; |
| 140 | } |
| 141 | bdev->bd_inode->i_blkbits = blksize_bits(bsize); |
Jan Kara | 04906b2 | 2019-01-14 09:48:10 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | int set_blocksize(struct block_device *bdev, int size) |
| 145 | { |
| 146 | /* Size must be a power of two, and between 512 and PAGE_SIZE */ |
Vignesh Babu BM | 1368c4f | 2007-05-08 00:24:32 -0700 | [diff] [blame] | 147 | if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | return -EINVAL; |
| 149 | |
| 150 | /* Size cannot be smaller than the size supported by the device */ |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 151 | if (size < bdev_logical_block_size(bdev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | return -EINVAL; |
| 153 | |
| 154 | /* Don't change the size if it is same as current */ |
Christoph Hellwig | 6b7b181 | 2020-06-26 10:01:55 +0200 | [diff] [blame] | 155 | if (bdev->bd_inode->i_blkbits != blksize_bits(size)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | sync_blockdev(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | bdev->bd_inode->i_blkbits = blksize_bits(size); |
| 158 | kill_bdev(bdev); |
| 159 | } |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | EXPORT_SYMBOL(set_blocksize); |
| 164 | |
| 165 | int sb_set_blocksize(struct super_block *sb, int size) |
| 166 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | if (set_blocksize(sb->s_bdev, size)) |
| 168 | return 0; |
| 169 | /* If we get here, we know size is power of two |
| 170 | * and it's value is between 512 and PAGE_SIZE */ |
| 171 | sb->s_blocksize = size; |
Coywolf Qi Hunt | 38885bd | 2006-03-24 03:18:05 -0800 | [diff] [blame] | 172 | sb->s_blocksize_bits = blksize_bits(size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | return sb->s_blocksize; |
| 174 | } |
| 175 | |
| 176 | EXPORT_SYMBOL(sb_set_blocksize); |
| 177 | |
| 178 | int sb_min_blocksize(struct super_block *sb, int size) |
| 179 | { |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 180 | int minsize = bdev_logical_block_size(sb->s_bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | if (size < minsize) |
| 182 | size = minsize; |
| 183 | return sb_set_blocksize(sb, size); |
| 184 | } |
| 185 | |
| 186 | EXPORT_SYMBOL(sb_min_blocksize); |
| 187 | |
| 188 | static int |
| 189 | blkdev_get_block(struct inode *inode, sector_t iblock, |
| 190 | struct buffer_head *bh, int create) |
| 191 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | bh->b_bdev = I_BDEV(inode); |
| 193 | bh->b_blocknr = iblock; |
| 194 | set_buffer_mapped(bh); |
| 195 | return 0; |
| 196 | } |
| 197 | |
Dan Williams | 4ebb16c | 2015-10-28 07:48:19 +0900 | [diff] [blame] | 198 | static struct inode *bdev_file_inode(struct file *file) |
| 199 | { |
| 200 | return file->f_mapping->host; |
| 201 | } |
| 202 | |
Jens Axboe | 78250c0 | 2016-11-17 17:50:47 +0100 | [diff] [blame] | 203 | static unsigned int dio_bio_write_op(struct kiocb *iocb) |
| 204 | { |
| 205 | unsigned int op = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE; |
| 206 | |
| 207 | /* avoid the need for a I/O completion work item */ |
| 208 | if (iocb->ki_flags & IOCB_DSYNC) |
| 209 | op |= REQ_FUA; |
| 210 | return op; |
| 211 | } |
| 212 | |
Christoph Hellwig | 189ce2b | 2016-10-31 11:59:25 -0600 | [diff] [blame] | 213 | #define DIO_INLINE_BIO_VECS 4 |
| 214 | |
| 215 | static void blkdev_bio_end_io_simple(struct bio *bio) |
| 216 | { |
| 217 | struct task_struct *waiter = bio->bi_private; |
| 218 | |
| 219 | WRITE_ONCE(bio->bi_private, NULL); |
Jens Axboe | 0619317 | 2018-11-13 21:16:54 -0700 | [diff] [blame] | 220 | blk_wake_io_task(waiter); |
Christoph Hellwig | 189ce2b | 2016-10-31 11:59:25 -0600 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | static ssize_t |
| 224 | __blkdev_direct_IO_simple(struct kiocb *iocb, struct iov_iter *iter, |
| 225 | int nr_pages) |
| 226 | { |
| 227 | struct file *file = iocb->ki_filp; |
| 228 | struct block_device *bdev = I_BDEV(bdev_file_inode(file)); |
Christoph Hellwig | 9fec4a2 | 2019-06-26 15:49:26 +0200 | [diff] [blame] | 229 | struct bio_vec inline_vecs[DIO_INLINE_BIO_VECS], *vecs; |
Christoph Hellwig | 189ce2b | 2016-10-31 11:59:25 -0600 | [diff] [blame] | 230 | loff_t pos = iocb->ki_pos; |
| 231 | bool should_dirty = false; |
| 232 | struct bio bio; |
| 233 | ssize_t ret; |
| 234 | blk_qc_t qc; |
Christoph Hellwig | 189ce2b | 2016-10-31 11:59:25 -0600 | [diff] [blame] | 235 | |
Jens Axboe | 9a794fb | 2016-11-22 08:12:39 -0700 | [diff] [blame] | 236 | if ((pos | iov_iter_alignment(iter)) & |
| 237 | (bdev_logical_block_size(bdev) - 1)) |
Christoph Hellwig | 189ce2b | 2016-10-31 11:59:25 -0600 | [diff] [blame] | 238 | return -EINVAL; |
| 239 | |
Jens Axboe | 72ecad2 | 2016-11-16 23:11:42 -0700 | [diff] [blame] | 240 | if (nr_pages <= DIO_INLINE_BIO_VECS) |
| 241 | vecs = inline_vecs; |
| 242 | else { |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 243 | vecs = kmalloc_array(nr_pages, sizeof(struct bio_vec), |
| 244 | GFP_KERNEL); |
Jens Axboe | 72ecad2 | 2016-11-16 23:11:42 -0700 | [diff] [blame] | 245 | if (!vecs) |
| 246 | return -ENOMEM; |
| 247 | } |
| 248 | |
Ming Lei | 3a83f46 | 2016-11-22 08:57:21 -0700 | [diff] [blame] | 249 | bio_init(&bio, vecs, nr_pages); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 250 | bio_set_dev(&bio, bdev); |
Damien Le Moal | 4d1a476 | 2016-11-22 15:38:49 +0900 | [diff] [blame] | 251 | bio.bi_iter.bi_sector = pos >> 9; |
Jens Axboe | 45d06cf | 2017-06-27 11:01:22 -0600 | [diff] [blame] | 252 | bio.bi_write_hint = iocb->ki_hint; |
Christoph Hellwig | 189ce2b | 2016-10-31 11:59:25 -0600 | [diff] [blame] | 253 | bio.bi_private = current; |
| 254 | bio.bi_end_io = blkdev_bio_end_io_simple; |
Adam Manzanares | 074111c | 2018-05-22 10:52:20 -0700 | [diff] [blame] | 255 | bio.bi_ioprio = iocb->ki_ioprio; |
Christoph Hellwig | 189ce2b | 2016-10-31 11:59:25 -0600 | [diff] [blame] | 256 | |
| 257 | ret = bio_iov_iter_get_pages(&bio, iter); |
| 258 | if (unlikely(ret)) |
Martin Wilck | 9362dd1 | 2018-07-25 23:15:08 +0200 | [diff] [blame] | 259 | goto out; |
Christoph Hellwig | 189ce2b | 2016-10-31 11:59:25 -0600 | [diff] [blame] | 260 | ret = bio.bi_iter.bi_size; |
| 261 | |
| 262 | if (iov_iter_rw(iter) == READ) { |
Jens Axboe | 78250c0 | 2016-11-17 17:50:47 +0100 | [diff] [blame] | 263 | bio.bi_opf = REQ_OP_READ; |
Christoph Hellwig | 189ce2b | 2016-10-31 11:59:25 -0600 | [diff] [blame] | 264 | if (iter_is_iovec(iter)) |
| 265 | should_dirty = true; |
| 266 | } else { |
Jens Axboe | 78250c0 | 2016-11-17 17:50:47 +0100 | [diff] [blame] | 267 | bio.bi_opf = dio_bio_write_op(iocb); |
Christoph Hellwig | 189ce2b | 2016-10-31 11:59:25 -0600 | [diff] [blame] | 268 | task_io_account_write(ret); |
| 269 | } |
Jens Axboe | d1e3628 | 2018-08-29 10:36:56 -0600 | [diff] [blame] | 270 | if (iocb->ki_flags & IOCB_HIPRI) |
Jens Axboe | 0bbb280 | 2018-12-21 09:10:46 -0700 | [diff] [blame] | 271 | bio_set_polled(&bio, iocb); |
Christoph Hellwig | 189ce2b | 2016-10-31 11:59:25 -0600 | [diff] [blame] | 272 | |
| 273 | qc = submit_bio(&bio); |
| 274 | for (;;) { |
Linus Torvalds | 1ac5cd4 | 2019-01-02 10:46:03 -0800 | [diff] [blame] | 275 | set_current_state(TASK_UNINTERRUPTIBLE); |
Christoph Hellwig | 189ce2b | 2016-10-31 11:59:25 -0600 | [diff] [blame] | 276 | if (!READ_ONCE(bio.bi_private)) |
| 277 | break; |
| 278 | if (!(iocb->ki_flags & IOCB_HIPRI) || |
Jens Axboe | 0a1b8b8 | 2018-11-26 08:24:43 -0700 | [diff] [blame] | 279 | !blk_poll(bdev_get_queue(bdev), qc, true)) |
Ming Lei | e6249cd | 2020-05-03 09:54:22 +0800 | [diff] [blame] | 280 | blk_io_schedule(); |
Christoph Hellwig | 189ce2b | 2016-10-31 11:59:25 -0600 | [diff] [blame] | 281 | } |
| 282 | __set_current_state(TASK_RUNNING); |
| 283 | |
Christoph Hellwig | 9fec4a2 | 2019-06-26 15:49:26 +0200 | [diff] [blame] | 284 | bio_release_pages(&bio, should_dirty); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 285 | if (unlikely(bio.bi_status)) |
Linus Torvalds | c6b1e36 | 2017-07-03 10:34:51 -0700 | [diff] [blame] | 286 | ret = blk_status_to_errno(bio.bi_status); |
Jens Axboe | 9ae3b3f5 | 2017-06-28 15:30:13 -0600 | [diff] [blame] | 287 | |
Martin Wilck | 9362dd1 | 2018-07-25 23:15:08 +0200 | [diff] [blame] | 288 | out: |
| 289 | if (vecs != inline_vecs) |
| 290 | kfree(vecs); |
| 291 | |
Jens Axboe | 9ae3b3f5 | 2017-06-28 15:30:13 -0600 | [diff] [blame] | 292 | bio_uninit(&bio); |
| 293 | |
Christoph Hellwig | 189ce2b | 2016-10-31 11:59:25 -0600 | [diff] [blame] | 294 | return ret; |
| 295 | } |
| 296 | |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 297 | struct blkdev_dio { |
| 298 | union { |
| 299 | struct kiocb *iocb; |
| 300 | struct task_struct *waiter; |
| 301 | }; |
| 302 | size_t size; |
| 303 | atomic_t ref; |
| 304 | bool multi_bio : 1; |
| 305 | bool should_dirty : 1; |
| 306 | bool is_sync : 1; |
| 307 | struct bio bio; |
| 308 | }; |
| 309 | |
Kent Overstreet | 52190f8 | 2018-05-20 18:25:55 -0400 | [diff] [blame] | 310 | static struct bio_set blkdev_dio_pool; |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 311 | |
Christoph Hellwig | eae83ce | 2018-11-30 08:31:52 -0700 | [diff] [blame] | 312 | static int blkdev_iopoll(struct kiocb *kiocb, bool wait) |
| 313 | { |
| 314 | struct block_device *bdev = I_BDEV(kiocb->ki_filp->f_mapping->host); |
| 315 | struct request_queue *q = bdev_get_queue(bdev); |
| 316 | |
| 317 | return blk_poll(q, READ_ONCE(kiocb->ki_cookie), wait); |
| 318 | } |
| 319 | |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 320 | static void blkdev_bio_end_io(struct bio *bio) |
| 321 | { |
| 322 | struct blkdev_dio *dio = bio->bi_private; |
| 323 | bool should_dirty = dio->should_dirty; |
| 324 | |
Jason Yan | a89afe5 | 2019-04-12 10:09:16 +0800 | [diff] [blame] | 325 | if (bio->bi_status && !dio->bio.bi_status) |
| 326 | dio->bio.bi_status = bio->bi_status; |
| 327 | |
| 328 | if (!dio->multi_bio || atomic_dec_and_test(&dio->ref)) { |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 329 | if (!dio->is_sync) { |
| 330 | struct kiocb *iocb = dio->iocb; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 331 | ssize_t ret; |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 332 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 333 | if (likely(!dio->bio.bi_status)) { |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 334 | ret = dio->size; |
| 335 | iocb->ki_pos += ret; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 336 | } else { |
| 337 | ret = blk_status_to_errno(dio->bio.bi_status); |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | dio->iocb->ki_complete(iocb, ret, 0); |
Christoph Hellwig | 531724a | 2018-11-30 09:23:48 +0100 | [diff] [blame] | 341 | if (dio->multi_bio) |
| 342 | bio_put(&dio->bio); |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 343 | } else { |
| 344 | struct task_struct *waiter = dio->waiter; |
| 345 | |
| 346 | WRITE_ONCE(dio->waiter, NULL); |
Jens Axboe | 0619317 | 2018-11-13 21:16:54 -0700 | [diff] [blame] | 347 | blk_wake_io_task(waiter); |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
| 351 | if (should_dirty) { |
| 352 | bio_check_pages_dirty(bio); |
| 353 | } else { |
Christoph Hellwig | 57dfe3c | 2019-06-26 15:49:25 +0200 | [diff] [blame] | 354 | bio_release_pages(bio, false); |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 355 | bio_put(bio); |
| 356 | } |
| 357 | } |
| 358 | |
Andrew Morton | b2e895d | 2007-02-03 01:14:01 -0800 | [diff] [blame] | 359 | static ssize_t |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 360 | __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter, int nr_pages) |
Andrew Morton | b2e895d | 2007-02-03 01:14:01 -0800 | [diff] [blame] | 361 | { |
| 362 | struct file *file = iocb->ki_filp; |
Dan Williams | 4ebb16c | 2015-10-28 07:48:19 +0900 | [diff] [blame] | 363 | struct inode *inode = bdev_file_inode(file); |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 364 | struct block_device *bdev = I_BDEV(inode); |
Christoph Hellwig | 64d656a | 2016-12-22 19:20:45 +0100 | [diff] [blame] | 365 | struct blk_plug plug; |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 366 | struct blkdev_dio *dio; |
| 367 | struct bio *bio; |
Jens Axboe | cb700eb | 2018-11-15 19:56:53 -0700 | [diff] [blame] | 368 | bool is_poll = (iocb->ki_flags & IOCB_HIPRI) != 0; |
Christoph Hellwig | 690e532 | 2017-01-24 14:50:19 +0100 | [diff] [blame] | 369 | bool is_read = (iov_iter_rw(iter) == READ), is_sync; |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 370 | loff_t pos = iocb->ki_pos; |
| 371 | blk_qc_t qc = BLK_QC_T_NONE; |
Jens Axboe | 7b6620d | 2019-08-15 11:09:16 -0600 | [diff] [blame] | 372 | int ret = 0; |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 373 | |
Jens Axboe | 9a794fb | 2016-11-22 08:12:39 -0700 | [diff] [blame] | 374 | if ((pos | iov_iter_alignment(iter)) & |
| 375 | (bdev_logical_block_size(bdev) - 1)) |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 376 | return -EINVAL; |
| 377 | |
Jens Axboe | 7b6620d | 2019-08-15 11:09:16 -0600 | [diff] [blame] | 378 | bio = bio_alloc_bioset(GFP_KERNEL, nr_pages, &blkdev_dio_pool); |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 379 | |
| 380 | dio = container_of(bio, struct blkdev_dio, bio); |
Christoph Hellwig | 690e532 | 2017-01-24 14:50:19 +0100 | [diff] [blame] | 381 | dio->is_sync = is_sync = is_sync_kiocb(iocb); |
Christoph Hellwig | 531724a | 2018-11-30 09:23:48 +0100 | [diff] [blame] | 382 | if (dio->is_sync) { |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 383 | dio->waiter = current; |
Christoph Hellwig | 531724a | 2018-11-30 09:23:48 +0100 | [diff] [blame] | 384 | bio_get(bio); |
| 385 | } else { |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 386 | dio->iocb = iocb; |
Christoph Hellwig | 531724a | 2018-11-30 09:23:48 +0100 | [diff] [blame] | 387 | } |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 388 | |
| 389 | dio->size = 0; |
| 390 | dio->multi_bio = false; |
David Howells | 00e2370 | 2018-10-22 13:07:28 +0100 | [diff] [blame] | 391 | dio->should_dirty = is_read && iter_is_iovec(iter); |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 392 | |
Jens Axboe | cb700eb | 2018-11-15 19:56:53 -0700 | [diff] [blame] | 393 | /* |
| 394 | * Don't plug for HIPRI/polled IO, as those should go straight |
| 395 | * to issue |
| 396 | */ |
| 397 | if (!is_poll) |
| 398 | blk_start_plug(&plug); |
| 399 | |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 400 | for (;;) { |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 401 | bio_set_dev(bio, bdev); |
Damien Le Moal | 4d1a476 | 2016-11-22 15:38:49 +0900 | [diff] [blame] | 402 | bio->bi_iter.bi_sector = pos >> 9; |
Jens Axboe | 45d06cf | 2017-06-27 11:01:22 -0600 | [diff] [blame] | 403 | bio->bi_write_hint = iocb->ki_hint; |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 404 | bio->bi_private = dio; |
| 405 | bio->bi_end_io = blkdev_bio_end_io; |
Adam Manzanares | 074111c | 2018-05-22 10:52:20 -0700 | [diff] [blame] | 406 | bio->bi_ioprio = iocb->ki_ioprio; |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 407 | |
Jens Axboe | e15c2ff | 2019-08-06 13:34:31 -0600 | [diff] [blame] | 408 | ret = bio_iov_iter_get_pages(bio, iter); |
| 409 | if (unlikely(ret)) { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 410 | bio->bi_status = BLK_STS_IOERR; |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 411 | bio_endio(bio); |
| 412 | break; |
| 413 | } |
| 414 | |
| 415 | if (is_read) { |
| 416 | bio->bi_opf = REQ_OP_READ; |
| 417 | if (dio->should_dirty) |
| 418 | bio_set_pages_dirty(bio); |
| 419 | } else { |
| 420 | bio->bi_opf = dio_bio_write_op(iocb); |
| 421 | task_io_account_write(bio->bi_iter.bi_size); |
| 422 | } |
| 423 | |
Jens Axboe | 7b6620d | 2019-08-15 11:09:16 -0600 | [diff] [blame] | 424 | dio->size += bio->bi_iter.bi_size; |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 425 | pos += bio->bi_iter.bi_size; |
| 426 | |
| 427 | nr_pages = iov_iter_npages(iter, BIO_MAX_PAGES); |
| 428 | if (!nr_pages) { |
Christoph Hellwig | eae83ce | 2018-11-30 08:31:52 -0700 | [diff] [blame] | 429 | bool polled = false; |
| 430 | |
| 431 | if (iocb->ki_flags & IOCB_HIPRI) { |
Jens Axboe | 0bbb280 | 2018-12-21 09:10:46 -0700 | [diff] [blame] | 432 | bio_set_polled(bio, iocb); |
Christoph Hellwig | eae83ce | 2018-11-30 08:31:52 -0700 | [diff] [blame] | 433 | polled = true; |
| 434 | } |
Jens Axboe | d34513d | 2018-11-06 14:29:11 -0700 | [diff] [blame] | 435 | |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 436 | qc = submit_bio(bio); |
Christoph Hellwig | eae83ce | 2018-11-30 08:31:52 -0700 | [diff] [blame] | 437 | |
| 438 | if (polled) |
| 439 | WRITE_ONCE(iocb->ki_cookie, qc); |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 440 | break; |
| 441 | } |
| 442 | |
| 443 | if (!dio->multi_bio) { |
Christoph Hellwig | 531724a | 2018-11-30 09:23:48 +0100 | [diff] [blame] | 444 | /* |
| 445 | * AIO needs an extra reference to ensure the dio |
| 446 | * structure which is embedded into the first bio |
| 447 | * stays around. |
| 448 | */ |
| 449 | if (!is_sync) |
| 450 | bio_get(bio); |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 451 | dio->multi_bio = true; |
| 452 | atomic_set(&dio->ref, 2); |
| 453 | } else { |
| 454 | atomic_inc(&dio->ref); |
| 455 | } |
| 456 | |
Jens Axboe | 7b6620d | 2019-08-15 11:09:16 -0600 | [diff] [blame] | 457 | submit_bio(bio); |
| 458 | bio = bio_alloc(GFP_KERNEL, nr_pages); |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 459 | } |
Jens Axboe | cb700eb | 2018-11-15 19:56:53 -0700 | [diff] [blame] | 460 | |
| 461 | if (!is_poll) |
| 462 | blk_finish_plug(&plug); |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 463 | |
Christoph Hellwig | 690e532 | 2017-01-24 14:50:19 +0100 | [diff] [blame] | 464 | if (!is_sync) |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 465 | return -EIOCBQUEUED; |
| 466 | |
| 467 | for (;;) { |
Linus Torvalds | 1ac5cd4 | 2019-01-02 10:46:03 -0800 | [diff] [blame] | 468 | set_current_state(TASK_UNINTERRUPTIBLE); |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 469 | if (!READ_ONCE(dio->waiter)) |
| 470 | break; |
| 471 | |
| 472 | if (!(iocb->ki_flags & IOCB_HIPRI) || |
Jens Axboe | 0a1b8b8 | 2018-11-26 08:24:43 -0700 | [diff] [blame] | 473 | !blk_poll(bdev_get_queue(bdev), qc, true)) |
Ming Lei | e6249cd | 2020-05-03 09:54:22 +0800 | [diff] [blame] | 474 | blk_io_schedule(); |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 475 | } |
| 476 | __set_current_state(TASK_RUNNING); |
| 477 | |
Christoph Hellwig | 36ffc6c | 2017-06-03 09:38:00 +0200 | [diff] [blame] | 478 | if (!ret) |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 479 | ret = blk_status_to_errno(dio->bio.bi_status); |
Jens Axboe | e15c2ff | 2019-08-06 13:34:31 -0600 | [diff] [blame] | 480 | if (likely(!ret)) |
| 481 | ret = dio->size; |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 482 | |
| 483 | bio_put(&dio->bio); |
| 484 | return ret; |
| 485 | } |
| 486 | |
| 487 | static ssize_t |
| 488 | blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter) |
| 489 | { |
Christoph Hellwig | 189ce2b | 2016-10-31 11:59:25 -0600 | [diff] [blame] | 490 | int nr_pages; |
Andrew Morton | b2e895d | 2007-02-03 01:14:01 -0800 | [diff] [blame] | 491 | |
Jens Axboe | 72ecad2 | 2016-11-16 23:11:42 -0700 | [diff] [blame] | 492 | nr_pages = iov_iter_npages(iter, BIO_MAX_PAGES + 1); |
Christoph Hellwig | 189ce2b | 2016-10-31 11:59:25 -0600 | [diff] [blame] | 493 | if (!nr_pages) |
| 494 | return 0; |
Jens Axboe | 72ecad2 | 2016-11-16 23:11:42 -0700 | [diff] [blame] | 495 | if (is_sync_kiocb(iocb) && nr_pages <= BIO_MAX_PAGES) |
Christoph Hellwig | 189ce2b | 2016-10-31 11:59:25 -0600 | [diff] [blame] | 496 | return __blkdev_direct_IO_simple(iocb, iter, nr_pages); |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 497 | |
| 498 | return __blkdev_direct_IO(iocb, iter, min(nr_pages, BIO_MAX_PAGES)); |
Andrew Morton | b2e895d | 2007-02-03 01:14:01 -0800 | [diff] [blame] | 499 | } |
| 500 | |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 501 | static __init int blkdev_init(void) |
| 502 | { |
Kent Overstreet | 52190f8 | 2018-05-20 18:25:55 -0400 | [diff] [blame] | 503 | return bioset_init(&blkdev_dio_pool, 4, offsetof(struct blkdev_dio, bio), BIOSET_NEED_BVECS); |
Christoph Hellwig | 542ff7b | 2016-11-16 23:14:22 -0700 | [diff] [blame] | 504 | } |
| 505 | module_init(blkdev_init); |
| 506 | |
Jan Kara | 5cee581 | 2009-04-27 16:43:51 +0200 | [diff] [blame] | 507 | int __sync_blockdev(struct block_device *bdev, int wait) |
| 508 | { |
| 509 | if (!bdev) |
| 510 | return 0; |
| 511 | if (!wait) |
| 512 | return filemap_flush(bdev->bd_inode->i_mapping); |
| 513 | return filemap_write_and_wait(bdev->bd_inode->i_mapping); |
| 514 | } |
| 515 | |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 516 | /* |
| 517 | * Write out and wait upon all the dirty data associated with a block |
| 518 | * device via its mapping. Does not take the superblock lock. |
| 519 | */ |
| 520 | int sync_blockdev(struct block_device *bdev) |
| 521 | { |
Jan Kara | 5cee581 | 2009-04-27 16:43:51 +0200 | [diff] [blame] | 522 | return __sync_blockdev(bdev, 1); |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 523 | } |
| 524 | EXPORT_SYMBOL(sync_blockdev); |
| 525 | |
| 526 | /* |
| 527 | * Write out and wait upon all dirty data associated with this |
| 528 | * device. Filesystem data as well as the underlying block |
| 529 | * device. Takes the superblock lock. |
| 530 | */ |
| 531 | int fsync_bdev(struct block_device *bdev) |
| 532 | { |
| 533 | struct super_block *sb = get_super(bdev); |
| 534 | if (sb) { |
Jan Kara | 60b0680 | 2009-04-27 16:43:53 +0200 | [diff] [blame] | 535 | int res = sync_filesystem(sb); |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 536 | drop_super(sb); |
| 537 | return res; |
| 538 | } |
| 539 | return sync_blockdev(bdev); |
| 540 | } |
Al Viro | 47e4491 | 2009-04-01 07:07:16 -0400 | [diff] [blame] | 541 | EXPORT_SYMBOL(fsync_bdev); |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 542 | |
| 543 | /** |
| 544 | * freeze_bdev -- lock a filesystem and force it into a consistent state |
| 545 | * @bdev: blockdevice to lock |
| 546 | * |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 547 | * If a superblock is found on this device, we take the s_umount semaphore |
| 548 | * on it to make sure nobody unmounts until the snapshot creation is done. |
| 549 | * The reference counter (bd_fsfreeze_count) guarantees that only the last |
| 550 | * unfreeze process can unfreeze the frozen filesystem actually when multiple |
| 551 | * freeze requests arrive simultaneously. It counts up in freeze_bdev() and |
| 552 | * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze |
| 553 | * actually. |
| 554 | */ |
Christoph Hellwig | 040f04b | 2020-11-24 11:54:06 +0100 | [diff] [blame] | 555 | int freeze_bdev(struct block_device *bdev) |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 556 | { |
| 557 | struct super_block *sb; |
| 558 | int error = 0; |
| 559 | |
| 560 | mutex_lock(&bdev->bd_fsfreeze_mutex); |
Christoph Hellwig | 040f04b | 2020-11-24 11:54:06 +0100 | [diff] [blame] | 561 | if (++bdev->bd_fsfreeze_count > 1) |
| 562 | goto done; |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 563 | |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 564 | sb = get_active_super(bdev); |
| 565 | if (!sb) |
Christoph Hellwig | 040f04b | 2020-11-24 11:54:06 +0100 | [diff] [blame] | 566 | goto sync; |
Benjamin Marzinski | 48b6bca | 2014-11-13 20:42:03 -0600 | [diff] [blame] | 567 | if (sb->s_op->freeze_super) |
| 568 | error = sb->s_op->freeze_super(sb); |
| 569 | else |
| 570 | error = freeze_super(sb); |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 571 | deactivate_super(sb); |
Christoph Hellwig | 040f04b | 2020-11-24 11:54:06 +0100 | [diff] [blame] | 572 | |
| 573 | if (error) { |
| 574 | bdev->bd_fsfreeze_count--; |
| 575 | goto done; |
| 576 | } |
| 577 | bdev->bd_fsfreeze_sb = sb; |
| 578 | |
| 579 | sync: |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 580 | sync_blockdev(bdev); |
Christoph Hellwig | 040f04b | 2020-11-24 11:54:06 +0100 | [diff] [blame] | 581 | done: |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 582 | mutex_unlock(&bdev->bd_fsfreeze_mutex); |
Christoph Hellwig | 040f04b | 2020-11-24 11:54:06 +0100 | [diff] [blame] | 583 | return error; |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 584 | } |
| 585 | EXPORT_SYMBOL(freeze_bdev); |
| 586 | |
| 587 | /** |
| 588 | * thaw_bdev -- unlock filesystem |
| 589 | * @bdev: blockdevice to unlock |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 590 | * |
| 591 | * Unlocks the filesystem and marks it writeable again after freeze_bdev(). |
| 592 | */ |
Christoph Hellwig | 040f04b | 2020-11-24 11:54:06 +0100 | [diff] [blame] | 593 | int thaw_bdev(struct block_device *bdev) |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 594 | { |
Christoph Hellwig | 040f04b | 2020-11-24 11:54:06 +0100 | [diff] [blame] | 595 | struct super_block *sb; |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 596 | int error = -EINVAL; |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 597 | |
| 598 | mutex_lock(&bdev->bd_fsfreeze_mutex); |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 599 | if (!bdev->bd_fsfreeze_count) |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 600 | goto out; |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 601 | |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 602 | error = 0; |
| 603 | if (--bdev->bd_fsfreeze_count > 0) |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 604 | goto out; |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 605 | |
Christoph Hellwig | 040f04b | 2020-11-24 11:54:06 +0100 | [diff] [blame] | 606 | sb = bdev->bd_fsfreeze_sb; |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 607 | if (!sb) |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 608 | goto out; |
Christoph Hellwig | 4504230 | 2009-08-03 23:28:35 +0200 | [diff] [blame] | 609 | |
Benjamin Marzinski | 48b6bca | 2014-11-13 20:42:03 -0600 | [diff] [blame] | 610 | if (sb->s_op->thaw_super) |
| 611 | error = sb->s_op->thaw_super(sb); |
| 612 | else |
| 613 | error = thaw_super(sb); |
Pierre Morel | 997198b | 2016-10-04 10:53:40 +0200 | [diff] [blame] | 614 | if (error) |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 615 | bdev->bd_fsfreeze_count++; |
Satya Tangirala | 04a6a53 | 2020-12-24 04:49:54 +0000 | [diff] [blame] | 616 | else |
| 617 | bdev->bd_fsfreeze_sb = NULL; |
Josef Bacik | 18e9e51 | 2010-03-23 10:34:56 -0400 | [diff] [blame] | 618 | out: |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 619 | mutex_unlock(&bdev->bd_fsfreeze_mutex); |
Pierre Morel | 997198b | 2016-10-04 10:53:40 +0200 | [diff] [blame] | 620 | return error; |
Nick Piggin | 585d3bc | 2009-02-25 10:44:19 +0100 | [diff] [blame] | 621 | } |
| 622 | EXPORT_SYMBOL(thaw_bdev); |
| 623 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | static int blkdev_writepage(struct page *page, struct writeback_control *wbc) |
| 625 | { |
| 626 | return block_write_full_page(page, blkdev_get_block, wbc); |
| 627 | } |
| 628 | |
| 629 | static int blkdev_readpage(struct file * file, struct page * page) |
| 630 | { |
| 631 | return block_read_full_page(page, blkdev_get_block); |
| 632 | } |
| 633 | |
Matthew Wilcox (Oracle) | d438834 | 2020-06-01 21:47:02 -0700 | [diff] [blame] | 634 | static void blkdev_readahead(struct readahead_control *rac) |
Akinobu Mita | 447f05b | 2014-10-09 15:26:58 -0700 | [diff] [blame] | 635 | { |
Matthew Wilcox (Oracle) | d438834 | 2020-06-01 21:47:02 -0700 | [diff] [blame] | 636 | mpage_readahead(rac, blkdev_get_block); |
Akinobu Mita | 447f05b | 2014-10-09 15:26:58 -0700 | [diff] [blame] | 637 | } |
| 638 | |
Nick Piggin | 6272b5a | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 639 | static int blkdev_write_begin(struct file *file, struct address_space *mapping, |
| 640 | loff_t pos, unsigned len, unsigned flags, |
| 641 | struct page **pagep, void **fsdata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | { |
Christoph Hellwig | 155130a | 2010-06-04 11:29:58 +0200 | [diff] [blame] | 643 | return block_write_begin(mapping, pos, len, flags, pagep, |
| 644 | blkdev_get_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Nick Piggin | 6272b5a | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 647 | static int blkdev_write_end(struct file *file, struct address_space *mapping, |
| 648 | loff_t pos, unsigned len, unsigned copied, |
| 649 | struct page *page, void *fsdata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | { |
Nick Piggin | 6272b5a | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 651 | int ret; |
| 652 | ret = block_write_end(file, mapping, pos, len, copied, page, fsdata); |
| 653 | |
| 654 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 655 | put_page(page); |
Nick Piggin | 6272b5a | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 656 | |
| 657 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | /* |
| 661 | * private llseek: |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 662 | * for a block special file file_inode(file)->i_size is zero |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | * so we compute the size by hand (just as in block_read/write above) |
| 664 | */ |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 665 | static loff_t block_llseek(struct file *file, loff_t offset, int whence) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | { |
Dan Williams | 4ebb16c | 2015-10-28 07:48:19 +0900 | [diff] [blame] | 667 | struct inode *bd_inode = bdev_file_inode(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | loff_t retval; |
| 669 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 670 | inode_lock(bd_inode); |
Al Viro | 5d48f3a | 2013-06-23 21:34:45 +0400 | [diff] [blame] | 671 | retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode)); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 672 | inode_unlock(bd_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | return retval; |
| 674 | } |
| 675 | |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 676 | int blkdev_fsync(struct file *filp, loff_t start, loff_t end, int datasync) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | { |
Dan Williams | 4ebb16c | 2015-10-28 07:48:19 +0900 | [diff] [blame] | 678 | struct inode *bd_inode = bdev_file_inode(filp); |
Anton Blanchard | b8af67e | 2010-04-23 13:18:06 -0400 | [diff] [blame] | 679 | struct block_device *bdev = I_BDEV(bd_inode); |
Christoph Hellwig | ab0a973 | 2009-10-29 14:14:04 +0100 | [diff] [blame] | 680 | int error; |
Rafael J. Wysocki | da5aa86 | 2011-08-02 02:17:48 +0200 | [diff] [blame] | 681 | |
Jeff Layton | 372cf24 | 2017-07-06 07:02:28 -0400 | [diff] [blame] | 682 | error = file_write_and_wait_range(filp, start, end); |
Rafael J. Wysocki | da5aa86 | 2011-08-02 02:17:48 +0200 | [diff] [blame] | 683 | if (error) |
| 684 | return error; |
Christoph Hellwig | ab0a973 | 2009-10-29 14:14:04 +0100 | [diff] [blame] | 685 | |
Anton Blanchard | b8af67e | 2010-04-23 13:18:06 -0400 | [diff] [blame] | 686 | /* |
| 687 | * There is no need to serialise calls to blkdev_issue_flush with |
| 688 | * i_mutex and doing so causes performance issues with concurrent |
| 689 | * O_SYNC writers to a block device. |
| 690 | */ |
Christoph Hellwig | 9398554 | 2020-05-13 14:36:00 +0200 | [diff] [blame] | 691 | error = blkdev_issue_flush(bdev, GFP_KERNEL); |
Christoph Hellwig | ab0a973 | 2009-10-29 14:14:04 +0100 | [diff] [blame] | 692 | if (error == -EOPNOTSUPP) |
| 693 | error = 0; |
Anton Blanchard | b8af67e | 2010-04-23 13:18:06 -0400 | [diff] [blame] | 694 | |
Christoph Hellwig | ab0a973 | 2009-10-29 14:14:04 +0100 | [diff] [blame] | 695 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | } |
Andrew Morton | b1dd3b2 | 2010-04-06 14:35:00 -0700 | [diff] [blame] | 697 | EXPORT_SYMBOL(blkdev_fsync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | |
Matthew Wilcox | 47a191f | 2014-06-04 16:07:46 -0700 | [diff] [blame] | 699 | /** |
| 700 | * bdev_read_page() - Start reading a page from a block device |
| 701 | * @bdev: The device to read the page from |
| 702 | * @sector: The offset on the device to read the page to (need not be aligned) |
| 703 | * @page: The page to read |
| 704 | * |
| 705 | * On entry, the page should be locked. It will be unlocked when the page |
| 706 | * has been read. If the block driver implements rw_page synchronously, |
| 707 | * that will be true on exit from this function, but it need not be. |
| 708 | * |
| 709 | * Errors returned by this function are usually "soft", eg out of memory, or |
| 710 | * queue full; callers should try a different route to read this page rather |
| 711 | * than propagate an error back up the stack. |
| 712 | * |
| 713 | * Return: negative errno if an error occurs, 0 if submission was successful. |
| 714 | */ |
| 715 | int bdev_read_page(struct block_device *bdev, sector_t sector, |
| 716 | struct page *page) |
| 717 | { |
| 718 | const struct block_device_operations *ops = bdev->bd_disk->fops; |
Dan Williams | 2e6edc95 | 2015-11-19 13:29:28 -0800 | [diff] [blame] | 719 | int result = -EOPNOTSUPP; |
| 720 | |
Vishal Verma | f68eb1e | 2015-05-12 13:48:53 -0400 | [diff] [blame] | 721 | if (!ops->rw_page || bdev_get_integrity(bdev)) |
Dan Williams | 2e6edc95 | 2015-11-19 13:29:28 -0800 | [diff] [blame] | 722 | return result; |
| 723 | |
Christoph Hellwig | e556f6b | 2020-06-26 10:01:56 +0200 | [diff] [blame] | 724 | result = blk_queue_enter(bdev->bd_disk->queue, 0); |
Dan Williams | 2e6edc95 | 2015-11-19 13:29:28 -0800 | [diff] [blame] | 725 | if (result) |
| 726 | return result; |
Tejun Heo | 3f289dc | 2018-07-18 04:47:36 -0700 | [diff] [blame] | 727 | result = ops->rw_page(bdev, sector + get_start_sect(bdev), page, |
| 728 | REQ_OP_READ); |
Christoph Hellwig | e556f6b | 2020-06-26 10:01:56 +0200 | [diff] [blame] | 729 | blk_queue_exit(bdev->bd_disk->queue); |
Dan Williams | 2e6edc95 | 2015-11-19 13:29:28 -0800 | [diff] [blame] | 730 | return result; |
Matthew Wilcox | 47a191f | 2014-06-04 16:07:46 -0700 | [diff] [blame] | 731 | } |
Matthew Wilcox | 47a191f | 2014-06-04 16:07:46 -0700 | [diff] [blame] | 732 | |
| 733 | /** |
| 734 | * bdev_write_page() - Start writing a page to a block device |
| 735 | * @bdev: The device to write the page to |
| 736 | * @sector: The offset on the device to write the page to (need not be aligned) |
| 737 | * @page: The page to write |
| 738 | * @wbc: The writeback_control for the write |
| 739 | * |
| 740 | * On entry, the page should be locked and not currently under writeback. |
| 741 | * On exit, if the write started successfully, the page will be unlocked and |
| 742 | * under writeback. If the write failed already (eg the driver failed to |
| 743 | * queue the page to the device), the page will still be locked. If the |
| 744 | * caller is a ->writepage implementation, it will need to unlock the page. |
| 745 | * |
| 746 | * Errors returned by this function are usually "soft", eg out of memory, or |
| 747 | * queue full; callers should try a different route to write this page rather |
| 748 | * than propagate an error back up the stack. |
| 749 | * |
| 750 | * Return: negative errno if an error occurs, 0 if submission was successful. |
| 751 | */ |
| 752 | int bdev_write_page(struct block_device *bdev, sector_t sector, |
| 753 | struct page *page, struct writeback_control *wbc) |
| 754 | { |
| 755 | int result; |
Matthew Wilcox | 47a191f | 2014-06-04 16:07:46 -0700 | [diff] [blame] | 756 | const struct block_device_operations *ops = bdev->bd_disk->fops; |
Dan Williams | 2e6edc95 | 2015-11-19 13:29:28 -0800 | [diff] [blame] | 757 | |
Vishal Verma | f68eb1e | 2015-05-12 13:48:53 -0400 | [diff] [blame] | 758 | if (!ops->rw_page || bdev_get_integrity(bdev)) |
Matthew Wilcox | 47a191f | 2014-06-04 16:07:46 -0700 | [diff] [blame] | 759 | return -EOPNOTSUPP; |
Christoph Hellwig | e556f6b | 2020-06-26 10:01:56 +0200 | [diff] [blame] | 760 | result = blk_queue_enter(bdev->bd_disk->queue, 0); |
Dan Williams | 2e6edc95 | 2015-11-19 13:29:28 -0800 | [diff] [blame] | 761 | if (result) |
| 762 | return result; |
| 763 | |
Matthew Wilcox | 47a191f | 2014-06-04 16:07:46 -0700 | [diff] [blame] | 764 | set_page_writeback(page); |
Tejun Heo | 3f289dc | 2018-07-18 04:47:36 -0700 | [diff] [blame] | 765 | result = ops->rw_page(bdev, sector + get_start_sect(bdev), page, |
| 766 | REQ_OP_WRITE); |
Matthew Wilcox | f892760 | 2017-10-13 15:58:15 -0700 | [diff] [blame] | 767 | if (result) { |
Matthew Wilcox | 47a191f | 2014-06-04 16:07:46 -0700 | [diff] [blame] | 768 | end_page_writeback(page); |
Matthew Wilcox | f892760 | 2017-10-13 15:58:15 -0700 | [diff] [blame] | 769 | } else { |
| 770 | clean_page_buffers(page); |
Matthew Wilcox | 47a191f | 2014-06-04 16:07:46 -0700 | [diff] [blame] | 771 | unlock_page(page); |
Matthew Wilcox | f892760 | 2017-10-13 15:58:15 -0700 | [diff] [blame] | 772 | } |
Christoph Hellwig | e556f6b | 2020-06-26 10:01:56 +0200 | [diff] [blame] | 773 | blk_queue_exit(bdev->bd_disk->queue); |
Matthew Wilcox | 47a191f | 2014-06-04 16:07:46 -0700 | [diff] [blame] | 774 | return result; |
| 775 | } |
Matthew Wilcox | 47a191f | 2014-06-04 16:07:46 -0700 | [diff] [blame] | 776 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | /* |
| 778 | * pseudo-fs |
| 779 | */ |
| 780 | |
| 781 | static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock); |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 782 | static struct kmem_cache * bdev_cachep __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | |
| 784 | static struct inode *bdev_alloc_inode(struct super_block *sb) |
| 785 | { |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 786 | struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL); |
Christoph Hellwig | 2d2f6f1 | 2021-01-07 19:36:40 +0100 | [diff] [blame] | 787 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | if (!ei) |
| 789 | return NULL; |
Christoph Hellwig | 2d2f6f1 | 2021-01-07 19:36:40 +0100 | [diff] [blame] | 790 | memset(&ei->bdev, 0, sizeof(ei->bdev)); |
| 791 | ei->bdev.bd_bdi = &noop_backing_dev_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | return &ei->vfs_inode; |
| 793 | } |
| 794 | |
Al Viro | 41149cb | 2019-04-10 15:12:38 -0400 | [diff] [blame] | 795 | static void bdev_free_inode(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | { |
Christoph Hellwig | 15e3d2c | 2020-11-24 09:34:00 +0100 | [diff] [blame] | 797 | struct block_device *bdev = I_BDEV(inode); |
| 798 | |
| 799 | free_percpu(bdev->bd_stats); |
Christoph Hellwig | 231926d | 2020-11-24 12:01:45 +0100 | [diff] [blame] | 800 | kfree(bdev->bd_meta_info); |
Christoph Hellwig | 15e3d2c | 2020-11-24 09:34:00 +0100 | [diff] [blame] | 801 | |
Al Viro | 41149cb | 2019-04-10 15:12:38 -0400 | [diff] [blame] | 802 | kmem_cache_free(bdev_cachep, BDEV_I(inode)); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 803 | } |
| 804 | |
Christoph Hellwig | e6cb538 | 2020-11-23 15:41:40 +0100 | [diff] [blame] | 805 | static void init_once(void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | { |
Christoph Hellwig | e6cb538 | 2020-11-23 15:41:40 +0100 | [diff] [blame] | 807 | struct bdev_inode *ei = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | |
Christoph Lameter | a35afb8 | 2007-05-16 22:10:57 -0700 | [diff] [blame] | 809 | inode_init_once(&ei->vfs_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | } |
| 811 | |
Al Viro | b57922d | 2010-06-07 14:34:48 -0400 | [diff] [blame] | 812 | static void bdev_evict_inode(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | { |
| 814 | struct block_device *bdev = &BDEV_I(inode)->bdev; |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 815 | truncate_inode_pages_final(&inode->i_data); |
Al Viro | b57922d | 2010-06-07 14:34:48 -0400 | [diff] [blame] | 816 | invalidate_inode_buffers(inode); /* is it needed here? */ |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 817 | clear_inode(inode); |
Jan Kara | f759741 | 2017-03-23 01:37:00 +0100 | [diff] [blame] | 818 | /* Detach inode from wb early as bdi_put() may free bdi->wb */ |
| 819 | inode_detach_wb(inode); |
Jan Kara | a5a79d0 | 2017-03-02 16:50:13 +0100 | [diff] [blame] | 820 | if (bdev->bd_bdi != &noop_backing_dev_info) { |
Jan Kara | b1d2dc56 | 2017-02-02 15:56:52 +0100 | [diff] [blame] | 821 | bdi_put(bdev->bd_bdi); |
Jan Kara | a5a79d0 | 2017-03-02 16:50:13 +0100 | [diff] [blame] | 822 | bdev->bd_bdi = &noop_backing_dev_info; |
| 823 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | } |
| 825 | |
Josef 'Jeff' Sipek | ee9b6d6 | 2007-02-12 00:55:41 -0800 | [diff] [blame] | 826 | static const struct super_operations bdev_sops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | .statfs = simple_statfs, |
| 828 | .alloc_inode = bdev_alloc_inode, |
Al Viro | 41149cb | 2019-04-10 15:12:38 -0400 | [diff] [blame] | 829 | .free_inode = bdev_free_inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | .drop_inode = generic_delete_inode, |
Al Viro | b57922d | 2010-06-07 14:34:48 -0400 | [diff] [blame] | 831 | .evict_inode = bdev_evict_inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | }; |
| 833 | |
David Howells | 9030d16 | 2019-03-25 16:38:23 +0000 | [diff] [blame] | 834 | static int bd_init_fs_context(struct fs_context *fc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | { |
David Howells | 9030d16 | 2019-03-25 16:38:23 +0000 | [diff] [blame] | 836 | struct pseudo_fs_context *ctx = init_pseudo(fc, BDEVFS_MAGIC); |
| 837 | if (!ctx) |
| 838 | return -ENOMEM; |
| 839 | fc->s_iflags |= SB_I_CGROUPWB; |
| 840 | ctx->ops = &bdev_sops; |
| 841 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | static struct file_system_type bd_type = { |
| 845 | .name = "bdev", |
David Howells | 9030d16 | 2019-03-25 16:38:23 +0000 | [diff] [blame] | 846 | .init_fs_context = bd_init_fs_context, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | .kill_sb = kill_anon_super, |
| 848 | }; |
| 849 | |
Tejun Heo | a212b10 | 2015-05-22 17:13:33 -0400 | [diff] [blame] | 850 | struct super_block *blockdev_superblock __read_mostly; |
| 851 | EXPORT_SYMBOL_GPL(blockdev_superblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | |
| 853 | void __init bdev_cache_init(void) |
| 854 | { |
| 855 | int err; |
Sergey Senozhatsky | ace8577 | 2012-01-10 02:43:59 +0300 | [diff] [blame] | 856 | static struct vfsmount *bd_mnt; |
Denis ChengRq | c2acf7b | 2008-12-01 14:34:56 -0800 | [diff] [blame] | 857 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode), |
Paul Jackson | fffb60f | 2006-03-24 03:16:06 -0800 | [diff] [blame] | 859 | 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT| |
Vladimir Davydov | 5d09705 | 2016-01-14 15:18:21 -0800 | [diff] [blame] | 860 | SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 861 | init_once); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | err = register_filesystem(&bd_type); |
| 863 | if (err) |
| 864 | panic("Cannot register bdev pseudo-fs"); |
| 865 | bd_mnt = kern_mount(&bd_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | if (IS_ERR(bd_mnt)) |
| 867 | panic("Cannot create bdev pseudo-fs"); |
Sergey Senozhatsky | ace8577 | 2012-01-10 02:43:59 +0300 | [diff] [blame] | 868 | blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | } |
| 870 | |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 871 | struct block_device *bdev_alloc(struct gendisk *disk, u8 partno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | { |
| 873 | struct block_device *bdev; |
| 874 | struct inode *inode; |
| 875 | |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 876 | inode = new_inode(blockdev_superblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | if (!inode) |
| 878 | return NULL; |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 879 | inode->i_mode = S_IFBLK; |
| 880 | inode->i_rdev = 0; |
| 881 | inode->i_data.a_ops = &def_blk_aops; |
| 882 | mapping_set_gfp_mask(&inode->i_data, GFP_USER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 884 | bdev = I_BDEV(inode); |
Christoph Hellwig | e6cb538 | 2020-11-23 15:41:40 +0100 | [diff] [blame] | 885 | mutex_init(&bdev->bd_mutex); |
| 886 | mutex_init(&bdev->bd_fsfreeze_mutex); |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 887 | spin_lock_init(&bdev->bd_size_lock); |
| 888 | bdev->bd_disk = disk; |
| 889 | bdev->bd_partno = partno; |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 890 | bdev->bd_inode = inode; |
Christoph Hellwig | e6cb538 | 2020-11-23 15:41:40 +0100 | [diff] [blame] | 891 | #ifdef CONFIG_SYSFS |
| 892 | INIT_LIST_HEAD(&bdev->bd_holder_disks); |
| 893 | #endif |
Christoph Hellwig | 15e3d2c | 2020-11-24 09:34:00 +0100 | [diff] [blame] | 894 | bdev->bd_stats = alloc_percpu(struct disk_stats); |
| 895 | if (!bdev->bd_stats) { |
| 896 | iput(inode); |
| 897 | return NULL; |
| 898 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | return bdev; |
| 900 | } |
| 901 | |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 902 | void bdev_add(struct block_device *bdev, dev_t dev) |
| 903 | { |
| 904 | bdev->bd_dev = dev; |
| 905 | bdev->bd_inode->i_rdev = dev; |
| 906 | bdev->bd_inode->i_ino = dev; |
| 907 | insert_inode_hash(bdev->bd_inode); |
| 908 | } |
| 909 | |
| 910 | static struct block_device *bdget(dev_t dev) |
| 911 | { |
| 912 | struct inode *inode; |
| 913 | |
| 914 | inode = ilookup(blockdev_superblock, dev); |
| 915 | if (!inode) |
| 916 | return NULL; |
| 917 | return &BDEV_I(inode)->bdev; |
| 918 | } |
| 919 | |
Alan Jenkins | dddac6a | 2009-07-29 21:07:55 +0200 | [diff] [blame] | 920 | /** |
| 921 | * bdgrab -- Grab a reference to an already referenced block device |
| 922 | * @bdev: Block device to grab a reference to. |
Christoph Hellwig | 3a4174e | 2020-11-27 16:45:27 +0100 | [diff] [blame] | 923 | * |
| 924 | * Returns the block_device with an additional reference when successful, |
| 925 | * or NULL if the inode is already beeing freed. |
Alan Jenkins | dddac6a | 2009-07-29 21:07:55 +0200 | [diff] [blame] | 926 | */ |
| 927 | struct block_device *bdgrab(struct block_device *bdev) |
| 928 | { |
Christoph Hellwig | 3a4174e | 2020-11-27 16:45:27 +0100 | [diff] [blame] | 929 | if (!igrab(bdev->bd_inode)) |
| 930 | return NULL; |
Alan Jenkins | dddac6a | 2009-07-29 21:07:55 +0200 | [diff] [blame] | 931 | return bdev; |
| 932 | } |
Anatol Pomozov | c1681bf | 2013-04-01 09:47:56 -0700 | [diff] [blame] | 933 | EXPORT_SYMBOL(bdgrab); |
Alan Jenkins | dddac6a | 2009-07-29 21:07:55 +0200 | [diff] [blame] | 934 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | long nr_blockdev_pages(void) |
| 936 | { |
Christoph Hellwig | 1008fe6 | 2020-06-26 10:01:58 +0200 | [diff] [blame] | 937 | struct inode *inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | long ret = 0; |
Christoph Hellwig | 1008fe6 | 2020-06-26 10:01:58 +0200 | [diff] [blame] | 939 | |
| 940 | spin_lock(&blockdev_superblock->s_inode_list_lock); |
| 941 | list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) |
| 942 | ret += inode->i_mapping->nrpages; |
| 943 | spin_unlock(&blockdev_superblock->s_inode_list_lock); |
| 944 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | return ret; |
| 946 | } |
| 947 | |
| 948 | void bdput(struct block_device *bdev) |
| 949 | { |
| 950 | iput(bdev->bd_inode); |
| 951 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | EXPORT_SYMBOL(bdput); |
| 953 | |
Tejun Heo | 1a3cbbc | 2010-04-07 18:52:29 +0900 | [diff] [blame] | 954 | /** |
| 955 | * bd_may_claim - test whether a block device can be claimed |
| 956 | * @bdev: block device of interest |
| 957 | * @whole: whole block device containing @bdev, may equal @bdev |
| 958 | * @holder: holder trying to claim @bdev |
| 959 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 960 | * Test whether @bdev can be claimed by @holder. |
Tejun Heo | 1a3cbbc | 2010-04-07 18:52:29 +0900 | [diff] [blame] | 961 | * |
| 962 | * CONTEXT: |
| 963 | * spin_lock(&bdev_lock). |
| 964 | * |
| 965 | * RETURNS: |
| 966 | * %true if @bdev can be claimed, %false otherwise. |
| 967 | */ |
| 968 | static bool bd_may_claim(struct block_device *bdev, struct block_device *whole, |
| 969 | void *holder) |
| 970 | { |
| 971 | if (bdev->bd_holder == holder) |
| 972 | return true; /* already a holder */ |
| 973 | else if (bdev->bd_holder != NULL) |
| 974 | return false; /* held by someone else */ |
NeilBrown | bcc7f5b | 2016-12-12 08:21:51 -0700 | [diff] [blame] | 975 | else if (whole == bdev) |
Tejun Heo | 1a3cbbc | 2010-04-07 18:52:29 +0900 | [diff] [blame] | 976 | return true; /* is a whole device which isn't held */ |
| 977 | |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 978 | else if (whole->bd_holder == bd_may_claim) |
Tejun Heo | 1a3cbbc | 2010-04-07 18:52:29 +0900 | [diff] [blame] | 979 | return true; /* is a partition of a device that is being partitioned */ |
| 980 | else if (whole->bd_holder != NULL) |
| 981 | return false; /* is a partition of a held device */ |
| 982 | else |
| 983 | return true; /* is a partition of an un-held device */ |
| 984 | } |
| 985 | |
| 986 | /** |
Christoph Hellwig | 58e46ed | 2020-07-16 16:33:08 +0200 | [diff] [blame] | 987 | * bd_prepare_to_claim - claim a block device |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 988 | * @bdev: block device of interest |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 989 | * @holder: holder trying to claim @bdev |
| 990 | * |
Christoph Hellwig | 58e46ed | 2020-07-16 16:33:08 +0200 | [diff] [blame] | 991 | * Claim @bdev. This function fails if @bdev is already claimed by another |
| 992 | * holder and waits if another claiming is in progress. return, the caller |
| 993 | * has ownership of bd_claiming and bd_holder[s]. |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 994 | * |
| 995 | * RETURNS: |
| 996 | * 0 if @bdev can be claimed, -EBUSY otherwise. |
| 997 | */ |
Christoph Hellwig | 37c3fc9 | 2020-11-25 21:20:08 +0100 | [diff] [blame] | 998 | int bd_prepare_to_claim(struct block_device *bdev, void *holder) |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 999 | { |
Christoph Hellwig | 37c3fc9 | 2020-11-25 21:20:08 +0100 | [diff] [blame] | 1000 | struct block_device *whole = bdev_whole(bdev); |
| 1001 | |
| 1002 | if (WARN_ON_ONCE(!holder)) |
| 1003 | return -EINVAL; |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 1004 | retry: |
Christoph Hellwig | 58e46ed | 2020-07-16 16:33:08 +0200 | [diff] [blame] | 1005 | spin_lock(&bdev_lock); |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 1006 | /* if someone else claimed, fail */ |
Christoph Hellwig | 58e46ed | 2020-07-16 16:33:08 +0200 | [diff] [blame] | 1007 | if (!bd_may_claim(bdev, whole, holder)) { |
| 1008 | spin_unlock(&bdev_lock); |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 1009 | return -EBUSY; |
Christoph Hellwig | 58e46ed | 2020-07-16 16:33:08 +0200 | [diff] [blame] | 1010 | } |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 1011 | |
Tejun Heo | e75aa85 | 2010-08-04 17:59:39 +0200 | [diff] [blame] | 1012 | /* if claiming is already in progress, wait for it to finish */ |
| 1013 | if (whole->bd_claiming) { |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 1014 | wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0); |
| 1015 | DEFINE_WAIT(wait); |
| 1016 | |
| 1017 | prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE); |
| 1018 | spin_unlock(&bdev_lock); |
| 1019 | schedule(); |
| 1020 | finish_wait(wq, &wait); |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 1021 | goto retry; |
| 1022 | } |
| 1023 | |
| 1024 | /* yay, all mine */ |
Christoph Hellwig | 58e46ed | 2020-07-16 16:33:08 +0200 | [diff] [blame] | 1025 | whole->bd_claiming = holder; |
| 1026 | spin_unlock(&bdev_lock); |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 1027 | return 0; |
| 1028 | } |
Christoph Hellwig | ecbe6bc | 2020-07-16 16:33:09 +0200 | [diff] [blame] | 1029 | EXPORT_SYMBOL_GPL(bd_prepare_to_claim); /* only for the loop driver */ |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 1030 | |
Jan Kara | 89e524c0 | 2019-07-30 13:10:14 +0200 | [diff] [blame] | 1031 | static void bd_clear_claiming(struct block_device *whole, void *holder) |
| 1032 | { |
| 1033 | lockdep_assert_held(&bdev_lock); |
| 1034 | /* tell others that we're done */ |
| 1035 | BUG_ON(whole->bd_claiming != holder); |
| 1036 | whole->bd_claiming = NULL; |
| 1037 | wake_up_bit(&whole->bd_claiming, 0); |
| 1038 | } |
| 1039 | |
| 1040 | /** |
| 1041 | * bd_finish_claiming - finish claiming of a block device |
| 1042 | * @bdev: block device of interest |
Jan Kara | 89e524c0 | 2019-07-30 13:10:14 +0200 | [diff] [blame] | 1043 | * @holder: holder that has claimed @bdev |
| 1044 | * |
| 1045 | * Finish exclusive open of a block device. Mark the device as exlusively |
| 1046 | * open by the holder and wake up all waiters for exclusive open to finish. |
| 1047 | */ |
Christoph Hellwig | 37c3fc9 | 2020-11-25 21:20:08 +0100 | [diff] [blame] | 1048 | static void bd_finish_claiming(struct block_device *bdev, void *holder) |
Jan Kara | 89e524c0 | 2019-07-30 13:10:14 +0200 | [diff] [blame] | 1049 | { |
Christoph Hellwig | 37c3fc9 | 2020-11-25 21:20:08 +0100 | [diff] [blame] | 1050 | struct block_device *whole = bdev_whole(bdev); |
| 1051 | |
Jan Kara | 89e524c0 | 2019-07-30 13:10:14 +0200 | [diff] [blame] | 1052 | spin_lock(&bdev_lock); |
| 1053 | BUG_ON(!bd_may_claim(bdev, whole, holder)); |
| 1054 | /* |
| 1055 | * Note that for a whole device bd_holders will be incremented twice, |
| 1056 | * and bd_holder will be set to bd_may_claim before being set to holder |
| 1057 | */ |
| 1058 | whole->bd_holders++; |
| 1059 | whole->bd_holder = bd_may_claim; |
| 1060 | bdev->bd_holders++; |
| 1061 | bdev->bd_holder = holder; |
| 1062 | bd_clear_claiming(whole, holder); |
| 1063 | spin_unlock(&bdev_lock); |
| 1064 | } |
Jan Kara | 89e524c0 | 2019-07-30 13:10:14 +0200 | [diff] [blame] | 1065 | |
| 1066 | /** |
| 1067 | * bd_abort_claiming - abort claiming of a block device |
| 1068 | * @bdev: block device of interest |
Jan Kara | 89e524c0 | 2019-07-30 13:10:14 +0200 | [diff] [blame] | 1069 | * @holder: holder that has claimed @bdev |
| 1070 | * |
| 1071 | * Abort claiming of a block device when the exclusive open failed. This can be |
| 1072 | * also used when exclusive open is not actually desired and we just needed |
| 1073 | * to block other exclusive openers for a while. |
| 1074 | */ |
Christoph Hellwig | 37c3fc9 | 2020-11-25 21:20:08 +0100 | [diff] [blame] | 1075 | void bd_abort_claiming(struct block_device *bdev, void *holder) |
Jan Kara | 89e524c0 | 2019-07-30 13:10:14 +0200 | [diff] [blame] | 1076 | { |
| 1077 | spin_lock(&bdev_lock); |
Christoph Hellwig | 37c3fc9 | 2020-11-25 21:20:08 +0100 | [diff] [blame] | 1078 | bd_clear_claiming(bdev_whole(bdev), holder); |
Jan Kara | 89e524c0 | 2019-07-30 13:10:14 +0200 | [diff] [blame] | 1079 | spin_unlock(&bdev_lock); |
| 1080 | } |
| 1081 | EXPORT_SYMBOL(bd_abort_claiming); |
Tejun Heo | 6b4517a | 2010-04-07 18:53:59 +0900 | [diff] [blame] | 1082 | |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 1083 | #ifdef CONFIG_SYSFS |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 1084 | struct bd_holder_disk { |
| 1085 | struct list_head list; |
| 1086 | struct gendisk *disk; |
| 1087 | int refcnt; |
| 1088 | }; |
| 1089 | |
| 1090 | static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev, |
| 1091 | struct gendisk *disk) |
| 1092 | { |
| 1093 | struct bd_holder_disk *holder; |
| 1094 | |
| 1095 | list_for_each_entry(holder, &bdev->bd_holder_disks, list) |
| 1096 | if (holder->disk == disk) |
| 1097 | return holder; |
| 1098 | return NULL; |
| 1099 | } |
| 1100 | |
Andrew Morton | 4d7dd8fd | 2006-09-29 01:58:56 -0700 | [diff] [blame] | 1101 | static int add_symlink(struct kobject *from, struct kobject *to) |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 1102 | { |
Andrew Morton | 4d7dd8fd | 2006-09-29 01:58:56 -0700 | [diff] [blame] | 1103 | return sysfs_create_link(from, to, kobject_name(to)); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | static void del_symlink(struct kobject *from, struct kobject *to) |
| 1107 | { |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 1108 | sysfs_remove_link(from, kobject_name(to)); |
| 1109 | } |
| 1110 | |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 1111 | /** |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1112 | * bd_link_disk_holder - create symlinks between holding disk and slave bdev |
| 1113 | * @bdev: the claimed slave bdev |
| 1114 | * @disk: the holding disk |
Jun'ichi Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 1115 | * |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 1116 | * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT. |
| 1117 | * |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1118 | * This functions creates the following sysfs symlinks. |
Jun'ichi Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 1119 | * |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1120 | * - from "slaves" directory of the holder @disk to the claimed @bdev |
| 1121 | * - from "holders" directory of the @bdev to the holder @disk |
| 1122 | * |
| 1123 | * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is |
| 1124 | * passed to bd_link_disk_holder(), then: |
| 1125 | * |
| 1126 | * /sys/block/dm-0/slaves/sda --> /sys/block/sda |
| 1127 | * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0 |
| 1128 | * |
| 1129 | * The caller must have claimed @bdev before calling this function and |
| 1130 | * ensure that both @bdev and @disk are valid during the creation and |
| 1131 | * lifetime of these symlinks. |
| 1132 | * |
| 1133 | * CONTEXT: |
| 1134 | * Might sleep. |
| 1135 | * |
| 1136 | * RETURNS: |
| 1137 | * 0 on success, -errno on failure. |
Jun'ichi Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 1138 | */ |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1139 | int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk) |
Jun'ichi Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 1140 | { |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 1141 | struct bd_holder_disk *holder; |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1142 | int ret = 0; |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 1143 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1144 | mutex_lock(&bdev->bd_mutex); |
Jun'ichi Nomura | df6c0cd | 2006-10-30 16:23:56 -0500 | [diff] [blame] | 1145 | |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 1146 | WARN_ON_ONCE(!bdev->bd_holder); |
Johannes Weiner | 4e91672 | 2007-07-15 23:41:25 -0700 | [diff] [blame] | 1147 | |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1148 | /* FIXME: remove the following once add_disk() handles errors */ |
Christoph Hellwig | 1bdd5ae | 2020-11-23 19:00:13 +0100 | [diff] [blame] | 1149 | if (WARN_ON(!disk->slave_dir || !bdev->bd_holder_dir)) |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1150 | goto out_unlock; |
Johannes Weiner | 4e91672 | 2007-07-15 23:41:25 -0700 | [diff] [blame] | 1151 | |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 1152 | holder = bd_find_holder_disk(bdev, disk); |
| 1153 | if (holder) { |
| 1154 | holder->refcnt++; |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1155 | goto out_unlock; |
| 1156 | } |
| 1157 | |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 1158 | holder = kzalloc(sizeof(*holder), GFP_KERNEL); |
| 1159 | if (!holder) { |
| 1160 | ret = -ENOMEM; |
| 1161 | goto out_unlock; |
| 1162 | } |
| 1163 | |
| 1164 | INIT_LIST_HEAD(&holder->list); |
| 1165 | holder->disk = disk; |
| 1166 | holder->refcnt = 1; |
| 1167 | |
Christoph Hellwig | 8d65269 | 2020-11-17 08:18:55 +0100 | [diff] [blame] | 1168 | ret = add_symlink(disk->slave_dir, bdev_kobj(bdev)); |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 1169 | if (ret) |
| 1170 | goto out_free; |
| 1171 | |
Christoph Hellwig | 1bdd5ae | 2020-11-23 19:00:13 +0100 | [diff] [blame] | 1172 | ret = add_symlink(bdev->bd_holder_dir, &disk_to_dev(disk)->kobj); |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 1173 | if (ret) |
| 1174 | goto out_del; |
Tejun Heo | e7407d1 | 2011-02-24 09:56:32 +0100 | [diff] [blame] | 1175 | /* |
| 1176 | * bdev could be deleted beneath us which would implicitly destroy |
| 1177 | * the holder directory. Hold on to it. |
| 1178 | */ |
Christoph Hellwig | 1bdd5ae | 2020-11-23 19:00:13 +0100 | [diff] [blame] | 1179 | kobject_get(bdev->bd_holder_dir); |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 1180 | |
| 1181 | list_add(&holder->list, &bdev->bd_holder_disks); |
| 1182 | goto out_unlock; |
| 1183 | |
| 1184 | out_del: |
Christoph Hellwig | 8d65269 | 2020-11-17 08:18:55 +0100 | [diff] [blame] | 1185 | del_symlink(disk->slave_dir, bdev_kobj(bdev)); |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 1186 | out_free: |
| 1187 | kfree(holder); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1188 | out_unlock: |
Jun'ichi Nomura | b4cf1b7 | 2006-03-27 01:18:00 -0800 | [diff] [blame] | 1189 | mutex_unlock(&bdev->bd_mutex); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1190 | return ret; |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 1191 | } |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1192 | EXPORT_SYMBOL_GPL(bd_link_disk_holder); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 1193 | |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 1194 | /** |
| 1195 | * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder() |
| 1196 | * @bdev: the calimed slave bdev |
| 1197 | * @disk: the holding disk |
| 1198 | * |
| 1199 | * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT. |
| 1200 | * |
| 1201 | * CONTEXT: |
| 1202 | * Might sleep. |
| 1203 | */ |
| 1204 | void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk) |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 1205 | { |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 1206 | struct bd_holder_disk *holder; |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1207 | |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 1208 | mutex_lock(&bdev->bd_mutex); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 1209 | |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 1210 | holder = bd_find_holder_disk(bdev, disk); |
| 1211 | |
| 1212 | if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) { |
Christoph Hellwig | 8d65269 | 2020-11-17 08:18:55 +0100 | [diff] [blame] | 1213 | del_symlink(disk->slave_dir, bdev_kobj(bdev)); |
Christoph Hellwig | 1bdd5ae | 2020-11-23 19:00:13 +0100 | [diff] [blame] | 1214 | del_symlink(bdev->bd_holder_dir, &disk_to_dev(disk)->kobj); |
| 1215 | kobject_put(bdev->bd_holder_dir); |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 1216 | list_del_init(&holder->list); |
| 1217 | kfree(holder); |
| 1218 | } |
| 1219 | |
| 1220 | mutex_unlock(&bdev->bd_mutex); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 1221 | } |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 1222 | EXPORT_SYMBOL_GPL(bd_unlink_disk_holder); |
Jun'ichi Nomura | 641dc63 | 2006-03-27 01:17:57 -0800 | [diff] [blame] | 1223 | #endif |
| 1224 | |
Al Viro | 4385bab | 2013-05-05 22:11:03 -0400 | [diff] [blame] | 1225 | static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part); |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1226 | |
Christoph Hellwig | 142fe8f | 2019-11-14 15:34:35 +0100 | [diff] [blame] | 1227 | int bdev_disk_changed(struct block_device *bdev, bool invalidate) |
Christoph Hellwig | a1548b6 | 2019-11-14 15:34:34 +0100 | [diff] [blame] | 1228 | { |
Christoph Hellwig | 142fe8f | 2019-11-14 15:34:35 +0100 | [diff] [blame] | 1229 | struct gendisk *disk = bdev->bd_disk; |
Christoph Hellwig | a1548b6 | 2019-11-14 15:34:34 +0100 | [diff] [blame] | 1230 | int ret; |
| 1231 | |
Christoph Hellwig | f0b870d | 2019-11-14 15:34:36 +0100 | [diff] [blame] | 1232 | lockdep_assert_held(&bdev->bd_mutex); |
| 1233 | |
Christoph Hellwig | 38430f0 | 2020-09-21 09:19:45 +0200 | [diff] [blame] | 1234 | clear_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state); |
Christoph Hellwig | 6540fbf | 2020-09-01 17:57:41 +0200 | [diff] [blame] | 1235 | |
Christoph Hellwig | a1548b6 | 2019-11-14 15:34:34 +0100 | [diff] [blame] | 1236 | rescan: |
Christoph Hellwig | d46430b | 2020-04-14 09:28:57 +0200 | [diff] [blame] | 1237 | ret = blk_drop_partitions(bdev); |
Christoph Hellwig | a1548b6 | 2019-11-14 15:34:34 +0100 | [diff] [blame] | 1238 | if (ret) |
| 1239 | return ret; |
| 1240 | |
Christoph Hellwig | d981cb5 | 2020-03-18 09:12:06 +0100 | [diff] [blame] | 1241 | /* |
| 1242 | * Historically we only set the capacity to zero for devices that |
| 1243 | * support partitions (independ of actually having partitions created). |
| 1244 | * Doing that is rather inconsistent, but changing it broke legacy |
| 1245 | * udisks polling for legacy ide-cdrom devices. Use the crude check |
| 1246 | * below to get the sane behavior for most device while not breaking |
| 1247 | * userspace for this particular setup. |
| 1248 | */ |
| 1249 | if (invalidate) { |
| 1250 | if (disk_part_scan_enabled(disk) || |
| 1251 | !(disk->flags & GENHD_FL_REMOVABLE)) |
| 1252 | set_capacity(disk, 0); |
| 1253 | } else { |
| 1254 | if (disk->fops->revalidate_disk) |
| 1255 | disk->fops->revalidate_disk(disk); |
| 1256 | } |
Christoph Hellwig | a1548b6 | 2019-11-14 15:34:34 +0100 | [diff] [blame] | 1257 | |
Christoph Hellwig | 142fe8f | 2019-11-14 15:34:35 +0100 | [diff] [blame] | 1258 | if (get_capacity(disk)) { |
| 1259 | ret = blk_add_partitions(disk, bdev); |
| 1260 | if (ret == -EAGAIN) |
| 1261 | goto rescan; |
Eric Biggers | 490547c | 2019-12-02 10:21:34 -0800 | [diff] [blame] | 1262 | } else if (invalidate) { |
Christoph Hellwig | a1548b6 | 2019-11-14 15:34:34 +0100 | [diff] [blame] | 1263 | /* |
| 1264 | * Tell userspace that the media / partition table may have |
| 1265 | * changed. |
| 1266 | */ |
| 1267 | kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE); |
Christoph Hellwig | a1548b6 | 2019-11-14 15:34:34 +0100 | [diff] [blame] | 1268 | } |
| 1269 | |
Christoph Hellwig | a1548b6 | 2019-11-14 15:34:34 +0100 | [diff] [blame] | 1270 | return ret; |
| 1271 | } |
Christoph Hellwig | f0b870d | 2019-11-14 15:34:36 +0100 | [diff] [blame] | 1272 | /* |
| 1273 | * Only exported for for loop and dasd for historic reasons. Don't use in new |
| 1274 | * code! |
| 1275 | */ |
| 1276 | EXPORT_SYMBOL_GPL(bdev_disk_changed); |
Christoph Hellwig | a1548b6 | 2019-11-14 15:34:34 +0100 | [diff] [blame] | 1277 | |
Peter Zijlstra | 6d740cd | 2007-02-20 13:58:18 -0800 | [diff] [blame] | 1278 | /* |
| 1279 | * bd_mutex locking: |
| 1280 | * |
| 1281 | * mutex_lock(part->bd_mutex) |
| 1282 | * mutex_lock_nested(whole->bd_mutex, 1) |
| 1283 | */ |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 1284 | static int __blkdev_get(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | { |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 1286 | struct gendisk *disk = bdev->bd_disk; |
Christoph Hellwig | cb8432d | 2020-11-26 18:47:17 +0100 | [diff] [blame] | 1287 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | if (!bdev->bd_openers) { |
Christoph Hellwig | a954ea8 | 2020-11-23 13:29:55 +0100 | [diff] [blame] | 1290 | if (!bdev_is_partition(bdev)) { |
Tejun Heo | 1196f8b | 2011-04-21 20:54:45 +0200 | [diff] [blame] | 1291 | ret = 0; |
Christoph Hellwig | 5b56b6ed | 2020-11-23 10:19:22 +0100 | [diff] [blame] | 1292 | if (disk->fops->open) |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1293 | ret = disk->fops->open(bdev, mode); |
Tejun Heo | 7e69723 | 2011-05-23 13:26:07 +0200 | [diff] [blame] | 1294 | |
Christoph Hellwig | a782483 | 2020-11-26 18:43:37 +0100 | [diff] [blame] | 1295 | if (!ret) |
Jan Kara | 04906b2 | 2019-01-14 09:48:10 +0100 | [diff] [blame] | 1296 | set_init_blocksize(bdev); |
Tejun Heo | 7e69723 | 2011-05-23 13:26:07 +0200 | [diff] [blame] | 1297 | |
Tejun Heo | 1196f8b | 2011-04-21 20:54:45 +0200 | [diff] [blame] | 1298 | /* |
| 1299 | * If the device is invalidated, rescan partition |
| 1300 | * if open succeeded or failed with -ENOMEDIUM. |
| 1301 | * The latter is necessary to prevent ghost |
| 1302 | * partitions on a removed medium. |
| 1303 | */ |
Christoph Hellwig | 38430f0 | 2020-09-21 09:19:45 +0200 | [diff] [blame] | 1304 | if (test_bit(GD_NEED_PART_SCAN, &disk->state) && |
Jan Kara | 731dc48 | 2019-10-21 10:37:59 +0200 | [diff] [blame] | 1305 | (!ret || ret == -ENOMEDIUM)) |
| 1306 | bdev_disk_changed(bdev, ret == -ENOMEDIUM); |
Dan Williams | 5a023cd | 2015-11-30 10:20:29 -0800 | [diff] [blame] | 1307 | |
Tejun Heo | 1196f8b | 2011-04-21 20:54:45 +0200 | [diff] [blame] | 1308 | if (ret) |
Christoph Hellwig | cb8432d | 2020-11-26 18:47:17 +0100 | [diff] [blame] | 1309 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | } else { |
Christoph Hellwig | 977115c | 2020-11-26 10:41:07 +0100 | [diff] [blame] | 1311 | struct block_device *whole = bdgrab(disk->part0); |
Christoph Hellwig | 5b56b6ed | 2020-11-23 10:19:22 +0100 | [diff] [blame] | 1312 | |
| 1313 | mutex_lock_nested(&whole->bd_mutex, 1); |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 1314 | ret = __blkdev_get(whole, mode); |
Christoph Hellwig | 5b56b6ed | 2020-11-23 10:19:22 +0100 | [diff] [blame] | 1315 | if (ret) { |
| 1316 | mutex_unlock(&whole->bd_mutex); |
| 1317 | bdput(whole); |
Christoph Hellwig | cb8432d | 2020-11-26 18:47:17 +0100 | [diff] [blame] | 1318 | return ret; |
Christoph Hellwig | 5b56b6ed | 2020-11-23 10:19:22 +0100 | [diff] [blame] | 1319 | } |
| 1320 | whole->bd_part_count++; |
| 1321 | mutex_unlock(&whole->bd_mutex); |
| 1322 | |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 1323 | if (!(disk->flags & GENHD_FL_UP) || |
Christoph Hellwig | cb8432d | 2020-11-26 18:47:17 +0100 | [diff] [blame] | 1324 | !bdev_nr_sectors(bdev)) { |
Christoph Hellwig | 5b56b6ed | 2020-11-23 10:19:22 +0100 | [diff] [blame] | 1325 | __blkdev_put(whole, mode, 1); |
Christoph Hellwig | 63d9932 | 2020-11-26 09:22:18 +0100 | [diff] [blame] | 1326 | bdput(whole); |
Christoph Hellwig | cb8432d | 2020-11-26 18:47:17 +0100 | [diff] [blame] | 1327 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | } |
Jan Kara | 04906b2 | 2019-01-14 09:48:10 +0100 | [diff] [blame] | 1329 | set_init_blocksize(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | } |
Jan Kara | 03e2627 | 2017-03-23 01:36:53 +0100 | [diff] [blame] | 1331 | |
| 1332 | if (bdev->bd_bdi == &noop_backing_dev_info) |
| 1333 | bdev->bd_bdi = bdi_get(disk->queue->backing_dev_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | } else { |
Christoph Hellwig | a954ea8 | 2020-11-23 13:29:55 +0100 | [diff] [blame] | 1335 | if (!bdev_is_partition(bdev)) { |
Tejun Heo | 1196f8b | 2011-04-21 20:54:45 +0200 | [diff] [blame] | 1336 | if (bdev->bd_disk->fops->open) |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1337 | ret = bdev->bd_disk->fops->open(bdev, mode); |
Tejun Heo | 1196f8b | 2011-04-21 20:54:45 +0200 | [diff] [blame] | 1338 | /* the same as first opener case, read comment there */ |
Christoph Hellwig | 38430f0 | 2020-09-21 09:19:45 +0200 | [diff] [blame] | 1339 | if (test_bit(GD_NEED_PART_SCAN, &disk->state) && |
Jan Kara | 731dc48 | 2019-10-21 10:37:59 +0200 | [diff] [blame] | 1340 | (!ret || ret == -ENOMEDIUM)) |
| 1341 | bdev_disk_changed(bdev, ret == -ENOMEDIUM); |
Tejun Heo | 1196f8b | 2011-04-21 20:54:45 +0200 | [diff] [blame] | 1342 | if (ret) |
Christoph Hellwig | 5b56b6ed | 2020-11-23 10:19:22 +0100 | [diff] [blame] | 1343 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | } |
| 1345 | } |
| 1346 | bdev->bd_openers++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | } |
| 1349 | |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 1350 | struct block_device *blkdev_get_no_open(dev_t dev) |
| 1351 | { |
| 1352 | struct block_device *bdev; |
| 1353 | struct gendisk *disk; |
| 1354 | |
| 1355 | down_read(&bdev_lookup_sem); |
| 1356 | bdev = bdget(dev); |
| 1357 | if (!bdev) { |
| 1358 | up_read(&bdev_lookup_sem); |
| 1359 | blk_request_module(dev); |
| 1360 | down_read(&bdev_lookup_sem); |
| 1361 | |
| 1362 | bdev = bdget(dev); |
| 1363 | if (!bdev) |
| 1364 | goto unlock; |
| 1365 | } |
| 1366 | |
| 1367 | disk = bdev->bd_disk; |
| 1368 | if (!kobject_get_unless_zero(&disk_to_dev(disk)->kobj)) |
| 1369 | goto bdput; |
| 1370 | if ((disk->flags & (GENHD_FL_UP | GENHD_FL_HIDDEN)) != GENHD_FL_UP) |
| 1371 | goto put_disk; |
| 1372 | if (!try_module_get(bdev->bd_disk->fops->owner)) |
| 1373 | goto put_disk; |
| 1374 | up_read(&bdev_lookup_sem); |
| 1375 | return bdev; |
| 1376 | put_disk: |
| 1377 | put_disk(disk); |
| 1378 | bdput: |
| 1379 | bdput(bdev); |
| 1380 | unlock: |
| 1381 | up_read(&bdev_lookup_sem); |
| 1382 | return NULL; |
| 1383 | } |
| 1384 | |
| 1385 | void blkdev_put_no_open(struct block_device *bdev) |
| 1386 | { |
| 1387 | module_put(bdev->bd_disk->fops->owner); |
| 1388 | put_disk(bdev->bd_disk); |
| 1389 | bdput(bdev); |
| 1390 | } |
| 1391 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1392 | /** |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 1393 | * blkdev_get_by_dev - open a block device by device number |
| 1394 | * @dev: device number of block device to open |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1395 | * @mode: FMODE_* mask |
| 1396 | * @holder: exclusive holder identifier |
| 1397 | * |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 1398 | * Open the block device described by device number @dev. If @mode includes |
| 1399 | * %FMODE_EXCL, the block device is opened with exclusive access. Specifying |
| 1400 | * %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may nest for |
| 1401 | * the same @holder. |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1402 | * |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 1403 | * Use this interface ONLY if you really do not have anything better - i.e. when |
| 1404 | * you are behind a truly sucky interface and all you are given is a device |
| 1405 | * number. Everything else should use blkdev_get_by_path(). |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1406 | * |
| 1407 | * CONTEXT: |
| 1408 | * Might sleep. |
| 1409 | * |
| 1410 | * RETURNS: |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 1411 | * Reference to the block_device on success, ERR_PTR(-errno) on failure. |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1412 | */ |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 1413 | struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | { |
Christoph Hellwig | 5b56b6ed | 2020-11-23 10:19:22 +0100 | [diff] [blame] | 1415 | bool unblock_events = true; |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 1416 | struct block_device *bdev; |
Christoph Hellwig | 5b56b6ed | 2020-11-23 10:19:22 +0100 | [diff] [blame] | 1417 | struct gendisk *disk; |
Christoph Hellwig | 5b56b6ed | 2020-11-23 10:19:22 +0100 | [diff] [blame] | 1418 | int ret; |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1419 | |
Christoph Hellwig | 7918f0f | 2020-11-23 13:44:44 +0100 | [diff] [blame] | 1420 | ret = devcgroup_check_permission(DEVCG_DEV_BLOCK, |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 1421 | MAJOR(dev), MINOR(dev), |
Christoph Hellwig | 7918f0f | 2020-11-23 13:44:44 +0100 | [diff] [blame] | 1422 | ((mode & FMODE_READ) ? DEVCG_ACC_READ : 0) | |
| 1423 | ((mode & FMODE_WRITE) ? DEVCG_ACC_WRITE : 0)); |
Christoph Hellwig | e5c7fb4 | 2020-08-31 20:02:36 +0200 | [diff] [blame] | 1424 | if (ret) |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 1425 | return ERR_PTR(ret); |
| 1426 | |
Christoph Hellwig | 5b56b6ed | 2020-11-23 10:19:22 +0100 | [diff] [blame] | 1427 | /* |
| 1428 | * If we lost a race with 'disk' being deleted, try again. See md.c. |
| 1429 | */ |
| 1430 | retry: |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 1431 | bdev = blkdev_get_no_open(dev); |
| 1432 | if (!bdev) |
| 1433 | return ERR_PTR(-ENXIO); |
| 1434 | disk = bdev->bd_disk; |
Christoph Hellwig | e5c7fb4 | 2020-08-31 20:02:36 +0200 | [diff] [blame] | 1435 | |
Christoph Hellwig | 5b56b6ed | 2020-11-23 10:19:22 +0100 | [diff] [blame] | 1436 | if (mode & FMODE_EXCL) { |
Christoph Hellwig | 37c3fc9 | 2020-11-25 21:20:08 +0100 | [diff] [blame] | 1437 | ret = bd_prepare_to_claim(bdev, holder); |
Christoph Hellwig | 5b56b6ed | 2020-11-23 10:19:22 +0100 | [diff] [blame] | 1438 | if (ret) |
Christoph Hellwig | 37c3fc9 | 2020-11-25 21:20:08 +0100 | [diff] [blame] | 1439 | goto put_blkdev; |
Christoph Hellwig | 5b56b6ed | 2020-11-23 10:19:22 +0100 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | disk_block_events(disk); |
| 1443 | |
| 1444 | mutex_lock(&bdev->bd_mutex); |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 1445 | ret =__blkdev_get(bdev, mode); |
| 1446 | if (ret) |
| 1447 | goto abort_claiming; |
| 1448 | if (mode & FMODE_EXCL) { |
Christoph Hellwig | 37c3fc9 | 2020-11-25 21:20:08 +0100 | [diff] [blame] | 1449 | bd_finish_claiming(bdev, holder); |
Christoph Hellwig | 5b56b6ed | 2020-11-23 10:19:22 +0100 | [diff] [blame] | 1450 | |
| 1451 | /* |
| 1452 | * Block event polling for write claims if requested. Any write |
| 1453 | * holder makes the write_holder state stick until all are |
| 1454 | * released. This is good enough and tracking individual |
| 1455 | * writeable reference is too fragile given the way @mode is |
| 1456 | * used in blkdev_get/put(). |
| 1457 | */ |
| 1458 | if ((mode & FMODE_WRITE) && !bdev->bd_write_holder && |
| 1459 | (disk->flags & GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE)) { |
| 1460 | bdev->bd_write_holder = true; |
| 1461 | unblock_events = false; |
| 1462 | } |
| 1463 | } |
| 1464 | mutex_unlock(&bdev->bd_mutex); |
| 1465 | |
| 1466 | if (unblock_events) |
| 1467 | disk_unblock_events(disk); |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 1468 | return bdev; |
Christoph Hellwig | 5b56b6ed | 2020-11-23 10:19:22 +0100 | [diff] [blame] | 1469 | |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 1470 | abort_claiming: |
| 1471 | if (mode & FMODE_EXCL) |
Christoph Hellwig | 37c3fc9 | 2020-11-25 21:20:08 +0100 | [diff] [blame] | 1472 | bd_abort_claiming(bdev, holder); |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 1473 | mutex_unlock(&bdev->bd_mutex); |
| 1474 | disk_unblock_events(disk); |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 1475 | put_blkdev: |
| 1476 | blkdev_put_no_open(bdev); |
Christoph Hellwig | 5b56b6ed | 2020-11-23 10:19:22 +0100 | [diff] [blame] | 1477 | if (ret == -ERESTARTSYS) |
| 1478 | goto retry; |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 1479 | return ERR_PTR(ret); |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1480 | } |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 1481 | EXPORT_SYMBOL(blkdev_get_by_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1483 | /** |
| 1484 | * blkdev_get_by_path - open a block device by name |
| 1485 | * @path: path to the block device to open |
| 1486 | * @mode: FMODE_* mask |
| 1487 | * @holder: exclusive holder identifier |
| 1488 | * |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 1489 | * Open the block device described by the device file at @path. If @mode |
| 1490 | * includes %FMODE_EXCL, the block device is opened with exclusive access. |
| 1491 | * Specifying %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may |
| 1492 | * nest for the same @holder. |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1493 | * |
| 1494 | * CONTEXT: |
| 1495 | * Might sleep. |
| 1496 | * |
| 1497 | * RETURNS: |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 1498 | * Reference to the block_device on success, ERR_PTR(-errno) on failure. |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1499 | */ |
| 1500 | struct block_device *blkdev_get_by_path(const char *path, fmode_t mode, |
| 1501 | void *holder) |
| 1502 | { |
| 1503 | struct block_device *bdev; |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 1504 | dev_t dev; |
| 1505 | int error; |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1506 | |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 1507 | error = lookup_bdev(path, &dev); |
| 1508 | if (error) |
| 1509 | return ERR_PTR(error); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1510 | |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 1511 | bdev = blkdev_get_by_dev(dev, mode, holder); |
| 1512 | if (!IS_ERR(bdev) && (mode & FMODE_WRITE) && bdev_read_only(bdev)) { |
Chuck Ebbert | e51900f | 2011-02-16 18:11:53 -0500 | [diff] [blame] | 1513 | blkdev_put(bdev, mode); |
| 1514 | return ERR_PTR(-EACCES); |
| 1515 | } |
| 1516 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1517 | return bdev; |
| 1518 | } |
| 1519 | EXPORT_SYMBOL(blkdev_get_by_path); |
| 1520 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | static int blkdev_open(struct inode * inode, struct file * filp) |
| 1522 | { |
| 1523 | struct block_device *bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | |
| 1525 | /* |
| 1526 | * Preserve backwards compatibility and allow large file access |
| 1527 | * even if userspace doesn't ask for it explicitly. Some mkfs |
| 1528 | * binary needs it. We might want to drop this workaround |
| 1529 | * during an unstable branch. |
| 1530 | */ |
| 1531 | filp->f_flags |= O_LARGEFILE; |
| 1532 | |
Jens Axboe | a304f07 | 2020-05-22 09:14:08 -0600 | [diff] [blame] | 1533 | filp->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC; |
Christoph Hellwig | c35fc7a | 2017-08-29 16:13:21 +0200 | [diff] [blame] | 1534 | |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1535 | if (filp->f_flags & O_NDELAY) |
| 1536 | filp->f_mode |= FMODE_NDELAY; |
| 1537 | if (filp->f_flags & O_EXCL) |
| 1538 | filp->f_mode |= FMODE_EXCL; |
| 1539 | if ((filp->f_flags & O_ACCMODE) == 3) |
| 1540 | filp->f_mode |= FMODE_WRITE_IOCTL; |
| 1541 | |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 1542 | bdev = blkdev_get_by_dev(inode->i_rdev, filp->f_mode, filp); |
| 1543 | if (IS_ERR(bdev)) |
| 1544 | return PTR_ERR(bdev); |
Al Viro | 572c489 | 2007-10-08 13:24:05 -0400 | [diff] [blame] | 1545 | filp->f_mapping = bdev->bd_inode->i_mapping; |
Jeff Layton | 5660e13 | 2017-07-06 07:02:25 -0400 | [diff] [blame] | 1546 | filp->f_wb_err = filemap_sample_wb_err(filp->f_mapping); |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 1547 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | } |
| 1549 | |
Al Viro | 4385bab | 2013-05-05 22:11:03 -0400 | [diff] [blame] | 1550 | static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part) |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1551 | { |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1552 | struct gendisk *disk = bdev->bd_disk; |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1553 | struct block_device *victim = NULL; |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1554 | |
Douglas Anderson | b849dd8 | 2020-03-24 14:48:27 -0700 | [diff] [blame] | 1555 | /* |
| 1556 | * Sync early if it looks like we're the last one. If someone else |
| 1557 | * opens the block device between now and the decrement of bd_openers |
| 1558 | * then we did a sync that we didn't need to, but that's not the end |
| 1559 | * of the world and we want to avoid long (could be several minute) |
| 1560 | * syncs while holding the mutex. |
| 1561 | */ |
| 1562 | if (bdev->bd_openers == 1) |
| 1563 | sync_blockdev(bdev); |
| 1564 | |
NeilBrown | 6796bf5 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1565 | mutex_lock_nested(&bdev->bd_mutex, for_part); |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1566 | if (for_part) |
| 1567 | bdev->bd_part_count--; |
| 1568 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1569 | if (!--bdev->bd_openers) { |
Tejun Heo | 6a027ef | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1570 | WARN_ON_ONCE(bdev->bd_holders); |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1571 | sync_blockdev(bdev); |
| 1572 | kill_bdev(bdev); |
Vivek Goyal | dbd3ca5 | 2015-11-09 09:23:40 -0700 | [diff] [blame] | 1573 | bdev_write_inode(bdev); |
Christoph Hellwig | ec5d451 | 2020-11-17 12:01:26 +0100 | [diff] [blame] | 1574 | if (bdev_is_partition(bdev)) |
Christoph Hellwig | a954ea8 | 2020-11-23 13:29:55 +0100 | [diff] [blame] | 1575 | victim = bdev_whole(bdev); |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1576 | } |
Christoph Hellwig | cb8432d | 2020-11-26 18:47:17 +0100 | [diff] [blame] | 1577 | |
| 1578 | if (!bdev_is_partition(bdev) && disk->fops->release) |
| 1579 | disk->fops->release(disk, mode); |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1580 | mutex_unlock(&bdev->bd_mutex); |
Christoph Hellwig | 63d9932 | 2020-11-26 09:22:18 +0100 | [diff] [blame] | 1581 | if (victim) { |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1582 | __blkdev_put(victim, mode, 1); |
Christoph Hellwig | 63d9932 | 2020-11-26 09:22:18 +0100 | [diff] [blame] | 1583 | bdput(victim); |
| 1584 | } |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1585 | } |
| 1586 | |
Al Viro | 4385bab | 2013-05-05 22:11:03 -0400 | [diff] [blame] | 1587 | void blkdev_put(struct block_device *bdev, fmode_t mode) |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1588 | { |
Christoph Hellwig | 5b56b6ed | 2020-11-23 10:19:22 +0100 | [diff] [blame] | 1589 | struct gendisk *disk = bdev->bd_disk; |
| 1590 | |
Tejun Heo | 85ef06d | 2011-07-01 16:17:47 +0200 | [diff] [blame] | 1591 | mutex_lock(&bdev->bd_mutex); |
| 1592 | |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1593 | if (mode & FMODE_EXCL) { |
Christoph Hellwig | a954ea8 | 2020-11-23 13:29:55 +0100 | [diff] [blame] | 1594 | struct block_device *whole = bdev_whole(bdev); |
Tejun Heo | 6a027ef | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1595 | bool bdev_free; |
| 1596 | |
| 1597 | /* |
| 1598 | * Release a claim on the device. The holder fields |
| 1599 | * are protected with bdev_lock. bd_mutex is to |
| 1600 | * synchronize disk_holder unlinking. |
| 1601 | */ |
Tejun Heo | 6a027ef | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1602 | spin_lock(&bdev_lock); |
| 1603 | |
| 1604 | WARN_ON_ONCE(--bdev->bd_holders < 0); |
Christoph Hellwig | a954ea8 | 2020-11-23 13:29:55 +0100 | [diff] [blame] | 1605 | WARN_ON_ONCE(--whole->bd_holders < 0); |
Tejun Heo | 6a027ef | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1606 | |
Tejun Heo | 6a027ef | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1607 | if ((bdev_free = !bdev->bd_holders)) |
| 1608 | bdev->bd_holder = NULL; |
Christoph Hellwig | a954ea8 | 2020-11-23 13:29:55 +0100 | [diff] [blame] | 1609 | if (!whole->bd_holders) |
| 1610 | whole->bd_holder = NULL; |
Tejun Heo | 6a027ef | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1611 | |
| 1612 | spin_unlock(&bdev_lock); |
| 1613 | |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1614 | /* |
| 1615 | * If this was the last claim, remove holder link and |
| 1616 | * unblock evpoll if it was a write holder. |
| 1617 | */ |
Tejun Heo | 85ef06d | 2011-07-01 16:17:47 +0200 | [diff] [blame] | 1618 | if (bdev_free && bdev->bd_write_holder) { |
Christoph Hellwig | 5b56b6ed | 2020-11-23 10:19:22 +0100 | [diff] [blame] | 1619 | disk_unblock_events(disk); |
Tejun Heo | 85ef06d | 2011-07-01 16:17:47 +0200 | [diff] [blame] | 1620 | bdev->bd_write_holder = false; |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1621 | } |
Tejun Heo | 6936217 | 2011-03-09 19:54:27 +0100 | [diff] [blame] | 1622 | } |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1623 | |
Tejun Heo | 85ef06d | 2011-07-01 16:17:47 +0200 | [diff] [blame] | 1624 | /* |
| 1625 | * Trigger event checking and tell drivers to flush MEDIA_CHANGE |
| 1626 | * event. This is to ensure detection of media removal commanded |
| 1627 | * from userland - e.g. eject(1). |
| 1628 | */ |
Christoph Hellwig | 5b56b6ed | 2020-11-23 10:19:22 +0100 | [diff] [blame] | 1629 | disk_flush_events(disk, DISK_EVENT_MEDIA_CHANGE); |
Tejun Heo | 85ef06d | 2011-07-01 16:17:47 +0200 | [diff] [blame] | 1630 | mutex_unlock(&bdev->bd_mutex); |
| 1631 | |
Al Viro | 4385bab | 2013-05-05 22:11:03 -0400 | [diff] [blame] | 1632 | __blkdev_put(bdev, mode, 0); |
Christoph Hellwig | 22ae8ce | 2020-11-26 09:23:26 +0100 | [diff] [blame] | 1633 | blkdev_put_no_open(bdev); |
NeilBrown | 37be412 | 2006-12-08 02:36:16 -0800 | [diff] [blame] | 1634 | } |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1635 | EXPORT_SYMBOL(blkdev_put); |
| 1636 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | static int blkdev_close(struct inode * inode, struct file * filp) |
| 1638 | { |
Dan Williams | 4ebb16c | 2015-10-28 07:48:19 +0900 | [diff] [blame] | 1639 | struct block_device *bdev = I_BDEV(bdev_file_inode(filp)); |
Al Viro | 4385bab | 2013-05-05 22:11:03 -0400 | [diff] [blame] | 1640 | blkdev_put(bdev, filp->f_mode); |
| 1641 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | } |
| 1643 | |
Arnd Bergmann | bb93e3a | 2005-06-23 00:10:15 -0700 | [diff] [blame] | 1644 | static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | { |
Dan Williams | 4ebb16c | 2015-10-28 07:48:19 +0900 | [diff] [blame] | 1646 | struct block_device *bdev = I_BDEV(bdev_file_inode(file)); |
Al Viro | 56b26ad | 2008-09-19 03:17:36 -0400 | [diff] [blame] | 1647 | fmode_t mode = file->f_mode; |
Christoph Hellwig | fd4ce1a | 2008-11-05 14:58:42 +0100 | [diff] [blame] | 1648 | |
| 1649 | /* |
| 1650 | * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have |
| 1651 | * to updated it before every ioctl. |
| 1652 | */ |
Al Viro | 56b26ad | 2008-09-19 03:17:36 -0400 | [diff] [blame] | 1653 | if (file->f_flags & O_NDELAY) |
Christoph Hellwig | fd4ce1a | 2008-11-05 14:58:42 +0100 | [diff] [blame] | 1654 | mode |= FMODE_NDELAY; |
| 1655 | else |
| 1656 | mode &= ~FMODE_NDELAY; |
| 1657 | |
Al Viro | 56b26ad | 2008-09-19 03:17:36 -0400 | [diff] [blame] | 1658 | return blkdev_ioctl(bdev, mode, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | } |
| 1660 | |
Theodore Ts'o | 87d8fe1 | 2009-01-03 09:47:09 -0500 | [diff] [blame] | 1661 | /* |
Christoph Hellwig | eef9938 | 2009-08-20 17:43:41 +0200 | [diff] [blame] | 1662 | * Write data to the block device. Only intended for the block device itself |
| 1663 | * and the raw driver which basically is a fake block device. |
| 1664 | * |
| 1665 | * Does not take i_mutex for the write and thus is not for general purpose |
| 1666 | * use. |
| 1667 | */ |
Al Viro | 1456c0a | 2014-04-03 03:21:50 -0400 | [diff] [blame] | 1668 | ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from) |
Christoph Hellwig | eef9938 | 2009-08-20 17:43:41 +0200 | [diff] [blame] | 1669 | { |
| 1670 | struct file *file = iocb->ki_filp; |
Dan Williams | 4ebb16c | 2015-10-28 07:48:19 +0900 | [diff] [blame] | 1671 | struct inode *bd_inode = bdev_file_inode(file); |
Al Viro | 7ec7b94 | 2015-04-07 11:35:14 -0400 | [diff] [blame] | 1672 | loff_t size = i_size_read(bd_inode); |
Jianpeng Ma | 53362a0 | 2012-08-02 09:50:39 +0200 | [diff] [blame] | 1673 | struct blk_plug plug; |
Christoph Hellwig | eef9938 | 2009-08-20 17:43:41 +0200 | [diff] [blame] | 1674 | ssize_t ret; |
Al Viro | 5f380c7 | 2015-04-07 11:28:12 -0400 | [diff] [blame] | 1675 | |
Al Viro | 7ec7b94 | 2015-04-07 11:35:14 -0400 | [diff] [blame] | 1676 | if (bdev_read_only(I_BDEV(bd_inode))) |
| 1677 | return -EPERM; |
Al Viro | 5f380c7 | 2015-04-07 11:28:12 -0400 | [diff] [blame] | 1678 | |
Christoph Hellwig | bb3247a39 | 2020-09-21 09:19:55 +0200 | [diff] [blame] | 1679 | if (IS_SWAPFILE(bd_inode) && !is_hibernate_resume_dev(bd_inode->i_rdev)) |
Darrick J. Wong | dc617f2 | 2019-08-20 07:55:16 -0700 | [diff] [blame] | 1680 | return -ETXTBSY; |
| 1681 | |
Al Viro | 7ec7b94 | 2015-04-07 11:35:14 -0400 | [diff] [blame] | 1682 | if (!iov_iter_count(from)) |
Al Viro | 5f380c7 | 2015-04-07 11:28:12 -0400 | [diff] [blame] | 1683 | return 0; |
| 1684 | |
Al Viro | 7ec7b94 | 2015-04-07 11:35:14 -0400 | [diff] [blame] | 1685 | if (iocb->ki_pos >= size) |
| 1686 | return -ENOSPC; |
| 1687 | |
Christoph Hellwig | c35fc7a | 2017-08-29 16:13:21 +0200 | [diff] [blame] | 1688 | if ((iocb->ki_flags & (IOCB_NOWAIT | IOCB_DIRECT)) == IOCB_NOWAIT) |
| 1689 | return -EOPNOTSUPP; |
| 1690 | |
Al Viro | 7ec7b94 | 2015-04-07 11:35:14 -0400 | [diff] [blame] | 1691 | iov_iter_truncate(from, size - iocb->ki_pos); |
Christoph Hellwig | eef9938 | 2009-08-20 17:43:41 +0200 | [diff] [blame] | 1692 | |
Jianpeng Ma | 53362a0 | 2012-08-02 09:50:39 +0200 | [diff] [blame] | 1693 | blk_start_plug(&plug); |
Al Viro | 1456c0a | 2014-04-03 03:21:50 -0400 | [diff] [blame] | 1694 | ret = __generic_file_write_iter(iocb, from); |
Christoph Hellwig | e259221 | 2016-04-07 08:52:01 -0700 | [diff] [blame] | 1695 | if (ret > 0) |
| 1696 | ret = generic_write_sync(iocb, ret); |
Jianpeng Ma | 53362a0 | 2012-08-02 09:50:39 +0200 | [diff] [blame] | 1697 | blk_finish_plug(&plug); |
Christoph Hellwig | eef9938 | 2009-08-20 17:43:41 +0200 | [diff] [blame] | 1698 | return ret; |
| 1699 | } |
Al Viro | 1456c0a | 2014-04-03 03:21:50 -0400 | [diff] [blame] | 1700 | EXPORT_SYMBOL_GPL(blkdev_write_iter); |
Christoph Hellwig | eef9938 | 2009-08-20 17:43:41 +0200 | [diff] [blame] | 1701 | |
David Jeffery | b2de525 | 2014-09-29 10:21:10 -0400 | [diff] [blame] | 1702 | ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to) |
Linus Torvalds | 684c9aa | 2012-12-07 16:48:39 -0800 | [diff] [blame] | 1703 | { |
| 1704 | struct file *file = iocb->ki_filp; |
Dan Williams | 4ebb16c | 2015-10-28 07:48:19 +0900 | [diff] [blame] | 1705 | struct inode *bd_inode = bdev_file_inode(file); |
Linus Torvalds | 684c9aa | 2012-12-07 16:48:39 -0800 | [diff] [blame] | 1706 | loff_t size = i_size_read(bd_inode); |
Al Viro | a886038 | 2014-04-02 20:02:21 -0400 | [diff] [blame] | 1707 | loff_t pos = iocb->ki_pos; |
Linus Torvalds | 684c9aa | 2012-12-07 16:48:39 -0800 | [diff] [blame] | 1708 | |
| 1709 | if (pos >= size) |
| 1710 | return 0; |
| 1711 | |
| 1712 | size -= pos; |
Al Viro | a886038 | 2014-04-02 20:02:21 -0400 | [diff] [blame] | 1713 | iov_iter_truncate(to, size); |
| 1714 | return generic_file_read_iter(iocb, to); |
Linus Torvalds | 684c9aa | 2012-12-07 16:48:39 -0800 | [diff] [blame] | 1715 | } |
David Jeffery | b2de525 | 2014-09-29 10:21:10 -0400 | [diff] [blame] | 1716 | EXPORT_SYMBOL_GPL(blkdev_read_iter); |
Linus Torvalds | 684c9aa | 2012-12-07 16:48:39 -0800 | [diff] [blame] | 1717 | |
Christoph Hellwig | eef9938 | 2009-08-20 17:43:41 +0200 | [diff] [blame] | 1718 | /* |
Theodore Ts'o | 87d8fe1 | 2009-01-03 09:47:09 -0500 | [diff] [blame] | 1719 | * Try to release a page associated with block device when the system |
| 1720 | * is under memory pressure. |
| 1721 | */ |
| 1722 | static int blkdev_releasepage(struct page *page, gfp_t wait) |
| 1723 | { |
| 1724 | struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super; |
| 1725 | |
| 1726 | if (super && super->s_op->bdev_try_to_free_page) |
| 1727 | return super->s_op->bdev_try_to_free_page(super, page, wait); |
| 1728 | |
| 1729 | return try_to_free_buffers(page); |
| 1730 | } |
| 1731 | |
Ross Zwisler | 7f6d5b5 | 2016-02-26 15:19:55 -0800 | [diff] [blame] | 1732 | static int blkdev_writepages(struct address_space *mapping, |
| 1733 | struct writeback_control *wbc) |
| 1734 | { |
Ross Zwisler | 7f6d5b5 | 2016-02-26 15:19:55 -0800 | [diff] [blame] | 1735 | return generic_writepages(mapping, wbc); |
| 1736 | } |
| 1737 | |
Adrian Bunk | 4c54ac6 | 2008-02-18 13:48:31 +0100 | [diff] [blame] | 1738 | static const struct address_space_operations def_blk_aops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | .readpage = blkdev_readpage, |
Matthew Wilcox (Oracle) | d438834 | 2020-06-01 21:47:02 -0700 | [diff] [blame] | 1740 | .readahead = blkdev_readahead, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | .writepage = blkdev_writepage, |
Nick Piggin | 6272b5a | 2007-10-16 01:25:04 -0700 | [diff] [blame] | 1742 | .write_begin = blkdev_write_begin, |
| 1743 | .write_end = blkdev_write_end, |
Ross Zwisler | 7f6d5b5 | 2016-02-26 15:19:55 -0800 | [diff] [blame] | 1744 | .writepages = blkdev_writepages, |
Theodore Ts'o | 87d8fe1 | 2009-01-03 09:47:09 -0500 | [diff] [blame] | 1745 | .releasepage = blkdev_releasepage, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | .direct_IO = blkdev_direct_IO, |
Jan Kara | 88dbcbb | 2018-12-28 00:39:16 -0800 | [diff] [blame] | 1747 | .migratepage = buffer_migrate_page_norefs, |
Mel Gorman | b459722 | 2013-07-03 15:02:05 -0700 | [diff] [blame] | 1748 | .is_dirty_writeback = buffer_check_dirty_writeback, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1749 | }; |
| 1750 | |
Darrick J. Wong | 25f4c41 | 2016-10-11 13:51:11 -0700 | [diff] [blame] | 1751 | #define BLKDEV_FALLOC_FL_SUPPORTED \ |
| 1752 | (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \ |
| 1753 | FALLOC_FL_ZERO_RANGE | FALLOC_FL_NO_HIDE_STALE) |
| 1754 | |
| 1755 | static long blkdev_fallocate(struct file *file, int mode, loff_t start, |
| 1756 | loff_t len) |
| 1757 | { |
| 1758 | struct block_device *bdev = I_BDEV(bdev_file_inode(file)); |
Darrick J. Wong | 25f4c41 | 2016-10-11 13:51:11 -0700 | [diff] [blame] | 1759 | loff_t end = start + len - 1; |
| 1760 | loff_t isize; |
| 1761 | int error; |
| 1762 | |
| 1763 | /* Fail if we don't recognize the flags. */ |
| 1764 | if (mode & ~BLKDEV_FALLOC_FL_SUPPORTED) |
| 1765 | return -EOPNOTSUPP; |
| 1766 | |
| 1767 | /* Don't go off the end of the device. */ |
| 1768 | isize = i_size_read(bdev->bd_inode); |
| 1769 | if (start >= isize) |
| 1770 | return -EINVAL; |
| 1771 | if (end >= isize) { |
| 1772 | if (mode & FALLOC_FL_KEEP_SIZE) { |
| 1773 | len = isize - start; |
| 1774 | end = start + len - 1; |
| 1775 | } else |
| 1776 | return -EINVAL; |
| 1777 | } |
| 1778 | |
| 1779 | /* |
| 1780 | * Don't allow IO that isn't aligned to logical block size. |
| 1781 | */ |
| 1782 | if ((start | len) & (bdev_logical_block_size(bdev) - 1)) |
| 1783 | return -EINVAL; |
| 1784 | |
| 1785 | /* Invalidate the page cache, including dirty pages. */ |
Jan Kara | 384d87e | 2020-09-04 10:58:52 +0200 | [diff] [blame] | 1786 | error = truncate_bdev_range(bdev, file->f_mode, start, end); |
| 1787 | if (error) |
| 1788 | return error; |
Darrick J. Wong | 25f4c41 | 2016-10-11 13:51:11 -0700 | [diff] [blame] | 1789 | |
| 1790 | switch (mode) { |
| 1791 | case FALLOC_FL_ZERO_RANGE: |
| 1792 | case FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE: |
| 1793 | error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9, |
Christoph Hellwig | ee472d8 | 2017-04-05 19:21:08 +0200 | [diff] [blame] | 1794 | GFP_KERNEL, BLKDEV_ZERO_NOUNMAP); |
Darrick J. Wong | 25f4c41 | 2016-10-11 13:51:11 -0700 | [diff] [blame] | 1795 | break; |
| 1796 | case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE: |
Christoph Hellwig | 3404512 | 2017-04-05 19:21:11 +0200 | [diff] [blame] | 1797 | error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9, |
| 1798 | GFP_KERNEL, BLKDEV_ZERO_NOFALLBACK); |
Darrick J. Wong | 25f4c41 | 2016-10-11 13:51:11 -0700 | [diff] [blame] | 1799 | break; |
| 1800 | case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE | FALLOC_FL_NO_HIDE_STALE: |
Darrick J. Wong | 25f4c41 | 2016-10-11 13:51:11 -0700 | [diff] [blame] | 1801 | error = blkdev_issue_discard(bdev, start >> 9, len >> 9, |
| 1802 | GFP_KERNEL, 0); |
| 1803 | break; |
| 1804 | default: |
| 1805 | return -EOPNOTSUPP; |
| 1806 | } |
| 1807 | if (error) |
| 1808 | return error; |
| 1809 | |
| 1810 | /* |
| 1811 | * Invalidate again; if someone wandered in and dirtied a page, |
| 1812 | * the caller will be given -EBUSY. The third argument is |
| 1813 | * inclusive, so the rounding here is safe. |
| 1814 | */ |
Jan Kara | 384d87e | 2020-09-04 10:58:52 +0200 | [diff] [blame] | 1815 | return invalidate_inode_pages2_range(bdev->bd_inode->i_mapping, |
Darrick J. Wong | 25f4c41 | 2016-10-11 13:51:11 -0700 | [diff] [blame] | 1816 | start >> PAGE_SHIFT, |
| 1817 | end >> PAGE_SHIFT); |
| 1818 | } |
| 1819 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 1820 | const struct file_operations def_blk_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | .open = blkdev_open, |
| 1822 | .release = blkdev_close, |
| 1823 | .llseek = block_llseek, |
Al Viro | a886038 | 2014-04-02 20:02:21 -0400 | [diff] [blame] | 1824 | .read_iter = blkdev_read_iter, |
Al Viro | 1456c0a | 2014-04-03 03:21:50 -0400 | [diff] [blame] | 1825 | .write_iter = blkdev_write_iter, |
Christoph Hellwig | eae83ce | 2018-11-30 08:31:52 -0700 | [diff] [blame] | 1826 | .iopoll = blkdev_iopoll, |
Dan Williams | acc93d3 | 2016-05-07 11:40:28 -0700 | [diff] [blame] | 1827 | .mmap = generic_file_mmap, |
Andrew Morton | b1dd3b2 | 2010-04-06 14:35:00 -0700 | [diff] [blame] | 1828 | .fsync = blkdev_fsync, |
Arnd Bergmann | bb93e3a | 2005-06-23 00:10:15 -0700 | [diff] [blame] | 1829 | .unlocked_ioctl = block_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | #ifdef CONFIG_COMPAT |
| 1831 | .compat_ioctl = compat_blkdev_ioctl, |
| 1832 | #endif |
Linus Torvalds | 1e8b333 | 2012-11-29 10:49:50 -0800 | [diff] [blame] | 1833 | .splice_read = generic_file_splice_read, |
Al Viro | 8d02076 | 2014-04-05 04:27:08 -0400 | [diff] [blame] | 1834 | .splice_write = iter_file_splice_write, |
Darrick J. Wong | 25f4c41 | 2016-10-11 13:51:11 -0700 | [diff] [blame] | 1835 | .fallocate = blkdev_fallocate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | }; |
| 1837 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | /** |
| 1839 | * lookup_bdev - lookup a struct block_device by name |
Randy Dunlap | 94e2959 | 2009-01-06 14:41:15 -0800 | [diff] [blame] | 1840 | * @pathname: special file representing the block device |
Randy Dunlap | 875b237 | 2020-12-28 19:47:06 -0800 | [diff] [blame] | 1841 | * @dev: return value of the block device's dev_t |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | * |
Randy Dunlap | 57d1b53 | 2008-10-09 10:42:38 +0200 | [diff] [blame] | 1843 | * Get a reference to the blockdevice at @pathname in the current |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | * namespace if possible and return it. Return ERR_PTR(error) |
| 1845 | * otherwise. |
| 1846 | */ |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 1847 | int lookup_bdev(const char *pathname, dev_t *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1849 | struct inode *inode; |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1850 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | int error; |
| 1852 | |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1853 | if (!pathname || !*pathname) |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 1854 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1855 | |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1856 | error = kern_path(pathname, LOOKUP_FOLLOW, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | if (error) |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 1858 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | |
David Howells | bb668734 | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 1860 | inode = d_backing_inode(path.dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | error = -ENOTBLK; |
| 1862 | if (!S_ISBLK(inode->i_mode)) |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 1863 | goto out_path_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | error = -EACCES; |
Eric W. Biederman | a2982cc | 2016-06-09 15:34:02 -0500 | [diff] [blame] | 1865 | if (!may_open_dev(&path)) |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 1866 | goto out_path_put; |
| 1867 | |
| 1868 | *dev = inode->i_rdev; |
| 1869 | error = 0; |
| 1870 | out_path_put: |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 1871 | path_put(&path); |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 1872 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | } |
Al Viro | d5686b4 | 2008-08-01 05:00:11 -0400 | [diff] [blame] | 1874 | EXPORT_SYMBOL(lookup_bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | |
NeilBrown | 93b270f | 2011-02-24 17:25:47 +1100 | [diff] [blame] | 1876 | int __invalidate_device(struct block_device *bdev, bool kill_dirty) |
David Howells | b71e8a4 | 2006-08-29 19:06:11 +0100 | [diff] [blame] | 1877 | { |
| 1878 | struct super_block *sb = get_super(bdev); |
| 1879 | int res = 0; |
| 1880 | |
| 1881 | if (sb) { |
| 1882 | /* |
| 1883 | * no need to lock the super, get_super holds the |
| 1884 | * read mutex so the filesystem cannot go away |
| 1885 | * under us (->put_super runs with the write lock |
| 1886 | * hold). |
| 1887 | */ |
| 1888 | shrink_dcache_sb(sb); |
NeilBrown | 93b270f | 2011-02-24 17:25:47 +1100 | [diff] [blame] | 1889 | res = invalidate_inodes(sb, kill_dirty); |
David Howells | b71e8a4 | 2006-08-29 19:06:11 +0100 | [diff] [blame] | 1890 | drop_super(sb); |
| 1891 | } |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 1892 | invalidate_bdev(bdev); |
David Howells | b71e8a4 | 2006-08-29 19:06:11 +0100 | [diff] [blame] | 1893 | return res; |
| 1894 | } |
| 1895 | EXPORT_SYMBOL(__invalidate_device); |
Jan Kara | 5c0d6b6 | 2012-07-03 16:45:31 +0200 | [diff] [blame] | 1896 | |
| 1897 | void iterate_bdevs(void (*func)(struct block_device *, void *), void *arg) |
| 1898 | { |
| 1899 | struct inode *inode, *old_inode = NULL; |
| 1900 | |
Dave Chinner | 74278da | 2015-03-04 12:37:22 -0500 | [diff] [blame] | 1901 | spin_lock(&blockdev_superblock->s_inode_list_lock); |
Jan Kara | 5c0d6b6 | 2012-07-03 16:45:31 +0200 | [diff] [blame] | 1902 | list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) { |
| 1903 | struct address_space *mapping = inode->i_mapping; |
Rabin Vincent | af30922 | 2016-12-01 09:18:28 +0100 | [diff] [blame] | 1904 | struct block_device *bdev; |
Jan Kara | 5c0d6b6 | 2012-07-03 16:45:31 +0200 | [diff] [blame] | 1905 | |
| 1906 | spin_lock(&inode->i_lock); |
| 1907 | if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) || |
| 1908 | mapping->nrpages == 0) { |
| 1909 | spin_unlock(&inode->i_lock); |
| 1910 | continue; |
| 1911 | } |
| 1912 | __iget(inode); |
| 1913 | spin_unlock(&inode->i_lock); |
Dave Chinner | 74278da | 2015-03-04 12:37:22 -0500 | [diff] [blame] | 1914 | spin_unlock(&blockdev_superblock->s_inode_list_lock); |
Jan Kara | 5c0d6b6 | 2012-07-03 16:45:31 +0200 | [diff] [blame] | 1915 | /* |
| 1916 | * We hold a reference to 'inode' so it couldn't have been |
| 1917 | * removed from s_inodes list while we dropped the |
Dave Chinner | 74278da | 2015-03-04 12:37:22 -0500 | [diff] [blame] | 1918 | * s_inode_list_lock We cannot iput the inode now as we can |
Jan Kara | 5c0d6b6 | 2012-07-03 16:45:31 +0200 | [diff] [blame] | 1919 | * be holding the last reference and we cannot iput it under |
Dave Chinner | 74278da | 2015-03-04 12:37:22 -0500 | [diff] [blame] | 1920 | * s_inode_list_lock. So we keep the reference and iput it |
Jan Kara | 5c0d6b6 | 2012-07-03 16:45:31 +0200 | [diff] [blame] | 1921 | * later. |
| 1922 | */ |
| 1923 | iput(old_inode); |
| 1924 | old_inode = inode; |
Rabin Vincent | af30922 | 2016-12-01 09:18:28 +0100 | [diff] [blame] | 1925 | bdev = I_BDEV(inode); |
Jan Kara | 5c0d6b6 | 2012-07-03 16:45:31 +0200 | [diff] [blame] | 1926 | |
Rabin Vincent | af30922 | 2016-12-01 09:18:28 +0100 | [diff] [blame] | 1927 | mutex_lock(&bdev->bd_mutex); |
| 1928 | if (bdev->bd_openers) |
| 1929 | func(bdev, arg); |
| 1930 | mutex_unlock(&bdev->bd_mutex); |
Jan Kara | 5c0d6b6 | 2012-07-03 16:45:31 +0200 | [diff] [blame] | 1931 | |
Dave Chinner | 74278da | 2015-03-04 12:37:22 -0500 | [diff] [blame] | 1932 | spin_lock(&blockdev_superblock->s_inode_list_lock); |
Jan Kara | 5c0d6b6 | 2012-07-03 16:45:31 +0200 | [diff] [blame] | 1933 | } |
Dave Chinner | 74278da | 2015-03-04 12:37:22 -0500 | [diff] [blame] | 1934 | spin_unlock(&blockdev_superblock->s_inode_list_lock); |
Jan Kara | 5c0d6b6 | 2012-07-03 16:45:31 +0200 | [diff] [blame] | 1935 | iput(old_inode); |
| 1936 | } |