xref: /openbmc/linux/fs/zonefs/super.c (revision a608da3bd730d718f2d3ebec1c26f9865f8f17ce)
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 /*
3187c9ce3fSDamien Le Moal  * Manage the active zone count. Called with zi->i_truncate_mutex held.
3287c9ce3fSDamien Le Moal  */
3387c9ce3fSDamien Le Moal static void zonefs_account_active(struct inode *inode)
3487c9ce3fSDamien Le Moal {
3587c9ce3fSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
3687c9ce3fSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
3787c9ce3fSDamien Le Moal 
3887c9ce3fSDamien Le Moal 	lockdep_assert_held(&zi->i_truncate_mutex);
3987c9ce3fSDamien Le Moal 
4087c9ce3fSDamien Le Moal 	if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
4187c9ce3fSDamien Le Moal 		return;
4287c9ce3fSDamien Le Moal 
4387c9ce3fSDamien Le Moal 	/*
44db58653cSDamien Le Moal 	 * For zones that transitioned to the offline or readonly condition,
45db58653cSDamien Le Moal 	 * we only need to clear the active state.
46db58653cSDamien Le Moal 	 */
47db58653cSDamien Le Moal 	if (zi->i_flags & (ZONEFS_ZONE_OFFLINE | ZONEFS_ZONE_READONLY))
48db58653cSDamien Le Moal 		goto out;
49db58653cSDamien Le Moal 
50db58653cSDamien Le Moal 	/*
5187c9ce3fSDamien Le Moal 	 * If the zone is active, that is, if it is explicitly open or
5287c9ce3fSDamien Le Moal 	 * partially written, check if it was already accounted as active.
5387c9ce3fSDamien Le Moal 	 */
5487c9ce3fSDamien Le Moal 	if ((zi->i_flags & ZONEFS_ZONE_OPEN) ||
5587c9ce3fSDamien Le Moal 	    (zi->i_wpoffset > 0 && zi->i_wpoffset < zi->i_max_size)) {
5687c9ce3fSDamien Le Moal 		if (!(zi->i_flags & ZONEFS_ZONE_ACTIVE)) {
5787c9ce3fSDamien Le Moal 			zi->i_flags |= ZONEFS_ZONE_ACTIVE;
5887c9ce3fSDamien Le Moal 			atomic_inc(&sbi->s_active_seq_files);
5987c9ce3fSDamien Le Moal 		}
6087c9ce3fSDamien Le Moal 		return;
6187c9ce3fSDamien Le Moal 	}
6287c9ce3fSDamien Le Moal 
63db58653cSDamien Le Moal out:
6487c9ce3fSDamien Le Moal 	/* The zone is not active. If it was, update the active count */
6587c9ce3fSDamien Le Moal 	if (zi->i_flags & ZONEFS_ZONE_ACTIVE) {
6687c9ce3fSDamien Le Moal 		zi->i_flags &= ~ZONEFS_ZONE_ACTIVE;
6787c9ce3fSDamien Le Moal 		atomic_dec(&sbi->s_active_seq_files);
6887c9ce3fSDamien Le Moal 	}
6987c9ce3fSDamien Le Moal }
7087c9ce3fSDamien Le Moal 
71ff07a02eSBart Van Assche static inline int zonefs_zone_mgmt(struct inode *inode, enum req_op op)
725498d5f9SJohannes Thumshirn {
735498d5f9SJohannes Thumshirn 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
745498d5f9SJohannes Thumshirn 	int ret;
755498d5f9SJohannes Thumshirn 
765498d5f9SJohannes Thumshirn 	lockdep_assert_held(&zi->i_truncate_mutex);
775498d5f9SJohannes Thumshirn 
781da18a29SDamien Le Moal 	/*
791da18a29SDamien Le Moal 	 * With ZNS drives, closing an explicitly open zone that has not been
801da18a29SDamien Le Moal 	 * written will change the zone state to "closed", that is, the zone
811da18a29SDamien Le Moal 	 * will remain active. Since this can then cause failure of explicit
821da18a29SDamien Le Moal 	 * open operation on other zones if the drive active zone resources
831da18a29SDamien Le Moal 	 * are exceeded, make sure that the zone does not remain active by
841da18a29SDamien Le Moal 	 * resetting it.
851da18a29SDamien Le Moal 	 */
861da18a29SDamien Le Moal 	if (op == REQ_OP_ZONE_CLOSE && !zi->i_wpoffset)
871da18a29SDamien Le Moal 		op = REQ_OP_ZONE_RESET;
881da18a29SDamien Le Moal 
8962ab1aadSJohannes Thumshirn 	trace_zonefs_zone_mgmt(inode, op);
905498d5f9SJohannes Thumshirn 	ret = blkdev_zone_mgmt(inode->i_sb->s_bdev, op, zi->i_zsector,
915498d5f9SJohannes Thumshirn 			       zi->i_zone_size >> SECTOR_SHIFT, GFP_NOFS);
925498d5f9SJohannes Thumshirn 	if (ret) {
935498d5f9SJohannes Thumshirn 		zonefs_err(inode->i_sb,
945498d5f9SJohannes Thumshirn 			   "Zone management operation %s at %llu failed %d\n",
955498d5f9SJohannes Thumshirn 			   blk_op_str(op), zi->i_zsector, ret);
965498d5f9SJohannes Thumshirn 		return ret;
975498d5f9SJohannes Thumshirn 	}
985498d5f9SJohannes Thumshirn 
995498d5f9SJohannes Thumshirn 	return 0;
1005498d5f9SJohannes Thumshirn }
1015498d5f9SJohannes Thumshirn 
102b5c00e97SJohannes Thumshirn static inline void zonefs_i_size_write(struct inode *inode, loff_t isize)
103b5c00e97SJohannes Thumshirn {
104b5c00e97SJohannes Thumshirn 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
105b5c00e97SJohannes Thumshirn 
106b5c00e97SJohannes Thumshirn 	i_size_write(inode, isize);
107b5c00e97SJohannes Thumshirn 	/*
108b5c00e97SJohannes Thumshirn 	 * A full zone is no longer open/active and does not need
109b5c00e97SJohannes Thumshirn 	 * explicit closing.
110b5c00e97SJohannes Thumshirn 	 */
11187c9ce3fSDamien Le Moal 	if (isize >= zi->i_max_size) {
11287c9ce3fSDamien Le Moal 		struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
11387c9ce3fSDamien Le Moal 
11487c9ce3fSDamien Le Moal 		if (zi->i_flags & ZONEFS_ZONE_ACTIVE)
11587c9ce3fSDamien Le Moal 			atomic_dec(&sbi->s_active_seq_files);
11687c9ce3fSDamien Le Moal 		zi->i_flags &= ~(ZONEFS_ZONE_OPEN | ZONEFS_ZONE_ACTIVE);
11787c9ce3fSDamien Le Moal 	}
118b5c00e97SJohannes Thumshirn }
119b5c00e97SJohannes Thumshirn 
120c1c1204cSDamien Le Moal static int zonefs_read_iomap_begin(struct inode *inode, loff_t offset,
121c1c1204cSDamien Le Moal 				   loff_t length, unsigned int flags,
122c1c1204cSDamien Le Moal 				   struct iomap *iomap, struct iomap *srcmap)
1238dcc1a9dSDamien Le Moal {
1248dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
1258dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
1268dcc1a9dSDamien Le Moal 	loff_t isize;
1278dcc1a9dSDamien Le Moal 
128c1c1204cSDamien Le Moal 	/*
129c1c1204cSDamien Le Moal 	 * All blocks are always mapped below EOF. If reading past EOF,
130c1c1204cSDamien Le Moal 	 * act as if there is a hole up to the file maximum size.
131c1c1204cSDamien Le Moal 	 */
132c1c1204cSDamien Le Moal 	mutex_lock(&zi->i_truncate_mutex);
133c1c1204cSDamien Le Moal 	iomap->bdev = inode->i_sb->s_bdev;
134c1c1204cSDamien Le Moal 	iomap->offset = ALIGN_DOWN(offset, sb->s_blocksize);
135c1c1204cSDamien Le Moal 	isize = i_size_read(inode);
136c1c1204cSDamien Le Moal 	if (iomap->offset >= isize) {
137c1c1204cSDamien Le Moal 		iomap->type = IOMAP_HOLE;
138c1c1204cSDamien Le Moal 		iomap->addr = IOMAP_NULL_ADDR;
139c1c1204cSDamien Le Moal 		iomap->length = length;
140c1c1204cSDamien Le Moal 	} else {
141c1c1204cSDamien Le Moal 		iomap->type = IOMAP_MAPPED;
142c1c1204cSDamien Le Moal 		iomap->addr = (zi->i_zsector << SECTOR_SHIFT) + iomap->offset;
143c1c1204cSDamien Le Moal 		iomap->length = isize - iomap->offset;
144c1c1204cSDamien Le Moal 	}
145c1c1204cSDamien Le Moal 	mutex_unlock(&zi->i_truncate_mutex);
146c1c1204cSDamien Le Moal 
147c1c1204cSDamien Le Moal 	trace_zonefs_iomap_begin(inode, iomap);
148c1c1204cSDamien Le Moal 
149c1c1204cSDamien Le Moal 	return 0;
150c1c1204cSDamien Le Moal }
151c1c1204cSDamien Le Moal 
152c1c1204cSDamien Le Moal static const struct iomap_ops zonefs_read_iomap_ops = {
153c1c1204cSDamien Le Moal 	.iomap_begin	= zonefs_read_iomap_begin,
154c1c1204cSDamien Le Moal };
155c1c1204cSDamien Le Moal 
156c1c1204cSDamien Le Moal static int zonefs_write_iomap_begin(struct inode *inode, loff_t offset,
157c1c1204cSDamien Le Moal 				    loff_t length, unsigned int flags,
158c1c1204cSDamien Le Moal 				    struct iomap *iomap, struct iomap *srcmap)
159c1c1204cSDamien Le Moal {
160c1c1204cSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
161c1c1204cSDamien Le Moal 	struct super_block *sb = inode->i_sb;
162c1c1204cSDamien Le Moal 	loff_t isize;
163c1c1204cSDamien Le Moal 
164c1c1204cSDamien Le Moal 	/* All write I/Os should always be within the file maximum size */
1658dcc1a9dSDamien Le Moal 	if (WARN_ON_ONCE(offset + length > zi->i_max_size))
1668dcc1a9dSDamien Le Moal 		return -EIO;
1678dcc1a9dSDamien Le Moal 
1688dcc1a9dSDamien Le Moal 	/*
1698dcc1a9dSDamien Le Moal 	 * Sequential zones can only accept direct writes. This is already
1708dcc1a9dSDamien Le Moal 	 * checked when writes are issued, so warn if we see a page writeback
1718dcc1a9dSDamien Le Moal 	 * operation.
1728dcc1a9dSDamien Le Moal 	 */
1738dcc1a9dSDamien Le Moal 	if (WARN_ON_ONCE(zi->i_ztype == ZONEFS_ZTYPE_SEQ &&
174c1c1204cSDamien Le Moal 			 !(flags & IOMAP_DIRECT)))
1758dcc1a9dSDamien Le Moal 		return -EIO;
1768dcc1a9dSDamien Le Moal 
1778dcc1a9dSDamien Le Moal 	/*
1788dcc1a9dSDamien Le Moal 	 * For conventional zones, all blocks are always mapped. For sequential
1798dcc1a9dSDamien Le Moal 	 * zones, all blocks after always mapped below the inode size (zone
1808dcc1a9dSDamien Le Moal 	 * write pointer) and unwriten beyond.
1818dcc1a9dSDamien Le Moal 	 */
1828dcc1a9dSDamien Le Moal 	mutex_lock(&zi->i_truncate_mutex);
1838dcc1a9dSDamien Le Moal 	iomap->bdev = inode->i_sb->s_bdev;
184c1c1204cSDamien Le Moal 	iomap->offset = ALIGN_DOWN(offset, sb->s_blocksize);
1858dcc1a9dSDamien Le Moal 	iomap->addr = (zi->i_zsector << SECTOR_SHIFT) + iomap->offset;
186c1c1204cSDamien Le Moal 	isize = i_size_read(inode);
187c1c1204cSDamien Le Moal 	if (iomap->offset >= isize) {
188c1c1204cSDamien Le Moal 		iomap->type = IOMAP_UNWRITTEN;
189c1c1204cSDamien Le Moal 		iomap->length = zi->i_max_size - iomap->offset;
190c1c1204cSDamien Le Moal 	} else {
191c1c1204cSDamien Le Moal 		iomap->type = IOMAP_MAPPED;
192c1c1204cSDamien Le Moal 		iomap->length = isize - iomap->offset;
193c1c1204cSDamien Le Moal 	}
194c1c1204cSDamien Le Moal 	mutex_unlock(&zi->i_truncate_mutex);
1958dcc1a9dSDamien Le Moal 
19662ab1aadSJohannes Thumshirn 	trace_zonefs_iomap_begin(inode, iomap);
19762ab1aadSJohannes Thumshirn 
1988dcc1a9dSDamien Le Moal 	return 0;
1998dcc1a9dSDamien Le Moal }
2008dcc1a9dSDamien Le Moal 
201c1c1204cSDamien Le Moal static const struct iomap_ops zonefs_write_iomap_ops = {
202c1c1204cSDamien Le Moal 	.iomap_begin	= zonefs_write_iomap_begin,
2038dcc1a9dSDamien Le Moal };
2048dcc1a9dSDamien Le Moal 
2057479c505SMatthew Wilcox (Oracle) static int zonefs_read_folio(struct file *unused, struct folio *folio)
2068dcc1a9dSDamien Le Moal {
207c1c1204cSDamien Le Moal 	return iomap_read_folio(folio, &zonefs_read_iomap_ops);
2088dcc1a9dSDamien Le Moal }
2098dcc1a9dSDamien Le Moal 
2109d24a13aSMatthew Wilcox (Oracle) static void zonefs_readahead(struct readahead_control *rac)
2118dcc1a9dSDamien Le Moal {
212c1c1204cSDamien Le Moal 	iomap_readahead(rac, &zonefs_read_iomap_ops);
2138dcc1a9dSDamien Le Moal }
2148dcc1a9dSDamien Le Moal 
2158dcc1a9dSDamien Le Moal /*
2168dcc1a9dSDamien Le Moal  * Map blocks for page writeback. This is used only on conventional zone files,
2178dcc1a9dSDamien Le Moal  * which implies that the page range can only be within the fixed inode size.
2188dcc1a9dSDamien Le Moal  */
219c1c1204cSDamien Le Moal static int zonefs_write_map_blocks(struct iomap_writepage_ctx *wpc,
2208dcc1a9dSDamien Le Moal 				   struct inode *inode, loff_t offset)
2218dcc1a9dSDamien Le Moal {
2228dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
2238dcc1a9dSDamien Le Moal 
2248dcc1a9dSDamien Le Moal 	if (WARN_ON_ONCE(zi->i_ztype != ZONEFS_ZTYPE_CNV))
2258dcc1a9dSDamien Le Moal 		return -EIO;
2268dcc1a9dSDamien Le Moal 	if (WARN_ON_ONCE(offset >= i_size_read(inode)))
2278dcc1a9dSDamien Le Moal 		return -EIO;
2288dcc1a9dSDamien Le Moal 
2298dcc1a9dSDamien Le Moal 	/* If the mapping is already OK, nothing needs to be done */
2308dcc1a9dSDamien Le Moal 	if (offset >= wpc->iomap.offset &&
2318dcc1a9dSDamien Le Moal 	    offset < wpc->iomap.offset + wpc->iomap.length)
2328dcc1a9dSDamien Le Moal 		return 0;
2338dcc1a9dSDamien Le Moal 
234c1c1204cSDamien Le Moal 	return zonefs_write_iomap_begin(inode, offset, zi->i_max_size - offset,
2358dcc1a9dSDamien Le Moal 					IOMAP_WRITE, &wpc->iomap, NULL);
2368dcc1a9dSDamien Le Moal }
2378dcc1a9dSDamien Le Moal 
2388dcc1a9dSDamien Le Moal static const struct iomap_writeback_ops zonefs_writeback_ops = {
239c1c1204cSDamien Le Moal 	.map_blocks		= zonefs_write_map_blocks,
2408dcc1a9dSDamien Le Moal };
2418dcc1a9dSDamien Le Moal 
2428dcc1a9dSDamien Le Moal static int zonefs_writepages(struct address_space *mapping,
2438dcc1a9dSDamien Le Moal 			     struct writeback_control *wbc)
2448dcc1a9dSDamien Le Moal {
2458dcc1a9dSDamien Le Moal 	struct iomap_writepage_ctx wpc = { };
2468dcc1a9dSDamien Le Moal 
2478dcc1a9dSDamien Le Moal 	return iomap_writepages(mapping, wbc, &wpc, &zonefs_writeback_ops);
2488dcc1a9dSDamien Le Moal }
2498dcc1a9dSDamien Le Moal 
2501601ea06SDamien Le Moal static int zonefs_swap_activate(struct swap_info_struct *sis,
2511601ea06SDamien Le Moal 				struct file *swap_file, sector_t *span)
2521601ea06SDamien Le Moal {
2531601ea06SDamien Le Moal 	struct inode *inode = file_inode(swap_file);
2541601ea06SDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
2551601ea06SDamien Le Moal 
2561601ea06SDamien Le Moal 	if (zi->i_ztype != ZONEFS_ZTYPE_CNV) {
2571601ea06SDamien Le Moal 		zonefs_err(inode->i_sb,
2581601ea06SDamien Le Moal 			   "swap file: not a conventional zone file\n");
2591601ea06SDamien Le Moal 		return -EINVAL;
2601601ea06SDamien Le Moal 	}
2611601ea06SDamien Le Moal 
262c1c1204cSDamien Le Moal 	return iomap_swapfile_activate(sis, swap_file, span,
263c1c1204cSDamien Le Moal 				       &zonefs_read_iomap_ops);
2641601ea06SDamien Le Moal }
2651601ea06SDamien Le Moal 
2668dcc1a9dSDamien Le Moal static const struct address_space_operations zonefs_file_aops = {
2677479c505SMatthew Wilcox (Oracle) 	.read_folio		= zonefs_read_folio,
2689d24a13aSMatthew Wilcox (Oracle) 	.readahead		= zonefs_readahead,
2698dcc1a9dSDamien Le Moal 	.writepages		= zonefs_writepages,
270187c82cbSMatthew Wilcox (Oracle) 	.dirty_folio		= filemap_dirty_folio,
2718597447dSMatthew Wilcox (Oracle) 	.release_folio		= iomap_release_folio,
272d82354f6SMatthew Wilcox (Oracle) 	.invalidate_folio	= iomap_invalidate_folio,
2732ec810d5SMatthew Wilcox (Oracle) 	.migrate_folio		= filemap_migrate_folio,
2748dcc1a9dSDamien Le Moal 	.is_partially_uptodate	= iomap_is_partially_uptodate,
2758dcc1a9dSDamien Le Moal 	.error_remove_page	= generic_error_remove_page,
2768dcc1a9dSDamien Le Moal 	.direct_IO		= noop_direct_IO,
2771601ea06SDamien Le Moal 	.swap_activate		= zonefs_swap_activate,
2788dcc1a9dSDamien Le Moal };
2798dcc1a9dSDamien Le Moal 
2808dcc1a9dSDamien Le Moal static void zonefs_update_stats(struct inode *inode, loff_t new_isize)
2818dcc1a9dSDamien Le Moal {
2828dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
2838dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
2848dcc1a9dSDamien Le Moal 	loff_t old_isize = i_size_read(inode);
2858dcc1a9dSDamien Le Moal 	loff_t nr_blocks;
2868dcc1a9dSDamien Le Moal 
2878dcc1a9dSDamien Le Moal 	if (new_isize == old_isize)
2888dcc1a9dSDamien Le Moal 		return;
2898dcc1a9dSDamien Le Moal 
2908dcc1a9dSDamien Le Moal 	spin_lock(&sbi->s_lock);
2918dcc1a9dSDamien Le Moal 
2928dcc1a9dSDamien Le Moal 	/*
2938dcc1a9dSDamien Le Moal 	 * This may be called for an update after an IO error.
2948dcc1a9dSDamien Le Moal 	 * So beware of the values seen.
2958dcc1a9dSDamien Le Moal 	 */
2968dcc1a9dSDamien Le Moal 	if (new_isize < old_isize) {
2978dcc1a9dSDamien Le Moal 		nr_blocks = (old_isize - new_isize) >> sb->s_blocksize_bits;
2988dcc1a9dSDamien Le Moal 		if (sbi->s_used_blocks > nr_blocks)
2998dcc1a9dSDamien Le Moal 			sbi->s_used_blocks -= nr_blocks;
3008dcc1a9dSDamien Le Moal 		else
3018dcc1a9dSDamien Le Moal 			sbi->s_used_blocks = 0;
3028dcc1a9dSDamien Le Moal 	} else {
3038dcc1a9dSDamien Le Moal 		sbi->s_used_blocks +=
3048dcc1a9dSDamien Le Moal 			(new_isize - old_isize) >> sb->s_blocksize_bits;
3058dcc1a9dSDamien Le Moal 		if (sbi->s_used_blocks > sbi->s_blocks)
3068dcc1a9dSDamien Le Moal 			sbi->s_used_blocks = sbi->s_blocks;
3078dcc1a9dSDamien Le Moal 	}
3088dcc1a9dSDamien Le Moal 
3098dcc1a9dSDamien Le Moal 	spin_unlock(&sbi->s_lock);
3108dcc1a9dSDamien Le Moal }
3118dcc1a9dSDamien Le Moal 
3128dcc1a9dSDamien Le Moal /*
3138dcc1a9dSDamien Le Moal  * Check a zone condition and adjust its file inode access permissions for
3148dcc1a9dSDamien Le Moal  * offline and readonly zones. Return the inode size corresponding to the
3158dcc1a9dSDamien Le Moal  * amount of readable data in the zone.
3168dcc1a9dSDamien Le Moal  */
3178dcc1a9dSDamien Le Moal static loff_t zonefs_check_zone_condition(struct inode *inode,
318ccf4ad7dSDamien Le Moal 					  struct blk_zone *zone, bool warn,
319ccf4ad7dSDamien Le Moal 					  bool mount)
3208dcc1a9dSDamien Le Moal {
3218dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
3228dcc1a9dSDamien Le Moal 
3238dcc1a9dSDamien Le Moal 	switch (zone->cond) {
3248dcc1a9dSDamien Le Moal 	case BLK_ZONE_COND_OFFLINE:
3258dcc1a9dSDamien Le Moal 		/*
3268dcc1a9dSDamien Le Moal 		 * Dead zone: make the inode immutable, disable all accesses
3278dcc1a9dSDamien Le Moal 		 * and set the file size to 0 (zone wp set to zone start).
3288dcc1a9dSDamien Le Moal 		 */
3298dcc1a9dSDamien Le Moal 		if (warn)
3308dcc1a9dSDamien Le Moal 			zonefs_warn(inode->i_sb, "inode %lu: offline zone\n",
3318dcc1a9dSDamien Le Moal 				    inode->i_ino);
3328dcc1a9dSDamien Le Moal 		inode->i_flags |= S_IMMUTABLE;
3338dcc1a9dSDamien Le Moal 		inode->i_mode &= ~0777;
3348dcc1a9dSDamien Le Moal 		zone->wp = zone->start;
335db58653cSDamien Le Moal 		zi->i_flags |= ZONEFS_ZONE_OFFLINE;
3368dcc1a9dSDamien Le Moal 		return 0;
3378dcc1a9dSDamien Le Moal 	case BLK_ZONE_COND_READONLY:
338ccf4ad7dSDamien Le Moal 		/*
339ccf4ad7dSDamien Le Moal 		 * The write pointer of read-only zones is invalid. If such a
340ccf4ad7dSDamien Le Moal 		 * zone is found during mount, the file size cannot be retrieved
341ccf4ad7dSDamien Le Moal 		 * so we treat the zone as offline (mount == true case).
342ccf4ad7dSDamien Le Moal 		 * Otherwise, keep the file size as it was when last updated
343ccf4ad7dSDamien Le Moal 		 * so that the user can recover data. In both cases, writes are
344ccf4ad7dSDamien Le Moal 		 * always disabled for the zone.
345ccf4ad7dSDamien Le Moal 		 */
3468dcc1a9dSDamien Le Moal 		if (warn)
3478dcc1a9dSDamien Le Moal 			zonefs_warn(inode->i_sb, "inode %lu: read-only zone\n",
3488dcc1a9dSDamien Le Moal 				    inode->i_ino);
3498dcc1a9dSDamien Le Moal 		inode->i_flags |= S_IMMUTABLE;
350ccf4ad7dSDamien Le Moal 		if (mount) {
351ccf4ad7dSDamien Le Moal 			zone->cond = BLK_ZONE_COND_OFFLINE;
352ccf4ad7dSDamien Le Moal 			inode->i_mode &= ~0777;
353ccf4ad7dSDamien Le Moal 			zone->wp = zone->start;
354db58653cSDamien Le Moal 			zi->i_flags |= ZONEFS_ZONE_OFFLINE;
355ccf4ad7dSDamien Le Moal 			return 0;
356ccf4ad7dSDamien Le Moal 		}
357db58653cSDamien Le Moal 		zi->i_flags |= ZONEFS_ZONE_READONLY;
3588dcc1a9dSDamien Le Moal 		inode->i_mode &= ~0222;
359ccf4ad7dSDamien Le Moal 		return i_size_read(inode);
360059c0103SShin'ichiro Kawasaki 	case BLK_ZONE_COND_FULL:
361059c0103SShin'ichiro Kawasaki 		/* The write pointer of full zones is invalid. */
362059c0103SShin'ichiro Kawasaki 		return zi->i_max_size;
3638dcc1a9dSDamien Le Moal 	default:
3648dcc1a9dSDamien Le Moal 		if (zi->i_ztype == ZONEFS_ZTYPE_CNV)
3658dcc1a9dSDamien Le Moal 			return zi->i_max_size;
3668dcc1a9dSDamien Le Moal 		return (zone->wp - zone->start) << SECTOR_SHIFT;
3678dcc1a9dSDamien Le Moal 	}
3688dcc1a9dSDamien Le Moal }
3698dcc1a9dSDamien Le Moal 
3708dcc1a9dSDamien Le Moal struct zonefs_ioerr_data {
3718dcc1a9dSDamien Le Moal 	struct inode	*inode;
3728dcc1a9dSDamien Le Moal 	bool		write;
3738dcc1a9dSDamien Le Moal };
3748dcc1a9dSDamien Le Moal 
3758dcc1a9dSDamien Le Moal static int zonefs_io_error_cb(struct blk_zone *zone, unsigned int idx,
3768dcc1a9dSDamien Le Moal 			      void *data)
3778dcc1a9dSDamien Le Moal {
3788dcc1a9dSDamien Le Moal 	struct zonefs_ioerr_data *err = data;
3798dcc1a9dSDamien Le Moal 	struct inode *inode = err->inode;
3808dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
3818dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
3828dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
3838dcc1a9dSDamien Le Moal 	loff_t isize, data_size;
3848dcc1a9dSDamien Le Moal 
3858dcc1a9dSDamien Le Moal 	/*
3868dcc1a9dSDamien Le Moal 	 * Check the zone condition: if the zone is not "bad" (offline or
3878dcc1a9dSDamien Le Moal 	 * read-only), read errors are simply signaled to the IO issuer as long
3888dcc1a9dSDamien Le Moal 	 * as there is no inconsistency between the inode size and the amount of
3898dcc1a9dSDamien Le Moal 	 * data writen in the zone (data_size).
3908dcc1a9dSDamien Le Moal 	 */
391ccf4ad7dSDamien Le Moal 	data_size = zonefs_check_zone_condition(inode, zone, true, false);
3928dcc1a9dSDamien Le Moal 	isize = i_size_read(inode);
3938dcc1a9dSDamien Le Moal 	if (zone->cond != BLK_ZONE_COND_OFFLINE &&
3948dcc1a9dSDamien Le Moal 	    zone->cond != BLK_ZONE_COND_READONLY &&
3958dcc1a9dSDamien Le Moal 	    !err->write && isize == data_size)
3968dcc1a9dSDamien Le Moal 		return 0;
3978dcc1a9dSDamien Le Moal 
3988dcc1a9dSDamien Le Moal 	/*
3998dcc1a9dSDamien Le Moal 	 * At this point, we detected either a bad zone or an inconsistency
4008dcc1a9dSDamien Le Moal 	 * between the inode size and the amount of data written in the zone.
4018dcc1a9dSDamien Le Moal 	 * For the latter case, the cause may be a write IO error or an external
4028dcc1a9dSDamien Le Moal 	 * action on the device. Two error patterns exist:
4038dcc1a9dSDamien Le Moal 	 * 1) The inode size is lower than the amount of data in the zone:
4048dcc1a9dSDamien Le Moal 	 *    a write operation partially failed and data was writen at the end
4058dcc1a9dSDamien Le Moal 	 *    of the file. This can happen in the case of a large direct IO
4068dcc1a9dSDamien Le Moal 	 *    needing several BIOs and/or write requests to be processed.
4078dcc1a9dSDamien Le Moal 	 * 2) The inode size is larger than the amount of data in the zone:
4088dcc1a9dSDamien Le Moal 	 *    this can happen with a deferred write error with the use of the
4098dcc1a9dSDamien Le Moal 	 *    device side write cache after getting successful write IO
4108dcc1a9dSDamien Le Moal 	 *    completions. Other possibilities are (a) an external corruption,
4118dcc1a9dSDamien Le Moal 	 *    e.g. an application reset the zone directly, or (b) the device
4128dcc1a9dSDamien Le Moal 	 *    has a serious problem (e.g. firmware bug).
4138dcc1a9dSDamien Le Moal 	 *
4148dcc1a9dSDamien Le Moal 	 * In all cases, warn about inode size inconsistency and handle the
4158dcc1a9dSDamien Le Moal 	 * IO error according to the zone condition and to the mount options.
4168dcc1a9dSDamien Le Moal 	 */
4178dcc1a9dSDamien Le Moal 	if (zi->i_ztype == ZONEFS_ZTYPE_SEQ && isize != data_size)
4188dcc1a9dSDamien Le Moal 		zonefs_warn(sb, "inode %lu: invalid size %lld (should be %lld)\n",
4198dcc1a9dSDamien Le Moal 			    inode->i_ino, isize, data_size);
4208dcc1a9dSDamien Le Moal 
4218dcc1a9dSDamien Le Moal 	/*
4228dcc1a9dSDamien Le Moal 	 * First handle bad zones signaled by hardware. The mount options
4238dcc1a9dSDamien Le Moal 	 * errors=zone-ro and errors=zone-offline result in changing the
4248dcc1a9dSDamien Le Moal 	 * zone condition to read-only and offline respectively, as if the
4258dcc1a9dSDamien Le Moal 	 * condition was signaled by the hardware.
4268dcc1a9dSDamien Le Moal 	 */
4278dcc1a9dSDamien Le Moal 	if (zone->cond == BLK_ZONE_COND_OFFLINE ||
4288dcc1a9dSDamien Le Moal 	    sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL) {
4298dcc1a9dSDamien Le Moal 		zonefs_warn(sb, "inode %lu: read/write access disabled\n",
4308dcc1a9dSDamien Le Moal 			    inode->i_ino);
4318dcc1a9dSDamien Le Moal 		if (zone->cond != BLK_ZONE_COND_OFFLINE) {
4328dcc1a9dSDamien Le Moal 			zone->cond = BLK_ZONE_COND_OFFLINE;
4338dcc1a9dSDamien Le Moal 			data_size = zonefs_check_zone_condition(inode, zone,
434ccf4ad7dSDamien Le Moal 								false, false);
4358dcc1a9dSDamien Le Moal 		}
4368dcc1a9dSDamien Le Moal 	} else if (zone->cond == BLK_ZONE_COND_READONLY ||
4378dcc1a9dSDamien Le Moal 		   sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO) {
4388dcc1a9dSDamien Le Moal 		zonefs_warn(sb, "inode %lu: write access disabled\n",
4398dcc1a9dSDamien Le Moal 			    inode->i_ino);
4408dcc1a9dSDamien Le Moal 		if (zone->cond != BLK_ZONE_COND_READONLY) {
4418dcc1a9dSDamien Le Moal 			zone->cond = BLK_ZONE_COND_READONLY;
4428dcc1a9dSDamien Le Moal 			data_size = zonefs_check_zone_condition(inode, zone,
443ccf4ad7dSDamien Le Moal 								false, false);
4448dcc1a9dSDamien Le Moal 		}
445*a608da3bSDamien Le Moal 	} else if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO &&
446*a608da3bSDamien Le Moal 		   data_size > isize) {
447*a608da3bSDamien Le Moal 		/* Do not expose garbage data */
448*a608da3bSDamien Le Moal 		data_size = isize;
4498dcc1a9dSDamien Le Moal 	}
4508dcc1a9dSDamien Le Moal 
4518dcc1a9dSDamien Le Moal 	/*
452b5c00e97SJohannes Thumshirn 	 * If the filesystem is mounted with the explicit-open mount option, we
453b5c00e97SJohannes Thumshirn 	 * need to clear the ZONEFS_ZONE_OPEN flag if the zone transitioned to
454b5c00e97SJohannes Thumshirn 	 * the read-only or offline condition, to avoid attempting an explicit
455b5c00e97SJohannes Thumshirn 	 * close of the zone when the inode file is closed.
456b5c00e97SJohannes Thumshirn 	 */
457b5c00e97SJohannes Thumshirn 	if ((sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) &&
458b5c00e97SJohannes Thumshirn 	    (zone->cond == BLK_ZONE_COND_OFFLINE ||
459b5c00e97SJohannes Thumshirn 	     zone->cond == BLK_ZONE_COND_READONLY))
460b5c00e97SJohannes Thumshirn 		zi->i_flags &= ~ZONEFS_ZONE_OPEN;
461b5c00e97SJohannes Thumshirn 
462b5c00e97SJohannes Thumshirn 	/*
4638dcc1a9dSDamien Le Moal 	 * If error=remount-ro was specified, any error result in remounting
4648dcc1a9dSDamien Le Moal 	 * the volume as read-only.
4658dcc1a9dSDamien Le Moal 	 */
4668dcc1a9dSDamien Le Moal 	if ((sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO) && !sb_rdonly(sb)) {
4678dcc1a9dSDamien Le Moal 		zonefs_warn(sb, "remounting filesystem read-only\n");
4688dcc1a9dSDamien Le Moal 		sb->s_flags |= SB_RDONLY;
4698dcc1a9dSDamien Le Moal 	}
4708dcc1a9dSDamien Le Moal 
4718dcc1a9dSDamien Le Moal 	/*
4728dcc1a9dSDamien Le Moal 	 * Update block usage stats and the inode size  to prevent access to
4738dcc1a9dSDamien Le Moal 	 * invalid data.
4748dcc1a9dSDamien Le Moal 	 */
4758dcc1a9dSDamien Le Moal 	zonefs_update_stats(inode, data_size);
476b5c00e97SJohannes Thumshirn 	zonefs_i_size_write(inode, data_size);
4778dcc1a9dSDamien Le Moal 	zi->i_wpoffset = data_size;
47887c9ce3fSDamien Le Moal 	zonefs_account_active(inode);
4798dcc1a9dSDamien Le Moal 
4808dcc1a9dSDamien Le Moal 	return 0;
4818dcc1a9dSDamien Le Moal }
4828dcc1a9dSDamien Le Moal 
4838dcc1a9dSDamien Le Moal /*
4848dcc1a9dSDamien Le Moal  * When an file IO error occurs, check the file zone to see if there is a change
4858dcc1a9dSDamien Le Moal  * in the zone condition (e.g. offline or read-only). For a failed write to a
4868dcc1a9dSDamien Le Moal  * sequential zone, the zone write pointer position must also be checked to
4878dcc1a9dSDamien Le Moal  * eventually correct the file size and zonefs inode write pointer offset
4888dcc1a9dSDamien Le Moal  * (which can be out of sync with the drive due to partial write failures).
4898dcc1a9dSDamien Le Moal  */
49048d546a8SJohannes Thumshirn static void __zonefs_io_error(struct inode *inode, bool write)
4918dcc1a9dSDamien Le Moal {
4928dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
4938dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
4948dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
4958dcc1a9dSDamien Le Moal 	unsigned int noio_flag;
4967dd12d65SDamien Le Moal 	unsigned int nr_zones = 1;
4978dcc1a9dSDamien Le Moal 	struct zonefs_ioerr_data err = {
4988dcc1a9dSDamien Le Moal 		.inode = inode,
4998dcc1a9dSDamien Le Moal 		.write = write,
5008dcc1a9dSDamien Le Moal 	};
5018dcc1a9dSDamien Le Moal 	int ret;
5028dcc1a9dSDamien Le Moal 
5038dcc1a9dSDamien Le Moal 	/*
5047dd12d65SDamien Le Moal 	 * The only files that have more than one zone are conventional zone
5057dd12d65SDamien Le Moal 	 * files with aggregated conventional zones, for which the inode zone
5067dd12d65SDamien Le Moal 	 * size is always larger than the device zone size.
5077dd12d65SDamien Le Moal 	 */
5087dd12d65SDamien Le Moal 	if (zi->i_zone_size > bdev_zone_sectors(sb->s_bdev))
5097dd12d65SDamien Le Moal 		nr_zones = zi->i_zone_size >>
5107dd12d65SDamien Le Moal 			(sbi->s_zone_sectors_shift + SECTOR_SHIFT);
5117dd12d65SDamien Le Moal 
5127dd12d65SDamien Le Moal 	/*
5138dcc1a9dSDamien Le Moal 	 * Memory allocations in blkdev_report_zones() can trigger a memory
5148dcc1a9dSDamien Le Moal 	 * reclaim which may in turn cause a recursion into zonefs as well as
5158dcc1a9dSDamien Le Moal 	 * struct request allocations for the same device. The former case may
5168dcc1a9dSDamien Le Moal 	 * end up in a deadlock on the inode truncate mutex, while the latter
5178dcc1a9dSDamien Le Moal 	 * may prevent IO forward progress. Executing the report zones under
5188dcc1a9dSDamien Le Moal 	 * the GFP_NOIO context avoids both problems.
5198dcc1a9dSDamien Le Moal 	 */
5208dcc1a9dSDamien Le Moal 	noio_flag = memalloc_noio_save();
5218dcc1a9dSDamien Le Moal 	ret = blkdev_report_zones(sb->s_bdev, zi->i_zsector, nr_zones,
5228dcc1a9dSDamien Le Moal 				  zonefs_io_error_cb, &err);
5238dcc1a9dSDamien Le Moal 	if (ret != nr_zones)
5248dcc1a9dSDamien Le Moal 		zonefs_err(sb, "Get inode %lu zone information failed %d\n",
5258dcc1a9dSDamien Le Moal 			   inode->i_ino, ret);
5268dcc1a9dSDamien Le Moal 	memalloc_noio_restore(noio_flag);
52748d546a8SJohannes Thumshirn }
5288dcc1a9dSDamien Le Moal 
52948d546a8SJohannes Thumshirn static void zonefs_io_error(struct inode *inode, bool write)
53048d546a8SJohannes Thumshirn {
53148d546a8SJohannes Thumshirn 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
53248d546a8SJohannes Thumshirn 
53348d546a8SJohannes Thumshirn 	mutex_lock(&zi->i_truncate_mutex);
53448d546a8SJohannes Thumshirn 	__zonefs_io_error(inode, write);
5358dcc1a9dSDamien Le Moal 	mutex_unlock(&zi->i_truncate_mutex);
5368dcc1a9dSDamien Le Moal }
5378dcc1a9dSDamien Le Moal 
5388dcc1a9dSDamien Le Moal static int zonefs_file_truncate(struct inode *inode, loff_t isize)
5398dcc1a9dSDamien Le Moal {
5408dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
5418dcc1a9dSDamien Le Moal 	loff_t old_isize;
542ff07a02eSBart Van Assche 	enum req_op op;
5438dcc1a9dSDamien Le Moal 	int ret = 0;
5448dcc1a9dSDamien Le Moal 
5458dcc1a9dSDamien Le Moal 	/*
5468dcc1a9dSDamien Le Moal 	 * Only sequential zone files can be truncated and truncation is allowed
5478dcc1a9dSDamien Le Moal 	 * only down to a 0 size, which is equivalent to a zone reset, and to
5488dcc1a9dSDamien Le Moal 	 * the maximum file size, which is equivalent to a zone finish.
5498dcc1a9dSDamien Le Moal 	 */
5508dcc1a9dSDamien Le Moal 	if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
5518dcc1a9dSDamien Le Moal 		return -EPERM;
5528dcc1a9dSDamien Le Moal 
5538dcc1a9dSDamien Le Moal 	if (!isize)
5548dcc1a9dSDamien Le Moal 		op = REQ_OP_ZONE_RESET;
5558dcc1a9dSDamien Le Moal 	else if (isize == zi->i_max_size)
5568dcc1a9dSDamien Le Moal 		op = REQ_OP_ZONE_FINISH;
5578dcc1a9dSDamien Le Moal 	else
5588dcc1a9dSDamien Le Moal 		return -EPERM;
5598dcc1a9dSDamien Le Moal 
5608dcc1a9dSDamien Le Moal 	inode_dio_wait(inode);
5618dcc1a9dSDamien Le Moal 
5628dcc1a9dSDamien Le Moal 	/* Serialize against page faults */
563448f9490SJan Kara 	filemap_invalidate_lock(inode->i_mapping);
5648dcc1a9dSDamien Le Moal 
5658dcc1a9dSDamien Le Moal 	/* Serialize against zonefs_iomap_begin() */
5668dcc1a9dSDamien Le Moal 	mutex_lock(&zi->i_truncate_mutex);
5678dcc1a9dSDamien Le Moal 
5688dcc1a9dSDamien Le Moal 	old_isize = i_size_read(inode);
5698dcc1a9dSDamien Le Moal 	if (isize == old_isize)
5708dcc1a9dSDamien Le Moal 		goto unlock;
5718dcc1a9dSDamien Le Moal 
5725498d5f9SJohannes Thumshirn 	ret = zonefs_zone_mgmt(inode, op);
5735498d5f9SJohannes Thumshirn 	if (ret)
5748dcc1a9dSDamien Le Moal 		goto unlock;
5758dcc1a9dSDamien Le Moal 
576b5c00e97SJohannes Thumshirn 	/*
577b5c00e97SJohannes Thumshirn 	 * If the mount option ZONEFS_MNTOPT_EXPLICIT_OPEN is set,
578b5c00e97SJohannes Thumshirn 	 * take care of open zones.
579b5c00e97SJohannes Thumshirn 	 */
580b5c00e97SJohannes Thumshirn 	if (zi->i_flags & ZONEFS_ZONE_OPEN) {
581b5c00e97SJohannes Thumshirn 		/*
582b5c00e97SJohannes Thumshirn 		 * Truncating a zone to EMPTY or FULL is the equivalent of
583b5c00e97SJohannes Thumshirn 		 * closing the zone. For a truncation to 0, we need to
584b5c00e97SJohannes Thumshirn 		 * re-open the zone to ensure new writes can be processed.
585b5c00e97SJohannes Thumshirn 		 * For a truncation to the maximum file size, the zone is
586b5c00e97SJohannes Thumshirn 		 * closed and writes cannot be accepted anymore, so clear
587b5c00e97SJohannes Thumshirn 		 * the open flag.
588b5c00e97SJohannes Thumshirn 		 */
589b5c00e97SJohannes Thumshirn 		if (!isize)
590b5c00e97SJohannes Thumshirn 			ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_OPEN);
591b5c00e97SJohannes Thumshirn 		else
592b5c00e97SJohannes Thumshirn 			zi->i_flags &= ~ZONEFS_ZONE_OPEN;
593b5c00e97SJohannes Thumshirn 	}
594b5c00e97SJohannes Thumshirn 
5958dcc1a9dSDamien Le Moal 	zonefs_update_stats(inode, isize);
5968dcc1a9dSDamien Le Moal 	truncate_setsize(inode, isize);
5978dcc1a9dSDamien Le Moal 	zi->i_wpoffset = isize;
59887c9ce3fSDamien Le Moal 	zonefs_account_active(inode);
5998dcc1a9dSDamien Le Moal 
6008dcc1a9dSDamien Le Moal unlock:
6018dcc1a9dSDamien Le Moal 	mutex_unlock(&zi->i_truncate_mutex);
602448f9490SJan Kara 	filemap_invalidate_unlock(inode->i_mapping);
6038dcc1a9dSDamien Le Moal 
6048dcc1a9dSDamien Le Moal 	return ret;
6058dcc1a9dSDamien Le Moal }
6068dcc1a9dSDamien Le Moal 
607549c7297SChristian Brauner static int zonefs_inode_setattr(struct user_namespace *mnt_userns,
608549c7297SChristian Brauner 				struct dentry *dentry, struct iattr *iattr)
6098dcc1a9dSDamien Le Moal {
6108dcc1a9dSDamien Le Moal 	struct inode *inode = d_inode(dentry);
6118dcc1a9dSDamien Le Moal 	int ret;
6128dcc1a9dSDamien Le Moal 
6138dcc1a9dSDamien Le Moal 	if (unlikely(IS_IMMUTABLE(inode)))
6148dcc1a9dSDamien Le Moal 		return -EPERM;
6158dcc1a9dSDamien Le Moal 
6162f221d6fSChristian Brauner 	ret = setattr_prepare(&init_user_ns, dentry, iattr);
6178dcc1a9dSDamien Le Moal 	if (ret)
6188dcc1a9dSDamien Le Moal 		return ret;
6198dcc1a9dSDamien Le Moal 
6208dcc1a9dSDamien Le Moal 	/*
6218dcc1a9dSDamien Le Moal 	 * Since files and directories cannot be created nor deleted, do not
6228dcc1a9dSDamien Le Moal 	 * allow setting any write attributes on the sub-directories grouping
6238dcc1a9dSDamien Le Moal 	 * files by zone type.
6248dcc1a9dSDamien Le Moal 	 */
6258dcc1a9dSDamien Le Moal 	if ((iattr->ia_valid & ATTR_MODE) && S_ISDIR(inode->i_mode) &&
6268dcc1a9dSDamien Le Moal 	    (iattr->ia_mode & 0222))
6278dcc1a9dSDamien Le Moal 		return -EPERM;
6288dcc1a9dSDamien Le Moal 
6298dcc1a9dSDamien Le Moal 	if (((iattr->ia_valid & ATTR_UID) &&
6308dcc1a9dSDamien Le Moal 	     !uid_eq(iattr->ia_uid, inode->i_uid)) ||
6318dcc1a9dSDamien Le Moal 	    ((iattr->ia_valid & ATTR_GID) &&
6328dcc1a9dSDamien Le Moal 	     !gid_eq(iattr->ia_gid, inode->i_gid))) {
633b27c82e1SChristian Brauner 		ret = dquot_transfer(mnt_userns, inode, iattr);
6348dcc1a9dSDamien Le Moal 		if (ret)
6358dcc1a9dSDamien Le Moal 			return ret;
6368dcc1a9dSDamien Le Moal 	}
6378dcc1a9dSDamien Le Moal 
6388dcc1a9dSDamien Le Moal 	if (iattr->ia_valid & ATTR_SIZE) {
6398dcc1a9dSDamien Le Moal 		ret = zonefs_file_truncate(inode, iattr->ia_size);
6408dcc1a9dSDamien Le Moal 		if (ret)
6418dcc1a9dSDamien Le Moal 			return ret;
6428dcc1a9dSDamien Le Moal 	}
6438dcc1a9dSDamien Le Moal 
6442f221d6fSChristian Brauner 	setattr_copy(&init_user_ns, inode, iattr);
6458dcc1a9dSDamien Le Moal 
6468dcc1a9dSDamien Le Moal 	return 0;
6478dcc1a9dSDamien Le Moal }
6488dcc1a9dSDamien Le Moal 
6498dcc1a9dSDamien Le Moal static const struct inode_operations zonefs_file_inode_operations = {
6508dcc1a9dSDamien Le Moal 	.setattr	= zonefs_inode_setattr,
6518dcc1a9dSDamien Le Moal };
6528dcc1a9dSDamien Le Moal 
6538dcc1a9dSDamien Le Moal static int zonefs_file_fsync(struct file *file, loff_t start, loff_t end,
6548dcc1a9dSDamien Le Moal 			     int datasync)
6558dcc1a9dSDamien Le Moal {
6568dcc1a9dSDamien Le Moal 	struct inode *inode = file_inode(file);
6578dcc1a9dSDamien Le Moal 	int ret = 0;
6588dcc1a9dSDamien Le Moal 
6598dcc1a9dSDamien Le Moal 	if (unlikely(IS_IMMUTABLE(inode)))
6608dcc1a9dSDamien Le Moal 		return -EPERM;
6618dcc1a9dSDamien Le Moal 
6628dcc1a9dSDamien Le Moal 	/*
6638dcc1a9dSDamien Le Moal 	 * Since only direct writes are allowed in sequential files, page cache
6648dcc1a9dSDamien Le Moal 	 * flush is needed only for conventional zone files.
6658dcc1a9dSDamien Le Moal 	 */
6668dcc1a9dSDamien Le Moal 	if (ZONEFS_I(inode)->i_ztype == ZONEFS_ZTYPE_CNV)
6678dcc1a9dSDamien Le Moal 		ret = file_write_and_wait_range(file, start, end);
6688dcc1a9dSDamien Le Moal 	if (!ret)
669c6bf3f0eSChristoph Hellwig 		ret = blkdev_issue_flush(inode->i_sb->s_bdev);
6708dcc1a9dSDamien Le Moal 
6718dcc1a9dSDamien Le Moal 	if (ret)
6728dcc1a9dSDamien Le Moal 		zonefs_io_error(inode, true);
6738dcc1a9dSDamien Le Moal 
6748dcc1a9dSDamien Le Moal 	return ret;
6758dcc1a9dSDamien Le Moal }
6768dcc1a9dSDamien Le Moal 
6778dcc1a9dSDamien Le Moal static vm_fault_t zonefs_filemap_page_mkwrite(struct vm_fault *vmf)
6788dcc1a9dSDamien Le Moal {
6798dcc1a9dSDamien Le Moal 	struct inode *inode = file_inode(vmf->vma->vm_file);
6808dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
6818dcc1a9dSDamien Le Moal 	vm_fault_t ret;
6828dcc1a9dSDamien Le Moal 
6838dcc1a9dSDamien Le Moal 	if (unlikely(IS_IMMUTABLE(inode)))
6848dcc1a9dSDamien Le Moal 		return VM_FAULT_SIGBUS;
6858dcc1a9dSDamien Le Moal 
6868dcc1a9dSDamien Le Moal 	/*
6878dcc1a9dSDamien Le Moal 	 * Sanity check: only conventional zone files can have shared
6888dcc1a9dSDamien Le Moal 	 * writeable mappings.
6898dcc1a9dSDamien Le Moal 	 */
6908dcc1a9dSDamien Le Moal 	if (WARN_ON_ONCE(zi->i_ztype != ZONEFS_ZTYPE_CNV))
6918dcc1a9dSDamien Le Moal 		return VM_FAULT_NOPAGE;
6928dcc1a9dSDamien Le Moal 
6938dcc1a9dSDamien Le Moal 	sb_start_pagefault(inode->i_sb);
6948dcc1a9dSDamien Le Moal 	file_update_time(vmf->vma->vm_file);
6958dcc1a9dSDamien Le Moal 
6968dcc1a9dSDamien Le Moal 	/* Serialize against truncates */
697448f9490SJan Kara 	filemap_invalidate_lock_shared(inode->i_mapping);
698c1c1204cSDamien Le Moal 	ret = iomap_page_mkwrite(vmf, &zonefs_write_iomap_ops);
699448f9490SJan Kara 	filemap_invalidate_unlock_shared(inode->i_mapping);
7008dcc1a9dSDamien Le Moal 
7018dcc1a9dSDamien Le Moal 	sb_end_pagefault(inode->i_sb);
7028dcc1a9dSDamien Le Moal 	return ret;
7038dcc1a9dSDamien Le Moal }
7048dcc1a9dSDamien Le Moal 
7058dcc1a9dSDamien Le Moal static const struct vm_operations_struct zonefs_file_vm_ops = {
706448f9490SJan Kara 	.fault		= filemap_fault,
7078dcc1a9dSDamien Le Moal 	.map_pages	= filemap_map_pages,
7088dcc1a9dSDamien Le Moal 	.page_mkwrite	= zonefs_filemap_page_mkwrite,
7098dcc1a9dSDamien Le Moal };
7108dcc1a9dSDamien Le Moal 
7118dcc1a9dSDamien Le Moal static int zonefs_file_mmap(struct file *file, struct vm_area_struct *vma)
7128dcc1a9dSDamien Le Moal {
7138dcc1a9dSDamien Le Moal 	/*
7148dcc1a9dSDamien Le Moal 	 * Conventional zones accept random writes, so their files can support
7158dcc1a9dSDamien Le Moal 	 * shared writable mappings. For sequential zone files, only read
7168dcc1a9dSDamien Le Moal 	 * mappings are possible since there are no guarantees for write
7178dcc1a9dSDamien Le Moal 	 * ordering between msync() and page cache writeback.
7188dcc1a9dSDamien Le Moal 	 */
7198dcc1a9dSDamien Le Moal 	if (ZONEFS_I(file_inode(file))->i_ztype == ZONEFS_ZTYPE_SEQ &&
7208dcc1a9dSDamien Le Moal 	    (vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
7218dcc1a9dSDamien Le Moal 		return -EINVAL;
7228dcc1a9dSDamien Le Moal 
7238dcc1a9dSDamien Le Moal 	file_accessed(file);
7248dcc1a9dSDamien Le Moal 	vma->vm_ops = &zonefs_file_vm_ops;
7258dcc1a9dSDamien Le Moal 
7268dcc1a9dSDamien Le Moal 	return 0;
7278dcc1a9dSDamien Le Moal }
7288dcc1a9dSDamien Le Moal 
7298dcc1a9dSDamien Le Moal static loff_t zonefs_file_llseek(struct file *file, loff_t offset, int whence)
7308dcc1a9dSDamien Le Moal {
7318dcc1a9dSDamien Le Moal 	loff_t isize = i_size_read(file_inode(file));
7328dcc1a9dSDamien Le Moal 
7338dcc1a9dSDamien Le Moal 	/*
7348dcc1a9dSDamien Le Moal 	 * Seeks are limited to below the zone size for conventional zones
7358dcc1a9dSDamien Le Moal 	 * and below the zone write pointer for sequential zones. In both
7368dcc1a9dSDamien Le Moal 	 * cases, this limit is the inode size.
7378dcc1a9dSDamien Le Moal 	 */
7388dcc1a9dSDamien Le Moal 	return generic_file_llseek_size(file, offset, whence, isize, isize);
7398dcc1a9dSDamien Le Moal }
7408dcc1a9dSDamien Le Moal 
7418dcc1a9dSDamien Le Moal static int zonefs_file_write_dio_end_io(struct kiocb *iocb, ssize_t size,
7428dcc1a9dSDamien Le Moal 					int error, unsigned int flags)
7438dcc1a9dSDamien Le Moal {
7448dcc1a9dSDamien Le Moal 	struct inode *inode = file_inode(iocb->ki_filp);
7458dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
7468dcc1a9dSDamien Le Moal 
7478dcc1a9dSDamien Le Moal 	if (error) {
7488dcc1a9dSDamien Le Moal 		zonefs_io_error(inode, true);
7498dcc1a9dSDamien Le Moal 		return error;
7508dcc1a9dSDamien Le Moal 	}
7518dcc1a9dSDamien Le Moal 
7528dcc1a9dSDamien Le Moal 	if (size && zi->i_ztype != ZONEFS_ZTYPE_CNV) {
7538dcc1a9dSDamien Le Moal 		/*
7548dcc1a9dSDamien Le Moal 		 * Note that we may be seeing completions out of order,
7558dcc1a9dSDamien Le Moal 		 * but that is not a problem since a write completed
7568dcc1a9dSDamien Le Moal 		 * successfully necessarily means that all preceding writes
7578dcc1a9dSDamien Le Moal 		 * were also successful. So we can safely increase the inode
7588dcc1a9dSDamien Le Moal 		 * size to the write end location.
7598dcc1a9dSDamien Le Moal 		 */
7608dcc1a9dSDamien Le Moal 		mutex_lock(&zi->i_truncate_mutex);
7618dcc1a9dSDamien Le Moal 		if (i_size_read(inode) < iocb->ki_pos + size) {
7628dcc1a9dSDamien Le Moal 			zonefs_update_stats(inode, iocb->ki_pos + size);
763b5c00e97SJohannes Thumshirn 			zonefs_i_size_write(inode, iocb->ki_pos + size);
7648dcc1a9dSDamien Le Moal 		}
7658dcc1a9dSDamien Le Moal 		mutex_unlock(&zi->i_truncate_mutex);
7668dcc1a9dSDamien Le Moal 	}
7678dcc1a9dSDamien Le Moal 
7688dcc1a9dSDamien Le Moal 	return 0;
7698dcc1a9dSDamien Le Moal }
7708dcc1a9dSDamien Le Moal 
7718dcc1a9dSDamien Le Moal static const struct iomap_dio_ops zonefs_write_dio_ops = {
7728dcc1a9dSDamien Le Moal 	.end_io			= zonefs_file_write_dio_end_io,
7738dcc1a9dSDamien Le Moal };
7748dcc1a9dSDamien Le Moal 
77502ef12a6SJohannes Thumshirn static ssize_t zonefs_file_dio_append(struct kiocb *iocb, struct iov_iter *from)
77602ef12a6SJohannes Thumshirn {
77702ef12a6SJohannes Thumshirn 	struct inode *inode = file_inode(iocb->ki_filp);
77802ef12a6SJohannes Thumshirn 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
77902ef12a6SJohannes Thumshirn 	struct block_device *bdev = inode->i_sb->s_bdev;
7802aba0d19SChristoph Hellwig 	unsigned int max = bdev_max_zone_append_sectors(bdev);
78102ef12a6SJohannes Thumshirn 	struct bio *bio;
78202ef12a6SJohannes Thumshirn 	ssize_t size;
78302ef12a6SJohannes Thumshirn 	int nr_pages;
78402ef12a6SJohannes Thumshirn 	ssize_t ret;
78502ef12a6SJohannes Thumshirn 
78602ef12a6SJohannes Thumshirn 	max = ALIGN_DOWN(max << SECTOR_SHIFT, inode->i_sb->s_blocksize);
78702ef12a6SJohannes Thumshirn 	iov_iter_truncate(from, max);
78802ef12a6SJohannes Thumshirn 
789a8affc03SChristoph Hellwig 	nr_pages = iov_iter_npages(from, BIO_MAX_VECS);
79089ee7237SJohannes Thumshirn 	if (!nr_pages)
79189ee7237SJohannes Thumshirn 		return 0;
79289ee7237SJohannes Thumshirn 
79307888c66SChristoph Hellwig 	bio = bio_alloc(bdev, nr_pages,
79407888c66SChristoph Hellwig 			REQ_OP_ZONE_APPEND | REQ_SYNC | REQ_IDLE, GFP_NOFS);
79502ef12a6SJohannes Thumshirn 	bio->bi_iter.bi_sector = zi->i_zsector;
79602ef12a6SJohannes Thumshirn 	bio->bi_ioprio = iocb->ki_ioprio;
79791b94c5dSAl Viro 	if (iocb_is_dsync(iocb))
79802ef12a6SJohannes Thumshirn 		bio->bi_opf |= REQ_FUA;
79902ef12a6SJohannes Thumshirn 
80002ef12a6SJohannes Thumshirn 	ret = bio_iov_iter_get_pages(bio, from);
8016bea0225SDamien Le Moal 	if (unlikely(ret))
8026bea0225SDamien Le Moal 		goto out_release;
8036bea0225SDamien Le Moal 
80402ef12a6SJohannes Thumshirn 	size = bio->bi_iter.bi_size;
8056bea0225SDamien Le Moal 	task_io_account_write(size);
80602ef12a6SJohannes Thumshirn 
80702ef12a6SJohannes Thumshirn 	if (iocb->ki_flags & IOCB_HIPRI)
80802ef12a6SJohannes Thumshirn 		bio_set_polled(bio, iocb);
80902ef12a6SJohannes Thumshirn 
81002ef12a6SJohannes Thumshirn 	ret = submit_bio_wait(bio);
81102ef12a6SJohannes Thumshirn 
812*a608da3bSDamien Le Moal 	/*
813*a608da3bSDamien Le Moal 	 * If the file zone was written underneath the file system, the zone
814*a608da3bSDamien Le Moal 	 * write pointer may not be where we expect it to be, but the zone
815*a608da3bSDamien Le Moal 	 * append write can still succeed. So check manually that we wrote where
816*a608da3bSDamien Le Moal 	 * we intended to, that is, at zi->i_wpoffset.
817*a608da3bSDamien Le Moal 	 */
818*a608da3bSDamien Le Moal 	if (!ret) {
819*a608da3bSDamien Le Moal 		sector_t wpsector =
820*a608da3bSDamien Le Moal 			zi->i_zsector + (zi->i_wpoffset >> SECTOR_SHIFT);
821*a608da3bSDamien Le Moal 
822*a608da3bSDamien Le Moal 		if (bio->bi_iter.bi_sector != wpsector) {
823*a608da3bSDamien Le Moal 			zonefs_warn(inode->i_sb,
824*a608da3bSDamien Le Moal 				"Corrupted write pointer %llu for zone at %llu\n",
825*a608da3bSDamien Le Moal 				wpsector, zi->i_zsector);
826*a608da3bSDamien Le Moal 			ret = -EIO;
827*a608da3bSDamien Le Moal 		}
828*a608da3bSDamien Le Moal 	}
829*a608da3bSDamien Le Moal 
8306bea0225SDamien Le Moal 	zonefs_file_write_dio_end_io(iocb, size, ret, 0);
83162ab1aadSJohannes Thumshirn 	trace_zonefs_file_dio_append(inode, size, ret);
8326bea0225SDamien Le Moal 
8336bea0225SDamien Le Moal out_release:
8346bea0225SDamien Le Moal 	bio_release_pages(bio, false);
83502ef12a6SJohannes Thumshirn 	bio_put(bio);
83602ef12a6SJohannes Thumshirn 
83702ef12a6SJohannes Thumshirn 	if (ret >= 0) {
83802ef12a6SJohannes Thumshirn 		iocb->ki_pos += size;
83902ef12a6SJohannes Thumshirn 		return size;
84002ef12a6SJohannes Thumshirn 	}
84102ef12a6SJohannes Thumshirn 
84202ef12a6SJohannes Thumshirn 	return ret;
84302ef12a6SJohannes Thumshirn }
84402ef12a6SJohannes Thumshirn 
8458dcc1a9dSDamien Le Moal /*
846ebfd68cdSDamien Le Moal  * Do not exceed the LFS limits nor the file zone size. If pos is under the
847ebfd68cdSDamien Le Moal  * limit it becomes a short access. If it exceeds the limit, return -EFBIG.
848ebfd68cdSDamien Le Moal  */
849ebfd68cdSDamien Le Moal static loff_t zonefs_write_check_limits(struct file *file, loff_t pos,
850ebfd68cdSDamien Le Moal 					loff_t count)
851ebfd68cdSDamien Le Moal {
852ebfd68cdSDamien Le Moal 	struct inode *inode = file_inode(file);
853ebfd68cdSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
854ebfd68cdSDamien Le Moal 	loff_t limit = rlimit(RLIMIT_FSIZE);
855ebfd68cdSDamien Le Moal 	loff_t max_size = zi->i_max_size;
856ebfd68cdSDamien Le Moal 
857ebfd68cdSDamien Le Moal 	if (limit != RLIM_INFINITY) {
858ebfd68cdSDamien Le Moal 		if (pos >= limit) {
859ebfd68cdSDamien Le Moal 			send_sig(SIGXFSZ, current, 0);
860ebfd68cdSDamien Le Moal 			return -EFBIG;
861ebfd68cdSDamien Le Moal 		}
862ebfd68cdSDamien Le Moal 		count = min(count, limit - pos);
863ebfd68cdSDamien Le Moal 	}
864ebfd68cdSDamien Le Moal 
865ebfd68cdSDamien Le Moal 	if (!(file->f_flags & O_LARGEFILE))
866ebfd68cdSDamien Le Moal 		max_size = min_t(loff_t, MAX_NON_LFS, max_size);
867ebfd68cdSDamien Le Moal 
868ebfd68cdSDamien Le Moal 	if (unlikely(pos >= max_size))
869ebfd68cdSDamien Le Moal 		return -EFBIG;
870ebfd68cdSDamien Le Moal 
871ebfd68cdSDamien Le Moal 	return min(count, max_size - pos);
872ebfd68cdSDamien Le Moal }
873ebfd68cdSDamien Le Moal 
874ebfd68cdSDamien Le Moal static ssize_t zonefs_write_checks(struct kiocb *iocb, struct iov_iter *from)
875ebfd68cdSDamien Le Moal {
876ebfd68cdSDamien Le Moal 	struct file *file = iocb->ki_filp;
877ebfd68cdSDamien Le Moal 	struct inode *inode = file_inode(file);
878ebfd68cdSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
879ebfd68cdSDamien Le Moal 	loff_t count;
880ebfd68cdSDamien Le Moal 
881ebfd68cdSDamien Le Moal 	if (IS_SWAPFILE(inode))
882ebfd68cdSDamien Le Moal 		return -ETXTBSY;
883ebfd68cdSDamien Le Moal 
884ebfd68cdSDamien Le Moal 	if (!iov_iter_count(from))
885ebfd68cdSDamien Le Moal 		return 0;
886ebfd68cdSDamien Le Moal 
887ebfd68cdSDamien Le Moal 	if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
888ebfd68cdSDamien Le Moal 		return -EINVAL;
889ebfd68cdSDamien Le Moal 
890ebfd68cdSDamien Le Moal 	if (iocb->ki_flags & IOCB_APPEND) {
891ebfd68cdSDamien Le Moal 		if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
892ebfd68cdSDamien Le Moal 			return -EINVAL;
893ebfd68cdSDamien Le Moal 		mutex_lock(&zi->i_truncate_mutex);
894ebfd68cdSDamien Le Moal 		iocb->ki_pos = zi->i_wpoffset;
895ebfd68cdSDamien Le Moal 		mutex_unlock(&zi->i_truncate_mutex);
896ebfd68cdSDamien Le Moal 	}
897ebfd68cdSDamien Le Moal 
898ebfd68cdSDamien Le Moal 	count = zonefs_write_check_limits(file, iocb->ki_pos,
899ebfd68cdSDamien Le Moal 					  iov_iter_count(from));
900ebfd68cdSDamien Le Moal 	if (count < 0)
901ebfd68cdSDamien Le Moal 		return count;
902ebfd68cdSDamien Le Moal 
903ebfd68cdSDamien Le Moal 	iov_iter_truncate(from, count);
904ebfd68cdSDamien Le Moal 	return iov_iter_count(from);
905ebfd68cdSDamien Le Moal }
906ebfd68cdSDamien Le Moal 
907ebfd68cdSDamien Le Moal /*
9088dcc1a9dSDamien Le Moal  * Handle direct writes. For sequential zone files, this is the only possible
9098dcc1a9dSDamien Le Moal  * write path. For these files, check that the user is issuing writes
9108dcc1a9dSDamien Le Moal  * sequentially from the end of the file. This code assumes that the block layer
9118dcc1a9dSDamien Le Moal  * delivers write requests to the device in sequential order. This is always the
9128dcc1a9dSDamien Le Moal  * case if a block IO scheduler implementing the ELEVATOR_F_ZBD_SEQ_WRITE
9138dcc1a9dSDamien Le Moal  * elevator feature is being used (e.g. mq-deadline). The block layer always
9148dcc1a9dSDamien Le Moal  * automatically select such an elevator for zoned block devices during the
9158dcc1a9dSDamien Le Moal  * device initialization.
9168dcc1a9dSDamien Le Moal  */
9178dcc1a9dSDamien Le Moal static ssize_t zonefs_file_dio_write(struct kiocb *iocb, struct iov_iter *from)
9188dcc1a9dSDamien Le Moal {
9198dcc1a9dSDamien Le Moal 	struct inode *inode = file_inode(iocb->ki_filp);
9208dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
9218dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
92202ef12a6SJohannes Thumshirn 	bool sync = is_sync_kiocb(iocb);
92302ef12a6SJohannes Thumshirn 	bool append = false;
924ebfd68cdSDamien Le Moal 	ssize_t ret, count;
9258dcc1a9dSDamien Le Moal 
9268dcc1a9dSDamien Le Moal 	/*
9277c69eb84SChristoph Hellwig 	 * For async direct IOs to sequential zone files, refuse IOCB_NOWAIT
9288dcc1a9dSDamien Le Moal 	 * as this can cause write reordering (e.g. the first aio gets EAGAIN
9298dcc1a9dSDamien Le Moal 	 * on the inode lock but the second goes through but is now unaligned).
9308dcc1a9dSDamien Le Moal 	 */
93102ef12a6SJohannes Thumshirn 	if (zi->i_ztype == ZONEFS_ZTYPE_SEQ && !sync &&
9327c69eb84SChristoph Hellwig 	    (iocb->ki_flags & IOCB_NOWAIT))
9337c69eb84SChristoph Hellwig 		return -EOPNOTSUPP;
9348dcc1a9dSDamien Le Moal 
9358dcc1a9dSDamien Le Moal 	if (iocb->ki_flags & IOCB_NOWAIT) {
9368dcc1a9dSDamien Le Moal 		if (!inode_trylock(inode))
9378dcc1a9dSDamien Le Moal 			return -EAGAIN;
9388dcc1a9dSDamien Le Moal 	} else {
9398dcc1a9dSDamien Le Moal 		inode_lock(inode);
9408dcc1a9dSDamien Le Moal 	}
9418dcc1a9dSDamien Le Moal 
942ebfd68cdSDamien Le Moal 	count = zonefs_write_checks(iocb, from);
943ebfd68cdSDamien Le Moal 	if (count <= 0) {
944ebfd68cdSDamien Le Moal 		ret = count;
9458dcc1a9dSDamien Le Moal 		goto inode_unlock;
946ebfd68cdSDamien Le Moal 	}
9478dcc1a9dSDamien Le Moal 
9488dcc1a9dSDamien Le Moal 	if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) {
9498dcc1a9dSDamien Le Moal 		ret = -EINVAL;
9508dcc1a9dSDamien Le Moal 		goto inode_unlock;
9518dcc1a9dSDamien Le Moal 	}
9528dcc1a9dSDamien Le Moal 
9538dcc1a9dSDamien Le Moal 	/* Enforce sequential writes (append only) in sequential zones */
95402ef12a6SJohannes Thumshirn 	if (zi->i_ztype == ZONEFS_ZTYPE_SEQ) {
9558dcc1a9dSDamien Le Moal 		mutex_lock(&zi->i_truncate_mutex);
95602ef12a6SJohannes Thumshirn 		if (iocb->ki_pos != zi->i_wpoffset) {
9578dcc1a9dSDamien Le Moal 			mutex_unlock(&zi->i_truncate_mutex);
9588dcc1a9dSDamien Le Moal 			ret = -EINVAL;
9598dcc1a9dSDamien Le Moal 			goto inode_unlock;
9608dcc1a9dSDamien Le Moal 		}
9618dcc1a9dSDamien Le Moal 		mutex_unlock(&zi->i_truncate_mutex);
96202ef12a6SJohannes Thumshirn 		append = sync;
96302ef12a6SJohannes Thumshirn 	}
9648dcc1a9dSDamien Le Moal 
96502ef12a6SJohannes Thumshirn 	if (append)
96602ef12a6SJohannes Thumshirn 		ret = zonefs_file_dio_append(iocb, from);
96702ef12a6SJohannes Thumshirn 	else
968c1c1204cSDamien Le Moal 		ret = iomap_dio_rw(iocb, from, &zonefs_write_iomap_ops,
969786f847fSChristoph Hellwig 				   &zonefs_write_dio_ops, 0, NULL, 0);
9708dcc1a9dSDamien Le Moal 	if (zi->i_ztype == ZONEFS_ZTYPE_SEQ &&
9718dcc1a9dSDamien Le Moal 	    (ret > 0 || ret == -EIOCBQUEUED)) {
9728dcc1a9dSDamien Le Moal 		if (ret > 0)
9738dcc1a9dSDamien Le Moal 			count = ret;
97487c9ce3fSDamien Le Moal 
97587c9ce3fSDamien Le Moal 		/*
97687c9ce3fSDamien Le Moal 		 * Update the zone write pointer offset assuming the write
97787c9ce3fSDamien Le Moal 		 * operation succeeded. If it did not, the error recovery path
97887c9ce3fSDamien Le Moal 		 * will correct it. Also do active seq file accounting.
97987c9ce3fSDamien Le Moal 		 */
9808dcc1a9dSDamien Le Moal 		mutex_lock(&zi->i_truncate_mutex);
9818dcc1a9dSDamien Le Moal 		zi->i_wpoffset += count;
98287c9ce3fSDamien Le Moal 		zonefs_account_active(inode);
9838dcc1a9dSDamien Le Moal 		mutex_unlock(&zi->i_truncate_mutex);
9848dcc1a9dSDamien Le Moal 	}
9858dcc1a9dSDamien Le Moal 
9868dcc1a9dSDamien Le Moal inode_unlock:
9878dcc1a9dSDamien Le Moal 	inode_unlock(inode);
9888dcc1a9dSDamien Le Moal 
9898dcc1a9dSDamien Le Moal 	return ret;
9908dcc1a9dSDamien Le Moal }
9918dcc1a9dSDamien Le Moal 
9928dcc1a9dSDamien Le Moal static ssize_t zonefs_file_buffered_write(struct kiocb *iocb,
9938dcc1a9dSDamien Le Moal 					  struct iov_iter *from)
9948dcc1a9dSDamien Le Moal {
9958dcc1a9dSDamien Le Moal 	struct inode *inode = file_inode(iocb->ki_filp);
9968dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
9978dcc1a9dSDamien Le Moal 	ssize_t ret;
9988dcc1a9dSDamien Le Moal 
9998dcc1a9dSDamien Le Moal 	/*
10008dcc1a9dSDamien Le Moal 	 * Direct IO writes are mandatory for sequential zone files so that the
10018dcc1a9dSDamien Le Moal 	 * write IO issuing order is preserved.
10028dcc1a9dSDamien Le Moal 	 */
10038dcc1a9dSDamien Le Moal 	if (zi->i_ztype != ZONEFS_ZTYPE_CNV)
10048dcc1a9dSDamien Le Moal 		return -EIO;
10058dcc1a9dSDamien Le Moal 
10068dcc1a9dSDamien Le Moal 	if (iocb->ki_flags & IOCB_NOWAIT) {
10078dcc1a9dSDamien Le Moal 		if (!inode_trylock(inode))
10088dcc1a9dSDamien Le Moal 			return -EAGAIN;
10098dcc1a9dSDamien Le Moal 	} else {
10108dcc1a9dSDamien Le Moal 		inode_lock(inode);
10118dcc1a9dSDamien Le Moal 	}
10128dcc1a9dSDamien Le Moal 
1013ebfd68cdSDamien Le Moal 	ret = zonefs_write_checks(iocb, from);
10148dcc1a9dSDamien Le Moal 	if (ret <= 0)
10158dcc1a9dSDamien Le Moal 		goto inode_unlock;
10168dcc1a9dSDamien Le Moal 
1017c1c1204cSDamien Le Moal 	ret = iomap_file_buffered_write(iocb, from, &zonefs_write_iomap_ops);
10188dcc1a9dSDamien Le Moal 	if (ret > 0)
10198dcc1a9dSDamien Le Moal 		iocb->ki_pos += ret;
10208dcc1a9dSDamien Le Moal 	else if (ret == -EIO)
10218dcc1a9dSDamien Le Moal 		zonefs_io_error(inode, true);
10228dcc1a9dSDamien Le Moal 
10238dcc1a9dSDamien Le Moal inode_unlock:
10248dcc1a9dSDamien Le Moal 	inode_unlock(inode);
10258dcc1a9dSDamien Le Moal 	if (ret > 0)
10268dcc1a9dSDamien Le Moal 		ret = generic_write_sync(iocb, ret);
10278dcc1a9dSDamien Le Moal 
10288dcc1a9dSDamien Le Moal 	return ret;
10298dcc1a9dSDamien Le Moal }
10308dcc1a9dSDamien Le Moal 
10318dcc1a9dSDamien Le Moal static ssize_t zonefs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
10328dcc1a9dSDamien Le Moal {
10338dcc1a9dSDamien Le Moal 	struct inode *inode = file_inode(iocb->ki_filp);
10348dcc1a9dSDamien Le Moal 
10358dcc1a9dSDamien Le Moal 	if (unlikely(IS_IMMUTABLE(inode)))
10368dcc1a9dSDamien Le Moal 		return -EPERM;
10378dcc1a9dSDamien Le Moal 
10388dcc1a9dSDamien Le Moal 	if (sb_rdonly(inode->i_sb))
10398dcc1a9dSDamien Le Moal 		return -EROFS;
10408dcc1a9dSDamien Le Moal 
10418dcc1a9dSDamien Le Moal 	/* Write operations beyond the zone size are not allowed */
10428dcc1a9dSDamien Le Moal 	if (iocb->ki_pos >= ZONEFS_I(inode)->i_max_size)
10438dcc1a9dSDamien Le Moal 		return -EFBIG;
10448dcc1a9dSDamien Le Moal 
104560263d58SChristoph Hellwig 	if (iocb->ki_flags & IOCB_DIRECT) {
104660263d58SChristoph Hellwig 		ssize_t ret = zonefs_file_dio_write(iocb, from);
104760263d58SChristoph Hellwig 		if (ret != -ENOTBLK)
104860263d58SChristoph Hellwig 			return ret;
104960263d58SChristoph Hellwig 	}
10508dcc1a9dSDamien Le Moal 
10518dcc1a9dSDamien Le Moal 	return zonefs_file_buffered_write(iocb, from);
10528dcc1a9dSDamien Le Moal }
10538dcc1a9dSDamien Le Moal 
10548dcc1a9dSDamien Le Moal static int zonefs_file_read_dio_end_io(struct kiocb *iocb, ssize_t size,
10558dcc1a9dSDamien Le Moal 				       int error, unsigned int flags)
10568dcc1a9dSDamien Le Moal {
10578dcc1a9dSDamien Le Moal 	if (error) {
10588dcc1a9dSDamien Le Moal 		zonefs_io_error(file_inode(iocb->ki_filp), false);
10598dcc1a9dSDamien Le Moal 		return error;
10608dcc1a9dSDamien Le Moal 	}
10618dcc1a9dSDamien Le Moal 
10628dcc1a9dSDamien Le Moal 	return 0;
10638dcc1a9dSDamien Le Moal }
10648dcc1a9dSDamien Le Moal 
10658dcc1a9dSDamien Le Moal static const struct iomap_dio_ops zonefs_read_dio_ops = {
10668dcc1a9dSDamien Le Moal 	.end_io			= zonefs_file_read_dio_end_io,
10678dcc1a9dSDamien Le Moal };
10688dcc1a9dSDamien Le Moal 
10698dcc1a9dSDamien Le Moal static ssize_t zonefs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
10708dcc1a9dSDamien Le Moal {
10718dcc1a9dSDamien Le Moal 	struct inode *inode = file_inode(iocb->ki_filp);
10728dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
10738dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
10748dcc1a9dSDamien Le Moal 	loff_t isize;
10758dcc1a9dSDamien Le Moal 	ssize_t ret;
10768dcc1a9dSDamien Le Moal 
10778dcc1a9dSDamien Le Moal 	/* Offline zones cannot be read */
10788dcc1a9dSDamien Le Moal 	if (unlikely(IS_IMMUTABLE(inode) && !(inode->i_mode & 0777)))
10798dcc1a9dSDamien Le Moal 		return -EPERM;
10808dcc1a9dSDamien Le Moal 
10818dcc1a9dSDamien Le Moal 	if (iocb->ki_pos >= zi->i_max_size)
10828dcc1a9dSDamien Le Moal 		return 0;
10838dcc1a9dSDamien Le Moal 
10848dcc1a9dSDamien Le Moal 	if (iocb->ki_flags & IOCB_NOWAIT) {
10858dcc1a9dSDamien Le Moal 		if (!inode_trylock_shared(inode))
10868dcc1a9dSDamien Le Moal 			return -EAGAIN;
10878dcc1a9dSDamien Le Moal 	} else {
10888dcc1a9dSDamien Le Moal 		inode_lock_shared(inode);
10898dcc1a9dSDamien Le Moal 	}
10908dcc1a9dSDamien Le Moal 
10918dcc1a9dSDamien Le Moal 	/* Limit read operations to written data */
10928dcc1a9dSDamien Le Moal 	mutex_lock(&zi->i_truncate_mutex);
10938dcc1a9dSDamien Le Moal 	isize = i_size_read(inode);
10948dcc1a9dSDamien Le Moal 	if (iocb->ki_pos >= isize) {
10958dcc1a9dSDamien Le Moal 		mutex_unlock(&zi->i_truncate_mutex);
10968dcc1a9dSDamien Le Moal 		ret = 0;
10978dcc1a9dSDamien Le Moal 		goto inode_unlock;
10988dcc1a9dSDamien Le Moal 	}
10998dcc1a9dSDamien Le Moal 	iov_iter_truncate(to, isize - iocb->ki_pos);
11008dcc1a9dSDamien Le Moal 	mutex_unlock(&zi->i_truncate_mutex);
11018dcc1a9dSDamien Le Moal 
11028dcc1a9dSDamien Le Moal 	if (iocb->ki_flags & IOCB_DIRECT) {
11038dcc1a9dSDamien Le Moal 		size_t count = iov_iter_count(to);
11048dcc1a9dSDamien Le Moal 
11058dcc1a9dSDamien Le Moal 		if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) {
11068dcc1a9dSDamien Le Moal 			ret = -EINVAL;
11078dcc1a9dSDamien Le Moal 			goto inode_unlock;
11088dcc1a9dSDamien Le Moal 		}
11098dcc1a9dSDamien Le Moal 		file_accessed(iocb->ki_filp);
1110c1c1204cSDamien Le Moal 		ret = iomap_dio_rw(iocb, to, &zonefs_read_iomap_ops,
1111786f847fSChristoph Hellwig 				   &zonefs_read_dio_ops, 0, NULL, 0);
11128dcc1a9dSDamien Le Moal 	} else {
11138dcc1a9dSDamien Le Moal 		ret = generic_file_read_iter(iocb, to);
11148dcc1a9dSDamien Le Moal 		if (ret == -EIO)
11158dcc1a9dSDamien Le Moal 			zonefs_io_error(inode, false);
11168dcc1a9dSDamien Le Moal 	}
11178dcc1a9dSDamien Le Moal 
11188dcc1a9dSDamien Le Moal inode_unlock:
11198dcc1a9dSDamien Le Moal 	inode_unlock_shared(inode);
11208dcc1a9dSDamien Le Moal 
11218dcc1a9dSDamien Le Moal 	return ret;
11228dcc1a9dSDamien Le Moal }
11238dcc1a9dSDamien Le Moal 
11247d6dfbe0SDamien Le Moal /*
11257d6dfbe0SDamien Le Moal  * Write open accounting is done only for sequential files.
11267d6dfbe0SDamien Le Moal  */
11277d6dfbe0SDamien Le Moal static inline bool zonefs_seq_file_need_wro(struct inode *inode,
11287d6dfbe0SDamien Le Moal 					    struct file *file)
1129b5c00e97SJohannes Thumshirn {
1130b5c00e97SJohannes Thumshirn 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
1131b5c00e97SJohannes Thumshirn 
1132b5c00e97SJohannes Thumshirn 	if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
1133b5c00e97SJohannes Thumshirn 		return false;
1134b5c00e97SJohannes Thumshirn 
1135b5c00e97SJohannes Thumshirn 	if (!(file->f_mode & FMODE_WRITE))
1136b5c00e97SJohannes Thumshirn 		return false;
1137b5c00e97SJohannes Thumshirn 
1138b5c00e97SJohannes Thumshirn 	return true;
1139b5c00e97SJohannes Thumshirn }
1140b5c00e97SJohannes Thumshirn 
11417d6dfbe0SDamien Le Moal static int zonefs_seq_file_write_open(struct inode *inode)
1142b5c00e97SJohannes Thumshirn {
1143b5c00e97SJohannes Thumshirn 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
1144b5c00e97SJohannes Thumshirn 	int ret = 0;
1145b5c00e97SJohannes Thumshirn 
1146b5c00e97SJohannes Thumshirn 	mutex_lock(&zi->i_truncate_mutex);
1147b5c00e97SJohannes Thumshirn 
11486980d29cSChao Yu 	if (!zi->i_wr_refcnt) {
11497d6dfbe0SDamien Le Moal 		struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
11502b95a23cSDamien Le Moal 		unsigned int wro = atomic_inc_return(&sbi->s_wro_seq_files);
11512b95a23cSDamien Le Moal 
11527d6dfbe0SDamien Le Moal 		if (sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) {
11537d6dfbe0SDamien Le Moal 
115496eca145SDamien Le Moal 			if (sbi->s_max_wro_seq_files
115596eca145SDamien Le Moal 			    && wro > sbi->s_max_wro_seq_files) {
11562b95a23cSDamien Le Moal 				atomic_dec(&sbi->s_wro_seq_files);
1157b5c00e97SJohannes Thumshirn 				ret = -EBUSY;
1158b5c00e97SJohannes Thumshirn 				goto unlock;
1159b5c00e97SJohannes Thumshirn 			}
1160b5c00e97SJohannes Thumshirn 
1161b5c00e97SJohannes Thumshirn 			if (i_size_read(inode) < zi->i_max_size) {
1162b5c00e97SJohannes Thumshirn 				ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_OPEN);
1163b5c00e97SJohannes Thumshirn 				if (ret) {
11642b95a23cSDamien Le Moal 					atomic_dec(&sbi->s_wro_seq_files);
1165b5c00e97SJohannes Thumshirn 					goto unlock;
1166b5c00e97SJohannes Thumshirn 				}
1167b5c00e97SJohannes Thumshirn 				zi->i_flags |= ZONEFS_ZONE_OPEN;
116887c9ce3fSDamien Le Moal 				zonefs_account_active(inode);
1169b5c00e97SJohannes Thumshirn 			}
1170b5c00e97SJohannes Thumshirn 		}
1171b5c00e97SJohannes Thumshirn 	}
1172b5c00e97SJohannes Thumshirn 
11736980d29cSChao Yu 	zi->i_wr_refcnt++;
11746980d29cSChao Yu 
1175b5c00e97SJohannes Thumshirn unlock:
1176b5c00e97SJohannes Thumshirn 	mutex_unlock(&zi->i_truncate_mutex);
1177b5c00e97SJohannes Thumshirn 
1178b5c00e97SJohannes Thumshirn 	return ret;
1179b5c00e97SJohannes Thumshirn }
1180b5c00e97SJohannes Thumshirn 
1181b5c00e97SJohannes Thumshirn static int zonefs_file_open(struct inode *inode, struct file *file)
1182b5c00e97SJohannes Thumshirn {
1183b5c00e97SJohannes Thumshirn 	int ret;
1184b5c00e97SJohannes Thumshirn 
1185b5c00e97SJohannes Thumshirn 	ret = generic_file_open(inode, file);
1186b5c00e97SJohannes Thumshirn 	if (ret)
1187b5c00e97SJohannes Thumshirn 		return ret;
1188b5c00e97SJohannes Thumshirn 
11897d6dfbe0SDamien Le Moal 	if (zonefs_seq_file_need_wro(inode, file))
11907d6dfbe0SDamien Le Moal 		return zonefs_seq_file_write_open(inode);
1191b5c00e97SJohannes Thumshirn 
1192b5c00e97SJohannes Thumshirn 	return 0;
1193b5c00e97SJohannes Thumshirn }
1194b5c00e97SJohannes Thumshirn 
11957d6dfbe0SDamien Le Moal static void zonefs_seq_file_write_close(struct inode *inode)
1196b5c00e97SJohannes Thumshirn {
1197b5c00e97SJohannes Thumshirn 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
11987d6dfbe0SDamien Le Moal 	struct super_block *sb = inode->i_sb;
11997d6dfbe0SDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1200b5c00e97SJohannes Thumshirn 	int ret = 0;
1201b5c00e97SJohannes Thumshirn 
1202b5c00e97SJohannes Thumshirn 	mutex_lock(&zi->i_truncate_mutex);
12037d6dfbe0SDamien Le Moal 
1204b5c00e97SJohannes Thumshirn 	zi->i_wr_refcnt--;
12057d6dfbe0SDamien Le Moal 	if (zi->i_wr_refcnt)
12067d6dfbe0SDamien Le Moal 		goto unlock;
1207b5c00e97SJohannes Thumshirn 
1208b5c00e97SJohannes Thumshirn 	/*
12097d6dfbe0SDamien Le Moal 	 * The file zone may not be open anymore (e.g. the file was truncated to
12107d6dfbe0SDamien Le Moal 	 * its maximum size or it was fully written). For this case, we only
12117d6dfbe0SDamien Le Moal 	 * need to decrement the write open count.
1212b5c00e97SJohannes Thumshirn 	 */
12137d6dfbe0SDamien Le Moal 	if (zi->i_flags & ZONEFS_ZONE_OPEN) {
1214b5c00e97SJohannes Thumshirn 		ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_CLOSE);
1215b5c00e97SJohannes Thumshirn 		if (ret) {
1216b5c00e97SJohannes Thumshirn 			__zonefs_io_error(inode, false);
1217b5c00e97SJohannes Thumshirn 			/*
1218b5c00e97SJohannes Thumshirn 			 * Leaving zones explicitly open may lead to a state
1219b5c00e97SJohannes Thumshirn 			 * where most zones cannot be written (zone resources
1220b5c00e97SJohannes Thumshirn 			 * exhausted). So take preventive action by remounting
1221b5c00e97SJohannes Thumshirn 			 * read-only.
1222b5c00e97SJohannes Thumshirn 			 */
1223b5c00e97SJohannes Thumshirn 			if (zi->i_flags & ZONEFS_ZONE_OPEN &&
1224b5c00e97SJohannes Thumshirn 			    !(sb->s_flags & SB_RDONLY)) {
12257d6dfbe0SDamien Le Moal 				zonefs_warn(sb,
12267d6dfbe0SDamien Le Moal 					"closing zone at %llu failed %d\n",
12277d6dfbe0SDamien Le Moal 					zi->i_zsector, ret);
12287d6dfbe0SDamien Le Moal 				zonefs_warn(sb,
12297d6dfbe0SDamien Le Moal 					"remounting filesystem read-only\n");
1230b5c00e97SJohannes Thumshirn 				sb->s_flags |= SB_RDONLY;
1231b5c00e97SJohannes Thumshirn 			}
12327d6dfbe0SDamien Le Moal 			goto unlock;
1233b5c00e97SJohannes Thumshirn 		}
12347d6dfbe0SDamien Le Moal 
1235b5c00e97SJohannes Thumshirn 		zi->i_flags &= ~ZONEFS_ZONE_OPEN;
123687c9ce3fSDamien Le Moal 		zonefs_account_active(inode);
1237b5c00e97SJohannes Thumshirn 	}
12387d6dfbe0SDamien Le Moal 
12397d6dfbe0SDamien Le Moal 	atomic_dec(&sbi->s_wro_seq_files);
12407d6dfbe0SDamien Le Moal 
12417d6dfbe0SDamien Le Moal unlock:
1242b5c00e97SJohannes Thumshirn 	mutex_unlock(&zi->i_truncate_mutex);
1243b5c00e97SJohannes Thumshirn }
1244b5c00e97SJohannes Thumshirn 
1245b5c00e97SJohannes Thumshirn static int zonefs_file_release(struct inode *inode, struct file *file)
1246b5c00e97SJohannes Thumshirn {
1247b5c00e97SJohannes Thumshirn 	/*
1248b5c00e97SJohannes Thumshirn 	 * If we explicitly open a zone we must close it again as well, but the
1249b5c00e97SJohannes Thumshirn 	 * zone management operation can fail (either due to an IO error or as
1250b5c00e97SJohannes Thumshirn 	 * the zone has gone offline or read-only). Make sure we don't fail the
1251b5c00e97SJohannes Thumshirn 	 * close(2) for user-space.
1252b5c00e97SJohannes Thumshirn 	 */
12537d6dfbe0SDamien Le Moal 	if (zonefs_seq_file_need_wro(inode, file))
12547d6dfbe0SDamien Le Moal 		zonefs_seq_file_write_close(inode);
1255b5c00e97SJohannes Thumshirn 
1256b5c00e97SJohannes Thumshirn 	return 0;
1257b5c00e97SJohannes Thumshirn }
1258b5c00e97SJohannes Thumshirn 
12598dcc1a9dSDamien Le Moal static const struct file_operations zonefs_file_operations = {
1260b5c00e97SJohannes Thumshirn 	.open		= zonefs_file_open,
1261b5c00e97SJohannes Thumshirn 	.release	= zonefs_file_release,
12628dcc1a9dSDamien Le Moal 	.fsync		= zonefs_file_fsync,
12638dcc1a9dSDamien Le Moal 	.mmap		= zonefs_file_mmap,
12648dcc1a9dSDamien Le Moal 	.llseek		= zonefs_file_llseek,
12658dcc1a9dSDamien Le Moal 	.read_iter	= zonefs_file_read_iter,
12668dcc1a9dSDamien Le Moal 	.write_iter	= zonefs_file_write_iter,
12678dcc1a9dSDamien Le Moal 	.splice_read	= generic_file_splice_read,
12688dcc1a9dSDamien Le Moal 	.splice_write	= iter_file_splice_write,
12693e08773cSChristoph Hellwig 	.iopoll		= iocb_bio_iopoll,
12708dcc1a9dSDamien Le Moal };
12718dcc1a9dSDamien Le Moal 
12728dcc1a9dSDamien Le Moal static struct kmem_cache *zonefs_inode_cachep;
12738dcc1a9dSDamien Le Moal 
12748dcc1a9dSDamien Le Moal static struct inode *zonefs_alloc_inode(struct super_block *sb)
12758dcc1a9dSDamien Le Moal {
12768dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi;
12778dcc1a9dSDamien Le Moal 
1278fd60b288SMuchun Song 	zi = alloc_inode_sb(sb, zonefs_inode_cachep, GFP_KERNEL);
12798dcc1a9dSDamien Le Moal 	if (!zi)
12808dcc1a9dSDamien Le Moal 		return NULL;
12818dcc1a9dSDamien Le Moal 
12828dcc1a9dSDamien Le Moal 	inode_init_once(&zi->i_vnode);
12838dcc1a9dSDamien Le Moal 	mutex_init(&zi->i_truncate_mutex);
1284b5c00e97SJohannes Thumshirn 	zi->i_wr_refcnt = 0;
1285694852eaSDamien Le Moal 	zi->i_flags = 0;
12868dcc1a9dSDamien Le Moal 
12878dcc1a9dSDamien Le Moal 	return &zi->i_vnode;
12888dcc1a9dSDamien Le Moal }
12898dcc1a9dSDamien Le Moal 
12908dcc1a9dSDamien Le Moal static void zonefs_free_inode(struct inode *inode)
12918dcc1a9dSDamien Le Moal {
12928dcc1a9dSDamien Le Moal 	kmem_cache_free(zonefs_inode_cachep, ZONEFS_I(inode));
12938dcc1a9dSDamien Le Moal }
12948dcc1a9dSDamien Le Moal 
12958dcc1a9dSDamien Le Moal /*
12968dcc1a9dSDamien Le Moal  * File system stat.
12978dcc1a9dSDamien Le Moal  */
12988dcc1a9dSDamien Le Moal static int zonefs_statfs(struct dentry *dentry, struct kstatfs *buf)
12998dcc1a9dSDamien Le Moal {
13008dcc1a9dSDamien Le Moal 	struct super_block *sb = dentry->d_sb;
13018dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
13028dcc1a9dSDamien Le Moal 	enum zonefs_ztype t;
13038dcc1a9dSDamien Le Moal 
13048dcc1a9dSDamien Le Moal 	buf->f_type = ZONEFS_MAGIC;
13058dcc1a9dSDamien Le Moal 	buf->f_bsize = sb->s_blocksize;
13068dcc1a9dSDamien Le Moal 	buf->f_namelen = ZONEFS_NAME_MAX;
13078dcc1a9dSDamien Le Moal 
13088dcc1a9dSDamien Le Moal 	spin_lock(&sbi->s_lock);
13098dcc1a9dSDamien Le Moal 
13108dcc1a9dSDamien Le Moal 	buf->f_blocks = sbi->s_blocks;
13118dcc1a9dSDamien Le Moal 	if (WARN_ON(sbi->s_used_blocks > sbi->s_blocks))
13128dcc1a9dSDamien Le Moal 		buf->f_bfree = 0;
13138dcc1a9dSDamien Le Moal 	else
13148dcc1a9dSDamien Le Moal 		buf->f_bfree = buf->f_blocks - sbi->s_used_blocks;
13158dcc1a9dSDamien Le Moal 	buf->f_bavail = buf->f_bfree;
13168dcc1a9dSDamien Le Moal 
13178dcc1a9dSDamien Le Moal 	for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) {
13188dcc1a9dSDamien Le Moal 		if (sbi->s_nr_files[t])
13198dcc1a9dSDamien Le Moal 			buf->f_files += sbi->s_nr_files[t] + 1;
13208dcc1a9dSDamien Le Moal 	}
13218dcc1a9dSDamien Le Moal 	buf->f_ffree = 0;
13228dcc1a9dSDamien Le Moal 
13238dcc1a9dSDamien Le Moal 	spin_unlock(&sbi->s_lock);
13248dcc1a9dSDamien Le Moal 
13259591c3a3SAmir Goldstein 	buf->f_fsid = uuid_to_fsid(sbi->s_uuid.b);
13268dcc1a9dSDamien Le Moal 
13278dcc1a9dSDamien Le Moal 	return 0;
13288dcc1a9dSDamien Le Moal }
13298dcc1a9dSDamien Le Moal 
13308dcc1a9dSDamien Le Moal enum {
13318dcc1a9dSDamien Le Moal 	Opt_errors_ro, Opt_errors_zro, Opt_errors_zol, Opt_errors_repair,
1332b5c00e97SJohannes Thumshirn 	Opt_explicit_open, Opt_err,
13338dcc1a9dSDamien Le Moal };
13348dcc1a9dSDamien Le Moal 
13358dcc1a9dSDamien Le Moal static const match_table_t tokens = {
13368dcc1a9dSDamien Le Moal 	{ Opt_errors_ro,	"errors=remount-ro"},
13378dcc1a9dSDamien Le Moal 	{ Opt_errors_zro,	"errors=zone-ro"},
13388dcc1a9dSDamien Le Moal 	{ Opt_errors_zol,	"errors=zone-offline"},
13398dcc1a9dSDamien Le Moal 	{ Opt_errors_repair,	"errors=repair"},
1340b5c00e97SJohannes Thumshirn 	{ Opt_explicit_open,	"explicit-open" },
13418dcc1a9dSDamien Le Moal 	{ Opt_err,		NULL}
13428dcc1a9dSDamien Le Moal };
13438dcc1a9dSDamien Le Moal 
13448dcc1a9dSDamien Le Moal static int zonefs_parse_options(struct super_block *sb, char *options)
13458dcc1a9dSDamien Le Moal {
13468dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
13478dcc1a9dSDamien Le Moal 	substring_t args[MAX_OPT_ARGS];
13488dcc1a9dSDamien Le Moal 	char *p;
13498dcc1a9dSDamien Le Moal 
13508dcc1a9dSDamien Le Moal 	if (!options)
13518dcc1a9dSDamien Le Moal 		return 0;
13528dcc1a9dSDamien Le Moal 
13538dcc1a9dSDamien Le Moal 	while ((p = strsep(&options, ",")) != NULL) {
13548dcc1a9dSDamien Le Moal 		int token;
13558dcc1a9dSDamien Le Moal 
13568dcc1a9dSDamien Le Moal 		if (!*p)
13578dcc1a9dSDamien Le Moal 			continue;
13588dcc1a9dSDamien Le Moal 
13598dcc1a9dSDamien Le Moal 		token = match_token(p, tokens, args);
13608dcc1a9dSDamien Le Moal 		switch (token) {
13618dcc1a9dSDamien Le Moal 		case Opt_errors_ro:
13628dcc1a9dSDamien Le Moal 			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
13638dcc1a9dSDamien Le Moal 			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_RO;
13648dcc1a9dSDamien Le Moal 			break;
13658dcc1a9dSDamien Le Moal 		case Opt_errors_zro:
13668dcc1a9dSDamien Le Moal 			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
13678dcc1a9dSDamien Le Moal 			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZRO;
13688dcc1a9dSDamien Le Moal 			break;
13698dcc1a9dSDamien Le Moal 		case Opt_errors_zol:
13708dcc1a9dSDamien Le Moal 			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
13718dcc1a9dSDamien Le Moal 			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZOL;
13728dcc1a9dSDamien Le Moal 			break;
13738dcc1a9dSDamien Le Moal 		case Opt_errors_repair:
13748dcc1a9dSDamien Le Moal 			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
13758dcc1a9dSDamien Le Moal 			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_REPAIR;
13768dcc1a9dSDamien Le Moal 			break;
1377b5c00e97SJohannes Thumshirn 		case Opt_explicit_open:
1378b5c00e97SJohannes Thumshirn 			sbi->s_mount_opts |= ZONEFS_MNTOPT_EXPLICIT_OPEN;
1379b5c00e97SJohannes Thumshirn 			break;
13808dcc1a9dSDamien Le Moal 		default:
13818dcc1a9dSDamien Le Moal 			return -EINVAL;
13828dcc1a9dSDamien Le Moal 		}
13838dcc1a9dSDamien Le Moal 	}
13848dcc1a9dSDamien Le Moal 
13858dcc1a9dSDamien Le Moal 	return 0;
13868dcc1a9dSDamien Le Moal }
13878dcc1a9dSDamien Le Moal 
13888dcc1a9dSDamien Le Moal static int zonefs_show_options(struct seq_file *seq, struct dentry *root)
13898dcc1a9dSDamien Le Moal {
13908dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(root->d_sb);
13918dcc1a9dSDamien Le Moal 
13928dcc1a9dSDamien Le Moal 	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO)
13938dcc1a9dSDamien Le Moal 		seq_puts(seq, ",errors=remount-ro");
13948dcc1a9dSDamien Le Moal 	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO)
13958dcc1a9dSDamien Le Moal 		seq_puts(seq, ",errors=zone-ro");
13968dcc1a9dSDamien Le Moal 	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL)
13978dcc1a9dSDamien Le Moal 		seq_puts(seq, ",errors=zone-offline");
13988dcc1a9dSDamien Le Moal 	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_REPAIR)
13998dcc1a9dSDamien Le Moal 		seq_puts(seq, ",errors=repair");
14008dcc1a9dSDamien Le Moal 
14018dcc1a9dSDamien Le Moal 	return 0;
14028dcc1a9dSDamien Le Moal }
14038dcc1a9dSDamien Le Moal 
14048dcc1a9dSDamien Le Moal static int zonefs_remount(struct super_block *sb, int *flags, char *data)
14058dcc1a9dSDamien Le Moal {
14068dcc1a9dSDamien Le Moal 	sync_filesystem(sb);
14078dcc1a9dSDamien Le Moal 
14088dcc1a9dSDamien Le Moal 	return zonefs_parse_options(sb, data);
14098dcc1a9dSDamien Le Moal }
14108dcc1a9dSDamien Le Moal 
14118dcc1a9dSDamien Le Moal static const struct super_operations zonefs_sops = {
14128dcc1a9dSDamien Le Moal 	.alloc_inode	= zonefs_alloc_inode,
14138dcc1a9dSDamien Le Moal 	.free_inode	= zonefs_free_inode,
14148dcc1a9dSDamien Le Moal 	.statfs		= zonefs_statfs,
14158dcc1a9dSDamien Le Moal 	.remount_fs	= zonefs_remount,
14168dcc1a9dSDamien Le Moal 	.show_options	= zonefs_show_options,
14178dcc1a9dSDamien Le Moal };
14188dcc1a9dSDamien Le Moal 
14198dcc1a9dSDamien Le Moal static const struct inode_operations zonefs_dir_inode_operations = {
14208dcc1a9dSDamien Le Moal 	.lookup		= simple_lookup,
14218dcc1a9dSDamien Le Moal 	.setattr	= zonefs_inode_setattr,
14228dcc1a9dSDamien Le Moal };
14238dcc1a9dSDamien Le Moal 
14248dcc1a9dSDamien Le Moal static void zonefs_init_dir_inode(struct inode *parent, struct inode *inode,
14258dcc1a9dSDamien Le Moal 				  enum zonefs_ztype type)
14268dcc1a9dSDamien Le Moal {
14278dcc1a9dSDamien Le Moal 	struct super_block *sb = parent->i_sb;
14288dcc1a9dSDamien Le Moal 
1429b623e347SChristoph Hellwig 	inode->i_ino = bdev_nr_zones(sb->s_bdev) + type + 1;
143021cb47beSChristian Brauner 	inode_init_owner(&init_user_ns, inode, parent, S_IFDIR | 0555);
14318dcc1a9dSDamien Le Moal 	inode->i_op = &zonefs_dir_inode_operations;
14328dcc1a9dSDamien Le Moal 	inode->i_fop = &simple_dir_operations;
14338dcc1a9dSDamien Le Moal 	set_nlink(inode, 2);
14348dcc1a9dSDamien Le Moal 	inc_nlink(parent);
14358dcc1a9dSDamien Le Moal }
14368dcc1a9dSDamien Le Moal 
14371da18a29SDamien Le Moal static int zonefs_init_file_inode(struct inode *inode, struct blk_zone *zone,
14388dcc1a9dSDamien Le Moal 				  enum zonefs_ztype type)
14398dcc1a9dSDamien Le Moal {
14408dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
14418dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
14428dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
144314bdb047SDamien Le Moal 	int ret = 0;
14448dcc1a9dSDamien Le Moal 
14458dcc1a9dSDamien Le Moal 	inode->i_ino = zone->start >> sbi->s_zone_sectors_shift;
14468dcc1a9dSDamien Le Moal 	inode->i_mode = S_IFREG | sbi->s_perm;
14478dcc1a9dSDamien Le Moal 
14488dcc1a9dSDamien Le Moal 	zi->i_ztype = type;
14498dcc1a9dSDamien Le Moal 	zi->i_zsector = zone->start;
1450e3c3155bSJohannes Thumshirn 	zi->i_zone_size = zone->len << SECTOR_SHIFT;
14517dd12d65SDamien Le Moal 	if (zi->i_zone_size > bdev_zone_sectors(sb->s_bdev) << SECTOR_SHIFT &&
14527dd12d65SDamien Le Moal 	    !(sbi->s_features & ZONEFS_F_AGGRCNV)) {
14537dd12d65SDamien Le Moal 		zonefs_err(sb,
14547dd12d65SDamien Le Moal 			   "zone size %llu doesn't match device's zone sectors %llu\n",
14557dd12d65SDamien Le Moal 			   zi->i_zone_size,
14567dd12d65SDamien Le Moal 			   bdev_zone_sectors(sb->s_bdev) << SECTOR_SHIFT);
14577dd12d65SDamien Le Moal 		return -EINVAL;
14587dd12d65SDamien Le Moal 	}
1459e3c3155bSJohannes Thumshirn 
14608dcc1a9dSDamien Le Moal 	zi->i_max_size = min_t(loff_t, MAX_LFS_FILESIZE,
1461e3c3155bSJohannes Thumshirn 			       zone->capacity << SECTOR_SHIFT);
1462ccf4ad7dSDamien Le Moal 	zi->i_wpoffset = zonefs_check_zone_condition(inode, zone, true, true);
14638dcc1a9dSDamien Le Moal 
14648dcc1a9dSDamien Le Moal 	inode->i_uid = sbi->s_uid;
14658dcc1a9dSDamien Le Moal 	inode->i_gid = sbi->s_gid;
14668dcc1a9dSDamien Le Moal 	inode->i_size = zi->i_wpoffset;
1467e3c3155bSJohannes Thumshirn 	inode->i_blocks = zi->i_max_size >> SECTOR_SHIFT;
14688dcc1a9dSDamien Le Moal 
14698dcc1a9dSDamien Le Moal 	inode->i_op = &zonefs_file_inode_operations;
14708dcc1a9dSDamien Le Moal 	inode->i_fop = &zonefs_file_operations;
14718dcc1a9dSDamien Le Moal 	inode->i_mapping->a_ops = &zonefs_file_aops;
14728dcc1a9dSDamien Le Moal 
14738dcc1a9dSDamien Le Moal 	sb->s_maxbytes = max(zi->i_max_size, sb->s_maxbytes);
14748dcc1a9dSDamien Le Moal 	sbi->s_blocks += zi->i_max_size >> sb->s_blocksize_bits;
14758dcc1a9dSDamien Le Moal 	sbi->s_used_blocks += zi->i_wpoffset >> sb->s_blocksize_bits;
14761da18a29SDamien Le Moal 
147787c9ce3fSDamien Le Moal 	mutex_lock(&zi->i_truncate_mutex);
147887c9ce3fSDamien Le Moal 
14791da18a29SDamien Le Moal 	/*
14801da18a29SDamien Le Moal 	 * For sequential zones, make sure that any open zone is closed first
14811da18a29SDamien Le Moal 	 * to ensure that the initial number of open zones is 0, in sync with
14821da18a29SDamien Le Moal 	 * the open zone accounting done when the mount option
14831da18a29SDamien Le Moal 	 * ZONEFS_MNTOPT_EXPLICIT_OPEN is used.
14841da18a29SDamien Le Moal 	 */
14851da18a29SDamien Le Moal 	if (type == ZONEFS_ZTYPE_SEQ &&
14861da18a29SDamien Le Moal 	    (zone->cond == BLK_ZONE_COND_IMP_OPEN ||
14871da18a29SDamien Le Moal 	     zone->cond == BLK_ZONE_COND_EXP_OPEN)) {
14881da18a29SDamien Le Moal 		ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_CLOSE);
148987c9ce3fSDamien Le Moal 		if (ret)
149087c9ce3fSDamien Le Moal 			goto unlock;
14911da18a29SDamien Le Moal 	}
14921da18a29SDamien Le Moal 
149387c9ce3fSDamien Le Moal 	zonefs_account_active(inode);
149487c9ce3fSDamien Le Moal 
149587c9ce3fSDamien Le Moal unlock:
149687c9ce3fSDamien Le Moal 	mutex_unlock(&zi->i_truncate_mutex);
149787c9ce3fSDamien Le Moal 
149814bdb047SDamien Le Moal 	return ret;
14998dcc1a9dSDamien Le Moal }
15008dcc1a9dSDamien Le Moal 
15018dcc1a9dSDamien Le Moal static struct dentry *zonefs_create_inode(struct dentry *parent,
15028dcc1a9dSDamien Le Moal 					const char *name, struct blk_zone *zone,
15038dcc1a9dSDamien Le Moal 					enum zonefs_ztype type)
15048dcc1a9dSDamien Le Moal {
15058dcc1a9dSDamien Le Moal 	struct inode *dir = d_inode(parent);
15068dcc1a9dSDamien Le Moal 	struct dentry *dentry;
15078dcc1a9dSDamien Le Moal 	struct inode *inode;
15087dd12d65SDamien Le Moal 	int ret = -ENOMEM;
15098dcc1a9dSDamien Le Moal 
15108dcc1a9dSDamien Le Moal 	dentry = d_alloc_name(parent, name);
15118dcc1a9dSDamien Le Moal 	if (!dentry)
15127dd12d65SDamien Le Moal 		return ERR_PTR(ret);
15138dcc1a9dSDamien Le Moal 
15148dcc1a9dSDamien Le Moal 	inode = new_inode(parent->d_sb);
15158dcc1a9dSDamien Le Moal 	if (!inode)
15168dcc1a9dSDamien Le Moal 		goto dput;
15178dcc1a9dSDamien Le Moal 
15188dcc1a9dSDamien Le Moal 	inode->i_ctime = inode->i_mtime = inode->i_atime = dir->i_ctime;
15191da18a29SDamien Le Moal 	if (zone) {
15201da18a29SDamien Le Moal 		ret = zonefs_init_file_inode(inode, zone, type);
15211da18a29SDamien Le Moal 		if (ret) {
15221da18a29SDamien Le Moal 			iput(inode);
15231da18a29SDamien Le Moal 			goto dput;
15241da18a29SDamien Le Moal 		}
15251da18a29SDamien Le Moal 	} else {
15268dcc1a9dSDamien Le Moal 		zonefs_init_dir_inode(dir, inode, type);
15271da18a29SDamien Le Moal 	}
15281da18a29SDamien Le Moal 
15298dcc1a9dSDamien Le Moal 	d_add(dentry, inode);
15308dcc1a9dSDamien Le Moal 	dir->i_size++;
15318dcc1a9dSDamien Le Moal 
15328dcc1a9dSDamien Le Moal 	return dentry;
15338dcc1a9dSDamien Le Moal 
15348dcc1a9dSDamien Le Moal dput:
15358dcc1a9dSDamien Le Moal 	dput(dentry);
15368dcc1a9dSDamien Le Moal 
15377dd12d65SDamien Le Moal 	return ERR_PTR(ret);
15388dcc1a9dSDamien Le Moal }
15398dcc1a9dSDamien Le Moal 
15408dcc1a9dSDamien Le Moal struct zonefs_zone_data {
15418dcc1a9dSDamien Le Moal 	struct super_block	*sb;
15428dcc1a9dSDamien Le Moal 	unsigned int		nr_zones[ZONEFS_ZTYPE_MAX];
15438dcc1a9dSDamien Le Moal 	struct blk_zone		*zones;
15448dcc1a9dSDamien Le Moal };
15458dcc1a9dSDamien Le Moal 
15468dcc1a9dSDamien Le Moal /*
15478dcc1a9dSDamien Le Moal  * Create a zone group and populate it with zone files.
15488dcc1a9dSDamien Le Moal  */
15498dcc1a9dSDamien Le Moal static int zonefs_create_zgroup(struct zonefs_zone_data *zd,
15508dcc1a9dSDamien Le Moal 				enum zonefs_ztype type)
15518dcc1a9dSDamien Le Moal {
15528dcc1a9dSDamien Le Moal 	struct super_block *sb = zd->sb;
15538dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
15548dcc1a9dSDamien Le Moal 	struct blk_zone *zone, *next, *end;
15558dcc1a9dSDamien Le Moal 	const char *zgroup_name;
15568dcc1a9dSDamien Le Moal 	char *file_name;
15577dd12d65SDamien Le Moal 	struct dentry *dir, *dent;
15588dcc1a9dSDamien Le Moal 	unsigned int n = 0;
155901b2651cSDamien Le Moal 	int ret;
15608dcc1a9dSDamien Le Moal 
15618dcc1a9dSDamien Le Moal 	/* If the group is empty, there is nothing to do */
15628dcc1a9dSDamien Le Moal 	if (!zd->nr_zones[type])
15638dcc1a9dSDamien Le Moal 		return 0;
15648dcc1a9dSDamien Le Moal 
15658dcc1a9dSDamien Le Moal 	file_name = kmalloc(ZONEFS_NAME_MAX, GFP_KERNEL);
15668dcc1a9dSDamien Le Moal 	if (!file_name)
15678dcc1a9dSDamien Le Moal 		return -ENOMEM;
15688dcc1a9dSDamien Le Moal 
15698dcc1a9dSDamien Le Moal 	if (type == ZONEFS_ZTYPE_CNV)
15708dcc1a9dSDamien Le Moal 		zgroup_name = "cnv";
15718dcc1a9dSDamien Le Moal 	else
15728dcc1a9dSDamien Le Moal 		zgroup_name = "seq";
15738dcc1a9dSDamien Le Moal 
15748dcc1a9dSDamien Le Moal 	dir = zonefs_create_inode(sb->s_root, zgroup_name, NULL, type);
15757dd12d65SDamien Le Moal 	if (IS_ERR(dir)) {
15767dd12d65SDamien Le Moal 		ret = PTR_ERR(dir);
15778dcc1a9dSDamien Le Moal 		goto free;
157801b2651cSDamien Le Moal 	}
15798dcc1a9dSDamien Le Moal 
15808dcc1a9dSDamien Le Moal 	/*
15818dcc1a9dSDamien Le Moal 	 * The first zone contains the super block: skip it.
15828dcc1a9dSDamien Le Moal 	 */
1583b623e347SChristoph Hellwig 	end = zd->zones + bdev_nr_zones(sb->s_bdev);
15848dcc1a9dSDamien Le Moal 	for (zone = &zd->zones[1]; zone < end; zone = next) {
15858dcc1a9dSDamien Le Moal 
15868dcc1a9dSDamien Le Moal 		next = zone + 1;
15878dcc1a9dSDamien Le Moal 		if (zonefs_zone_type(zone) != type)
15888dcc1a9dSDamien Le Moal 			continue;
15898dcc1a9dSDamien Le Moal 
15908dcc1a9dSDamien Le Moal 		/*
15918dcc1a9dSDamien Le Moal 		 * For conventional zones, contiguous zones can be aggregated
15928dcc1a9dSDamien Le Moal 		 * together to form larger files. Note that this overwrites the
15938dcc1a9dSDamien Le Moal 		 * length of the first zone of the set of contiguous zones
15948dcc1a9dSDamien Le Moal 		 * aggregated together. If one offline or read-only zone is
15958dcc1a9dSDamien Le Moal 		 * found, assume that all zones aggregated have the same
15968dcc1a9dSDamien Le Moal 		 * condition.
15978dcc1a9dSDamien Le Moal 		 */
15988dcc1a9dSDamien Le Moal 		if (type == ZONEFS_ZTYPE_CNV &&
15998dcc1a9dSDamien Le Moal 		    (sbi->s_features & ZONEFS_F_AGGRCNV)) {
16008dcc1a9dSDamien Le Moal 			for (; next < end; next++) {
16018dcc1a9dSDamien Le Moal 				if (zonefs_zone_type(next) != type)
16028dcc1a9dSDamien Le Moal 					break;
16038dcc1a9dSDamien Le Moal 				zone->len += next->len;
1604e3c3155bSJohannes Thumshirn 				zone->capacity += next->capacity;
16058dcc1a9dSDamien Le Moal 				if (next->cond == BLK_ZONE_COND_READONLY &&
16068dcc1a9dSDamien Le Moal 				    zone->cond != BLK_ZONE_COND_OFFLINE)
16078dcc1a9dSDamien Le Moal 					zone->cond = BLK_ZONE_COND_READONLY;
16088dcc1a9dSDamien Le Moal 				else if (next->cond == BLK_ZONE_COND_OFFLINE)
16098dcc1a9dSDamien Le Moal 					zone->cond = BLK_ZONE_COND_OFFLINE;
16108dcc1a9dSDamien Le Moal 			}
1611e3c3155bSJohannes Thumshirn 			if (zone->capacity != zone->len) {
1612e3c3155bSJohannes Thumshirn 				zonefs_err(sb, "Invalid conventional zone capacity\n");
1613e3c3155bSJohannes Thumshirn 				ret = -EINVAL;
1614e3c3155bSJohannes Thumshirn 				goto free;
1615e3c3155bSJohannes Thumshirn 			}
16168dcc1a9dSDamien Le Moal 		}
16178dcc1a9dSDamien Le Moal 
16188dcc1a9dSDamien Le Moal 		/*
16198dcc1a9dSDamien Le Moal 		 * Use the file number within its group as file name.
16208dcc1a9dSDamien Le Moal 		 */
16218dcc1a9dSDamien Le Moal 		snprintf(file_name, ZONEFS_NAME_MAX - 1, "%u", n);
16227dd12d65SDamien Le Moal 		dent = zonefs_create_inode(dir, file_name, zone, type);
16237dd12d65SDamien Le Moal 		if (IS_ERR(dent)) {
16247dd12d65SDamien Le Moal 			ret = PTR_ERR(dent);
16258dcc1a9dSDamien Le Moal 			goto free;
162601b2651cSDamien Le Moal 		}
16278dcc1a9dSDamien Le Moal 
16288dcc1a9dSDamien Le Moal 		n++;
16298dcc1a9dSDamien Le Moal 	}
16308dcc1a9dSDamien Le Moal 
16318dcc1a9dSDamien Le Moal 	zonefs_info(sb, "Zone group \"%s\" has %u file%s\n",
16328dcc1a9dSDamien Le Moal 		    zgroup_name, n, n > 1 ? "s" : "");
16338dcc1a9dSDamien Le Moal 
16348dcc1a9dSDamien Le Moal 	sbi->s_nr_files[type] = n;
16358dcc1a9dSDamien Le Moal 	ret = 0;
16368dcc1a9dSDamien Le Moal 
16378dcc1a9dSDamien Le Moal free:
16388dcc1a9dSDamien Le Moal 	kfree(file_name);
16398dcc1a9dSDamien Le Moal 
16408dcc1a9dSDamien Le Moal 	return ret;
16418dcc1a9dSDamien Le Moal }
16428dcc1a9dSDamien Le Moal 
16438dcc1a9dSDamien Le Moal static int zonefs_get_zone_info_cb(struct blk_zone *zone, unsigned int idx,
16448dcc1a9dSDamien Le Moal 				   void *data)
16458dcc1a9dSDamien Le Moal {
16468dcc1a9dSDamien Le Moal 	struct zonefs_zone_data *zd = data;
16478dcc1a9dSDamien Le Moal 
16488dcc1a9dSDamien Le Moal 	/*
16498dcc1a9dSDamien Le Moal 	 * Count the number of usable zones: the first zone at index 0 contains
16508dcc1a9dSDamien Le Moal 	 * the super block and is ignored.
16518dcc1a9dSDamien Le Moal 	 */
16528dcc1a9dSDamien Le Moal 	switch (zone->type) {
16538dcc1a9dSDamien Le Moal 	case BLK_ZONE_TYPE_CONVENTIONAL:
16548dcc1a9dSDamien Le Moal 		zone->wp = zone->start + zone->len;
16558dcc1a9dSDamien Le Moal 		if (idx)
16568dcc1a9dSDamien Le Moal 			zd->nr_zones[ZONEFS_ZTYPE_CNV]++;
16578dcc1a9dSDamien Le Moal 		break;
16588dcc1a9dSDamien Le Moal 	case BLK_ZONE_TYPE_SEQWRITE_REQ:
16598dcc1a9dSDamien Le Moal 	case BLK_ZONE_TYPE_SEQWRITE_PREF:
16608dcc1a9dSDamien Le Moal 		if (idx)
16618dcc1a9dSDamien Le Moal 			zd->nr_zones[ZONEFS_ZTYPE_SEQ]++;
16628dcc1a9dSDamien Le Moal 		break;
16638dcc1a9dSDamien Le Moal 	default:
16648dcc1a9dSDamien Le Moal 		zonefs_err(zd->sb, "Unsupported zone type 0x%x\n",
16658dcc1a9dSDamien Le Moal 			   zone->type);
16668dcc1a9dSDamien Le Moal 		return -EIO;
16678dcc1a9dSDamien Le Moal 	}
16688dcc1a9dSDamien Le Moal 
16698dcc1a9dSDamien Le Moal 	memcpy(&zd->zones[idx], zone, sizeof(struct blk_zone));
16708dcc1a9dSDamien Le Moal 
16718dcc1a9dSDamien Le Moal 	return 0;
16728dcc1a9dSDamien Le Moal }
16738dcc1a9dSDamien Le Moal 
16748dcc1a9dSDamien Le Moal static int zonefs_get_zone_info(struct zonefs_zone_data *zd)
16758dcc1a9dSDamien Le Moal {
16768dcc1a9dSDamien Le Moal 	struct block_device *bdev = zd->sb->s_bdev;
16778dcc1a9dSDamien Le Moal 	int ret;
16788dcc1a9dSDamien Le Moal 
1679b623e347SChristoph Hellwig 	zd->zones = kvcalloc(bdev_nr_zones(bdev), sizeof(struct blk_zone),
1680b623e347SChristoph Hellwig 			     GFP_KERNEL);
16818dcc1a9dSDamien Le Moal 	if (!zd->zones)
16828dcc1a9dSDamien Le Moal 		return -ENOMEM;
16838dcc1a9dSDamien Le Moal 
16848dcc1a9dSDamien Le Moal 	/* Get zones information from the device */
16858dcc1a9dSDamien Le Moal 	ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES,
16868dcc1a9dSDamien Le Moal 				  zonefs_get_zone_info_cb, zd);
16878dcc1a9dSDamien Le Moal 	if (ret < 0) {
16888dcc1a9dSDamien Le Moal 		zonefs_err(zd->sb, "Zone report failed %d\n", ret);
16898dcc1a9dSDamien Le Moal 		return ret;
16908dcc1a9dSDamien Le Moal 	}
16918dcc1a9dSDamien Le Moal 
1692b623e347SChristoph Hellwig 	if (ret != bdev_nr_zones(bdev)) {
16938dcc1a9dSDamien Le Moal 		zonefs_err(zd->sb, "Invalid zone report (%d/%u zones)\n",
1694b623e347SChristoph Hellwig 			   ret, bdev_nr_zones(bdev));
16958dcc1a9dSDamien Le Moal 		return -EIO;
16968dcc1a9dSDamien Le Moal 	}
16978dcc1a9dSDamien Le Moal 
16988dcc1a9dSDamien Le Moal 	return 0;
16998dcc1a9dSDamien Le Moal }
17008dcc1a9dSDamien Le Moal 
17018dcc1a9dSDamien Le Moal static inline void zonefs_cleanup_zone_info(struct zonefs_zone_data *zd)
17028dcc1a9dSDamien Le Moal {
17038dcc1a9dSDamien Le Moal 	kvfree(zd->zones);
17048dcc1a9dSDamien Le Moal }
17058dcc1a9dSDamien Le Moal 
17068dcc1a9dSDamien Le Moal /*
17078dcc1a9dSDamien Le Moal  * Read super block information from the device.
17088dcc1a9dSDamien Le Moal  */
17098dcc1a9dSDamien Le Moal static int zonefs_read_super(struct super_block *sb)
17108dcc1a9dSDamien Le Moal {
17118dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
17128dcc1a9dSDamien Le Moal 	struct zonefs_super *super;
17138dcc1a9dSDamien Le Moal 	u32 crc, stored_crc;
17148dcc1a9dSDamien Le Moal 	struct page *page;
17158dcc1a9dSDamien Le Moal 	struct bio_vec bio_vec;
17168dcc1a9dSDamien Le Moal 	struct bio bio;
17178dcc1a9dSDamien Le Moal 	int ret;
17188dcc1a9dSDamien Le Moal 
17198dcc1a9dSDamien Le Moal 	page = alloc_page(GFP_KERNEL);
17208dcc1a9dSDamien Le Moal 	if (!page)
17218dcc1a9dSDamien Le Moal 		return -ENOMEM;
17228dcc1a9dSDamien Le Moal 
172349add496SChristoph Hellwig 	bio_init(&bio, sb->s_bdev, &bio_vec, 1, REQ_OP_READ);
17248dcc1a9dSDamien Le Moal 	bio.bi_iter.bi_sector = 0;
17258dcc1a9dSDamien Le Moal 	bio_add_page(&bio, page, PAGE_SIZE, 0);
17268dcc1a9dSDamien Le Moal 
17278dcc1a9dSDamien Le Moal 	ret = submit_bio_wait(&bio);
17288dcc1a9dSDamien Le Moal 	if (ret)
17298dcc1a9dSDamien Le Moal 		goto free_page;
17308dcc1a9dSDamien Le Moal 
17316bac30bbSFabio M. De Francesco 	super = page_address(page);
17328dcc1a9dSDamien Le Moal 
17338dcc1a9dSDamien Le Moal 	ret = -EINVAL;
17348dcc1a9dSDamien Le Moal 	if (le32_to_cpu(super->s_magic) != ZONEFS_MAGIC)
17356bac30bbSFabio M. De Francesco 		goto free_page;
17368dcc1a9dSDamien Le Moal 
17378dcc1a9dSDamien Le Moal 	stored_crc = le32_to_cpu(super->s_crc);
17388dcc1a9dSDamien Le Moal 	super->s_crc = 0;
17398dcc1a9dSDamien Le Moal 	crc = crc32(~0U, (unsigned char *)super, sizeof(struct zonefs_super));
17408dcc1a9dSDamien Le Moal 	if (crc != stored_crc) {
17418dcc1a9dSDamien Le Moal 		zonefs_err(sb, "Invalid checksum (Expected 0x%08x, got 0x%08x)",
17428dcc1a9dSDamien Le Moal 			   crc, stored_crc);
17436bac30bbSFabio M. De Francesco 		goto free_page;
17448dcc1a9dSDamien Le Moal 	}
17458dcc1a9dSDamien Le Moal 
17468dcc1a9dSDamien Le Moal 	sbi->s_features = le64_to_cpu(super->s_features);
17478dcc1a9dSDamien Le Moal 	if (sbi->s_features & ~ZONEFS_F_DEFINED_FEATURES) {
17488dcc1a9dSDamien Le Moal 		zonefs_err(sb, "Unknown features set 0x%llx\n",
17498dcc1a9dSDamien Le Moal 			   sbi->s_features);
17506bac30bbSFabio M. De Francesco 		goto free_page;
17518dcc1a9dSDamien Le Moal 	}
17528dcc1a9dSDamien Le Moal 
17538dcc1a9dSDamien Le Moal 	if (sbi->s_features & ZONEFS_F_UID) {
17548dcc1a9dSDamien Le Moal 		sbi->s_uid = make_kuid(current_user_ns(),
17558dcc1a9dSDamien Le Moal 				       le32_to_cpu(super->s_uid));
17568dcc1a9dSDamien Le Moal 		if (!uid_valid(sbi->s_uid)) {
17578dcc1a9dSDamien Le Moal 			zonefs_err(sb, "Invalid UID feature\n");
17586bac30bbSFabio M. De Francesco 			goto free_page;
17598dcc1a9dSDamien Le Moal 		}
17608dcc1a9dSDamien Le Moal 	}
17618dcc1a9dSDamien Le Moal 
17628dcc1a9dSDamien Le Moal 	if (sbi->s_features & ZONEFS_F_GID) {
17638dcc1a9dSDamien Le Moal 		sbi->s_gid = make_kgid(current_user_ns(),
17648dcc1a9dSDamien Le Moal 				       le32_to_cpu(super->s_gid));
17658dcc1a9dSDamien Le Moal 		if (!gid_valid(sbi->s_gid)) {
17668dcc1a9dSDamien Le Moal 			zonefs_err(sb, "Invalid GID feature\n");
17676bac30bbSFabio M. De Francesco 			goto free_page;
17688dcc1a9dSDamien Le Moal 		}
17698dcc1a9dSDamien Le Moal 	}
17708dcc1a9dSDamien Le Moal 
17718dcc1a9dSDamien Le Moal 	if (sbi->s_features & ZONEFS_F_PERM)
17728dcc1a9dSDamien Le Moal 		sbi->s_perm = le32_to_cpu(super->s_perm);
17738dcc1a9dSDamien Le Moal 
17748dcc1a9dSDamien Le Moal 	if (memchr_inv(super->s_reserved, 0, sizeof(super->s_reserved))) {
17758dcc1a9dSDamien Le Moal 		zonefs_err(sb, "Reserved area is being used\n");
17766bac30bbSFabio M. De Francesco 		goto free_page;
17778dcc1a9dSDamien Le Moal 	}
17788dcc1a9dSDamien Le Moal 
1779568776f9SAndy Shevchenko 	import_uuid(&sbi->s_uuid, super->s_uuid);
17808dcc1a9dSDamien Le Moal 	ret = 0;
17818dcc1a9dSDamien Le Moal 
17828dcc1a9dSDamien Le Moal free_page:
17838dcc1a9dSDamien Le Moal 	__free_page(page);
17848dcc1a9dSDamien Le Moal 
17858dcc1a9dSDamien Le Moal 	return ret;
17868dcc1a9dSDamien Le Moal }
17878dcc1a9dSDamien Le Moal 
17888dcc1a9dSDamien Le Moal /*
17898dcc1a9dSDamien Le Moal  * Check that the device is zoned. If it is, get the list of zones and create
17908dcc1a9dSDamien Le Moal  * sub-directories and files according to the device zone configuration and
17918dcc1a9dSDamien Le Moal  * format options.
17928dcc1a9dSDamien Le Moal  */
17938dcc1a9dSDamien Le Moal static int zonefs_fill_super(struct super_block *sb, void *data, int silent)
17948dcc1a9dSDamien Le Moal {
17958dcc1a9dSDamien Le Moal 	struct zonefs_zone_data zd;
17968dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi;
17978dcc1a9dSDamien Le Moal 	struct inode *inode;
17988dcc1a9dSDamien Le Moal 	enum zonefs_ztype t;
17998dcc1a9dSDamien Le Moal 	int ret;
18008dcc1a9dSDamien Le Moal 
18018dcc1a9dSDamien Le Moal 	if (!bdev_is_zoned(sb->s_bdev)) {
18028dcc1a9dSDamien Le Moal 		zonefs_err(sb, "Not a zoned block device\n");
18038dcc1a9dSDamien Le Moal 		return -EINVAL;
18048dcc1a9dSDamien Le Moal 	}
18058dcc1a9dSDamien Le Moal 
18068dcc1a9dSDamien Le Moal 	/*
18078dcc1a9dSDamien Le Moal 	 * Initialize super block information: the maximum file size is updated
18088dcc1a9dSDamien Le Moal 	 * when the zone files are created so that the format option
18098dcc1a9dSDamien Le Moal 	 * ZONEFS_F_AGGRCNV which increases the maximum file size of a file
18108dcc1a9dSDamien Le Moal 	 * beyond the zone size is taken into account.
18118dcc1a9dSDamien Le Moal 	 */
18128dcc1a9dSDamien Le Moal 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
18138dcc1a9dSDamien Le Moal 	if (!sbi)
18148dcc1a9dSDamien Le Moal 		return -ENOMEM;
18158dcc1a9dSDamien Le Moal 
18168dcc1a9dSDamien Le Moal 	spin_lock_init(&sbi->s_lock);
18178dcc1a9dSDamien Le Moal 	sb->s_fs_info = sbi;
18188dcc1a9dSDamien Le Moal 	sb->s_magic = ZONEFS_MAGIC;
18198dcc1a9dSDamien Le Moal 	sb->s_maxbytes = 0;
18208dcc1a9dSDamien Le Moal 	sb->s_op = &zonefs_sops;
18218dcc1a9dSDamien Le Moal 	sb->s_time_gran	= 1;
18228dcc1a9dSDamien Le Moal 
18238dcc1a9dSDamien Le Moal 	/*
18240f1ba5f5SDamien Le Moal 	 * The block size is set to the device zone write granularity to ensure
18250f1ba5f5SDamien Le Moal 	 * that write operations are always aligned according to the device
18260f1ba5f5SDamien Le Moal 	 * interface constraints.
18278dcc1a9dSDamien Le Moal 	 */
18280f1ba5f5SDamien Le Moal 	sb_set_blocksize(sb, bdev_zone_write_granularity(sb->s_bdev));
18298dcc1a9dSDamien Le Moal 	sbi->s_zone_sectors_shift = ilog2(bdev_zone_sectors(sb->s_bdev));
18308dcc1a9dSDamien Le Moal 	sbi->s_uid = GLOBAL_ROOT_UID;
18318dcc1a9dSDamien Le Moal 	sbi->s_gid = GLOBAL_ROOT_GID;
18328dcc1a9dSDamien Le Moal 	sbi->s_perm = 0640;
18338dcc1a9dSDamien Le Moal 	sbi->s_mount_opts = ZONEFS_MNTOPT_ERRORS_RO;
18342b95a23cSDamien Le Moal 
18352b95a23cSDamien Le Moal 	atomic_set(&sbi->s_wro_seq_files, 0);
18362b95a23cSDamien Le Moal 	sbi->s_max_wro_seq_files = bdev_max_open_zones(sb->s_bdev);
183787c9ce3fSDamien Le Moal 	atomic_set(&sbi->s_active_seq_files, 0);
183887c9ce3fSDamien Le Moal 	sbi->s_max_active_seq_files = bdev_max_active_zones(sb->s_bdev);
183987c9ce3fSDamien Le Moal 
18408dcc1a9dSDamien Le Moal 	ret = zonefs_read_super(sb);
18418dcc1a9dSDamien Le Moal 	if (ret)
18428dcc1a9dSDamien Le Moal 		return ret;
18438dcc1a9dSDamien Le Moal 
18448dcc1a9dSDamien Le Moal 	ret = zonefs_parse_options(sb, data);
18458dcc1a9dSDamien Le Moal 	if (ret)
18468dcc1a9dSDamien Le Moal 		return ret;
18478dcc1a9dSDamien Le Moal 
18488dcc1a9dSDamien Le Moal 	memset(&zd, 0, sizeof(struct zonefs_zone_data));
18498dcc1a9dSDamien Le Moal 	zd.sb = sb;
18508dcc1a9dSDamien Le Moal 	ret = zonefs_get_zone_info(&zd);
18518dcc1a9dSDamien Le Moal 	if (ret)
18528dcc1a9dSDamien Le Moal 		goto cleanup;
18538dcc1a9dSDamien Le Moal 
18549277a6d4SDamien Le Moal 	ret = zonefs_sysfs_register(sb);
18559277a6d4SDamien Le Moal 	if (ret)
18569277a6d4SDamien Le Moal 		goto cleanup;
18579277a6d4SDamien Le Moal 
1858b623e347SChristoph Hellwig 	zonefs_info(sb, "Mounting %u zones", bdev_nr_zones(sb->s_bdev));
18598dcc1a9dSDamien Le Moal 
1860a2a513beSDamien Le Moal 	if (!sbi->s_max_wro_seq_files &&
186196eca145SDamien Le Moal 	    !sbi->s_max_active_seq_files &&
1862a2a513beSDamien Le Moal 	    sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) {
186396eca145SDamien Le Moal 		zonefs_info(sb,
186496eca145SDamien Le Moal 			"No open and active zone limits. Ignoring explicit_open mount option\n");
1865a2a513beSDamien Le Moal 		sbi->s_mount_opts &= ~ZONEFS_MNTOPT_EXPLICIT_OPEN;
1866a2a513beSDamien Le Moal 	}
1867a2a513beSDamien Le Moal 
18688dcc1a9dSDamien Le Moal 	/* Create root directory inode */
18698dcc1a9dSDamien Le Moal 	ret = -ENOMEM;
18708dcc1a9dSDamien Le Moal 	inode = new_inode(sb);
18718dcc1a9dSDamien Le Moal 	if (!inode)
18728dcc1a9dSDamien Le Moal 		goto cleanup;
18738dcc1a9dSDamien Le Moal 
1874b623e347SChristoph Hellwig 	inode->i_ino = bdev_nr_zones(sb->s_bdev);
18758dcc1a9dSDamien Le Moal 	inode->i_mode = S_IFDIR | 0555;
18768dcc1a9dSDamien Le Moal 	inode->i_ctime = inode->i_mtime = inode->i_atime = current_time(inode);
18778dcc1a9dSDamien Le Moal 	inode->i_op = &zonefs_dir_inode_operations;
18788dcc1a9dSDamien Le Moal 	inode->i_fop = &simple_dir_operations;
18798dcc1a9dSDamien Le Moal 	set_nlink(inode, 2);
18808dcc1a9dSDamien Le Moal 
18818dcc1a9dSDamien Le Moal 	sb->s_root = d_make_root(inode);
18828dcc1a9dSDamien Le Moal 	if (!sb->s_root)
18838dcc1a9dSDamien Le Moal 		goto cleanup;
18848dcc1a9dSDamien Le Moal 
18858dcc1a9dSDamien Le Moal 	/* Create and populate files in zone groups directories */
18868dcc1a9dSDamien Le Moal 	for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) {
18878dcc1a9dSDamien Le Moal 		ret = zonefs_create_zgroup(&zd, t);
18888dcc1a9dSDamien Le Moal 		if (ret)
18898dcc1a9dSDamien Le Moal 			break;
18908dcc1a9dSDamien Le Moal 	}
18918dcc1a9dSDamien Le Moal 
18928dcc1a9dSDamien Le Moal cleanup:
18938dcc1a9dSDamien Le Moal 	zonefs_cleanup_zone_info(&zd);
18948dcc1a9dSDamien Le Moal 
18958dcc1a9dSDamien Le Moal 	return ret;
18968dcc1a9dSDamien Le Moal }
18978dcc1a9dSDamien Le Moal 
18988dcc1a9dSDamien Le Moal static struct dentry *zonefs_mount(struct file_system_type *fs_type,
18998dcc1a9dSDamien Le Moal 				   int flags, const char *dev_name, void *data)
19008dcc1a9dSDamien Le Moal {
19018dcc1a9dSDamien Le Moal 	return mount_bdev(fs_type, flags, dev_name, data, zonefs_fill_super);
19028dcc1a9dSDamien Le Moal }
19038dcc1a9dSDamien Le Moal 
19048dcc1a9dSDamien Le Moal static void zonefs_kill_super(struct super_block *sb)
19058dcc1a9dSDamien Le Moal {
19068dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
19078dcc1a9dSDamien Le Moal 
19088dcc1a9dSDamien Le Moal 	if (sb->s_root)
19098dcc1a9dSDamien Le Moal 		d_genocide(sb->s_root);
19109277a6d4SDamien Le Moal 
19119277a6d4SDamien Le Moal 	zonefs_sysfs_unregister(sb);
19128dcc1a9dSDamien Le Moal 	kill_block_super(sb);
19138dcc1a9dSDamien Le Moal 	kfree(sbi);
19148dcc1a9dSDamien Le Moal }
19158dcc1a9dSDamien Le Moal 
19168dcc1a9dSDamien Le Moal /*
19178dcc1a9dSDamien Le Moal  * File system definition and registration.
19188dcc1a9dSDamien Le Moal  */
19198dcc1a9dSDamien Le Moal static struct file_system_type zonefs_type = {
19208dcc1a9dSDamien Le Moal 	.owner		= THIS_MODULE,
19218dcc1a9dSDamien Le Moal 	.name		= "zonefs",
19228dcc1a9dSDamien Le Moal 	.mount		= zonefs_mount,
19238dcc1a9dSDamien Le Moal 	.kill_sb	= zonefs_kill_super,
19248dcc1a9dSDamien Le Moal 	.fs_flags	= FS_REQUIRES_DEV,
19258dcc1a9dSDamien Le Moal };
19268dcc1a9dSDamien Le Moal 
19278dcc1a9dSDamien Le Moal static int __init zonefs_init_inodecache(void)
19288dcc1a9dSDamien Le Moal {
19298dcc1a9dSDamien Le Moal 	zonefs_inode_cachep = kmem_cache_create("zonefs_inode_cache",
19308dcc1a9dSDamien Le Moal 			sizeof(struct zonefs_inode_info), 0,
19318dcc1a9dSDamien Le Moal 			(SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT),
19328dcc1a9dSDamien Le Moal 			NULL);
19338dcc1a9dSDamien Le Moal 	if (zonefs_inode_cachep == NULL)
19348dcc1a9dSDamien Le Moal 		return -ENOMEM;
19358dcc1a9dSDamien Le Moal 	return 0;
19368dcc1a9dSDamien Le Moal }
19378dcc1a9dSDamien Le Moal 
19388dcc1a9dSDamien Le Moal static void zonefs_destroy_inodecache(void)
19398dcc1a9dSDamien Le Moal {
19408dcc1a9dSDamien Le Moal 	/*
19418dcc1a9dSDamien Le Moal 	 * Make sure all delayed rcu free inodes are flushed before we
19428dcc1a9dSDamien Le Moal 	 * destroy the inode cache.
19438dcc1a9dSDamien Le Moal 	 */
19448dcc1a9dSDamien Le Moal 	rcu_barrier();
19458dcc1a9dSDamien Le Moal 	kmem_cache_destroy(zonefs_inode_cachep);
19468dcc1a9dSDamien Le Moal }
19478dcc1a9dSDamien Le Moal 
19488dcc1a9dSDamien Le Moal static int __init zonefs_init(void)
19498dcc1a9dSDamien Le Moal {
19508dcc1a9dSDamien Le Moal 	int ret;
19518dcc1a9dSDamien Le Moal 
19528dcc1a9dSDamien Le Moal 	BUILD_BUG_ON(sizeof(struct zonefs_super) != ZONEFS_SUPER_SIZE);
19538dcc1a9dSDamien Le Moal 
19548dcc1a9dSDamien Le Moal 	ret = zonefs_init_inodecache();
19558dcc1a9dSDamien Le Moal 	if (ret)
19568dcc1a9dSDamien Le Moal 		return ret;
19578dcc1a9dSDamien Le Moal 
19584e458869SZhang Xiaoxu 	ret = zonefs_sysfs_init();
19599277a6d4SDamien Le Moal 	if (ret)
19609277a6d4SDamien Le Moal 		goto destroy_inodecache;
19619277a6d4SDamien Le Moal 
19624e458869SZhang Xiaoxu 	ret = register_filesystem(&zonefs_type);
19639277a6d4SDamien Le Moal 	if (ret)
19644e458869SZhang Xiaoxu 		goto sysfs_exit;
19658dcc1a9dSDamien Le Moal 
19668dcc1a9dSDamien Le Moal 	return 0;
19679277a6d4SDamien Le Moal 
19684e458869SZhang Xiaoxu sysfs_exit:
19694e458869SZhang Xiaoxu 	zonefs_sysfs_exit();
19709277a6d4SDamien Le Moal destroy_inodecache:
19719277a6d4SDamien Le Moal 	zonefs_destroy_inodecache();
19729277a6d4SDamien Le Moal 
19739277a6d4SDamien Le Moal 	return ret;
19748dcc1a9dSDamien Le Moal }
19758dcc1a9dSDamien Le Moal 
19768dcc1a9dSDamien Le Moal static void __exit zonefs_exit(void)
19778dcc1a9dSDamien Le Moal {
19784e458869SZhang Xiaoxu 	unregister_filesystem(&zonefs_type);
19799277a6d4SDamien Le Moal 	zonefs_sysfs_exit();
19808dcc1a9dSDamien Le Moal 	zonefs_destroy_inodecache();
19818dcc1a9dSDamien Le Moal }
19828dcc1a9dSDamien Le Moal 
19838dcc1a9dSDamien Le Moal MODULE_AUTHOR("Damien Le Moal");
19848dcc1a9dSDamien Le Moal MODULE_DESCRIPTION("Zone file system for zoned block devices");
19858dcc1a9dSDamien Le Moal MODULE_LICENSE("GPL");
19868ffea259SNaohiro Aota MODULE_ALIAS_FS("zonefs");
19878dcc1a9dSDamien Le Moal module_init(zonefs_init);
19888dcc1a9dSDamien Le Moal module_exit(zonefs_exit);
1989