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>
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.
21 #define ZONEFS_NAME_MAX 16
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.
34 static inline enum zonefs_ztype
zonefs_zone_type(struct blk_zone
*zone
)
36 if (zone
->type
== BLK_ZONE_TYPE_CONVENTIONAL
)
37 return ZONEFS_ZTYPE_CNV
;
38 return ZONEFS_ZTYPE_SEQ
;
42 * In-memory inode data.
44 struct zonefs_inode_info
{
48 enum zonefs_ztype i_ztype
;
50 /* File zone start sector (512B unit) */
53 /* File zone write pointer position (sequential zones only) */
56 /* File maximum size */
60 * To serialise fully against both syscall and mmap based IO and
61 * sequential file truncation, two locks are used. For serializing
62 * zonefs_seq_file_truncate() against zonefs_iomap_begin(), that is,
63 * file truncate operations against block mapping, i_truncate_mutex is
64 * used. i_truncate_mutex also protects against concurrent accesses
65 * and changes to the inode private data, and in particular changes to
66 * a sequential file size on completion of direct IO writes.
67 * Serialization of mmap read IOs with truncate and syscall IO
68 * operations is done with i_mmap_sem in addition to i_truncate_mutex.
69 * Only zonefs_seq_file_truncate() takes both lock (i_mmap_sem first,
70 * i_truncate_mutex second).
72 struct mutex i_truncate_mutex
;
73 struct rw_semaphore i_mmap_sem
;
76 static inline struct zonefs_inode_info
*ZONEFS_I(struct inode
*inode
)
78 return container_of(inode
, struct zonefs_inode_info
, i_vnode
);
82 * On-disk super block (block 0).
84 #define ZONEFS_LABEL_LEN 64
85 #define ZONEFS_UUID_SIZE 16
86 #define ZONEFS_SUPER_SIZE 4096
97 char s_label
[ZONEFS_LABEL_LEN
];
100 __u8 s_uuid
[ZONEFS_UUID_SIZE
];
105 /* UID/GID to use for files */
109 /* File permissions */
112 /* Padding to ZONEFS_SUPER_SIZE bytes */
113 __u8 s_reserved
[3988];
118 * Feature flags: specified in the s_features field of the on-disk super
119 * block struct zonefs_super and in-memory in the s_feartures field of
120 * struct zonefs_sb_info.
122 enum zonefs_features
{
124 * Aggregate contiguous conventional zones into a single file.
126 ZONEFS_F_AGGRCNV
= 1ULL << 0,
128 * Use super block specified UID for files instead of default 0.
130 ZONEFS_F_UID
= 1ULL << 1,
132 * Use super block specified GID for files instead of default 0.
134 ZONEFS_F_GID
= 1ULL << 2,
136 * Use super block specified file permissions instead of default 640.
138 ZONEFS_F_PERM
= 1ULL << 3,
141 #define ZONEFS_F_DEFINED_FEATURES \
142 (ZONEFS_F_AGGRCNV | ZONEFS_F_UID | ZONEFS_F_GID | ZONEFS_F_PERM)
145 * Mount options for zone write pointer error handling.
147 #define ZONEFS_MNTOPT_ERRORS_RO (1 << 0) /* Make zone file readonly */
148 #define ZONEFS_MNTOPT_ERRORS_ZRO (1 << 1) /* Make zone file offline */
149 #define ZONEFS_MNTOPT_ERRORS_ZOL (1 << 2) /* Make zone file offline */
150 #define ZONEFS_MNTOPT_ERRORS_REPAIR (1 << 3) /* Remount read-only */
151 #define ZONEFS_MNTOPT_ERRORS_MASK \
152 (ZONEFS_MNTOPT_ERRORS_RO | ZONEFS_MNTOPT_ERRORS_ZRO | \
153 ZONEFS_MNTOPT_ERRORS_ZOL | ZONEFS_MNTOPT_ERRORS_REPAIR)
156 * In-memory Super block information.
158 struct zonefs_sb_info
{
160 unsigned long s_mount_opts
;
164 unsigned long long s_features
;
169 unsigned int s_zone_sectors_shift
;
171 unsigned int s_nr_files
[ZONEFS_ZTYPE_MAX
];
174 loff_t s_used_blocks
;
177 static inline struct zonefs_sb_info
*ZONEFS_SB(struct super_block
*sb
)
179 return sb
->s_fs_info
;
182 #define zonefs_info(sb, format, args...) \
183 pr_info("zonefs (%s): " format, sb->s_id, ## args)
184 #define zonefs_err(sb, format, args...) \
185 pr_err("zonefs (%s) ERROR: " format, sb->s_id, ## args)
186 #define zonefs_warn(sb, format, args...) \
187 pr_warn("zonefs (%s) WARNING: " format, sb->s_id, ## args)