xref: /openbmc/linux/fs/zonefs/super.c (revision 0f1ba5f5d80f84b605e70cf4661cb1a44a3c02a6)
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>
88dcc1a9dSDamien Le Moal #include <linux/fs.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 
275498d5f9SJohannes Thumshirn static inline int zonefs_zone_mgmt(struct inode *inode,
285498d5f9SJohannes Thumshirn 				   enum req_opf op)
295498d5f9SJohannes Thumshirn {
305498d5f9SJohannes Thumshirn 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
315498d5f9SJohannes Thumshirn 	int ret;
325498d5f9SJohannes Thumshirn 
335498d5f9SJohannes Thumshirn 	lockdep_assert_held(&zi->i_truncate_mutex);
345498d5f9SJohannes Thumshirn 
355498d5f9SJohannes Thumshirn 	ret = blkdev_zone_mgmt(inode->i_sb->s_bdev, op, zi->i_zsector,
365498d5f9SJohannes Thumshirn 			       zi->i_zone_size >> SECTOR_SHIFT, GFP_NOFS);
375498d5f9SJohannes Thumshirn 	if (ret) {
385498d5f9SJohannes Thumshirn 		zonefs_err(inode->i_sb,
395498d5f9SJohannes Thumshirn 			   "Zone management operation %s at %llu failed %d\n",
405498d5f9SJohannes Thumshirn 			   blk_op_str(op), zi->i_zsector, ret);
415498d5f9SJohannes Thumshirn 		return ret;
425498d5f9SJohannes Thumshirn 	}
435498d5f9SJohannes Thumshirn 
445498d5f9SJohannes Thumshirn 	return 0;
455498d5f9SJohannes Thumshirn }
465498d5f9SJohannes Thumshirn 
47b5c00e97SJohannes Thumshirn static inline void zonefs_i_size_write(struct inode *inode, loff_t isize)
48b5c00e97SJohannes Thumshirn {
49b5c00e97SJohannes Thumshirn 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
50b5c00e97SJohannes Thumshirn 
51b5c00e97SJohannes Thumshirn 	i_size_write(inode, isize);
52b5c00e97SJohannes Thumshirn 	/*
53b5c00e97SJohannes Thumshirn 	 * A full zone is no longer open/active and does not need
54b5c00e97SJohannes Thumshirn 	 * explicit closing.
55b5c00e97SJohannes Thumshirn 	 */
56b5c00e97SJohannes Thumshirn 	if (isize >= zi->i_max_size)
57b5c00e97SJohannes Thumshirn 		zi->i_flags &= ~ZONEFS_ZONE_OPEN;
58b5c00e97SJohannes Thumshirn }
59b5c00e97SJohannes Thumshirn 
608dcc1a9dSDamien Le Moal static int zonefs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
618dcc1a9dSDamien Le Moal 			      unsigned int flags, struct iomap *iomap,
628dcc1a9dSDamien Le Moal 			      struct iomap *srcmap)
638dcc1a9dSDamien Le Moal {
648dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
658dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
668dcc1a9dSDamien Le Moal 	loff_t isize;
678dcc1a9dSDamien Le Moal 
688dcc1a9dSDamien Le Moal 	/* All I/Os should always be within the file maximum size */
698dcc1a9dSDamien Le Moal 	if (WARN_ON_ONCE(offset + length > zi->i_max_size))
708dcc1a9dSDamien Le Moal 		return -EIO;
718dcc1a9dSDamien Le Moal 
728dcc1a9dSDamien Le Moal 	/*
738dcc1a9dSDamien Le Moal 	 * Sequential zones can only accept direct writes. This is already
748dcc1a9dSDamien Le Moal 	 * checked when writes are issued, so warn if we see a page writeback
758dcc1a9dSDamien Le Moal 	 * operation.
768dcc1a9dSDamien Le Moal 	 */
778dcc1a9dSDamien Le Moal 	if (WARN_ON_ONCE(zi->i_ztype == ZONEFS_ZTYPE_SEQ &&
788dcc1a9dSDamien Le Moal 			 (flags & IOMAP_WRITE) && !(flags & IOMAP_DIRECT)))
798dcc1a9dSDamien Le Moal 		return -EIO;
808dcc1a9dSDamien Le Moal 
818dcc1a9dSDamien Le Moal 	/*
828dcc1a9dSDamien Le Moal 	 * For conventional zones, all blocks are always mapped. For sequential
838dcc1a9dSDamien Le Moal 	 * zones, all blocks after always mapped below the inode size (zone
848dcc1a9dSDamien Le Moal 	 * write pointer) and unwriten beyond.
858dcc1a9dSDamien Le Moal 	 */
868dcc1a9dSDamien Le Moal 	mutex_lock(&zi->i_truncate_mutex);
878dcc1a9dSDamien Le Moal 	isize = i_size_read(inode);
888dcc1a9dSDamien Le Moal 	if (offset >= isize)
898dcc1a9dSDamien Le Moal 		iomap->type = IOMAP_UNWRITTEN;
908dcc1a9dSDamien Le Moal 	else
918dcc1a9dSDamien Le Moal 		iomap->type = IOMAP_MAPPED;
928dcc1a9dSDamien Le Moal 	if (flags & IOMAP_WRITE)
938dcc1a9dSDamien Le Moal 		length = zi->i_max_size - offset;
948dcc1a9dSDamien Le Moal 	else
958dcc1a9dSDamien Le Moal 		length = min(length, isize - offset);
968dcc1a9dSDamien Le Moal 	mutex_unlock(&zi->i_truncate_mutex);
978dcc1a9dSDamien Le Moal 
988dcc1a9dSDamien Le Moal 	iomap->offset = ALIGN_DOWN(offset, sb->s_blocksize);
998dcc1a9dSDamien Le Moal 	iomap->length = ALIGN(offset + length, sb->s_blocksize) - iomap->offset;
1008dcc1a9dSDamien Le Moal 	iomap->bdev = inode->i_sb->s_bdev;
1018dcc1a9dSDamien Le Moal 	iomap->addr = (zi->i_zsector << SECTOR_SHIFT) + iomap->offset;
1028dcc1a9dSDamien Le Moal 
1038dcc1a9dSDamien Le Moal 	return 0;
1048dcc1a9dSDamien Le Moal }
1058dcc1a9dSDamien Le Moal 
1068dcc1a9dSDamien Le Moal static const struct iomap_ops zonefs_iomap_ops = {
1078dcc1a9dSDamien Le Moal 	.iomap_begin	= zonefs_iomap_begin,
1088dcc1a9dSDamien Le Moal };
1098dcc1a9dSDamien Le Moal 
1108dcc1a9dSDamien Le Moal static int zonefs_readpage(struct file *unused, struct page *page)
1118dcc1a9dSDamien Le Moal {
1128dcc1a9dSDamien Le Moal 	return iomap_readpage(page, &zonefs_iomap_ops);
1138dcc1a9dSDamien Le Moal }
1148dcc1a9dSDamien Le Moal 
1159d24a13aSMatthew Wilcox (Oracle) static void zonefs_readahead(struct readahead_control *rac)
1168dcc1a9dSDamien Le Moal {
1179d24a13aSMatthew Wilcox (Oracle) 	iomap_readahead(rac, &zonefs_iomap_ops);
1188dcc1a9dSDamien Le Moal }
1198dcc1a9dSDamien Le Moal 
1208dcc1a9dSDamien Le Moal /*
1218dcc1a9dSDamien Le Moal  * Map blocks for page writeback. This is used only on conventional zone files,
1228dcc1a9dSDamien Le Moal  * which implies that the page range can only be within the fixed inode size.
1238dcc1a9dSDamien Le Moal  */
1248dcc1a9dSDamien Le Moal static int zonefs_map_blocks(struct iomap_writepage_ctx *wpc,
1258dcc1a9dSDamien Le Moal 			     struct inode *inode, loff_t offset)
1268dcc1a9dSDamien Le Moal {
1278dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
1288dcc1a9dSDamien Le Moal 
1298dcc1a9dSDamien Le Moal 	if (WARN_ON_ONCE(zi->i_ztype != ZONEFS_ZTYPE_CNV))
1308dcc1a9dSDamien Le Moal 		return -EIO;
1318dcc1a9dSDamien Le Moal 	if (WARN_ON_ONCE(offset >= i_size_read(inode)))
1328dcc1a9dSDamien Le Moal 		return -EIO;
1338dcc1a9dSDamien Le Moal 
1348dcc1a9dSDamien Le Moal 	/* If the mapping is already OK, nothing needs to be done */
1358dcc1a9dSDamien Le Moal 	if (offset >= wpc->iomap.offset &&
1368dcc1a9dSDamien Le Moal 	    offset < wpc->iomap.offset + wpc->iomap.length)
1378dcc1a9dSDamien Le Moal 		return 0;
1388dcc1a9dSDamien Le Moal 
1398dcc1a9dSDamien Le Moal 	return zonefs_iomap_begin(inode, offset, zi->i_max_size - offset,
1408dcc1a9dSDamien Le Moal 				  IOMAP_WRITE, &wpc->iomap, NULL);
1418dcc1a9dSDamien Le Moal }
1428dcc1a9dSDamien Le Moal 
1438dcc1a9dSDamien Le Moal static const struct iomap_writeback_ops zonefs_writeback_ops = {
1448dcc1a9dSDamien Le Moal 	.map_blocks		= zonefs_map_blocks,
1458dcc1a9dSDamien Le Moal };
1468dcc1a9dSDamien Le Moal 
1478dcc1a9dSDamien Le Moal static int zonefs_writepage(struct page *page, struct writeback_control *wbc)
1488dcc1a9dSDamien Le Moal {
1498dcc1a9dSDamien Le Moal 	struct iomap_writepage_ctx wpc = { };
1508dcc1a9dSDamien Le Moal 
1518dcc1a9dSDamien Le Moal 	return iomap_writepage(page, wbc, &wpc, &zonefs_writeback_ops);
1528dcc1a9dSDamien Le Moal }
1538dcc1a9dSDamien Le Moal 
1548dcc1a9dSDamien Le Moal static int zonefs_writepages(struct address_space *mapping,
1558dcc1a9dSDamien Le Moal 			     struct writeback_control *wbc)
1568dcc1a9dSDamien Le Moal {
1578dcc1a9dSDamien Le Moal 	struct iomap_writepage_ctx wpc = { };
1588dcc1a9dSDamien Le Moal 
1598dcc1a9dSDamien Le Moal 	return iomap_writepages(mapping, wbc, &wpc, &zonefs_writeback_ops);
1608dcc1a9dSDamien Le Moal }
1618dcc1a9dSDamien Le Moal 
1628dcc1a9dSDamien Le Moal static const struct address_space_operations zonefs_file_aops = {
1638dcc1a9dSDamien Le Moal 	.readpage		= zonefs_readpage,
1649d24a13aSMatthew Wilcox (Oracle) 	.readahead		= zonefs_readahead,
1658dcc1a9dSDamien Le Moal 	.writepage		= zonefs_writepage,
1668dcc1a9dSDamien Le Moal 	.writepages		= zonefs_writepages,
1678dcc1a9dSDamien Le Moal 	.set_page_dirty		= iomap_set_page_dirty,
1688dcc1a9dSDamien Le Moal 	.releasepage		= iomap_releasepage,
1698dcc1a9dSDamien Le Moal 	.invalidatepage		= iomap_invalidatepage,
1708dcc1a9dSDamien Le Moal 	.migratepage		= iomap_migrate_page,
1718dcc1a9dSDamien Le Moal 	.is_partially_uptodate	= iomap_is_partially_uptodate,
1728dcc1a9dSDamien Le Moal 	.error_remove_page	= generic_error_remove_page,
1738dcc1a9dSDamien Le Moal 	.direct_IO		= noop_direct_IO,
1748dcc1a9dSDamien Le Moal };
1758dcc1a9dSDamien Le Moal 
1768dcc1a9dSDamien Le Moal static void zonefs_update_stats(struct inode *inode, loff_t new_isize)
1778dcc1a9dSDamien Le Moal {
1788dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
1798dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1808dcc1a9dSDamien Le Moal 	loff_t old_isize = i_size_read(inode);
1818dcc1a9dSDamien Le Moal 	loff_t nr_blocks;
1828dcc1a9dSDamien Le Moal 
1838dcc1a9dSDamien Le Moal 	if (new_isize == old_isize)
1848dcc1a9dSDamien Le Moal 		return;
1858dcc1a9dSDamien Le Moal 
1868dcc1a9dSDamien Le Moal 	spin_lock(&sbi->s_lock);
1878dcc1a9dSDamien Le Moal 
1888dcc1a9dSDamien Le Moal 	/*
1898dcc1a9dSDamien Le Moal 	 * This may be called for an update after an IO error.
1908dcc1a9dSDamien Le Moal 	 * So beware of the values seen.
1918dcc1a9dSDamien Le Moal 	 */
1928dcc1a9dSDamien Le Moal 	if (new_isize < old_isize) {
1938dcc1a9dSDamien Le Moal 		nr_blocks = (old_isize - new_isize) >> sb->s_blocksize_bits;
1948dcc1a9dSDamien Le Moal 		if (sbi->s_used_blocks > nr_blocks)
1958dcc1a9dSDamien Le Moal 			sbi->s_used_blocks -= nr_blocks;
1968dcc1a9dSDamien Le Moal 		else
1978dcc1a9dSDamien Le Moal 			sbi->s_used_blocks = 0;
1988dcc1a9dSDamien Le Moal 	} else {
1998dcc1a9dSDamien Le Moal 		sbi->s_used_blocks +=
2008dcc1a9dSDamien Le Moal 			(new_isize - old_isize) >> sb->s_blocksize_bits;
2018dcc1a9dSDamien Le Moal 		if (sbi->s_used_blocks > sbi->s_blocks)
2028dcc1a9dSDamien Le Moal 			sbi->s_used_blocks = sbi->s_blocks;
2038dcc1a9dSDamien Le Moal 	}
2048dcc1a9dSDamien Le Moal 
2058dcc1a9dSDamien Le Moal 	spin_unlock(&sbi->s_lock);
2068dcc1a9dSDamien Le Moal }
2078dcc1a9dSDamien Le Moal 
2088dcc1a9dSDamien Le Moal /*
2098dcc1a9dSDamien Le Moal  * Check a zone condition and adjust its file inode access permissions for
2108dcc1a9dSDamien Le Moal  * offline and readonly zones. Return the inode size corresponding to the
2118dcc1a9dSDamien Le Moal  * amount of readable data in the zone.
2128dcc1a9dSDamien Le Moal  */
2138dcc1a9dSDamien Le Moal static loff_t zonefs_check_zone_condition(struct inode *inode,
214ccf4ad7dSDamien Le Moal 					  struct blk_zone *zone, bool warn,
215ccf4ad7dSDamien Le Moal 					  bool mount)
2168dcc1a9dSDamien Le Moal {
2178dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
2188dcc1a9dSDamien Le Moal 
2198dcc1a9dSDamien Le Moal 	switch (zone->cond) {
2208dcc1a9dSDamien Le Moal 	case BLK_ZONE_COND_OFFLINE:
2218dcc1a9dSDamien Le Moal 		/*
2228dcc1a9dSDamien Le Moal 		 * Dead zone: make the inode immutable, disable all accesses
2238dcc1a9dSDamien Le Moal 		 * and set the file size to 0 (zone wp set to zone start).
2248dcc1a9dSDamien Le Moal 		 */
2258dcc1a9dSDamien Le Moal 		if (warn)
2268dcc1a9dSDamien Le Moal 			zonefs_warn(inode->i_sb, "inode %lu: offline zone\n",
2278dcc1a9dSDamien Le Moal 				    inode->i_ino);
2288dcc1a9dSDamien Le Moal 		inode->i_flags |= S_IMMUTABLE;
2298dcc1a9dSDamien Le Moal 		inode->i_mode &= ~0777;
2308dcc1a9dSDamien Le Moal 		zone->wp = zone->start;
2318dcc1a9dSDamien Le Moal 		return 0;
2328dcc1a9dSDamien Le Moal 	case BLK_ZONE_COND_READONLY:
233ccf4ad7dSDamien Le Moal 		/*
234ccf4ad7dSDamien Le Moal 		 * The write pointer of read-only zones is invalid. If such a
235ccf4ad7dSDamien Le Moal 		 * zone is found during mount, the file size cannot be retrieved
236ccf4ad7dSDamien Le Moal 		 * so we treat the zone as offline (mount == true case).
237ccf4ad7dSDamien Le Moal 		 * Otherwise, keep the file size as it was when last updated
238ccf4ad7dSDamien Le Moal 		 * so that the user can recover data. In both cases, writes are
239ccf4ad7dSDamien Le Moal 		 * always disabled for the zone.
240ccf4ad7dSDamien Le Moal 		 */
2418dcc1a9dSDamien Le Moal 		if (warn)
2428dcc1a9dSDamien Le Moal 			zonefs_warn(inode->i_sb, "inode %lu: read-only zone\n",
2438dcc1a9dSDamien Le Moal 				    inode->i_ino);
2448dcc1a9dSDamien Le Moal 		inode->i_flags |= S_IMMUTABLE;
245ccf4ad7dSDamien Le Moal 		if (mount) {
246ccf4ad7dSDamien Le Moal 			zone->cond = BLK_ZONE_COND_OFFLINE;
247ccf4ad7dSDamien Le Moal 			inode->i_mode &= ~0777;
248ccf4ad7dSDamien Le Moal 			zone->wp = zone->start;
249ccf4ad7dSDamien Le Moal 			return 0;
250ccf4ad7dSDamien Le Moal 		}
2518dcc1a9dSDamien Le Moal 		inode->i_mode &= ~0222;
252ccf4ad7dSDamien Le Moal 		return i_size_read(inode);
2538dcc1a9dSDamien Le Moal 	default:
2548dcc1a9dSDamien Le Moal 		if (zi->i_ztype == ZONEFS_ZTYPE_CNV)
2558dcc1a9dSDamien Le Moal 			return zi->i_max_size;
2568dcc1a9dSDamien Le Moal 		return (zone->wp - zone->start) << SECTOR_SHIFT;
2578dcc1a9dSDamien Le Moal 	}
2588dcc1a9dSDamien Le Moal }
2598dcc1a9dSDamien Le Moal 
2608dcc1a9dSDamien Le Moal struct zonefs_ioerr_data {
2618dcc1a9dSDamien Le Moal 	struct inode	*inode;
2628dcc1a9dSDamien Le Moal 	bool		write;
2638dcc1a9dSDamien Le Moal };
2648dcc1a9dSDamien Le Moal 
2658dcc1a9dSDamien Le Moal static int zonefs_io_error_cb(struct blk_zone *zone, unsigned int idx,
2668dcc1a9dSDamien Le Moal 			      void *data)
2678dcc1a9dSDamien Le Moal {
2688dcc1a9dSDamien Le Moal 	struct zonefs_ioerr_data *err = data;
2698dcc1a9dSDamien Le Moal 	struct inode *inode = err->inode;
2708dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
2718dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
2728dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
2738dcc1a9dSDamien Le Moal 	loff_t isize, data_size;
2748dcc1a9dSDamien Le Moal 
2758dcc1a9dSDamien Le Moal 	/*
2768dcc1a9dSDamien Le Moal 	 * Check the zone condition: if the zone is not "bad" (offline or
2778dcc1a9dSDamien Le Moal 	 * read-only), read errors are simply signaled to the IO issuer as long
2788dcc1a9dSDamien Le Moal 	 * as there is no inconsistency between the inode size and the amount of
2798dcc1a9dSDamien Le Moal 	 * data writen in the zone (data_size).
2808dcc1a9dSDamien Le Moal 	 */
281ccf4ad7dSDamien Le Moal 	data_size = zonefs_check_zone_condition(inode, zone, true, false);
2828dcc1a9dSDamien Le Moal 	isize = i_size_read(inode);
2838dcc1a9dSDamien Le Moal 	if (zone->cond != BLK_ZONE_COND_OFFLINE &&
2848dcc1a9dSDamien Le Moal 	    zone->cond != BLK_ZONE_COND_READONLY &&
2858dcc1a9dSDamien Le Moal 	    !err->write && isize == data_size)
2868dcc1a9dSDamien Le Moal 		return 0;
2878dcc1a9dSDamien Le Moal 
2888dcc1a9dSDamien Le Moal 	/*
2898dcc1a9dSDamien Le Moal 	 * At this point, we detected either a bad zone or an inconsistency
2908dcc1a9dSDamien Le Moal 	 * between the inode size and the amount of data written in the zone.
2918dcc1a9dSDamien Le Moal 	 * For the latter case, the cause may be a write IO error or an external
2928dcc1a9dSDamien Le Moal 	 * action on the device. Two error patterns exist:
2938dcc1a9dSDamien Le Moal 	 * 1) The inode size is lower than the amount of data in the zone:
2948dcc1a9dSDamien Le Moal 	 *    a write operation partially failed and data was writen at the end
2958dcc1a9dSDamien Le Moal 	 *    of the file. This can happen in the case of a large direct IO
2968dcc1a9dSDamien Le Moal 	 *    needing several BIOs and/or write requests to be processed.
2978dcc1a9dSDamien Le Moal 	 * 2) The inode size is larger than the amount of data in the zone:
2988dcc1a9dSDamien Le Moal 	 *    this can happen with a deferred write error with the use of the
2998dcc1a9dSDamien Le Moal 	 *    device side write cache after getting successful write IO
3008dcc1a9dSDamien Le Moal 	 *    completions. Other possibilities are (a) an external corruption,
3018dcc1a9dSDamien Le Moal 	 *    e.g. an application reset the zone directly, or (b) the device
3028dcc1a9dSDamien Le Moal 	 *    has a serious problem (e.g. firmware bug).
3038dcc1a9dSDamien Le Moal 	 *
3048dcc1a9dSDamien Le Moal 	 * In all cases, warn about inode size inconsistency and handle the
3058dcc1a9dSDamien Le Moal 	 * IO error according to the zone condition and to the mount options.
3068dcc1a9dSDamien Le Moal 	 */
3078dcc1a9dSDamien Le Moal 	if (zi->i_ztype == ZONEFS_ZTYPE_SEQ && isize != data_size)
3088dcc1a9dSDamien Le Moal 		zonefs_warn(sb, "inode %lu: invalid size %lld (should be %lld)\n",
3098dcc1a9dSDamien Le Moal 			    inode->i_ino, isize, data_size);
3108dcc1a9dSDamien Le Moal 
3118dcc1a9dSDamien Le Moal 	/*
3128dcc1a9dSDamien Le Moal 	 * First handle bad zones signaled by hardware. The mount options
3138dcc1a9dSDamien Le Moal 	 * errors=zone-ro and errors=zone-offline result in changing the
3148dcc1a9dSDamien Le Moal 	 * zone condition to read-only and offline respectively, as if the
3158dcc1a9dSDamien Le Moal 	 * condition was signaled by the hardware.
3168dcc1a9dSDamien Le Moal 	 */
3178dcc1a9dSDamien Le Moal 	if (zone->cond == BLK_ZONE_COND_OFFLINE ||
3188dcc1a9dSDamien Le Moal 	    sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL) {
3198dcc1a9dSDamien Le Moal 		zonefs_warn(sb, "inode %lu: read/write access disabled\n",
3208dcc1a9dSDamien Le Moal 			    inode->i_ino);
3218dcc1a9dSDamien Le Moal 		if (zone->cond != BLK_ZONE_COND_OFFLINE) {
3228dcc1a9dSDamien Le Moal 			zone->cond = BLK_ZONE_COND_OFFLINE;
3238dcc1a9dSDamien Le Moal 			data_size = zonefs_check_zone_condition(inode, zone,
324ccf4ad7dSDamien Le Moal 								false, false);
3258dcc1a9dSDamien Le Moal 		}
3268dcc1a9dSDamien Le Moal 	} else if (zone->cond == BLK_ZONE_COND_READONLY ||
3278dcc1a9dSDamien Le Moal 		   sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO) {
3288dcc1a9dSDamien Le Moal 		zonefs_warn(sb, "inode %lu: write access disabled\n",
3298dcc1a9dSDamien Le Moal 			    inode->i_ino);
3308dcc1a9dSDamien Le Moal 		if (zone->cond != BLK_ZONE_COND_READONLY) {
3318dcc1a9dSDamien Le Moal 			zone->cond = BLK_ZONE_COND_READONLY;
3328dcc1a9dSDamien Le Moal 			data_size = zonefs_check_zone_condition(inode, zone,
333ccf4ad7dSDamien Le Moal 								false, false);
3348dcc1a9dSDamien Le Moal 		}
3358dcc1a9dSDamien Le Moal 	}
3368dcc1a9dSDamien Le Moal 
3378dcc1a9dSDamien Le Moal 	/*
338b5c00e97SJohannes Thumshirn 	 * If the filesystem is mounted with the explicit-open mount option, we
339b5c00e97SJohannes Thumshirn 	 * need to clear the ZONEFS_ZONE_OPEN flag if the zone transitioned to
340b5c00e97SJohannes Thumshirn 	 * the read-only or offline condition, to avoid attempting an explicit
341b5c00e97SJohannes Thumshirn 	 * close of the zone when the inode file is closed.
342b5c00e97SJohannes Thumshirn 	 */
343b5c00e97SJohannes Thumshirn 	if ((sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) &&
344b5c00e97SJohannes Thumshirn 	    (zone->cond == BLK_ZONE_COND_OFFLINE ||
345b5c00e97SJohannes Thumshirn 	     zone->cond == BLK_ZONE_COND_READONLY))
346b5c00e97SJohannes Thumshirn 		zi->i_flags &= ~ZONEFS_ZONE_OPEN;
347b5c00e97SJohannes Thumshirn 
348b5c00e97SJohannes Thumshirn 	/*
3498dcc1a9dSDamien Le Moal 	 * If error=remount-ro was specified, any error result in remounting
3508dcc1a9dSDamien Le Moal 	 * the volume as read-only.
3518dcc1a9dSDamien Le Moal 	 */
3528dcc1a9dSDamien Le Moal 	if ((sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO) && !sb_rdonly(sb)) {
3538dcc1a9dSDamien Le Moal 		zonefs_warn(sb, "remounting filesystem read-only\n");
3548dcc1a9dSDamien Le Moal 		sb->s_flags |= SB_RDONLY;
3558dcc1a9dSDamien Le Moal 	}
3568dcc1a9dSDamien Le Moal 
3578dcc1a9dSDamien Le Moal 	/*
3588dcc1a9dSDamien Le Moal 	 * Update block usage stats and the inode size  to prevent access to
3598dcc1a9dSDamien Le Moal 	 * invalid data.
3608dcc1a9dSDamien Le Moal 	 */
3618dcc1a9dSDamien Le Moal 	zonefs_update_stats(inode, data_size);
362b5c00e97SJohannes Thumshirn 	zonefs_i_size_write(inode, data_size);
3638dcc1a9dSDamien Le Moal 	zi->i_wpoffset = data_size;
3648dcc1a9dSDamien Le Moal 
3658dcc1a9dSDamien Le Moal 	return 0;
3668dcc1a9dSDamien Le Moal }
3678dcc1a9dSDamien Le Moal 
3688dcc1a9dSDamien Le Moal /*
3698dcc1a9dSDamien Le Moal  * When an file IO error occurs, check the file zone to see if there is a change
3708dcc1a9dSDamien Le Moal  * in the zone condition (e.g. offline or read-only). For a failed write to a
3718dcc1a9dSDamien Le Moal  * sequential zone, the zone write pointer position must also be checked to
3728dcc1a9dSDamien Le Moal  * eventually correct the file size and zonefs inode write pointer offset
3738dcc1a9dSDamien Le Moal  * (which can be out of sync with the drive due to partial write failures).
3748dcc1a9dSDamien Le Moal  */
37548d546a8SJohannes Thumshirn static void __zonefs_io_error(struct inode *inode, bool write)
3768dcc1a9dSDamien Le Moal {
3778dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
3788dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
3798dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
3808dcc1a9dSDamien Le Moal 	unsigned int noio_flag;
3818dcc1a9dSDamien Le Moal 	unsigned int nr_zones =
382e3c3155bSJohannes Thumshirn 		zi->i_zone_size >> (sbi->s_zone_sectors_shift + SECTOR_SHIFT);
3838dcc1a9dSDamien Le Moal 	struct zonefs_ioerr_data err = {
3848dcc1a9dSDamien Le Moal 		.inode = inode,
3858dcc1a9dSDamien Le Moal 		.write = write,
3868dcc1a9dSDamien Le Moal 	};
3878dcc1a9dSDamien Le Moal 	int ret;
3888dcc1a9dSDamien Le Moal 
3898dcc1a9dSDamien Le Moal 	/*
3908dcc1a9dSDamien Le Moal 	 * Memory allocations in blkdev_report_zones() can trigger a memory
3918dcc1a9dSDamien Le Moal 	 * reclaim which may in turn cause a recursion into zonefs as well as
3928dcc1a9dSDamien Le Moal 	 * struct request allocations for the same device. The former case may
3938dcc1a9dSDamien Le Moal 	 * end up in a deadlock on the inode truncate mutex, while the latter
3948dcc1a9dSDamien Le Moal 	 * may prevent IO forward progress. Executing the report zones under
3958dcc1a9dSDamien Le Moal 	 * the GFP_NOIO context avoids both problems.
3968dcc1a9dSDamien Le Moal 	 */
3978dcc1a9dSDamien Le Moal 	noio_flag = memalloc_noio_save();
3988dcc1a9dSDamien Le Moal 	ret = blkdev_report_zones(sb->s_bdev, zi->i_zsector, nr_zones,
3998dcc1a9dSDamien Le Moal 				  zonefs_io_error_cb, &err);
4008dcc1a9dSDamien Le Moal 	if (ret != nr_zones)
4018dcc1a9dSDamien Le Moal 		zonefs_err(sb, "Get inode %lu zone information failed %d\n",
4028dcc1a9dSDamien Le Moal 			   inode->i_ino, ret);
4038dcc1a9dSDamien Le Moal 	memalloc_noio_restore(noio_flag);
40448d546a8SJohannes Thumshirn }
4058dcc1a9dSDamien Le Moal 
40648d546a8SJohannes Thumshirn static void zonefs_io_error(struct inode *inode, bool write)
40748d546a8SJohannes Thumshirn {
40848d546a8SJohannes Thumshirn 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
40948d546a8SJohannes Thumshirn 
41048d546a8SJohannes Thumshirn 	mutex_lock(&zi->i_truncate_mutex);
41148d546a8SJohannes Thumshirn 	__zonefs_io_error(inode, write);
4128dcc1a9dSDamien Le Moal 	mutex_unlock(&zi->i_truncate_mutex);
4138dcc1a9dSDamien Le Moal }
4148dcc1a9dSDamien Le Moal 
4158dcc1a9dSDamien Le Moal static int zonefs_file_truncate(struct inode *inode, loff_t isize)
4168dcc1a9dSDamien Le Moal {
4178dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
4188dcc1a9dSDamien Le Moal 	loff_t old_isize;
4198dcc1a9dSDamien Le Moal 	enum req_opf op;
4208dcc1a9dSDamien Le Moal 	int ret = 0;
4218dcc1a9dSDamien Le Moal 
4228dcc1a9dSDamien Le Moal 	/*
4238dcc1a9dSDamien Le Moal 	 * Only sequential zone files can be truncated and truncation is allowed
4248dcc1a9dSDamien Le Moal 	 * only down to a 0 size, which is equivalent to a zone reset, and to
4258dcc1a9dSDamien Le Moal 	 * the maximum file size, which is equivalent to a zone finish.
4268dcc1a9dSDamien Le Moal 	 */
4278dcc1a9dSDamien Le Moal 	if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
4288dcc1a9dSDamien Le Moal 		return -EPERM;
4298dcc1a9dSDamien Le Moal 
4308dcc1a9dSDamien Le Moal 	if (!isize)
4318dcc1a9dSDamien Le Moal 		op = REQ_OP_ZONE_RESET;
4328dcc1a9dSDamien Le Moal 	else if (isize == zi->i_max_size)
4338dcc1a9dSDamien Le Moal 		op = REQ_OP_ZONE_FINISH;
4348dcc1a9dSDamien Le Moal 	else
4358dcc1a9dSDamien Le Moal 		return -EPERM;
4368dcc1a9dSDamien Le Moal 
4378dcc1a9dSDamien Le Moal 	inode_dio_wait(inode);
4388dcc1a9dSDamien Le Moal 
4398dcc1a9dSDamien Le Moal 	/* Serialize against page faults */
4408dcc1a9dSDamien Le Moal 	down_write(&zi->i_mmap_sem);
4418dcc1a9dSDamien Le Moal 
4428dcc1a9dSDamien Le Moal 	/* Serialize against zonefs_iomap_begin() */
4438dcc1a9dSDamien Le Moal 	mutex_lock(&zi->i_truncate_mutex);
4448dcc1a9dSDamien Le Moal 
4458dcc1a9dSDamien Le Moal 	old_isize = i_size_read(inode);
4468dcc1a9dSDamien Le Moal 	if (isize == old_isize)
4478dcc1a9dSDamien Le Moal 		goto unlock;
4488dcc1a9dSDamien Le Moal 
4495498d5f9SJohannes Thumshirn 	ret = zonefs_zone_mgmt(inode, op);
4505498d5f9SJohannes Thumshirn 	if (ret)
4518dcc1a9dSDamien Le Moal 		goto unlock;
4528dcc1a9dSDamien Le Moal 
453b5c00e97SJohannes Thumshirn 	/*
454b5c00e97SJohannes Thumshirn 	 * If the mount option ZONEFS_MNTOPT_EXPLICIT_OPEN is set,
455b5c00e97SJohannes Thumshirn 	 * take care of open zones.
456b5c00e97SJohannes Thumshirn 	 */
457b5c00e97SJohannes Thumshirn 	if (zi->i_flags & ZONEFS_ZONE_OPEN) {
458b5c00e97SJohannes Thumshirn 		/*
459b5c00e97SJohannes Thumshirn 		 * Truncating a zone to EMPTY or FULL is the equivalent of
460b5c00e97SJohannes Thumshirn 		 * closing the zone. For a truncation to 0, we need to
461b5c00e97SJohannes Thumshirn 		 * re-open the zone to ensure new writes can be processed.
462b5c00e97SJohannes Thumshirn 		 * For a truncation to the maximum file size, the zone is
463b5c00e97SJohannes Thumshirn 		 * closed and writes cannot be accepted anymore, so clear
464b5c00e97SJohannes Thumshirn 		 * the open flag.
465b5c00e97SJohannes Thumshirn 		 */
466b5c00e97SJohannes Thumshirn 		if (!isize)
467b5c00e97SJohannes Thumshirn 			ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_OPEN);
468b5c00e97SJohannes Thumshirn 		else
469b5c00e97SJohannes Thumshirn 			zi->i_flags &= ~ZONEFS_ZONE_OPEN;
470b5c00e97SJohannes Thumshirn 	}
471b5c00e97SJohannes Thumshirn 
4728dcc1a9dSDamien Le Moal 	zonefs_update_stats(inode, isize);
4738dcc1a9dSDamien Le Moal 	truncate_setsize(inode, isize);
4748dcc1a9dSDamien Le Moal 	zi->i_wpoffset = isize;
4758dcc1a9dSDamien Le Moal 
4768dcc1a9dSDamien Le Moal unlock:
4778dcc1a9dSDamien Le Moal 	mutex_unlock(&zi->i_truncate_mutex);
4788dcc1a9dSDamien Le Moal 	up_write(&zi->i_mmap_sem);
4798dcc1a9dSDamien Le Moal 
4808dcc1a9dSDamien Le Moal 	return ret;
4818dcc1a9dSDamien Le Moal }
4828dcc1a9dSDamien Le Moal 
4838dcc1a9dSDamien Le Moal static int zonefs_inode_setattr(struct dentry *dentry, struct iattr *iattr)
4848dcc1a9dSDamien Le Moal {
4858dcc1a9dSDamien Le Moal 	struct inode *inode = d_inode(dentry);
4868dcc1a9dSDamien Le Moal 	int ret;
4878dcc1a9dSDamien Le Moal 
4888dcc1a9dSDamien Le Moal 	if (unlikely(IS_IMMUTABLE(inode)))
4898dcc1a9dSDamien Le Moal 		return -EPERM;
4908dcc1a9dSDamien Le Moal 
4918dcc1a9dSDamien Le Moal 	ret = setattr_prepare(dentry, iattr);
4928dcc1a9dSDamien Le Moal 	if (ret)
4938dcc1a9dSDamien Le Moal 		return ret;
4948dcc1a9dSDamien Le Moal 
4958dcc1a9dSDamien Le Moal 	/*
4968dcc1a9dSDamien Le Moal 	 * Since files and directories cannot be created nor deleted, do not
4978dcc1a9dSDamien Le Moal 	 * allow setting any write attributes on the sub-directories grouping
4988dcc1a9dSDamien Le Moal 	 * files by zone type.
4998dcc1a9dSDamien Le Moal 	 */
5008dcc1a9dSDamien Le Moal 	if ((iattr->ia_valid & ATTR_MODE) && S_ISDIR(inode->i_mode) &&
5018dcc1a9dSDamien Le Moal 	    (iattr->ia_mode & 0222))
5028dcc1a9dSDamien Le Moal 		return -EPERM;
5038dcc1a9dSDamien Le Moal 
5048dcc1a9dSDamien Le Moal 	if (((iattr->ia_valid & ATTR_UID) &&
5058dcc1a9dSDamien Le Moal 	     !uid_eq(iattr->ia_uid, inode->i_uid)) ||
5068dcc1a9dSDamien Le Moal 	    ((iattr->ia_valid & ATTR_GID) &&
5078dcc1a9dSDamien Le Moal 	     !gid_eq(iattr->ia_gid, inode->i_gid))) {
5088dcc1a9dSDamien Le Moal 		ret = dquot_transfer(inode, iattr);
5098dcc1a9dSDamien Le Moal 		if (ret)
5108dcc1a9dSDamien Le Moal 			return ret;
5118dcc1a9dSDamien Le Moal 	}
5128dcc1a9dSDamien Le Moal 
5138dcc1a9dSDamien Le Moal 	if (iattr->ia_valid & ATTR_SIZE) {
5148dcc1a9dSDamien Le Moal 		ret = zonefs_file_truncate(inode, iattr->ia_size);
5158dcc1a9dSDamien Le Moal 		if (ret)
5168dcc1a9dSDamien Le Moal 			return ret;
5178dcc1a9dSDamien Le Moal 	}
5188dcc1a9dSDamien Le Moal 
5198dcc1a9dSDamien Le Moal 	setattr_copy(inode, iattr);
5208dcc1a9dSDamien Le Moal 
5218dcc1a9dSDamien Le Moal 	return 0;
5228dcc1a9dSDamien Le Moal }
5238dcc1a9dSDamien Le Moal 
5248dcc1a9dSDamien Le Moal static const struct inode_operations zonefs_file_inode_operations = {
5258dcc1a9dSDamien Le Moal 	.setattr	= zonefs_inode_setattr,
5268dcc1a9dSDamien Le Moal };
5278dcc1a9dSDamien Le Moal 
5288dcc1a9dSDamien Le Moal static int zonefs_file_fsync(struct file *file, loff_t start, loff_t end,
5298dcc1a9dSDamien Le Moal 			     int datasync)
5308dcc1a9dSDamien Le Moal {
5318dcc1a9dSDamien Le Moal 	struct inode *inode = file_inode(file);
5328dcc1a9dSDamien Le Moal 	int ret = 0;
5338dcc1a9dSDamien Le Moal 
5348dcc1a9dSDamien Le Moal 	if (unlikely(IS_IMMUTABLE(inode)))
5358dcc1a9dSDamien Le Moal 		return -EPERM;
5368dcc1a9dSDamien Le Moal 
5378dcc1a9dSDamien Le Moal 	/*
5388dcc1a9dSDamien Le Moal 	 * Since only direct writes are allowed in sequential files, page cache
5398dcc1a9dSDamien Le Moal 	 * flush is needed only for conventional zone files.
5408dcc1a9dSDamien Le Moal 	 */
5418dcc1a9dSDamien Le Moal 	if (ZONEFS_I(inode)->i_ztype == ZONEFS_ZTYPE_CNV)
5428dcc1a9dSDamien Le Moal 		ret = file_write_and_wait_range(file, start, end);
5438dcc1a9dSDamien Le Moal 	if (!ret)
544c6bf3f0eSChristoph Hellwig 		ret = blkdev_issue_flush(inode->i_sb->s_bdev);
5458dcc1a9dSDamien Le Moal 
5468dcc1a9dSDamien Le Moal 	if (ret)
5478dcc1a9dSDamien Le Moal 		zonefs_io_error(inode, true);
5488dcc1a9dSDamien Le Moal 
5498dcc1a9dSDamien Le Moal 	return ret;
5508dcc1a9dSDamien Le Moal }
5518dcc1a9dSDamien Le Moal 
5528dcc1a9dSDamien Le Moal static vm_fault_t zonefs_filemap_fault(struct vm_fault *vmf)
5538dcc1a9dSDamien Le Moal {
5548dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(file_inode(vmf->vma->vm_file));
5558dcc1a9dSDamien Le Moal 	vm_fault_t ret;
5568dcc1a9dSDamien Le Moal 
5578dcc1a9dSDamien Le Moal 	down_read(&zi->i_mmap_sem);
5588dcc1a9dSDamien Le Moal 	ret = filemap_fault(vmf);
5598dcc1a9dSDamien Le Moal 	up_read(&zi->i_mmap_sem);
5608dcc1a9dSDamien Le Moal 
5618dcc1a9dSDamien Le Moal 	return ret;
5628dcc1a9dSDamien Le Moal }
5638dcc1a9dSDamien Le Moal 
5648dcc1a9dSDamien Le Moal static vm_fault_t zonefs_filemap_page_mkwrite(struct vm_fault *vmf)
5658dcc1a9dSDamien Le Moal {
5668dcc1a9dSDamien Le Moal 	struct inode *inode = file_inode(vmf->vma->vm_file);
5678dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
5688dcc1a9dSDamien Le Moal 	vm_fault_t ret;
5698dcc1a9dSDamien Le Moal 
5708dcc1a9dSDamien Le Moal 	if (unlikely(IS_IMMUTABLE(inode)))
5718dcc1a9dSDamien Le Moal 		return VM_FAULT_SIGBUS;
5728dcc1a9dSDamien Le Moal 
5738dcc1a9dSDamien Le Moal 	/*
5748dcc1a9dSDamien Le Moal 	 * Sanity check: only conventional zone files can have shared
5758dcc1a9dSDamien Le Moal 	 * writeable mappings.
5768dcc1a9dSDamien Le Moal 	 */
5778dcc1a9dSDamien Le Moal 	if (WARN_ON_ONCE(zi->i_ztype != ZONEFS_ZTYPE_CNV))
5788dcc1a9dSDamien Le Moal 		return VM_FAULT_NOPAGE;
5798dcc1a9dSDamien Le Moal 
5808dcc1a9dSDamien Le Moal 	sb_start_pagefault(inode->i_sb);
5818dcc1a9dSDamien Le Moal 	file_update_time(vmf->vma->vm_file);
5828dcc1a9dSDamien Le Moal 
5838dcc1a9dSDamien Le Moal 	/* Serialize against truncates */
5848dcc1a9dSDamien Le Moal 	down_read(&zi->i_mmap_sem);
5858dcc1a9dSDamien Le Moal 	ret = iomap_page_mkwrite(vmf, &zonefs_iomap_ops);
5868dcc1a9dSDamien Le Moal 	up_read(&zi->i_mmap_sem);
5878dcc1a9dSDamien Le Moal 
5888dcc1a9dSDamien Le Moal 	sb_end_pagefault(inode->i_sb);
5898dcc1a9dSDamien Le Moal 	return ret;
5908dcc1a9dSDamien Le Moal }
5918dcc1a9dSDamien Le Moal 
5928dcc1a9dSDamien Le Moal static const struct vm_operations_struct zonefs_file_vm_ops = {
5938dcc1a9dSDamien Le Moal 	.fault		= zonefs_filemap_fault,
5948dcc1a9dSDamien Le Moal 	.map_pages	= filemap_map_pages,
5958dcc1a9dSDamien Le Moal 	.page_mkwrite	= zonefs_filemap_page_mkwrite,
5968dcc1a9dSDamien Le Moal };
5978dcc1a9dSDamien Le Moal 
5988dcc1a9dSDamien Le Moal static int zonefs_file_mmap(struct file *file, struct vm_area_struct *vma)
5998dcc1a9dSDamien Le Moal {
6008dcc1a9dSDamien Le Moal 	/*
6018dcc1a9dSDamien Le Moal 	 * Conventional zones accept random writes, so their files can support
6028dcc1a9dSDamien Le Moal 	 * shared writable mappings. For sequential zone files, only read
6038dcc1a9dSDamien Le Moal 	 * mappings are possible since there are no guarantees for write
6048dcc1a9dSDamien Le Moal 	 * ordering between msync() and page cache writeback.
6058dcc1a9dSDamien Le Moal 	 */
6068dcc1a9dSDamien Le Moal 	if (ZONEFS_I(file_inode(file))->i_ztype == ZONEFS_ZTYPE_SEQ &&
6078dcc1a9dSDamien Le Moal 	    (vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
6088dcc1a9dSDamien Le Moal 		return -EINVAL;
6098dcc1a9dSDamien Le Moal 
6108dcc1a9dSDamien Le Moal 	file_accessed(file);
6118dcc1a9dSDamien Le Moal 	vma->vm_ops = &zonefs_file_vm_ops;
6128dcc1a9dSDamien Le Moal 
6138dcc1a9dSDamien Le Moal 	return 0;
6148dcc1a9dSDamien Le Moal }
6158dcc1a9dSDamien Le Moal 
6168dcc1a9dSDamien Le Moal static loff_t zonefs_file_llseek(struct file *file, loff_t offset, int whence)
6178dcc1a9dSDamien Le Moal {
6188dcc1a9dSDamien Le Moal 	loff_t isize = i_size_read(file_inode(file));
6198dcc1a9dSDamien Le Moal 
6208dcc1a9dSDamien Le Moal 	/*
6218dcc1a9dSDamien Le Moal 	 * Seeks are limited to below the zone size for conventional zones
6228dcc1a9dSDamien Le Moal 	 * and below the zone write pointer for sequential zones. In both
6238dcc1a9dSDamien Le Moal 	 * cases, this limit is the inode size.
6248dcc1a9dSDamien Le Moal 	 */
6258dcc1a9dSDamien Le Moal 	return generic_file_llseek_size(file, offset, whence, isize, isize);
6268dcc1a9dSDamien Le Moal }
6278dcc1a9dSDamien Le Moal 
6288dcc1a9dSDamien Le Moal static int zonefs_file_write_dio_end_io(struct kiocb *iocb, ssize_t size,
6298dcc1a9dSDamien Le Moal 					int error, unsigned int flags)
6308dcc1a9dSDamien Le Moal {
6318dcc1a9dSDamien Le Moal 	struct inode *inode = file_inode(iocb->ki_filp);
6328dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
6338dcc1a9dSDamien Le Moal 
6348dcc1a9dSDamien Le Moal 	if (error) {
6358dcc1a9dSDamien Le Moal 		zonefs_io_error(inode, true);
6368dcc1a9dSDamien Le Moal 		return error;
6378dcc1a9dSDamien Le Moal 	}
6388dcc1a9dSDamien Le Moal 
6398dcc1a9dSDamien Le Moal 	if (size && zi->i_ztype != ZONEFS_ZTYPE_CNV) {
6408dcc1a9dSDamien Le Moal 		/*
6418dcc1a9dSDamien Le Moal 		 * Note that we may be seeing completions out of order,
6428dcc1a9dSDamien Le Moal 		 * but that is not a problem since a write completed
6438dcc1a9dSDamien Le Moal 		 * successfully necessarily means that all preceding writes
6448dcc1a9dSDamien Le Moal 		 * were also successful. So we can safely increase the inode
6458dcc1a9dSDamien Le Moal 		 * size to the write end location.
6468dcc1a9dSDamien Le Moal 		 */
6478dcc1a9dSDamien Le Moal 		mutex_lock(&zi->i_truncate_mutex);
6488dcc1a9dSDamien Le Moal 		if (i_size_read(inode) < iocb->ki_pos + size) {
6498dcc1a9dSDamien Le Moal 			zonefs_update_stats(inode, iocb->ki_pos + size);
650b5c00e97SJohannes Thumshirn 			zonefs_i_size_write(inode, iocb->ki_pos + size);
6518dcc1a9dSDamien Le Moal 		}
6528dcc1a9dSDamien Le Moal 		mutex_unlock(&zi->i_truncate_mutex);
6538dcc1a9dSDamien Le Moal 	}
6548dcc1a9dSDamien Le Moal 
6558dcc1a9dSDamien Le Moal 	return 0;
6568dcc1a9dSDamien Le Moal }
6578dcc1a9dSDamien Le Moal 
6588dcc1a9dSDamien Le Moal static const struct iomap_dio_ops zonefs_write_dio_ops = {
6598dcc1a9dSDamien Le Moal 	.end_io			= zonefs_file_write_dio_end_io,
6608dcc1a9dSDamien Le Moal };
6618dcc1a9dSDamien Le Moal 
66202ef12a6SJohannes Thumshirn static ssize_t zonefs_file_dio_append(struct kiocb *iocb, struct iov_iter *from)
66302ef12a6SJohannes Thumshirn {
66402ef12a6SJohannes Thumshirn 	struct inode *inode = file_inode(iocb->ki_filp);
66502ef12a6SJohannes Thumshirn 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
66602ef12a6SJohannes Thumshirn 	struct block_device *bdev = inode->i_sb->s_bdev;
66702ef12a6SJohannes Thumshirn 	unsigned int max;
66802ef12a6SJohannes Thumshirn 	struct bio *bio;
66902ef12a6SJohannes Thumshirn 	ssize_t size;
67002ef12a6SJohannes Thumshirn 	int nr_pages;
67102ef12a6SJohannes Thumshirn 	ssize_t ret;
67202ef12a6SJohannes Thumshirn 
67302ef12a6SJohannes Thumshirn 	max = queue_max_zone_append_sectors(bdev_get_queue(bdev));
67402ef12a6SJohannes Thumshirn 	max = ALIGN_DOWN(max << SECTOR_SHIFT, inode->i_sb->s_blocksize);
67502ef12a6SJohannes Thumshirn 	iov_iter_truncate(from, max);
67602ef12a6SJohannes Thumshirn 
67789ee7237SJohannes Thumshirn 	nr_pages = iov_iter_npages(from, BIO_MAX_PAGES);
67889ee7237SJohannes Thumshirn 	if (!nr_pages)
67989ee7237SJohannes Thumshirn 		return 0;
68089ee7237SJohannes Thumshirn 
681f91ca2a3SChristoph Hellwig 	bio = bio_alloc(GFP_NOFS, nr_pages);
68202ef12a6SJohannes Thumshirn 	if (!bio)
68302ef12a6SJohannes Thumshirn 		return -ENOMEM;
68402ef12a6SJohannes Thumshirn 
68502ef12a6SJohannes Thumshirn 	bio_set_dev(bio, bdev);
68602ef12a6SJohannes Thumshirn 	bio->bi_iter.bi_sector = zi->i_zsector;
68702ef12a6SJohannes Thumshirn 	bio->bi_write_hint = iocb->ki_hint;
68802ef12a6SJohannes Thumshirn 	bio->bi_ioprio = iocb->ki_ioprio;
68902ef12a6SJohannes Thumshirn 	bio->bi_opf = REQ_OP_ZONE_APPEND | REQ_SYNC | REQ_IDLE;
69002ef12a6SJohannes Thumshirn 	if (iocb->ki_flags & IOCB_DSYNC)
69102ef12a6SJohannes Thumshirn 		bio->bi_opf |= REQ_FUA;
69202ef12a6SJohannes Thumshirn 
69302ef12a6SJohannes Thumshirn 	ret = bio_iov_iter_get_pages(bio, from);
6946bea0225SDamien Le Moal 	if (unlikely(ret))
6956bea0225SDamien Le Moal 		goto out_release;
6966bea0225SDamien Le Moal 
69702ef12a6SJohannes Thumshirn 	size = bio->bi_iter.bi_size;
6986bea0225SDamien Le Moal 	task_io_account_write(size);
69902ef12a6SJohannes Thumshirn 
70002ef12a6SJohannes Thumshirn 	if (iocb->ki_flags & IOCB_HIPRI)
70102ef12a6SJohannes Thumshirn 		bio_set_polled(bio, iocb);
70202ef12a6SJohannes Thumshirn 
70302ef12a6SJohannes Thumshirn 	ret = submit_bio_wait(bio);
70402ef12a6SJohannes Thumshirn 
7056bea0225SDamien Le Moal 	zonefs_file_write_dio_end_io(iocb, size, ret, 0);
7066bea0225SDamien Le Moal 
7076bea0225SDamien Le Moal out_release:
7086bea0225SDamien Le Moal 	bio_release_pages(bio, false);
70902ef12a6SJohannes Thumshirn 	bio_put(bio);
71002ef12a6SJohannes Thumshirn 
71102ef12a6SJohannes Thumshirn 	if (ret >= 0) {
71202ef12a6SJohannes Thumshirn 		iocb->ki_pos += size;
71302ef12a6SJohannes Thumshirn 		return size;
71402ef12a6SJohannes Thumshirn 	}
71502ef12a6SJohannes Thumshirn 
71602ef12a6SJohannes Thumshirn 	return ret;
71702ef12a6SJohannes Thumshirn }
71802ef12a6SJohannes Thumshirn 
7198dcc1a9dSDamien Le Moal /*
7208dcc1a9dSDamien Le Moal  * Handle direct writes. For sequential zone files, this is the only possible
7218dcc1a9dSDamien Le Moal  * write path. For these files, check that the user is issuing writes
7228dcc1a9dSDamien Le Moal  * sequentially from the end of the file. This code assumes that the block layer
7238dcc1a9dSDamien Le Moal  * delivers write requests to the device in sequential order. This is always the
7248dcc1a9dSDamien Le Moal  * case if a block IO scheduler implementing the ELEVATOR_F_ZBD_SEQ_WRITE
7258dcc1a9dSDamien Le Moal  * elevator feature is being used (e.g. mq-deadline). The block layer always
7268dcc1a9dSDamien Le Moal  * automatically select such an elevator for zoned block devices during the
7278dcc1a9dSDamien Le Moal  * device initialization.
7288dcc1a9dSDamien Le Moal  */
7298dcc1a9dSDamien Le Moal static ssize_t zonefs_file_dio_write(struct kiocb *iocb, struct iov_iter *from)
7308dcc1a9dSDamien Le Moal {
7318dcc1a9dSDamien Le Moal 	struct inode *inode = file_inode(iocb->ki_filp);
7328dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
7338dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
73402ef12a6SJohannes Thumshirn 	bool sync = is_sync_kiocb(iocb);
73502ef12a6SJohannes Thumshirn 	bool append = false;
7368dcc1a9dSDamien Le Moal 	size_t count;
7378dcc1a9dSDamien Le Moal 	ssize_t ret;
7388dcc1a9dSDamien Le Moal 
7398dcc1a9dSDamien Le Moal 	/*
7407c69eb84SChristoph Hellwig 	 * For async direct IOs to sequential zone files, refuse IOCB_NOWAIT
7418dcc1a9dSDamien Le Moal 	 * as this can cause write reordering (e.g. the first aio gets EAGAIN
7428dcc1a9dSDamien Le Moal 	 * on the inode lock but the second goes through but is now unaligned).
7438dcc1a9dSDamien Le Moal 	 */
74402ef12a6SJohannes Thumshirn 	if (zi->i_ztype == ZONEFS_ZTYPE_SEQ && !sync &&
7457c69eb84SChristoph Hellwig 	    (iocb->ki_flags & IOCB_NOWAIT))
7467c69eb84SChristoph Hellwig 		return -EOPNOTSUPP;
7478dcc1a9dSDamien Le Moal 
7488dcc1a9dSDamien Le Moal 	if (iocb->ki_flags & IOCB_NOWAIT) {
7498dcc1a9dSDamien Le Moal 		if (!inode_trylock(inode))
7508dcc1a9dSDamien Le Moal 			return -EAGAIN;
7518dcc1a9dSDamien Le Moal 	} else {
7528dcc1a9dSDamien Le Moal 		inode_lock(inode);
7538dcc1a9dSDamien Le Moal 	}
7548dcc1a9dSDamien Le Moal 
7558dcc1a9dSDamien Le Moal 	ret = generic_write_checks(iocb, from);
7568dcc1a9dSDamien Le Moal 	if (ret <= 0)
7578dcc1a9dSDamien Le Moal 		goto inode_unlock;
7588dcc1a9dSDamien Le Moal 
7598dcc1a9dSDamien Le Moal 	iov_iter_truncate(from, zi->i_max_size - iocb->ki_pos);
7608dcc1a9dSDamien Le Moal 	count = iov_iter_count(from);
7618dcc1a9dSDamien Le Moal 
7628dcc1a9dSDamien Le Moal 	if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) {
7638dcc1a9dSDamien Le Moal 		ret = -EINVAL;
7648dcc1a9dSDamien Le Moal 		goto inode_unlock;
7658dcc1a9dSDamien Le Moal 	}
7668dcc1a9dSDamien Le Moal 
7678dcc1a9dSDamien Le Moal 	/* Enforce sequential writes (append only) in sequential zones */
76802ef12a6SJohannes Thumshirn 	if (zi->i_ztype == ZONEFS_ZTYPE_SEQ) {
7698dcc1a9dSDamien Le Moal 		mutex_lock(&zi->i_truncate_mutex);
77002ef12a6SJohannes Thumshirn 		if (iocb->ki_pos != zi->i_wpoffset) {
7718dcc1a9dSDamien Le Moal 			mutex_unlock(&zi->i_truncate_mutex);
7728dcc1a9dSDamien Le Moal 			ret = -EINVAL;
7738dcc1a9dSDamien Le Moal 			goto inode_unlock;
7748dcc1a9dSDamien Le Moal 		}
7758dcc1a9dSDamien Le Moal 		mutex_unlock(&zi->i_truncate_mutex);
77602ef12a6SJohannes Thumshirn 		append = sync;
77702ef12a6SJohannes Thumshirn 	}
7788dcc1a9dSDamien Le Moal 
77902ef12a6SJohannes Thumshirn 	if (append)
78002ef12a6SJohannes Thumshirn 		ret = zonefs_file_dio_append(iocb, from);
78102ef12a6SJohannes Thumshirn 	else
7828dcc1a9dSDamien Le Moal 		ret = iomap_dio_rw(iocb, from, &zonefs_iomap_ops,
78302ef12a6SJohannes Thumshirn 				   &zonefs_write_dio_ops, sync);
7848dcc1a9dSDamien Le Moal 	if (zi->i_ztype == ZONEFS_ZTYPE_SEQ &&
7858dcc1a9dSDamien Le Moal 	    (ret > 0 || ret == -EIOCBQUEUED)) {
7868dcc1a9dSDamien Le Moal 		if (ret > 0)
7878dcc1a9dSDamien Le Moal 			count = ret;
7888dcc1a9dSDamien Le Moal 		mutex_lock(&zi->i_truncate_mutex);
7898dcc1a9dSDamien Le Moal 		zi->i_wpoffset += count;
7908dcc1a9dSDamien Le Moal 		mutex_unlock(&zi->i_truncate_mutex);
7918dcc1a9dSDamien Le Moal 	}
7928dcc1a9dSDamien Le Moal 
7938dcc1a9dSDamien Le Moal inode_unlock:
7948dcc1a9dSDamien Le Moal 	inode_unlock(inode);
7958dcc1a9dSDamien Le Moal 
7968dcc1a9dSDamien Le Moal 	return ret;
7978dcc1a9dSDamien Le Moal }
7988dcc1a9dSDamien Le Moal 
7998dcc1a9dSDamien Le Moal static ssize_t zonefs_file_buffered_write(struct kiocb *iocb,
8008dcc1a9dSDamien Le Moal 					  struct iov_iter *from)
8018dcc1a9dSDamien Le Moal {
8028dcc1a9dSDamien Le Moal 	struct inode *inode = file_inode(iocb->ki_filp);
8038dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
8048dcc1a9dSDamien Le Moal 	ssize_t ret;
8058dcc1a9dSDamien Le Moal 
8068dcc1a9dSDamien Le Moal 	/*
8078dcc1a9dSDamien Le Moal 	 * Direct IO writes are mandatory for sequential zone files so that the
8088dcc1a9dSDamien Le Moal 	 * write IO issuing order is preserved.
8098dcc1a9dSDamien Le Moal 	 */
8108dcc1a9dSDamien Le Moal 	if (zi->i_ztype != ZONEFS_ZTYPE_CNV)
8118dcc1a9dSDamien Le Moal 		return -EIO;
8128dcc1a9dSDamien Le Moal 
8138dcc1a9dSDamien Le Moal 	if (iocb->ki_flags & IOCB_NOWAIT) {
8148dcc1a9dSDamien Le Moal 		if (!inode_trylock(inode))
8158dcc1a9dSDamien Le Moal 			return -EAGAIN;
8168dcc1a9dSDamien Le Moal 	} else {
8178dcc1a9dSDamien Le Moal 		inode_lock(inode);
8188dcc1a9dSDamien Le Moal 	}
8198dcc1a9dSDamien Le Moal 
8208dcc1a9dSDamien Le Moal 	ret = generic_write_checks(iocb, from);
8218dcc1a9dSDamien Le Moal 	if (ret <= 0)
8228dcc1a9dSDamien Le Moal 		goto inode_unlock;
8238dcc1a9dSDamien Le Moal 
8248dcc1a9dSDamien Le Moal 	iov_iter_truncate(from, zi->i_max_size - iocb->ki_pos);
8258dcc1a9dSDamien Le Moal 
8268dcc1a9dSDamien Le Moal 	ret = iomap_file_buffered_write(iocb, from, &zonefs_iomap_ops);
8278dcc1a9dSDamien Le Moal 	if (ret > 0)
8288dcc1a9dSDamien Le Moal 		iocb->ki_pos += ret;
8298dcc1a9dSDamien Le Moal 	else if (ret == -EIO)
8308dcc1a9dSDamien Le Moal 		zonefs_io_error(inode, true);
8318dcc1a9dSDamien Le Moal 
8328dcc1a9dSDamien Le Moal inode_unlock:
8338dcc1a9dSDamien Le Moal 	inode_unlock(inode);
8348dcc1a9dSDamien Le Moal 	if (ret > 0)
8358dcc1a9dSDamien Le Moal 		ret = generic_write_sync(iocb, ret);
8368dcc1a9dSDamien Le Moal 
8378dcc1a9dSDamien Le Moal 	return ret;
8388dcc1a9dSDamien Le Moal }
8398dcc1a9dSDamien Le Moal 
8408dcc1a9dSDamien Le Moal static ssize_t zonefs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
8418dcc1a9dSDamien Le Moal {
8428dcc1a9dSDamien Le Moal 	struct inode *inode = file_inode(iocb->ki_filp);
8438dcc1a9dSDamien Le Moal 
8448dcc1a9dSDamien Le Moal 	if (unlikely(IS_IMMUTABLE(inode)))
8458dcc1a9dSDamien Le Moal 		return -EPERM;
8468dcc1a9dSDamien Le Moal 
8478dcc1a9dSDamien Le Moal 	if (sb_rdonly(inode->i_sb))
8488dcc1a9dSDamien Le Moal 		return -EROFS;
8498dcc1a9dSDamien Le Moal 
8508dcc1a9dSDamien Le Moal 	/* Write operations beyond the zone size are not allowed */
8518dcc1a9dSDamien Le Moal 	if (iocb->ki_pos >= ZONEFS_I(inode)->i_max_size)
8528dcc1a9dSDamien Le Moal 		return -EFBIG;
8538dcc1a9dSDamien Le Moal 
85460263d58SChristoph Hellwig 	if (iocb->ki_flags & IOCB_DIRECT) {
85560263d58SChristoph Hellwig 		ssize_t ret = zonefs_file_dio_write(iocb, from);
85660263d58SChristoph Hellwig 		if (ret != -ENOTBLK)
85760263d58SChristoph Hellwig 			return ret;
85860263d58SChristoph Hellwig 	}
8598dcc1a9dSDamien Le Moal 
8608dcc1a9dSDamien Le Moal 	return zonefs_file_buffered_write(iocb, from);
8618dcc1a9dSDamien Le Moal }
8628dcc1a9dSDamien Le Moal 
8638dcc1a9dSDamien Le Moal static int zonefs_file_read_dio_end_io(struct kiocb *iocb, ssize_t size,
8648dcc1a9dSDamien Le Moal 				       int error, unsigned int flags)
8658dcc1a9dSDamien Le Moal {
8668dcc1a9dSDamien Le Moal 	if (error) {
8678dcc1a9dSDamien Le Moal 		zonefs_io_error(file_inode(iocb->ki_filp), false);
8688dcc1a9dSDamien Le Moal 		return error;
8698dcc1a9dSDamien Le Moal 	}
8708dcc1a9dSDamien Le Moal 
8718dcc1a9dSDamien Le Moal 	return 0;
8728dcc1a9dSDamien Le Moal }
8738dcc1a9dSDamien Le Moal 
8748dcc1a9dSDamien Le Moal static const struct iomap_dio_ops zonefs_read_dio_ops = {
8758dcc1a9dSDamien Le Moal 	.end_io			= zonefs_file_read_dio_end_io,
8768dcc1a9dSDamien Le Moal };
8778dcc1a9dSDamien Le Moal 
8788dcc1a9dSDamien Le Moal static ssize_t zonefs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
8798dcc1a9dSDamien Le Moal {
8808dcc1a9dSDamien Le Moal 	struct inode *inode = file_inode(iocb->ki_filp);
8818dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
8828dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
8838dcc1a9dSDamien Le Moal 	loff_t isize;
8848dcc1a9dSDamien Le Moal 	ssize_t ret;
8858dcc1a9dSDamien Le Moal 
8868dcc1a9dSDamien Le Moal 	/* Offline zones cannot be read */
8878dcc1a9dSDamien Le Moal 	if (unlikely(IS_IMMUTABLE(inode) && !(inode->i_mode & 0777)))
8888dcc1a9dSDamien Le Moal 		return -EPERM;
8898dcc1a9dSDamien Le Moal 
8908dcc1a9dSDamien Le Moal 	if (iocb->ki_pos >= zi->i_max_size)
8918dcc1a9dSDamien Le Moal 		return 0;
8928dcc1a9dSDamien Le Moal 
8938dcc1a9dSDamien Le Moal 	if (iocb->ki_flags & IOCB_NOWAIT) {
8948dcc1a9dSDamien Le Moal 		if (!inode_trylock_shared(inode))
8958dcc1a9dSDamien Le Moal 			return -EAGAIN;
8968dcc1a9dSDamien Le Moal 	} else {
8978dcc1a9dSDamien Le Moal 		inode_lock_shared(inode);
8988dcc1a9dSDamien Le Moal 	}
8998dcc1a9dSDamien Le Moal 
9008dcc1a9dSDamien Le Moal 	/* Limit read operations to written data */
9018dcc1a9dSDamien Le Moal 	mutex_lock(&zi->i_truncate_mutex);
9028dcc1a9dSDamien Le Moal 	isize = i_size_read(inode);
9038dcc1a9dSDamien Le Moal 	if (iocb->ki_pos >= isize) {
9048dcc1a9dSDamien Le Moal 		mutex_unlock(&zi->i_truncate_mutex);
9058dcc1a9dSDamien Le Moal 		ret = 0;
9068dcc1a9dSDamien Le Moal 		goto inode_unlock;
9078dcc1a9dSDamien Le Moal 	}
9088dcc1a9dSDamien Le Moal 	iov_iter_truncate(to, isize - iocb->ki_pos);
9098dcc1a9dSDamien Le Moal 	mutex_unlock(&zi->i_truncate_mutex);
9108dcc1a9dSDamien Le Moal 
9118dcc1a9dSDamien Le Moal 	if (iocb->ki_flags & IOCB_DIRECT) {
9128dcc1a9dSDamien Le Moal 		size_t count = iov_iter_count(to);
9138dcc1a9dSDamien Le Moal 
9148dcc1a9dSDamien Le Moal 		if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) {
9158dcc1a9dSDamien Le Moal 			ret = -EINVAL;
9168dcc1a9dSDamien Le Moal 			goto inode_unlock;
9178dcc1a9dSDamien Le Moal 		}
9188dcc1a9dSDamien Le Moal 		file_accessed(iocb->ki_filp);
9198dcc1a9dSDamien Le Moal 		ret = iomap_dio_rw(iocb, to, &zonefs_iomap_ops,
9208dcc1a9dSDamien Le Moal 				   &zonefs_read_dio_ops, is_sync_kiocb(iocb));
9218dcc1a9dSDamien Le Moal 	} else {
9228dcc1a9dSDamien Le Moal 		ret = generic_file_read_iter(iocb, to);
9238dcc1a9dSDamien Le Moal 		if (ret == -EIO)
9248dcc1a9dSDamien Le Moal 			zonefs_io_error(inode, false);
9258dcc1a9dSDamien Le Moal 	}
9268dcc1a9dSDamien Le Moal 
9278dcc1a9dSDamien Le Moal inode_unlock:
9288dcc1a9dSDamien Le Moal 	inode_unlock_shared(inode);
9298dcc1a9dSDamien Le Moal 
9308dcc1a9dSDamien Le Moal 	return ret;
9318dcc1a9dSDamien Le Moal }
9328dcc1a9dSDamien Le Moal 
933b5c00e97SJohannes Thumshirn static inline bool zonefs_file_use_exp_open(struct inode *inode, struct file *file)
934b5c00e97SJohannes Thumshirn {
935b5c00e97SJohannes Thumshirn 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
936b5c00e97SJohannes Thumshirn 	struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
937b5c00e97SJohannes Thumshirn 
938b5c00e97SJohannes Thumshirn 	if (!(sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN))
939b5c00e97SJohannes Thumshirn 		return false;
940b5c00e97SJohannes Thumshirn 
941b5c00e97SJohannes Thumshirn 	if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
942b5c00e97SJohannes Thumshirn 		return false;
943b5c00e97SJohannes Thumshirn 
944b5c00e97SJohannes Thumshirn 	if (!(file->f_mode & FMODE_WRITE))
945b5c00e97SJohannes Thumshirn 		return false;
946b5c00e97SJohannes Thumshirn 
947b5c00e97SJohannes Thumshirn 	return true;
948b5c00e97SJohannes Thumshirn }
949b5c00e97SJohannes Thumshirn 
950b5c00e97SJohannes Thumshirn static int zonefs_open_zone(struct inode *inode)
951b5c00e97SJohannes Thumshirn {
952b5c00e97SJohannes Thumshirn 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
953b5c00e97SJohannes Thumshirn 	struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
954b5c00e97SJohannes Thumshirn 	int ret = 0;
955b5c00e97SJohannes Thumshirn 
956b5c00e97SJohannes Thumshirn 	mutex_lock(&zi->i_truncate_mutex);
957b5c00e97SJohannes Thumshirn 
958b5c00e97SJohannes Thumshirn 	zi->i_wr_refcnt++;
959b5c00e97SJohannes Thumshirn 	if (zi->i_wr_refcnt == 1) {
960b5c00e97SJohannes Thumshirn 
961b5c00e97SJohannes Thumshirn 		if (atomic_inc_return(&sbi->s_open_zones) > sbi->s_max_open_zones) {
962b5c00e97SJohannes Thumshirn 			atomic_dec(&sbi->s_open_zones);
963b5c00e97SJohannes Thumshirn 			ret = -EBUSY;
964b5c00e97SJohannes Thumshirn 			goto unlock;
965b5c00e97SJohannes Thumshirn 		}
966b5c00e97SJohannes Thumshirn 
967b5c00e97SJohannes Thumshirn 		if (i_size_read(inode) < zi->i_max_size) {
968b5c00e97SJohannes Thumshirn 			ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_OPEN);
969b5c00e97SJohannes Thumshirn 			if (ret) {
970b5c00e97SJohannes Thumshirn 				zi->i_wr_refcnt--;
971b5c00e97SJohannes Thumshirn 				atomic_dec(&sbi->s_open_zones);
972b5c00e97SJohannes Thumshirn 				goto unlock;
973b5c00e97SJohannes Thumshirn 			}
974b5c00e97SJohannes Thumshirn 			zi->i_flags |= ZONEFS_ZONE_OPEN;
975b5c00e97SJohannes Thumshirn 		}
976b5c00e97SJohannes Thumshirn 	}
977b5c00e97SJohannes Thumshirn 
978b5c00e97SJohannes Thumshirn unlock:
979b5c00e97SJohannes Thumshirn 	mutex_unlock(&zi->i_truncate_mutex);
980b5c00e97SJohannes Thumshirn 
981b5c00e97SJohannes Thumshirn 	return ret;
982b5c00e97SJohannes Thumshirn }
983b5c00e97SJohannes Thumshirn 
984b5c00e97SJohannes Thumshirn static int zonefs_file_open(struct inode *inode, struct file *file)
985b5c00e97SJohannes Thumshirn {
986b5c00e97SJohannes Thumshirn 	int ret;
987b5c00e97SJohannes Thumshirn 
988b5c00e97SJohannes Thumshirn 	ret = generic_file_open(inode, file);
989b5c00e97SJohannes Thumshirn 	if (ret)
990b5c00e97SJohannes Thumshirn 		return ret;
991b5c00e97SJohannes Thumshirn 
992b5c00e97SJohannes Thumshirn 	if (zonefs_file_use_exp_open(inode, file))
993b5c00e97SJohannes Thumshirn 		return zonefs_open_zone(inode);
994b5c00e97SJohannes Thumshirn 
995b5c00e97SJohannes Thumshirn 	return 0;
996b5c00e97SJohannes Thumshirn }
997b5c00e97SJohannes Thumshirn 
998b5c00e97SJohannes Thumshirn static void zonefs_close_zone(struct inode *inode)
999b5c00e97SJohannes Thumshirn {
1000b5c00e97SJohannes Thumshirn 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
1001b5c00e97SJohannes Thumshirn 	int ret = 0;
1002b5c00e97SJohannes Thumshirn 
1003b5c00e97SJohannes Thumshirn 	mutex_lock(&zi->i_truncate_mutex);
1004b5c00e97SJohannes Thumshirn 	zi->i_wr_refcnt--;
1005b5c00e97SJohannes Thumshirn 	if (!zi->i_wr_refcnt) {
1006b5c00e97SJohannes Thumshirn 		struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
1007b5c00e97SJohannes Thumshirn 		struct super_block *sb = inode->i_sb;
1008b5c00e97SJohannes Thumshirn 
1009b5c00e97SJohannes Thumshirn 		/*
1010b5c00e97SJohannes Thumshirn 		 * If the file zone is full, it is not open anymore and we only
1011b5c00e97SJohannes Thumshirn 		 * need to decrement the open count.
1012b5c00e97SJohannes Thumshirn 		 */
1013b5c00e97SJohannes Thumshirn 		if (!(zi->i_flags & ZONEFS_ZONE_OPEN))
1014b5c00e97SJohannes Thumshirn 			goto dec;
1015b5c00e97SJohannes Thumshirn 
1016b5c00e97SJohannes Thumshirn 		ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_CLOSE);
1017b5c00e97SJohannes Thumshirn 		if (ret) {
1018b5c00e97SJohannes Thumshirn 			__zonefs_io_error(inode, false);
1019b5c00e97SJohannes Thumshirn 			/*
1020b5c00e97SJohannes Thumshirn 			 * Leaving zones explicitly open may lead to a state
1021b5c00e97SJohannes Thumshirn 			 * where most zones cannot be written (zone resources
1022b5c00e97SJohannes Thumshirn 			 * exhausted). So take preventive action by remounting
1023b5c00e97SJohannes Thumshirn 			 * read-only.
1024b5c00e97SJohannes Thumshirn 			 */
1025b5c00e97SJohannes Thumshirn 			if (zi->i_flags & ZONEFS_ZONE_OPEN &&
1026b5c00e97SJohannes Thumshirn 			    !(sb->s_flags & SB_RDONLY)) {
1027b5c00e97SJohannes Thumshirn 				zonefs_warn(sb, "closing zone failed, remounting filesystem read-only\n");
1028b5c00e97SJohannes Thumshirn 				sb->s_flags |= SB_RDONLY;
1029b5c00e97SJohannes Thumshirn 			}
1030b5c00e97SJohannes Thumshirn 		}
1031b5c00e97SJohannes Thumshirn 		zi->i_flags &= ~ZONEFS_ZONE_OPEN;
1032b5c00e97SJohannes Thumshirn dec:
1033b5c00e97SJohannes Thumshirn 		atomic_dec(&sbi->s_open_zones);
1034b5c00e97SJohannes Thumshirn 	}
1035b5c00e97SJohannes Thumshirn 	mutex_unlock(&zi->i_truncate_mutex);
1036b5c00e97SJohannes Thumshirn }
1037b5c00e97SJohannes Thumshirn 
1038b5c00e97SJohannes Thumshirn static int zonefs_file_release(struct inode *inode, struct file *file)
1039b5c00e97SJohannes Thumshirn {
1040b5c00e97SJohannes Thumshirn 	/*
1041b5c00e97SJohannes Thumshirn 	 * If we explicitly open a zone we must close it again as well, but the
1042b5c00e97SJohannes Thumshirn 	 * zone management operation can fail (either due to an IO error or as
1043b5c00e97SJohannes Thumshirn 	 * the zone has gone offline or read-only). Make sure we don't fail the
1044b5c00e97SJohannes Thumshirn 	 * close(2) for user-space.
1045b5c00e97SJohannes Thumshirn 	 */
1046b5c00e97SJohannes Thumshirn 	if (zonefs_file_use_exp_open(inode, file))
1047b5c00e97SJohannes Thumshirn 		zonefs_close_zone(inode);
1048b5c00e97SJohannes Thumshirn 
1049b5c00e97SJohannes Thumshirn 	return 0;
1050b5c00e97SJohannes Thumshirn }
1051b5c00e97SJohannes Thumshirn 
10528dcc1a9dSDamien Le Moal static const struct file_operations zonefs_file_operations = {
1053b5c00e97SJohannes Thumshirn 	.open		= zonefs_file_open,
1054b5c00e97SJohannes Thumshirn 	.release	= zonefs_file_release,
10558dcc1a9dSDamien Le Moal 	.fsync		= zonefs_file_fsync,
10568dcc1a9dSDamien Le Moal 	.mmap		= zonefs_file_mmap,
10578dcc1a9dSDamien Le Moal 	.llseek		= zonefs_file_llseek,
10588dcc1a9dSDamien Le Moal 	.read_iter	= zonefs_file_read_iter,
10598dcc1a9dSDamien Le Moal 	.write_iter	= zonefs_file_write_iter,
10608dcc1a9dSDamien Le Moal 	.splice_read	= generic_file_splice_read,
10618dcc1a9dSDamien Le Moal 	.splice_write	= iter_file_splice_write,
10628dcc1a9dSDamien Le Moal 	.iopoll		= iomap_dio_iopoll,
10638dcc1a9dSDamien Le Moal };
10648dcc1a9dSDamien Le Moal 
10658dcc1a9dSDamien Le Moal static struct kmem_cache *zonefs_inode_cachep;
10668dcc1a9dSDamien Le Moal 
10678dcc1a9dSDamien Le Moal static struct inode *zonefs_alloc_inode(struct super_block *sb)
10688dcc1a9dSDamien Le Moal {
10698dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi;
10708dcc1a9dSDamien Le Moal 
10718dcc1a9dSDamien Le Moal 	zi = kmem_cache_alloc(zonefs_inode_cachep, GFP_KERNEL);
10728dcc1a9dSDamien Le Moal 	if (!zi)
10738dcc1a9dSDamien Le Moal 		return NULL;
10748dcc1a9dSDamien Le Moal 
10758dcc1a9dSDamien Le Moal 	inode_init_once(&zi->i_vnode);
10768dcc1a9dSDamien Le Moal 	mutex_init(&zi->i_truncate_mutex);
10778dcc1a9dSDamien Le Moal 	init_rwsem(&zi->i_mmap_sem);
1078b5c00e97SJohannes Thumshirn 	zi->i_wr_refcnt = 0;
10798dcc1a9dSDamien Le Moal 
10808dcc1a9dSDamien Le Moal 	return &zi->i_vnode;
10818dcc1a9dSDamien Le Moal }
10828dcc1a9dSDamien Le Moal 
10838dcc1a9dSDamien Le Moal static void zonefs_free_inode(struct inode *inode)
10848dcc1a9dSDamien Le Moal {
10858dcc1a9dSDamien Le Moal 	kmem_cache_free(zonefs_inode_cachep, ZONEFS_I(inode));
10868dcc1a9dSDamien Le Moal }
10878dcc1a9dSDamien Le Moal 
10888dcc1a9dSDamien Le Moal /*
10898dcc1a9dSDamien Le Moal  * File system stat.
10908dcc1a9dSDamien Le Moal  */
10918dcc1a9dSDamien Le Moal static int zonefs_statfs(struct dentry *dentry, struct kstatfs *buf)
10928dcc1a9dSDamien Le Moal {
10938dcc1a9dSDamien Le Moal 	struct super_block *sb = dentry->d_sb;
10948dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
10958dcc1a9dSDamien Le Moal 	enum zonefs_ztype t;
10968dcc1a9dSDamien Le Moal 	u64 fsid;
10978dcc1a9dSDamien Le Moal 
10988dcc1a9dSDamien Le Moal 	buf->f_type = ZONEFS_MAGIC;
10998dcc1a9dSDamien Le Moal 	buf->f_bsize = sb->s_blocksize;
11008dcc1a9dSDamien Le Moal 	buf->f_namelen = ZONEFS_NAME_MAX;
11018dcc1a9dSDamien Le Moal 
11028dcc1a9dSDamien Le Moal 	spin_lock(&sbi->s_lock);
11038dcc1a9dSDamien Le Moal 
11048dcc1a9dSDamien Le Moal 	buf->f_blocks = sbi->s_blocks;
11058dcc1a9dSDamien Le Moal 	if (WARN_ON(sbi->s_used_blocks > sbi->s_blocks))
11068dcc1a9dSDamien Le Moal 		buf->f_bfree = 0;
11078dcc1a9dSDamien Le Moal 	else
11088dcc1a9dSDamien Le Moal 		buf->f_bfree = buf->f_blocks - sbi->s_used_blocks;
11098dcc1a9dSDamien Le Moal 	buf->f_bavail = buf->f_bfree;
11108dcc1a9dSDamien Le Moal 
11118dcc1a9dSDamien Le Moal 	for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) {
11128dcc1a9dSDamien Le Moal 		if (sbi->s_nr_files[t])
11138dcc1a9dSDamien Le Moal 			buf->f_files += sbi->s_nr_files[t] + 1;
11148dcc1a9dSDamien Le Moal 	}
11158dcc1a9dSDamien Le Moal 	buf->f_ffree = 0;
11168dcc1a9dSDamien Le Moal 
11178dcc1a9dSDamien Le Moal 	spin_unlock(&sbi->s_lock);
11188dcc1a9dSDamien Le Moal 
11198dcc1a9dSDamien Le Moal 	fsid = le64_to_cpup((void *)sbi->s_uuid.b) ^
11208dcc1a9dSDamien Le Moal 		le64_to_cpup((void *)sbi->s_uuid.b + sizeof(u64));
11216d1349c7SAl Viro 	buf->f_fsid = u64_to_fsid(fsid);
11228dcc1a9dSDamien Le Moal 
11238dcc1a9dSDamien Le Moal 	return 0;
11248dcc1a9dSDamien Le Moal }
11258dcc1a9dSDamien Le Moal 
11268dcc1a9dSDamien Le Moal enum {
11278dcc1a9dSDamien Le Moal 	Opt_errors_ro, Opt_errors_zro, Opt_errors_zol, Opt_errors_repair,
1128b5c00e97SJohannes Thumshirn 	Opt_explicit_open, Opt_err,
11298dcc1a9dSDamien Le Moal };
11308dcc1a9dSDamien Le Moal 
11318dcc1a9dSDamien Le Moal static const match_table_t tokens = {
11328dcc1a9dSDamien Le Moal 	{ Opt_errors_ro,	"errors=remount-ro"},
11338dcc1a9dSDamien Le Moal 	{ Opt_errors_zro,	"errors=zone-ro"},
11348dcc1a9dSDamien Le Moal 	{ Opt_errors_zol,	"errors=zone-offline"},
11358dcc1a9dSDamien Le Moal 	{ Opt_errors_repair,	"errors=repair"},
1136b5c00e97SJohannes Thumshirn 	{ Opt_explicit_open,	"explicit-open" },
11378dcc1a9dSDamien Le Moal 	{ Opt_err,		NULL}
11388dcc1a9dSDamien Le Moal };
11398dcc1a9dSDamien Le Moal 
11408dcc1a9dSDamien Le Moal static int zonefs_parse_options(struct super_block *sb, char *options)
11418dcc1a9dSDamien Le Moal {
11428dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
11438dcc1a9dSDamien Le Moal 	substring_t args[MAX_OPT_ARGS];
11448dcc1a9dSDamien Le Moal 	char *p;
11458dcc1a9dSDamien Le Moal 
11468dcc1a9dSDamien Le Moal 	if (!options)
11478dcc1a9dSDamien Le Moal 		return 0;
11488dcc1a9dSDamien Le Moal 
11498dcc1a9dSDamien Le Moal 	while ((p = strsep(&options, ",")) != NULL) {
11508dcc1a9dSDamien Le Moal 		int token;
11518dcc1a9dSDamien Le Moal 
11528dcc1a9dSDamien Le Moal 		if (!*p)
11538dcc1a9dSDamien Le Moal 			continue;
11548dcc1a9dSDamien Le Moal 
11558dcc1a9dSDamien Le Moal 		token = match_token(p, tokens, args);
11568dcc1a9dSDamien Le Moal 		switch (token) {
11578dcc1a9dSDamien Le Moal 		case Opt_errors_ro:
11588dcc1a9dSDamien Le Moal 			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
11598dcc1a9dSDamien Le Moal 			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_RO;
11608dcc1a9dSDamien Le Moal 			break;
11618dcc1a9dSDamien Le Moal 		case Opt_errors_zro:
11628dcc1a9dSDamien Le Moal 			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
11638dcc1a9dSDamien Le Moal 			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZRO;
11648dcc1a9dSDamien Le Moal 			break;
11658dcc1a9dSDamien Le Moal 		case Opt_errors_zol:
11668dcc1a9dSDamien Le Moal 			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
11678dcc1a9dSDamien Le Moal 			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZOL;
11688dcc1a9dSDamien Le Moal 			break;
11698dcc1a9dSDamien Le Moal 		case Opt_errors_repair:
11708dcc1a9dSDamien Le Moal 			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
11718dcc1a9dSDamien Le Moal 			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_REPAIR;
11728dcc1a9dSDamien Le Moal 			break;
1173b5c00e97SJohannes Thumshirn 		case Opt_explicit_open:
1174b5c00e97SJohannes Thumshirn 			sbi->s_mount_opts |= ZONEFS_MNTOPT_EXPLICIT_OPEN;
1175b5c00e97SJohannes Thumshirn 			break;
11768dcc1a9dSDamien Le Moal 		default:
11778dcc1a9dSDamien Le Moal 			return -EINVAL;
11788dcc1a9dSDamien Le Moal 		}
11798dcc1a9dSDamien Le Moal 	}
11808dcc1a9dSDamien Le Moal 
11818dcc1a9dSDamien Le Moal 	return 0;
11828dcc1a9dSDamien Le Moal }
11838dcc1a9dSDamien Le Moal 
11848dcc1a9dSDamien Le Moal static int zonefs_show_options(struct seq_file *seq, struct dentry *root)
11858dcc1a9dSDamien Le Moal {
11868dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(root->d_sb);
11878dcc1a9dSDamien Le Moal 
11888dcc1a9dSDamien Le Moal 	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO)
11898dcc1a9dSDamien Le Moal 		seq_puts(seq, ",errors=remount-ro");
11908dcc1a9dSDamien Le Moal 	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO)
11918dcc1a9dSDamien Le Moal 		seq_puts(seq, ",errors=zone-ro");
11928dcc1a9dSDamien Le Moal 	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL)
11938dcc1a9dSDamien Le Moal 		seq_puts(seq, ",errors=zone-offline");
11948dcc1a9dSDamien Le Moal 	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_REPAIR)
11958dcc1a9dSDamien Le Moal 		seq_puts(seq, ",errors=repair");
11968dcc1a9dSDamien Le Moal 
11978dcc1a9dSDamien Le Moal 	return 0;
11988dcc1a9dSDamien Le Moal }
11998dcc1a9dSDamien Le Moal 
12008dcc1a9dSDamien Le Moal static int zonefs_remount(struct super_block *sb, int *flags, char *data)
12018dcc1a9dSDamien Le Moal {
12028dcc1a9dSDamien Le Moal 	sync_filesystem(sb);
12038dcc1a9dSDamien Le Moal 
12048dcc1a9dSDamien Le Moal 	return zonefs_parse_options(sb, data);
12058dcc1a9dSDamien Le Moal }
12068dcc1a9dSDamien Le Moal 
12078dcc1a9dSDamien Le Moal static const struct super_operations zonefs_sops = {
12088dcc1a9dSDamien Le Moal 	.alloc_inode	= zonefs_alloc_inode,
12098dcc1a9dSDamien Le Moal 	.free_inode	= zonefs_free_inode,
12108dcc1a9dSDamien Le Moal 	.statfs		= zonefs_statfs,
12118dcc1a9dSDamien Le Moal 	.remount_fs	= zonefs_remount,
12128dcc1a9dSDamien Le Moal 	.show_options	= zonefs_show_options,
12138dcc1a9dSDamien Le Moal };
12148dcc1a9dSDamien Le Moal 
12158dcc1a9dSDamien Le Moal static const struct inode_operations zonefs_dir_inode_operations = {
12168dcc1a9dSDamien Le Moal 	.lookup		= simple_lookup,
12178dcc1a9dSDamien Le Moal 	.setattr	= zonefs_inode_setattr,
12188dcc1a9dSDamien Le Moal };
12198dcc1a9dSDamien Le Moal 
12208dcc1a9dSDamien Le Moal static void zonefs_init_dir_inode(struct inode *parent, struct inode *inode,
12218dcc1a9dSDamien Le Moal 				  enum zonefs_ztype type)
12228dcc1a9dSDamien Le Moal {
12238dcc1a9dSDamien Le Moal 	struct super_block *sb = parent->i_sb;
12248dcc1a9dSDamien Le Moal 
12258dcc1a9dSDamien Le Moal 	inode->i_ino = blkdev_nr_zones(sb->s_bdev->bd_disk) + type + 1;
12268dcc1a9dSDamien Le Moal 	inode_init_owner(inode, parent, S_IFDIR | 0555);
12278dcc1a9dSDamien Le Moal 	inode->i_op = &zonefs_dir_inode_operations;
12288dcc1a9dSDamien Le Moal 	inode->i_fop = &simple_dir_operations;
12298dcc1a9dSDamien Le Moal 	set_nlink(inode, 2);
12308dcc1a9dSDamien Le Moal 	inc_nlink(parent);
12318dcc1a9dSDamien Le Moal }
12328dcc1a9dSDamien Le Moal 
12338dcc1a9dSDamien Le Moal static void zonefs_init_file_inode(struct inode *inode, struct blk_zone *zone,
12348dcc1a9dSDamien Le Moal 				   enum zonefs_ztype type)
12358dcc1a9dSDamien Le Moal {
12368dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
12378dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
12388dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
12398dcc1a9dSDamien Le Moal 
12408dcc1a9dSDamien Le Moal 	inode->i_ino = zone->start >> sbi->s_zone_sectors_shift;
12418dcc1a9dSDamien Le Moal 	inode->i_mode = S_IFREG | sbi->s_perm;
12428dcc1a9dSDamien Le Moal 
12438dcc1a9dSDamien Le Moal 	zi->i_ztype = type;
12448dcc1a9dSDamien Le Moal 	zi->i_zsector = zone->start;
1245e3c3155bSJohannes Thumshirn 	zi->i_zone_size = zone->len << SECTOR_SHIFT;
1246e3c3155bSJohannes Thumshirn 
12478dcc1a9dSDamien Le Moal 	zi->i_max_size = min_t(loff_t, MAX_LFS_FILESIZE,
1248e3c3155bSJohannes Thumshirn 			       zone->capacity << SECTOR_SHIFT);
1249ccf4ad7dSDamien Le Moal 	zi->i_wpoffset = zonefs_check_zone_condition(inode, zone, true, true);
12508dcc1a9dSDamien Le Moal 
12518dcc1a9dSDamien Le Moal 	inode->i_uid = sbi->s_uid;
12528dcc1a9dSDamien Le Moal 	inode->i_gid = sbi->s_gid;
12538dcc1a9dSDamien Le Moal 	inode->i_size = zi->i_wpoffset;
1254e3c3155bSJohannes Thumshirn 	inode->i_blocks = zi->i_max_size >> SECTOR_SHIFT;
12558dcc1a9dSDamien Le Moal 
12568dcc1a9dSDamien Le Moal 	inode->i_op = &zonefs_file_inode_operations;
12578dcc1a9dSDamien Le Moal 	inode->i_fop = &zonefs_file_operations;
12588dcc1a9dSDamien Le Moal 	inode->i_mapping->a_ops = &zonefs_file_aops;
12598dcc1a9dSDamien Le Moal 
12608dcc1a9dSDamien Le Moal 	sb->s_maxbytes = max(zi->i_max_size, sb->s_maxbytes);
12618dcc1a9dSDamien Le Moal 	sbi->s_blocks += zi->i_max_size >> sb->s_blocksize_bits;
12628dcc1a9dSDamien Le Moal 	sbi->s_used_blocks += zi->i_wpoffset >> sb->s_blocksize_bits;
12638dcc1a9dSDamien Le Moal }
12648dcc1a9dSDamien Le Moal 
12658dcc1a9dSDamien Le Moal static struct dentry *zonefs_create_inode(struct dentry *parent,
12668dcc1a9dSDamien Le Moal 					const char *name, struct blk_zone *zone,
12678dcc1a9dSDamien Le Moal 					enum zonefs_ztype type)
12688dcc1a9dSDamien Le Moal {
12698dcc1a9dSDamien Le Moal 	struct inode *dir = d_inode(parent);
12708dcc1a9dSDamien Le Moal 	struct dentry *dentry;
12718dcc1a9dSDamien Le Moal 	struct inode *inode;
12728dcc1a9dSDamien Le Moal 
12738dcc1a9dSDamien Le Moal 	dentry = d_alloc_name(parent, name);
12748dcc1a9dSDamien Le Moal 	if (!dentry)
12758dcc1a9dSDamien Le Moal 		return NULL;
12768dcc1a9dSDamien Le Moal 
12778dcc1a9dSDamien Le Moal 	inode = new_inode(parent->d_sb);
12788dcc1a9dSDamien Le Moal 	if (!inode)
12798dcc1a9dSDamien Le Moal 		goto dput;
12808dcc1a9dSDamien Le Moal 
12818dcc1a9dSDamien Le Moal 	inode->i_ctime = inode->i_mtime = inode->i_atime = dir->i_ctime;
12828dcc1a9dSDamien Le Moal 	if (zone)
12838dcc1a9dSDamien Le Moal 		zonefs_init_file_inode(inode, zone, type);
12848dcc1a9dSDamien Le Moal 	else
12858dcc1a9dSDamien Le Moal 		zonefs_init_dir_inode(dir, inode, type);
12868dcc1a9dSDamien Le Moal 	d_add(dentry, inode);
12878dcc1a9dSDamien Le Moal 	dir->i_size++;
12888dcc1a9dSDamien Le Moal 
12898dcc1a9dSDamien Le Moal 	return dentry;
12908dcc1a9dSDamien Le Moal 
12918dcc1a9dSDamien Le Moal dput:
12928dcc1a9dSDamien Le Moal 	dput(dentry);
12938dcc1a9dSDamien Le Moal 
12948dcc1a9dSDamien Le Moal 	return NULL;
12958dcc1a9dSDamien Le Moal }
12968dcc1a9dSDamien Le Moal 
12978dcc1a9dSDamien Le Moal struct zonefs_zone_data {
12988dcc1a9dSDamien Le Moal 	struct super_block	*sb;
12998dcc1a9dSDamien Le Moal 	unsigned int		nr_zones[ZONEFS_ZTYPE_MAX];
13008dcc1a9dSDamien Le Moal 	struct blk_zone		*zones;
13018dcc1a9dSDamien Le Moal };
13028dcc1a9dSDamien Le Moal 
13038dcc1a9dSDamien Le Moal /*
13048dcc1a9dSDamien Le Moal  * Create a zone group and populate it with zone files.
13058dcc1a9dSDamien Le Moal  */
13068dcc1a9dSDamien Le Moal static int zonefs_create_zgroup(struct zonefs_zone_data *zd,
13078dcc1a9dSDamien Le Moal 				enum zonefs_ztype type)
13088dcc1a9dSDamien Le Moal {
13098dcc1a9dSDamien Le Moal 	struct super_block *sb = zd->sb;
13108dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
13118dcc1a9dSDamien Le Moal 	struct blk_zone *zone, *next, *end;
13128dcc1a9dSDamien Le Moal 	const char *zgroup_name;
13138dcc1a9dSDamien Le Moal 	char *file_name;
13148dcc1a9dSDamien Le Moal 	struct dentry *dir;
13158dcc1a9dSDamien Le Moal 	unsigned int n = 0;
131601b2651cSDamien Le Moal 	int ret;
13178dcc1a9dSDamien Le Moal 
13188dcc1a9dSDamien Le Moal 	/* If the group is empty, there is nothing to do */
13198dcc1a9dSDamien Le Moal 	if (!zd->nr_zones[type])
13208dcc1a9dSDamien Le Moal 		return 0;
13218dcc1a9dSDamien Le Moal 
13228dcc1a9dSDamien Le Moal 	file_name = kmalloc(ZONEFS_NAME_MAX, GFP_KERNEL);
13238dcc1a9dSDamien Le Moal 	if (!file_name)
13248dcc1a9dSDamien Le Moal 		return -ENOMEM;
13258dcc1a9dSDamien Le Moal 
13268dcc1a9dSDamien Le Moal 	if (type == ZONEFS_ZTYPE_CNV)
13278dcc1a9dSDamien Le Moal 		zgroup_name = "cnv";
13288dcc1a9dSDamien Le Moal 	else
13298dcc1a9dSDamien Le Moal 		zgroup_name = "seq";
13308dcc1a9dSDamien Le Moal 
13318dcc1a9dSDamien Le Moal 	dir = zonefs_create_inode(sb->s_root, zgroup_name, NULL, type);
133201b2651cSDamien Le Moal 	if (!dir) {
133301b2651cSDamien Le Moal 		ret = -ENOMEM;
13348dcc1a9dSDamien Le Moal 		goto free;
133501b2651cSDamien Le Moal 	}
13368dcc1a9dSDamien Le Moal 
13378dcc1a9dSDamien Le Moal 	/*
13388dcc1a9dSDamien Le Moal 	 * The first zone contains the super block: skip it.
13398dcc1a9dSDamien Le Moal 	 */
13408dcc1a9dSDamien Le Moal 	end = zd->zones + blkdev_nr_zones(sb->s_bdev->bd_disk);
13418dcc1a9dSDamien Le Moal 	for (zone = &zd->zones[1]; zone < end; zone = next) {
13428dcc1a9dSDamien Le Moal 
13438dcc1a9dSDamien Le Moal 		next = zone + 1;
13448dcc1a9dSDamien Le Moal 		if (zonefs_zone_type(zone) != type)
13458dcc1a9dSDamien Le Moal 			continue;
13468dcc1a9dSDamien Le Moal 
13478dcc1a9dSDamien Le Moal 		/*
13488dcc1a9dSDamien Le Moal 		 * For conventional zones, contiguous zones can be aggregated
13498dcc1a9dSDamien Le Moal 		 * together to form larger files. Note that this overwrites the
13508dcc1a9dSDamien Le Moal 		 * length of the first zone of the set of contiguous zones
13518dcc1a9dSDamien Le Moal 		 * aggregated together. If one offline or read-only zone is
13528dcc1a9dSDamien Le Moal 		 * found, assume that all zones aggregated have the same
13538dcc1a9dSDamien Le Moal 		 * condition.
13548dcc1a9dSDamien Le Moal 		 */
13558dcc1a9dSDamien Le Moal 		if (type == ZONEFS_ZTYPE_CNV &&
13568dcc1a9dSDamien Le Moal 		    (sbi->s_features & ZONEFS_F_AGGRCNV)) {
13578dcc1a9dSDamien Le Moal 			for (; next < end; next++) {
13588dcc1a9dSDamien Le Moal 				if (zonefs_zone_type(next) != type)
13598dcc1a9dSDamien Le Moal 					break;
13608dcc1a9dSDamien Le Moal 				zone->len += next->len;
1361e3c3155bSJohannes Thumshirn 				zone->capacity += next->capacity;
13628dcc1a9dSDamien Le Moal 				if (next->cond == BLK_ZONE_COND_READONLY &&
13638dcc1a9dSDamien Le Moal 				    zone->cond != BLK_ZONE_COND_OFFLINE)
13648dcc1a9dSDamien Le Moal 					zone->cond = BLK_ZONE_COND_READONLY;
13658dcc1a9dSDamien Le Moal 				else if (next->cond == BLK_ZONE_COND_OFFLINE)
13668dcc1a9dSDamien Le Moal 					zone->cond = BLK_ZONE_COND_OFFLINE;
13678dcc1a9dSDamien Le Moal 			}
1368e3c3155bSJohannes Thumshirn 			if (zone->capacity != zone->len) {
1369e3c3155bSJohannes Thumshirn 				zonefs_err(sb, "Invalid conventional zone capacity\n");
1370e3c3155bSJohannes Thumshirn 				ret = -EINVAL;
1371e3c3155bSJohannes Thumshirn 				goto free;
1372e3c3155bSJohannes Thumshirn 			}
13738dcc1a9dSDamien Le Moal 		}
13748dcc1a9dSDamien Le Moal 
13758dcc1a9dSDamien Le Moal 		/*
13768dcc1a9dSDamien Le Moal 		 * Use the file number within its group as file name.
13778dcc1a9dSDamien Le Moal 		 */
13788dcc1a9dSDamien Le Moal 		snprintf(file_name, ZONEFS_NAME_MAX - 1, "%u", n);
137901b2651cSDamien Le Moal 		if (!zonefs_create_inode(dir, file_name, zone, type)) {
138001b2651cSDamien Le Moal 			ret = -ENOMEM;
13818dcc1a9dSDamien Le Moal 			goto free;
138201b2651cSDamien Le Moal 		}
13838dcc1a9dSDamien Le Moal 
13848dcc1a9dSDamien Le Moal 		n++;
13858dcc1a9dSDamien Le Moal 	}
13868dcc1a9dSDamien Le Moal 
13878dcc1a9dSDamien Le Moal 	zonefs_info(sb, "Zone group \"%s\" has %u file%s\n",
13888dcc1a9dSDamien Le Moal 		    zgroup_name, n, n > 1 ? "s" : "");
13898dcc1a9dSDamien Le Moal 
13908dcc1a9dSDamien Le Moal 	sbi->s_nr_files[type] = n;
13918dcc1a9dSDamien Le Moal 	ret = 0;
13928dcc1a9dSDamien Le Moal 
13938dcc1a9dSDamien Le Moal free:
13948dcc1a9dSDamien Le Moal 	kfree(file_name);
13958dcc1a9dSDamien Le Moal 
13968dcc1a9dSDamien Le Moal 	return ret;
13978dcc1a9dSDamien Le Moal }
13988dcc1a9dSDamien Le Moal 
13998dcc1a9dSDamien Le Moal static int zonefs_get_zone_info_cb(struct blk_zone *zone, unsigned int idx,
14008dcc1a9dSDamien Le Moal 				   void *data)
14018dcc1a9dSDamien Le Moal {
14028dcc1a9dSDamien Le Moal 	struct zonefs_zone_data *zd = data;
14038dcc1a9dSDamien Le Moal 
14048dcc1a9dSDamien Le Moal 	/*
14058dcc1a9dSDamien Le Moal 	 * Count the number of usable zones: the first zone at index 0 contains
14068dcc1a9dSDamien Le Moal 	 * the super block and is ignored.
14078dcc1a9dSDamien Le Moal 	 */
14088dcc1a9dSDamien Le Moal 	switch (zone->type) {
14098dcc1a9dSDamien Le Moal 	case BLK_ZONE_TYPE_CONVENTIONAL:
14108dcc1a9dSDamien Le Moal 		zone->wp = zone->start + zone->len;
14118dcc1a9dSDamien Le Moal 		if (idx)
14128dcc1a9dSDamien Le Moal 			zd->nr_zones[ZONEFS_ZTYPE_CNV]++;
14138dcc1a9dSDamien Le Moal 		break;
14148dcc1a9dSDamien Le Moal 	case BLK_ZONE_TYPE_SEQWRITE_REQ:
14158dcc1a9dSDamien Le Moal 	case BLK_ZONE_TYPE_SEQWRITE_PREF:
14168dcc1a9dSDamien Le Moal 		if (idx)
14178dcc1a9dSDamien Le Moal 			zd->nr_zones[ZONEFS_ZTYPE_SEQ]++;
14188dcc1a9dSDamien Le Moal 		break;
14198dcc1a9dSDamien Le Moal 	default:
14208dcc1a9dSDamien Le Moal 		zonefs_err(zd->sb, "Unsupported zone type 0x%x\n",
14218dcc1a9dSDamien Le Moal 			   zone->type);
14228dcc1a9dSDamien Le Moal 		return -EIO;
14238dcc1a9dSDamien Le Moal 	}
14248dcc1a9dSDamien Le Moal 
14258dcc1a9dSDamien Le Moal 	memcpy(&zd->zones[idx], zone, sizeof(struct blk_zone));
14268dcc1a9dSDamien Le Moal 
14278dcc1a9dSDamien Le Moal 	return 0;
14288dcc1a9dSDamien Le Moal }
14298dcc1a9dSDamien Le Moal 
14308dcc1a9dSDamien Le Moal static int zonefs_get_zone_info(struct zonefs_zone_data *zd)
14318dcc1a9dSDamien Le Moal {
14328dcc1a9dSDamien Le Moal 	struct block_device *bdev = zd->sb->s_bdev;
14338dcc1a9dSDamien Le Moal 	int ret;
14348dcc1a9dSDamien Le Moal 
14358dcc1a9dSDamien Le Moal 	zd->zones = kvcalloc(blkdev_nr_zones(bdev->bd_disk),
14368dcc1a9dSDamien Le Moal 			     sizeof(struct blk_zone), GFP_KERNEL);
14378dcc1a9dSDamien Le Moal 	if (!zd->zones)
14388dcc1a9dSDamien Le Moal 		return -ENOMEM;
14398dcc1a9dSDamien Le Moal 
14408dcc1a9dSDamien Le Moal 	/* Get zones information from the device */
14418dcc1a9dSDamien Le Moal 	ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES,
14428dcc1a9dSDamien Le Moal 				  zonefs_get_zone_info_cb, zd);
14438dcc1a9dSDamien Le Moal 	if (ret < 0) {
14448dcc1a9dSDamien Le Moal 		zonefs_err(zd->sb, "Zone report failed %d\n", ret);
14458dcc1a9dSDamien Le Moal 		return ret;
14468dcc1a9dSDamien Le Moal 	}
14478dcc1a9dSDamien Le Moal 
14488dcc1a9dSDamien Le Moal 	if (ret != blkdev_nr_zones(bdev->bd_disk)) {
14498dcc1a9dSDamien Le Moal 		zonefs_err(zd->sb, "Invalid zone report (%d/%u zones)\n",
14508dcc1a9dSDamien Le Moal 			   ret, blkdev_nr_zones(bdev->bd_disk));
14518dcc1a9dSDamien Le Moal 		return -EIO;
14528dcc1a9dSDamien Le Moal 	}
14538dcc1a9dSDamien Le Moal 
14548dcc1a9dSDamien Le Moal 	return 0;
14558dcc1a9dSDamien Le Moal }
14568dcc1a9dSDamien Le Moal 
14578dcc1a9dSDamien Le Moal static inline void zonefs_cleanup_zone_info(struct zonefs_zone_data *zd)
14588dcc1a9dSDamien Le Moal {
14598dcc1a9dSDamien Le Moal 	kvfree(zd->zones);
14608dcc1a9dSDamien Le Moal }
14618dcc1a9dSDamien Le Moal 
14628dcc1a9dSDamien Le Moal /*
14638dcc1a9dSDamien Le Moal  * Read super block information from the device.
14648dcc1a9dSDamien Le Moal  */
14658dcc1a9dSDamien Le Moal static int zonefs_read_super(struct super_block *sb)
14668dcc1a9dSDamien Le Moal {
14678dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
14688dcc1a9dSDamien Le Moal 	struct zonefs_super *super;
14698dcc1a9dSDamien Le Moal 	u32 crc, stored_crc;
14708dcc1a9dSDamien Le Moal 	struct page *page;
14718dcc1a9dSDamien Le Moal 	struct bio_vec bio_vec;
14728dcc1a9dSDamien Le Moal 	struct bio bio;
14738dcc1a9dSDamien Le Moal 	int ret;
14748dcc1a9dSDamien Le Moal 
14758dcc1a9dSDamien Le Moal 	page = alloc_page(GFP_KERNEL);
14768dcc1a9dSDamien Le Moal 	if (!page)
14778dcc1a9dSDamien Le Moal 		return -ENOMEM;
14788dcc1a9dSDamien Le Moal 
14798dcc1a9dSDamien Le Moal 	bio_init(&bio, &bio_vec, 1);
14808dcc1a9dSDamien Le Moal 	bio.bi_iter.bi_sector = 0;
14818dcc1a9dSDamien Le Moal 	bio.bi_opf = REQ_OP_READ;
14828dcc1a9dSDamien Le Moal 	bio_set_dev(&bio, sb->s_bdev);
14838dcc1a9dSDamien Le Moal 	bio_add_page(&bio, page, PAGE_SIZE, 0);
14848dcc1a9dSDamien Le Moal 
14858dcc1a9dSDamien Le Moal 	ret = submit_bio_wait(&bio);
14868dcc1a9dSDamien Le Moal 	if (ret)
14878dcc1a9dSDamien Le Moal 		goto free_page;
14888dcc1a9dSDamien Le Moal 
14898dcc1a9dSDamien Le Moal 	super = kmap(page);
14908dcc1a9dSDamien Le Moal 
14918dcc1a9dSDamien Le Moal 	ret = -EINVAL;
14928dcc1a9dSDamien Le Moal 	if (le32_to_cpu(super->s_magic) != ZONEFS_MAGIC)
14938dcc1a9dSDamien Le Moal 		goto unmap;
14948dcc1a9dSDamien Le Moal 
14958dcc1a9dSDamien Le Moal 	stored_crc = le32_to_cpu(super->s_crc);
14968dcc1a9dSDamien Le Moal 	super->s_crc = 0;
14978dcc1a9dSDamien Le Moal 	crc = crc32(~0U, (unsigned char *)super, sizeof(struct zonefs_super));
14988dcc1a9dSDamien Le Moal 	if (crc != stored_crc) {
14998dcc1a9dSDamien Le Moal 		zonefs_err(sb, "Invalid checksum (Expected 0x%08x, got 0x%08x)",
15008dcc1a9dSDamien Le Moal 			   crc, stored_crc);
15018dcc1a9dSDamien Le Moal 		goto unmap;
15028dcc1a9dSDamien Le Moal 	}
15038dcc1a9dSDamien Le Moal 
15048dcc1a9dSDamien Le Moal 	sbi->s_features = le64_to_cpu(super->s_features);
15058dcc1a9dSDamien Le Moal 	if (sbi->s_features & ~ZONEFS_F_DEFINED_FEATURES) {
15068dcc1a9dSDamien Le Moal 		zonefs_err(sb, "Unknown features set 0x%llx\n",
15078dcc1a9dSDamien Le Moal 			   sbi->s_features);
15088dcc1a9dSDamien Le Moal 		goto unmap;
15098dcc1a9dSDamien Le Moal 	}
15108dcc1a9dSDamien Le Moal 
15118dcc1a9dSDamien Le Moal 	if (sbi->s_features & ZONEFS_F_UID) {
15128dcc1a9dSDamien Le Moal 		sbi->s_uid = make_kuid(current_user_ns(),
15138dcc1a9dSDamien Le Moal 				       le32_to_cpu(super->s_uid));
15148dcc1a9dSDamien Le Moal 		if (!uid_valid(sbi->s_uid)) {
15158dcc1a9dSDamien Le Moal 			zonefs_err(sb, "Invalid UID feature\n");
15168dcc1a9dSDamien Le Moal 			goto unmap;
15178dcc1a9dSDamien Le Moal 		}
15188dcc1a9dSDamien Le Moal 	}
15198dcc1a9dSDamien Le Moal 
15208dcc1a9dSDamien Le Moal 	if (sbi->s_features & ZONEFS_F_GID) {
15218dcc1a9dSDamien Le Moal 		sbi->s_gid = make_kgid(current_user_ns(),
15228dcc1a9dSDamien Le Moal 				       le32_to_cpu(super->s_gid));
15238dcc1a9dSDamien Le Moal 		if (!gid_valid(sbi->s_gid)) {
15248dcc1a9dSDamien Le Moal 			zonefs_err(sb, "Invalid GID feature\n");
15258dcc1a9dSDamien Le Moal 			goto unmap;
15268dcc1a9dSDamien Le Moal 		}
15278dcc1a9dSDamien Le Moal 	}
15288dcc1a9dSDamien Le Moal 
15298dcc1a9dSDamien Le Moal 	if (sbi->s_features & ZONEFS_F_PERM)
15308dcc1a9dSDamien Le Moal 		sbi->s_perm = le32_to_cpu(super->s_perm);
15318dcc1a9dSDamien Le Moal 
15328dcc1a9dSDamien Le Moal 	if (memchr_inv(super->s_reserved, 0, sizeof(super->s_reserved))) {
15338dcc1a9dSDamien Le Moal 		zonefs_err(sb, "Reserved area is being used\n");
15348dcc1a9dSDamien Le Moal 		goto unmap;
15358dcc1a9dSDamien Le Moal 	}
15368dcc1a9dSDamien Le Moal 
1537568776f9SAndy Shevchenko 	import_uuid(&sbi->s_uuid, super->s_uuid);
15388dcc1a9dSDamien Le Moal 	ret = 0;
15398dcc1a9dSDamien Le Moal 
15408dcc1a9dSDamien Le Moal unmap:
15418dcc1a9dSDamien Le Moal 	kunmap(page);
15428dcc1a9dSDamien Le Moal free_page:
15438dcc1a9dSDamien Le Moal 	__free_page(page);
15448dcc1a9dSDamien Le Moal 
15458dcc1a9dSDamien Le Moal 	return ret;
15468dcc1a9dSDamien Le Moal }
15478dcc1a9dSDamien Le Moal 
15488dcc1a9dSDamien Le Moal /*
15498dcc1a9dSDamien Le Moal  * Check that the device is zoned. If it is, get the list of zones and create
15508dcc1a9dSDamien Le Moal  * sub-directories and files according to the device zone configuration and
15518dcc1a9dSDamien Le Moal  * format options.
15528dcc1a9dSDamien Le Moal  */
15538dcc1a9dSDamien Le Moal static int zonefs_fill_super(struct super_block *sb, void *data, int silent)
15548dcc1a9dSDamien Le Moal {
15558dcc1a9dSDamien Le Moal 	struct zonefs_zone_data zd;
15568dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi;
15578dcc1a9dSDamien Le Moal 	struct inode *inode;
15588dcc1a9dSDamien Le Moal 	enum zonefs_ztype t;
15598dcc1a9dSDamien Le Moal 	int ret;
15608dcc1a9dSDamien Le Moal 
15618dcc1a9dSDamien Le Moal 	if (!bdev_is_zoned(sb->s_bdev)) {
15628dcc1a9dSDamien Le Moal 		zonefs_err(sb, "Not a zoned block device\n");
15638dcc1a9dSDamien Le Moal 		return -EINVAL;
15648dcc1a9dSDamien Le Moal 	}
15658dcc1a9dSDamien Le Moal 
15668dcc1a9dSDamien Le Moal 	/*
15678dcc1a9dSDamien Le Moal 	 * Initialize super block information: the maximum file size is updated
15688dcc1a9dSDamien Le Moal 	 * when the zone files are created so that the format option
15698dcc1a9dSDamien Le Moal 	 * ZONEFS_F_AGGRCNV which increases the maximum file size of a file
15708dcc1a9dSDamien Le Moal 	 * beyond the zone size is taken into account.
15718dcc1a9dSDamien Le Moal 	 */
15728dcc1a9dSDamien Le Moal 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
15738dcc1a9dSDamien Le Moal 	if (!sbi)
15748dcc1a9dSDamien Le Moal 		return -ENOMEM;
15758dcc1a9dSDamien Le Moal 
15768dcc1a9dSDamien Le Moal 	spin_lock_init(&sbi->s_lock);
15778dcc1a9dSDamien Le Moal 	sb->s_fs_info = sbi;
15788dcc1a9dSDamien Le Moal 	sb->s_magic = ZONEFS_MAGIC;
15798dcc1a9dSDamien Le Moal 	sb->s_maxbytes = 0;
15808dcc1a9dSDamien Le Moal 	sb->s_op = &zonefs_sops;
15818dcc1a9dSDamien Le Moal 	sb->s_time_gran	= 1;
15828dcc1a9dSDamien Le Moal 
15838dcc1a9dSDamien Le Moal 	/*
1584*0f1ba5f5SDamien Le Moal 	 * The block size is set to the device zone write granularity to ensure
1585*0f1ba5f5SDamien Le Moal 	 * that write operations are always aligned according to the device
1586*0f1ba5f5SDamien Le Moal 	 * interface constraints.
15878dcc1a9dSDamien Le Moal 	 */
1588*0f1ba5f5SDamien Le Moal 	sb_set_blocksize(sb, bdev_zone_write_granularity(sb->s_bdev));
15898dcc1a9dSDamien Le Moal 	sbi->s_zone_sectors_shift = ilog2(bdev_zone_sectors(sb->s_bdev));
15908dcc1a9dSDamien Le Moal 	sbi->s_uid = GLOBAL_ROOT_UID;
15918dcc1a9dSDamien Le Moal 	sbi->s_gid = GLOBAL_ROOT_GID;
15928dcc1a9dSDamien Le Moal 	sbi->s_perm = 0640;
15938dcc1a9dSDamien Le Moal 	sbi->s_mount_opts = ZONEFS_MNTOPT_ERRORS_RO;
1594b5c00e97SJohannes Thumshirn 	sbi->s_max_open_zones = bdev_max_open_zones(sb->s_bdev);
1595b5c00e97SJohannes Thumshirn 	atomic_set(&sbi->s_open_zones, 0);
1596b5c00e97SJohannes Thumshirn 	if (!sbi->s_max_open_zones &&
1597b5c00e97SJohannes Thumshirn 	    sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) {
1598b5c00e97SJohannes Thumshirn 		zonefs_info(sb, "No open zones limit. Ignoring explicit_open mount option\n");
1599b5c00e97SJohannes Thumshirn 		sbi->s_mount_opts &= ~ZONEFS_MNTOPT_EXPLICIT_OPEN;
1600b5c00e97SJohannes Thumshirn 	}
16018dcc1a9dSDamien Le Moal 
16028dcc1a9dSDamien Le Moal 	ret = zonefs_read_super(sb);
16038dcc1a9dSDamien Le Moal 	if (ret)
16048dcc1a9dSDamien Le Moal 		return ret;
16058dcc1a9dSDamien Le Moal 
16068dcc1a9dSDamien Le Moal 	ret = zonefs_parse_options(sb, data);
16078dcc1a9dSDamien Le Moal 	if (ret)
16088dcc1a9dSDamien Le Moal 		return ret;
16098dcc1a9dSDamien Le Moal 
16108dcc1a9dSDamien Le Moal 	memset(&zd, 0, sizeof(struct zonefs_zone_data));
16118dcc1a9dSDamien Le Moal 	zd.sb = sb;
16128dcc1a9dSDamien Le Moal 	ret = zonefs_get_zone_info(&zd);
16138dcc1a9dSDamien Le Moal 	if (ret)
16148dcc1a9dSDamien Le Moal 		goto cleanup;
16158dcc1a9dSDamien Le Moal 
16168dcc1a9dSDamien Le Moal 	zonefs_info(sb, "Mounting %u zones",
16178dcc1a9dSDamien Le Moal 		    blkdev_nr_zones(sb->s_bdev->bd_disk));
16188dcc1a9dSDamien Le Moal 
16198dcc1a9dSDamien Le Moal 	/* Create root directory inode */
16208dcc1a9dSDamien Le Moal 	ret = -ENOMEM;
16218dcc1a9dSDamien Le Moal 	inode = new_inode(sb);
16228dcc1a9dSDamien Le Moal 	if (!inode)
16238dcc1a9dSDamien Le Moal 		goto cleanup;
16248dcc1a9dSDamien Le Moal 
16258dcc1a9dSDamien Le Moal 	inode->i_ino = blkdev_nr_zones(sb->s_bdev->bd_disk);
16268dcc1a9dSDamien Le Moal 	inode->i_mode = S_IFDIR | 0555;
16278dcc1a9dSDamien Le Moal 	inode->i_ctime = inode->i_mtime = inode->i_atime = current_time(inode);
16288dcc1a9dSDamien Le Moal 	inode->i_op = &zonefs_dir_inode_operations;
16298dcc1a9dSDamien Le Moal 	inode->i_fop = &simple_dir_operations;
16308dcc1a9dSDamien Le Moal 	set_nlink(inode, 2);
16318dcc1a9dSDamien Le Moal 
16328dcc1a9dSDamien Le Moal 	sb->s_root = d_make_root(inode);
16338dcc1a9dSDamien Le Moal 	if (!sb->s_root)
16348dcc1a9dSDamien Le Moal 		goto cleanup;
16358dcc1a9dSDamien Le Moal 
16368dcc1a9dSDamien Le Moal 	/* Create and populate files in zone groups directories */
16378dcc1a9dSDamien Le Moal 	for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) {
16388dcc1a9dSDamien Le Moal 		ret = zonefs_create_zgroup(&zd, t);
16398dcc1a9dSDamien Le Moal 		if (ret)
16408dcc1a9dSDamien Le Moal 			break;
16418dcc1a9dSDamien Le Moal 	}
16428dcc1a9dSDamien Le Moal 
16438dcc1a9dSDamien Le Moal cleanup:
16448dcc1a9dSDamien Le Moal 	zonefs_cleanup_zone_info(&zd);
16458dcc1a9dSDamien Le Moal 
16468dcc1a9dSDamien Le Moal 	return ret;
16478dcc1a9dSDamien Le Moal }
16488dcc1a9dSDamien Le Moal 
16498dcc1a9dSDamien Le Moal static struct dentry *zonefs_mount(struct file_system_type *fs_type,
16508dcc1a9dSDamien Le Moal 				   int flags, const char *dev_name, void *data)
16518dcc1a9dSDamien Le Moal {
16528dcc1a9dSDamien Le Moal 	return mount_bdev(fs_type, flags, dev_name, data, zonefs_fill_super);
16538dcc1a9dSDamien Le Moal }
16548dcc1a9dSDamien Le Moal 
16558dcc1a9dSDamien Le Moal static void zonefs_kill_super(struct super_block *sb)
16568dcc1a9dSDamien Le Moal {
16578dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
16588dcc1a9dSDamien Le Moal 
16598dcc1a9dSDamien Le Moal 	if (sb->s_root)
16608dcc1a9dSDamien Le Moal 		d_genocide(sb->s_root);
16618dcc1a9dSDamien Le Moal 	kill_block_super(sb);
16628dcc1a9dSDamien Le Moal 	kfree(sbi);
16638dcc1a9dSDamien Le Moal }
16648dcc1a9dSDamien Le Moal 
16658dcc1a9dSDamien Le Moal /*
16668dcc1a9dSDamien Le Moal  * File system definition and registration.
16678dcc1a9dSDamien Le Moal  */
16688dcc1a9dSDamien Le Moal static struct file_system_type zonefs_type = {
16698dcc1a9dSDamien Le Moal 	.owner		= THIS_MODULE,
16708dcc1a9dSDamien Le Moal 	.name		= "zonefs",
16718dcc1a9dSDamien Le Moal 	.mount		= zonefs_mount,
16728dcc1a9dSDamien Le Moal 	.kill_sb	= zonefs_kill_super,
16738dcc1a9dSDamien Le Moal 	.fs_flags	= FS_REQUIRES_DEV,
16748dcc1a9dSDamien Le Moal };
16758dcc1a9dSDamien Le Moal 
16768dcc1a9dSDamien Le Moal static int __init zonefs_init_inodecache(void)
16778dcc1a9dSDamien Le Moal {
16788dcc1a9dSDamien Le Moal 	zonefs_inode_cachep = kmem_cache_create("zonefs_inode_cache",
16798dcc1a9dSDamien Le Moal 			sizeof(struct zonefs_inode_info), 0,
16808dcc1a9dSDamien Le Moal 			(SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT),
16818dcc1a9dSDamien Le Moal 			NULL);
16828dcc1a9dSDamien Le Moal 	if (zonefs_inode_cachep == NULL)
16838dcc1a9dSDamien Le Moal 		return -ENOMEM;
16848dcc1a9dSDamien Le Moal 	return 0;
16858dcc1a9dSDamien Le Moal }
16868dcc1a9dSDamien Le Moal 
16878dcc1a9dSDamien Le Moal static void zonefs_destroy_inodecache(void)
16888dcc1a9dSDamien Le Moal {
16898dcc1a9dSDamien Le Moal 	/*
16908dcc1a9dSDamien Le Moal 	 * Make sure all delayed rcu free inodes are flushed before we
16918dcc1a9dSDamien Le Moal 	 * destroy the inode cache.
16928dcc1a9dSDamien Le Moal 	 */
16938dcc1a9dSDamien Le Moal 	rcu_barrier();
16948dcc1a9dSDamien Le Moal 	kmem_cache_destroy(zonefs_inode_cachep);
16958dcc1a9dSDamien Le Moal }
16968dcc1a9dSDamien Le Moal 
16978dcc1a9dSDamien Le Moal static int __init zonefs_init(void)
16988dcc1a9dSDamien Le Moal {
16998dcc1a9dSDamien Le Moal 	int ret;
17008dcc1a9dSDamien Le Moal 
17018dcc1a9dSDamien Le Moal 	BUILD_BUG_ON(sizeof(struct zonefs_super) != ZONEFS_SUPER_SIZE);
17028dcc1a9dSDamien Le Moal 
17038dcc1a9dSDamien Le Moal 	ret = zonefs_init_inodecache();
17048dcc1a9dSDamien Le Moal 	if (ret)
17058dcc1a9dSDamien Le Moal 		return ret;
17068dcc1a9dSDamien Le Moal 
17078dcc1a9dSDamien Le Moal 	ret = register_filesystem(&zonefs_type);
17088dcc1a9dSDamien Le Moal 	if (ret) {
17098dcc1a9dSDamien Le Moal 		zonefs_destroy_inodecache();
17108dcc1a9dSDamien Le Moal 		return ret;
17118dcc1a9dSDamien Le Moal 	}
17128dcc1a9dSDamien Le Moal 
17138dcc1a9dSDamien Le Moal 	return 0;
17148dcc1a9dSDamien Le Moal }
17158dcc1a9dSDamien Le Moal 
17168dcc1a9dSDamien Le Moal static void __exit zonefs_exit(void)
17178dcc1a9dSDamien Le Moal {
17188dcc1a9dSDamien Le Moal 	zonefs_destroy_inodecache();
17198dcc1a9dSDamien Le Moal 	unregister_filesystem(&zonefs_type);
17208dcc1a9dSDamien Le Moal }
17218dcc1a9dSDamien Le Moal 
17228dcc1a9dSDamien Le Moal MODULE_AUTHOR("Damien Le Moal");
17238dcc1a9dSDamien Le Moal MODULE_DESCRIPTION("Zone file system for zoned block devices");
17248dcc1a9dSDamien Le Moal MODULE_LICENSE("GPL");
17258dcc1a9dSDamien Le Moal module_init(zonefs_init);
17268dcc1a9dSDamien Le Moal module_exit(zonefs_exit);
1727