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