18dcc1a9dSDamien Le Moal // SPDX-License-Identifier: GPL-2.0
28dcc1a9dSDamien Le Moal /*
38dcc1a9dSDamien Le Moal * Simple file system for zoned block devices exposing zones as files.
48dcc1a9dSDamien Le Moal *
58dcc1a9dSDamien Le Moal * Copyright (C) 2019 Western Digital Corporation or its affiliates.
68dcc1a9dSDamien Le Moal */
78dcc1a9dSDamien Le Moal #include <linux/module.h>
83a6b2162SMatthew Wilcox (Oracle) #include <linux/pagemap.h>
98dcc1a9dSDamien Le Moal #include <linux/magic.h>
108dcc1a9dSDamien Le Moal #include <linux/iomap.h>
118dcc1a9dSDamien Le Moal #include <linux/init.h>
128dcc1a9dSDamien Le Moal #include <linux/slab.h>
138dcc1a9dSDamien Le Moal #include <linux/blkdev.h>
148dcc1a9dSDamien Le Moal #include <linux/statfs.h>
158dcc1a9dSDamien Le Moal #include <linux/writeback.h>
168dcc1a9dSDamien Le Moal #include <linux/quotaops.h>
178dcc1a9dSDamien Le Moal #include <linux/seq_file.h>
188dcc1a9dSDamien Le Moal #include <linux/parser.h>
198dcc1a9dSDamien Le Moal #include <linux/uio.h>
208dcc1a9dSDamien Le Moal #include <linux/mman.h>
218dcc1a9dSDamien Le Moal #include <linux/sched/mm.h>
228dcc1a9dSDamien Le Moal #include <linux/crc32.h>
2302ef12a6SJohannes Thumshirn #include <linux/task_io_accounting_ops.h>
248dcc1a9dSDamien Le Moal
258dcc1a9dSDamien Le Moal #include "zonefs.h"
268dcc1a9dSDamien Le Moal
2762ab1aadSJohannes Thumshirn #define CREATE_TRACE_POINTS
2862ab1aadSJohannes Thumshirn #include "trace.h"
2962ab1aadSJohannes Thumshirn
3087c9ce3fSDamien Le Moal /*
31aa7f243fSDamien Le Moal * Get the name of a zone group directory.
3287c9ce3fSDamien Le Moal */
zonefs_zgroup_name(enum zonefs_ztype ztype)33aa7f243fSDamien Le Moal static const char *zonefs_zgroup_name(enum zonefs_ztype ztype)
3487c9ce3fSDamien Le Moal {
35aa7f243fSDamien Le Moal switch (ztype) {
36aa7f243fSDamien Le Moal case ZONEFS_ZTYPE_CNV:
37aa7f243fSDamien Le Moal return "cnv";
38aa7f243fSDamien Le Moal case ZONEFS_ZTYPE_SEQ:
39aa7f243fSDamien Le Moal return "seq";
40aa7f243fSDamien Le Moal default:
41aa7f243fSDamien Le Moal WARN_ON_ONCE(1);
42aa7f243fSDamien Le Moal return "???";
43aa7f243fSDamien Le Moal }
44aa7f243fSDamien Le Moal }
4587c9ce3fSDamien Le Moal
46aa7f243fSDamien Le Moal /*
47aa7f243fSDamien Le Moal * Manage the active zone count.
48aa7f243fSDamien Le Moal */
zonefs_account_active(struct super_block * sb,struct zonefs_zone * z)49aa7f243fSDamien Le Moal static void zonefs_account_active(struct super_block *sb,
50aa7f243fSDamien Le Moal struct zonefs_zone *z)
51aa7f243fSDamien Le Moal {
52aa7f243fSDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
5387c9ce3fSDamien Le Moal
54aa7f243fSDamien Le Moal if (zonefs_zone_is_cnv(z))
5587c9ce3fSDamien Le Moal return;
5687c9ce3fSDamien Le Moal
5787c9ce3fSDamien Le Moal /*
58db58653cSDamien Le Moal * For zones that transitioned to the offline or readonly condition,
59db58653cSDamien Le Moal * we only need to clear the active state.
60db58653cSDamien Le Moal */
61aa7f243fSDamien Le Moal if (z->z_flags & (ZONEFS_ZONE_OFFLINE | ZONEFS_ZONE_READONLY))
62db58653cSDamien Le Moal goto out;
63db58653cSDamien Le Moal
64db58653cSDamien Le Moal /*
6587c9ce3fSDamien Le Moal * If the zone is active, that is, if it is explicitly open or
6687c9ce3fSDamien Le Moal * partially written, check if it was already accounted as active.
6787c9ce3fSDamien Le Moal */
68aa7f243fSDamien Le Moal if ((z->z_flags & ZONEFS_ZONE_OPEN) ||
69aa7f243fSDamien Le Moal (z->z_wpoffset > 0 && z->z_wpoffset < z->z_capacity)) {
70aa7f243fSDamien Le Moal if (!(z->z_flags & ZONEFS_ZONE_ACTIVE)) {
71aa7f243fSDamien Le Moal z->z_flags |= ZONEFS_ZONE_ACTIVE;
7287c9ce3fSDamien Le Moal atomic_inc(&sbi->s_active_seq_files);
7387c9ce3fSDamien Le Moal }
7487c9ce3fSDamien Le Moal return;
7587c9ce3fSDamien Le Moal }
7687c9ce3fSDamien Le Moal
77db58653cSDamien Le Moal out:
7887c9ce3fSDamien Le Moal /* The zone is not active. If it was, update the active count */
79aa7f243fSDamien Le Moal if (z->z_flags & ZONEFS_ZONE_ACTIVE) {
80aa7f243fSDamien Le Moal z->z_flags &= ~ZONEFS_ZONE_ACTIVE;
8187c9ce3fSDamien Le Moal atomic_dec(&sbi->s_active_seq_files);
8287c9ce3fSDamien Le Moal }
8387c9ce3fSDamien Le Moal }
8487c9ce3fSDamien Le Moal
85aa7f243fSDamien Le Moal /*
86aa7f243fSDamien Le Moal * Manage the active zone count. Called with zi->i_truncate_mutex held.
87aa7f243fSDamien Le Moal */
zonefs_inode_account_active(struct inode * inode)88aa7f243fSDamien Le Moal void zonefs_inode_account_active(struct inode *inode)
895498d5f9SJohannes Thumshirn {
90aa7f243fSDamien Le Moal lockdep_assert_held(&ZONEFS_I(inode)->i_truncate_mutex);
915498d5f9SJohannes Thumshirn
92aa7f243fSDamien Le Moal return zonefs_account_active(inode->i_sb, zonefs_inode_zone(inode));
93aa7f243fSDamien Le Moal }
94aa7f243fSDamien Le Moal
95aa7f243fSDamien Le Moal /*
96aa7f243fSDamien Le Moal * Execute a zone management operation.
97aa7f243fSDamien Le Moal */
zonefs_zone_mgmt(struct super_block * sb,struct zonefs_zone * z,enum req_op op)98aa7f243fSDamien Le Moal static int zonefs_zone_mgmt(struct super_block *sb,
99aa7f243fSDamien Le Moal struct zonefs_zone *z, enum req_op op)
100aa7f243fSDamien Le Moal {
101aa7f243fSDamien Le Moal int ret;
1025498d5f9SJohannes Thumshirn
1031da18a29SDamien Le Moal /*
1041da18a29SDamien Le Moal * With ZNS drives, closing an explicitly open zone that has not been
1051da18a29SDamien Le Moal * written will change the zone state to "closed", that is, the zone
1061da18a29SDamien Le Moal * will remain active. Since this can then cause failure of explicit
1071da18a29SDamien Le Moal * open operation on other zones if the drive active zone resources
1081da18a29SDamien Le Moal * are exceeded, make sure that the zone does not remain active by
1091da18a29SDamien Le Moal * resetting it.
1101da18a29SDamien Le Moal */
111aa7f243fSDamien Le Moal if (op == REQ_OP_ZONE_CLOSE && !z->z_wpoffset)
1121da18a29SDamien Le Moal op = REQ_OP_ZONE_RESET;
1131da18a29SDamien Le Moal
114aa7f243fSDamien Le Moal trace_zonefs_zone_mgmt(sb, z, op);
115aa7f243fSDamien Le Moal ret = blkdev_zone_mgmt(sb->s_bdev, op, z->z_sector,
116aa7f243fSDamien Le Moal z->z_size >> SECTOR_SHIFT, GFP_NOFS);
1175498d5f9SJohannes Thumshirn if (ret) {
118aa7f243fSDamien Le Moal zonefs_err(sb,
1195498d5f9SJohannes Thumshirn "Zone management operation %s at %llu failed %d\n",
120aa7f243fSDamien Le Moal blk_op_str(op), z->z_sector, ret);
1215498d5f9SJohannes Thumshirn return ret;
1225498d5f9SJohannes Thumshirn }
1235498d5f9SJohannes Thumshirn
1245498d5f9SJohannes Thumshirn return 0;
1255498d5f9SJohannes Thumshirn }
1265498d5f9SJohannes Thumshirn
zonefs_inode_zone_mgmt(struct inode * inode,enum req_op op)127aa7f243fSDamien Le Moal int zonefs_inode_zone_mgmt(struct inode *inode, enum req_op op)
128b5c00e97SJohannes Thumshirn {
129aa7f243fSDamien Le Moal lockdep_assert_held(&ZONEFS_I(inode)->i_truncate_mutex);
130aa7f243fSDamien Le Moal
131aa7f243fSDamien Le Moal return zonefs_zone_mgmt(inode->i_sb, zonefs_inode_zone(inode), op);
132aa7f243fSDamien Le Moal }
133aa7f243fSDamien Le Moal
zonefs_i_size_write(struct inode * inode,loff_t isize)1344008e2a0SDamien Le Moal void zonefs_i_size_write(struct inode *inode, loff_t isize)
135b5c00e97SJohannes Thumshirn {
136aa7f243fSDamien Le Moal struct zonefs_zone *z = zonefs_inode_zone(inode);
137b5c00e97SJohannes Thumshirn
138b5c00e97SJohannes Thumshirn i_size_write(inode, isize);
139aa7f243fSDamien Le Moal
140b5c00e97SJohannes Thumshirn /*
141b5c00e97SJohannes Thumshirn * A full zone is no longer open/active and does not need
142b5c00e97SJohannes Thumshirn * explicit closing.
143b5c00e97SJohannes Thumshirn */
144aa7f243fSDamien Le Moal if (isize >= z->z_capacity) {
14587c9ce3fSDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
14687c9ce3fSDamien Le Moal
147aa7f243fSDamien Le Moal if (z->z_flags & ZONEFS_ZONE_ACTIVE)
14887c9ce3fSDamien Le Moal atomic_dec(&sbi->s_active_seq_files);
149aa7f243fSDamien Le Moal z->z_flags &= ~(ZONEFS_ZONE_OPEN | ZONEFS_ZONE_ACTIVE);
15087c9ce3fSDamien Le Moal }
151b5c00e97SJohannes Thumshirn }
152b5c00e97SJohannes Thumshirn
zonefs_update_stats(struct inode * inode,loff_t new_isize)1534008e2a0SDamien Le Moal void zonefs_update_stats(struct inode *inode, loff_t new_isize)
1548dcc1a9dSDamien Le Moal {
1558dcc1a9dSDamien Le Moal struct super_block *sb = inode->i_sb;
1568dcc1a9dSDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1578dcc1a9dSDamien Le Moal loff_t old_isize = i_size_read(inode);
1588dcc1a9dSDamien Le Moal loff_t nr_blocks;
1598dcc1a9dSDamien Le Moal
1608dcc1a9dSDamien Le Moal if (new_isize == old_isize)
1618dcc1a9dSDamien Le Moal return;
1628dcc1a9dSDamien Le Moal
1638dcc1a9dSDamien Le Moal spin_lock(&sbi->s_lock);
1648dcc1a9dSDamien Le Moal
1658dcc1a9dSDamien Le Moal /*
1668dcc1a9dSDamien Le Moal * This may be called for an update after an IO error.
1678dcc1a9dSDamien Le Moal * So beware of the values seen.
1688dcc1a9dSDamien Le Moal */
1698dcc1a9dSDamien Le Moal if (new_isize < old_isize) {
1708dcc1a9dSDamien Le Moal nr_blocks = (old_isize - new_isize) >> sb->s_blocksize_bits;
1718dcc1a9dSDamien Le Moal if (sbi->s_used_blocks > nr_blocks)
1728dcc1a9dSDamien Le Moal sbi->s_used_blocks -= nr_blocks;
1738dcc1a9dSDamien Le Moal else
1748dcc1a9dSDamien Le Moal sbi->s_used_blocks = 0;
1758dcc1a9dSDamien Le Moal } else {
1768dcc1a9dSDamien Le Moal sbi->s_used_blocks +=
1778dcc1a9dSDamien Le Moal (new_isize - old_isize) >> sb->s_blocksize_bits;
1788dcc1a9dSDamien Le Moal if (sbi->s_used_blocks > sbi->s_blocks)
1798dcc1a9dSDamien Le Moal sbi->s_used_blocks = sbi->s_blocks;
1808dcc1a9dSDamien Le Moal }
1818dcc1a9dSDamien Le Moal
1828dcc1a9dSDamien Le Moal spin_unlock(&sbi->s_lock);
1838dcc1a9dSDamien Le Moal }
1848dcc1a9dSDamien Le Moal
1858dcc1a9dSDamien Le Moal /*
186aa7f243fSDamien Le Moal * Check a zone condition. Return the amount of written (and still readable)
187aa7f243fSDamien Le Moal * data in the zone.
1888dcc1a9dSDamien Le Moal */
zonefs_check_zone_condition(struct super_block * sb,struct zonefs_zone * z,struct blk_zone * zone)189aa7f243fSDamien Le Moal static loff_t zonefs_check_zone_condition(struct super_block *sb,
190aa7f243fSDamien Le Moal struct zonefs_zone *z,
19146a9c526SDamien Le Moal struct blk_zone *zone)
1928dcc1a9dSDamien Le Moal {
1938dcc1a9dSDamien Le Moal switch (zone->cond) {
1948dcc1a9dSDamien Le Moal case BLK_ZONE_COND_OFFLINE:
195aa7f243fSDamien Le Moal zonefs_warn(sb, "Zone %llu: offline zone\n",
196aa7f243fSDamien Le Moal z->z_sector);
197aa7f243fSDamien Le Moal z->z_flags |= ZONEFS_ZONE_OFFLINE;
1988dcc1a9dSDamien Le Moal return 0;
1998dcc1a9dSDamien Le Moal case BLK_ZONE_COND_READONLY:
200ccf4ad7dSDamien Le Moal /*
20146a9c526SDamien Le Moal * The write pointer of read-only zones is invalid, so we cannot
20246a9c526SDamien Le Moal * determine the zone wpoffset (inode size). We thus keep the
20346a9c526SDamien Le Moal * zone wpoffset as is, which leads to an empty file
20446a9c526SDamien Le Moal * (wpoffset == 0) on mount. For a runtime error, this keeps
20546a9c526SDamien Le Moal * the inode size as it was when last updated so that the user
20646a9c526SDamien Le Moal * can recover data.
207ccf4ad7dSDamien Le Moal */
208aa7f243fSDamien Le Moal zonefs_warn(sb, "Zone %llu: read-only zone\n",
209aa7f243fSDamien Le Moal z->z_sector);
210aa7f243fSDamien Le Moal z->z_flags |= ZONEFS_ZONE_READONLY;
211aa7f243fSDamien Le Moal if (zonefs_zone_is_cnv(z))
212aa7f243fSDamien Le Moal return z->z_capacity;
213aa7f243fSDamien Le Moal return z->z_wpoffset;
214059c0103SShin'ichiro Kawasaki case BLK_ZONE_COND_FULL:
215059c0103SShin'ichiro Kawasaki /* The write pointer of full zones is invalid. */
216aa7f243fSDamien Le Moal return z->z_capacity;
2178dcc1a9dSDamien Le Moal default:
218aa7f243fSDamien Le Moal if (zonefs_zone_is_cnv(z))
219aa7f243fSDamien Le Moal return z->z_capacity;
2208dcc1a9dSDamien Le Moal return (zone->wp - zone->start) << SECTOR_SHIFT;
2218dcc1a9dSDamien Le Moal }
2228dcc1a9dSDamien Le Moal }
2238dcc1a9dSDamien Le Moal
22446a9c526SDamien Le Moal /*
22546a9c526SDamien Le Moal * Check a zone condition and adjust its inode access permissions for
22646a9c526SDamien Le Moal * offline and readonly zones.
22746a9c526SDamien Le Moal */
zonefs_inode_update_mode(struct inode * inode)22846a9c526SDamien Le Moal static void zonefs_inode_update_mode(struct inode *inode)
22946a9c526SDamien Le Moal {
230aa7f243fSDamien Le Moal struct zonefs_zone *z = zonefs_inode_zone(inode);
23146a9c526SDamien Le Moal
232aa7f243fSDamien Le Moal if (z->z_flags & ZONEFS_ZONE_OFFLINE) {
23346a9c526SDamien Le Moal /* Offline zones cannot be read nor written */
23446a9c526SDamien Le Moal inode->i_flags |= S_IMMUTABLE;
23546a9c526SDamien Le Moal inode->i_mode &= ~0777;
236aa7f243fSDamien Le Moal } else if (z->z_flags & ZONEFS_ZONE_READONLY) {
23746a9c526SDamien Le Moal /* Readonly zones cannot be written */
23846a9c526SDamien Le Moal inode->i_flags |= S_IMMUTABLE;
239aa7f243fSDamien Le Moal if (z->z_flags & ZONEFS_ZONE_INIT_MODE)
24046a9c526SDamien Le Moal inode->i_mode &= ~0777;
24146a9c526SDamien Le Moal else
24246a9c526SDamien Le Moal inode->i_mode &= ~0222;
24346a9c526SDamien Le Moal }
24446a9c526SDamien Le Moal
245aa7f243fSDamien Le Moal z->z_flags &= ~ZONEFS_ZONE_INIT_MODE;
246d207794aSDamien Le Moal z->z_mode = inode->i_mode;
24746a9c526SDamien Le Moal }
24846a9c526SDamien Le Moal
zonefs_io_error_cb(struct blk_zone * zone,unsigned int idx,void * data)2498dcc1a9dSDamien Le Moal static int zonefs_io_error_cb(struct blk_zone *zone, unsigned int idx,
2508dcc1a9dSDamien Le Moal void *data)
2518dcc1a9dSDamien Le Moal {
252*6d5eae9aSDamien Le Moal struct blk_zone *z = data;
253*6d5eae9aSDamien Le Moal
254*6d5eae9aSDamien Le Moal *z = *zone;
255*6d5eae9aSDamien Le Moal return 0;
256*6d5eae9aSDamien Le Moal }
257*6d5eae9aSDamien Le Moal
zonefs_handle_io_error(struct inode * inode,struct blk_zone * zone,bool write)258*6d5eae9aSDamien Le Moal static void zonefs_handle_io_error(struct inode *inode, struct blk_zone *zone,
259*6d5eae9aSDamien Le Moal bool write)
260*6d5eae9aSDamien Le Moal {
261aa7f243fSDamien Le Moal struct zonefs_zone *z = zonefs_inode_zone(inode);
2628dcc1a9dSDamien Le Moal struct super_block *sb = inode->i_sb;
2638dcc1a9dSDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
2648dcc1a9dSDamien Le Moal loff_t isize, data_size;
2658dcc1a9dSDamien Le Moal
2668dcc1a9dSDamien Le Moal /*
2678dcc1a9dSDamien Le Moal * Check the zone condition: if the zone is not "bad" (offline or
2688dcc1a9dSDamien Le Moal * read-only), read errors are simply signaled to the IO issuer as long
2698dcc1a9dSDamien Le Moal * as there is no inconsistency between the inode size and the amount of
2708dcc1a9dSDamien Le Moal * data writen in the zone (data_size).
2718dcc1a9dSDamien Le Moal */
272aa7f243fSDamien Le Moal data_size = zonefs_check_zone_condition(sb, z, zone);
2738dcc1a9dSDamien Le Moal isize = i_size_read(inode);
274aa7f243fSDamien Le Moal if (!(z->z_flags & (ZONEFS_ZONE_READONLY | ZONEFS_ZONE_OFFLINE)) &&
275*6d5eae9aSDamien Le Moal !write && isize == data_size)
276*6d5eae9aSDamien Le Moal return;
2778dcc1a9dSDamien Le Moal
2788dcc1a9dSDamien Le Moal /*
2798dcc1a9dSDamien Le Moal * At this point, we detected either a bad zone or an inconsistency
2808dcc1a9dSDamien Le Moal * between the inode size and the amount of data written in the zone.
2818dcc1a9dSDamien Le Moal * For the latter case, the cause may be a write IO error or an external
2828dcc1a9dSDamien Le Moal * action on the device. Two error patterns exist:
2838dcc1a9dSDamien Le Moal * 1) The inode size is lower than the amount of data in the zone:
2848dcc1a9dSDamien Le Moal * a write operation partially failed and data was writen at the end
2858dcc1a9dSDamien Le Moal * of the file. This can happen in the case of a large direct IO
2868dcc1a9dSDamien Le Moal * needing several BIOs and/or write requests to be processed.
2878dcc1a9dSDamien Le Moal * 2) The inode size is larger than the amount of data in the zone:
2888dcc1a9dSDamien Le Moal * this can happen with a deferred write error with the use of the
2898dcc1a9dSDamien Le Moal * device side write cache after getting successful write IO
2908dcc1a9dSDamien Le Moal * completions. Other possibilities are (a) an external corruption,
2918dcc1a9dSDamien Le Moal * e.g. an application reset the zone directly, or (b) the device
2928dcc1a9dSDamien Le Moal * has a serious problem (e.g. firmware bug).
2938dcc1a9dSDamien Le Moal *
2948dcc1a9dSDamien Le Moal * In all cases, warn about inode size inconsistency and handle the
2958dcc1a9dSDamien Le Moal * IO error according to the zone condition and to the mount options.
2968dcc1a9dSDamien Le Moal */
297*6d5eae9aSDamien Le Moal if (isize != data_size)
298aa7f243fSDamien Le Moal zonefs_warn(sb,
299aa7f243fSDamien Le Moal "inode %lu: invalid size %lld (should be %lld)\n",
3008dcc1a9dSDamien Le Moal inode->i_ino, isize, data_size);
3018dcc1a9dSDamien Le Moal
3028dcc1a9dSDamien Le Moal /*
3038dcc1a9dSDamien Le Moal * First handle bad zones signaled by hardware. The mount options
3048dcc1a9dSDamien Le Moal * errors=zone-ro and errors=zone-offline result in changing the
3058dcc1a9dSDamien Le Moal * zone condition to read-only and offline respectively, as if the
3068dcc1a9dSDamien Le Moal * condition was signaled by the hardware.
3078dcc1a9dSDamien Le Moal */
308aa7f243fSDamien Le Moal if ((z->z_flags & ZONEFS_ZONE_OFFLINE) ||
30946a9c526SDamien Le Moal (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL)) {
3108dcc1a9dSDamien Le Moal zonefs_warn(sb, "inode %lu: read/write access disabled\n",
3118dcc1a9dSDamien Le Moal inode->i_ino);
312aa7f243fSDamien Le Moal if (!(z->z_flags & ZONEFS_ZONE_OFFLINE))
313aa7f243fSDamien Le Moal z->z_flags |= ZONEFS_ZONE_OFFLINE;
31446a9c526SDamien Le Moal zonefs_inode_update_mode(inode);
31546a9c526SDamien Le Moal data_size = 0;
316aa7f243fSDamien Le Moal } else if ((z->z_flags & ZONEFS_ZONE_READONLY) ||
31746a9c526SDamien Le Moal (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO)) {
3188dcc1a9dSDamien Le Moal zonefs_warn(sb, "inode %lu: write access disabled\n",
3198dcc1a9dSDamien Le Moal inode->i_ino);
320aa7f243fSDamien Le Moal if (!(z->z_flags & ZONEFS_ZONE_READONLY))
321aa7f243fSDamien Le Moal z->z_flags |= ZONEFS_ZONE_READONLY;
32246a9c526SDamien Le Moal zonefs_inode_update_mode(inode);
32346a9c526SDamien Le Moal data_size = isize;
324a608da3bSDamien Le Moal } else if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO &&
325a608da3bSDamien Le Moal data_size > isize) {
326a608da3bSDamien Le Moal /* Do not expose garbage data */
327a608da3bSDamien Le Moal data_size = isize;
3288dcc1a9dSDamien Le Moal }
3298dcc1a9dSDamien Le Moal
3308dcc1a9dSDamien Le Moal /*
331b5c00e97SJohannes Thumshirn * If the filesystem is mounted with the explicit-open mount option, we
332b5c00e97SJohannes Thumshirn * need to clear the ZONEFS_ZONE_OPEN flag if the zone transitioned to
333b5c00e97SJohannes Thumshirn * the read-only or offline condition, to avoid attempting an explicit
334b5c00e97SJohannes Thumshirn * close of the zone when the inode file is closed.
335b5c00e97SJohannes Thumshirn */
336b5c00e97SJohannes Thumshirn if ((sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) &&
337aa7f243fSDamien Le Moal (z->z_flags & (ZONEFS_ZONE_READONLY | ZONEFS_ZONE_OFFLINE)))
338aa7f243fSDamien Le Moal z->z_flags &= ~ZONEFS_ZONE_OPEN;
339b5c00e97SJohannes Thumshirn
340b5c00e97SJohannes Thumshirn /*
3418dcc1a9dSDamien Le Moal * If error=remount-ro was specified, any error result in remounting
3428dcc1a9dSDamien Le Moal * the volume as read-only.
3438dcc1a9dSDamien Le Moal */
3448dcc1a9dSDamien Le Moal if ((sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO) && !sb_rdonly(sb)) {
3458dcc1a9dSDamien Le Moal zonefs_warn(sb, "remounting filesystem read-only\n");
3468dcc1a9dSDamien Le Moal sb->s_flags |= SB_RDONLY;
3478dcc1a9dSDamien Le Moal }
3488dcc1a9dSDamien Le Moal
3498dcc1a9dSDamien Le Moal /*
3508dcc1a9dSDamien Le Moal * Update block usage stats and the inode size to prevent access to
3518dcc1a9dSDamien Le Moal * invalid data.
3528dcc1a9dSDamien Le Moal */
3538dcc1a9dSDamien Le Moal zonefs_update_stats(inode, data_size);
354b5c00e97SJohannes Thumshirn zonefs_i_size_write(inode, data_size);
355aa7f243fSDamien Le Moal z->z_wpoffset = data_size;
356aa7f243fSDamien Le Moal zonefs_inode_account_active(inode);
3578dcc1a9dSDamien Le Moal }
3588dcc1a9dSDamien Le Moal
3598dcc1a9dSDamien Le Moal /*
3608dcc1a9dSDamien Le Moal * When an file IO error occurs, check the file zone to see if there is a change
3618dcc1a9dSDamien Le Moal * in the zone condition (e.g. offline or read-only). For a failed write to a
3628dcc1a9dSDamien Le Moal * sequential zone, the zone write pointer position must also be checked to
3638dcc1a9dSDamien Le Moal * eventually correct the file size and zonefs inode write pointer offset
3648dcc1a9dSDamien Le Moal * (which can be out of sync with the drive due to partial write failures).
3658dcc1a9dSDamien Le Moal */
__zonefs_io_error(struct inode * inode,bool write)3664008e2a0SDamien Le Moal void __zonefs_io_error(struct inode *inode, bool write)
3678dcc1a9dSDamien Le Moal {
368aa7f243fSDamien Le Moal struct zonefs_zone *z = zonefs_inode_zone(inode);
3698dcc1a9dSDamien Le Moal struct super_block *sb = inode->i_sb;
3708dcc1a9dSDamien Le Moal unsigned int noio_flag;
371*6d5eae9aSDamien Le Moal struct blk_zone zone;
3728dcc1a9dSDamien Le Moal int ret;
3738dcc1a9dSDamien Le Moal
3748dcc1a9dSDamien Le Moal /*
375*6d5eae9aSDamien Le Moal * Conventional zone have no write pointer and cannot become read-only
376*6d5eae9aSDamien Le Moal * or offline. So simply fake a report for a single or aggregated zone
377*6d5eae9aSDamien Le Moal * and let zonefs_handle_io_error() correct the zone inode information
378*6d5eae9aSDamien Le Moal * according to the mount options.
3797dd12d65SDamien Le Moal */
380*6d5eae9aSDamien Le Moal if (!zonefs_zone_is_seq(z)) {
381*6d5eae9aSDamien Le Moal zone.start = z->z_sector;
382*6d5eae9aSDamien Le Moal zone.len = z->z_size >> SECTOR_SHIFT;
383*6d5eae9aSDamien Le Moal zone.wp = zone.start + zone.len;
384*6d5eae9aSDamien Le Moal zone.type = BLK_ZONE_TYPE_CONVENTIONAL;
385*6d5eae9aSDamien Le Moal zone.cond = BLK_ZONE_COND_NOT_WP;
386*6d5eae9aSDamien Le Moal zone.capacity = zone.len;
387*6d5eae9aSDamien Le Moal goto handle_io_error;
388*6d5eae9aSDamien Le Moal }
3897dd12d65SDamien Le Moal
3907dd12d65SDamien Le Moal /*
3918dcc1a9dSDamien Le Moal * Memory allocations in blkdev_report_zones() can trigger a memory
3928dcc1a9dSDamien Le Moal * reclaim which may in turn cause a recursion into zonefs as well as
3938dcc1a9dSDamien Le Moal * struct request allocations for the same device. The former case may
3948dcc1a9dSDamien Le Moal * end up in a deadlock on the inode truncate mutex, while the latter
3958dcc1a9dSDamien Le Moal * may prevent IO forward progress. Executing the report zones under
3968dcc1a9dSDamien Le Moal * the GFP_NOIO context avoids both problems.
3978dcc1a9dSDamien Le Moal */
3988dcc1a9dSDamien Le Moal noio_flag = memalloc_noio_save();
399*6d5eae9aSDamien Le Moal ret = blkdev_report_zones(sb->s_bdev, z->z_sector, 1,
400*6d5eae9aSDamien Le Moal zonefs_io_error_cb, &zone);
401*6d5eae9aSDamien Le Moal memalloc_noio_restore(noio_flag);
402*6d5eae9aSDamien Le Moal
403*6d5eae9aSDamien Le Moal if (ret != 1) {
4048dcc1a9dSDamien Le Moal zonefs_err(sb, "Get inode %lu zone information failed %d\n",
4058dcc1a9dSDamien Le Moal inode->i_ino, ret);
406*6d5eae9aSDamien Le Moal zonefs_warn(sb, "remounting filesystem read-only\n");
407*6d5eae9aSDamien Le Moal sb->s_flags |= SB_RDONLY;
408*6d5eae9aSDamien Le Moal return;
409*6d5eae9aSDamien Le Moal }
410*6d5eae9aSDamien Le Moal
411*6d5eae9aSDamien Le Moal handle_io_error:
412*6d5eae9aSDamien Le Moal zonefs_handle_io_error(inode, &zone, write);
41348d546a8SJohannes Thumshirn }
4148dcc1a9dSDamien Le Moal
4158dcc1a9dSDamien Le Moal static struct kmem_cache *zonefs_inode_cachep;
4168dcc1a9dSDamien Le Moal
zonefs_alloc_inode(struct super_block * sb)4178dcc1a9dSDamien Le Moal static struct inode *zonefs_alloc_inode(struct super_block *sb)
4188dcc1a9dSDamien Le Moal {
4198dcc1a9dSDamien Le Moal struct zonefs_inode_info *zi;
4208dcc1a9dSDamien Le Moal
421fd60b288SMuchun Song zi = alloc_inode_sb(sb, zonefs_inode_cachep, GFP_KERNEL);
4228dcc1a9dSDamien Le Moal if (!zi)
4238dcc1a9dSDamien Le Moal return NULL;
4248dcc1a9dSDamien Le Moal
4258dcc1a9dSDamien Le Moal inode_init_once(&zi->i_vnode);
4268dcc1a9dSDamien Le Moal mutex_init(&zi->i_truncate_mutex);
427b5c00e97SJohannes Thumshirn zi->i_wr_refcnt = 0;
4288dcc1a9dSDamien Le Moal
4298dcc1a9dSDamien Le Moal return &zi->i_vnode;
4308dcc1a9dSDamien Le Moal }
4318dcc1a9dSDamien Le Moal
zonefs_free_inode(struct inode * inode)4328dcc1a9dSDamien Le Moal static void zonefs_free_inode(struct inode *inode)
4338dcc1a9dSDamien Le Moal {
4348dcc1a9dSDamien Le Moal kmem_cache_free(zonefs_inode_cachep, ZONEFS_I(inode));
4358dcc1a9dSDamien Le Moal }
4368dcc1a9dSDamien Le Moal
4378dcc1a9dSDamien Le Moal /*
4388dcc1a9dSDamien Le Moal * File system stat.
4398dcc1a9dSDamien Le Moal */
zonefs_statfs(struct dentry * dentry,struct kstatfs * buf)4408dcc1a9dSDamien Le Moal static int zonefs_statfs(struct dentry *dentry, struct kstatfs *buf)
4418dcc1a9dSDamien Le Moal {
4428dcc1a9dSDamien Le Moal struct super_block *sb = dentry->d_sb;
4438dcc1a9dSDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
4448dcc1a9dSDamien Le Moal enum zonefs_ztype t;
4458dcc1a9dSDamien Le Moal
4468dcc1a9dSDamien Le Moal buf->f_type = ZONEFS_MAGIC;
4478dcc1a9dSDamien Le Moal buf->f_bsize = sb->s_blocksize;
4488dcc1a9dSDamien Le Moal buf->f_namelen = ZONEFS_NAME_MAX;
4498dcc1a9dSDamien Le Moal
4508dcc1a9dSDamien Le Moal spin_lock(&sbi->s_lock);
4518dcc1a9dSDamien Le Moal
4528dcc1a9dSDamien Le Moal buf->f_blocks = sbi->s_blocks;
4538dcc1a9dSDamien Le Moal if (WARN_ON(sbi->s_used_blocks > sbi->s_blocks))
4548dcc1a9dSDamien Le Moal buf->f_bfree = 0;
4558dcc1a9dSDamien Le Moal else
4568dcc1a9dSDamien Le Moal buf->f_bfree = buf->f_blocks - sbi->s_used_blocks;
4578dcc1a9dSDamien Le Moal buf->f_bavail = buf->f_bfree;
4588dcc1a9dSDamien Le Moal
4598dcc1a9dSDamien Le Moal for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) {
460aa7f243fSDamien Le Moal if (sbi->s_zgroup[t].g_nr_zones)
461aa7f243fSDamien Le Moal buf->f_files += sbi->s_zgroup[t].g_nr_zones + 1;
4628dcc1a9dSDamien Le Moal }
4638dcc1a9dSDamien Le Moal buf->f_ffree = 0;
4648dcc1a9dSDamien Le Moal
4658dcc1a9dSDamien Le Moal spin_unlock(&sbi->s_lock);
4668dcc1a9dSDamien Le Moal
4679591c3a3SAmir Goldstein buf->f_fsid = uuid_to_fsid(sbi->s_uuid.b);
4688dcc1a9dSDamien Le Moal
4698dcc1a9dSDamien Le Moal return 0;
4708dcc1a9dSDamien Le Moal }
4718dcc1a9dSDamien Le Moal
4728dcc1a9dSDamien Le Moal enum {
4738dcc1a9dSDamien Le Moal Opt_errors_ro, Opt_errors_zro, Opt_errors_zol, Opt_errors_repair,
474b5c00e97SJohannes Thumshirn Opt_explicit_open, Opt_err,
4758dcc1a9dSDamien Le Moal };
4768dcc1a9dSDamien Le Moal
4778dcc1a9dSDamien Le Moal static const match_table_t tokens = {
4788dcc1a9dSDamien Le Moal { Opt_errors_ro, "errors=remount-ro"},
4798dcc1a9dSDamien Le Moal { Opt_errors_zro, "errors=zone-ro"},
4808dcc1a9dSDamien Le Moal { Opt_errors_zol, "errors=zone-offline"},
4818dcc1a9dSDamien Le Moal { Opt_errors_repair, "errors=repair"},
482b5c00e97SJohannes Thumshirn { Opt_explicit_open, "explicit-open" },
4838dcc1a9dSDamien Le Moal { Opt_err, NULL}
4848dcc1a9dSDamien Le Moal };
4858dcc1a9dSDamien Le Moal
zonefs_parse_options(struct super_block * sb,char * options)4868dcc1a9dSDamien Le Moal static int zonefs_parse_options(struct super_block *sb, char *options)
4878dcc1a9dSDamien Le Moal {
4888dcc1a9dSDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
4898dcc1a9dSDamien Le Moal substring_t args[MAX_OPT_ARGS];
4908dcc1a9dSDamien Le Moal char *p;
4918dcc1a9dSDamien Le Moal
4928dcc1a9dSDamien Le Moal if (!options)
4938dcc1a9dSDamien Le Moal return 0;
4948dcc1a9dSDamien Le Moal
4958dcc1a9dSDamien Le Moal while ((p = strsep(&options, ",")) != NULL) {
4968dcc1a9dSDamien Le Moal int token;
4978dcc1a9dSDamien Le Moal
4988dcc1a9dSDamien Le Moal if (!*p)
4998dcc1a9dSDamien Le Moal continue;
5008dcc1a9dSDamien Le Moal
5018dcc1a9dSDamien Le Moal token = match_token(p, tokens, args);
5028dcc1a9dSDamien Le Moal switch (token) {
5038dcc1a9dSDamien Le Moal case Opt_errors_ro:
5048dcc1a9dSDamien Le Moal sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
5058dcc1a9dSDamien Le Moal sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_RO;
5068dcc1a9dSDamien Le Moal break;
5078dcc1a9dSDamien Le Moal case Opt_errors_zro:
5088dcc1a9dSDamien Le Moal sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
5098dcc1a9dSDamien Le Moal sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZRO;
5108dcc1a9dSDamien Le Moal break;
5118dcc1a9dSDamien Le Moal case Opt_errors_zol:
5128dcc1a9dSDamien Le Moal sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
5138dcc1a9dSDamien Le Moal sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZOL;
5148dcc1a9dSDamien Le Moal break;
5158dcc1a9dSDamien Le Moal case Opt_errors_repair:
5168dcc1a9dSDamien Le Moal sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
5178dcc1a9dSDamien Le Moal sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_REPAIR;
5188dcc1a9dSDamien Le Moal break;
519b5c00e97SJohannes Thumshirn case Opt_explicit_open:
520b5c00e97SJohannes Thumshirn sbi->s_mount_opts |= ZONEFS_MNTOPT_EXPLICIT_OPEN;
521b5c00e97SJohannes Thumshirn break;
5228dcc1a9dSDamien Le Moal default:
5238dcc1a9dSDamien Le Moal return -EINVAL;
5248dcc1a9dSDamien Le Moal }
5258dcc1a9dSDamien Le Moal }
5268dcc1a9dSDamien Le Moal
5278dcc1a9dSDamien Le Moal return 0;
5288dcc1a9dSDamien Le Moal }
5298dcc1a9dSDamien Le Moal
zonefs_show_options(struct seq_file * seq,struct dentry * root)5308dcc1a9dSDamien Le Moal static int zonefs_show_options(struct seq_file *seq, struct dentry *root)
5318dcc1a9dSDamien Le Moal {
5328dcc1a9dSDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(root->d_sb);
5338dcc1a9dSDamien Le Moal
5348dcc1a9dSDamien Le Moal if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO)
5358dcc1a9dSDamien Le Moal seq_puts(seq, ",errors=remount-ro");
5368dcc1a9dSDamien Le Moal if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO)
5378dcc1a9dSDamien Le Moal seq_puts(seq, ",errors=zone-ro");
5388dcc1a9dSDamien Le Moal if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL)
5398dcc1a9dSDamien Le Moal seq_puts(seq, ",errors=zone-offline");
5408dcc1a9dSDamien Le Moal if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_REPAIR)
5418dcc1a9dSDamien Le Moal seq_puts(seq, ",errors=repair");
5428dcc1a9dSDamien Le Moal
5438dcc1a9dSDamien Le Moal return 0;
5448dcc1a9dSDamien Le Moal }
5458dcc1a9dSDamien Le Moal
zonefs_remount(struct super_block * sb,int * flags,char * data)5468dcc1a9dSDamien Le Moal static int zonefs_remount(struct super_block *sb, int *flags, char *data)
5478dcc1a9dSDamien Le Moal {
5488dcc1a9dSDamien Le Moal sync_filesystem(sb);
5498dcc1a9dSDamien Le Moal
5508dcc1a9dSDamien Le Moal return zonefs_parse_options(sb, data);
5518dcc1a9dSDamien Le Moal }
5528dcc1a9dSDamien Le Moal
zonefs_inode_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * iattr)553232dd599SLinus Torvalds static int zonefs_inode_setattr(struct mnt_idmap *idmap,
5544008e2a0SDamien Le Moal struct dentry *dentry, struct iattr *iattr)
5554008e2a0SDamien Le Moal {
5564008e2a0SDamien Le Moal struct inode *inode = d_inode(dentry);
5574008e2a0SDamien Le Moal int ret;
5588dcc1a9dSDamien Le Moal
5594008e2a0SDamien Le Moal if (unlikely(IS_IMMUTABLE(inode)))
5604008e2a0SDamien Le Moal return -EPERM;
5614008e2a0SDamien Le Moal
562232dd599SLinus Torvalds ret = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
5634008e2a0SDamien Le Moal if (ret)
5644008e2a0SDamien Le Moal return ret;
5654008e2a0SDamien Le Moal
5664008e2a0SDamien Le Moal /*
5674008e2a0SDamien Le Moal * Since files and directories cannot be created nor deleted, do not
5684008e2a0SDamien Le Moal * allow setting any write attributes on the sub-directories grouping
5694008e2a0SDamien Le Moal * files by zone type.
5704008e2a0SDamien Le Moal */
5714008e2a0SDamien Le Moal if ((iattr->ia_valid & ATTR_MODE) && S_ISDIR(inode->i_mode) &&
5724008e2a0SDamien Le Moal (iattr->ia_mode & 0222))
5734008e2a0SDamien Le Moal return -EPERM;
5744008e2a0SDamien Le Moal
5754008e2a0SDamien Le Moal if (((iattr->ia_valid & ATTR_UID) &&
5764008e2a0SDamien Le Moal !uid_eq(iattr->ia_uid, inode->i_uid)) ||
5774008e2a0SDamien Le Moal ((iattr->ia_valid & ATTR_GID) &&
5784008e2a0SDamien Le Moal !gid_eq(iattr->ia_gid, inode->i_gid))) {
579232dd599SLinus Torvalds ret = dquot_transfer(&nop_mnt_idmap, inode, iattr);
5804008e2a0SDamien Le Moal if (ret)
5814008e2a0SDamien Le Moal return ret;
5824008e2a0SDamien Le Moal }
5834008e2a0SDamien Le Moal
5844008e2a0SDamien Le Moal if (iattr->ia_valid & ATTR_SIZE) {
5854008e2a0SDamien Le Moal ret = zonefs_file_truncate(inode, iattr->ia_size);
5864008e2a0SDamien Le Moal if (ret)
5874008e2a0SDamien Le Moal return ret;
5884008e2a0SDamien Le Moal }
5894008e2a0SDamien Le Moal
590232dd599SLinus Torvalds setattr_copy(&nop_mnt_idmap, inode, iattr);
5914008e2a0SDamien Le Moal
592d207794aSDamien Le Moal if (S_ISREG(inode->i_mode)) {
593d207794aSDamien Le Moal struct zonefs_zone *z = zonefs_inode_zone(inode);
594d207794aSDamien Le Moal
595d207794aSDamien Le Moal z->z_mode = inode->i_mode;
596d207794aSDamien Le Moal z->z_uid = inode->i_uid;
597d207794aSDamien Le Moal z->z_gid = inode->i_gid;
5984008e2a0SDamien Le Moal }
5998dcc1a9dSDamien Le Moal
600d207794aSDamien Le Moal return 0;
6018dcc1a9dSDamien Le Moal }
6028dcc1a9dSDamien Le Moal
6034008e2a0SDamien Le Moal static const struct inode_operations zonefs_file_inode_operations = {
6048dcc1a9dSDamien Le Moal .setattr = zonefs_inode_setattr,
6058dcc1a9dSDamien Le Moal };
6068dcc1a9dSDamien Le Moal
zonefs_fname_to_fno(const struct qstr * fname)607d207794aSDamien Le Moal static long zonefs_fname_to_fno(const struct qstr *fname)
6088dcc1a9dSDamien Le Moal {
609d207794aSDamien Le Moal const char *name = fname->name;
610d207794aSDamien Le Moal unsigned int len = fname->len;
611d207794aSDamien Le Moal long fno = 0, shift = 1;
612d207794aSDamien Le Moal const char *rname;
613d207794aSDamien Le Moal char c = *name;
614d207794aSDamien Le Moal unsigned int i;
6158dcc1a9dSDamien Le Moal
616d207794aSDamien Le Moal /*
617d207794aSDamien Le Moal * File names are always a base-10 number string without any
618d207794aSDamien Le Moal * leading 0s.
619d207794aSDamien Le Moal */
620d207794aSDamien Le Moal if (!isdigit(c))
621d207794aSDamien Le Moal return -ENOENT;
622d207794aSDamien Le Moal
623d207794aSDamien Le Moal if (len > 1 && c == '0')
624d207794aSDamien Le Moal return -ENOENT;
625d207794aSDamien Le Moal
626d207794aSDamien Le Moal if (len == 1)
627d207794aSDamien Le Moal return c - '0';
628d207794aSDamien Le Moal
629d207794aSDamien Le Moal for (i = 0, rname = name + len - 1; i < len; i++, rname--) {
630d207794aSDamien Le Moal c = *rname;
631d207794aSDamien Le Moal if (!isdigit(c))
632d207794aSDamien Le Moal return -ENOENT;
633d207794aSDamien Le Moal fno += (c - '0') * shift;
634d207794aSDamien Le Moal shift *= 10;
6358dcc1a9dSDamien Le Moal }
6368dcc1a9dSDamien Le Moal
637d207794aSDamien Le Moal return fno;
638d207794aSDamien Le Moal }
639d207794aSDamien Le Moal
zonefs_get_file_inode(struct inode * dir,struct dentry * dentry)640d207794aSDamien Le Moal static struct inode *zonefs_get_file_inode(struct inode *dir,
641d207794aSDamien Le Moal struct dentry *dentry)
6428dcc1a9dSDamien Le Moal {
643d207794aSDamien Le Moal struct zonefs_zone_group *zgroup = dir->i_private;
644d207794aSDamien Le Moal struct super_block *sb = dir->i_sb;
6458dcc1a9dSDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
646d207794aSDamien Le Moal struct zonefs_zone *z;
647d207794aSDamien Le Moal struct inode *inode;
648d207794aSDamien Le Moal ino_t ino;
649d207794aSDamien Le Moal long fno;
6508dcc1a9dSDamien Le Moal
651d207794aSDamien Le Moal /* Get the file number from the file name */
652d207794aSDamien Le Moal fno = zonefs_fname_to_fno(&dentry->d_name);
653d207794aSDamien Le Moal if (fno < 0)
654d207794aSDamien Le Moal return ERR_PTR(fno);
6558dcc1a9dSDamien Le Moal
656d207794aSDamien Le Moal if (!zgroup->g_nr_zones || fno >= zgroup->g_nr_zones)
657d207794aSDamien Le Moal return ERR_PTR(-ENOENT);
658d207794aSDamien Le Moal
659d207794aSDamien Le Moal z = &zgroup->g_zones[fno];
660d207794aSDamien Le Moal ino = z->z_sector >> sbi->s_zone_sectors_shift;
661d207794aSDamien Le Moal inode = iget_locked(sb, ino);
662d207794aSDamien Le Moal if (!inode)
663d207794aSDamien Le Moal return ERR_PTR(-ENOMEM);
664d207794aSDamien Le Moal if (!(inode->i_state & I_NEW)) {
665d207794aSDamien Le Moal WARN_ON_ONCE(inode->i_private != z);
666d207794aSDamien Le Moal return inode;
6677dd12d65SDamien Le Moal }
668e3c3155bSJohannes Thumshirn
669d207794aSDamien Le Moal inode->i_ino = ino;
670d207794aSDamien Le Moal inode->i_mode = z->z_mode;
671f74207d8SJeff Layton inode->i_mtime = inode->i_atime = inode_set_ctime_to_ts(inode,
672f74207d8SJeff Layton inode_get_ctime(dir));
673d207794aSDamien Le Moal inode->i_uid = z->z_uid;
674d207794aSDamien Le Moal inode->i_gid = z->z_gid;
675aa7f243fSDamien Le Moal inode->i_size = z->z_wpoffset;
676aa7f243fSDamien Le Moal inode->i_blocks = z->z_capacity >> SECTOR_SHIFT;
677d207794aSDamien Le Moal inode->i_private = z;
6788dcc1a9dSDamien Le Moal
6798dcc1a9dSDamien Le Moal inode->i_op = &zonefs_file_inode_operations;
6808dcc1a9dSDamien Le Moal inode->i_fop = &zonefs_file_operations;
6818dcc1a9dSDamien Le Moal inode->i_mapping->a_ops = &zonefs_file_aops;
6828dcc1a9dSDamien Le Moal
68346a9c526SDamien Le Moal /* Update the inode access rights depending on the zone condition */
68446a9c526SDamien Le Moal zonefs_inode_update_mode(inode);
6851da18a29SDamien Le Moal
686d207794aSDamien Le Moal unlock_new_inode(inode);
687d207794aSDamien Le Moal
688d207794aSDamien Le Moal return inode;
6898dcc1a9dSDamien Le Moal }
6908dcc1a9dSDamien Le Moal
zonefs_get_zgroup_inode(struct super_block * sb,enum zonefs_ztype ztype)691d207794aSDamien Le Moal static struct inode *zonefs_get_zgroup_inode(struct super_block *sb,
692aa7f243fSDamien Le Moal enum zonefs_ztype ztype)
6938dcc1a9dSDamien Le Moal {
694d207794aSDamien Le Moal struct inode *root = d_inode(sb->s_root);
695d207794aSDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
6968dcc1a9dSDamien Le Moal struct inode *inode;
697d207794aSDamien Le Moal ino_t ino = bdev_nr_zones(sb->s_bdev) + ztype + 1;
6988dcc1a9dSDamien Le Moal
699d207794aSDamien Le Moal inode = iget_locked(sb, ino);
7008dcc1a9dSDamien Le Moal if (!inode)
701d207794aSDamien Le Moal return ERR_PTR(-ENOMEM);
702d207794aSDamien Le Moal if (!(inode->i_state & I_NEW))
703d207794aSDamien Le Moal return inode;
7048dcc1a9dSDamien Le Moal
705d207794aSDamien Le Moal inode->i_ino = ino;
706232dd599SLinus Torvalds inode_init_owner(&nop_mnt_idmap, inode, root, S_IFDIR | 0555);
707d207794aSDamien Le Moal inode->i_size = sbi->s_zgroup[ztype].g_nr_zones;
708f74207d8SJeff Layton inode->i_mtime = inode->i_atime = inode_set_ctime_to_ts(inode,
709f74207d8SJeff Layton inode_get_ctime(root));
710d207794aSDamien Le Moal inode->i_private = &sbi->s_zgroup[ztype];
711d207794aSDamien Le Moal set_nlink(inode, 2);
7121da18a29SDamien Le Moal
713d207794aSDamien Le Moal inode->i_op = &zonefs_dir_inode_operations;
714d207794aSDamien Le Moal inode->i_fop = &zonefs_dir_operations;
7158dcc1a9dSDamien Le Moal
716d207794aSDamien Le Moal unlock_new_inode(inode);
7178dcc1a9dSDamien Le Moal
718d207794aSDamien Le Moal return inode;
7198dcc1a9dSDamien Le Moal }
7208dcc1a9dSDamien Le Moal
721d207794aSDamien Le Moal
zonefs_get_dir_inode(struct inode * dir,struct dentry * dentry)722d207794aSDamien Le Moal static struct inode *zonefs_get_dir_inode(struct inode *dir,
723d207794aSDamien Le Moal struct dentry *dentry)
724d207794aSDamien Le Moal {
725d207794aSDamien Le Moal struct super_block *sb = dir->i_sb;
726d207794aSDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
727d207794aSDamien Le Moal const char *name = dentry->d_name.name;
728d207794aSDamien Le Moal enum zonefs_ztype ztype;
7298dcc1a9dSDamien Le Moal
7308dcc1a9dSDamien Le Moal /*
731d207794aSDamien Le Moal * We only need to check for the "seq" directory and
732d207794aSDamien Le Moal * the "cnv" directory if we have conventional zones.
7338dcc1a9dSDamien Le Moal */
734d207794aSDamien Le Moal if (dentry->d_name.len != 3)
735d207794aSDamien Le Moal return ERR_PTR(-ENOENT);
736d207794aSDamien Le Moal
737d207794aSDamien Le Moal for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
738d207794aSDamien Le Moal if (sbi->s_zgroup[ztype].g_nr_zones &&
739d207794aSDamien Le Moal memcmp(name, zonefs_zgroup_name(ztype), 3) == 0)
740d207794aSDamien Le Moal break;
741d207794aSDamien Le Moal }
742d207794aSDamien Le Moal if (ztype == ZONEFS_ZTYPE_MAX)
743d207794aSDamien Le Moal return ERR_PTR(-ENOENT);
744d207794aSDamien Le Moal
745d207794aSDamien Le Moal return zonefs_get_zgroup_inode(sb, ztype);
7468dcc1a9dSDamien Le Moal }
7478dcc1a9dSDamien Le Moal
zonefs_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)748d207794aSDamien Le Moal static struct dentry *zonefs_lookup(struct inode *dir, struct dentry *dentry,
749d207794aSDamien Le Moal unsigned int flags)
7508dcc1a9dSDamien Le Moal {
7518dcc1a9dSDamien Le Moal struct inode *inode;
7528dcc1a9dSDamien Le Moal
753d207794aSDamien Le Moal if (dentry->d_name.len > ZONEFS_NAME_MAX)
754d207794aSDamien Le Moal return ERR_PTR(-ENAMETOOLONG);
7558dcc1a9dSDamien Le Moal
756d207794aSDamien Le Moal if (dir == d_inode(dir->i_sb->s_root))
757d207794aSDamien Le Moal inode = zonefs_get_dir_inode(dir, dentry);
758d207794aSDamien Le Moal else
759d207794aSDamien Le Moal inode = zonefs_get_file_inode(dir, dentry);
760d207794aSDamien Le Moal if (IS_ERR(inode))
761d207794aSDamien Le Moal return ERR_CAST(inode);
7628dcc1a9dSDamien Le Moal
763d207794aSDamien Le Moal return d_splice_alias(inode, dentry);
7648dcc1a9dSDamien Le Moal }
7658dcc1a9dSDamien Le Moal
zonefs_readdir_root(struct file * file,struct dir_context * ctx)766d207794aSDamien Le Moal static int zonefs_readdir_root(struct file *file, struct dir_context *ctx)
767d207794aSDamien Le Moal {
768d207794aSDamien Le Moal struct inode *inode = file_inode(file);
769d207794aSDamien Le Moal struct super_block *sb = inode->i_sb;
770d207794aSDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
771d207794aSDamien Le Moal enum zonefs_ztype ztype = ZONEFS_ZTYPE_CNV;
772d207794aSDamien Le Moal ino_t base_ino = bdev_nr_zones(sb->s_bdev) + 1;
7738dcc1a9dSDamien Le Moal
774d207794aSDamien Le Moal if (ctx->pos >= inode->i_size)
775d207794aSDamien Le Moal return 0;
7768dcc1a9dSDamien Le Moal
777d207794aSDamien Le Moal if (!dir_emit_dots(file, ctx))
778d207794aSDamien Le Moal return 0;
7798dcc1a9dSDamien Le Moal
780d207794aSDamien Le Moal if (ctx->pos == 2) {
781d207794aSDamien Le Moal if (!sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones)
782d207794aSDamien Le Moal ztype = ZONEFS_ZTYPE_SEQ;
783d207794aSDamien Le Moal
784d207794aSDamien Le Moal if (!dir_emit(ctx, zonefs_zgroup_name(ztype), 3,
785d207794aSDamien Le Moal base_ino + ztype, DT_DIR))
786d207794aSDamien Le Moal return 0;
787d207794aSDamien Le Moal ctx->pos++;
7888dcc1a9dSDamien Le Moal }
7898dcc1a9dSDamien Le Moal
790d207794aSDamien Le Moal if (ctx->pos == 3 && ztype != ZONEFS_ZTYPE_SEQ) {
791d207794aSDamien Le Moal ztype = ZONEFS_ZTYPE_SEQ;
792d207794aSDamien Le Moal if (!dir_emit(ctx, zonefs_zgroup_name(ztype), 3,
793d207794aSDamien Le Moal base_ino + ztype, DT_DIR))
794d207794aSDamien Le Moal return 0;
795d207794aSDamien Le Moal ctx->pos++;
796d207794aSDamien Le Moal }
797d207794aSDamien Le Moal
798d207794aSDamien Le Moal return 0;
799d207794aSDamien Le Moal }
800d207794aSDamien Le Moal
zonefs_readdir_zgroup(struct file * file,struct dir_context * ctx)801d207794aSDamien Le Moal static int zonefs_readdir_zgroup(struct file *file,
802d207794aSDamien Le Moal struct dir_context *ctx)
803d207794aSDamien Le Moal {
804d207794aSDamien Le Moal struct inode *inode = file_inode(file);
805d207794aSDamien Le Moal struct zonefs_zone_group *zgroup = inode->i_private;
806d207794aSDamien Le Moal struct super_block *sb = inode->i_sb;
807d207794aSDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
808d207794aSDamien Le Moal struct zonefs_zone *z;
809d207794aSDamien Le Moal int fname_len;
810d207794aSDamien Le Moal char *fname;
811d207794aSDamien Le Moal ino_t ino;
812d207794aSDamien Le Moal int f;
813d207794aSDamien Le Moal
814d207794aSDamien Le Moal /*
815d207794aSDamien Le Moal * The size of zone group directories is equal to the number
816d207794aSDamien Le Moal * of zone files in the group and does note include the "." and
817d207794aSDamien Le Moal * ".." entries. Hence the "+ 2" here.
818d207794aSDamien Le Moal */
819d207794aSDamien Le Moal if (ctx->pos >= inode->i_size + 2)
820d207794aSDamien Le Moal return 0;
821d207794aSDamien Le Moal
822d207794aSDamien Le Moal if (!dir_emit_dots(file, ctx))
823d207794aSDamien Le Moal return 0;
824d207794aSDamien Le Moal
825d207794aSDamien Le Moal fname = kmalloc(ZONEFS_NAME_MAX, GFP_KERNEL);
826d207794aSDamien Le Moal if (!fname)
827d207794aSDamien Le Moal return -ENOMEM;
828d207794aSDamien Le Moal
829d207794aSDamien Le Moal for (f = ctx->pos - 2; f < zgroup->g_nr_zones; f++) {
830d207794aSDamien Le Moal z = &zgroup->g_zones[f];
831d207794aSDamien Le Moal ino = z->z_sector >> sbi->s_zone_sectors_shift;
832d207794aSDamien Le Moal fname_len = snprintf(fname, ZONEFS_NAME_MAX - 1, "%u", f);
833d207794aSDamien Le Moal if (!dir_emit(ctx, fname, fname_len, ino, DT_REG))
834d207794aSDamien Le Moal break;
835d207794aSDamien Le Moal ctx->pos++;
836d207794aSDamien Le Moal }
837d207794aSDamien Le Moal
838d207794aSDamien Le Moal kfree(fname);
839d207794aSDamien Le Moal
840d207794aSDamien Le Moal return 0;
841d207794aSDamien Le Moal }
842d207794aSDamien Le Moal
zonefs_readdir(struct file * file,struct dir_context * ctx)843d207794aSDamien Le Moal static int zonefs_readdir(struct file *file, struct dir_context *ctx)
844d207794aSDamien Le Moal {
845d207794aSDamien Le Moal struct inode *inode = file_inode(file);
846d207794aSDamien Le Moal
847d207794aSDamien Le Moal if (inode == d_inode(inode->i_sb->s_root))
848d207794aSDamien Le Moal return zonefs_readdir_root(file, ctx);
849d207794aSDamien Le Moal
850d207794aSDamien Le Moal return zonefs_readdir_zgroup(file, ctx);
851d207794aSDamien Le Moal }
852d207794aSDamien Le Moal
853d207794aSDamien Le Moal const struct inode_operations zonefs_dir_inode_operations = {
854d207794aSDamien Le Moal .lookup = zonefs_lookup,
855d207794aSDamien Le Moal .setattr = zonefs_inode_setattr,
856d207794aSDamien Le Moal };
857d207794aSDamien Le Moal
858d207794aSDamien Le Moal const struct file_operations zonefs_dir_operations = {
859d207794aSDamien Le Moal .llseek = generic_file_llseek,
860d207794aSDamien Le Moal .read = generic_read_dir,
861d207794aSDamien Le Moal .iterate_shared = zonefs_readdir,
862d207794aSDamien Le Moal };
863d207794aSDamien Le Moal
8648dcc1a9dSDamien Le Moal struct zonefs_zone_data {
8658dcc1a9dSDamien Le Moal struct super_block *sb;
8668dcc1a9dSDamien Le Moal unsigned int nr_zones[ZONEFS_ZTYPE_MAX];
867aa7f243fSDamien Le Moal sector_t cnv_zone_start;
8688dcc1a9dSDamien Le Moal struct blk_zone *zones;
8698dcc1a9dSDamien Le Moal };
8708dcc1a9dSDamien Le Moal
zonefs_get_zone_info_cb(struct blk_zone * zone,unsigned int idx,void * data)8718dcc1a9dSDamien Le Moal static int zonefs_get_zone_info_cb(struct blk_zone *zone, unsigned int idx,
8728dcc1a9dSDamien Le Moal void *data)
8738dcc1a9dSDamien Le Moal {
8748dcc1a9dSDamien Le Moal struct zonefs_zone_data *zd = data;
875aa7f243fSDamien Le Moal struct super_block *sb = zd->sb;
876aa7f243fSDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
8778dcc1a9dSDamien Le Moal
8788dcc1a9dSDamien Le Moal /*
879aa7f243fSDamien Le Moal * We do not care about the first zone: it contains the super block
880aa7f243fSDamien Le Moal * and not exposed as a file.
881aa7f243fSDamien Le Moal */
882aa7f243fSDamien Le Moal if (!idx)
883aa7f243fSDamien Le Moal return 0;
884aa7f243fSDamien Le Moal
885aa7f243fSDamien Le Moal /*
886aa7f243fSDamien Le Moal * Count the number of zones that will be exposed as files.
887aa7f243fSDamien Le Moal * For sequential zones, we always have as many files as zones.
888aa7f243fSDamien Le Moal * FOr conventional zones, the number of files depends on if we have
889aa7f243fSDamien Le Moal * conventional zones aggregation enabled.
8908dcc1a9dSDamien Le Moal */
8918dcc1a9dSDamien Le Moal switch (zone->type) {
8928dcc1a9dSDamien Le Moal case BLK_ZONE_TYPE_CONVENTIONAL:
893aa7f243fSDamien Le Moal if (sbi->s_features & ZONEFS_F_AGGRCNV) {
894aa7f243fSDamien Le Moal /* One file per set of contiguous conventional zones */
895aa7f243fSDamien Le Moal if (!(sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones) ||
896aa7f243fSDamien Le Moal zone->start != zd->cnv_zone_start)
897aa7f243fSDamien Le Moal sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones++;
898aa7f243fSDamien Le Moal zd->cnv_zone_start = zone->start + zone->len;
899aa7f243fSDamien Le Moal } else {
900aa7f243fSDamien Le Moal /* One file per zone */
901aa7f243fSDamien Le Moal sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones++;
902aa7f243fSDamien Le Moal }
9038dcc1a9dSDamien Le Moal break;
9048dcc1a9dSDamien Le Moal case BLK_ZONE_TYPE_SEQWRITE_REQ:
9058dcc1a9dSDamien Le Moal case BLK_ZONE_TYPE_SEQWRITE_PREF:
906aa7f243fSDamien Le Moal sbi->s_zgroup[ZONEFS_ZTYPE_SEQ].g_nr_zones++;
9078dcc1a9dSDamien Le Moal break;
9088dcc1a9dSDamien Le Moal default:
9098dcc1a9dSDamien Le Moal zonefs_err(zd->sb, "Unsupported zone type 0x%x\n",
9108dcc1a9dSDamien Le Moal zone->type);
9118dcc1a9dSDamien Le Moal return -EIO;
9128dcc1a9dSDamien Le Moal }
9138dcc1a9dSDamien Le Moal
9148dcc1a9dSDamien Le Moal memcpy(&zd->zones[idx], zone, sizeof(struct blk_zone));
9158dcc1a9dSDamien Le Moal
9168dcc1a9dSDamien Le Moal return 0;
9178dcc1a9dSDamien Le Moal }
9188dcc1a9dSDamien Le Moal
zonefs_get_zone_info(struct zonefs_zone_data * zd)9198dcc1a9dSDamien Le Moal static int zonefs_get_zone_info(struct zonefs_zone_data *zd)
9208dcc1a9dSDamien Le Moal {
9218dcc1a9dSDamien Le Moal struct block_device *bdev = zd->sb->s_bdev;
9228dcc1a9dSDamien Le Moal int ret;
9238dcc1a9dSDamien Le Moal
924b623e347SChristoph Hellwig zd->zones = kvcalloc(bdev_nr_zones(bdev), sizeof(struct blk_zone),
925b623e347SChristoph Hellwig GFP_KERNEL);
9268dcc1a9dSDamien Le Moal if (!zd->zones)
9278dcc1a9dSDamien Le Moal return -ENOMEM;
9288dcc1a9dSDamien Le Moal
9298dcc1a9dSDamien Le Moal /* Get zones information from the device */
9308dcc1a9dSDamien Le Moal ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES,
9318dcc1a9dSDamien Le Moal zonefs_get_zone_info_cb, zd);
9328dcc1a9dSDamien Le Moal if (ret < 0) {
9338dcc1a9dSDamien Le Moal zonefs_err(zd->sb, "Zone report failed %d\n", ret);
9348dcc1a9dSDamien Le Moal return ret;
9358dcc1a9dSDamien Le Moal }
9368dcc1a9dSDamien Le Moal
937b623e347SChristoph Hellwig if (ret != bdev_nr_zones(bdev)) {
9388dcc1a9dSDamien Le Moal zonefs_err(zd->sb, "Invalid zone report (%d/%u zones)\n",
939b623e347SChristoph Hellwig ret, bdev_nr_zones(bdev));
9408dcc1a9dSDamien Le Moal return -EIO;
9418dcc1a9dSDamien Le Moal }
9428dcc1a9dSDamien Le Moal
9438dcc1a9dSDamien Le Moal return 0;
9448dcc1a9dSDamien Le Moal }
9458dcc1a9dSDamien Le Moal
zonefs_free_zone_info(struct zonefs_zone_data * zd)946aa7f243fSDamien Le Moal static inline void zonefs_free_zone_info(struct zonefs_zone_data *zd)
9478dcc1a9dSDamien Le Moal {
9488dcc1a9dSDamien Le Moal kvfree(zd->zones);
9498dcc1a9dSDamien Le Moal }
9508dcc1a9dSDamien Le Moal
9518dcc1a9dSDamien Le Moal /*
952aa7f243fSDamien Le Moal * Create a zone group and populate it with zone files.
953aa7f243fSDamien Le Moal */
zonefs_init_zgroup(struct super_block * sb,struct zonefs_zone_data * zd,enum zonefs_ztype ztype)954aa7f243fSDamien Le Moal static int zonefs_init_zgroup(struct super_block *sb,
955aa7f243fSDamien Le Moal struct zonefs_zone_data *zd,
956aa7f243fSDamien Le Moal enum zonefs_ztype ztype)
957aa7f243fSDamien Le Moal {
958aa7f243fSDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
959aa7f243fSDamien Le Moal struct zonefs_zone_group *zgroup = &sbi->s_zgroup[ztype];
960aa7f243fSDamien Le Moal struct blk_zone *zone, *next, *end;
961aa7f243fSDamien Le Moal struct zonefs_zone *z;
962aa7f243fSDamien Le Moal unsigned int n = 0;
963aa7f243fSDamien Le Moal int ret;
964aa7f243fSDamien Le Moal
965aa7f243fSDamien Le Moal /* Allocate the zone group. If it is empty, we have nothing to do. */
966aa7f243fSDamien Le Moal if (!zgroup->g_nr_zones)
967aa7f243fSDamien Le Moal return 0;
968aa7f243fSDamien Le Moal
969aa7f243fSDamien Le Moal zgroup->g_zones = kvcalloc(zgroup->g_nr_zones,
970aa7f243fSDamien Le Moal sizeof(struct zonefs_zone), GFP_KERNEL);
971aa7f243fSDamien Le Moal if (!zgroup->g_zones)
972aa7f243fSDamien Le Moal return -ENOMEM;
973aa7f243fSDamien Le Moal
974aa7f243fSDamien Le Moal /*
975aa7f243fSDamien Le Moal * Initialize the zone groups using the device zone information.
976aa7f243fSDamien Le Moal * We always skip the first zone as it contains the super block
977aa7f243fSDamien Le Moal * and is not use to back a file.
978aa7f243fSDamien Le Moal */
979aa7f243fSDamien Le Moal end = zd->zones + bdev_nr_zones(sb->s_bdev);
980aa7f243fSDamien Le Moal for (zone = &zd->zones[1]; zone < end; zone = next) {
981aa7f243fSDamien Le Moal
982aa7f243fSDamien Le Moal next = zone + 1;
983aa7f243fSDamien Le Moal if (zonefs_zone_type(zone) != ztype)
984aa7f243fSDamien Le Moal continue;
985aa7f243fSDamien Le Moal
986aa7f243fSDamien Le Moal if (WARN_ON_ONCE(n >= zgroup->g_nr_zones))
987aa7f243fSDamien Le Moal return -EINVAL;
988aa7f243fSDamien Le Moal
989aa7f243fSDamien Le Moal /*
990aa7f243fSDamien Le Moal * For conventional zones, contiguous zones can be aggregated
991aa7f243fSDamien Le Moal * together to form larger files. Note that this overwrites the
992aa7f243fSDamien Le Moal * length of the first zone of the set of contiguous zones
993aa7f243fSDamien Le Moal * aggregated together. If one offline or read-only zone is
994aa7f243fSDamien Le Moal * found, assume that all zones aggregated have the same
995aa7f243fSDamien Le Moal * condition.
996aa7f243fSDamien Le Moal */
997aa7f243fSDamien Le Moal if (ztype == ZONEFS_ZTYPE_CNV &&
998aa7f243fSDamien Le Moal (sbi->s_features & ZONEFS_F_AGGRCNV)) {
999aa7f243fSDamien Le Moal for (; next < end; next++) {
1000aa7f243fSDamien Le Moal if (zonefs_zone_type(next) != ztype)
1001aa7f243fSDamien Le Moal break;
1002aa7f243fSDamien Le Moal zone->len += next->len;
1003aa7f243fSDamien Le Moal zone->capacity += next->capacity;
1004aa7f243fSDamien Le Moal if (next->cond == BLK_ZONE_COND_READONLY &&
1005aa7f243fSDamien Le Moal zone->cond != BLK_ZONE_COND_OFFLINE)
1006aa7f243fSDamien Le Moal zone->cond = BLK_ZONE_COND_READONLY;
1007aa7f243fSDamien Le Moal else if (next->cond == BLK_ZONE_COND_OFFLINE)
1008aa7f243fSDamien Le Moal zone->cond = BLK_ZONE_COND_OFFLINE;
1009aa7f243fSDamien Le Moal }
1010aa7f243fSDamien Le Moal }
1011aa7f243fSDamien Le Moal
1012aa7f243fSDamien Le Moal z = &zgroup->g_zones[n];
1013aa7f243fSDamien Le Moal if (ztype == ZONEFS_ZTYPE_CNV)
1014aa7f243fSDamien Le Moal z->z_flags |= ZONEFS_ZONE_CNV;
1015aa7f243fSDamien Le Moal z->z_sector = zone->start;
1016aa7f243fSDamien Le Moal z->z_size = zone->len << SECTOR_SHIFT;
1017aa7f243fSDamien Le Moal if (z->z_size > bdev_zone_sectors(sb->s_bdev) << SECTOR_SHIFT &&
1018aa7f243fSDamien Le Moal !(sbi->s_features & ZONEFS_F_AGGRCNV)) {
1019aa7f243fSDamien Le Moal zonefs_err(sb,
1020aa7f243fSDamien Le Moal "Invalid zone size %llu (device zone sectors %llu)\n",
1021aa7f243fSDamien Le Moal z->z_size,
1022aa7f243fSDamien Le Moal bdev_zone_sectors(sb->s_bdev) << SECTOR_SHIFT);
1023aa7f243fSDamien Le Moal return -EINVAL;
1024aa7f243fSDamien Le Moal }
1025aa7f243fSDamien Le Moal
1026aa7f243fSDamien Le Moal z->z_capacity = min_t(loff_t, MAX_LFS_FILESIZE,
1027aa7f243fSDamien Le Moal zone->capacity << SECTOR_SHIFT);
1028aa7f243fSDamien Le Moal z->z_wpoffset = zonefs_check_zone_condition(sb, z, zone);
1029aa7f243fSDamien Le Moal
1030d207794aSDamien Le Moal z->z_mode = S_IFREG | sbi->s_perm;
1031d207794aSDamien Le Moal z->z_uid = sbi->s_uid;
1032d207794aSDamien Le Moal z->z_gid = sbi->s_gid;
1033d207794aSDamien Le Moal
1034d207794aSDamien Le Moal /*
1035d207794aSDamien Le Moal * Let zonefs_inode_update_mode() know that we will need
1036d207794aSDamien Le Moal * special initialization of the inode mode the first time
1037d207794aSDamien Le Moal * it is accessed.
1038d207794aSDamien Le Moal */
1039d207794aSDamien Le Moal z->z_flags |= ZONEFS_ZONE_INIT_MODE;
1040d207794aSDamien Le Moal
1041aa7f243fSDamien Le Moal sb->s_maxbytes = max(z->z_capacity, sb->s_maxbytes);
1042aa7f243fSDamien Le Moal sbi->s_blocks += z->z_capacity >> sb->s_blocksize_bits;
1043aa7f243fSDamien Le Moal sbi->s_used_blocks += z->z_wpoffset >> sb->s_blocksize_bits;
1044aa7f243fSDamien Le Moal
1045aa7f243fSDamien Le Moal /*
1046aa7f243fSDamien Le Moal * For sequential zones, make sure that any open zone is closed
1047aa7f243fSDamien Le Moal * first to ensure that the initial number of open zones is 0,
1048aa7f243fSDamien Le Moal * in sync with the open zone accounting done when the mount
1049aa7f243fSDamien Le Moal * option ZONEFS_MNTOPT_EXPLICIT_OPEN is used.
1050aa7f243fSDamien Le Moal */
1051aa7f243fSDamien Le Moal if (ztype == ZONEFS_ZTYPE_SEQ &&
1052aa7f243fSDamien Le Moal (zone->cond == BLK_ZONE_COND_IMP_OPEN ||
1053aa7f243fSDamien Le Moal zone->cond == BLK_ZONE_COND_EXP_OPEN)) {
1054aa7f243fSDamien Le Moal ret = zonefs_zone_mgmt(sb, z, REQ_OP_ZONE_CLOSE);
1055aa7f243fSDamien Le Moal if (ret)
1056aa7f243fSDamien Le Moal return ret;
1057aa7f243fSDamien Le Moal }
1058aa7f243fSDamien Le Moal
1059aa7f243fSDamien Le Moal zonefs_account_active(sb, z);
1060aa7f243fSDamien Le Moal
1061aa7f243fSDamien Le Moal n++;
1062aa7f243fSDamien Le Moal }
1063aa7f243fSDamien Le Moal
1064aa7f243fSDamien Le Moal if (WARN_ON_ONCE(n != zgroup->g_nr_zones))
1065aa7f243fSDamien Le Moal return -EINVAL;
1066aa7f243fSDamien Le Moal
1067aa7f243fSDamien Le Moal zonefs_info(sb, "Zone group \"%s\" has %u file%s\n",
1068aa7f243fSDamien Le Moal zonefs_zgroup_name(ztype),
1069aa7f243fSDamien Le Moal zgroup->g_nr_zones,
1070aa7f243fSDamien Le Moal zgroup->g_nr_zones > 1 ? "s" : "");
1071aa7f243fSDamien Le Moal
1072aa7f243fSDamien Le Moal return 0;
1073aa7f243fSDamien Le Moal }
1074aa7f243fSDamien Le Moal
zonefs_free_zgroups(struct super_block * sb)1075aa7f243fSDamien Le Moal static void zonefs_free_zgroups(struct super_block *sb)
1076aa7f243fSDamien Le Moal {
1077aa7f243fSDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1078aa7f243fSDamien Le Moal enum zonefs_ztype ztype;
1079aa7f243fSDamien Le Moal
1080aa7f243fSDamien Le Moal if (!sbi)
1081aa7f243fSDamien Le Moal return;
1082aa7f243fSDamien Le Moal
1083aa7f243fSDamien Le Moal for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
1084aa7f243fSDamien Le Moal kvfree(sbi->s_zgroup[ztype].g_zones);
1085aa7f243fSDamien Le Moal sbi->s_zgroup[ztype].g_zones = NULL;
1086aa7f243fSDamien Le Moal }
1087aa7f243fSDamien Le Moal }
1088aa7f243fSDamien Le Moal
1089aa7f243fSDamien Le Moal /*
1090aa7f243fSDamien Le Moal * Create a zone group and populate it with zone files.
1091aa7f243fSDamien Le Moal */
zonefs_init_zgroups(struct super_block * sb)1092aa7f243fSDamien Le Moal static int zonefs_init_zgroups(struct super_block *sb)
1093aa7f243fSDamien Le Moal {
1094aa7f243fSDamien Le Moal struct zonefs_zone_data zd;
1095aa7f243fSDamien Le Moal enum zonefs_ztype ztype;
1096aa7f243fSDamien Le Moal int ret;
1097aa7f243fSDamien Le Moal
1098aa7f243fSDamien Le Moal /* First get the device zone information */
1099aa7f243fSDamien Le Moal memset(&zd, 0, sizeof(struct zonefs_zone_data));
1100aa7f243fSDamien Le Moal zd.sb = sb;
1101aa7f243fSDamien Le Moal ret = zonefs_get_zone_info(&zd);
1102aa7f243fSDamien Le Moal if (ret)
1103aa7f243fSDamien Le Moal goto cleanup;
1104aa7f243fSDamien Le Moal
1105aa7f243fSDamien Le Moal /* Allocate and initialize the zone groups */
1106aa7f243fSDamien Le Moal for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
1107aa7f243fSDamien Le Moal ret = zonefs_init_zgroup(sb, &zd, ztype);
1108aa7f243fSDamien Le Moal if (ret) {
1109aa7f243fSDamien Le Moal zonefs_info(sb,
1110aa7f243fSDamien Le Moal "Zone group \"%s\" initialization failed\n",
1111aa7f243fSDamien Le Moal zonefs_zgroup_name(ztype));
1112aa7f243fSDamien Le Moal break;
1113aa7f243fSDamien Le Moal }
1114aa7f243fSDamien Le Moal }
1115aa7f243fSDamien Le Moal
1116aa7f243fSDamien Le Moal cleanup:
1117aa7f243fSDamien Le Moal zonefs_free_zone_info(&zd);
1118aa7f243fSDamien Le Moal if (ret)
1119aa7f243fSDamien Le Moal zonefs_free_zgroups(sb);
1120aa7f243fSDamien Le Moal
1121aa7f243fSDamien Le Moal return ret;
1122aa7f243fSDamien Le Moal }
1123aa7f243fSDamien Le Moal
1124aa7f243fSDamien Le Moal /*
11258dcc1a9dSDamien Le Moal * Read super block information from the device.
11268dcc1a9dSDamien Le Moal */
zonefs_read_super(struct super_block * sb)11278dcc1a9dSDamien Le Moal static int zonefs_read_super(struct super_block *sb)
11288dcc1a9dSDamien Le Moal {
11298dcc1a9dSDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
11308dcc1a9dSDamien Le Moal struct zonefs_super *super;
11318dcc1a9dSDamien Le Moal u32 crc, stored_crc;
11328dcc1a9dSDamien Le Moal struct page *page;
11338dcc1a9dSDamien Le Moal struct bio_vec bio_vec;
11348dcc1a9dSDamien Le Moal struct bio bio;
11358dcc1a9dSDamien Le Moal int ret;
11368dcc1a9dSDamien Le Moal
11378dcc1a9dSDamien Le Moal page = alloc_page(GFP_KERNEL);
11388dcc1a9dSDamien Le Moal if (!page)
11398dcc1a9dSDamien Le Moal return -ENOMEM;
11408dcc1a9dSDamien Le Moal
114149add496SChristoph Hellwig bio_init(&bio, sb->s_bdev, &bio_vec, 1, REQ_OP_READ);
11428dcc1a9dSDamien Le Moal bio.bi_iter.bi_sector = 0;
11430fa5b08cSJohannes Thumshirn __bio_add_page(&bio, page, PAGE_SIZE, 0);
11448dcc1a9dSDamien Le Moal
11458dcc1a9dSDamien Le Moal ret = submit_bio_wait(&bio);
11468dcc1a9dSDamien Le Moal if (ret)
11478dcc1a9dSDamien Le Moal goto free_page;
11488dcc1a9dSDamien Le Moal
11496bac30bbSFabio M. De Francesco super = page_address(page);
11508dcc1a9dSDamien Le Moal
11518dcc1a9dSDamien Le Moal ret = -EINVAL;
11528dcc1a9dSDamien Le Moal if (le32_to_cpu(super->s_magic) != ZONEFS_MAGIC)
11536bac30bbSFabio M. De Francesco goto free_page;
11548dcc1a9dSDamien Le Moal
11558dcc1a9dSDamien Le Moal stored_crc = le32_to_cpu(super->s_crc);
11568dcc1a9dSDamien Le Moal super->s_crc = 0;
11578dcc1a9dSDamien Le Moal crc = crc32(~0U, (unsigned char *)super, sizeof(struct zonefs_super));
11588dcc1a9dSDamien Le Moal if (crc != stored_crc) {
11598dcc1a9dSDamien Le Moal zonefs_err(sb, "Invalid checksum (Expected 0x%08x, got 0x%08x)",
11608dcc1a9dSDamien Le Moal crc, stored_crc);
11616bac30bbSFabio M. De Francesco goto free_page;
11628dcc1a9dSDamien Le Moal }
11638dcc1a9dSDamien Le Moal
11648dcc1a9dSDamien Le Moal sbi->s_features = le64_to_cpu(super->s_features);
11658dcc1a9dSDamien Le Moal if (sbi->s_features & ~ZONEFS_F_DEFINED_FEATURES) {
11668dcc1a9dSDamien Le Moal zonefs_err(sb, "Unknown features set 0x%llx\n",
11678dcc1a9dSDamien Le Moal sbi->s_features);
11686bac30bbSFabio M. De Francesco goto free_page;
11698dcc1a9dSDamien Le Moal }
11708dcc1a9dSDamien Le Moal
11718dcc1a9dSDamien Le Moal if (sbi->s_features & ZONEFS_F_UID) {
11728dcc1a9dSDamien Le Moal sbi->s_uid = make_kuid(current_user_ns(),
11738dcc1a9dSDamien Le Moal le32_to_cpu(super->s_uid));
11748dcc1a9dSDamien Le Moal if (!uid_valid(sbi->s_uid)) {
11758dcc1a9dSDamien Le Moal zonefs_err(sb, "Invalid UID feature\n");
11766bac30bbSFabio M. De Francesco goto free_page;
11778dcc1a9dSDamien Le Moal }
11788dcc1a9dSDamien Le Moal }
11798dcc1a9dSDamien Le Moal
11808dcc1a9dSDamien Le Moal if (sbi->s_features & ZONEFS_F_GID) {
11818dcc1a9dSDamien Le Moal sbi->s_gid = make_kgid(current_user_ns(),
11828dcc1a9dSDamien Le Moal le32_to_cpu(super->s_gid));
11838dcc1a9dSDamien Le Moal if (!gid_valid(sbi->s_gid)) {
11848dcc1a9dSDamien Le Moal zonefs_err(sb, "Invalid GID feature\n");
11856bac30bbSFabio M. De Francesco goto free_page;
11868dcc1a9dSDamien Le Moal }
11878dcc1a9dSDamien Le Moal }
11888dcc1a9dSDamien Le Moal
11898dcc1a9dSDamien Le Moal if (sbi->s_features & ZONEFS_F_PERM)
11908dcc1a9dSDamien Le Moal sbi->s_perm = le32_to_cpu(super->s_perm);
11918dcc1a9dSDamien Le Moal
11928dcc1a9dSDamien Le Moal if (memchr_inv(super->s_reserved, 0, sizeof(super->s_reserved))) {
11938dcc1a9dSDamien Le Moal zonefs_err(sb, "Reserved area is being used\n");
11946bac30bbSFabio M. De Francesco goto free_page;
11958dcc1a9dSDamien Le Moal }
11968dcc1a9dSDamien Le Moal
1197568776f9SAndy Shevchenko import_uuid(&sbi->s_uuid, super->s_uuid);
11988dcc1a9dSDamien Le Moal ret = 0;
11998dcc1a9dSDamien Le Moal
12008dcc1a9dSDamien Le Moal free_page:
12018dcc1a9dSDamien Le Moal __free_page(page);
12028dcc1a9dSDamien Le Moal
12038dcc1a9dSDamien Le Moal return ret;
12048dcc1a9dSDamien Le Moal }
12058dcc1a9dSDamien Le Moal
12064008e2a0SDamien Le Moal static const struct super_operations zonefs_sops = {
12074008e2a0SDamien Le Moal .alloc_inode = zonefs_alloc_inode,
12084008e2a0SDamien Le Moal .free_inode = zonefs_free_inode,
12094008e2a0SDamien Le Moal .statfs = zonefs_statfs,
12104008e2a0SDamien Le Moal .remount_fs = zonefs_remount,
12114008e2a0SDamien Le Moal .show_options = zonefs_show_options,
12124008e2a0SDamien Le Moal };
12134008e2a0SDamien Le Moal
zonefs_get_zgroup_inodes(struct super_block * sb)121443592c46SDamien Le Moal static int zonefs_get_zgroup_inodes(struct super_block *sb)
121543592c46SDamien Le Moal {
121643592c46SDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
121743592c46SDamien Le Moal struct inode *dir_inode;
121843592c46SDamien Le Moal enum zonefs_ztype ztype;
121943592c46SDamien Le Moal
122043592c46SDamien Le Moal for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
122143592c46SDamien Le Moal if (!sbi->s_zgroup[ztype].g_nr_zones)
122243592c46SDamien Le Moal continue;
122343592c46SDamien Le Moal
122443592c46SDamien Le Moal dir_inode = zonefs_get_zgroup_inode(sb, ztype);
122543592c46SDamien Le Moal if (IS_ERR(dir_inode))
122643592c46SDamien Le Moal return PTR_ERR(dir_inode);
122743592c46SDamien Le Moal
122843592c46SDamien Le Moal sbi->s_zgroup[ztype].g_inode = dir_inode;
122943592c46SDamien Le Moal }
123043592c46SDamien Le Moal
123143592c46SDamien Le Moal return 0;
123243592c46SDamien Le Moal }
123343592c46SDamien Le Moal
zonefs_release_zgroup_inodes(struct super_block * sb)123443592c46SDamien Le Moal static void zonefs_release_zgroup_inodes(struct super_block *sb)
123543592c46SDamien Le Moal {
123643592c46SDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
123743592c46SDamien Le Moal enum zonefs_ztype ztype;
123843592c46SDamien Le Moal
123943592c46SDamien Le Moal if (!sbi)
124043592c46SDamien Le Moal return;
124143592c46SDamien Le Moal
124243592c46SDamien Le Moal for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
124343592c46SDamien Le Moal if (sbi->s_zgroup[ztype].g_inode) {
124443592c46SDamien Le Moal iput(sbi->s_zgroup[ztype].g_inode);
124543592c46SDamien Le Moal sbi->s_zgroup[ztype].g_inode = NULL;
124643592c46SDamien Le Moal }
124743592c46SDamien Le Moal }
124843592c46SDamien Le Moal }
124943592c46SDamien Le Moal
12508dcc1a9dSDamien Le Moal /*
12518dcc1a9dSDamien Le Moal * Check that the device is zoned. If it is, get the list of zones and create
12528dcc1a9dSDamien Le Moal * sub-directories and files according to the device zone configuration and
12538dcc1a9dSDamien Le Moal * format options.
12548dcc1a9dSDamien Le Moal */
zonefs_fill_super(struct super_block * sb,void * data,int silent)12558dcc1a9dSDamien Le Moal static int zonefs_fill_super(struct super_block *sb, void *data, int silent)
12568dcc1a9dSDamien Le Moal {
12578dcc1a9dSDamien Le Moal struct zonefs_sb_info *sbi;
12588dcc1a9dSDamien Le Moal struct inode *inode;
1259d207794aSDamien Le Moal enum zonefs_ztype ztype;
12608dcc1a9dSDamien Le Moal int ret;
12618dcc1a9dSDamien Le Moal
12628dcc1a9dSDamien Le Moal if (!bdev_is_zoned(sb->s_bdev)) {
12638dcc1a9dSDamien Le Moal zonefs_err(sb, "Not a zoned block device\n");
12648dcc1a9dSDamien Le Moal return -EINVAL;
12658dcc1a9dSDamien Le Moal }
12668dcc1a9dSDamien Le Moal
12678dcc1a9dSDamien Le Moal /*
12688dcc1a9dSDamien Le Moal * Initialize super block information: the maximum file size is updated
12698dcc1a9dSDamien Le Moal * when the zone files are created so that the format option
12708dcc1a9dSDamien Le Moal * ZONEFS_F_AGGRCNV which increases the maximum file size of a file
12718dcc1a9dSDamien Le Moal * beyond the zone size is taken into account.
12728dcc1a9dSDamien Le Moal */
12738dcc1a9dSDamien Le Moal sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
12748dcc1a9dSDamien Le Moal if (!sbi)
12758dcc1a9dSDamien Le Moal return -ENOMEM;
12768dcc1a9dSDamien Le Moal
12778dcc1a9dSDamien Le Moal spin_lock_init(&sbi->s_lock);
12788dcc1a9dSDamien Le Moal sb->s_fs_info = sbi;
12798dcc1a9dSDamien Le Moal sb->s_magic = ZONEFS_MAGIC;
12808dcc1a9dSDamien Le Moal sb->s_maxbytes = 0;
12818dcc1a9dSDamien Le Moal sb->s_op = &zonefs_sops;
12828dcc1a9dSDamien Le Moal sb->s_time_gran = 1;
12838dcc1a9dSDamien Le Moal
12848dcc1a9dSDamien Le Moal /*
12850f1ba5f5SDamien Le Moal * The block size is set to the device zone write granularity to ensure
12860f1ba5f5SDamien Le Moal * that write operations are always aligned according to the device
12870f1ba5f5SDamien Le Moal * interface constraints.
12888dcc1a9dSDamien Le Moal */
12890f1ba5f5SDamien Le Moal sb_set_blocksize(sb, bdev_zone_write_granularity(sb->s_bdev));
12908dcc1a9dSDamien Le Moal sbi->s_zone_sectors_shift = ilog2(bdev_zone_sectors(sb->s_bdev));
12918dcc1a9dSDamien Le Moal sbi->s_uid = GLOBAL_ROOT_UID;
12928dcc1a9dSDamien Le Moal sbi->s_gid = GLOBAL_ROOT_GID;
12938dcc1a9dSDamien Le Moal sbi->s_perm = 0640;
12948dcc1a9dSDamien Le Moal sbi->s_mount_opts = ZONEFS_MNTOPT_ERRORS_RO;
12952b95a23cSDamien Le Moal
12962b95a23cSDamien Le Moal atomic_set(&sbi->s_wro_seq_files, 0);
12972b95a23cSDamien Le Moal sbi->s_max_wro_seq_files = bdev_max_open_zones(sb->s_bdev);
129887c9ce3fSDamien Le Moal atomic_set(&sbi->s_active_seq_files, 0);
129987c9ce3fSDamien Le Moal sbi->s_max_active_seq_files = bdev_max_active_zones(sb->s_bdev);
130087c9ce3fSDamien Le Moal
13018dcc1a9dSDamien Le Moal ret = zonefs_read_super(sb);
13028dcc1a9dSDamien Le Moal if (ret)
13038dcc1a9dSDamien Le Moal return ret;
13048dcc1a9dSDamien Le Moal
13058dcc1a9dSDamien Le Moal ret = zonefs_parse_options(sb, data);
13068dcc1a9dSDamien Le Moal if (ret)
13078dcc1a9dSDamien Le Moal return ret;
13088dcc1a9dSDamien Le Moal
1309b623e347SChristoph Hellwig zonefs_info(sb, "Mounting %u zones", bdev_nr_zones(sb->s_bdev));
13108dcc1a9dSDamien Le Moal
1311a2a513beSDamien Le Moal if (!sbi->s_max_wro_seq_files &&
131296eca145SDamien Le Moal !sbi->s_max_active_seq_files &&
1313a2a513beSDamien Le Moal sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) {
131496eca145SDamien Le Moal zonefs_info(sb,
131596eca145SDamien Le Moal "No open and active zone limits. Ignoring explicit_open mount option\n");
1316a2a513beSDamien Le Moal sbi->s_mount_opts &= ~ZONEFS_MNTOPT_EXPLICIT_OPEN;
1317a2a513beSDamien Le Moal }
1318a2a513beSDamien Le Moal
1319aa7f243fSDamien Le Moal /* Initialize the zone groups */
1320aa7f243fSDamien Le Moal ret = zonefs_init_zgroups(sb);
1321aa7f243fSDamien Le Moal if (ret)
1322aa7f243fSDamien Le Moal goto cleanup;
1323aa7f243fSDamien Le Moal
1324d207794aSDamien Le Moal /* Create the root directory inode */
13258dcc1a9dSDamien Le Moal ret = -ENOMEM;
13268dcc1a9dSDamien Le Moal inode = new_inode(sb);
13278dcc1a9dSDamien Le Moal if (!inode)
13288dcc1a9dSDamien Le Moal goto cleanup;
13298dcc1a9dSDamien Le Moal
1330b623e347SChristoph Hellwig inode->i_ino = bdev_nr_zones(sb->s_bdev);
13318dcc1a9dSDamien Le Moal inode->i_mode = S_IFDIR | 0555;
1332f74207d8SJeff Layton inode->i_mtime = inode->i_atime = inode_set_ctime_current(inode);
13338dcc1a9dSDamien Le Moal inode->i_op = &zonefs_dir_inode_operations;
1334d207794aSDamien Le Moal inode->i_fop = &zonefs_dir_operations;
1335d207794aSDamien Le Moal inode->i_size = 2;
13368dcc1a9dSDamien Le Moal set_nlink(inode, 2);
1337d207794aSDamien Le Moal for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
1338d207794aSDamien Le Moal if (sbi->s_zgroup[ztype].g_nr_zones) {
1339d207794aSDamien Le Moal inc_nlink(inode);
1340d207794aSDamien Le Moal inode->i_size++;
1341d207794aSDamien Le Moal }
1342d207794aSDamien Le Moal }
13438dcc1a9dSDamien Le Moal
13448dcc1a9dSDamien Le Moal sb->s_root = d_make_root(inode);
13458dcc1a9dSDamien Le Moal if (!sb->s_root)
13468dcc1a9dSDamien Le Moal goto cleanup;
13478dcc1a9dSDamien Le Moal
134843592c46SDamien Le Moal /*
134943592c46SDamien Le Moal * Take a reference on the zone groups directory inodes
135043592c46SDamien Le Moal * to keep them in the inode cache.
135143592c46SDamien Le Moal */
135243592c46SDamien Le Moal ret = zonefs_get_zgroup_inodes(sb);
13538dcc1a9dSDamien Le Moal if (ret)
135443592c46SDamien Le Moal goto cleanup;
135543592c46SDamien Le Moal
1356aa7f243fSDamien Le Moal ret = zonefs_sysfs_register(sb);
1357aa7f243fSDamien Le Moal if (ret)
1358aa7f243fSDamien Le Moal goto cleanup;
1359aa7f243fSDamien Le Moal
1360aa7f243fSDamien Le Moal return 0;
13618dcc1a9dSDamien Le Moal
13628dcc1a9dSDamien Le Moal cleanup:
136343592c46SDamien Le Moal zonefs_release_zgroup_inodes(sb);
1364aa7f243fSDamien Le Moal zonefs_free_zgroups(sb);
13658dcc1a9dSDamien Le Moal
13668dcc1a9dSDamien Le Moal return ret;
13678dcc1a9dSDamien Le Moal }
13688dcc1a9dSDamien Le Moal
zonefs_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data)13698dcc1a9dSDamien Le Moal static struct dentry *zonefs_mount(struct file_system_type *fs_type,
13708dcc1a9dSDamien Le Moal int flags, const char *dev_name, void *data)
13718dcc1a9dSDamien Le Moal {
13728dcc1a9dSDamien Le Moal return mount_bdev(fs_type, flags, dev_name, data, zonefs_fill_super);
13738dcc1a9dSDamien Le Moal }
13748dcc1a9dSDamien Le Moal
zonefs_kill_super(struct super_block * sb)13758dcc1a9dSDamien Le Moal static void zonefs_kill_super(struct super_block *sb)
13768dcc1a9dSDamien Le Moal {
13778dcc1a9dSDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
13788dcc1a9dSDamien Le Moal
137943592c46SDamien Le Moal /* Release the reference on the zone group directory inodes */
138043592c46SDamien Le Moal zonefs_release_zgroup_inodes(sb);
138143592c46SDamien Le Moal
1382d207794aSDamien Le Moal kill_block_super(sb);
13839277a6d4SDamien Le Moal
13849277a6d4SDamien Le Moal zonefs_sysfs_unregister(sb);
1385aa7f243fSDamien Le Moal zonefs_free_zgroups(sb);
13868dcc1a9dSDamien Le Moal kfree(sbi);
13878dcc1a9dSDamien Le Moal }
13888dcc1a9dSDamien Le Moal
13898dcc1a9dSDamien Le Moal /*
13908dcc1a9dSDamien Le Moal * File system definition and registration.
13918dcc1a9dSDamien Le Moal */
13928dcc1a9dSDamien Le Moal static struct file_system_type zonefs_type = {
13938dcc1a9dSDamien Le Moal .owner = THIS_MODULE,
13948dcc1a9dSDamien Le Moal .name = "zonefs",
13958dcc1a9dSDamien Le Moal .mount = zonefs_mount,
13968dcc1a9dSDamien Le Moal .kill_sb = zonefs_kill_super,
13978dcc1a9dSDamien Le Moal .fs_flags = FS_REQUIRES_DEV,
13988dcc1a9dSDamien Le Moal };
13998dcc1a9dSDamien Le Moal
zonefs_init_inodecache(void)14008dcc1a9dSDamien Le Moal static int __init zonefs_init_inodecache(void)
14018dcc1a9dSDamien Le Moal {
14028dcc1a9dSDamien Le Moal zonefs_inode_cachep = kmem_cache_create("zonefs_inode_cache",
14038dcc1a9dSDamien Le Moal sizeof(struct zonefs_inode_info), 0,
14048dcc1a9dSDamien Le Moal (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT),
14058dcc1a9dSDamien Le Moal NULL);
14068dcc1a9dSDamien Le Moal if (zonefs_inode_cachep == NULL)
14078dcc1a9dSDamien Le Moal return -ENOMEM;
14088dcc1a9dSDamien Le Moal return 0;
14098dcc1a9dSDamien Le Moal }
14108dcc1a9dSDamien Le Moal
zonefs_destroy_inodecache(void)14118dcc1a9dSDamien Le Moal static void zonefs_destroy_inodecache(void)
14128dcc1a9dSDamien Le Moal {
14138dcc1a9dSDamien Le Moal /*
14148dcc1a9dSDamien Le Moal * Make sure all delayed rcu free inodes are flushed before we
14158dcc1a9dSDamien Le Moal * destroy the inode cache.
14168dcc1a9dSDamien Le Moal */
14178dcc1a9dSDamien Le Moal rcu_barrier();
14188dcc1a9dSDamien Le Moal kmem_cache_destroy(zonefs_inode_cachep);
14198dcc1a9dSDamien Le Moal }
14208dcc1a9dSDamien Le Moal
zonefs_init(void)14218dcc1a9dSDamien Le Moal static int __init zonefs_init(void)
14228dcc1a9dSDamien Le Moal {
14238dcc1a9dSDamien Le Moal int ret;
14248dcc1a9dSDamien Le Moal
14258dcc1a9dSDamien Le Moal BUILD_BUG_ON(sizeof(struct zonefs_super) != ZONEFS_SUPER_SIZE);
14268dcc1a9dSDamien Le Moal
142716d7fd3cSDamien Le Moal ret = zonefs_init_inodecache();
142816d7fd3cSDamien Le Moal if (ret)
1429fe9da61fSDamien Le Moal return ret;
143016d7fd3cSDamien Le Moal
14314e458869SZhang Xiaoxu ret = zonefs_sysfs_init();
14329277a6d4SDamien Le Moal if (ret)
14339277a6d4SDamien Le Moal goto destroy_inodecache;
14349277a6d4SDamien Le Moal
14354e458869SZhang Xiaoxu ret = register_filesystem(&zonefs_type);
14369277a6d4SDamien Le Moal if (ret)
14374e458869SZhang Xiaoxu goto sysfs_exit;
14388dcc1a9dSDamien Le Moal
14398dcc1a9dSDamien Le Moal return 0;
14409277a6d4SDamien Le Moal
14414e458869SZhang Xiaoxu sysfs_exit:
14424e458869SZhang Xiaoxu zonefs_sysfs_exit();
14439277a6d4SDamien Le Moal destroy_inodecache:
14449277a6d4SDamien Le Moal zonefs_destroy_inodecache();
14459277a6d4SDamien Le Moal
14469277a6d4SDamien Le Moal return ret;
14478dcc1a9dSDamien Le Moal }
14488dcc1a9dSDamien Le Moal
zonefs_exit(void)14498dcc1a9dSDamien Le Moal static void __exit zonefs_exit(void)
14508dcc1a9dSDamien Le Moal {
14514e458869SZhang Xiaoxu unregister_filesystem(&zonefs_type);
14529277a6d4SDamien Le Moal zonefs_sysfs_exit();
14538dcc1a9dSDamien Le Moal zonefs_destroy_inodecache();
14548dcc1a9dSDamien Le Moal }
14558dcc1a9dSDamien Le Moal
14568dcc1a9dSDamien Le Moal MODULE_AUTHOR("Damien Le Moal");
14578dcc1a9dSDamien Le Moal MODULE_DESCRIPTION("Zone file system for zoned block devices");
14588dcc1a9dSDamien Le Moal MODULE_LICENSE("GPL");
14598ffea259SNaohiro Aota MODULE_ALIAS_FS("zonefs");
14608dcc1a9dSDamien Le Moal module_init(zonefs_init);
14618dcc1a9dSDamien Le Moal module_exit(zonefs_exit);
1462