xref: /openbmc/linux/fs/zonefs/super.c (revision b623e347323f6464b20fb0d899a0a73522ed8f6c)
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 	/*
4487c9ce3fSDamien Le Moal 	 * If the zone is active, that is, if it is explicitly open or
4587c9ce3fSDamien Le Moal 	 * partially written, check if it was already accounted as active.
4687c9ce3fSDamien Le Moal 	 */
4787c9ce3fSDamien Le Moal 	if ((zi->i_flags & ZONEFS_ZONE_OPEN) ||
4887c9ce3fSDamien Le Moal 	    (zi->i_wpoffset > 0 && zi->i_wpoffset < zi->i_max_size)) {
4987c9ce3fSDamien Le Moal 		if (!(zi->i_flags & ZONEFS_ZONE_ACTIVE)) {
5087c9ce3fSDamien Le Moal 			zi->i_flags |= ZONEFS_ZONE_ACTIVE;
5187c9ce3fSDamien Le Moal 			atomic_inc(&sbi->s_active_seq_files);
5287c9ce3fSDamien Le Moal 		}
5387c9ce3fSDamien Le Moal 		return;
5487c9ce3fSDamien Le Moal 	}
5587c9ce3fSDamien Le Moal 
5687c9ce3fSDamien Le Moal 	/* The zone is not active. If it was, update the active count */
5787c9ce3fSDamien Le Moal 	if (zi->i_flags & ZONEFS_ZONE_ACTIVE) {
5887c9ce3fSDamien Le Moal 		zi->i_flags &= ~ZONEFS_ZONE_ACTIVE;
5987c9ce3fSDamien Le Moal 		atomic_dec(&sbi->s_active_seq_files);
6087c9ce3fSDamien Le Moal 	}
6187c9ce3fSDamien Le Moal }
6287c9ce3fSDamien Le Moal 
635498d5f9SJohannes Thumshirn static inline int zonefs_zone_mgmt(struct inode *inode,
645498d5f9SJohannes Thumshirn 				   enum req_opf op)
655498d5f9SJohannes Thumshirn {
665498d5f9SJohannes Thumshirn 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
675498d5f9SJohannes Thumshirn 	int ret;
685498d5f9SJohannes Thumshirn 
695498d5f9SJohannes Thumshirn 	lockdep_assert_held(&zi->i_truncate_mutex);
705498d5f9SJohannes Thumshirn 
711da18a29SDamien Le Moal 	/*
721da18a29SDamien Le Moal 	 * With ZNS drives, closing an explicitly open zone that has not been
731da18a29SDamien Le Moal 	 * written will change the zone state to "closed", that is, the zone
741da18a29SDamien Le Moal 	 * will remain active. Since this can then cause failure of explicit
751da18a29SDamien Le Moal 	 * open operation on other zones if the drive active zone resources
761da18a29SDamien Le Moal 	 * are exceeded, make sure that the zone does not remain active by
771da18a29SDamien Le Moal 	 * resetting it.
781da18a29SDamien Le Moal 	 */
791da18a29SDamien Le Moal 	if (op == REQ_OP_ZONE_CLOSE && !zi->i_wpoffset)
801da18a29SDamien Le Moal 		op = REQ_OP_ZONE_RESET;
811da18a29SDamien Le Moal 
8262ab1aadSJohannes Thumshirn 	trace_zonefs_zone_mgmt(inode, op);
835498d5f9SJohannes Thumshirn 	ret = blkdev_zone_mgmt(inode->i_sb->s_bdev, op, zi->i_zsector,
845498d5f9SJohannes Thumshirn 			       zi->i_zone_size >> SECTOR_SHIFT, GFP_NOFS);
855498d5f9SJohannes Thumshirn 	if (ret) {
865498d5f9SJohannes Thumshirn 		zonefs_err(inode->i_sb,
875498d5f9SJohannes Thumshirn 			   "Zone management operation %s at %llu failed %d\n",
885498d5f9SJohannes Thumshirn 			   blk_op_str(op), zi->i_zsector, ret);
895498d5f9SJohannes Thumshirn 		return ret;
905498d5f9SJohannes Thumshirn 	}
915498d5f9SJohannes Thumshirn 
925498d5f9SJohannes Thumshirn 	return 0;
935498d5f9SJohannes Thumshirn }
945498d5f9SJohannes Thumshirn 
95b5c00e97SJohannes Thumshirn static inline void zonefs_i_size_write(struct inode *inode, loff_t isize)
96b5c00e97SJohannes Thumshirn {
97b5c00e97SJohannes Thumshirn 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
98b5c00e97SJohannes Thumshirn 
99b5c00e97SJohannes Thumshirn 	i_size_write(inode, isize);
100b5c00e97SJohannes Thumshirn 	/*
101b5c00e97SJohannes Thumshirn 	 * A full zone is no longer open/active and does not need
102b5c00e97SJohannes Thumshirn 	 * explicit closing.
103b5c00e97SJohannes Thumshirn 	 */
10487c9ce3fSDamien Le Moal 	if (isize >= zi->i_max_size) {
10587c9ce3fSDamien Le Moal 		struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
10687c9ce3fSDamien Le Moal 
10787c9ce3fSDamien Le Moal 		if (zi->i_flags & ZONEFS_ZONE_ACTIVE)
10887c9ce3fSDamien Le Moal 			atomic_dec(&sbi->s_active_seq_files);
10987c9ce3fSDamien Le Moal 		zi->i_flags &= ~(ZONEFS_ZONE_OPEN | ZONEFS_ZONE_ACTIVE);
11087c9ce3fSDamien Le Moal 	}
111b5c00e97SJohannes Thumshirn }
112b5c00e97SJohannes Thumshirn 
113c1c1204cSDamien Le Moal static int zonefs_read_iomap_begin(struct inode *inode, loff_t offset,
114c1c1204cSDamien Le Moal 				   loff_t length, unsigned int flags,
115c1c1204cSDamien Le Moal 				   struct iomap *iomap, struct iomap *srcmap)
1168dcc1a9dSDamien Le Moal {
1178dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
1188dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
1198dcc1a9dSDamien Le Moal 	loff_t isize;
1208dcc1a9dSDamien Le Moal 
121c1c1204cSDamien Le Moal 	/*
122c1c1204cSDamien Le Moal 	 * All blocks are always mapped below EOF. If reading past EOF,
123c1c1204cSDamien Le Moal 	 * act as if there is a hole up to the file maximum size.
124c1c1204cSDamien Le Moal 	 */
125c1c1204cSDamien Le Moal 	mutex_lock(&zi->i_truncate_mutex);
126c1c1204cSDamien Le Moal 	iomap->bdev = inode->i_sb->s_bdev;
127c1c1204cSDamien Le Moal 	iomap->offset = ALIGN_DOWN(offset, sb->s_blocksize);
128c1c1204cSDamien Le Moal 	isize = i_size_read(inode);
129c1c1204cSDamien Le Moal 	if (iomap->offset >= isize) {
130c1c1204cSDamien Le Moal 		iomap->type = IOMAP_HOLE;
131c1c1204cSDamien Le Moal 		iomap->addr = IOMAP_NULL_ADDR;
132c1c1204cSDamien Le Moal 		iomap->length = length;
133c1c1204cSDamien Le Moal 	} else {
134c1c1204cSDamien Le Moal 		iomap->type = IOMAP_MAPPED;
135c1c1204cSDamien Le Moal 		iomap->addr = (zi->i_zsector << SECTOR_SHIFT) + iomap->offset;
136c1c1204cSDamien Le Moal 		iomap->length = isize - iomap->offset;
137c1c1204cSDamien Le Moal 	}
138c1c1204cSDamien Le Moal 	mutex_unlock(&zi->i_truncate_mutex);
139c1c1204cSDamien Le Moal 
140c1c1204cSDamien Le Moal 	trace_zonefs_iomap_begin(inode, iomap);
141c1c1204cSDamien Le Moal 
142c1c1204cSDamien Le Moal 	return 0;
143c1c1204cSDamien Le Moal }
144c1c1204cSDamien Le Moal 
145c1c1204cSDamien Le Moal static const struct iomap_ops zonefs_read_iomap_ops = {
146c1c1204cSDamien Le Moal 	.iomap_begin	= zonefs_read_iomap_begin,
147c1c1204cSDamien Le Moal };
148c1c1204cSDamien Le Moal 
149c1c1204cSDamien Le Moal static int zonefs_write_iomap_begin(struct inode *inode, loff_t offset,
150c1c1204cSDamien Le Moal 				    loff_t length, unsigned int flags,
151c1c1204cSDamien Le Moal 				    struct iomap *iomap, struct iomap *srcmap)
152c1c1204cSDamien Le Moal {
153c1c1204cSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
154c1c1204cSDamien Le Moal 	struct super_block *sb = inode->i_sb;
155c1c1204cSDamien Le Moal 	loff_t isize;
156c1c1204cSDamien Le Moal 
157c1c1204cSDamien Le Moal 	/* All write I/Os should always be within the file maximum size */
1588dcc1a9dSDamien Le Moal 	if (WARN_ON_ONCE(offset + length > zi->i_max_size))
1598dcc1a9dSDamien Le Moal 		return -EIO;
1608dcc1a9dSDamien Le Moal 
1618dcc1a9dSDamien Le Moal 	/*
1628dcc1a9dSDamien Le Moal 	 * Sequential zones can only accept direct writes. This is already
1638dcc1a9dSDamien Le Moal 	 * checked when writes are issued, so warn if we see a page writeback
1648dcc1a9dSDamien Le Moal 	 * operation.
1658dcc1a9dSDamien Le Moal 	 */
1668dcc1a9dSDamien Le Moal 	if (WARN_ON_ONCE(zi->i_ztype == ZONEFS_ZTYPE_SEQ &&
167c1c1204cSDamien Le Moal 			 !(flags & IOMAP_DIRECT)))
1688dcc1a9dSDamien Le Moal 		return -EIO;
1698dcc1a9dSDamien Le Moal 
1708dcc1a9dSDamien Le Moal 	/*
1718dcc1a9dSDamien Le Moal 	 * For conventional zones, all blocks are always mapped. For sequential
1728dcc1a9dSDamien Le Moal 	 * zones, all blocks after always mapped below the inode size (zone
1738dcc1a9dSDamien Le Moal 	 * write pointer) and unwriten beyond.
1748dcc1a9dSDamien Le Moal 	 */
1758dcc1a9dSDamien Le Moal 	mutex_lock(&zi->i_truncate_mutex);
1768dcc1a9dSDamien Le Moal 	iomap->bdev = inode->i_sb->s_bdev;
177c1c1204cSDamien Le Moal 	iomap->offset = ALIGN_DOWN(offset, sb->s_blocksize);
1788dcc1a9dSDamien Le Moal 	iomap->addr = (zi->i_zsector << SECTOR_SHIFT) + iomap->offset;
179c1c1204cSDamien Le Moal 	isize = i_size_read(inode);
180c1c1204cSDamien Le Moal 	if (iomap->offset >= isize) {
181c1c1204cSDamien Le Moal 		iomap->type = IOMAP_UNWRITTEN;
182c1c1204cSDamien Le Moal 		iomap->length = zi->i_max_size - iomap->offset;
183c1c1204cSDamien Le Moal 	} else {
184c1c1204cSDamien Le Moal 		iomap->type = IOMAP_MAPPED;
185c1c1204cSDamien Le Moal 		iomap->length = isize - iomap->offset;
186c1c1204cSDamien Le Moal 	}
187c1c1204cSDamien Le Moal 	mutex_unlock(&zi->i_truncate_mutex);
1888dcc1a9dSDamien Le Moal 
18962ab1aadSJohannes Thumshirn 	trace_zonefs_iomap_begin(inode, iomap);
19062ab1aadSJohannes Thumshirn 
1918dcc1a9dSDamien Le Moal 	return 0;
1928dcc1a9dSDamien Le Moal }
1938dcc1a9dSDamien Le Moal 
194c1c1204cSDamien Le Moal static const struct iomap_ops zonefs_write_iomap_ops = {
195c1c1204cSDamien Le Moal 	.iomap_begin	= zonefs_write_iomap_begin,
1968dcc1a9dSDamien Le Moal };
1978dcc1a9dSDamien Le Moal 
1987479c505SMatthew Wilcox (Oracle) static int zonefs_read_folio(struct file *unused, struct folio *folio)
1998dcc1a9dSDamien Le Moal {
200c1c1204cSDamien Le Moal 	return iomap_read_folio(folio, &zonefs_read_iomap_ops);
2018dcc1a9dSDamien Le Moal }
2028dcc1a9dSDamien Le Moal 
2039d24a13aSMatthew Wilcox (Oracle) static void zonefs_readahead(struct readahead_control *rac)
2048dcc1a9dSDamien Le Moal {
205c1c1204cSDamien Le Moal 	iomap_readahead(rac, &zonefs_read_iomap_ops);
2068dcc1a9dSDamien Le Moal }
2078dcc1a9dSDamien Le Moal 
2088dcc1a9dSDamien Le Moal /*
2098dcc1a9dSDamien Le Moal  * Map blocks for page writeback. This is used only on conventional zone files,
2108dcc1a9dSDamien Le Moal  * which implies that the page range can only be within the fixed inode size.
2118dcc1a9dSDamien Le Moal  */
212c1c1204cSDamien Le Moal static int zonefs_write_map_blocks(struct iomap_writepage_ctx *wpc,
2138dcc1a9dSDamien Le Moal 				   struct inode *inode, loff_t offset)
2148dcc1a9dSDamien Le Moal {
2158dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
2168dcc1a9dSDamien Le Moal 
2178dcc1a9dSDamien Le Moal 	if (WARN_ON_ONCE(zi->i_ztype != ZONEFS_ZTYPE_CNV))
2188dcc1a9dSDamien Le Moal 		return -EIO;
2198dcc1a9dSDamien Le Moal 	if (WARN_ON_ONCE(offset >= i_size_read(inode)))
2208dcc1a9dSDamien Le Moal 		return -EIO;
2218dcc1a9dSDamien Le Moal 
2228dcc1a9dSDamien Le Moal 	/* If the mapping is already OK, nothing needs to be done */
2238dcc1a9dSDamien Le Moal 	if (offset >= wpc->iomap.offset &&
2248dcc1a9dSDamien Le Moal 	    offset < wpc->iomap.offset + wpc->iomap.length)
2258dcc1a9dSDamien Le Moal 		return 0;
2268dcc1a9dSDamien Le Moal 
227c1c1204cSDamien Le Moal 	return zonefs_write_iomap_begin(inode, offset, zi->i_max_size - offset,
2288dcc1a9dSDamien Le Moal 					IOMAP_WRITE, &wpc->iomap, NULL);
2298dcc1a9dSDamien Le Moal }
2308dcc1a9dSDamien Le Moal 
2318dcc1a9dSDamien Le Moal static const struct iomap_writeback_ops zonefs_writeback_ops = {
232c1c1204cSDamien Le Moal 	.map_blocks		= zonefs_write_map_blocks,
2338dcc1a9dSDamien Le Moal };
2348dcc1a9dSDamien Le Moal 
2358dcc1a9dSDamien Le Moal static int zonefs_writepage(struct page *page, struct writeback_control *wbc)
2368dcc1a9dSDamien Le Moal {
2378dcc1a9dSDamien Le Moal 	struct iomap_writepage_ctx wpc = { };
2388dcc1a9dSDamien Le Moal 
2398dcc1a9dSDamien Le Moal 	return iomap_writepage(page, wbc, &wpc, &zonefs_writeback_ops);
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 	.writepage		= zonefs_writepage,
2708dcc1a9dSDamien Le Moal 	.writepages		= zonefs_writepages,
271187c82cbSMatthew Wilcox (Oracle) 	.dirty_folio		= filemap_dirty_folio,
2728597447dSMatthew Wilcox (Oracle) 	.release_folio		= iomap_release_folio,
273d82354f6SMatthew Wilcox (Oracle) 	.invalidate_folio	= iomap_invalidate_folio,
2748dcc1a9dSDamien Le Moal 	.migratepage		= iomap_migrate_page,
2758dcc1a9dSDamien Le Moal 	.is_partially_uptodate	= iomap_is_partially_uptodate,
2768dcc1a9dSDamien Le Moal 	.error_remove_page	= generic_error_remove_page,
2778dcc1a9dSDamien Le Moal 	.direct_IO		= noop_direct_IO,
2781601ea06SDamien Le Moal 	.swap_activate		= zonefs_swap_activate,
2798dcc1a9dSDamien Le Moal };
2808dcc1a9dSDamien Le Moal 
2818dcc1a9dSDamien Le Moal static void zonefs_update_stats(struct inode *inode, loff_t new_isize)
2828dcc1a9dSDamien Le Moal {
2838dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
2848dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
2858dcc1a9dSDamien Le Moal 	loff_t old_isize = i_size_read(inode);
2868dcc1a9dSDamien Le Moal 	loff_t nr_blocks;
2878dcc1a9dSDamien Le Moal 
2888dcc1a9dSDamien Le Moal 	if (new_isize == old_isize)
2898dcc1a9dSDamien Le Moal 		return;
2908dcc1a9dSDamien Le Moal 
2918dcc1a9dSDamien Le Moal 	spin_lock(&sbi->s_lock);
2928dcc1a9dSDamien Le Moal 
2938dcc1a9dSDamien Le Moal 	/*
2948dcc1a9dSDamien Le Moal 	 * This may be called for an update after an IO error.
2958dcc1a9dSDamien Le Moal 	 * So beware of the values seen.
2968dcc1a9dSDamien Le Moal 	 */
2978dcc1a9dSDamien Le Moal 	if (new_isize < old_isize) {
2988dcc1a9dSDamien Le Moal 		nr_blocks = (old_isize - new_isize) >> sb->s_blocksize_bits;
2998dcc1a9dSDamien Le Moal 		if (sbi->s_used_blocks > nr_blocks)
3008dcc1a9dSDamien Le Moal 			sbi->s_used_blocks -= nr_blocks;
3018dcc1a9dSDamien Le Moal 		else
3028dcc1a9dSDamien Le Moal 			sbi->s_used_blocks = 0;
3038dcc1a9dSDamien Le Moal 	} else {
3048dcc1a9dSDamien Le Moal 		sbi->s_used_blocks +=
3058dcc1a9dSDamien Le Moal 			(new_isize - old_isize) >> sb->s_blocksize_bits;
3068dcc1a9dSDamien Le Moal 		if (sbi->s_used_blocks > sbi->s_blocks)
3078dcc1a9dSDamien Le Moal 			sbi->s_used_blocks = sbi->s_blocks;
3088dcc1a9dSDamien Le Moal 	}
3098dcc1a9dSDamien Le Moal 
3108dcc1a9dSDamien Le Moal 	spin_unlock(&sbi->s_lock);
3118dcc1a9dSDamien Le Moal }
3128dcc1a9dSDamien Le Moal 
3138dcc1a9dSDamien Le Moal /*
3148dcc1a9dSDamien Le Moal  * Check a zone condition and adjust its file inode access permissions for
3158dcc1a9dSDamien Le Moal  * offline and readonly zones. Return the inode size corresponding to the
3168dcc1a9dSDamien Le Moal  * amount of readable data in the zone.
3178dcc1a9dSDamien Le Moal  */
3188dcc1a9dSDamien Le Moal static loff_t zonefs_check_zone_condition(struct inode *inode,
319ccf4ad7dSDamien Le Moal 					  struct blk_zone *zone, bool warn,
320ccf4ad7dSDamien Le Moal 					  bool mount)
3218dcc1a9dSDamien Le Moal {
3228dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
3238dcc1a9dSDamien Le Moal 
3248dcc1a9dSDamien Le Moal 	switch (zone->cond) {
3258dcc1a9dSDamien Le Moal 	case BLK_ZONE_COND_OFFLINE:
3268dcc1a9dSDamien Le Moal 		/*
3278dcc1a9dSDamien Le Moal 		 * Dead zone: make the inode immutable, disable all accesses
3288dcc1a9dSDamien Le Moal 		 * and set the file size to 0 (zone wp set to zone start).
3298dcc1a9dSDamien Le Moal 		 */
3308dcc1a9dSDamien Le Moal 		if (warn)
3318dcc1a9dSDamien Le Moal 			zonefs_warn(inode->i_sb, "inode %lu: offline zone\n",
3328dcc1a9dSDamien Le Moal 				    inode->i_ino);
3338dcc1a9dSDamien Le Moal 		inode->i_flags |= S_IMMUTABLE;
3348dcc1a9dSDamien Le Moal 		inode->i_mode &= ~0777;
3358dcc1a9dSDamien Le Moal 		zone->wp = zone->start;
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;
354ccf4ad7dSDamien Le Moal 			return 0;
355ccf4ad7dSDamien Le Moal 		}
3568dcc1a9dSDamien Le Moal 		inode->i_mode &= ~0222;
357ccf4ad7dSDamien Le Moal 		return i_size_read(inode);
358059c0103SShin'ichiro Kawasaki 	case BLK_ZONE_COND_FULL:
359059c0103SShin'ichiro Kawasaki 		/* The write pointer of full zones is invalid. */
360059c0103SShin'ichiro Kawasaki 		return zi->i_max_size;
3618dcc1a9dSDamien Le Moal 	default:
3628dcc1a9dSDamien Le Moal 		if (zi->i_ztype == ZONEFS_ZTYPE_CNV)
3638dcc1a9dSDamien Le Moal 			return zi->i_max_size;
3648dcc1a9dSDamien Le Moal 		return (zone->wp - zone->start) << SECTOR_SHIFT;
3658dcc1a9dSDamien Le Moal 	}
3668dcc1a9dSDamien Le Moal }
3678dcc1a9dSDamien Le Moal 
3688dcc1a9dSDamien Le Moal struct zonefs_ioerr_data {
3698dcc1a9dSDamien Le Moal 	struct inode	*inode;
3708dcc1a9dSDamien Le Moal 	bool		write;
3718dcc1a9dSDamien Le Moal };
3728dcc1a9dSDamien Le Moal 
3738dcc1a9dSDamien Le Moal static int zonefs_io_error_cb(struct blk_zone *zone, unsigned int idx,
3748dcc1a9dSDamien Le Moal 			      void *data)
3758dcc1a9dSDamien Le Moal {
3768dcc1a9dSDamien Le Moal 	struct zonefs_ioerr_data *err = data;
3778dcc1a9dSDamien Le Moal 	struct inode *inode = err->inode;
3788dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
3798dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
3808dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
3818dcc1a9dSDamien Le Moal 	loff_t isize, data_size;
3828dcc1a9dSDamien Le Moal 
3838dcc1a9dSDamien Le Moal 	/*
3848dcc1a9dSDamien Le Moal 	 * Check the zone condition: if the zone is not "bad" (offline or
3858dcc1a9dSDamien Le Moal 	 * read-only), read errors are simply signaled to the IO issuer as long
3868dcc1a9dSDamien Le Moal 	 * as there is no inconsistency between the inode size and the amount of
3878dcc1a9dSDamien Le Moal 	 * data writen in the zone (data_size).
3888dcc1a9dSDamien Le Moal 	 */
389ccf4ad7dSDamien Le Moal 	data_size = zonefs_check_zone_condition(inode, zone, true, false);
3908dcc1a9dSDamien Le Moal 	isize = i_size_read(inode);
3918dcc1a9dSDamien Le Moal 	if (zone->cond != BLK_ZONE_COND_OFFLINE &&
3928dcc1a9dSDamien Le Moal 	    zone->cond != BLK_ZONE_COND_READONLY &&
3938dcc1a9dSDamien Le Moal 	    !err->write && isize == data_size)
3948dcc1a9dSDamien Le Moal 		return 0;
3958dcc1a9dSDamien Le Moal 
3968dcc1a9dSDamien Le Moal 	/*
3978dcc1a9dSDamien Le Moal 	 * At this point, we detected either a bad zone or an inconsistency
3988dcc1a9dSDamien Le Moal 	 * between the inode size and the amount of data written in the zone.
3998dcc1a9dSDamien Le Moal 	 * For the latter case, the cause may be a write IO error or an external
4008dcc1a9dSDamien Le Moal 	 * action on the device. Two error patterns exist:
4018dcc1a9dSDamien Le Moal 	 * 1) The inode size is lower than the amount of data in the zone:
4028dcc1a9dSDamien Le Moal 	 *    a write operation partially failed and data was writen at the end
4038dcc1a9dSDamien Le Moal 	 *    of the file. This can happen in the case of a large direct IO
4048dcc1a9dSDamien Le Moal 	 *    needing several BIOs and/or write requests to be processed.
4058dcc1a9dSDamien Le Moal 	 * 2) The inode size is larger than the amount of data in the zone:
4068dcc1a9dSDamien Le Moal 	 *    this can happen with a deferred write error with the use of the
4078dcc1a9dSDamien Le Moal 	 *    device side write cache after getting successful write IO
4088dcc1a9dSDamien Le Moal 	 *    completions. Other possibilities are (a) an external corruption,
4098dcc1a9dSDamien Le Moal 	 *    e.g. an application reset the zone directly, or (b) the device
4108dcc1a9dSDamien Le Moal 	 *    has a serious problem (e.g. firmware bug).
4118dcc1a9dSDamien Le Moal 	 *
4128dcc1a9dSDamien Le Moal 	 * In all cases, warn about inode size inconsistency and handle the
4138dcc1a9dSDamien Le Moal 	 * IO error according to the zone condition and to the mount options.
4148dcc1a9dSDamien Le Moal 	 */
4158dcc1a9dSDamien Le Moal 	if (zi->i_ztype == ZONEFS_ZTYPE_SEQ && isize != data_size)
4168dcc1a9dSDamien Le Moal 		zonefs_warn(sb, "inode %lu: invalid size %lld (should be %lld)\n",
4178dcc1a9dSDamien Le Moal 			    inode->i_ino, isize, data_size);
4188dcc1a9dSDamien Le Moal 
4198dcc1a9dSDamien Le Moal 	/*
4208dcc1a9dSDamien Le Moal 	 * First handle bad zones signaled by hardware. The mount options
4218dcc1a9dSDamien Le Moal 	 * errors=zone-ro and errors=zone-offline result in changing the
4228dcc1a9dSDamien Le Moal 	 * zone condition to read-only and offline respectively, as if the
4238dcc1a9dSDamien Le Moal 	 * condition was signaled by the hardware.
4248dcc1a9dSDamien Le Moal 	 */
4258dcc1a9dSDamien Le Moal 	if (zone->cond == BLK_ZONE_COND_OFFLINE ||
4268dcc1a9dSDamien Le Moal 	    sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL) {
4278dcc1a9dSDamien Le Moal 		zonefs_warn(sb, "inode %lu: read/write access disabled\n",
4288dcc1a9dSDamien Le Moal 			    inode->i_ino);
4298dcc1a9dSDamien Le Moal 		if (zone->cond != BLK_ZONE_COND_OFFLINE) {
4308dcc1a9dSDamien Le Moal 			zone->cond = BLK_ZONE_COND_OFFLINE;
4318dcc1a9dSDamien Le Moal 			data_size = zonefs_check_zone_condition(inode, zone,
432ccf4ad7dSDamien Le Moal 								false, false);
4338dcc1a9dSDamien Le Moal 		}
4348dcc1a9dSDamien Le Moal 	} else if (zone->cond == BLK_ZONE_COND_READONLY ||
4358dcc1a9dSDamien Le Moal 		   sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO) {
4368dcc1a9dSDamien Le Moal 		zonefs_warn(sb, "inode %lu: write access disabled\n",
4378dcc1a9dSDamien Le Moal 			    inode->i_ino);
4388dcc1a9dSDamien Le Moal 		if (zone->cond != BLK_ZONE_COND_READONLY) {
4398dcc1a9dSDamien Le Moal 			zone->cond = BLK_ZONE_COND_READONLY;
4408dcc1a9dSDamien Le Moal 			data_size = zonefs_check_zone_condition(inode, zone,
441ccf4ad7dSDamien Le Moal 								false, false);
4428dcc1a9dSDamien Le Moal 		}
4438dcc1a9dSDamien Le Moal 	}
4448dcc1a9dSDamien Le Moal 
4458dcc1a9dSDamien Le Moal 	/*
446b5c00e97SJohannes Thumshirn 	 * If the filesystem is mounted with the explicit-open mount option, we
447b5c00e97SJohannes Thumshirn 	 * need to clear the ZONEFS_ZONE_OPEN flag if the zone transitioned to
448b5c00e97SJohannes Thumshirn 	 * the read-only or offline condition, to avoid attempting an explicit
449b5c00e97SJohannes Thumshirn 	 * close of the zone when the inode file is closed.
450b5c00e97SJohannes Thumshirn 	 */
451b5c00e97SJohannes Thumshirn 	if ((sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) &&
452b5c00e97SJohannes Thumshirn 	    (zone->cond == BLK_ZONE_COND_OFFLINE ||
453b5c00e97SJohannes Thumshirn 	     zone->cond == BLK_ZONE_COND_READONLY))
454b5c00e97SJohannes Thumshirn 		zi->i_flags &= ~ZONEFS_ZONE_OPEN;
455b5c00e97SJohannes Thumshirn 
456b5c00e97SJohannes Thumshirn 	/*
4578dcc1a9dSDamien Le Moal 	 * If error=remount-ro was specified, any error result in remounting
4588dcc1a9dSDamien Le Moal 	 * the volume as read-only.
4598dcc1a9dSDamien Le Moal 	 */
4608dcc1a9dSDamien Le Moal 	if ((sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO) && !sb_rdonly(sb)) {
4618dcc1a9dSDamien Le Moal 		zonefs_warn(sb, "remounting filesystem read-only\n");
4628dcc1a9dSDamien Le Moal 		sb->s_flags |= SB_RDONLY;
4638dcc1a9dSDamien Le Moal 	}
4648dcc1a9dSDamien Le Moal 
4658dcc1a9dSDamien Le Moal 	/*
4668dcc1a9dSDamien Le Moal 	 * Update block usage stats and the inode size  to prevent access to
4678dcc1a9dSDamien Le Moal 	 * invalid data.
4688dcc1a9dSDamien Le Moal 	 */
4698dcc1a9dSDamien Le Moal 	zonefs_update_stats(inode, data_size);
470b5c00e97SJohannes Thumshirn 	zonefs_i_size_write(inode, data_size);
4718dcc1a9dSDamien Le Moal 	zi->i_wpoffset = data_size;
47287c9ce3fSDamien Le Moal 	zonefs_account_active(inode);
4738dcc1a9dSDamien Le Moal 
4748dcc1a9dSDamien Le Moal 	return 0;
4758dcc1a9dSDamien Le Moal }
4768dcc1a9dSDamien Le Moal 
4778dcc1a9dSDamien Le Moal /*
4788dcc1a9dSDamien Le Moal  * When an file IO error occurs, check the file zone to see if there is a change
4798dcc1a9dSDamien Le Moal  * in the zone condition (e.g. offline or read-only). For a failed write to a
4808dcc1a9dSDamien Le Moal  * sequential zone, the zone write pointer position must also be checked to
4818dcc1a9dSDamien Le Moal  * eventually correct the file size and zonefs inode write pointer offset
4828dcc1a9dSDamien Le Moal  * (which can be out of sync with the drive due to partial write failures).
4838dcc1a9dSDamien Le Moal  */
48448d546a8SJohannes Thumshirn static void __zonefs_io_error(struct inode *inode, bool write)
4858dcc1a9dSDamien Le Moal {
4868dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
4878dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
4888dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
4898dcc1a9dSDamien Le Moal 	unsigned int noio_flag;
4908dcc1a9dSDamien Le Moal 	unsigned int nr_zones =
491e3c3155bSJohannes Thumshirn 		zi->i_zone_size >> (sbi->s_zone_sectors_shift + SECTOR_SHIFT);
4928dcc1a9dSDamien Le Moal 	struct zonefs_ioerr_data err = {
4938dcc1a9dSDamien Le Moal 		.inode = inode,
4948dcc1a9dSDamien Le Moal 		.write = write,
4958dcc1a9dSDamien Le Moal 	};
4968dcc1a9dSDamien Le Moal 	int ret;
4978dcc1a9dSDamien Le Moal 
4988dcc1a9dSDamien Le Moal 	/*
4998dcc1a9dSDamien Le Moal 	 * Memory allocations in blkdev_report_zones() can trigger a memory
5008dcc1a9dSDamien Le Moal 	 * reclaim which may in turn cause a recursion into zonefs as well as
5018dcc1a9dSDamien Le Moal 	 * struct request allocations for the same device. The former case may
5028dcc1a9dSDamien Le Moal 	 * end up in a deadlock on the inode truncate mutex, while the latter
5038dcc1a9dSDamien Le Moal 	 * may prevent IO forward progress. Executing the report zones under
5048dcc1a9dSDamien Le Moal 	 * the GFP_NOIO context avoids both problems.
5058dcc1a9dSDamien Le Moal 	 */
5068dcc1a9dSDamien Le Moal 	noio_flag = memalloc_noio_save();
5078dcc1a9dSDamien Le Moal 	ret = blkdev_report_zones(sb->s_bdev, zi->i_zsector, nr_zones,
5088dcc1a9dSDamien Le Moal 				  zonefs_io_error_cb, &err);
5098dcc1a9dSDamien Le Moal 	if (ret != nr_zones)
5108dcc1a9dSDamien Le Moal 		zonefs_err(sb, "Get inode %lu zone information failed %d\n",
5118dcc1a9dSDamien Le Moal 			   inode->i_ino, ret);
5128dcc1a9dSDamien Le Moal 	memalloc_noio_restore(noio_flag);
51348d546a8SJohannes Thumshirn }
5148dcc1a9dSDamien Le Moal 
51548d546a8SJohannes Thumshirn static void zonefs_io_error(struct inode *inode, bool write)
51648d546a8SJohannes Thumshirn {
51748d546a8SJohannes Thumshirn 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
51848d546a8SJohannes Thumshirn 
51948d546a8SJohannes Thumshirn 	mutex_lock(&zi->i_truncate_mutex);
52048d546a8SJohannes Thumshirn 	__zonefs_io_error(inode, write);
5218dcc1a9dSDamien Le Moal 	mutex_unlock(&zi->i_truncate_mutex);
5228dcc1a9dSDamien Le Moal }
5238dcc1a9dSDamien Le Moal 
5248dcc1a9dSDamien Le Moal static int zonefs_file_truncate(struct inode *inode, loff_t isize)
5258dcc1a9dSDamien Le Moal {
5268dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
5278dcc1a9dSDamien Le Moal 	loff_t old_isize;
5288dcc1a9dSDamien Le Moal 	enum req_opf op;
5298dcc1a9dSDamien Le Moal 	int ret = 0;
5308dcc1a9dSDamien Le Moal 
5318dcc1a9dSDamien Le Moal 	/*
5328dcc1a9dSDamien Le Moal 	 * Only sequential zone files can be truncated and truncation is allowed
5338dcc1a9dSDamien Le Moal 	 * only down to a 0 size, which is equivalent to a zone reset, and to
5348dcc1a9dSDamien Le Moal 	 * the maximum file size, which is equivalent to a zone finish.
5358dcc1a9dSDamien Le Moal 	 */
5368dcc1a9dSDamien Le Moal 	if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
5378dcc1a9dSDamien Le Moal 		return -EPERM;
5388dcc1a9dSDamien Le Moal 
5398dcc1a9dSDamien Le Moal 	if (!isize)
5408dcc1a9dSDamien Le Moal 		op = REQ_OP_ZONE_RESET;
5418dcc1a9dSDamien Le Moal 	else if (isize == zi->i_max_size)
5428dcc1a9dSDamien Le Moal 		op = REQ_OP_ZONE_FINISH;
5438dcc1a9dSDamien Le Moal 	else
5448dcc1a9dSDamien Le Moal 		return -EPERM;
5458dcc1a9dSDamien Le Moal 
5468dcc1a9dSDamien Le Moal 	inode_dio_wait(inode);
5478dcc1a9dSDamien Le Moal 
5488dcc1a9dSDamien Le Moal 	/* Serialize against page faults */
549448f9490SJan Kara 	filemap_invalidate_lock(inode->i_mapping);
5508dcc1a9dSDamien Le Moal 
5518dcc1a9dSDamien Le Moal 	/* Serialize against zonefs_iomap_begin() */
5528dcc1a9dSDamien Le Moal 	mutex_lock(&zi->i_truncate_mutex);
5538dcc1a9dSDamien Le Moal 
5548dcc1a9dSDamien Le Moal 	old_isize = i_size_read(inode);
5558dcc1a9dSDamien Le Moal 	if (isize == old_isize)
5568dcc1a9dSDamien Le Moal 		goto unlock;
5578dcc1a9dSDamien Le Moal 
5585498d5f9SJohannes Thumshirn 	ret = zonefs_zone_mgmt(inode, op);
5595498d5f9SJohannes Thumshirn 	if (ret)
5608dcc1a9dSDamien Le Moal 		goto unlock;
5618dcc1a9dSDamien Le Moal 
562b5c00e97SJohannes Thumshirn 	/*
563b5c00e97SJohannes Thumshirn 	 * If the mount option ZONEFS_MNTOPT_EXPLICIT_OPEN is set,
564b5c00e97SJohannes Thumshirn 	 * take care of open zones.
565b5c00e97SJohannes Thumshirn 	 */
566b5c00e97SJohannes Thumshirn 	if (zi->i_flags & ZONEFS_ZONE_OPEN) {
567b5c00e97SJohannes Thumshirn 		/*
568b5c00e97SJohannes Thumshirn 		 * Truncating a zone to EMPTY or FULL is the equivalent of
569b5c00e97SJohannes Thumshirn 		 * closing the zone. For a truncation to 0, we need to
570b5c00e97SJohannes Thumshirn 		 * re-open the zone to ensure new writes can be processed.
571b5c00e97SJohannes Thumshirn 		 * For a truncation to the maximum file size, the zone is
572b5c00e97SJohannes Thumshirn 		 * closed and writes cannot be accepted anymore, so clear
573b5c00e97SJohannes Thumshirn 		 * the open flag.
574b5c00e97SJohannes Thumshirn 		 */
575b5c00e97SJohannes Thumshirn 		if (!isize)
576b5c00e97SJohannes Thumshirn 			ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_OPEN);
577b5c00e97SJohannes Thumshirn 		else
578b5c00e97SJohannes Thumshirn 			zi->i_flags &= ~ZONEFS_ZONE_OPEN;
579b5c00e97SJohannes Thumshirn 	}
580b5c00e97SJohannes Thumshirn 
5818dcc1a9dSDamien Le Moal 	zonefs_update_stats(inode, isize);
5828dcc1a9dSDamien Le Moal 	truncate_setsize(inode, isize);
5838dcc1a9dSDamien Le Moal 	zi->i_wpoffset = isize;
58487c9ce3fSDamien Le Moal 	zonefs_account_active(inode);
5858dcc1a9dSDamien Le Moal 
5868dcc1a9dSDamien Le Moal unlock:
5878dcc1a9dSDamien Le Moal 	mutex_unlock(&zi->i_truncate_mutex);
588448f9490SJan Kara 	filemap_invalidate_unlock(inode->i_mapping);
5898dcc1a9dSDamien Le Moal 
5908dcc1a9dSDamien Le Moal 	return ret;
5918dcc1a9dSDamien Le Moal }
5928dcc1a9dSDamien Le Moal 
593549c7297SChristian Brauner static int zonefs_inode_setattr(struct user_namespace *mnt_userns,
594549c7297SChristian Brauner 				struct dentry *dentry, struct iattr *iattr)
5958dcc1a9dSDamien Le Moal {
5968dcc1a9dSDamien Le Moal 	struct inode *inode = d_inode(dentry);
5978dcc1a9dSDamien Le Moal 	int ret;
5988dcc1a9dSDamien Le Moal 
5998dcc1a9dSDamien Le Moal 	if (unlikely(IS_IMMUTABLE(inode)))
6008dcc1a9dSDamien Le Moal 		return -EPERM;
6018dcc1a9dSDamien Le Moal 
6022f221d6fSChristian Brauner 	ret = setattr_prepare(&init_user_ns, dentry, iattr);
6038dcc1a9dSDamien Le Moal 	if (ret)
6048dcc1a9dSDamien Le Moal 		return ret;
6058dcc1a9dSDamien Le Moal 
6068dcc1a9dSDamien Le Moal 	/*
6078dcc1a9dSDamien Le Moal 	 * Since files and directories cannot be created nor deleted, do not
6088dcc1a9dSDamien Le Moal 	 * allow setting any write attributes on the sub-directories grouping
6098dcc1a9dSDamien Le Moal 	 * files by zone type.
6108dcc1a9dSDamien Le Moal 	 */
6118dcc1a9dSDamien Le Moal 	if ((iattr->ia_valid & ATTR_MODE) && S_ISDIR(inode->i_mode) &&
6128dcc1a9dSDamien Le Moal 	    (iattr->ia_mode & 0222))
6138dcc1a9dSDamien Le Moal 		return -EPERM;
6148dcc1a9dSDamien Le Moal 
6158dcc1a9dSDamien Le Moal 	if (((iattr->ia_valid & ATTR_UID) &&
6168dcc1a9dSDamien Le Moal 	     !uid_eq(iattr->ia_uid, inode->i_uid)) ||
6178dcc1a9dSDamien Le Moal 	    ((iattr->ia_valid & ATTR_GID) &&
6188dcc1a9dSDamien Le Moal 	     !gid_eq(iattr->ia_gid, inode->i_gid))) {
6198dcc1a9dSDamien Le Moal 		ret = dquot_transfer(inode, iattr);
6208dcc1a9dSDamien Le Moal 		if (ret)
6218dcc1a9dSDamien Le Moal 			return ret;
6228dcc1a9dSDamien Le Moal 	}
6238dcc1a9dSDamien Le Moal 
6248dcc1a9dSDamien Le Moal 	if (iattr->ia_valid & ATTR_SIZE) {
6258dcc1a9dSDamien Le Moal 		ret = zonefs_file_truncate(inode, iattr->ia_size);
6268dcc1a9dSDamien Le Moal 		if (ret)
6278dcc1a9dSDamien Le Moal 			return ret;
6288dcc1a9dSDamien Le Moal 	}
6298dcc1a9dSDamien Le Moal 
6302f221d6fSChristian Brauner 	setattr_copy(&init_user_ns, inode, iattr);
6318dcc1a9dSDamien Le Moal 
6328dcc1a9dSDamien Le Moal 	return 0;
6338dcc1a9dSDamien Le Moal }
6348dcc1a9dSDamien Le Moal 
6358dcc1a9dSDamien Le Moal static const struct inode_operations zonefs_file_inode_operations = {
6368dcc1a9dSDamien Le Moal 	.setattr	= zonefs_inode_setattr,
6378dcc1a9dSDamien Le Moal };
6388dcc1a9dSDamien Le Moal 
6398dcc1a9dSDamien Le Moal static int zonefs_file_fsync(struct file *file, loff_t start, loff_t end,
6408dcc1a9dSDamien Le Moal 			     int datasync)
6418dcc1a9dSDamien Le Moal {
6428dcc1a9dSDamien Le Moal 	struct inode *inode = file_inode(file);
6438dcc1a9dSDamien Le Moal 	int ret = 0;
6448dcc1a9dSDamien Le Moal 
6458dcc1a9dSDamien Le Moal 	if (unlikely(IS_IMMUTABLE(inode)))
6468dcc1a9dSDamien Le Moal 		return -EPERM;
6478dcc1a9dSDamien Le Moal 
6488dcc1a9dSDamien Le Moal 	/*
6498dcc1a9dSDamien Le Moal 	 * Since only direct writes are allowed in sequential files, page cache
6508dcc1a9dSDamien Le Moal 	 * flush is needed only for conventional zone files.
6518dcc1a9dSDamien Le Moal 	 */
6528dcc1a9dSDamien Le Moal 	if (ZONEFS_I(inode)->i_ztype == ZONEFS_ZTYPE_CNV)
6538dcc1a9dSDamien Le Moal 		ret = file_write_and_wait_range(file, start, end);
6548dcc1a9dSDamien Le Moal 	if (!ret)
655c6bf3f0eSChristoph Hellwig 		ret = blkdev_issue_flush(inode->i_sb->s_bdev);
6568dcc1a9dSDamien Le Moal 
6578dcc1a9dSDamien Le Moal 	if (ret)
6588dcc1a9dSDamien Le Moal 		zonefs_io_error(inode, true);
6598dcc1a9dSDamien Le Moal 
6608dcc1a9dSDamien Le Moal 	return ret;
6618dcc1a9dSDamien Le Moal }
6628dcc1a9dSDamien Le Moal 
6638dcc1a9dSDamien Le Moal static vm_fault_t zonefs_filemap_page_mkwrite(struct vm_fault *vmf)
6648dcc1a9dSDamien Le Moal {
6658dcc1a9dSDamien Le Moal 	struct inode *inode = file_inode(vmf->vma->vm_file);
6668dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
6678dcc1a9dSDamien Le Moal 	vm_fault_t ret;
6688dcc1a9dSDamien Le Moal 
6698dcc1a9dSDamien Le Moal 	if (unlikely(IS_IMMUTABLE(inode)))
6708dcc1a9dSDamien Le Moal 		return VM_FAULT_SIGBUS;
6718dcc1a9dSDamien Le Moal 
6728dcc1a9dSDamien Le Moal 	/*
6738dcc1a9dSDamien Le Moal 	 * Sanity check: only conventional zone files can have shared
6748dcc1a9dSDamien Le Moal 	 * writeable mappings.
6758dcc1a9dSDamien Le Moal 	 */
6768dcc1a9dSDamien Le Moal 	if (WARN_ON_ONCE(zi->i_ztype != ZONEFS_ZTYPE_CNV))
6778dcc1a9dSDamien Le Moal 		return VM_FAULT_NOPAGE;
6788dcc1a9dSDamien Le Moal 
6798dcc1a9dSDamien Le Moal 	sb_start_pagefault(inode->i_sb);
6808dcc1a9dSDamien Le Moal 	file_update_time(vmf->vma->vm_file);
6818dcc1a9dSDamien Le Moal 
6828dcc1a9dSDamien Le Moal 	/* Serialize against truncates */
683448f9490SJan Kara 	filemap_invalidate_lock_shared(inode->i_mapping);
684c1c1204cSDamien Le Moal 	ret = iomap_page_mkwrite(vmf, &zonefs_write_iomap_ops);
685448f9490SJan Kara 	filemap_invalidate_unlock_shared(inode->i_mapping);
6868dcc1a9dSDamien Le Moal 
6878dcc1a9dSDamien Le Moal 	sb_end_pagefault(inode->i_sb);
6888dcc1a9dSDamien Le Moal 	return ret;
6898dcc1a9dSDamien Le Moal }
6908dcc1a9dSDamien Le Moal 
6918dcc1a9dSDamien Le Moal static const struct vm_operations_struct zonefs_file_vm_ops = {
692448f9490SJan Kara 	.fault		= filemap_fault,
6938dcc1a9dSDamien Le Moal 	.map_pages	= filemap_map_pages,
6948dcc1a9dSDamien Le Moal 	.page_mkwrite	= zonefs_filemap_page_mkwrite,
6958dcc1a9dSDamien Le Moal };
6968dcc1a9dSDamien Le Moal 
6978dcc1a9dSDamien Le Moal static int zonefs_file_mmap(struct file *file, struct vm_area_struct *vma)
6988dcc1a9dSDamien Le Moal {
6998dcc1a9dSDamien Le Moal 	/*
7008dcc1a9dSDamien Le Moal 	 * Conventional zones accept random writes, so their files can support
7018dcc1a9dSDamien Le Moal 	 * shared writable mappings. For sequential zone files, only read
7028dcc1a9dSDamien Le Moal 	 * mappings are possible since there are no guarantees for write
7038dcc1a9dSDamien Le Moal 	 * ordering between msync() and page cache writeback.
7048dcc1a9dSDamien Le Moal 	 */
7058dcc1a9dSDamien Le Moal 	if (ZONEFS_I(file_inode(file))->i_ztype == ZONEFS_ZTYPE_SEQ &&
7068dcc1a9dSDamien Le Moal 	    (vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
7078dcc1a9dSDamien Le Moal 		return -EINVAL;
7088dcc1a9dSDamien Le Moal 
7098dcc1a9dSDamien Le Moal 	file_accessed(file);
7108dcc1a9dSDamien Le Moal 	vma->vm_ops = &zonefs_file_vm_ops;
7118dcc1a9dSDamien Le Moal 
7128dcc1a9dSDamien Le Moal 	return 0;
7138dcc1a9dSDamien Le Moal }
7148dcc1a9dSDamien Le Moal 
7158dcc1a9dSDamien Le Moal static loff_t zonefs_file_llseek(struct file *file, loff_t offset, int whence)
7168dcc1a9dSDamien Le Moal {
7178dcc1a9dSDamien Le Moal 	loff_t isize = i_size_read(file_inode(file));
7188dcc1a9dSDamien Le Moal 
7198dcc1a9dSDamien Le Moal 	/*
7208dcc1a9dSDamien Le Moal 	 * Seeks are limited to below the zone size for conventional zones
7218dcc1a9dSDamien Le Moal 	 * and below the zone write pointer for sequential zones. In both
7228dcc1a9dSDamien Le Moal 	 * cases, this limit is the inode size.
7238dcc1a9dSDamien Le Moal 	 */
7248dcc1a9dSDamien Le Moal 	return generic_file_llseek_size(file, offset, whence, isize, isize);
7258dcc1a9dSDamien Le Moal }
7268dcc1a9dSDamien Le Moal 
7278dcc1a9dSDamien Le Moal static int zonefs_file_write_dio_end_io(struct kiocb *iocb, ssize_t size,
7288dcc1a9dSDamien Le Moal 					int error, unsigned int flags)
7298dcc1a9dSDamien Le Moal {
7308dcc1a9dSDamien Le Moal 	struct inode *inode = file_inode(iocb->ki_filp);
7318dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
7328dcc1a9dSDamien Le Moal 
7338dcc1a9dSDamien Le Moal 	if (error) {
7348dcc1a9dSDamien Le Moal 		zonefs_io_error(inode, true);
7358dcc1a9dSDamien Le Moal 		return error;
7368dcc1a9dSDamien Le Moal 	}
7378dcc1a9dSDamien Le Moal 
7388dcc1a9dSDamien Le Moal 	if (size && zi->i_ztype != ZONEFS_ZTYPE_CNV) {
7398dcc1a9dSDamien Le Moal 		/*
7408dcc1a9dSDamien Le Moal 		 * Note that we may be seeing completions out of order,
7418dcc1a9dSDamien Le Moal 		 * but that is not a problem since a write completed
7428dcc1a9dSDamien Le Moal 		 * successfully necessarily means that all preceding writes
7438dcc1a9dSDamien Le Moal 		 * were also successful. So we can safely increase the inode
7448dcc1a9dSDamien Le Moal 		 * size to the write end location.
7458dcc1a9dSDamien Le Moal 		 */
7468dcc1a9dSDamien Le Moal 		mutex_lock(&zi->i_truncate_mutex);
7478dcc1a9dSDamien Le Moal 		if (i_size_read(inode) < iocb->ki_pos + size) {
7488dcc1a9dSDamien Le Moal 			zonefs_update_stats(inode, iocb->ki_pos + size);
749b5c00e97SJohannes Thumshirn 			zonefs_i_size_write(inode, iocb->ki_pos + size);
7508dcc1a9dSDamien Le Moal 		}
7518dcc1a9dSDamien Le Moal 		mutex_unlock(&zi->i_truncate_mutex);
7528dcc1a9dSDamien Le Moal 	}
7538dcc1a9dSDamien Le Moal 
7548dcc1a9dSDamien Le Moal 	return 0;
7558dcc1a9dSDamien Le Moal }
7568dcc1a9dSDamien Le Moal 
7578dcc1a9dSDamien Le Moal static const struct iomap_dio_ops zonefs_write_dio_ops = {
7588dcc1a9dSDamien Le Moal 	.end_io			= zonefs_file_write_dio_end_io,
7598dcc1a9dSDamien Le Moal };
7608dcc1a9dSDamien Le Moal 
76102ef12a6SJohannes Thumshirn static ssize_t zonefs_file_dio_append(struct kiocb *iocb, struct iov_iter *from)
76202ef12a6SJohannes Thumshirn {
76302ef12a6SJohannes Thumshirn 	struct inode *inode = file_inode(iocb->ki_filp);
76402ef12a6SJohannes Thumshirn 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
76502ef12a6SJohannes Thumshirn 	struct block_device *bdev = inode->i_sb->s_bdev;
7662aba0d19SChristoph Hellwig 	unsigned int max = bdev_max_zone_append_sectors(bdev);
76702ef12a6SJohannes Thumshirn 	struct bio *bio;
76802ef12a6SJohannes Thumshirn 	ssize_t size;
76902ef12a6SJohannes Thumshirn 	int nr_pages;
77002ef12a6SJohannes Thumshirn 	ssize_t ret;
77102ef12a6SJohannes Thumshirn 
77202ef12a6SJohannes Thumshirn 	max = ALIGN_DOWN(max << SECTOR_SHIFT, inode->i_sb->s_blocksize);
77302ef12a6SJohannes Thumshirn 	iov_iter_truncate(from, max);
77402ef12a6SJohannes Thumshirn 
775a8affc03SChristoph Hellwig 	nr_pages = iov_iter_npages(from, BIO_MAX_VECS);
77689ee7237SJohannes Thumshirn 	if (!nr_pages)
77789ee7237SJohannes Thumshirn 		return 0;
77889ee7237SJohannes Thumshirn 
77907888c66SChristoph Hellwig 	bio = bio_alloc(bdev, nr_pages,
78007888c66SChristoph Hellwig 			REQ_OP_ZONE_APPEND | REQ_SYNC | REQ_IDLE, GFP_NOFS);
78102ef12a6SJohannes Thumshirn 	bio->bi_iter.bi_sector = zi->i_zsector;
78202ef12a6SJohannes Thumshirn 	bio->bi_ioprio = iocb->ki_ioprio;
78302ef12a6SJohannes Thumshirn 	if (iocb->ki_flags & IOCB_DSYNC)
78402ef12a6SJohannes Thumshirn 		bio->bi_opf |= REQ_FUA;
78502ef12a6SJohannes Thumshirn 
78602ef12a6SJohannes Thumshirn 	ret = bio_iov_iter_get_pages(bio, from);
7876bea0225SDamien Le Moal 	if (unlikely(ret))
7886bea0225SDamien Le Moal 		goto out_release;
7896bea0225SDamien Le Moal 
79002ef12a6SJohannes Thumshirn 	size = bio->bi_iter.bi_size;
7916bea0225SDamien Le Moal 	task_io_account_write(size);
79202ef12a6SJohannes Thumshirn 
79302ef12a6SJohannes Thumshirn 	if (iocb->ki_flags & IOCB_HIPRI)
79402ef12a6SJohannes Thumshirn 		bio_set_polled(bio, iocb);
79502ef12a6SJohannes Thumshirn 
79602ef12a6SJohannes Thumshirn 	ret = submit_bio_wait(bio);
79702ef12a6SJohannes Thumshirn 
7986bea0225SDamien Le Moal 	zonefs_file_write_dio_end_io(iocb, size, ret, 0);
79962ab1aadSJohannes Thumshirn 	trace_zonefs_file_dio_append(inode, size, ret);
8006bea0225SDamien Le Moal 
8016bea0225SDamien Le Moal out_release:
8026bea0225SDamien Le Moal 	bio_release_pages(bio, false);
80302ef12a6SJohannes Thumshirn 	bio_put(bio);
80402ef12a6SJohannes Thumshirn 
80502ef12a6SJohannes Thumshirn 	if (ret >= 0) {
80602ef12a6SJohannes Thumshirn 		iocb->ki_pos += size;
80702ef12a6SJohannes Thumshirn 		return size;
80802ef12a6SJohannes Thumshirn 	}
80902ef12a6SJohannes Thumshirn 
81002ef12a6SJohannes Thumshirn 	return ret;
81102ef12a6SJohannes Thumshirn }
81202ef12a6SJohannes Thumshirn 
8138dcc1a9dSDamien Le Moal /*
814ebfd68cdSDamien Le Moal  * Do not exceed the LFS limits nor the file zone size. If pos is under the
815ebfd68cdSDamien Le Moal  * limit it becomes a short access. If it exceeds the limit, return -EFBIG.
816ebfd68cdSDamien Le Moal  */
817ebfd68cdSDamien Le Moal static loff_t zonefs_write_check_limits(struct file *file, loff_t pos,
818ebfd68cdSDamien Le Moal 					loff_t count)
819ebfd68cdSDamien Le Moal {
820ebfd68cdSDamien Le Moal 	struct inode *inode = file_inode(file);
821ebfd68cdSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
822ebfd68cdSDamien Le Moal 	loff_t limit = rlimit(RLIMIT_FSIZE);
823ebfd68cdSDamien Le Moal 	loff_t max_size = zi->i_max_size;
824ebfd68cdSDamien Le Moal 
825ebfd68cdSDamien Le Moal 	if (limit != RLIM_INFINITY) {
826ebfd68cdSDamien Le Moal 		if (pos >= limit) {
827ebfd68cdSDamien Le Moal 			send_sig(SIGXFSZ, current, 0);
828ebfd68cdSDamien Le Moal 			return -EFBIG;
829ebfd68cdSDamien Le Moal 		}
830ebfd68cdSDamien Le Moal 		count = min(count, limit - pos);
831ebfd68cdSDamien Le Moal 	}
832ebfd68cdSDamien Le Moal 
833ebfd68cdSDamien Le Moal 	if (!(file->f_flags & O_LARGEFILE))
834ebfd68cdSDamien Le Moal 		max_size = min_t(loff_t, MAX_NON_LFS, max_size);
835ebfd68cdSDamien Le Moal 
836ebfd68cdSDamien Le Moal 	if (unlikely(pos >= max_size))
837ebfd68cdSDamien Le Moal 		return -EFBIG;
838ebfd68cdSDamien Le Moal 
839ebfd68cdSDamien Le Moal 	return min(count, max_size - pos);
840ebfd68cdSDamien Le Moal }
841ebfd68cdSDamien Le Moal 
842ebfd68cdSDamien Le Moal static ssize_t zonefs_write_checks(struct kiocb *iocb, struct iov_iter *from)
843ebfd68cdSDamien Le Moal {
844ebfd68cdSDamien Le Moal 	struct file *file = iocb->ki_filp;
845ebfd68cdSDamien Le Moal 	struct inode *inode = file_inode(file);
846ebfd68cdSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
847ebfd68cdSDamien Le Moal 	loff_t count;
848ebfd68cdSDamien Le Moal 
849ebfd68cdSDamien Le Moal 	if (IS_SWAPFILE(inode))
850ebfd68cdSDamien Le Moal 		return -ETXTBSY;
851ebfd68cdSDamien Le Moal 
852ebfd68cdSDamien Le Moal 	if (!iov_iter_count(from))
853ebfd68cdSDamien Le Moal 		return 0;
854ebfd68cdSDamien Le Moal 
855ebfd68cdSDamien Le Moal 	if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
856ebfd68cdSDamien Le Moal 		return -EINVAL;
857ebfd68cdSDamien Le Moal 
858ebfd68cdSDamien Le Moal 	if (iocb->ki_flags & IOCB_APPEND) {
859ebfd68cdSDamien Le Moal 		if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
860ebfd68cdSDamien Le Moal 			return -EINVAL;
861ebfd68cdSDamien Le Moal 		mutex_lock(&zi->i_truncate_mutex);
862ebfd68cdSDamien Le Moal 		iocb->ki_pos = zi->i_wpoffset;
863ebfd68cdSDamien Le Moal 		mutex_unlock(&zi->i_truncate_mutex);
864ebfd68cdSDamien Le Moal 	}
865ebfd68cdSDamien Le Moal 
866ebfd68cdSDamien Le Moal 	count = zonefs_write_check_limits(file, iocb->ki_pos,
867ebfd68cdSDamien Le Moal 					  iov_iter_count(from));
868ebfd68cdSDamien Le Moal 	if (count < 0)
869ebfd68cdSDamien Le Moal 		return count;
870ebfd68cdSDamien Le Moal 
871ebfd68cdSDamien Le Moal 	iov_iter_truncate(from, count);
872ebfd68cdSDamien Le Moal 	return iov_iter_count(from);
873ebfd68cdSDamien Le Moal }
874ebfd68cdSDamien Le Moal 
875ebfd68cdSDamien Le Moal /*
8768dcc1a9dSDamien Le Moal  * Handle direct writes. For sequential zone files, this is the only possible
8778dcc1a9dSDamien Le Moal  * write path. For these files, check that the user is issuing writes
8788dcc1a9dSDamien Le Moal  * sequentially from the end of the file. This code assumes that the block layer
8798dcc1a9dSDamien Le Moal  * delivers write requests to the device in sequential order. This is always the
8808dcc1a9dSDamien Le Moal  * case if a block IO scheduler implementing the ELEVATOR_F_ZBD_SEQ_WRITE
8818dcc1a9dSDamien Le Moal  * elevator feature is being used (e.g. mq-deadline). The block layer always
8828dcc1a9dSDamien Le Moal  * automatically select such an elevator for zoned block devices during the
8838dcc1a9dSDamien Le Moal  * device initialization.
8848dcc1a9dSDamien Le Moal  */
8858dcc1a9dSDamien Le Moal static ssize_t zonefs_file_dio_write(struct kiocb *iocb, struct iov_iter *from)
8868dcc1a9dSDamien Le Moal {
8878dcc1a9dSDamien Le Moal 	struct inode *inode = file_inode(iocb->ki_filp);
8888dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
8898dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
89002ef12a6SJohannes Thumshirn 	bool sync = is_sync_kiocb(iocb);
89102ef12a6SJohannes Thumshirn 	bool append = false;
892ebfd68cdSDamien Le Moal 	ssize_t ret, count;
8938dcc1a9dSDamien Le Moal 
8948dcc1a9dSDamien Le Moal 	/*
8957c69eb84SChristoph Hellwig 	 * For async direct IOs to sequential zone files, refuse IOCB_NOWAIT
8968dcc1a9dSDamien Le Moal 	 * as this can cause write reordering (e.g. the first aio gets EAGAIN
8978dcc1a9dSDamien Le Moal 	 * on the inode lock but the second goes through but is now unaligned).
8988dcc1a9dSDamien Le Moal 	 */
89902ef12a6SJohannes Thumshirn 	if (zi->i_ztype == ZONEFS_ZTYPE_SEQ && !sync &&
9007c69eb84SChristoph Hellwig 	    (iocb->ki_flags & IOCB_NOWAIT))
9017c69eb84SChristoph Hellwig 		return -EOPNOTSUPP;
9028dcc1a9dSDamien Le Moal 
9038dcc1a9dSDamien Le Moal 	if (iocb->ki_flags & IOCB_NOWAIT) {
9048dcc1a9dSDamien Le Moal 		if (!inode_trylock(inode))
9058dcc1a9dSDamien Le Moal 			return -EAGAIN;
9068dcc1a9dSDamien Le Moal 	} else {
9078dcc1a9dSDamien Le Moal 		inode_lock(inode);
9088dcc1a9dSDamien Le Moal 	}
9098dcc1a9dSDamien Le Moal 
910ebfd68cdSDamien Le Moal 	count = zonefs_write_checks(iocb, from);
911ebfd68cdSDamien Le Moal 	if (count <= 0) {
912ebfd68cdSDamien Le Moal 		ret = count;
9138dcc1a9dSDamien Le Moal 		goto inode_unlock;
914ebfd68cdSDamien Le Moal 	}
9158dcc1a9dSDamien Le Moal 
9168dcc1a9dSDamien Le Moal 	if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) {
9178dcc1a9dSDamien Le Moal 		ret = -EINVAL;
9188dcc1a9dSDamien Le Moal 		goto inode_unlock;
9198dcc1a9dSDamien Le Moal 	}
9208dcc1a9dSDamien Le Moal 
9218dcc1a9dSDamien Le Moal 	/* Enforce sequential writes (append only) in sequential zones */
92202ef12a6SJohannes Thumshirn 	if (zi->i_ztype == ZONEFS_ZTYPE_SEQ) {
9238dcc1a9dSDamien Le Moal 		mutex_lock(&zi->i_truncate_mutex);
92402ef12a6SJohannes Thumshirn 		if (iocb->ki_pos != zi->i_wpoffset) {
9258dcc1a9dSDamien Le Moal 			mutex_unlock(&zi->i_truncate_mutex);
9268dcc1a9dSDamien Le Moal 			ret = -EINVAL;
9278dcc1a9dSDamien Le Moal 			goto inode_unlock;
9288dcc1a9dSDamien Le Moal 		}
9298dcc1a9dSDamien Le Moal 		mutex_unlock(&zi->i_truncate_mutex);
93002ef12a6SJohannes Thumshirn 		append = sync;
93102ef12a6SJohannes Thumshirn 	}
9328dcc1a9dSDamien Le Moal 
93302ef12a6SJohannes Thumshirn 	if (append)
93402ef12a6SJohannes Thumshirn 		ret = zonefs_file_dio_append(iocb, from);
93502ef12a6SJohannes Thumshirn 	else
936c1c1204cSDamien Le Moal 		ret = iomap_dio_rw(iocb, from, &zonefs_write_iomap_ops,
937786f847fSChristoph Hellwig 				   &zonefs_write_dio_ops, 0, NULL, 0);
9388dcc1a9dSDamien Le Moal 	if (zi->i_ztype == ZONEFS_ZTYPE_SEQ &&
9398dcc1a9dSDamien Le Moal 	    (ret > 0 || ret == -EIOCBQUEUED)) {
9408dcc1a9dSDamien Le Moal 		if (ret > 0)
9418dcc1a9dSDamien Le Moal 			count = ret;
94287c9ce3fSDamien Le Moal 
94387c9ce3fSDamien Le Moal 		/*
94487c9ce3fSDamien Le Moal 		 * Update the zone write pointer offset assuming the write
94587c9ce3fSDamien Le Moal 		 * operation succeeded. If it did not, the error recovery path
94687c9ce3fSDamien Le Moal 		 * will correct it. Also do active seq file accounting.
94787c9ce3fSDamien Le Moal 		 */
9488dcc1a9dSDamien Le Moal 		mutex_lock(&zi->i_truncate_mutex);
9498dcc1a9dSDamien Le Moal 		zi->i_wpoffset += count;
95087c9ce3fSDamien Le Moal 		zonefs_account_active(inode);
9518dcc1a9dSDamien Le Moal 		mutex_unlock(&zi->i_truncate_mutex);
9528dcc1a9dSDamien Le Moal 	}
9538dcc1a9dSDamien Le Moal 
9548dcc1a9dSDamien Le Moal inode_unlock:
9558dcc1a9dSDamien Le Moal 	inode_unlock(inode);
9568dcc1a9dSDamien Le Moal 
9578dcc1a9dSDamien Le Moal 	return ret;
9588dcc1a9dSDamien Le Moal }
9598dcc1a9dSDamien Le Moal 
9608dcc1a9dSDamien Le Moal static ssize_t zonefs_file_buffered_write(struct kiocb *iocb,
9618dcc1a9dSDamien Le Moal 					  struct iov_iter *from)
9628dcc1a9dSDamien Le Moal {
9638dcc1a9dSDamien Le Moal 	struct inode *inode = file_inode(iocb->ki_filp);
9648dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
9658dcc1a9dSDamien Le Moal 	ssize_t ret;
9668dcc1a9dSDamien Le Moal 
9678dcc1a9dSDamien Le Moal 	/*
9688dcc1a9dSDamien Le Moal 	 * Direct IO writes are mandatory for sequential zone files so that the
9698dcc1a9dSDamien Le Moal 	 * write IO issuing order is preserved.
9708dcc1a9dSDamien Le Moal 	 */
9718dcc1a9dSDamien Le Moal 	if (zi->i_ztype != ZONEFS_ZTYPE_CNV)
9728dcc1a9dSDamien Le Moal 		return -EIO;
9738dcc1a9dSDamien Le Moal 
9748dcc1a9dSDamien Le Moal 	if (iocb->ki_flags & IOCB_NOWAIT) {
9758dcc1a9dSDamien Le Moal 		if (!inode_trylock(inode))
9768dcc1a9dSDamien Le Moal 			return -EAGAIN;
9778dcc1a9dSDamien Le Moal 	} else {
9788dcc1a9dSDamien Le Moal 		inode_lock(inode);
9798dcc1a9dSDamien Le Moal 	}
9808dcc1a9dSDamien Le Moal 
981ebfd68cdSDamien Le Moal 	ret = zonefs_write_checks(iocb, from);
9828dcc1a9dSDamien Le Moal 	if (ret <= 0)
9838dcc1a9dSDamien Le Moal 		goto inode_unlock;
9848dcc1a9dSDamien Le Moal 
985c1c1204cSDamien Le Moal 	ret = iomap_file_buffered_write(iocb, from, &zonefs_write_iomap_ops);
9868dcc1a9dSDamien Le Moal 	if (ret > 0)
9878dcc1a9dSDamien Le Moal 		iocb->ki_pos += ret;
9888dcc1a9dSDamien Le Moal 	else if (ret == -EIO)
9898dcc1a9dSDamien Le Moal 		zonefs_io_error(inode, true);
9908dcc1a9dSDamien Le Moal 
9918dcc1a9dSDamien Le Moal inode_unlock:
9928dcc1a9dSDamien Le Moal 	inode_unlock(inode);
9938dcc1a9dSDamien Le Moal 	if (ret > 0)
9948dcc1a9dSDamien Le Moal 		ret = generic_write_sync(iocb, ret);
9958dcc1a9dSDamien Le Moal 
9968dcc1a9dSDamien Le Moal 	return ret;
9978dcc1a9dSDamien Le Moal }
9988dcc1a9dSDamien Le Moal 
9998dcc1a9dSDamien Le Moal static ssize_t zonefs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
10008dcc1a9dSDamien Le Moal {
10018dcc1a9dSDamien Le Moal 	struct inode *inode = file_inode(iocb->ki_filp);
10028dcc1a9dSDamien Le Moal 
10038dcc1a9dSDamien Le Moal 	if (unlikely(IS_IMMUTABLE(inode)))
10048dcc1a9dSDamien Le Moal 		return -EPERM;
10058dcc1a9dSDamien Le Moal 
10068dcc1a9dSDamien Le Moal 	if (sb_rdonly(inode->i_sb))
10078dcc1a9dSDamien Le Moal 		return -EROFS;
10088dcc1a9dSDamien Le Moal 
10098dcc1a9dSDamien Le Moal 	/* Write operations beyond the zone size are not allowed */
10108dcc1a9dSDamien Le Moal 	if (iocb->ki_pos >= ZONEFS_I(inode)->i_max_size)
10118dcc1a9dSDamien Le Moal 		return -EFBIG;
10128dcc1a9dSDamien Le Moal 
101360263d58SChristoph Hellwig 	if (iocb->ki_flags & IOCB_DIRECT) {
101460263d58SChristoph Hellwig 		ssize_t ret = zonefs_file_dio_write(iocb, from);
101560263d58SChristoph Hellwig 		if (ret != -ENOTBLK)
101660263d58SChristoph Hellwig 			return ret;
101760263d58SChristoph Hellwig 	}
10188dcc1a9dSDamien Le Moal 
10198dcc1a9dSDamien Le Moal 	return zonefs_file_buffered_write(iocb, from);
10208dcc1a9dSDamien Le Moal }
10218dcc1a9dSDamien Le Moal 
10228dcc1a9dSDamien Le Moal static int zonefs_file_read_dio_end_io(struct kiocb *iocb, ssize_t size,
10238dcc1a9dSDamien Le Moal 				       int error, unsigned int flags)
10248dcc1a9dSDamien Le Moal {
10258dcc1a9dSDamien Le Moal 	if (error) {
10268dcc1a9dSDamien Le Moal 		zonefs_io_error(file_inode(iocb->ki_filp), false);
10278dcc1a9dSDamien Le Moal 		return error;
10288dcc1a9dSDamien Le Moal 	}
10298dcc1a9dSDamien Le Moal 
10308dcc1a9dSDamien Le Moal 	return 0;
10318dcc1a9dSDamien Le Moal }
10328dcc1a9dSDamien Le Moal 
10338dcc1a9dSDamien Le Moal static const struct iomap_dio_ops zonefs_read_dio_ops = {
10348dcc1a9dSDamien Le Moal 	.end_io			= zonefs_file_read_dio_end_io,
10358dcc1a9dSDamien Le Moal };
10368dcc1a9dSDamien Le Moal 
10378dcc1a9dSDamien Le Moal static ssize_t zonefs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
10388dcc1a9dSDamien Le Moal {
10398dcc1a9dSDamien Le Moal 	struct inode *inode = file_inode(iocb->ki_filp);
10408dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
10418dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
10428dcc1a9dSDamien Le Moal 	loff_t isize;
10438dcc1a9dSDamien Le Moal 	ssize_t ret;
10448dcc1a9dSDamien Le Moal 
10458dcc1a9dSDamien Le Moal 	/* Offline zones cannot be read */
10468dcc1a9dSDamien Le Moal 	if (unlikely(IS_IMMUTABLE(inode) && !(inode->i_mode & 0777)))
10478dcc1a9dSDamien Le Moal 		return -EPERM;
10488dcc1a9dSDamien Le Moal 
10498dcc1a9dSDamien Le Moal 	if (iocb->ki_pos >= zi->i_max_size)
10508dcc1a9dSDamien Le Moal 		return 0;
10518dcc1a9dSDamien Le Moal 
10528dcc1a9dSDamien Le Moal 	if (iocb->ki_flags & IOCB_NOWAIT) {
10538dcc1a9dSDamien Le Moal 		if (!inode_trylock_shared(inode))
10548dcc1a9dSDamien Le Moal 			return -EAGAIN;
10558dcc1a9dSDamien Le Moal 	} else {
10568dcc1a9dSDamien Le Moal 		inode_lock_shared(inode);
10578dcc1a9dSDamien Le Moal 	}
10588dcc1a9dSDamien Le Moal 
10598dcc1a9dSDamien Le Moal 	/* Limit read operations to written data */
10608dcc1a9dSDamien Le Moal 	mutex_lock(&zi->i_truncate_mutex);
10618dcc1a9dSDamien Le Moal 	isize = i_size_read(inode);
10628dcc1a9dSDamien Le Moal 	if (iocb->ki_pos >= isize) {
10638dcc1a9dSDamien Le Moal 		mutex_unlock(&zi->i_truncate_mutex);
10648dcc1a9dSDamien Le Moal 		ret = 0;
10658dcc1a9dSDamien Le Moal 		goto inode_unlock;
10668dcc1a9dSDamien Le Moal 	}
10678dcc1a9dSDamien Le Moal 	iov_iter_truncate(to, isize - iocb->ki_pos);
10688dcc1a9dSDamien Le Moal 	mutex_unlock(&zi->i_truncate_mutex);
10698dcc1a9dSDamien Le Moal 
10708dcc1a9dSDamien Le Moal 	if (iocb->ki_flags & IOCB_DIRECT) {
10718dcc1a9dSDamien Le Moal 		size_t count = iov_iter_count(to);
10728dcc1a9dSDamien Le Moal 
10738dcc1a9dSDamien Le Moal 		if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) {
10748dcc1a9dSDamien Le Moal 			ret = -EINVAL;
10758dcc1a9dSDamien Le Moal 			goto inode_unlock;
10768dcc1a9dSDamien Le Moal 		}
10778dcc1a9dSDamien Le Moal 		file_accessed(iocb->ki_filp);
1078c1c1204cSDamien Le Moal 		ret = iomap_dio_rw(iocb, to, &zonefs_read_iomap_ops,
1079786f847fSChristoph Hellwig 				   &zonefs_read_dio_ops, 0, NULL, 0);
10808dcc1a9dSDamien Le Moal 	} else {
10818dcc1a9dSDamien Le Moal 		ret = generic_file_read_iter(iocb, to);
10828dcc1a9dSDamien Le Moal 		if (ret == -EIO)
10838dcc1a9dSDamien Le Moal 			zonefs_io_error(inode, false);
10848dcc1a9dSDamien Le Moal 	}
10858dcc1a9dSDamien Le Moal 
10868dcc1a9dSDamien Le Moal inode_unlock:
10878dcc1a9dSDamien Le Moal 	inode_unlock_shared(inode);
10888dcc1a9dSDamien Le Moal 
10898dcc1a9dSDamien Le Moal 	return ret;
10908dcc1a9dSDamien Le Moal }
10918dcc1a9dSDamien Le Moal 
10927d6dfbe0SDamien Le Moal /*
10937d6dfbe0SDamien Le Moal  * Write open accounting is done only for sequential files.
10947d6dfbe0SDamien Le Moal  */
10957d6dfbe0SDamien Le Moal static inline bool zonefs_seq_file_need_wro(struct inode *inode,
10967d6dfbe0SDamien Le Moal 					    struct file *file)
1097b5c00e97SJohannes Thumshirn {
1098b5c00e97SJohannes Thumshirn 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
1099b5c00e97SJohannes Thumshirn 
1100b5c00e97SJohannes Thumshirn 	if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
1101b5c00e97SJohannes Thumshirn 		return false;
1102b5c00e97SJohannes Thumshirn 
1103b5c00e97SJohannes Thumshirn 	if (!(file->f_mode & FMODE_WRITE))
1104b5c00e97SJohannes Thumshirn 		return false;
1105b5c00e97SJohannes Thumshirn 
1106b5c00e97SJohannes Thumshirn 	return true;
1107b5c00e97SJohannes Thumshirn }
1108b5c00e97SJohannes Thumshirn 
11097d6dfbe0SDamien Le Moal static int zonefs_seq_file_write_open(struct inode *inode)
1110b5c00e97SJohannes Thumshirn {
1111b5c00e97SJohannes Thumshirn 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
1112b5c00e97SJohannes Thumshirn 	int ret = 0;
1113b5c00e97SJohannes Thumshirn 
1114b5c00e97SJohannes Thumshirn 	mutex_lock(&zi->i_truncate_mutex);
1115b5c00e97SJohannes Thumshirn 
11166980d29cSChao Yu 	if (!zi->i_wr_refcnt) {
11177d6dfbe0SDamien Le Moal 		struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
11182b95a23cSDamien Le Moal 		unsigned int wro = atomic_inc_return(&sbi->s_wro_seq_files);
11192b95a23cSDamien Le Moal 
11207d6dfbe0SDamien Le Moal 		if (sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) {
11217d6dfbe0SDamien Le Moal 
112296eca145SDamien Le Moal 			if (sbi->s_max_wro_seq_files
112396eca145SDamien Le Moal 			    && wro > sbi->s_max_wro_seq_files) {
11242b95a23cSDamien Le Moal 				atomic_dec(&sbi->s_wro_seq_files);
1125b5c00e97SJohannes Thumshirn 				ret = -EBUSY;
1126b5c00e97SJohannes Thumshirn 				goto unlock;
1127b5c00e97SJohannes Thumshirn 			}
1128b5c00e97SJohannes Thumshirn 
1129b5c00e97SJohannes Thumshirn 			if (i_size_read(inode) < zi->i_max_size) {
1130b5c00e97SJohannes Thumshirn 				ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_OPEN);
1131b5c00e97SJohannes Thumshirn 				if (ret) {
11322b95a23cSDamien Le Moal 					atomic_dec(&sbi->s_wro_seq_files);
1133b5c00e97SJohannes Thumshirn 					goto unlock;
1134b5c00e97SJohannes Thumshirn 				}
1135b5c00e97SJohannes Thumshirn 				zi->i_flags |= ZONEFS_ZONE_OPEN;
113687c9ce3fSDamien Le Moal 				zonefs_account_active(inode);
1137b5c00e97SJohannes Thumshirn 			}
1138b5c00e97SJohannes Thumshirn 		}
1139b5c00e97SJohannes Thumshirn 	}
1140b5c00e97SJohannes Thumshirn 
11416980d29cSChao Yu 	zi->i_wr_refcnt++;
11426980d29cSChao Yu 
1143b5c00e97SJohannes Thumshirn unlock:
1144b5c00e97SJohannes Thumshirn 	mutex_unlock(&zi->i_truncate_mutex);
1145b5c00e97SJohannes Thumshirn 
1146b5c00e97SJohannes Thumshirn 	return ret;
1147b5c00e97SJohannes Thumshirn }
1148b5c00e97SJohannes Thumshirn 
1149b5c00e97SJohannes Thumshirn static int zonefs_file_open(struct inode *inode, struct file *file)
1150b5c00e97SJohannes Thumshirn {
1151b5c00e97SJohannes Thumshirn 	int ret;
1152b5c00e97SJohannes Thumshirn 
1153b5c00e97SJohannes Thumshirn 	ret = generic_file_open(inode, file);
1154b5c00e97SJohannes Thumshirn 	if (ret)
1155b5c00e97SJohannes Thumshirn 		return ret;
1156b5c00e97SJohannes Thumshirn 
11577d6dfbe0SDamien Le Moal 	if (zonefs_seq_file_need_wro(inode, file))
11587d6dfbe0SDamien Le Moal 		return zonefs_seq_file_write_open(inode);
1159b5c00e97SJohannes Thumshirn 
1160b5c00e97SJohannes Thumshirn 	return 0;
1161b5c00e97SJohannes Thumshirn }
1162b5c00e97SJohannes Thumshirn 
11637d6dfbe0SDamien Le Moal static void zonefs_seq_file_write_close(struct inode *inode)
1164b5c00e97SJohannes Thumshirn {
1165b5c00e97SJohannes Thumshirn 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
11667d6dfbe0SDamien Le Moal 	struct super_block *sb = inode->i_sb;
11677d6dfbe0SDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1168b5c00e97SJohannes Thumshirn 	int ret = 0;
1169b5c00e97SJohannes Thumshirn 
1170b5c00e97SJohannes Thumshirn 	mutex_lock(&zi->i_truncate_mutex);
11717d6dfbe0SDamien Le Moal 
1172b5c00e97SJohannes Thumshirn 	zi->i_wr_refcnt--;
11737d6dfbe0SDamien Le Moal 	if (zi->i_wr_refcnt)
11747d6dfbe0SDamien Le Moal 		goto unlock;
1175b5c00e97SJohannes Thumshirn 
1176b5c00e97SJohannes Thumshirn 	/*
11777d6dfbe0SDamien Le Moal 	 * The file zone may not be open anymore (e.g. the file was truncated to
11787d6dfbe0SDamien Le Moal 	 * its maximum size or it was fully written). For this case, we only
11797d6dfbe0SDamien Le Moal 	 * need to decrement the write open count.
1180b5c00e97SJohannes Thumshirn 	 */
11817d6dfbe0SDamien Le Moal 	if (zi->i_flags & ZONEFS_ZONE_OPEN) {
1182b5c00e97SJohannes Thumshirn 		ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_CLOSE);
1183b5c00e97SJohannes Thumshirn 		if (ret) {
1184b5c00e97SJohannes Thumshirn 			__zonefs_io_error(inode, false);
1185b5c00e97SJohannes Thumshirn 			/*
1186b5c00e97SJohannes Thumshirn 			 * Leaving zones explicitly open may lead to a state
1187b5c00e97SJohannes Thumshirn 			 * where most zones cannot be written (zone resources
1188b5c00e97SJohannes Thumshirn 			 * exhausted). So take preventive action by remounting
1189b5c00e97SJohannes Thumshirn 			 * read-only.
1190b5c00e97SJohannes Thumshirn 			 */
1191b5c00e97SJohannes Thumshirn 			if (zi->i_flags & ZONEFS_ZONE_OPEN &&
1192b5c00e97SJohannes Thumshirn 			    !(sb->s_flags & SB_RDONLY)) {
11937d6dfbe0SDamien Le Moal 				zonefs_warn(sb,
11947d6dfbe0SDamien Le Moal 					"closing zone at %llu failed %d\n",
11957d6dfbe0SDamien Le Moal 					zi->i_zsector, ret);
11967d6dfbe0SDamien Le Moal 				zonefs_warn(sb,
11977d6dfbe0SDamien Le Moal 					"remounting filesystem read-only\n");
1198b5c00e97SJohannes Thumshirn 				sb->s_flags |= SB_RDONLY;
1199b5c00e97SJohannes Thumshirn 			}
12007d6dfbe0SDamien Le Moal 			goto unlock;
1201b5c00e97SJohannes Thumshirn 		}
12027d6dfbe0SDamien Le Moal 
1203b5c00e97SJohannes Thumshirn 		zi->i_flags &= ~ZONEFS_ZONE_OPEN;
120487c9ce3fSDamien Le Moal 		zonefs_account_active(inode);
1205b5c00e97SJohannes Thumshirn 	}
12067d6dfbe0SDamien Le Moal 
12077d6dfbe0SDamien Le Moal 	atomic_dec(&sbi->s_wro_seq_files);
12087d6dfbe0SDamien Le Moal 
12097d6dfbe0SDamien Le Moal unlock:
1210b5c00e97SJohannes Thumshirn 	mutex_unlock(&zi->i_truncate_mutex);
1211b5c00e97SJohannes Thumshirn }
1212b5c00e97SJohannes Thumshirn 
1213b5c00e97SJohannes Thumshirn static int zonefs_file_release(struct inode *inode, struct file *file)
1214b5c00e97SJohannes Thumshirn {
1215b5c00e97SJohannes Thumshirn 	/*
1216b5c00e97SJohannes Thumshirn 	 * If we explicitly open a zone we must close it again as well, but the
1217b5c00e97SJohannes Thumshirn 	 * zone management operation can fail (either due to an IO error or as
1218b5c00e97SJohannes Thumshirn 	 * the zone has gone offline or read-only). Make sure we don't fail the
1219b5c00e97SJohannes Thumshirn 	 * close(2) for user-space.
1220b5c00e97SJohannes Thumshirn 	 */
12217d6dfbe0SDamien Le Moal 	if (zonefs_seq_file_need_wro(inode, file))
12227d6dfbe0SDamien Le Moal 		zonefs_seq_file_write_close(inode);
1223b5c00e97SJohannes Thumshirn 
1224b5c00e97SJohannes Thumshirn 	return 0;
1225b5c00e97SJohannes Thumshirn }
1226b5c00e97SJohannes Thumshirn 
12278dcc1a9dSDamien Le Moal static const struct file_operations zonefs_file_operations = {
1228b5c00e97SJohannes Thumshirn 	.open		= zonefs_file_open,
1229b5c00e97SJohannes Thumshirn 	.release	= zonefs_file_release,
12308dcc1a9dSDamien Le Moal 	.fsync		= zonefs_file_fsync,
12318dcc1a9dSDamien Le Moal 	.mmap		= zonefs_file_mmap,
12328dcc1a9dSDamien Le Moal 	.llseek		= zonefs_file_llseek,
12338dcc1a9dSDamien Le Moal 	.read_iter	= zonefs_file_read_iter,
12348dcc1a9dSDamien Le Moal 	.write_iter	= zonefs_file_write_iter,
12358dcc1a9dSDamien Le Moal 	.splice_read	= generic_file_splice_read,
12368dcc1a9dSDamien Le Moal 	.splice_write	= iter_file_splice_write,
12373e08773cSChristoph Hellwig 	.iopoll		= iocb_bio_iopoll,
12388dcc1a9dSDamien Le Moal };
12398dcc1a9dSDamien Le Moal 
12408dcc1a9dSDamien Le Moal static struct kmem_cache *zonefs_inode_cachep;
12418dcc1a9dSDamien Le Moal 
12428dcc1a9dSDamien Le Moal static struct inode *zonefs_alloc_inode(struct super_block *sb)
12438dcc1a9dSDamien Le Moal {
12448dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi;
12458dcc1a9dSDamien Le Moal 
1246fd60b288SMuchun Song 	zi = alloc_inode_sb(sb, zonefs_inode_cachep, GFP_KERNEL);
12478dcc1a9dSDamien Le Moal 	if (!zi)
12488dcc1a9dSDamien Le Moal 		return NULL;
12498dcc1a9dSDamien Le Moal 
12508dcc1a9dSDamien Le Moal 	inode_init_once(&zi->i_vnode);
12518dcc1a9dSDamien Le Moal 	mutex_init(&zi->i_truncate_mutex);
1252b5c00e97SJohannes Thumshirn 	zi->i_wr_refcnt = 0;
1253694852eaSDamien Le Moal 	zi->i_flags = 0;
12548dcc1a9dSDamien Le Moal 
12558dcc1a9dSDamien Le Moal 	return &zi->i_vnode;
12568dcc1a9dSDamien Le Moal }
12578dcc1a9dSDamien Le Moal 
12588dcc1a9dSDamien Le Moal static void zonefs_free_inode(struct inode *inode)
12598dcc1a9dSDamien Le Moal {
12608dcc1a9dSDamien Le Moal 	kmem_cache_free(zonefs_inode_cachep, ZONEFS_I(inode));
12618dcc1a9dSDamien Le Moal }
12628dcc1a9dSDamien Le Moal 
12638dcc1a9dSDamien Le Moal /*
12648dcc1a9dSDamien Le Moal  * File system stat.
12658dcc1a9dSDamien Le Moal  */
12668dcc1a9dSDamien Le Moal static int zonefs_statfs(struct dentry *dentry, struct kstatfs *buf)
12678dcc1a9dSDamien Le Moal {
12688dcc1a9dSDamien Le Moal 	struct super_block *sb = dentry->d_sb;
12698dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
12708dcc1a9dSDamien Le Moal 	enum zonefs_ztype t;
12718dcc1a9dSDamien Le Moal 
12728dcc1a9dSDamien Le Moal 	buf->f_type = ZONEFS_MAGIC;
12738dcc1a9dSDamien Le Moal 	buf->f_bsize = sb->s_blocksize;
12748dcc1a9dSDamien Le Moal 	buf->f_namelen = ZONEFS_NAME_MAX;
12758dcc1a9dSDamien Le Moal 
12768dcc1a9dSDamien Le Moal 	spin_lock(&sbi->s_lock);
12778dcc1a9dSDamien Le Moal 
12788dcc1a9dSDamien Le Moal 	buf->f_blocks = sbi->s_blocks;
12798dcc1a9dSDamien Le Moal 	if (WARN_ON(sbi->s_used_blocks > sbi->s_blocks))
12808dcc1a9dSDamien Le Moal 		buf->f_bfree = 0;
12818dcc1a9dSDamien Le Moal 	else
12828dcc1a9dSDamien Le Moal 		buf->f_bfree = buf->f_blocks - sbi->s_used_blocks;
12838dcc1a9dSDamien Le Moal 	buf->f_bavail = buf->f_bfree;
12848dcc1a9dSDamien Le Moal 
12858dcc1a9dSDamien Le Moal 	for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) {
12868dcc1a9dSDamien Le Moal 		if (sbi->s_nr_files[t])
12878dcc1a9dSDamien Le Moal 			buf->f_files += sbi->s_nr_files[t] + 1;
12888dcc1a9dSDamien Le Moal 	}
12898dcc1a9dSDamien Le Moal 	buf->f_ffree = 0;
12908dcc1a9dSDamien Le Moal 
12918dcc1a9dSDamien Le Moal 	spin_unlock(&sbi->s_lock);
12928dcc1a9dSDamien Le Moal 
12939591c3a3SAmir Goldstein 	buf->f_fsid = uuid_to_fsid(sbi->s_uuid.b);
12948dcc1a9dSDamien Le Moal 
12958dcc1a9dSDamien Le Moal 	return 0;
12968dcc1a9dSDamien Le Moal }
12978dcc1a9dSDamien Le Moal 
12988dcc1a9dSDamien Le Moal enum {
12998dcc1a9dSDamien Le Moal 	Opt_errors_ro, Opt_errors_zro, Opt_errors_zol, Opt_errors_repair,
1300b5c00e97SJohannes Thumshirn 	Opt_explicit_open, Opt_err,
13018dcc1a9dSDamien Le Moal };
13028dcc1a9dSDamien Le Moal 
13038dcc1a9dSDamien Le Moal static const match_table_t tokens = {
13048dcc1a9dSDamien Le Moal 	{ Opt_errors_ro,	"errors=remount-ro"},
13058dcc1a9dSDamien Le Moal 	{ Opt_errors_zro,	"errors=zone-ro"},
13068dcc1a9dSDamien Le Moal 	{ Opt_errors_zol,	"errors=zone-offline"},
13078dcc1a9dSDamien Le Moal 	{ Opt_errors_repair,	"errors=repair"},
1308b5c00e97SJohannes Thumshirn 	{ Opt_explicit_open,	"explicit-open" },
13098dcc1a9dSDamien Le Moal 	{ Opt_err,		NULL}
13108dcc1a9dSDamien Le Moal };
13118dcc1a9dSDamien Le Moal 
13128dcc1a9dSDamien Le Moal static int zonefs_parse_options(struct super_block *sb, char *options)
13138dcc1a9dSDamien Le Moal {
13148dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
13158dcc1a9dSDamien Le Moal 	substring_t args[MAX_OPT_ARGS];
13168dcc1a9dSDamien Le Moal 	char *p;
13178dcc1a9dSDamien Le Moal 
13188dcc1a9dSDamien Le Moal 	if (!options)
13198dcc1a9dSDamien Le Moal 		return 0;
13208dcc1a9dSDamien Le Moal 
13218dcc1a9dSDamien Le Moal 	while ((p = strsep(&options, ",")) != NULL) {
13228dcc1a9dSDamien Le Moal 		int token;
13238dcc1a9dSDamien Le Moal 
13248dcc1a9dSDamien Le Moal 		if (!*p)
13258dcc1a9dSDamien Le Moal 			continue;
13268dcc1a9dSDamien Le Moal 
13278dcc1a9dSDamien Le Moal 		token = match_token(p, tokens, args);
13288dcc1a9dSDamien Le Moal 		switch (token) {
13298dcc1a9dSDamien Le Moal 		case Opt_errors_ro:
13308dcc1a9dSDamien Le Moal 			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
13318dcc1a9dSDamien Le Moal 			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_RO;
13328dcc1a9dSDamien Le Moal 			break;
13338dcc1a9dSDamien Le Moal 		case Opt_errors_zro:
13348dcc1a9dSDamien Le Moal 			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
13358dcc1a9dSDamien Le Moal 			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZRO;
13368dcc1a9dSDamien Le Moal 			break;
13378dcc1a9dSDamien Le Moal 		case Opt_errors_zol:
13388dcc1a9dSDamien Le Moal 			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
13398dcc1a9dSDamien Le Moal 			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZOL;
13408dcc1a9dSDamien Le Moal 			break;
13418dcc1a9dSDamien Le Moal 		case Opt_errors_repair:
13428dcc1a9dSDamien Le Moal 			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
13438dcc1a9dSDamien Le Moal 			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_REPAIR;
13448dcc1a9dSDamien Le Moal 			break;
1345b5c00e97SJohannes Thumshirn 		case Opt_explicit_open:
1346b5c00e97SJohannes Thumshirn 			sbi->s_mount_opts |= ZONEFS_MNTOPT_EXPLICIT_OPEN;
1347b5c00e97SJohannes Thumshirn 			break;
13488dcc1a9dSDamien Le Moal 		default:
13498dcc1a9dSDamien Le Moal 			return -EINVAL;
13508dcc1a9dSDamien Le Moal 		}
13518dcc1a9dSDamien Le Moal 	}
13528dcc1a9dSDamien Le Moal 
13538dcc1a9dSDamien Le Moal 	return 0;
13548dcc1a9dSDamien Le Moal }
13558dcc1a9dSDamien Le Moal 
13568dcc1a9dSDamien Le Moal static int zonefs_show_options(struct seq_file *seq, struct dentry *root)
13578dcc1a9dSDamien Le Moal {
13588dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(root->d_sb);
13598dcc1a9dSDamien Le Moal 
13608dcc1a9dSDamien Le Moal 	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO)
13618dcc1a9dSDamien Le Moal 		seq_puts(seq, ",errors=remount-ro");
13628dcc1a9dSDamien Le Moal 	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO)
13638dcc1a9dSDamien Le Moal 		seq_puts(seq, ",errors=zone-ro");
13648dcc1a9dSDamien Le Moal 	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL)
13658dcc1a9dSDamien Le Moal 		seq_puts(seq, ",errors=zone-offline");
13668dcc1a9dSDamien Le Moal 	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_REPAIR)
13678dcc1a9dSDamien Le Moal 		seq_puts(seq, ",errors=repair");
13688dcc1a9dSDamien Le Moal 
13698dcc1a9dSDamien Le Moal 	return 0;
13708dcc1a9dSDamien Le Moal }
13718dcc1a9dSDamien Le Moal 
13728dcc1a9dSDamien Le Moal static int zonefs_remount(struct super_block *sb, int *flags, char *data)
13738dcc1a9dSDamien Le Moal {
13748dcc1a9dSDamien Le Moal 	sync_filesystem(sb);
13758dcc1a9dSDamien Le Moal 
13768dcc1a9dSDamien Le Moal 	return zonefs_parse_options(sb, data);
13778dcc1a9dSDamien Le Moal }
13788dcc1a9dSDamien Le Moal 
13798dcc1a9dSDamien Le Moal static const struct super_operations zonefs_sops = {
13808dcc1a9dSDamien Le Moal 	.alloc_inode	= zonefs_alloc_inode,
13818dcc1a9dSDamien Le Moal 	.free_inode	= zonefs_free_inode,
13828dcc1a9dSDamien Le Moal 	.statfs		= zonefs_statfs,
13838dcc1a9dSDamien Le Moal 	.remount_fs	= zonefs_remount,
13848dcc1a9dSDamien Le Moal 	.show_options	= zonefs_show_options,
13858dcc1a9dSDamien Le Moal };
13868dcc1a9dSDamien Le Moal 
13878dcc1a9dSDamien Le Moal static const struct inode_operations zonefs_dir_inode_operations = {
13888dcc1a9dSDamien Le Moal 	.lookup		= simple_lookup,
13898dcc1a9dSDamien Le Moal 	.setattr	= zonefs_inode_setattr,
13908dcc1a9dSDamien Le Moal };
13918dcc1a9dSDamien Le Moal 
13928dcc1a9dSDamien Le Moal static void zonefs_init_dir_inode(struct inode *parent, struct inode *inode,
13938dcc1a9dSDamien Le Moal 				  enum zonefs_ztype type)
13948dcc1a9dSDamien Le Moal {
13958dcc1a9dSDamien Le Moal 	struct super_block *sb = parent->i_sb;
13968dcc1a9dSDamien Le Moal 
1397*b623e347SChristoph Hellwig 	inode->i_ino = bdev_nr_zones(sb->s_bdev) + type + 1;
139821cb47beSChristian Brauner 	inode_init_owner(&init_user_ns, inode, parent, S_IFDIR | 0555);
13998dcc1a9dSDamien Le Moal 	inode->i_op = &zonefs_dir_inode_operations;
14008dcc1a9dSDamien Le Moal 	inode->i_fop = &simple_dir_operations;
14018dcc1a9dSDamien Le Moal 	set_nlink(inode, 2);
14028dcc1a9dSDamien Le Moal 	inc_nlink(parent);
14038dcc1a9dSDamien Le Moal }
14048dcc1a9dSDamien Le Moal 
14051da18a29SDamien Le Moal static int zonefs_init_file_inode(struct inode *inode, struct blk_zone *zone,
14068dcc1a9dSDamien Le Moal 				  enum zonefs_ztype type)
14078dcc1a9dSDamien Le Moal {
14088dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
14098dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
14108dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
141114bdb047SDamien Le Moal 	int ret = 0;
14128dcc1a9dSDamien Le Moal 
14138dcc1a9dSDamien Le Moal 	inode->i_ino = zone->start >> sbi->s_zone_sectors_shift;
14148dcc1a9dSDamien Le Moal 	inode->i_mode = S_IFREG | sbi->s_perm;
14158dcc1a9dSDamien Le Moal 
14168dcc1a9dSDamien Le Moal 	zi->i_ztype = type;
14178dcc1a9dSDamien Le Moal 	zi->i_zsector = zone->start;
1418e3c3155bSJohannes Thumshirn 	zi->i_zone_size = zone->len << SECTOR_SHIFT;
1419e3c3155bSJohannes Thumshirn 
14208dcc1a9dSDamien Le Moal 	zi->i_max_size = min_t(loff_t, MAX_LFS_FILESIZE,
1421e3c3155bSJohannes Thumshirn 			       zone->capacity << SECTOR_SHIFT);
1422ccf4ad7dSDamien Le Moal 	zi->i_wpoffset = zonefs_check_zone_condition(inode, zone, true, true);
14238dcc1a9dSDamien Le Moal 
14248dcc1a9dSDamien Le Moal 	inode->i_uid = sbi->s_uid;
14258dcc1a9dSDamien Le Moal 	inode->i_gid = sbi->s_gid;
14268dcc1a9dSDamien Le Moal 	inode->i_size = zi->i_wpoffset;
1427e3c3155bSJohannes Thumshirn 	inode->i_blocks = zi->i_max_size >> SECTOR_SHIFT;
14288dcc1a9dSDamien Le Moal 
14298dcc1a9dSDamien Le Moal 	inode->i_op = &zonefs_file_inode_operations;
14308dcc1a9dSDamien Le Moal 	inode->i_fop = &zonefs_file_operations;
14318dcc1a9dSDamien Le Moal 	inode->i_mapping->a_ops = &zonefs_file_aops;
14328dcc1a9dSDamien Le Moal 
14338dcc1a9dSDamien Le Moal 	sb->s_maxbytes = max(zi->i_max_size, sb->s_maxbytes);
14348dcc1a9dSDamien Le Moal 	sbi->s_blocks += zi->i_max_size >> sb->s_blocksize_bits;
14358dcc1a9dSDamien Le Moal 	sbi->s_used_blocks += zi->i_wpoffset >> sb->s_blocksize_bits;
14361da18a29SDamien Le Moal 
143787c9ce3fSDamien Le Moal 	mutex_lock(&zi->i_truncate_mutex);
143887c9ce3fSDamien Le Moal 
14391da18a29SDamien Le Moal 	/*
14401da18a29SDamien Le Moal 	 * For sequential zones, make sure that any open zone is closed first
14411da18a29SDamien Le Moal 	 * to ensure that the initial number of open zones is 0, in sync with
14421da18a29SDamien Le Moal 	 * the open zone accounting done when the mount option
14431da18a29SDamien Le Moal 	 * ZONEFS_MNTOPT_EXPLICIT_OPEN is used.
14441da18a29SDamien Le Moal 	 */
14451da18a29SDamien Le Moal 	if (type == ZONEFS_ZTYPE_SEQ &&
14461da18a29SDamien Le Moal 	    (zone->cond == BLK_ZONE_COND_IMP_OPEN ||
14471da18a29SDamien Le Moal 	     zone->cond == BLK_ZONE_COND_EXP_OPEN)) {
14481da18a29SDamien Le Moal 		ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_CLOSE);
144987c9ce3fSDamien Le Moal 		if (ret)
145087c9ce3fSDamien Le Moal 			goto unlock;
14511da18a29SDamien Le Moal 	}
14521da18a29SDamien Le Moal 
145387c9ce3fSDamien Le Moal 	zonefs_account_active(inode);
145487c9ce3fSDamien Le Moal 
145587c9ce3fSDamien Le Moal unlock:
145687c9ce3fSDamien Le Moal 	mutex_unlock(&zi->i_truncate_mutex);
145787c9ce3fSDamien Le Moal 
145814bdb047SDamien Le Moal 	return ret;
14598dcc1a9dSDamien Le Moal }
14608dcc1a9dSDamien Le Moal 
14618dcc1a9dSDamien Le Moal static struct dentry *zonefs_create_inode(struct dentry *parent,
14628dcc1a9dSDamien Le Moal 					const char *name, struct blk_zone *zone,
14638dcc1a9dSDamien Le Moal 					enum zonefs_ztype type)
14648dcc1a9dSDamien Le Moal {
14658dcc1a9dSDamien Le Moal 	struct inode *dir = d_inode(parent);
14668dcc1a9dSDamien Le Moal 	struct dentry *dentry;
14678dcc1a9dSDamien Le Moal 	struct inode *inode;
14681da18a29SDamien Le Moal 	int ret;
14698dcc1a9dSDamien Le Moal 
14708dcc1a9dSDamien Le Moal 	dentry = d_alloc_name(parent, name);
14718dcc1a9dSDamien Le Moal 	if (!dentry)
14728dcc1a9dSDamien Le Moal 		return NULL;
14738dcc1a9dSDamien Le Moal 
14748dcc1a9dSDamien Le Moal 	inode = new_inode(parent->d_sb);
14758dcc1a9dSDamien Le Moal 	if (!inode)
14768dcc1a9dSDamien Le Moal 		goto dput;
14778dcc1a9dSDamien Le Moal 
14788dcc1a9dSDamien Le Moal 	inode->i_ctime = inode->i_mtime = inode->i_atime = dir->i_ctime;
14791da18a29SDamien Le Moal 	if (zone) {
14801da18a29SDamien Le Moal 		ret = zonefs_init_file_inode(inode, zone, type);
14811da18a29SDamien Le Moal 		if (ret) {
14821da18a29SDamien Le Moal 			iput(inode);
14831da18a29SDamien Le Moal 			goto dput;
14841da18a29SDamien Le Moal 		}
14851da18a29SDamien Le Moal 	} else {
14868dcc1a9dSDamien Le Moal 		zonefs_init_dir_inode(dir, inode, type);
14871da18a29SDamien Le Moal 	}
14881da18a29SDamien Le Moal 
14898dcc1a9dSDamien Le Moal 	d_add(dentry, inode);
14908dcc1a9dSDamien Le Moal 	dir->i_size++;
14918dcc1a9dSDamien Le Moal 
14928dcc1a9dSDamien Le Moal 	return dentry;
14938dcc1a9dSDamien Le Moal 
14948dcc1a9dSDamien Le Moal dput:
14958dcc1a9dSDamien Le Moal 	dput(dentry);
14968dcc1a9dSDamien Le Moal 
14978dcc1a9dSDamien Le Moal 	return NULL;
14988dcc1a9dSDamien Le Moal }
14998dcc1a9dSDamien Le Moal 
15008dcc1a9dSDamien Le Moal struct zonefs_zone_data {
15018dcc1a9dSDamien Le Moal 	struct super_block	*sb;
15028dcc1a9dSDamien Le Moal 	unsigned int		nr_zones[ZONEFS_ZTYPE_MAX];
15038dcc1a9dSDamien Le Moal 	struct blk_zone		*zones;
15048dcc1a9dSDamien Le Moal };
15058dcc1a9dSDamien Le Moal 
15068dcc1a9dSDamien Le Moal /*
15078dcc1a9dSDamien Le Moal  * Create a zone group and populate it with zone files.
15088dcc1a9dSDamien Le Moal  */
15098dcc1a9dSDamien Le Moal static int zonefs_create_zgroup(struct zonefs_zone_data *zd,
15108dcc1a9dSDamien Le Moal 				enum zonefs_ztype type)
15118dcc1a9dSDamien Le Moal {
15128dcc1a9dSDamien Le Moal 	struct super_block *sb = zd->sb;
15138dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
15148dcc1a9dSDamien Le Moal 	struct blk_zone *zone, *next, *end;
15158dcc1a9dSDamien Le Moal 	const char *zgroup_name;
15168dcc1a9dSDamien Le Moal 	char *file_name;
15178dcc1a9dSDamien Le Moal 	struct dentry *dir;
15188dcc1a9dSDamien Le Moal 	unsigned int n = 0;
151901b2651cSDamien Le Moal 	int ret;
15208dcc1a9dSDamien Le Moal 
15218dcc1a9dSDamien Le Moal 	/* If the group is empty, there is nothing to do */
15228dcc1a9dSDamien Le Moal 	if (!zd->nr_zones[type])
15238dcc1a9dSDamien Le Moal 		return 0;
15248dcc1a9dSDamien Le Moal 
15258dcc1a9dSDamien Le Moal 	file_name = kmalloc(ZONEFS_NAME_MAX, GFP_KERNEL);
15268dcc1a9dSDamien Le Moal 	if (!file_name)
15278dcc1a9dSDamien Le Moal 		return -ENOMEM;
15288dcc1a9dSDamien Le Moal 
15298dcc1a9dSDamien Le Moal 	if (type == ZONEFS_ZTYPE_CNV)
15308dcc1a9dSDamien Le Moal 		zgroup_name = "cnv";
15318dcc1a9dSDamien Le Moal 	else
15328dcc1a9dSDamien Le Moal 		zgroup_name = "seq";
15338dcc1a9dSDamien Le Moal 
15348dcc1a9dSDamien Le Moal 	dir = zonefs_create_inode(sb->s_root, zgroup_name, NULL, type);
153501b2651cSDamien Le Moal 	if (!dir) {
153601b2651cSDamien Le Moal 		ret = -ENOMEM;
15378dcc1a9dSDamien Le Moal 		goto free;
153801b2651cSDamien Le Moal 	}
15398dcc1a9dSDamien Le Moal 
15408dcc1a9dSDamien Le Moal 	/*
15418dcc1a9dSDamien Le Moal 	 * The first zone contains the super block: skip it.
15428dcc1a9dSDamien Le Moal 	 */
1543*b623e347SChristoph Hellwig 	end = zd->zones + bdev_nr_zones(sb->s_bdev);
15448dcc1a9dSDamien Le Moal 	for (zone = &zd->zones[1]; zone < end; zone = next) {
15458dcc1a9dSDamien Le Moal 
15468dcc1a9dSDamien Le Moal 		next = zone + 1;
15478dcc1a9dSDamien Le Moal 		if (zonefs_zone_type(zone) != type)
15488dcc1a9dSDamien Le Moal 			continue;
15498dcc1a9dSDamien Le Moal 
15508dcc1a9dSDamien Le Moal 		/*
15518dcc1a9dSDamien Le Moal 		 * For conventional zones, contiguous zones can be aggregated
15528dcc1a9dSDamien Le Moal 		 * together to form larger files. Note that this overwrites the
15538dcc1a9dSDamien Le Moal 		 * length of the first zone of the set of contiguous zones
15548dcc1a9dSDamien Le Moal 		 * aggregated together. If one offline or read-only zone is
15558dcc1a9dSDamien Le Moal 		 * found, assume that all zones aggregated have the same
15568dcc1a9dSDamien Le Moal 		 * condition.
15578dcc1a9dSDamien Le Moal 		 */
15588dcc1a9dSDamien Le Moal 		if (type == ZONEFS_ZTYPE_CNV &&
15598dcc1a9dSDamien Le Moal 		    (sbi->s_features & ZONEFS_F_AGGRCNV)) {
15608dcc1a9dSDamien Le Moal 			for (; next < end; next++) {
15618dcc1a9dSDamien Le Moal 				if (zonefs_zone_type(next) != type)
15628dcc1a9dSDamien Le Moal 					break;
15638dcc1a9dSDamien Le Moal 				zone->len += next->len;
1564e3c3155bSJohannes Thumshirn 				zone->capacity += next->capacity;
15658dcc1a9dSDamien Le Moal 				if (next->cond == BLK_ZONE_COND_READONLY &&
15668dcc1a9dSDamien Le Moal 				    zone->cond != BLK_ZONE_COND_OFFLINE)
15678dcc1a9dSDamien Le Moal 					zone->cond = BLK_ZONE_COND_READONLY;
15688dcc1a9dSDamien Le Moal 				else if (next->cond == BLK_ZONE_COND_OFFLINE)
15698dcc1a9dSDamien Le Moal 					zone->cond = BLK_ZONE_COND_OFFLINE;
15708dcc1a9dSDamien Le Moal 			}
1571e3c3155bSJohannes Thumshirn 			if (zone->capacity != zone->len) {
1572e3c3155bSJohannes Thumshirn 				zonefs_err(sb, "Invalid conventional zone capacity\n");
1573e3c3155bSJohannes Thumshirn 				ret = -EINVAL;
1574e3c3155bSJohannes Thumshirn 				goto free;
1575e3c3155bSJohannes Thumshirn 			}
15768dcc1a9dSDamien Le Moal 		}
15778dcc1a9dSDamien Le Moal 
15788dcc1a9dSDamien Le Moal 		/*
15798dcc1a9dSDamien Le Moal 		 * Use the file number within its group as file name.
15808dcc1a9dSDamien Le Moal 		 */
15818dcc1a9dSDamien Le Moal 		snprintf(file_name, ZONEFS_NAME_MAX - 1, "%u", n);
158201b2651cSDamien Le Moal 		if (!zonefs_create_inode(dir, file_name, zone, type)) {
158301b2651cSDamien Le Moal 			ret = -ENOMEM;
15848dcc1a9dSDamien Le Moal 			goto free;
158501b2651cSDamien Le Moal 		}
15868dcc1a9dSDamien Le Moal 
15878dcc1a9dSDamien Le Moal 		n++;
15888dcc1a9dSDamien Le Moal 	}
15898dcc1a9dSDamien Le Moal 
15908dcc1a9dSDamien Le Moal 	zonefs_info(sb, "Zone group \"%s\" has %u file%s\n",
15918dcc1a9dSDamien Le Moal 		    zgroup_name, n, n > 1 ? "s" : "");
15928dcc1a9dSDamien Le Moal 
15938dcc1a9dSDamien Le Moal 	sbi->s_nr_files[type] = n;
15948dcc1a9dSDamien Le Moal 	ret = 0;
15958dcc1a9dSDamien Le Moal 
15968dcc1a9dSDamien Le Moal free:
15978dcc1a9dSDamien Le Moal 	kfree(file_name);
15988dcc1a9dSDamien Le Moal 
15998dcc1a9dSDamien Le Moal 	return ret;
16008dcc1a9dSDamien Le Moal }
16018dcc1a9dSDamien Le Moal 
16028dcc1a9dSDamien Le Moal static int zonefs_get_zone_info_cb(struct blk_zone *zone, unsigned int idx,
16038dcc1a9dSDamien Le Moal 				   void *data)
16048dcc1a9dSDamien Le Moal {
16058dcc1a9dSDamien Le Moal 	struct zonefs_zone_data *zd = data;
16068dcc1a9dSDamien Le Moal 
16078dcc1a9dSDamien Le Moal 	/*
16088dcc1a9dSDamien Le Moal 	 * Count the number of usable zones: the first zone at index 0 contains
16098dcc1a9dSDamien Le Moal 	 * the super block and is ignored.
16108dcc1a9dSDamien Le Moal 	 */
16118dcc1a9dSDamien Le Moal 	switch (zone->type) {
16128dcc1a9dSDamien Le Moal 	case BLK_ZONE_TYPE_CONVENTIONAL:
16138dcc1a9dSDamien Le Moal 		zone->wp = zone->start + zone->len;
16148dcc1a9dSDamien Le Moal 		if (idx)
16158dcc1a9dSDamien Le Moal 			zd->nr_zones[ZONEFS_ZTYPE_CNV]++;
16168dcc1a9dSDamien Le Moal 		break;
16178dcc1a9dSDamien Le Moal 	case BLK_ZONE_TYPE_SEQWRITE_REQ:
16188dcc1a9dSDamien Le Moal 	case BLK_ZONE_TYPE_SEQWRITE_PREF:
16198dcc1a9dSDamien Le Moal 		if (idx)
16208dcc1a9dSDamien Le Moal 			zd->nr_zones[ZONEFS_ZTYPE_SEQ]++;
16218dcc1a9dSDamien Le Moal 		break;
16228dcc1a9dSDamien Le Moal 	default:
16238dcc1a9dSDamien Le Moal 		zonefs_err(zd->sb, "Unsupported zone type 0x%x\n",
16248dcc1a9dSDamien Le Moal 			   zone->type);
16258dcc1a9dSDamien Le Moal 		return -EIO;
16268dcc1a9dSDamien Le Moal 	}
16278dcc1a9dSDamien Le Moal 
16288dcc1a9dSDamien Le Moal 	memcpy(&zd->zones[idx], zone, sizeof(struct blk_zone));
16298dcc1a9dSDamien Le Moal 
16308dcc1a9dSDamien Le Moal 	return 0;
16318dcc1a9dSDamien Le Moal }
16328dcc1a9dSDamien Le Moal 
16338dcc1a9dSDamien Le Moal static int zonefs_get_zone_info(struct zonefs_zone_data *zd)
16348dcc1a9dSDamien Le Moal {
16358dcc1a9dSDamien Le Moal 	struct block_device *bdev = zd->sb->s_bdev;
16368dcc1a9dSDamien Le Moal 	int ret;
16378dcc1a9dSDamien Le Moal 
1638*b623e347SChristoph Hellwig 	zd->zones = kvcalloc(bdev_nr_zones(bdev), sizeof(struct blk_zone),
1639*b623e347SChristoph Hellwig 			     GFP_KERNEL);
16408dcc1a9dSDamien Le Moal 	if (!zd->zones)
16418dcc1a9dSDamien Le Moal 		return -ENOMEM;
16428dcc1a9dSDamien Le Moal 
16438dcc1a9dSDamien Le Moal 	/* Get zones information from the device */
16448dcc1a9dSDamien Le Moal 	ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES,
16458dcc1a9dSDamien Le Moal 				  zonefs_get_zone_info_cb, zd);
16468dcc1a9dSDamien Le Moal 	if (ret < 0) {
16478dcc1a9dSDamien Le Moal 		zonefs_err(zd->sb, "Zone report failed %d\n", ret);
16488dcc1a9dSDamien Le Moal 		return ret;
16498dcc1a9dSDamien Le Moal 	}
16508dcc1a9dSDamien Le Moal 
1651*b623e347SChristoph Hellwig 	if (ret != bdev_nr_zones(bdev)) {
16528dcc1a9dSDamien Le Moal 		zonefs_err(zd->sb, "Invalid zone report (%d/%u zones)\n",
1653*b623e347SChristoph Hellwig 			   ret, bdev_nr_zones(bdev));
16548dcc1a9dSDamien Le Moal 		return -EIO;
16558dcc1a9dSDamien Le Moal 	}
16568dcc1a9dSDamien Le Moal 
16578dcc1a9dSDamien Le Moal 	return 0;
16588dcc1a9dSDamien Le Moal }
16598dcc1a9dSDamien Le Moal 
16608dcc1a9dSDamien Le Moal static inline void zonefs_cleanup_zone_info(struct zonefs_zone_data *zd)
16618dcc1a9dSDamien Le Moal {
16628dcc1a9dSDamien Le Moal 	kvfree(zd->zones);
16638dcc1a9dSDamien Le Moal }
16648dcc1a9dSDamien Le Moal 
16658dcc1a9dSDamien Le Moal /*
16668dcc1a9dSDamien Le Moal  * Read super block information from the device.
16678dcc1a9dSDamien Le Moal  */
16688dcc1a9dSDamien Le Moal static int zonefs_read_super(struct super_block *sb)
16698dcc1a9dSDamien Le Moal {
16708dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
16718dcc1a9dSDamien Le Moal 	struct zonefs_super *super;
16728dcc1a9dSDamien Le Moal 	u32 crc, stored_crc;
16738dcc1a9dSDamien Le Moal 	struct page *page;
16748dcc1a9dSDamien Le Moal 	struct bio_vec bio_vec;
16758dcc1a9dSDamien Le Moal 	struct bio bio;
16768dcc1a9dSDamien Le Moal 	int ret;
16778dcc1a9dSDamien Le Moal 
16788dcc1a9dSDamien Le Moal 	page = alloc_page(GFP_KERNEL);
16798dcc1a9dSDamien Le Moal 	if (!page)
16808dcc1a9dSDamien Le Moal 		return -ENOMEM;
16818dcc1a9dSDamien Le Moal 
168249add496SChristoph Hellwig 	bio_init(&bio, sb->s_bdev, &bio_vec, 1, REQ_OP_READ);
16838dcc1a9dSDamien Le Moal 	bio.bi_iter.bi_sector = 0;
16848dcc1a9dSDamien Le Moal 	bio_add_page(&bio, page, PAGE_SIZE, 0);
16858dcc1a9dSDamien Le Moal 
16868dcc1a9dSDamien Le Moal 	ret = submit_bio_wait(&bio);
16878dcc1a9dSDamien Le Moal 	if (ret)
16888dcc1a9dSDamien Le Moal 		goto free_page;
16898dcc1a9dSDamien Le Moal 
16908dcc1a9dSDamien Le Moal 	super = kmap(page);
16918dcc1a9dSDamien Le Moal 
16928dcc1a9dSDamien Le Moal 	ret = -EINVAL;
16938dcc1a9dSDamien Le Moal 	if (le32_to_cpu(super->s_magic) != ZONEFS_MAGIC)
16948dcc1a9dSDamien Le Moal 		goto unmap;
16958dcc1a9dSDamien Le Moal 
16968dcc1a9dSDamien Le Moal 	stored_crc = le32_to_cpu(super->s_crc);
16978dcc1a9dSDamien Le Moal 	super->s_crc = 0;
16988dcc1a9dSDamien Le Moal 	crc = crc32(~0U, (unsigned char *)super, sizeof(struct zonefs_super));
16998dcc1a9dSDamien Le Moal 	if (crc != stored_crc) {
17008dcc1a9dSDamien Le Moal 		zonefs_err(sb, "Invalid checksum (Expected 0x%08x, got 0x%08x)",
17018dcc1a9dSDamien Le Moal 			   crc, stored_crc);
17028dcc1a9dSDamien Le Moal 		goto unmap;
17038dcc1a9dSDamien Le Moal 	}
17048dcc1a9dSDamien Le Moal 
17058dcc1a9dSDamien Le Moal 	sbi->s_features = le64_to_cpu(super->s_features);
17068dcc1a9dSDamien Le Moal 	if (sbi->s_features & ~ZONEFS_F_DEFINED_FEATURES) {
17078dcc1a9dSDamien Le Moal 		zonefs_err(sb, "Unknown features set 0x%llx\n",
17088dcc1a9dSDamien Le Moal 			   sbi->s_features);
17098dcc1a9dSDamien Le Moal 		goto unmap;
17108dcc1a9dSDamien Le Moal 	}
17118dcc1a9dSDamien Le Moal 
17128dcc1a9dSDamien Le Moal 	if (sbi->s_features & ZONEFS_F_UID) {
17138dcc1a9dSDamien Le Moal 		sbi->s_uid = make_kuid(current_user_ns(),
17148dcc1a9dSDamien Le Moal 				       le32_to_cpu(super->s_uid));
17158dcc1a9dSDamien Le Moal 		if (!uid_valid(sbi->s_uid)) {
17168dcc1a9dSDamien Le Moal 			zonefs_err(sb, "Invalid UID feature\n");
17178dcc1a9dSDamien Le Moal 			goto unmap;
17188dcc1a9dSDamien Le Moal 		}
17198dcc1a9dSDamien Le Moal 	}
17208dcc1a9dSDamien Le Moal 
17218dcc1a9dSDamien Le Moal 	if (sbi->s_features & ZONEFS_F_GID) {
17228dcc1a9dSDamien Le Moal 		sbi->s_gid = make_kgid(current_user_ns(),
17238dcc1a9dSDamien Le Moal 				       le32_to_cpu(super->s_gid));
17248dcc1a9dSDamien Le Moal 		if (!gid_valid(sbi->s_gid)) {
17258dcc1a9dSDamien Le Moal 			zonefs_err(sb, "Invalid GID feature\n");
17268dcc1a9dSDamien Le Moal 			goto unmap;
17278dcc1a9dSDamien Le Moal 		}
17288dcc1a9dSDamien Le Moal 	}
17298dcc1a9dSDamien Le Moal 
17308dcc1a9dSDamien Le Moal 	if (sbi->s_features & ZONEFS_F_PERM)
17318dcc1a9dSDamien Le Moal 		sbi->s_perm = le32_to_cpu(super->s_perm);
17328dcc1a9dSDamien Le Moal 
17338dcc1a9dSDamien Le Moal 	if (memchr_inv(super->s_reserved, 0, sizeof(super->s_reserved))) {
17348dcc1a9dSDamien Le Moal 		zonefs_err(sb, "Reserved area is being used\n");
17358dcc1a9dSDamien Le Moal 		goto unmap;
17368dcc1a9dSDamien Le Moal 	}
17378dcc1a9dSDamien Le Moal 
1738568776f9SAndy Shevchenko 	import_uuid(&sbi->s_uuid, super->s_uuid);
17398dcc1a9dSDamien Le Moal 	ret = 0;
17408dcc1a9dSDamien Le Moal 
17418dcc1a9dSDamien Le Moal unmap:
17428dcc1a9dSDamien Le Moal 	kunmap(page);
17438dcc1a9dSDamien Le Moal free_page:
17448dcc1a9dSDamien Le Moal 	__free_page(page);
17458dcc1a9dSDamien Le Moal 
17468dcc1a9dSDamien Le Moal 	return ret;
17478dcc1a9dSDamien Le Moal }
17488dcc1a9dSDamien Le Moal 
17498dcc1a9dSDamien Le Moal /*
17508dcc1a9dSDamien Le Moal  * Check that the device is zoned. If it is, get the list of zones and create
17518dcc1a9dSDamien Le Moal  * sub-directories and files according to the device zone configuration and
17528dcc1a9dSDamien Le Moal  * format options.
17538dcc1a9dSDamien Le Moal  */
17548dcc1a9dSDamien Le Moal static int zonefs_fill_super(struct super_block *sb, void *data, int silent)
17558dcc1a9dSDamien Le Moal {
17568dcc1a9dSDamien Le Moal 	struct zonefs_zone_data zd;
17578dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi;
17588dcc1a9dSDamien Le Moal 	struct inode *inode;
17598dcc1a9dSDamien Le Moal 	enum zonefs_ztype t;
17608dcc1a9dSDamien Le Moal 	int ret;
17618dcc1a9dSDamien Le Moal 
17628dcc1a9dSDamien Le Moal 	if (!bdev_is_zoned(sb->s_bdev)) {
17638dcc1a9dSDamien Le Moal 		zonefs_err(sb, "Not a zoned block device\n");
17648dcc1a9dSDamien Le Moal 		return -EINVAL;
17658dcc1a9dSDamien Le Moal 	}
17668dcc1a9dSDamien Le Moal 
17678dcc1a9dSDamien Le Moal 	/*
17688dcc1a9dSDamien Le Moal 	 * Initialize super block information: the maximum file size is updated
17698dcc1a9dSDamien Le Moal 	 * when the zone files are created so that the format option
17708dcc1a9dSDamien Le Moal 	 * ZONEFS_F_AGGRCNV which increases the maximum file size of a file
17718dcc1a9dSDamien Le Moal 	 * beyond the zone size is taken into account.
17728dcc1a9dSDamien Le Moal 	 */
17738dcc1a9dSDamien Le Moal 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
17748dcc1a9dSDamien Le Moal 	if (!sbi)
17758dcc1a9dSDamien Le Moal 		return -ENOMEM;
17768dcc1a9dSDamien Le Moal 
17778dcc1a9dSDamien Le Moal 	spin_lock_init(&sbi->s_lock);
17788dcc1a9dSDamien Le Moal 	sb->s_fs_info = sbi;
17798dcc1a9dSDamien Le Moal 	sb->s_magic = ZONEFS_MAGIC;
17808dcc1a9dSDamien Le Moal 	sb->s_maxbytes = 0;
17818dcc1a9dSDamien Le Moal 	sb->s_op = &zonefs_sops;
17828dcc1a9dSDamien Le Moal 	sb->s_time_gran	= 1;
17838dcc1a9dSDamien Le Moal 
17848dcc1a9dSDamien Le Moal 	/*
17850f1ba5f5SDamien Le Moal 	 * The block size is set to the device zone write granularity to ensure
17860f1ba5f5SDamien Le Moal 	 * that write operations are always aligned according to the device
17870f1ba5f5SDamien Le Moal 	 * interface constraints.
17888dcc1a9dSDamien Le Moal 	 */
17890f1ba5f5SDamien Le Moal 	sb_set_blocksize(sb, bdev_zone_write_granularity(sb->s_bdev));
17908dcc1a9dSDamien Le Moal 	sbi->s_zone_sectors_shift = ilog2(bdev_zone_sectors(sb->s_bdev));
17918dcc1a9dSDamien Le Moal 	sbi->s_uid = GLOBAL_ROOT_UID;
17928dcc1a9dSDamien Le Moal 	sbi->s_gid = GLOBAL_ROOT_GID;
17938dcc1a9dSDamien Le Moal 	sbi->s_perm = 0640;
17948dcc1a9dSDamien Le Moal 	sbi->s_mount_opts = ZONEFS_MNTOPT_ERRORS_RO;
17952b95a23cSDamien Le Moal 
17962b95a23cSDamien Le Moal 	atomic_set(&sbi->s_wro_seq_files, 0);
17972b95a23cSDamien Le Moal 	sbi->s_max_wro_seq_files = bdev_max_open_zones(sb->s_bdev);
179887c9ce3fSDamien Le Moal 	atomic_set(&sbi->s_active_seq_files, 0);
179987c9ce3fSDamien Le Moal 	sbi->s_max_active_seq_files = bdev_max_active_zones(sb->s_bdev);
180087c9ce3fSDamien Le Moal 
18018dcc1a9dSDamien Le Moal 	ret = zonefs_read_super(sb);
18028dcc1a9dSDamien Le Moal 	if (ret)
18038dcc1a9dSDamien Le Moal 		return ret;
18048dcc1a9dSDamien Le Moal 
18058dcc1a9dSDamien Le Moal 	ret = zonefs_parse_options(sb, data);
18068dcc1a9dSDamien Le Moal 	if (ret)
18078dcc1a9dSDamien Le Moal 		return ret;
18088dcc1a9dSDamien Le Moal 
18098dcc1a9dSDamien Le Moal 	memset(&zd, 0, sizeof(struct zonefs_zone_data));
18108dcc1a9dSDamien Le Moal 	zd.sb = sb;
18118dcc1a9dSDamien Le Moal 	ret = zonefs_get_zone_info(&zd);
18128dcc1a9dSDamien Le Moal 	if (ret)
18138dcc1a9dSDamien Le Moal 		goto cleanup;
18148dcc1a9dSDamien Le Moal 
18159277a6d4SDamien Le Moal 	ret = zonefs_sysfs_register(sb);
18169277a6d4SDamien Le Moal 	if (ret)
18179277a6d4SDamien Le Moal 		goto cleanup;
18189277a6d4SDamien Le Moal 
1819*b623e347SChristoph Hellwig 	zonefs_info(sb, "Mounting %u zones", bdev_nr_zones(sb->s_bdev));
18208dcc1a9dSDamien Le Moal 
1821a2a513beSDamien Le Moal 	if (!sbi->s_max_wro_seq_files &&
182296eca145SDamien Le Moal 	    !sbi->s_max_active_seq_files &&
1823a2a513beSDamien Le Moal 	    sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) {
182496eca145SDamien Le Moal 		zonefs_info(sb,
182596eca145SDamien Le Moal 			"No open and active zone limits. Ignoring explicit_open mount option\n");
1826a2a513beSDamien Le Moal 		sbi->s_mount_opts &= ~ZONEFS_MNTOPT_EXPLICIT_OPEN;
1827a2a513beSDamien Le Moal 	}
1828a2a513beSDamien Le Moal 
18298dcc1a9dSDamien Le Moal 	/* Create root directory inode */
18308dcc1a9dSDamien Le Moal 	ret = -ENOMEM;
18318dcc1a9dSDamien Le Moal 	inode = new_inode(sb);
18328dcc1a9dSDamien Le Moal 	if (!inode)
18338dcc1a9dSDamien Le Moal 		goto cleanup;
18348dcc1a9dSDamien Le Moal 
1835*b623e347SChristoph Hellwig 	inode->i_ino = bdev_nr_zones(sb->s_bdev);
18368dcc1a9dSDamien Le Moal 	inode->i_mode = S_IFDIR | 0555;
18378dcc1a9dSDamien Le Moal 	inode->i_ctime = inode->i_mtime = inode->i_atime = current_time(inode);
18388dcc1a9dSDamien Le Moal 	inode->i_op = &zonefs_dir_inode_operations;
18398dcc1a9dSDamien Le Moal 	inode->i_fop = &simple_dir_operations;
18408dcc1a9dSDamien Le Moal 	set_nlink(inode, 2);
18418dcc1a9dSDamien Le Moal 
18428dcc1a9dSDamien Le Moal 	sb->s_root = d_make_root(inode);
18438dcc1a9dSDamien Le Moal 	if (!sb->s_root)
18448dcc1a9dSDamien Le Moal 		goto cleanup;
18458dcc1a9dSDamien Le Moal 
18468dcc1a9dSDamien Le Moal 	/* Create and populate files in zone groups directories */
18478dcc1a9dSDamien Le Moal 	for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) {
18488dcc1a9dSDamien Le Moal 		ret = zonefs_create_zgroup(&zd, t);
18498dcc1a9dSDamien Le Moal 		if (ret)
18508dcc1a9dSDamien Le Moal 			break;
18518dcc1a9dSDamien Le Moal 	}
18528dcc1a9dSDamien Le Moal 
18538dcc1a9dSDamien Le Moal cleanup:
18548dcc1a9dSDamien Le Moal 	zonefs_cleanup_zone_info(&zd);
18558dcc1a9dSDamien Le Moal 
18568dcc1a9dSDamien Le Moal 	return ret;
18578dcc1a9dSDamien Le Moal }
18588dcc1a9dSDamien Le Moal 
18598dcc1a9dSDamien Le Moal static struct dentry *zonefs_mount(struct file_system_type *fs_type,
18608dcc1a9dSDamien Le Moal 				   int flags, const char *dev_name, void *data)
18618dcc1a9dSDamien Le Moal {
18628dcc1a9dSDamien Le Moal 	return mount_bdev(fs_type, flags, dev_name, data, zonefs_fill_super);
18638dcc1a9dSDamien Le Moal }
18648dcc1a9dSDamien Le Moal 
18658dcc1a9dSDamien Le Moal static void zonefs_kill_super(struct super_block *sb)
18668dcc1a9dSDamien Le Moal {
18678dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
18688dcc1a9dSDamien Le Moal 
18698dcc1a9dSDamien Le Moal 	if (sb->s_root)
18708dcc1a9dSDamien Le Moal 		d_genocide(sb->s_root);
18719277a6d4SDamien Le Moal 
18729277a6d4SDamien Le Moal 	zonefs_sysfs_unregister(sb);
18738dcc1a9dSDamien Le Moal 	kill_block_super(sb);
18748dcc1a9dSDamien Le Moal 	kfree(sbi);
18758dcc1a9dSDamien Le Moal }
18768dcc1a9dSDamien Le Moal 
18778dcc1a9dSDamien Le Moal /*
18788dcc1a9dSDamien Le Moal  * File system definition and registration.
18798dcc1a9dSDamien Le Moal  */
18808dcc1a9dSDamien Le Moal static struct file_system_type zonefs_type = {
18818dcc1a9dSDamien Le Moal 	.owner		= THIS_MODULE,
18828dcc1a9dSDamien Le Moal 	.name		= "zonefs",
18838dcc1a9dSDamien Le Moal 	.mount		= zonefs_mount,
18848dcc1a9dSDamien Le Moal 	.kill_sb	= zonefs_kill_super,
18858dcc1a9dSDamien Le Moal 	.fs_flags	= FS_REQUIRES_DEV,
18868dcc1a9dSDamien Le Moal };
18878dcc1a9dSDamien Le Moal 
18888dcc1a9dSDamien Le Moal static int __init zonefs_init_inodecache(void)
18898dcc1a9dSDamien Le Moal {
18908dcc1a9dSDamien Le Moal 	zonefs_inode_cachep = kmem_cache_create("zonefs_inode_cache",
18918dcc1a9dSDamien Le Moal 			sizeof(struct zonefs_inode_info), 0,
18928dcc1a9dSDamien Le Moal 			(SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT),
18938dcc1a9dSDamien Le Moal 			NULL);
18948dcc1a9dSDamien Le Moal 	if (zonefs_inode_cachep == NULL)
18958dcc1a9dSDamien Le Moal 		return -ENOMEM;
18968dcc1a9dSDamien Le Moal 	return 0;
18978dcc1a9dSDamien Le Moal }
18988dcc1a9dSDamien Le Moal 
18998dcc1a9dSDamien Le Moal static void zonefs_destroy_inodecache(void)
19008dcc1a9dSDamien Le Moal {
19018dcc1a9dSDamien Le Moal 	/*
19028dcc1a9dSDamien Le Moal 	 * Make sure all delayed rcu free inodes are flushed before we
19038dcc1a9dSDamien Le Moal 	 * destroy the inode cache.
19048dcc1a9dSDamien Le Moal 	 */
19058dcc1a9dSDamien Le Moal 	rcu_barrier();
19068dcc1a9dSDamien Le Moal 	kmem_cache_destroy(zonefs_inode_cachep);
19078dcc1a9dSDamien Le Moal }
19088dcc1a9dSDamien Le Moal 
19098dcc1a9dSDamien Le Moal static int __init zonefs_init(void)
19108dcc1a9dSDamien Le Moal {
19118dcc1a9dSDamien Le Moal 	int ret;
19128dcc1a9dSDamien Le Moal 
19138dcc1a9dSDamien Le Moal 	BUILD_BUG_ON(sizeof(struct zonefs_super) != ZONEFS_SUPER_SIZE);
19148dcc1a9dSDamien Le Moal 
19158dcc1a9dSDamien Le Moal 	ret = zonefs_init_inodecache();
19168dcc1a9dSDamien Le Moal 	if (ret)
19178dcc1a9dSDamien Le Moal 		return ret;
19188dcc1a9dSDamien Le Moal 
19198dcc1a9dSDamien Le Moal 	ret = register_filesystem(&zonefs_type);
19209277a6d4SDamien Le Moal 	if (ret)
19219277a6d4SDamien Le Moal 		goto destroy_inodecache;
19229277a6d4SDamien Le Moal 
19239277a6d4SDamien Le Moal 	ret = zonefs_sysfs_init();
19249277a6d4SDamien Le Moal 	if (ret)
19259277a6d4SDamien Le Moal 		goto unregister_fs;
19268dcc1a9dSDamien Le Moal 
19278dcc1a9dSDamien Le Moal 	return 0;
19289277a6d4SDamien Le Moal 
19299277a6d4SDamien Le Moal unregister_fs:
19309277a6d4SDamien Le Moal 	unregister_filesystem(&zonefs_type);
19319277a6d4SDamien Le Moal destroy_inodecache:
19329277a6d4SDamien Le Moal 	zonefs_destroy_inodecache();
19339277a6d4SDamien Le Moal 
19349277a6d4SDamien Le Moal 	return ret;
19358dcc1a9dSDamien Le Moal }
19368dcc1a9dSDamien Le Moal 
19378dcc1a9dSDamien Le Moal static void __exit zonefs_exit(void)
19388dcc1a9dSDamien Le Moal {
19399277a6d4SDamien Le Moal 	zonefs_sysfs_exit();
19408dcc1a9dSDamien Le Moal 	zonefs_destroy_inodecache();
19418dcc1a9dSDamien Le Moal 	unregister_filesystem(&zonefs_type);
19428dcc1a9dSDamien Le Moal }
19438dcc1a9dSDamien Le Moal 
19448dcc1a9dSDamien Le Moal MODULE_AUTHOR("Damien Le Moal");
19458dcc1a9dSDamien Le Moal MODULE_DESCRIPTION("Zone file system for zoned block devices");
19468dcc1a9dSDamien Le Moal MODULE_LICENSE("GPL");
19478ffea259SNaohiro Aota MODULE_ALIAS_FS("zonefs");
19488dcc1a9dSDamien Le Moal module_init(zonefs_init);
19498dcc1a9dSDamien Le Moal module_exit(zonefs_exit);
1950