]>
Commit | Line | Data |
---|---|---|
cd82cca7 CH |
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> | |
f278eb3d | 17 | #include <linux/fs.h> |
487c607d | 18 | #include <linux/iomap.h> |
8581fd40 | 19 | #include <linux/module.h> |
cd82cca7 CH |
20 | #include "blk.h" |
21 | ||
fac7c6d5 | 22 | static inline struct inode *bdev_file_inode(struct file *file) |
cd82cca7 CH |
23 | { |
24 | return file->f_mapping->host; | |
25 | } | |
26 | ||
27 | static int blkdev_get_block(struct inode *inode, sector_t iblock, | |
28 | struct buffer_head *bh, int create) | |
29 | { | |
30 | bh->b_bdev = I_BDEV(inode); | |
31 | bh->b_blocknr = iblock; | |
32 | set_buffer_mapped(bh); | |
33 | return 0; | |
34 | } | |
35 | ||
16458cf3 | 36 | static blk_opf_t dio_bio_write_op(struct kiocb *iocb) |
cd82cca7 | 37 | { |
16458cf3 | 38 | blk_opf_t opf = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE; |
cd82cca7 CH |
39 | |
40 | /* avoid the need for a I/O completion work item */ | |
91b94c5d | 41 | if (iocb_is_dsync(iocb)) |
16458cf3 BVA |
42 | opf |= REQ_FUA; |
43 | return opf; | |
cd82cca7 CH |
44 | } |
45 | ||
37fee2e4 KB |
46 | static bool blkdev_dio_unaligned(struct block_device *bdev, loff_t pos, |
47 | struct iov_iter *iter) | |
48 | { | |
b1a000d3 KB |
49 | return pos & (bdev_logical_block_size(bdev) - 1) || |
50 | !bdev_iter_is_aligned(bdev, iter); | |
37fee2e4 KB |
51 | } |
52 | ||
cd82cca7 CH |
53 | #define DIO_INLINE_BIO_VECS 4 |
54 | ||
cd82cca7 CH |
55 | static ssize_t __blkdev_direct_IO_simple(struct kiocb *iocb, |
56 | struct iov_iter *iter, unsigned int nr_pages) | |
57 | { | |
4e762d86 | 58 | struct block_device *bdev = I_BDEV(iocb->ki_filp->f_mapping->host); |
cd82cca7 CH |
59 | struct bio_vec inline_vecs[DIO_INLINE_BIO_VECS], *vecs; |
60 | loff_t pos = iocb->ki_pos; | |
61 | bool should_dirty = false; | |
62 | struct bio bio; | |
63 | ssize_t ret; | |
cd82cca7 | 64 | |
37fee2e4 | 65 | if (blkdev_dio_unaligned(bdev, pos, iter)) |
cd82cca7 CH |
66 | return -EINVAL; |
67 | ||
68 | if (nr_pages <= DIO_INLINE_BIO_VECS) | |
69 | vecs = inline_vecs; | |
70 | else { | |
71 | vecs = kmalloc_array(nr_pages, sizeof(struct bio_vec), | |
72 | GFP_KERNEL); | |
73 | if (!vecs) | |
74 | return -ENOMEM; | |
75 | } | |
76 | ||
49add496 CH |
77 | if (iov_iter_rw(iter) == READ) { |
78 | bio_init(&bio, bdev, vecs, nr_pages, REQ_OP_READ); | |
fcb14cb1 | 79 | if (user_backed_iter(iter)) |
49add496 CH |
80 | should_dirty = true; |
81 | } else { | |
82 | bio_init(&bio, bdev, vecs, nr_pages, dio_bio_write_op(iocb)); | |
83 | } | |
6549a874 | 84 | bio.bi_iter.bi_sector = pos >> SECTOR_SHIFT; |
cd82cca7 CH |
85 | bio.bi_ioprio = iocb->ki_ioprio; |
86 | ||
87 | ret = bio_iov_iter_get_pages(&bio, iter); | |
88 | if (unlikely(ret)) | |
89 | goto out; | |
90 | ret = bio.bi_iter.bi_size; | |
91 | ||
49add496 | 92 | if (iov_iter_rw(iter) == WRITE) |
cd82cca7 | 93 | task_io_account_write(ret); |
49add496 | 94 | |
cd82cca7 CH |
95 | if (iocb->ki_flags & IOCB_NOWAIT) |
96 | bio.bi_opf |= REQ_NOWAIT; | |
9650b453 ML |
97 | |
98 | submit_bio_wait(&bio); | |
cd82cca7 CH |
99 | |
100 | bio_release_pages(&bio, should_dirty); | |
101 | if (unlikely(bio.bi_status)) | |
102 | ret = blk_status_to_errno(bio.bi_status); | |
103 | ||
104 | out: | |
105 | if (vecs != inline_vecs) | |
106 | kfree(vecs); | |
107 | ||
108 | bio_uninit(&bio); | |
109 | ||
110 | return ret; | |
111 | } | |
112 | ||
09ce8744 | 113 | enum { |
e71aa913 PB |
114 | DIO_SHOULD_DIRTY = 1, |
115 | DIO_IS_SYNC = 2, | |
09ce8744 JA |
116 | }; |
117 | ||
cd82cca7 CH |
118 | struct blkdev_dio { |
119 | union { | |
120 | struct kiocb *iocb; | |
121 | struct task_struct *waiter; | |
122 | }; | |
123 | size_t size; | |
124 | atomic_t ref; | |
09ce8744 | 125 | unsigned int flags; |
6155631a | 126 | struct bio bio ____cacheline_aligned_in_smp; |
cd82cca7 CH |
127 | }; |
128 | ||
129 | static struct bio_set blkdev_dio_pool; | |
130 | ||
cd82cca7 CH |
131 | static void blkdev_bio_end_io(struct bio *bio) |
132 | { | |
133 | struct blkdev_dio *dio = bio->bi_private; | |
09ce8744 | 134 | bool should_dirty = dio->flags & DIO_SHOULD_DIRTY; |
cd82cca7 CH |
135 | |
136 | if (bio->bi_status && !dio->bio.bi_status) | |
137 | dio->bio.bi_status = bio->bi_status; | |
138 | ||
e71aa913 | 139 | if (atomic_dec_and_test(&dio->ref)) { |
09ce8744 | 140 | if (!(dio->flags & DIO_IS_SYNC)) { |
cd82cca7 CH |
141 | struct kiocb *iocb = dio->iocb; |
142 | ssize_t ret; | |
143 | ||
3e08773c CH |
144 | WRITE_ONCE(iocb->private, NULL); |
145 | ||
cd82cca7 CH |
146 | if (likely(!dio->bio.bi_status)) { |
147 | ret = dio->size; | |
148 | iocb->ki_pos += ret; | |
149 | } else { | |
150 | ret = blk_status_to_errno(dio->bio.bi_status); | |
151 | } | |
152 | ||
6b19b766 | 153 | dio->iocb->ki_complete(iocb, ret); |
e71aa913 | 154 | bio_put(&dio->bio); |
cd82cca7 CH |
155 | } else { |
156 | struct task_struct *waiter = dio->waiter; | |
157 | ||
158 | WRITE_ONCE(dio->waiter, NULL); | |
159 | blk_wake_io_task(waiter); | |
160 | } | |
161 | } | |
162 | ||
163 | if (should_dirty) { | |
164 | bio_check_pages_dirty(bio); | |
165 | } else { | |
166 | bio_release_pages(bio, false); | |
167 | bio_put(bio); | |
168 | } | |
169 | } | |
170 | ||
171 | static ssize_t __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter, | |
172 | unsigned int nr_pages) | |
173 | { | |
4e762d86 | 174 | struct block_device *bdev = I_BDEV(iocb->ki_filp->f_mapping->host); |
cd82cca7 CH |
175 | struct blk_plug plug; |
176 | struct blkdev_dio *dio; | |
177 | struct bio *bio; | |
cd82cca7 | 178 | bool is_read = (iov_iter_rw(iter) == READ), is_sync; |
16458cf3 | 179 | blk_opf_t opf = is_read ? REQ_OP_READ : dio_bio_write_op(iocb); |
cd82cca7 | 180 | loff_t pos = iocb->ki_pos; |
cd82cca7 CH |
181 | int ret = 0; |
182 | ||
37fee2e4 | 183 | if (blkdev_dio_unaligned(bdev, pos, iter)) |
cd82cca7 CH |
184 | return -EINVAL; |
185 | ||
0df71650 MS |
186 | if (iocb->ki_flags & IOCB_ALLOC_CACHE) |
187 | opf |= REQ_ALLOC_CACHE; | |
188 | bio = bio_alloc_bioset(bdev, nr_pages, opf, GFP_KERNEL, | |
189 | &blkdev_dio_pool); | |
cd82cca7 | 190 | dio = container_of(bio, struct blkdev_dio, bio); |
e71aa913 PB |
191 | atomic_set(&dio->ref, 1); |
192 | /* | |
193 | * Grab an extra reference to ensure the dio structure which is embedded | |
194 | * into the first bio stays around. | |
195 | */ | |
196 | bio_get(bio); | |
197 | ||
09ce8744 JA |
198 | is_sync = is_sync_kiocb(iocb); |
199 | if (is_sync) { | |
200 | dio->flags = DIO_IS_SYNC; | |
cd82cca7 | 201 | dio->waiter = current; |
cd82cca7 | 202 | } else { |
09ce8744 | 203 | dio->flags = 0; |
cd82cca7 CH |
204 | dio->iocb = iocb; |
205 | } | |
206 | ||
207 | dio->size = 0; | |
fcb14cb1 | 208 | if (is_read && user_backed_iter(iter)) |
09ce8744 | 209 | dio->flags |= DIO_SHOULD_DIRTY; |
cd82cca7 | 210 | |
25d207dc | 211 | blk_start_plug(&plug); |
cd82cca7 CH |
212 | |
213 | for (;;) { | |
6549a874 | 214 | bio->bi_iter.bi_sector = pos >> SECTOR_SHIFT; |
cd82cca7 CH |
215 | bio->bi_private = dio; |
216 | bio->bi_end_io = blkdev_bio_end_io; | |
217 | bio->bi_ioprio = iocb->ki_ioprio; | |
218 | ||
219 | ret = bio_iov_iter_get_pages(bio, iter); | |
220 | if (unlikely(ret)) { | |
221 | bio->bi_status = BLK_STS_IOERR; | |
222 | bio_endio(bio); | |
223 | break; | |
224 | } | |
67d59247 JA |
225 | if (iocb->ki_flags & IOCB_NOWAIT) { |
226 | /* | |
227 | * This is nonblocking IO, and we need to allocate | |
228 | * another bio if we have data left to map. As we | |
229 | * cannot guarantee that one of the sub bios will not | |
230 | * fail getting issued FOR NOWAIT and as error results | |
231 | * are coalesced across all of them, be safe and ask for | |
232 | * a retry of this from blocking context. | |
233 | */ | |
234 | if (unlikely(iov_iter_count(iter))) { | |
235 | bio_release_pages(bio, false); | |
236 | bio_clear_flag(bio, BIO_REFFED); | |
237 | bio_put(bio); | |
238 | blk_finish_plug(&plug); | |
239 | return -EAGAIN; | |
240 | } | |
241 | bio->bi_opf |= REQ_NOWAIT; | |
242 | } | |
cd82cca7 CH |
243 | |
244 | if (is_read) { | |
09ce8744 | 245 | if (dio->flags & DIO_SHOULD_DIRTY) |
cd82cca7 CH |
246 | bio_set_pages_dirty(bio); |
247 | } else { | |
cd82cca7 CH |
248 | task_io_account_write(bio->bi_iter.bi_size); |
249 | } | |
cd82cca7 CH |
250 | dio->size += bio->bi_iter.bi_size; |
251 | pos += bio->bi_iter.bi_size; | |
252 | ||
253 | nr_pages = bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS); | |
254 | if (!nr_pages) { | |
3e08773c | 255 | submit_bio(bio); |
cd82cca7 CH |
256 | break; |
257 | } | |
e71aa913 | 258 | atomic_inc(&dio->ref); |
cd82cca7 | 259 | submit_bio(bio); |
07888c66 | 260 | bio = bio_alloc(bdev, nr_pages, opf, GFP_KERNEL); |
cd82cca7 CH |
261 | } |
262 | ||
25d207dc | 263 | blk_finish_plug(&plug); |
cd82cca7 CH |
264 | |
265 | if (!is_sync) | |
266 | return -EIOCBQUEUED; | |
267 | ||
268 | for (;;) { | |
269 | set_current_state(TASK_UNINTERRUPTIBLE); | |
270 | if (!READ_ONCE(dio->waiter)) | |
271 | break; | |
25d207dc | 272 | blk_io_schedule(); |
cd82cca7 CH |
273 | } |
274 | __set_current_state(TASK_RUNNING); | |
275 | ||
276 | if (!ret) | |
277 | ret = blk_status_to_errno(dio->bio.bi_status); | |
278 | if (likely(!ret)) | |
279 | ret = dio->size; | |
280 | ||
281 | bio_put(&dio->bio); | |
282 | return ret; | |
283 | } | |
284 | ||
54a88eb8 PB |
285 | static void blkdev_bio_end_io_async(struct bio *bio) |
286 | { | |
287 | struct blkdev_dio *dio = container_of(bio, struct blkdev_dio, bio); | |
288 | struct kiocb *iocb = dio->iocb; | |
289 | ssize_t ret; | |
290 | ||
bb49c6fa SG |
291 | WRITE_ONCE(iocb->private, NULL); |
292 | ||
54a88eb8 PB |
293 | if (likely(!bio->bi_status)) { |
294 | ret = dio->size; | |
295 | iocb->ki_pos += ret; | |
296 | } else { | |
297 | ret = blk_status_to_errno(bio->bi_status); | |
298 | } | |
299 | ||
b6773cdb | 300 | iocb->ki_complete(iocb, ret); |
54a88eb8 PB |
301 | |
302 | if (dio->flags & DIO_SHOULD_DIRTY) { | |
303 | bio_check_pages_dirty(bio); | |
304 | } else { | |
305 | bio_release_pages(bio, false); | |
306 | bio_put(bio); | |
307 | } | |
308 | } | |
309 | ||
310 | static ssize_t __blkdev_direct_IO_async(struct kiocb *iocb, | |
311 | struct iov_iter *iter, | |
312 | unsigned int nr_pages) | |
313 | { | |
4e762d86 | 314 | struct block_device *bdev = I_BDEV(iocb->ki_filp->f_mapping->host); |
b77c88c2 | 315 | bool is_read = iov_iter_rw(iter) == READ; |
16458cf3 | 316 | blk_opf_t opf = is_read ? REQ_OP_READ : dio_bio_write_op(iocb); |
54a88eb8 PB |
317 | struct blkdev_dio *dio; |
318 | struct bio *bio; | |
319 | loff_t pos = iocb->ki_pos; | |
320 | int ret = 0; | |
321 | ||
37fee2e4 | 322 | if (blkdev_dio_unaligned(bdev, pos, iter)) |
54a88eb8 PB |
323 | return -EINVAL; |
324 | ||
0df71650 MS |
325 | if (iocb->ki_flags & IOCB_ALLOC_CACHE) |
326 | opf |= REQ_ALLOC_CACHE; | |
327 | bio = bio_alloc_bioset(bdev, nr_pages, opf, GFP_KERNEL, | |
328 | &blkdev_dio_pool); | |
54a88eb8 PB |
329 | dio = container_of(bio, struct blkdev_dio, bio); |
330 | dio->flags = 0; | |
331 | dio->iocb = iocb; | |
54a88eb8 | 332 | bio->bi_iter.bi_sector = pos >> SECTOR_SHIFT; |
54a88eb8 PB |
333 | bio->bi_end_io = blkdev_bio_end_io_async; |
334 | bio->bi_ioprio = iocb->ki_ioprio; | |
335 | ||
1bb6b810 PB |
336 | if (iov_iter_is_bvec(iter)) { |
337 | /* | |
338 | * Users don't rely on the iterator being in any particular | |
339 | * state for async I/O returning -EIOCBQUEUED, hence we can | |
340 | * avoid expensive iov_iter_advance(). Bypass | |
341 | * bio_iov_iter_get_pages() and set the bvec directly. | |
342 | */ | |
343 | bio_iov_bvec_set(bio, iter); | |
344 | } else { | |
345 | ret = bio_iov_iter_get_pages(bio, iter); | |
346 | if (unlikely(ret)) { | |
75feae73 | 347 | bio_put(bio); |
1bb6b810 PB |
348 | return ret; |
349 | } | |
54a88eb8 PB |
350 | } |
351 | dio->size = bio->bi_iter.bi_size; | |
352 | ||
b77c88c2 | 353 | if (is_read) { |
fcb14cb1 | 354 | if (user_backed_iter(iter)) { |
54a88eb8 PB |
355 | dio->flags |= DIO_SHOULD_DIRTY; |
356 | bio_set_pages_dirty(bio); | |
357 | } | |
358 | } else { | |
54a88eb8 PB |
359 | task_io_account_write(bio->bi_iter.bi_size); |
360 | } | |
361 | ||
54a88eb8 | 362 | if (iocb->ki_flags & IOCB_HIPRI) { |
842e39b0 | 363 | bio->bi_opf |= REQ_POLLED | REQ_NOWAIT; |
54a88eb8 PB |
364 | submit_bio(bio); |
365 | WRITE_ONCE(iocb->private, bio); | |
366 | } else { | |
842e39b0 PB |
367 | if (iocb->ki_flags & IOCB_NOWAIT) |
368 | bio->bi_opf |= REQ_NOWAIT; | |
54a88eb8 PB |
369 | submit_bio(bio); |
370 | } | |
371 | return -EIOCBQUEUED; | |
372 | } | |
373 | ||
cd82cca7 CH |
374 | static ssize_t blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter) |
375 | { | |
376 | unsigned int nr_pages; | |
377 | ||
378 | if (!iov_iter_count(iter)) | |
379 | return 0; | |
380 | ||
381 | nr_pages = bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS + 1); | |
54a88eb8 PB |
382 | if (likely(nr_pages <= BIO_MAX_VECS)) { |
383 | if (is_sync_kiocb(iocb)) | |
384 | return __blkdev_direct_IO_simple(iocb, iter, nr_pages); | |
385 | return __blkdev_direct_IO_async(iocb, iter, nr_pages); | |
386 | } | |
cd82cca7 CH |
387 | return __blkdev_direct_IO(iocb, iter, bio_max_segs(nr_pages)); |
388 | } | |
389 | ||
487c607d CH |
390 | static int blkdev_iomap_begin(struct inode *inode, loff_t offset, loff_t length, |
391 | unsigned int flags, struct iomap *iomap, struct iomap *srcmap) | |
392 | { | |
393 | struct block_device *bdev = I_BDEV(inode); | |
394 | loff_t isize = i_size_read(inode); | |
395 | ||
396 | iomap->bdev = bdev; | |
397 | iomap->offset = ALIGN_DOWN(offset, bdev_logical_block_size(bdev)); | |
398 | if (iomap->offset >= isize) | |
399 | return -EIO; | |
400 | iomap->type = IOMAP_MAPPED; | |
401 | iomap->addr = iomap->offset; | |
402 | iomap->length = isize - iomap->offset; | |
403 | iomap->flags |= IOMAP_F_BUFFER_HEAD; | |
404 | return 0; | |
405 | } | |
406 | ||
407 | static const struct iomap_ops blkdev_iomap_ops = { | |
408 | .iomap_begin = blkdev_iomap_begin, | |
409 | }; | |
410 | ||
cd82cca7 CH |
411 | static int blkdev_writepage(struct page *page, struct writeback_control *wbc) |
412 | { | |
413 | return block_write_full_page(page, blkdev_get_block, wbc); | |
414 | } | |
415 | ||
2c69e205 | 416 | static int blkdev_read_folio(struct file *file, struct folio *folio) |
cd82cca7 | 417 | { |
2c69e205 | 418 | return block_read_full_folio(folio, blkdev_get_block); |
cd82cca7 CH |
419 | } |
420 | ||
421 | static void blkdev_readahead(struct readahead_control *rac) | |
422 | { | |
423 | mpage_readahead(rac, blkdev_get_block); | |
424 | } | |
425 | ||
426 | static int blkdev_write_begin(struct file *file, struct address_space *mapping, | |
9d6b0cd7 | 427 | loff_t pos, unsigned len, struct page **pagep, void **fsdata) |
cd82cca7 | 428 | { |
b3992d1e | 429 | return block_write_begin(mapping, pos, len, pagep, blkdev_get_block); |
cd82cca7 CH |
430 | } |
431 | ||
432 | static int blkdev_write_end(struct file *file, struct address_space *mapping, | |
433 | loff_t pos, unsigned len, unsigned copied, struct page *page, | |
434 | void *fsdata) | |
435 | { | |
436 | int ret; | |
437 | ret = block_write_end(file, mapping, pos, len, copied, page, fsdata); | |
438 | ||
439 | unlock_page(page); | |
440 | put_page(page); | |
441 | ||
442 | return ret; | |
443 | } | |
444 | ||
cd82cca7 | 445 | const struct address_space_operations def_blk_aops = { |
e621900a | 446 | .dirty_folio = block_dirty_folio, |
7ba13abb | 447 | .invalidate_folio = block_invalidate_folio, |
2c69e205 | 448 | .read_folio = blkdev_read_folio, |
cd82cca7 CH |
449 | .readahead = blkdev_readahead, |
450 | .writepage = blkdev_writepage, | |
451 | .write_begin = blkdev_write_begin, | |
452 | .write_end = blkdev_write_end, | |
67235182 | 453 | .migrate_folio = buffer_migrate_folio_norefs, |
cd82cca7 CH |
454 | .is_dirty_writeback = buffer_check_dirty_writeback, |
455 | }; | |
456 | ||
457 | /* | |
458 | * for a block special file file_inode(file)->i_size is zero | |
459 | * so we compute the size by hand (just as in block_read/write above) | |
460 | */ | |
461 | static loff_t blkdev_llseek(struct file *file, loff_t offset, int whence) | |
462 | { | |
463 | struct inode *bd_inode = bdev_file_inode(file); | |
464 | loff_t retval; | |
465 | ||
466 | inode_lock(bd_inode); | |
467 | retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode)); | |
468 | inode_unlock(bd_inode); | |
469 | return retval; | |
470 | } | |
471 | ||
472 | static int blkdev_fsync(struct file *filp, loff_t start, loff_t end, | |
473 | int datasync) | |
474 | { | |
4e762d86 | 475 | struct block_device *bdev = I_BDEV(filp->f_mapping->host); |
cd82cca7 CH |
476 | int error; |
477 | ||
478 | error = file_write_and_wait_range(filp, start, end); | |
479 | if (error) | |
480 | return error; | |
481 | ||
482 | /* | |
483 | * There is no need to serialise calls to blkdev_issue_flush with | |
484 | * i_mutex and doing so causes performance issues with concurrent | |
485 | * O_SYNC writers to a block device. | |
486 | */ | |
487 | error = blkdev_issue_flush(bdev); | |
488 | if (error == -EOPNOTSUPP) | |
489 | error = 0; | |
490 | ||
491 | return error; | |
492 | } | |
493 | ||
05bdb996 CH |
494 | blk_mode_t file_to_blk_mode(struct file *file) |
495 | { | |
496 | blk_mode_t mode = 0; | |
497 | ||
498 | if (file->f_mode & FMODE_READ) | |
499 | mode |= BLK_OPEN_READ; | |
500 | if (file->f_mode & FMODE_WRITE) | |
501 | mode |= BLK_OPEN_WRITE; | |
ee3249a8 | 502 | if (file->private_data) |
05bdb996 CH |
503 | mode |= BLK_OPEN_EXCL; |
504 | if (file->f_flags & O_NDELAY) | |
505 | mode |= BLK_OPEN_NDELAY; | |
506 | ||
507 | /* | |
508 | * If all bits in O_ACCMODE set (aka O_RDWR | O_WRONLY), the floppy | |
509 | * driver has historically allowed ioctls as if the file was opened for | |
510 | * writing, but does not allow and actual reads or writes. | |
511 | */ | |
512 | if ((file->f_flags & O_ACCMODE) == (O_RDWR | O_WRONLY)) | |
513 | mode |= BLK_OPEN_WRITE_IOCTL; | |
514 | ||
515 | return mode; | |
516 | } | |
517 | ||
cd82cca7 CH |
518 | static int blkdev_open(struct inode *inode, struct file *filp) |
519 | { | |
520 | struct block_device *bdev; | |
521 | ||
522 | /* | |
523 | * Preserve backwards compatibility and allow large file access | |
524 | * even if userspace doesn't ask for it explicitly. Some mkfs | |
525 | * binary needs it. We might want to drop this workaround | |
526 | * during an unstable branch. | |
527 | */ | |
528 | filp->f_flags |= O_LARGEFILE; | |
a05f7bd9 | 529 | filp->f_mode |= FMODE_BUF_RASYNC | FMODE_CAN_ODIRECT; |
cd82cca7 | 530 | |
ee3249a8 CH |
531 | /* |
532 | * Use the file private data to store the holder for exclusive openes. | |
533 | * file_to_blk_mode relies on it being present to set BLK_OPEN_EXCL. | |
534 | */ | |
cd82cca7 | 535 | if (filp->f_flags & O_EXCL) |
ee3249a8 | 536 | filp->private_data = filp; |
cd82cca7 | 537 | |
05bdb996 | 538 | bdev = blkdev_get_by_dev(inode->i_rdev, file_to_blk_mode(filp), |
ee3249a8 | 539 | filp->private_data, NULL); |
cd82cca7 CH |
540 | if (IS_ERR(bdev)) |
541 | return PTR_ERR(bdev); | |
fac7c6d5 | 542 | |
e9833d87 JA |
543 | if (bdev_nowait(bdev)) |
544 | filp->f_mode |= FMODE_NOWAIT; | |
545 | ||
cd82cca7 CH |
546 | filp->f_mapping = bdev->bd_inode->i_mapping; |
547 | filp->f_wb_err = filemap_sample_wb_err(filp->f_mapping); | |
548 | return 0; | |
549 | } | |
550 | ||
7ee34cbc | 551 | static int blkdev_release(struct inode *inode, struct file *filp) |
cd82cca7 | 552 | { |
ee3249a8 | 553 | blkdev_put(I_BDEV(filp->f_mapping->host), filp->private_data); |
cd82cca7 CH |
554 | return 0; |
555 | } | |
556 | ||
727cfe97 CH |
557 | static ssize_t |
558 | blkdev_direct_write(struct kiocb *iocb, struct iov_iter *from) | |
559 | { | |
560 | size_t count = iov_iter_count(from); | |
561 | ssize_t written; | |
562 | ||
563 | written = kiocb_invalidate_pages(iocb, count); | |
564 | if (written) { | |
565 | if (written == -EBUSY) | |
566 | return 0; | |
567 | return written; | |
568 | } | |
569 | ||
570 | written = blkdev_direct_IO(iocb, from); | |
571 | if (written > 0) { | |
572 | kiocb_invalidate_post_direct_write(iocb, count); | |
573 | iocb->ki_pos += written; | |
574 | count -= written; | |
575 | } | |
576 | if (written != -EIOCBQUEUED) | |
577 | iov_iter_revert(from, count - iov_iter_count(from)); | |
578 | return written; | |
579 | } | |
580 | ||
487c607d CH |
581 | static ssize_t blkdev_buffered_write(struct kiocb *iocb, struct iov_iter *from) |
582 | { | |
583 | return iomap_file_buffered_write(iocb, from, &blkdev_iomap_ops); | |
584 | } | |
585 | ||
cd82cca7 CH |
586 | /* |
587 | * Write data to the block device. Only intended for the block device itself | |
588 | * and the raw driver which basically is a fake block device. | |
589 | * | |
590 | * Does not take i_mutex for the write and thus is not for general purpose | |
591 | * use. | |
592 | */ | |
593 | static ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from) | |
594 | { | |
727cfe97 CH |
595 | struct file *file = iocb->ki_filp; |
596 | struct block_device *bdev = I_BDEV(file->f_mapping->host); | |
fac7c6d5 | 597 | struct inode *bd_inode = bdev->bd_inode; |
138c1a38 | 598 | loff_t size = bdev_nr_bytes(bdev); |
cd82cca7 CH |
599 | size_t shorted = 0; |
600 | ssize_t ret; | |
601 | ||
fac7c6d5 | 602 | if (bdev_read_only(bdev)) |
cd82cca7 CH |
603 | return -EPERM; |
604 | ||
605 | if (IS_SWAPFILE(bd_inode) && !is_hibernate_resume_dev(bd_inode->i_rdev)) | |
606 | return -ETXTBSY; | |
607 | ||
608 | if (!iov_iter_count(from)) | |
609 | return 0; | |
610 | ||
611 | if (iocb->ki_pos >= size) | |
612 | return -ENOSPC; | |
613 | ||
614 | if ((iocb->ki_flags & (IOCB_NOWAIT | IOCB_DIRECT)) == IOCB_NOWAIT) | |
615 | return -EOPNOTSUPP; | |
616 | ||
617 | size -= iocb->ki_pos; | |
618 | if (iov_iter_count(from) > size) { | |
619 | shorted = iov_iter_count(from) - size; | |
620 | iov_iter_truncate(from, size); | |
621 | } | |
622 | ||
727cfe97 CH |
623 | ret = file_remove_privs(file); |
624 | if (ret) | |
625 | return ret; | |
626 | ||
627 | ret = file_update_time(file); | |
628 | if (ret) | |
629 | return ret; | |
630 | ||
631 | if (iocb->ki_flags & IOCB_DIRECT) { | |
632 | ret = blkdev_direct_write(iocb, from); | |
633 | if (ret >= 0 && iov_iter_count(from)) | |
634 | ret = direct_write_fallback(iocb, from, ret, | |
487c607d | 635 | blkdev_buffered_write(iocb, from)); |
727cfe97 | 636 | } else { |
487c607d | 637 | ret = blkdev_buffered_write(iocb, from); |
727cfe97 CH |
638 | } |
639 | ||
cd82cca7 CH |
640 | if (ret > 0) |
641 | ret = generic_write_sync(iocb, ret); | |
642 | iov_iter_reexpand(from, iov_iter_count(from) + shorted); | |
cd82cca7 CH |
643 | return ret; |
644 | } | |
645 | ||
646 | static ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to) | |
647 | { | |
4e762d86 | 648 | struct block_device *bdev = I_BDEV(iocb->ki_filp->f_mapping->host); |
138c1a38 | 649 | loff_t size = bdev_nr_bytes(bdev); |
cd82cca7 CH |
650 | loff_t pos = iocb->ki_pos; |
651 | size_t shorted = 0; | |
ceaa7625 | 652 | ssize_t ret = 0; |
3e1f941d | 653 | size_t count; |
cd82cca7 | 654 | |
3e1f941d | 655 | if (unlikely(pos + iov_iter_count(to) > size)) { |
6450fe1f PB |
656 | if (pos >= size) |
657 | return 0; | |
658 | size -= pos; | |
3e1f941d ID |
659 | shorted = iov_iter_count(to) - size; |
660 | iov_iter_truncate(to, size); | |
cd82cca7 CH |
661 | } |
662 | ||
3e1f941d ID |
663 | count = iov_iter_count(to); |
664 | if (!count) | |
665 | goto reexpand; /* skip atime */ | |
666 | ||
ceaa7625 | 667 | if (iocb->ki_flags & IOCB_DIRECT) { |
3c435a0f CH |
668 | ret = kiocb_write_and_wait(iocb, count); |
669 | if (ret < 0) | |
670 | goto reexpand; | |
ceaa7625 JA |
671 | file_accessed(iocb->ki_filp); |
672 | ||
673 | ret = blkdev_direct_IO(iocb, to); | |
674 | if (ret >= 0) { | |
675 | iocb->ki_pos += ret; | |
676 | count -= ret; | |
677 | } | |
3e1f941d | 678 | iov_iter_revert(to, count - iov_iter_count(to)); |
ceaa7625 | 679 | if (ret < 0 || !count) |
3e1f941d | 680 | goto reexpand; |
ceaa7625 JA |
681 | } |
682 | ||
683 | ret = filemap_read(iocb, to, ret); | |
6450fe1f | 684 | |
3e1f941d | 685 | reexpand: |
6450fe1f PB |
686 | if (unlikely(shorted)) |
687 | iov_iter_reexpand(to, iov_iter_count(to) + shorted); | |
cd82cca7 CH |
688 | return ret; |
689 | } | |
690 | ||
691 | #define BLKDEV_FALLOC_FL_SUPPORTED \ | |
692 | (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \ | |
693 | FALLOC_FL_ZERO_RANGE | FALLOC_FL_NO_HIDE_STALE) | |
694 | ||
695 | static long blkdev_fallocate(struct file *file, int mode, loff_t start, | |
696 | loff_t len) | |
697 | { | |
f278eb3d ML |
698 | struct inode *inode = bdev_file_inode(file); |
699 | struct block_device *bdev = I_BDEV(inode); | |
cd82cca7 CH |
700 | loff_t end = start + len - 1; |
701 | loff_t isize; | |
702 | int error; | |
703 | ||
704 | /* Fail if we don't recognize the flags. */ | |
705 | if (mode & ~BLKDEV_FALLOC_FL_SUPPORTED) | |
706 | return -EOPNOTSUPP; | |
707 | ||
708 | /* Don't go off the end of the device. */ | |
2a93ad8f | 709 | isize = bdev_nr_bytes(bdev); |
cd82cca7 CH |
710 | if (start >= isize) |
711 | return -EINVAL; | |
712 | if (end >= isize) { | |
713 | if (mode & FALLOC_FL_KEEP_SIZE) { | |
714 | len = isize - start; | |
715 | end = start + len - 1; | |
716 | } else | |
717 | return -EINVAL; | |
718 | } | |
719 | ||
720 | /* | |
721 | * Don't allow IO that isn't aligned to logical block size. | |
722 | */ | |
723 | if ((start | len) & (bdev_logical_block_size(bdev) - 1)) | |
724 | return -EINVAL; | |
725 | ||
f278eb3d ML |
726 | filemap_invalidate_lock(inode->i_mapping); |
727 | ||
cd82cca7 | 728 | /* Invalidate the page cache, including dirty pages. */ |
05bdb996 | 729 | error = truncate_bdev_range(bdev, file_to_blk_mode(file), start, end); |
cd82cca7 | 730 | if (error) |
f278eb3d | 731 | goto fail; |
cd82cca7 CH |
732 | |
733 | switch (mode) { | |
734 | case FALLOC_FL_ZERO_RANGE: | |
735 | case FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE: | |
6549a874 PB |
736 | error = blkdev_issue_zeroout(bdev, start >> SECTOR_SHIFT, |
737 | len >> SECTOR_SHIFT, GFP_KERNEL, | |
738 | BLKDEV_ZERO_NOUNMAP); | |
cd82cca7 CH |
739 | break; |
740 | case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE: | |
6549a874 PB |
741 | error = blkdev_issue_zeroout(bdev, start >> SECTOR_SHIFT, |
742 | len >> SECTOR_SHIFT, GFP_KERNEL, | |
743 | BLKDEV_ZERO_NOFALLBACK); | |
cd82cca7 CH |
744 | break; |
745 | case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE | FALLOC_FL_NO_HIDE_STALE: | |
6549a874 | 746 | error = blkdev_issue_discard(bdev, start >> SECTOR_SHIFT, |
44abff2c | 747 | len >> SECTOR_SHIFT, GFP_KERNEL); |
cd82cca7 CH |
748 | break; |
749 | default: | |
f278eb3d | 750 | error = -EOPNOTSUPP; |
cd82cca7 | 751 | } |
cd82cca7 | 752 | |
f278eb3d ML |
753 | fail: |
754 | filemap_invalidate_unlock(inode->i_mapping); | |
755 | return error; | |
cd82cca7 CH |
756 | } |
757 | ||
69baa3a6 LP |
758 | static int blkdev_mmap(struct file *file, struct vm_area_struct *vma) |
759 | { | |
760 | struct inode *bd_inode = bdev_file_inode(file); | |
761 | ||
762 | if (bdev_read_only(I_BDEV(bd_inode))) | |
763 | return generic_file_readonly_mmap(file, vma); | |
764 | ||
765 | return generic_file_mmap(file, vma); | |
766 | } | |
767 | ||
cd82cca7 CH |
768 | const struct file_operations def_blk_fops = { |
769 | .open = blkdev_open, | |
7ee34cbc | 770 | .release = blkdev_release, |
cd82cca7 CH |
771 | .llseek = blkdev_llseek, |
772 | .read_iter = blkdev_read_iter, | |
773 | .write_iter = blkdev_write_iter, | |
3e08773c | 774 | .iopoll = iocb_bio_iopoll, |
69baa3a6 | 775 | .mmap = blkdev_mmap, |
cd82cca7 | 776 | .fsync = blkdev_fsync, |
8a709512 | 777 | .unlocked_ioctl = blkdev_ioctl, |
cd82cca7 CH |
778 | #ifdef CONFIG_COMPAT |
779 | .compat_ioctl = compat_blkdev_ioctl, | |
780 | #endif | |
2cb1e089 | 781 | .splice_read = filemap_splice_read, |
cd82cca7 CH |
782 | .splice_write = iter_file_splice_write, |
783 | .fallocate = blkdev_fallocate, | |
784 | }; | |
785 | ||
786 | static __init int blkdev_init(void) | |
787 | { | |
788 | return bioset_init(&blkdev_dio_pool, 4, | |
789 | offsetof(struct blkdev_dio, bio), | |
790 | BIOSET_NEED_BVECS|BIOSET_PERCPU_CACHE); | |
791 | } | |
792 | module_init(blkdev_init); |