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