1 // SPDX-License-Identifier: GPL-2.0
3 * Simple file system for zoned block devices exposing zones as files.
5 * Copyright (C) 2019 Western Digital Corporation or its affiliates.
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>
28 #define CREATE_TRACE_POINTS
32 * Get the name of a zone group directory.
34 static const char *zonefs_zgroup_name(enum zonefs_ztype ztype)
37 case ZONEFS_ZTYPE_CNV:
39 case ZONEFS_ZTYPE_SEQ:
48 * Manage the active zone count.
50 static void zonefs_account_active(struct super_block *sb,
51 struct zonefs_zone *z)
53 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
55 if (zonefs_zone_is_cnv(z))
59 * For zones that transitioned to the offline or readonly condition,
60 * we only need to clear the active state.
62 if (z->z_flags & (ZONEFS_ZONE_OFFLINE | ZONEFS_ZONE_READONLY))
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.
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);
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);
87 * Manage the active zone count. Called with zi->i_truncate_mutex held.
89 void zonefs_inode_account_active(struct inode *inode)
91 lockdep_assert_held(&ZONEFS_I(inode)->i_truncate_mutex);
93 return zonefs_account_active(inode->i_sb, zonefs_inode_zone(inode));
97 * Execute a zone management operation.
99 static int zonefs_zone_mgmt(struct super_block *sb,
100 struct zonefs_zone *z, enum req_op op)
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
112 if (op == REQ_OP_ZONE_CLOSE && !z->z_wpoffset)
113 op = REQ_OP_ZONE_RESET;
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);
120 "Zone management operation %s at %llu failed %d\n",
121 blk_op_str(op), z->z_sector, ret);
128 int zonefs_inode_zone_mgmt(struct inode *inode, enum req_op op)
130 lockdep_assert_held(&ZONEFS_I(inode)->i_truncate_mutex);
132 return zonefs_zone_mgmt(inode->i_sb, zonefs_inode_zone(inode), op);
135 void zonefs_i_size_write(struct inode *inode, loff_t isize)
137 struct zonefs_zone *z = zonefs_inode_zone(inode);
139 i_size_write(inode, isize);
142 * A full zone is no longer open/active and does not need
145 if (isize >= z->z_capacity) {
146 struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
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);
154 void zonefs_update_stats(struct inode *inode, loff_t new_isize)
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);
161 if (new_isize == old_isize)
164 spin_lock(&sbi->s_lock);
167 * This may be called for an update after an IO error.
168 * So beware of the values seen.
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;
175 sbi->s_used_blocks = 0;
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;
183 spin_unlock(&sbi->s_lock);
187 * Check a zone condition. Return the amount of written (and still readable)
190 static loff_t zonefs_check_zone_condition(struct super_block *sb,
191 struct zonefs_zone *z,
192 struct blk_zone *zone)
194 switch (zone->cond) {
195 case BLK_ZONE_COND_OFFLINE:
196 zonefs_warn(sb, "Zone %llu: offline zone\n",
198 z->z_flags |= ZONEFS_ZONE_OFFLINE;
200 case BLK_ZONE_COND_READONLY:
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
209 zonefs_warn(sb, "Zone %llu: read-only zone\n",
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;
219 if (zonefs_zone_is_cnv(z))
220 return z->z_capacity;
221 return (zone->wp - zone->start) << SECTOR_SHIFT;
226 * Check a zone condition and adjust its inode access permissions for
227 * offline and readonly zones.
229 static void zonefs_inode_update_mode(struct inode *inode)
231 struct zonefs_zone *z = zonefs_inode_zone(inode);
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;
243 inode->i_mode &= ~0222;
246 z->z_flags &= ~ZONEFS_ZONE_INIT_MODE;
247 z->z_mode = inode->i_mode;
250 static int zonefs_io_error_cb(struct blk_zone *zone, unsigned int idx,
253 struct blk_zone *z = data;
259 static void zonefs_handle_io_error(struct inode *inode, struct blk_zone *zone,
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;
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 writen in the zone (data_size).
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)
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 writen 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).
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.
298 if (isize != data_size)
300 "inode %lu: invalid size %lld (should be %lld)\n",
301 inode->i_ino, isize, data_size);
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.
309 if ((z->z_flags & ZONEFS_ZONE_OFFLINE) ||
310 (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL)) {
311 zonefs_warn(sb, "inode %lu: read/write access disabled\n",
313 if (!(z->z_flags & ZONEFS_ZONE_OFFLINE))
314 z->z_flags |= ZONEFS_ZONE_OFFLINE;
315 zonefs_inode_update_mode(inode);
317 } else if ((z->z_flags & ZONEFS_ZONE_READONLY) ||
318 (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO)) {
319 zonefs_warn(sb, "inode %lu: write access disabled\n",
321 if (!(z->z_flags & ZONEFS_ZONE_READONLY))
322 z->z_flags |= ZONEFS_ZONE_READONLY;
323 zonefs_inode_update_mode(inode);
325 } else if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO &&
327 /* Do not expose garbage data */
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.
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;
342 * If error=remount-ro was specified, any error result in remounting
343 * the volume as read-only.
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;
351 * Update block usage stats and the inode size to prevent access to
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);
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).
367 void __zonefs_io_error(struct inode *inode, bool write)
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;
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.
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;
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.
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);
405 zonefs_err(sb, "Get inode %lu zone information failed %d\n",
407 zonefs_warn(sb, "remounting filesystem read-only\n");
408 sb->s_flags |= SB_RDONLY;
413 zonefs_handle_io_error(inode, &zone, write);
416 static struct kmem_cache *zonefs_inode_cachep;
418 static struct inode *zonefs_alloc_inode(struct super_block *sb)
420 struct zonefs_inode_info *zi;
422 zi = alloc_inode_sb(sb, zonefs_inode_cachep, GFP_KERNEL);
426 inode_init_once(&zi->i_vnode);
427 mutex_init(&zi->i_truncate_mutex);
433 static void zonefs_free_inode(struct inode *inode)
435 kmem_cache_free(zonefs_inode_cachep, ZONEFS_I(inode));
441 static int zonefs_statfs(struct dentry *dentry, struct kstatfs *buf)
443 struct super_block *sb = dentry->d_sb;
444 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
447 buf->f_type = ZONEFS_MAGIC;
448 buf->f_bsize = sb->s_blocksize;
449 buf->f_namelen = ZONEFS_NAME_MAX;
451 spin_lock(&sbi->s_lock);
453 buf->f_blocks = sbi->s_blocks;
454 if (WARN_ON(sbi->s_used_blocks > sbi->s_blocks))
457 buf->f_bfree = buf->f_blocks - sbi->s_used_blocks;
458 buf->f_bavail = buf->f_bfree;
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;
466 spin_unlock(&sbi->s_lock);
468 buf->f_fsid = uuid_to_fsid(sbi->s_uuid.b);
474 Opt_errors, Opt_explicit_open,
477 struct zonefs_context {
478 unsigned long s_mount_opts;
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},
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),
495 static int zonefs_parse_param(struct fs_context *fc, struct fs_parameter *param)
497 struct zonefs_context *ctx = fc->fs_private;
498 struct fs_parse_result result;
501 opt = fs_parse(fc, zonefs_param_spec, param, &result);
507 ctx->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
508 ctx->s_mount_opts |= result.uint_32;
510 case Opt_explicit_open:
511 ctx->s_mount_opts |= ZONEFS_MNTOPT_EXPLICIT_OPEN;
520 static int zonefs_show_options(struct seq_file *seq, struct dentry *root)
522 struct zonefs_sb_info *sbi = ZONEFS_SB(root->d_sb);
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");
536 static int zonefs_inode_setattr(struct mnt_idmap *idmap,
537 struct dentry *dentry, struct iattr *iattr)
539 struct inode *inode = d_inode(dentry);
542 if (unlikely(IS_IMMUTABLE(inode)))
545 ret = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
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.
554 if ((iattr->ia_valid & ATTR_MODE) && S_ISDIR(inode->i_mode) &&
555 (iattr->ia_mode & 0222))
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);
567 if (iattr->ia_valid & ATTR_SIZE) {
568 ret = zonefs_file_truncate(inode, iattr->ia_size);
573 setattr_copy(&nop_mnt_idmap, inode, iattr);
575 if (S_ISREG(inode->i_mode)) {
576 struct zonefs_zone *z = zonefs_inode_zone(inode);
578 z->z_mode = inode->i_mode;
579 z->z_uid = inode->i_uid;
580 z->z_gid = inode->i_gid;
586 static const struct inode_operations zonefs_file_inode_operations = {
587 .setattr = zonefs_inode_setattr,
590 static long zonefs_fname_to_fno(const struct qstr *fname)
592 const char *name = fname->name;
593 unsigned int len = fname->len;
594 long fno = 0, shift = 1;
600 * File names are always a base-10 number string without any
606 if (len > 1 && c == '0')
612 for (i = 0, rname = name + len - 1; i < len; i++, rname--) {
616 fno += (c - '0') * shift;
623 static struct inode *zonefs_get_file_inode(struct inode *dir,
624 struct dentry *dentry)
626 struct zonefs_zone_group *zgroup = dir->i_private;
627 struct super_block *sb = dir->i_sb;
628 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
629 struct zonefs_zone *z;
634 /* Get the file number from the file name */
635 fno = zonefs_fname_to_fno(&dentry->d_name);
639 if (!zgroup->g_nr_zones || fno >= zgroup->g_nr_zones)
640 return ERR_PTR(-ENOENT);
642 z = &zgroup->g_zones[fno];
643 ino = z->z_sector >> sbi->s_zone_sectors_shift;
644 inode = iget_locked(sb, ino);
646 return ERR_PTR(-ENOMEM);
647 if (!(inode->i_state & I_NEW)) {
648 WARN_ON_ONCE(inode->i_private != z);
653 inode->i_mode = z->z_mode;
654 inode_set_mtime_to_ts(inode,
655 inode_set_atime_to_ts(inode, inode_set_ctime_to_ts(inode, inode_get_ctime(dir))));
656 inode->i_uid = z->z_uid;
657 inode->i_gid = z->z_gid;
658 inode->i_size = z->z_wpoffset;
659 inode->i_blocks = z->z_capacity >> SECTOR_SHIFT;
660 inode->i_private = z;
662 inode->i_op = &zonefs_file_inode_operations;
663 inode->i_fop = &zonefs_file_operations;
664 inode->i_mapping->a_ops = &zonefs_file_aops;
666 /* Update the inode access rights depending on the zone condition */
667 zonefs_inode_update_mode(inode);
669 unlock_new_inode(inode);
674 static struct inode *zonefs_get_zgroup_inode(struct super_block *sb,
675 enum zonefs_ztype ztype)
677 struct inode *root = d_inode(sb->s_root);
678 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
680 ino_t ino = bdev_nr_zones(sb->s_bdev) + ztype + 1;
682 inode = iget_locked(sb, ino);
684 return ERR_PTR(-ENOMEM);
685 if (!(inode->i_state & I_NEW))
689 inode_init_owner(&nop_mnt_idmap, inode, root, S_IFDIR | 0555);
690 inode->i_size = sbi->s_zgroup[ztype].g_nr_zones;
691 inode_set_mtime_to_ts(inode,
692 inode_set_atime_to_ts(inode, inode_set_ctime_to_ts(inode, inode_get_ctime(root))));
693 inode->i_private = &sbi->s_zgroup[ztype];
696 inode->i_op = &zonefs_dir_inode_operations;
697 inode->i_fop = &zonefs_dir_operations;
699 unlock_new_inode(inode);
705 static struct inode *zonefs_get_dir_inode(struct inode *dir,
706 struct dentry *dentry)
708 struct super_block *sb = dir->i_sb;
709 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
710 const char *name = dentry->d_name.name;
711 enum zonefs_ztype ztype;
714 * We only need to check for the "seq" directory and
715 * the "cnv" directory if we have conventional zones.
717 if (dentry->d_name.len != 3)
718 return ERR_PTR(-ENOENT);
720 for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
721 if (sbi->s_zgroup[ztype].g_nr_zones &&
722 memcmp(name, zonefs_zgroup_name(ztype), 3) == 0)
725 if (ztype == ZONEFS_ZTYPE_MAX)
726 return ERR_PTR(-ENOENT);
728 return zonefs_get_zgroup_inode(sb, ztype);
731 static struct dentry *zonefs_lookup(struct inode *dir, struct dentry *dentry,
736 if (dentry->d_name.len > ZONEFS_NAME_MAX)
737 return ERR_PTR(-ENAMETOOLONG);
739 if (dir == d_inode(dir->i_sb->s_root))
740 inode = zonefs_get_dir_inode(dir, dentry);
742 inode = zonefs_get_file_inode(dir, dentry);
744 return d_splice_alias(inode, dentry);
747 static int zonefs_readdir_root(struct file *file, struct dir_context *ctx)
749 struct inode *inode = file_inode(file);
750 struct super_block *sb = inode->i_sb;
751 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
752 enum zonefs_ztype ztype = ZONEFS_ZTYPE_CNV;
753 ino_t base_ino = bdev_nr_zones(sb->s_bdev) + 1;
755 if (ctx->pos >= inode->i_size)
758 if (!dir_emit_dots(file, ctx))
762 if (!sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones)
763 ztype = ZONEFS_ZTYPE_SEQ;
765 if (!dir_emit(ctx, zonefs_zgroup_name(ztype), 3,
766 base_ino + ztype, DT_DIR))
771 if (ctx->pos == 3 && ztype != ZONEFS_ZTYPE_SEQ) {
772 ztype = ZONEFS_ZTYPE_SEQ;
773 if (!dir_emit(ctx, zonefs_zgroup_name(ztype), 3,
774 base_ino + ztype, DT_DIR))
782 static int zonefs_readdir_zgroup(struct file *file,
783 struct dir_context *ctx)
785 struct inode *inode = file_inode(file);
786 struct zonefs_zone_group *zgroup = inode->i_private;
787 struct super_block *sb = inode->i_sb;
788 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
789 struct zonefs_zone *z;
796 * The size of zone group directories is equal to the number
797 * of zone files in the group and does note include the "." and
798 * ".." entries. Hence the "+ 2" here.
800 if (ctx->pos >= inode->i_size + 2)
803 if (!dir_emit_dots(file, ctx))
806 fname = kmalloc(ZONEFS_NAME_MAX, GFP_KERNEL);
810 for (f = ctx->pos - 2; f < zgroup->g_nr_zones; f++) {
811 z = &zgroup->g_zones[f];
812 ino = z->z_sector >> sbi->s_zone_sectors_shift;
813 fname_len = snprintf(fname, ZONEFS_NAME_MAX - 1, "%u", f);
814 if (!dir_emit(ctx, fname, fname_len, ino, DT_REG))
824 static int zonefs_readdir(struct file *file, struct dir_context *ctx)
826 struct inode *inode = file_inode(file);
828 if (inode == d_inode(inode->i_sb->s_root))
829 return zonefs_readdir_root(file, ctx);
831 return zonefs_readdir_zgroup(file, ctx);
834 const struct inode_operations zonefs_dir_inode_operations = {
835 .lookup = zonefs_lookup,
836 .setattr = zonefs_inode_setattr,
839 const struct file_operations zonefs_dir_operations = {
840 .llseek = generic_file_llseek,
841 .read = generic_read_dir,
842 .iterate_shared = zonefs_readdir,
845 struct zonefs_zone_data {
846 struct super_block *sb;
847 unsigned int nr_zones[ZONEFS_ZTYPE_MAX];
848 sector_t cnv_zone_start;
849 struct blk_zone *zones;
852 static int zonefs_get_zone_info_cb(struct blk_zone *zone, unsigned int idx,
855 struct zonefs_zone_data *zd = data;
856 struct super_block *sb = zd->sb;
857 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
860 * We do not care about the first zone: it contains the super block
861 * and not exposed as a file.
867 * Count the number of zones that will be exposed as files.
868 * For sequential zones, we always have as many files as zones.
869 * FOr conventional zones, the number of files depends on if we have
870 * conventional zones aggregation enabled.
872 switch (zone->type) {
873 case BLK_ZONE_TYPE_CONVENTIONAL:
874 if (sbi->s_features & ZONEFS_F_AGGRCNV) {
875 /* One file per set of contiguous conventional zones */
876 if (!(sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones) ||
877 zone->start != zd->cnv_zone_start)
878 sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones++;
879 zd->cnv_zone_start = zone->start + zone->len;
881 /* One file per zone */
882 sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones++;
885 case BLK_ZONE_TYPE_SEQWRITE_REQ:
886 case BLK_ZONE_TYPE_SEQWRITE_PREF:
887 sbi->s_zgroup[ZONEFS_ZTYPE_SEQ].g_nr_zones++;
890 zonefs_err(zd->sb, "Unsupported zone type 0x%x\n",
895 memcpy(&zd->zones[idx], zone, sizeof(struct blk_zone));
900 static int zonefs_get_zone_info(struct zonefs_zone_data *zd)
902 struct block_device *bdev = zd->sb->s_bdev;
905 zd->zones = kvcalloc(bdev_nr_zones(bdev), sizeof(struct blk_zone),
910 /* Get zones information from the device */
911 ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES,
912 zonefs_get_zone_info_cb, zd);
914 zonefs_err(zd->sb, "Zone report failed %d\n", ret);
918 if (ret != bdev_nr_zones(bdev)) {
919 zonefs_err(zd->sb, "Invalid zone report (%d/%u zones)\n",
920 ret, bdev_nr_zones(bdev));
927 static inline void zonefs_free_zone_info(struct zonefs_zone_data *zd)
933 * Create a zone group and populate it with zone files.
935 static int zonefs_init_zgroup(struct super_block *sb,
936 struct zonefs_zone_data *zd,
937 enum zonefs_ztype ztype)
939 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
940 struct zonefs_zone_group *zgroup = &sbi->s_zgroup[ztype];
941 struct blk_zone *zone, *next, *end;
942 struct zonefs_zone *z;
946 /* Allocate the zone group. If it is empty, we have nothing to do. */
947 if (!zgroup->g_nr_zones)
950 zgroup->g_zones = kvcalloc(zgroup->g_nr_zones,
951 sizeof(struct zonefs_zone), GFP_KERNEL);
952 if (!zgroup->g_zones)
956 * Initialize the zone groups using the device zone information.
957 * We always skip the first zone as it contains the super block
958 * and is not use to back a file.
960 end = zd->zones + bdev_nr_zones(sb->s_bdev);
961 for (zone = &zd->zones[1]; zone < end; zone = next) {
964 if (zonefs_zone_type(zone) != ztype)
967 if (WARN_ON_ONCE(n >= zgroup->g_nr_zones))
971 * For conventional zones, contiguous zones can be aggregated
972 * together to form larger files. Note that this overwrites the
973 * length of the first zone of the set of contiguous zones
974 * aggregated together. If one offline or read-only zone is
975 * found, assume that all zones aggregated have the same
978 if (ztype == ZONEFS_ZTYPE_CNV &&
979 (sbi->s_features & ZONEFS_F_AGGRCNV)) {
980 for (; next < end; next++) {
981 if (zonefs_zone_type(next) != ztype)
983 zone->len += next->len;
984 zone->capacity += next->capacity;
985 if (next->cond == BLK_ZONE_COND_READONLY &&
986 zone->cond != BLK_ZONE_COND_OFFLINE)
987 zone->cond = BLK_ZONE_COND_READONLY;
988 else if (next->cond == BLK_ZONE_COND_OFFLINE)
989 zone->cond = BLK_ZONE_COND_OFFLINE;
993 z = &zgroup->g_zones[n];
994 if (ztype == ZONEFS_ZTYPE_CNV)
995 z->z_flags |= ZONEFS_ZONE_CNV;
996 z->z_sector = zone->start;
997 z->z_size = zone->len << SECTOR_SHIFT;
998 if (z->z_size > bdev_zone_sectors(sb->s_bdev) << SECTOR_SHIFT &&
999 !(sbi->s_features & ZONEFS_F_AGGRCNV)) {
1001 "Invalid zone size %llu (device zone sectors %llu)\n",
1003 bdev_zone_sectors(sb->s_bdev) << SECTOR_SHIFT);
1007 z->z_capacity = min_t(loff_t, MAX_LFS_FILESIZE,
1008 zone->capacity << SECTOR_SHIFT);
1009 z->z_wpoffset = zonefs_check_zone_condition(sb, z, zone);
1011 z->z_mode = S_IFREG | sbi->s_perm;
1012 z->z_uid = sbi->s_uid;
1013 z->z_gid = sbi->s_gid;
1016 * Let zonefs_inode_update_mode() know that we will need
1017 * special initialization of the inode mode the first time
1020 z->z_flags |= ZONEFS_ZONE_INIT_MODE;
1022 sb->s_maxbytes = max(z->z_capacity, sb->s_maxbytes);
1023 sbi->s_blocks += z->z_capacity >> sb->s_blocksize_bits;
1024 sbi->s_used_blocks += z->z_wpoffset >> sb->s_blocksize_bits;
1027 * For sequential zones, make sure that any open zone is closed
1028 * first to ensure that the initial number of open zones is 0,
1029 * in sync with the open zone accounting done when the mount
1030 * option ZONEFS_MNTOPT_EXPLICIT_OPEN is used.
1032 if (ztype == ZONEFS_ZTYPE_SEQ &&
1033 (zone->cond == BLK_ZONE_COND_IMP_OPEN ||
1034 zone->cond == BLK_ZONE_COND_EXP_OPEN)) {
1035 ret = zonefs_zone_mgmt(sb, z, REQ_OP_ZONE_CLOSE);
1040 zonefs_account_active(sb, z);
1045 if (WARN_ON_ONCE(n != zgroup->g_nr_zones))
1048 zonefs_info(sb, "Zone group \"%s\" has %u file%s\n",
1049 zonefs_zgroup_name(ztype),
1051 str_plural(zgroup->g_nr_zones));
1056 static void zonefs_free_zgroups(struct super_block *sb)
1058 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1059 enum zonefs_ztype ztype;
1064 for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
1065 kvfree(sbi->s_zgroup[ztype].g_zones);
1066 sbi->s_zgroup[ztype].g_zones = NULL;
1071 * Create a zone group and populate it with zone files.
1073 static int zonefs_init_zgroups(struct super_block *sb)
1075 struct zonefs_zone_data zd;
1076 enum zonefs_ztype ztype;
1079 /* First get the device zone information */
1080 memset(&zd, 0, sizeof(struct zonefs_zone_data));
1082 ret = zonefs_get_zone_info(&zd);
1086 /* Allocate and initialize the zone groups */
1087 for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
1088 ret = zonefs_init_zgroup(sb, &zd, ztype);
1091 "Zone group \"%s\" initialization failed\n",
1092 zonefs_zgroup_name(ztype));
1098 zonefs_free_zone_info(&zd);
1100 zonefs_free_zgroups(sb);
1106 * Read super block information from the device.
1108 static int zonefs_read_super(struct super_block *sb)
1110 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1111 struct zonefs_super *super;
1112 u32 crc, stored_crc;
1114 struct bio_vec bio_vec;
1118 page = alloc_page(GFP_KERNEL);
1122 bio_init(&bio, sb->s_bdev, &bio_vec, 1, REQ_OP_READ);
1123 bio.bi_iter.bi_sector = 0;
1124 __bio_add_page(&bio, page, PAGE_SIZE, 0);
1126 ret = submit_bio_wait(&bio);
1130 super = page_address(page);
1133 if (le32_to_cpu(super->s_magic) != ZONEFS_MAGIC)
1136 stored_crc = le32_to_cpu(super->s_crc);
1138 crc = crc32(~0U, (unsigned char *)super, sizeof(struct zonefs_super));
1139 if (crc != stored_crc) {
1140 zonefs_err(sb, "Invalid checksum (Expected 0x%08x, got 0x%08x)",
1145 sbi->s_features = le64_to_cpu(super->s_features);
1146 if (sbi->s_features & ~ZONEFS_F_DEFINED_FEATURES) {
1147 zonefs_err(sb, "Unknown features set 0x%llx\n",
1152 if (sbi->s_features & ZONEFS_F_UID) {
1153 sbi->s_uid = make_kuid(current_user_ns(),
1154 le32_to_cpu(super->s_uid));
1155 if (!uid_valid(sbi->s_uid)) {
1156 zonefs_err(sb, "Invalid UID feature\n");
1161 if (sbi->s_features & ZONEFS_F_GID) {
1162 sbi->s_gid = make_kgid(current_user_ns(),
1163 le32_to_cpu(super->s_gid));
1164 if (!gid_valid(sbi->s_gid)) {
1165 zonefs_err(sb, "Invalid GID feature\n");
1170 if (sbi->s_features & ZONEFS_F_PERM)
1171 sbi->s_perm = le32_to_cpu(super->s_perm);
1173 if (memchr_inv(super->s_reserved, 0, sizeof(super->s_reserved))) {
1174 zonefs_err(sb, "Reserved area is being used\n");
1178 import_uuid(&sbi->s_uuid, super->s_uuid);
1187 static const struct super_operations zonefs_sops = {
1188 .alloc_inode = zonefs_alloc_inode,
1189 .free_inode = zonefs_free_inode,
1190 .statfs = zonefs_statfs,
1191 .show_options = zonefs_show_options,
1194 static int zonefs_get_zgroup_inodes(struct super_block *sb)
1196 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1197 struct inode *dir_inode;
1198 enum zonefs_ztype ztype;
1200 for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
1201 if (!sbi->s_zgroup[ztype].g_nr_zones)
1204 dir_inode = zonefs_get_zgroup_inode(sb, ztype);
1205 if (IS_ERR(dir_inode))
1206 return PTR_ERR(dir_inode);
1208 sbi->s_zgroup[ztype].g_inode = dir_inode;
1214 static void zonefs_release_zgroup_inodes(struct super_block *sb)
1216 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1217 enum zonefs_ztype ztype;
1222 for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
1223 if (sbi->s_zgroup[ztype].g_inode) {
1224 iput(sbi->s_zgroup[ztype].g_inode);
1225 sbi->s_zgroup[ztype].g_inode = NULL;
1231 * Check that the device is zoned. If it is, get the list of zones and create
1232 * sub-directories and files according to the device zone configuration and
1235 static int zonefs_fill_super(struct super_block *sb, struct fs_context *fc)
1237 struct zonefs_sb_info *sbi;
1238 struct zonefs_context *ctx = fc->fs_private;
1239 struct inode *inode;
1240 enum zonefs_ztype ztype;
1243 if (!bdev_is_zoned(sb->s_bdev)) {
1244 zonefs_err(sb, "Not a zoned block device\n");
1249 * Initialize super block information: the maximum file size is updated
1250 * when the zone files are created so that the format option
1251 * ZONEFS_F_AGGRCNV which increases the maximum file size of a file
1252 * beyond the zone size is taken into account.
1254 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1258 spin_lock_init(&sbi->s_lock);
1259 sb->s_fs_info = sbi;
1260 sb->s_magic = ZONEFS_MAGIC;
1262 sb->s_op = &zonefs_sops;
1263 sb->s_time_gran = 1;
1266 * The block size is set to the device zone write granularity to ensure
1267 * that write operations are always aligned according to the device
1268 * interface constraints.
1270 sb_set_blocksize(sb, bdev_zone_write_granularity(sb->s_bdev));
1271 sbi->s_zone_sectors_shift = ilog2(bdev_zone_sectors(sb->s_bdev));
1272 sbi->s_uid = GLOBAL_ROOT_UID;
1273 sbi->s_gid = GLOBAL_ROOT_GID;
1275 sbi->s_mount_opts = ctx->s_mount_opts;
1277 atomic_set(&sbi->s_wro_seq_files, 0);
1278 sbi->s_max_wro_seq_files = bdev_max_open_zones(sb->s_bdev);
1279 atomic_set(&sbi->s_active_seq_files, 0);
1280 sbi->s_max_active_seq_files = bdev_max_active_zones(sb->s_bdev);
1282 ret = zonefs_read_super(sb);
1286 zonefs_info(sb, "Mounting %u zones", bdev_nr_zones(sb->s_bdev));
1288 if (!sbi->s_max_wro_seq_files &&
1289 !sbi->s_max_active_seq_files &&
1290 sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) {
1292 "No open and active zone limits. Ignoring explicit_open mount option\n");
1293 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_EXPLICIT_OPEN;
1296 /* Initialize the zone groups */
1297 ret = zonefs_init_zgroups(sb);
1301 /* Create the root directory inode */
1303 inode = new_inode(sb);
1307 inode->i_ino = bdev_nr_zones(sb->s_bdev);
1308 inode->i_mode = S_IFDIR | 0555;
1309 simple_inode_init_ts(inode);
1310 inode->i_op = &zonefs_dir_inode_operations;
1311 inode->i_fop = &zonefs_dir_operations;
1313 set_nlink(inode, 2);
1314 for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
1315 if (sbi->s_zgroup[ztype].g_nr_zones) {
1321 sb->s_root = d_make_root(inode);
1326 * Take a reference on the zone groups directory inodes
1327 * to keep them in the inode cache.
1329 ret = zonefs_get_zgroup_inodes(sb);
1333 ret = zonefs_sysfs_register(sb);
1340 zonefs_release_zgroup_inodes(sb);
1341 zonefs_free_zgroups(sb);
1346 static void zonefs_kill_super(struct super_block *sb)
1348 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1350 /* Release the reference on the zone group directory inodes */
1351 zonefs_release_zgroup_inodes(sb);
1353 kill_block_super(sb);
1355 zonefs_sysfs_unregister(sb);
1356 zonefs_free_zgroups(sb);
1360 static void zonefs_free_fc(struct fs_context *fc)
1362 struct zonefs_context *ctx = fc->fs_private;
1367 static int zonefs_get_tree(struct fs_context *fc)
1369 return get_tree_bdev(fc, zonefs_fill_super);
1372 static int zonefs_reconfigure(struct fs_context *fc)
1374 struct zonefs_context *ctx = fc->fs_private;
1375 struct super_block *sb = fc->root->d_sb;
1376 struct zonefs_sb_info *sbi = sb->s_fs_info;
1378 sync_filesystem(fc->root->d_sb);
1379 /* Copy new options from ctx into sbi. */
1380 sbi->s_mount_opts = ctx->s_mount_opts;
1385 static const struct fs_context_operations zonefs_context_ops = {
1386 .parse_param = zonefs_parse_param,
1387 .get_tree = zonefs_get_tree,
1388 .reconfigure = zonefs_reconfigure,
1389 .free = zonefs_free_fc,
1393 * Set up the filesystem mount context.
1395 static int zonefs_init_fs_context(struct fs_context *fc)
1397 struct zonefs_context *ctx;
1399 ctx = kzalloc(sizeof(struct zonefs_context), GFP_KERNEL);
1402 ctx->s_mount_opts = ZONEFS_MNTOPT_ERRORS_RO;
1403 fc->ops = &zonefs_context_ops;
1404 fc->fs_private = ctx;
1410 * File system definition and registration.
1412 static struct file_system_type zonefs_type = {
1413 .owner = THIS_MODULE,
1415 .kill_sb = zonefs_kill_super,
1416 .fs_flags = FS_REQUIRES_DEV,
1417 .init_fs_context = zonefs_init_fs_context,
1418 .parameters = zonefs_param_spec,
1421 static int __init zonefs_init_inodecache(void)
1423 zonefs_inode_cachep = kmem_cache_create("zonefs_inode_cache",
1424 sizeof(struct zonefs_inode_info), 0,
1425 SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT,
1427 if (zonefs_inode_cachep == NULL)
1432 static void zonefs_destroy_inodecache(void)
1435 * Make sure all delayed rcu free inodes are flushed before we
1436 * destroy the inode cache.
1439 kmem_cache_destroy(zonefs_inode_cachep);
1442 static int __init zonefs_init(void)
1446 BUILD_BUG_ON(sizeof(struct zonefs_super) != ZONEFS_SUPER_SIZE);
1448 ret = zonefs_init_inodecache();
1452 ret = zonefs_sysfs_init();
1454 goto destroy_inodecache;
1456 ret = register_filesystem(&zonefs_type);
1463 zonefs_sysfs_exit();
1465 zonefs_destroy_inodecache();
1470 static void __exit zonefs_exit(void)
1472 unregister_filesystem(&zonefs_type);
1473 zonefs_sysfs_exit();
1474 zonefs_destroy_inodecache();
1477 MODULE_AUTHOR("Damien Le Moal");
1478 MODULE_DESCRIPTION("Zone file system for zoned block devices");
1479 MODULE_LICENSE("GPL");
1480 MODULE_ALIAS_FS("zonefs");
1481 module_init(zonefs_init);
1482 module_exit(zonefs_exit);