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