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