]>
Commit | Line | Data |
---|---|---|
1 | // SPDX-License-Identifier: GPL-2.0-only | |
2 | /* | |
3 | * Copyright (C) 1991, 1992 Linus Torvalds | |
4 | * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE | |
5 | * Copyright (C) 2016 - 2020 Christoph Hellwig | |
6 | */ | |
7 | #include <linux/init.h> | |
8 | #include <linux/mm.h> | |
9 | #include <linux/blkdev.h> | |
10 | #include <linux/buffer_head.h> | |
11 | #include <linux/mpage.h> | |
12 | #include <linux/uio.h> | |
13 | #include <linux/namei.h> | |
14 | #include <linux/task_io_accounting_ops.h> | |
15 | #include <linux/falloc.h> | |
16 | #include <linux/suspend.h> | |
17 | #include <linux/fs.h> | |
18 | #include <linux/iomap.h> | |
19 | #include <linux/module.h> | |
20 | #include <linux/io_uring/cmd.h> | |
21 | #include "blk.h" | |
22 | ||
23 | static inline struct inode *bdev_file_inode(struct file *file) | |
24 | { | |
25 | return file->f_mapping->host; | |
26 | } | |
27 | ||
28 | static blk_opf_t dio_bio_write_op(struct kiocb *iocb) | |
29 | { | |
30 | blk_opf_t opf = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE; | |
31 | ||
32 | /* avoid the need for a I/O completion work item */ | |
33 | if (iocb_is_dsync(iocb)) | |
34 | opf |= REQ_FUA; | |
35 | return opf; | |
36 | } | |
37 | ||
38 | static bool blkdev_dio_invalid(struct block_device *bdev, struct kiocb *iocb, | |
39 | struct iov_iter *iter) | |
40 | { | |
41 | return iocb->ki_pos & (bdev_logical_block_size(bdev) - 1) || | |
42 | !bdev_iter_is_aligned(bdev, iter); | |
43 | } | |
44 | ||
45 | #define DIO_INLINE_BIO_VECS 4 | |
46 | ||
47 | static ssize_t __blkdev_direct_IO_simple(struct kiocb *iocb, | |
48 | struct iov_iter *iter, struct block_device *bdev, | |
49 | unsigned int nr_pages) | |
50 | { | |
51 | struct bio_vec inline_vecs[DIO_INLINE_BIO_VECS], *vecs; | |
52 | loff_t pos = iocb->ki_pos; | |
53 | bool should_dirty = false; | |
54 | struct bio bio; | |
55 | ssize_t ret; | |
56 | ||
57 | WARN_ON_ONCE(iocb->ki_flags & IOCB_HAS_METADATA); | |
58 | if (nr_pages <= DIO_INLINE_BIO_VECS) | |
59 | vecs = inline_vecs; | |
60 | else { | |
61 | vecs = kmalloc_array(nr_pages, sizeof(struct bio_vec), | |
62 | GFP_KERNEL); | |
63 | if (!vecs) | |
64 | return -ENOMEM; | |
65 | } | |
66 | ||
67 | if (iov_iter_rw(iter) == READ) { | |
68 | bio_init(&bio, bdev, vecs, nr_pages, REQ_OP_READ); | |
69 | if (user_backed_iter(iter)) | |
70 | should_dirty = true; | |
71 | } else { | |
72 | bio_init(&bio, bdev, vecs, nr_pages, dio_bio_write_op(iocb)); | |
73 | } | |
74 | bio.bi_iter.bi_sector = pos >> SECTOR_SHIFT; | |
75 | bio.bi_write_hint = file_inode(iocb->ki_filp)->i_write_hint; | |
76 | bio.bi_write_stream = iocb->ki_write_stream; | |
77 | bio.bi_ioprio = iocb->ki_ioprio; | |
78 | if (iocb->ki_flags & IOCB_ATOMIC) | |
79 | bio.bi_opf |= REQ_ATOMIC; | |
80 | ||
81 | ret = bio_iov_iter_get_pages(&bio, iter); | |
82 | if (unlikely(ret)) | |
83 | goto out; | |
84 | ret = bio.bi_iter.bi_size; | |
85 | ||
86 | if (iov_iter_rw(iter) == WRITE) | |
87 | task_io_account_write(ret); | |
88 | ||
89 | if (iocb->ki_flags & IOCB_NOWAIT) | |
90 | bio.bi_opf |= REQ_NOWAIT; | |
91 | ||
92 | submit_bio_wait(&bio); | |
93 | ||
94 | bio_release_pages(&bio, should_dirty); | |
95 | if (unlikely(bio.bi_status)) | |
96 | ret = blk_status_to_errno(bio.bi_status); | |
97 | ||
98 | out: | |
99 | if (vecs != inline_vecs) | |
100 | kfree(vecs); | |
101 | ||
102 | bio_uninit(&bio); | |
103 | ||
104 | return ret; | |
105 | } | |
106 | ||
107 | enum { | |
108 | DIO_SHOULD_DIRTY = 1, | |
109 | DIO_IS_SYNC = 2, | |
110 | }; | |
111 | ||
112 | struct blkdev_dio { | |
113 | union { | |
114 | struct kiocb *iocb; | |
115 | struct task_struct *waiter; | |
116 | }; | |
117 | size_t size; | |
118 | atomic_t ref; | |
119 | unsigned int flags; | |
120 | struct bio bio ____cacheline_aligned_in_smp; | |
121 | }; | |
122 | ||
123 | static struct bio_set blkdev_dio_pool; | |
124 | ||
125 | static void blkdev_bio_end_io(struct bio *bio) | |
126 | { | |
127 | struct blkdev_dio *dio = bio->bi_private; | |
128 | bool should_dirty = dio->flags & DIO_SHOULD_DIRTY; | |
129 | bool is_sync = dio->flags & DIO_IS_SYNC; | |
130 | ||
131 | if (bio->bi_status && !dio->bio.bi_status) | |
132 | dio->bio.bi_status = bio->bi_status; | |
133 | ||
134 | if (!is_sync && (dio->iocb->ki_flags & IOCB_HAS_METADATA)) | |
135 | bio_integrity_unmap_user(bio); | |
136 | ||
137 | if (atomic_dec_and_test(&dio->ref)) { | |
138 | if (!is_sync) { | |
139 | struct kiocb *iocb = dio->iocb; | |
140 | ssize_t ret; | |
141 | ||
142 | WRITE_ONCE(iocb->private, NULL); | |
143 | ||
144 | if (likely(!dio->bio.bi_status)) { | |
145 | ret = dio->size; | |
146 | iocb->ki_pos += ret; | |
147 | } else { | |
148 | ret = blk_status_to_errno(dio->bio.bi_status); | |
149 | } | |
150 | ||
151 | dio->iocb->ki_complete(iocb, ret); | |
152 | bio_put(&dio->bio); | |
153 | } else { | |
154 | struct task_struct *waiter = dio->waiter; | |
155 | ||
156 | WRITE_ONCE(dio->waiter, NULL); | |
157 | blk_wake_io_task(waiter); | |
158 | } | |
159 | } | |
160 | ||
161 | if (should_dirty) { | |
162 | bio_check_pages_dirty(bio); | |
163 | } else { | |
164 | bio_release_pages(bio, false); | |
165 | bio_put(bio); | |
166 | } | |
167 | } | |
168 | ||
169 | static ssize_t __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter, | |
170 | struct block_device *bdev, unsigned int nr_pages) | |
171 | { | |
172 | struct blk_plug plug; | |
173 | struct blkdev_dio *dio; | |
174 | struct bio *bio; | |
175 | bool is_read = (iov_iter_rw(iter) == READ), is_sync; | |
176 | blk_opf_t opf = is_read ? REQ_OP_READ : dio_bio_write_op(iocb); | |
177 | loff_t pos = iocb->ki_pos; | |
178 | int ret = 0; | |
179 | ||
180 | if (iocb->ki_flags & IOCB_ALLOC_CACHE) | |
181 | opf |= REQ_ALLOC_CACHE; | |
182 | bio = bio_alloc_bioset(bdev, nr_pages, opf, GFP_KERNEL, | |
183 | &blkdev_dio_pool); | |
184 | dio = container_of(bio, struct blkdev_dio, bio); | |
185 | atomic_set(&dio->ref, 1); | |
186 | /* | |
187 | * Grab an extra reference to ensure the dio structure which is embedded | |
188 | * into the first bio stays around. | |
189 | */ | |
190 | bio_get(bio); | |
191 | ||
192 | is_sync = is_sync_kiocb(iocb); | |
193 | if (is_sync) { | |
194 | dio->flags = DIO_IS_SYNC; | |
195 | dio->waiter = current; | |
196 | } else { | |
197 | dio->flags = 0; | |
198 | dio->iocb = iocb; | |
199 | } | |
200 | ||
201 | dio->size = 0; | |
202 | if (is_read && user_backed_iter(iter)) | |
203 | dio->flags |= DIO_SHOULD_DIRTY; | |
204 | ||
205 | blk_start_plug(&plug); | |
206 | ||
207 | for (;;) { | |
208 | bio->bi_iter.bi_sector = pos >> SECTOR_SHIFT; | |
209 | bio->bi_write_hint = file_inode(iocb->ki_filp)->i_write_hint; | |
210 | bio->bi_write_stream = iocb->ki_write_stream; | |
211 | bio->bi_private = dio; | |
212 | bio->bi_end_io = blkdev_bio_end_io; | |
213 | bio->bi_ioprio = iocb->ki_ioprio; | |
214 | ||
215 | ret = bio_iov_iter_get_pages(bio, iter); | |
216 | if (unlikely(ret)) { | |
217 | bio->bi_status = BLK_STS_IOERR; | |
218 | bio_endio(bio); | |
219 | break; | |
220 | } | |
221 | if (iocb->ki_flags & IOCB_NOWAIT) { | |
222 | /* | |
223 | * This is nonblocking IO, and we need to allocate | |
224 | * another bio if we have data left to map. As we | |
225 | * cannot guarantee that one of the sub bios will not | |
226 | * fail getting issued FOR NOWAIT and as error results | |
227 | * are coalesced across all of them, be safe and ask for | |
228 | * a retry of this from blocking context. | |
229 | */ | |
230 | if (unlikely(iov_iter_count(iter))) { | |
231 | ret = -EAGAIN; | |
232 | goto fail; | |
233 | } | |
234 | bio->bi_opf |= REQ_NOWAIT; | |
235 | } | |
236 | if (!is_sync && (iocb->ki_flags & IOCB_HAS_METADATA)) { | |
237 | ret = bio_integrity_map_iter(bio, iocb->private); | |
238 | if (unlikely(ret)) | |
239 | goto fail; | |
240 | } | |
241 | ||
242 | if (is_read) { | |
243 | if (dio->flags & DIO_SHOULD_DIRTY) | |
244 | bio_set_pages_dirty(bio); | |
245 | } else { | |
246 | task_io_account_write(bio->bi_iter.bi_size); | |
247 | } | |
248 | dio->size += bio->bi_iter.bi_size; | |
249 | pos += bio->bi_iter.bi_size; | |
250 | ||
251 | nr_pages = bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS); | |
252 | if (!nr_pages) { | |
253 | submit_bio(bio); | |
254 | break; | |
255 | } | |
256 | atomic_inc(&dio->ref); | |
257 | submit_bio(bio); | |
258 | bio = bio_alloc(bdev, nr_pages, opf, GFP_KERNEL); | |
259 | } | |
260 | ||
261 | blk_finish_plug(&plug); | |
262 | ||
263 | if (!is_sync) | |
264 | return -EIOCBQUEUED; | |
265 | ||
266 | for (;;) { | |
267 | set_current_state(TASK_UNINTERRUPTIBLE); | |
268 | if (!READ_ONCE(dio->waiter)) | |
269 | break; | |
270 | blk_io_schedule(); | |
271 | } | |
272 | __set_current_state(TASK_RUNNING); | |
273 | ||
274 | if (!ret) | |
275 | ret = blk_status_to_errno(dio->bio.bi_status); | |
276 | if (likely(!ret)) | |
277 | ret = dio->size; | |
278 | ||
279 | bio_put(&dio->bio); | |
280 | return ret; | |
281 | fail: | |
282 | bio_release_pages(bio, false); | |
283 | bio_clear_flag(bio, BIO_REFFED); | |
284 | bio_put(bio); | |
285 | blk_finish_plug(&plug); | |
286 | return ret; | |
287 | } | |
288 | ||
289 | static void blkdev_bio_end_io_async(struct bio *bio) | |
290 | { | |
291 | struct blkdev_dio *dio = container_of(bio, struct blkdev_dio, bio); | |
292 | struct kiocb *iocb = dio->iocb; | |
293 | ssize_t ret; | |
294 | ||
295 | WRITE_ONCE(iocb->private, NULL); | |
296 | ||
297 | if (likely(!bio->bi_status)) { | |
298 | ret = dio->size; | |
299 | iocb->ki_pos += ret; | |
300 | } else { | |
301 | ret = blk_status_to_errno(bio->bi_status); | |
302 | } | |
303 | ||
304 | if (iocb->ki_flags & IOCB_HAS_METADATA) | |
305 | bio_integrity_unmap_user(bio); | |
306 | ||
307 | iocb->ki_complete(iocb, ret); | |
308 | ||
309 | if (dio->flags & DIO_SHOULD_DIRTY) { | |
310 | bio_check_pages_dirty(bio); | |
311 | } else { | |
312 | bio_release_pages(bio, false); | |
313 | bio_put(bio); | |
314 | } | |
315 | } | |
316 | ||
317 | static ssize_t __blkdev_direct_IO_async(struct kiocb *iocb, | |
318 | struct iov_iter *iter, | |
319 | struct block_device *bdev, | |
320 | unsigned int nr_pages) | |
321 | { | |
322 | bool is_read = iov_iter_rw(iter) == READ; | |
323 | blk_opf_t opf = is_read ? REQ_OP_READ : dio_bio_write_op(iocb); | |
324 | struct blkdev_dio *dio; | |
325 | struct bio *bio; | |
326 | loff_t pos = iocb->ki_pos; | |
327 | int ret = 0; | |
328 | ||
329 | if (iocb->ki_flags & IOCB_ALLOC_CACHE) | |
330 | opf |= REQ_ALLOC_CACHE; | |
331 | bio = bio_alloc_bioset(bdev, nr_pages, opf, GFP_KERNEL, | |
332 | &blkdev_dio_pool); | |
333 | dio = container_of(bio, struct blkdev_dio, bio); | |
334 | dio->flags = 0; | |
335 | dio->iocb = iocb; | |
336 | bio->bi_iter.bi_sector = pos >> SECTOR_SHIFT; | |
337 | bio->bi_write_hint = file_inode(iocb->ki_filp)->i_write_hint; | |
338 | bio->bi_write_stream = iocb->ki_write_stream; | |
339 | bio->bi_end_io = blkdev_bio_end_io_async; | |
340 | bio->bi_ioprio = iocb->ki_ioprio; | |
341 | ||
342 | if (iov_iter_is_bvec(iter)) { | |
343 | /* | |
344 | * Users don't rely on the iterator being in any particular | |
345 | * state for async I/O returning -EIOCBQUEUED, hence we can | |
346 | * avoid expensive iov_iter_advance(). Bypass | |
347 | * bio_iov_iter_get_pages() and set the bvec directly. | |
348 | */ | |
349 | bio_iov_bvec_set(bio, iter); | |
350 | } else { | |
351 | ret = bio_iov_iter_get_pages(bio, iter); | |
352 | if (unlikely(ret)) | |
353 | goto out_bio_put; | |
354 | } | |
355 | dio->size = bio->bi_iter.bi_size; | |
356 | ||
357 | if (is_read) { | |
358 | if (user_backed_iter(iter)) { | |
359 | dio->flags |= DIO_SHOULD_DIRTY; | |
360 | bio_set_pages_dirty(bio); | |
361 | } | |
362 | } else { | |
363 | task_io_account_write(bio->bi_iter.bi_size); | |
364 | } | |
365 | ||
366 | if (iocb->ki_flags & IOCB_HAS_METADATA) { | |
367 | ret = bio_integrity_map_iter(bio, iocb->private); | |
368 | WRITE_ONCE(iocb->private, NULL); | |
369 | if (unlikely(ret)) | |
370 | goto out_bio_put; | |
371 | } | |
372 | ||
373 | if (iocb->ki_flags & IOCB_ATOMIC) | |
374 | bio->bi_opf |= REQ_ATOMIC; | |
375 | ||
376 | if (iocb->ki_flags & IOCB_NOWAIT) | |
377 | bio->bi_opf |= REQ_NOWAIT; | |
378 | ||
379 | if (iocb->ki_flags & IOCB_HIPRI) { | |
380 | bio->bi_opf |= REQ_POLLED; | |
381 | submit_bio(bio); | |
382 | WRITE_ONCE(iocb->private, bio); | |
383 | } else { | |
384 | submit_bio(bio); | |
385 | } | |
386 | return -EIOCBQUEUED; | |
387 | ||
388 | out_bio_put: | |
389 | bio_put(bio); | |
390 | return ret; | |
391 | } | |
392 | ||
393 | static ssize_t blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter) | |
394 | { | |
395 | struct block_device *bdev = I_BDEV(iocb->ki_filp->f_mapping->host); | |
396 | unsigned int nr_pages; | |
397 | ||
398 | if (!iov_iter_count(iter)) | |
399 | return 0; | |
400 | ||
401 | if (blkdev_dio_invalid(bdev, iocb, iter)) | |
402 | return -EINVAL; | |
403 | ||
404 | if (iov_iter_rw(iter) == WRITE) { | |
405 | u16 max_write_streams = bdev_max_write_streams(bdev); | |
406 | ||
407 | if (iocb->ki_write_stream) { | |
408 | if (iocb->ki_write_stream > max_write_streams) | |
409 | return -EINVAL; | |
410 | } else if (max_write_streams) { | |
411 | enum rw_hint write_hint = | |
412 | file_inode(iocb->ki_filp)->i_write_hint; | |
413 | ||
414 | /* | |
415 | * Just use the write hint as write stream for block | |
416 | * device writes. This assumes no file system is | |
417 | * mounted that would use the streams differently. | |
418 | */ | |
419 | if (write_hint <= max_write_streams) | |
420 | iocb->ki_write_stream = write_hint; | |
421 | } | |
422 | } | |
423 | ||
424 | nr_pages = bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS + 1); | |
425 | if (likely(nr_pages <= BIO_MAX_VECS)) { | |
426 | if (is_sync_kiocb(iocb)) | |
427 | return __blkdev_direct_IO_simple(iocb, iter, bdev, | |
428 | nr_pages); | |
429 | return __blkdev_direct_IO_async(iocb, iter, bdev, nr_pages); | |
430 | } else if (iocb->ki_flags & IOCB_ATOMIC) { | |
431 | return -EINVAL; | |
432 | } | |
433 | return __blkdev_direct_IO(iocb, iter, bdev, bio_max_segs(nr_pages)); | |
434 | } | |
435 | ||
436 | static int blkdev_iomap_begin(struct inode *inode, loff_t offset, loff_t length, | |
437 | unsigned int flags, struct iomap *iomap, struct iomap *srcmap) | |
438 | { | |
439 | struct block_device *bdev = I_BDEV(inode); | |
440 | loff_t isize = i_size_read(inode); | |
441 | ||
442 | if (offset >= isize) | |
443 | return -EIO; | |
444 | ||
445 | iomap->bdev = bdev; | |
446 | iomap->offset = ALIGN_DOWN(offset, bdev_logical_block_size(bdev)); | |
447 | iomap->type = IOMAP_MAPPED; | |
448 | iomap->addr = iomap->offset; | |
449 | iomap->length = isize - iomap->offset; | |
450 | iomap->flags |= IOMAP_F_BUFFER_HEAD; /* noop for !CONFIG_BUFFER_HEAD */ | |
451 | return 0; | |
452 | } | |
453 | ||
454 | static const struct iomap_ops blkdev_iomap_ops = { | |
455 | .iomap_begin = blkdev_iomap_begin, | |
456 | }; | |
457 | ||
458 | #ifdef CONFIG_BUFFER_HEAD | |
459 | static int blkdev_get_block(struct inode *inode, sector_t iblock, | |
460 | struct buffer_head *bh, int create) | |
461 | { | |
462 | bh->b_bdev = I_BDEV(inode); | |
463 | bh->b_blocknr = iblock; | |
464 | set_buffer_mapped(bh); | |
465 | return 0; | |
466 | } | |
467 | ||
468 | /* | |
469 | * We cannot call mpage_writepages() as it does not take the buffer lock. | |
470 | * We must use block_write_full_folio() directly which holds the buffer | |
471 | * lock. The buffer lock provides the synchronisation with writeback | |
472 | * that filesystems rely on when they use the blockdev's mapping. | |
473 | */ | |
474 | static int blkdev_writepages(struct address_space *mapping, | |
475 | struct writeback_control *wbc) | |
476 | { | |
477 | struct folio *folio = NULL; | |
478 | struct blk_plug plug; | |
479 | int err; | |
480 | ||
481 | blk_start_plug(&plug); | |
482 | while ((folio = writeback_iter(mapping, wbc, folio, &err))) | |
483 | err = block_write_full_folio(folio, wbc, blkdev_get_block); | |
484 | blk_finish_plug(&plug); | |
485 | ||
486 | return err; | |
487 | } | |
488 | ||
489 | static int blkdev_read_folio(struct file *file, struct folio *folio) | |
490 | { | |
491 | return block_read_full_folio(folio, blkdev_get_block); | |
492 | } | |
493 | ||
494 | static void blkdev_readahead(struct readahead_control *rac) | |
495 | { | |
496 | mpage_readahead(rac, blkdev_get_block); | |
497 | } | |
498 | ||
499 | static int blkdev_write_begin(struct file *file, struct address_space *mapping, | |
500 | loff_t pos, unsigned len, struct folio **foliop, void **fsdata) | |
501 | { | |
502 | return block_write_begin(mapping, pos, len, foliop, blkdev_get_block); | |
503 | } | |
504 | ||
505 | static int blkdev_write_end(struct file *file, struct address_space *mapping, | |
506 | loff_t pos, unsigned len, unsigned copied, struct folio *folio, | |
507 | void *fsdata) | |
508 | { | |
509 | int ret; | |
510 | ret = block_write_end(file, mapping, pos, len, copied, folio, fsdata); | |
511 | ||
512 | folio_unlock(folio); | |
513 | folio_put(folio); | |
514 | ||
515 | return ret; | |
516 | } | |
517 | ||
518 | const struct address_space_operations def_blk_aops = { | |
519 | .dirty_folio = block_dirty_folio, | |
520 | .invalidate_folio = block_invalidate_folio, | |
521 | .read_folio = blkdev_read_folio, | |
522 | .readahead = blkdev_readahead, | |
523 | .writepages = blkdev_writepages, | |
524 | .write_begin = blkdev_write_begin, | |
525 | .write_end = blkdev_write_end, | |
526 | .migrate_folio = buffer_migrate_folio_norefs, | |
527 | .is_dirty_writeback = buffer_check_dirty_writeback, | |
528 | }; | |
529 | #else /* CONFIG_BUFFER_HEAD */ | |
530 | static int blkdev_read_folio(struct file *file, struct folio *folio) | |
531 | { | |
532 | return iomap_read_folio(folio, &blkdev_iomap_ops); | |
533 | } | |
534 | ||
535 | static void blkdev_readahead(struct readahead_control *rac) | |
536 | { | |
537 | iomap_readahead(rac, &blkdev_iomap_ops); | |
538 | } | |
539 | ||
540 | static int blkdev_map_blocks(struct iomap_writepage_ctx *wpc, | |
541 | struct inode *inode, loff_t offset, unsigned int len) | |
542 | { | |
543 | loff_t isize = i_size_read(inode); | |
544 | ||
545 | if (WARN_ON_ONCE(offset >= isize)) | |
546 | return -EIO; | |
547 | if (offset >= wpc->iomap.offset && | |
548 | offset < wpc->iomap.offset + wpc->iomap.length) | |
549 | return 0; | |
550 | return blkdev_iomap_begin(inode, offset, isize - offset, | |
551 | IOMAP_WRITE, &wpc->iomap, NULL); | |
552 | } | |
553 | ||
554 | static const struct iomap_writeback_ops blkdev_writeback_ops = { | |
555 | .map_blocks = blkdev_map_blocks, | |
556 | }; | |
557 | ||
558 | static int blkdev_writepages(struct address_space *mapping, | |
559 | struct writeback_control *wbc) | |
560 | { | |
561 | struct iomap_writepage_ctx wpc = { }; | |
562 | ||
563 | return iomap_writepages(mapping, wbc, &wpc, &blkdev_writeback_ops); | |
564 | } | |
565 | ||
566 | const struct address_space_operations def_blk_aops = { | |
567 | .dirty_folio = filemap_dirty_folio, | |
568 | .release_folio = iomap_release_folio, | |
569 | .invalidate_folio = iomap_invalidate_folio, | |
570 | .read_folio = blkdev_read_folio, | |
571 | .readahead = blkdev_readahead, | |
572 | .writepages = blkdev_writepages, | |
573 | .is_partially_uptodate = iomap_is_partially_uptodate, | |
574 | .error_remove_folio = generic_error_remove_folio, | |
575 | .migrate_folio = filemap_migrate_folio, | |
576 | }; | |
577 | #endif /* CONFIG_BUFFER_HEAD */ | |
578 | ||
579 | /* | |
580 | * for a block special file file_inode(file)->i_size is zero | |
581 | * so we compute the size by hand (just as in block_read/write above) | |
582 | */ | |
583 | static loff_t blkdev_llseek(struct file *file, loff_t offset, int whence) | |
584 | { | |
585 | struct inode *bd_inode = bdev_file_inode(file); | |
586 | loff_t retval; | |
587 | ||
588 | inode_lock(bd_inode); | |
589 | retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode)); | |
590 | inode_unlock(bd_inode); | |
591 | return retval; | |
592 | } | |
593 | ||
594 | static int blkdev_fsync(struct file *filp, loff_t start, loff_t end, | |
595 | int datasync) | |
596 | { | |
597 | struct block_device *bdev = I_BDEV(filp->f_mapping->host); | |
598 | int error; | |
599 | ||
600 | error = file_write_and_wait_range(filp, start, end); | |
601 | if (error) | |
602 | return error; | |
603 | ||
604 | /* | |
605 | * There is no need to serialise calls to blkdev_issue_flush with | |
606 | * i_mutex and doing so causes performance issues with concurrent | |
607 | * O_SYNC writers to a block device. | |
608 | */ | |
609 | error = blkdev_issue_flush(bdev); | |
610 | if (error == -EOPNOTSUPP) | |
611 | error = 0; | |
612 | ||
613 | return error; | |
614 | } | |
615 | ||
616 | /** | |
617 | * file_to_blk_mode - get block open flags from file flags | |
618 | * @file: file whose open flags should be converted | |
619 | * | |
620 | * Look at file open flags and generate corresponding block open flags from | |
621 | * them. The function works both for file just being open (e.g. during ->open | |
622 | * callback) and for file that is already open. This is actually non-trivial | |
623 | * (see comment in the function). | |
624 | */ | |
625 | blk_mode_t file_to_blk_mode(struct file *file) | |
626 | { | |
627 | blk_mode_t mode = 0; | |
628 | ||
629 | if (file->f_mode & FMODE_READ) | |
630 | mode |= BLK_OPEN_READ; | |
631 | if (file->f_mode & FMODE_WRITE) | |
632 | mode |= BLK_OPEN_WRITE; | |
633 | /* | |
634 | * do_dentry_open() clears O_EXCL from f_flags, use file->private_data | |
635 | * to determine whether the open was exclusive for already open files. | |
636 | */ | |
637 | if (file->private_data) | |
638 | mode |= BLK_OPEN_EXCL; | |
639 | else if (file->f_flags & O_EXCL) | |
640 | mode |= BLK_OPEN_EXCL; | |
641 | if (file->f_flags & O_NDELAY) | |
642 | mode |= BLK_OPEN_NDELAY; | |
643 | ||
644 | /* | |
645 | * If all bits in O_ACCMODE set (aka O_RDWR | O_WRONLY), the floppy | |
646 | * driver has historically allowed ioctls as if the file was opened for | |
647 | * writing, but does not allow and actual reads or writes. | |
648 | */ | |
649 | if ((file->f_flags & O_ACCMODE) == (O_RDWR | O_WRONLY)) | |
650 | mode |= BLK_OPEN_WRITE_IOCTL; | |
651 | ||
652 | return mode; | |
653 | } | |
654 | ||
655 | static int blkdev_open(struct inode *inode, struct file *filp) | |
656 | { | |
657 | struct block_device *bdev; | |
658 | blk_mode_t mode; | |
659 | int ret; | |
660 | ||
661 | mode = file_to_blk_mode(filp); | |
662 | /* Use the file as the holder. */ | |
663 | if (mode & BLK_OPEN_EXCL) | |
664 | filp->private_data = filp; | |
665 | ret = bdev_permission(inode->i_rdev, mode, filp->private_data); | |
666 | if (ret) | |
667 | return ret; | |
668 | ||
669 | bdev = blkdev_get_no_open(inode->i_rdev, true); | |
670 | if (!bdev) | |
671 | return -ENXIO; | |
672 | ||
673 | if (bdev_can_atomic_write(bdev)) | |
674 | filp->f_mode |= FMODE_CAN_ATOMIC_WRITE; | |
675 | ||
676 | ret = bdev_open(bdev, mode, filp->private_data, NULL, filp); | |
677 | if (ret) | |
678 | blkdev_put_no_open(bdev); | |
679 | return ret; | |
680 | } | |
681 | ||
682 | static int blkdev_release(struct inode *inode, struct file *filp) | |
683 | { | |
684 | bdev_release(filp); | |
685 | return 0; | |
686 | } | |
687 | ||
688 | static ssize_t | |
689 | blkdev_direct_write(struct kiocb *iocb, struct iov_iter *from) | |
690 | { | |
691 | size_t count = iov_iter_count(from); | |
692 | ssize_t written; | |
693 | ||
694 | written = kiocb_invalidate_pages(iocb, count); | |
695 | if (written) { | |
696 | if (written == -EBUSY) | |
697 | return 0; | |
698 | return written; | |
699 | } | |
700 | ||
701 | written = blkdev_direct_IO(iocb, from); | |
702 | if (written > 0) { | |
703 | kiocb_invalidate_post_direct_write(iocb, count); | |
704 | iocb->ki_pos += written; | |
705 | count -= written; | |
706 | } | |
707 | if (written != -EIOCBQUEUED) | |
708 | iov_iter_revert(from, count - iov_iter_count(from)); | |
709 | return written; | |
710 | } | |
711 | ||
712 | static ssize_t blkdev_buffered_write(struct kiocb *iocb, struct iov_iter *from) | |
713 | { | |
714 | return iomap_file_buffered_write(iocb, from, &blkdev_iomap_ops, NULL); | |
715 | } | |
716 | ||
717 | /* | |
718 | * Write data to the block device. Only intended for the block device itself | |
719 | * and the raw driver which basically is a fake block device. | |
720 | * | |
721 | * Does not take i_mutex for the write and thus is not for general purpose | |
722 | * use. | |
723 | */ | |
724 | static ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from) | |
725 | { | |
726 | struct file *file = iocb->ki_filp; | |
727 | struct inode *bd_inode = bdev_file_inode(file); | |
728 | struct block_device *bdev = I_BDEV(bd_inode); | |
729 | bool atomic = iocb->ki_flags & IOCB_ATOMIC; | |
730 | loff_t size = bdev_nr_bytes(bdev); | |
731 | size_t shorted = 0; | |
732 | ssize_t ret; | |
733 | ||
734 | if (bdev_read_only(bdev)) | |
735 | return -EPERM; | |
736 | ||
737 | if (IS_SWAPFILE(bd_inode) && !is_hibernate_resume_dev(bd_inode->i_rdev)) | |
738 | return -ETXTBSY; | |
739 | ||
740 | if (!iov_iter_count(from)) | |
741 | return 0; | |
742 | ||
743 | if (iocb->ki_pos >= size) | |
744 | return -ENOSPC; | |
745 | ||
746 | if ((iocb->ki_flags & (IOCB_NOWAIT | IOCB_DIRECT)) == IOCB_NOWAIT) | |
747 | return -EOPNOTSUPP; | |
748 | ||
749 | if (atomic) { | |
750 | ret = generic_atomic_write_valid(iocb, from); | |
751 | if (ret) | |
752 | return ret; | |
753 | } | |
754 | ||
755 | size -= iocb->ki_pos; | |
756 | if (iov_iter_count(from) > size) { | |
757 | if (atomic) | |
758 | return -EINVAL; | |
759 | shorted = iov_iter_count(from) - size; | |
760 | iov_iter_truncate(from, size); | |
761 | } | |
762 | ||
763 | ret = file_update_time(file); | |
764 | if (ret) | |
765 | return ret; | |
766 | ||
767 | if (iocb->ki_flags & IOCB_DIRECT) { | |
768 | ret = blkdev_direct_write(iocb, from); | |
769 | if (ret >= 0 && iov_iter_count(from)) | |
770 | ret = direct_write_fallback(iocb, from, ret, | |
771 | blkdev_buffered_write(iocb, from)); | |
772 | } else { | |
773 | /* | |
774 | * Take i_rwsem and invalidate_lock to avoid racing with | |
775 | * set_blocksize changing i_blkbits/folio order and punching | |
776 | * out the pagecache. | |
777 | */ | |
778 | inode_lock_shared(bd_inode); | |
779 | ret = blkdev_buffered_write(iocb, from); | |
780 | inode_unlock_shared(bd_inode); | |
781 | } | |
782 | ||
783 | if (ret > 0) | |
784 | ret = generic_write_sync(iocb, ret); | |
785 | iov_iter_reexpand(from, iov_iter_count(from) + shorted); | |
786 | return ret; | |
787 | } | |
788 | ||
789 | static ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to) | |
790 | { | |
791 | struct inode *bd_inode = bdev_file_inode(iocb->ki_filp); | |
792 | struct block_device *bdev = I_BDEV(iocb->ki_filp->f_mapping->host); | |
793 | loff_t size = bdev_nr_bytes(bdev); | |
794 | loff_t pos = iocb->ki_pos; | |
795 | size_t shorted = 0; | |
796 | ssize_t ret = 0; | |
797 | size_t count; | |
798 | ||
799 | if (unlikely(pos + iov_iter_count(to) > size)) { | |
800 | if (pos >= size) | |
801 | return 0; | |
802 | size -= pos; | |
803 | shorted = iov_iter_count(to) - size; | |
804 | iov_iter_truncate(to, size); | |
805 | } | |
806 | ||
807 | count = iov_iter_count(to); | |
808 | if (!count) | |
809 | goto reexpand; /* skip atime */ | |
810 | ||
811 | if (iocb->ki_flags & IOCB_DIRECT) { | |
812 | ret = kiocb_write_and_wait(iocb, count); | |
813 | if (ret < 0) | |
814 | goto reexpand; | |
815 | file_accessed(iocb->ki_filp); | |
816 | ||
817 | ret = blkdev_direct_IO(iocb, to); | |
818 | if (ret > 0) { | |
819 | iocb->ki_pos += ret; | |
820 | count -= ret; | |
821 | } | |
822 | if (ret != -EIOCBQUEUED) | |
823 | iov_iter_revert(to, count - iov_iter_count(to)); | |
824 | if (ret < 0 || !count) | |
825 | goto reexpand; | |
826 | } | |
827 | ||
828 | /* | |
829 | * Take i_rwsem and invalidate_lock to avoid racing with set_blocksize | |
830 | * changing i_blkbits/folio order and punching out the pagecache. | |
831 | */ | |
832 | inode_lock_shared(bd_inode); | |
833 | ret = filemap_read(iocb, to, ret); | |
834 | inode_unlock_shared(bd_inode); | |
835 | ||
836 | reexpand: | |
837 | if (unlikely(shorted)) | |
838 | iov_iter_reexpand(to, iov_iter_count(to) + shorted); | |
839 | return ret; | |
840 | } | |
841 | ||
842 | #define BLKDEV_FALLOC_FL_SUPPORTED \ | |
843 | (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \ | |
844 | FALLOC_FL_ZERO_RANGE) | |
845 | ||
846 | static long blkdev_fallocate(struct file *file, int mode, loff_t start, | |
847 | loff_t len) | |
848 | { | |
849 | struct inode *inode = bdev_file_inode(file); | |
850 | struct block_device *bdev = I_BDEV(inode); | |
851 | loff_t end = start + len - 1; | |
852 | loff_t isize; | |
853 | int error; | |
854 | ||
855 | /* Fail if we don't recognize the flags. */ | |
856 | if (mode & ~BLKDEV_FALLOC_FL_SUPPORTED) | |
857 | return -EOPNOTSUPP; | |
858 | ||
859 | /* Don't go off the end of the device. */ | |
860 | isize = bdev_nr_bytes(bdev); | |
861 | if (start >= isize) | |
862 | return -EINVAL; | |
863 | if (end >= isize) { | |
864 | if (mode & FALLOC_FL_KEEP_SIZE) { | |
865 | len = isize - start; | |
866 | end = start + len - 1; | |
867 | } else | |
868 | return -EINVAL; | |
869 | } | |
870 | ||
871 | /* | |
872 | * Don't allow IO that isn't aligned to logical block size. | |
873 | */ | |
874 | if ((start | len) & (bdev_logical_block_size(bdev) - 1)) | |
875 | return -EINVAL; | |
876 | ||
877 | inode_lock(inode); | |
878 | filemap_invalidate_lock(inode->i_mapping); | |
879 | ||
880 | /* | |
881 | * Invalidate the page cache, including dirty pages, for valid | |
882 | * de-allocate mode calls to fallocate(). | |
883 | */ | |
884 | switch (mode) { | |
885 | case FALLOC_FL_ZERO_RANGE: | |
886 | case FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE: | |
887 | error = truncate_bdev_range(bdev, file_to_blk_mode(file), start, end); | |
888 | if (error) | |
889 | goto fail; | |
890 | ||
891 | error = blkdev_issue_zeroout(bdev, start >> SECTOR_SHIFT, | |
892 | len >> SECTOR_SHIFT, GFP_KERNEL, | |
893 | BLKDEV_ZERO_NOUNMAP); | |
894 | break; | |
895 | case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE: | |
896 | error = truncate_bdev_range(bdev, file_to_blk_mode(file), start, end); | |
897 | if (error) | |
898 | goto fail; | |
899 | ||
900 | error = blkdev_issue_zeroout(bdev, start >> SECTOR_SHIFT, | |
901 | len >> SECTOR_SHIFT, GFP_KERNEL, | |
902 | BLKDEV_ZERO_NOFALLBACK); | |
903 | break; | |
904 | default: | |
905 | error = -EOPNOTSUPP; | |
906 | } | |
907 | ||
908 | fail: | |
909 | filemap_invalidate_unlock(inode->i_mapping); | |
910 | inode_unlock(inode); | |
911 | return error; | |
912 | } | |
913 | ||
914 | static int blkdev_mmap(struct file *file, struct vm_area_struct *vma) | |
915 | { | |
916 | struct inode *bd_inode = bdev_file_inode(file); | |
917 | ||
918 | if (bdev_read_only(I_BDEV(bd_inode))) | |
919 | return generic_file_readonly_mmap(file, vma); | |
920 | ||
921 | return generic_file_mmap(file, vma); | |
922 | } | |
923 | ||
924 | const struct file_operations def_blk_fops = { | |
925 | .open = blkdev_open, | |
926 | .release = blkdev_release, | |
927 | .llseek = blkdev_llseek, | |
928 | .read_iter = blkdev_read_iter, | |
929 | .write_iter = blkdev_write_iter, | |
930 | .iopoll = iocb_bio_iopoll, | |
931 | .mmap = blkdev_mmap, | |
932 | .fsync = blkdev_fsync, | |
933 | .unlocked_ioctl = blkdev_ioctl, | |
934 | #ifdef CONFIG_COMPAT | |
935 | .compat_ioctl = compat_blkdev_ioctl, | |
936 | #endif | |
937 | .splice_read = filemap_splice_read, | |
938 | .splice_write = iter_file_splice_write, | |
939 | .fallocate = blkdev_fallocate, | |
940 | .uring_cmd = blkdev_uring_cmd, | |
941 | .fop_flags = FOP_BUFFER_RASYNC, | |
942 | }; | |
943 | ||
944 | static __init int blkdev_init(void) | |
945 | { | |
946 | return bioset_init(&blkdev_dio_pool, 4, | |
947 | offsetof(struct blkdev_dio, bio), | |
948 | BIOSET_NEED_BVECS|BIOSET_PERCPU_CACHE); | |
949 | } | |
950 | module_init(blkdev_init); |