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