1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Simple file system for zoned block devices exposing zones as files. 4 * 5 * Copyright (C) 2022 Western Digital Corporation or its affiliates. 6 */ 7 #include <linux/module.h> 8 #include <linux/pagemap.h> 9 #include <linux/iomap.h> 10 #include <linux/init.h> 11 #include <linux/slab.h> 12 #include <linux/blkdev.h> 13 #include <linux/statfs.h> 14 #include <linux/writeback.h> 15 #include <linux/quotaops.h> 16 #include <linux/seq_file.h> 17 #include <linux/parser.h> 18 #include <linux/uio.h> 19 #include <linux/mman.h> 20 #include <linux/sched/mm.h> 21 #include <linux/task_io_accounting_ops.h> 22 23 #include "zonefs.h" 24 25 #include "trace.h" 26 27 static int zonefs_read_iomap_begin(struct inode *inode, loff_t offset, 28 loff_t length, unsigned int flags, 29 struct iomap *iomap, struct iomap *srcmap) 30 { 31 struct zonefs_inode_info *zi = ZONEFS_I(inode); 32 struct zonefs_zone *z = zonefs_inode_zone(inode); 33 struct super_block *sb = inode->i_sb; 34 loff_t isize; 35 36 /* 37 * All blocks are always mapped below EOF. If reading past EOF, 38 * act as if there is a hole up to the file maximum size. 39 */ 40 mutex_lock(&zi->i_truncate_mutex); 41 iomap->bdev = inode->i_sb->s_bdev; 42 iomap->offset = ALIGN_DOWN(offset, sb->s_blocksize); 43 isize = i_size_read(inode); 44 if (iomap->offset >= isize) { 45 iomap->type = IOMAP_HOLE; 46 iomap->addr = IOMAP_NULL_ADDR; 47 iomap->length = length; 48 } else { 49 iomap->type = IOMAP_MAPPED; 50 iomap->addr = (z->z_sector << SECTOR_SHIFT) + iomap->offset; 51 iomap->length = isize - iomap->offset; 52 } 53 mutex_unlock(&zi->i_truncate_mutex); 54 55 trace_zonefs_iomap_begin(inode, iomap); 56 57 return 0; 58 } 59 60 static const struct iomap_ops zonefs_read_iomap_ops = { 61 .iomap_begin = zonefs_read_iomap_begin, 62 }; 63 64 static int zonefs_write_iomap_begin(struct inode *inode, loff_t offset, 65 loff_t length, unsigned int flags, 66 struct iomap *iomap, struct iomap *srcmap) 67 { 68 struct zonefs_inode_info *zi = ZONEFS_I(inode); 69 struct zonefs_zone *z = zonefs_inode_zone(inode); 70 struct super_block *sb = inode->i_sb; 71 loff_t isize; 72 73 /* All write I/Os should always be within the file maximum size */ 74 if (WARN_ON_ONCE(offset + length > z->z_capacity)) 75 return -EIO; 76 77 /* 78 * Sequential zones can only accept direct writes. This is already 79 * checked when writes are issued, so warn if we see a page writeback 80 * operation. 81 */ 82 if (WARN_ON_ONCE(zonefs_zone_is_seq(z) && !(flags & IOMAP_DIRECT))) 83 return -EIO; 84 85 /* 86 * For conventional zones, all blocks are always mapped. For sequential 87 * zones, all blocks after always mapped below the inode size (zone 88 * write pointer) and unwriten beyond. 89 */ 90 mutex_lock(&zi->i_truncate_mutex); 91 iomap->bdev = inode->i_sb->s_bdev; 92 iomap->offset = ALIGN_DOWN(offset, sb->s_blocksize); 93 iomap->addr = (z->z_sector << SECTOR_SHIFT) + iomap->offset; 94 isize = i_size_read(inode); 95 if (iomap->offset >= isize) { 96 iomap->type = IOMAP_UNWRITTEN; 97 iomap->length = z->z_capacity - iomap->offset; 98 } else { 99 iomap->type = IOMAP_MAPPED; 100 iomap->length = isize - iomap->offset; 101 } 102 mutex_unlock(&zi->i_truncate_mutex); 103 104 trace_zonefs_iomap_begin(inode, iomap); 105 106 return 0; 107 } 108 109 static const struct iomap_ops zonefs_write_iomap_ops = { 110 .iomap_begin = zonefs_write_iomap_begin, 111 }; 112 113 static int zonefs_read_folio(struct file *unused, struct folio *folio) 114 { 115 return iomap_read_folio(folio, &zonefs_read_iomap_ops); 116 } 117 118 static void zonefs_readahead(struct readahead_control *rac) 119 { 120 iomap_readahead(rac, &zonefs_read_iomap_ops); 121 } 122 123 /* 124 * Map blocks for page writeback. This is used only on conventional zone files, 125 * which implies that the page range can only be within the fixed inode size. 126 */ 127 static ssize_t zonefs_writeback_range(struct iomap_writepage_ctx *wpc, 128 struct folio *folio, u64 offset, unsigned len, u64 end_pos) 129 { 130 struct zonefs_zone *z = zonefs_inode_zone(wpc->inode); 131 132 if (WARN_ON_ONCE(zonefs_zone_is_seq(z))) 133 return -EIO; 134 if (WARN_ON_ONCE(offset >= i_size_read(wpc->inode))) 135 return -EIO; 136 137 /* If the mapping is already OK, nothing needs to be done */ 138 if (offset < wpc->iomap.offset || 139 offset >= wpc->iomap.offset + wpc->iomap.length) { 140 int error; 141 142 error = zonefs_write_iomap_begin(wpc->inode, offset, 143 z->z_capacity - offset, IOMAP_WRITE, 144 &wpc->iomap, NULL); 145 if (error) 146 return error; 147 } 148 149 return iomap_add_to_ioend(wpc, folio, offset, end_pos, len); 150 } 151 152 static const struct iomap_writeback_ops zonefs_writeback_ops = { 153 .writeback_range = zonefs_writeback_range, 154 .writeback_submit = iomap_ioend_writeback_submit, 155 }; 156 157 static int zonefs_writepages(struct address_space *mapping, 158 struct writeback_control *wbc) 159 { 160 struct iomap_writepage_ctx wpc = { 161 .inode = mapping->host, 162 .wbc = wbc, 163 .ops = &zonefs_writeback_ops, 164 }; 165 166 return iomap_writepages(&wpc); 167 } 168 169 static int zonefs_swap_activate(struct swap_info_struct *sis, 170 struct file *swap_file, sector_t *span) 171 { 172 struct inode *inode = file_inode(swap_file); 173 174 if (zonefs_inode_is_seq(inode)) { 175 zonefs_err(inode->i_sb, 176 "swap file: not a conventional zone file\n"); 177 return -EINVAL; 178 } 179 180 return iomap_swapfile_activate(sis, swap_file, span, 181 &zonefs_read_iomap_ops); 182 } 183 184 const struct address_space_operations zonefs_file_aops = { 185 .read_folio = zonefs_read_folio, 186 .readahead = zonefs_readahead, 187 .writepages = zonefs_writepages, 188 .dirty_folio = iomap_dirty_folio, 189 .release_folio = iomap_release_folio, 190 .invalidate_folio = iomap_invalidate_folio, 191 .migrate_folio = filemap_migrate_folio, 192 .is_partially_uptodate = iomap_is_partially_uptodate, 193 .error_remove_folio = generic_error_remove_folio, 194 .swap_activate = zonefs_swap_activate, 195 }; 196 197 int zonefs_file_truncate(struct inode *inode, loff_t isize) 198 { 199 struct zonefs_inode_info *zi = ZONEFS_I(inode); 200 struct zonefs_zone *z = zonefs_inode_zone(inode); 201 loff_t old_isize; 202 enum req_op op; 203 int ret = 0; 204 205 /* 206 * Only sequential zone files can be truncated and truncation is allowed 207 * only down to a 0 size, which is equivalent to a zone reset, and to 208 * the maximum file size, which is equivalent to a zone finish. 209 */ 210 if (!zonefs_zone_is_seq(z)) 211 return -EPERM; 212 213 if (!isize) 214 op = REQ_OP_ZONE_RESET; 215 else if (isize == z->z_capacity) 216 op = REQ_OP_ZONE_FINISH; 217 else 218 return -EPERM; 219 220 inode_dio_wait(inode); 221 222 /* Serialize against page faults */ 223 filemap_invalidate_lock(inode->i_mapping); 224 225 /* Serialize against zonefs_iomap_begin() */ 226 mutex_lock(&zi->i_truncate_mutex); 227 228 old_isize = i_size_read(inode); 229 if (isize == old_isize) 230 goto unlock; 231 232 ret = zonefs_inode_zone_mgmt(inode, op); 233 if (ret) 234 goto unlock; 235 236 /* 237 * If the mount option ZONEFS_MNTOPT_EXPLICIT_OPEN is set, 238 * take care of open zones. 239 */ 240 if (z->z_flags & ZONEFS_ZONE_OPEN) { 241 /* 242 * Truncating a zone to EMPTY or FULL is the equivalent of 243 * closing the zone. For a truncation to 0, we need to 244 * re-open the zone to ensure new writes can be processed. 245 * For a truncation to the maximum file size, the zone is 246 * closed and writes cannot be accepted anymore, so clear 247 * the open flag. 248 */ 249 if (!isize) 250 ret = zonefs_inode_zone_mgmt(inode, REQ_OP_ZONE_OPEN); 251 else 252 z->z_flags &= ~ZONEFS_ZONE_OPEN; 253 } 254 255 zonefs_update_stats(inode, isize); 256 truncate_setsize(inode, isize); 257 z->z_wpoffset = isize; 258 zonefs_inode_account_active(inode); 259 260 unlock: 261 mutex_unlock(&zi->i_truncate_mutex); 262 filemap_invalidate_unlock(inode->i_mapping); 263 264 return ret; 265 } 266 267 static int zonefs_file_fsync(struct file *file, loff_t start, loff_t end, 268 int datasync) 269 { 270 struct inode *inode = file_inode(file); 271 int ret = 0; 272 273 if (unlikely(IS_IMMUTABLE(inode))) 274 return -EPERM; 275 276 /* 277 * Since only direct writes are allowed in sequential files, page cache 278 * flush is needed only for conventional zone files. 279 */ 280 if (zonefs_inode_is_cnv(inode)) 281 ret = file_write_and_wait_range(file, start, end); 282 if (!ret) 283 ret = blkdev_issue_flush(inode->i_sb->s_bdev); 284 285 if (ret) 286 zonefs_io_error(inode, true); 287 288 return ret; 289 } 290 291 static vm_fault_t zonefs_filemap_page_mkwrite(struct vm_fault *vmf) 292 { 293 struct inode *inode = file_inode(vmf->vma->vm_file); 294 vm_fault_t ret; 295 296 if (unlikely(IS_IMMUTABLE(inode))) 297 return VM_FAULT_SIGBUS; 298 299 /* 300 * Sanity check: only conventional zone files can have shared 301 * writeable mappings. 302 */ 303 if (zonefs_inode_is_seq(inode)) 304 return VM_FAULT_NOPAGE; 305 306 sb_start_pagefault(inode->i_sb); 307 file_update_time(vmf->vma->vm_file); 308 309 /* Serialize against truncates */ 310 filemap_invalidate_lock_shared(inode->i_mapping); 311 ret = iomap_page_mkwrite(vmf, &zonefs_write_iomap_ops, NULL); 312 filemap_invalidate_unlock_shared(inode->i_mapping); 313 314 sb_end_pagefault(inode->i_sb); 315 return ret; 316 } 317 318 static const struct vm_operations_struct zonefs_file_vm_ops = { 319 .fault = filemap_fault, 320 .map_pages = filemap_map_pages, 321 .page_mkwrite = zonefs_filemap_page_mkwrite, 322 }; 323 324 static int zonefs_file_mmap(struct file *file, struct vm_area_struct *vma) 325 { 326 /* 327 * Conventional zones accept random writes, so their files can support 328 * shared writable mappings. For sequential zone files, only read 329 * mappings are possible since there are no guarantees for write 330 * ordering between msync() and page cache writeback. 331 */ 332 if (zonefs_inode_is_seq(file_inode(file)) && 333 (vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) 334 return -EINVAL; 335 336 file_accessed(file); 337 vma->vm_ops = &zonefs_file_vm_ops; 338 339 return 0; 340 } 341 342 static loff_t zonefs_file_llseek(struct file *file, loff_t offset, int whence) 343 { 344 loff_t isize = i_size_read(file_inode(file)); 345 346 /* 347 * Seeks are limited to below the zone size for conventional zones 348 * and below the zone write pointer for sequential zones. In both 349 * cases, this limit is the inode size. 350 */ 351 return generic_file_llseek_size(file, offset, whence, isize, isize); 352 } 353 354 static int zonefs_file_write_dio_end_io(struct kiocb *iocb, ssize_t size, 355 int error, unsigned int flags) 356 { 357 struct inode *inode = file_inode(iocb->ki_filp); 358 struct zonefs_inode_info *zi = ZONEFS_I(inode); 359 360 if (error) { 361 /* 362 * For Sync IOs, error recovery is called from 363 * zonefs_file_dio_write(). 364 */ 365 if (!is_sync_kiocb(iocb)) 366 zonefs_io_error(inode, true); 367 return error; 368 } 369 370 if (size && zonefs_inode_is_seq(inode)) { 371 /* 372 * Note that we may be seeing completions out of order, 373 * but that is not a problem since a write completed 374 * successfully necessarily means that all preceding writes 375 * were also successful. So we can safely increase the inode 376 * size to the write end location. 377 */ 378 mutex_lock(&zi->i_truncate_mutex); 379 if (i_size_read(inode) < iocb->ki_pos + size) { 380 zonefs_update_stats(inode, iocb->ki_pos + size); 381 zonefs_i_size_write(inode, iocb->ki_pos + size); 382 } 383 mutex_unlock(&zi->i_truncate_mutex); 384 } 385 386 return 0; 387 } 388 389 static const struct iomap_dio_ops zonefs_write_dio_ops = { 390 .end_io = zonefs_file_write_dio_end_io, 391 }; 392 393 /* 394 * Do not exceed the LFS limits nor the file zone size. If pos is under the 395 * limit it becomes a short access. If it exceeds the limit, return -EFBIG. 396 */ 397 static loff_t zonefs_write_check_limits(struct file *file, loff_t pos, 398 loff_t count) 399 { 400 struct inode *inode = file_inode(file); 401 struct zonefs_zone *z = zonefs_inode_zone(inode); 402 loff_t limit = rlimit(RLIMIT_FSIZE); 403 loff_t max_size = z->z_capacity; 404 405 if (limit != RLIM_INFINITY) { 406 if (pos >= limit) { 407 send_sig(SIGXFSZ, current, 0); 408 return -EFBIG; 409 } 410 count = min(count, limit - pos); 411 } 412 413 if (!(file->f_flags & O_LARGEFILE)) 414 max_size = min_t(loff_t, MAX_NON_LFS, max_size); 415 416 if (unlikely(pos >= max_size)) 417 return -EFBIG; 418 419 return min(count, max_size - pos); 420 } 421 422 static ssize_t zonefs_write_checks(struct kiocb *iocb, struct iov_iter *from) 423 { 424 struct file *file = iocb->ki_filp; 425 struct inode *inode = file_inode(file); 426 struct zonefs_inode_info *zi = ZONEFS_I(inode); 427 struct zonefs_zone *z = zonefs_inode_zone(inode); 428 loff_t count; 429 430 if (IS_SWAPFILE(inode)) 431 return -ETXTBSY; 432 433 if (!iov_iter_count(from)) 434 return 0; 435 436 if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT)) 437 return -EINVAL; 438 439 if (iocb->ki_flags & IOCB_APPEND) { 440 if (zonefs_zone_is_cnv(z)) 441 return -EINVAL; 442 mutex_lock(&zi->i_truncate_mutex); 443 iocb->ki_pos = z->z_wpoffset; 444 mutex_unlock(&zi->i_truncate_mutex); 445 } 446 447 count = zonefs_write_check_limits(file, iocb->ki_pos, 448 iov_iter_count(from)); 449 if (count < 0) 450 return count; 451 452 iov_iter_truncate(from, count); 453 return iov_iter_count(from); 454 } 455 456 /* 457 * Handle direct writes. For sequential zone files, this is the only possible 458 * write path. For these files, check that the user is issuing writes 459 * sequentially from the end of the file. This code assumes that the block layer 460 * delivers write requests to the device in sequential order. This is always the 461 * case if a block IO scheduler implementing the ELEVATOR_F_ZBD_SEQ_WRITE 462 * elevator feature is being used (e.g. mq-deadline). The block layer always 463 * automatically select such an elevator for zoned block devices during the 464 * device initialization. 465 */ 466 static ssize_t zonefs_file_dio_write(struct kiocb *iocb, struct iov_iter *from) 467 { 468 struct inode *inode = file_inode(iocb->ki_filp); 469 struct zonefs_inode_info *zi = ZONEFS_I(inode); 470 struct zonefs_zone *z = zonefs_inode_zone(inode); 471 struct super_block *sb = inode->i_sb; 472 ssize_t ret, count; 473 474 /* 475 * For async direct IOs to sequential zone files, refuse IOCB_NOWAIT 476 * as this can cause write reordering (e.g. the first aio gets EAGAIN 477 * on the inode lock but the second goes through but is now unaligned). 478 */ 479 if (zonefs_zone_is_seq(z) && !is_sync_kiocb(iocb) && 480 (iocb->ki_flags & IOCB_NOWAIT)) 481 return -EOPNOTSUPP; 482 483 if (iocb->ki_flags & IOCB_NOWAIT) { 484 if (!inode_trylock(inode)) 485 return -EAGAIN; 486 } else { 487 inode_lock(inode); 488 } 489 490 count = zonefs_write_checks(iocb, from); 491 if (count <= 0) { 492 ret = count; 493 goto inode_unlock; 494 } 495 496 if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) { 497 ret = -EINVAL; 498 goto inode_unlock; 499 } 500 501 /* Enforce sequential writes (append only) in sequential zones */ 502 if (zonefs_zone_is_seq(z)) { 503 mutex_lock(&zi->i_truncate_mutex); 504 if (iocb->ki_pos != z->z_wpoffset) { 505 mutex_unlock(&zi->i_truncate_mutex); 506 ret = -EINVAL; 507 goto inode_unlock; 508 } 509 /* 510 * Advance the zone write pointer offset. This assumes that the 511 * IO will succeed, which is OK to do because we do not allow 512 * partial writes (IOMAP_DIO_PARTIAL is not set) and if the IO 513 * fails, the error path will correct the write pointer offset. 514 */ 515 z->z_wpoffset += count; 516 zonefs_inode_account_active(inode); 517 mutex_unlock(&zi->i_truncate_mutex); 518 } 519 520 /* 521 * iomap_dio_rw() may return ENOTBLK if there was an issue with 522 * page invalidation. Overwrite that error code with EBUSY so that 523 * the user can make sense of the error. 524 */ 525 ret = iomap_dio_rw(iocb, from, &zonefs_write_iomap_ops, 526 &zonefs_write_dio_ops, 0, NULL, 0); 527 if (ret == -ENOTBLK) 528 ret = -EBUSY; 529 530 /* 531 * For a failed IO or partial completion, trigger error recovery 532 * to update the zone write pointer offset to a correct value. 533 * For asynchronous IOs, zonefs_file_write_dio_end_io() may already 534 * have executed error recovery if the IO already completed when we 535 * reach here. However, we cannot know that and execute error recovery 536 * again (that will not change anything). 537 */ 538 if (zonefs_zone_is_seq(z)) { 539 if (ret > 0 && ret != count) 540 ret = -EIO; 541 if (ret < 0 && ret != -EIOCBQUEUED) 542 zonefs_io_error(inode, true); 543 } 544 545 inode_unlock: 546 inode_unlock(inode); 547 548 return ret; 549 } 550 551 static ssize_t zonefs_file_buffered_write(struct kiocb *iocb, 552 struct iov_iter *from) 553 { 554 struct inode *inode = file_inode(iocb->ki_filp); 555 ssize_t ret; 556 557 /* 558 * Direct IO writes are mandatory for sequential zone files so that the 559 * write IO issuing order is preserved. 560 */ 561 if (zonefs_inode_is_seq(inode)) 562 return -EIO; 563 564 if (iocb->ki_flags & IOCB_NOWAIT) { 565 if (!inode_trylock(inode)) 566 return -EAGAIN; 567 } else { 568 inode_lock(inode); 569 } 570 571 ret = zonefs_write_checks(iocb, from); 572 if (ret <= 0) 573 goto inode_unlock; 574 575 ret = iomap_file_buffered_write(iocb, from, &zonefs_write_iomap_ops, 576 NULL, NULL); 577 if (ret == -EIO) 578 zonefs_io_error(inode, true); 579 580 inode_unlock: 581 inode_unlock(inode); 582 if (ret > 0) 583 ret = generic_write_sync(iocb, ret); 584 585 return ret; 586 } 587 588 static ssize_t zonefs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) 589 { 590 struct inode *inode = file_inode(iocb->ki_filp); 591 struct zonefs_zone *z = zonefs_inode_zone(inode); 592 593 if (unlikely(IS_IMMUTABLE(inode))) 594 return -EPERM; 595 596 if (sb_rdonly(inode->i_sb)) 597 return -EROFS; 598 599 /* Write operations beyond the zone capacity are not allowed */ 600 if (iocb->ki_pos >= z->z_capacity) 601 return -EFBIG; 602 603 if (iocb->ki_flags & IOCB_DIRECT) { 604 ssize_t ret = zonefs_file_dio_write(iocb, from); 605 606 if (ret != -ENOTBLK) 607 return ret; 608 } 609 610 return zonefs_file_buffered_write(iocb, from); 611 } 612 613 static int zonefs_file_read_dio_end_io(struct kiocb *iocb, ssize_t size, 614 int error, unsigned int flags) 615 { 616 if (error) { 617 zonefs_io_error(file_inode(iocb->ki_filp), false); 618 return error; 619 } 620 621 return 0; 622 } 623 624 static const struct iomap_dio_ops zonefs_read_dio_ops = { 625 .end_io = zonefs_file_read_dio_end_io, 626 }; 627 628 static ssize_t zonefs_file_read_iter(struct kiocb *iocb, struct iov_iter *to) 629 { 630 struct inode *inode = file_inode(iocb->ki_filp); 631 struct zonefs_inode_info *zi = ZONEFS_I(inode); 632 struct zonefs_zone *z = zonefs_inode_zone(inode); 633 struct super_block *sb = inode->i_sb; 634 loff_t isize; 635 ssize_t ret; 636 637 /* Offline zones cannot be read */ 638 if (unlikely(IS_IMMUTABLE(inode) && !(inode->i_mode & 0777))) 639 return -EPERM; 640 641 if (iocb->ki_pos >= z->z_capacity) 642 return 0; 643 644 if (iocb->ki_flags & IOCB_NOWAIT) { 645 if (!inode_trylock_shared(inode)) 646 return -EAGAIN; 647 } else { 648 inode_lock_shared(inode); 649 } 650 651 /* Limit read operations to written data */ 652 mutex_lock(&zi->i_truncate_mutex); 653 isize = i_size_read(inode); 654 if (iocb->ki_pos >= isize) { 655 mutex_unlock(&zi->i_truncate_mutex); 656 ret = 0; 657 goto inode_unlock; 658 } 659 iov_iter_truncate(to, isize - iocb->ki_pos); 660 mutex_unlock(&zi->i_truncate_mutex); 661 662 if (iocb->ki_flags & IOCB_DIRECT) { 663 size_t count = iov_iter_count(to); 664 665 if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) { 666 ret = -EINVAL; 667 goto inode_unlock; 668 } 669 file_accessed(iocb->ki_filp); 670 ret = iomap_dio_rw(iocb, to, &zonefs_read_iomap_ops, 671 &zonefs_read_dio_ops, 0, NULL, 0); 672 } else { 673 ret = generic_file_read_iter(iocb, to); 674 if (ret == -EIO) 675 zonefs_io_error(inode, false); 676 } 677 678 inode_unlock: 679 inode_unlock_shared(inode); 680 681 return ret; 682 } 683 684 static ssize_t zonefs_file_splice_read(struct file *in, loff_t *ppos, 685 struct pipe_inode_info *pipe, 686 size_t len, unsigned int flags) 687 { 688 struct inode *inode = file_inode(in); 689 struct zonefs_inode_info *zi = ZONEFS_I(inode); 690 struct zonefs_zone *z = zonefs_inode_zone(inode); 691 loff_t isize; 692 ssize_t ret = 0; 693 694 /* Offline zones cannot be read */ 695 if (unlikely(IS_IMMUTABLE(inode) && !(inode->i_mode & 0777))) 696 return -EPERM; 697 698 if (*ppos >= z->z_capacity) 699 return 0; 700 701 inode_lock_shared(inode); 702 703 /* Limit read operations to written data */ 704 mutex_lock(&zi->i_truncate_mutex); 705 isize = i_size_read(inode); 706 if (*ppos >= isize) 707 len = 0; 708 else 709 len = min_t(loff_t, len, isize - *ppos); 710 mutex_unlock(&zi->i_truncate_mutex); 711 712 if (len > 0) { 713 ret = filemap_splice_read(in, ppos, pipe, len, flags); 714 if (ret == -EIO) 715 zonefs_io_error(inode, false); 716 } 717 718 inode_unlock_shared(inode); 719 return ret; 720 } 721 722 /* 723 * Write open accounting is done only for sequential files. 724 */ 725 static inline bool zonefs_seq_file_need_wro(struct inode *inode, 726 struct file *file) 727 { 728 if (zonefs_inode_is_cnv(inode)) 729 return false; 730 731 if (!(file->f_mode & FMODE_WRITE)) 732 return false; 733 734 return true; 735 } 736 737 static int zonefs_seq_file_write_open(struct inode *inode) 738 { 739 struct zonefs_inode_info *zi = ZONEFS_I(inode); 740 struct zonefs_zone *z = zonefs_inode_zone(inode); 741 int ret = 0; 742 743 mutex_lock(&zi->i_truncate_mutex); 744 745 if (!zi->i_wr_refcnt) { 746 struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb); 747 unsigned int wro = atomic_inc_return(&sbi->s_wro_seq_files); 748 749 if (sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) { 750 751 if (sbi->s_max_wro_seq_files 752 && wro > sbi->s_max_wro_seq_files) { 753 atomic_dec(&sbi->s_wro_seq_files); 754 ret = -EBUSY; 755 goto unlock; 756 } 757 758 if (i_size_read(inode) < z->z_capacity) { 759 ret = zonefs_inode_zone_mgmt(inode, 760 REQ_OP_ZONE_OPEN); 761 if (ret) { 762 atomic_dec(&sbi->s_wro_seq_files); 763 goto unlock; 764 } 765 z->z_flags |= ZONEFS_ZONE_OPEN; 766 zonefs_inode_account_active(inode); 767 } 768 } 769 } 770 771 zi->i_wr_refcnt++; 772 773 unlock: 774 mutex_unlock(&zi->i_truncate_mutex); 775 776 return ret; 777 } 778 779 static int zonefs_file_open(struct inode *inode, struct file *file) 780 { 781 int ret; 782 783 file->f_mode |= FMODE_CAN_ODIRECT; 784 ret = generic_file_open(inode, file); 785 if (ret) 786 return ret; 787 788 if (zonefs_seq_file_need_wro(inode, file)) 789 return zonefs_seq_file_write_open(inode); 790 791 return 0; 792 } 793 794 static void zonefs_seq_file_write_close(struct inode *inode) 795 { 796 struct zonefs_inode_info *zi = ZONEFS_I(inode); 797 struct zonefs_zone *z = zonefs_inode_zone(inode); 798 struct super_block *sb = inode->i_sb; 799 struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 800 int ret = 0; 801 802 mutex_lock(&zi->i_truncate_mutex); 803 804 zi->i_wr_refcnt--; 805 if (zi->i_wr_refcnt) 806 goto unlock; 807 808 /* 809 * The file zone may not be open anymore (e.g. the file was truncated to 810 * its maximum size or it was fully written). For this case, we only 811 * need to decrement the write open count. 812 */ 813 if (z->z_flags & ZONEFS_ZONE_OPEN) { 814 ret = zonefs_inode_zone_mgmt(inode, REQ_OP_ZONE_CLOSE); 815 if (ret) { 816 __zonefs_io_error(inode, false); 817 /* 818 * Leaving zones explicitly open may lead to a state 819 * where most zones cannot be written (zone resources 820 * exhausted). So take preventive action by remounting 821 * read-only. 822 */ 823 if (z->z_flags & ZONEFS_ZONE_OPEN && 824 !(sb->s_flags & SB_RDONLY)) { 825 zonefs_warn(sb, 826 "closing zone at %llu failed %d\n", 827 z->z_sector, ret); 828 zonefs_warn(sb, 829 "remounting filesystem read-only\n"); 830 sb->s_flags |= SB_RDONLY; 831 } 832 goto unlock; 833 } 834 835 z->z_flags &= ~ZONEFS_ZONE_OPEN; 836 zonefs_inode_account_active(inode); 837 } 838 839 atomic_dec(&sbi->s_wro_seq_files); 840 841 unlock: 842 mutex_unlock(&zi->i_truncate_mutex); 843 } 844 845 static int zonefs_file_release(struct inode *inode, struct file *file) 846 { 847 /* 848 * If we explicitly open a zone we must close it again as well, but the 849 * zone management operation can fail (either due to an IO error or as 850 * the zone has gone offline or read-only). Make sure we don't fail the 851 * close(2) for user-space. 852 */ 853 if (zonefs_seq_file_need_wro(inode, file)) 854 zonefs_seq_file_write_close(inode); 855 856 return 0; 857 } 858 859 const struct file_operations zonefs_file_operations = { 860 .open = zonefs_file_open, 861 .release = zonefs_file_release, 862 .fsync = zonefs_file_fsync, 863 .mmap = zonefs_file_mmap, 864 .llseek = zonefs_file_llseek, 865 .read_iter = zonefs_file_read_iter, 866 .write_iter = zonefs_file_write_iter, 867 .splice_read = zonefs_file_splice_read, 868 .splice_write = iter_file_splice_write, 869 .iopoll = iocb_bio_iopoll, 870 }; 871