1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Simple file system for zoned block devices exposing zones as files. 4 * 5 * Copyright (C) 2019 Western Digital Corporation or its affiliates. 6 */ 7 #include <linux/module.h> 8 #include <linux/pagemap.h> 9 #include <linux/magic.h> 10 #include <linux/iomap.h> 11 #include <linux/init.h> 12 #include <linux/slab.h> 13 #include <linux/blkdev.h> 14 #include <linux/statfs.h> 15 #include <linux/writeback.h> 16 #include <linux/quotaops.h> 17 #include <linux/seq_file.h> 18 #include <linux/parser.h> 19 #include <linux/uio.h> 20 #include <linux/mman.h> 21 #include <linux/sched/mm.h> 22 #include <linux/crc32.h> 23 #include <linux/task_io_accounting_ops.h> 24 25 #include "zonefs.h" 26 27 #define CREATE_TRACE_POINTS 28 #include "trace.h" 29 30 /* 31 * Manage the active zone count. Called with zi->i_truncate_mutex held. 32 */ 33 static void zonefs_account_active(struct inode *inode) 34 { 35 struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb); 36 struct zonefs_inode_info *zi = ZONEFS_I(inode); 37 38 lockdep_assert_held(&zi->i_truncate_mutex); 39 40 if (zi->i_ztype != ZONEFS_ZTYPE_SEQ) 41 return; 42 43 /* 44 * If the zone is active, that is, if it is explicitly open or 45 * partially written, check if it was already accounted as active. 46 */ 47 if ((zi->i_flags & ZONEFS_ZONE_OPEN) || 48 (zi->i_wpoffset > 0 && zi->i_wpoffset < zi->i_max_size)) { 49 if (!(zi->i_flags & ZONEFS_ZONE_ACTIVE)) { 50 zi->i_flags |= ZONEFS_ZONE_ACTIVE; 51 atomic_inc(&sbi->s_active_seq_files); 52 } 53 return; 54 } 55 56 /* The zone is not active. If it was, update the active count */ 57 if (zi->i_flags & ZONEFS_ZONE_ACTIVE) { 58 zi->i_flags &= ~ZONEFS_ZONE_ACTIVE; 59 atomic_dec(&sbi->s_active_seq_files); 60 } 61 } 62 63 static inline int zonefs_zone_mgmt(struct inode *inode, enum req_op op) 64 { 65 struct zonefs_inode_info *zi = ZONEFS_I(inode); 66 int ret; 67 68 lockdep_assert_held(&zi->i_truncate_mutex); 69 70 /* 71 * With ZNS drives, closing an explicitly open zone that has not been 72 * written will change the zone state to "closed", that is, the zone 73 * will remain active. Since this can then cause failure of explicit 74 * open operation on other zones if the drive active zone resources 75 * are exceeded, make sure that the zone does not remain active by 76 * resetting it. 77 */ 78 if (op == REQ_OP_ZONE_CLOSE && !zi->i_wpoffset) 79 op = REQ_OP_ZONE_RESET; 80 81 trace_zonefs_zone_mgmt(inode, op); 82 ret = blkdev_zone_mgmt(inode->i_sb->s_bdev, op, zi->i_zsector, 83 zi->i_zone_size >> SECTOR_SHIFT, GFP_NOFS); 84 if (ret) { 85 zonefs_err(inode->i_sb, 86 "Zone management operation %s at %llu failed %d\n", 87 blk_op_str(op), zi->i_zsector, ret); 88 return ret; 89 } 90 91 return 0; 92 } 93 94 static inline void zonefs_i_size_write(struct inode *inode, loff_t isize) 95 { 96 struct zonefs_inode_info *zi = ZONEFS_I(inode); 97 98 i_size_write(inode, isize); 99 /* 100 * A full zone is no longer open/active and does not need 101 * explicit closing. 102 */ 103 if (isize >= zi->i_max_size) { 104 struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb); 105 106 if (zi->i_flags & ZONEFS_ZONE_ACTIVE) 107 atomic_dec(&sbi->s_active_seq_files); 108 zi->i_flags &= ~(ZONEFS_ZONE_OPEN | ZONEFS_ZONE_ACTIVE); 109 } 110 } 111 112 static int zonefs_read_iomap_begin(struct inode *inode, loff_t offset, 113 loff_t length, unsigned int flags, 114 struct iomap *iomap, struct iomap *srcmap) 115 { 116 struct zonefs_inode_info *zi = ZONEFS_I(inode); 117 struct super_block *sb = inode->i_sb; 118 loff_t isize; 119 120 /* 121 * All blocks are always mapped below EOF. If reading past EOF, 122 * act as if there is a hole up to the file maximum size. 123 */ 124 mutex_lock(&zi->i_truncate_mutex); 125 iomap->bdev = inode->i_sb->s_bdev; 126 iomap->offset = ALIGN_DOWN(offset, sb->s_blocksize); 127 isize = i_size_read(inode); 128 if (iomap->offset >= isize) { 129 iomap->type = IOMAP_HOLE; 130 iomap->addr = IOMAP_NULL_ADDR; 131 iomap->length = length; 132 } else { 133 iomap->type = IOMAP_MAPPED; 134 iomap->addr = (zi->i_zsector << SECTOR_SHIFT) + iomap->offset; 135 iomap->length = isize - iomap->offset; 136 } 137 mutex_unlock(&zi->i_truncate_mutex); 138 139 trace_zonefs_iomap_begin(inode, iomap); 140 141 return 0; 142 } 143 144 static const struct iomap_ops zonefs_read_iomap_ops = { 145 .iomap_begin = zonefs_read_iomap_begin, 146 }; 147 148 static int zonefs_write_iomap_begin(struct inode *inode, loff_t offset, 149 loff_t length, unsigned int flags, 150 struct iomap *iomap, struct iomap *srcmap) 151 { 152 struct zonefs_inode_info *zi = ZONEFS_I(inode); 153 struct super_block *sb = inode->i_sb; 154 loff_t isize; 155 156 /* All write I/Os should always be within the file maximum size */ 157 if (WARN_ON_ONCE(offset + length > zi->i_max_size)) 158 return -EIO; 159 160 /* 161 * Sequential zones can only accept direct writes. This is already 162 * checked when writes are issued, so warn if we see a page writeback 163 * operation. 164 */ 165 if (WARN_ON_ONCE(zi->i_ztype == ZONEFS_ZTYPE_SEQ && 166 !(flags & IOMAP_DIRECT))) 167 return -EIO; 168 169 /* 170 * For conventional zones, all blocks are always mapped. For sequential 171 * zones, all blocks after always mapped below the inode size (zone 172 * write pointer) and unwriten beyond. 173 */ 174 mutex_lock(&zi->i_truncate_mutex); 175 iomap->bdev = inode->i_sb->s_bdev; 176 iomap->offset = ALIGN_DOWN(offset, sb->s_blocksize); 177 iomap->addr = (zi->i_zsector << SECTOR_SHIFT) + iomap->offset; 178 isize = i_size_read(inode); 179 if (iomap->offset >= isize) { 180 iomap->type = IOMAP_UNWRITTEN; 181 iomap->length = zi->i_max_size - iomap->offset; 182 } else { 183 iomap->type = IOMAP_MAPPED; 184 iomap->length = isize - iomap->offset; 185 } 186 mutex_unlock(&zi->i_truncate_mutex); 187 188 trace_zonefs_iomap_begin(inode, iomap); 189 190 return 0; 191 } 192 193 static const struct iomap_ops zonefs_write_iomap_ops = { 194 .iomap_begin = zonefs_write_iomap_begin, 195 }; 196 197 static int zonefs_read_folio(struct file *unused, struct folio *folio) 198 { 199 return iomap_read_folio(folio, &zonefs_read_iomap_ops); 200 } 201 202 static void zonefs_readahead(struct readahead_control *rac) 203 { 204 iomap_readahead(rac, &zonefs_read_iomap_ops); 205 } 206 207 /* 208 * Map blocks for page writeback. This is used only on conventional zone files, 209 * which implies that the page range can only be within the fixed inode size. 210 */ 211 static int zonefs_write_map_blocks(struct iomap_writepage_ctx *wpc, 212 struct inode *inode, loff_t offset) 213 { 214 struct zonefs_inode_info *zi = ZONEFS_I(inode); 215 216 if (WARN_ON_ONCE(zi->i_ztype != ZONEFS_ZTYPE_CNV)) 217 return -EIO; 218 if (WARN_ON_ONCE(offset >= i_size_read(inode))) 219 return -EIO; 220 221 /* If the mapping is already OK, nothing needs to be done */ 222 if (offset >= wpc->iomap.offset && 223 offset < wpc->iomap.offset + wpc->iomap.length) 224 return 0; 225 226 return zonefs_write_iomap_begin(inode, offset, zi->i_max_size - offset, 227 IOMAP_WRITE, &wpc->iomap, NULL); 228 } 229 230 static const struct iomap_writeback_ops zonefs_writeback_ops = { 231 .map_blocks = zonefs_write_map_blocks, 232 }; 233 234 static int zonefs_writepages(struct address_space *mapping, 235 struct writeback_control *wbc) 236 { 237 struct iomap_writepage_ctx wpc = { }; 238 239 return iomap_writepages(mapping, wbc, &wpc, &zonefs_writeback_ops); 240 } 241 242 static int zonefs_swap_activate(struct swap_info_struct *sis, 243 struct file *swap_file, sector_t *span) 244 { 245 struct inode *inode = file_inode(swap_file); 246 struct zonefs_inode_info *zi = ZONEFS_I(inode); 247 248 if (zi->i_ztype != ZONEFS_ZTYPE_CNV) { 249 zonefs_err(inode->i_sb, 250 "swap file: not a conventional zone file\n"); 251 return -EINVAL; 252 } 253 254 return iomap_swapfile_activate(sis, swap_file, span, 255 &zonefs_read_iomap_ops); 256 } 257 258 static const struct address_space_operations zonefs_file_aops = { 259 .read_folio = zonefs_read_folio, 260 .readahead = zonefs_readahead, 261 .writepages = zonefs_writepages, 262 .dirty_folio = filemap_dirty_folio, 263 .release_folio = iomap_release_folio, 264 .invalidate_folio = iomap_invalidate_folio, 265 .migrate_folio = filemap_migrate_folio, 266 .is_partially_uptodate = iomap_is_partially_uptodate, 267 .error_remove_page = generic_error_remove_page, 268 .direct_IO = noop_direct_IO, 269 .swap_activate = zonefs_swap_activate, 270 }; 271 272 static void zonefs_update_stats(struct inode *inode, loff_t new_isize) 273 { 274 struct super_block *sb = inode->i_sb; 275 struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 276 loff_t old_isize = i_size_read(inode); 277 loff_t nr_blocks; 278 279 if (new_isize == old_isize) 280 return; 281 282 spin_lock(&sbi->s_lock); 283 284 /* 285 * This may be called for an update after an IO error. 286 * So beware of the values seen. 287 */ 288 if (new_isize < old_isize) { 289 nr_blocks = (old_isize - new_isize) >> sb->s_blocksize_bits; 290 if (sbi->s_used_blocks > nr_blocks) 291 sbi->s_used_blocks -= nr_blocks; 292 else 293 sbi->s_used_blocks = 0; 294 } else { 295 sbi->s_used_blocks += 296 (new_isize - old_isize) >> sb->s_blocksize_bits; 297 if (sbi->s_used_blocks > sbi->s_blocks) 298 sbi->s_used_blocks = sbi->s_blocks; 299 } 300 301 spin_unlock(&sbi->s_lock); 302 } 303 304 /* 305 * Check a zone condition and adjust its file inode access permissions for 306 * offline and readonly zones. Return the inode size corresponding to the 307 * amount of readable data in the zone. 308 */ 309 static loff_t zonefs_check_zone_condition(struct inode *inode, 310 struct blk_zone *zone, bool warn, 311 bool mount) 312 { 313 struct zonefs_inode_info *zi = ZONEFS_I(inode); 314 315 switch (zone->cond) { 316 case BLK_ZONE_COND_OFFLINE: 317 /* 318 * Dead zone: make the inode immutable, disable all accesses 319 * and set the file size to 0 (zone wp set to zone start). 320 */ 321 if (warn) 322 zonefs_warn(inode->i_sb, "inode %lu: offline zone\n", 323 inode->i_ino); 324 inode->i_flags |= S_IMMUTABLE; 325 inode->i_mode &= ~0777; 326 zone->wp = zone->start; 327 return 0; 328 case BLK_ZONE_COND_READONLY: 329 /* 330 * The write pointer of read-only zones is invalid. If such a 331 * zone is found during mount, the file size cannot be retrieved 332 * so we treat the zone as offline (mount == true case). 333 * Otherwise, keep the file size as it was when last updated 334 * so that the user can recover data. In both cases, writes are 335 * always disabled for the zone. 336 */ 337 if (warn) 338 zonefs_warn(inode->i_sb, "inode %lu: read-only zone\n", 339 inode->i_ino); 340 inode->i_flags |= S_IMMUTABLE; 341 if (mount) { 342 zone->cond = BLK_ZONE_COND_OFFLINE; 343 inode->i_mode &= ~0777; 344 zone->wp = zone->start; 345 return 0; 346 } 347 inode->i_mode &= ~0222; 348 return i_size_read(inode); 349 case BLK_ZONE_COND_FULL: 350 /* The write pointer of full zones is invalid. */ 351 return zi->i_max_size; 352 default: 353 if (zi->i_ztype == ZONEFS_ZTYPE_CNV) 354 return zi->i_max_size; 355 return (zone->wp - zone->start) << SECTOR_SHIFT; 356 } 357 } 358 359 struct zonefs_ioerr_data { 360 struct inode *inode; 361 bool write; 362 }; 363 364 static int zonefs_io_error_cb(struct blk_zone *zone, unsigned int idx, 365 void *data) 366 { 367 struct zonefs_ioerr_data *err = data; 368 struct inode *inode = err->inode; 369 struct zonefs_inode_info *zi = ZONEFS_I(inode); 370 struct super_block *sb = inode->i_sb; 371 struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 372 loff_t isize, data_size; 373 374 /* 375 * Check the zone condition: if the zone is not "bad" (offline or 376 * read-only), read errors are simply signaled to the IO issuer as long 377 * as there is no inconsistency between the inode size and the amount of 378 * data writen in the zone (data_size). 379 */ 380 data_size = zonefs_check_zone_condition(inode, zone, true, false); 381 isize = i_size_read(inode); 382 if (zone->cond != BLK_ZONE_COND_OFFLINE && 383 zone->cond != BLK_ZONE_COND_READONLY && 384 !err->write && isize == data_size) 385 return 0; 386 387 /* 388 * At this point, we detected either a bad zone or an inconsistency 389 * between the inode size and the amount of data written in the zone. 390 * For the latter case, the cause may be a write IO error or an external 391 * action on the device. Two error patterns exist: 392 * 1) The inode size is lower than the amount of data in the zone: 393 * a write operation partially failed and data was writen at the end 394 * of the file. This can happen in the case of a large direct IO 395 * needing several BIOs and/or write requests to be processed. 396 * 2) The inode size is larger than the amount of data in the zone: 397 * this can happen with a deferred write error with the use of the 398 * device side write cache after getting successful write IO 399 * completions. Other possibilities are (a) an external corruption, 400 * e.g. an application reset the zone directly, or (b) the device 401 * has a serious problem (e.g. firmware bug). 402 * 403 * In all cases, warn about inode size inconsistency and handle the 404 * IO error according to the zone condition and to the mount options. 405 */ 406 if (zi->i_ztype == ZONEFS_ZTYPE_SEQ && isize != data_size) 407 zonefs_warn(sb, "inode %lu: invalid size %lld (should be %lld)\n", 408 inode->i_ino, isize, data_size); 409 410 /* 411 * First handle bad zones signaled by hardware. The mount options 412 * errors=zone-ro and errors=zone-offline result in changing the 413 * zone condition to read-only and offline respectively, as if the 414 * condition was signaled by the hardware. 415 */ 416 if (zone->cond == BLK_ZONE_COND_OFFLINE || 417 sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL) { 418 zonefs_warn(sb, "inode %lu: read/write access disabled\n", 419 inode->i_ino); 420 if (zone->cond != BLK_ZONE_COND_OFFLINE) { 421 zone->cond = BLK_ZONE_COND_OFFLINE; 422 data_size = zonefs_check_zone_condition(inode, zone, 423 false, false); 424 } 425 } else if (zone->cond == BLK_ZONE_COND_READONLY || 426 sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO) { 427 zonefs_warn(sb, "inode %lu: write access disabled\n", 428 inode->i_ino); 429 if (zone->cond != BLK_ZONE_COND_READONLY) { 430 zone->cond = BLK_ZONE_COND_READONLY; 431 data_size = zonefs_check_zone_condition(inode, zone, 432 false, false); 433 } 434 } 435 436 /* 437 * If the filesystem is mounted with the explicit-open mount option, we 438 * need to clear the ZONEFS_ZONE_OPEN flag if the zone transitioned to 439 * the read-only or offline condition, to avoid attempting an explicit 440 * close of the zone when the inode file is closed. 441 */ 442 if ((sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) && 443 (zone->cond == BLK_ZONE_COND_OFFLINE || 444 zone->cond == BLK_ZONE_COND_READONLY)) 445 zi->i_flags &= ~ZONEFS_ZONE_OPEN; 446 447 /* 448 * If error=remount-ro was specified, any error result in remounting 449 * the volume as read-only. 450 */ 451 if ((sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO) && !sb_rdonly(sb)) { 452 zonefs_warn(sb, "remounting filesystem read-only\n"); 453 sb->s_flags |= SB_RDONLY; 454 } 455 456 /* 457 * Update block usage stats and the inode size to prevent access to 458 * invalid data. 459 */ 460 zonefs_update_stats(inode, data_size); 461 zonefs_i_size_write(inode, data_size); 462 zi->i_wpoffset = data_size; 463 zonefs_account_active(inode); 464 465 return 0; 466 } 467 468 /* 469 * When an file IO error occurs, check the file zone to see if there is a change 470 * in the zone condition (e.g. offline or read-only). For a failed write to a 471 * sequential zone, the zone write pointer position must also be checked to 472 * eventually correct the file size and zonefs inode write pointer offset 473 * (which can be out of sync with the drive due to partial write failures). 474 */ 475 static void __zonefs_io_error(struct inode *inode, bool write) 476 { 477 struct zonefs_inode_info *zi = ZONEFS_I(inode); 478 struct super_block *sb = inode->i_sb; 479 struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 480 unsigned int noio_flag; 481 unsigned int nr_zones = 1; 482 struct zonefs_ioerr_data err = { 483 .inode = inode, 484 .write = write, 485 }; 486 int ret; 487 488 /* 489 * The only files that have more than one zone are conventional zone 490 * files with aggregated conventional zones, for which the inode zone 491 * size is always larger than the device zone size. 492 */ 493 if (zi->i_zone_size > bdev_zone_sectors(sb->s_bdev)) 494 nr_zones = zi->i_zone_size >> 495 (sbi->s_zone_sectors_shift + SECTOR_SHIFT); 496 497 /* 498 * Memory allocations in blkdev_report_zones() can trigger a memory 499 * reclaim which may in turn cause a recursion into zonefs as well as 500 * struct request allocations for the same device. The former case may 501 * end up in a deadlock on the inode truncate mutex, while the latter 502 * may prevent IO forward progress. Executing the report zones under 503 * the GFP_NOIO context avoids both problems. 504 */ 505 noio_flag = memalloc_noio_save(); 506 ret = blkdev_report_zones(sb->s_bdev, zi->i_zsector, nr_zones, 507 zonefs_io_error_cb, &err); 508 if (ret != nr_zones) 509 zonefs_err(sb, "Get inode %lu zone information failed %d\n", 510 inode->i_ino, ret); 511 memalloc_noio_restore(noio_flag); 512 } 513 514 static void zonefs_io_error(struct inode *inode, bool write) 515 { 516 struct zonefs_inode_info *zi = ZONEFS_I(inode); 517 518 mutex_lock(&zi->i_truncate_mutex); 519 __zonefs_io_error(inode, write); 520 mutex_unlock(&zi->i_truncate_mutex); 521 } 522 523 static int zonefs_file_truncate(struct inode *inode, loff_t isize) 524 { 525 struct zonefs_inode_info *zi = ZONEFS_I(inode); 526 loff_t old_isize; 527 enum req_op op; 528 int ret = 0; 529 530 /* 531 * Only sequential zone files can be truncated and truncation is allowed 532 * only down to a 0 size, which is equivalent to a zone reset, and to 533 * the maximum file size, which is equivalent to a zone finish. 534 */ 535 if (zi->i_ztype != ZONEFS_ZTYPE_SEQ) 536 return -EPERM; 537 538 if (!isize) 539 op = REQ_OP_ZONE_RESET; 540 else if (isize == zi->i_max_size) 541 op = REQ_OP_ZONE_FINISH; 542 else 543 return -EPERM; 544 545 inode_dio_wait(inode); 546 547 /* Serialize against page faults */ 548 filemap_invalidate_lock(inode->i_mapping); 549 550 /* Serialize against zonefs_iomap_begin() */ 551 mutex_lock(&zi->i_truncate_mutex); 552 553 old_isize = i_size_read(inode); 554 if (isize == old_isize) 555 goto unlock; 556 557 ret = zonefs_zone_mgmt(inode, op); 558 if (ret) 559 goto unlock; 560 561 /* 562 * If the mount option ZONEFS_MNTOPT_EXPLICIT_OPEN is set, 563 * take care of open zones. 564 */ 565 if (zi->i_flags & ZONEFS_ZONE_OPEN) { 566 /* 567 * Truncating a zone to EMPTY or FULL is the equivalent of 568 * closing the zone. For a truncation to 0, we need to 569 * re-open the zone to ensure new writes can be processed. 570 * For a truncation to the maximum file size, the zone is 571 * closed and writes cannot be accepted anymore, so clear 572 * the open flag. 573 */ 574 if (!isize) 575 ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_OPEN); 576 else 577 zi->i_flags &= ~ZONEFS_ZONE_OPEN; 578 } 579 580 zonefs_update_stats(inode, isize); 581 truncate_setsize(inode, isize); 582 zi->i_wpoffset = isize; 583 zonefs_account_active(inode); 584 585 unlock: 586 mutex_unlock(&zi->i_truncate_mutex); 587 filemap_invalidate_unlock(inode->i_mapping); 588 589 return ret; 590 } 591 592 static int zonefs_inode_setattr(struct user_namespace *mnt_userns, 593 struct dentry *dentry, struct iattr *iattr) 594 { 595 struct inode *inode = d_inode(dentry); 596 int ret; 597 598 if (unlikely(IS_IMMUTABLE(inode))) 599 return -EPERM; 600 601 ret = setattr_prepare(&init_user_ns, dentry, iattr); 602 if (ret) 603 return ret; 604 605 /* 606 * Since files and directories cannot be created nor deleted, do not 607 * allow setting any write attributes on the sub-directories grouping 608 * files by zone type. 609 */ 610 if ((iattr->ia_valid & ATTR_MODE) && S_ISDIR(inode->i_mode) && 611 (iattr->ia_mode & 0222)) 612 return -EPERM; 613 614 if (((iattr->ia_valid & ATTR_UID) && 615 !uid_eq(iattr->ia_uid, inode->i_uid)) || 616 ((iattr->ia_valid & ATTR_GID) && 617 !gid_eq(iattr->ia_gid, inode->i_gid))) { 618 ret = dquot_transfer(mnt_userns, inode, iattr); 619 if (ret) 620 return ret; 621 } 622 623 if (iattr->ia_valid & ATTR_SIZE) { 624 ret = zonefs_file_truncate(inode, iattr->ia_size); 625 if (ret) 626 return ret; 627 } 628 629 setattr_copy(&init_user_ns, inode, iattr); 630 631 return 0; 632 } 633 634 static const struct inode_operations zonefs_file_inode_operations = { 635 .setattr = zonefs_inode_setattr, 636 }; 637 638 static int zonefs_file_fsync(struct file *file, loff_t start, loff_t end, 639 int datasync) 640 { 641 struct inode *inode = file_inode(file); 642 int ret = 0; 643 644 if (unlikely(IS_IMMUTABLE(inode))) 645 return -EPERM; 646 647 /* 648 * Since only direct writes are allowed in sequential files, page cache 649 * flush is needed only for conventional zone files. 650 */ 651 if (ZONEFS_I(inode)->i_ztype == ZONEFS_ZTYPE_CNV) 652 ret = file_write_and_wait_range(file, start, end); 653 if (!ret) 654 ret = blkdev_issue_flush(inode->i_sb->s_bdev); 655 656 if (ret) 657 zonefs_io_error(inode, true); 658 659 return ret; 660 } 661 662 static vm_fault_t zonefs_filemap_page_mkwrite(struct vm_fault *vmf) 663 { 664 struct inode *inode = file_inode(vmf->vma->vm_file); 665 struct zonefs_inode_info *zi = ZONEFS_I(inode); 666 vm_fault_t ret; 667 668 if (unlikely(IS_IMMUTABLE(inode))) 669 return VM_FAULT_SIGBUS; 670 671 /* 672 * Sanity check: only conventional zone files can have shared 673 * writeable mappings. 674 */ 675 if (WARN_ON_ONCE(zi->i_ztype != ZONEFS_ZTYPE_CNV)) 676 return VM_FAULT_NOPAGE; 677 678 sb_start_pagefault(inode->i_sb); 679 file_update_time(vmf->vma->vm_file); 680 681 /* Serialize against truncates */ 682 filemap_invalidate_lock_shared(inode->i_mapping); 683 ret = iomap_page_mkwrite(vmf, &zonefs_write_iomap_ops); 684 filemap_invalidate_unlock_shared(inode->i_mapping); 685 686 sb_end_pagefault(inode->i_sb); 687 return ret; 688 } 689 690 static const struct vm_operations_struct zonefs_file_vm_ops = { 691 .fault = filemap_fault, 692 .map_pages = filemap_map_pages, 693 .page_mkwrite = zonefs_filemap_page_mkwrite, 694 }; 695 696 static int zonefs_file_mmap(struct file *file, struct vm_area_struct *vma) 697 { 698 /* 699 * Conventional zones accept random writes, so their files can support 700 * shared writable mappings. For sequential zone files, only read 701 * mappings are possible since there are no guarantees for write 702 * ordering between msync() and page cache writeback. 703 */ 704 if (ZONEFS_I(file_inode(file))->i_ztype == ZONEFS_ZTYPE_SEQ && 705 (vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) 706 return -EINVAL; 707 708 file_accessed(file); 709 vma->vm_ops = &zonefs_file_vm_ops; 710 711 return 0; 712 } 713 714 static loff_t zonefs_file_llseek(struct file *file, loff_t offset, int whence) 715 { 716 loff_t isize = i_size_read(file_inode(file)); 717 718 /* 719 * Seeks are limited to below the zone size for conventional zones 720 * and below the zone write pointer for sequential zones. In both 721 * cases, this limit is the inode size. 722 */ 723 return generic_file_llseek_size(file, offset, whence, isize, isize); 724 } 725 726 static int zonefs_file_write_dio_end_io(struct kiocb *iocb, ssize_t size, 727 int error, unsigned int flags) 728 { 729 struct inode *inode = file_inode(iocb->ki_filp); 730 struct zonefs_inode_info *zi = ZONEFS_I(inode); 731 732 if (error) { 733 zonefs_io_error(inode, true); 734 return error; 735 } 736 737 if (size && zi->i_ztype != ZONEFS_ZTYPE_CNV) { 738 /* 739 * Note that we may be seeing completions out of order, 740 * but that is not a problem since a write completed 741 * successfully necessarily means that all preceding writes 742 * were also successful. So we can safely increase the inode 743 * size to the write end location. 744 */ 745 mutex_lock(&zi->i_truncate_mutex); 746 if (i_size_read(inode) < iocb->ki_pos + size) { 747 zonefs_update_stats(inode, iocb->ki_pos + size); 748 zonefs_i_size_write(inode, iocb->ki_pos + size); 749 } 750 mutex_unlock(&zi->i_truncate_mutex); 751 } 752 753 return 0; 754 } 755 756 static const struct iomap_dio_ops zonefs_write_dio_ops = { 757 .end_io = zonefs_file_write_dio_end_io, 758 }; 759 760 static ssize_t zonefs_file_dio_append(struct kiocb *iocb, struct iov_iter *from) 761 { 762 struct inode *inode = file_inode(iocb->ki_filp); 763 struct zonefs_inode_info *zi = ZONEFS_I(inode); 764 struct block_device *bdev = inode->i_sb->s_bdev; 765 unsigned int max = bdev_max_zone_append_sectors(bdev); 766 struct bio *bio; 767 ssize_t size; 768 int nr_pages; 769 ssize_t ret; 770 771 max = ALIGN_DOWN(max << SECTOR_SHIFT, inode->i_sb->s_blocksize); 772 iov_iter_truncate(from, max); 773 774 nr_pages = iov_iter_npages(from, BIO_MAX_VECS); 775 if (!nr_pages) 776 return 0; 777 778 bio = bio_alloc(bdev, nr_pages, 779 REQ_OP_ZONE_APPEND | REQ_SYNC | REQ_IDLE, GFP_NOFS); 780 bio->bi_iter.bi_sector = zi->i_zsector; 781 bio->bi_ioprio = iocb->ki_ioprio; 782 if (iocb_is_dsync(iocb)) 783 bio->bi_opf |= REQ_FUA; 784 785 ret = bio_iov_iter_get_pages(bio, from); 786 if (unlikely(ret)) 787 goto out_release; 788 789 size = bio->bi_iter.bi_size; 790 task_io_account_write(size); 791 792 if (iocb->ki_flags & IOCB_HIPRI) 793 bio_set_polled(bio, iocb); 794 795 ret = submit_bio_wait(bio); 796 797 zonefs_file_write_dio_end_io(iocb, size, ret, 0); 798 trace_zonefs_file_dio_append(inode, size, ret); 799 800 out_release: 801 bio_release_pages(bio, false); 802 bio_put(bio); 803 804 if (ret >= 0) { 805 iocb->ki_pos += size; 806 return size; 807 } 808 809 return ret; 810 } 811 812 /* 813 * Do not exceed the LFS limits nor the file zone size. If pos is under the 814 * limit it becomes a short access. If it exceeds the limit, return -EFBIG. 815 */ 816 static loff_t zonefs_write_check_limits(struct file *file, loff_t pos, 817 loff_t count) 818 { 819 struct inode *inode = file_inode(file); 820 struct zonefs_inode_info *zi = ZONEFS_I(inode); 821 loff_t limit = rlimit(RLIMIT_FSIZE); 822 loff_t max_size = zi->i_max_size; 823 824 if (limit != RLIM_INFINITY) { 825 if (pos >= limit) { 826 send_sig(SIGXFSZ, current, 0); 827 return -EFBIG; 828 } 829 count = min(count, limit - pos); 830 } 831 832 if (!(file->f_flags & O_LARGEFILE)) 833 max_size = min_t(loff_t, MAX_NON_LFS, max_size); 834 835 if (unlikely(pos >= max_size)) 836 return -EFBIG; 837 838 return min(count, max_size - pos); 839 } 840 841 static ssize_t zonefs_write_checks(struct kiocb *iocb, struct iov_iter *from) 842 { 843 struct file *file = iocb->ki_filp; 844 struct inode *inode = file_inode(file); 845 struct zonefs_inode_info *zi = ZONEFS_I(inode); 846 loff_t count; 847 848 if (IS_SWAPFILE(inode)) 849 return -ETXTBSY; 850 851 if (!iov_iter_count(from)) 852 return 0; 853 854 if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT)) 855 return -EINVAL; 856 857 if (iocb->ki_flags & IOCB_APPEND) { 858 if (zi->i_ztype != ZONEFS_ZTYPE_SEQ) 859 return -EINVAL; 860 mutex_lock(&zi->i_truncate_mutex); 861 iocb->ki_pos = zi->i_wpoffset; 862 mutex_unlock(&zi->i_truncate_mutex); 863 } 864 865 count = zonefs_write_check_limits(file, iocb->ki_pos, 866 iov_iter_count(from)); 867 if (count < 0) 868 return count; 869 870 iov_iter_truncate(from, count); 871 return iov_iter_count(from); 872 } 873 874 /* 875 * Handle direct writes. For sequential zone files, this is the only possible 876 * write path. For these files, check that the user is issuing writes 877 * sequentially from the end of the file. This code assumes that the block layer 878 * delivers write requests to the device in sequential order. This is always the 879 * case if a block IO scheduler implementing the ELEVATOR_F_ZBD_SEQ_WRITE 880 * elevator feature is being used (e.g. mq-deadline). The block layer always 881 * automatically select such an elevator for zoned block devices during the 882 * device initialization. 883 */ 884 static ssize_t zonefs_file_dio_write(struct kiocb *iocb, struct iov_iter *from) 885 { 886 struct inode *inode = file_inode(iocb->ki_filp); 887 struct zonefs_inode_info *zi = ZONEFS_I(inode); 888 struct super_block *sb = inode->i_sb; 889 bool sync = is_sync_kiocb(iocb); 890 bool append = false; 891 ssize_t ret, count; 892 893 /* 894 * For async direct IOs to sequential zone files, refuse IOCB_NOWAIT 895 * as this can cause write reordering (e.g. the first aio gets EAGAIN 896 * on the inode lock but the second goes through but is now unaligned). 897 */ 898 if (zi->i_ztype == ZONEFS_ZTYPE_SEQ && !sync && 899 (iocb->ki_flags & IOCB_NOWAIT)) 900 return -EOPNOTSUPP; 901 902 if (iocb->ki_flags & IOCB_NOWAIT) { 903 if (!inode_trylock(inode)) 904 return -EAGAIN; 905 } else { 906 inode_lock(inode); 907 } 908 909 count = zonefs_write_checks(iocb, from); 910 if (count <= 0) { 911 ret = count; 912 goto inode_unlock; 913 } 914 915 if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) { 916 ret = -EINVAL; 917 goto inode_unlock; 918 } 919 920 /* Enforce sequential writes (append only) in sequential zones */ 921 if (zi->i_ztype == ZONEFS_ZTYPE_SEQ) { 922 mutex_lock(&zi->i_truncate_mutex); 923 if (iocb->ki_pos != zi->i_wpoffset) { 924 mutex_unlock(&zi->i_truncate_mutex); 925 ret = -EINVAL; 926 goto inode_unlock; 927 } 928 mutex_unlock(&zi->i_truncate_mutex); 929 append = sync; 930 } 931 932 if (append) 933 ret = zonefs_file_dio_append(iocb, from); 934 else 935 ret = iomap_dio_rw(iocb, from, &zonefs_write_iomap_ops, 936 &zonefs_write_dio_ops, 0, NULL, 0); 937 if (zi->i_ztype == ZONEFS_ZTYPE_SEQ && 938 (ret > 0 || ret == -EIOCBQUEUED)) { 939 if (ret > 0) 940 count = ret; 941 942 /* 943 * Update the zone write pointer offset assuming the write 944 * operation succeeded. If it did not, the error recovery path 945 * will correct it. Also do active seq file accounting. 946 */ 947 mutex_lock(&zi->i_truncate_mutex); 948 zi->i_wpoffset += count; 949 zonefs_account_active(inode); 950 mutex_unlock(&zi->i_truncate_mutex); 951 } 952 953 inode_unlock: 954 inode_unlock(inode); 955 956 return ret; 957 } 958 959 static ssize_t zonefs_file_buffered_write(struct kiocb *iocb, 960 struct iov_iter *from) 961 { 962 struct inode *inode = file_inode(iocb->ki_filp); 963 struct zonefs_inode_info *zi = ZONEFS_I(inode); 964 ssize_t ret; 965 966 /* 967 * Direct IO writes are mandatory for sequential zone files so that the 968 * write IO issuing order is preserved. 969 */ 970 if (zi->i_ztype != ZONEFS_ZTYPE_CNV) 971 return -EIO; 972 973 if (iocb->ki_flags & IOCB_NOWAIT) { 974 if (!inode_trylock(inode)) 975 return -EAGAIN; 976 } else { 977 inode_lock(inode); 978 } 979 980 ret = zonefs_write_checks(iocb, from); 981 if (ret <= 0) 982 goto inode_unlock; 983 984 ret = iomap_file_buffered_write(iocb, from, &zonefs_write_iomap_ops); 985 if (ret > 0) 986 iocb->ki_pos += ret; 987 else if (ret == -EIO) 988 zonefs_io_error(inode, true); 989 990 inode_unlock: 991 inode_unlock(inode); 992 if (ret > 0) 993 ret = generic_write_sync(iocb, ret); 994 995 return ret; 996 } 997 998 static ssize_t zonefs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) 999 { 1000 struct inode *inode = file_inode(iocb->ki_filp); 1001 1002 if (unlikely(IS_IMMUTABLE(inode))) 1003 return -EPERM; 1004 1005 if (sb_rdonly(inode->i_sb)) 1006 return -EROFS; 1007 1008 /* Write operations beyond the zone size are not allowed */ 1009 if (iocb->ki_pos >= ZONEFS_I(inode)->i_max_size) 1010 return -EFBIG; 1011 1012 if (iocb->ki_flags & IOCB_DIRECT) { 1013 ssize_t ret = zonefs_file_dio_write(iocb, from); 1014 if (ret != -ENOTBLK) 1015 return ret; 1016 } 1017 1018 return zonefs_file_buffered_write(iocb, from); 1019 } 1020 1021 static int zonefs_file_read_dio_end_io(struct kiocb *iocb, ssize_t size, 1022 int error, unsigned int flags) 1023 { 1024 if (error) { 1025 zonefs_io_error(file_inode(iocb->ki_filp), false); 1026 return error; 1027 } 1028 1029 return 0; 1030 } 1031 1032 static const struct iomap_dio_ops zonefs_read_dio_ops = { 1033 .end_io = zonefs_file_read_dio_end_io, 1034 }; 1035 1036 static ssize_t zonefs_file_read_iter(struct kiocb *iocb, struct iov_iter *to) 1037 { 1038 struct inode *inode = file_inode(iocb->ki_filp); 1039 struct zonefs_inode_info *zi = ZONEFS_I(inode); 1040 struct super_block *sb = inode->i_sb; 1041 loff_t isize; 1042 ssize_t ret; 1043 1044 /* Offline zones cannot be read */ 1045 if (unlikely(IS_IMMUTABLE(inode) && !(inode->i_mode & 0777))) 1046 return -EPERM; 1047 1048 if (iocb->ki_pos >= zi->i_max_size) 1049 return 0; 1050 1051 if (iocb->ki_flags & IOCB_NOWAIT) { 1052 if (!inode_trylock_shared(inode)) 1053 return -EAGAIN; 1054 } else { 1055 inode_lock_shared(inode); 1056 } 1057 1058 /* Limit read operations to written data */ 1059 mutex_lock(&zi->i_truncate_mutex); 1060 isize = i_size_read(inode); 1061 if (iocb->ki_pos >= isize) { 1062 mutex_unlock(&zi->i_truncate_mutex); 1063 ret = 0; 1064 goto inode_unlock; 1065 } 1066 iov_iter_truncate(to, isize - iocb->ki_pos); 1067 mutex_unlock(&zi->i_truncate_mutex); 1068 1069 if (iocb->ki_flags & IOCB_DIRECT) { 1070 size_t count = iov_iter_count(to); 1071 1072 if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) { 1073 ret = -EINVAL; 1074 goto inode_unlock; 1075 } 1076 file_accessed(iocb->ki_filp); 1077 ret = iomap_dio_rw(iocb, to, &zonefs_read_iomap_ops, 1078 &zonefs_read_dio_ops, 0, NULL, 0); 1079 } else { 1080 ret = generic_file_read_iter(iocb, to); 1081 if (ret == -EIO) 1082 zonefs_io_error(inode, false); 1083 } 1084 1085 inode_unlock: 1086 inode_unlock_shared(inode); 1087 1088 return ret; 1089 } 1090 1091 /* 1092 * Write open accounting is done only for sequential files. 1093 */ 1094 static inline bool zonefs_seq_file_need_wro(struct inode *inode, 1095 struct file *file) 1096 { 1097 struct zonefs_inode_info *zi = ZONEFS_I(inode); 1098 1099 if (zi->i_ztype != ZONEFS_ZTYPE_SEQ) 1100 return false; 1101 1102 if (!(file->f_mode & FMODE_WRITE)) 1103 return false; 1104 1105 return true; 1106 } 1107 1108 static int zonefs_seq_file_write_open(struct inode *inode) 1109 { 1110 struct zonefs_inode_info *zi = ZONEFS_I(inode); 1111 int ret = 0; 1112 1113 mutex_lock(&zi->i_truncate_mutex); 1114 1115 if (!zi->i_wr_refcnt) { 1116 struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb); 1117 unsigned int wro = atomic_inc_return(&sbi->s_wro_seq_files); 1118 1119 if (sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) { 1120 1121 if (sbi->s_max_wro_seq_files 1122 && wro > sbi->s_max_wro_seq_files) { 1123 atomic_dec(&sbi->s_wro_seq_files); 1124 ret = -EBUSY; 1125 goto unlock; 1126 } 1127 1128 if (i_size_read(inode) < zi->i_max_size) { 1129 ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_OPEN); 1130 if (ret) { 1131 atomic_dec(&sbi->s_wro_seq_files); 1132 goto unlock; 1133 } 1134 zi->i_flags |= ZONEFS_ZONE_OPEN; 1135 zonefs_account_active(inode); 1136 } 1137 } 1138 } 1139 1140 zi->i_wr_refcnt++; 1141 1142 unlock: 1143 mutex_unlock(&zi->i_truncate_mutex); 1144 1145 return ret; 1146 } 1147 1148 static int zonefs_file_open(struct inode *inode, struct file *file) 1149 { 1150 int ret; 1151 1152 ret = generic_file_open(inode, file); 1153 if (ret) 1154 return ret; 1155 1156 if (zonefs_seq_file_need_wro(inode, file)) 1157 return zonefs_seq_file_write_open(inode); 1158 1159 return 0; 1160 } 1161 1162 static void zonefs_seq_file_write_close(struct inode *inode) 1163 { 1164 struct zonefs_inode_info *zi = ZONEFS_I(inode); 1165 struct super_block *sb = inode->i_sb; 1166 struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 1167 int ret = 0; 1168 1169 mutex_lock(&zi->i_truncate_mutex); 1170 1171 zi->i_wr_refcnt--; 1172 if (zi->i_wr_refcnt) 1173 goto unlock; 1174 1175 /* 1176 * The file zone may not be open anymore (e.g. the file was truncated to 1177 * its maximum size or it was fully written). For this case, we only 1178 * need to decrement the write open count. 1179 */ 1180 if (zi->i_flags & ZONEFS_ZONE_OPEN) { 1181 ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_CLOSE); 1182 if (ret) { 1183 __zonefs_io_error(inode, false); 1184 /* 1185 * Leaving zones explicitly open may lead to a state 1186 * where most zones cannot be written (zone resources 1187 * exhausted). So take preventive action by remounting 1188 * read-only. 1189 */ 1190 if (zi->i_flags & ZONEFS_ZONE_OPEN && 1191 !(sb->s_flags & SB_RDONLY)) { 1192 zonefs_warn(sb, 1193 "closing zone at %llu failed %d\n", 1194 zi->i_zsector, ret); 1195 zonefs_warn(sb, 1196 "remounting filesystem read-only\n"); 1197 sb->s_flags |= SB_RDONLY; 1198 } 1199 goto unlock; 1200 } 1201 1202 zi->i_flags &= ~ZONEFS_ZONE_OPEN; 1203 zonefs_account_active(inode); 1204 } 1205 1206 atomic_dec(&sbi->s_wro_seq_files); 1207 1208 unlock: 1209 mutex_unlock(&zi->i_truncate_mutex); 1210 } 1211 1212 static int zonefs_file_release(struct inode *inode, struct file *file) 1213 { 1214 /* 1215 * If we explicitly open a zone we must close it again as well, but the 1216 * zone management operation can fail (either due to an IO error or as 1217 * the zone has gone offline or read-only). Make sure we don't fail the 1218 * close(2) for user-space. 1219 */ 1220 if (zonefs_seq_file_need_wro(inode, file)) 1221 zonefs_seq_file_write_close(inode); 1222 1223 return 0; 1224 } 1225 1226 static const struct file_operations zonefs_file_operations = { 1227 .open = zonefs_file_open, 1228 .release = zonefs_file_release, 1229 .fsync = zonefs_file_fsync, 1230 .mmap = zonefs_file_mmap, 1231 .llseek = zonefs_file_llseek, 1232 .read_iter = zonefs_file_read_iter, 1233 .write_iter = zonefs_file_write_iter, 1234 .splice_read = generic_file_splice_read, 1235 .splice_write = iter_file_splice_write, 1236 .iopoll = iocb_bio_iopoll, 1237 }; 1238 1239 static struct kmem_cache *zonefs_inode_cachep; 1240 1241 static struct inode *zonefs_alloc_inode(struct super_block *sb) 1242 { 1243 struct zonefs_inode_info *zi; 1244 1245 zi = alloc_inode_sb(sb, zonefs_inode_cachep, GFP_KERNEL); 1246 if (!zi) 1247 return NULL; 1248 1249 inode_init_once(&zi->i_vnode); 1250 mutex_init(&zi->i_truncate_mutex); 1251 zi->i_wr_refcnt = 0; 1252 zi->i_flags = 0; 1253 1254 return &zi->i_vnode; 1255 } 1256 1257 static void zonefs_free_inode(struct inode *inode) 1258 { 1259 kmem_cache_free(zonefs_inode_cachep, ZONEFS_I(inode)); 1260 } 1261 1262 /* 1263 * File system stat. 1264 */ 1265 static int zonefs_statfs(struct dentry *dentry, struct kstatfs *buf) 1266 { 1267 struct super_block *sb = dentry->d_sb; 1268 struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 1269 enum zonefs_ztype t; 1270 1271 buf->f_type = ZONEFS_MAGIC; 1272 buf->f_bsize = sb->s_blocksize; 1273 buf->f_namelen = ZONEFS_NAME_MAX; 1274 1275 spin_lock(&sbi->s_lock); 1276 1277 buf->f_blocks = sbi->s_blocks; 1278 if (WARN_ON(sbi->s_used_blocks > sbi->s_blocks)) 1279 buf->f_bfree = 0; 1280 else 1281 buf->f_bfree = buf->f_blocks - sbi->s_used_blocks; 1282 buf->f_bavail = buf->f_bfree; 1283 1284 for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) { 1285 if (sbi->s_nr_files[t]) 1286 buf->f_files += sbi->s_nr_files[t] + 1; 1287 } 1288 buf->f_ffree = 0; 1289 1290 spin_unlock(&sbi->s_lock); 1291 1292 buf->f_fsid = uuid_to_fsid(sbi->s_uuid.b); 1293 1294 return 0; 1295 } 1296 1297 enum { 1298 Opt_errors_ro, Opt_errors_zro, Opt_errors_zol, Opt_errors_repair, 1299 Opt_explicit_open, Opt_err, 1300 }; 1301 1302 static const match_table_t tokens = { 1303 { Opt_errors_ro, "errors=remount-ro"}, 1304 { Opt_errors_zro, "errors=zone-ro"}, 1305 { Opt_errors_zol, "errors=zone-offline"}, 1306 { Opt_errors_repair, "errors=repair"}, 1307 { Opt_explicit_open, "explicit-open" }, 1308 { Opt_err, NULL} 1309 }; 1310 1311 static int zonefs_parse_options(struct super_block *sb, char *options) 1312 { 1313 struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 1314 substring_t args[MAX_OPT_ARGS]; 1315 char *p; 1316 1317 if (!options) 1318 return 0; 1319 1320 while ((p = strsep(&options, ",")) != NULL) { 1321 int token; 1322 1323 if (!*p) 1324 continue; 1325 1326 token = match_token(p, tokens, args); 1327 switch (token) { 1328 case Opt_errors_ro: 1329 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK; 1330 sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_RO; 1331 break; 1332 case Opt_errors_zro: 1333 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK; 1334 sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZRO; 1335 break; 1336 case Opt_errors_zol: 1337 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK; 1338 sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZOL; 1339 break; 1340 case Opt_errors_repair: 1341 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK; 1342 sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_REPAIR; 1343 break; 1344 case Opt_explicit_open: 1345 sbi->s_mount_opts |= ZONEFS_MNTOPT_EXPLICIT_OPEN; 1346 break; 1347 default: 1348 return -EINVAL; 1349 } 1350 } 1351 1352 return 0; 1353 } 1354 1355 static int zonefs_show_options(struct seq_file *seq, struct dentry *root) 1356 { 1357 struct zonefs_sb_info *sbi = ZONEFS_SB(root->d_sb); 1358 1359 if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO) 1360 seq_puts(seq, ",errors=remount-ro"); 1361 if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO) 1362 seq_puts(seq, ",errors=zone-ro"); 1363 if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL) 1364 seq_puts(seq, ",errors=zone-offline"); 1365 if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_REPAIR) 1366 seq_puts(seq, ",errors=repair"); 1367 1368 return 0; 1369 } 1370 1371 static int zonefs_remount(struct super_block *sb, int *flags, char *data) 1372 { 1373 sync_filesystem(sb); 1374 1375 return zonefs_parse_options(sb, data); 1376 } 1377 1378 static const struct super_operations zonefs_sops = { 1379 .alloc_inode = zonefs_alloc_inode, 1380 .free_inode = zonefs_free_inode, 1381 .statfs = zonefs_statfs, 1382 .remount_fs = zonefs_remount, 1383 .show_options = zonefs_show_options, 1384 }; 1385 1386 static const struct inode_operations zonefs_dir_inode_operations = { 1387 .lookup = simple_lookup, 1388 .setattr = zonefs_inode_setattr, 1389 }; 1390 1391 static void zonefs_init_dir_inode(struct inode *parent, struct inode *inode, 1392 enum zonefs_ztype type) 1393 { 1394 struct super_block *sb = parent->i_sb; 1395 1396 inode->i_ino = bdev_nr_zones(sb->s_bdev) + type + 1; 1397 inode_init_owner(&init_user_ns, inode, parent, S_IFDIR | 0555); 1398 inode->i_op = &zonefs_dir_inode_operations; 1399 inode->i_fop = &simple_dir_operations; 1400 set_nlink(inode, 2); 1401 inc_nlink(parent); 1402 } 1403 1404 static int zonefs_init_file_inode(struct inode *inode, struct blk_zone *zone, 1405 enum zonefs_ztype type) 1406 { 1407 struct super_block *sb = inode->i_sb; 1408 struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 1409 struct zonefs_inode_info *zi = ZONEFS_I(inode); 1410 int ret = 0; 1411 1412 inode->i_ino = zone->start >> sbi->s_zone_sectors_shift; 1413 inode->i_mode = S_IFREG | sbi->s_perm; 1414 1415 zi->i_ztype = type; 1416 zi->i_zsector = zone->start; 1417 zi->i_zone_size = zone->len << SECTOR_SHIFT; 1418 if (zi->i_zone_size > bdev_zone_sectors(sb->s_bdev) << SECTOR_SHIFT && 1419 !(sbi->s_features & ZONEFS_F_AGGRCNV)) { 1420 zonefs_err(sb, 1421 "zone size %llu doesn't match device's zone sectors %llu\n", 1422 zi->i_zone_size, 1423 bdev_zone_sectors(sb->s_bdev) << SECTOR_SHIFT); 1424 return -EINVAL; 1425 } 1426 1427 zi->i_max_size = min_t(loff_t, MAX_LFS_FILESIZE, 1428 zone->capacity << SECTOR_SHIFT); 1429 zi->i_wpoffset = zonefs_check_zone_condition(inode, zone, true, true); 1430 1431 inode->i_uid = sbi->s_uid; 1432 inode->i_gid = sbi->s_gid; 1433 inode->i_size = zi->i_wpoffset; 1434 inode->i_blocks = zi->i_max_size >> SECTOR_SHIFT; 1435 1436 inode->i_op = &zonefs_file_inode_operations; 1437 inode->i_fop = &zonefs_file_operations; 1438 inode->i_mapping->a_ops = &zonefs_file_aops; 1439 1440 sb->s_maxbytes = max(zi->i_max_size, sb->s_maxbytes); 1441 sbi->s_blocks += zi->i_max_size >> sb->s_blocksize_bits; 1442 sbi->s_used_blocks += zi->i_wpoffset >> sb->s_blocksize_bits; 1443 1444 mutex_lock(&zi->i_truncate_mutex); 1445 1446 /* 1447 * For sequential zones, make sure that any open zone is closed first 1448 * to ensure that the initial number of open zones is 0, in sync with 1449 * the open zone accounting done when the mount option 1450 * ZONEFS_MNTOPT_EXPLICIT_OPEN is used. 1451 */ 1452 if (type == ZONEFS_ZTYPE_SEQ && 1453 (zone->cond == BLK_ZONE_COND_IMP_OPEN || 1454 zone->cond == BLK_ZONE_COND_EXP_OPEN)) { 1455 ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_CLOSE); 1456 if (ret) 1457 goto unlock; 1458 } 1459 1460 zonefs_account_active(inode); 1461 1462 unlock: 1463 mutex_unlock(&zi->i_truncate_mutex); 1464 1465 return ret; 1466 } 1467 1468 static struct dentry *zonefs_create_inode(struct dentry *parent, 1469 const char *name, struct blk_zone *zone, 1470 enum zonefs_ztype type) 1471 { 1472 struct inode *dir = d_inode(parent); 1473 struct dentry *dentry; 1474 struct inode *inode; 1475 int ret = -ENOMEM; 1476 1477 dentry = d_alloc_name(parent, name); 1478 if (!dentry) 1479 return ERR_PTR(ret); 1480 1481 inode = new_inode(parent->d_sb); 1482 if (!inode) 1483 goto dput; 1484 1485 inode->i_ctime = inode->i_mtime = inode->i_atime = dir->i_ctime; 1486 if (zone) { 1487 ret = zonefs_init_file_inode(inode, zone, type); 1488 if (ret) { 1489 iput(inode); 1490 goto dput; 1491 } 1492 } else { 1493 zonefs_init_dir_inode(dir, inode, type); 1494 } 1495 1496 d_add(dentry, inode); 1497 dir->i_size++; 1498 1499 return dentry; 1500 1501 dput: 1502 dput(dentry); 1503 1504 return ERR_PTR(ret); 1505 } 1506 1507 struct zonefs_zone_data { 1508 struct super_block *sb; 1509 unsigned int nr_zones[ZONEFS_ZTYPE_MAX]; 1510 struct blk_zone *zones; 1511 }; 1512 1513 /* 1514 * Create a zone group and populate it with zone files. 1515 */ 1516 static int zonefs_create_zgroup(struct zonefs_zone_data *zd, 1517 enum zonefs_ztype type) 1518 { 1519 struct super_block *sb = zd->sb; 1520 struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 1521 struct blk_zone *zone, *next, *end; 1522 const char *zgroup_name; 1523 char *file_name; 1524 struct dentry *dir, *dent; 1525 unsigned int n = 0; 1526 int ret; 1527 1528 /* If the group is empty, there is nothing to do */ 1529 if (!zd->nr_zones[type]) 1530 return 0; 1531 1532 file_name = kmalloc(ZONEFS_NAME_MAX, GFP_KERNEL); 1533 if (!file_name) 1534 return -ENOMEM; 1535 1536 if (type == ZONEFS_ZTYPE_CNV) 1537 zgroup_name = "cnv"; 1538 else 1539 zgroup_name = "seq"; 1540 1541 dir = zonefs_create_inode(sb->s_root, zgroup_name, NULL, type); 1542 if (IS_ERR(dir)) { 1543 ret = PTR_ERR(dir); 1544 goto free; 1545 } 1546 1547 /* 1548 * The first zone contains the super block: skip it. 1549 */ 1550 end = zd->zones + bdev_nr_zones(sb->s_bdev); 1551 for (zone = &zd->zones[1]; zone < end; zone = next) { 1552 1553 next = zone + 1; 1554 if (zonefs_zone_type(zone) != type) 1555 continue; 1556 1557 /* 1558 * For conventional zones, contiguous zones can be aggregated 1559 * together to form larger files. Note that this overwrites the 1560 * length of the first zone of the set of contiguous zones 1561 * aggregated together. If one offline or read-only zone is 1562 * found, assume that all zones aggregated have the same 1563 * condition. 1564 */ 1565 if (type == ZONEFS_ZTYPE_CNV && 1566 (sbi->s_features & ZONEFS_F_AGGRCNV)) { 1567 for (; next < end; next++) { 1568 if (zonefs_zone_type(next) != type) 1569 break; 1570 zone->len += next->len; 1571 zone->capacity += next->capacity; 1572 if (next->cond == BLK_ZONE_COND_READONLY && 1573 zone->cond != BLK_ZONE_COND_OFFLINE) 1574 zone->cond = BLK_ZONE_COND_READONLY; 1575 else if (next->cond == BLK_ZONE_COND_OFFLINE) 1576 zone->cond = BLK_ZONE_COND_OFFLINE; 1577 } 1578 if (zone->capacity != zone->len) { 1579 zonefs_err(sb, "Invalid conventional zone capacity\n"); 1580 ret = -EINVAL; 1581 goto free; 1582 } 1583 } 1584 1585 /* 1586 * Use the file number within its group as file name. 1587 */ 1588 snprintf(file_name, ZONEFS_NAME_MAX - 1, "%u", n); 1589 dent = zonefs_create_inode(dir, file_name, zone, type); 1590 if (IS_ERR(dent)) { 1591 ret = PTR_ERR(dent); 1592 goto free; 1593 } 1594 1595 n++; 1596 } 1597 1598 zonefs_info(sb, "Zone group \"%s\" has %u file%s\n", 1599 zgroup_name, n, n > 1 ? "s" : ""); 1600 1601 sbi->s_nr_files[type] = n; 1602 ret = 0; 1603 1604 free: 1605 kfree(file_name); 1606 1607 return ret; 1608 } 1609 1610 static int zonefs_get_zone_info_cb(struct blk_zone *zone, unsigned int idx, 1611 void *data) 1612 { 1613 struct zonefs_zone_data *zd = data; 1614 1615 /* 1616 * Count the number of usable zones: the first zone at index 0 contains 1617 * the super block and is ignored. 1618 */ 1619 switch (zone->type) { 1620 case BLK_ZONE_TYPE_CONVENTIONAL: 1621 zone->wp = zone->start + zone->len; 1622 if (idx) 1623 zd->nr_zones[ZONEFS_ZTYPE_CNV]++; 1624 break; 1625 case BLK_ZONE_TYPE_SEQWRITE_REQ: 1626 case BLK_ZONE_TYPE_SEQWRITE_PREF: 1627 if (idx) 1628 zd->nr_zones[ZONEFS_ZTYPE_SEQ]++; 1629 break; 1630 default: 1631 zonefs_err(zd->sb, "Unsupported zone type 0x%x\n", 1632 zone->type); 1633 return -EIO; 1634 } 1635 1636 memcpy(&zd->zones[idx], zone, sizeof(struct blk_zone)); 1637 1638 return 0; 1639 } 1640 1641 static int zonefs_get_zone_info(struct zonefs_zone_data *zd) 1642 { 1643 struct block_device *bdev = zd->sb->s_bdev; 1644 int ret; 1645 1646 zd->zones = kvcalloc(bdev_nr_zones(bdev), sizeof(struct blk_zone), 1647 GFP_KERNEL); 1648 if (!zd->zones) 1649 return -ENOMEM; 1650 1651 /* Get zones information from the device */ 1652 ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES, 1653 zonefs_get_zone_info_cb, zd); 1654 if (ret < 0) { 1655 zonefs_err(zd->sb, "Zone report failed %d\n", ret); 1656 return ret; 1657 } 1658 1659 if (ret != bdev_nr_zones(bdev)) { 1660 zonefs_err(zd->sb, "Invalid zone report (%d/%u zones)\n", 1661 ret, bdev_nr_zones(bdev)); 1662 return -EIO; 1663 } 1664 1665 return 0; 1666 } 1667 1668 static inline void zonefs_cleanup_zone_info(struct zonefs_zone_data *zd) 1669 { 1670 kvfree(zd->zones); 1671 } 1672 1673 /* 1674 * Read super block information from the device. 1675 */ 1676 static int zonefs_read_super(struct super_block *sb) 1677 { 1678 struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 1679 struct zonefs_super *super; 1680 u32 crc, stored_crc; 1681 struct page *page; 1682 struct bio_vec bio_vec; 1683 struct bio bio; 1684 int ret; 1685 1686 page = alloc_page(GFP_KERNEL); 1687 if (!page) 1688 return -ENOMEM; 1689 1690 bio_init(&bio, sb->s_bdev, &bio_vec, 1, REQ_OP_READ); 1691 bio.bi_iter.bi_sector = 0; 1692 bio_add_page(&bio, page, PAGE_SIZE, 0); 1693 1694 ret = submit_bio_wait(&bio); 1695 if (ret) 1696 goto free_page; 1697 1698 super = page_address(page); 1699 1700 ret = -EINVAL; 1701 if (le32_to_cpu(super->s_magic) != ZONEFS_MAGIC) 1702 goto free_page; 1703 1704 stored_crc = le32_to_cpu(super->s_crc); 1705 super->s_crc = 0; 1706 crc = crc32(~0U, (unsigned char *)super, sizeof(struct zonefs_super)); 1707 if (crc != stored_crc) { 1708 zonefs_err(sb, "Invalid checksum (Expected 0x%08x, got 0x%08x)", 1709 crc, stored_crc); 1710 goto free_page; 1711 } 1712 1713 sbi->s_features = le64_to_cpu(super->s_features); 1714 if (sbi->s_features & ~ZONEFS_F_DEFINED_FEATURES) { 1715 zonefs_err(sb, "Unknown features set 0x%llx\n", 1716 sbi->s_features); 1717 goto free_page; 1718 } 1719 1720 if (sbi->s_features & ZONEFS_F_UID) { 1721 sbi->s_uid = make_kuid(current_user_ns(), 1722 le32_to_cpu(super->s_uid)); 1723 if (!uid_valid(sbi->s_uid)) { 1724 zonefs_err(sb, "Invalid UID feature\n"); 1725 goto free_page; 1726 } 1727 } 1728 1729 if (sbi->s_features & ZONEFS_F_GID) { 1730 sbi->s_gid = make_kgid(current_user_ns(), 1731 le32_to_cpu(super->s_gid)); 1732 if (!gid_valid(sbi->s_gid)) { 1733 zonefs_err(sb, "Invalid GID feature\n"); 1734 goto free_page; 1735 } 1736 } 1737 1738 if (sbi->s_features & ZONEFS_F_PERM) 1739 sbi->s_perm = le32_to_cpu(super->s_perm); 1740 1741 if (memchr_inv(super->s_reserved, 0, sizeof(super->s_reserved))) { 1742 zonefs_err(sb, "Reserved area is being used\n"); 1743 goto free_page; 1744 } 1745 1746 import_uuid(&sbi->s_uuid, super->s_uuid); 1747 ret = 0; 1748 1749 free_page: 1750 __free_page(page); 1751 1752 return ret; 1753 } 1754 1755 /* 1756 * Check that the device is zoned. If it is, get the list of zones and create 1757 * sub-directories and files according to the device zone configuration and 1758 * format options. 1759 */ 1760 static int zonefs_fill_super(struct super_block *sb, void *data, int silent) 1761 { 1762 struct zonefs_zone_data zd; 1763 struct zonefs_sb_info *sbi; 1764 struct inode *inode; 1765 enum zonefs_ztype t; 1766 int ret; 1767 1768 if (!bdev_is_zoned(sb->s_bdev)) { 1769 zonefs_err(sb, "Not a zoned block device\n"); 1770 return -EINVAL; 1771 } 1772 1773 /* 1774 * Initialize super block information: the maximum file size is updated 1775 * when the zone files are created so that the format option 1776 * ZONEFS_F_AGGRCNV which increases the maximum file size of a file 1777 * beyond the zone size is taken into account. 1778 */ 1779 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL); 1780 if (!sbi) 1781 return -ENOMEM; 1782 1783 spin_lock_init(&sbi->s_lock); 1784 sb->s_fs_info = sbi; 1785 sb->s_magic = ZONEFS_MAGIC; 1786 sb->s_maxbytes = 0; 1787 sb->s_op = &zonefs_sops; 1788 sb->s_time_gran = 1; 1789 1790 /* 1791 * The block size is set to the device zone write granularity to ensure 1792 * that write operations are always aligned according to the device 1793 * interface constraints. 1794 */ 1795 sb_set_blocksize(sb, bdev_zone_write_granularity(sb->s_bdev)); 1796 sbi->s_zone_sectors_shift = ilog2(bdev_zone_sectors(sb->s_bdev)); 1797 sbi->s_uid = GLOBAL_ROOT_UID; 1798 sbi->s_gid = GLOBAL_ROOT_GID; 1799 sbi->s_perm = 0640; 1800 sbi->s_mount_opts = ZONEFS_MNTOPT_ERRORS_RO; 1801 1802 atomic_set(&sbi->s_wro_seq_files, 0); 1803 sbi->s_max_wro_seq_files = bdev_max_open_zones(sb->s_bdev); 1804 atomic_set(&sbi->s_active_seq_files, 0); 1805 sbi->s_max_active_seq_files = bdev_max_active_zones(sb->s_bdev); 1806 1807 ret = zonefs_read_super(sb); 1808 if (ret) 1809 return ret; 1810 1811 ret = zonefs_parse_options(sb, data); 1812 if (ret) 1813 return ret; 1814 1815 memset(&zd, 0, sizeof(struct zonefs_zone_data)); 1816 zd.sb = sb; 1817 ret = zonefs_get_zone_info(&zd); 1818 if (ret) 1819 goto cleanup; 1820 1821 ret = zonefs_sysfs_register(sb); 1822 if (ret) 1823 goto cleanup; 1824 1825 zonefs_info(sb, "Mounting %u zones", bdev_nr_zones(sb->s_bdev)); 1826 1827 if (!sbi->s_max_wro_seq_files && 1828 !sbi->s_max_active_seq_files && 1829 sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) { 1830 zonefs_info(sb, 1831 "No open and active zone limits. Ignoring explicit_open mount option\n"); 1832 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_EXPLICIT_OPEN; 1833 } 1834 1835 /* Create root directory inode */ 1836 ret = -ENOMEM; 1837 inode = new_inode(sb); 1838 if (!inode) 1839 goto cleanup; 1840 1841 inode->i_ino = bdev_nr_zones(sb->s_bdev); 1842 inode->i_mode = S_IFDIR | 0555; 1843 inode->i_ctime = inode->i_mtime = inode->i_atime = current_time(inode); 1844 inode->i_op = &zonefs_dir_inode_operations; 1845 inode->i_fop = &simple_dir_operations; 1846 set_nlink(inode, 2); 1847 1848 sb->s_root = d_make_root(inode); 1849 if (!sb->s_root) 1850 goto cleanup; 1851 1852 /* Create and populate files in zone groups directories */ 1853 for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) { 1854 ret = zonefs_create_zgroup(&zd, t); 1855 if (ret) 1856 break; 1857 } 1858 1859 cleanup: 1860 zonefs_cleanup_zone_info(&zd); 1861 1862 return ret; 1863 } 1864 1865 static struct dentry *zonefs_mount(struct file_system_type *fs_type, 1866 int flags, const char *dev_name, void *data) 1867 { 1868 return mount_bdev(fs_type, flags, dev_name, data, zonefs_fill_super); 1869 } 1870 1871 static void zonefs_kill_super(struct super_block *sb) 1872 { 1873 struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 1874 1875 if (sb->s_root) 1876 d_genocide(sb->s_root); 1877 1878 zonefs_sysfs_unregister(sb); 1879 kill_block_super(sb); 1880 kfree(sbi); 1881 } 1882 1883 /* 1884 * File system definition and registration. 1885 */ 1886 static struct file_system_type zonefs_type = { 1887 .owner = THIS_MODULE, 1888 .name = "zonefs", 1889 .mount = zonefs_mount, 1890 .kill_sb = zonefs_kill_super, 1891 .fs_flags = FS_REQUIRES_DEV, 1892 }; 1893 1894 static int __init zonefs_init_inodecache(void) 1895 { 1896 zonefs_inode_cachep = kmem_cache_create("zonefs_inode_cache", 1897 sizeof(struct zonefs_inode_info), 0, 1898 (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT), 1899 NULL); 1900 if (zonefs_inode_cachep == NULL) 1901 return -ENOMEM; 1902 return 0; 1903 } 1904 1905 static void zonefs_destroy_inodecache(void) 1906 { 1907 /* 1908 * Make sure all delayed rcu free inodes are flushed before we 1909 * destroy the inode cache. 1910 */ 1911 rcu_barrier(); 1912 kmem_cache_destroy(zonefs_inode_cachep); 1913 } 1914 1915 static int __init zonefs_init(void) 1916 { 1917 int ret; 1918 1919 BUILD_BUG_ON(sizeof(struct zonefs_super) != ZONEFS_SUPER_SIZE); 1920 1921 ret = zonefs_init_inodecache(); 1922 if (ret) 1923 return ret; 1924 1925 ret = register_filesystem(&zonefs_type); 1926 if (ret) 1927 goto destroy_inodecache; 1928 1929 ret = zonefs_sysfs_init(); 1930 if (ret) 1931 goto unregister_fs; 1932 1933 return 0; 1934 1935 unregister_fs: 1936 unregister_filesystem(&zonefs_type); 1937 destroy_inodecache: 1938 zonefs_destroy_inodecache(); 1939 1940 return ret; 1941 } 1942 1943 static void __exit zonefs_exit(void) 1944 { 1945 zonefs_sysfs_exit(); 1946 zonefs_destroy_inodecache(); 1947 unregister_filesystem(&zonefs_type); 1948 } 1949 1950 MODULE_AUTHOR("Damien Le Moal"); 1951 MODULE_DESCRIPTION("Zone file system for zoned block devices"); 1952 MODULE_LICENSE("GPL"); 1953 MODULE_ALIAS_FS("zonefs"); 1954 module_init(zonefs_init); 1955 module_exit(zonefs_exit); 1956