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