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