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