18dcc1a9dSDamien Le Moal /* SPDX-License-Identifier: GPL-2.0 */
28dcc1a9dSDamien Le Moal /*
38dcc1a9dSDamien Le Moal * Simple zone file system for zoned block devices.
48dcc1a9dSDamien Le Moal *
58dcc1a9dSDamien Le Moal * Copyright (C) 2019 Western Digital Corporation or its affiliates.
68dcc1a9dSDamien Le Moal */
78dcc1a9dSDamien Le Moal #ifndef __ZONEFS_H__
88dcc1a9dSDamien Le Moal #define __ZONEFS_H__
98dcc1a9dSDamien Le Moal
108dcc1a9dSDamien Le Moal #include <linux/fs.h>
118dcc1a9dSDamien Le Moal #include <linux/magic.h>
128dcc1a9dSDamien Le Moal #include <linux/uuid.h>
138dcc1a9dSDamien Le Moal #include <linux/mutex.h>
148dcc1a9dSDamien Le Moal #include <linux/rwsem.h>
159277a6d4SDamien Le Moal #include <linux/kobject.h>
168dcc1a9dSDamien Le Moal
178dcc1a9dSDamien Le Moal /*
188dcc1a9dSDamien Le Moal * Maximum length of file names: this only needs to be large enough to fit
198dcc1a9dSDamien Le Moal * the zone group directory names and a decimal zone number for file names.
208dcc1a9dSDamien Le Moal * 16 characters is plenty.
218dcc1a9dSDamien Le Moal */
228dcc1a9dSDamien Le Moal #define ZONEFS_NAME_MAX 16
238dcc1a9dSDamien Le Moal
248dcc1a9dSDamien Le Moal /*
258dcc1a9dSDamien Le Moal * Zone types: ZONEFS_ZTYPE_SEQ is used for all sequential zone types
268dcc1a9dSDamien Le Moal * defined in linux/blkzoned.h, that is, BLK_ZONE_TYPE_SEQWRITE_REQ and
278dcc1a9dSDamien Le Moal * BLK_ZONE_TYPE_SEQWRITE_PREF.
288dcc1a9dSDamien Le Moal */
298dcc1a9dSDamien Le Moal enum zonefs_ztype {
308dcc1a9dSDamien Le Moal ZONEFS_ZTYPE_CNV,
318dcc1a9dSDamien Le Moal ZONEFS_ZTYPE_SEQ,
328dcc1a9dSDamien Le Moal ZONEFS_ZTYPE_MAX,
338dcc1a9dSDamien Le Moal };
348dcc1a9dSDamien Le Moal
zonefs_zone_type(struct blk_zone * zone)358dcc1a9dSDamien Le Moal static inline enum zonefs_ztype zonefs_zone_type(struct blk_zone *zone)
368dcc1a9dSDamien Le Moal {
378dcc1a9dSDamien Le Moal if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
388dcc1a9dSDamien Le Moal return ZONEFS_ZTYPE_CNV;
398dcc1a9dSDamien Le Moal return ZONEFS_ZTYPE_SEQ;
408dcc1a9dSDamien Le Moal }
418dcc1a9dSDamien Le Moal
4246a9c526SDamien Le Moal #define ZONEFS_ZONE_INIT_MODE (1U << 0)
4346a9c526SDamien Le Moal #define ZONEFS_ZONE_OPEN (1U << 1)
4446a9c526SDamien Le Moal #define ZONEFS_ZONE_ACTIVE (1U << 2)
4546a9c526SDamien Le Moal #define ZONEFS_ZONE_OFFLINE (1U << 3)
4646a9c526SDamien Le Moal #define ZONEFS_ZONE_READONLY (1U << 4)
4734422914SDamien Le Moal #define ZONEFS_ZONE_CNV (1U << 31)
48b5c00e97SJohannes Thumshirn
498dcc1a9dSDamien Le Moal /*
50aa7f243fSDamien Le Moal * In-memory per-file inode zone data.
51aa7f243fSDamien Le Moal */
52aa7f243fSDamien Le Moal struct zonefs_zone {
53aa7f243fSDamien Le Moal /* Zone state flags */
54aa7f243fSDamien Le Moal unsigned int z_flags;
55aa7f243fSDamien Le Moal
56aa7f243fSDamien Le Moal /* Zone start sector (512B unit) */
57aa7f243fSDamien Le Moal sector_t z_sector;
58aa7f243fSDamien Le Moal
59aa7f243fSDamien Le Moal /* Zone size (bytes) */
60aa7f243fSDamien Le Moal loff_t z_size;
61aa7f243fSDamien Le Moal
62aa7f243fSDamien Le Moal /* Zone capacity (file maximum size, bytes) */
63aa7f243fSDamien Le Moal loff_t z_capacity;
64aa7f243fSDamien Le Moal
65aa7f243fSDamien Le Moal /* Write pointer offset in the zone (sequential zones only, bytes) */
66aa7f243fSDamien Le Moal loff_t z_wpoffset;
67d207794aSDamien Le Moal
68d207794aSDamien Le Moal /* Saved inode uid, gid and access rights */
69d207794aSDamien Le Moal umode_t z_mode;
70d207794aSDamien Le Moal kuid_t z_uid;
71d207794aSDamien Le Moal kgid_t z_gid;
72aa7f243fSDamien Le Moal };
73aa7f243fSDamien Le Moal
74aa7f243fSDamien Le Moal /*
75aa7f243fSDamien Le Moal * In memory zone group information: all zones of a group are exposed
76aa7f243fSDamien Le Moal * as files, one file per zone.
77aa7f243fSDamien Le Moal */
78aa7f243fSDamien Le Moal struct zonefs_zone_group {
79*43592c46SDamien Le Moal struct inode *g_inode;
80aa7f243fSDamien Le Moal unsigned int g_nr_zones;
81aa7f243fSDamien Le Moal struct zonefs_zone *g_zones;
82aa7f243fSDamien Le Moal };
83aa7f243fSDamien Le Moal
84aa7f243fSDamien Le Moal /*
858dcc1a9dSDamien Le Moal * In-memory inode data.
868dcc1a9dSDamien Le Moal */
878dcc1a9dSDamien Le Moal struct zonefs_inode_info {
888dcc1a9dSDamien Le Moal struct inode i_vnode;
898dcc1a9dSDamien Le Moal
908dcc1a9dSDamien Le Moal /*
918dcc1a9dSDamien Le Moal * To serialise fully against both syscall and mmap based IO and
928dcc1a9dSDamien Le Moal * sequential file truncation, two locks are used. For serializing
938dcc1a9dSDamien Le Moal * zonefs_seq_file_truncate() against zonefs_iomap_begin(), that is,
948dcc1a9dSDamien Le Moal * file truncate operations against block mapping, i_truncate_mutex is
958dcc1a9dSDamien Le Moal * used. i_truncate_mutex also protects against concurrent accesses
968dcc1a9dSDamien Le Moal * and changes to the inode private data, and in particular changes to
978dcc1a9dSDamien Le Moal * a sequential file size on completion of direct IO writes.
988dcc1a9dSDamien Le Moal * Serialization of mmap read IOs with truncate and syscall IO
99448f9490SJan Kara * operations is done with invalidate_lock in addition to
100448f9490SJan Kara * i_truncate_mutex. Only zonefs_seq_file_truncate() takes both lock
101448f9490SJan Kara * (invalidate_lock first, i_truncate_mutex second).
1028dcc1a9dSDamien Le Moal */
1038dcc1a9dSDamien Le Moal struct mutex i_truncate_mutex;
104b5c00e97SJohannes Thumshirn
105b5c00e97SJohannes Thumshirn /* guarded by i_truncate_mutex */
106b5c00e97SJohannes Thumshirn unsigned int i_wr_refcnt;
1078dcc1a9dSDamien Le Moal };
1088dcc1a9dSDamien Le Moal
ZONEFS_I(struct inode * inode)1098dcc1a9dSDamien Le Moal static inline struct zonefs_inode_info *ZONEFS_I(struct inode *inode)
1108dcc1a9dSDamien Le Moal {
1118dcc1a9dSDamien Le Moal return container_of(inode, struct zonefs_inode_info, i_vnode);
1128dcc1a9dSDamien Le Moal }
1138dcc1a9dSDamien Le Moal
zonefs_zone_is_cnv(struct zonefs_zone * z)114aa7f243fSDamien Le Moal static inline bool zonefs_zone_is_cnv(struct zonefs_zone *z)
11534422914SDamien Le Moal {
116aa7f243fSDamien Le Moal return z->z_flags & ZONEFS_ZONE_CNV;
11734422914SDamien Le Moal }
11834422914SDamien Le Moal
zonefs_zone_is_seq(struct zonefs_zone * z)119aa7f243fSDamien Le Moal static inline bool zonefs_zone_is_seq(struct zonefs_zone *z)
12034422914SDamien Le Moal {
121aa7f243fSDamien Le Moal return !zonefs_zone_is_cnv(z);
122aa7f243fSDamien Le Moal }
123aa7f243fSDamien Le Moal
zonefs_inode_zone(struct inode * inode)124aa7f243fSDamien Le Moal static inline struct zonefs_zone *zonefs_inode_zone(struct inode *inode)
125aa7f243fSDamien Le Moal {
126aa7f243fSDamien Le Moal return inode->i_private;
12734422914SDamien Le Moal }
12834422914SDamien Le Moal
zonefs_inode_is_cnv(struct inode * inode)12934422914SDamien Le Moal static inline bool zonefs_inode_is_cnv(struct inode *inode)
13034422914SDamien Le Moal {
131aa7f243fSDamien Le Moal return zonefs_zone_is_cnv(zonefs_inode_zone(inode));
13234422914SDamien Le Moal }
13334422914SDamien Le Moal
zonefs_inode_is_seq(struct inode * inode)13434422914SDamien Le Moal static inline bool zonefs_inode_is_seq(struct inode *inode)
13534422914SDamien Le Moal {
136aa7f243fSDamien Le Moal return zonefs_zone_is_seq(zonefs_inode_zone(inode));
13734422914SDamien Le Moal }
13834422914SDamien Le Moal
1398dcc1a9dSDamien Le Moal /*
1408dcc1a9dSDamien Le Moal * On-disk super block (block 0).
1418dcc1a9dSDamien Le Moal */
1428dcc1a9dSDamien Le Moal #define ZONEFS_LABEL_LEN 64
1438dcc1a9dSDamien Le Moal #define ZONEFS_UUID_SIZE 16
1448dcc1a9dSDamien Le Moal #define ZONEFS_SUPER_SIZE 4096
1458dcc1a9dSDamien Le Moal
1468dcc1a9dSDamien Le Moal struct zonefs_super {
1478dcc1a9dSDamien Le Moal
1488dcc1a9dSDamien Le Moal /* Magic number */
1498dcc1a9dSDamien Le Moal __le32 s_magic;
1508dcc1a9dSDamien Le Moal
1518dcc1a9dSDamien Le Moal /* Checksum */
1528dcc1a9dSDamien Le Moal __le32 s_crc;
1538dcc1a9dSDamien Le Moal
1548dcc1a9dSDamien Le Moal /* Volume label */
1558dcc1a9dSDamien Le Moal char s_label[ZONEFS_LABEL_LEN];
1568dcc1a9dSDamien Le Moal
1578dcc1a9dSDamien Le Moal /* 128-bit uuid */
1588dcc1a9dSDamien Le Moal __u8 s_uuid[ZONEFS_UUID_SIZE];
1598dcc1a9dSDamien Le Moal
1608dcc1a9dSDamien Le Moal /* Features */
1618dcc1a9dSDamien Le Moal __le64 s_features;
1628dcc1a9dSDamien Le Moal
1638dcc1a9dSDamien Le Moal /* UID/GID to use for files */
1648dcc1a9dSDamien Le Moal __le32 s_uid;
1658dcc1a9dSDamien Le Moal __le32 s_gid;
1668dcc1a9dSDamien Le Moal
1678dcc1a9dSDamien Le Moal /* File permissions */
1688dcc1a9dSDamien Le Moal __le32 s_perm;
1698dcc1a9dSDamien Le Moal
1708dcc1a9dSDamien Le Moal /* Padding to ZONEFS_SUPER_SIZE bytes */
1718dcc1a9dSDamien Le Moal __u8 s_reserved[3988];
1728dcc1a9dSDamien Le Moal
1738dcc1a9dSDamien Le Moal } __packed;
1748dcc1a9dSDamien Le Moal
1758dcc1a9dSDamien Le Moal /*
1768dcc1a9dSDamien Le Moal * Feature flags: specified in the s_features field of the on-disk super
1778dcc1a9dSDamien Le Moal * block struct zonefs_super and in-memory in the s_feartures field of
1788dcc1a9dSDamien Le Moal * struct zonefs_sb_info.
1798dcc1a9dSDamien Le Moal */
1808dcc1a9dSDamien Le Moal enum zonefs_features {
1818dcc1a9dSDamien Le Moal /*
1828dcc1a9dSDamien Le Moal * Aggregate contiguous conventional zones into a single file.
1838dcc1a9dSDamien Le Moal */
1848dcc1a9dSDamien Le Moal ZONEFS_F_AGGRCNV = 1ULL << 0,
1858dcc1a9dSDamien Le Moal /*
1868dcc1a9dSDamien Le Moal * Use super block specified UID for files instead of default 0.
1878dcc1a9dSDamien Le Moal */
1888dcc1a9dSDamien Le Moal ZONEFS_F_UID = 1ULL << 1,
1898dcc1a9dSDamien Le Moal /*
1908dcc1a9dSDamien Le Moal * Use super block specified GID for files instead of default 0.
1918dcc1a9dSDamien Le Moal */
1928dcc1a9dSDamien Le Moal ZONEFS_F_GID = 1ULL << 2,
1938dcc1a9dSDamien Le Moal /*
1948dcc1a9dSDamien Le Moal * Use super block specified file permissions instead of default 640.
1958dcc1a9dSDamien Le Moal */
1968dcc1a9dSDamien Le Moal ZONEFS_F_PERM = 1ULL << 3,
1978dcc1a9dSDamien Le Moal };
1988dcc1a9dSDamien Le Moal
1998dcc1a9dSDamien Le Moal #define ZONEFS_F_DEFINED_FEATURES \
2008dcc1a9dSDamien Le Moal (ZONEFS_F_AGGRCNV | ZONEFS_F_UID | ZONEFS_F_GID | ZONEFS_F_PERM)
2018dcc1a9dSDamien Le Moal
2028dcc1a9dSDamien Le Moal /*
2038dcc1a9dSDamien Le Moal * Mount options for zone write pointer error handling.
2048dcc1a9dSDamien Le Moal */
2058dcc1a9dSDamien Le Moal #define ZONEFS_MNTOPT_ERRORS_RO (1 << 0) /* Make zone file readonly */
2068dcc1a9dSDamien Le Moal #define ZONEFS_MNTOPT_ERRORS_ZRO (1 << 1) /* Make zone file offline */
2078dcc1a9dSDamien Le Moal #define ZONEFS_MNTOPT_ERRORS_ZOL (1 << 2) /* Make zone file offline */
2088dcc1a9dSDamien Le Moal #define ZONEFS_MNTOPT_ERRORS_REPAIR (1 << 3) /* Remount read-only */
2098dcc1a9dSDamien Le Moal #define ZONEFS_MNTOPT_ERRORS_MASK \
2108dcc1a9dSDamien Le Moal (ZONEFS_MNTOPT_ERRORS_RO | ZONEFS_MNTOPT_ERRORS_ZRO | \
2118dcc1a9dSDamien Le Moal ZONEFS_MNTOPT_ERRORS_ZOL | ZONEFS_MNTOPT_ERRORS_REPAIR)
212b5c00e97SJohannes Thumshirn #define ZONEFS_MNTOPT_EXPLICIT_OPEN (1 << 4) /* Explicit open/close of zones on open/close */
2138dcc1a9dSDamien Le Moal
2148dcc1a9dSDamien Le Moal /*
2158dcc1a9dSDamien Le Moal * In-memory Super block information.
2168dcc1a9dSDamien Le Moal */
2178dcc1a9dSDamien Le Moal struct zonefs_sb_info {
2188dcc1a9dSDamien Le Moal
2198dcc1a9dSDamien Le Moal unsigned long s_mount_opts;
2208dcc1a9dSDamien Le Moal
2218dcc1a9dSDamien Le Moal spinlock_t s_lock;
2228dcc1a9dSDamien Le Moal
2238dcc1a9dSDamien Le Moal unsigned long long s_features;
2248dcc1a9dSDamien Le Moal kuid_t s_uid;
2258dcc1a9dSDamien Le Moal kgid_t s_gid;
2268dcc1a9dSDamien Le Moal umode_t s_perm;
2278dcc1a9dSDamien Le Moal uuid_t s_uuid;
2288dcc1a9dSDamien Le Moal unsigned int s_zone_sectors_shift;
2298dcc1a9dSDamien Le Moal
230aa7f243fSDamien Le Moal struct zonefs_zone_group s_zgroup[ZONEFS_ZTYPE_MAX];
2318dcc1a9dSDamien Le Moal
2328dcc1a9dSDamien Le Moal loff_t s_blocks;
2338dcc1a9dSDamien Le Moal loff_t s_used_blocks;
234b5c00e97SJohannes Thumshirn
2352b95a23cSDamien Le Moal unsigned int s_max_wro_seq_files;
2362b95a23cSDamien Le Moal atomic_t s_wro_seq_files;
2379277a6d4SDamien Le Moal
23887c9ce3fSDamien Le Moal unsigned int s_max_active_seq_files;
23987c9ce3fSDamien Le Moal atomic_t s_active_seq_files;
24087c9ce3fSDamien Le Moal
2419277a6d4SDamien Le Moal bool s_sysfs_registered;
2429277a6d4SDamien Le Moal struct kobject s_kobj;
2439277a6d4SDamien Le Moal struct completion s_kobj_unregister;
2448dcc1a9dSDamien Le Moal };
2458dcc1a9dSDamien Le Moal
ZONEFS_SB(struct super_block * sb)2468dcc1a9dSDamien Le Moal static inline struct zonefs_sb_info *ZONEFS_SB(struct super_block *sb)
2478dcc1a9dSDamien Le Moal {
2488dcc1a9dSDamien Le Moal return sb->s_fs_info;
2498dcc1a9dSDamien Le Moal }
2508dcc1a9dSDamien Le Moal
2518dcc1a9dSDamien Le Moal #define zonefs_info(sb, format, args...) \
2528dcc1a9dSDamien Le Moal pr_info("zonefs (%s): " format, sb->s_id, ## args)
2538dcc1a9dSDamien Le Moal #define zonefs_err(sb, format, args...) \
2548dcc1a9dSDamien Le Moal pr_err("zonefs (%s) ERROR: " format, sb->s_id, ## args)
2558dcc1a9dSDamien Le Moal #define zonefs_warn(sb, format, args...) \
2568dcc1a9dSDamien Le Moal pr_warn("zonefs (%s) WARNING: " format, sb->s_id, ## args)
2578dcc1a9dSDamien Le Moal
2584008e2a0SDamien Le Moal /* In super.c */
259aa7f243fSDamien Le Moal void zonefs_inode_account_active(struct inode *inode);
260aa7f243fSDamien Le Moal int zonefs_inode_zone_mgmt(struct inode *inode, enum req_op op);
2614008e2a0SDamien Le Moal void zonefs_i_size_write(struct inode *inode, loff_t isize);
2624008e2a0SDamien Le Moal void zonefs_update_stats(struct inode *inode, loff_t new_isize);
2634008e2a0SDamien Le Moal void __zonefs_io_error(struct inode *inode, bool write);
2644008e2a0SDamien Le Moal
zonefs_io_error(struct inode * inode,bool write)2654008e2a0SDamien Le Moal static inline void zonefs_io_error(struct inode *inode, bool write)
2664008e2a0SDamien Le Moal {
2674008e2a0SDamien Le Moal struct zonefs_inode_info *zi = ZONEFS_I(inode);
2684008e2a0SDamien Le Moal
2694008e2a0SDamien Le Moal mutex_lock(&zi->i_truncate_mutex);
2704008e2a0SDamien Le Moal __zonefs_io_error(inode, write);
2714008e2a0SDamien Le Moal mutex_unlock(&zi->i_truncate_mutex);
2724008e2a0SDamien Le Moal }
2734008e2a0SDamien Le Moal
274d207794aSDamien Le Moal /* In super.c */
275d207794aSDamien Le Moal extern const struct inode_operations zonefs_dir_inode_operations;
276d207794aSDamien Le Moal extern const struct file_operations zonefs_dir_operations;
277d207794aSDamien Le Moal
2784008e2a0SDamien Le Moal /* In file.c */
2794008e2a0SDamien Le Moal extern const struct address_space_operations zonefs_file_aops;
2804008e2a0SDamien Le Moal extern const struct file_operations zonefs_file_operations;
2814008e2a0SDamien Le Moal int zonefs_file_truncate(struct inode *inode, loff_t isize);
2824008e2a0SDamien Le Moal
2834008e2a0SDamien Le Moal /* In sysfs.c */
2849277a6d4SDamien Le Moal int zonefs_sysfs_register(struct super_block *sb);
2859277a6d4SDamien Le Moal void zonefs_sysfs_unregister(struct super_block *sb);
2869277a6d4SDamien Le Moal int zonefs_sysfs_init(void);
2879277a6d4SDamien Le Moal void zonefs_sysfs_exit(void);
2889277a6d4SDamien Le Moal
2898dcc1a9dSDamien Le Moal #endif
290