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/uio.h> 19 #include <linux/mman.h> 20 #include <linux/sched/mm.h> 21 #include <linux/crc32.h> 22 #include <linux/task_io_accounting_ops.h> 23 #include <linux/fs_parser.h> 24 #include <linux/fs_context.h> 25 26 #include "zonefs.h" 27 28 #define CREATE_TRACE_POINTS 29 #include "trace.h" 30 31 /* 32 * Get the name of a zone group directory. 33 */ 34 static const char *zonefs_zgroup_name(enum zonefs_ztype ztype) 35 { 36 switch (ztype) { 37 case ZONEFS_ZTYPE_CNV: 38 return "cnv"; 39 case ZONEFS_ZTYPE_SEQ: 40 return "seq"; 41 default: 42 WARN_ON_ONCE(1); 43 return "???"; 44 } 45 } 46 47 /* 48 * Manage the active zone count. 49 */ 50 static void zonefs_account_active(struct super_block *sb, 51 struct zonefs_zone *z) 52 { 53 struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 54 55 if (zonefs_zone_is_cnv(z)) 56 return; 57 58 /* 59 * For zones that transitioned to the offline or readonly condition, 60 * we only need to clear the active state. 61 */ 62 if (z->z_flags & (ZONEFS_ZONE_OFFLINE | ZONEFS_ZONE_READONLY)) 63 goto out; 64 65 /* 66 * If the zone is active, that is, if it is explicitly open or 67 * partially written, check if it was already accounted as active. 68 */ 69 if ((z->z_flags & ZONEFS_ZONE_OPEN) || 70 (z->z_wpoffset > 0 && z->z_wpoffset < z->z_capacity)) { 71 if (!(z->z_flags & ZONEFS_ZONE_ACTIVE)) { 72 z->z_flags |= ZONEFS_ZONE_ACTIVE; 73 atomic_inc(&sbi->s_active_seq_files); 74 } 75 return; 76 } 77 78 out: 79 /* The zone is not active. If it was, update the active count */ 80 if (z->z_flags & ZONEFS_ZONE_ACTIVE) { 81 z->z_flags &= ~ZONEFS_ZONE_ACTIVE; 82 atomic_dec(&sbi->s_active_seq_files); 83 } 84 } 85 86 /* 87 * Manage the active zone count. Called with zi->i_truncate_mutex held. 88 */ 89 void zonefs_inode_account_active(struct inode *inode) 90 { 91 lockdep_assert_held(&ZONEFS_I(inode)->i_truncate_mutex); 92 93 return zonefs_account_active(inode->i_sb, zonefs_inode_zone(inode)); 94 } 95 96 /* 97 * Execute a zone management operation. 98 */ 99 static int zonefs_zone_mgmt(struct super_block *sb, 100 struct zonefs_zone *z, enum req_op op) 101 { 102 int ret; 103 104 /* 105 * With ZNS drives, closing an explicitly open zone that has not been 106 * written will change the zone state to "closed", that is, the zone 107 * will remain active. Since this can then cause failure of explicit 108 * open operation on other zones if the drive active zone resources 109 * are exceeded, make sure that the zone does not remain active by 110 * resetting it. 111 */ 112 if (op == REQ_OP_ZONE_CLOSE && !z->z_wpoffset) 113 op = REQ_OP_ZONE_RESET; 114 115 trace_zonefs_zone_mgmt(sb, z, op); 116 ret = blkdev_zone_mgmt(sb->s_bdev, op, z->z_sector, 117 z->z_size >> SECTOR_SHIFT); 118 if (ret) { 119 zonefs_err(sb, 120 "Zone management operation %s at %llu failed %d\n", 121 blk_op_str(op), z->z_sector, ret); 122 return ret; 123 } 124 125 return 0; 126 } 127 128 int zonefs_inode_zone_mgmt(struct inode *inode, enum req_op op) 129 { 130 lockdep_assert_held(&ZONEFS_I(inode)->i_truncate_mutex); 131 132 return zonefs_zone_mgmt(inode->i_sb, zonefs_inode_zone(inode), op); 133 } 134 135 void zonefs_i_size_write(struct inode *inode, loff_t isize) 136 { 137 struct zonefs_zone *z = zonefs_inode_zone(inode); 138 139 i_size_write(inode, isize); 140 141 /* 142 * A full zone is no longer open/active and does not need 143 * explicit closing. 144 */ 145 if (isize >= z->z_capacity) { 146 struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb); 147 148 if (z->z_flags & ZONEFS_ZONE_ACTIVE) 149 atomic_dec(&sbi->s_active_seq_files); 150 z->z_flags &= ~(ZONEFS_ZONE_OPEN | ZONEFS_ZONE_ACTIVE); 151 } 152 } 153 154 void zonefs_update_stats(struct inode *inode, loff_t new_isize) 155 { 156 struct super_block *sb = inode->i_sb; 157 struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 158 loff_t old_isize = i_size_read(inode); 159 loff_t nr_blocks; 160 161 if (new_isize == old_isize) 162 return; 163 164 spin_lock(&sbi->s_lock); 165 166 /* 167 * This may be called for an update after an IO error. 168 * So beware of the values seen. 169 */ 170 if (new_isize < old_isize) { 171 nr_blocks = (old_isize - new_isize) >> sb->s_blocksize_bits; 172 if (sbi->s_used_blocks > nr_blocks) 173 sbi->s_used_blocks -= nr_blocks; 174 else 175 sbi->s_used_blocks = 0; 176 } else { 177 sbi->s_used_blocks += 178 (new_isize - old_isize) >> sb->s_blocksize_bits; 179 if (sbi->s_used_blocks > sbi->s_blocks) 180 sbi->s_used_blocks = sbi->s_blocks; 181 } 182 183 spin_unlock(&sbi->s_lock); 184 } 185 186 /* 187 * Check a zone condition. Return the amount of written (and still readable) 188 * data in the zone. 189 */ 190 static loff_t zonefs_check_zone_condition(struct super_block *sb, 191 struct zonefs_zone *z, 192 struct blk_zone *zone) 193 { 194 switch (zone->cond) { 195 case BLK_ZONE_COND_OFFLINE: 196 zonefs_warn(sb, "Zone %llu: offline zone\n", 197 z->z_sector); 198 z->z_flags |= ZONEFS_ZONE_OFFLINE; 199 return 0; 200 case BLK_ZONE_COND_READONLY: 201 /* 202 * The write pointer of read-only zones is invalid, so we cannot 203 * determine the zone wpoffset (inode size). We thus keep the 204 * zone wpoffset as is, which leads to an empty file 205 * (wpoffset == 0) on mount. For a runtime error, this keeps 206 * the inode size as it was when last updated so that the user 207 * can recover data. 208 */ 209 zonefs_warn(sb, "Zone %llu: read-only zone\n", 210 z->z_sector); 211 z->z_flags |= ZONEFS_ZONE_READONLY; 212 if (zonefs_zone_is_cnv(z)) 213 return z->z_capacity; 214 return z->z_wpoffset; 215 case BLK_ZONE_COND_FULL: 216 /* The write pointer of full zones is invalid. */ 217 return z->z_capacity; 218 default: 219 if (zonefs_zone_is_cnv(z)) 220 return z->z_capacity; 221 return (zone->wp - zone->start) << SECTOR_SHIFT; 222 } 223 } 224 225 /* 226 * Check a zone condition and adjust its inode access permissions for 227 * offline and readonly zones. 228 */ 229 static void zonefs_inode_update_mode(struct inode *inode) 230 { 231 struct zonefs_zone *z = zonefs_inode_zone(inode); 232 233 if (z->z_flags & ZONEFS_ZONE_OFFLINE) { 234 /* Offline zones cannot be read nor written */ 235 inode->i_flags |= S_IMMUTABLE; 236 inode->i_mode &= ~0777; 237 } else if (z->z_flags & ZONEFS_ZONE_READONLY) { 238 /* Readonly zones cannot be written */ 239 inode->i_flags |= S_IMMUTABLE; 240 if (z->z_flags & ZONEFS_ZONE_INIT_MODE) 241 inode->i_mode &= ~0777; 242 else 243 inode->i_mode &= ~0222; 244 } 245 246 z->z_flags &= ~ZONEFS_ZONE_INIT_MODE; 247 z->z_mode = inode->i_mode; 248 } 249 250 static int zonefs_io_error_cb(struct blk_zone *zone, unsigned int idx, 251 void *data) 252 { 253 struct blk_zone *z = data; 254 255 *z = *zone; 256 return 0; 257 } 258 259 static void zonefs_handle_io_error(struct inode *inode, struct blk_zone *zone, 260 bool write) 261 { 262 struct zonefs_zone *z = zonefs_inode_zone(inode); 263 struct super_block *sb = inode->i_sb; 264 struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 265 loff_t isize, data_size; 266 267 /* 268 * Check the zone condition: if the zone is not "bad" (offline or 269 * read-only), read errors are simply signaled to the IO issuer as long 270 * as there is no inconsistency between the inode size and the amount of 271 * data written in the zone (data_size). 272 */ 273 data_size = zonefs_check_zone_condition(sb, z, zone); 274 isize = i_size_read(inode); 275 if (!(z->z_flags & (ZONEFS_ZONE_READONLY | ZONEFS_ZONE_OFFLINE)) && 276 !write && isize == data_size) 277 return; 278 279 /* 280 * At this point, we detected either a bad zone or an inconsistency 281 * between the inode size and the amount of data written in the zone. 282 * For the latter case, the cause may be a write IO error or an external 283 * action on the device. Two error patterns exist: 284 * 1) The inode size is lower than the amount of data in the zone: 285 * a write operation partially failed and data was written at the end 286 * of the file. This can happen in the case of a large direct IO 287 * needing several BIOs and/or write requests to be processed. 288 * 2) The inode size is larger than the amount of data in the zone: 289 * this can happen with a deferred write error with the use of the 290 * device side write cache after getting successful write IO 291 * completions. Other possibilities are (a) an external corruption, 292 * e.g. an application reset the zone directly, or (b) the device 293 * has a serious problem (e.g. firmware bug). 294 * 295 * In all cases, warn about inode size inconsistency and handle the 296 * IO error according to the zone condition and to the mount options. 297 */ 298 if (isize != data_size) 299 zonefs_warn(sb, 300 "inode %llu: invalid size %lld (should be %lld)\n", 301 inode->i_ino, isize, data_size); 302 303 /* 304 * First handle bad zones signaled by hardware. The mount options 305 * errors=zone-ro and errors=zone-offline result in changing the 306 * zone condition to read-only and offline respectively, as if the 307 * condition was signaled by the hardware. 308 */ 309 if ((z->z_flags & ZONEFS_ZONE_OFFLINE) || 310 (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL)) { 311 zonefs_warn(sb, "inode %llu: read/write access disabled\n", 312 inode->i_ino); 313 if (!(z->z_flags & ZONEFS_ZONE_OFFLINE)) 314 z->z_flags |= ZONEFS_ZONE_OFFLINE; 315 zonefs_inode_update_mode(inode); 316 data_size = 0; 317 } else if ((z->z_flags & ZONEFS_ZONE_READONLY) || 318 (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO)) { 319 zonefs_warn(sb, "inode %llu: write access disabled\n", 320 inode->i_ino); 321 if (!(z->z_flags & ZONEFS_ZONE_READONLY)) 322 z->z_flags |= ZONEFS_ZONE_READONLY; 323 zonefs_inode_update_mode(inode); 324 data_size = isize; 325 } else if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO && 326 data_size > isize) { 327 /* Do not expose garbage data */ 328 data_size = isize; 329 } 330 331 /* 332 * If the filesystem is mounted with the explicit-open mount option, we 333 * need to clear the ZONEFS_ZONE_OPEN flag if the zone transitioned to 334 * the read-only or offline condition, to avoid attempting an explicit 335 * close of the zone when the inode file is closed. 336 */ 337 if ((sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) && 338 (z->z_flags & (ZONEFS_ZONE_READONLY | ZONEFS_ZONE_OFFLINE))) 339 z->z_flags &= ~ZONEFS_ZONE_OPEN; 340 341 /* 342 * If error=remount-ro was specified, any error result in remounting 343 * the volume as read-only. 344 */ 345 if ((sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO) && !sb_rdonly(sb)) { 346 zonefs_warn(sb, "remounting filesystem read-only\n"); 347 sb->s_flags |= SB_RDONLY; 348 } 349 350 /* 351 * Update block usage stats and the inode size to prevent access to 352 * invalid data. 353 */ 354 zonefs_update_stats(inode, data_size); 355 zonefs_i_size_write(inode, data_size); 356 z->z_wpoffset = data_size; 357 zonefs_inode_account_active(inode); 358 } 359 360 /* 361 * When an file IO error occurs, check the file zone to see if there is a change 362 * in the zone condition (e.g. offline or read-only). For a failed write to a 363 * sequential zone, the zone write pointer position must also be checked to 364 * eventually correct the file size and zonefs inode write pointer offset 365 * (which can be out of sync with the drive due to partial write failures). 366 */ 367 void __zonefs_io_error(struct inode *inode, bool write) 368 { 369 struct zonefs_zone *z = zonefs_inode_zone(inode); 370 struct super_block *sb = inode->i_sb; 371 unsigned int noio_flag; 372 struct blk_zone zone; 373 int ret; 374 375 /* 376 * Conventional zone have no write pointer and cannot become read-only 377 * or offline. So simply fake a report for a single or aggregated zone 378 * and let zonefs_handle_io_error() correct the zone inode information 379 * according to the mount options. 380 */ 381 if (!zonefs_zone_is_seq(z)) { 382 zone.start = z->z_sector; 383 zone.len = z->z_size >> SECTOR_SHIFT; 384 zone.wp = zone.start + zone.len; 385 zone.type = BLK_ZONE_TYPE_CONVENTIONAL; 386 zone.cond = BLK_ZONE_COND_NOT_WP; 387 zone.capacity = zone.len; 388 goto handle_io_error; 389 } 390 391 /* 392 * Memory allocations in blkdev_report_zones() can trigger a memory 393 * reclaim which may in turn cause a recursion into zonefs as well as 394 * struct request allocations for the same device. The former case may 395 * end up in a deadlock on the inode truncate mutex, while the latter 396 * may prevent IO forward progress. Executing the report zones under 397 * the GFP_NOIO context avoids both problems. 398 */ 399 noio_flag = memalloc_noio_save(); 400 ret = blkdev_report_zones(sb->s_bdev, z->z_sector, 1, 401 zonefs_io_error_cb, &zone); 402 memalloc_noio_restore(noio_flag); 403 404 if (ret != 1) { 405 zonefs_err(sb, "Get inode %llu zone information failed %d\n", 406 inode->i_ino, ret); 407 zonefs_warn(sb, "remounting filesystem read-only\n"); 408 sb->s_flags |= SB_RDONLY; 409 return; 410 } 411 412 handle_io_error: 413 zonefs_handle_io_error(inode, &zone, write); 414 } 415 416 static struct kmem_cache *zonefs_inode_cachep; 417 418 static struct inode *zonefs_alloc_inode(struct super_block *sb) 419 { 420 struct zonefs_inode_info *zi; 421 422 zi = alloc_inode_sb(sb, zonefs_inode_cachep, GFP_KERNEL); 423 if (!zi) 424 return NULL; 425 426 inode_init_once(&zi->i_vnode); 427 mutex_init(&zi->i_truncate_mutex); 428 zi->i_wr_refcnt = 0; 429 430 return &zi->i_vnode; 431 } 432 433 static void zonefs_free_inode(struct inode *inode) 434 { 435 kmem_cache_free(zonefs_inode_cachep, ZONEFS_I(inode)); 436 } 437 438 /* 439 * File system stat. 440 */ 441 static int zonefs_statfs(struct dentry *dentry, struct kstatfs *buf) 442 { 443 struct super_block *sb = dentry->d_sb; 444 struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 445 enum zonefs_ztype t; 446 447 buf->f_type = ZONEFS_MAGIC; 448 buf->f_bsize = sb->s_blocksize; 449 buf->f_namelen = ZONEFS_NAME_MAX; 450 451 spin_lock(&sbi->s_lock); 452 453 buf->f_blocks = sbi->s_blocks; 454 if (WARN_ON(sbi->s_used_blocks > sbi->s_blocks)) 455 buf->f_bfree = 0; 456 else 457 buf->f_bfree = buf->f_blocks - sbi->s_used_blocks; 458 buf->f_bavail = buf->f_bfree; 459 460 for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) { 461 if (sbi->s_zgroup[t].g_nr_zones) 462 buf->f_files += sbi->s_zgroup[t].g_nr_zones + 1; 463 } 464 buf->f_ffree = 0; 465 466 spin_unlock(&sbi->s_lock); 467 468 buf->f_fsid = uuid_to_fsid(sbi->s_uuid.b); 469 470 return 0; 471 } 472 473 enum { 474 Opt_errors, Opt_explicit_open, 475 }; 476 477 struct zonefs_context { 478 unsigned long s_mount_opts; 479 }; 480 481 static const struct constant_table zonefs_param_errors[] = { 482 {"remount-ro", ZONEFS_MNTOPT_ERRORS_RO}, 483 {"zone-ro", ZONEFS_MNTOPT_ERRORS_ZRO}, 484 {"zone-offline", ZONEFS_MNTOPT_ERRORS_ZOL}, 485 {"repair", ZONEFS_MNTOPT_ERRORS_REPAIR}, 486 {} 487 }; 488 489 static const struct fs_parameter_spec zonefs_param_spec[] = { 490 fsparam_enum ("errors", Opt_errors, zonefs_param_errors), 491 fsparam_flag ("explicit-open", Opt_explicit_open), 492 {} 493 }; 494 495 static int zonefs_parse_param(struct fs_context *fc, struct fs_parameter *param) 496 { 497 struct zonefs_context *ctx = fc->fs_private; 498 struct fs_parse_result result; 499 int opt; 500 501 opt = fs_parse(fc, zonefs_param_spec, param, &result); 502 if (opt < 0) 503 return opt; 504 505 switch (opt) { 506 case Opt_errors: 507 ctx->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK; 508 ctx->s_mount_opts |= result.uint_32; 509 break; 510 case Opt_explicit_open: 511 ctx->s_mount_opts |= ZONEFS_MNTOPT_EXPLICIT_OPEN; 512 break; 513 default: 514 return -EINVAL; 515 } 516 517 return 0; 518 } 519 520 static int zonefs_show_options(struct seq_file *seq, struct dentry *root) 521 { 522 struct zonefs_sb_info *sbi = ZONEFS_SB(root->d_sb); 523 524 if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO) 525 seq_puts(seq, ",errors=remount-ro"); 526 if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO) 527 seq_puts(seq, ",errors=zone-ro"); 528 if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL) 529 seq_puts(seq, ",errors=zone-offline"); 530 if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_REPAIR) 531 seq_puts(seq, ",errors=repair"); 532 533 return 0; 534 } 535 536 static int zonefs_inode_setattr(struct mnt_idmap *idmap, 537 struct dentry *dentry, struct iattr *iattr) 538 { 539 struct inode *inode = d_inode(dentry); 540 int ret; 541 542 if (unlikely(IS_IMMUTABLE(inode))) 543 return -EPERM; 544 545 ret = setattr_prepare(&nop_mnt_idmap, dentry, iattr); 546 if (ret) 547 return ret; 548 549 /* 550 * Since files and directories cannot be created nor deleted, do not 551 * allow setting any write attributes on the sub-directories grouping 552 * files by zone type. 553 */ 554 if ((iattr->ia_valid & ATTR_MODE) && S_ISDIR(inode->i_mode) && 555 (iattr->ia_mode & 0222)) 556 return -EPERM; 557 558 if (((iattr->ia_valid & ATTR_UID) && 559 !uid_eq(iattr->ia_uid, inode->i_uid)) || 560 ((iattr->ia_valid & ATTR_GID) && 561 !gid_eq(iattr->ia_gid, inode->i_gid))) { 562 ret = dquot_transfer(&nop_mnt_idmap, inode, iattr); 563 if (ret) 564 return ret; 565 } 566 567 if (iattr->ia_valid & ATTR_SIZE) { 568 ret = zonefs_file_truncate(inode, iattr->ia_size); 569 if (ret) 570 return ret; 571 } 572 573 setattr_copy(&nop_mnt_idmap, inode, iattr); 574 575 if (S_ISREG(inode->i_mode)) { 576 struct zonefs_zone *z = zonefs_inode_zone(inode); 577 578 z->z_mode = inode->i_mode; 579 z->z_uid = inode->i_uid; 580 z->z_gid = inode->i_gid; 581 } 582 583 return 0; 584 } 585 586 static const struct inode_operations zonefs_file_inode_operations = { 587 .setattr = zonefs_inode_setattr, 588 }; 589 590 static long zonefs_fname_to_fno(const struct qstr *fname) 591 { 592 const char *name = fname->name; 593 unsigned int len = fname->len; 594 long fno = 0, shift = 1; 595 const char *rname; 596 char c = *name; 597 unsigned int i; 598 599 /* 600 * File names are always a base-10 number string without any 601 * leading 0s. 602 */ 603 if (!isdigit(c)) 604 return -ENOENT; 605 606 if (len > 1 && c == '0') 607 return -ENOENT; 608 609 if (len == 1) 610 return c - '0'; 611 612 for (i = 0, rname = name + len - 1; i < len; i++, rname--) { 613 long digit; 614 615 c = *rname; 616 if (!isdigit(c)) 617 return -ENOENT; 618 digit = (c - '0') * shift; 619 if (check_add_overflow(fno, digit, &fno)) 620 return -ENOENT; 621 shift *= 10; 622 } 623 624 return fno; 625 } 626 627 static struct inode *zonefs_get_file_inode(struct inode *dir, 628 struct dentry *dentry) 629 { 630 struct zonefs_zone_group *zgroup = dir->i_private; 631 struct super_block *sb = dir->i_sb; 632 struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 633 struct zonefs_zone *z; 634 struct inode *inode; 635 ino_t ino; 636 long fno; 637 638 /* Get the file number from the file name */ 639 fno = zonefs_fname_to_fno(&dentry->d_name); 640 if (fno < 0) 641 return ERR_PTR(fno); 642 643 if (!zgroup->g_nr_zones || fno >= zgroup->g_nr_zones) 644 return ERR_PTR(-ENOENT); 645 646 z = &zgroup->g_zones[fno]; 647 ino = z->z_sector >> sbi->s_zone_sectors_shift; 648 inode = iget_locked(sb, ino); 649 if (!inode) 650 return ERR_PTR(-ENOMEM); 651 if (!(inode_state_read_once(inode) & I_NEW)) { 652 WARN_ON_ONCE(inode->i_private != z); 653 return inode; 654 } 655 656 inode->i_ino = ino; 657 inode->i_mode = z->z_mode; 658 inode_set_mtime_to_ts(inode, 659 inode_set_atime_to_ts(inode, inode_set_ctime_to_ts(inode, inode_get_ctime(dir)))); 660 inode->i_uid = z->z_uid; 661 inode->i_gid = z->z_gid; 662 inode->i_size = z->z_wpoffset; 663 inode->i_blocks = z->z_capacity >> SECTOR_SHIFT; 664 inode->i_private = z; 665 666 inode->i_op = &zonefs_file_inode_operations; 667 inode->i_fop = &zonefs_file_operations; 668 inode->i_mapping->a_ops = &zonefs_file_aops; 669 mapping_set_large_folios(inode->i_mapping); 670 671 /* Update the inode access rights depending on the zone condition */ 672 zonefs_inode_update_mode(inode); 673 674 unlock_new_inode(inode); 675 676 return inode; 677 } 678 679 static struct inode *zonefs_get_zgroup_inode(struct super_block *sb, 680 enum zonefs_ztype ztype) 681 { 682 struct inode *root = d_inode(sb->s_root); 683 struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 684 struct inode *inode; 685 ino_t ino = bdev_nr_zones(sb->s_bdev) + ztype + 1; 686 687 inode = iget_locked(sb, ino); 688 if (!inode) 689 return ERR_PTR(-ENOMEM); 690 if (!(inode_state_read_once(inode) & I_NEW)) 691 return inode; 692 693 inode->i_ino = ino; 694 inode_init_owner(&nop_mnt_idmap, inode, root, S_IFDIR | 0555); 695 inode->i_size = sbi->s_zgroup[ztype].g_nr_zones; 696 inode_set_mtime_to_ts(inode, 697 inode_set_atime_to_ts(inode, inode_set_ctime_to_ts(inode, inode_get_ctime(root)))); 698 inode->i_private = &sbi->s_zgroup[ztype]; 699 set_nlink(inode, 2); 700 701 inode->i_op = &zonefs_dir_inode_operations; 702 inode->i_fop = &zonefs_dir_operations; 703 704 unlock_new_inode(inode); 705 706 return inode; 707 } 708 709 710 static struct inode *zonefs_get_dir_inode(struct inode *dir, 711 struct dentry *dentry) 712 { 713 struct super_block *sb = dir->i_sb; 714 struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 715 const char *name = dentry->d_name.name; 716 enum zonefs_ztype ztype; 717 718 /* 719 * We only need to check for the "seq" directory and 720 * the "cnv" directory if we have conventional zones. 721 */ 722 if (dentry->d_name.len != 3) 723 return ERR_PTR(-ENOENT); 724 725 for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) { 726 if (sbi->s_zgroup[ztype].g_nr_zones && 727 memcmp(name, zonefs_zgroup_name(ztype), 3) == 0) 728 break; 729 } 730 if (ztype == ZONEFS_ZTYPE_MAX) 731 return ERR_PTR(-ENOENT); 732 733 return zonefs_get_zgroup_inode(sb, ztype); 734 } 735 736 static struct dentry *zonefs_lookup(struct inode *dir, struct dentry *dentry, 737 unsigned int flags) 738 { 739 struct inode *inode; 740 741 if (dentry->d_name.len > ZONEFS_NAME_MAX) 742 return ERR_PTR(-ENAMETOOLONG); 743 744 if (dir == d_inode(dir->i_sb->s_root)) 745 inode = zonefs_get_dir_inode(dir, dentry); 746 else 747 inode = zonefs_get_file_inode(dir, dentry); 748 749 return d_splice_alias(inode, dentry); 750 } 751 752 static int zonefs_readdir_root(struct file *file, struct dir_context *ctx) 753 { 754 struct inode *inode = file_inode(file); 755 struct super_block *sb = inode->i_sb; 756 struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 757 enum zonefs_ztype ztype = ZONEFS_ZTYPE_CNV; 758 ino_t base_ino = bdev_nr_zones(sb->s_bdev) + 1; 759 760 if (ctx->pos >= inode->i_size) 761 return 0; 762 763 if (!dir_emit_dots(file, ctx)) 764 return 0; 765 766 if (ctx->pos == 2) { 767 if (!sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones) 768 ztype = ZONEFS_ZTYPE_SEQ; 769 770 if (!dir_emit(ctx, zonefs_zgroup_name(ztype), 3, 771 base_ino + ztype, DT_DIR)) 772 return 0; 773 ctx->pos++; 774 } 775 776 if (ctx->pos == 3 && ztype != ZONEFS_ZTYPE_SEQ) { 777 ztype = ZONEFS_ZTYPE_SEQ; 778 if (!dir_emit(ctx, zonefs_zgroup_name(ztype), 3, 779 base_ino + ztype, DT_DIR)) 780 return 0; 781 ctx->pos++; 782 } 783 784 return 0; 785 } 786 787 static int zonefs_readdir_zgroup(struct file *file, 788 struct dir_context *ctx) 789 { 790 struct inode *inode = file_inode(file); 791 struct zonefs_zone_group *zgroup = inode->i_private; 792 struct super_block *sb = inode->i_sb; 793 struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 794 struct zonefs_zone *z; 795 int fname_len; 796 char *fname; 797 ino_t ino; 798 int f; 799 800 /* 801 * The size of zone group directories is equal to the number 802 * of zone files in the group and does note include the "." and 803 * ".." entries. Hence the "+ 2" here. 804 */ 805 if (ctx->pos >= inode->i_size + 2) 806 return 0; 807 808 if (!dir_emit_dots(file, ctx)) 809 return 0; 810 811 fname = kmalloc(ZONEFS_NAME_MAX, GFP_KERNEL); 812 if (!fname) 813 return -ENOMEM; 814 815 for (f = ctx->pos - 2; f < zgroup->g_nr_zones; f++) { 816 z = &zgroup->g_zones[f]; 817 ino = z->z_sector >> sbi->s_zone_sectors_shift; 818 fname_len = snprintf(fname, ZONEFS_NAME_MAX - 1, "%u", f); 819 if (!dir_emit(ctx, fname, fname_len, ino, DT_REG)) 820 break; 821 ctx->pos++; 822 } 823 824 kfree(fname); 825 826 return 0; 827 } 828 829 static int zonefs_readdir(struct file *file, struct dir_context *ctx) 830 { 831 struct inode *inode = file_inode(file); 832 833 if (inode == d_inode(inode->i_sb->s_root)) 834 return zonefs_readdir_root(file, ctx); 835 836 return zonefs_readdir_zgroup(file, ctx); 837 } 838 839 const struct inode_operations zonefs_dir_inode_operations = { 840 .lookup = zonefs_lookup, 841 .setattr = zonefs_inode_setattr, 842 }; 843 844 const struct file_operations zonefs_dir_operations = { 845 .llseek = generic_file_llseek, 846 .read = generic_read_dir, 847 .iterate_shared = zonefs_readdir, 848 }; 849 850 struct zonefs_zone_data { 851 struct super_block *sb; 852 unsigned int nr_zones[ZONEFS_ZTYPE_MAX]; 853 sector_t cnv_zone_start; 854 struct blk_zone *zones; 855 }; 856 857 static int zonefs_get_zone_info_cb(struct blk_zone *zone, unsigned int idx, 858 void *data) 859 { 860 struct zonefs_zone_data *zd = data; 861 struct super_block *sb = zd->sb; 862 struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 863 864 /* 865 * We do not care about the first zone: it contains the super block 866 * and not exposed as a file. 867 */ 868 if (!idx) 869 return 0; 870 871 /* 872 * Count the number of zones that will be exposed as files. 873 * For sequential zones, we always have as many files as zones. 874 * FOr conventional zones, the number of files depends on if we have 875 * conventional zones aggregation enabled. 876 */ 877 switch (zone->type) { 878 case BLK_ZONE_TYPE_CONVENTIONAL: 879 if (sbi->s_features & ZONEFS_F_AGGRCNV) { 880 /* One file per set of contiguous conventional zones */ 881 if (!(sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones) || 882 zone->start != zd->cnv_zone_start) 883 sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones++; 884 zd->cnv_zone_start = zone->start + zone->len; 885 } else { 886 /* One file per zone */ 887 sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones++; 888 } 889 break; 890 case BLK_ZONE_TYPE_SEQWRITE_REQ: 891 case BLK_ZONE_TYPE_SEQWRITE_PREF: 892 sbi->s_zgroup[ZONEFS_ZTYPE_SEQ].g_nr_zones++; 893 break; 894 default: 895 zonefs_err(zd->sb, "Unsupported zone type 0x%x\n", 896 zone->type); 897 return -EIO; 898 } 899 900 memcpy(&zd->zones[idx], zone, sizeof(struct blk_zone)); 901 902 return 0; 903 } 904 905 static int zonefs_get_zone_info(struct zonefs_zone_data *zd) 906 { 907 struct block_device *bdev = zd->sb->s_bdev; 908 int ret; 909 910 zd->zones = kvzalloc_objs(struct blk_zone, bdev_nr_zones(bdev)); 911 if (!zd->zones) 912 return -ENOMEM; 913 914 /* Get zones information from the device */ 915 ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES, 916 zonefs_get_zone_info_cb, zd); 917 if (ret < 0) { 918 zonefs_err(zd->sb, "Zone report failed %d\n", ret); 919 return ret; 920 } 921 922 if (ret != bdev_nr_zones(bdev)) { 923 zonefs_err(zd->sb, "Invalid zone report (%d/%u zones)\n", 924 ret, bdev_nr_zones(bdev)); 925 return -EIO; 926 } 927 928 return 0; 929 } 930 931 static inline void zonefs_free_zone_info(struct zonefs_zone_data *zd) 932 { 933 kvfree(zd->zones); 934 } 935 936 /* 937 * Create a zone group and populate it with zone files. 938 */ 939 static int zonefs_init_zgroup(struct super_block *sb, 940 struct zonefs_zone_data *zd, 941 enum zonefs_ztype ztype) 942 { 943 struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 944 struct zonefs_zone_group *zgroup = &sbi->s_zgroup[ztype]; 945 struct blk_zone *zone, *next, *end; 946 struct zonefs_zone *z; 947 unsigned int n = 0; 948 int ret; 949 950 /* Allocate the zone group. If it is empty, we have nothing to do. */ 951 if (!zgroup->g_nr_zones) 952 return 0; 953 954 zgroup->g_zones = kvzalloc_objs(struct zonefs_zone, zgroup->g_nr_zones); 955 if (!zgroup->g_zones) 956 return -ENOMEM; 957 958 /* 959 * Initialize the zone groups using the device zone information. 960 * We always skip the first zone as it contains the super block 961 * and is not use to back a file. 962 */ 963 end = zd->zones + bdev_nr_zones(sb->s_bdev); 964 for (zone = &zd->zones[1]; zone < end; zone = next) { 965 966 next = zone + 1; 967 if (zonefs_zone_type(zone) != ztype) 968 continue; 969 970 if (WARN_ON_ONCE(n >= zgroup->g_nr_zones)) 971 return -EINVAL; 972 973 /* 974 * For conventional zones, contiguous zones can be aggregated 975 * together to form larger files. Note that this overwrites the 976 * length of the first zone of the set of contiguous zones 977 * aggregated together. If one offline or read-only zone is 978 * found, assume that all zones aggregated have the same 979 * condition. 980 */ 981 if (ztype == ZONEFS_ZTYPE_CNV && 982 (sbi->s_features & ZONEFS_F_AGGRCNV)) { 983 for (; next < end; next++) { 984 if (zonefs_zone_type(next) != ztype) 985 break; 986 zone->len += next->len; 987 zone->capacity += next->capacity; 988 if (next->cond == BLK_ZONE_COND_READONLY && 989 zone->cond != BLK_ZONE_COND_OFFLINE) 990 zone->cond = BLK_ZONE_COND_READONLY; 991 else if (next->cond == BLK_ZONE_COND_OFFLINE) 992 zone->cond = BLK_ZONE_COND_OFFLINE; 993 } 994 } 995 996 z = &zgroup->g_zones[n]; 997 if (ztype == ZONEFS_ZTYPE_CNV) 998 z->z_flags |= ZONEFS_ZONE_CNV; 999 z->z_sector = zone->start; 1000 z->z_size = zone->len << SECTOR_SHIFT; 1001 if (z->z_size > bdev_zone_sectors(sb->s_bdev) << SECTOR_SHIFT && 1002 !(sbi->s_features & ZONEFS_F_AGGRCNV)) { 1003 zonefs_err(sb, 1004 "Invalid zone size %llu (device zone sectors %llu)\n", 1005 z->z_size, 1006 bdev_zone_sectors(sb->s_bdev) << SECTOR_SHIFT); 1007 return -EINVAL; 1008 } 1009 1010 z->z_capacity = min_t(loff_t, MAX_LFS_FILESIZE, 1011 zone->capacity << SECTOR_SHIFT); 1012 z->z_wpoffset = zonefs_check_zone_condition(sb, z, zone); 1013 1014 z->z_mode = S_IFREG | sbi->s_perm; 1015 z->z_uid = sbi->s_uid; 1016 z->z_gid = sbi->s_gid; 1017 1018 /* 1019 * Let zonefs_inode_update_mode() know that we will need 1020 * special initialization of the inode mode the first time 1021 * it is accessed. 1022 */ 1023 z->z_flags |= ZONEFS_ZONE_INIT_MODE; 1024 1025 sb->s_maxbytes = max(z->z_capacity, sb->s_maxbytes); 1026 sbi->s_blocks += z->z_capacity >> sb->s_blocksize_bits; 1027 sbi->s_used_blocks += z->z_wpoffset >> sb->s_blocksize_bits; 1028 1029 /* 1030 * For sequential zones, make sure that any open zone is closed 1031 * first to ensure that the initial number of open zones is 0, 1032 * in sync with the open zone accounting done when the mount 1033 * option ZONEFS_MNTOPT_EXPLICIT_OPEN is used. 1034 */ 1035 if (ztype == ZONEFS_ZTYPE_SEQ && 1036 (zone->cond == BLK_ZONE_COND_IMP_OPEN || 1037 zone->cond == BLK_ZONE_COND_EXP_OPEN)) { 1038 ret = zonefs_zone_mgmt(sb, z, REQ_OP_ZONE_CLOSE); 1039 if (ret) 1040 return ret; 1041 } 1042 1043 zonefs_account_active(sb, z); 1044 1045 n++; 1046 } 1047 1048 if (WARN_ON_ONCE(n != zgroup->g_nr_zones)) 1049 return -EINVAL; 1050 1051 zonefs_info(sb, "Zone group \"%s\" has %u file%s\n", 1052 zonefs_zgroup_name(ztype), 1053 zgroup->g_nr_zones, 1054 str_plural(zgroup->g_nr_zones)); 1055 1056 return 0; 1057 } 1058 1059 static void zonefs_free_zgroups(struct super_block *sb) 1060 { 1061 struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 1062 enum zonefs_ztype ztype; 1063 1064 if (!sbi) 1065 return; 1066 1067 for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) { 1068 kvfree(sbi->s_zgroup[ztype].g_zones); 1069 sbi->s_zgroup[ztype].g_zones = NULL; 1070 } 1071 } 1072 1073 /* 1074 * Create a zone group and populate it with zone files. 1075 */ 1076 static int zonefs_init_zgroups(struct super_block *sb) 1077 { 1078 struct zonefs_zone_data zd; 1079 enum zonefs_ztype ztype; 1080 int ret; 1081 1082 /* First get the device zone information */ 1083 memset(&zd, 0, sizeof(struct zonefs_zone_data)); 1084 zd.sb = sb; 1085 ret = zonefs_get_zone_info(&zd); 1086 if (ret) 1087 goto cleanup; 1088 1089 /* Allocate and initialize the zone groups */ 1090 for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) { 1091 ret = zonefs_init_zgroup(sb, &zd, ztype); 1092 if (ret) { 1093 zonefs_info(sb, 1094 "Zone group \"%s\" initialization failed\n", 1095 zonefs_zgroup_name(ztype)); 1096 break; 1097 } 1098 } 1099 1100 cleanup: 1101 zonefs_free_zone_info(&zd); 1102 if (ret) 1103 zonefs_free_zgroups(sb); 1104 1105 return ret; 1106 } 1107 1108 /* 1109 * Read super block information from the device. 1110 */ 1111 static int zonefs_read_super(struct super_block *sb) 1112 { 1113 struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 1114 struct zonefs_super *super; 1115 u32 crc, stored_crc; 1116 int ret; 1117 1118 super = kmalloc(ZONEFS_SUPER_SIZE, GFP_KERNEL); 1119 if (!super) 1120 return -ENOMEM; 1121 1122 ret = bdev_rw_virt(sb->s_bdev, 0, super, ZONEFS_SUPER_SIZE, 1123 REQ_OP_READ); 1124 if (ret) 1125 goto free_super; 1126 1127 ret = -EINVAL; 1128 if (le32_to_cpu(super->s_magic) != ZONEFS_MAGIC) 1129 goto free_super; 1130 1131 stored_crc = le32_to_cpu(super->s_crc); 1132 super->s_crc = 0; 1133 crc = crc32(~0U, (unsigned char *)super, sizeof(struct zonefs_super)); 1134 if (crc != stored_crc) { 1135 zonefs_err(sb, "Invalid checksum (Expected 0x%08x, got 0x%08x)", 1136 crc, stored_crc); 1137 goto free_super; 1138 } 1139 1140 sbi->s_features = le64_to_cpu(super->s_features); 1141 if (sbi->s_features & ~ZONEFS_F_DEFINED_FEATURES) { 1142 zonefs_err(sb, "Unknown features set 0x%llx\n", 1143 sbi->s_features); 1144 goto free_super; 1145 } 1146 1147 if (sbi->s_features & ZONEFS_F_UID) { 1148 sbi->s_uid = make_kuid(current_user_ns(), 1149 le32_to_cpu(super->s_uid)); 1150 if (!uid_valid(sbi->s_uid)) { 1151 zonefs_err(sb, "Invalid UID feature\n"); 1152 goto free_super; 1153 } 1154 } 1155 1156 if (sbi->s_features & ZONEFS_F_GID) { 1157 sbi->s_gid = make_kgid(current_user_ns(), 1158 le32_to_cpu(super->s_gid)); 1159 if (!gid_valid(sbi->s_gid)) { 1160 zonefs_err(sb, "Invalid GID feature\n"); 1161 goto free_super; 1162 } 1163 } 1164 1165 if (sbi->s_features & ZONEFS_F_PERM) 1166 sbi->s_perm = le32_to_cpu(super->s_perm); 1167 1168 if (memchr_inv(super->s_reserved, 0, sizeof(super->s_reserved))) { 1169 zonefs_err(sb, "Reserved area is being used\n"); 1170 goto free_super; 1171 } 1172 1173 import_uuid(&sbi->s_uuid, super->s_uuid); 1174 ret = 0; 1175 1176 free_super: 1177 kfree(super); 1178 return ret; 1179 } 1180 1181 static const struct super_operations zonefs_sops = { 1182 .alloc_inode = zonefs_alloc_inode, 1183 .free_inode = zonefs_free_inode, 1184 .statfs = zonefs_statfs, 1185 .show_options = zonefs_show_options, 1186 }; 1187 1188 static int zonefs_get_zgroup_inodes(struct super_block *sb) 1189 { 1190 struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 1191 struct inode *dir_inode; 1192 enum zonefs_ztype ztype; 1193 1194 for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) { 1195 if (!sbi->s_zgroup[ztype].g_nr_zones) 1196 continue; 1197 1198 dir_inode = zonefs_get_zgroup_inode(sb, ztype); 1199 if (IS_ERR(dir_inode)) 1200 return PTR_ERR(dir_inode); 1201 1202 sbi->s_zgroup[ztype].g_inode = dir_inode; 1203 } 1204 1205 return 0; 1206 } 1207 1208 static void zonefs_release_zgroup_inodes(struct super_block *sb) 1209 { 1210 struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 1211 enum zonefs_ztype ztype; 1212 1213 if (!sbi) 1214 return; 1215 1216 for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) { 1217 if (sbi->s_zgroup[ztype].g_inode) { 1218 iput(sbi->s_zgroup[ztype].g_inode); 1219 sbi->s_zgroup[ztype].g_inode = NULL; 1220 } 1221 } 1222 } 1223 1224 /* 1225 * Check that the device is zoned. If it is, get the list of zones and create 1226 * sub-directories and files according to the device zone configuration and 1227 * format options. 1228 */ 1229 static int zonefs_fill_super(struct super_block *sb, struct fs_context *fc) 1230 { 1231 struct zonefs_sb_info *sbi; 1232 struct zonefs_context *ctx = fc->fs_private; 1233 struct inode *inode; 1234 enum zonefs_ztype ztype; 1235 int ret; 1236 1237 if (!bdev_is_zoned(sb->s_bdev)) { 1238 zonefs_err(sb, "Not a zoned block device\n"); 1239 return -EINVAL; 1240 } 1241 1242 /* 1243 * Initialize super block information: the maximum file size is updated 1244 * when the zone files are created so that the format option 1245 * ZONEFS_F_AGGRCNV which increases the maximum file size of a file 1246 * beyond the zone size is taken into account. 1247 */ 1248 sbi = kzalloc_obj(*sbi); 1249 if (!sbi) 1250 return -ENOMEM; 1251 1252 spin_lock_init(&sbi->s_lock); 1253 sb->s_fs_info = sbi; 1254 sb->s_magic = ZONEFS_MAGIC; 1255 sb->s_maxbytes = 0; 1256 sb->s_op = &zonefs_sops; 1257 sb->s_time_gran = 1; 1258 1259 /* 1260 * The block size is set to the device zone write granularity to ensure 1261 * that write operations are always aligned according to the device 1262 * interface constraints. 1263 */ 1264 sb_set_blocksize(sb, bdev_zone_write_granularity(sb->s_bdev)); 1265 sbi->s_zone_sectors_shift = ilog2(bdev_zone_sectors(sb->s_bdev)); 1266 sbi->s_uid = GLOBAL_ROOT_UID; 1267 sbi->s_gid = GLOBAL_ROOT_GID; 1268 sbi->s_perm = 0640; 1269 sbi->s_mount_opts = ctx->s_mount_opts; 1270 1271 atomic_set(&sbi->s_wro_seq_files, 0); 1272 sbi->s_max_wro_seq_files = bdev_max_open_zones(sb->s_bdev); 1273 atomic_set(&sbi->s_active_seq_files, 0); 1274 sbi->s_max_active_seq_files = bdev_max_active_zones(sb->s_bdev); 1275 1276 ret = zonefs_read_super(sb); 1277 if (ret) 1278 return ret; 1279 1280 zonefs_info(sb, "Mounting %u zones", bdev_nr_zones(sb->s_bdev)); 1281 1282 if (!sbi->s_max_wro_seq_files && 1283 !sbi->s_max_active_seq_files && 1284 sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) { 1285 zonefs_info(sb, 1286 "No open and active zone limits. Ignoring explicit_open mount option\n"); 1287 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_EXPLICIT_OPEN; 1288 } 1289 1290 /* Initialize the zone groups */ 1291 ret = zonefs_init_zgroups(sb); 1292 if (ret) 1293 goto cleanup; 1294 1295 /* Create the root directory inode */ 1296 ret = -ENOMEM; 1297 inode = new_inode(sb); 1298 if (!inode) 1299 goto cleanup; 1300 1301 inode->i_ino = bdev_nr_zones(sb->s_bdev); 1302 inode->i_mode = S_IFDIR | 0555; 1303 simple_inode_init_ts(inode); 1304 inode->i_op = &zonefs_dir_inode_operations; 1305 inode->i_fop = &zonefs_dir_operations; 1306 inode->i_size = 2; 1307 set_nlink(inode, 2); 1308 for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) { 1309 if (sbi->s_zgroup[ztype].g_nr_zones) { 1310 inc_nlink(inode); 1311 inode->i_size++; 1312 } 1313 } 1314 1315 sb->s_root = d_make_root(inode); 1316 if (!sb->s_root) 1317 goto cleanup; 1318 1319 /* 1320 * Take a reference on the zone groups directory inodes 1321 * to keep them in the inode cache. 1322 */ 1323 ret = zonefs_get_zgroup_inodes(sb); 1324 if (ret) 1325 goto cleanup; 1326 1327 ret = zonefs_sysfs_register(sb); 1328 if (ret) 1329 goto cleanup; 1330 1331 return 0; 1332 1333 cleanup: 1334 zonefs_release_zgroup_inodes(sb); 1335 zonefs_free_zgroups(sb); 1336 1337 return ret; 1338 } 1339 1340 static void zonefs_kill_super(struct super_block *sb) 1341 { 1342 struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 1343 1344 /* Release the reference on the zone group directory inodes */ 1345 zonefs_release_zgroup_inodes(sb); 1346 1347 kill_block_super(sb); 1348 1349 zonefs_sysfs_unregister(sb); 1350 zonefs_free_zgroups(sb); 1351 kfree(sbi); 1352 } 1353 1354 static void zonefs_free_fc(struct fs_context *fc) 1355 { 1356 struct zonefs_context *ctx = fc->fs_private; 1357 1358 kfree(ctx); 1359 } 1360 1361 static int zonefs_get_tree(struct fs_context *fc) 1362 { 1363 return get_tree_bdev(fc, zonefs_fill_super); 1364 } 1365 1366 static int zonefs_reconfigure(struct fs_context *fc) 1367 { 1368 struct zonefs_context *ctx = fc->fs_private; 1369 struct super_block *sb = fc->root->d_sb; 1370 struct zonefs_sb_info *sbi = sb->s_fs_info; 1371 1372 sync_filesystem(fc->root->d_sb); 1373 /* Copy new options from ctx into sbi. */ 1374 sbi->s_mount_opts = ctx->s_mount_opts; 1375 1376 return 0; 1377 } 1378 1379 static const struct fs_context_operations zonefs_context_ops = { 1380 .parse_param = zonefs_parse_param, 1381 .get_tree = zonefs_get_tree, 1382 .reconfigure = zonefs_reconfigure, 1383 .free = zonefs_free_fc, 1384 }; 1385 1386 /* 1387 * Set up the filesystem mount context. 1388 */ 1389 static int zonefs_init_fs_context(struct fs_context *fc) 1390 { 1391 struct zonefs_context *ctx; 1392 1393 ctx = kzalloc_obj(struct zonefs_context); 1394 if (!ctx) 1395 return -ENOMEM; 1396 ctx->s_mount_opts = ZONEFS_MNTOPT_ERRORS_RO; 1397 fc->ops = &zonefs_context_ops; 1398 fc->fs_private = ctx; 1399 1400 return 0; 1401 } 1402 1403 /* 1404 * File system definition and registration. 1405 */ 1406 static struct file_system_type zonefs_type = { 1407 .owner = THIS_MODULE, 1408 .name = "zonefs", 1409 .kill_sb = zonefs_kill_super, 1410 .fs_flags = FS_REQUIRES_DEV, 1411 .init_fs_context = zonefs_init_fs_context, 1412 .parameters = zonefs_param_spec, 1413 }; 1414 1415 static int __init zonefs_init_inodecache(void) 1416 { 1417 zonefs_inode_cachep = kmem_cache_create("zonefs_inode_cache", 1418 sizeof(struct zonefs_inode_info), 0, 1419 SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT, 1420 NULL); 1421 if (zonefs_inode_cachep == NULL) 1422 return -ENOMEM; 1423 return 0; 1424 } 1425 1426 static void zonefs_destroy_inodecache(void) 1427 { 1428 /* 1429 * Make sure all delayed rcu free inodes are flushed before we 1430 * destroy the inode cache. 1431 */ 1432 rcu_barrier(); 1433 kmem_cache_destroy(zonefs_inode_cachep); 1434 } 1435 1436 static int __init zonefs_init(void) 1437 { 1438 int ret; 1439 1440 BUILD_BUG_ON(sizeof(struct zonefs_super) != ZONEFS_SUPER_SIZE); 1441 1442 ret = zonefs_init_inodecache(); 1443 if (ret) 1444 return ret; 1445 1446 ret = zonefs_sysfs_init(); 1447 if (ret) 1448 goto destroy_inodecache; 1449 1450 ret = register_filesystem(&zonefs_type); 1451 if (ret) 1452 goto sysfs_exit; 1453 1454 return 0; 1455 1456 sysfs_exit: 1457 zonefs_sysfs_exit(); 1458 destroy_inodecache: 1459 zonefs_destroy_inodecache(); 1460 1461 return ret; 1462 } 1463 1464 static void __exit zonefs_exit(void) 1465 { 1466 unregister_filesystem(&zonefs_type); 1467 zonefs_sysfs_exit(); 1468 zonefs_destroy_inodecache(); 1469 } 1470 1471 MODULE_AUTHOR("Damien Le Moal"); 1472 MODULE_DESCRIPTION("Zone file system for zoned block devices"); 1473 MODULE_LICENSE("GPL"); 1474 MODULE_ALIAS_FS("zonefs"); 1475 module_init(zonefs_init); 1476 module_exit(zonefs_exit); 1477