xref: /openbmc/linux/fs/zonefs/super.c (revision 0fa5b08cf6e17b0a64ffcc5894d8efe186691ab8)
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  */
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  */
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  */
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  */
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 
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 
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 
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  */
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  */
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 
2498dcc1a9dSDamien Le Moal struct zonefs_ioerr_data {
2508dcc1a9dSDamien Le Moal 	struct inode	*inode;
2518dcc1a9dSDamien Le Moal 	bool		write;
2528dcc1a9dSDamien Le Moal };
2538dcc1a9dSDamien Le Moal 
2548dcc1a9dSDamien Le Moal static int zonefs_io_error_cb(struct blk_zone *zone, unsigned int idx,
2558dcc1a9dSDamien Le Moal 			      void *data)
2568dcc1a9dSDamien Le Moal {
2578dcc1a9dSDamien Le Moal 	struct zonefs_ioerr_data *err = data;
2588dcc1a9dSDamien Le Moal 	struct inode *inode = err->inode;
259aa7f243fSDamien Le Moal 	struct zonefs_zone *z = zonefs_inode_zone(inode);
2608dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
2618dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
2628dcc1a9dSDamien Le Moal 	loff_t isize, data_size;
2638dcc1a9dSDamien Le Moal 
2648dcc1a9dSDamien Le Moal 	/*
2658dcc1a9dSDamien Le Moal 	 * Check the zone condition: if the zone is not "bad" (offline or
2668dcc1a9dSDamien Le Moal 	 * read-only), read errors are simply signaled to the IO issuer as long
2678dcc1a9dSDamien Le Moal 	 * as there is no inconsistency between the inode size and the amount of
2688dcc1a9dSDamien Le Moal 	 * data writen in the zone (data_size).
2698dcc1a9dSDamien Le Moal 	 */
270aa7f243fSDamien Le Moal 	data_size = zonefs_check_zone_condition(sb, z, zone);
2718dcc1a9dSDamien Le Moal 	isize = i_size_read(inode);
272aa7f243fSDamien Le Moal 	if (!(z->z_flags & (ZONEFS_ZONE_READONLY | ZONEFS_ZONE_OFFLINE)) &&
2738dcc1a9dSDamien Le Moal 	    !err->write && isize == data_size)
2748dcc1a9dSDamien Le Moal 		return 0;
2758dcc1a9dSDamien Le Moal 
2768dcc1a9dSDamien Le Moal 	/*
2778dcc1a9dSDamien Le Moal 	 * At this point, we detected either a bad zone or an inconsistency
2788dcc1a9dSDamien Le Moal 	 * between the inode size and the amount of data written in the zone.
2798dcc1a9dSDamien Le Moal 	 * For the latter case, the cause may be a write IO error or an external
2808dcc1a9dSDamien Le Moal 	 * action on the device. Two error patterns exist:
2818dcc1a9dSDamien Le Moal 	 * 1) The inode size is lower than the amount of data in the zone:
2828dcc1a9dSDamien Le Moal 	 *    a write operation partially failed and data was writen at the end
2838dcc1a9dSDamien Le Moal 	 *    of the file. This can happen in the case of a large direct IO
2848dcc1a9dSDamien Le Moal 	 *    needing several BIOs and/or write requests to be processed.
2858dcc1a9dSDamien Le Moal 	 * 2) The inode size is larger than the amount of data in the zone:
2868dcc1a9dSDamien Le Moal 	 *    this can happen with a deferred write error with the use of the
2878dcc1a9dSDamien Le Moal 	 *    device side write cache after getting successful write IO
2888dcc1a9dSDamien Le Moal 	 *    completions. Other possibilities are (a) an external corruption,
2898dcc1a9dSDamien Le Moal 	 *    e.g. an application reset the zone directly, or (b) the device
2908dcc1a9dSDamien Le Moal 	 *    has a serious problem (e.g. firmware bug).
2918dcc1a9dSDamien Le Moal 	 *
2928dcc1a9dSDamien Le Moal 	 * In all cases, warn about inode size inconsistency and handle the
2938dcc1a9dSDamien Le Moal 	 * IO error according to the zone condition and to the mount options.
2948dcc1a9dSDamien Le Moal 	 */
295aa7f243fSDamien Le Moal 	if (zonefs_zone_is_seq(z) && isize != data_size)
296aa7f243fSDamien Le Moal 		zonefs_warn(sb,
297aa7f243fSDamien Le Moal 			    "inode %lu: invalid size %lld (should be %lld)\n",
2988dcc1a9dSDamien Le Moal 			    inode->i_ino, isize, data_size);
2998dcc1a9dSDamien Le Moal 
3008dcc1a9dSDamien Le Moal 	/*
3018dcc1a9dSDamien Le Moal 	 * First handle bad zones signaled by hardware. The mount options
3028dcc1a9dSDamien Le Moal 	 * errors=zone-ro and errors=zone-offline result in changing the
3038dcc1a9dSDamien Le Moal 	 * zone condition to read-only and offline respectively, as if the
3048dcc1a9dSDamien Le Moal 	 * condition was signaled by the hardware.
3058dcc1a9dSDamien Le Moal 	 */
306aa7f243fSDamien Le Moal 	if ((z->z_flags & ZONEFS_ZONE_OFFLINE) ||
30746a9c526SDamien Le Moal 	    (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL)) {
3088dcc1a9dSDamien Le Moal 		zonefs_warn(sb, "inode %lu: read/write access disabled\n",
3098dcc1a9dSDamien Le Moal 			    inode->i_ino);
310aa7f243fSDamien Le Moal 		if (!(z->z_flags & ZONEFS_ZONE_OFFLINE))
311aa7f243fSDamien Le Moal 			z->z_flags |= ZONEFS_ZONE_OFFLINE;
31246a9c526SDamien Le Moal 		zonefs_inode_update_mode(inode);
31346a9c526SDamien Le Moal 		data_size = 0;
314aa7f243fSDamien Le Moal 	} else if ((z->z_flags & ZONEFS_ZONE_READONLY) ||
31546a9c526SDamien Le Moal 		   (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO)) {
3168dcc1a9dSDamien Le Moal 		zonefs_warn(sb, "inode %lu: write access disabled\n",
3178dcc1a9dSDamien Le Moal 			    inode->i_ino);
318aa7f243fSDamien Le Moal 		if (!(z->z_flags & ZONEFS_ZONE_READONLY))
319aa7f243fSDamien Le Moal 			z->z_flags |= ZONEFS_ZONE_READONLY;
32046a9c526SDamien Le Moal 		zonefs_inode_update_mode(inode);
32146a9c526SDamien Le Moal 		data_size = isize;
322a608da3bSDamien Le Moal 	} else if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO &&
323a608da3bSDamien Le Moal 		   data_size > isize) {
324a608da3bSDamien Le Moal 		/* Do not expose garbage data */
325a608da3bSDamien Le Moal 		data_size = isize;
3268dcc1a9dSDamien Le Moal 	}
3278dcc1a9dSDamien Le Moal 
3288dcc1a9dSDamien Le Moal 	/*
329b5c00e97SJohannes Thumshirn 	 * If the filesystem is mounted with the explicit-open mount option, we
330b5c00e97SJohannes Thumshirn 	 * need to clear the ZONEFS_ZONE_OPEN flag if the zone transitioned to
331b5c00e97SJohannes Thumshirn 	 * the read-only or offline condition, to avoid attempting an explicit
332b5c00e97SJohannes Thumshirn 	 * close of the zone when the inode file is closed.
333b5c00e97SJohannes Thumshirn 	 */
334b5c00e97SJohannes Thumshirn 	if ((sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) &&
335aa7f243fSDamien Le Moal 	    (z->z_flags & (ZONEFS_ZONE_READONLY | ZONEFS_ZONE_OFFLINE)))
336aa7f243fSDamien Le Moal 		z->z_flags &= ~ZONEFS_ZONE_OPEN;
337b5c00e97SJohannes Thumshirn 
338b5c00e97SJohannes Thumshirn 	/*
3398dcc1a9dSDamien Le Moal 	 * If error=remount-ro was specified, any error result in remounting
3408dcc1a9dSDamien Le Moal 	 * the volume as read-only.
3418dcc1a9dSDamien Le Moal 	 */
3428dcc1a9dSDamien Le Moal 	if ((sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO) && !sb_rdonly(sb)) {
3438dcc1a9dSDamien Le Moal 		zonefs_warn(sb, "remounting filesystem read-only\n");
3448dcc1a9dSDamien Le Moal 		sb->s_flags |= SB_RDONLY;
3458dcc1a9dSDamien Le Moal 	}
3468dcc1a9dSDamien Le Moal 
3478dcc1a9dSDamien Le Moal 	/*
3488dcc1a9dSDamien Le Moal 	 * Update block usage stats and the inode size  to prevent access to
3498dcc1a9dSDamien Le Moal 	 * invalid data.
3508dcc1a9dSDamien Le Moal 	 */
3518dcc1a9dSDamien Le Moal 	zonefs_update_stats(inode, data_size);
352b5c00e97SJohannes Thumshirn 	zonefs_i_size_write(inode, data_size);
353aa7f243fSDamien Le Moal 	z->z_wpoffset = data_size;
354aa7f243fSDamien Le Moal 	zonefs_inode_account_active(inode);
3558dcc1a9dSDamien Le Moal 
3568dcc1a9dSDamien Le Moal 	return 0;
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  */
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 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
3718dcc1a9dSDamien Le Moal 	unsigned int noio_flag;
3727dd12d65SDamien Le Moal 	unsigned int nr_zones = 1;
3738dcc1a9dSDamien Le Moal 	struct zonefs_ioerr_data err = {
3748dcc1a9dSDamien Le Moal 		.inode = inode,
3758dcc1a9dSDamien Le Moal 		.write = write,
3768dcc1a9dSDamien Le Moal 	};
3778dcc1a9dSDamien Le Moal 	int ret;
3788dcc1a9dSDamien Le Moal 
3798dcc1a9dSDamien Le Moal 	/*
3807dd12d65SDamien Le Moal 	 * The only files that have more than one zone are conventional zone
3817dd12d65SDamien Le Moal 	 * files with aggregated conventional zones, for which the inode zone
3827dd12d65SDamien Le Moal 	 * size is always larger than the device zone size.
3837dd12d65SDamien Le Moal 	 */
384aa7f243fSDamien Le Moal 	if (z->z_size > bdev_zone_sectors(sb->s_bdev))
385aa7f243fSDamien Le Moal 		nr_zones = z->z_size >>
3867dd12d65SDamien Le Moal 			(sbi->s_zone_sectors_shift + SECTOR_SHIFT);
3877dd12d65SDamien Le Moal 
3887dd12d65SDamien Le Moal 	/*
3898dcc1a9dSDamien Le Moal 	 * Memory allocations in blkdev_report_zones() can trigger a memory
3908dcc1a9dSDamien Le Moal 	 * reclaim which may in turn cause a recursion into zonefs as well as
3918dcc1a9dSDamien Le Moal 	 * struct request allocations for the same device. The former case may
3928dcc1a9dSDamien Le Moal 	 * end up in a deadlock on the inode truncate mutex, while the latter
3938dcc1a9dSDamien Le Moal 	 * may prevent IO forward progress. Executing the report zones under
3948dcc1a9dSDamien Le Moal 	 * the GFP_NOIO context avoids both problems.
3958dcc1a9dSDamien Le Moal 	 */
3968dcc1a9dSDamien Le Moal 	noio_flag = memalloc_noio_save();
397aa7f243fSDamien Le Moal 	ret = blkdev_report_zones(sb->s_bdev, z->z_sector, nr_zones,
3988dcc1a9dSDamien Le Moal 				  zonefs_io_error_cb, &err);
3998dcc1a9dSDamien Le Moal 	if (ret != nr_zones)
4008dcc1a9dSDamien Le Moal 		zonefs_err(sb, "Get inode %lu zone information failed %d\n",
4018dcc1a9dSDamien Le Moal 			   inode->i_ino, ret);
4028dcc1a9dSDamien Le Moal 	memalloc_noio_restore(noio_flag);
40348d546a8SJohannes Thumshirn }
4048dcc1a9dSDamien Le Moal 
4058dcc1a9dSDamien Le Moal static struct kmem_cache *zonefs_inode_cachep;
4068dcc1a9dSDamien Le Moal 
4078dcc1a9dSDamien Le Moal static struct inode *zonefs_alloc_inode(struct super_block *sb)
4088dcc1a9dSDamien Le Moal {
4098dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi;
4108dcc1a9dSDamien Le Moal 
411fd60b288SMuchun Song 	zi = alloc_inode_sb(sb, zonefs_inode_cachep, GFP_KERNEL);
4128dcc1a9dSDamien Le Moal 	if (!zi)
4138dcc1a9dSDamien Le Moal 		return NULL;
4148dcc1a9dSDamien Le Moal 
4158dcc1a9dSDamien Le Moal 	inode_init_once(&zi->i_vnode);
4168dcc1a9dSDamien Le Moal 	mutex_init(&zi->i_truncate_mutex);
417b5c00e97SJohannes Thumshirn 	zi->i_wr_refcnt = 0;
4188dcc1a9dSDamien Le Moal 
4198dcc1a9dSDamien Le Moal 	return &zi->i_vnode;
4208dcc1a9dSDamien Le Moal }
4218dcc1a9dSDamien Le Moal 
4228dcc1a9dSDamien Le Moal static void zonefs_free_inode(struct inode *inode)
4238dcc1a9dSDamien Le Moal {
4248dcc1a9dSDamien Le Moal 	kmem_cache_free(zonefs_inode_cachep, ZONEFS_I(inode));
4258dcc1a9dSDamien Le Moal }
4268dcc1a9dSDamien Le Moal 
4278dcc1a9dSDamien Le Moal /*
4288dcc1a9dSDamien Le Moal  * File system stat.
4298dcc1a9dSDamien Le Moal  */
4308dcc1a9dSDamien Le Moal static int zonefs_statfs(struct dentry *dentry, struct kstatfs *buf)
4318dcc1a9dSDamien Le Moal {
4328dcc1a9dSDamien Le Moal 	struct super_block *sb = dentry->d_sb;
4338dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
4348dcc1a9dSDamien Le Moal 	enum zonefs_ztype t;
4358dcc1a9dSDamien Le Moal 
4368dcc1a9dSDamien Le Moal 	buf->f_type = ZONEFS_MAGIC;
4378dcc1a9dSDamien Le Moal 	buf->f_bsize = sb->s_blocksize;
4388dcc1a9dSDamien Le Moal 	buf->f_namelen = ZONEFS_NAME_MAX;
4398dcc1a9dSDamien Le Moal 
4408dcc1a9dSDamien Le Moal 	spin_lock(&sbi->s_lock);
4418dcc1a9dSDamien Le Moal 
4428dcc1a9dSDamien Le Moal 	buf->f_blocks = sbi->s_blocks;
4438dcc1a9dSDamien Le Moal 	if (WARN_ON(sbi->s_used_blocks > sbi->s_blocks))
4448dcc1a9dSDamien Le Moal 		buf->f_bfree = 0;
4458dcc1a9dSDamien Le Moal 	else
4468dcc1a9dSDamien Le Moal 		buf->f_bfree = buf->f_blocks - sbi->s_used_blocks;
4478dcc1a9dSDamien Le Moal 	buf->f_bavail = buf->f_bfree;
4488dcc1a9dSDamien Le Moal 
4498dcc1a9dSDamien Le Moal 	for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) {
450aa7f243fSDamien Le Moal 		if (sbi->s_zgroup[t].g_nr_zones)
451aa7f243fSDamien Le Moal 			buf->f_files += sbi->s_zgroup[t].g_nr_zones + 1;
4528dcc1a9dSDamien Le Moal 	}
4538dcc1a9dSDamien Le Moal 	buf->f_ffree = 0;
4548dcc1a9dSDamien Le Moal 
4558dcc1a9dSDamien Le Moal 	spin_unlock(&sbi->s_lock);
4568dcc1a9dSDamien Le Moal 
4579591c3a3SAmir Goldstein 	buf->f_fsid = uuid_to_fsid(sbi->s_uuid.b);
4588dcc1a9dSDamien Le Moal 
4598dcc1a9dSDamien Le Moal 	return 0;
4608dcc1a9dSDamien Le Moal }
4618dcc1a9dSDamien Le Moal 
4628dcc1a9dSDamien Le Moal enum {
4638dcc1a9dSDamien Le Moal 	Opt_errors_ro, Opt_errors_zro, Opt_errors_zol, Opt_errors_repair,
464b5c00e97SJohannes Thumshirn 	Opt_explicit_open, Opt_err,
4658dcc1a9dSDamien Le Moal };
4668dcc1a9dSDamien Le Moal 
4678dcc1a9dSDamien Le Moal static const match_table_t tokens = {
4688dcc1a9dSDamien Le Moal 	{ Opt_errors_ro,	"errors=remount-ro"},
4698dcc1a9dSDamien Le Moal 	{ Opt_errors_zro,	"errors=zone-ro"},
4708dcc1a9dSDamien Le Moal 	{ Opt_errors_zol,	"errors=zone-offline"},
4718dcc1a9dSDamien Le Moal 	{ Opt_errors_repair,	"errors=repair"},
472b5c00e97SJohannes Thumshirn 	{ Opt_explicit_open,	"explicit-open" },
4738dcc1a9dSDamien Le Moal 	{ Opt_err,		NULL}
4748dcc1a9dSDamien Le Moal };
4758dcc1a9dSDamien Le Moal 
4768dcc1a9dSDamien Le Moal static int zonefs_parse_options(struct super_block *sb, char *options)
4778dcc1a9dSDamien Le Moal {
4788dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
4798dcc1a9dSDamien Le Moal 	substring_t args[MAX_OPT_ARGS];
4808dcc1a9dSDamien Le Moal 	char *p;
4818dcc1a9dSDamien Le Moal 
4828dcc1a9dSDamien Le Moal 	if (!options)
4838dcc1a9dSDamien Le Moal 		return 0;
4848dcc1a9dSDamien Le Moal 
4858dcc1a9dSDamien Le Moal 	while ((p = strsep(&options, ",")) != NULL) {
4868dcc1a9dSDamien Le Moal 		int token;
4878dcc1a9dSDamien Le Moal 
4888dcc1a9dSDamien Le Moal 		if (!*p)
4898dcc1a9dSDamien Le Moal 			continue;
4908dcc1a9dSDamien Le Moal 
4918dcc1a9dSDamien Le Moal 		token = match_token(p, tokens, args);
4928dcc1a9dSDamien Le Moal 		switch (token) {
4938dcc1a9dSDamien Le Moal 		case Opt_errors_ro:
4948dcc1a9dSDamien Le Moal 			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
4958dcc1a9dSDamien Le Moal 			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_RO;
4968dcc1a9dSDamien Le Moal 			break;
4978dcc1a9dSDamien Le Moal 		case Opt_errors_zro:
4988dcc1a9dSDamien Le Moal 			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
4998dcc1a9dSDamien Le Moal 			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZRO;
5008dcc1a9dSDamien Le Moal 			break;
5018dcc1a9dSDamien Le Moal 		case Opt_errors_zol:
5028dcc1a9dSDamien Le Moal 			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
5038dcc1a9dSDamien Le Moal 			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZOL;
5048dcc1a9dSDamien Le Moal 			break;
5058dcc1a9dSDamien Le Moal 		case Opt_errors_repair:
5068dcc1a9dSDamien Le Moal 			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
5078dcc1a9dSDamien Le Moal 			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_REPAIR;
5088dcc1a9dSDamien Le Moal 			break;
509b5c00e97SJohannes Thumshirn 		case Opt_explicit_open:
510b5c00e97SJohannes Thumshirn 			sbi->s_mount_opts |= ZONEFS_MNTOPT_EXPLICIT_OPEN;
511b5c00e97SJohannes Thumshirn 			break;
5128dcc1a9dSDamien Le Moal 		default:
5138dcc1a9dSDamien Le Moal 			return -EINVAL;
5148dcc1a9dSDamien Le Moal 		}
5158dcc1a9dSDamien Le Moal 	}
5168dcc1a9dSDamien Le Moal 
5178dcc1a9dSDamien Le Moal 	return 0;
5188dcc1a9dSDamien Le Moal }
5198dcc1a9dSDamien Le Moal 
5208dcc1a9dSDamien Le Moal static int zonefs_show_options(struct seq_file *seq, struct dentry *root)
5218dcc1a9dSDamien Le Moal {
5228dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(root->d_sb);
5238dcc1a9dSDamien Le Moal 
5248dcc1a9dSDamien Le Moal 	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO)
5258dcc1a9dSDamien Le Moal 		seq_puts(seq, ",errors=remount-ro");
5268dcc1a9dSDamien Le Moal 	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO)
5278dcc1a9dSDamien Le Moal 		seq_puts(seq, ",errors=zone-ro");
5288dcc1a9dSDamien Le Moal 	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL)
5298dcc1a9dSDamien Le Moal 		seq_puts(seq, ",errors=zone-offline");
5308dcc1a9dSDamien Le Moal 	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_REPAIR)
5318dcc1a9dSDamien Le Moal 		seq_puts(seq, ",errors=repair");
5328dcc1a9dSDamien Le Moal 
5338dcc1a9dSDamien Le Moal 	return 0;
5348dcc1a9dSDamien Le Moal }
5358dcc1a9dSDamien Le Moal 
5368dcc1a9dSDamien Le Moal static int zonefs_remount(struct super_block *sb, int *flags, char *data)
5378dcc1a9dSDamien Le Moal {
5388dcc1a9dSDamien Le Moal 	sync_filesystem(sb);
5398dcc1a9dSDamien Le Moal 
5408dcc1a9dSDamien Le Moal 	return zonefs_parse_options(sb, data);
5418dcc1a9dSDamien Le Moal }
5428dcc1a9dSDamien Le Moal 
543232dd599SLinus Torvalds static int zonefs_inode_setattr(struct mnt_idmap *idmap,
5444008e2a0SDamien Le Moal 				struct dentry *dentry, struct iattr *iattr)
5454008e2a0SDamien Le Moal {
5464008e2a0SDamien Le Moal 	struct inode *inode = d_inode(dentry);
5474008e2a0SDamien Le Moal 	int ret;
5488dcc1a9dSDamien Le Moal 
5494008e2a0SDamien Le Moal 	if (unlikely(IS_IMMUTABLE(inode)))
5504008e2a0SDamien Le Moal 		return -EPERM;
5514008e2a0SDamien Le Moal 
552232dd599SLinus Torvalds 	ret = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
5534008e2a0SDamien Le Moal 	if (ret)
5544008e2a0SDamien Le Moal 		return ret;
5554008e2a0SDamien Le Moal 
5564008e2a0SDamien Le Moal 	/*
5574008e2a0SDamien Le Moal 	 * Since files and directories cannot be created nor deleted, do not
5584008e2a0SDamien Le Moal 	 * allow setting any write attributes on the sub-directories grouping
5594008e2a0SDamien Le Moal 	 * files by zone type.
5604008e2a0SDamien Le Moal 	 */
5614008e2a0SDamien Le Moal 	if ((iattr->ia_valid & ATTR_MODE) && S_ISDIR(inode->i_mode) &&
5624008e2a0SDamien Le Moal 	    (iattr->ia_mode & 0222))
5634008e2a0SDamien Le Moal 		return -EPERM;
5644008e2a0SDamien Le Moal 
5654008e2a0SDamien Le Moal 	if (((iattr->ia_valid & ATTR_UID) &&
5664008e2a0SDamien Le Moal 	     !uid_eq(iattr->ia_uid, inode->i_uid)) ||
5674008e2a0SDamien Le Moal 	    ((iattr->ia_valid & ATTR_GID) &&
5684008e2a0SDamien Le Moal 	     !gid_eq(iattr->ia_gid, inode->i_gid))) {
569232dd599SLinus Torvalds 		ret = dquot_transfer(&nop_mnt_idmap, inode, iattr);
5704008e2a0SDamien Le Moal 		if (ret)
5714008e2a0SDamien Le Moal 			return ret;
5724008e2a0SDamien Le Moal 	}
5734008e2a0SDamien Le Moal 
5744008e2a0SDamien Le Moal 	if (iattr->ia_valid & ATTR_SIZE) {
5754008e2a0SDamien Le Moal 		ret = zonefs_file_truncate(inode, iattr->ia_size);
5764008e2a0SDamien Le Moal 		if (ret)
5774008e2a0SDamien Le Moal 			return ret;
5784008e2a0SDamien Le Moal 	}
5794008e2a0SDamien Le Moal 
580232dd599SLinus Torvalds 	setattr_copy(&nop_mnt_idmap, inode, iattr);
5814008e2a0SDamien Le Moal 
582d207794aSDamien Le Moal 	if (S_ISREG(inode->i_mode)) {
583d207794aSDamien Le Moal 		struct zonefs_zone *z = zonefs_inode_zone(inode);
584d207794aSDamien Le Moal 
585d207794aSDamien Le Moal 		z->z_mode = inode->i_mode;
586d207794aSDamien Le Moal 		z->z_uid = inode->i_uid;
587d207794aSDamien Le Moal 		z->z_gid = inode->i_gid;
5884008e2a0SDamien Le Moal 	}
5898dcc1a9dSDamien Le Moal 
590d207794aSDamien Le Moal 	return 0;
5918dcc1a9dSDamien Le Moal }
5928dcc1a9dSDamien Le Moal 
5934008e2a0SDamien Le Moal static const struct inode_operations zonefs_file_inode_operations = {
5948dcc1a9dSDamien Le Moal 	.setattr	= zonefs_inode_setattr,
5958dcc1a9dSDamien Le Moal };
5968dcc1a9dSDamien Le Moal 
597d207794aSDamien Le Moal static long zonefs_fname_to_fno(const struct qstr *fname)
5988dcc1a9dSDamien Le Moal {
599d207794aSDamien Le Moal 	const char *name = fname->name;
600d207794aSDamien Le Moal 	unsigned int len = fname->len;
601d207794aSDamien Le Moal 	long fno = 0, shift = 1;
602d207794aSDamien Le Moal 	const char *rname;
603d207794aSDamien Le Moal 	char c = *name;
604d207794aSDamien Le Moal 	unsigned int i;
6058dcc1a9dSDamien Le Moal 
606d207794aSDamien Le Moal 	/*
607d207794aSDamien Le Moal 	 * File names are always a base-10 number string without any
608d207794aSDamien Le Moal 	 * leading 0s.
609d207794aSDamien Le Moal 	 */
610d207794aSDamien Le Moal 	if (!isdigit(c))
611d207794aSDamien Le Moal 		return -ENOENT;
612d207794aSDamien Le Moal 
613d207794aSDamien Le Moal 	if (len > 1 && c == '0')
614d207794aSDamien Le Moal 		return -ENOENT;
615d207794aSDamien Le Moal 
616d207794aSDamien Le Moal 	if (len == 1)
617d207794aSDamien Le Moal 		return c - '0';
618d207794aSDamien Le Moal 
619d207794aSDamien Le Moal 	for (i = 0, rname = name + len - 1; i < len; i++, rname--) {
620d207794aSDamien Le Moal 		c = *rname;
621d207794aSDamien Le Moal 		if (!isdigit(c))
622d207794aSDamien Le Moal 			return -ENOENT;
623d207794aSDamien Le Moal 		fno += (c - '0') * shift;
624d207794aSDamien Le Moal 		shift *= 10;
6258dcc1a9dSDamien Le Moal 	}
6268dcc1a9dSDamien Le Moal 
627d207794aSDamien Le Moal 	return fno;
628d207794aSDamien Le Moal }
629d207794aSDamien Le Moal 
630d207794aSDamien Le Moal static struct inode *zonefs_get_file_inode(struct inode *dir,
631d207794aSDamien Le Moal 					   struct dentry *dentry)
6328dcc1a9dSDamien Le Moal {
633d207794aSDamien Le Moal 	struct zonefs_zone_group *zgroup = dir->i_private;
634d207794aSDamien Le Moal 	struct super_block *sb = dir->i_sb;
6358dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
636d207794aSDamien Le Moal 	struct zonefs_zone *z;
637d207794aSDamien Le Moal 	struct inode *inode;
638d207794aSDamien Le Moal 	ino_t ino;
639d207794aSDamien Le Moal 	long fno;
6408dcc1a9dSDamien Le Moal 
641d207794aSDamien Le Moal 	/* Get the file number from the file name */
642d207794aSDamien Le Moal 	fno = zonefs_fname_to_fno(&dentry->d_name);
643d207794aSDamien Le Moal 	if (fno < 0)
644d207794aSDamien Le Moal 		return ERR_PTR(fno);
6458dcc1a9dSDamien Le Moal 
646d207794aSDamien Le Moal 	if (!zgroup->g_nr_zones || fno >= zgroup->g_nr_zones)
647d207794aSDamien Le Moal 		return ERR_PTR(-ENOENT);
648d207794aSDamien Le Moal 
649d207794aSDamien Le Moal 	z = &zgroup->g_zones[fno];
650d207794aSDamien Le Moal 	ino = z->z_sector >> sbi->s_zone_sectors_shift;
651d207794aSDamien Le Moal 	inode = iget_locked(sb, ino);
652d207794aSDamien Le Moal 	if (!inode)
653d207794aSDamien Le Moal 		return ERR_PTR(-ENOMEM);
654d207794aSDamien Le Moal 	if (!(inode->i_state & I_NEW)) {
655d207794aSDamien Le Moal 		WARN_ON_ONCE(inode->i_private != z);
656d207794aSDamien Le Moal 		return inode;
6577dd12d65SDamien Le Moal 	}
658e3c3155bSJohannes Thumshirn 
659d207794aSDamien Le Moal 	inode->i_ino = ino;
660d207794aSDamien Le Moal 	inode->i_mode = z->z_mode;
661d207794aSDamien Le Moal 	inode->i_ctime = inode->i_mtime = inode->i_atime = dir->i_ctime;
662d207794aSDamien Le Moal 	inode->i_uid = z->z_uid;
663d207794aSDamien Le Moal 	inode->i_gid = z->z_gid;
664aa7f243fSDamien Le Moal 	inode->i_size = z->z_wpoffset;
665aa7f243fSDamien Le Moal 	inode->i_blocks = z->z_capacity >> SECTOR_SHIFT;
666d207794aSDamien Le Moal 	inode->i_private = z;
6678dcc1a9dSDamien Le Moal 
6688dcc1a9dSDamien Le Moal 	inode->i_op = &zonefs_file_inode_operations;
6698dcc1a9dSDamien Le Moal 	inode->i_fop = &zonefs_file_operations;
6708dcc1a9dSDamien Le Moal 	inode->i_mapping->a_ops = &zonefs_file_aops;
6718dcc1a9dSDamien Le Moal 
67246a9c526SDamien Le Moal 	/* Update the inode access rights depending on the zone condition */
67346a9c526SDamien Le Moal 	zonefs_inode_update_mode(inode);
6741da18a29SDamien Le Moal 
675d207794aSDamien Le Moal 	unlock_new_inode(inode);
676d207794aSDamien Le Moal 
677d207794aSDamien Le Moal 	return inode;
6788dcc1a9dSDamien Le Moal }
6798dcc1a9dSDamien Le Moal 
680d207794aSDamien Le Moal static struct inode *zonefs_get_zgroup_inode(struct super_block *sb,
681aa7f243fSDamien Le Moal 					     enum zonefs_ztype ztype)
6828dcc1a9dSDamien Le Moal {
683d207794aSDamien Le Moal 	struct inode *root = d_inode(sb->s_root);
684d207794aSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
6858dcc1a9dSDamien Le Moal 	struct inode *inode;
686d207794aSDamien Le Moal 	ino_t ino = bdev_nr_zones(sb->s_bdev) + ztype + 1;
6878dcc1a9dSDamien Le Moal 
688d207794aSDamien Le Moal 	inode = iget_locked(sb, ino);
6898dcc1a9dSDamien Le Moal 	if (!inode)
690d207794aSDamien Le Moal 		return ERR_PTR(-ENOMEM);
691d207794aSDamien Le Moal 	if (!(inode->i_state & I_NEW))
692d207794aSDamien Le Moal 		return inode;
6938dcc1a9dSDamien Le Moal 
694d207794aSDamien Le Moal 	inode->i_ino = ino;
695232dd599SLinus Torvalds 	inode_init_owner(&nop_mnt_idmap, inode, root, S_IFDIR | 0555);
696d207794aSDamien Le Moal 	inode->i_size = sbi->s_zgroup[ztype].g_nr_zones;
697d207794aSDamien Le Moal 	inode->i_ctime = inode->i_mtime = inode->i_atime = root->i_ctime;
698d207794aSDamien Le Moal 	inode->i_private = &sbi->s_zgroup[ztype];
699d207794aSDamien Le Moal 	set_nlink(inode, 2);
7001da18a29SDamien Le Moal 
701d207794aSDamien Le Moal 	inode->i_op = &zonefs_dir_inode_operations;
702d207794aSDamien Le Moal 	inode->i_fop = &zonefs_dir_operations;
7038dcc1a9dSDamien Le Moal 
704d207794aSDamien Le Moal 	unlock_new_inode(inode);
7058dcc1a9dSDamien Le Moal 
706d207794aSDamien Le Moal 	return inode;
7078dcc1a9dSDamien Le Moal }
7088dcc1a9dSDamien Le Moal 
709d207794aSDamien Le Moal 
710d207794aSDamien Le Moal static struct inode *zonefs_get_dir_inode(struct inode *dir,
711d207794aSDamien Le Moal 					  struct dentry *dentry)
712d207794aSDamien Le Moal {
713d207794aSDamien Le Moal 	struct super_block *sb = dir->i_sb;
714d207794aSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
715d207794aSDamien Le Moal 	const char *name = dentry->d_name.name;
716d207794aSDamien Le Moal 	enum zonefs_ztype ztype;
7178dcc1a9dSDamien Le Moal 
7188dcc1a9dSDamien Le Moal 	/*
719d207794aSDamien Le Moal 	 * We only need to check for the "seq" directory and
720d207794aSDamien Le Moal 	 * the "cnv" directory if we have conventional zones.
7218dcc1a9dSDamien Le Moal 	 */
722d207794aSDamien Le Moal 	if (dentry->d_name.len != 3)
723d207794aSDamien Le Moal 		return ERR_PTR(-ENOENT);
724d207794aSDamien Le Moal 
725d207794aSDamien Le Moal 	for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
726d207794aSDamien Le Moal 		if (sbi->s_zgroup[ztype].g_nr_zones &&
727d207794aSDamien Le Moal 		    memcmp(name, zonefs_zgroup_name(ztype), 3) == 0)
728d207794aSDamien Le Moal 			break;
729d207794aSDamien Le Moal 	}
730d207794aSDamien Le Moal 	if (ztype == ZONEFS_ZTYPE_MAX)
731d207794aSDamien Le Moal 		return ERR_PTR(-ENOENT);
732d207794aSDamien Le Moal 
733d207794aSDamien Le Moal 	return zonefs_get_zgroup_inode(sb, ztype);
7348dcc1a9dSDamien Le Moal }
7358dcc1a9dSDamien Le Moal 
736d207794aSDamien Le Moal static struct dentry *zonefs_lookup(struct inode *dir, struct dentry *dentry,
737d207794aSDamien Le Moal 				    unsigned int flags)
7388dcc1a9dSDamien Le Moal {
7398dcc1a9dSDamien Le Moal 	struct inode *inode;
7408dcc1a9dSDamien Le Moal 
741d207794aSDamien Le Moal 	if (dentry->d_name.len > ZONEFS_NAME_MAX)
742d207794aSDamien Le Moal 		return ERR_PTR(-ENAMETOOLONG);
7438dcc1a9dSDamien Le Moal 
744d207794aSDamien Le Moal 	if (dir == d_inode(dir->i_sb->s_root))
745d207794aSDamien Le Moal 		inode = zonefs_get_dir_inode(dir, dentry);
746d207794aSDamien Le Moal 	else
747d207794aSDamien Le Moal 		inode = zonefs_get_file_inode(dir, dentry);
748d207794aSDamien Le Moal 	if (IS_ERR(inode))
749d207794aSDamien Le Moal 		return ERR_CAST(inode);
7508dcc1a9dSDamien Le Moal 
751d207794aSDamien Le Moal 	return d_splice_alias(inode, dentry);
7528dcc1a9dSDamien Le Moal }
7538dcc1a9dSDamien Le Moal 
754d207794aSDamien Le Moal static int zonefs_readdir_root(struct file *file, struct dir_context *ctx)
755d207794aSDamien Le Moal {
756d207794aSDamien Le Moal 	struct inode *inode = file_inode(file);
757d207794aSDamien Le Moal 	struct super_block *sb = inode->i_sb;
758d207794aSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
759d207794aSDamien Le Moal 	enum zonefs_ztype ztype = ZONEFS_ZTYPE_CNV;
760d207794aSDamien Le Moal 	ino_t base_ino = bdev_nr_zones(sb->s_bdev) + 1;
7618dcc1a9dSDamien Le Moal 
762d207794aSDamien Le Moal 	if (ctx->pos >= inode->i_size)
763d207794aSDamien Le Moal 		return 0;
7648dcc1a9dSDamien Le Moal 
765d207794aSDamien Le Moal 	if (!dir_emit_dots(file, ctx))
766d207794aSDamien Le Moal 		return 0;
7678dcc1a9dSDamien Le Moal 
768d207794aSDamien Le Moal 	if (ctx->pos == 2) {
769d207794aSDamien Le Moal 		if (!sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones)
770d207794aSDamien Le Moal 			ztype = ZONEFS_ZTYPE_SEQ;
771d207794aSDamien Le Moal 
772d207794aSDamien Le Moal 		if (!dir_emit(ctx, zonefs_zgroup_name(ztype), 3,
773d207794aSDamien Le Moal 			      base_ino + ztype, DT_DIR))
774d207794aSDamien Le Moal 			return 0;
775d207794aSDamien Le Moal 		ctx->pos++;
7768dcc1a9dSDamien Le Moal 	}
7778dcc1a9dSDamien Le Moal 
778d207794aSDamien Le Moal 	if (ctx->pos == 3 && ztype != ZONEFS_ZTYPE_SEQ) {
779d207794aSDamien Le Moal 		ztype = ZONEFS_ZTYPE_SEQ;
780d207794aSDamien Le Moal 		if (!dir_emit(ctx, zonefs_zgroup_name(ztype), 3,
781d207794aSDamien Le Moal 			      base_ino + ztype, DT_DIR))
782d207794aSDamien Le Moal 			return 0;
783d207794aSDamien Le Moal 		ctx->pos++;
784d207794aSDamien Le Moal 	}
785d207794aSDamien Le Moal 
786d207794aSDamien Le Moal 	return 0;
787d207794aSDamien Le Moal }
788d207794aSDamien Le Moal 
789d207794aSDamien Le Moal static int zonefs_readdir_zgroup(struct file *file,
790d207794aSDamien Le Moal 				 struct dir_context *ctx)
791d207794aSDamien Le Moal {
792d207794aSDamien Le Moal 	struct inode *inode = file_inode(file);
793d207794aSDamien Le Moal 	struct zonefs_zone_group *zgroup = inode->i_private;
794d207794aSDamien Le Moal 	struct super_block *sb = inode->i_sb;
795d207794aSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
796d207794aSDamien Le Moal 	struct zonefs_zone *z;
797d207794aSDamien Le Moal 	int fname_len;
798d207794aSDamien Le Moal 	char *fname;
799d207794aSDamien Le Moal 	ino_t ino;
800d207794aSDamien Le Moal 	int f;
801d207794aSDamien Le Moal 
802d207794aSDamien Le Moal 	/*
803d207794aSDamien Le Moal 	 * The size of zone group directories is equal to the number
804d207794aSDamien Le Moal 	 * of zone files in the group and does note include the "." and
805d207794aSDamien Le Moal 	 * ".." entries. Hence the "+ 2" here.
806d207794aSDamien Le Moal 	 */
807d207794aSDamien Le Moal 	if (ctx->pos >= inode->i_size + 2)
808d207794aSDamien Le Moal 		return 0;
809d207794aSDamien Le Moal 
810d207794aSDamien Le Moal 	if (!dir_emit_dots(file, ctx))
811d207794aSDamien Le Moal 		return 0;
812d207794aSDamien Le Moal 
813d207794aSDamien Le Moal 	fname = kmalloc(ZONEFS_NAME_MAX, GFP_KERNEL);
814d207794aSDamien Le Moal 	if (!fname)
815d207794aSDamien Le Moal 		return -ENOMEM;
816d207794aSDamien Le Moal 
817d207794aSDamien Le Moal 	for (f = ctx->pos - 2; f < zgroup->g_nr_zones; f++) {
818d207794aSDamien Le Moal 		z = &zgroup->g_zones[f];
819d207794aSDamien Le Moal 		ino = z->z_sector >> sbi->s_zone_sectors_shift;
820d207794aSDamien Le Moal 		fname_len = snprintf(fname, ZONEFS_NAME_MAX - 1, "%u", f);
821d207794aSDamien Le Moal 		if (!dir_emit(ctx, fname, fname_len, ino, DT_REG))
822d207794aSDamien Le Moal 			break;
823d207794aSDamien Le Moal 		ctx->pos++;
824d207794aSDamien Le Moal 	}
825d207794aSDamien Le Moal 
826d207794aSDamien Le Moal 	kfree(fname);
827d207794aSDamien Le Moal 
828d207794aSDamien Le Moal 	return 0;
829d207794aSDamien Le Moal }
830d207794aSDamien Le Moal 
831d207794aSDamien Le Moal static int zonefs_readdir(struct file *file, struct dir_context *ctx)
832d207794aSDamien Le Moal {
833d207794aSDamien Le Moal 	struct inode *inode = file_inode(file);
834d207794aSDamien Le Moal 
835d207794aSDamien Le Moal 	if (inode == d_inode(inode->i_sb->s_root))
836d207794aSDamien Le Moal 		return zonefs_readdir_root(file, ctx);
837d207794aSDamien Le Moal 
838d207794aSDamien Le Moal 	return zonefs_readdir_zgroup(file, ctx);
839d207794aSDamien Le Moal }
840d207794aSDamien Le Moal 
841d207794aSDamien Le Moal const struct inode_operations zonefs_dir_inode_operations = {
842d207794aSDamien Le Moal 	.lookup		= zonefs_lookup,
843d207794aSDamien Le Moal 	.setattr	= zonefs_inode_setattr,
844d207794aSDamien Le Moal };
845d207794aSDamien Le Moal 
846d207794aSDamien Le Moal const struct file_operations zonefs_dir_operations = {
847d207794aSDamien Le Moal 	.llseek		= generic_file_llseek,
848d207794aSDamien Le Moal 	.read		= generic_read_dir,
849d207794aSDamien Le Moal 	.iterate_shared	= zonefs_readdir,
850d207794aSDamien Le Moal };
851d207794aSDamien Le Moal 
8528dcc1a9dSDamien Le Moal struct zonefs_zone_data {
8538dcc1a9dSDamien Le Moal 	struct super_block	*sb;
8548dcc1a9dSDamien Le Moal 	unsigned int		nr_zones[ZONEFS_ZTYPE_MAX];
855aa7f243fSDamien Le Moal 	sector_t		cnv_zone_start;
8568dcc1a9dSDamien Le Moal 	struct blk_zone		*zones;
8578dcc1a9dSDamien Le Moal };
8588dcc1a9dSDamien Le Moal 
8598dcc1a9dSDamien Le Moal static int zonefs_get_zone_info_cb(struct blk_zone *zone, unsigned int idx,
8608dcc1a9dSDamien Le Moal 				   void *data)
8618dcc1a9dSDamien Le Moal {
8628dcc1a9dSDamien Le Moal 	struct zonefs_zone_data *zd = data;
863aa7f243fSDamien Le Moal 	struct super_block *sb = zd->sb;
864aa7f243fSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
8658dcc1a9dSDamien Le Moal 
8668dcc1a9dSDamien Le Moal 	/*
867aa7f243fSDamien Le Moal 	 * We do not care about the first zone: it contains the super block
868aa7f243fSDamien Le Moal 	 * and not exposed as a file.
869aa7f243fSDamien Le Moal 	 */
870aa7f243fSDamien Le Moal 	if (!idx)
871aa7f243fSDamien Le Moal 		return 0;
872aa7f243fSDamien Le Moal 
873aa7f243fSDamien Le Moal 	/*
874aa7f243fSDamien Le Moal 	 * Count the number of zones that will be exposed as files.
875aa7f243fSDamien Le Moal 	 * For sequential zones, we always have as many files as zones.
876aa7f243fSDamien Le Moal 	 * FOr conventional zones, the number of files depends on if we have
877aa7f243fSDamien Le Moal 	 * conventional zones aggregation enabled.
8788dcc1a9dSDamien Le Moal 	 */
8798dcc1a9dSDamien Le Moal 	switch (zone->type) {
8808dcc1a9dSDamien Le Moal 	case BLK_ZONE_TYPE_CONVENTIONAL:
881aa7f243fSDamien Le Moal 		if (sbi->s_features & ZONEFS_F_AGGRCNV) {
882aa7f243fSDamien Le Moal 			/* One file per set of contiguous conventional zones */
883aa7f243fSDamien Le Moal 			if (!(sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones) ||
884aa7f243fSDamien Le Moal 			    zone->start != zd->cnv_zone_start)
885aa7f243fSDamien Le Moal 				sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones++;
886aa7f243fSDamien Le Moal 			zd->cnv_zone_start = zone->start + zone->len;
887aa7f243fSDamien Le Moal 		} else {
888aa7f243fSDamien Le Moal 			/* One file per zone */
889aa7f243fSDamien Le Moal 			sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones++;
890aa7f243fSDamien Le Moal 		}
8918dcc1a9dSDamien Le Moal 		break;
8928dcc1a9dSDamien Le Moal 	case BLK_ZONE_TYPE_SEQWRITE_REQ:
8938dcc1a9dSDamien Le Moal 	case BLK_ZONE_TYPE_SEQWRITE_PREF:
894aa7f243fSDamien Le Moal 		sbi->s_zgroup[ZONEFS_ZTYPE_SEQ].g_nr_zones++;
8958dcc1a9dSDamien Le Moal 		break;
8968dcc1a9dSDamien Le Moal 	default:
8978dcc1a9dSDamien Le Moal 		zonefs_err(zd->sb, "Unsupported zone type 0x%x\n",
8988dcc1a9dSDamien Le Moal 			   zone->type);
8998dcc1a9dSDamien Le Moal 		return -EIO;
9008dcc1a9dSDamien Le Moal 	}
9018dcc1a9dSDamien Le Moal 
9028dcc1a9dSDamien Le Moal 	memcpy(&zd->zones[idx], zone, sizeof(struct blk_zone));
9038dcc1a9dSDamien Le Moal 
9048dcc1a9dSDamien Le Moal 	return 0;
9058dcc1a9dSDamien Le Moal }
9068dcc1a9dSDamien Le Moal 
9078dcc1a9dSDamien Le Moal static int zonefs_get_zone_info(struct zonefs_zone_data *zd)
9088dcc1a9dSDamien Le Moal {
9098dcc1a9dSDamien Le Moal 	struct block_device *bdev = zd->sb->s_bdev;
9108dcc1a9dSDamien Le Moal 	int ret;
9118dcc1a9dSDamien Le Moal 
912b623e347SChristoph Hellwig 	zd->zones = kvcalloc(bdev_nr_zones(bdev), sizeof(struct blk_zone),
913b623e347SChristoph Hellwig 			     GFP_KERNEL);
9148dcc1a9dSDamien Le Moal 	if (!zd->zones)
9158dcc1a9dSDamien Le Moal 		return -ENOMEM;
9168dcc1a9dSDamien Le Moal 
9178dcc1a9dSDamien Le Moal 	/* Get zones information from the device */
9188dcc1a9dSDamien Le Moal 	ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES,
9198dcc1a9dSDamien Le Moal 				  zonefs_get_zone_info_cb, zd);
9208dcc1a9dSDamien Le Moal 	if (ret < 0) {
9218dcc1a9dSDamien Le Moal 		zonefs_err(zd->sb, "Zone report failed %d\n", ret);
9228dcc1a9dSDamien Le Moal 		return ret;
9238dcc1a9dSDamien Le Moal 	}
9248dcc1a9dSDamien Le Moal 
925b623e347SChristoph Hellwig 	if (ret != bdev_nr_zones(bdev)) {
9268dcc1a9dSDamien Le Moal 		zonefs_err(zd->sb, "Invalid zone report (%d/%u zones)\n",
927b623e347SChristoph Hellwig 			   ret, bdev_nr_zones(bdev));
9288dcc1a9dSDamien Le Moal 		return -EIO;
9298dcc1a9dSDamien Le Moal 	}
9308dcc1a9dSDamien Le Moal 
9318dcc1a9dSDamien Le Moal 	return 0;
9328dcc1a9dSDamien Le Moal }
9338dcc1a9dSDamien Le Moal 
934aa7f243fSDamien Le Moal static inline void zonefs_free_zone_info(struct zonefs_zone_data *zd)
9358dcc1a9dSDamien Le Moal {
9368dcc1a9dSDamien Le Moal 	kvfree(zd->zones);
9378dcc1a9dSDamien Le Moal }
9388dcc1a9dSDamien Le Moal 
9398dcc1a9dSDamien Le Moal /*
940aa7f243fSDamien Le Moal  * Create a zone group and populate it with zone files.
941aa7f243fSDamien Le Moal  */
942aa7f243fSDamien Le Moal static int zonefs_init_zgroup(struct super_block *sb,
943aa7f243fSDamien Le Moal 			      struct zonefs_zone_data *zd,
944aa7f243fSDamien Le Moal 			      enum zonefs_ztype ztype)
945aa7f243fSDamien Le Moal {
946aa7f243fSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
947aa7f243fSDamien Le Moal 	struct zonefs_zone_group *zgroup = &sbi->s_zgroup[ztype];
948aa7f243fSDamien Le Moal 	struct blk_zone *zone, *next, *end;
949aa7f243fSDamien Le Moal 	struct zonefs_zone *z;
950aa7f243fSDamien Le Moal 	unsigned int n = 0;
951aa7f243fSDamien Le Moal 	int ret;
952aa7f243fSDamien Le Moal 
953aa7f243fSDamien Le Moal 	/* Allocate the zone group. If it is empty, we have nothing to do. */
954aa7f243fSDamien Le Moal 	if (!zgroup->g_nr_zones)
955aa7f243fSDamien Le Moal 		return 0;
956aa7f243fSDamien Le Moal 
957aa7f243fSDamien Le Moal 	zgroup->g_zones = kvcalloc(zgroup->g_nr_zones,
958aa7f243fSDamien Le Moal 				   sizeof(struct zonefs_zone), GFP_KERNEL);
959aa7f243fSDamien Le Moal 	if (!zgroup->g_zones)
960aa7f243fSDamien Le Moal 		return -ENOMEM;
961aa7f243fSDamien Le Moal 
962aa7f243fSDamien Le Moal 	/*
963aa7f243fSDamien Le Moal 	 * Initialize the zone groups using the device zone information.
964aa7f243fSDamien Le Moal 	 * We always skip the first zone as it contains the super block
965aa7f243fSDamien Le Moal 	 * and is not use to back a file.
966aa7f243fSDamien Le Moal 	 */
967aa7f243fSDamien Le Moal 	end = zd->zones + bdev_nr_zones(sb->s_bdev);
968aa7f243fSDamien Le Moal 	for (zone = &zd->zones[1]; zone < end; zone = next) {
969aa7f243fSDamien Le Moal 
970aa7f243fSDamien Le Moal 		next = zone + 1;
971aa7f243fSDamien Le Moal 		if (zonefs_zone_type(zone) != ztype)
972aa7f243fSDamien Le Moal 			continue;
973aa7f243fSDamien Le Moal 
974aa7f243fSDamien Le Moal 		if (WARN_ON_ONCE(n >= zgroup->g_nr_zones))
975aa7f243fSDamien Le Moal 			return -EINVAL;
976aa7f243fSDamien Le Moal 
977aa7f243fSDamien Le Moal 		/*
978aa7f243fSDamien Le Moal 		 * For conventional zones, contiguous zones can be aggregated
979aa7f243fSDamien Le Moal 		 * together to form larger files. Note that this overwrites the
980aa7f243fSDamien Le Moal 		 * length of the first zone of the set of contiguous zones
981aa7f243fSDamien Le Moal 		 * aggregated together. If one offline or read-only zone is
982aa7f243fSDamien Le Moal 		 * found, assume that all zones aggregated have the same
983aa7f243fSDamien Le Moal 		 * condition.
984aa7f243fSDamien Le Moal 		 */
985aa7f243fSDamien Le Moal 		if (ztype == ZONEFS_ZTYPE_CNV &&
986aa7f243fSDamien Le Moal 		    (sbi->s_features & ZONEFS_F_AGGRCNV)) {
987aa7f243fSDamien Le Moal 			for (; next < end; next++) {
988aa7f243fSDamien Le Moal 				if (zonefs_zone_type(next) != ztype)
989aa7f243fSDamien Le Moal 					break;
990aa7f243fSDamien Le Moal 				zone->len += next->len;
991aa7f243fSDamien Le Moal 				zone->capacity += next->capacity;
992aa7f243fSDamien Le Moal 				if (next->cond == BLK_ZONE_COND_READONLY &&
993aa7f243fSDamien Le Moal 				    zone->cond != BLK_ZONE_COND_OFFLINE)
994aa7f243fSDamien Le Moal 					zone->cond = BLK_ZONE_COND_READONLY;
995aa7f243fSDamien Le Moal 				else if (next->cond == BLK_ZONE_COND_OFFLINE)
996aa7f243fSDamien Le Moal 					zone->cond = BLK_ZONE_COND_OFFLINE;
997aa7f243fSDamien Le Moal 			}
998aa7f243fSDamien Le Moal 		}
999aa7f243fSDamien Le Moal 
1000aa7f243fSDamien Le Moal 		z = &zgroup->g_zones[n];
1001aa7f243fSDamien Le Moal 		if (ztype == ZONEFS_ZTYPE_CNV)
1002aa7f243fSDamien Le Moal 			z->z_flags |= ZONEFS_ZONE_CNV;
1003aa7f243fSDamien Le Moal 		z->z_sector = zone->start;
1004aa7f243fSDamien Le Moal 		z->z_size = zone->len << SECTOR_SHIFT;
1005aa7f243fSDamien Le Moal 		if (z->z_size > bdev_zone_sectors(sb->s_bdev) << SECTOR_SHIFT &&
1006aa7f243fSDamien Le Moal 		    !(sbi->s_features & ZONEFS_F_AGGRCNV)) {
1007aa7f243fSDamien Le Moal 			zonefs_err(sb,
1008aa7f243fSDamien Le Moal 				"Invalid zone size %llu (device zone sectors %llu)\n",
1009aa7f243fSDamien Le Moal 				z->z_size,
1010aa7f243fSDamien Le Moal 				bdev_zone_sectors(sb->s_bdev) << SECTOR_SHIFT);
1011aa7f243fSDamien Le Moal 			return -EINVAL;
1012aa7f243fSDamien Le Moal 		}
1013aa7f243fSDamien Le Moal 
1014aa7f243fSDamien Le Moal 		z->z_capacity = min_t(loff_t, MAX_LFS_FILESIZE,
1015aa7f243fSDamien Le Moal 				      zone->capacity << SECTOR_SHIFT);
1016aa7f243fSDamien Le Moal 		z->z_wpoffset = zonefs_check_zone_condition(sb, z, zone);
1017aa7f243fSDamien Le Moal 
1018d207794aSDamien Le Moal 		z->z_mode = S_IFREG | sbi->s_perm;
1019d207794aSDamien Le Moal 		z->z_uid = sbi->s_uid;
1020d207794aSDamien Le Moal 		z->z_gid = sbi->s_gid;
1021d207794aSDamien Le Moal 
1022d207794aSDamien Le Moal 		/*
1023d207794aSDamien Le Moal 		 * Let zonefs_inode_update_mode() know that we will need
1024d207794aSDamien Le Moal 		 * special initialization of the inode mode the first time
1025d207794aSDamien Le Moal 		 * it is accessed.
1026d207794aSDamien Le Moal 		 */
1027d207794aSDamien Le Moal 		z->z_flags |= ZONEFS_ZONE_INIT_MODE;
1028d207794aSDamien Le Moal 
1029aa7f243fSDamien Le Moal 		sb->s_maxbytes = max(z->z_capacity, sb->s_maxbytes);
1030aa7f243fSDamien Le Moal 		sbi->s_blocks += z->z_capacity >> sb->s_blocksize_bits;
1031aa7f243fSDamien Le Moal 		sbi->s_used_blocks += z->z_wpoffset >> sb->s_blocksize_bits;
1032aa7f243fSDamien Le Moal 
1033aa7f243fSDamien Le Moal 		/*
1034aa7f243fSDamien Le Moal 		 * For sequential zones, make sure that any open zone is closed
1035aa7f243fSDamien Le Moal 		 * first to ensure that the initial number of open zones is 0,
1036aa7f243fSDamien Le Moal 		 * in sync with the open zone accounting done when the mount
1037aa7f243fSDamien Le Moal 		 * option ZONEFS_MNTOPT_EXPLICIT_OPEN is used.
1038aa7f243fSDamien Le Moal 		 */
1039aa7f243fSDamien Le Moal 		if (ztype == ZONEFS_ZTYPE_SEQ &&
1040aa7f243fSDamien Le Moal 		    (zone->cond == BLK_ZONE_COND_IMP_OPEN ||
1041aa7f243fSDamien Le Moal 		     zone->cond == BLK_ZONE_COND_EXP_OPEN)) {
1042aa7f243fSDamien Le Moal 			ret = zonefs_zone_mgmt(sb, z, REQ_OP_ZONE_CLOSE);
1043aa7f243fSDamien Le Moal 			if (ret)
1044aa7f243fSDamien Le Moal 				return ret;
1045aa7f243fSDamien Le Moal 		}
1046aa7f243fSDamien Le Moal 
1047aa7f243fSDamien Le Moal 		zonefs_account_active(sb, z);
1048aa7f243fSDamien Le Moal 
1049aa7f243fSDamien Le Moal 		n++;
1050aa7f243fSDamien Le Moal 	}
1051aa7f243fSDamien Le Moal 
1052aa7f243fSDamien Le Moal 	if (WARN_ON_ONCE(n != zgroup->g_nr_zones))
1053aa7f243fSDamien Le Moal 		return -EINVAL;
1054aa7f243fSDamien Le Moal 
1055aa7f243fSDamien Le Moal 	zonefs_info(sb, "Zone group \"%s\" has %u file%s\n",
1056aa7f243fSDamien Le Moal 		    zonefs_zgroup_name(ztype),
1057aa7f243fSDamien Le Moal 		    zgroup->g_nr_zones,
1058aa7f243fSDamien Le Moal 		    zgroup->g_nr_zones > 1 ? "s" : "");
1059aa7f243fSDamien Le Moal 
1060aa7f243fSDamien Le Moal 	return 0;
1061aa7f243fSDamien Le Moal }
1062aa7f243fSDamien Le Moal 
1063aa7f243fSDamien Le Moal static void zonefs_free_zgroups(struct super_block *sb)
1064aa7f243fSDamien Le Moal {
1065aa7f243fSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1066aa7f243fSDamien Le Moal 	enum zonefs_ztype ztype;
1067aa7f243fSDamien Le Moal 
1068aa7f243fSDamien Le Moal 	if (!sbi)
1069aa7f243fSDamien Le Moal 		return;
1070aa7f243fSDamien Le Moal 
1071aa7f243fSDamien Le Moal 	for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
1072aa7f243fSDamien Le Moal 		kvfree(sbi->s_zgroup[ztype].g_zones);
1073aa7f243fSDamien Le Moal 		sbi->s_zgroup[ztype].g_zones = NULL;
1074aa7f243fSDamien Le Moal 	}
1075aa7f243fSDamien Le Moal }
1076aa7f243fSDamien Le Moal 
1077aa7f243fSDamien Le Moal /*
1078aa7f243fSDamien Le Moal  * Create a zone group and populate it with zone files.
1079aa7f243fSDamien Le Moal  */
1080aa7f243fSDamien Le Moal static int zonefs_init_zgroups(struct super_block *sb)
1081aa7f243fSDamien Le Moal {
1082aa7f243fSDamien Le Moal 	struct zonefs_zone_data zd;
1083aa7f243fSDamien Le Moal 	enum zonefs_ztype ztype;
1084aa7f243fSDamien Le Moal 	int ret;
1085aa7f243fSDamien Le Moal 
1086aa7f243fSDamien Le Moal 	/* First get the device zone information */
1087aa7f243fSDamien Le Moal 	memset(&zd, 0, sizeof(struct zonefs_zone_data));
1088aa7f243fSDamien Le Moal 	zd.sb = sb;
1089aa7f243fSDamien Le Moal 	ret = zonefs_get_zone_info(&zd);
1090aa7f243fSDamien Le Moal 	if (ret)
1091aa7f243fSDamien Le Moal 		goto cleanup;
1092aa7f243fSDamien Le Moal 
1093aa7f243fSDamien Le Moal 	/* Allocate and initialize the zone groups */
1094aa7f243fSDamien Le Moal 	for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
1095aa7f243fSDamien Le Moal 		ret = zonefs_init_zgroup(sb, &zd, ztype);
1096aa7f243fSDamien Le Moal 		if (ret) {
1097aa7f243fSDamien Le Moal 			zonefs_info(sb,
1098aa7f243fSDamien Le Moal 				    "Zone group \"%s\" initialization failed\n",
1099aa7f243fSDamien Le Moal 				    zonefs_zgroup_name(ztype));
1100aa7f243fSDamien Le Moal 			break;
1101aa7f243fSDamien Le Moal 		}
1102aa7f243fSDamien Le Moal 	}
1103aa7f243fSDamien Le Moal 
1104aa7f243fSDamien Le Moal cleanup:
1105aa7f243fSDamien Le Moal 	zonefs_free_zone_info(&zd);
1106aa7f243fSDamien Le Moal 	if (ret)
1107aa7f243fSDamien Le Moal 		zonefs_free_zgroups(sb);
1108aa7f243fSDamien Le Moal 
1109aa7f243fSDamien Le Moal 	return ret;
1110aa7f243fSDamien Le Moal }
1111aa7f243fSDamien Le Moal 
1112aa7f243fSDamien Le Moal /*
11138dcc1a9dSDamien Le Moal  * Read super block information from the device.
11148dcc1a9dSDamien Le Moal  */
11158dcc1a9dSDamien Le Moal static int zonefs_read_super(struct super_block *sb)
11168dcc1a9dSDamien Le Moal {
11178dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
11188dcc1a9dSDamien Le Moal 	struct zonefs_super *super;
11198dcc1a9dSDamien Le Moal 	u32 crc, stored_crc;
11208dcc1a9dSDamien Le Moal 	struct page *page;
11218dcc1a9dSDamien Le Moal 	struct bio_vec bio_vec;
11228dcc1a9dSDamien Le Moal 	struct bio bio;
11238dcc1a9dSDamien Le Moal 	int ret;
11248dcc1a9dSDamien Le Moal 
11258dcc1a9dSDamien Le Moal 	page = alloc_page(GFP_KERNEL);
11268dcc1a9dSDamien Le Moal 	if (!page)
11278dcc1a9dSDamien Le Moal 		return -ENOMEM;
11288dcc1a9dSDamien Le Moal 
112949add496SChristoph Hellwig 	bio_init(&bio, sb->s_bdev, &bio_vec, 1, REQ_OP_READ);
11308dcc1a9dSDamien Le Moal 	bio.bi_iter.bi_sector = 0;
1131*0fa5b08cSJohannes Thumshirn 	__bio_add_page(&bio, page, PAGE_SIZE, 0);
11328dcc1a9dSDamien Le Moal 
11338dcc1a9dSDamien Le Moal 	ret = submit_bio_wait(&bio);
11348dcc1a9dSDamien Le Moal 	if (ret)
11358dcc1a9dSDamien Le Moal 		goto free_page;
11368dcc1a9dSDamien Le Moal 
11376bac30bbSFabio M. De Francesco 	super = page_address(page);
11388dcc1a9dSDamien Le Moal 
11398dcc1a9dSDamien Le Moal 	ret = -EINVAL;
11408dcc1a9dSDamien Le Moal 	if (le32_to_cpu(super->s_magic) != ZONEFS_MAGIC)
11416bac30bbSFabio M. De Francesco 		goto free_page;
11428dcc1a9dSDamien Le Moal 
11438dcc1a9dSDamien Le Moal 	stored_crc = le32_to_cpu(super->s_crc);
11448dcc1a9dSDamien Le Moal 	super->s_crc = 0;
11458dcc1a9dSDamien Le Moal 	crc = crc32(~0U, (unsigned char *)super, sizeof(struct zonefs_super));
11468dcc1a9dSDamien Le Moal 	if (crc != stored_crc) {
11478dcc1a9dSDamien Le Moal 		zonefs_err(sb, "Invalid checksum (Expected 0x%08x, got 0x%08x)",
11488dcc1a9dSDamien Le Moal 			   crc, stored_crc);
11496bac30bbSFabio M. De Francesco 		goto free_page;
11508dcc1a9dSDamien Le Moal 	}
11518dcc1a9dSDamien Le Moal 
11528dcc1a9dSDamien Le Moal 	sbi->s_features = le64_to_cpu(super->s_features);
11538dcc1a9dSDamien Le Moal 	if (sbi->s_features & ~ZONEFS_F_DEFINED_FEATURES) {
11548dcc1a9dSDamien Le Moal 		zonefs_err(sb, "Unknown features set 0x%llx\n",
11558dcc1a9dSDamien Le Moal 			   sbi->s_features);
11566bac30bbSFabio M. De Francesco 		goto free_page;
11578dcc1a9dSDamien Le Moal 	}
11588dcc1a9dSDamien Le Moal 
11598dcc1a9dSDamien Le Moal 	if (sbi->s_features & ZONEFS_F_UID) {
11608dcc1a9dSDamien Le Moal 		sbi->s_uid = make_kuid(current_user_ns(),
11618dcc1a9dSDamien Le Moal 				       le32_to_cpu(super->s_uid));
11628dcc1a9dSDamien Le Moal 		if (!uid_valid(sbi->s_uid)) {
11638dcc1a9dSDamien Le Moal 			zonefs_err(sb, "Invalid UID feature\n");
11646bac30bbSFabio M. De Francesco 			goto free_page;
11658dcc1a9dSDamien Le Moal 		}
11668dcc1a9dSDamien Le Moal 	}
11678dcc1a9dSDamien Le Moal 
11688dcc1a9dSDamien Le Moal 	if (sbi->s_features & ZONEFS_F_GID) {
11698dcc1a9dSDamien Le Moal 		sbi->s_gid = make_kgid(current_user_ns(),
11708dcc1a9dSDamien Le Moal 				       le32_to_cpu(super->s_gid));
11718dcc1a9dSDamien Le Moal 		if (!gid_valid(sbi->s_gid)) {
11728dcc1a9dSDamien Le Moal 			zonefs_err(sb, "Invalid GID feature\n");
11736bac30bbSFabio M. De Francesco 			goto free_page;
11748dcc1a9dSDamien Le Moal 		}
11758dcc1a9dSDamien Le Moal 	}
11768dcc1a9dSDamien Le Moal 
11778dcc1a9dSDamien Le Moal 	if (sbi->s_features & ZONEFS_F_PERM)
11788dcc1a9dSDamien Le Moal 		sbi->s_perm = le32_to_cpu(super->s_perm);
11798dcc1a9dSDamien Le Moal 
11808dcc1a9dSDamien Le Moal 	if (memchr_inv(super->s_reserved, 0, sizeof(super->s_reserved))) {
11818dcc1a9dSDamien Le Moal 		zonefs_err(sb, "Reserved area is being used\n");
11826bac30bbSFabio M. De Francesco 		goto free_page;
11838dcc1a9dSDamien Le Moal 	}
11848dcc1a9dSDamien Le Moal 
1185568776f9SAndy Shevchenko 	import_uuid(&sbi->s_uuid, super->s_uuid);
11868dcc1a9dSDamien Le Moal 	ret = 0;
11878dcc1a9dSDamien Le Moal 
11888dcc1a9dSDamien Le Moal free_page:
11898dcc1a9dSDamien Le Moal 	__free_page(page);
11908dcc1a9dSDamien Le Moal 
11918dcc1a9dSDamien Le Moal 	return ret;
11928dcc1a9dSDamien Le Moal }
11938dcc1a9dSDamien Le Moal 
11944008e2a0SDamien Le Moal static const struct super_operations zonefs_sops = {
11954008e2a0SDamien Le Moal 	.alloc_inode	= zonefs_alloc_inode,
11964008e2a0SDamien Le Moal 	.free_inode	= zonefs_free_inode,
11974008e2a0SDamien Le Moal 	.statfs		= zonefs_statfs,
11984008e2a0SDamien Le Moal 	.remount_fs	= zonefs_remount,
11994008e2a0SDamien Le Moal 	.show_options	= zonefs_show_options,
12004008e2a0SDamien Le Moal };
12014008e2a0SDamien Le Moal 
120243592c46SDamien Le Moal static int zonefs_get_zgroup_inodes(struct super_block *sb)
120343592c46SDamien Le Moal {
120443592c46SDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
120543592c46SDamien Le Moal 	struct inode *dir_inode;
120643592c46SDamien Le Moal 	enum zonefs_ztype ztype;
120743592c46SDamien Le Moal 
120843592c46SDamien Le Moal 	for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
120943592c46SDamien Le Moal 		if (!sbi->s_zgroup[ztype].g_nr_zones)
121043592c46SDamien Le Moal 			continue;
121143592c46SDamien Le Moal 
121243592c46SDamien Le Moal 		dir_inode = zonefs_get_zgroup_inode(sb, ztype);
121343592c46SDamien Le Moal 		if (IS_ERR(dir_inode))
121443592c46SDamien Le Moal 			return PTR_ERR(dir_inode);
121543592c46SDamien Le Moal 
121643592c46SDamien Le Moal 		sbi->s_zgroup[ztype].g_inode = dir_inode;
121743592c46SDamien Le Moal 	}
121843592c46SDamien Le Moal 
121943592c46SDamien Le Moal 	return 0;
122043592c46SDamien Le Moal }
122143592c46SDamien Le Moal 
122243592c46SDamien Le Moal static void zonefs_release_zgroup_inodes(struct super_block *sb)
122343592c46SDamien Le Moal {
122443592c46SDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
122543592c46SDamien Le Moal 	enum zonefs_ztype ztype;
122643592c46SDamien Le Moal 
122743592c46SDamien Le Moal 	if (!sbi)
122843592c46SDamien Le Moal 		return;
122943592c46SDamien Le Moal 
123043592c46SDamien Le Moal 	for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
123143592c46SDamien Le Moal 		if (sbi->s_zgroup[ztype].g_inode) {
123243592c46SDamien Le Moal 			iput(sbi->s_zgroup[ztype].g_inode);
123343592c46SDamien Le Moal 			sbi->s_zgroup[ztype].g_inode = NULL;
123443592c46SDamien Le Moal 		}
123543592c46SDamien Le Moal 	}
123643592c46SDamien Le Moal }
123743592c46SDamien Le Moal 
12388dcc1a9dSDamien Le Moal /*
12398dcc1a9dSDamien Le Moal  * Check that the device is zoned. If it is, get the list of zones and create
12408dcc1a9dSDamien Le Moal  * sub-directories and files according to the device zone configuration and
12418dcc1a9dSDamien Le Moal  * format options.
12428dcc1a9dSDamien Le Moal  */
12438dcc1a9dSDamien Le Moal static int zonefs_fill_super(struct super_block *sb, void *data, int silent)
12448dcc1a9dSDamien Le Moal {
12458dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi;
12468dcc1a9dSDamien Le Moal 	struct inode *inode;
1247d207794aSDamien Le Moal 	enum zonefs_ztype ztype;
12488dcc1a9dSDamien Le Moal 	int ret;
12498dcc1a9dSDamien Le Moal 
12508dcc1a9dSDamien Le Moal 	if (!bdev_is_zoned(sb->s_bdev)) {
12518dcc1a9dSDamien Le Moal 		zonefs_err(sb, "Not a zoned block device\n");
12528dcc1a9dSDamien Le Moal 		return -EINVAL;
12538dcc1a9dSDamien Le Moal 	}
12548dcc1a9dSDamien Le Moal 
12558dcc1a9dSDamien Le Moal 	/*
12568dcc1a9dSDamien Le Moal 	 * Initialize super block information: the maximum file size is updated
12578dcc1a9dSDamien Le Moal 	 * when the zone files are created so that the format option
12588dcc1a9dSDamien Le Moal 	 * ZONEFS_F_AGGRCNV which increases the maximum file size of a file
12598dcc1a9dSDamien Le Moal 	 * beyond the zone size is taken into account.
12608dcc1a9dSDamien Le Moal 	 */
12618dcc1a9dSDamien Le Moal 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
12628dcc1a9dSDamien Le Moal 	if (!sbi)
12638dcc1a9dSDamien Le Moal 		return -ENOMEM;
12648dcc1a9dSDamien Le Moal 
12658dcc1a9dSDamien Le Moal 	spin_lock_init(&sbi->s_lock);
12668dcc1a9dSDamien Le Moal 	sb->s_fs_info = sbi;
12678dcc1a9dSDamien Le Moal 	sb->s_magic = ZONEFS_MAGIC;
12688dcc1a9dSDamien Le Moal 	sb->s_maxbytes = 0;
12698dcc1a9dSDamien Le Moal 	sb->s_op = &zonefs_sops;
12708dcc1a9dSDamien Le Moal 	sb->s_time_gran	= 1;
12718dcc1a9dSDamien Le Moal 
12728dcc1a9dSDamien Le Moal 	/*
12730f1ba5f5SDamien Le Moal 	 * The block size is set to the device zone write granularity to ensure
12740f1ba5f5SDamien Le Moal 	 * that write operations are always aligned according to the device
12750f1ba5f5SDamien Le Moal 	 * interface constraints.
12768dcc1a9dSDamien Le Moal 	 */
12770f1ba5f5SDamien Le Moal 	sb_set_blocksize(sb, bdev_zone_write_granularity(sb->s_bdev));
12788dcc1a9dSDamien Le Moal 	sbi->s_zone_sectors_shift = ilog2(bdev_zone_sectors(sb->s_bdev));
12798dcc1a9dSDamien Le Moal 	sbi->s_uid = GLOBAL_ROOT_UID;
12808dcc1a9dSDamien Le Moal 	sbi->s_gid = GLOBAL_ROOT_GID;
12818dcc1a9dSDamien Le Moal 	sbi->s_perm = 0640;
12828dcc1a9dSDamien Le Moal 	sbi->s_mount_opts = ZONEFS_MNTOPT_ERRORS_RO;
12832b95a23cSDamien Le Moal 
12842b95a23cSDamien Le Moal 	atomic_set(&sbi->s_wro_seq_files, 0);
12852b95a23cSDamien Le Moal 	sbi->s_max_wro_seq_files = bdev_max_open_zones(sb->s_bdev);
128687c9ce3fSDamien Le Moal 	atomic_set(&sbi->s_active_seq_files, 0);
128787c9ce3fSDamien Le Moal 	sbi->s_max_active_seq_files = bdev_max_active_zones(sb->s_bdev);
128887c9ce3fSDamien Le Moal 
12898dcc1a9dSDamien Le Moal 	ret = zonefs_read_super(sb);
12908dcc1a9dSDamien Le Moal 	if (ret)
12918dcc1a9dSDamien Le Moal 		return ret;
12928dcc1a9dSDamien Le Moal 
12938dcc1a9dSDamien Le Moal 	ret = zonefs_parse_options(sb, data);
12948dcc1a9dSDamien Le Moal 	if (ret)
12958dcc1a9dSDamien Le Moal 		return ret;
12968dcc1a9dSDamien Le Moal 
1297b623e347SChristoph Hellwig 	zonefs_info(sb, "Mounting %u zones", bdev_nr_zones(sb->s_bdev));
12988dcc1a9dSDamien Le Moal 
1299a2a513beSDamien Le Moal 	if (!sbi->s_max_wro_seq_files &&
130096eca145SDamien Le Moal 	    !sbi->s_max_active_seq_files &&
1301a2a513beSDamien Le Moal 	    sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) {
130296eca145SDamien Le Moal 		zonefs_info(sb,
130396eca145SDamien Le Moal 			"No open and active zone limits. Ignoring explicit_open mount option\n");
1304a2a513beSDamien Le Moal 		sbi->s_mount_opts &= ~ZONEFS_MNTOPT_EXPLICIT_OPEN;
1305a2a513beSDamien Le Moal 	}
1306a2a513beSDamien Le Moal 
1307aa7f243fSDamien Le Moal 	/* Initialize the zone groups */
1308aa7f243fSDamien Le Moal 	ret = zonefs_init_zgroups(sb);
1309aa7f243fSDamien Le Moal 	if (ret)
1310aa7f243fSDamien Le Moal 		goto cleanup;
1311aa7f243fSDamien Le Moal 
1312d207794aSDamien Le Moal 	/* Create the root directory inode */
13138dcc1a9dSDamien Le Moal 	ret = -ENOMEM;
13148dcc1a9dSDamien Le Moal 	inode = new_inode(sb);
13158dcc1a9dSDamien Le Moal 	if (!inode)
13168dcc1a9dSDamien Le Moal 		goto cleanup;
13178dcc1a9dSDamien Le Moal 
1318b623e347SChristoph Hellwig 	inode->i_ino = bdev_nr_zones(sb->s_bdev);
13198dcc1a9dSDamien Le Moal 	inode->i_mode = S_IFDIR | 0555;
13208dcc1a9dSDamien Le Moal 	inode->i_ctime = inode->i_mtime = inode->i_atime = current_time(inode);
13218dcc1a9dSDamien Le Moal 	inode->i_op = &zonefs_dir_inode_operations;
1322d207794aSDamien Le Moal 	inode->i_fop = &zonefs_dir_operations;
1323d207794aSDamien Le Moal 	inode->i_size = 2;
13248dcc1a9dSDamien Le Moal 	set_nlink(inode, 2);
1325d207794aSDamien Le Moal 	for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
1326d207794aSDamien Le Moal 		if (sbi->s_zgroup[ztype].g_nr_zones) {
1327d207794aSDamien Le Moal 			inc_nlink(inode);
1328d207794aSDamien Le Moal 			inode->i_size++;
1329d207794aSDamien Le Moal 		}
1330d207794aSDamien Le Moal 	}
13318dcc1a9dSDamien Le Moal 
13328dcc1a9dSDamien Le Moal 	sb->s_root = d_make_root(inode);
13338dcc1a9dSDamien Le Moal 	if (!sb->s_root)
13348dcc1a9dSDamien Le Moal 		goto cleanup;
13358dcc1a9dSDamien Le Moal 
133643592c46SDamien Le Moal 	/*
133743592c46SDamien Le Moal 	 * Take a reference on the zone groups directory inodes
133843592c46SDamien Le Moal 	 * to keep them in the inode cache.
133943592c46SDamien Le Moal 	 */
134043592c46SDamien Le Moal 	ret = zonefs_get_zgroup_inodes(sb);
13418dcc1a9dSDamien Le Moal 	if (ret)
134243592c46SDamien Le Moal 		goto cleanup;
134343592c46SDamien Le Moal 
1344aa7f243fSDamien Le Moal 	ret = zonefs_sysfs_register(sb);
1345aa7f243fSDamien Le Moal 	if (ret)
1346aa7f243fSDamien Le Moal 		goto cleanup;
1347aa7f243fSDamien Le Moal 
1348aa7f243fSDamien Le Moal 	return 0;
13498dcc1a9dSDamien Le Moal 
13508dcc1a9dSDamien Le Moal cleanup:
135143592c46SDamien Le Moal 	zonefs_release_zgroup_inodes(sb);
1352aa7f243fSDamien Le Moal 	zonefs_free_zgroups(sb);
13538dcc1a9dSDamien Le Moal 
13548dcc1a9dSDamien Le Moal 	return ret;
13558dcc1a9dSDamien Le Moal }
13568dcc1a9dSDamien Le Moal 
13578dcc1a9dSDamien Le Moal static struct dentry *zonefs_mount(struct file_system_type *fs_type,
13588dcc1a9dSDamien Le Moal 				   int flags, const char *dev_name, void *data)
13598dcc1a9dSDamien Le Moal {
13608dcc1a9dSDamien Le Moal 	return mount_bdev(fs_type, flags, dev_name, data, zonefs_fill_super);
13618dcc1a9dSDamien Le Moal }
13628dcc1a9dSDamien Le Moal 
13638dcc1a9dSDamien Le Moal static void zonefs_kill_super(struct super_block *sb)
13648dcc1a9dSDamien Le Moal {
13658dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
13668dcc1a9dSDamien Le Moal 
136743592c46SDamien Le Moal 	/* Release the reference on the zone group directory inodes */
136843592c46SDamien Le Moal 	zonefs_release_zgroup_inodes(sb);
136943592c46SDamien Le Moal 
1370d207794aSDamien Le Moal 	kill_block_super(sb);
13719277a6d4SDamien Le Moal 
13729277a6d4SDamien Le Moal 	zonefs_sysfs_unregister(sb);
1373aa7f243fSDamien Le Moal 	zonefs_free_zgroups(sb);
13748dcc1a9dSDamien Le Moal 	kfree(sbi);
13758dcc1a9dSDamien Le Moal }
13768dcc1a9dSDamien Le Moal 
13778dcc1a9dSDamien Le Moal /*
13788dcc1a9dSDamien Le Moal  * File system definition and registration.
13798dcc1a9dSDamien Le Moal  */
13808dcc1a9dSDamien Le Moal static struct file_system_type zonefs_type = {
13818dcc1a9dSDamien Le Moal 	.owner		= THIS_MODULE,
13828dcc1a9dSDamien Le Moal 	.name		= "zonefs",
13838dcc1a9dSDamien Le Moal 	.mount		= zonefs_mount,
13848dcc1a9dSDamien Le Moal 	.kill_sb	= zonefs_kill_super,
13858dcc1a9dSDamien Le Moal 	.fs_flags	= FS_REQUIRES_DEV,
13868dcc1a9dSDamien Le Moal };
13878dcc1a9dSDamien Le Moal 
13888dcc1a9dSDamien Le Moal static int __init zonefs_init_inodecache(void)
13898dcc1a9dSDamien Le Moal {
13908dcc1a9dSDamien Le Moal 	zonefs_inode_cachep = kmem_cache_create("zonefs_inode_cache",
13918dcc1a9dSDamien Le Moal 			sizeof(struct zonefs_inode_info), 0,
13928dcc1a9dSDamien Le Moal 			(SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT),
13938dcc1a9dSDamien Le Moal 			NULL);
13948dcc1a9dSDamien Le Moal 	if (zonefs_inode_cachep == NULL)
13958dcc1a9dSDamien Le Moal 		return -ENOMEM;
13968dcc1a9dSDamien Le Moal 	return 0;
13978dcc1a9dSDamien Le Moal }
13988dcc1a9dSDamien Le Moal 
13998dcc1a9dSDamien Le Moal static void zonefs_destroy_inodecache(void)
14008dcc1a9dSDamien Le Moal {
14018dcc1a9dSDamien Le Moal 	/*
14028dcc1a9dSDamien Le Moal 	 * Make sure all delayed rcu free inodes are flushed before we
14038dcc1a9dSDamien Le Moal 	 * destroy the inode cache.
14048dcc1a9dSDamien Le Moal 	 */
14058dcc1a9dSDamien Le Moal 	rcu_barrier();
14068dcc1a9dSDamien Le Moal 	kmem_cache_destroy(zonefs_inode_cachep);
14078dcc1a9dSDamien Le Moal }
14088dcc1a9dSDamien Le Moal 
14098dcc1a9dSDamien Le Moal static int __init zonefs_init(void)
14108dcc1a9dSDamien Le Moal {
14118dcc1a9dSDamien Le Moal 	int ret;
14128dcc1a9dSDamien Le Moal 
14138dcc1a9dSDamien Le Moal 	BUILD_BUG_ON(sizeof(struct zonefs_super) != ZONEFS_SUPER_SIZE);
14148dcc1a9dSDamien Le Moal 
14158dcc1a9dSDamien Le Moal 	ret = zonefs_init_inodecache();
14168dcc1a9dSDamien Le Moal 	if (ret)
14178dcc1a9dSDamien Le Moal 		return ret;
14188dcc1a9dSDamien Le Moal 
14194e458869SZhang Xiaoxu 	ret = zonefs_sysfs_init();
14209277a6d4SDamien Le Moal 	if (ret)
14219277a6d4SDamien Le Moal 		goto destroy_inodecache;
14229277a6d4SDamien Le Moal 
14234e458869SZhang Xiaoxu 	ret = register_filesystem(&zonefs_type);
14249277a6d4SDamien Le Moal 	if (ret)
14254e458869SZhang Xiaoxu 		goto sysfs_exit;
14268dcc1a9dSDamien Le Moal 
14278dcc1a9dSDamien Le Moal 	return 0;
14289277a6d4SDamien Le Moal 
14294e458869SZhang Xiaoxu sysfs_exit:
14304e458869SZhang Xiaoxu 	zonefs_sysfs_exit();
14319277a6d4SDamien Le Moal destroy_inodecache:
14329277a6d4SDamien Le Moal 	zonefs_destroy_inodecache();
14339277a6d4SDamien Le Moal 
14349277a6d4SDamien Le Moal 	return ret;
14358dcc1a9dSDamien Le Moal }
14368dcc1a9dSDamien Le Moal 
14378dcc1a9dSDamien Le Moal static void __exit zonefs_exit(void)
14388dcc1a9dSDamien Le Moal {
14394e458869SZhang Xiaoxu 	unregister_filesystem(&zonefs_type);
14409277a6d4SDamien Le Moal 	zonefs_sysfs_exit();
14418dcc1a9dSDamien Le Moal 	zonefs_destroy_inodecache();
14428dcc1a9dSDamien Le Moal }
14438dcc1a9dSDamien Le Moal 
14448dcc1a9dSDamien Le Moal MODULE_AUTHOR("Damien Le Moal");
14458dcc1a9dSDamien Le Moal MODULE_DESCRIPTION("Zone file system for zoned block devices");
14468dcc1a9dSDamien Le Moal MODULE_LICENSE("GPL");
14478ffea259SNaohiro Aota MODULE_ALIAS_FS("zonefs");
14488dcc1a9dSDamien Le Moal module_init(zonefs_init);
14498dcc1a9dSDamien Le Moal module_exit(zonefs_exit);
1450