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/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>
27 #define CREATE_TRACE_POINTS
31 * Get the name of a zone group directory.
33 static const char *zonefs_zgroup_name(enum zonefs_ztype ztype)
36 case ZONEFS_ZTYPE_CNV:
38 case ZONEFS_ZTYPE_SEQ:
47 * Manage the active zone count.
49 static void zonefs_account_active(struct super_block *sb,
50 struct zonefs_zone *z)
52 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
54 if (zonefs_zone_is_cnv(z))
58 * For zones that transitioned to the offline or readonly condition,
59 * we only need to clear the active state.
61 if (z->z_flags & (ZONEFS_ZONE_OFFLINE | ZONEFS_ZONE_READONLY))
65 * If the zone is active, that is, if it is explicitly open or
66 * partially written, check if it was already accounted as active.
68 if ((z->z_flags & ZONEFS_ZONE_OPEN) ||
69 (z->z_wpoffset > 0 && z->z_wpoffset < z->z_capacity)) {
70 if (!(z->z_flags & ZONEFS_ZONE_ACTIVE)) {
71 z->z_flags |= ZONEFS_ZONE_ACTIVE;
72 atomic_inc(&sbi->s_active_seq_files);
78 /* The zone is not active. If it was, update the active count */
79 if (z->z_flags & ZONEFS_ZONE_ACTIVE) {
80 z->z_flags &= ~ZONEFS_ZONE_ACTIVE;
81 atomic_dec(&sbi->s_active_seq_files);
86 * Manage the active zone count. Called with zi->i_truncate_mutex held.
88 void zonefs_inode_account_active(struct inode *inode)
90 lockdep_assert_held(&ZONEFS_I(inode)->i_truncate_mutex);
92 return zonefs_account_active(inode->i_sb, zonefs_inode_zone(inode));
96 * Execute a zone management operation.
98 static int zonefs_zone_mgmt(struct super_block *sb,
99 struct zonefs_zone *z, enum req_op op)
104 * With ZNS drives, closing an explicitly open zone that has not been
105 * written will change the zone state to "closed", that is, the zone
106 * will remain active. Since this can then cause failure of explicit
107 * open operation on other zones if the drive active zone resources
108 * are exceeded, make sure that the zone does not remain active by
111 if (op == REQ_OP_ZONE_CLOSE && !z->z_wpoffset)
112 op = REQ_OP_ZONE_RESET;
114 trace_zonefs_zone_mgmt(sb, z, op);
115 ret = blkdev_zone_mgmt(sb->s_bdev, op, z->z_sector,
116 z->z_size >> SECTOR_SHIFT, GFP_NOFS);
119 "Zone management operation %s at %llu failed %d\n",
120 blk_op_str(op), z->z_sector, ret);
127 int zonefs_inode_zone_mgmt(struct inode *inode, enum req_op op)
129 lockdep_assert_held(&ZONEFS_I(inode)->i_truncate_mutex);
131 return zonefs_zone_mgmt(inode->i_sb, zonefs_inode_zone(inode), op);
134 void zonefs_i_size_write(struct inode *inode, loff_t isize)
136 struct zonefs_zone *z = zonefs_inode_zone(inode);
138 i_size_write(inode, isize);
141 * A full zone is no longer open/active and does not need
144 if (isize >= z->z_capacity) {
145 struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
147 if (z->z_flags & ZONEFS_ZONE_ACTIVE)
148 atomic_dec(&sbi->s_active_seq_files);
149 z->z_flags &= ~(ZONEFS_ZONE_OPEN | ZONEFS_ZONE_ACTIVE);
153 void zonefs_update_stats(struct inode *inode, loff_t new_isize)
155 struct super_block *sb = inode->i_sb;
156 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
157 loff_t old_isize = i_size_read(inode);
160 if (new_isize == old_isize)
163 spin_lock(&sbi->s_lock);
166 * This may be called for an update after an IO error.
167 * So beware of the values seen.
169 if (new_isize < old_isize) {
170 nr_blocks = (old_isize - new_isize) >> sb->s_blocksize_bits;
171 if (sbi->s_used_blocks > nr_blocks)
172 sbi->s_used_blocks -= nr_blocks;
174 sbi->s_used_blocks = 0;
176 sbi->s_used_blocks +=
177 (new_isize - old_isize) >> sb->s_blocksize_bits;
178 if (sbi->s_used_blocks > sbi->s_blocks)
179 sbi->s_used_blocks = sbi->s_blocks;
182 spin_unlock(&sbi->s_lock);
186 * Check a zone condition. Return the amount of written (and still readable)
189 static loff_t zonefs_check_zone_condition(struct super_block *sb,
190 struct zonefs_zone *z,
191 struct blk_zone *zone)
193 switch (zone->cond) {
194 case BLK_ZONE_COND_OFFLINE:
195 zonefs_warn(sb, "Zone %llu: offline zone\n",
197 z->z_flags |= ZONEFS_ZONE_OFFLINE;
199 case BLK_ZONE_COND_READONLY:
201 * The write pointer of read-only zones is invalid, so we cannot
202 * determine the zone wpoffset (inode size). We thus keep the
203 * zone wpoffset as is, which leads to an empty file
204 * (wpoffset == 0) on mount. For a runtime error, this keeps
205 * the inode size as it was when last updated so that the user
208 zonefs_warn(sb, "Zone %llu: read-only zone\n",
210 z->z_flags |= ZONEFS_ZONE_READONLY;
211 if (zonefs_zone_is_cnv(z))
212 return z->z_capacity;
213 return z->z_wpoffset;
214 case BLK_ZONE_COND_FULL:
215 /* The write pointer of full zones is invalid. */
216 return z->z_capacity;
218 if (zonefs_zone_is_cnv(z))
219 return z->z_capacity;
220 return (zone->wp - zone->start) << SECTOR_SHIFT;
225 * Check a zone condition and adjust its inode access permissions for
226 * offline and readonly zones.
228 static void zonefs_inode_update_mode(struct inode *inode)
230 struct zonefs_zone *z = zonefs_inode_zone(inode);
232 if (z->z_flags & ZONEFS_ZONE_OFFLINE) {
233 /* Offline zones cannot be read nor written */
234 inode->i_flags |= S_IMMUTABLE;
235 inode->i_mode &= ~0777;
236 } else if (z->z_flags & ZONEFS_ZONE_READONLY) {
237 /* Readonly zones cannot be written */
238 inode->i_flags |= S_IMMUTABLE;
239 if (z->z_flags & ZONEFS_ZONE_INIT_MODE)
240 inode->i_mode &= ~0777;
242 inode->i_mode &= ~0222;
245 z->z_flags &= ~ZONEFS_ZONE_INIT_MODE;
246 z->z_mode = inode->i_mode;
249 static int zonefs_io_error_cb(struct blk_zone *zone, unsigned int idx,
252 struct blk_zone *z = data;
258 static void zonefs_handle_io_error(struct inode *inode, struct blk_zone *zone,
261 struct zonefs_zone *z = zonefs_inode_zone(inode);
262 struct super_block *sb = inode->i_sb;
263 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
264 loff_t isize, data_size;
267 * Check the zone condition: if the zone is not "bad" (offline or
268 * read-only), read errors are simply signaled to the IO issuer as long
269 * as there is no inconsistency between the inode size and the amount of
270 * data writen in the zone (data_size).
272 data_size = zonefs_check_zone_condition(sb, z, zone);
273 isize = i_size_read(inode);
274 if (!(z->z_flags & (ZONEFS_ZONE_READONLY | ZONEFS_ZONE_OFFLINE)) &&
275 !write && isize == data_size)
279 * At this point, we detected either a bad zone or an inconsistency
280 * between the inode size and the amount of data written in the zone.
281 * For the latter case, the cause may be a write IO error or an external
282 * action on the device. Two error patterns exist:
283 * 1) The inode size is lower than the amount of data in the zone:
284 * a write operation partially failed and data was writen at the end
285 * of the file. This can happen in the case of a large direct IO
286 * needing several BIOs and/or write requests to be processed.
287 * 2) The inode size is larger than the amount of data in the zone:
288 * this can happen with a deferred write error with the use of the
289 * device side write cache after getting successful write IO
290 * completions. Other possibilities are (a) an external corruption,
291 * e.g. an application reset the zone directly, or (b) the device
292 * has a serious problem (e.g. firmware bug).
294 * In all cases, warn about inode size inconsistency and handle the
295 * IO error according to the zone condition and to the mount options.
297 if (isize != data_size)
299 "inode %lu: invalid size %lld (should be %lld)\n",
300 inode->i_ino, isize, data_size);
303 * First handle bad zones signaled by hardware. The mount options
304 * errors=zone-ro and errors=zone-offline result in changing the
305 * zone condition to read-only and offline respectively, as if the
306 * condition was signaled by the hardware.
308 if ((z->z_flags & ZONEFS_ZONE_OFFLINE) ||
309 (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL)) {
310 zonefs_warn(sb, "inode %lu: read/write access disabled\n",
312 if (!(z->z_flags & ZONEFS_ZONE_OFFLINE))
313 z->z_flags |= ZONEFS_ZONE_OFFLINE;
314 zonefs_inode_update_mode(inode);
316 } else if ((z->z_flags & ZONEFS_ZONE_READONLY) ||
317 (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO)) {
318 zonefs_warn(sb, "inode %lu: write access disabled\n",
320 if (!(z->z_flags & ZONEFS_ZONE_READONLY))
321 z->z_flags |= ZONEFS_ZONE_READONLY;
322 zonefs_inode_update_mode(inode);
324 } else if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO &&
326 /* Do not expose garbage data */
331 * If the filesystem is mounted with the explicit-open mount option, we
332 * need to clear the ZONEFS_ZONE_OPEN flag if the zone transitioned to
333 * the read-only or offline condition, to avoid attempting an explicit
334 * close of the zone when the inode file is closed.
336 if ((sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) &&
337 (z->z_flags & (ZONEFS_ZONE_READONLY | ZONEFS_ZONE_OFFLINE)))
338 z->z_flags &= ~ZONEFS_ZONE_OPEN;
341 * If error=remount-ro was specified, any error result in remounting
342 * the volume as read-only.
344 if ((sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO) && !sb_rdonly(sb)) {
345 zonefs_warn(sb, "remounting filesystem read-only\n");
346 sb->s_flags |= SB_RDONLY;
350 * Update block usage stats and the inode size to prevent access to
353 zonefs_update_stats(inode, data_size);
354 zonefs_i_size_write(inode, data_size);
355 z->z_wpoffset = data_size;
356 zonefs_inode_account_active(inode);
360 * When an file IO error occurs, check the file zone to see if there is a change
361 * in the zone condition (e.g. offline or read-only). For a failed write to a
362 * sequential zone, the zone write pointer position must also be checked to
363 * eventually correct the file size and zonefs inode write pointer offset
364 * (which can be out of sync with the drive due to partial write failures).
366 void __zonefs_io_error(struct inode *inode, bool write)
368 struct zonefs_zone *z = zonefs_inode_zone(inode);
369 struct super_block *sb = inode->i_sb;
370 unsigned int noio_flag;
371 struct blk_zone zone;
375 * Conventional zone have no write pointer and cannot become read-only
376 * or offline. So simply fake a report for a single or aggregated zone
377 * and let zonefs_handle_io_error() correct the zone inode information
378 * according to the mount options.
380 if (!zonefs_zone_is_seq(z)) {
381 zone.start = z->z_sector;
382 zone.len = z->z_size >> SECTOR_SHIFT;
383 zone.wp = zone.start + zone.len;
384 zone.type = BLK_ZONE_TYPE_CONVENTIONAL;
385 zone.cond = BLK_ZONE_COND_NOT_WP;
386 zone.capacity = zone.len;
387 goto handle_io_error;
391 * Memory allocations in blkdev_report_zones() can trigger a memory
392 * reclaim which may in turn cause a recursion into zonefs as well as
393 * struct request allocations for the same device. The former case may
394 * end up in a deadlock on the inode truncate mutex, while the latter
395 * may prevent IO forward progress. Executing the report zones under
396 * the GFP_NOIO context avoids both problems.
398 noio_flag = memalloc_noio_save();
399 ret = blkdev_report_zones(sb->s_bdev, z->z_sector, 1,
400 zonefs_io_error_cb, &zone);
401 memalloc_noio_restore(noio_flag);
404 zonefs_err(sb, "Get inode %lu zone information failed %d\n",
406 zonefs_warn(sb, "remounting filesystem read-only\n");
407 sb->s_flags |= SB_RDONLY;
412 zonefs_handle_io_error(inode, &zone, write);
415 static struct kmem_cache *zonefs_inode_cachep;
417 static struct inode *zonefs_alloc_inode(struct super_block *sb)
419 struct zonefs_inode_info *zi;
421 zi = alloc_inode_sb(sb, zonefs_inode_cachep, GFP_KERNEL);
425 inode_init_once(&zi->i_vnode);
426 mutex_init(&zi->i_truncate_mutex);
432 static void zonefs_free_inode(struct inode *inode)
434 kmem_cache_free(zonefs_inode_cachep, ZONEFS_I(inode));
440 static int zonefs_statfs(struct dentry *dentry, struct kstatfs *buf)
442 struct super_block *sb = dentry->d_sb;
443 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
446 buf->f_type = ZONEFS_MAGIC;
447 buf->f_bsize = sb->s_blocksize;
448 buf->f_namelen = ZONEFS_NAME_MAX;
450 spin_lock(&sbi->s_lock);
452 buf->f_blocks = sbi->s_blocks;
453 if (WARN_ON(sbi->s_used_blocks > sbi->s_blocks))
456 buf->f_bfree = buf->f_blocks - sbi->s_used_blocks;
457 buf->f_bavail = buf->f_bfree;
459 for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) {
460 if (sbi->s_zgroup[t].g_nr_zones)
461 buf->f_files += sbi->s_zgroup[t].g_nr_zones + 1;
465 spin_unlock(&sbi->s_lock);
467 buf->f_fsid = uuid_to_fsid(sbi->s_uuid.b);
473 Opt_errors_ro, Opt_errors_zro, Opt_errors_zol, Opt_errors_repair,
474 Opt_explicit_open, Opt_err,
477 static const match_table_t tokens = {
478 { Opt_errors_ro, "errors=remount-ro"},
479 { Opt_errors_zro, "errors=zone-ro"},
480 { Opt_errors_zol, "errors=zone-offline"},
481 { Opt_errors_repair, "errors=repair"},
482 { Opt_explicit_open, "explicit-open" },
486 static int zonefs_parse_options(struct super_block *sb, char *options)
488 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
489 substring_t args[MAX_OPT_ARGS];
495 while ((p = strsep(&options, ",")) != NULL) {
501 token = match_token(p, tokens, args);
504 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
505 sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_RO;
508 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
509 sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZRO;
512 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
513 sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZOL;
515 case Opt_errors_repair:
516 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
517 sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_REPAIR;
519 case Opt_explicit_open:
520 sbi->s_mount_opts |= ZONEFS_MNTOPT_EXPLICIT_OPEN;
530 static int zonefs_show_options(struct seq_file *seq, struct dentry *root)
532 struct zonefs_sb_info *sbi = ZONEFS_SB(root->d_sb);
534 if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO)
535 seq_puts(seq, ",errors=remount-ro");
536 if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO)
537 seq_puts(seq, ",errors=zone-ro");
538 if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL)
539 seq_puts(seq, ",errors=zone-offline");
540 if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_REPAIR)
541 seq_puts(seq, ",errors=repair");
546 static int zonefs_remount(struct super_block *sb, int *flags, char *data)
550 return zonefs_parse_options(sb, data);
553 static int zonefs_inode_setattr(struct mnt_idmap *idmap,
554 struct dentry *dentry, struct iattr *iattr)
556 struct inode *inode = d_inode(dentry);
559 if (unlikely(IS_IMMUTABLE(inode)))
562 ret = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
567 * Since files and directories cannot be created nor deleted, do not
568 * allow setting any write attributes on the sub-directories grouping
569 * files by zone type.
571 if ((iattr->ia_valid & ATTR_MODE) && S_ISDIR(inode->i_mode) &&
572 (iattr->ia_mode & 0222))
575 if (((iattr->ia_valid & ATTR_UID) &&
576 !uid_eq(iattr->ia_uid, inode->i_uid)) ||
577 ((iattr->ia_valid & ATTR_GID) &&
578 !gid_eq(iattr->ia_gid, inode->i_gid))) {
579 ret = dquot_transfer(&nop_mnt_idmap, inode, iattr);
584 if (iattr->ia_valid & ATTR_SIZE) {
585 ret = zonefs_file_truncate(inode, iattr->ia_size);
590 setattr_copy(&nop_mnt_idmap, inode, iattr);
592 if (S_ISREG(inode->i_mode)) {
593 struct zonefs_zone *z = zonefs_inode_zone(inode);
595 z->z_mode = inode->i_mode;
596 z->z_uid = inode->i_uid;
597 z->z_gid = inode->i_gid;
603 static const struct inode_operations zonefs_file_inode_operations = {
604 .setattr = zonefs_inode_setattr,
607 static long zonefs_fname_to_fno(const struct qstr *fname)
609 const char *name = fname->name;
610 unsigned int len = fname->len;
611 long fno = 0, shift = 1;
617 * File names are always a base-10 number string without any
623 if (len > 1 && c == '0')
629 for (i = 0, rname = name + len - 1; i < len; i++, rname--) {
633 fno += (c - '0') * shift;
640 static struct inode *zonefs_get_file_inode(struct inode *dir,
641 struct dentry *dentry)
643 struct zonefs_zone_group *zgroup = dir->i_private;
644 struct super_block *sb = dir->i_sb;
645 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
646 struct zonefs_zone *z;
651 /* Get the file number from the file name */
652 fno = zonefs_fname_to_fno(&dentry->d_name);
656 if (!zgroup->g_nr_zones || fno >= zgroup->g_nr_zones)
657 return ERR_PTR(-ENOENT);
659 z = &zgroup->g_zones[fno];
660 ino = z->z_sector >> sbi->s_zone_sectors_shift;
661 inode = iget_locked(sb, ino);
663 return ERR_PTR(-ENOMEM);
664 if (!(inode->i_state & I_NEW)) {
665 WARN_ON_ONCE(inode->i_private != z);
670 inode->i_mode = z->z_mode;
671 inode_set_mtime_to_ts(inode,
672 inode_set_atime_to_ts(inode, inode_set_ctime_to_ts(inode, inode_get_ctime(dir))));
673 inode->i_uid = z->z_uid;
674 inode->i_gid = z->z_gid;
675 inode->i_size = z->z_wpoffset;
676 inode->i_blocks = z->z_capacity >> SECTOR_SHIFT;
677 inode->i_private = z;
679 inode->i_op = &zonefs_file_inode_operations;
680 inode->i_fop = &zonefs_file_operations;
681 inode->i_mapping->a_ops = &zonefs_file_aops;
683 /* Update the inode access rights depending on the zone condition */
684 zonefs_inode_update_mode(inode);
686 unlock_new_inode(inode);
691 static struct inode *zonefs_get_zgroup_inode(struct super_block *sb,
692 enum zonefs_ztype ztype)
694 struct inode *root = d_inode(sb->s_root);
695 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
697 ino_t ino = bdev_nr_zones(sb->s_bdev) + ztype + 1;
699 inode = iget_locked(sb, ino);
701 return ERR_PTR(-ENOMEM);
702 if (!(inode->i_state & I_NEW))
706 inode_init_owner(&nop_mnt_idmap, inode, root, S_IFDIR | 0555);
707 inode->i_size = sbi->s_zgroup[ztype].g_nr_zones;
708 inode_set_mtime_to_ts(inode,
709 inode_set_atime_to_ts(inode, inode_set_ctime_to_ts(inode, inode_get_ctime(root))));
710 inode->i_private = &sbi->s_zgroup[ztype];
713 inode->i_op = &zonefs_dir_inode_operations;
714 inode->i_fop = &zonefs_dir_operations;
716 unlock_new_inode(inode);
722 static struct inode *zonefs_get_dir_inode(struct inode *dir,
723 struct dentry *dentry)
725 struct super_block *sb = dir->i_sb;
726 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
727 const char *name = dentry->d_name.name;
728 enum zonefs_ztype ztype;
731 * We only need to check for the "seq" directory and
732 * the "cnv" directory if we have conventional zones.
734 if (dentry->d_name.len != 3)
735 return ERR_PTR(-ENOENT);
737 for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
738 if (sbi->s_zgroup[ztype].g_nr_zones &&
739 memcmp(name, zonefs_zgroup_name(ztype), 3) == 0)
742 if (ztype == ZONEFS_ZTYPE_MAX)
743 return ERR_PTR(-ENOENT);
745 return zonefs_get_zgroup_inode(sb, ztype);
748 static struct dentry *zonefs_lookup(struct inode *dir, struct dentry *dentry,
753 if (dentry->d_name.len > ZONEFS_NAME_MAX)
754 return ERR_PTR(-ENAMETOOLONG);
756 if (dir == d_inode(dir->i_sb->s_root))
757 inode = zonefs_get_dir_inode(dir, dentry);
759 inode = zonefs_get_file_inode(dir, dentry);
761 return d_splice_alias(inode, dentry);
764 static int zonefs_readdir_root(struct file *file, struct dir_context *ctx)
766 struct inode *inode = file_inode(file);
767 struct super_block *sb = inode->i_sb;
768 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
769 enum zonefs_ztype ztype = ZONEFS_ZTYPE_CNV;
770 ino_t base_ino = bdev_nr_zones(sb->s_bdev) + 1;
772 if (ctx->pos >= inode->i_size)
775 if (!dir_emit_dots(file, ctx))
779 if (!sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones)
780 ztype = ZONEFS_ZTYPE_SEQ;
782 if (!dir_emit(ctx, zonefs_zgroup_name(ztype), 3,
783 base_ino + ztype, DT_DIR))
788 if (ctx->pos == 3 && ztype != ZONEFS_ZTYPE_SEQ) {
789 ztype = ZONEFS_ZTYPE_SEQ;
790 if (!dir_emit(ctx, zonefs_zgroup_name(ztype), 3,
791 base_ino + ztype, DT_DIR))
799 static int zonefs_readdir_zgroup(struct file *file,
800 struct dir_context *ctx)
802 struct inode *inode = file_inode(file);
803 struct zonefs_zone_group *zgroup = inode->i_private;
804 struct super_block *sb = inode->i_sb;
805 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
806 struct zonefs_zone *z;
813 * The size of zone group directories is equal to the number
814 * of zone files in the group and does note include the "." and
815 * ".." entries. Hence the "+ 2" here.
817 if (ctx->pos >= inode->i_size + 2)
820 if (!dir_emit_dots(file, ctx))
823 fname = kmalloc(ZONEFS_NAME_MAX, GFP_KERNEL);
827 for (f = ctx->pos - 2; f < zgroup->g_nr_zones; f++) {
828 z = &zgroup->g_zones[f];
829 ino = z->z_sector >> sbi->s_zone_sectors_shift;
830 fname_len = snprintf(fname, ZONEFS_NAME_MAX - 1, "%u", f);
831 if (!dir_emit(ctx, fname, fname_len, ino, DT_REG))
841 static int zonefs_readdir(struct file *file, struct dir_context *ctx)
843 struct inode *inode = file_inode(file);
845 if (inode == d_inode(inode->i_sb->s_root))
846 return zonefs_readdir_root(file, ctx);
848 return zonefs_readdir_zgroup(file, ctx);
851 const struct inode_operations zonefs_dir_inode_operations = {
852 .lookup = zonefs_lookup,
853 .setattr = zonefs_inode_setattr,
856 const struct file_operations zonefs_dir_operations = {
857 .llseek = generic_file_llseek,
858 .read = generic_read_dir,
859 .iterate_shared = zonefs_readdir,
862 struct zonefs_zone_data {
863 struct super_block *sb;
864 unsigned int nr_zones[ZONEFS_ZTYPE_MAX];
865 sector_t cnv_zone_start;
866 struct blk_zone *zones;
869 static int zonefs_get_zone_info_cb(struct blk_zone *zone, unsigned int idx,
872 struct zonefs_zone_data *zd = data;
873 struct super_block *sb = zd->sb;
874 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
877 * We do not care about the first zone: it contains the super block
878 * and not exposed as a file.
884 * Count the number of zones that will be exposed as files.
885 * For sequential zones, we always have as many files as zones.
886 * FOr conventional zones, the number of files depends on if we have
887 * conventional zones aggregation enabled.
889 switch (zone->type) {
890 case BLK_ZONE_TYPE_CONVENTIONAL:
891 if (sbi->s_features & ZONEFS_F_AGGRCNV) {
892 /* One file per set of contiguous conventional zones */
893 if (!(sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones) ||
894 zone->start != zd->cnv_zone_start)
895 sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones++;
896 zd->cnv_zone_start = zone->start + zone->len;
898 /* One file per zone */
899 sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones++;
902 case BLK_ZONE_TYPE_SEQWRITE_REQ:
903 case BLK_ZONE_TYPE_SEQWRITE_PREF:
904 sbi->s_zgroup[ZONEFS_ZTYPE_SEQ].g_nr_zones++;
907 zonefs_err(zd->sb, "Unsupported zone type 0x%x\n",
912 memcpy(&zd->zones[idx], zone, sizeof(struct blk_zone));
917 static int zonefs_get_zone_info(struct zonefs_zone_data *zd)
919 struct block_device *bdev = zd->sb->s_bdev;
922 zd->zones = kvcalloc(bdev_nr_zones(bdev), sizeof(struct blk_zone),
927 /* Get zones information from the device */
928 ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES,
929 zonefs_get_zone_info_cb, zd);
931 zonefs_err(zd->sb, "Zone report failed %d\n", ret);
935 if (ret != bdev_nr_zones(bdev)) {
936 zonefs_err(zd->sb, "Invalid zone report (%d/%u zones)\n",
937 ret, bdev_nr_zones(bdev));
944 static inline void zonefs_free_zone_info(struct zonefs_zone_data *zd)
950 * Create a zone group and populate it with zone files.
952 static int zonefs_init_zgroup(struct super_block *sb,
953 struct zonefs_zone_data *zd,
954 enum zonefs_ztype ztype)
956 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
957 struct zonefs_zone_group *zgroup = &sbi->s_zgroup[ztype];
958 struct blk_zone *zone, *next, *end;
959 struct zonefs_zone *z;
963 /* Allocate the zone group. If it is empty, we have nothing to do. */
964 if (!zgroup->g_nr_zones)
967 zgroup->g_zones = kvcalloc(zgroup->g_nr_zones,
968 sizeof(struct zonefs_zone), GFP_KERNEL);
969 if (!zgroup->g_zones)
973 * Initialize the zone groups using the device zone information.
974 * We always skip the first zone as it contains the super block
975 * and is not use to back a file.
977 end = zd->zones + bdev_nr_zones(sb->s_bdev);
978 for (zone = &zd->zones[1]; zone < end; zone = next) {
981 if (zonefs_zone_type(zone) != ztype)
984 if (WARN_ON_ONCE(n >= zgroup->g_nr_zones))
988 * For conventional zones, contiguous zones can be aggregated
989 * together to form larger files. Note that this overwrites the
990 * length of the first zone of the set of contiguous zones
991 * aggregated together. If one offline or read-only zone is
992 * found, assume that all zones aggregated have the same
995 if (ztype == ZONEFS_ZTYPE_CNV &&
996 (sbi->s_features & ZONEFS_F_AGGRCNV)) {
997 for (; next < end; next++) {
998 if (zonefs_zone_type(next) != ztype)
1000 zone->len += next->len;
1001 zone->capacity += next->capacity;
1002 if (next->cond == BLK_ZONE_COND_READONLY &&
1003 zone->cond != BLK_ZONE_COND_OFFLINE)
1004 zone->cond = BLK_ZONE_COND_READONLY;
1005 else if (next->cond == BLK_ZONE_COND_OFFLINE)
1006 zone->cond = BLK_ZONE_COND_OFFLINE;
1010 z = &zgroup->g_zones[n];
1011 if (ztype == ZONEFS_ZTYPE_CNV)
1012 z->z_flags |= ZONEFS_ZONE_CNV;
1013 z->z_sector = zone->start;
1014 z->z_size = zone->len << SECTOR_SHIFT;
1015 if (z->z_size > bdev_zone_sectors(sb->s_bdev) << SECTOR_SHIFT &&
1016 !(sbi->s_features & ZONEFS_F_AGGRCNV)) {
1018 "Invalid zone size %llu (device zone sectors %llu)\n",
1020 bdev_zone_sectors(sb->s_bdev) << SECTOR_SHIFT);
1024 z->z_capacity = min_t(loff_t, MAX_LFS_FILESIZE,
1025 zone->capacity << SECTOR_SHIFT);
1026 z->z_wpoffset = zonefs_check_zone_condition(sb, z, zone);
1028 z->z_mode = S_IFREG | sbi->s_perm;
1029 z->z_uid = sbi->s_uid;
1030 z->z_gid = sbi->s_gid;
1033 * Let zonefs_inode_update_mode() know that we will need
1034 * special initialization of the inode mode the first time
1037 z->z_flags |= ZONEFS_ZONE_INIT_MODE;
1039 sb->s_maxbytes = max(z->z_capacity, sb->s_maxbytes);
1040 sbi->s_blocks += z->z_capacity >> sb->s_blocksize_bits;
1041 sbi->s_used_blocks += z->z_wpoffset >> sb->s_blocksize_bits;
1044 * For sequential zones, make sure that any open zone is closed
1045 * first to ensure that the initial number of open zones is 0,
1046 * in sync with the open zone accounting done when the mount
1047 * option ZONEFS_MNTOPT_EXPLICIT_OPEN is used.
1049 if (ztype == ZONEFS_ZTYPE_SEQ &&
1050 (zone->cond == BLK_ZONE_COND_IMP_OPEN ||
1051 zone->cond == BLK_ZONE_COND_EXP_OPEN)) {
1052 ret = zonefs_zone_mgmt(sb, z, REQ_OP_ZONE_CLOSE);
1057 zonefs_account_active(sb, z);
1062 if (WARN_ON_ONCE(n != zgroup->g_nr_zones))
1065 zonefs_info(sb, "Zone group \"%s\" has %u file%s\n",
1066 zonefs_zgroup_name(ztype),
1068 zgroup->g_nr_zones > 1 ? "s" : "");
1073 static void zonefs_free_zgroups(struct super_block *sb)
1075 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1076 enum zonefs_ztype ztype;
1081 for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
1082 kvfree(sbi->s_zgroup[ztype].g_zones);
1083 sbi->s_zgroup[ztype].g_zones = NULL;
1088 * Create a zone group and populate it with zone files.
1090 static int zonefs_init_zgroups(struct super_block *sb)
1092 struct zonefs_zone_data zd;
1093 enum zonefs_ztype ztype;
1096 /* First get the device zone information */
1097 memset(&zd, 0, sizeof(struct zonefs_zone_data));
1099 ret = zonefs_get_zone_info(&zd);
1103 /* Allocate and initialize the zone groups */
1104 for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
1105 ret = zonefs_init_zgroup(sb, &zd, ztype);
1108 "Zone group \"%s\" initialization failed\n",
1109 zonefs_zgroup_name(ztype));
1115 zonefs_free_zone_info(&zd);
1117 zonefs_free_zgroups(sb);
1123 * Read super block information from the device.
1125 static int zonefs_read_super(struct super_block *sb)
1127 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1128 struct zonefs_super *super;
1129 u32 crc, stored_crc;
1131 struct bio_vec bio_vec;
1135 page = alloc_page(GFP_KERNEL);
1139 bio_init(&bio, sb->s_bdev, &bio_vec, 1, REQ_OP_READ);
1140 bio.bi_iter.bi_sector = 0;
1141 __bio_add_page(&bio, page, PAGE_SIZE, 0);
1143 ret = submit_bio_wait(&bio);
1147 super = page_address(page);
1150 if (le32_to_cpu(super->s_magic) != ZONEFS_MAGIC)
1153 stored_crc = le32_to_cpu(super->s_crc);
1155 crc = crc32(~0U, (unsigned char *)super, sizeof(struct zonefs_super));
1156 if (crc != stored_crc) {
1157 zonefs_err(sb, "Invalid checksum (Expected 0x%08x, got 0x%08x)",
1162 sbi->s_features = le64_to_cpu(super->s_features);
1163 if (sbi->s_features & ~ZONEFS_F_DEFINED_FEATURES) {
1164 zonefs_err(sb, "Unknown features set 0x%llx\n",
1169 if (sbi->s_features & ZONEFS_F_UID) {
1170 sbi->s_uid = make_kuid(current_user_ns(),
1171 le32_to_cpu(super->s_uid));
1172 if (!uid_valid(sbi->s_uid)) {
1173 zonefs_err(sb, "Invalid UID feature\n");
1178 if (sbi->s_features & ZONEFS_F_GID) {
1179 sbi->s_gid = make_kgid(current_user_ns(),
1180 le32_to_cpu(super->s_gid));
1181 if (!gid_valid(sbi->s_gid)) {
1182 zonefs_err(sb, "Invalid GID feature\n");
1187 if (sbi->s_features & ZONEFS_F_PERM)
1188 sbi->s_perm = le32_to_cpu(super->s_perm);
1190 if (memchr_inv(super->s_reserved, 0, sizeof(super->s_reserved))) {
1191 zonefs_err(sb, "Reserved area is being used\n");
1195 import_uuid(&sbi->s_uuid, super->s_uuid);
1204 static const struct super_operations zonefs_sops = {
1205 .alloc_inode = zonefs_alloc_inode,
1206 .free_inode = zonefs_free_inode,
1207 .statfs = zonefs_statfs,
1208 .remount_fs = zonefs_remount,
1209 .show_options = zonefs_show_options,
1212 static int zonefs_get_zgroup_inodes(struct super_block *sb)
1214 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1215 struct inode *dir_inode;
1216 enum zonefs_ztype ztype;
1218 for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
1219 if (!sbi->s_zgroup[ztype].g_nr_zones)
1222 dir_inode = zonefs_get_zgroup_inode(sb, ztype);
1223 if (IS_ERR(dir_inode))
1224 return PTR_ERR(dir_inode);
1226 sbi->s_zgroup[ztype].g_inode = dir_inode;
1232 static void zonefs_release_zgroup_inodes(struct super_block *sb)
1234 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1235 enum zonefs_ztype ztype;
1240 for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
1241 if (sbi->s_zgroup[ztype].g_inode) {
1242 iput(sbi->s_zgroup[ztype].g_inode);
1243 sbi->s_zgroup[ztype].g_inode = NULL;
1249 * Check that the device is zoned. If it is, get the list of zones and create
1250 * sub-directories and files according to the device zone configuration and
1253 static int zonefs_fill_super(struct super_block *sb, void *data, int silent)
1255 struct zonefs_sb_info *sbi;
1256 struct inode *inode;
1257 enum zonefs_ztype ztype;
1260 if (!bdev_is_zoned(sb->s_bdev)) {
1261 zonefs_err(sb, "Not a zoned block device\n");
1266 * Initialize super block information: the maximum file size is updated
1267 * when the zone files are created so that the format option
1268 * ZONEFS_F_AGGRCNV which increases the maximum file size of a file
1269 * beyond the zone size is taken into account.
1271 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1275 spin_lock_init(&sbi->s_lock);
1276 sb->s_fs_info = sbi;
1277 sb->s_magic = ZONEFS_MAGIC;
1279 sb->s_op = &zonefs_sops;
1280 sb->s_time_gran = 1;
1283 * The block size is set to the device zone write granularity to ensure
1284 * that write operations are always aligned according to the device
1285 * interface constraints.
1287 sb_set_blocksize(sb, bdev_zone_write_granularity(sb->s_bdev));
1288 sbi->s_zone_sectors_shift = ilog2(bdev_zone_sectors(sb->s_bdev));
1289 sbi->s_uid = GLOBAL_ROOT_UID;
1290 sbi->s_gid = GLOBAL_ROOT_GID;
1292 sbi->s_mount_opts = ZONEFS_MNTOPT_ERRORS_RO;
1294 atomic_set(&sbi->s_wro_seq_files, 0);
1295 sbi->s_max_wro_seq_files = bdev_max_open_zones(sb->s_bdev);
1296 atomic_set(&sbi->s_active_seq_files, 0);
1297 sbi->s_max_active_seq_files = bdev_max_active_zones(sb->s_bdev);
1299 ret = zonefs_read_super(sb);
1303 ret = zonefs_parse_options(sb, data);
1307 zonefs_info(sb, "Mounting %u zones", bdev_nr_zones(sb->s_bdev));
1309 if (!sbi->s_max_wro_seq_files &&
1310 !sbi->s_max_active_seq_files &&
1311 sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) {
1313 "No open and active zone limits. Ignoring explicit_open mount option\n");
1314 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_EXPLICIT_OPEN;
1317 /* Initialize the zone groups */
1318 ret = zonefs_init_zgroups(sb);
1322 /* Create the root directory inode */
1324 inode = new_inode(sb);
1328 inode->i_ino = bdev_nr_zones(sb->s_bdev);
1329 inode->i_mode = S_IFDIR | 0555;
1330 simple_inode_init_ts(inode);
1331 inode->i_op = &zonefs_dir_inode_operations;
1332 inode->i_fop = &zonefs_dir_operations;
1334 set_nlink(inode, 2);
1335 for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
1336 if (sbi->s_zgroup[ztype].g_nr_zones) {
1342 sb->s_root = d_make_root(inode);
1347 * Take a reference on the zone groups directory inodes
1348 * to keep them in the inode cache.
1350 ret = zonefs_get_zgroup_inodes(sb);
1354 ret = zonefs_sysfs_register(sb);
1361 zonefs_release_zgroup_inodes(sb);
1362 zonefs_free_zgroups(sb);
1367 static struct dentry *zonefs_mount(struct file_system_type *fs_type,
1368 int flags, const char *dev_name, void *data)
1370 return mount_bdev(fs_type, flags, dev_name, data, zonefs_fill_super);
1373 static void zonefs_kill_super(struct super_block *sb)
1375 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1377 /* Release the reference on the zone group directory inodes */
1378 zonefs_release_zgroup_inodes(sb);
1380 kill_block_super(sb);
1382 zonefs_sysfs_unregister(sb);
1383 zonefs_free_zgroups(sb);
1388 * File system definition and registration.
1390 static struct file_system_type zonefs_type = {
1391 .owner = THIS_MODULE,
1393 .mount = zonefs_mount,
1394 .kill_sb = zonefs_kill_super,
1395 .fs_flags = FS_REQUIRES_DEV,
1398 static int __init zonefs_init_inodecache(void)
1400 zonefs_inode_cachep = kmem_cache_create("zonefs_inode_cache",
1401 sizeof(struct zonefs_inode_info), 0,
1402 (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT),
1404 if (zonefs_inode_cachep == NULL)
1409 static void zonefs_destroy_inodecache(void)
1412 * Make sure all delayed rcu free inodes are flushed before we
1413 * destroy the inode cache.
1416 kmem_cache_destroy(zonefs_inode_cachep);
1419 static int __init zonefs_init(void)
1423 BUILD_BUG_ON(sizeof(struct zonefs_super) != ZONEFS_SUPER_SIZE);
1425 ret = zonefs_init_inodecache();
1429 ret = zonefs_sysfs_init();
1431 goto destroy_inodecache;
1433 ret = register_filesystem(&zonefs_type);
1440 zonefs_sysfs_exit();
1442 zonefs_destroy_inodecache();
1447 static void __exit zonefs_exit(void)
1449 unregister_filesystem(&zonefs_type);
1450 zonefs_sysfs_exit();
1451 zonefs_destroy_inodecache();
1454 MODULE_AUTHOR("Damien Le Moal");
1455 MODULE_DESCRIPTION("Zone file system for zoned block devices");
1456 MODULE_LICENSE("GPL");
1457 MODULE_ALIAS_FS("zonefs");
1458 module_init(zonefs_init);
1459 module_exit(zonefs_exit);