xref: /openbmc/linux/fs/zonefs/super.c (revision 5498d5f93210ab4c55cd191473c8f4a59cc68e8e)
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 
27*5498d5f9SJohannes Thumshirn static inline int zonefs_zone_mgmt(struct inode *inode,
28*5498d5f9SJohannes Thumshirn 				   enum req_opf op)
29*5498d5f9SJohannes Thumshirn {
30*5498d5f9SJohannes Thumshirn 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
31*5498d5f9SJohannes Thumshirn 	int ret;
32*5498d5f9SJohannes Thumshirn 
33*5498d5f9SJohannes Thumshirn 	lockdep_assert_held(&zi->i_truncate_mutex);
34*5498d5f9SJohannes Thumshirn 
35*5498d5f9SJohannes Thumshirn 	ret = blkdev_zone_mgmt(inode->i_sb->s_bdev, op, zi->i_zsector,
36*5498d5f9SJohannes Thumshirn 			       zi->i_zone_size >> SECTOR_SHIFT, GFP_NOFS);
37*5498d5f9SJohannes Thumshirn 	if (ret) {
38*5498d5f9SJohannes Thumshirn 		zonefs_err(inode->i_sb,
39*5498d5f9SJohannes Thumshirn 			   "Zone management operation %s at %llu failed %d\n",
40*5498d5f9SJohannes Thumshirn 			   blk_op_str(op), zi->i_zsector, ret);
41*5498d5f9SJohannes Thumshirn 		return ret;
42*5498d5f9SJohannes Thumshirn 	}
43*5498d5f9SJohannes Thumshirn 
44*5498d5f9SJohannes Thumshirn 	return 0;
45*5498d5f9SJohannes Thumshirn }
46*5498d5f9SJohannes Thumshirn 
478dcc1a9dSDamien Le Moal static int zonefs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
488dcc1a9dSDamien Le Moal 			      unsigned int flags, struct iomap *iomap,
498dcc1a9dSDamien Le Moal 			      struct iomap *srcmap)
508dcc1a9dSDamien Le Moal {
518dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
528dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
538dcc1a9dSDamien Le Moal 	loff_t isize;
548dcc1a9dSDamien Le Moal 
558dcc1a9dSDamien Le Moal 	/* All I/Os should always be within the file maximum size */
568dcc1a9dSDamien Le Moal 	if (WARN_ON_ONCE(offset + length > zi->i_max_size))
578dcc1a9dSDamien Le Moal 		return -EIO;
588dcc1a9dSDamien Le Moal 
598dcc1a9dSDamien Le Moal 	/*
608dcc1a9dSDamien Le Moal 	 * Sequential zones can only accept direct writes. This is already
618dcc1a9dSDamien Le Moal 	 * checked when writes are issued, so warn if we see a page writeback
628dcc1a9dSDamien Le Moal 	 * operation.
638dcc1a9dSDamien Le Moal 	 */
648dcc1a9dSDamien Le Moal 	if (WARN_ON_ONCE(zi->i_ztype == ZONEFS_ZTYPE_SEQ &&
658dcc1a9dSDamien Le Moal 			 (flags & IOMAP_WRITE) && !(flags & IOMAP_DIRECT)))
668dcc1a9dSDamien Le Moal 		return -EIO;
678dcc1a9dSDamien Le Moal 
688dcc1a9dSDamien Le Moal 	/*
698dcc1a9dSDamien Le Moal 	 * For conventional zones, all blocks are always mapped. For sequential
708dcc1a9dSDamien Le Moal 	 * zones, all blocks after always mapped below the inode size (zone
718dcc1a9dSDamien Le Moal 	 * write pointer) and unwriten beyond.
728dcc1a9dSDamien Le Moal 	 */
738dcc1a9dSDamien Le Moal 	mutex_lock(&zi->i_truncate_mutex);
748dcc1a9dSDamien Le Moal 	isize = i_size_read(inode);
758dcc1a9dSDamien Le Moal 	if (offset >= isize)
768dcc1a9dSDamien Le Moal 		iomap->type = IOMAP_UNWRITTEN;
778dcc1a9dSDamien Le Moal 	else
788dcc1a9dSDamien Le Moal 		iomap->type = IOMAP_MAPPED;
798dcc1a9dSDamien Le Moal 	if (flags & IOMAP_WRITE)
808dcc1a9dSDamien Le Moal 		length = zi->i_max_size - offset;
818dcc1a9dSDamien Le Moal 	else
828dcc1a9dSDamien Le Moal 		length = min(length, isize - offset);
838dcc1a9dSDamien Le Moal 	mutex_unlock(&zi->i_truncate_mutex);
848dcc1a9dSDamien Le Moal 
858dcc1a9dSDamien Le Moal 	iomap->offset = ALIGN_DOWN(offset, sb->s_blocksize);
868dcc1a9dSDamien Le Moal 	iomap->length = ALIGN(offset + length, sb->s_blocksize) - iomap->offset;
878dcc1a9dSDamien Le Moal 	iomap->bdev = inode->i_sb->s_bdev;
888dcc1a9dSDamien Le Moal 	iomap->addr = (zi->i_zsector << SECTOR_SHIFT) + iomap->offset;
898dcc1a9dSDamien Le Moal 
908dcc1a9dSDamien Le Moal 	return 0;
918dcc1a9dSDamien Le Moal }
928dcc1a9dSDamien Le Moal 
938dcc1a9dSDamien Le Moal static const struct iomap_ops zonefs_iomap_ops = {
948dcc1a9dSDamien Le Moal 	.iomap_begin	= zonefs_iomap_begin,
958dcc1a9dSDamien Le Moal };
968dcc1a9dSDamien Le Moal 
978dcc1a9dSDamien Le Moal static int zonefs_readpage(struct file *unused, struct page *page)
988dcc1a9dSDamien Le Moal {
998dcc1a9dSDamien Le Moal 	return iomap_readpage(page, &zonefs_iomap_ops);
1008dcc1a9dSDamien Le Moal }
1018dcc1a9dSDamien Le Moal 
1029d24a13aSMatthew Wilcox (Oracle) static void zonefs_readahead(struct readahead_control *rac)
1038dcc1a9dSDamien Le Moal {
1049d24a13aSMatthew Wilcox (Oracle) 	iomap_readahead(rac, &zonefs_iomap_ops);
1058dcc1a9dSDamien Le Moal }
1068dcc1a9dSDamien Le Moal 
1078dcc1a9dSDamien Le Moal /*
1088dcc1a9dSDamien Le Moal  * Map blocks for page writeback. This is used only on conventional zone files,
1098dcc1a9dSDamien Le Moal  * which implies that the page range can only be within the fixed inode size.
1108dcc1a9dSDamien Le Moal  */
1118dcc1a9dSDamien Le Moal static int zonefs_map_blocks(struct iomap_writepage_ctx *wpc,
1128dcc1a9dSDamien Le Moal 			     struct inode *inode, loff_t offset)
1138dcc1a9dSDamien Le Moal {
1148dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
1158dcc1a9dSDamien Le Moal 
1168dcc1a9dSDamien Le Moal 	if (WARN_ON_ONCE(zi->i_ztype != ZONEFS_ZTYPE_CNV))
1178dcc1a9dSDamien Le Moal 		return -EIO;
1188dcc1a9dSDamien Le Moal 	if (WARN_ON_ONCE(offset >= i_size_read(inode)))
1198dcc1a9dSDamien Le Moal 		return -EIO;
1208dcc1a9dSDamien Le Moal 
1218dcc1a9dSDamien Le Moal 	/* If the mapping is already OK, nothing needs to be done */
1228dcc1a9dSDamien Le Moal 	if (offset >= wpc->iomap.offset &&
1238dcc1a9dSDamien Le Moal 	    offset < wpc->iomap.offset + wpc->iomap.length)
1248dcc1a9dSDamien Le Moal 		return 0;
1258dcc1a9dSDamien Le Moal 
1268dcc1a9dSDamien Le Moal 	return zonefs_iomap_begin(inode, offset, zi->i_max_size - offset,
1278dcc1a9dSDamien Le Moal 				  IOMAP_WRITE, &wpc->iomap, NULL);
1288dcc1a9dSDamien Le Moal }
1298dcc1a9dSDamien Le Moal 
1308dcc1a9dSDamien Le Moal static const struct iomap_writeback_ops zonefs_writeback_ops = {
1318dcc1a9dSDamien Le Moal 	.map_blocks		= zonefs_map_blocks,
1328dcc1a9dSDamien Le Moal };
1338dcc1a9dSDamien Le Moal 
1348dcc1a9dSDamien Le Moal static int zonefs_writepage(struct page *page, struct writeback_control *wbc)
1358dcc1a9dSDamien Le Moal {
1368dcc1a9dSDamien Le Moal 	struct iomap_writepage_ctx wpc = { };
1378dcc1a9dSDamien Le Moal 
1388dcc1a9dSDamien Le Moal 	return iomap_writepage(page, wbc, &wpc, &zonefs_writeback_ops);
1398dcc1a9dSDamien Le Moal }
1408dcc1a9dSDamien Le Moal 
1418dcc1a9dSDamien Le Moal static int zonefs_writepages(struct address_space *mapping,
1428dcc1a9dSDamien Le Moal 			     struct writeback_control *wbc)
1438dcc1a9dSDamien Le Moal {
1448dcc1a9dSDamien Le Moal 	struct iomap_writepage_ctx wpc = { };
1458dcc1a9dSDamien Le Moal 
1468dcc1a9dSDamien Le Moal 	return iomap_writepages(mapping, wbc, &wpc, &zonefs_writeback_ops);
1478dcc1a9dSDamien Le Moal }
1488dcc1a9dSDamien Le Moal 
1498dcc1a9dSDamien Le Moal static const struct address_space_operations zonefs_file_aops = {
1508dcc1a9dSDamien Le Moal 	.readpage		= zonefs_readpage,
1519d24a13aSMatthew Wilcox (Oracle) 	.readahead		= zonefs_readahead,
1528dcc1a9dSDamien Le Moal 	.writepage		= zonefs_writepage,
1538dcc1a9dSDamien Le Moal 	.writepages		= zonefs_writepages,
1548dcc1a9dSDamien Le Moal 	.set_page_dirty		= iomap_set_page_dirty,
1558dcc1a9dSDamien Le Moal 	.releasepage		= iomap_releasepage,
1568dcc1a9dSDamien Le Moal 	.invalidatepage		= iomap_invalidatepage,
1578dcc1a9dSDamien Le Moal 	.migratepage		= iomap_migrate_page,
1588dcc1a9dSDamien Le Moal 	.is_partially_uptodate	= iomap_is_partially_uptodate,
1598dcc1a9dSDamien Le Moal 	.error_remove_page	= generic_error_remove_page,
1608dcc1a9dSDamien Le Moal 	.direct_IO		= noop_direct_IO,
1618dcc1a9dSDamien Le Moal };
1628dcc1a9dSDamien Le Moal 
1638dcc1a9dSDamien Le Moal static void zonefs_update_stats(struct inode *inode, loff_t new_isize)
1648dcc1a9dSDamien Le Moal {
1658dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
1668dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1678dcc1a9dSDamien Le Moal 	loff_t old_isize = i_size_read(inode);
1688dcc1a9dSDamien Le Moal 	loff_t nr_blocks;
1698dcc1a9dSDamien Le Moal 
1708dcc1a9dSDamien Le Moal 	if (new_isize == old_isize)
1718dcc1a9dSDamien Le Moal 		return;
1728dcc1a9dSDamien Le Moal 
1738dcc1a9dSDamien Le Moal 	spin_lock(&sbi->s_lock);
1748dcc1a9dSDamien Le Moal 
1758dcc1a9dSDamien Le Moal 	/*
1768dcc1a9dSDamien Le Moal 	 * This may be called for an update after an IO error.
1778dcc1a9dSDamien Le Moal 	 * So beware of the values seen.
1788dcc1a9dSDamien Le Moal 	 */
1798dcc1a9dSDamien Le Moal 	if (new_isize < old_isize) {
1808dcc1a9dSDamien Le Moal 		nr_blocks = (old_isize - new_isize) >> sb->s_blocksize_bits;
1818dcc1a9dSDamien Le Moal 		if (sbi->s_used_blocks > nr_blocks)
1828dcc1a9dSDamien Le Moal 			sbi->s_used_blocks -= nr_blocks;
1838dcc1a9dSDamien Le Moal 		else
1848dcc1a9dSDamien Le Moal 			sbi->s_used_blocks = 0;
1858dcc1a9dSDamien Le Moal 	} else {
1868dcc1a9dSDamien Le Moal 		sbi->s_used_blocks +=
1878dcc1a9dSDamien Le Moal 			(new_isize - old_isize) >> sb->s_blocksize_bits;
1888dcc1a9dSDamien Le Moal 		if (sbi->s_used_blocks > sbi->s_blocks)
1898dcc1a9dSDamien Le Moal 			sbi->s_used_blocks = sbi->s_blocks;
1908dcc1a9dSDamien Le Moal 	}
1918dcc1a9dSDamien Le Moal 
1928dcc1a9dSDamien Le Moal 	spin_unlock(&sbi->s_lock);
1938dcc1a9dSDamien Le Moal }
1948dcc1a9dSDamien Le Moal 
1958dcc1a9dSDamien Le Moal /*
1968dcc1a9dSDamien Le Moal  * Check a zone condition and adjust its file inode access permissions for
1978dcc1a9dSDamien Le Moal  * offline and readonly zones. Return the inode size corresponding to the
1988dcc1a9dSDamien Le Moal  * amount of readable data in the zone.
1998dcc1a9dSDamien Le Moal  */
2008dcc1a9dSDamien Le Moal static loff_t zonefs_check_zone_condition(struct inode *inode,
201ccf4ad7dSDamien Le Moal 					  struct blk_zone *zone, bool warn,
202ccf4ad7dSDamien Le Moal 					  bool mount)
2038dcc1a9dSDamien Le Moal {
2048dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
2058dcc1a9dSDamien Le Moal 
2068dcc1a9dSDamien Le Moal 	switch (zone->cond) {
2078dcc1a9dSDamien Le Moal 	case BLK_ZONE_COND_OFFLINE:
2088dcc1a9dSDamien Le Moal 		/*
2098dcc1a9dSDamien Le Moal 		 * Dead zone: make the inode immutable, disable all accesses
2108dcc1a9dSDamien Le Moal 		 * and set the file size to 0 (zone wp set to zone start).
2118dcc1a9dSDamien Le Moal 		 */
2128dcc1a9dSDamien Le Moal 		if (warn)
2138dcc1a9dSDamien Le Moal 			zonefs_warn(inode->i_sb, "inode %lu: offline zone\n",
2148dcc1a9dSDamien Le Moal 				    inode->i_ino);
2158dcc1a9dSDamien Le Moal 		inode->i_flags |= S_IMMUTABLE;
2168dcc1a9dSDamien Le Moal 		inode->i_mode &= ~0777;
2178dcc1a9dSDamien Le Moal 		zone->wp = zone->start;
2188dcc1a9dSDamien Le Moal 		return 0;
2198dcc1a9dSDamien Le Moal 	case BLK_ZONE_COND_READONLY:
220ccf4ad7dSDamien Le Moal 		/*
221ccf4ad7dSDamien Le Moal 		 * The write pointer of read-only zones is invalid. If such a
222ccf4ad7dSDamien Le Moal 		 * zone is found during mount, the file size cannot be retrieved
223ccf4ad7dSDamien Le Moal 		 * so we treat the zone as offline (mount == true case).
224ccf4ad7dSDamien Le Moal 		 * Otherwise, keep the file size as it was when last updated
225ccf4ad7dSDamien Le Moal 		 * so that the user can recover data. In both cases, writes are
226ccf4ad7dSDamien Le Moal 		 * always disabled for the zone.
227ccf4ad7dSDamien Le Moal 		 */
2288dcc1a9dSDamien Le Moal 		if (warn)
2298dcc1a9dSDamien Le Moal 			zonefs_warn(inode->i_sb, "inode %lu: read-only zone\n",
2308dcc1a9dSDamien Le Moal 				    inode->i_ino);
2318dcc1a9dSDamien Le Moal 		inode->i_flags |= S_IMMUTABLE;
232ccf4ad7dSDamien Le Moal 		if (mount) {
233ccf4ad7dSDamien Le Moal 			zone->cond = BLK_ZONE_COND_OFFLINE;
234ccf4ad7dSDamien Le Moal 			inode->i_mode &= ~0777;
235ccf4ad7dSDamien Le Moal 			zone->wp = zone->start;
236ccf4ad7dSDamien Le Moal 			return 0;
237ccf4ad7dSDamien Le Moal 		}
2388dcc1a9dSDamien Le Moal 		inode->i_mode &= ~0222;
239ccf4ad7dSDamien Le Moal 		return i_size_read(inode);
2408dcc1a9dSDamien Le Moal 	default:
2418dcc1a9dSDamien Le Moal 		if (zi->i_ztype == ZONEFS_ZTYPE_CNV)
2428dcc1a9dSDamien Le Moal 			return zi->i_max_size;
2438dcc1a9dSDamien Le Moal 		return (zone->wp - zone->start) << SECTOR_SHIFT;
2448dcc1a9dSDamien Le Moal 	}
2458dcc1a9dSDamien Le Moal }
2468dcc1a9dSDamien Le Moal 
2478dcc1a9dSDamien Le Moal struct zonefs_ioerr_data {
2488dcc1a9dSDamien Le Moal 	struct inode	*inode;
2498dcc1a9dSDamien Le Moal 	bool		write;
2508dcc1a9dSDamien Le Moal };
2518dcc1a9dSDamien Le Moal 
2528dcc1a9dSDamien Le Moal static int zonefs_io_error_cb(struct blk_zone *zone, unsigned int idx,
2538dcc1a9dSDamien Le Moal 			      void *data)
2548dcc1a9dSDamien Le Moal {
2558dcc1a9dSDamien Le Moal 	struct zonefs_ioerr_data *err = data;
2568dcc1a9dSDamien Le Moal 	struct inode *inode = err->inode;
2578dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
2588dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
2598dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
2608dcc1a9dSDamien Le Moal 	loff_t isize, data_size;
2618dcc1a9dSDamien Le Moal 
2628dcc1a9dSDamien Le Moal 	/*
2638dcc1a9dSDamien Le Moal 	 * Check the zone condition: if the zone is not "bad" (offline or
2648dcc1a9dSDamien Le Moal 	 * read-only), read errors are simply signaled to the IO issuer as long
2658dcc1a9dSDamien Le Moal 	 * as there is no inconsistency between the inode size and the amount of
2668dcc1a9dSDamien Le Moal 	 * data writen in the zone (data_size).
2678dcc1a9dSDamien Le Moal 	 */
268ccf4ad7dSDamien Le Moal 	data_size = zonefs_check_zone_condition(inode, zone, true, false);
2698dcc1a9dSDamien Le Moal 	isize = i_size_read(inode);
2708dcc1a9dSDamien Le Moal 	if (zone->cond != BLK_ZONE_COND_OFFLINE &&
2718dcc1a9dSDamien Le Moal 	    zone->cond != BLK_ZONE_COND_READONLY &&
2728dcc1a9dSDamien Le Moal 	    !err->write && isize == data_size)
2738dcc1a9dSDamien Le Moal 		return 0;
2748dcc1a9dSDamien Le Moal 
2758dcc1a9dSDamien Le Moal 	/*
2768dcc1a9dSDamien Le Moal 	 * At this point, we detected either a bad zone or an inconsistency
2778dcc1a9dSDamien Le Moal 	 * between the inode size and the amount of data written in the zone.
2788dcc1a9dSDamien Le Moal 	 * For the latter case, the cause may be a write IO error or an external
2798dcc1a9dSDamien Le Moal 	 * action on the device. Two error patterns exist:
2808dcc1a9dSDamien Le Moal 	 * 1) The inode size is lower than the amount of data in the zone:
2818dcc1a9dSDamien Le Moal 	 *    a write operation partially failed and data was writen at the end
2828dcc1a9dSDamien Le Moal 	 *    of the file. This can happen in the case of a large direct IO
2838dcc1a9dSDamien Le Moal 	 *    needing several BIOs and/or write requests to be processed.
2848dcc1a9dSDamien Le Moal 	 * 2) The inode size is larger than the amount of data in the zone:
2858dcc1a9dSDamien Le Moal 	 *    this can happen with a deferred write error with the use of the
2868dcc1a9dSDamien Le Moal 	 *    device side write cache after getting successful write IO
2878dcc1a9dSDamien Le Moal 	 *    completions. Other possibilities are (a) an external corruption,
2888dcc1a9dSDamien Le Moal 	 *    e.g. an application reset the zone directly, or (b) the device
2898dcc1a9dSDamien Le Moal 	 *    has a serious problem (e.g. firmware bug).
2908dcc1a9dSDamien Le Moal 	 *
2918dcc1a9dSDamien Le Moal 	 * In all cases, warn about inode size inconsistency and handle the
2928dcc1a9dSDamien Le Moal 	 * IO error according to the zone condition and to the mount options.
2938dcc1a9dSDamien Le Moal 	 */
2948dcc1a9dSDamien Le Moal 	if (zi->i_ztype == ZONEFS_ZTYPE_SEQ && isize != data_size)
2958dcc1a9dSDamien Le Moal 		zonefs_warn(sb, "inode %lu: invalid size %lld (should be %lld)\n",
2968dcc1a9dSDamien Le Moal 			    inode->i_ino, isize, data_size);
2978dcc1a9dSDamien Le Moal 
2988dcc1a9dSDamien Le Moal 	/*
2998dcc1a9dSDamien Le Moal 	 * First handle bad zones signaled by hardware. The mount options
3008dcc1a9dSDamien Le Moal 	 * errors=zone-ro and errors=zone-offline result in changing the
3018dcc1a9dSDamien Le Moal 	 * zone condition to read-only and offline respectively, as if the
3028dcc1a9dSDamien Le Moal 	 * condition was signaled by the hardware.
3038dcc1a9dSDamien Le Moal 	 */
3048dcc1a9dSDamien Le Moal 	if (zone->cond == BLK_ZONE_COND_OFFLINE ||
3058dcc1a9dSDamien Le Moal 	    sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL) {
3068dcc1a9dSDamien Le Moal 		zonefs_warn(sb, "inode %lu: read/write access disabled\n",
3078dcc1a9dSDamien Le Moal 			    inode->i_ino);
3088dcc1a9dSDamien Le Moal 		if (zone->cond != BLK_ZONE_COND_OFFLINE) {
3098dcc1a9dSDamien Le Moal 			zone->cond = BLK_ZONE_COND_OFFLINE;
3108dcc1a9dSDamien Le Moal 			data_size = zonefs_check_zone_condition(inode, zone,
311ccf4ad7dSDamien Le Moal 								false, false);
3128dcc1a9dSDamien Le Moal 		}
3138dcc1a9dSDamien Le Moal 	} else if (zone->cond == BLK_ZONE_COND_READONLY ||
3148dcc1a9dSDamien Le Moal 		   sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO) {
3158dcc1a9dSDamien Le Moal 		zonefs_warn(sb, "inode %lu: write access disabled\n",
3168dcc1a9dSDamien Le Moal 			    inode->i_ino);
3178dcc1a9dSDamien Le Moal 		if (zone->cond != BLK_ZONE_COND_READONLY) {
3188dcc1a9dSDamien Le Moal 			zone->cond = BLK_ZONE_COND_READONLY;
3198dcc1a9dSDamien Le Moal 			data_size = zonefs_check_zone_condition(inode, zone,
320ccf4ad7dSDamien Le Moal 								false, false);
3218dcc1a9dSDamien Le Moal 		}
3228dcc1a9dSDamien Le Moal 	}
3238dcc1a9dSDamien Le Moal 
3248dcc1a9dSDamien Le Moal 	/*
3258dcc1a9dSDamien Le Moal 	 * If error=remount-ro was specified, any error result in remounting
3268dcc1a9dSDamien Le Moal 	 * the volume as read-only.
3278dcc1a9dSDamien Le Moal 	 */
3288dcc1a9dSDamien Le Moal 	if ((sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO) && !sb_rdonly(sb)) {
3298dcc1a9dSDamien Le Moal 		zonefs_warn(sb, "remounting filesystem read-only\n");
3308dcc1a9dSDamien Le Moal 		sb->s_flags |= SB_RDONLY;
3318dcc1a9dSDamien Le Moal 	}
3328dcc1a9dSDamien Le Moal 
3338dcc1a9dSDamien Le Moal 	/*
3348dcc1a9dSDamien Le Moal 	 * Update block usage stats and the inode size  to prevent access to
3358dcc1a9dSDamien Le Moal 	 * invalid data.
3368dcc1a9dSDamien Le Moal 	 */
3378dcc1a9dSDamien Le Moal 	zonefs_update_stats(inode, data_size);
3388dcc1a9dSDamien Le Moal 	i_size_write(inode, data_size);
3398dcc1a9dSDamien Le Moal 	zi->i_wpoffset = data_size;
3408dcc1a9dSDamien Le Moal 
3418dcc1a9dSDamien Le Moal 	return 0;
3428dcc1a9dSDamien Le Moal }
3438dcc1a9dSDamien Le Moal 
3448dcc1a9dSDamien Le Moal /*
3458dcc1a9dSDamien Le Moal  * When an file IO error occurs, check the file zone to see if there is a change
3468dcc1a9dSDamien Le Moal  * in the zone condition (e.g. offline or read-only). For a failed write to a
3478dcc1a9dSDamien Le Moal  * sequential zone, the zone write pointer position must also be checked to
3488dcc1a9dSDamien Le Moal  * eventually correct the file size and zonefs inode write pointer offset
3498dcc1a9dSDamien Le Moal  * (which can be out of sync with the drive due to partial write failures).
3508dcc1a9dSDamien Le Moal  */
3518dcc1a9dSDamien Le Moal static void zonefs_io_error(struct inode *inode, bool write)
3528dcc1a9dSDamien Le Moal {
3538dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
3548dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
3558dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
3568dcc1a9dSDamien Le Moal 	unsigned int noio_flag;
3578dcc1a9dSDamien Le Moal 	unsigned int nr_zones =
358e3c3155bSJohannes Thumshirn 		zi->i_zone_size >> (sbi->s_zone_sectors_shift + SECTOR_SHIFT);
3598dcc1a9dSDamien Le Moal 	struct zonefs_ioerr_data err = {
3608dcc1a9dSDamien Le Moal 		.inode = inode,
3618dcc1a9dSDamien Le Moal 		.write = write,
3628dcc1a9dSDamien Le Moal 	};
3638dcc1a9dSDamien Le Moal 	int ret;
3648dcc1a9dSDamien Le Moal 
3658dcc1a9dSDamien Le Moal 	mutex_lock(&zi->i_truncate_mutex);
3668dcc1a9dSDamien Le Moal 
3678dcc1a9dSDamien Le Moal 	/*
3688dcc1a9dSDamien Le Moal 	 * Memory allocations in blkdev_report_zones() can trigger a memory
3698dcc1a9dSDamien Le Moal 	 * reclaim which may in turn cause a recursion into zonefs as well as
3708dcc1a9dSDamien Le Moal 	 * struct request allocations for the same device. The former case may
3718dcc1a9dSDamien Le Moal 	 * end up in a deadlock on the inode truncate mutex, while the latter
3728dcc1a9dSDamien Le Moal 	 * may prevent IO forward progress. Executing the report zones under
3738dcc1a9dSDamien Le Moal 	 * the GFP_NOIO context avoids both problems.
3748dcc1a9dSDamien Le Moal 	 */
3758dcc1a9dSDamien Le Moal 	noio_flag = memalloc_noio_save();
3768dcc1a9dSDamien Le Moal 	ret = blkdev_report_zones(sb->s_bdev, zi->i_zsector, nr_zones,
3778dcc1a9dSDamien Le Moal 				  zonefs_io_error_cb, &err);
3788dcc1a9dSDamien Le Moal 	if (ret != nr_zones)
3798dcc1a9dSDamien Le Moal 		zonefs_err(sb, "Get inode %lu zone information failed %d\n",
3808dcc1a9dSDamien Le Moal 			   inode->i_ino, ret);
3818dcc1a9dSDamien Le Moal 	memalloc_noio_restore(noio_flag);
3828dcc1a9dSDamien Le Moal 
3838dcc1a9dSDamien Le Moal 	mutex_unlock(&zi->i_truncate_mutex);
3848dcc1a9dSDamien Le Moal }
3858dcc1a9dSDamien Le Moal 
3868dcc1a9dSDamien Le Moal static int zonefs_file_truncate(struct inode *inode, loff_t isize)
3878dcc1a9dSDamien Le Moal {
3888dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
3898dcc1a9dSDamien Le Moal 	loff_t old_isize;
3908dcc1a9dSDamien Le Moal 	enum req_opf op;
3918dcc1a9dSDamien Le Moal 	int ret = 0;
3928dcc1a9dSDamien Le Moal 
3938dcc1a9dSDamien Le Moal 	/*
3948dcc1a9dSDamien Le Moal 	 * Only sequential zone files can be truncated and truncation is allowed
3958dcc1a9dSDamien Le Moal 	 * only down to a 0 size, which is equivalent to a zone reset, and to
3968dcc1a9dSDamien Le Moal 	 * the maximum file size, which is equivalent to a zone finish.
3978dcc1a9dSDamien Le Moal 	 */
3988dcc1a9dSDamien Le Moal 	if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
3998dcc1a9dSDamien Le Moal 		return -EPERM;
4008dcc1a9dSDamien Le Moal 
4018dcc1a9dSDamien Le Moal 	if (!isize)
4028dcc1a9dSDamien Le Moal 		op = REQ_OP_ZONE_RESET;
4038dcc1a9dSDamien Le Moal 	else if (isize == zi->i_max_size)
4048dcc1a9dSDamien Le Moal 		op = REQ_OP_ZONE_FINISH;
4058dcc1a9dSDamien Le Moal 	else
4068dcc1a9dSDamien Le Moal 		return -EPERM;
4078dcc1a9dSDamien Le Moal 
4088dcc1a9dSDamien Le Moal 	inode_dio_wait(inode);
4098dcc1a9dSDamien Le Moal 
4108dcc1a9dSDamien Le Moal 	/* Serialize against page faults */
4118dcc1a9dSDamien Le Moal 	down_write(&zi->i_mmap_sem);
4128dcc1a9dSDamien Le Moal 
4138dcc1a9dSDamien Le Moal 	/* Serialize against zonefs_iomap_begin() */
4148dcc1a9dSDamien Le Moal 	mutex_lock(&zi->i_truncate_mutex);
4158dcc1a9dSDamien Le Moal 
4168dcc1a9dSDamien Le Moal 	old_isize = i_size_read(inode);
4178dcc1a9dSDamien Le Moal 	if (isize == old_isize)
4188dcc1a9dSDamien Le Moal 		goto unlock;
4198dcc1a9dSDamien Le Moal 
420*5498d5f9SJohannes Thumshirn 	ret = zonefs_zone_mgmt(inode, op);
421*5498d5f9SJohannes Thumshirn 	if (ret)
4228dcc1a9dSDamien Le Moal 		goto unlock;
4238dcc1a9dSDamien Le Moal 
4248dcc1a9dSDamien Le Moal 	zonefs_update_stats(inode, isize);
4258dcc1a9dSDamien Le Moal 	truncate_setsize(inode, isize);
4268dcc1a9dSDamien Le Moal 	zi->i_wpoffset = isize;
4278dcc1a9dSDamien Le Moal 
4288dcc1a9dSDamien Le Moal unlock:
4298dcc1a9dSDamien Le Moal 	mutex_unlock(&zi->i_truncate_mutex);
4308dcc1a9dSDamien Le Moal 	up_write(&zi->i_mmap_sem);
4318dcc1a9dSDamien Le Moal 
4328dcc1a9dSDamien Le Moal 	return ret;
4338dcc1a9dSDamien Le Moal }
4348dcc1a9dSDamien Le Moal 
4358dcc1a9dSDamien Le Moal static int zonefs_inode_setattr(struct dentry *dentry, struct iattr *iattr)
4368dcc1a9dSDamien Le Moal {
4378dcc1a9dSDamien Le Moal 	struct inode *inode = d_inode(dentry);
4388dcc1a9dSDamien Le Moal 	int ret;
4398dcc1a9dSDamien Le Moal 
4408dcc1a9dSDamien Le Moal 	if (unlikely(IS_IMMUTABLE(inode)))
4418dcc1a9dSDamien Le Moal 		return -EPERM;
4428dcc1a9dSDamien Le Moal 
4438dcc1a9dSDamien Le Moal 	ret = setattr_prepare(dentry, iattr);
4448dcc1a9dSDamien Le Moal 	if (ret)
4458dcc1a9dSDamien Le Moal 		return ret;
4468dcc1a9dSDamien Le Moal 
4478dcc1a9dSDamien Le Moal 	/*
4488dcc1a9dSDamien Le Moal 	 * Since files and directories cannot be created nor deleted, do not
4498dcc1a9dSDamien Le Moal 	 * allow setting any write attributes on the sub-directories grouping
4508dcc1a9dSDamien Le Moal 	 * files by zone type.
4518dcc1a9dSDamien Le Moal 	 */
4528dcc1a9dSDamien Le Moal 	if ((iattr->ia_valid & ATTR_MODE) && S_ISDIR(inode->i_mode) &&
4538dcc1a9dSDamien Le Moal 	    (iattr->ia_mode & 0222))
4548dcc1a9dSDamien Le Moal 		return -EPERM;
4558dcc1a9dSDamien Le Moal 
4568dcc1a9dSDamien Le Moal 	if (((iattr->ia_valid & ATTR_UID) &&
4578dcc1a9dSDamien Le Moal 	     !uid_eq(iattr->ia_uid, inode->i_uid)) ||
4588dcc1a9dSDamien Le Moal 	    ((iattr->ia_valid & ATTR_GID) &&
4598dcc1a9dSDamien Le Moal 	     !gid_eq(iattr->ia_gid, inode->i_gid))) {
4608dcc1a9dSDamien Le Moal 		ret = dquot_transfer(inode, iattr);
4618dcc1a9dSDamien Le Moal 		if (ret)
4628dcc1a9dSDamien Le Moal 			return ret;
4638dcc1a9dSDamien Le Moal 	}
4648dcc1a9dSDamien Le Moal 
4658dcc1a9dSDamien Le Moal 	if (iattr->ia_valid & ATTR_SIZE) {
4668dcc1a9dSDamien Le Moal 		ret = zonefs_file_truncate(inode, iattr->ia_size);
4678dcc1a9dSDamien Le Moal 		if (ret)
4688dcc1a9dSDamien Le Moal 			return ret;
4698dcc1a9dSDamien Le Moal 	}
4708dcc1a9dSDamien Le Moal 
4718dcc1a9dSDamien Le Moal 	setattr_copy(inode, iattr);
4728dcc1a9dSDamien Le Moal 
4738dcc1a9dSDamien Le Moal 	return 0;
4748dcc1a9dSDamien Le Moal }
4758dcc1a9dSDamien Le Moal 
4768dcc1a9dSDamien Le Moal static const struct inode_operations zonefs_file_inode_operations = {
4778dcc1a9dSDamien Le Moal 	.setattr	= zonefs_inode_setattr,
4788dcc1a9dSDamien Le Moal };
4798dcc1a9dSDamien Le Moal 
4808dcc1a9dSDamien Le Moal static int zonefs_file_fsync(struct file *file, loff_t start, loff_t end,
4818dcc1a9dSDamien Le Moal 			     int datasync)
4828dcc1a9dSDamien Le Moal {
4838dcc1a9dSDamien Le Moal 	struct inode *inode = file_inode(file);
4848dcc1a9dSDamien Le Moal 	int ret = 0;
4858dcc1a9dSDamien Le Moal 
4868dcc1a9dSDamien Le Moal 	if (unlikely(IS_IMMUTABLE(inode)))
4878dcc1a9dSDamien Le Moal 		return -EPERM;
4888dcc1a9dSDamien Le Moal 
4898dcc1a9dSDamien Le Moal 	/*
4908dcc1a9dSDamien Le Moal 	 * Since only direct writes are allowed in sequential files, page cache
4918dcc1a9dSDamien Le Moal 	 * flush is needed only for conventional zone files.
4928dcc1a9dSDamien Le Moal 	 */
4938dcc1a9dSDamien Le Moal 	if (ZONEFS_I(inode)->i_ztype == ZONEFS_ZTYPE_CNV)
4948dcc1a9dSDamien Le Moal 		ret = file_write_and_wait_range(file, start, end);
4958dcc1a9dSDamien Le Moal 	if (!ret)
4969398554fSChristoph Hellwig 		ret = blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL);
4978dcc1a9dSDamien Le Moal 
4988dcc1a9dSDamien Le Moal 	if (ret)
4998dcc1a9dSDamien Le Moal 		zonefs_io_error(inode, true);
5008dcc1a9dSDamien Le Moal 
5018dcc1a9dSDamien Le Moal 	return ret;
5028dcc1a9dSDamien Le Moal }
5038dcc1a9dSDamien Le Moal 
5048dcc1a9dSDamien Le Moal static vm_fault_t zonefs_filemap_fault(struct vm_fault *vmf)
5058dcc1a9dSDamien Le Moal {
5068dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(file_inode(vmf->vma->vm_file));
5078dcc1a9dSDamien Le Moal 	vm_fault_t ret;
5088dcc1a9dSDamien Le Moal 
5098dcc1a9dSDamien Le Moal 	down_read(&zi->i_mmap_sem);
5108dcc1a9dSDamien Le Moal 	ret = filemap_fault(vmf);
5118dcc1a9dSDamien Le Moal 	up_read(&zi->i_mmap_sem);
5128dcc1a9dSDamien Le Moal 
5138dcc1a9dSDamien Le Moal 	return ret;
5148dcc1a9dSDamien Le Moal }
5158dcc1a9dSDamien Le Moal 
5168dcc1a9dSDamien Le Moal static vm_fault_t zonefs_filemap_page_mkwrite(struct vm_fault *vmf)
5178dcc1a9dSDamien Le Moal {
5188dcc1a9dSDamien Le Moal 	struct inode *inode = file_inode(vmf->vma->vm_file);
5198dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
5208dcc1a9dSDamien Le Moal 	vm_fault_t ret;
5218dcc1a9dSDamien Le Moal 
5228dcc1a9dSDamien Le Moal 	if (unlikely(IS_IMMUTABLE(inode)))
5238dcc1a9dSDamien Le Moal 		return VM_FAULT_SIGBUS;
5248dcc1a9dSDamien Le Moal 
5258dcc1a9dSDamien Le Moal 	/*
5268dcc1a9dSDamien Le Moal 	 * Sanity check: only conventional zone files can have shared
5278dcc1a9dSDamien Le Moal 	 * writeable mappings.
5288dcc1a9dSDamien Le Moal 	 */
5298dcc1a9dSDamien Le Moal 	if (WARN_ON_ONCE(zi->i_ztype != ZONEFS_ZTYPE_CNV))
5308dcc1a9dSDamien Le Moal 		return VM_FAULT_NOPAGE;
5318dcc1a9dSDamien Le Moal 
5328dcc1a9dSDamien Le Moal 	sb_start_pagefault(inode->i_sb);
5338dcc1a9dSDamien Le Moal 	file_update_time(vmf->vma->vm_file);
5348dcc1a9dSDamien Le Moal 
5358dcc1a9dSDamien Le Moal 	/* Serialize against truncates */
5368dcc1a9dSDamien Le Moal 	down_read(&zi->i_mmap_sem);
5378dcc1a9dSDamien Le Moal 	ret = iomap_page_mkwrite(vmf, &zonefs_iomap_ops);
5388dcc1a9dSDamien Le Moal 	up_read(&zi->i_mmap_sem);
5398dcc1a9dSDamien Le Moal 
5408dcc1a9dSDamien Le Moal 	sb_end_pagefault(inode->i_sb);
5418dcc1a9dSDamien Le Moal 	return ret;
5428dcc1a9dSDamien Le Moal }
5438dcc1a9dSDamien Le Moal 
5448dcc1a9dSDamien Le Moal static const struct vm_operations_struct zonefs_file_vm_ops = {
5458dcc1a9dSDamien Le Moal 	.fault		= zonefs_filemap_fault,
5468dcc1a9dSDamien Le Moal 	.map_pages	= filemap_map_pages,
5478dcc1a9dSDamien Le Moal 	.page_mkwrite	= zonefs_filemap_page_mkwrite,
5488dcc1a9dSDamien Le Moal };
5498dcc1a9dSDamien Le Moal 
5508dcc1a9dSDamien Le Moal static int zonefs_file_mmap(struct file *file, struct vm_area_struct *vma)
5518dcc1a9dSDamien Le Moal {
5528dcc1a9dSDamien Le Moal 	/*
5538dcc1a9dSDamien Le Moal 	 * Conventional zones accept random writes, so their files can support
5548dcc1a9dSDamien Le Moal 	 * shared writable mappings. For sequential zone files, only read
5558dcc1a9dSDamien Le Moal 	 * mappings are possible since there are no guarantees for write
5568dcc1a9dSDamien Le Moal 	 * ordering between msync() and page cache writeback.
5578dcc1a9dSDamien Le Moal 	 */
5588dcc1a9dSDamien Le Moal 	if (ZONEFS_I(file_inode(file))->i_ztype == ZONEFS_ZTYPE_SEQ &&
5598dcc1a9dSDamien Le Moal 	    (vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
5608dcc1a9dSDamien Le Moal 		return -EINVAL;
5618dcc1a9dSDamien Le Moal 
5628dcc1a9dSDamien Le Moal 	file_accessed(file);
5638dcc1a9dSDamien Le Moal 	vma->vm_ops = &zonefs_file_vm_ops;
5648dcc1a9dSDamien Le Moal 
5658dcc1a9dSDamien Le Moal 	return 0;
5668dcc1a9dSDamien Le Moal }
5678dcc1a9dSDamien Le Moal 
5688dcc1a9dSDamien Le Moal static loff_t zonefs_file_llseek(struct file *file, loff_t offset, int whence)
5698dcc1a9dSDamien Le Moal {
5708dcc1a9dSDamien Le Moal 	loff_t isize = i_size_read(file_inode(file));
5718dcc1a9dSDamien Le Moal 
5728dcc1a9dSDamien Le Moal 	/*
5738dcc1a9dSDamien Le Moal 	 * Seeks are limited to below the zone size for conventional zones
5748dcc1a9dSDamien Le Moal 	 * and below the zone write pointer for sequential zones. In both
5758dcc1a9dSDamien Le Moal 	 * cases, this limit is the inode size.
5768dcc1a9dSDamien Le Moal 	 */
5778dcc1a9dSDamien Le Moal 	return generic_file_llseek_size(file, offset, whence, isize, isize);
5788dcc1a9dSDamien Le Moal }
5798dcc1a9dSDamien Le Moal 
5808dcc1a9dSDamien Le Moal static int zonefs_file_write_dio_end_io(struct kiocb *iocb, ssize_t size,
5818dcc1a9dSDamien Le Moal 					int error, unsigned int flags)
5828dcc1a9dSDamien Le Moal {
5838dcc1a9dSDamien Le Moal 	struct inode *inode = file_inode(iocb->ki_filp);
5848dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
5858dcc1a9dSDamien Le Moal 
5868dcc1a9dSDamien Le Moal 	if (error) {
5878dcc1a9dSDamien Le Moal 		zonefs_io_error(inode, true);
5888dcc1a9dSDamien Le Moal 		return error;
5898dcc1a9dSDamien Le Moal 	}
5908dcc1a9dSDamien Le Moal 
5918dcc1a9dSDamien Le Moal 	if (size && zi->i_ztype != ZONEFS_ZTYPE_CNV) {
5928dcc1a9dSDamien Le Moal 		/*
5938dcc1a9dSDamien Le Moal 		 * Note that we may be seeing completions out of order,
5948dcc1a9dSDamien Le Moal 		 * but that is not a problem since a write completed
5958dcc1a9dSDamien Le Moal 		 * successfully necessarily means that all preceding writes
5968dcc1a9dSDamien Le Moal 		 * were also successful. So we can safely increase the inode
5978dcc1a9dSDamien Le Moal 		 * size to the write end location.
5988dcc1a9dSDamien Le Moal 		 */
5998dcc1a9dSDamien Le Moal 		mutex_lock(&zi->i_truncate_mutex);
6008dcc1a9dSDamien Le Moal 		if (i_size_read(inode) < iocb->ki_pos + size) {
6018dcc1a9dSDamien Le Moal 			zonefs_update_stats(inode, iocb->ki_pos + size);
6028dcc1a9dSDamien Le Moal 			i_size_write(inode, iocb->ki_pos + size);
6038dcc1a9dSDamien Le Moal 		}
6048dcc1a9dSDamien Le Moal 		mutex_unlock(&zi->i_truncate_mutex);
6058dcc1a9dSDamien Le Moal 	}
6068dcc1a9dSDamien Le Moal 
6078dcc1a9dSDamien Le Moal 	return 0;
6088dcc1a9dSDamien Le Moal }
6098dcc1a9dSDamien Le Moal 
6108dcc1a9dSDamien Le Moal static const struct iomap_dio_ops zonefs_write_dio_ops = {
6118dcc1a9dSDamien Le Moal 	.end_io			= zonefs_file_write_dio_end_io,
6128dcc1a9dSDamien Le Moal };
6138dcc1a9dSDamien Le Moal 
61402ef12a6SJohannes Thumshirn static ssize_t zonefs_file_dio_append(struct kiocb *iocb, struct iov_iter *from)
61502ef12a6SJohannes Thumshirn {
61602ef12a6SJohannes Thumshirn 	struct inode *inode = file_inode(iocb->ki_filp);
61702ef12a6SJohannes Thumshirn 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
61802ef12a6SJohannes Thumshirn 	struct block_device *bdev = inode->i_sb->s_bdev;
61902ef12a6SJohannes Thumshirn 	unsigned int max;
62002ef12a6SJohannes Thumshirn 	struct bio *bio;
62102ef12a6SJohannes Thumshirn 	ssize_t size;
62202ef12a6SJohannes Thumshirn 	int nr_pages;
62302ef12a6SJohannes Thumshirn 	ssize_t ret;
62402ef12a6SJohannes Thumshirn 
62502ef12a6SJohannes Thumshirn 	max = queue_max_zone_append_sectors(bdev_get_queue(bdev));
62602ef12a6SJohannes Thumshirn 	max = ALIGN_DOWN(max << SECTOR_SHIFT, inode->i_sb->s_blocksize);
62702ef12a6SJohannes Thumshirn 	iov_iter_truncate(from, max);
62802ef12a6SJohannes Thumshirn 
62989ee7237SJohannes Thumshirn 	nr_pages = iov_iter_npages(from, BIO_MAX_PAGES);
63089ee7237SJohannes Thumshirn 	if (!nr_pages)
63189ee7237SJohannes Thumshirn 		return 0;
63289ee7237SJohannes Thumshirn 
63302ef12a6SJohannes Thumshirn 	bio = bio_alloc_bioset(GFP_NOFS, nr_pages, &fs_bio_set);
63402ef12a6SJohannes Thumshirn 	if (!bio)
63502ef12a6SJohannes Thumshirn 		return -ENOMEM;
63602ef12a6SJohannes Thumshirn 
63702ef12a6SJohannes Thumshirn 	bio_set_dev(bio, bdev);
63802ef12a6SJohannes Thumshirn 	bio->bi_iter.bi_sector = zi->i_zsector;
63902ef12a6SJohannes Thumshirn 	bio->bi_write_hint = iocb->ki_hint;
64002ef12a6SJohannes Thumshirn 	bio->bi_ioprio = iocb->ki_ioprio;
64102ef12a6SJohannes Thumshirn 	bio->bi_opf = REQ_OP_ZONE_APPEND | REQ_SYNC | REQ_IDLE;
64202ef12a6SJohannes Thumshirn 	if (iocb->ki_flags & IOCB_DSYNC)
64302ef12a6SJohannes Thumshirn 		bio->bi_opf |= REQ_FUA;
64402ef12a6SJohannes Thumshirn 
64502ef12a6SJohannes Thumshirn 	ret = bio_iov_iter_get_pages(bio, from);
64602ef12a6SJohannes Thumshirn 	if (unlikely(ret)) {
64702ef12a6SJohannes Thumshirn 		bio_io_error(bio);
64802ef12a6SJohannes Thumshirn 		return ret;
64902ef12a6SJohannes Thumshirn 	}
65002ef12a6SJohannes Thumshirn 	size = bio->bi_iter.bi_size;
65102ef12a6SJohannes Thumshirn 	task_io_account_write(ret);
65202ef12a6SJohannes Thumshirn 
65302ef12a6SJohannes Thumshirn 	if (iocb->ki_flags & IOCB_HIPRI)
65402ef12a6SJohannes Thumshirn 		bio_set_polled(bio, iocb);
65502ef12a6SJohannes Thumshirn 
65602ef12a6SJohannes Thumshirn 	ret = submit_bio_wait(bio);
65702ef12a6SJohannes Thumshirn 
65802ef12a6SJohannes Thumshirn 	bio_put(bio);
65902ef12a6SJohannes Thumshirn 
66002ef12a6SJohannes Thumshirn 	zonefs_file_write_dio_end_io(iocb, size, ret, 0);
66102ef12a6SJohannes Thumshirn 	if (ret >= 0) {
66202ef12a6SJohannes Thumshirn 		iocb->ki_pos += size;
66302ef12a6SJohannes Thumshirn 		return size;
66402ef12a6SJohannes Thumshirn 	}
66502ef12a6SJohannes Thumshirn 
66602ef12a6SJohannes Thumshirn 	return ret;
66702ef12a6SJohannes Thumshirn }
66802ef12a6SJohannes Thumshirn 
6698dcc1a9dSDamien Le Moal /*
6708dcc1a9dSDamien Le Moal  * Handle direct writes. For sequential zone files, this is the only possible
6718dcc1a9dSDamien Le Moal  * write path. For these files, check that the user is issuing writes
6728dcc1a9dSDamien Le Moal  * sequentially from the end of the file. This code assumes that the block layer
6738dcc1a9dSDamien Le Moal  * delivers write requests to the device in sequential order. This is always the
6748dcc1a9dSDamien Le Moal  * case if a block IO scheduler implementing the ELEVATOR_F_ZBD_SEQ_WRITE
6758dcc1a9dSDamien Le Moal  * elevator feature is being used (e.g. mq-deadline). The block layer always
6768dcc1a9dSDamien Le Moal  * automatically select such an elevator for zoned block devices during the
6778dcc1a9dSDamien Le Moal  * device initialization.
6788dcc1a9dSDamien Le Moal  */
6798dcc1a9dSDamien Le Moal static ssize_t zonefs_file_dio_write(struct kiocb *iocb, struct iov_iter *from)
6808dcc1a9dSDamien Le Moal {
6818dcc1a9dSDamien Le Moal 	struct inode *inode = file_inode(iocb->ki_filp);
6828dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
6838dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
68402ef12a6SJohannes Thumshirn 	bool sync = is_sync_kiocb(iocb);
68502ef12a6SJohannes Thumshirn 	bool append = false;
6868dcc1a9dSDamien Le Moal 	size_t count;
6878dcc1a9dSDamien Le Moal 	ssize_t ret;
6888dcc1a9dSDamien Le Moal 
6898dcc1a9dSDamien Le Moal 	/*
6907c69eb84SChristoph Hellwig 	 * For async direct IOs to sequential zone files, refuse IOCB_NOWAIT
6918dcc1a9dSDamien Le Moal 	 * as this can cause write reordering (e.g. the first aio gets EAGAIN
6928dcc1a9dSDamien Le Moal 	 * on the inode lock but the second goes through but is now unaligned).
6938dcc1a9dSDamien Le Moal 	 */
69402ef12a6SJohannes Thumshirn 	if (zi->i_ztype == ZONEFS_ZTYPE_SEQ && !sync &&
6957c69eb84SChristoph Hellwig 	    (iocb->ki_flags & IOCB_NOWAIT))
6967c69eb84SChristoph Hellwig 		return -EOPNOTSUPP;
6978dcc1a9dSDamien Le Moal 
6988dcc1a9dSDamien Le Moal 	if (iocb->ki_flags & IOCB_NOWAIT) {
6998dcc1a9dSDamien Le Moal 		if (!inode_trylock(inode))
7008dcc1a9dSDamien Le Moal 			return -EAGAIN;
7018dcc1a9dSDamien Le Moal 	} else {
7028dcc1a9dSDamien Le Moal 		inode_lock(inode);
7038dcc1a9dSDamien Le Moal 	}
7048dcc1a9dSDamien Le Moal 
7058dcc1a9dSDamien Le Moal 	ret = generic_write_checks(iocb, from);
7068dcc1a9dSDamien Le Moal 	if (ret <= 0)
7078dcc1a9dSDamien Le Moal 		goto inode_unlock;
7088dcc1a9dSDamien Le Moal 
7098dcc1a9dSDamien Le Moal 	iov_iter_truncate(from, zi->i_max_size - iocb->ki_pos);
7108dcc1a9dSDamien Le Moal 	count = iov_iter_count(from);
7118dcc1a9dSDamien Le Moal 
7128dcc1a9dSDamien Le Moal 	if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) {
7138dcc1a9dSDamien Le Moal 		ret = -EINVAL;
7148dcc1a9dSDamien Le Moal 		goto inode_unlock;
7158dcc1a9dSDamien Le Moal 	}
7168dcc1a9dSDamien Le Moal 
7178dcc1a9dSDamien Le Moal 	/* Enforce sequential writes (append only) in sequential zones */
71802ef12a6SJohannes Thumshirn 	if (zi->i_ztype == ZONEFS_ZTYPE_SEQ) {
7198dcc1a9dSDamien Le Moal 		mutex_lock(&zi->i_truncate_mutex);
72002ef12a6SJohannes Thumshirn 		if (iocb->ki_pos != zi->i_wpoffset) {
7218dcc1a9dSDamien Le Moal 			mutex_unlock(&zi->i_truncate_mutex);
7228dcc1a9dSDamien Le Moal 			ret = -EINVAL;
7238dcc1a9dSDamien Le Moal 			goto inode_unlock;
7248dcc1a9dSDamien Le Moal 		}
7258dcc1a9dSDamien Le Moal 		mutex_unlock(&zi->i_truncate_mutex);
72602ef12a6SJohannes Thumshirn 		append = sync;
72702ef12a6SJohannes Thumshirn 	}
7288dcc1a9dSDamien Le Moal 
72902ef12a6SJohannes Thumshirn 	if (append)
73002ef12a6SJohannes Thumshirn 		ret = zonefs_file_dio_append(iocb, from);
73102ef12a6SJohannes Thumshirn 	else
7328dcc1a9dSDamien Le Moal 		ret = iomap_dio_rw(iocb, from, &zonefs_iomap_ops,
73302ef12a6SJohannes Thumshirn 				   &zonefs_write_dio_ops, sync);
7348dcc1a9dSDamien Le Moal 	if (zi->i_ztype == ZONEFS_ZTYPE_SEQ &&
7358dcc1a9dSDamien Le Moal 	    (ret > 0 || ret == -EIOCBQUEUED)) {
7368dcc1a9dSDamien Le Moal 		if (ret > 0)
7378dcc1a9dSDamien Le Moal 			count = ret;
7388dcc1a9dSDamien Le Moal 		mutex_lock(&zi->i_truncate_mutex);
7398dcc1a9dSDamien Le Moal 		zi->i_wpoffset += count;
7408dcc1a9dSDamien Le Moal 		mutex_unlock(&zi->i_truncate_mutex);
7418dcc1a9dSDamien Le Moal 	}
7428dcc1a9dSDamien Le Moal 
7438dcc1a9dSDamien Le Moal inode_unlock:
7448dcc1a9dSDamien Le Moal 	inode_unlock(inode);
7458dcc1a9dSDamien Le Moal 
7468dcc1a9dSDamien Le Moal 	return ret;
7478dcc1a9dSDamien Le Moal }
7488dcc1a9dSDamien Le Moal 
7498dcc1a9dSDamien Le Moal static ssize_t zonefs_file_buffered_write(struct kiocb *iocb,
7508dcc1a9dSDamien Le Moal 					  struct iov_iter *from)
7518dcc1a9dSDamien Le Moal {
7528dcc1a9dSDamien Le Moal 	struct inode *inode = file_inode(iocb->ki_filp);
7538dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
7548dcc1a9dSDamien Le Moal 	ssize_t ret;
7558dcc1a9dSDamien Le Moal 
7568dcc1a9dSDamien Le Moal 	/*
7578dcc1a9dSDamien Le Moal 	 * Direct IO writes are mandatory for sequential zone files so that the
7588dcc1a9dSDamien Le Moal 	 * write IO issuing order is preserved.
7598dcc1a9dSDamien Le Moal 	 */
7608dcc1a9dSDamien Le Moal 	if (zi->i_ztype != ZONEFS_ZTYPE_CNV)
7618dcc1a9dSDamien Le Moal 		return -EIO;
7628dcc1a9dSDamien Le Moal 
7638dcc1a9dSDamien Le Moal 	if (iocb->ki_flags & IOCB_NOWAIT) {
7648dcc1a9dSDamien Le Moal 		if (!inode_trylock(inode))
7658dcc1a9dSDamien Le Moal 			return -EAGAIN;
7668dcc1a9dSDamien Le Moal 	} else {
7678dcc1a9dSDamien Le Moal 		inode_lock(inode);
7688dcc1a9dSDamien Le Moal 	}
7698dcc1a9dSDamien Le Moal 
7708dcc1a9dSDamien Le Moal 	ret = generic_write_checks(iocb, from);
7718dcc1a9dSDamien Le Moal 	if (ret <= 0)
7728dcc1a9dSDamien Le Moal 		goto inode_unlock;
7738dcc1a9dSDamien Le Moal 
7748dcc1a9dSDamien Le Moal 	iov_iter_truncate(from, zi->i_max_size - iocb->ki_pos);
7758dcc1a9dSDamien Le Moal 
7768dcc1a9dSDamien Le Moal 	ret = iomap_file_buffered_write(iocb, from, &zonefs_iomap_ops);
7778dcc1a9dSDamien Le Moal 	if (ret > 0)
7788dcc1a9dSDamien Le Moal 		iocb->ki_pos += ret;
7798dcc1a9dSDamien Le Moal 	else if (ret == -EIO)
7808dcc1a9dSDamien Le Moal 		zonefs_io_error(inode, true);
7818dcc1a9dSDamien Le Moal 
7828dcc1a9dSDamien Le Moal inode_unlock:
7838dcc1a9dSDamien Le Moal 	inode_unlock(inode);
7848dcc1a9dSDamien Le Moal 	if (ret > 0)
7858dcc1a9dSDamien Le Moal 		ret = generic_write_sync(iocb, ret);
7868dcc1a9dSDamien Le Moal 
7878dcc1a9dSDamien Le Moal 	return ret;
7888dcc1a9dSDamien Le Moal }
7898dcc1a9dSDamien Le Moal 
7908dcc1a9dSDamien Le Moal static ssize_t zonefs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
7918dcc1a9dSDamien Le Moal {
7928dcc1a9dSDamien Le Moal 	struct inode *inode = file_inode(iocb->ki_filp);
7938dcc1a9dSDamien Le Moal 
7948dcc1a9dSDamien Le Moal 	if (unlikely(IS_IMMUTABLE(inode)))
7958dcc1a9dSDamien Le Moal 		return -EPERM;
7968dcc1a9dSDamien Le Moal 
7978dcc1a9dSDamien Le Moal 	if (sb_rdonly(inode->i_sb))
7988dcc1a9dSDamien Le Moal 		return -EROFS;
7998dcc1a9dSDamien Le Moal 
8008dcc1a9dSDamien Le Moal 	/* Write operations beyond the zone size are not allowed */
8018dcc1a9dSDamien Le Moal 	if (iocb->ki_pos >= ZONEFS_I(inode)->i_max_size)
8028dcc1a9dSDamien Le Moal 		return -EFBIG;
8038dcc1a9dSDamien Le Moal 
80460263d58SChristoph Hellwig 	if (iocb->ki_flags & IOCB_DIRECT) {
80560263d58SChristoph Hellwig 		ssize_t ret = zonefs_file_dio_write(iocb, from);
80660263d58SChristoph Hellwig 		if (ret != -ENOTBLK)
80760263d58SChristoph Hellwig 			return ret;
80860263d58SChristoph Hellwig 	}
8098dcc1a9dSDamien Le Moal 
8108dcc1a9dSDamien Le Moal 	return zonefs_file_buffered_write(iocb, from);
8118dcc1a9dSDamien Le Moal }
8128dcc1a9dSDamien Le Moal 
8138dcc1a9dSDamien Le Moal static int zonefs_file_read_dio_end_io(struct kiocb *iocb, ssize_t size,
8148dcc1a9dSDamien Le Moal 				       int error, unsigned int flags)
8158dcc1a9dSDamien Le Moal {
8168dcc1a9dSDamien Le Moal 	if (error) {
8178dcc1a9dSDamien Le Moal 		zonefs_io_error(file_inode(iocb->ki_filp), false);
8188dcc1a9dSDamien Le Moal 		return error;
8198dcc1a9dSDamien Le Moal 	}
8208dcc1a9dSDamien Le Moal 
8218dcc1a9dSDamien Le Moal 	return 0;
8228dcc1a9dSDamien Le Moal }
8238dcc1a9dSDamien Le Moal 
8248dcc1a9dSDamien Le Moal static const struct iomap_dio_ops zonefs_read_dio_ops = {
8258dcc1a9dSDamien Le Moal 	.end_io			= zonefs_file_read_dio_end_io,
8268dcc1a9dSDamien Le Moal };
8278dcc1a9dSDamien Le Moal 
8288dcc1a9dSDamien Le Moal static ssize_t zonefs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
8298dcc1a9dSDamien Le Moal {
8308dcc1a9dSDamien Le Moal 	struct inode *inode = file_inode(iocb->ki_filp);
8318dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
8328dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
8338dcc1a9dSDamien Le Moal 	loff_t isize;
8348dcc1a9dSDamien Le Moal 	ssize_t ret;
8358dcc1a9dSDamien Le Moal 
8368dcc1a9dSDamien Le Moal 	/* Offline zones cannot be read */
8378dcc1a9dSDamien Le Moal 	if (unlikely(IS_IMMUTABLE(inode) && !(inode->i_mode & 0777)))
8388dcc1a9dSDamien Le Moal 		return -EPERM;
8398dcc1a9dSDamien Le Moal 
8408dcc1a9dSDamien Le Moal 	if (iocb->ki_pos >= zi->i_max_size)
8418dcc1a9dSDamien Le Moal 		return 0;
8428dcc1a9dSDamien Le Moal 
8438dcc1a9dSDamien Le Moal 	if (iocb->ki_flags & IOCB_NOWAIT) {
8448dcc1a9dSDamien Le Moal 		if (!inode_trylock_shared(inode))
8458dcc1a9dSDamien Le Moal 			return -EAGAIN;
8468dcc1a9dSDamien Le Moal 	} else {
8478dcc1a9dSDamien Le Moal 		inode_lock_shared(inode);
8488dcc1a9dSDamien Le Moal 	}
8498dcc1a9dSDamien Le Moal 
8508dcc1a9dSDamien Le Moal 	/* Limit read operations to written data */
8518dcc1a9dSDamien Le Moal 	mutex_lock(&zi->i_truncate_mutex);
8528dcc1a9dSDamien Le Moal 	isize = i_size_read(inode);
8538dcc1a9dSDamien Le Moal 	if (iocb->ki_pos >= isize) {
8548dcc1a9dSDamien Le Moal 		mutex_unlock(&zi->i_truncate_mutex);
8558dcc1a9dSDamien Le Moal 		ret = 0;
8568dcc1a9dSDamien Le Moal 		goto inode_unlock;
8578dcc1a9dSDamien Le Moal 	}
8588dcc1a9dSDamien Le Moal 	iov_iter_truncate(to, isize - iocb->ki_pos);
8598dcc1a9dSDamien Le Moal 	mutex_unlock(&zi->i_truncate_mutex);
8608dcc1a9dSDamien Le Moal 
8618dcc1a9dSDamien Le Moal 	if (iocb->ki_flags & IOCB_DIRECT) {
8628dcc1a9dSDamien Le Moal 		size_t count = iov_iter_count(to);
8638dcc1a9dSDamien Le Moal 
8648dcc1a9dSDamien Le Moal 		if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) {
8658dcc1a9dSDamien Le Moal 			ret = -EINVAL;
8668dcc1a9dSDamien Le Moal 			goto inode_unlock;
8678dcc1a9dSDamien Le Moal 		}
8688dcc1a9dSDamien Le Moal 		file_accessed(iocb->ki_filp);
8698dcc1a9dSDamien Le Moal 		ret = iomap_dio_rw(iocb, to, &zonefs_iomap_ops,
8708dcc1a9dSDamien Le Moal 				   &zonefs_read_dio_ops, is_sync_kiocb(iocb));
8718dcc1a9dSDamien Le Moal 	} else {
8728dcc1a9dSDamien Le Moal 		ret = generic_file_read_iter(iocb, to);
8738dcc1a9dSDamien Le Moal 		if (ret == -EIO)
8748dcc1a9dSDamien Le Moal 			zonefs_io_error(inode, false);
8758dcc1a9dSDamien Le Moal 	}
8768dcc1a9dSDamien Le Moal 
8778dcc1a9dSDamien Le Moal inode_unlock:
8788dcc1a9dSDamien Le Moal 	inode_unlock_shared(inode);
8798dcc1a9dSDamien Le Moal 
8808dcc1a9dSDamien Le Moal 	return ret;
8818dcc1a9dSDamien Le Moal }
8828dcc1a9dSDamien Le Moal 
8838dcc1a9dSDamien Le Moal static const struct file_operations zonefs_file_operations = {
8848dcc1a9dSDamien Le Moal 	.open		= generic_file_open,
8858dcc1a9dSDamien Le Moal 	.fsync		= zonefs_file_fsync,
8868dcc1a9dSDamien Le Moal 	.mmap		= zonefs_file_mmap,
8878dcc1a9dSDamien Le Moal 	.llseek		= zonefs_file_llseek,
8888dcc1a9dSDamien Le Moal 	.read_iter	= zonefs_file_read_iter,
8898dcc1a9dSDamien Le Moal 	.write_iter	= zonefs_file_write_iter,
8908dcc1a9dSDamien Le Moal 	.splice_read	= generic_file_splice_read,
8918dcc1a9dSDamien Le Moal 	.splice_write	= iter_file_splice_write,
8928dcc1a9dSDamien Le Moal 	.iopoll		= iomap_dio_iopoll,
8938dcc1a9dSDamien Le Moal };
8948dcc1a9dSDamien Le Moal 
8958dcc1a9dSDamien Le Moal static struct kmem_cache *zonefs_inode_cachep;
8968dcc1a9dSDamien Le Moal 
8978dcc1a9dSDamien Le Moal static struct inode *zonefs_alloc_inode(struct super_block *sb)
8988dcc1a9dSDamien Le Moal {
8998dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi;
9008dcc1a9dSDamien Le Moal 
9018dcc1a9dSDamien Le Moal 	zi = kmem_cache_alloc(zonefs_inode_cachep, GFP_KERNEL);
9028dcc1a9dSDamien Le Moal 	if (!zi)
9038dcc1a9dSDamien Le Moal 		return NULL;
9048dcc1a9dSDamien Le Moal 
9058dcc1a9dSDamien Le Moal 	inode_init_once(&zi->i_vnode);
9068dcc1a9dSDamien Le Moal 	mutex_init(&zi->i_truncate_mutex);
9078dcc1a9dSDamien Le Moal 	init_rwsem(&zi->i_mmap_sem);
9088dcc1a9dSDamien Le Moal 
9098dcc1a9dSDamien Le Moal 	return &zi->i_vnode;
9108dcc1a9dSDamien Le Moal }
9118dcc1a9dSDamien Le Moal 
9128dcc1a9dSDamien Le Moal static void zonefs_free_inode(struct inode *inode)
9138dcc1a9dSDamien Le Moal {
9148dcc1a9dSDamien Le Moal 	kmem_cache_free(zonefs_inode_cachep, ZONEFS_I(inode));
9158dcc1a9dSDamien Le Moal }
9168dcc1a9dSDamien Le Moal 
9178dcc1a9dSDamien Le Moal /*
9188dcc1a9dSDamien Le Moal  * File system stat.
9198dcc1a9dSDamien Le Moal  */
9208dcc1a9dSDamien Le Moal static int zonefs_statfs(struct dentry *dentry, struct kstatfs *buf)
9218dcc1a9dSDamien Le Moal {
9228dcc1a9dSDamien Le Moal 	struct super_block *sb = dentry->d_sb;
9238dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
9248dcc1a9dSDamien Le Moal 	enum zonefs_ztype t;
9258dcc1a9dSDamien Le Moal 	u64 fsid;
9268dcc1a9dSDamien Le Moal 
9278dcc1a9dSDamien Le Moal 	buf->f_type = ZONEFS_MAGIC;
9288dcc1a9dSDamien Le Moal 	buf->f_bsize = sb->s_blocksize;
9298dcc1a9dSDamien Le Moal 	buf->f_namelen = ZONEFS_NAME_MAX;
9308dcc1a9dSDamien Le Moal 
9318dcc1a9dSDamien Le Moal 	spin_lock(&sbi->s_lock);
9328dcc1a9dSDamien Le Moal 
9338dcc1a9dSDamien Le Moal 	buf->f_blocks = sbi->s_blocks;
9348dcc1a9dSDamien Le Moal 	if (WARN_ON(sbi->s_used_blocks > sbi->s_blocks))
9358dcc1a9dSDamien Le Moal 		buf->f_bfree = 0;
9368dcc1a9dSDamien Le Moal 	else
9378dcc1a9dSDamien Le Moal 		buf->f_bfree = buf->f_blocks - sbi->s_used_blocks;
9388dcc1a9dSDamien Le Moal 	buf->f_bavail = buf->f_bfree;
9398dcc1a9dSDamien Le Moal 
9408dcc1a9dSDamien Le Moal 	for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) {
9418dcc1a9dSDamien Le Moal 		if (sbi->s_nr_files[t])
9428dcc1a9dSDamien Le Moal 			buf->f_files += sbi->s_nr_files[t] + 1;
9438dcc1a9dSDamien Le Moal 	}
9448dcc1a9dSDamien Le Moal 	buf->f_ffree = 0;
9458dcc1a9dSDamien Le Moal 
9468dcc1a9dSDamien Le Moal 	spin_unlock(&sbi->s_lock);
9478dcc1a9dSDamien Le Moal 
9488dcc1a9dSDamien Le Moal 	fsid = le64_to_cpup((void *)sbi->s_uuid.b) ^
9498dcc1a9dSDamien Le Moal 		le64_to_cpup((void *)sbi->s_uuid.b + sizeof(u64));
9508dcc1a9dSDamien Le Moal 	buf->f_fsid.val[0] = (u32)fsid;
9518dcc1a9dSDamien Le Moal 	buf->f_fsid.val[1] = (u32)(fsid >> 32);
9528dcc1a9dSDamien Le Moal 
9538dcc1a9dSDamien Le Moal 	return 0;
9548dcc1a9dSDamien Le Moal }
9558dcc1a9dSDamien Le Moal 
9568dcc1a9dSDamien Le Moal enum {
9578dcc1a9dSDamien Le Moal 	Opt_errors_ro, Opt_errors_zro, Opt_errors_zol, Opt_errors_repair,
9588dcc1a9dSDamien Le Moal 	Opt_err,
9598dcc1a9dSDamien Le Moal };
9608dcc1a9dSDamien Le Moal 
9618dcc1a9dSDamien Le Moal static const match_table_t tokens = {
9628dcc1a9dSDamien Le Moal 	{ Opt_errors_ro,	"errors=remount-ro"},
9638dcc1a9dSDamien Le Moal 	{ Opt_errors_zro,	"errors=zone-ro"},
9648dcc1a9dSDamien Le Moal 	{ Opt_errors_zol,	"errors=zone-offline"},
9658dcc1a9dSDamien Le Moal 	{ Opt_errors_repair,	"errors=repair"},
9668dcc1a9dSDamien Le Moal 	{ Opt_err,		NULL}
9678dcc1a9dSDamien Le Moal };
9688dcc1a9dSDamien Le Moal 
9698dcc1a9dSDamien Le Moal static int zonefs_parse_options(struct super_block *sb, char *options)
9708dcc1a9dSDamien Le Moal {
9718dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
9728dcc1a9dSDamien Le Moal 	substring_t args[MAX_OPT_ARGS];
9738dcc1a9dSDamien Le Moal 	char *p;
9748dcc1a9dSDamien Le Moal 
9758dcc1a9dSDamien Le Moal 	if (!options)
9768dcc1a9dSDamien Le Moal 		return 0;
9778dcc1a9dSDamien Le Moal 
9788dcc1a9dSDamien Le Moal 	while ((p = strsep(&options, ",")) != NULL) {
9798dcc1a9dSDamien Le Moal 		int token;
9808dcc1a9dSDamien Le Moal 
9818dcc1a9dSDamien Le Moal 		if (!*p)
9828dcc1a9dSDamien Le Moal 			continue;
9838dcc1a9dSDamien Le Moal 
9848dcc1a9dSDamien Le Moal 		token = match_token(p, tokens, args);
9858dcc1a9dSDamien Le Moal 		switch (token) {
9868dcc1a9dSDamien Le Moal 		case Opt_errors_ro:
9878dcc1a9dSDamien Le Moal 			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
9888dcc1a9dSDamien Le Moal 			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_RO;
9898dcc1a9dSDamien Le Moal 			break;
9908dcc1a9dSDamien Le Moal 		case Opt_errors_zro:
9918dcc1a9dSDamien Le Moal 			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
9928dcc1a9dSDamien Le Moal 			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZRO;
9938dcc1a9dSDamien Le Moal 			break;
9948dcc1a9dSDamien Le Moal 		case Opt_errors_zol:
9958dcc1a9dSDamien Le Moal 			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
9968dcc1a9dSDamien Le Moal 			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZOL;
9978dcc1a9dSDamien Le Moal 			break;
9988dcc1a9dSDamien Le Moal 		case Opt_errors_repair:
9998dcc1a9dSDamien Le Moal 			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
10008dcc1a9dSDamien Le Moal 			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_REPAIR;
10018dcc1a9dSDamien Le Moal 			break;
10028dcc1a9dSDamien Le Moal 		default:
10038dcc1a9dSDamien Le Moal 			return -EINVAL;
10048dcc1a9dSDamien Le Moal 		}
10058dcc1a9dSDamien Le Moal 	}
10068dcc1a9dSDamien Le Moal 
10078dcc1a9dSDamien Le Moal 	return 0;
10088dcc1a9dSDamien Le Moal }
10098dcc1a9dSDamien Le Moal 
10108dcc1a9dSDamien Le Moal static int zonefs_show_options(struct seq_file *seq, struct dentry *root)
10118dcc1a9dSDamien Le Moal {
10128dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(root->d_sb);
10138dcc1a9dSDamien Le Moal 
10148dcc1a9dSDamien Le Moal 	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO)
10158dcc1a9dSDamien Le Moal 		seq_puts(seq, ",errors=remount-ro");
10168dcc1a9dSDamien Le Moal 	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO)
10178dcc1a9dSDamien Le Moal 		seq_puts(seq, ",errors=zone-ro");
10188dcc1a9dSDamien Le Moal 	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL)
10198dcc1a9dSDamien Le Moal 		seq_puts(seq, ",errors=zone-offline");
10208dcc1a9dSDamien Le Moal 	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_REPAIR)
10218dcc1a9dSDamien Le Moal 		seq_puts(seq, ",errors=repair");
10228dcc1a9dSDamien Le Moal 
10238dcc1a9dSDamien Le Moal 	return 0;
10248dcc1a9dSDamien Le Moal }
10258dcc1a9dSDamien Le Moal 
10268dcc1a9dSDamien Le Moal static int zonefs_remount(struct super_block *sb, int *flags, char *data)
10278dcc1a9dSDamien Le Moal {
10288dcc1a9dSDamien Le Moal 	sync_filesystem(sb);
10298dcc1a9dSDamien Le Moal 
10308dcc1a9dSDamien Le Moal 	return zonefs_parse_options(sb, data);
10318dcc1a9dSDamien Le Moal }
10328dcc1a9dSDamien Le Moal 
10338dcc1a9dSDamien Le Moal static const struct super_operations zonefs_sops = {
10348dcc1a9dSDamien Le Moal 	.alloc_inode	= zonefs_alloc_inode,
10358dcc1a9dSDamien Le Moal 	.free_inode	= zonefs_free_inode,
10368dcc1a9dSDamien Le Moal 	.statfs		= zonefs_statfs,
10378dcc1a9dSDamien Le Moal 	.remount_fs	= zonefs_remount,
10388dcc1a9dSDamien Le Moal 	.show_options	= zonefs_show_options,
10398dcc1a9dSDamien Le Moal };
10408dcc1a9dSDamien Le Moal 
10418dcc1a9dSDamien Le Moal static const struct inode_operations zonefs_dir_inode_operations = {
10428dcc1a9dSDamien Le Moal 	.lookup		= simple_lookup,
10438dcc1a9dSDamien Le Moal 	.setattr	= zonefs_inode_setattr,
10448dcc1a9dSDamien Le Moal };
10458dcc1a9dSDamien Le Moal 
10468dcc1a9dSDamien Le Moal static void zonefs_init_dir_inode(struct inode *parent, struct inode *inode,
10478dcc1a9dSDamien Le Moal 				  enum zonefs_ztype type)
10488dcc1a9dSDamien Le Moal {
10498dcc1a9dSDamien Le Moal 	struct super_block *sb = parent->i_sb;
10508dcc1a9dSDamien Le Moal 
10518dcc1a9dSDamien Le Moal 	inode->i_ino = blkdev_nr_zones(sb->s_bdev->bd_disk) + type + 1;
10528dcc1a9dSDamien Le Moal 	inode_init_owner(inode, parent, S_IFDIR | 0555);
10538dcc1a9dSDamien Le Moal 	inode->i_op = &zonefs_dir_inode_operations;
10548dcc1a9dSDamien Le Moal 	inode->i_fop = &simple_dir_operations;
10558dcc1a9dSDamien Le Moal 	set_nlink(inode, 2);
10568dcc1a9dSDamien Le Moal 	inc_nlink(parent);
10578dcc1a9dSDamien Le Moal }
10588dcc1a9dSDamien Le Moal 
10598dcc1a9dSDamien Le Moal static void zonefs_init_file_inode(struct inode *inode, struct blk_zone *zone,
10608dcc1a9dSDamien Le Moal 				   enum zonefs_ztype type)
10618dcc1a9dSDamien Le Moal {
10628dcc1a9dSDamien Le Moal 	struct super_block *sb = inode->i_sb;
10638dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
10648dcc1a9dSDamien Le Moal 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
10658dcc1a9dSDamien Le Moal 
10668dcc1a9dSDamien Le Moal 	inode->i_ino = zone->start >> sbi->s_zone_sectors_shift;
10678dcc1a9dSDamien Le Moal 	inode->i_mode = S_IFREG | sbi->s_perm;
10688dcc1a9dSDamien Le Moal 
10698dcc1a9dSDamien Le Moal 	zi->i_ztype = type;
10708dcc1a9dSDamien Le Moal 	zi->i_zsector = zone->start;
1071e3c3155bSJohannes Thumshirn 	zi->i_zone_size = zone->len << SECTOR_SHIFT;
1072e3c3155bSJohannes Thumshirn 
10738dcc1a9dSDamien Le Moal 	zi->i_max_size = min_t(loff_t, MAX_LFS_FILESIZE,
1074e3c3155bSJohannes Thumshirn 			       zone->capacity << SECTOR_SHIFT);
1075ccf4ad7dSDamien Le Moal 	zi->i_wpoffset = zonefs_check_zone_condition(inode, zone, true, true);
10768dcc1a9dSDamien Le Moal 
10778dcc1a9dSDamien Le Moal 	inode->i_uid = sbi->s_uid;
10788dcc1a9dSDamien Le Moal 	inode->i_gid = sbi->s_gid;
10798dcc1a9dSDamien Le Moal 	inode->i_size = zi->i_wpoffset;
1080e3c3155bSJohannes Thumshirn 	inode->i_blocks = zi->i_max_size >> SECTOR_SHIFT;
10818dcc1a9dSDamien Le Moal 
10828dcc1a9dSDamien Le Moal 	inode->i_op = &zonefs_file_inode_operations;
10838dcc1a9dSDamien Le Moal 	inode->i_fop = &zonefs_file_operations;
10848dcc1a9dSDamien Le Moal 	inode->i_mapping->a_ops = &zonefs_file_aops;
10858dcc1a9dSDamien Le Moal 
10868dcc1a9dSDamien Le Moal 	sb->s_maxbytes = max(zi->i_max_size, sb->s_maxbytes);
10878dcc1a9dSDamien Le Moal 	sbi->s_blocks += zi->i_max_size >> sb->s_blocksize_bits;
10888dcc1a9dSDamien Le Moal 	sbi->s_used_blocks += zi->i_wpoffset >> sb->s_blocksize_bits;
10898dcc1a9dSDamien Le Moal }
10908dcc1a9dSDamien Le Moal 
10918dcc1a9dSDamien Le Moal static struct dentry *zonefs_create_inode(struct dentry *parent,
10928dcc1a9dSDamien Le Moal 					const char *name, struct blk_zone *zone,
10938dcc1a9dSDamien Le Moal 					enum zonefs_ztype type)
10948dcc1a9dSDamien Le Moal {
10958dcc1a9dSDamien Le Moal 	struct inode *dir = d_inode(parent);
10968dcc1a9dSDamien Le Moal 	struct dentry *dentry;
10978dcc1a9dSDamien Le Moal 	struct inode *inode;
10988dcc1a9dSDamien Le Moal 
10998dcc1a9dSDamien Le Moal 	dentry = d_alloc_name(parent, name);
11008dcc1a9dSDamien Le Moal 	if (!dentry)
11018dcc1a9dSDamien Le Moal 		return NULL;
11028dcc1a9dSDamien Le Moal 
11038dcc1a9dSDamien Le Moal 	inode = new_inode(parent->d_sb);
11048dcc1a9dSDamien Le Moal 	if (!inode)
11058dcc1a9dSDamien Le Moal 		goto dput;
11068dcc1a9dSDamien Le Moal 
11078dcc1a9dSDamien Le Moal 	inode->i_ctime = inode->i_mtime = inode->i_atime = dir->i_ctime;
11088dcc1a9dSDamien Le Moal 	if (zone)
11098dcc1a9dSDamien Le Moal 		zonefs_init_file_inode(inode, zone, type);
11108dcc1a9dSDamien Le Moal 	else
11118dcc1a9dSDamien Le Moal 		zonefs_init_dir_inode(dir, inode, type);
11128dcc1a9dSDamien Le Moal 	d_add(dentry, inode);
11138dcc1a9dSDamien Le Moal 	dir->i_size++;
11148dcc1a9dSDamien Le Moal 
11158dcc1a9dSDamien Le Moal 	return dentry;
11168dcc1a9dSDamien Le Moal 
11178dcc1a9dSDamien Le Moal dput:
11188dcc1a9dSDamien Le Moal 	dput(dentry);
11198dcc1a9dSDamien Le Moal 
11208dcc1a9dSDamien Le Moal 	return NULL;
11218dcc1a9dSDamien Le Moal }
11228dcc1a9dSDamien Le Moal 
11238dcc1a9dSDamien Le Moal struct zonefs_zone_data {
11248dcc1a9dSDamien Le Moal 	struct super_block	*sb;
11258dcc1a9dSDamien Le Moal 	unsigned int		nr_zones[ZONEFS_ZTYPE_MAX];
11268dcc1a9dSDamien Le Moal 	struct blk_zone		*zones;
11278dcc1a9dSDamien Le Moal };
11288dcc1a9dSDamien Le Moal 
11298dcc1a9dSDamien Le Moal /*
11308dcc1a9dSDamien Le Moal  * Create a zone group and populate it with zone files.
11318dcc1a9dSDamien Le Moal  */
11328dcc1a9dSDamien Le Moal static int zonefs_create_zgroup(struct zonefs_zone_data *zd,
11338dcc1a9dSDamien Le Moal 				enum zonefs_ztype type)
11348dcc1a9dSDamien Le Moal {
11358dcc1a9dSDamien Le Moal 	struct super_block *sb = zd->sb;
11368dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
11378dcc1a9dSDamien Le Moal 	struct blk_zone *zone, *next, *end;
11388dcc1a9dSDamien Le Moal 	const char *zgroup_name;
11398dcc1a9dSDamien Le Moal 	char *file_name;
11408dcc1a9dSDamien Le Moal 	struct dentry *dir;
11418dcc1a9dSDamien Le Moal 	unsigned int n = 0;
114201b2651cSDamien Le Moal 	int ret;
11438dcc1a9dSDamien Le Moal 
11448dcc1a9dSDamien Le Moal 	/* If the group is empty, there is nothing to do */
11458dcc1a9dSDamien Le Moal 	if (!zd->nr_zones[type])
11468dcc1a9dSDamien Le Moal 		return 0;
11478dcc1a9dSDamien Le Moal 
11488dcc1a9dSDamien Le Moal 	file_name = kmalloc(ZONEFS_NAME_MAX, GFP_KERNEL);
11498dcc1a9dSDamien Le Moal 	if (!file_name)
11508dcc1a9dSDamien Le Moal 		return -ENOMEM;
11518dcc1a9dSDamien Le Moal 
11528dcc1a9dSDamien Le Moal 	if (type == ZONEFS_ZTYPE_CNV)
11538dcc1a9dSDamien Le Moal 		zgroup_name = "cnv";
11548dcc1a9dSDamien Le Moal 	else
11558dcc1a9dSDamien Le Moal 		zgroup_name = "seq";
11568dcc1a9dSDamien Le Moal 
11578dcc1a9dSDamien Le Moal 	dir = zonefs_create_inode(sb->s_root, zgroup_name, NULL, type);
115801b2651cSDamien Le Moal 	if (!dir) {
115901b2651cSDamien Le Moal 		ret = -ENOMEM;
11608dcc1a9dSDamien Le Moal 		goto free;
116101b2651cSDamien Le Moal 	}
11628dcc1a9dSDamien Le Moal 
11638dcc1a9dSDamien Le Moal 	/*
11648dcc1a9dSDamien Le Moal 	 * The first zone contains the super block: skip it.
11658dcc1a9dSDamien Le Moal 	 */
11668dcc1a9dSDamien Le Moal 	end = zd->zones + blkdev_nr_zones(sb->s_bdev->bd_disk);
11678dcc1a9dSDamien Le Moal 	for (zone = &zd->zones[1]; zone < end; zone = next) {
11688dcc1a9dSDamien Le Moal 
11698dcc1a9dSDamien Le Moal 		next = zone + 1;
11708dcc1a9dSDamien Le Moal 		if (zonefs_zone_type(zone) != type)
11718dcc1a9dSDamien Le Moal 			continue;
11728dcc1a9dSDamien Le Moal 
11738dcc1a9dSDamien Le Moal 		/*
11748dcc1a9dSDamien Le Moal 		 * For conventional zones, contiguous zones can be aggregated
11758dcc1a9dSDamien Le Moal 		 * together to form larger files. Note that this overwrites the
11768dcc1a9dSDamien Le Moal 		 * length of the first zone of the set of contiguous zones
11778dcc1a9dSDamien Le Moal 		 * aggregated together. If one offline or read-only zone is
11788dcc1a9dSDamien Le Moal 		 * found, assume that all zones aggregated have the same
11798dcc1a9dSDamien Le Moal 		 * condition.
11808dcc1a9dSDamien Le Moal 		 */
11818dcc1a9dSDamien Le Moal 		if (type == ZONEFS_ZTYPE_CNV &&
11828dcc1a9dSDamien Le Moal 		    (sbi->s_features & ZONEFS_F_AGGRCNV)) {
11838dcc1a9dSDamien Le Moal 			for (; next < end; next++) {
11848dcc1a9dSDamien Le Moal 				if (zonefs_zone_type(next) != type)
11858dcc1a9dSDamien Le Moal 					break;
11868dcc1a9dSDamien Le Moal 				zone->len += next->len;
1187e3c3155bSJohannes Thumshirn 				zone->capacity += next->capacity;
11888dcc1a9dSDamien Le Moal 				if (next->cond == BLK_ZONE_COND_READONLY &&
11898dcc1a9dSDamien Le Moal 				    zone->cond != BLK_ZONE_COND_OFFLINE)
11908dcc1a9dSDamien Le Moal 					zone->cond = BLK_ZONE_COND_READONLY;
11918dcc1a9dSDamien Le Moal 				else if (next->cond == BLK_ZONE_COND_OFFLINE)
11928dcc1a9dSDamien Le Moal 					zone->cond = BLK_ZONE_COND_OFFLINE;
11938dcc1a9dSDamien Le Moal 			}
1194e3c3155bSJohannes Thumshirn 			if (zone->capacity != zone->len) {
1195e3c3155bSJohannes Thumshirn 				zonefs_err(sb, "Invalid conventional zone capacity\n");
1196e3c3155bSJohannes Thumshirn 				ret = -EINVAL;
1197e3c3155bSJohannes Thumshirn 				goto free;
1198e3c3155bSJohannes Thumshirn 			}
11998dcc1a9dSDamien Le Moal 		}
12008dcc1a9dSDamien Le Moal 
12018dcc1a9dSDamien Le Moal 		/*
12028dcc1a9dSDamien Le Moal 		 * Use the file number within its group as file name.
12038dcc1a9dSDamien Le Moal 		 */
12048dcc1a9dSDamien Le Moal 		snprintf(file_name, ZONEFS_NAME_MAX - 1, "%u", n);
120501b2651cSDamien Le Moal 		if (!zonefs_create_inode(dir, file_name, zone, type)) {
120601b2651cSDamien Le Moal 			ret = -ENOMEM;
12078dcc1a9dSDamien Le Moal 			goto free;
120801b2651cSDamien Le Moal 		}
12098dcc1a9dSDamien Le Moal 
12108dcc1a9dSDamien Le Moal 		n++;
12118dcc1a9dSDamien Le Moal 	}
12128dcc1a9dSDamien Le Moal 
12138dcc1a9dSDamien Le Moal 	zonefs_info(sb, "Zone group \"%s\" has %u file%s\n",
12148dcc1a9dSDamien Le Moal 		    zgroup_name, n, n > 1 ? "s" : "");
12158dcc1a9dSDamien Le Moal 
12168dcc1a9dSDamien Le Moal 	sbi->s_nr_files[type] = n;
12178dcc1a9dSDamien Le Moal 	ret = 0;
12188dcc1a9dSDamien Le Moal 
12198dcc1a9dSDamien Le Moal free:
12208dcc1a9dSDamien Le Moal 	kfree(file_name);
12218dcc1a9dSDamien Le Moal 
12228dcc1a9dSDamien Le Moal 	return ret;
12238dcc1a9dSDamien Le Moal }
12248dcc1a9dSDamien Le Moal 
12258dcc1a9dSDamien Le Moal static int zonefs_get_zone_info_cb(struct blk_zone *zone, unsigned int idx,
12268dcc1a9dSDamien Le Moal 				   void *data)
12278dcc1a9dSDamien Le Moal {
12288dcc1a9dSDamien Le Moal 	struct zonefs_zone_data *zd = data;
12298dcc1a9dSDamien Le Moal 
12308dcc1a9dSDamien Le Moal 	/*
12318dcc1a9dSDamien Le Moal 	 * Count the number of usable zones: the first zone at index 0 contains
12328dcc1a9dSDamien Le Moal 	 * the super block and is ignored.
12338dcc1a9dSDamien Le Moal 	 */
12348dcc1a9dSDamien Le Moal 	switch (zone->type) {
12358dcc1a9dSDamien Le Moal 	case BLK_ZONE_TYPE_CONVENTIONAL:
12368dcc1a9dSDamien Le Moal 		zone->wp = zone->start + zone->len;
12378dcc1a9dSDamien Le Moal 		if (idx)
12388dcc1a9dSDamien Le Moal 			zd->nr_zones[ZONEFS_ZTYPE_CNV]++;
12398dcc1a9dSDamien Le Moal 		break;
12408dcc1a9dSDamien Le Moal 	case BLK_ZONE_TYPE_SEQWRITE_REQ:
12418dcc1a9dSDamien Le Moal 	case BLK_ZONE_TYPE_SEQWRITE_PREF:
12428dcc1a9dSDamien Le Moal 		if (idx)
12438dcc1a9dSDamien Le Moal 			zd->nr_zones[ZONEFS_ZTYPE_SEQ]++;
12448dcc1a9dSDamien Le Moal 		break;
12458dcc1a9dSDamien Le Moal 	default:
12468dcc1a9dSDamien Le Moal 		zonefs_err(zd->sb, "Unsupported zone type 0x%x\n",
12478dcc1a9dSDamien Le Moal 			   zone->type);
12488dcc1a9dSDamien Le Moal 		return -EIO;
12498dcc1a9dSDamien Le Moal 	}
12508dcc1a9dSDamien Le Moal 
12518dcc1a9dSDamien Le Moal 	memcpy(&zd->zones[idx], zone, sizeof(struct blk_zone));
12528dcc1a9dSDamien Le Moal 
12538dcc1a9dSDamien Le Moal 	return 0;
12548dcc1a9dSDamien Le Moal }
12558dcc1a9dSDamien Le Moal 
12568dcc1a9dSDamien Le Moal static int zonefs_get_zone_info(struct zonefs_zone_data *zd)
12578dcc1a9dSDamien Le Moal {
12588dcc1a9dSDamien Le Moal 	struct block_device *bdev = zd->sb->s_bdev;
12598dcc1a9dSDamien Le Moal 	int ret;
12608dcc1a9dSDamien Le Moal 
12618dcc1a9dSDamien Le Moal 	zd->zones = kvcalloc(blkdev_nr_zones(bdev->bd_disk),
12628dcc1a9dSDamien Le Moal 			     sizeof(struct blk_zone), GFP_KERNEL);
12638dcc1a9dSDamien Le Moal 	if (!zd->zones)
12648dcc1a9dSDamien Le Moal 		return -ENOMEM;
12658dcc1a9dSDamien Le Moal 
12668dcc1a9dSDamien Le Moal 	/* Get zones information from the device */
12678dcc1a9dSDamien Le Moal 	ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES,
12688dcc1a9dSDamien Le Moal 				  zonefs_get_zone_info_cb, zd);
12698dcc1a9dSDamien Le Moal 	if (ret < 0) {
12708dcc1a9dSDamien Le Moal 		zonefs_err(zd->sb, "Zone report failed %d\n", ret);
12718dcc1a9dSDamien Le Moal 		return ret;
12728dcc1a9dSDamien Le Moal 	}
12738dcc1a9dSDamien Le Moal 
12748dcc1a9dSDamien Le Moal 	if (ret != blkdev_nr_zones(bdev->bd_disk)) {
12758dcc1a9dSDamien Le Moal 		zonefs_err(zd->sb, "Invalid zone report (%d/%u zones)\n",
12768dcc1a9dSDamien Le Moal 			   ret, blkdev_nr_zones(bdev->bd_disk));
12778dcc1a9dSDamien Le Moal 		return -EIO;
12788dcc1a9dSDamien Le Moal 	}
12798dcc1a9dSDamien Le Moal 
12808dcc1a9dSDamien Le Moal 	return 0;
12818dcc1a9dSDamien Le Moal }
12828dcc1a9dSDamien Le Moal 
12838dcc1a9dSDamien Le Moal static inline void zonefs_cleanup_zone_info(struct zonefs_zone_data *zd)
12848dcc1a9dSDamien Le Moal {
12858dcc1a9dSDamien Le Moal 	kvfree(zd->zones);
12868dcc1a9dSDamien Le Moal }
12878dcc1a9dSDamien Le Moal 
12888dcc1a9dSDamien Le Moal /*
12898dcc1a9dSDamien Le Moal  * Read super block information from the device.
12908dcc1a9dSDamien Le Moal  */
12918dcc1a9dSDamien Le Moal static int zonefs_read_super(struct super_block *sb)
12928dcc1a9dSDamien Le Moal {
12938dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
12948dcc1a9dSDamien Le Moal 	struct zonefs_super *super;
12958dcc1a9dSDamien Le Moal 	u32 crc, stored_crc;
12968dcc1a9dSDamien Le Moal 	struct page *page;
12978dcc1a9dSDamien Le Moal 	struct bio_vec bio_vec;
12988dcc1a9dSDamien Le Moal 	struct bio bio;
12998dcc1a9dSDamien Le Moal 	int ret;
13008dcc1a9dSDamien Le Moal 
13018dcc1a9dSDamien Le Moal 	page = alloc_page(GFP_KERNEL);
13028dcc1a9dSDamien Le Moal 	if (!page)
13038dcc1a9dSDamien Le Moal 		return -ENOMEM;
13048dcc1a9dSDamien Le Moal 
13058dcc1a9dSDamien Le Moal 	bio_init(&bio, &bio_vec, 1);
13068dcc1a9dSDamien Le Moal 	bio.bi_iter.bi_sector = 0;
13078dcc1a9dSDamien Le Moal 	bio.bi_opf = REQ_OP_READ;
13088dcc1a9dSDamien Le Moal 	bio_set_dev(&bio, sb->s_bdev);
13098dcc1a9dSDamien Le Moal 	bio_add_page(&bio, page, PAGE_SIZE, 0);
13108dcc1a9dSDamien Le Moal 
13118dcc1a9dSDamien Le Moal 	ret = submit_bio_wait(&bio);
13128dcc1a9dSDamien Le Moal 	if (ret)
13138dcc1a9dSDamien Le Moal 		goto free_page;
13148dcc1a9dSDamien Le Moal 
13158dcc1a9dSDamien Le Moal 	super = kmap(page);
13168dcc1a9dSDamien Le Moal 
13178dcc1a9dSDamien Le Moal 	ret = -EINVAL;
13188dcc1a9dSDamien Le Moal 	if (le32_to_cpu(super->s_magic) != ZONEFS_MAGIC)
13198dcc1a9dSDamien Le Moal 		goto unmap;
13208dcc1a9dSDamien Le Moal 
13218dcc1a9dSDamien Le Moal 	stored_crc = le32_to_cpu(super->s_crc);
13228dcc1a9dSDamien Le Moal 	super->s_crc = 0;
13238dcc1a9dSDamien Le Moal 	crc = crc32(~0U, (unsigned char *)super, sizeof(struct zonefs_super));
13248dcc1a9dSDamien Le Moal 	if (crc != stored_crc) {
13258dcc1a9dSDamien Le Moal 		zonefs_err(sb, "Invalid checksum (Expected 0x%08x, got 0x%08x)",
13268dcc1a9dSDamien Le Moal 			   crc, stored_crc);
13278dcc1a9dSDamien Le Moal 		goto unmap;
13288dcc1a9dSDamien Le Moal 	}
13298dcc1a9dSDamien Le Moal 
13308dcc1a9dSDamien Le Moal 	sbi->s_features = le64_to_cpu(super->s_features);
13318dcc1a9dSDamien Le Moal 	if (sbi->s_features & ~ZONEFS_F_DEFINED_FEATURES) {
13328dcc1a9dSDamien Le Moal 		zonefs_err(sb, "Unknown features set 0x%llx\n",
13338dcc1a9dSDamien Le Moal 			   sbi->s_features);
13348dcc1a9dSDamien Le Moal 		goto unmap;
13358dcc1a9dSDamien Le Moal 	}
13368dcc1a9dSDamien Le Moal 
13378dcc1a9dSDamien Le Moal 	if (sbi->s_features & ZONEFS_F_UID) {
13388dcc1a9dSDamien Le Moal 		sbi->s_uid = make_kuid(current_user_ns(),
13398dcc1a9dSDamien Le Moal 				       le32_to_cpu(super->s_uid));
13408dcc1a9dSDamien Le Moal 		if (!uid_valid(sbi->s_uid)) {
13418dcc1a9dSDamien Le Moal 			zonefs_err(sb, "Invalid UID feature\n");
13428dcc1a9dSDamien Le Moal 			goto unmap;
13438dcc1a9dSDamien Le Moal 		}
13448dcc1a9dSDamien Le Moal 	}
13458dcc1a9dSDamien Le Moal 
13468dcc1a9dSDamien Le Moal 	if (sbi->s_features & ZONEFS_F_GID) {
13478dcc1a9dSDamien Le Moal 		sbi->s_gid = make_kgid(current_user_ns(),
13488dcc1a9dSDamien Le Moal 				       le32_to_cpu(super->s_gid));
13498dcc1a9dSDamien Le Moal 		if (!gid_valid(sbi->s_gid)) {
13508dcc1a9dSDamien Le Moal 			zonefs_err(sb, "Invalid GID feature\n");
13518dcc1a9dSDamien Le Moal 			goto unmap;
13528dcc1a9dSDamien Le Moal 		}
13538dcc1a9dSDamien Le Moal 	}
13548dcc1a9dSDamien Le Moal 
13558dcc1a9dSDamien Le Moal 	if (sbi->s_features & ZONEFS_F_PERM)
13568dcc1a9dSDamien Le Moal 		sbi->s_perm = le32_to_cpu(super->s_perm);
13578dcc1a9dSDamien Le Moal 
13588dcc1a9dSDamien Le Moal 	if (memchr_inv(super->s_reserved, 0, sizeof(super->s_reserved))) {
13598dcc1a9dSDamien Le Moal 		zonefs_err(sb, "Reserved area is being used\n");
13608dcc1a9dSDamien Le Moal 		goto unmap;
13618dcc1a9dSDamien Le Moal 	}
13628dcc1a9dSDamien Le Moal 
1363568776f9SAndy Shevchenko 	import_uuid(&sbi->s_uuid, super->s_uuid);
13648dcc1a9dSDamien Le Moal 	ret = 0;
13658dcc1a9dSDamien Le Moal 
13668dcc1a9dSDamien Le Moal unmap:
13678dcc1a9dSDamien Le Moal 	kunmap(page);
13688dcc1a9dSDamien Le Moal free_page:
13698dcc1a9dSDamien Le Moal 	__free_page(page);
13708dcc1a9dSDamien Le Moal 
13718dcc1a9dSDamien Le Moal 	return ret;
13728dcc1a9dSDamien Le Moal }
13738dcc1a9dSDamien Le Moal 
13748dcc1a9dSDamien Le Moal /*
13758dcc1a9dSDamien Le Moal  * Check that the device is zoned. If it is, get the list of zones and create
13768dcc1a9dSDamien Le Moal  * sub-directories and files according to the device zone configuration and
13778dcc1a9dSDamien Le Moal  * format options.
13788dcc1a9dSDamien Le Moal  */
13798dcc1a9dSDamien Le Moal static int zonefs_fill_super(struct super_block *sb, void *data, int silent)
13808dcc1a9dSDamien Le Moal {
13818dcc1a9dSDamien Le Moal 	struct zonefs_zone_data zd;
13828dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi;
13838dcc1a9dSDamien Le Moal 	struct inode *inode;
13848dcc1a9dSDamien Le Moal 	enum zonefs_ztype t;
13858dcc1a9dSDamien Le Moal 	int ret;
13868dcc1a9dSDamien Le Moal 
13878dcc1a9dSDamien Le Moal 	if (!bdev_is_zoned(sb->s_bdev)) {
13888dcc1a9dSDamien Le Moal 		zonefs_err(sb, "Not a zoned block device\n");
13898dcc1a9dSDamien Le Moal 		return -EINVAL;
13908dcc1a9dSDamien Le Moal 	}
13918dcc1a9dSDamien Le Moal 
13928dcc1a9dSDamien Le Moal 	/*
13938dcc1a9dSDamien Le Moal 	 * Initialize super block information: the maximum file size is updated
13948dcc1a9dSDamien Le Moal 	 * when the zone files are created so that the format option
13958dcc1a9dSDamien Le Moal 	 * ZONEFS_F_AGGRCNV which increases the maximum file size of a file
13968dcc1a9dSDamien Le Moal 	 * beyond the zone size is taken into account.
13978dcc1a9dSDamien Le Moal 	 */
13988dcc1a9dSDamien Le Moal 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
13998dcc1a9dSDamien Le Moal 	if (!sbi)
14008dcc1a9dSDamien Le Moal 		return -ENOMEM;
14018dcc1a9dSDamien Le Moal 
14028dcc1a9dSDamien Le Moal 	spin_lock_init(&sbi->s_lock);
14038dcc1a9dSDamien Le Moal 	sb->s_fs_info = sbi;
14048dcc1a9dSDamien Le Moal 	sb->s_magic = ZONEFS_MAGIC;
14058dcc1a9dSDamien Le Moal 	sb->s_maxbytes = 0;
14068dcc1a9dSDamien Le Moal 	sb->s_op = &zonefs_sops;
14078dcc1a9dSDamien Le Moal 	sb->s_time_gran	= 1;
14088dcc1a9dSDamien Le Moal 
14098dcc1a9dSDamien Le Moal 	/*
14108dcc1a9dSDamien Le Moal 	 * The block size is set to the device physical sector size to ensure
14118dcc1a9dSDamien Le Moal 	 * that write operations on 512e devices (512B logical block and 4KB
14128dcc1a9dSDamien Le Moal 	 * physical block) are always aligned to the device physical blocks,
14138dcc1a9dSDamien Le Moal 	 * as mandated by the ZBC/ZAC specifications.
14148dcc1a9dSDamien Le Moal 	 */
14158dcc1a9dSDamien Le Moal 	sb_set_blocksize(sb, bdev_physical_block_size(sb->s_bdev));
14168dcc1a9dSDamien Le Moal 	sbi->s_zone_sectors_shift = ilog2(bdev_zone_sectors(sb->s_bdev));
14178dcc1a9dSDamien Le Moal 	sbi->s_uid = GLOBAL_ROOT_UID;
14188dcc1a9dSDamien Le Moal 	sbi->s_gid = GLOBAL_ROOT_GID;
14198dcc1a9dSDamien Le Moal 	sbi->s_perm = 0640;
14208dcc1a9dSDamien Le Moal 	sbi->s_mount_opts = ZONEFS_MNTOPT_ERRORS_RO;
14218dcc1a9dSDamien Le Moal 
14228dcc1a9dSDamien Le Moal 	ret = zonefs_read_super(sb);
14238dcc1a9dSDamien Le Moal 	if (ret)
14248dcc1a9dSDamien Le Moal 		return ret;
14258dcc1a9dSDamien Le Moal 
14268dcc1a9dSDamien Le Moal 	ret = zonefs_parse_options(sb, data);
14278dcc1a9dSDamien Le Moal 	if (ret)
14288dcc1a9dSDamien Le Moal 		return ret;
14298dcc1a9dSDamien Le Moal 
14308dcc1a9dSDamien Le Moal 	memset(&zd, 0, sizeof(struct zonefs_zone_data));
14318dcc1a9dSDamien Le Moal 	zd.sb = sb;
14328dcc1a9dSDamien Le Moal 	ret = zonefs_get_zone_info(&zd);
14338dcc1a9dSDamien Le Moal 	if (ret)
14348dcc1a9dSDamien Le Moal 		goto cleanup;
14358dcc1a9dSDamien Le Moal 
14368dcc1a9dSDamien Le Moal 	zonefs_info(sb, "Mounting %u zones",
14378dcc1a9dSDamien Le Moal 		    blkdev_nr_zones(sb->s_bdev->bd_disk));
14388dcc1a9dSDamien Le Moal 
14398dcc1a9dSDamien Le Moal 	/* Create root directory inode */
14408dcc1a9dSDamien Le Moal 	ret = -ENOMEM;
14418dcc1a9dSDamien Le Moal 	inode = new_inode(sb);
14428dcc1a9dSDamien Le Moal 	if (!inode)
14438dcc1a9dSDamien Le Moal 		goto cleanup;
14448dcc1a9dSDamien Le Moal 
14458dcc1a9dSDamien Le Moal 	inode->i_ino = blkdev_nr_zones(sb->s_bdev->bd_disk);
14468dcc1a9dSDamien Le Moal 	inode->i_mode = S_IFDIR | 0555;
14478dcc1a9dSDamien Le Moal 	inode->i_ctime = inode->i_mtime = inode->i_atime = current_time(inode);
14488dcc1a9dSDamien Le Moal 	inode->i_op = &zonefs_dir_inode_operations;
14498dcc1a9dSDamien Le Moal 	inode->i_fop = &simple_dir_operations;
14508dcc1a9dSDamien Le Moal 	set_nlink(inode, 2);
14518dcc1a9dSDamien Le Moal 
14528dcc1a9dSDamien Le Moal 	sb->s_root = d_make_root(inode);
14538dcc1a9dSDamien Le Moal 	if (!sb->s_root)
14548dcc1a9dSDamien Le Moal 		goto cleanup;
14558dcc1a9dSDamien Le Moal 
14568dcc1a9dSDamien Le Moal 	/* Create and populate files in zone groups directories */
14578dcc1a9dSDamien Le Moal 	for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) {
14588dcc1a9dSDamien Le Moal 		ret = zonefs_create_zgroup(&zd, t);
14598dcc1a9dSDamien Le Moal 		if (ret)
14608dcc1a9dSDamien Le Moal 			break;
14618dcc1a9dSDamien Le Moal 	}
14628dcc1a9dSDamien Le Moal 
14638dcc1a9dSDamien Le Moal cleanup:
14648dcc1a9dSDamien Le Moal 	zonefs_cleanup_zone_info(&zd);
14658dcc1a9dSDamien Le Moal 
14668dcc1a9dSDamien Le Moal 	return ret;
14678dcc1a9dSDamien Le Moal }
14688dcc1a9dSDamien Le Moal 
14698dcc1a9dSDamien Le Moal static struct dentry *zonefs_mount(struct file_system_type *fs_type,
14708dcc1a9dSDamien Le Moal 				   int flags, const char *dev_name, void *data)
14718dcc1a9dSDamien Le Moal {
14728dcc1a9dSDamien Le Moal 	return mount_bdev(fs_type, flags, dev_name, data, zonefs_fill_super);
14738dcc1a9dSDamien Le Moal }
14748dcc1a9dSDamien Le Moal 
14758dcc1a9dSDamien Le Moal static void zonefs_kill_super(struct super_block *sb)
14768dcc1a9dSDamien Le Moal {
14778dcc1a9dSDamien Le Moal 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
14788dcc1a9dSDamien Le Moal 
14798dcc1a9dSDamien Le Moal 	if (sb->s_root)
14808dcc1a9dSDamien Le Moal 		d_genocide(sb->s_root);
14818dcc1a9dSDamien Le Moal 	kill_block_super(sb);
14828dcc1a9dSDamien Le Moal 	kfree(sbi);
14838dcc1a9dSDamien Le Moal }
14848dcc1a9dSDamien Le Moal 
14858dcc1a9dSDamien Le Moal /*
14868dcc1a9dSDamien Le Moal  * File system definition and registration.
14878dcc1a9dSDamien Le Moal  */
14888dcc1a9dSDamien Le Moal static struct file_system_type zonefs_type = {
14898dcc1a9dSDamien Le Moal 	.owner		= THIS_MODULE,
14908dcc1a9dSDamien Le Moal 	.name		= "zonefs",
14918dcc1a9dSDamien Le Moal 	.mount		= zonefs_mount,
14928dcc1a9dSDamien Le Moal 	.kill_sb	= zonefs_kill_super,
14938dcc1a9dSDamien Le Moal 	.fs_flags	= FS_REQUIRES_DEV,
14948dcc1a9dSDamien Le Moal };
14958dcc1a9dSDamien Le Moal 
14968dcc1a9dSDamien Le Moal static int __init zonefs_init_inodecache(void)
14978dcc1a9dSDamien Le Moal {
14988dcc1a9dSDamien Le Moal 	zonefs_inode_cachep = kmem_cache_create("zonefs_inode_cache",
14998dcc1a9dSDamien Le Moal 			sizeof(struct zonefs_inode_info), 0,
15008dcc1a9dSDamien Le Moal 			(SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT),
15018dcc1a9dSDamien Le Moal 			NULL);
15028dcc1a9dSDamien Le Moal 	if (zonefs_inode_cachep == NULL)
15038dcc1a9dSDamien Le Moal 		return -ENOMEM;
15048dcc1a9dSDamien Le Moal 	return 0;
15058dcc1a9dSDamien Le Moal }
15068dcc1a9dSDamien Le Moal 
15078dcc1a9dSDamien Le Moal static void zonefs_destroy_inodecache(void)
15088dcc1a9dSDamien Le Moal {
15098dcc1a9dSDamien Le Moal 	/*
15108dcc1a9dSDamien Le Moal 	 * Make sure all delayed rcu free inodes are flushed before we
15118dcc1a9dSDamien Le Moal 	 * destroy the inode cache.
15128dcc1a9dSDamien Le Moal 	 */
15138dcc1a9dSDamien Le Moal 	rcu_barrier();
15148dcc1a9dSDamien Le Moal 	kmem_cache_destroy(zonefs_inode_cachep);
15158dcc1a9dSDamien Le Moal }
15168dcc1a9dSDamien Le Moal 
15178dcc1a9dSDamien Le Moal static int __init zonefs_init(void)
15188dcc1a9dSDamien Le Moal {
15198dcc1a9dSDamien Le Moal 	int ret;
15208dcc1a9dSDamien Le Moal 
15218dcc1a9dSDamien Le Moal 	BUILD_BUG_ON(sizeof(struct zonefs_super) != ZONEFS_SUPER_SIZE);
15228dcc1a9dSDamien Le Moal 
15238dcc1a9dSDamien Le Moal 	ret = zonefs_init_inodecache();
15248dcc1a9dSDamien Le Moal 	if (ret)
15258dcc1a9dSDamien Le Moal 		return ret;
15268dcc1a9dSDamien Le Moal 
15278dcc1a9dSDamien Le Moal 	ret = register_filesystem(&zonefs_type);
15288dcc1a9dSDamien Le Moal 	if (ret) {
15298dcc1a9dSDamien Le Moal 		zonefs_destroy_inodecache();
15308dcc1a9dSDamien Le Moal 		return ret;
15318dcc1a9dSDamien Le Moal 	}
15328dcc1a9dSDamien Le Moal 
15338dcc1a9dSDamien Le Moal 	return 0;
15348dcc1a9dSDamien Le Moal }
15358dcc1a9dSDamien Le Moal 
15368dcc1a9dSDamien Le Moal static void __exit zonefs_exit(void)
15378dcc1a9dSDamien Le Moal {
15388dcc1a9dSDamien Le Moal 	zonefs_destroy_inodecache();
15398dcc1a9dSDamien Le Moal 	unregister_filesystem(&zonefs_type);
15408dcc1a9dSDamien Le Moal }
15418dcc1a9dSDamien Le Moal 
15428dcc1a9dSDamien Le Moal MODULE_AUTHOR("Damien Le Moal");
15438dcc1a9dSDamien Le Moal MODULE_DESCRIPTION("Zone file system for zoned block devices");
15448dcc1a9dSDamien Le Moal MODULE_LICENSE("GPL");
15458dcc1a9dSDamien Le Moal module_init(zonefs_init);
15468dcc1a9dSDamien Le Moal module_exit(zonefs_exit);
1547