xref: /linux/fs/zonefs/zonefs.h (revision 1c07425e902cd3137961c3d45b4271bf8a9b8eb9)
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_OPEN	(1U << 0)
43 #define ZONEFS_ZONE_ACTIVE	(1U << 1)
44 #define ZONEFS_ZONE_OFFLINE	(1U << 2)
45 #define ZONEFS_ZONE_READONLY	(1U << 3)
46 
47 /*
48  * In-memory inode data.
49  */
50 struct zonefs_inode_info {
51 	struct inode		i_vnode;
52 
53 	/* File zone type */
54 	enum zonefs_ztype	i_ztype;
55 
56 	/* File zone start sector (512B unit) */
57 	sector_t		i_zsector;
58 
59 	/* File zone write pointer position (sequential zones only) */
60 	loff_t			i_wpoffset;
61 
62 	/* File maximum size */
63 	loff_t			i_max_size;
64 
65 	/* File zone size */
66 	loff_t			i_zone_size;
67 
68 	/*
69 	 * To serialise fully against both syscall and mmap based IO and
70 	 * sequential file truncation, two locks are used. For serializing
71 	 * zonefs_seq_file_truncate() against zonefs_iomap_begin(), that is,
72 	 * file truncate operations against block mapping, i_truncate_mutex is
73 	 * used. i_truncate_mutex also protects against concurrent accesses
74 	 * and changes to the inode private data, and in particular changes to
75 	 * a sequential file size on completion of direct IO writes.
76 	 * Serialization of mmap read IOs with truncate and syscall IO
77 	 * operations is done with invalidate_lock in addition to
78 	 * i_truncate_mutex.  Only zonefs_seq_file_truncate() takes both lock
79 	 * (invalidate_lock first, i_truncate_mutex second).
80 	 */
81 	struct mutex		i_truncate_mutex;
82 
83 	/* guarded by i_truncate_mutex */
84 	unsigned int		i_wr_refcnt;
85 	unsigned int		i_flags;
86 };
87 
88 static inline struct zonefs_inode_info *ZONEFS_I(struct inode *inode)
89 {
90 	return container_of(inode, struct zonefs_inode_info, i_vnode);
91 }
92 
93 /*
94  * On-disk super block (block 0).
95  */
96 #define ZONEFS_LABEL_LEN	64
97 #define ZONEFS_UUID_SIZE	16
98 #define ZONEFS_SUPER_SIZE	4096
99 
100 struct zonefs_super {
101 
102 	/* Magic number */
103 	__le32		s_magic;
104 
105 	/* Checksum */
106 	__le32		s_crc;
107 
108 	/* Volume label */
109 	char		s_label[ZONEFS_LABEL_LEN];
110 
111 	/* 128-bit uuid */
112 	__u8		s_uuid[ZONEFS_UUID_SIZE];
113 
114 	/* Features */
115 	__le64		s_features;
116 
117 	/* UID/GID to use for files */
118 	__le32		s_uid;
119 	__le32		s_gid;
120 
121 	/* File permissions */
122 	__le32		s_perm;
123 
124 	/* Padding to ZONEFS_SUPER_SIZE bytes */
125 	__u8		s_reserved[3988];
126 
127 } __packed;
128 
129 /*
130  * Feature flags: specified in the s_features field of the on-disk super
131  * block struct zonefs_super and in-memory in the s_feartures field of
132  * struct zonefs_sb_info.
133  */
134 enum zonefs_features {
135 	/*
136 	 * Aggregate contiguous conventional zones into a single file.
137 	 */
138 	ZONEFS_F_AGGRCNV = 1ULL << 0,
139 	/*
140 	 * Use super block specified UID for files instead of default 0.
141 	 */
142 	ZONEFS_F_UID = 1ULL << 1,
143 	/*
144 	 * Use super block specified GID for files instead of default 0.
145 	 */
146 	ZONEFS_F_GID = 1ULL << 2,
147 	/*
148 	 * Use super block specified file permissions instead of default 640.
149 	 */
150 	ZONEFS_F_PERM = 1ULL << 3,
151 };
152 
153 #define ZONEFS_F_DEFINED_FEATURES \
154 	(ZONEFS_F_AGGRCNV | ZONEFS_F_UID | ZONEFS_F_GID | ZONEFS_F_PERM)
155 
156 /*
157  * Mount options for zone write pointer error handling.
158  */
159 #define ZONEFS_MNTOPT_ERRORS_RO		(1 << 0) /* Make zone file readonly */
160 #define ZONEFS_MNTOPT_ERRORS_ZRO	(1 << 1) /* Make zone file offline */
161 #define ZONEFS_MNTOPT_ERRORS_ZOL	(1 << 2) /* Make zone file offline */
162 #define ZONEFS_MNTOPT_ERRORS_REPAIR	(1 << 3) /* Remount read-only */
163 #define ZONEFS_MNTOPT_ERRORS_MASK	\
164 	(ZONEFS_MNTOPT_ERRORS_RO | ZONEFS_MNTOPT_ERRORS_ZRO | \
165 	 ZONEFS_MNTOPT_ERRORS_ZOL | ZONEFS_MNTOPT_ERRORS_REPAIR)
166 #define ZONEFS_MNTOPT_EXPLICIT_OPEN	(1 << 4) /* Explicit open/close of zones on open/close */
167 
168 /*
169  * In-memory Super block information.
170  */
171 struct zonefs_sb_info {
172 
173 	unsigned long		s_mount_opts;
174 
175 	spinlock_t		s_lock;
176 
177 	unsigned long long	s_features;
178 	kuid_t			s_uid;
179 	kgid_t			s_gid;
180 	umode_t			s_perm;
181 	uuid_t			s_uuid;
182 	unsigned int		s_zone_sectors_shift;
183 
184 	unsigned int		s_nr_files[ZONEFS_ZTYPE_MAX];
185 
186 	loff_t			s_blocks;
187 	loff_t			s_used_blocks;
188 
189 	unsigned int		s_max_wro_seq_files;
190 	atomic_t		s_wro_seq_files;
191 
192 	unsigned int		s_max_active_seq_files;
193 	atomic_t		s_active_seq_files;
194 
195 	bool			s_sysfs_registered;
196 	struct kobject		s_kobj;
197 	struct completion	s_kobj_unregister;
198 };
199 
200 static inline struct zonefs_sb_info *ZONEFS_SB(struct super_block *sb)
201 {
202 	return sb->s_fs_info;
203 }
204 
205 #define zonefs_info(sb, format, args...)	\
206 	pr_info("zonefs (%s): " format, sb->s_id, ## args)
207 #define zonefs_err(sb, format, args...)		\
208 	pr_err("zonefs (%s) ERROR: " format, sb->s_id, ## args)
209 #define zonefs_warn(sb, format, args...)	\
210 	pr_warn("zonefs (%s) WARNING: " format, sb->s_id, ## args)
211 
212 int zonefs_sysfs_register(struct super_block *sb);
213 void zonefs_sysfs_unregister(struct super_block *sb);
214 int zonefs_sysfs_init(void);
215 void zonefs_sysfs_exit(void);
216 
217 #endif
218