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