xref: /openbmc/linux/fs/zonefs/zonefs.h (revision d207794a)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Simple zone file system for zoned block devices.
4  *
5  * Copyright (C) 2019 Western Digital Corporation or its affiliates.
6  */
7 #ifndef __ZONEFS_H__
8 #define __ZONEFS_H__
9 
10 #include <linux/fs.h>
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>
16 
17 /*
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.
21  */
22 #define ZONEFS_NAME_MAX		16
23 
24 /*
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.
28  */
29 enum zonefs_ztype {
30 	ZONEFS_ZTYPE_CNV,
31 	ZONEFS_ZTYPE_SEQ,
32 	ZONEFS_ZTYPE_MAX,
33 };
34 
35 static inline enum zonefs_ztype zonefs_zone_type(struct blk_zone *zone)
36 {
37 	if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
38 		return ZONEFS_ZTYPE_CNV;
39 	return ZONEFS_ZTYPE_SEQ;
40 }
41 
42 #define ZONEFS_ZONE_INIT_MODE	(1U << 0)
43 #define ZONEFS_ZONE_OPEN	(1U << 1)
44 #define ZONEFS_ZONE_ACTIVE	(1U << 2)
45 #define ZONEFS_ZONE_OFFLINE	(1U << 3)
46 #define ZONEFS_ZONE_READONLY	(1U << 4)
47 #define ZONEFS_ZONE_CNV		(1U << 31)
48 
49 /*
50  * In-memory per-file inode zone data.
51  */
52 struct zonefs_zone {
53 	/* Zone state flags */
54 	unsigned int		z_flags;
55 
56 	/* Zone start sector (512B unit) */
57 	sector_t		z_sector;
58 
59 	/* Zone size (bytes) */
60 	loff_t			z_size;
61 
62 	/* Zone capacity (file maximum size, bytes) */
63 	loff_t			z_capacity;
64 
65 	/* Write pointer offset in the zone (sequential zones only, bytes) */
66 	loff_t			z_wpoffset;
67 
68 	/* Saved inode uid, gid and access rights */
69 	umode_t			z_mode;
70 	kuid_t			z_uid;
71 	kgid_t			z_gid;
72 };
73 
74 /*
75  * In memory zone group information: all zones of a group are exposed
76  * as files, one file per zone.
77  */
78 struct zonefs_zone_group {
79 	unsigned int		g_nr_zones;
80 	struct zonefs_zone	*g_zones;
81 };
82 
83 /*
84  * In-memory inode data.
85  */
86 struct zonefs_inode_info {
87 	struct inode		i_vnode;
88 
89 	/*
90 	 * To serialise fully against both syscall and mmap based IO and
91 	 * sequential file truncation, two locks are used. For serializing
92 	 * zonefs_seq_file_truncate() against zonefs_iomap_begin(), that is,
93 	 * file truncate operations against block mapping, i_truncate_mutex is
94 	 * used. i_truncate_mutex also protects against concurrent accesses
95 	 * and changes to the inode private data, and in particular changes to
96 	 * a sequential file size on completion of direct IO writes.
97 	 * Serialization of mmap read IOs with truncate and syscall IO
98 	 * operations is done with invalidate_lock in addition to
99 	 * i_truncate_mutex.  Only zonefs_seq_file_truncate() takes both lock
100 	 * (invalidate_lock first, i_truncate_mutex second).
101 	 */
102 	struct mutex		i_truncate_mutex;
103 
104 	/* guarded by i_truncate_mutex */
105 	unsigned int		i_wr_refcnt;
106 };
107 
108 static inline struct zonefs_inode_info *ZONEFS_I(struct inode *inode)
109 {
110 	return container_of(inode, struct zonefs_inode_info, i_vnode);
111 }
112 
113 static inline bool zonefs_zone_is_cnv(struct zonefs_zone *z)
114 {
115 	return z->z_flags & ZONEFS_ZONE_CNV;
116 }
117 
118 static inline bool zonefs_zone_is_seq(struct zonefs_zone *z)
119 {
120 	return !zonefs_zone_is_cnv(z);
121 }
122 
123 static inline struct zonefs_zone *zonefs_inode_zone(struct inode *inode)
124 {
125 	return inode->i_private;
126 }
127 
128 static inline bool zonefs_inode_is_cnv(struct inode *inode)
129 {
130 	return zonefs_zone_is_cnv(zonefs_inode_zone(inode));
131 }
132 
133 static inline bool zonefs_inode_is_seq(struct inode *inode)
134 {
135 	return zonefs_zone_is_seq(zonefs_inode_zone(inode));
136 }
137 
138 /*
139  * On-disk super block (block 0).
140  */
141 #define ZONEFS_LABEL_LEN	64
142 #define ZONEFS_UUID_SIZE	16
143 #define ZONEFS_SUPER_SIZE	4096
144 
145 struct zonefs_super {
146 
147 	/* Magic number */
148 	__le32		s_magic;
149 
150 	/* Checksum */
151 	__le32		s_crc;
152 
153 	/* Volume label */
154 	char		s_label[ZONEFS_LABEL_LEN];
155 
156 	/* 128-bit uuid */
157 	__u8		s_uuid[ZONEFS_UUID_SIZE];
158 
159 	/* Features */
160 	__le64		s_features;
161 
162 	/* UID/GID to use for files */
163 	__le32		s_uid;
164 	__le32		s_gid;
165 
166 	/* File permissions */
167 	__le32		s_perm;
168 
169 	/* Padding to ZONEFS_SUPER_SIZE bytes */
170 	__u8		s_reserved[3988];
171 
172 } __packed;
173 
174 /*
175  * Feature flags: specified in the s_features field of the on-disk super
176  * block struct zonefs_super and in-memory in the s_feartures field of
177  * struct zonefs_sb_info.
178  */
179 enum zonefs_features {
180 	/*
181 	 * Aggregate contiguous conventional zones into a single file.
182 	 */
183 	ZONEFS_F_AGGRCNV = 1ULL << 0,
184 	/*
185 	 * Use super block specified UID for files instead of default 0.
186 	 */
187 	ZONEFS_F_UID = 1ULL << 1,
188 	/*
189 	 * Use super block specified GID for files instead of default 0.
190 	 */
191 	ZONEFS_F_GID = 1ULL << 2,
192 	/*
193 	 * Use super block specified file permissions instead of default 640.
194 	 */
195 	ZONEFS_F_PERM = 1ULL << 3,
196 };
197 
198 #define ZONEFS_F_DEFINED_FEATURES \
199 	(ZONEFS_F_AGGRCNV | ZONEFS_F_UID | ZONEFS_F_GID | ZONEFS_F_PERM)
200 
201 /*
202  * Mount options for zone write pointer error handling.
203  */
204 #define ZONEFS_MNTOPT_ERRORS_RO		(1 << 0) /* Make zone file readonly */
205 #define ZONEFS_MNTOPT_ERRORS_ZRO	(1 << 1) /* Make zone file offline */
206 #define ZONEFS_MNTOPT_ERRORS_ZOL	(1 << 2) /* Make zone file offline */
207 #define ZONEFS_MNTOPT_ERRORS_REPAIR	(1 << 3) /* Remount read-only */
208 #define ZONEFS_MNTOPT_ERRORS_MASK	\
209 	(ZONEFS_MNTOPT_ERRORS_RO | ZONEFS_MNTOPT_ERRORS_ZRO | \
210 	 ZONEFS_MNTOPT_ERRORS_ZOL | ZONEFS_MNTOPT_ERRORS_REPAIR)
211 #define ZONEFS_MNTOPT_EXPLICIT_OPEN	(1 << 4) /* Explicit open/close of zones on open/close */
212 
213 /*
214  * In-memory Super block information.
215  */
216 struct zonefs_sb_info {
217 
218 	unsigned long		s_mount_opts;
219 
220 	spinlock_t		s_lock;
221 
222 	unsigned long long	s_features;
223 	kuid_t			s_uid;
224 	kgid_t			s_gid;
225 	umode_t			s_perm;
226 	uuid_t			s_uuid;
227 	unsigned int		s_zone_sectors_shift;
228 
229 	struct zonefs_zone_group s_zgroup[ZONEFS_ZTYPE_MAX];
230 
231 	loff_t			s_blocks;
232 	loff_t			s_used_blocks;
233 
234 	unsigned int		s_max_wro_seq_files;
235 	atomic_t		s_wro_seq_files;
236 
237 	unsigned int		s_max_active_seq_files;
238 	atomic_t		s_active_seq_files;
239 
240 	bool			s_sysfs_registered;
241 	struct kobject		s_kobj;
242 	struct completion	s_kobj_unregister;
243 };
244 
245 static inline struct zonefs_sb_info *ZONEFS_SB(struct super_block *sb)
246 {
247 	return sb->s_fs_info;
248 }
249 
250 #define zonefs_info(sb, format, args...)	\
251 	pr_info("zonefs (%s): " format, sb->s_id, ## args)
252 #define zonefs_err(sb, format, args...)		\
253 	pr_err("zonefs (%s) ERROR: " format, sb->s_id, ## args)
254 #define zonefs_warn(sb, format, args...)	\
255 	pr_warn("zonefs (%s) WARNING: " format, sb->s_id, ## args)
256 
257 /* In super.c */
258 void zonefs_inode_account_active(struct inode *inode);
259 int zonefs_inode_zone_mgmt(struct inode *inode, enum req_op op);
260 void zonefs_i_size_write(struct inode *inode, loff_t isize);
261 void zonefs_update_stats(struct inode *inode, loff_t new_isize);
262 void __zonefs_io_error(struct inode *inode, bool write);
263 
264 static inline void zonefs_io_error(struct inode *inode, bool write)
265 {
266 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
267 
268 	mutex_lock(&zi->i_truncate_mutex);
269 	__zonefs_io_error(inode, write);
270 	mutex_unlock(&zi->i_truncate_mutex);
271 }
272 
273 /* In super.c */
274 extern const struct inode_operations zonefs_dir_inode_operations;
275 extern const struct file_operations zonefs_dir_operations;
276 
277 /* In file.c */
278 extern const struct address_space_operations zonefs_file_aops;
279 extern const struct file_operations zonefs_file_operations;
280 int zonefs_file_truncate(struct inode *inode, loff_t isize);
281 
282 /* In sysfs.c */
283 int zonefs_sysfs_register(struct super_block *sb);
284 void zonefs_sysfs_unregister(struct super_block *sb);
285 int zonefs_sysfs_init(void);
286 void zonefs_sysfs_exit(void);
287 
288 #endif
289