xref: /linux/fs/zonefs/super.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
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/uio.h>
198dcc1a9dSDamien Le Moal #include <linux/mman.h>
208dcc1a9dSDamien Le Moal #include <linux/sched/mm.h>
218dcc1a9dSDamien Le Moal #include <linux/crc32.h>
2202ef12a6SJohannes Thumshirn #include <linux/task_io_accounting_ops.h>
23567e629fSBill O'Donnell #include <linux/fs_parser.h>
24567e629fSBill O'Donnell #include <linux/fs_context.h>
258dcc1a9dSDamien Le Moal 
268dcc1a9dSDamien Le Moal #include "zonefs.h"
278dcc1a9dSDamien Le Moal 
2862ab1aadSJohannes Thumshirn #define CREATE_TRACE_POINTS
2962ab1aadSJohannes Thumshirn #include "trace.h"
3062ab1aadSJohannes Thumshirn 
3187c9ce3fSDamien Le Moal /*
32aa7f243fSDamien Le Moal  * Get the name of a zone group directory.
3387c9ce3fSDamien Le Moal  */
zonefs_zgroup_name(enum zonefs_ztype ztype)34aa7f243fSDamien Le Moal static const char *zonefs_zgroup_name(enum zonefs_ztype ztype)
3587c9ce3fSDamien Le Moal {
36aa7f243fSDamien Le Moal 	switch (ztype) {
37aa7f243fSDamien Le Moal 	case ZONEFS_ZTYPE_CNV:
38aa7f243fSDamien Le Moal 		return "cnv";
39aa7f243fSDamien Le Moal 	case ZONEFS_ZTYPE_SEQ:
40aa7f243fSDamien Le Moal 		return "seq";
41aa7f243fSDamien Le Moal 	default:
42aa7f243fSDamien Le Moal 		WARN_ON_ONCE(1);
43aa7f243fSDamien Le Moal 		return "???";
44aa7f243fSDamien Le Moal 	}
45aa7f243fSDamien Le Moal }
4687c9ce3fSDamien Le Moal 
47aa7f243fSDamien Le Moal /*
48aa7f243fSDamien Le Moal  * Manage the active zone count.
49aa7f243fSDamien Le Moal  */
zonefs_account_active(struct super_block * sb,struct zonefs_zone * z)50aa7f243fSDamien Le Moal static void zonefs_account_active(struct super_block *sb,
51aa7f243fSDamien Le Moal 				  struct zonefs_zone *z)
52aa7f243fSDamien Le Moal {
53aa7f243fSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
5487c9ce3fSDamien Le Moal 
55aa7f243fSDamien Le Moal 	if (zonefs_zone_is_cnv(z))
5687c9ce3fSDamien Le Moal 		return;
5787c9ce3fSDamien Le Moal 
5887c9ce3fSDamien Le Moal 	/*
59db58653cSDamien Le Moal 	 * For zones that transitioned to the offline or readonly condition,
60db58653cSDamien Le Moal 	 * we only need to clear the active state.
61db58653cSDamien Le Moal 	 */
62aa7f243fSDamien Le Moal 	if (z->z_flags & (ZONEFS_ZONE_OFFLINE | ZONEFS_ZONE_READONLY))
63db58653cSDamien Le Moal 		goto out;
64db58653cSDamien Le Moal 
65db58653cSDamien Le Moal 	/*
6687c9ce3fSDamien Le Moal 	 * If the zone is active, that is, if it is explicitly open or
6787c9ce3fSDamien Le Moal 	 * partially written, check if it was already accounted as active.
6887c9ce3fSDamien Le Moal 	 */
69aa7f243fSDamien Le Moal 	if ((z->z_flags & ZONEFS_ZONE_OPEN) ||
70aa7f243fSDamien Le Moal 	    (z->z_wpoffset > 0 && z->z_wpoffset < z->z_capacity)) {
71aa7f243fSDamien Le Moal 		if (!(z->z_flags & ZONEFS_ZONE_ACTIVE)) {
72aa7f243fSDamien Le Moal 			z->z_flags |= ZONEFS_ZONE_ACTIVE;
7387c9ce3fSDamien Le Moal 			atomic_inc(&sbi->s_active_seq_files);
7487c9ce3fSDamien Le Moal 		}
7587c9ce3fSDamien Le Moal 		return;
7687c9ce3fSDamien Le Moal 	}
7787c9ce3fSDamien Le Moal 
78db58653cSDamien Le Moal out:
7987c9ce3fSDamien Le Moal 	/* The zone is not active. If it was, update the active count */
80aa7f243fSDamien Le Moal 	if (z->z_flags & ZONEFS_ZONE_ACTIVE) {
81aa7f243fSDamien Le Moal 		z->z_flags &= ~ZONEFS_ZONE_ACTIVE;
8287c9ce3fSDamien Le Moal 		atomic_dec(&sbi->s_active_seq_files);
8387c9ce3fSDamien Le Moal 	}
8487c9ce3fSDamien Le Moal }
8587c9ce3fSDamien Le Moal 
86aa7f243fSDamien Le Moal /*
87aa7f243fSDamien Le Moal  * Manage the active zone count. Called with zi->i_truncate_mutex held.
88aa7f243fSDamien Le Moal  */
zonefs_inode_account_active(struct inode * inode)89aa7f243fSDamien Le Moal void zonefs_inode_account_active(struct inode *inode)
905498d5f9SJohannes Thumshirn {
91aa7f243fSDamien Le Moal 	lockdep_assert_held(&ZONEFS_I(inode)->i_truncate_mutex);
925498d5f9SJohannes Thumshirn 
93aa7f243fSDamien Le Moal 	return zonefs_account_active(inode->i_sb, zonefs_inode_zone(inode));
94aa7f243fSDamien Le Moal }
95aa7f243fSDamien Le Moal 
96aa7f243fSDamien Le Moal /*
97aa7f243fSDamien Le Moal  * Execute a zone management operation.
98aa7f243fSDamien Le Moal  */
zonefs_zone_mgmt(struct super_block * sb,struct zonefs_zone * z,enum req_op op)99aa7f243fSDamien Le Moal static int zonefs_zone_mgmt(struct super_block *sb,
100aa7f243fSDamien Le Moal 			    struct zonefs_zone *z, enum req_op op)
101aa7f243fSDamien Le Moal {
102aa7f243fSDamien Le Moal 	int ret;
1035498d5f9SJohannes Thumshirn 
1041da18a29SDamien Le Moal 	/*
1051da18a29SDamien Le Moal 	 * With ZNS drives, closing an explicitly open zone that has not been
1061da18a29SDamien Le Moal 	 * written will change the zone state to "closed", that is, the zone
1071da18a29SDamien Le Moal 	 * will remain active. Since this can then cause failure of explicit
1081da18a29SDamien Le Moal 	 * open operation on other zones if the drive active zone resources
1091da18a29SDamien Le Moal 	 * are exceeded, make sure that the zone does not remain active by
1101da18a29SDamien Le Moal 	 * resetting it.
1111da18a29SDamien Le Moal 	 */
112aa7f243fSDamien Le Moal 	if (op == REQ_OP_ZONE_CLOSE && !z->z_wpoffset)
1131da18a29SDamien Le Moal 		op = REQ_OP_ZONE_RESET;
1141da18a29SDamien Le Moal 
115aa7f243fSDamien Le Moal 	trace_zonefs_zone_mgmt(sb, z, op);
116aa7f243fSDamien Le Moal 	ret = blkdev_zone_mgmt(sb->s_bdev, op, z->z_sector,
11771f4ecdbSJohannes Thumshirn 			       z->z_size >> SECTOR_SHIFT);
1185498d5f9SJohannes Thumshirn 	if (ret) {
119aa7f243fSDamien Le Moal 		zonefs_err(sb,
1205498d5f9SJohannes Thumshirn 			   "Zone management operation %s at %llu failed %d\n",
121aa7f243fSDamien Le Moal 			   blk_op_str(op), z->z_sector, ret);
1225498d5f9SJohannes Thumshirn 		return ret;
1235498d5f9SJohannes Thumshirn 	}
1245498d5f9SJohannes Thumshirn 
1255498d5f9SJohannes Thumshirn 	return 0;
1265498d5f9SJohannes Thumshirn }
1275498d5f9SJohannes Thumshirn 
zonefs_inode_zone_mgmt(struct inode * inode,enum req_op op)128aa7f243fSDamien Le Moal int zonefs_inode_zone_mgmt(struct inode *inode, enum req_op op)
129b5c00e97SJohannes Thumshirn {
130aa7f243fSDamien Le Moal 	lockdep_assert_held(&ZONEFS_I(inode)->i_truncate_mutex);
131aa7f243fSDamien Le Moal 
132aa7f243fSDamien Le Moal 	return zonefs_zone_mgmt(inode->i_sb, zonefs_inode_zone(inode), op);
133aa7f243fSDamien Le Moal }
134aa7f243fSDamien Le Moal 
zonefs_i_size_write(struct inode * inode,loff_t isize)1354008e2a0SDamien Le Moal void zonefs_i_size_write(struct inode *inode, loff_t isize)
136b5c00e97SJohannes Thumshirn {
137aa7f243fSDamien Le Moal 	struct zonefs_zone *z = zonefs_inode_zone(inode);
138b5c00e97SJohannes Thumshirn 
139b5c00e97SJohannes Thumshirn 	i_size_write(inode, isize);
140aa7f243fSDamien Le Moal 
141b5c00e97SJohannes Thumshirn 	/*
142b5c00e97SJohannes Thumshirn 	 * A full zone is no longer open/active and does not need
143b5c00e97SJohannes Thumshirn 	 * explicit closing.
144b5c00e97SJohannes Thumshirn 	 */
145aa7f243fSDamien Le Moal 	if (isize >= z->z_capacity) {
14687c9ce3fSDamien Le Moal 		struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
14787c9ce3fSDamien Le Moal 
148aa7f243fSDamien Le Moal 		if (z->z_flags & ZONEFS_ZONE_ACTIVE)
14987c9ce3fSDamien Le Moal 			atomic_dec(&sbi->s_active_seq_files);
150aa7f243fSDamien Le Moal 		z->z_flags &= ~(ZONEFS_ZONE_OPEN | ZONEFS_ZONE_ACTIVE);
15187c9ce3fSDamien Le Moal 	}
152b5c00e97SJohannes Thumshirn }
153b5c00e97SJohannes Thumshirn 
zonefs_update_stats(struct inode * inode,loff_t new_isize)1544008e2a0SDamien Le Moal void zonefs_update_stats(struct inode *inode, loff_t new_isize)
1558dcc1a9dSDamien Le Moal {
1568dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
1578dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1588dcc1a9dSDamien Le Moal 	loff_t old_isize = i_size_read(inode);
1598dcc1a9dSDamien Le Moal 	loff_t nr_blocks;
1608dcc1a9dSDamien Le Moal 
1618dcc1a9dSDamien Le Moal 	if (new_isize == old_isize)
1628dcc1a9dSDamien Le Moal 		return;
1638dcc1a9dSDamien Le Moal 
1648dcc1a9dSDamien Le Moal 	spin_lock(&sbi->s_lock);
1658dcc1a9dSDamien Le Moal 
1668dcc1a9dSDamien Le Moal 	/*
1678dcc1a9dSDamien Le Moal 	 * This may be called for an update after an IO error.
1688dcc1a9dSDamien Le Moal 	 * So beware of the values seen.
1698dcc1a9dSDamien Le Moal 	 */
1708dcc1a9dSDamien Le Moal 	if (new_isize < old_isize) {
1718dcc1a9dSDamien Le Moal 		nr_blocks = (old_isize - new_isize) >> sb->s_blocksize_bits;
1728dcc1a9dSDamien Le Moal 		if (sbi->s_used_blocks > nr_blocks)
1738dcc1a9dSDamien Le Moal 			sbi->s_used_blocks -= nr_blocks;
1748dcc1a9dSDamien Le Moal 		else
1758dcc1a9dSDamien Le Moal 			sbi->s_used_blocks = 0;
1768dcc1a9dSDamien Le Moal 	} else {
1778dcc1a9dSDamien Le Moal 		sbi->s_used_blocks +=
1788dcc1a9dSDamien Le Moal 			(new_isize - old_isize) >> sb->s_blocksize_bits;
1798dcc1a9dSDamien Le Moal 		if (sbi->s_used_blocks > sbi->s_blocks)
1808dcc1a9dSDamien Le Moal 			sbi->s_used_blocks = sbi->s_blocks;
1818dcc1a9dSDamien Le Moal 	}
1828dcc1a9dSDamien Le Moal 
1838dcc1a9dSDamien Le Moal 	spin_unlock(&sbi->s_lock);
1848dcc1a9dSDamien Le Moal }
1858dcc1a9dSDamien Le Moal 
1868dcc1a9dSDamien Le Moal /*
187aa7f243fSDamien Le Moal  * Check a zone condition. Return the amount of written (and still readable)
188aa7f243fSDamien Le Moal  * data in the zone.
1898dcc1a9dSDamien Le Moal  */
zonefs_check_zone_condition(struct super_block * sb,struct zonefs_zone * z,struct blk_zone * zone)190aa7f243fSDamien Le Moal static loff_t zonefs_check_zone_condition(struct super_block *sb,
191aa7f243fSDamien Le Moal 					  struct zonefs_zone *z,
19246a9c526SDamien Le Moal 					  struct blk_zone *zone)
1938dcc1a9dSDamien Le Moal {
1948dcc1a9dSDamien Le Moal 	switch (zone->cond) {
1958dcc1a9dSDamien Le Moal 	case BLK_ZONE_COND_OFFLINE:
196aa7f243fSDamien Le Moal 		zonefs_warn(sb, "Zone %llu: offline zone\n",
197aa7f243fSDamien Le Moal 			    z->z_sector);
198aa7f243fSDamien Le Moal 		z->z_flags |= ZONEFS_ZONE_OFFLINE;
1998dcc1a9dSDamien Le Moal 		return 0;
2008dcc1a9dSDamien Le Moal 	case BLK_ZONE_COND_READONLY:
201ccf4ad7dSDamien Le Moal 		/*
20246a9c526SDamien Le Moal 		 * The write pointer of read-only zones is invalid, so we cannot
20346a9c526SDamien Le Moal 		 * determine the zone wpoffset (inode size). We thus keep the
20446a9c526SDamien Le Moal 		 * zone wpoffset as is, which leads to an empty file
20546a9c526SDamien Le Moal 		 * (wpoffset == 0) on mount. For a runtime error, this keeps
20646a9c526SDamien Le Moal 		 * the inode size as it was when last updated so that the user
20746a9c526SDamien Le Moal 		 * can recover data.
208ccf4ad7dSDamien Le Moal 		 */
209aa7f243fSDamien Le Moal 		zonefs_warn(sb, "Zone %llu: read-only zone\n",
210aa7f243fSDamien Le Moal 			    z->z_sector);
211aa7f243fSDamien Le Moal 		z->z_flags |= ZONEFS_ZONE_READONLY;
212aa7f243fSDamien Le Moal 		if (zonefs_zone_is_cnv(z))
213aa7f243fSDamien Le Moal 			return z->z_capacity;
214aa7f243fSDamien Le Moal 		return z->z_wpoffset;
215059c0103SShin'ichiro Kawasaki 	case BLK_ZONE_COND_FULL:
216059c0103SShin'ichiro Kawasaki 		/* The write pointer of full zones is invalid. */
217aa7f243fSDamien Le Moal 		return z->z_capacity;
2188dcc1a9dSDamien Le Moal 	default:
219aa7f243fSDamien Le Moal 		if (zonefs_zone_is_cnv(z))
220aa7f243fSDamien Le Moal 			return z->z_capacity;
2218dcc1a9dSDamien Le Moal 		return (zone->wp - zone->start) << SECTOR_SHIFT;
2228dcc1a9dSDamien Le Moal 	}
2238dcc1a9dSDamien Le Moal }
2248dcc1a9dSDamien Le Moal 
22546a9c526SDamien Le Moal /*
22646a9c526SDamien Le Moal  * Check a zone condition and adjust its inode access permissions for
22746a9c526SDamien Le Moal  * offline and readonly zones.
22846a9c526SDamien Le Moal  */
zonefs_inode_update_mode(struct inode * inode)22946a9c526SDamien Le Moal static void zonefs_inode_update_mode(struct inode *inode)
23046a9c526SDamien Le Moal {
231aa7f243fSDamien Le Moal 	struct zonefs_zone *z = zonefs_inode_zone(inode);
23246a9c526SDamien Le Moal 
233aa7f243fSDamien Le Moal 	if (z->z_flags & ZONEFS_ZONE_OFFLINE) {
23446a9c526SDamien Le Moal 		/* Offline zones cannot be read nor written */
23546a9c526SDamien Le Moal 		inode->i_flags |= S_IMMUTABLE;
23646a9c526SDamien Le Moal 		inode->i_mode &= ~0777;
237aa7f243fSDamien Le Moal 	} else if (z->z_flags & ZONEFS_ZONE_READONLY) {
23846a9c526SDamien Le Moal 		/* Readonly zones cannot be written */
23946a9c526SDamien Le Moal 		inode->i_flags |= S_IMMUTABLE;
240aa7f243fSDamien Le Moal 		if (z->z_flags & ZONEFS_ZONE_INIT_MODE)
24146a9c526SDamien Le Moal 			inode->i_mode &= ~0777;
24246a9c526SDamien Le Moal 		else
24346a9c526SDamien Le Moal 			inode->i_mode &= ~0222;
24446a9c526SDamien Le Moal 	}
24546a9c526SDamien Le Moal 
246aa7f243fSDamien Le Moal 	z->z_flags &= ~ZONEFS_ZONE_INIT_MODE;
247d207794aSDamien Le Moal 	z->z_mode = inode->i_mode;
24846a9c526SDamien Le Moal }
24946a9c526SDamien Le Moal 
zonefs_io_error_cb(struct blk_zone * zone,unsigned int idx,void * data)2508dcc1a9dSDamien Le Moal static int zonefs_io_error_cb(struct blk_zone *zone, unsigned int idx,
2518dcc1a9dSDamien Le Moal 			      void *data)
2528dcc1a9dSDamien Le Moal {
25314db5f64SDamien Le Moal 	struct blk_zone *z = data;
25414db5f64SDamien Le Moal 
25514db5f64SDamien Le Moal 	*z = *zone;
25614db5f64SDamien Le Moal 	return 0;
25714db5f64SDamien Le Moal }
25814db5f64SDamien Le Moal 
zonefs_handle_io_error(struct inode * inode,struct blk_zone * zone,bool write)25914db5f64SDamien Le Moal static void zonefs_handle_io_error(struct inode *inode, struct blk_zone *zone,
26014db5f64SDamien Le Moal 				   bool write)
26114db5f64SDamien Le Moal {
262aa7f243fSDamien Le Moal 	struct zonefs_zone *z = zonefs_inode_zone(inode);
2638dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
2648dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
2658dcc1a9dSDamien Le Moal 	loff_t isize, data_size;
2668dcc1a9dSDamien Le Moal 
2678dcc1a9dSDamien Le Moal 	/*
2688dcc1a9dSDamien Le Moal 	 * Check the zone condition: if the zone is not "bad" (offline or
2698dcc1a9dSDamien Le Moal 	 * read-only), read errors are simply signaled to the IO issuer as long
2708dcc1a9dSDamien Le Moal 	 * as there is no inconsistency between the inode size and the amount of
2718dcc1a9dSDamien Le Moal 	 * data writen in the zone (data_size).
2728dcc1a9dSDamien Le Moal 	 */
273aa7f243fSDamien Le Moal 	data_size = zonefs_check_zone_condition(sb, z, zone);
2748dcc1a9dSDamien Le Moal 	isize = i_size_read(inode);
275aa7f243fSDamien Le Moal 	if (!(z->z_flags & (ZONEFS_ZONE_READONLY | ZONEFS_ZONE_OFFLINE)) &&
27614db5f64SDamien Le Moal 	    !write && isize == data_size)
27714db5f64SDamien Le Moal 		return;
2788dcc1a9dSDamien Le Moal 
2798dcc1a9dSDamien Le Moal 	/*
2808dcc1a9dSDamien Le Moal 	 * At this point, we detected either a bad zone or an inconsistency
2818dcc1a9dSDamien Le Moal 	 * between the inode size and the amount of data written in the zone.
2828dcc1a9dSDamien Le Moal 	 * For the latter case, the cause may be a write IO error or an external
2838dcc1a9dSDamien Le Moal 	 * action on the device. Two error patterns exist:
2848dcc1a9dSDamien Le Moal 	 * 1) The inode size is lower than the amount of data in the zone:
2858dcc1a9dSDamien Le Moal 	 *    a write operation partially failed and data was writen at the end
2868dcc1a9dSDamien Le Moal 	 *    of the file. This can happen in the case of a large direct IO
2878dcc1a9dSDamien Le Moal 	 *    needing several BIOs and/or write requests to be processed.
2888dcc1a9dSDamien Le Moal 	 * 2) The inode size is larger than the amount of data in the zone:
2898dcc1a9dSDamien Le Moal 	 *    this can happen with a deferred write error with the use of the
2908dcc1a9dSDamien Le Moal 	 *    device side write cache after getting successful write IO
2918dcc1a9dSDamien Le Moal 	 *    completions. Other possibilities are (a) an external corruption,
2928dcc1a9dSDamien Le Moal 	 *    e.g. an application reset the zone directly, or (b) the device
2938dcc1a9dSDamien Le Moal 	 *    has a serious problem (e.g. firmware bug).
2948dcc1a9dSDamien Le Moal 	 *
2958dcc1a9dSDamien Le Moal 	 * In all cases, warn about inode size inconsistency and handle the
2968dcc1a9dSDamien Le Moal 	 * IO error according to the zone condition and to the mount options.
2978dcc1a9dSDamien Le Moal 	 */
29814db5f64SDamien Le Moal 	if (isize != data_size)
299aa7f243fSDamien Le Moal 		zonefs_warn(sb,
300aa7f243fSDamien Le Moal 			    "inode %lu: invalid size %lld (should be %lld)\n",
3018dcc1a9dSDamien Le Moal 			    inode->i_ino, isize, data_size);
3028dcc1a9dSDamien Le Moal 
3038dcc1a9dSDamien Le Moal 	/*
3048dcc1a9dSDamien Le Moal 	 * First handle bad zones signaled by hardware. The mount options
3058dcc1a9dSDamien Le Moal 	 * errors=zone-ro and errors=zone-offline result in changing the
3068dcc1a9dSDamien Le Moal 	 * zone condition to read-only and offline respectively, as if the
3078dcc1a9dSDamien Le Moal 	 * condition was signaled by the hardware.
3088dcc1a9dSDamien Le Moal 	 */
309aa7f243fSDamien Le Moal 	if ((z->z_flags & ZONEFS_ZONE_OFFLINE) ||
31046a9c526SDamien Le Moal 	    (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL)) {
3118dcc1a9dSDamien Le Moal 		zonefs_warn(sb, "inode %lu: read/write access disabled\n",
3128dcc1a9dSDamien Le Moal 			    inode->i_ino);
313aa7f243fSDamien Le Moal 		if (!(z->z_flags & ZONEFS_ZONE_OFFLINE))
314aa7f243fSDamien Le Moal 			z->z_flags |= ZONEFS_ZONE_OFFLINE;
31546a9c526SDamien Le Moal 		zonefs_inode_update_mode(inode);
31646a9c526SDamien Le Moal 		data_size = 0;
317aa7f243fSDamien Le Moal 	} else if ((z->z_flags & ZONEFS_ZONE_READONLY) ||
31846a9c526SDamien Le Moal 		   (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO)) {
3198dcc1a9dSDamien Le Moal 		zonefs_warn(sb, "inode %lu: write access disabled\n",
3208dcc1a9dSDamien Le Moal 			    inode->i_ino);
321aa7f243fSDamien Le Moal 		if (!(z->z_flags & ZONEFS_ZONE_READONLY))
322aa7f243fSDamien Le Moal 			z->z_flags |= ZONEFS_ZONE_READONLY;
32346a9c526SDamien Le Moal 		zonefs_inode_update_mode(inode);
32446a9c526SDamien Le Moal 		data_size = isize;
325a608da3bSDamien Le Moal 	} else if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO &&
326a608da3bSDamien Le Moal 		   data_size > isize) {
327a608da3bSDamien Le Moal 		/* Do not expose garbage data */
328a608da3bSDamien Le Moal 		data_size = isize;
3298dcc1a9dSDamien Le Moal 	}
3308dcc1a9dSDamien Le Moal 
3318dcc1a9dSDamien Le Moal 	/*
332b5c00e97SJohannes Thumshirn 	 * If the filesystem is mounted with the explicit-open mount option, we
333b5c00e97SJohannes Thumshirn 	 * need to clear the ZONEFS_ZONE_OPEN flag if the zone transitioned to
334b5c00e97SJohannes Thumshirn 	 * the read-only or offline condition, to avoid attempting an explicit
335b5c00e97SJohannes Thumshirn 	 * close of the zone when the inode file is closed.
336b5c00e97SJohannes Thumshirn 	 */
337b5c00e97SJohannes Thumshirn 	if ((sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) &&
338aa7f243fSDamien Le Moal 	    (z->z_flags & (ZONEFS_ZONE_READONLY | ZONEFS_ZONE_OFFLINE)))
339aa7f243fSDamien Le Moal 		z->z_flags &= ~ZONEFS_ZONE_OPEN;
340b5c00e97SJohannes Thumshirn 
341b5c00e97SJohannes Thumshirn 	/*
3428dcc1a9dSDamien Le Moal 	 * If error=remount-ro was specified, any error result in remounting
3438dcc1a9dSDamien Le Moal 	 * the volume as read-only.
3448dcc1a9dSDamien Le Moal 	 */
3458dcc1a9dSDamien Le Moal 	if ((sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO) && !sb_rdonly(sb)) {
3468dcc1a9dSDamien Le Moal 		zonefs_warn(sb, "remounting filesystem read-only\n");
3478dcc1a9dSDamien Le Moal 		sb->s_flags |= SB_RDONLY;
3488dcc1a9dSDamien Le Moal 	}
3498dcc1a9dSDamien Le Moal 
3508dcc1a9dSDamien Le Moal 	/*
3518dcc1a9dSDamien Le Moal 	 * Update block usage stats and the inode size  to prevent access to
3528dcc1a9dSDamien Le Moal 	 * invalid data.
3538dcc1a9dSDamien Le Moal 	 */
3548dcc1a9dSDamien Le Moal 	zonefs_update_stats(inode, data_size);
355b5c00e97SJohannes Thumshirn 	zonefs_i_size_write(inode, data_size);
356aa7f243fSDamien Le Moal 	z->z_wpoffset = data_size;
357aa7f243fSDamien Le Moal 	zonefs_inode_account_active(inode);
3588dcc1a9dSDamien Le Moal }
3598dcc1a9dSDamien Le Moal 
3608dcc1a9dSDamien Le Moal /*
3618dcc1a9dSDamien Le Moal  * When an file IO error occurs, check the file zone to see if there is a change
3628dcc1a9dSDamien Le Moal  * in the zone condition (e.g. offline or read-only). For a failed write to a
3638dcc1a9dSDamien Le Moal  * sequential zone, the zone write pointer position must also be checked to
3648dcc1a9dSDamien Le Moal  * eventually correct the file size and zonefs inode write pointer offset
3658dcc1a9dSDamien Le Moal  * (which can be out of sync with the drive due to partial write failures).
3668dcc1a9dSDamien Le Moal  */
__zonefs_io_error(struct inode * inode,bool write)3674008e2a0SDamien Le Moal void __zonefs_io_error(struct inode *inode, bool write)
3688dcc1a9dSDamien Le Moal {
369aa7f243fSDamien Le Moal 	struct zonefs_zone *z = zonefs_inode_zone(inode);
3708dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
3718dcc1a9dSDamien Le Moal 	unsigned int noio_flag;
37214db5f64SDamien Le Moal 	struct blk_zone zone;
3738dcc1a9dSDamien Le Moal 	int ret;
3748dcc1a9dSDamien Le Moal 
3758dcc1a9dSDamien Le Moal 	/*
37614db5f64SDamien Le Moal 	 * Conventional zone have no write pointer and cannot become read-only
37714db5f64SDamien Le Moal 	 * or offline. So simply fake a report for a single or aggregated zone
37814db5f64SDamien Le Moal 	 * and let zonefs_handle_io_error() correct the zone inode information
37914db5f64SDamien Le Moal 	 * according to the mount options.
3807dd12d65SDamien Le Moal 	 */
38114db5f64SDamien Le Moal 	if (!zonefs_zone_is_seq(z)) {
38214db5f64SDamien Le Moal 		zone.start = z->z_sector;
38314db5f64SDamien Le Moal 		zone.len = z->z_size >> SECTOR_SHIFT;
38414db5f64SDamien Le Moal 		zone.wp = zone.start + zone.len;
38514db5f64SDamien Le Moal 		zone.type = BLK_ZONE_TYPE_CONVENTIONAL;
38614db5f64SDamien Le Moal 		zone.cond = BLK_ZONE_COND_NOT_WP;
38714db5f64SDamien Le Moal 		zone.capacity = zone.len;
38814db5f64SDamien Le Moal 		goto handle_io_error;
38914db5f64SDamien Le Moal 	}
3907dd12d65SDamien Le Moal 
3917dd12d65SDamien Le Moal 	/*
3928dcc1a9dSDamien Le Moal 	 * Memory allocations in blkdev_report_zones() can trigger a memory
3938dcc1a9dSDamien Le Moal 	 * reclaim which may in turn cause a recursion into zonefs as well as
3948dcc1a9dSDamien Le Moal 	 * struct request allocations for the same device. The former case may
3958dcc1a9dSDamien Le Moal 	 * end up in a deadlock on the inode truncate mutex, while the latter
3968dcc1a9dSDamien Le Moal 	 * may prevent IO forward progress. Executing the report zones under
3978dcc1a9dSDamien Le Moal 	 * the GFP_NOIO context avoids both problems.
3988dcc1a9dSDamien Le Moal 	 */
3998dcc1a9dSDamien Le Moal 	noio_flag = memalloc_noio_save();
40014db5f64SDamien Le Moal 	ret = blkdev_report_zones(sb->s_bdev, z->z_sector, 1,
40114db5f64SDamien Le Moal 				  zonefs_io_error_cb, &zone);
40214db5f64SDamien Le Moal 	memalloc_noio_restore(noio_flag);
40314db5f64SDamien Le Moal 
40414db5f64SDamien Le Moal 	if (ret != 1) {
4058dcc1a9dSDamien Le Moal 		zonefs_err(sb, "Get inode %lu zone information failed %d\n",
4068dcc1a9dSDamien Le Moal 			   inode->i_ino, ret);
40714db5f64SDamien Le Moal 		zonefs_warn(sb, "remounting filesystem read-only\n");
40814db5f64SDamien Le Moal 		sb->s_flags |= SB_RDONLY;
40914db5f64SDamien Le Moal 		return;
41014db5f64SDamien Le Moal 	}
41114db5f64SDamien Le Moal 
41214db5f64SDamien Le Moal handle_io_error:
41314db5f64SDamien Le Moal 	zonefs_handle_io_error(inode, &zone, write);
41448d546a8SJohannes Thumshirn }
4158dcc1a9dSDamien Le Moal 
4168dcc1a9dSDamien Le Moal static struct kmem_cache *zonefs_inode_cachep;
4178dcc1a9dSDamien Le Moal 
zonefs_alloc_inode(struct super_block * sb)4188dcc1a9dSDamien Le Moal static struct inode *zonefs_alloc_inode(struct super_block *sb)
4198dcc1a9dSDamien Le Moal {
4208dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi;
4218dcc1a9dSDamien Le Moal 
422fd60b288SMuchun Song 	zi = alloc_inode_sb(sb, zonefs_inode_cachep, GFP_KERNEL);
4238dcc1a9dSDamien Le Moal 	if (!zi)
4248dcc1a9dSDamien Le Moal 		return NULL;
4258dcc1a9dSDamien Le Moal 
4268dcc1a9dSDamien Le Moal 	inode_init_once(&zi->i_vnode);
4278dcc1a9dSDamien Le Moal 	mutex_init(&zi->i_truncate_mutex);
428b5c00e97SJohannes Thumshirn 	zi->i_wr_refcnt = 0;
4298dcc1a9dSDamien Le Moal 
4308dcc1a9dSDamien Le Moal 	return &zi->i_vnode;
4318dcc1a9dSDamien Le Moal }
4328dcc1a9dSDamien Le Moal 
zonefs_free_inode(struct inode * inode)4338dcc1a9dSDamien Le Moal static void zonefs_free_inode(struct inode *inode)
4348dcc1a9dSDamien Le Moal {
4358dcc1a9dSDamien Le Moal 	kmem_cache_free(zonefs_inode_cachep, ZONEFS_I(inode));
4368dcc1a9dSDamien Le Moal }
4378dcc1a9dSDamien Le Moal 
4388dcc1a9dSDamien Le Moal /*
4398dcc1a9dSDamien Le Moal  * File system stat.
4408dcc1a9dSDamien Le Moal  */
zonefs_statfs(struct dentry * dentry,struct kstatfs * buf)4418dcc1a9dSDamien Le Moal static int zonefs_statfs(struct dentry *dentry, struct kstatfs *buf)
4428dcc1a9dSDamien Le Moal {
4438dcc1a9dSDamien Le Moal 	struct super_block *sb = dentry->d_sb;
4448dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
4458dcc1a9dSDamien Le Moal 	enum zonefs_ztype t;
4468dcc1a9dSDamien Le Moal 
4478dcc1a9dSDamien Le Moal 	buf->f_type = ZONEFS_MAGIC;
4488dcc1a9dSDamien Le Moal 	buf->f_bsize = sb->s_blocksize;
4498dcc1a9dSDamien Le Moal 	buf->f_namelen = ZONEFS_NAME_MAX;
4508dcc1a9dSDamien Le Moal 
4518dcc1a9dSDamien Le Moal 	spin_lock(&sbi->s_lock);
4528dcc1a9dSDamien Le Moal 
4538dcc1a9dSDamien Le Moal 	buf->f_blocks = sbi->s_blocks;
4548dcc1a9dSDamien Le Moal 	if (WARN_ON(sbi->s_used_blocks > sbi->s_blocks))
4558dcc1a9dSDamien Le Moal 		buf->f_bfree = 0;
4568dcc1a9dSDamien Le Moal 	else
4578dcc1a9dSDamien Le Moal 		buf->f_bfree = buf->f_blocks - sbi->s_used_blocks;
4588dcc1a9dSDamien Le Moal 	buf->f_bavail = buf->f_bfree;
4598dcc1a9dSDamien Le Moal 
4608dcc1a9dSDamien Le Moal 	for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) {
461aa7f243fSDamien Le Moal 		if (sbi->s_zgroup[t].g_nr_zones)
462aa7f243fSDamien Le Moal 			buf->f_files += sbi->s_zgroup[t].g_nr_zones + 1;
4638dcc1a9dSDamien Le Moal 	}
4648dcc1a9dSDamien Le Moal 	buf->f_ffree = 0;
4658dcc1a9dSDamien Le Moal 
4668dcc1a9dSDamien Le Moal 	spin_unlock(&sbi->s_lock);
4678dcc1a9dSDamien Le Moal 
4689591c3a3SAmir Goldstein 	buf->f_fsid = uuid_to_fsid(sbi->s_uuid.b);
4698dcc1a9dSDamien Le Moal 
4708dcc1a9dSDamien Le Moal 	return 0;
4718dcc1a9dSDamien Le Moal }
4728dcc1a9dSDamien Le Moal 
4738dcc1a9dSDamien Le Moal enum {
474567e629fSBill O'Donnell 	Opt_errors, Opt_explicit_open,
4758dcc1a9dSDamien Le Moal };
4768dcc1a9dSDamien Le Moal 
477567e629fSBill O'Donnell struct zonefs_context {
478567e629fSBill O'Donnell 	unsigned long s_mount_opts;
4798dcc1a9dSDamien Le Moal };
4808dcc1a9dSDamien Le Moal 
481567e629fSBill O'Donnell static const struct constant_table zonefs_param_errors[] = {
482567e629fSBill O'Donnell 	{"remount-ro",		ZONEFS_MNTOPT_ERRORS_RO},
483567e629fSBill O'Donnell 	{"zone-ro",		ZONEFS_MNTOPT_ERRORS_ZRO},
484567e629fSBill O'Donnell 	{"zone-offline",	ZONEFS_MNTOPT_ERRORS_ZOL},
485567e629fSBill O'Donnell 	{"repair", 		ZONEFS_MNTOPT_ERRORS_REPAIR},
486567e629fSBill O'Donnell 	{}
487567e629fSBill O'Donnell };
488567e629fSBill O'Donnell 
489567e629fSBill O'Donnell static const struct fs_parameter_spec zonefs_param_spec[] = {
490567e629fSBill O'Donnell 	fsparam_enum	("errors",		Opt_errors, zonefs_param_errors),
491567e629fSBill O'Donnell 	fsparam_flag	("explicit-open",	Opt_explicit_open),
492567e629fSBill O'Donnell 	{}
493567e629fSBill O'Donnell };
494567e629fSBill O'Donnell 
zonefs_parse_param(struct fs_context * fc,struct fs_parameter * param)495567e629fSBill O'Donnell static int zonefs_parse_param(struct fs_context *fc, struct fs_parameter *param)
4968dcc1a9dSDamien Le Moal {
497567e629fSBill O'Donnell 	struct zonefs_context *ctx = fc->fs_private;
498567e629fSBill O'Donnell 	struct fs_parse_result result;
499567e629fSBill O'Donnell 	int opt;
5008dcc1a9dSDamien Le Moal 
501567e629fSBill O'Donnell 	opt = fs_parse(fc, zonefs_param_spec, param, &result);
502567e629fSBill O'Donnell 	if (opt < 0)
503567e629fSBill O'Donnell 		return opt;
5048dcc1a9dSDamien Le Moal 
505567e629fSBill O'Donnell 	switch (opt) {
506567e629fSBill O'Donnell 	case Opt_errors:
507567e629fSBill O'Donnell 		ctx->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
508567e629fSBill O'Donnell 		ctx->s_mount_opts |= result.uint_32;
5098dcc1a9dSDamien Le Moal 		break;
510b5c00e97SJohannes Thumshirn 	case Opt_explicit_open:
511567e629fSBill O'Donnell 		ctx->s_mount_opts |= ZONEFS_MNTOPT_EXPLICIT_OPEN;
512b5c00e97SJohannes Thumshirn 		break;
5138dcc1a9dSDamien Le Moal 	default:
5148dcc1a9dSDamien Le Moal 		return -EINVAL;
5158dcc1a9dSDamien Le Moal 	}
5168dcc1a9dSDamien Le Moal 
5178dcc1a9dSDamien Le Moal 	return 0;
5188dcc1a9dSDamien Le Moal }
5198dcc1a9dSDamien Le Moal 
zonefs_show_options(struct seq_file * seq,struct dentry * root)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 
zonefs_inode_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * iattr)536232dd599SLinus Torvalds static int zonefs_inode_setattr(struct mnt_idmap *idmap,
5374008e2a0SDamien Le Moal 				struct dentry *dentry, struct iattr *iattr)
5384008e2a0SDamien Le Moal {
5394008e2a0SDamien Le Moal 	struct inode *inode = d_inode(dentry);
5404008e2a0SDamien Le Moal 	int ret;
5418dcc1a9dSDamien Le Moal 
5424008e2a0SDamien Le Moal 	if (unlikely(IS_IMMUTABLE(inode)))
5434008e2a0SDamien Le Moal 		return -EPERM;
5444008e2a0SDamien Le Moal 
545232dd599SLinus Torvalds 	ret = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
5464008e2a0SDamien Le Moal 	if (ret)
5474008e2a0SDamien Le Moal 		return ret;
5484008e2a0SDamien Le Moal 
5494008e2a0SDamien Le Moal 	/*
5504008e2a0SDamien Le Moal 	 * Since files and directories cannot be created nor deleted, do not
5514008e2a0SDamien Le Moal 	 * allow setting any write attributes on the sub-directories grouping
5524008e2a0SDamien Le Moal 	 * files by zone type.
5534008e2a0SDamien Le Moal 	 */
5544008e2a0SDamien Le Moal 	if ((iattr->ia_valid & ATTR_MODE) && S_ISDIR(inode->i_mode) &&
5554008e2a0SDamien Le Moal 	    (iattr->ia_mode & 0222))
5564008e2a0SDamien Le Moal 		return -EPERM;
5574008e2a0SDamien Le Moal 
5584008e2a0SDamien Le Moal 	if (((iattr->ia_valid & ATTR_UID) &&
5594008e2a0SDamien Le Moal 	     !uid_eq(iattr->ia_uid, inode->i_uid)) ||
5604008e2a0SDamien Le Moal 	    ((iattr->ia_valid & ATTR_GID) &&
5614008e2a0SDamien Le Moal 	     !gid_eq(iattr->ia_gid, inode->i_gid))) {
562232dd599SLinus Torvalds 		ret = dquot_transfer(&nop_mnt_idmap, inode, iattr);
5634008e2a0SDamien Le Moal 		if (ret)
5644008e2a0SDamien Le Moal 			return ret;
5654008e2a0SDamien Le Moal 	}
5664008e2a0SDamien Le Moal 
5674008e2a0SDamien Le Moal 	if (iattr->ia_valid & ATTR_SIZE) {
5684008e2a0SDamien Le Moal 		ret = zonefs_file_truncate(inode, iattr->ia_size);
5694008e2a0SDamien Le Moal 		if (ret)
5704008e2a0SDamien Le Moal 			return ret;
5714008e2a0SDamien Le Moal 	}
5724008e2a0SDamien Le Moal 
573232dd599SLinus Torvalds 	setattr_copy(&nop_mnt_idmap, inode, iattr);
5744008e2a0SDamien Le Moal 
575d207794aSDamien Le Moal 	if (S_ISREG(inode->i_mode)) {
576d207794aSDamien Le Moal 		struct zonefs_zone *z = zonefs_inode_zone(inode);
577d207794aSDamien Le Moal 
578d207794aSDamien Le Moal 		z->z_mode = inode->i_mode;
579d207794aSDamien Le Moal 		z->z_uid = inode->i_uid;
580d207794aSDamien Le Moal 		z->z_gid = inode->i_gid;
5814008e2a0SDamien Le Moal 	}
5828dcc1a9dSDamien Le Moal 
583d207794aSDamien Le Moal 	return 0;
5848dcc1a9dSDamien Le Moal }
5858dcc1a9dSDamien Le Moal 
5864008e2a0SDamien Le Moal static const struct inode_operations zonefs_file_inode_operations = {
5878dcc1a9dSDamien Le Moal 	.setattr	= zonefs_inode_setattr,
5888dcc1a9dSDamien Le Moal };
5898dcc1a9dSDamien Le Moal 
zonefs_fname_to_fno(const struct qstr * fname)590d207794aSDamien Le Moal static long zonefs_fname_to_fno(const struct qstr *fname)
5918dcc1a9dSDamien Le Moal {
592d207794aSDamien Le Moal 	const char *name = fname->name;
593d207794aSDamien Le Moal 	unsigned int len = fname->len;
594d207794aSDamien Le Moal 	long fno = 0, shift = 1;
595d207794aSDamien Le Moal 	const char *rname;
596d207794aSDamien Le Moal 	char c = *name;
597d207794aSDamien Le Moal 	unsigned int i;
5988dcc1a9dSDamien Le Moal 
599d207794aSDamien Le Moal 	/*
600d207794aSDamien Le Moal 	 * File names are always a base-10 number string without any
601d207794aSDamien Le Moal 	 * leading 0s.
602d207794aSDamien Le Moal 	 */
603d207794aSDamien Le Moal 	if (!isdigit(c))
604d207794aSDamien Le Moal 		return -ENOENT;
605d207794aSDamien Le Moal 
606d207794aSDamien Le Moal 	if (len > 1 && c == '0')
607d207794aSDamien Le Moal 		return -ENOENT;
608d207794aSDamien Le Moal 
609d207794aSDamien Le Moal 	if (len == 1)
610d207794aSDamien Le Moal 		return c - '0';
611d207794aSDamien Le Moal 
612d207794aSDamien Le Moal 	for (i = 0, rname = name + len - 1; i < len; i++, rname--) {
613d207794aSDamien Le Moal 		c = *rname;
614d207794aSDamien Le Moal 		if (!isdigit(c))
615d207794aSDamien Le Moal 			return -ENOENT;
616d207794aSDamien Le Moal 		fno += (c - '0') * shift;
617d207794aSDamien Le Moal 		shift *= 10;
6188dcc1a9dSDamien Le Moal 	}
6198dcc1a9dSDamien Le Moal 
620d207794aSDamien Le Moal 	return fno;
621d207794aSDamien Le Moal }
622d207794aSDamien Le Moal 
zonefs_get_file_inode(struct inode * dir,struct dentry * dentry)623d207794aSDamien Le Moal static struct inode *zonefs_get_file_inode(struct inode *dir,
624d207794aSDamien Le Moal 					   struct dentry *dentry)
6258dcc1a9dSDamien Le Moal {
626d207794aSDamien Le Moal 	struct zonefs_zone_group *zgroup = dir->i_private;
627d207794aSDamien Le Moal 	struct super_block *sb = dir->i_sb;
6288dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
629d207794aSDamien Le Moal 	struct zonefs_zone *z;
630d207794aSDamien Le Moal 	struct inode *inode;
631d207794aSDamien Le Moal 	ino_t ino;
632d207794aSDamien Le Moal 	long fno;
6338dcc1a9dSDamien Le Moal 
634d207794aSDamien Le Moal 	/* Get the file number from the file name */
635d207794aSDamien Le Moal 	fno = zonefs_fname_to_fno(&dentry->d_name);
636d207794aSDamien Le Moal 	if (fno < 0)
637d207794aSDamien Le Moal 		return ERR_PTR(fno);
6388dcc1a9dSDamien Le Moal 
639d207794aSDamien Le Moal 	if (!zgroup->g_nr_zones || fno >= zgroup->g_nr_zones)
640d207794aSDamien Le Moal 		return ERR_PTR(-ENOENT);
641d207794aSDamien Le Moal 
642d207794aSDamien Le Moal 	z = &zgroup->g_zones[fno];
643d207794aSDamien Le Moal 	ino = z->z_sector >> sbi->s_zone_sectors_shift;
644d207794aSDamien Le Moal 	inode = iget_locked(sb, ino);
645d207794aSDamien Le Moal 	if (!inode)
646d207794aSDamien Le Moal 		return ERR_PTR(-ENOMEM);
647d207794aSDamien Le Moal 	if (!(inode->i_state & I_NEW)) {
648d207794aSDamien Le Moal 		WARN_ON_ONCE(inode->i_private != z);
649d207794aSDamien Le Moal 		return inode;
6507dd12d65SDamien Le Moal 	}
651e3c3155bSJohannes Thumshirn 
652d207794aSDamien Le Moal 	inode->i_ino = ino;
653d207794aSDamien Le Moal 	inode->i_mode = z->z_mode;
6548df379a3SJeff Layton 	inode_set_mtime_to_ts(inode,
6558df379a3SJeff Layton 			      inode_set_atime_to_ts(inode, inode_set_ctime_to_ts(inode, inode_get_ctime(dir))));
656d207794aSDamien Le Moal 	inode->i_uid = z->z_uid;
657d207794aSDamien Le Moal 	inode->i_gid = z->z_gid;
658aa7f243fSDamien Le Moal 	inode->i_size = z->z_wpoffset;
659aa7f243fSDamien Le Moal 	inode->i_blocks = z->z_capacity >> SECTOR_SHIFT;
660d207794aSDamien Le Moal 	inode->i_private = z;
6618dcc1a9dSDamien Le Moal 
6628dcc1a9dSDamien Le Moal 	inode->i_op = &zonefs_file_inode_operations;
6638dcc1a9dSDamien Le Moal 	inode->i_fop = &zonefs_file_operations;
6648dcc1a9dSDamien Le Moal 	inode->i_mapping->a_ops = &zonefs_file_aops;
665*df2f9708SJohannes Thumshirn 	mapping_set_large_folios(inode->i_mapping);
6668dcc1a9dSDamien Le Moal 
66746a9c526SDamien Le Moal 	/* Update the inode access rights depending on the zone condition */
66846a9c526SDamien Le Moal 	zonefs_inode_update_mode(inode);
6691da18a29SDamien Le Moal 
670d207794aSDamien Le Moal 	unlock_new_inode(inode);
671d207794aSDamien Le Moal 
672d207794aSDamien Le Moal 	return inode;
6738dcc1a9dSDamien Le Moal }
6748dcc1a9dSDamien Le Moal 
zonefs_get_zgroup_inode(struct super_block * sb,enum zonefs_ztype ztype)675d207794aSDamien Le Moal static struct inode *zonefs_get_zgroup_inode(struct super_block *sb,
676aa7f243fSDamien Le Moal 					     enum zonefs_ztype ztype)
6778dcc1a9dSDamien Le Moal {
678d207794aSDamien Le Moal 	struct inode *root = d_inode(sb->s_root);
679d207794aSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
6808dcc1a9dSDamien Le Moal 	struct inode *inode;
681d207794aSDamien Le Moal 	ino_t ino = bdev_nr_zones(sb->s_bdev) + ztype + 1;
6828dcc1a9dSDamien Le Moal 
683d207794aSDamien Le Moal 	inode = iget_locked(sb, ino);
6848dcc1a9dSDamien Le Moal 	if (!inode)
685d207794aSDamien Le Moal 		return ERR_PTR(-ENOMEM);
686d207794aSDamien Le Moal 	if (!(inode->i_state & I_NEW))
687d207794aSDamien Le Moal 		return inode;
6888dcc1a9dSDamien Le Moal 
689d207794aSDamien Le Moal 	inode->i_ino = ino;
690232dd599SLinus Torvalds 	inode_init_owner(&nop_mnt_idmap, inode, root, S_IFDIR | 0555);
691d207794aSDamien Le Moal 	inode->i_size = sbi->s_zgroup[ztype].g_nr_zones;
6928df379a3SJeff Layton 	inode_set_mtime_to_ts(inode,
6938df379a3SJeff Layton 			      inode_set_atime_to_ts(inode, inode_set_ctime_to_ts(inode, inode_get_ctime(root))));
694d207794aSDamien Le Moal 	inode->i_private = &sbi->s_zgroup[ztype];
695d207794aSDamien Le Moal 	set_nlink(inode, 2);
6961da18a29SDamien Le Moal 
697d207794aSDamien Le Moal 	inode->i_op = &zonefs_dir_inode_operations;
698d207794aSDamien Le Moal 	inode->i_fop = &zonefs_dir_operations;
6998dcc1a9dSDamien Le Moal 
700d207794aSDamien Le Moal 	unlock_new_inode(inode);
7018dcc1a9dSDamien Le Moal 
702d207794aSDamien Le Moal 	return inode;
7038dcc1a9dSDamien Le Moal }
7048dcc1a9dSDamien Le Moal 
705d207794aSDamien Le Moal 
zonefs_get_dir_inode(struct inode * dir,struct dentry * dentry)706d207794aSDamien Le Moal static struct inode *zonefs_get_dir_inode(struct inode *dir,
707d207794aSDamien Le Moal 					  struct dentry *dentry)
708d207794aSDamien Le Moal {
709d207794aSDamien Le Moal 	struct super_block *sb = dir->i_sb;
710d207794aSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
711d207794aSDamien Le Moal 	const char *name = dentry->d_name.name;
712d207794aSDamien Le Moal 	enum zonefs_ztype ztype;
7138dcc1a9dSDamien Le Moal 
7148dcc1a9dSDamien Le Moal 	/*
715d207794aSDamien Le Moal 	 * We only need to check for the "seq" directory and
716d207794aSDamien Le Moal 	 * the "cnv" directory if we have conventional zones.
7178dcc1a9dSDamien Le Moal 	 */
718d207794aSDamien Le Moal 	if (dentry->d_name.len != 3)
719d207794aSDamien Le Moal 		return ERR_PTR(-ENOENT);
720d207794aSDamien Le Moal 
721d207794aSDamien Le Moal 	for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
722d207794aSDamien Le Moal 		if (sbi->s_zgroup[ztype].g_nr_zones &&
723d207794aSDamien Le Moal 		    memcmp(name, zonefs_zgroup_name(ztype), 3) == 0)
724d207794aSDamien Le Moal 			break;
725d207794aSDamien Le Moal 	}
726d207794aSDamien Le Moal 	if (ztype == ZONEFS_ZTYPE_MAX)
727d207794aSDamien Le Moal 		return ERR_PTR(-ENOENT);
728d207794aSDamien Le Moal 
729d207794aSDamien Le Moal 	return zonefs_get_zgroup_inode(sb, ztype);
7308dcc1a9dSDamien Le Moal }
7318dcc1a9dSDamien Le Moal 
zonefs_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)732d207794aSDamien Le Moal static struct dentry *zonefs_lookup(struct inode *dir, struct dentry *dentry,
733d207794aSDamien Le Moal 				    unsigned int flags)
7348dcc1a9dSDamien Le Moal {
7358dcc1a9dSDamien Le Moal 	struct inode *inode;
7368dcc1a9dSDamien Le Moal 
737d207794aSDamien Le Moal 	if (dentry->d_name.len > ZONEFS_NAME_MAX)
738d207794aSDamien Le Moal 		return ERR_PTR(-ENAMETOOLONG);
7398dcc1a9dSDamien Le Moal 
740d207794aSDamien Le Moal 	if (dir == d_inode(dir->i_sb->s_root))
741d207794aSDamien Le Moal 		inode = zonefs_get_dir_inode(dir, dentry);
742d207794aSDamien Le Moal 	else
743d207794aSDamien Le Moal 		inode = zonefs_get_file_inode(dir, dentry);
7448dcc1a9dSDamien Le Moal 
745d207794aSDamien Le Moal 	return d_splice_alias(inode, dentry);
7468dcc1a9dSDamien Le Moal }
7478dcc1a9dSDamien Le Moal 
zonefs_readdir_root(struct file * file,struct dir_context * ctx)748d207794aSDamien Le Moal static int zonefs_readdir_root(struct file *file, struct dir_context *ctx)
749d207794aSDamien Le Moal {
750d207794aSDamien Le Moal 	struct inode *inode = file_inode(file);
751d207794aSDamien Le Moal 	struct super_block *sb = inode->i_sb;
752d207794aSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
753d207794aSDamien Le Moal 	enum zonefs_ztype ztype = ZONEFS_ZTYPE_CNV;
754d207794aSDamien Le Moal 	ino_t base_ino = bdev_nr_zones(sb->s_bdev) + 1;
7558dcc1a9dSDamien Le Moal 
756d207794aSDamien Le Moal 	if (ctx->pos >= inode->i_size)
757d207794aSDamien Le Moal 		return 0;
7588dcc1a9dSDamien Le Moal 
759d207794aSDamien Le Moal 	if (!dir_emit_dots(file, ctx))
760d207794aSDamien Le Moal 		return 0;
7618dcc1a9dSDamien Le Moal 
762d207794aSDamien Le Moal 	if (ctx->pos == 2) {
763d207794aSDamien Le Moal 		if (!sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones)
764d207794aSDamien Le Moal 			ztype = ZONEFS_ZTYPE_SEQ;
765d207794aSDamien Le Moal 
766d207794aSDamien Le Moal 		if (!dir_emit(ctx, zonefs_zgroup_name(ztype), 3,
767d207794aSDamien Le Moal 			      base_ino + ztype, DT_DIR))
768d207794aSDamien Le Moal 			return 0;
769d207794aSDamien Le Moal 		ctx->pos++;
7708dcc1a9dSDamien Le Moal 	}
7718dcc1a9dSDamien Le Moal 
772d207794aSDamien Le Moal 	if (ctx->pos == 3 && ztype != ZONEFS_ZTYPE_SEQ) {
773d207794aSDamien Le Moal 		ztype = ZONEFS_ZTYPE_SEQ;
774d207794aSDamien Le Moal 		if (!dir_emit(ctx, zonefs_zgroup_name(ztype), 3,
775d207794aSDamien Le Moal 			      base_ino + ztype, DT_DIR))
776d207794aSDamien Le Moal 			return 0;
777d207794aSDamien Le Moal 		ctx->pos++;
778d207794aSDamien Le Moal 	}
779d207794aSDamien Le Moal 
780d207794aSDamien Le Moal 	return 0;
781d207794aSDamien Le Moal }
782d207794aSDamien Le Moal 
zonefs_readdir_zgroup(struct file * file,struct dir_context * ctx)783d207794aSDamien Le Moal static int zonefs_readdir_zgroup(struct file *file,
784d207794aSDamien Le Moal 				 struct dir_context *ctx)
785d207794aSDamien Le Moal {
786d207794aSDamien Le Moal 	struct inode *inode = file_inode(file);
787d207794aSDamien Le Moal 	struct zonefs_zone_group *zgroup = inode->i_private;
788d207794aSDamien Le Moal 	struct super_block *sb = inode->i_sb;
789d207794aSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
790d207794aSDamien Le Moal 	struct zonefs_zone *z;
791d207794aSDamien Le Moal 	int fname_len;
792d207794aSDamien Le Moal 	char *fname;
793d207794aSDamien Le Moal 	ino_t ino;
794d207794aSDamien Le Moal 	int f;
795d207794aSDamien Le Moal 
796d207794aSDamien Le Moal 	/*
797d207794aSDamien Le Moal 	 * The size of zone group directories is equal to the number
798d207794aSDamien Le Moal 	 * of zone files in the group and does note include the "." and
799d207794aSDamien Le Moal 	 * ".." entries. Hence the "+ 2" here.
800d207794aSDamien Le Moal 	 */
801d207794aSDamien Le Moal 	if (ctx->pos >= inode->i_size + 2)
802d207794aSDamien Le Moal 		return 0;
803d207794aSDamien Le Moal 
804d207794aSDamien Le Moal 	if (!dir_emit_dots(file, ctx))
805d207794aSDamien Le Moal 		return 0;
806d207794aSDamien Le Moal 
807d207794aSDamien Le Moal 	fname = kmalloc(ZONEFS_NAME_MAX, GFP_KERNEL);
808d207794aSDamien Le Moal 	if (!fname)
809d207794aSDamien Le Moal 		return -ENOMEM;
810d207794aSDamien Le Moal 
811d207794aSDamien Le Moal 	for (f = ctx->pos - 2; f < zgroup->g_nr_zones; f++) {
812d207794aSDamien Le Moal 		z = &zgroup->g_zones[f];
813d207794aSDamien Le Moal 		ino = z->z_sector >> sbi->s_zone_sectors_shift;
814d207794aSDamien Le Moal 		fname_len = snprintf(fname, ZONEFS_NAME_MAX - 1, "%u", f);
815d207794aSDamien Le Moal 		if (!dir_emit(ctx, fname, fname_len, ino, DT_REG))
816d207794aSDamien Le Moal 			break;
817d207794aSDamien Le Moal 		ctx->pos++;
818d207794aSDamien Le Moal 	}
819d207794aSDamien Le Moal 
820d207794aSDamien Le Moal 	kfree(fname);
821d207794aSDamien Le Moal 
822d207794aSDamien Le Moal 	return 0;
823d207794aSDamien Le Moal }
824d207794aSDamien Le Moal 
zonefs_readdir(struct file * file,struct dir_context * ctx)825d207794aSDamien Le Moal static int zonefs_readdir(struct file *file, struct dir_context *ctx)
826d207794aSDamien Le Moal {
827d207794aSDamien Le Moal 	struct inode *inode = file_inode(file);
828d207794aSDamien Le Moal 
829d207794aSDamien Le Moal 	if (inode == d_inode(inode->i_sb->s_root))
830d207794aSDamien Le Moal 		return zonefs_readdir_root(file, ctx);
831d207794aSDamien Le Moal 
832d207794aSDamien Le Moal 	return zonefs_readdir_zgroup(file, ctx);
833d207794aSDamien Le Moal }
834d207794aSDamien Le Moal 
835d207794aSDamien Le Moal const struct inode_operations zonefs_dir_inode_operations = {
836d207794aSDamien Le Moal 	.lookup		= zonefs_lookup,
837d207794aSDamien Le Moal 	.setattr	= zonefs_inode_setattr,
838d207794aSDamien Le Moal };
839d207794aSDamien Le Moal 
840d207794aSDamien Le Moal const struct file_operations zonefs_dir_operations = {
841d207794aSDamien Le Moal 	.llseek		= generic_file_llseek,
842d207794aSDamien Le Moal 	.read		= generic_read_dir,
843d207794aSDamien Le Moal 	.iterate_shared	= zonefs_readdir,
844d207794aSDamien Le Moal };
845d207794aSDamien Le Moal 
8468dcc1a9dSDamien Le Moal struct zonefs_zone_data {
8478dcc1a9dSDamien Le Moal 	struct super_block	*sb;
8488dcc1a9dSDamien Le Moal 	unsigned int		nr_zones[ZONEFS_ZTYPE_MAX];
849aa7f243fSDamien Le Moal 	sector_t		cnv_zone_start;
8508dcc1a9dSDamien Le Moal 	struct blk_zone		*zones;
8518dcc1a9dSDamien Le Moal };
8528dcc1a9dSDamien Le Moal 
zonefs_get_zone_info_cb(struct blk_zone * zone,unsigned int idx,void * data)8538dcc1a9dSDamien Le Moal static int zonefs_get_zone_info_cb(struct blk_zone *zone, unsigned int idx,
8548dcc1a9dSDamien Le Moal 				   void *data)
8558dcc1a9dSDamien Le Moal {
8568dcc1a9dSDamien Le Moal 	struct zonefs_zone_data *zd = data;
857aa7f243fSDamien Le Moal 	struct super_block *sb = zd->sb;
858aa7f243fSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
8598dcc1a9dSDamien Le Moal 
8608dcc1a9dSDamien Le Moal 	/*
861aa7f243fSDamien Le Moal 	 * We do not care about the first zone: it contains the super block
862aa7f243fSDamien Le Moal 	 * and not exposed as a file.
863aa7f243fSDamien Le Moal 	 */
864aa7f243fSDamien Le Moal 	if (!idx)
865aa7f243fSDamien Le Moal 		return 0;
866aa7f243fSDamien Le Moal 
867aa7f243fSDamien Le Moal 	/*
868aa7f243fSDamien Le Moal 	 * Count the number of zones that will be exposed as files.
869aa7f243fSDamien Le Moal 	 * For sequential zones, we always have as many files as zones.
870aa7f243fSDamien Le Moal 	 * FOr conventional zones, the number of files depends on if we have
871aa7f243fSDamien Le Moal 	 * conventional zones aggregation enabled.
8728dcc1a9dSDamien Le Moal 	 */
8738dcc1a9dSDamien Le Moal 	switch (zone->type) {
8748dcc1a9dSDamien Le Moal 	case BLK_ZONE_TYPE_CONVENTIONAL:
875aa7f243fSDamien Le Moal 		if (sbi->s_features & ZONEFS_F_AGGRCNV) {
876aa7f243fSDamien Le Moal 			/* One file per set of contiguous conventional zones */
877aa7f243fSDamien Le Moal 			if (!(sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones) ||
878aa7f243fSDamien Le Moal 			    zone->start != zd->cnv_zone_start)
879aa7f243fSDamien Le Moal 				sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones++;
880aa7f243fSDamien Le Moal 			zd->cnv_zone_start = zone->start + zone->len;
881aa7f243fSDamien Le Moal 		} else {
882aa7f243fSDamien Le Moal 			/* One file per zone */
883aa7f243fSDamien Le Moal 			sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones++;
884aa7f243fSDamien Le Moal 		}
8858dcc1a9dSDamien Le Moal 		break;
8868dcc1a9dSDamien Le Moal 	case BLK_ZONE_TYPE_SEQWRITE_REQ:
8878dcc1a9dSDamien Le Moal 	case BLK_ZONE_TYPE_SEQWRITE_PREF:
888aa7f243fSDamien Le Moal 		sbi->s_zgroup[ZONEFS_ZTYPE_SEQ].g_nr_zones++;
8898dcc1a9dSDamien Le Moal 		break;
8908dcc1a9dSDamien Le Moal 	default:
8918dcc1a9dSDamien Le Moal 		zonefs_err(zd->sb, "Unsupported zone type 0x%x\n",
8928dcc1a9dSDamien Le Moal 			   zone->type);
8938dcc1a9dSDamien Le Moal 		return -EIO;
8948dcc1a9dSDamien Le Moal 	}
8958dcc1a9dSDamien Le Moal 
8968dcc1a9dSDamien Le Moal 	memcpy(&zd->zones[idx], zone, sizeof(struct blk_zone));
8978dcc1a9dSDamien Le Moal 
8988dcc1a9dSDamien Le Moal 	return 0;
8998dcc1a9dSDamien Le Moal }
9008dcc1a9dSDamien Le Moal 
zonefs_get_zone_info(struct zonefs_zone_data * zd)9018dcc1a9dSDamien Le Moal static int zonefs_get_zone_info(struct zonefs_zone_data *zd)
9028dcc1a9dSDamien Le Moal {
9038dcc1a9dSDamien Le Moal 	struct block_device *bdev = zd->sb->s_bdev;
9048dcc1a9dSDamien Le Moal 	int ret;
9058dcc1a9dSDamien Le Moal 
906b623e347SChristoph Hellwig 	zd->zones = kvcalloc(bdev_nr_zones(bdev), sizeof(struct blk_zone),
907b623e347SChristoph Hellwig 			     GFP_KERNEL);
9088dcc1a9dSDamien Le Moal 	if (!zd->zones)
9098dcc1a9dSDamien Le Moal 		return -ENOMEM;
9108dcc1a9dSDamien Le Moal 
9118dcc1a9dSDamien Le Moal 	/* Get zones information from the device */
9128dcc1a9dSDamien Le Moal 	ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES,
9138dcc1a9dSDamien Le Moal 				  zonefs_get_zone_info_cb, zd);
9148dcc1a9dSDamien Le Moal 	if (ret < 0) {
9158dcc1a9dSDamien Le Moal 		zonefs_err(zd->sb, "Zone report failed %d\n", ret);
9168dcc1a9dSDamien Le Moal 		return ret;
9178dcc1a9dSDamien Le Moal 	}
9188dcc1a9dSDamien Le Moal 
919b623e347SChristoph Hellwig 	if (ret != bdev_nr_zones(bdev)) {
9208dcc1a9dSDamien Le Moal 		zonefs_err(zd->sb, "Invalid zone report (%d/%u zones)\n",
921b623e347SChristoph Hellwig 			   ret, bdev_nr_zones(bdev));
9228dcc1a9dSDamien Le Moal 		return -EIO;
9238dcc1a9dSDamien Le Moal 	}
9248dcc1a9dSDamien Le Moal 
9258dcc1a9dSDamien Le Moal 	return 0;
9268dcc1a9dSDamien Le Moal }
9278dcc1a9dSDamien Le Moal 
zonefs_free_zone_info(struct zonefs_zone_data * zd)928aa7f243fSDamien Le Moal static inline void zonefs_free_zone_info(struct zonefs_zone_data *zd)
9298dcc1a9dSDamien Le Moal {
9308dcc1a9dSDamien Le Moal 	kvfree(zd->zones);
9318dcc1a9dSDamien Le Moal }
9328dcc1a9dSDamien Le Moal 
9338dcc1a9dSDamien Le Moal /*
934aa7f243fSDamien Le Moal  * Create a zone group and populate it with zone files.
935aa7f243fSDamien Le Moal  */
zonefs_init_zgroup(struct super_block * sb,struct zonefs_zone_data * zd,enum zonefs_ztype ztype)936aa7f243fSDamien Le Moal static int zonefs_init_zgroup(struct super_block *sb,
937aa7f243fSDamien Le Moal 			      struct zonefs_zone_data *zd,
938aa7f243fSDamien Le Moal 			      enum zonefs_ztype ztype)
939aa7f243fSDamien Le Moal {
940aa7f243fSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
941aa7f243fSDamien Le Moal 	struct zonefs_zone_group *zgroup = &sbi->s_zgroup[ztype];
942aa7f243fSDamien Le Moal 	struct blk_zone *zone, *next, *end;
943aa7f243fSDamien Le Moal 	struct zonefs_zone *z;
944aa7f243fSDamien Le Moal 	unsigned int n = 0;
945aa7f243fSDamien Le Moal 	int ret;
946aa7f243fSDamien Le Moal 
947aa7f243fSDamien Le Moal 	/* Allocate the zone group. If it is empty, we have nothing to do. */
948aa7f243fSDamien Le Moal 	if (!zgroup->g_nr_zones)
949aa7f243fSDamien Le Moal 		return 0;
950aa7f243fSDamien Le Moal 
951aa7f243fSDamien Le Moal 	zgroup->g_zones = kvcalloc(zgroup->g_nr_zones,
952aa7f243fSDamien Le Moal 				   sizeof(struct zonefs_zone), GFP_KERNEL);
953aa7f243fSDamien Le Moal 	if (!zgroup->g_zones)
954aa7f243fSDamien Le Moal 		return -ENOMEM;
955aa7f243fSDamien Le Moal 
956aa7f243fSDamien Le Moal 	/*
957aa7f243fSDamien Le Moal 	 * Initialize the zone groups using the device zone information.
958aa7f243fSDamien Le Moal 	 * We always skip the first zone as it contains the super block
959aa7f243fSDamien Le Moal 	 * and is not use to back a file.
960aa7f243fSDamien Le Moal 	 */
961aa7f243fSDamien Le Moal 	end = zd->zones + bdev_nr_zones(sb->s_bdev);
962aa7f243fSDamien Le Moal 	for (zone = &zd->zones[1]; zone < end; zone = next) {
963aa7f243fSDamien Le Moal 
964aa7f243fSDamien Le Moal 		next = zone + 1;
965aa7f243fSDamien Le Moal 		if (zonefs_zone_type(zone) != ztype)
966aa7f243fSDamien Le Moal 			continue;
967aa7f243fSDamien Le Moal 
968aa7f243fSDamien Le Moal 		if (WARN_ON_ONCE(n >= zgroup->g_nr_zones))
969aa7f243fSDamien Le Moal 			return -EINVAL;
970aa7f243fSDamien Le Moal 
971aa7f243fSDamien Le Moal 		/*
972aa7f243fSDamien Le Moal 		 * For conventional zones, contiguous zones can be aggregated
973aa7f243fSDamien Le Moal 		 * together to form larger files. Note that this overwrites the
974aa7f243fSDamien Le Moal 		 * length of the first zone of the set of contiguous zones
975aa7f243fSDamien Le Moal 		 * aggregated together. If one offline or read-only zone is
976aa7f243fSDamien Le Moal 		 * found, assume that all zones aggregated have the same
977aa7f243fSDamien Le Moal 		 * condition.
978aa7f243fSDamien Le Moal 		 */
979aa7f243fSDamien Le Moal 		if (ztype == ZONEFS_ZTYPE_CNV &&
980aa7f243fSDamien Le Moal 		    (sbi->s_features & ZONEFS_F_AGGRCNV)) {
981aa7f243fSDamien Le Moal 			for (; next < end; next++) {
982aa7f243fSDamien Le Moal 				if (zonefs_zone_type(next) != ztype)
983aa7f243fSDamien Le Moal 					break;
984aa7f243fSDamien Le Moal 				zone->len += next->len;
985aa7f243fSDamien Le Moal 				zone->capacity += next->capacity;
986aa7f243fSDamien Le Moal 				if (next->cond == BLK_ZONE_COND_READONLY &&
987aa7f243fSDamien Le Moal 				    zone->cond != BLK_ZONE_COND_OFFLINE)
988aa7f243fSDamien Le Moal 					zone->cond = BLK_ZONE_COND_READONLY;
989aa7f243fSDamien Le Moal 				else if (next->cond == BLK_ZONE_COND_OFFLINE)
990aa7f243fSDamien Le Moal 					zone->cond = BLK_ZONE_COND_OFFLINE;
991aa7f243fSDamien Le Moal 			}
992aa7f243fSDamien Le Moal 		}
993aa7f243fSDamien Le Moal 
994aa7f243fSDamien Le Moal 		z = &zgroup->g_zones[n];
995aa7f243fSDamien Le Moal 		if (ztype == ZONEFS_ZTYPE_CNV)
996aa7f243fSDamien Le Moal 			z->z_flags |= ZONEFS_ZONE_CNV;
997aa7f243fSDamien Le Moal 		z->z_sector = zone->start;
998aa7f243fSDamien Le Moal 		z->z_size = zone->len << SECTOR_SHIFT;
999aa7f243fSDamien Le Moal 		if (z->z_size > bdev_zone_sectors(sb->s_bdev) << SECTOR_SHIFT &&
1000aa7f243fSDamien Le Moal 		    !(sbi->s_features & ZONEFS_F_AGGRCNV)) {
1001aa7f243fSDamien Le Moal 			zonefs_err(sb,
1002aa7f243fSDamien Le Moal 				"Invalid zone size %llu (device zone sectors %llu)\n",
1003aa7f243fSDamien Le Moal 				z->z_size,
1004aa7f243fSDamien Le Moal 				bdev_zone_sectors(sb->s_bdev) << SECTOR_SHIFT);
1005aa7f243fSDamien Le Moal 			return -EINVAL;
1006aa7f243fSDamien Le Moal 		}
1007aa7f243fSDamien Le Moal 
1008aa7f243fSDamien Le Moal 		z->z_capacity = min_t(loff_t, MAX_LFS_FILESIZE,
1009aa7f243fSDamien Le Moal 				      zone->capacity << SECTOR_SHIFT);
1010aa7f243fSDamien Le Moal 		z->z_wpoffset = zonefs_check_zone_condition(sb, z, zone);
1011aa7f243fSDamien Le Moal 
1012d207794aSDamien Le Moal 		z->z_mode = S_IFREG | sbi->s_perm;
1013d207794aSDamien Le Moal 		z->z_uid = sbi->s_uid;
1014d207794aSDamien Le Moal 		z->z_gid = sbi->s_gid;
1015d207794aSDamien Le Moal 
1016d207794aSDamien Le Moal 		/*
1017d207794aSDamien Le Moal 		 * Let zonefs_inode_update_mode() know that we will need
1018d207794aSDamien Le Moal 		 * special initialization of the inode mode the first time
1019d207794aSDamien Le Moal 		 * it is accessed.
1020d207794aSDamien Le Moal 		 */
1021d207794aSDamien Le Moal 		z->z_flags |= ZONEFS_ZONE_INIT_MODE;
1022d207794aSDamien Le Moal 
1023aa7f243fSDamien Le Moal 		sb->s_maxbytes = max(z->z_capacity, sb->s_maxbytes);
1024aa7f243fSDamien Le Moal 		sbi->s_blocks += z->z_capacity >> sb->s_blocksize_bits;
1025aa7f243fSDamien Le Moal 		sbi->s_used_blocks += z->z_wpoffset >> sb->s_blocksize_bits;
1026aa7f243fSDamien Le Moal 
1027aa7f243fSDamien Le Moal 		/*
1028aa7f243fSDamien Le Moal 		 * For sequential zones, make sure that any open zone is closed
1029aa7f243fSDamien Le Moal 		 * first to ensure that the initial number of open zones is 0,
1030aa7f243fSDamien Le Moal 		 * in sync with the open zone accounting done when the mount
1031aa7f243fSDamien Le Moal 		 * option ZONEFS_MNTOPT_EXPLICIT_OPEN is used.
1032aa7f243fSDamien Le Moal 		 */
1033aa7f243fSDamien Le Moal 		if (ztype == ZONEFS_ZTYPE_SEQ &&
1034aa7f243fSDamien Le Moal 		    (zone->cond == BLK_ZONE_COND_IMP_OPEN ||
1035aa7f243fSDamien Le Moal 		     zone->cond == BLK_ZONE_COND_EXP_OPEN)) {
1036aa7f243fSDamien Le Moal 			ret = zonefs_zone_mgmt(sb, z, REQ_OP_ZONE_CLOSE);
1037aa7f243fSDamien Le Moal 			if (ret)
1038aa7f243fSDamien Le Moal 				return ret;
1039aa7f243fSDamien Le Moal 		}
1040aa7f243fSDamien Le Moal 
1041aa7f243fSDamien Le Moal 		zonefs_account_active(sb, z);
1042aa7f243fSDamien Le Moal 
1043aa7f243fSDamien Le Moal 		n++;
1044aa7f243fSDamien Le Moal 	}
1045aa7f243fSDamien Le Moal 
1046aa7f243fSDamien Le Moal 	if (WARN_ON_ONCE(n != zgroup->g_nr_zones))
1047aa7f243fSDamien Le Moal 		return -EINVAL;
1048aa7f243fSDamien Le Moal 
1049aa7f243fSDamien Le Moal 	zonefs_info(sb, "Zone group \"%s\" has %u file%s\n",
1050aa7f243fSDamien Le Moal 		    zonefs_zgroup_name(ztype),
1051aa7f243fSDamien Le Moal 		    zgroup->g_nr_zones,
105260b703c7SThorsten Blum 		    str_plural(zgroup->g_nr_zones));
1053aa7f243fSDamien Le Moal 
1054aa7f243fSDamien Le Moal 	return 0;
1055aa7f243fSDamien Le Moal }
1056aa7f243fSDamien Le Moal 
zonefs_free_zgroups(struct super_block * sb)1057aa7f243fSDamien Le Moal static void zonefs_free_zgroups(struct super_block *sb)
1058aa7f243fSDamien Le Moal {
1059aa7f243fSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1060aa7f243fSDamien Le Moal 	enum zonefs_ztype ztype;
1061aa7f243fSDamien Le Moal 
1062aa7f243fSDamien Le Moal 	if (!sbi)
1063aa7f243fSDamien Le Moal 		return;
1064aa7f243fSDamien Le Moal 
1065aa7f243fSDamien Le Moal 	for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
1066aa7f243fSDamien Le Moal 		kvfree(sbi->s_zgroup[ztype].g_zones);
1067aa7f243fSDamien Le Moal 		sbi->s_zgroup[ztype].g_zones = NULL;
1068aa7f243fSDamien Le Moal 	}
1069aa7f243fSDamien Le Moal }
1070aa7f243fSDamien Le Moal 
1071aa7f243fSDamien Le Moal /*
1072aa7f243fSDamien Le Moal  * Create a zone group and populate it with zone files.
1073aa7f243fSDamien Le Moal  */
zonefs_init_zgroups(struct super_block * sb)1074aa7f243fSDamien Le Moal static int zonefs_init_zgroups(struct super_block *sb)
1075aa7f243fSDamien Le Moal {
1076aa7f243fSDamien Le Moal 	struct zonefs_zone_data zd;
1077aa7f243fSDamien Le Moal 	enum zonefs_ztype ztype;
1078aa7f243fSDamien Le Moal 	int ret;
1079aa7f243fSDamien Le Moal 
1080aa7f243fSDamien Le Moal 	/* First get the device zone information */
1081aa7f243fSDamien Le Moal 	memset(&zd, 0, sizeof(struct zonefs_zone_data));
1082aa7f243fSDamien Le Moal 	zd.sb = sb;
1083aa7f243fSDamien Le Moal 	ret = zonefs_get_zone_info(&zd);
1084aa7f243fSDamien Le Moal 	if (ret)
1085aa7f243fSDamien Le Moal 		goto cleanup;
1086aa7f243fSDamien Le Moal 
1087aa7f243fSDamien Le Moal 	/* Allocate and initialize the zone groups */
1088aa7f243fSDamien Le Moal 	for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
1089aa7f243fSDamien Le Moal 		ret = zonefs_init_zgroup(sb, &zd, ztype);
1090aa7f243fSDamien Le Moal 		if (ret) {
1091aa7f243fSDamien Le Moal 			zonefs_info(sb,
1092aa7f243fSDamien Le Moal 				    "Zone group \"%s\" initialization failed\n",
1093aa7f243fSDamien Le Moal 				    zonefs_zgroup_name(ztype));
1094aa7f243fSDamien Le Moal 			break;
1095aa7f243fSDamien Le Moal 		}
1096aa7f243fSDamien Le Moal 	}
1097aa7f243fSDamien Le Moal 
1098aa7f243fSDamien Le Moal cleanup:
1099aa7f243fSDamien Le Moal 	zonefs_free_zone_info(&zd);
1100aa7f243fSDamien Le Moal 	if (ret)
1101aa7f243fSDamien Le Moal 		zonefs_free_zgroups(sb);
1102aa7f243fSDamien Le Moal 
1103aa7f243fSDamien Le Moal 	return ret;
1104aa7f243fSDamien Le Moal }
1105aa7f243fSDamien Le Moal 
1106aa7f243fSDamien Le Moal /*
11078dcc1a9dSDamien Le Moal  * Read super block information from the device.
11088dcc1a9dSDamien Le Moal  */
zonefs_read_super(struct super_block * sb)11098dcc1a9dSDamien Le Moal static int zonefs_read_super(struct super_block *sb)
11108dcc1a9dSDamien Le Moal {
11118dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
11128dcc1a9dSDamien Le Moal 	struct zonefs_super *super;
11138dcc1a9dSDamien Le Moal 	u32 crc, stored_crc;
11148dcc1a9dSDamien Le Moal 	struct page *page;
11158dcc1a9dSDamien Le Moal 	struct bio_vec bio_vec;
11168dcc1a9dSDamien Le Moal 	struct bio bio;
11178dcc1a9dSDamien Le Moal 	int ret;
11188dcc1a9dSDamien Le Moal 
11198dcc1a9dSDamien Le Moal 	page = alloc_page(GFP_KERNEL);
11208dcc1a9dSDamien Le Moal 	if (!page)
11218dcc1a9dSDamien Le Moal 		return -ENOMEM;
11228dcc1a9dSDamien Le Moal 
112349add496SChristoph Hellwig 	bio_init(&bio, sb->s_bdev, &bio_vec, 1, REQ_OP_READ);
11248dcc1a9dSDamien Le Moal 	bio.bi_iter.bi_sector = 0;
11250fa5b08cSJohannes Thumshirn 	__bio_add_page(&bio, page, PAGE_SIZE, 0);
11268dcc1a9dSDamien Le Moal 
11278dcc1a9dSDamien Le Moal 	ret = submit_bio_wait(&bio);
11288dcc1a9dSDamien Le Moal 	if (ret)
11298dcc1a9dSDamien Le Moal 		goto free_page;
11308dcc1a9dSDamien Le Moal 
11316bac30bbSFabio M. De Francesco 	super = page_address(page);
11328dcc1a9dSDamien Le Moal 
11338dcc1a9dSDamien Le Moal 	ret = -EINVAL;
11348dcc1a9dSDamien Le Moal 	if (le32_to_cpu(super->s_magic) != ZONEFS_MAGIC)
11356bac30bbSFabio M. De Francesco 		goto free_page;
11368dcc1a9dSDamien Le Moal 
11378dcc1a9dSDamien Le Moal 	stored_crc = le32_to_cpu(super->s_crc);
11388dcc1a9dSDamien Le Moal 	super->s_crc = 0;
11398dcc1a9dSDamien Le Moal 	crc = crc32(~0U, (unsigned char *)super, sizeof(struct zonefs_super));
11408dcc1a9dSDamien Le Moal 	if (crc != stored_crc) {
11418dcc1a9dSDamien Le Moal 		zonefs_err(sb, "Invalid checksum (Expected 0x%08x, got 0x%08x)",
11428dcc1a9dSDamien Le Moal 			   crc, stored_crc);
11436bac30bbSFabio M. De Francesco 		goto free_page;
11448dcc1a9dSDamien Le Moal 	}
11458dcc1a9dSDamien Le Moal 
11468dcc1a9dSDamien Le Moal 	sbi->s_features = le64_to_cpu(super->s_features);
11478dcc1a9dSDamien Le Moal 	if (sbi->s_features & ~ZONEFS_F_DEFINED_FEATURES) {
11488dcc1a9dSDamien Le Moal 		zonefs_err(sb, "Unknown features set 0x%llx\n",
11498dcc1a9dSDamien Le Moal 			   sbi->s_features);
11506bac30bbSFabio M. De Francesco 		goto free_page;
11518dcc1a9dSDamien Le Moal 	}
11528dcc1a9dSDamien Le Moal 
11538dcc1a9dSDamien Le Moal 	if (sbi->s_features & ZONEFS_F_UID) {
11548dcc1a9dSDamien Le Moal 		sbi->s_uid = make_kuid(current_user_ns(),
11558dcc1a9dSDamien Le Moal 				       le32_to_cpu(super->s_uid));
11568dcc1a9dSDamien Le Moal 		if (!uid_valid(sbi->s_uid)) {
11578dcc1a9dSDamien Le Moal 			zonefs_err(sb, "Invalid UID feature\n");
11586bac30bbSFabio M. De Francesco 			goto free_page;
11598dcc1a9dSDamien Le Moal 		}
11608dcc1a9dSDamien Le Moal 	}
11618dcc1a9dSDamien Le Moal 
11628dcc1a9dSDamien Le Moal 	if (sbi->s_features & ZONEFS_F_GID) {
11638dcc1a9dSDamien Le Moal 		sbi->s_gid = make_kgid(current_user_ns(),
11648dcc1a9dSDamien Le Moal 				       le32_to_cpu(super->s_gid));
11658dcc1a9dSDamien Le Moal 		if (!gid_valid(sbi->s_gid)) {
11668dcc1a9dSDamien Le Moal 			zonefs_err(sb, "Invalid GID feature\n");
11676bac30bbSFabio M. De Francesco 			goto free_page;
11688dcc1a9dSDamien Le Moal 		}
11698dcc1a9dSDamien Le Moal 	}
11708dcc1a9dSDamien Le Moal 
11718dcc1a9dSDamien Le Moal 	if (sbi->s_features & ZONEFS_F_PERM)
11728dcc1a9dSDamien Le Moal 		sbi->s_perm = le32_to_cpu(super->s_perm);
11738dcc1a9dSDamien Le Moal 
11748dcc1a9dSDamien Le Moal 	if (memchr_inv(super->s_reserved, 0, sizeof(super->s_reserved))) {
11758dcc1a9dSDamien Le Moal 		zonefs_err(sb, "Reserved area is being used\n");
11766bac30bbSFabio M. De Francesco 		goto free_page;
11778dcc1a9dSDamien Le Moal 	}
11788dcc1a9dSDamien Le Moal 
1179568776f9SAndy Shevchenko 	import_uuid(&sbi->s_uuid, super->s_uuid);
11808dcc1a9dSDamien Le Moal 	ret = 0;
11818dcc1a9dSDamien Le Moal 
11828dcc1a9dSDamien Le Moal free_page:
11838dcc1a9dSDamien Le Moal 	__free_page(page);
11848dcc1a9dSDamien Le Moal 
11858dcc1a9dSDamien Le Moal 	return ret;
11868dcc1a9dSDamien Le Moal }
11878dcc1a9dSDamien Le Moal 
11884008e2a0SDamien Le Moal static const struct super_operations zonefs_sops = {
11894008e2a0SDamien Le Moal 	.alloc_inode	= zonefs_alloc_inode,
11904008e2a0SDamien Le Moal 	.free_inode	= zonefs_free_inode,
11914008e2a0SDamien Le Moal 	.statfs		= zonefs_statfs,
11924008e2a0SDamien Le Moal 	.show_options	= zonefs_show_options,
11934008e2a0SDamien Le Moal };
11944008e2a0SDamien Le Moal 
zonefs_get_zgroup_inodes(struct super_block * sb)119543592c46SDamien Le Moal static int zonefs_get_zgroup_inodes(struct super_block *sb)
119643592c46SDamien Le Moal {
119743592c46SDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
119843592c46SDamien Le Moal 	struct inode *dir_inode;
119943592c46SDamien Le Moal 	enum zonefs_ztype ztype;
120043592c46SDamien Le Moal 
120143592c46SDamien Le Moal 	for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
120243592c46SDamien Le Moal 		if (!sbi->s_zgroup[ztype].g_nr_zones)
120343592c46SDamien Le Moal 			continue;
120443592c46SDamien Le Moal 
120543592c46SDamien Le Moal 		dir_inode = zonefs_get_zgroup_inode(sb, ztype);
120643592c46SDamien Le Moal 		if (IS_ERR(dir_inode))
120743592c46SDamien Le Moal 			return PTR_ERR(dir_inode);
120843592c46SDamien Le Moal 
120943592c46SDamien Le Moal 		sbi->s_zgroup[ztype].g_inode = dir_inode;
121043592c46SDamien Le Moal 	}
121143592c46SDamien Le Moal 
121243592c46SDamien Le Moal 	return 0;
121343592c46SDamien Le Moal }
121443592c46SDamien Le Moal 
zonefs_release_zgroup_inodes(struct super_block * sb)121543592c46SDamien Le Moal static void zonefs_release_zgroup_inodes(struct super_block *sb)
121643592c46SDamien Le Moal {
121743592c46SDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
121843592c46SDamien Le Moal 	enum zonefs_ztype ztype;
121943592c46SDamien Le Moal 
122043592c46SDamien Le Moal 	if (!sbi)
122143592c46SDamien Le Moal 		return;
122243592c46SDamien Le Moal 
122343592c46SDamien Le Moal 	for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
122443592c46SDamien Le Moal 		if (sbi->s_zgroup[ztype].g_inode) {
122543592c46SDamien Le Moal 			iput(sbi->s_zgroup[ztype].g_inode);
122643592c46SDamien Le Moal 			sbi->s_zgroup[ztype].g_inode = NULL;
122743592c46SDamien Le Moal 		}
122843592c46SDamien Le Moal 	}
122943592c46SDamien Le Moal }
123043592c46SDamien Le Moal 
12318dcc1a9dSDamien Le Moal /*
12328dcc1a9dSDamien Le Moal  * Check that the device is zoned. If it is, get the list of zones and create
12338dcc1a9dSDamien Le Moal  * sub-directories and files according to the device zone configuration and
12348dcc1a9dSDamien Le Moal  * format options.
12358dcc1a9dSDamien Le Moal  */
zonefs_fill_super(struct super_block * sb,struct fs_context * fc)1236567e629fSBill O'Donnell static int zonefs_fill_super(struct super_block *sb, struct fs_context *fc)
12378dcc1a9dSDamien Le Moal {
12388dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi;
1239567e629fSBill O'Donnell 	struct zonefs_context *ctx = fc->fs_private;
12408dcc1a9dSDamien Le Moal 	struct inode *inode;
1241d207794aSDamien Le Moal 	enum zonefs_ztype ztype;
12428dcc1a9dSDamien Le Moal 	int ret;
12438dcc1a9dSDamien Le Moal 
12448dcc1a9dSDamien Le Moal 	if (!bdev_is_zoned(sb->s_bdev)) {
12458dcc1a9dSDamien Le Moal 		zonefs_err(sb, "Not a zoned block device\n");
12468dcc1a9dSDamien Le Moal 		return -EINVAL;
12478dcc1a9dSDamien Le Moal 	}
12488dcc1a9dSDamien Le Moal 
12498dcc1a9dSDamien Le Moal 	/*
12508dcc1a9dSDamien Le Moal 	 * Initialize super block information: the maximum file size is updated
12518dcc1a9dSDamien Le Moal 	 * when the zone files are created so that the format option
12528dcc1a9dSDamien Le Moal 	 * ZONEFS_F_AGGRCNV which increases the maximum file size of a file
12538dcc1a9dSDamien Le Moal 	 * beyond the zone size is taken into account.
12548dcc1a9dSDamien Le Moal 	 */
12558dcc1a9dSDamien Le Moal 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
12568dcc1a9dSDamien Le Moal 	if (!sbi)
12578dcc1a9dSDamien Le Moal 		return -ENOMEM;
12588dcc1a9dSDamien Le Moal 
12598dcc1a9dSDamien Le Moal 	spin_lock_init(&sbi->s_lock);
12608dcc1a9dSDamien Le Moal 	sb->s_fs_info = sbi;
12618dcc1a9dSDamien Le Moal 	sb->s_magic = ZONEFS_MAGIC;
12628dcc1a9dSDamien Le Moal 	sb->s_maxbytes = 0;
12638dcc1a9dSDamien Le Moal 	sb->s_op = &zonefs_sops;
12648dcc1a9dSDamien Le Moal 	sb->s_time_gran	= 1;
12658dcc1a9dSDamien Le Moal 
12668dcc1a9dSDamien Le Moal 	/*
12670f1ba5f5SDamien Le Moal 	 * The block size is set to the device zone write granularity to ensure
12680f1ba5f5SDamien Le Moal 	 * that write operations are always aligned according to the device
12690f1ba5f5SDamien Le Moal 	 * interface constraints.
12708dcc1a9dSDamien Le Moal 	 */
12710f1ba5f5SDamien Le Moal 	sb_set_blocksize(sb, bdev_zone_write_granularity(sb->s_bdev));
12728dcc1a9dSDamien Le Moal 	sbi->s_zone_sectors_shift = ilog2(bdev_zone_sectors(sb->s_bdev));
12738dcc1a9dSDamien Le Moal 	sbi->s_uid = GLOBAL_ROOT_UID;
12748dcc1a9dSDamien Le Moal 	sbi->s_gid = GLOBAL_ROOT_GID;
12758dcc1a9dSDamien Le Moal 	sbi->s_perm = 0640;
1276567e629fSBill O'Donnell 	sbi->s_mount_opts = ctx->s_mount_opts;
12772b95a23cSDamien Le Moal 
12782b95a23cSDamien Le Moal 	atomic_set(&sbi->s_wro_seq_files, 0);
12792b95a23cSDamien Le Moal 	sbi->s_max_wro_seq_files = bdev_max_open_zones(sb->s_bdev);
128087c9ce3fSDamien Le Moal 	atomic_set(&sbi->s_active_seq_files, 0);
128187c9ce3fSDamien Le Moal 	sbi->s_max_active_seq_files = bdev_max_active_zones(sb->s_bdev);
128287c9ce3fSDamien Le Moal 
12838dcc1a9dSDamien Le Moal 	ret = zonefs_read_super(sb);
12848dcc1a9dSDamien Le Moal 	if (ret)
12858dcc1a9dSDamien Le Moal 		return ret;
12868dcc1a9dSDamien Le Moal 
1287b623e347SChristoph Hellwig 	zonefs_info(sb, "Mounting %u zones", bdev_nr_zones(sb->s_bdev));
12888dcc1a9dSDamien Le Moal 
1289a2a513beSDamien Le Moal 	if (!sbi->s_max_wro_seq_files &&
129096eca145SDamien Le Moal 	    !sbi->s_max_active_seq_files &&
1291a2a513beSDamien Le Moal 	    sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) {
129296eca145SDamien Le Moal 		zonefs_info(sb,
129396eca145SDamien Le Moal 			"No open and active zone limits. Ignoring explicit_open mount option\n");
1294a2a513beSDamien Le Moal 		sbi->s_mount_opts &= ~ZONEFS_MNTOPT_EXPLICIT_OPEN;
1295a2a513beSDamien Le Moal 	}
1296a2a513beSDamien Le Moal 
1297aa7f243fSDamien Le Moal 	/* Initialize the zone groups */
1298aa7f243fSDamien Le Moal 	ret = zonefs_init_zgroups(sb);
1299aa7f243fSDamien Le Moal 	if (ret)
1300aa7f243fSDamien Le Moal 		goto cleanup;
1301aa7f243fSDamien Le Moal 
1302d207794aSDamien Le Moal 	/* Create the root directory inode */
13038dcc1a9dSDamien Le Moal 	ret = -ENOMEM;
13048dcc1a9dSDamien Le Moal 	inode = new_inode(sb);
13058dcc1a9dSDamien Le Moal 	if (!inode)
13068dcc1a9dSDamien Le Moal 		goto cleanup;
13078dcc1a9dSDamien Le Moal 
1308b623e347SChristoph Hellwig 	inode->i_ino = bdev_nr_zones(sb->s_bdev);
13098dcc1a9dSDamien Le Moal 	inode->i_mode = S_IFDIR | 0555;
13108df379a3SJeff Layton 	simple_inode_init_ts(inode);
13118dcc1a9dSDamien Le Moal 	inode->i_op = &zonefs_dir_inode_operations;
1312d207794aSDamien Le Moal 	inode->i_fop = &zonefs_dir_operations;
1313d207794aSDamien Le Moal 	inode->i_size = 2;
13148dcc1a9dSDamien Le Moal 	set_nlink(inode, 2);
1315d207794aSDamien Le Moal 	for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
1316d207794aSDamien Le Moal 		if (sbi->s_zgroup[ztype].g_nr_zones) {
1317d207794aSDamien Le Moal 			inc_nlink(inode);
1318d207794aSDamien Le Moal 			inode->i_size++;
1319d207794aSDamien Le Moal 		}
1320d207794aSDamien Le Moal 	}
13218dcc1a9dSDamien Le Moal 
13228dcc1a9dSDamien Le Moal 	sb->s_root = d_make_root(inode);
13238dcc1a9dSDamien Le Moal 	if (!sb->s_root)
13248dcc1a9dSDamien Le Moal 		goto cleanup;
13258dcc1a9dSDamien Le Moal 
132643592c46SDamien Le Moal 	/*
132743592c46SDamien Le Moal 	 * Take a reference on the zone groups directory inodes
132843592c46SDamien Le Moal 	 * to keep them in the inode cache.
132943592c46SDamien Le Moal 	 */
133043592c46SDamien Le Moal 	ret = zonefs_get_zgroup_inodes(sb);
13318dcc1a9dSDamien Le Moal 	if (ret)
133243592c46SDamien Le Moal 		goto cleanup;
133343592c46SDamien Le Moal 
1334aa7f243fSDamien Le Moal 	ret = zonefs_sysfs_register(sb);
1335aa7f243fSDamien Le Moal 	if (ret)
1336aa7f243fSDamien Le Moal 		goto cleanup;
1337aa7f243fSDamien Le Moal 
1338aa7f243fSDamien Le Moal 	return 0;
13398dcc1a9dSDamien Le Moal 
13408dcc1a9dSDamien Le Moal cleanup:
134143592c46SDamien Le Moal 	zonefs_release_zgroup_inodes(sb);
1342aa7f243fSDamien Le Moal 	zonefs_free_zgroups(sb);
13438dcc1a9dSDamien Le Moal 
13448dcc1a9dSDamien Le Moal 	return ret;
13458dcc1a9dSDamien Le Moal }
13468dcc1a9dSDamien Le Moal 
zonefs_kill_super(struct super_block * sb)13478dcc1a9dSDamien Le Moal static void zonefs_kill_super(struct super_block *sb)
13488dcc1a9dSDamien Le Moal {
13498dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
13508dcc1a9dSDamien Le Moal 
135143592c46SDamien Le Moal 	/* Release the reference on the zone group directory inodes */
135243592c46SDamien Le Moal 	zonefs_release_zgroup_inodes(sb);
135343592c46SDamien Le Moal 
1354d207794aSDamien Le Moal 	kill_block_super(sb);
13559277a6d4SDamien Le Moal 
13569277a6d4SDamien Le Moal 	zonefs_sysfs_unregister(sb);
1357aa7f243fSDamien Le Moal 	zonefs_free_zgroups(sb);
13588dcc1a9dSDamien Le Moal 	kfree(sbi);
13598dcc1a9dSDamien Le Moal }
13608dcc1a9dSDamien Le Moal 
zonefs_free_fc(struct fs_context * fc)1361567e629fSBill O'Donnell static void zonefs_free_fc(struct fs_context *fc)
1362567e629fSBill O'Donnell {
1363567e629fSBill O'Donnell 	struct zonefs_context *ctx = fc->fs_private;
1364567e629fSBill O'Donnell 
1365567e629fSBill O'Donnell 	kfree(ctx);
1366567e629fSBill O'Donnell }
1367567e629fSBill O'Donnell 
zonefs_get_tree(struct fs_context * fc)1368567e629fSBill O'Donnell static int zonefs_get_tree(struct fs_context *fc)
1369567e629fSBill O'Donnell {
1370567e629fSBill O'Donnell 	return get_tree_bdev(fc, zonefs_fill_super);
1371567e629fSBill O'Donnell }
1372567e629fSBill O'Donnell 
zonefs_reconfigure(struct fs_context * fc)1373567e629fSBill O'Donnell static int zonefs_reconfigure(struct fs_context *fc)
1374567e629fSBill O'Donnell {
1375567e629fSBill O'Donnell 	struct zonefs_context *ctx = fc->fs_private;
1376567e629fSBill O'Donnell 	struct super_block *sb = fc->root->d_sb;
1377567e629fSBill O'Donnell 	struct zonefs_sb_info *sbi = sb->s_fs_info;
1378567e629fSBill O'Donnell 
1379567e629fSBill O'Donnell 	sync_filesystem(fc->root->d_sb);
1380567e629fSBill O'Donnell 	/* Copy new options from ctx into sbi. */
1381567e629fSBill O'Donnell 	sbi->s_mount_opts = ctx->s_mount_opts;
1382567e629fSBill O'Donnell 
1383567e629fSBill O'Donnell 	return 0;
1384567e629fSBill O'Donnell }
1385567e629fSBill O'Donnell 
1386567e629fSBill O'Donnell static const struct fs_context_operations zonefs_context_ops = {
1387567e629fSBill O'Donnell 	.parse_param    = zonefs_parse_param,
1388567e629fSBill O'Donnell 	.get_tree       = zonefs_get_tree,
1389567e629fSBill O'Donnell 	.reconfigure	= zonefs_reconfigure,
1390567e629fSBill O'Donnell 	.free           = zonefs_free_fc,
1391567e629fSBill O'Donnell };
1392567e629fSBill O'Donnell 
1393567e629fSBill O'Donnell /*
1394567e629fSBill O'Donnell  * Set up the filesystem mount context.
1395567e629fSBill O'Donnell  */
zonefs_init_fs_context(struct fs_context * fc)1396567e629fSBill O'Donnell static int zonefs_init_fs_context(struct fs_context *fc)
1397567e629fSBill O'Donnell {
1398567e629fSBill O'Donnell 	struct zonefs_context *ctx;
1399567e629fSBill O'Donnell 
1400567e629fSBill O'Donnell 	ctx = kzalloc(sizeof(struct zonefs_context), GFP_KERNEL);
1401567e629fSBill O'Donnell 	if (!ctx)
1402567e629fSBill O'Donnell 		return -ENOMEM;
1403567e629fSBill O'Donnell 	ctx->s_mount_opts = ZONEFS_MNTOPT_ERRORS_RO;
1404567e629fSBill O'Donnell 	fc->ops = &zonefs_context_ops;
1405567e629fSBill O'Donnell 	fc->fs_private = ctx;
1406567e629fSBill O'Donnell 
1407567e629fSBill O'Donnell 	return 0;
1408567e629fSBill O'Donnell }
1409567e629fSBill O'Donnell 
14108dcc1a9dSDamien Le Moal /*
14118dcc1a9dSDamien Le Moal  * File system definition and registration.
14128dcc1a9dSDamien Le Moal  */
14138dcc1a9dSDamien Le Moal static struct file_system_type zonefs_type = {
14148dcc1a9dSDamien Le Moal 	.owner			= THIS_MODULE,
14158dcc1a9dSDamien Le Moal 	.name			= "zonefs",
14168dcc1a9dSDamien Le Moal 	.kill_sb		= zonefs_kill_super,
14178dcc1a9dSDamien Le Moal 	.fs_flags		= FS_REQUIRES_DEV,
1418567e629fSBill O'Donnell 	.init_fs_context	= zonefs_init_fs_context,
1419567e629fSBill O'Donnell 	.parameters		= zonefs_param_spec,
14208dcc1a9dSDamien Le Moal };
14218dcc1a9dSDamien Le Moal 
zonefs_init_inodecache(void)14228dcc1a9dSDamien Le Moal static int __init zonefs_init_inodecache(void)
14238dcc1a9dSDamien Le Moal {
14248dcc1a9dSDamien Le Moal 	zonefs_inode_cachep = kmem_cache_create("zonefs_inode_cache",
14258dcc1a9dSDamien Le Moal 			sizeof(struct zonefs_inode_info), 0,
1426f88c3fb8SLinus Torvalds 			SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT,
14278dcc1a9dSDamien Le Moal 			NULL);
14288dcc1a9dSDamien Le Moal 	if (zonefs_inode_cachep == NULL)
14298dcc1a9dSDamien Le Moal 		return -ENOMEM;
14308dcc1a9dSDamien Le Moal 	return 0;
14318dcc1a9dSDamien Le Moal }
14328dcc1a9dSDamien Le Moal 
zonefs_destroy_inodecache(void)14338dcc1a9dSDamien Le Moal static void zonefs_destroy_inodecache(void)
14348dcc1a9dSDamien Le Moal {
14358dcc1a9dSDamien Le Moal 	/*
14368dcc1a9dSDamien Le Moal 	 * Make sure all delayed rcu free inodes are flushed before we
14378dcc1a9dSDamien Le Moal 	 * destroy the inode cache.
14388dcc1a9dSDamien Le Moal 	 */
14398dcc1a9dSDamien Le Moal 	rcu_barrier();
14408dcc1a9dSDamien Le Moal 	kmem_cache_destroy(zonefs_inode_cachep);
14418dcc1a9dSDamien Le Moal }
14428dcc1a9dSDamien Le Moal 
zonefs_init(void)14438dcc1a9dSDamien Le Moal static int __init zonefs_init(void)
14448dcc1a9dSDamien Le Moal {
14458dcc1a9dSDamien Le Moal 	int ret;
14468dcc1a9dSDamien Le Moal 
14478dcc1a9dSDamien Le Moal 	BUILD_BUG_ON(sizeof(struct zonefs_super) != ZONEFS_SUPER_SIZE);
14488dcc1a9dSDamien Le Moal 
144916d7fd3cSDamien Le Moal 	ret = zonefs_init_inodecache();
145016d7fd3cSDamien Le Moal 	if (ret)
1451fe9da61fSDamien Le Moal 		return ret;
145216d7fd3cSDamien Le Moal 
14534e458869SZhang Xiaoxu 	ret = zonefs_sysfs_init();
14549277a6d4SDamien Le Moal 	if (ret)
14559277a6d4SDamien Le Moal 		goto destroy_inodecache;
14569277a6d4SDamien Le Moal 
14574e458869SZhang Xiaoxu 	ret = register_filesystem(&zonefs_type);
14589277a6d4SDamien Le Moal 	if (ret)
14594e458869SZhang Xiaoxu 		goto sysfs_exit;
14608dcc1a9dSDamien Le Moal 
14618dcc1a9dSDamien Le Moal 	return 0;
14629277a6d4SDamien Le Moal 
14634e458869SZhang Xiaoxu sysfs_exit:
14644e458869SZhang Xiaoxu 	zonefs_sysfs_exit();
14659277a6d4SDamien Le Moal destroy_inodecache:
14669277a6d4SDamien Le Moal 	zonefs_destroy_inodecache();
14679277a6d4SDamien Le Moal 
14689277a6d4SDamien Le Moal 	return ret;
14698dcc1a9dSDamien Le Moal }
14708dcc1a9dSDamien Le Moal 
zonefs_exit(void)14718dcc1a9dSDamien Le Moal static void __exit zonefs_exit(void)
14728dcc1a9dSDamien Le Moal {
14734e458869SZhang Xiaoxu 	unregister_filesystem(&zonefs_type);
14749277a6d4SDamien Le Moal 	zonefs_sysfs_exit();
14758dcc1a9dSDamien Le Moal 	zonefs_destroy_inodecache();
14768dcc1a9dSDamien Le Moal }
14778dcc1a9dSDamien Le Moal 
14788dcc1a9dSDamien Le Moal MODULE_AUTHOR("Damien Le Moal");
14798dcc1a9dSDamien Le Moal MODULE_DESCRIPTION("Zone file system for zoned block devices");
14808dcc1a9dSDamien Le Moal MODULE_LICENSE("GPL");
14818ffea259SNaohiro Aota MODULE_ALIAS_FS("zonefs");
14828dcc1a9dSDamien Le Moal module_init(zonefs_init);
14838dcc1a9dSDamien Le Moal module_exit(zonefs_exit);
1484