1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Simple zone file system for zoned block devices.
5 * Copyright (C) 2019 Western Digital Corporation or its affiliates.
11 #include <linux/magic.h>
12 #include <linux/uuid.h>
13 #include <linux/mutex.h>
14 #include <linux/rwsem.h>
15 #include <linux/kobject.h>
18 * Maximum length of file names: this only needs to be large enough to fit
19 * the zone group directory names and a decimal zone number for file names.
20 * 16 characters is plenty.
22 #define ZONEFS_NAME_MAX 16
25 * Zone types: ZONEFS_ZTYPE_SEQ is used for all sequential zone types
26 * defined in linux/blkzoned.h, that is, BLK_ZONE_TYPE_SEQWRITE_REQ and
27 * BLK_ZONE_TYPE_SEQWRITE_PREF.
35 static inline enum zonefs_ztype zonefs_zone_type(struct blk_zone *zone)
37 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
38 return ZONEFS_ZTYPE_CNV;
39 return ZONEFS_ZTYPE_SEQ;
42 #define ZONEFS_ZONE_OPEN (1 << 0)
43 #define ZONEFS_ZONE_ACTIVE (1 << 1)
46 * In-memory inode data.
48 struct zonefs_inode_info {
52 enum zonefs_ztype i_ztype;
54 /* File zone start sector (512B unit) */
57 /* File zone write pointer position (sequential zones only) */
60 /* File maximum size */
67 * To serialise fully against both syscall and mmap based IO and
68 * sequential file truncation, two locks are used. For serializing
69 * zonefs_seq_file_truncate() against zonefs_iomap_begin(), that is,
70 * file truncate operations against block mapping, i_truncate_mutex is
71 * used. i_truncate_mutex also protects against concurrent accesses
72 * and changes to the inode private data, and in particular changes to
73 * a sequential file size on completion of direct IO writes.
74 * Serialization of mmap read IOs with truncate and syscall IO
75 * operations is done with invalidate_lock in addition to
76 * i_truncate_mutex. Only zonefs_seq_file_truncate() takes both lock
77 * (invalidate_lock first, i_truncate_mutex second).
79 struct mutex i_truncate_mutex;
81 /* guarded by i_truncate_mutex */
82 unsigned int i_wr_refcnt;
86 static inline struct zonefs_inode_info *ZONEFS_I(struct inode *inode)
88 return container_of(inode, struct zonefs_inode_info, i_vnode);
92 * On-disk super block (block 0).
94 #define ZONEFS_LABEL_LEN 64
95 #define ZONEFS_UUID_SIZE 16
96 #define ZONEFS_SUPER_SIZE 4096
107 char s_label[ZONEFS_LABEL_LEN];
110 __u8 s_uuid[ZONEFS_UUID_SIZE];
115 /* UID/GID to use for files */
119 /* File permissions */
122 /* Padding to ZONEFS_SUPER_SIZE bytes */
123 __u8 s_reserved[3988];
128 * Feature flags: specified in the s_features field of the on-disk super
129 * block struct zonefs_super and in-memory in the s_feartures field of
130 * struct zonefs_sb_info.
132 enum zonefs_features {
134 * Aggregate contiguous conventional zones into a single file.
136 ZONEFS_F_AGGRCNV = 1ULL << 0,
138 * Use super block specified UID for files instead of default 0.
140 ZONEFS_F_UID = 1ULL << 1,
142 * Use super block specified GID for files instead of default 0.
144 ZONEFS_F_GID = 1ULL << 2,
146 * Use super block specified file permissions instead of default 640.
148 ZONEFS_F_PERM = 1ULL << 3,
151 #define ZONEFS_F_DEFINED_FEATURES \
152 (ZONEFS_F_AGGRCNV | ZONEFS_F_UID | ZONEFS_F_GID | ZONEFS_F_PERM)
155 * Mount options for zone write pointer error handling.
157 #define ZONEFS_MNTOPT_ERRORS_RO (1 << 0) /* Make zone file readonly */
158 #define ZONEFS_MNTOPT_ERRORS_ZRO (1 << 1) /* Make zone file offline */
159 #define ZONEFS_MNTOPT_ERRORS_ZOL (1 << 2) /* Make zone file offline */
160 #define ZONEFS_MNTOPT_ERRORS_REPAIR (1 << 3) /* Remount read-only */
161 #define ZONEFS_MNTOPT_ERRORS_MASK \
162 (ZONEFS_MNTOPT_ERRORS_RO | ZONEFS_MNTOPT_ERRORS_ZRO | \
163 ZONEFS_MNTOPT_ERRORS_ZOL | ZONEFS_MNTOPT_ERRORS_REPAIR)
164 #define ZONEFS_MNTOPT_EXPLICIT_OPEN (1 << 4) /* Explicit open/close of zones on open/close */
167 * In-memory Super block information.
169 struct zonefs_sb_info {
171 unsigned long s_mount_opts;
175 unsigned long long s_features;
180 unsigned int s_zone_sectors_shift;
182 unsigned int s_nr_files[ZONEFS_ZTYPE_MAX];
185 loff_t s_used_blocks;
187 unsigned int s_max_wro_seq_files;
188 atomic_t s_wro_seq_files;
190 unsigned int s_max_active_seq_files;
191 atomic_t s_active_seq_files;
193 bool s_sysfs_registered;
194 struct kobject s_kobj;
195 struct completion s_kobj_unregister;
198 static inline struct zonefs_sb_info *ZONEFS_SB(struct super_block *sb)
200 return sb->s_fs_info;
203 #define zonefs_info(sb, format, args...) \
204 pr_info("zonefs (%s): " format, sb->s_id, ## args)
205 #define zonefs_err(sb, format, args...) \
206 pr_err("zonefs (%s) ERROR: " format, sb->s_id, ## args)
207 #define zonefs_warn(sb, format, args...) \
208 pr_warn("zonefs (%s) WARNING: " format, sb->s_id, ## args)
210 int zonefs_sysfs_register(struct super_block *sb);
211 void zonefs_sysfs_unregister(struct super_block *sb);
212 int zonefs_sysfs_init(void);
213 void zonefs_sysfs_exit(void);