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