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