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