Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Simple file system for zoned block devices exposing zones as files.
   4 *
   5 * Copyright (C) 2019 Western Digital Corporation or its affiliates.
   6 */
   7#include <linux/module.h>
   8#include <linux/pagemap.h>
   9#include <linux/magic.h>
  10#include <linux/iomap.h>
  11#include <linux/init.h>
  12#include <linux/slab.h>
  13#include <linux/blkdev.h>
  14#include <linux/statfs.h>
  15#include <linux/writeback.h>
  16#include <linux/quotaops.h>
  17#include <linux/seq_file.h>
  18#include <linux/parser.h>
  19#include <linux/uio.h>
  20#include <linux/mman.h>
  21#include <linux/sched/mm.h>
  22#include <linux/crc32.h>
  23#include <linux/task_io_accounting_ops.h>
  24
  25#include "zonefs.h"
  26
  27#define CREATE_TRACE_POINTS
  28#include "trace.h"
  29
  30/*
  31 * Get the name of a zone group directory.
  32 */
  33static const char *zonefs_zgroup_name(enum zonefs_ztype ztype)
  34{
  35	switch (ztype) {
  36	case ZONEFS_ZTYPE_CNV:
  37		return "cnv";
  38	case ZONEFS_ZTYPE_SEQ:
  39		return "seq";
  40	default:
  41		WARN_ON_ONCE(1);
  42		return "???";
  43	}
  44}
  45
  46/*
  47 * Manage the active zone count.
  48 */
  49static void zonefs_account_active(struct super_block *sb,
  50				  struct zonefs_zone *z)
  51{
  52	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
  53
  54	if (zonefs_zone_is_cnv(z))
  55		return;
 
  56
  57	/*
  58	 * For zones that transitioned to the offline or readonly condition,
  59	 * we only need to clear the active state.
 
  60	 */
  61	if (z->z_flags & (ZONEFS_ZONE_OFFLINE | ZONEFS_ZONE_READONLY))
  62		goto out;
 
  63
  64	/*
  65	 * If the zone is active, that is, if it is explicitly open or
  66	 * partially written, check if it was already accounted as active.
 
  67	 */
  68	if ((z->z_flags & ZONEFS_ZONE_OPEN) ||
  69	    (z->z_wpoffset > 0 && z->z_wpoffset < z->z_capacity)) {
  70		if (!(z->z_flags & ZONEFS_ZONE_ACTIVE)) {
  71			z->z_flags |= ZONEFS_ZONE_ACTIVE;
  72			atomic_inc(&sbi->s_active_seq_files);
  73		}
  74		return;
  75	}
 
 
 
  76
  77out:
  78	/* The zone is not active. If it was, update the active count */
  79	if (z->z_flags & ZONEFS_ZONE_ACTIVE) {
  80		z->z_flags &= ~ZONEFS_ZONE_ACTIVE;
  81		atomic_dec(&sbi->s_active_seq_files);
  82	}
  83}
  84
  85/*
  86 * Manage the active zone count. Called with zi->i_truncate_mutex held.
  87 */
  88void zonefs_inode_account_active(struct inode *inode)
 
  89{
  90	lockdep_assert_held(&ZONEFS_I(inode)->i_truncate_mutex);
 
  91
  92	return zonefs_account_active(inode->i_sb, zonefs_inode_zone(inode));
 
 
  93}
  94
  95/*
  96 * Execute a zone management operation.
 
  97 */
  98static int zonefs_zone_mgmt(struct super_block *sb,
  99			    struct zonefs_zone *z, enum req_op op)
 100{
 101	int ret;
 102
 103	/*
 104	 * With ZNS drives, closing an explicitly open zone that has not been
 105	 * written will change the zone state to "closed", that is, the zone
 106	 * will remain active. Since this can then cause failure of explicit
 107	 * open operation on other zones if the drive active zone resources
 108	 * are exceeded, make sure that the zone does not remain active by
 109	 * resetting it.
 110	 */
 111	if (op == REQ_OP_ZONE_CLOSE && !z->z_wpoffset)
 112		op = REQ_OP_ZONE_RESET;
 113
 114	trace_zonefs_zone_mgmt(sb, z, op);
 115	ret = blkdev_zone_mgmt(sb->s_bdev, op, z->z_sector,
 116			       z->z_size >> SECTOR_SHIFT, GFP_NOFS);
 117	if (ret) {
 118		zonefs_err(sb,
 119			   "Zone management operation %s at %llu failed %d\n",
 120			   blk_op_str(op), z->z_sector, ret);
 121		return ret;
 122	}
 123
 124	return 0;
 
 125}
 126
 127int zonefs_inode_zone_mgmt(struct inode *inode, enum req_op op)
 
 
 
 
 128{
 129	lockdep_assert_held(&ZONEFS_I(inode)->i_truncate_mutex);
 130
 131	return zonefs_zone_mgmt(inode->i_sb, zonefs_inode_zone(inode), op);
 132}
 133
 134void zonefs_i_size_write(struct inode *inode, loff_t isize)
 
 135{
 136	struct zonefs_zone *z = zonefs_inode_zone(inode);
 137
 138	i_size_write(inode, isize);
 139
 140	/*
 141	 * A full zone is no longer open/active and does not need
 142	 * explicit closing.
 143	 */
 144	if (isize >= z->z_capacity) {
 145		struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
 146
 147		if (z->z_flags & ZONEFS_ZONE_ACTIVE)
 148			atomic_dec(&sbi->s_active_seq_files);
 149		z->z_flags &= ~(ZONEFS_ZONE_OPEN | ZONEFS_ZONE_ACTIVE);
 150	}
 151}
 152
 153void zonefs_update_stats(struct inode *inode, loff_t new_isize)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 154{
 155	struct super_block *sb = inode->i_sb;
 156	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
 157	loff_t old_isize = i_size_read(inode);
 158	loff_t nr_blocks;
 159
 160	if (new_isize == old_isize)
 161		return;
 162
 163	spin_lock(&sbi->s_lock);
 164
 165	/*
 166	 * This may be called for an update after an IO error.
 167	 * So beware of the values seen.
 168	 */
 169	if (new_isize < old_isize) {
 170		nr_blocks = (old_isize - new_isize) >> sb->s_blocksize_bits;
 171		if (sbi->s_used_blocks > nr_blocks)
 172			sbi->s_used_blocks -= nr_blocks;
 173		else
 174			sbi->s_used_blocks = 0;
 175	} else {
 176		sbi->s_used_blocks +=
 177			(new_isize - old_isize) >> sb->s_blocksize_bits;
 178		if (sbi->s_used_blocks > sbi->s_blocks)
 179			sbi->s_used_blocks = sbi->s_blocks;
 180	}
 181
 182	spin_unlock(&sbi->s_lock);
 183}
 184
 185/*
 186 * Check a zone condition. Return the amount of written (and still readable)
 187 * data in the zone.
 
 188 */
 189static loff_t zonefs_check_zone_condition(struct super_block *sb,
 190					  struct zonefs_zone *z,
 191					  struct blk_zone *zone)
 192{
 
 
 193	switch (zone->cond) {
 194	case BLK_ZONE_COND_OFFLINE:
 195		zonefs_warn(sb, "Zone %llu: offline zone\n",
 196			    z->z_sector);
 197		z->z_flags |= ZONEFS_ZONE_OFFLINE;
 
 
 
 
 
 
 
 198		return 0;
 199	case BLK_ZONE_COND_READONLY:
 200		/*
 201		 * The write pointer of read-only zones is invalid, so we cannot
 202		 * determine the zone wpoffset (inode size). We thus keep the
 203		 * zone wpoffset as is, which leads to an empty file
 204		 * (wpoffset == 0) on mount. For a runtime error, this keeps
 205		 * the inode size as it was when last updated so that the user
 206		 * can recover data.
 207		 */
 208		zonefs_warn(sb, "Zone %llu: read-only zone\n",
 209			    z->z_sector);
 210		z->z_flags |= ZONEFS_ZONE_READONLY;
 211		if (zonefs_zone_is_cnv(z))
 212			return z->z_capacity;
 213		return z->z_wpoffset;
 214	case BLK_ZONE_COND_FULL:
 215		/* The write pointer of full zones is invalid. */
 216		return z->z_capacity;
 
 
 
 217	default:
 218		if (zonefs_zone_is_cnv(z))
 219			return z->z_capacity;
 220		return (zone->wp - zone->start) << SECTOR_SHIFT;
 221	}
 222}
 223
 224/*
 225 * Check a zone condition and adjust its inode access permissions for
 226 * offline and readonly zones.
 227 */
 228static void zonefs_inode_update_mode(struct inode *inode)
 229{
 230	struct zonefs_zone *z = zonefs_inode_zone(inode);
 231
 232	if (z->z_flags & ZONEFS_ZONE_OFFLINE) {
 233		/* Offline zones cannot be read nor written */
 234		inode->i_flags |= S_IMMUTABLE;
 235		inode->i_mode &= ~0777;
 236	} else if (z->z_flags & ZONEFS_ZONE_READONLY) {
 237		/* Readonly zones cannot be written */
 238		inode->i_flags |= S_IMMUTABLE;
 239		if (z->z_flags & ZONEFS_ZONE_INIT_MODE)
 240			inode->i_mode &= ~0777;
 241		else
 242			inode->i_mode &= ~0222;
 243	}
 244
 245	z->z_flags &= ~ZONEFS_ZONE_INIT_MODE;
 246	z->z_mode = inode->i_mode;
 247}
 248
 249static int zonefs_io_error_cb(struct blk_zone *zone, unsigned int idx,
 250			      void *data)
 251{
 252	struct blk_zone *z = data;
 253
 254	*z = *zone;
 255	return 0;
 256}
 257
 258static void zonefs_handle_io_error(struct inode *inode, struct blk_zone *zone,
 259				   bool write)
 260{
 261	struct zonefs_zone *z = zonefs_inode_zone(inode);
 262	struct super_block *sb = inode->i_sb;
 263	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
 264	loff_t isize, data_size;
 265
 266	/*
 267	 * Check the zone condition: if the zone is not "bad" (offline or
 268	 * read-only), read errors are simply signaled to the IO issuer as long
 269	 * as there is no inconsistency between the inode size and the amount of
 270	 * data writen in the zone (data_size).
 271	 */
 272	data_size = zonefs_check_zone_condition(sb, z, zone);
 273	isize = i_size_read(inode);
 274	if (!(z->z_flags & (ZONEFS_ZONE_READONLY | ZONEFS_ZONE_OFFLINE)) &&
 275	    !write && isize == data_size)
 276		return;
 
 277
 278	/*
 279	 * At this point, we detected either a bad zone or an inconsistency
 280	 * between the inode size and the amount of data written in the zone.
 281	 * For the latter case, the cause may be a write IO error or an external
 282	 * action on the device. Two error patterns exist:
 283	 * 1) The inode size is lower than the amount of data in the zone:
 284	 *    a write operation partially failed and data was writen at the end
 285	 *    of the file. This can happen in the case of a large direct IO
 286	 *    needing several BIOs and/or write requests to be processed.
 287	 * 2) The inode size is larger than the amount of data in the zone:
 288	 *    this can happen with a deferred write error with the use of the
 289	 *    device side write cache after getting successful write IO
 290	 *    completions. Other possibilities are (a) an external corruption,
 291	 *    e.g. an application reset the zone directly, or (b) the device
 292	 *    has a serious problem (e.g. firmware bug).
 293	 *
 294	 * In all cases, warn about inode size inconsistency and handle the
 295	 * IO error according to the zone condition and to the mount options.
 296	 */
 297	if (isize != data_size)
 298		zonefs_warn(sb,
 299			    "inode %lu: invalid size %lld (should be %lld)\n",
 300			    inode->i_ino, isize, data_size);
 301
 302	/*
 303	 * First handle bad zones signaled by hardware. The mount options
 304	 * errors=zone-ro and errors=zone-offline result in changing the
 305	 * zone condition to read-only and offline respectively, as if the
 306	 * condition was signaled by the hardware.
 307	 */
 308	if ((z->z_flags & ZONEFS_ZONE_OFFLINE) ||
 309	    (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL)) {
 310		zonefs_warn(sb, "inode %lu: read/write access disabled\n",
 311			    inode->i_ino);
 312		if (!(z->z_flags & ZONEFS_ZONE_OFFLINE))
 313			z->z_flags |= ZONEFS_ZONE_OFFLINE;
 314		zonefs_inode_update_mode(inode);
 315		data_size = 0;
 316	} else if ((z->z_flags & ZONEFS_ZONE_READONLY) ||
 317		   (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO)) {
 
 318		zonefs_warn(sb, "inode %lu: write access disabled\n",
 319			    inode->i_ino);
 320		if (!(z->z_flags & ZONEFS_ZONE_READONLY))
 321			z->z_flags |= ZONEFS_ZONE_READONLY;
 322		zonefs_inode_update_mode(inode);
 323		data_size = isize;
 324	} else if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO &&
 325		   data_size > isize) {
 326		/* Do not expose garbage data */
 327		data_size = isize;
 328	}
 329
 330	/*
 331	 * If the filesystem is mounted with the explicit-open mount option, we
 332	 * need to clear the ZONEFS_ZONE_OPEN flag if the zone transitioned to
 333	 * the read-only or offline condition, to avoid attempting an explicit
 334	 * close of the zone when the inode file is closed.
 335	 */
 336	if ((sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) &&
 337	    (z->z_flags & (ZONEFS_ZONE_READONLY | ZONEFS_ZONE_OFFLINE)))
 338		z->z_flags &= ~ZONEFS_ZONE_OPEN;
 339
 340	/*
 341	 * If error=remount-ro was specified, any error result in remounting
 342	 * the volume as read-only.
 343	 */
 344	if ((sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO) && !sb_rdonly(sb)) {
 345		zonefs_warn(sb, "remounting filesystem read-only\n");
 346		sb->s_flags |= SB_RDONLY;
 347	}
 348
 349	/*
 350	 * Update block usage stats and the inode size  to prevent access to
 351	 * invalid data.
 352	 */
 353	zonefs_update_stats(inode, data_size);
 354	zonefs_i_size_write(inode, data_size);
 355	z->z_wpoffset = data_size;
 356	zonefs_inode_account_active(inode);
 
 357}
 358
 359/*
 360 * When an file IO error occurs, check the file zone to see if there is a change
 361 * in the zone condition (e.g. offline or read-only). For a failed write to a
 362 * sequential zone, the zone write pointer position must also be checked to
 363 * eventually correct the file size and zonefs inode write pointer offset
 364 * (which can be out of sync with the drive due to partial write failures).
 365 */
 366void __zonefs_io_error(struct inode *inode, bool write)
 367{
 368	struct zonefs_zone *z = zonefs_inode_zone(inode);
 369	struct super_block *sb = inode->i_sb;
 
 370	unsigned int noio_flag;
 371	struct blk_zone zone;
 
 
 
 
 
 372	int ret;
 373
 374	/*
 375	 * Conventional zone have no write pointer and cannot become read-only
 376	 * or offline. So simply fake a report for a single or aggregated zone
 377	 * and let zonefs_handle_io_error() correct the zone inode information
 378	 * according to the mount options.
 379	 */
 380	if (!zonefs_zone_is_seq(z)) {
 381		zone.start = z->z_sector;
 382		zone.len = z->z_size >> SECTOR_SHIFT;
 383		zone.wp = zone.start + zone.len;
 384		zone.type = BLK_ZONE_TYPE_CONVENTIONAL;
 385		zone.cond = BLK_ZONE_COND_NOT_WP;
 386		zone.capacity = zone.len;
 387		goto handle_io_error;
 388	}
 389
 390	/*
 391	 * Memory allocations in blkdev_report_zones() can trigger a memory
 392	 * reclaim which may in turn cause a recursion into zonefs as well as
 393	 * struct request allocations for the same device. The former case may
 394	 * end up in a deadlock on the inode truncate mutex, while the latter
 395	 * may prevent IO forward progress. Executing the report zones under
 396	 * the GFP_NOIO context avoids both problems.
 397	 */
 398	noio_flag = memalloc_noio_save();
 399	ret = blkdev_report_zones(sb->s_bdev, z->z_sector, 1,
 400				  zonefs_io_error_cb, &zone);
 401	memalloc_noio_restore(noio_flag);
 402
 403	if (ret != 1) {
 404		zonefs_err(sb, "Get inode %lu zone information failed %d\n",
 405			   inode->i_ino, ret);
 406		zonefs_warn(sb, "remounting filesystem read-only\n");
 407		sb->s_flags |= SB_RDONLY;
 408		return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 409	}
 410
 411handle_io_error:
 412	zonefs_handle_io_error(inode, &zone, write);
 
 
 
 
 
 
 
 413}
 414
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 415static struct kmem_cache *zonefs_inode_cachep;
 416
 417static struct inode *zonefs_alloc_inode(struct super_block *sb)
 418{
 419	struct zonefs_inode_info *zi;
 420
 421	zi = alloc_inode_sb(sb, zonefs_inode_cachep, GFP_KERNEL);
 422	if (!zi)
 423		return NULL;
 424
 425	inode_init_once(&zi->i_vnode);
 426	mutex_init(&zi->i_truncate_mutex);
 427	zi->i_wr_refcnt = 0;
 428
 429	return &zi->i_vnode;
 430}
 431
 432static void zonefs_free_inode(struct inode *inode)
 433{
 434	kmem_cache_free(zonefs_inode_cachep, ZONEFS_I(inode));
 435}
 436
 437/*
 438 * File system stat.
 439 */
 440static int zonefs_statfs(struct dentry *dentry, struct kstatfs *buf)
 441{
 442	struct super_block *sb = dentry->d_sb;
 443	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
 444	enum zonefs_ztype t;
 
 445
 446	buf->f_type = ZONEFS_MAGIC;
 447	buf->f_bsize = sb->s_blocksize;
 448	buf->f_namelen = ZONEFS_NAME_MAX;
 449
 450	spin_lock(&sbi->s_lock);
 451
 452	buf->f_blocks = sbi->s_blocks;
 453	if (WARN_ON(sbi->s_used_blocks > sbi->s_blocks))
 454		buf->f_bfree = 0;
 455	else
 456		buf->f_bfree = buf->f_blocks - sbi->s_used_blocks;
 457	buf->f_bavail = buf->f_bfree;
 458
 459	for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) {
 460		if (sbi->s_zgroup[t].g_nr_zones)
 461			buf->f_files += sbi->s_zgroup[t].g_nr_zones + 1;
 462	}
 463	buf->f_ffree = 0;
 464
 465	spin_unlock(&sbi->s_lock);
 466
 467	buf->f_fsid = uuid_to_fsid(sbi->s_uuid.b);
 
 
 
 468
 469	return 0;
 470}
 471
 472enum {
 473	Opt_errors_ro, Opt_errors_zro, Opt_errors_zol, Opt_errors_repair,
 474	Opt_explicit_open, Opt_err,
 475};
 476
 477static const match_table_t tokens = {
 478	{ Opt_errors_ro,	"errors=remount-ro"},
 479	{ Opt_errors_zro,	"errors=zone-ro"},
 480	{ Opt_errors_zol,	"errors=zone-offline"},
 481	{ Opt_errors_repair,	"errors=repair"},
 482	{ Opt_explicit_open,	"explicit-open" },
 483	{ Opt_err,		NULL}
 484};
 485
 486static int zonefs_parse_options(struct super_block *sb, char *options)
 487{
 488	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
 489	substring_t args[MAX_OPT_ARGS];
 490	char *p;
 491
 492	if (!options)
 493		return 0;
 494
 495	while ((p = strsep(&options, ",")) != NULL) {
 496		int token;
 497
 498		if (!*p)
 499			continue;
 500
 501		token = match_token(p, tokens, args);
 502		switch (token) {
 503		case Opt_errors_ro:
 504			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
 505			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_RO;
 506			break;
 507		case Opt_errors_zro:
 508			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
 509			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZRO;
 510			break;
 511		case Opt_errors_zol:
 512			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
 513			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZOL;
 514			break;
 515		case Opt_errors_repair:
 516			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
 517			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_REPAIR;
 518			break;
 519		case Opt_explicit_open:
 520			sbi->s_mount_opts |= ZONEFS_MNTOPT_EXPLICIT_OPEN;
 521			break;
 522		default:
 523			return -EINVAL;
 524		}
 525	}
 526
 527	return 0;
 528}
 529
 530static int zonefs_show_options(struct seq_file *seq, struct dentry *root)
 531{
 532	struct zonefs_sb_info *sbi = ZONEFS_SB(root->d_sb);
 533
 534	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO)
 535		seq_puts(seq, ",errors=remount-ro");
 536	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO)
 537		seq_puts(seq, ",errors=zone-ro");
 538	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL)
 539		seq_puts(seq, ",errors=zone-offline");
 540	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_REPAIR)
 541		seq_puts(seq, ",errors=repair");
 542
 543	return 0;
 544}
 545
 546static int zonefs_remount(struct super_block *sb, int *flags, char *data)
 547{
 548	sync_filesystem(sb);
 549
 550	return zonefs_parse_options(sb, data);
 551}
 552
 553static int zonefs_inode_setattr(struct mnt_idmap *idmap,
 554				struct dentry *dentry, struct iattr *iattr)
 555{
 556	struct inode *inode = d_inode(dentry);
 557	int ret;
 558
 559	if (unlikely(IS_IMMUTABLE(inode)))
 560		return -EPERM;
 561
 562	ret = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
 563	if (ret)
 564		return ret;
 565
 566	/*
 567	 * Since files and directories cannot be created nor deleted, do not
 568	 * allow setting any write attributes on the sub-directories grouping
 569	 * files by zone type.
 570	 */
 571	if ((iattr->ia_valid & ATTR_MODE) && S_ISDIR(inode->i_mode) &&
 572	    (iattr->ia_mode & 0222))
 573		return -EPERM;
 574
 575	if (((iattr->ia_valid & ATTR_UID) &&
 576	     !uid_eq(iattr->ia_uid, inode->i_uid)) ||
 577	    ((iattr->ia_valid & ATTR_GID) &&
 578	     !gid_eq(iattr->ia_gid, inode->i_gid))) {
 579		ret = dquot_transfer(&nop_mnt_idmap, inode, iattr);
 580		if (ret)
 581			return ret;
 582	}
 583
 584	if (iattr->ia_valid & ATTR_SIZE) {
 585		ret = zonefs_file_truncate(inode, iattr->ia_size);
 586		if (ret)
 587			return ret;
 588	}
 589
 590	setattr_copy(&nop_mnt_idmap, inode, iattr);
 591
 592	if (S_ISREG(inode->i_mode)) {
 593		struct zonefs_zone *z = zonefs_inode_zone(inode);
 594
 595		z->z_mode = inode->i_mode;
 596		z->z_uid = inode->i_uid;
 597		z->z_gid = inode->i_gid;
 598	}
 599
 600	return 0;
 601}
 602
 603static const struct inode_operations zonefs_file_inode_operations = {
 
 604	.setattr	= zonefs_inode_setattr,
 605};
 606
 607static long zonefs_fname_to_fno(const struct qstr *fname)
 
 608{
 609	const char *name = fname->name;
 610	unsigned int len = fname->len;
 611	long fno = 0, shift = 1;
 612	const char *rname;
 613	char c = *name;
 614	unsigned int i;
 615
 616	/*
 617	 * File names are always a base-10 number string without any
 618	 * leading 0s.
 619	 */
 620	if (!isdigit(c))
 621		return -ENOENT;
 622
 623	if (len > 1 && c == '0')
 624		return -ENOENT;
 625
 626	if (len == 1)
 627		return c - '0';
 628
 629	for (i = 0, rname = name + len - 1; i < len; i++, rname--) {
 630		c = *rname;
 631		if (!isdigit(c))
 632			return -ENOENT;
 633		fno += (c - '0') * shift;
 634		shift *= 10;
 635	}
 636
 637	return fno;
 638}
 639
 640static struct inode *zonefs_get_file_inode(struct inode *dir,
 641					   struct dentry *dentry)
 642{
 643	struct zonefs_zone_group *zgroup = dir->i_private;
 644	struct super_block *sb = dir->i_sb;
 645	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
 646	struct zonefs_zone *z;
 647	struct inode *inode;
 648	ino_t ino;
 649	long fno;
 650
 651	/* Get the file number from the file name */
 652	fno = zonefs_fname_to_fno(&dentry->d_name);
 653	if (fno < 0)
 654		return ERR_PTR(fno);
 655
 656	if (!zgroup->g_nr_zones || fno >= zgroup->g_nr_zones)
 657		return ERR_PTR(-ENOENT);
 658
 659	z = &zgroup->g_zones[fno];
 660	ino = z->z_sector >> sbi->s_zone_sectors_shift;
 661	inode = iget_locked(sb, ino);
 662	if (!inode)
 663		return ERR_PTR(-ENOMEM);
 664	if (!(inode->i_state & I_NEW)) {
 665		WARN_ON_ONCE(inode->i_private != z);
 666		return inode;
 667	}
 668
 669	inode->i_ino = ino;
 670	inode->i_mode = z->z_mode;
 671	inode_set_mtime_to_ts(inode,
 672			      inode_set_atime_to_ts(inode, inode_set_ctime_to_ts(inode, inode_get_ctime(dir))));
 673	inode->i_uid = z->z_uid;
 674	inode->i_gid = z->z_gid;
 675	inode->i_size = z->z_wpoffset;
 676	inode->i_blocks = z->z_capacity >> SECTOR_SHIFT;
 677	inode->i_private = z;
 
 
 
 678
 679	inode->i_op = &zonefs_file_inode_operations;
 680	inode->i_fop = &zonefs_file_operations;
 681	inode->i_mapping->a_ops = &zonefs_file_aops;
 682
 683	/* Update the inode access rights depending on the zone condition */
 684	zonefs_inode_update_mode(inode);
 685
 686	unlock_new_inode(inode);
 687
 688	return inode;
 689}
 690
 691static struct inode *zonefs_get_zgroup_inode(struct super_block *sb,
 692					     enum zonefs_ztype ztype)
 
 693{
 694	struct inode *root = d_inode(sb->s_root);
 695	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
 696	struct inode *inode;
 697	ino_t ino = bdev_nr_zones(sb->s_bdev) + ztype + 1;
 698
 699	inode = iget_locked(sb, ino);
 700	if (!inode)
 701		return ERR_PTR(-ENOMEM);
 702	if (!(inode->i_state & I_NEW))
 703		return inode;
 704
 705	inode->i_ino = ino;
 706	inode_init_owner(&nop_mnt_idmap, inode, root, S_IFDIR | 0555);
 707	inode->i_size = sbi->s_zgroup[ztype].g_nr_zones;
 708	inode_set_mtime_to_ts(inode,
 709			      inode_set_atime_to_ts(inode, inode_set_ctime_to_ts(inode, inode_get_ctime(root))));
 710	inode->i_private = &sbi->s_zgroup[ztype];
 711	set_nlink(inode, 2);
 712
 713	inode->i_op = &zonefs_dir_inode_operations;
 714	inode->i_fop = &zonefs_dir_operations;
 715
 716	unlock_new_inode(inode);
 717
 718	return inode;
 719}
 720
 721
 722static struct inode *zonefs_get_dir_inode(struct inode *dir,
 723					  struct dentry *dentry)
 724{
 725	struct super_block *sb = dir->i_sb;
 726	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
 727	const char *name = dentry->d_name.name;
 728	enum zonefs_ztype ztype;
 729
 730	/*
 731	 * We only need to check for the "seq" directory and
 732	 * the "cnv" directory if we have conventional zones.
 733	 */
 734	if (dentry->d_name.len != 3)
 735		return ERR_PTR(-ENOENT);
 736
 737	for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
 738		if (sbi->s_zgroup[ztype].g_nr_zones &&
 739		    memcmp(name, zonefs_zgroup_name(ztype), 3) == 0)
 740			break;
 741	}
 742	if (ztype == ZONEFS_ZTYPE_MAX)
 743		return ERR_PTR(-ENOENT);
 744
 745	return zonefs_get_zgroup_inode(sb, ztype);
 746}
 747
 748static struct dentry *zonefs_lookup(struct inode *dir, struct dentry *dentry,
 749				    unsigned int flags)
 750{
 751	struct inode *inode;
 752
 753	if (dentry->d_name.len > ZONEFS_NAME_MAX)
 754		return ERR_PTR(-ENAMETOOLONG);
 
 755
 756	if (dir == d_inode(dir->i_sb->s_root))
 757		inode = zonefs_get_dir_inode(dir, dentry);
 
 758	else
 759		inode = zonefs_get_file_inode(dir, dentry);
 760
 761	return d_splice_alias(inode, dentry);
 762}
 763
 764static int zonefs_readdir_root(struct file *file, struct dir_context *ctx)
 765{
 766	struct inode *inode = file_inode(file);
 767	struct super_block *sb = inode->i_sb;
 768	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
 769	enum zonefs_ztype ztype = ZONEFS_ZTYPE_CNV;
 770	ino_t base_ino = bdev_nr_zones(sb->s_bdev) + 1;
 771
 772	if (ctx->pos >= inode->i_size)
 773		return 0;
 774
 775	if (!dir_emit_dots(file, ctx))
 776		return 0;
 777
 778	if (ctx->pos == 2) {
 779		if (!sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones)
 780			ztype = ZONEFS_ZTYPE_SEQ;
 781
 782		if (!dir_emit(ctx, zonefs_zgroup_name(ztype), 3,
 783			      base_ino + ztype, DT_DIR))
 784			return 0;
 785		ctx->pos++;
 786	}
 787
 788	if (ctx->pos == 3 && ztype != ZONEFS_ZTYPE_SEQ) {
 789		ztype = ZONEFS_ZTYPE_SEQ;
 790		if (!dir_emit(ctx, zonefs_zgroup_name(ztype), 3,
 791			      base_ino + ztype, DT_DIR))
 792			return 0;
 793		ctx->pos++;
 794	}
 795
 796	return 0;
 797}
 798
 799static int zonefs_readdir_zgroup(struct file *file,
 800				 struct dir_context *ctx)
 801{
 802	struct inode *inode = file_inode(file);
 803	struct zonefs_zone_group *zgroup = inode->i_private;
 804	struct super_block *sb = inode->i_sb;
 805	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
 806	struct zonefs_zone *z;
 807	int fname_len;
 808	char *fname;
 809	ino_t ino;
 810	int f;
 811
 812	/*
 813	 * The size of zone group directories is equal to the number
 814	 * of zone files in the group and does note include the "." and
 815	 * ".." entries. Hence the "+ 2" here.
 816	 */
 817	if (ctx->pos >= inode->i_size + 2)
 818		return 0;
 819
 820	if (!dir_emit_dots(file, ctx))
 821		return 0;
 822
 823	fname = kmalloc(ZONEFS_NAME_MAX, GFP_KERNEL);
 824	if (!fname)
 825		return -ENOMEM;
 826
 827	for (f = ctx->pos - 2; f < zgroup->g_nr_zones; f++) {
 828		z = &zgroup->g_zones[f];
 829		ino = z->z_sector >> sbi->s_zone_sectors_shift;
 830		fname_len = snprintf(fname, ZONEFS_NAME_MAX - 1, "%u", f);
 831		if (!dir_emit(ctx, fname, fname_len, ino, DT_REG))
 832			break;
 833		ctx->pos++;
 834	}
 835
 836	kfree(fname);
 837
 838	return 0;
 839}
 840
 841static int zonefs_readdir(struct file *file, struct dir_context *ctx)
 842{
 843	struct inode *inode = file_inode(file);
 844
 845	if (inode == d_inode(inode->i_sb->s_root))
 846		return zonefs_readdir_root(file, ctx);
 847
 848	return zonefs_readdir_zgroup(file, ctx);
 849}
 850
 851const struct inode_operations zonefs_dir_inode_operations = {
 852	.lookup		= zonefs_lookup,
 853	.setattr	= zonefs_inode_setattr,
 854};
 855
 856const struct file_operations zonefs_dir_operations = {
 857	.llseek		= generic_file_llseek,
 858	.read		= generic_read_dir,
 859	.iterate_shared	= zonefs_readdir,
 860};
 861
 862struct zonefs_zone_data {
 863	struct super_block	*sb;
 864	unsigned int		nr_zones[ZONEFS_ZTYPE_MAX];
 865	sector_t		cnv_zone_start;
 866	struct blk_zone		*zones;
 867};
 868
 869static int zonefs_get_zone_info_cb(struct blk_zone *zone, unsigned int idx,
 870				   void *data)
 871{
 872	struct zonefs_zone_data *zd = data;
 873	struct super_block *sb = zd->sb;
 874	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
 875
 876	/*
 877	 * We do not care about the first zone: it contains the super block
 878	 * and not exposed as a file.
 879	 */
 880	if (!idx)
 881		return 0;
 882
 883	/*
 884	 * Count the number of zones that will be exposed as files.
 885	 * For sequential zones, we always have as many files as zones.
 886	 * FOr conventional zones, the number of files depends on if we have
 887	 * conventional zones aggregation enabled.
 888	 */
 889	switch (zone->type) {
 890	case BLK_ZONE_TYPE_CONVENTIONAL:
 891		if (sbi->s_features & ZONEFS_F_AGGRCNV) {
 892			/* One file per set of contiguous conventional zones */
 893			if (!(sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones) ||
 894			    zone->start != zd->cnv_zone_start)
 895				sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones++;
 896			zd->cnv_zone_start = zone->start + zone->len;
 897		} else {
 898			/* One file per zone */
 899			sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones++;
 900		}
 901		break;
 902	case BLK_ZONE_TYPE_SEQWRITE_REQ:
 903	case BLK_ZONE_TYPE_SEQWRITE_PREF:
 904		sbi->s_zgroup[ZONEFS_ZTYPE_SEQ].g_nr_zones++;
 905		break;
 906	default:
 907		zonefs_err(zd->sb, "Unsupported zone type 0x%x\n",
 908			   zone->type);
 909		return -EIO;
 910	}
 911
 912	memcpy(&zd->zones[idx], zone, sizeof(struct blk_zone));
 913
 914	return 0;
 915}
 916
 917static int zonefs_get_zone_info(struct zonefs_zone_data *zd)
 918{
 919	struct block_device *bdev = zd->sb->s_bdev;
 920	int ret;
 921
 922	zd->zones = kvcalloc(bdev_nr_zones(bdev), sizeof(struct blk_zone),
 923			     GFP_KERNEL);
 924	if (!zd->zones)
 925		return -ENOMEM;
 926
 927	/* Get zones information from the device */
 928	ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES,
 929				  zonefs_get_zone_info_cb, zd);
 930	if (ret < 0) {
 931		zonefs_err(zd->sb, "Zone report failed %d\n", ret);
 932		return ret;
 933	}
 934
 935	if (ret != bdev_nr_zones(bdev)) {
 936		zonefs_err(zd->sb, "Invalid zone report (%d/%u zones)\n",
 937			   ret, bdev_nr_zones(bdev));
 938		return -EIO;
 939	}
 940
 941	return 0;
 942}
 943
 944static inline void zonefs_free_zone_info(struct zonefs_zone_data *zd)
 945{
 946	kvfree(zd->zones);
 947}
 948
 949/*
 950 * Create a zone group and populate it with zone files.
 951 */
 952static int zonefs_init_zgroup(struct super_block *sb,
 953			      struct zonefs_zone_data *zd,
 954			      enum zonefs_ztype ztype)
 955{
 
 956	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
 957	struct zonefs_zone_group *zgroup = &sbi->s_zgroup[ztype];
 958	struct blk_zone *zone, *next, *end;
 959	struct zonefs_zone *z;
 
 
 960	unsigned int n = 0;
 961	int ret;
 962
 963	/* Allocate the zone group. If it is empty, we have nothing to do. */
 964	if (!zgroup->g_nr_zones)
 965		return 0;
 966
 967	zgroup->g_zones = kvcalloc(zgroup->g_nr_zones,
 968				   sizeof(struct zonefs_zone), GFP_KERNEL);
 969	if (!zgroup->g_zones)
 970		return -ENOMEM;
 971
 
 
 
 
 
 
 
 
 
 
 
 972	/*
 973	 * Initialize the zone groups using the device zone information.
 974	 * We always skip the first zone as it contains the super block
 975	 * and is not use to back a file.
 976	 */
 977	end = zd->zones + bdev_nr_zones(sb->s_bdev);
 978	for (zone = &zd->zones[1]; zone < end; zone = next) {
 979
 980		next = zone + 1;
 981		if (zonefs_zone_type(zone) != ztype)
 982			continue;
 983
 984		if (WARN_ON_ONCE(n >= zgroup->g_nr_zones))
 985			return -EINVAL;
 986
 987		/*
 988		 * For conventional zones, contiguous zones can be aggregated
 989		 * together to form larger files. Note that this overwrites the
 990		 * length of the first zone of the set of contiguous zones
 991		 * aggregated together. If one offline or read-only zone is
 992		 * found, assume that all zones aggregated have the same
 993		 * condition.
 994		 */
 995		if (ztype == ZONEFS_ZTYPE_CNV &&
 996		    (sbi->s_features & ZONEFS_F_AGGRCNV)) {
 997			for (; next < end; next++) {
 998				if (zonefs_zone_type(next) != ztype)
 999					break;
1000				zone->len += next->len;
1001				zone->capacity += next->capacity;
1002				if (next->cond == BLK_ZONE_COND_READONLY &&
1003				    zone->cond != BLK_ZONE_COND_OFFLINE)
1004					zone->cond = BLK_ZONE_COND_READONLY;
1005				else if (next->cond == BLK_ZONE_COND_OFFLINE)
1006					zone->cond = BLK_ZONE_COND_OFFLINE;
1007			}
 
 
 
 
 
1008		}
1009
1010		z = &zgroup->g_zones[n];
1011		if (ztype == ZONEFS_ZTYPE_CNV)
1012			z->z_flags |= ZONEFS_ZONE_CNV;
1013		z->z_sector = zone->start;
1014		z->z_size = zone->len << SECTOR_SHIFT;
1015		if (z->z_size > bdev_zone_sectors(sb->s_bdev) << SECTOR_SHIFT &&
1016		    !(sbi->s_features & ZONEFS_F_AGGRCNV)) {
1017			zonefs_err(sb,
1018				"Invalid zone size %llu (device zone sectors %llu)\n",
1019				z->z_size,
1020				bdev_zone_sectors(sb->s_bdev) << SECTOR_SHIFT);
1021			return -EINVAL;
1022		}
1023
1024		z->z_capacity = min_t(loff_t, MAX_LFS_FILESIZE,
1025				      zone->capacity << SECTOR_SHIFT);
1026		z->z_wpoffset = zonefs_check_zone_condition(sb, z, zone);
1027
1028		z->z_mode = S_IFREG | sbi->s_perm;
1029		z->z_uid = sbi->s_uid;
1030		z->z_gid = sbi->s_gid;
1031
1032		/*
1033		 * Let zonefs_inode_update_mode() know that we will need
1034		 * special initialization of the inode mode the first time
1035		 * it is accessed.
1036		 */
1037		z->z_flags |= ZONEFS_ZONE_INIT_MODE;
1038
1039		sb->s_maxbytes = max(z->z_capacity, sb->s_maxbytes);
1040		sbi->s_blocks += z->z_capacity >> sb->s_blocksize_bits;
1041		sbi->s_used_blocks += z->z_wpoffset >> sb->s_blocksize_bits;
1042
1043		/*
1044		 * For sequential zones, make sure that any open zone is closed
1045		 * first to ensure that the initial number of open zones is 0,
1046		 * in sync with the open zone accounting done when the mount
1047		 * option ZONEFS_MNTOPT_EXPLICIT_OPEN is used.
1048		 */
1049		if (ztype == ZONEFS_ZTYPE_SEQ &&
1050		    (zone->cond == BLK_ZONE_COND_IMP_OPEN ||
1051		     zone->cond == BLK_ZONE_COND_EXP_OPEN)) {
1052			ret = zonefs_zone_mgmt(sb, z, REQ_OP_ZONE_CLOSE);
1053			if (ret)
1054				return ret;
1055		}
1056
1057		zonefs_account_active(sb, z);
1058
1059		n++;
1060	}
1061
1062	if (WARN_ON_ONCE(n != zgroup->g_nr_zones))
1063		return -EINVAL;
1064
1065	zonefs_info(sb, "Zone group \"%s\" has %u file%s\n",
1066		    zonefs_zgroup_name(ztype),
1067		    zgroup->g_nr_zones,
1068		    zgroup->g_nr_zones > 1 ? "s" : "");
1069
1070	return 0;
 
 
 
 
 
 
1071}
1072
1073static void zonefs_free_zgroups(struct super_block *sb)
 
1074{
1075	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1076	enum zonefs_ztype ztype;
1077
1078	if (!sbi)
1079		return;
1080
1081	for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
1082		kvfree(sbi->s_zgroup[ztype].g_zones);
1083		sbi->s_zgroup[ztype].g_zones = NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1084	}
 
 
 
 
1085}
1086
1087/*
1088 * Create a zone group and populate it with zone files.
1089 */
1090static int zonefs_init_zgroups(struct super_block *sb)
1091{
1092	struct zonefs_zone_data zd;
1093	enum zonefs_ztype ztype;
1094	int ret;
1095
1096	/* First get the device zone information */
1097	memset(&zd, 0, sizeof(struct zonefs_zone_data));
1098	zd.sb = sb;
1099	ret = zonefs_get_zone_info(&zd);
1100	if (ret)
1101		goto cleanup;
1102
1103	/* Allocate and initialize the zone groups */
1104	for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
1105		ret = zonefs_init_zgroup(sb, &zd, ztype);
1106		if (ret) {
1107			zonefs_info(sb,
1108				    "Zone group \"%s\" initialization failed\n",
1109				    zonefs_zgroup_name(ztype));
1110			break;
1111		}
1112	}
1113
1114cleanup:
1115	zonefs_free_zone_info(&zd);
1116	if (ret)
1117		zonefs_free_zgroups(sb);
 
1118
1119	return ret;
 
 
 
 
 
1120}
1121
1122/*
1123 * Read super block information from the device.
1124 */
1125static int zonefs_read_super(struct super_block *sb)
1126{
1127	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1128	struct zonefs_super *super;
1129	u32 crc, stored_crc;
1130	struct page *page;
1131	struct bio_vec bio_vec;
1132	struct bio bio;
1133	int ret;
1134
1135	page = alloc_page(GFP_KERNEL);
1136	if (!page)
1137		return -ENOMEM;
1138
1139	bio_init(&bio, sb->s_bdev, &bio_vec, 1, REQ_OP_READ);
1140	bio.bi_iter.bi_sector = 0;
1141	__bio_add_page(&bio, page, PAGE_SIZE, 0);
 
 
1142
1143	ret = submit_bio_wait(&bio);
1144	if (ret)
1145		goto free_page;
1146
1147	super = page_address(page);
1148
1149	ret = -EINVAL;
1150	if (le32_to_cpu(super->s_magic) != ZONEFS_MAGIC)
1151		goto free_page;
1152
1153	stored_crc = le32_to_cpu(super->s_crc);
1154	super->s_crc = 0;
1155	crc = crc32(~0U, (unsigned char *)super, sizeof(struct zonefs_super));
1156	if (crc != stored_crc) {
1157		zonefs_err(sb, "Invalid checksum (Expected 0x%08x, got 0x%08x)",
1158			   crc, stored_crc);
1159		goto free_page;
1160	}
1161
1162	sbi->s_features = le64_to_cpu(super->s_features);
1163	if (sbi->s_features & ~ZONEFS_F_DEFINED_FEATURES) {
1164		zonefs_err(sb, "Unknown features set 0x%llx\n",
1165			   sbi->s_features);
1166		goto free_page;
1167	}
1168
1169	if (sbi->s_features & ZONEFS_F_UID) {
1170		sbi->s_uid = make_kuid(current_user_ns(),
1171				       le32_to_cpu(super->s_uid));
1172		if (!uid_valid(sbi->s_uid)) {
1173			zonefs_err(sb, "Invalid UID feature\n");
1174			goto free_page;
1175		}
1176	}
1177
1178	if (sbi->s_features & ZONEFS_F_GID) {
1179		sbi->s_gid = make_kgid(current_user_ns(),
1180				       le32_to_cpu(super->s_gid));
1181		if (!gid_valid(sbi->s_gid)) {
1182			zonefs_err(sb, "Invalid GID feature\n");
1183			goto free_page;
1184		}
1185	}
1186
1187	if (sbi->s_features & ZONEFS_F_PERM)
1188		sbi->s_perm = le32_to_cpu(super->s_perm);
1189
1190	if (memchr_inv(super->s_reserved, 0, sizeof(super->s_reserved))) {
1191		zonefs_err(sb, "Reserved area is being used\n");
1192		goto free_page;
1193	}
1194
1195	import_uuid(&sbi->s_uuid, super->s_uuid);
1196	ret = 0;
1197
 
 
1198free_page:
1199	__free_page(page);
1200
1201	return ret;
1202}
1203
1204static const struct super_operations zonefs_sops = {
1205	.alloc_inode	= zonefs_alloc_inode,
1206	.free_inode	= zonefs_free_inode,
1207	.statfs		= zonefs_statfs,
1208	.remount_fs	= zonefs_remount,
1209	.show_options	= zonefs_show_options,
1210};
1211
1212static int zonefs_get_zgroup_inodes(struct super_block *sb)
1213{
1214	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1215	struct inode *dir_inode;
1216	enum zonefs_ztype ztype;
1217
1218	for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
1219		if (!sbi->s_zgroup[ztype].g_nr_zones)
1220			continue;
1221
1222		dir_inode = zonefs_get_zgroup_inode(sb, ztype);
1223		if (IS_ERR(dir_inode))
1224			return PTR_ERR(dir_inode);
1225
1226		sbi->s_zgroup[ztype].g_inode = dir_inode;
1227	}
1228
1229	return 0;
1230}
1231
1232static void zonefs_release_zgroup_inodes(struct super_block *sb)
1233{
1234	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1235	enum zonefs_ztype ztype;
1236
1237	if (!sbi)
1238		return;
1239
1240	for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
1241		if (sbi->s_zgroup[ztype].g_inode) {
1242			iput(sbi->s_zgroup[ztype].g_inode);
1243			sbi->s_zgroup[ztype].g_inode = NULL;
1244		}
1245	}
1246}
1247
1248/*
1249 * Check that the device is zoned. If it is, get the list of zones and create
1250 * sub-directories and files according to the device zone configuration and
1251 * format options.
1252 */
1253static int zonefs_fill_super(struct super_block *sb, void *data, int silent)
1254{
 
1255	struct zonefs_sb_info *sbi;
1256	struct inode *inode;
1257	enum zonefs_ztype ztype;
1258	int ret;
1259
1260	if (!bdev_is_zoned(sb->s_bdev)) {
1261		zonefs_err(sb, "Not a zoned block device\n");
1262		return -EINVAL;
1263	}
1264
1265	/*
1266	 * Initialize super block information: the maximum file size is updated
1267	 * when the zone files are created so that the format option
1268	 * ZONEFS_F_AGGRCNV which increases the maximum file size of a file
1269	 * beyond the zone size is taken into account.
1270	 */
1271	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1272	if (!sbi)
1273		return -ENOMEM;
1274
1275	spin_lock_init(&sbi->s_lock);
1276	sb->s_fs_info = sbi;
1277	sb->s_magic = ZONEFS_MAGIC;
1278	sb->s_maxbytes = 0;
1279	sb->s_op = &zonefs_sops;
1280	sb->s_time_gran	= 1;
1281
1282	/*
1283	 * The block size is set to the device zone write granularity to ensure
1284	 * that write operations are always aligned according to the device
1285	 * interface constraints.
 
1286	 */
1287	sb_set_blocksize(sb, bdev_zone_write_granularity(sb->s_bdev));
1288	sbi->s_zone_sectors_shift = ilog2(bdev_zone_sectors(sb->s_bdev));
1289	sbi->s_uid = GLOBAL_ROOT_UID;
1290	sbi->s_gid = GLOBAL_ROOT_GID;
1291	sbi->s_perm = 0640;
1292	sbi->s_mount_opts = ZONEFS_MNTOPT_ERRORS_RO;
1293
1294	atomic_set(&sbi->s_wro_seq_files, 0);
1295	sbi->s_max_wro_seq_files = bdev_max_open_zones(sb->s_bdev);
1296	atomic_set(&sbi->s_active_seq_files, 0);
1297	sbi->s_max_active_seq_files = bdev_max_active_zones(sb->s_bdev);
1298
1299	ret = zonefs_read_super(sb);
1300	if (ret)
1301		return ret;
1302
1303	ret = zonefs_parse_options(sb, data);
1304	if (ret)
1305		return ret;
1306
1307	zonefs_info(sb, "Mounting %u zones", bdev_nr_zones(sb->s_bdev));
1308
1309	if (!sbi->s_max_wro_seq_files &&
1310	    !sbi->s_max_active_seq_files &&
1311	    sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) {
1312		zonefs_info(sb,
1313			"No open and active zone limits. Ignoring explicit_open mount option\n");
1314		sbi->s_mount_opts &= ~ZONEFS_MNTOPT_EXPLICIT_OPEN;
1315	}
1316
1317	/* Initialize the zone groups */
1318	ret = zonefs_init_zgroups(sb);
1319	if (ret)
1320		goto cleanup;
1321
1322	/* Create the root directory inode */
 
 
 
1323	ret = -ENOMEM;
1324	inode = new_inode(sb);
1325	if (!inode)
1326		goto cleanup;
1327
1328	inode->i_ino = bdev_nr_zones(sb->s_bdev);
1329	inode->i_mode = S_IFDIR | 0555;
1330	simple_inode_init_ts(inode);
1331	inode->i_op = &zonefs_dir_inode_operations;
1332	inode->i_fop = &zonefs_dir_operations;
1333	inode->i_size = 2;
1334	set_nlink(inode, 2);
1335	for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) {
1336		if (sbi->s_zgroup[ztype].g_nr_zones) {
1337			inc_nlink(inode);
1338			inode->i_size++;
1339		}
1340	}
1341
1342	sb->s_root = d_make_root(inode);
1343	if (!sb->s_root)
1344		goto cleanup;
1345
1346	/*
1347	 * Take a reference on the zone groups directory inodes
1348	 * to keep them in the inode cache.
1349	 */
1350	ret = zonefs_get_zgroup_inodes(sb);
1351	if (ret)
1352		goto cleanup;
1353
1354	ret = zonefs_sysfs_register(sb);
1355	if (ret)
1356		goto cleanup;
1357
1358	return 0;
1359
1360cleanup:
1361	zonefs_release_zgroup_inodes(sb);
1362	zonefs_free_zgroups(sb);
1363
1364	return ret;
1365}
1366
1367static struct dentry *zonefs_mount(struct file_system_type *fs_type,
1368				   int flags, const char *dev_name, void *data)
1369{
1370	return mount_bdev(fs_type, flags, dev_name, data, zonefs_fill_super);
1371}
1372
1373static void zonefs_kill_super(struct super_block *sb)
1374{
1375	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1376
1377	/* Release the reference on the zone group directory inodes */
1378	zonefs_release_zgroup_inodes(sb);
1379
1380	kill_block_super(sb);
1381
1382	zonefs_sysfs_unregister(sb);
1383	zonefs_free_zgroups(sb);
1384	kfree(sbi);
1385}
1386
1387/*
1388 * File system definition and registration.
1389 */
1390static struct file_system_type zonefs_type = {
1391	.owner		= THIS_MODULE,
1392	.name		= "zonefs",
1393	.mount		= zonefs_mount,
1394	.kill_sb	= zonefs_kill_super,
1395	.fs_flags	= FS_REQUIRES_DEV,
1396};
1397
1398static int __init zonefs_init_inodecache(void)
1399{
1400	zonefs_inode_cachep = kmem_cache_create("zonefs_inode_cache",
1401			sizeof(struct zonefs_inode_info), 0,
1402			(SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT),
1403			NULL);
1404	if (zonefs_inode_cachep == NULL)
1405		return -ENOMEM;
1406	return 0;
1407}
1408
1409static void zonefs_destroy_inodecache(void)
1410{
1411	/*
1412	 * Make sure all delayed rcu free inodes are flushed before we
1413	 * destroy the inode cache.
1414	 */
1415	rcu_barrier();
1416	kmem_cache_destroy(zonefs_inode_cachep);
1417}
1418
1419static int __init zonefs_init(void)
1420{
1421	int ret;
1422
1423	BUILD_BUG_ON(sizeof(struct zonefs_super) != ZONEFS_SUPER_SIZE);
1424
1425	ret = zonefs_init_inodecache();
1426	if (ret)
1427		return ret;
1428
1429	ret = zonefs_sysfs_init();
1430	if (ret)
1431		goto destroy_inodecache;
1432
1433	ret = register_filesystem(&zonefs_type);
1434	if (ret)
1435		goto sysfs_exit;
 
 
1436
1437	return 0;
1438
1439sysfs_exit:
1440	zonefs_sysfs_exit();
1441destroy_inodecache:
1442	zonefs_destroy_inodecache();
1443
1444	return ret;
1445}
1446
1447static void __exit zonefs_exit(void)
1448{
1449	unregister_filesystem(&zonefs_type);
1450	zonefs_sysfs_exit();
1451	zonefs_destroy_inodecache();
 
1452}
1453
1454MODULE_AUTHOR("Damien Le Moal");
1455MODULE_DESCRIPTION("Zone file system for zoned block devices");
1456MODULE_LICENSE("GPL");
1457MODULE_ALIAS_FS("zonefs");
1458module_init(zonefs_init);
1459module_exit(zonefs_exit);
v5.9
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Simple file system for zoned block devices exposing zones as files.
   4 *
   5 * Copyright (C) 2019 Western Digital Corporation or its affiliates.
   6 */
   7#include <linux/module.h>
   8#include <linux/fs.h>
   9#include <linux/magic.h>
  10#include <linux/iomap.h>
  11#include <linux/init.h>
  12#include <linux/slab.h>
  13#include <linux/blkdev.h>
  14#include <linux/statfs.h>
  15#include <linux/writeback.h>
  16#include <linux/quotaops.h>
  17#include <linux/seq_file.h>
  18#include <linux/parser.h>
  19#include <linux/uio.h>
  20#include <linux/mman.h>
  21#include <linux/sched/mm.h>
  22#include <linux/crc32.h>
  23#include <linux/task_io_accounting_ops.h>
  24
  25#include "zonefs.h"
  26
  27static int zonefs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
  28			      unsigned int flags, struct iomap *iomap,
  29			      struct iomap *srcmap)
 
 
 
 
  30{
  31	struct zonefs_inode_info *zi = ZONEFS_I(inode);
  32	struct super_block *sb = inode->i_sb;
  33	loff_t isize;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  34
  35	/* All I/Os should always be within the file maximum size */
  36	if (WARN_ON_ONCE(offset + length > zi->i_max_size))
  37		return -EIO;
  38
  39	/*
  40	 * Sequential zones can only accept direct writes. This is already
  41	 * checked when writes are issued, so warn if we see a page writeback
  42	 * operation.
  43	 */
  44	if (WARN_ON_ONCE(zi->i_ztype == ZONEFS_ZTYPE_SEQ &&
  45			 (flags & IOMAP_WRITE) && !(flags & IOMAP_DIRECT)))
  46		return -EIO;
  47
  48	/*
  49	 * For conventional zones, all blocks are always mapped. For sequential
  50	 * zones, all blocks after always mapped below the inode size (zone
  51	 * write pointer) and unwriten beyond.
  52	 */
  53	mutex_lock(&zi->i_truncate_mutex);
  54	isize = i_size_read(inode);
  55	if (offset >= isize)
  56		iomap->type = IOMAP_UNWRITTEN;
  57	else
  58		iomap->type = IOMAP_MAPPED;
  59	if (flags & IOMAP_WRITE)
  60		length = zi->i_max_size - offset;
  61	else
  62		length = min(length, isize - offset);
  63	mutex_unlock(&zi->i_truncate_mutex);
  64
  65	iomap->offset = ALIGN_DOWN(offset, sb->s_blocksize);
  66	iomap->length = ALIGN(offset + length, sb->s_blocksize) - iomap->offset;
  67	iomap->bdev = inode->i_sb->s_bdev;
  68	iomap->addr = (zi->i_zsector << SECTOR_SHIFT) + iomap->offset;
  69
  70	return 0;
  71}
  72
  73static const struct iomap_ops zonefs_iomap_ops = {
  74	.iomap_begin	= zonefs_iomap_begin,
  75};
  76
  77static int zonefs_readpage(struct file *unused, struct page *page)
  78{
  79	return iomap_readpage(page, &zonefs_iomap_ops);
  80}
  81
  82static void zonefs_readahead(struct readahead_control *rac)
  83{
  84	iomap_readahead(rac, &zonefs_iomap_ops);
  85}
  86
  87/*
  88 * Map blocks for page writeback. This is used only on conventional zone files,
  89 * which implies that the page range can only be within the fixed inode size.
  90 */
  91static int zonefs_map_blocks(struct iomap_writepage_ctx *wpc,
  92			     struct inode *inode, loff_t offset)
  93{
  94	struct zonefs_inode_info *zi = ZONEFS_I(inode);
  95
  96	if (WARN_ON_ONCE(zi->i_ztype != ZONEFS_ZTYPE_CNV))
  97		return -EIO;
  98	if (WARN_ON_ONCE(offset >= i_size_read(inode)))
  99		return -EIO;
 
 
 
 
 
 
 100
 101	/* If the mapping is already OK, nothing needs to be done */
 102	if (offset >= wpc->iomap.offset &&
 103	    offset < wpc->iomap.offset + wpc->iomap.length)
 104		return 0;
 
 
 
 
 
 105
 106	return zonefs_iomap_begin(inode, offset, zi->i_max_size - offset,
 107				  IOMAP_WRITE, &wpc->iomap, NULL);
 108}
 109
 110static const struct iomap_writeback_ops zonefs_writeback_ops = {
 111	.map_blocks		= zonefs_map_blocks,
 112};
 113
 114static int zonefs_writepage(struct page *page, struct writeback_control *wbc)
 115{
 116	struct iomap_writepage_ctx wpc = { };
 117
 118	return iomap_writepage(page, wbc, &wpc, &zonefs_writeback_ops);
 119}
 120
 121static int zonefs_writepages(struct address_space *mapping,
 122			     struct writeback_control *wbc)
 123{
 124	struct iomap_writepage_ctx wpc = { };
 
 
 
 
 
 
 
 
 
 125
 126	return iomap_writepages(mapping, wbc, &wpc, &zonefs_writeback_ops);
 
 
 
 127}
 128
 129static const struct address_space_operations zonefs_file_aops = {
 130	.readpage		= zonefs_readpage,
 131	.readahead		= zonefs_readahead,
 132	.writepage		= zonefs_writepage,
 133	.writepages		= zonefs_writepages,
 134	.set_page_dirty		= iomap_set_page_dirty,
 135	.releasepage		= iomap_releasepage,
 136	.invalidatepage		= iomap_invalidatepage,
 137	.migratepage		= iomap_migrate_page,
 138	.is_partially_uptodate	= iomap_is_partially_uptodate,
 139	.error_remove_page	= generic_error_remove_page,
 140	.direct_IO		= noop_direct_IO,
 141};
 142
 143static void zonefs_update_stats(struct inode *inode, loff_t new_isize)
 144{
 145	struct super_block *sb = inode->i_sb;
 146	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
 147	loff_t old_isize = i_size_read(inode);
 148	loff_t nr_blocks;
 149
 150	if (new_isize == old_isize)
 151		return;
 152
 153	spin_lock(&sbi->s_lock);
 154
 155	/*
 156	 * This may be called for an update after an IO error.
 157	 * So beware of the values seen.
 158	 */
 159	if (new_isize < old_isize) {
 160		nr_blocks = (old_isize - new_isize) >> sb->s_blocksize_bits;
 161		if (sbi->s_used_blocks > nr_blocks)
 162			sbi->s_used_blocks -= nr_blocks;
 163		else
 164			sbi->s_used_blocks = 0;
 165	} else {
 166		sbi->s_used_blocks +=
 167			(new_isize - old_isize) >> sb->s_blocksize_bits;
 168		if (sbi->s_used_blocks > sbi->s_blocks)
 169			sbi->s_used_blocks = sbi->s_blocks;
 170	}
 171
 172	spin_unlock(&sbi->s_lock);
 173}
 174
 175/*
 176 * Check a zone condition and adjust its file inode access permissions for
 177 * offline and readonly zones. Return the inode size corresponding to the
 178 * amount of readable data in the zone.
 179 */
 180static loff_t zonefs_check_zone_condition(struct inode *inode,
 181					  struct blk_zone *zone, bool warn,
 182					  bool mount)
 183{
 184	struct zonefs_inode_info *zi = ZONEFS_I(inode);
 185
 186	switch (zone->cond) {
 187	case BLK_ZONE_COND_OFFLINE:
 188		/*
 189		 * Dead zone: make the inode immutable, disable all accesses
 190		 * and set the file size to 0 (zone wp set to zone start).
 191		 */
 192		if (warn)
 193			zonefs_warn(inode->i_sb, "inode %lu: offline zone\n",
 194				    inode->i_ino);
 195		inode->i_flags |= S_IMMUTABLE;
 196		inode->i_mode &= ~0777;
 197		zone->wp = zone->start;
 198		return 0;
 199	case BLK_ZONE_COND_READONLY:
 200		/*
 201		 * The write pointer of read-only zones is invalid. If such a
 202		 * zone is found during mount, the file size cannot be retrieved
 203		 * so we treat the zone as offline (mount == true case).
 204		 * Otherwise, keep the file size as it was when last updated
 205		 * so that the user can recover data. In both cases, writes are
 206		 * always disabled for the zone.
 207		 */
 208		if (warn)
 209			zonefs_warn(inode->i_sb, "inode %lu: read-only zone\n",
 210				    inode->i_ino);
 211		inode->i_flags |= S_IMMUTABLE;
 212		if (mount) {
 213			zone->cond = BLK_ZONE_COND_OFFLINE;
 214			inode->i_mode &= ~0777;
 215			zone->wp = zone->start;
 216			return 0;
 217		}
 218		inode->i_mode &= ~0222;
 219		return i_size_read(inode);
 220	default:
 221		if (zi->i_ztype == ZONEFS_ZTYPE_CNV)
 222			return zi->i_max_size;
 223		return (zone->wp - zone->start) << SECTOR_SHIFT;
 224	}
 225}
 226
 227struct zonefs_ioerr_data {
 228	struct inode	*inode;
 229	bool		write;
 230};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 231
 232static int zonefs_io_error_cb(struct blk_zone *zone, unsigned int idx,
 233			      void *data)
 234{
 235	struct zonefs_ioerr_data *err = data;
 236	struct inode *inode = err->inode;
 237	struct zonefs_inode_info *zi = ZONEFS_I(inode);
 
 
 
 
 
 
 
 238	struct super_block *sb = inode->i_sb;
 239	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
 240	loff_t isize, data_size;
 241
 242	/*
 243	 * Check the zone condition: if the zone is not "bad" (offline or
 244	 * read-only), read errors are simply signaled to the IO issuer as long
 245	 * as there is no inconsistency between the inode size and the amount of
 246	 * data writen in the zone (data_size).
 247	 */
 248	data_size = zonefs_check_zone_condition(inode, zone, true, false);
 249	isize = i_size_read(inode);
 250	if (zone->cond != BLK_ZONE_COND_OFFLINE &&
 251	    zone->cond != BLK_ZONE_COND_READONLY &&
 252	    !err->write && isize == data_size)
 253		return 0;
 254
 255	/*
 256	 * At this point, we detected either a bad zone or an inconsistency
 257	 * between the inode size and the amount of data written in the zone.
 258	 * For the latter case, the cause may be a write IO error or an external
 259	 * action on the device. Two error patterns exist:
 260	 * 1) The inode size is lower than the amount of data in the zone:
 261	 *    a write operation partially failed and data was writen at the end
 262	 *    of the file. This can happen in the case of a large direct IO
 263	 *    needing several BIOs and/or write requests to be processed.
 264	 * 2) The inode size is larger than the amount of data in the zone:
 265	 *    this can happen with a deferred write error with the use of the
 266	 *    device side write cache after getting successful write IO
 267	 *    completions. Other possibilities are (a) an external corruption,
 268	 *    e.g. an application reset the zone directly, or (b) the device
 269	 *    has a serious problem (e.g. firmware bug).
 270	 *
 271	 * In all cases, warn about inode size inconsistency and handle the
 272	 * IO error according to the zone condition and to the mount options.
 273	 */
 274	if (zi->i_ztype == ZONEFS_ZTYPE_SEQ && isize != data_size)
 275		zonefs_warn(sb, "inode %lu: invalid size %lld (should be %lld)\n",
 
 276			    inode->i_ino, isize, data_size);
 277
 278	/*
 279	 * First handle bad zones signaled by hardware. The mount options
 280	 * errors=zone-ro and errors=zone-offline result in changing the
 281	 * zone condition to read-only and offline respectively, as if the
 282	 * condition was signaled by the hardware.
 283	 */
 284	if (zone->cond == BLK_ZONE_COND_OFFLINE ||
 285	    sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL) {
 286		zonefs_warn(sb, "inode %lu: read/write access disabled\n",
 287			    inode->i_ino);
 288		if (zone->cond != BLK_ZONE_COND_OFFLINE) {
 289			zone->cond = BLK_ZONE_COND_OFFLINE;
 290			data_size = zonefs_check_zone_condition(inode, zone,
 291								false, false);
 292		}
 293	} else if (zone->cond == BLK_ZONE_COND_READONLY ||
 294		   sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO) {
 295		zonefs_warn(sb, "inode %lu: write access disabled\n",
 296			    inode->i_ino);
 297		if (zone->cond != BLK_ZONE_COND_READONLY) {
 298			zone->cond = BLK_ZONE_COND_READONLY;
 299			data_size = zonefs_check_zone_condition(inode, zone,
 300								false, false);
 301		}
 
 
 
 302	}
 303
 304	/*
 
 
 
 
 
 
 
 
 
 
 305	 * If error=remount-ro was specified, any error result in remounting
 306	 * the volume as read-only.
 307	 */
 308	if ((sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO) && !sb_rdonly(sb)) {
 309		zonefs_warn(sb, "remounting filesystem read-only\n");
 310		sb->s_flags |= SB_RDONLY;
 311	}
 312
 313	/*
 314	 * Update block usage stats and the inode size  to prevent access to
 315	 * invalid data.
 316	 */
 317	zonefs_update_stats(inode, data_size);
 318	i_size_write(inode, data_size);
 319	zi->i_wpoffset = data_size;
 320
 321	return 0;
 322}
 323
 324/*
 325 * When an file IO error occurs, check the file zone to see if there is a change
 326 * in the zone condition (e.g. offline or read-only). For a failed write to a
 327 * sequential zone, the zone write pointer position must also be checked to
 328 * eventually correct the file size and zonefs inode write pointer offset
 329 * (which can be out of sync with the drive due to partial write failures).
 330 */
 331static void zonefs_io_error(struct inode *inode, bool write)
 332{
 333	struct zonefs_inode_info *zi = ZONEFS_I(inode);
 334	struct super_block *sb = inode->i_sb;
 335	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
 336	unsigned int noio_flag;
 337	unsigned int nr_zones =
 338		zi->i_zone_size >> (sbi->s_zone_sectors_shift + SECTOR_SHIFT);
 339	struct zonefs_ioerr_data err = {
 340		.inode = inode,
 341		.write = write,
 342	};
 343	int ret;
 344
 345	mutex_lock(&zi->i_truncate_mutex);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 346
 347	/*
 348	 * Memory allocations in blkdev_report_zones() can trigger a memory
 349	 * reclaim which may in turn cause a recursion into zonefs as well as
 350	 * struct request allocations for the same device. The former case may
 351	 * end up in a deadlock on the inode truncate mutex, while the latter
 352	 * may prevent IO forward progress. Executing the report zones under
 353	 * the GFP_NOIO context avoids both problems.
 354	 */
 355	noio_flag = memalloc_noio_save();
 356	ret = blkdev_report_zones(sb->s_bdev, zi->i_zsector, nr_zones,
 357				  zonefs_io_error_cb, &err);
 358	if (ret != nr_zones)
 
 
 359		zonefs_err(sb, "Get inode %lu zone information failed %d\n",
 360			   inode->i_ino, ret);
 361	memalloc_noio_restore(noio_flag);
 362
 363	mutex_unlock(&zi->i_truncate_mutex);
 364}
 365
 366static int zonefs_file_truncate(struct inode *inode, loff_t isize)
 367{
 368	struct zonefs_inode_info *zi = ZONEFS_I(inode);
 369	loff_t old_isize;
 370	enum req_opf op;
 371	int ret = 0;
 372
 373	/*
 374	 * Only sequential zone files can be truncated and truncation is allowed
 375	 * only down to a 0 size, which is equivalent to a zone reset, and to
 376	 * the maximum file size, which is equivalent to a zone finish.
 377	 */
 378	if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
 379		return -EPERM;
 380
 381	if (!isize)
 382		op = REQ_OP_ZONE_RESET;
 383	else if (isize == zi->i_max_size)
 384		op = REQ_OP_ZONE_FINISH;
 385	else
 386		return -EPERM;
 387
 388	inode_dio_wait(inode);
 389
 390	/* Serialize against page faults */
 391	down_write(&zi->i_mmap_sem);
 392
 393	/* Serialize against zonefs_iomap_begin() */
 394	mutex_lock(&zi->i_truncate_mutex);
 395
 396	old_isize = i_size_read(inode);
 397	if (isize == old_isize)
 398		goto unlock;
 399
 400	ret = blkdev_zone_mgmt(inode->i_sb->s_bdev, op, zi->i_zsector,
 401			       zi->i_zone_size >> SECTOR_SHIFT, GFP_NOFS);
 402	if (ret) {
 403		zonefs_err(inode->i_sb,
 404			   "Zone management operation at %llu failed %d",
 405			   zi->i_zsector, ret);
 406		goto unlock;
 407	}
 408
 409	zonefs_update_stats(inode, isize);
 410	truncate_setsize(inode, isize);
 411	zi->i_wpoffset = isize;
 412
 413unlock:
 414	mutex_unlock(&zi->i_truncate_mutex);
 415	up_write(&zi->i_mmap_sem);
 416
 417	return ret;
 418}
 419
 420static int zonefs_inode_setattr(struct dentry *dentry, struct iattr *iattr)
 421{
 422	struct inode *inode = d_inode(dentry);
 423	int ret;
 424
 425	if (unlikely(IS_IMMUTABLE(inode)))
 426		return -EPERM;
 427
 428	ret = setattr_prepare(dentry, iattr);
 429	if (ret)
 430		return ret;
 431
 432	/*
 433	 * Since files and directories cannot be created nor deleted, do not
 434	 * allow setting any write attributes on the sub-directories grouping
 435	 * files by zone type.
 436	 */
 437	if ((iattr->ia_valid & ATTR_MODE) && S_ISDIR(inode->i_mode) &&
 438	    (iattr->ia_mode & 0222))
 439		return -EPERM;
 440
 441	if (((iattr->ia_valid & ATTR_UID) &&
 442	     !uid_eq(iattr->ia_uid, inode->i_uid)) ||
 443	    ((iattr->ia_valid & ATTR_GID) &&
 444	     !gid_eq(iattr->ia_gid, inode->i_gid))) {
 445		ret = dquot_transfer(inode, iattr);
 446		if (ret)
 447			return ret;
 448	}
 449
 450	if (iattr->ia_valid & ATTR_SIZE) {
 451		ret = zonefs_file_truncate(inode, iattr->ia_size);
 452		if (ret)
 453			return ret;
 454	}
 455
 456	setattr_copy(inode, iattr);
 457
 458	return 0;
 459}
 460
 461static const struct inode_operations zonefs_file_inode_operations = {
 462	.setattr	= zonefs_inode_setattr,
 463};
 464
 465static int zonefs_file_fsync(struct file *file, loff_t start, loff_t end,
 466			     int datasync)
 467{
 468	struct inode *inode = file_inode(file);
 469	int ret = 0;
 470
 471	if (unlikely(IS_IMMUTABLE(inode)))
 472		return -EPERM;
 473
 474	/*
 475	 * Since only direct writes are allowed in sequential files, page cache
 476	 * flush is needed only for conventional zone files.
 477	 */
 478	if (ZONEFS_I(inode)->i_ztype == ZONEFS_ZTYPE_CNV)
 479		ret = file_write_and_wait_range(file, start, end);
 480	if (!ret)
 481		ret = blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL);
 482
 483	if (ret)
 484		zonefs_io_error(inode, true);
 485
 486	return ret;
 487}
 488
 489static vm_fault_t zonefs_filemap_fault(struct vm_fault *vmf)
 490{
 491	struct zonefs_inode_info *zi = ZONEFS_I(file_inode(vmf->vma->vm_file));
 492	vm_fault_t ret;
 493
 494	down_read(&zi->i_mmap_sem);
 495	ret = filemap_fault(vmf);
 496	up_read(&zi->i_mmap_sem);
 497
 498	return ret;
 499}
 500
 501static vm_fault_t zonefs_filemap_page_mkwrite(struct vm_fault *vmf)
 502{
 503	struct inode *inode = file_inode(vmf->vma->vm_file);
 504	struct zonefs_inode_info *zi = ZONEFS_I(inode);
 505	vm_fault_t ret;
 506
 507	if (unlikely(IS_IMMUTABLE(inode)))
 508		return VM_FAULT_SIGBUS;
 509
 510	/*
 511	 * Sanity check: only conventional zone files can have shared
 512	 * writeable mappings.
 513	 */
 514	if (WARN_ON_ONCE(zi->i_ztype != ZONEFS_ZTYPE_CNV))
 515		return VM_FAULT_NOPAGE;
 516
 517	sb_start_pagefault(inode->i_sb);
 518	file_update_time(vmf->vma->vm_file);
 519
 520	/* Serialize against truncates */
 521	down_read(&zi->i_mmap_sem);
 522	ret = iomap_page_mkwrite(vmf, &zonefs_iomap_ops);
 523	up_read(&zi->i_mmap_sem);
 524
 525	sb_end_pagefault(inode->i_sb);
 526	return ret;
 527}
 528
 529static const struct vm_operations_struct zonefs_file_vm_ops = {
 530	.fault		= zonefs_filemap_fault,
 531	.map_pages	= filemap_map_pages,
 532	.page_mkwrite	= zonefs_filemap_page_mkwrite,
 533};
 534
 535static int zonefs_file_mmap(struct file *file, struct vm_area_struct *vma)
 536{
 537	/*
 538	 * Conventional zones accept random writes, so their files can support
 539	 * shared writable mappings. For sequential zone files, only read
 540	 * mappings are possible since there are no guarantees for write
 541	 * ordering between msync() and page cache writeback.
 542	 */
 543	if (ZONEFS_I(file_inode(file))->i_ztype == ZONEFS_ZTYPE_SEQ &&
 544	    (vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
 545		return -EINVAL;
 546
 547	file_accessed(file);
 548	vma->vm_ops = &zonefs_file_vm_ops;
 549
 550	return 0;
 551}
 552
 553static loff_t zonefs_file_llseek(struct file *file, loff_t offset, int whence)
 554{
 555	loff_t isize = i_size_read(file_inode(file));
 556
 557	/*
 558	 * Seeks are limited to below the zone size for conventional zones
 559	 * and below the zone write pointer for sequential zones. In both
 560	 * cases, this limit is the inode size.
 561	 */
 562	return generic_file_llseek_size(file, offset, whence, isize, isize);
 563}
 564
 565static int zonefs_file_write_dio_end_io(struct kiocb *iocb, ssize_t size,
 566					int error, unsigned int flags)
 567{
 568	struct inode *inode = file_inode(iocb->ki_filp);
 569	struct zonefs_inode_info *zi = ZONEFS_I(inode);
 570
 571	if (error) {
 572		zonefs_io_error(inode, true);
 573		return error;
 574	}
 575
 576	if (size && zi->i_ztype != ZONEFS_ZTYPE_CNV) {
 577		/*
 578		 * Note that we may be seeing completions out of order,
 579		 * but that is not a problem since a write completed
 580		 * successfully necessarily means that all preceding writes
 581		 * were also successful. So we can safely increase the inode
 582		 * size to the write end location.
 583		 */
 584		mutex_lock(&zi->i_truncate_mutex);
 585		if (i_size_read(inode) < iocb->ki_pos + size) {
 586			zonefs_update_stats(inode, iocb->ki_pos + size);
 587			i_size_write(inode, iocb->ki_pos + size);
 588		}
 589		mutex_unlock(&zi->i_truncate_mutex);
 590	}
 591
 592	return 0;
 593}
 594
 595static const struct iomap_dio_ops zonefs_write_dio_ops = {
 596	.end_io			= zonefs_file_write_dio_end_io,
 597};
 598
 599static ssize_t zonefs_file_dio_append(struct kiocb *iocb, struct iov_iter *from)
 600{
 601	struct inode *inode = file_inode(iocb->ki_filp);
 602	struct zonefs_inode_info *zi = ZONEFS_I(inode);
 603	struct block_device *bdev = inode->i_sb->s_bdev;
 604	unsigned int max;
 605	struct bio *bio;
 606	ssize_t size;
 607	int nr_pages;
 608	ssize_t ret;
 609
 610	max = queue_max_zone_append_sectors(bdev_get_queue(bdev));
 611	max = ALIGN_DOWN(max << SECTOR_SHIFT, inode->i_sb->s_blocksize);
 612	iov_iter_truncate(from, max);
 613
 614	nr_pages = iov_iter_npages(from, BIO_MAX_PAGES);
 615	if (!nr_pages)
 616		return 0;
 617
 618	bio = bio_alloc_bioset(GFP_NOFS, nr_pages, &fs_bio_set);
 619	if (!bio)
 620		return -ENOMEM;
 621
 622	bio_set_dev(bio, bdev);
 623	bio->bi_iter.bi_sector = zi->i_zsector;
 624	bio->bi_write_hint = iocb->ki_hint;
 625	bio->bi_ioprio = iocb->ki_ioprio;
 626	bio->bi_opf = REQ_OP_ZONE_APPEND | REQ_SYNC | REQ_IDLE;
 627	if (iocb->ki_flags & IOCB_DSYNC)
 628		bio->bi_opf |= REQ_FUA;
 629
 630	ret = bio_iov_iter_get_pages(bio, from);
 631	if (unlikely(ret)) {
 632		bio_io_error(bio);
 633		return ret;
 634	}
 635	size = bio->bi_iter.bi_size;
 636	task_io_account_write(ret);
 637
 638	if (iocb->ki_flags & IOCB_HIPRI)
 639		bio_set_polled(bio, iocb);
 640
 641	ret = submit_bio_wait(bio);
 642
 643	bio_put(bio);
 644
 645	zonefs_file_write_dio_end_io(iocb, size, ret, 0);
 646	if (ret >= 0) {
 647		iocb->ki_pos += size;
 648		return size;
 649	}
 650
 651	return ret;
 652}
 653
 654/*
 655 * Handle direct writes. For sequential zone files, this is the only possible
 656 * write path. For these files, check that the user is issuing writes
 657 * sequentially from the end of the file. This code assumes that the block layer
 658 * delivers write requests to the device in sequential order. This is always the
 659 * case if a block IO scheduler implementing the ELEVATOR_F_ZBD_SEQ_WRITE
 660 * elevator feature is being used (e.g. mq-deadline). The block layer always
 661 * automatically select such an elevator for zoned block devices during the
 662 * device initialization.
 663 */
 664static ssize_t zonefs_file_dio_write(struct kiocb *iocb, struct iov_iter *from)
 665{
 666	struct inode *inode = file_inode(iocb->ki_filp);
 667	struct zonefs_inode_info *zi = ZONEFS_I(inode);
 668	struct super_block *sb = inode->i_sb;
 669	bool sync = is_sync_kiocb(iocb);
 670	bool append = false;
 671	size_t count;
 672	ssize_t ret;
 673
 674	/*
 675	 * For async direct IOs to sequential zone files, refuse IOCB_NOWAIT
 676	 * as this can cause write reordering (e.g. the first aio gets EAGAIN
 677	 * on the inode lock but the second goes through but is now unaligned).
 678	 */
 679	if (zi->i_ztype == ZONEFS_ZTYPE_SEQ && !sync &&
 680	    (iocb->ki_flags & IOCB_NOWAIT))
 681		return -EOPNOTSUPP;
 682
 683	if (iocb->ki_flags & IOCB_NOWAIT) {
 684		if (!inode_trylock(inode))
 685			return -EAGAIN;
 686	} else {
 687		inode_lock(inode);
 688	}
 689
 690	ret = generic_write_checks(iocb, from);
 691	if (ret <= 0)
 692		goto inode_unlock;
 693
 694	iov_iter_truncate(from, zi->i_max_size - iocb->ki_pos);
 695	count = iov_iter_count(from);
 696
 697	if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) {
 698		ret = -EINVAL;
 699		goto inode_unlock;
 700	}
 701
 702	/* Enforce sequential writes (append only) in sequential zones */
 703	if (zi->i_ztype == ZONEFS_ZTYPE_SEQ) {
 704		mutex_lock(&zi->i_truncate_mutex);
 705		if (iocb->ki_pos != zi->i_wpoffset) {
 706			mutex_unlock(&zi->i_truncate_mutex);
 707			ret = -EINVAL;
 708			goto inode_unlock;
 709		}
 710		mutex_unlock(&zi->i_truncate_mutex);
 711		append = sync;
 712	}
 713
 714	if (append)
 715		ret = zonefs_file_dio_append(iocb, from);
 716	else
 717		ret = iomap_dio_rw(iocb, from, &zonefs_iomap_ops,
 718				   &zonefs_write_dio_ops, sync);
 719	if (zi->i_ztype == ZONEFS_ZTYPE_SEQ &&
 720	    (ret > 0 || ret == -EIOCBQUEUED)) {
 721		if (ret > 0)
 722			count = ret;
 723		mutex_lock(&zi->i_truncate_mutex);
 724		zi->i_wpoffset += count;
 725		mutex_unlock(&zi->i_truncate_mutex);
 726	}
 727
 728inode_unlock:
 729	inode_unlock(inode);
 730
 731	return ret;
 732}
 733
 734static ssize_t zonefs_file_buffered_write(struct kiocb *iocb,
 735					  struct iov_iter *from)
 736{
 737	struct inode *inode = file_inode(iocb->ki_filp);
 738	struct zonefs_inode_info *zi = ZONEFS_I(inode);
 739	ssize_t ret;
 740
 741	/*
 742	 * Direct IO writes are mandatory for sequential zone files so that the
 743	 * write IO issuing order is preserved.
 744	 */
 745	if (zi->i_ztype != ZONEFS_ZTYPE_CNV)
 746		return -EIO;
 747
 748	if (iocb->ki_flags & IOCB_NOWAIT) {
 749		if (!inode_trylock(inode))
 750			return -EAGAIN;
 751	} else {
 752		inode_lock(inode);
 753	}
 754
 755	ret = generic_write_checks(iocb, from);
 756	if (ret <= 0)
 757		goto inode_unlock;
 758
 759	iov_iter_truncate(from, zi->i_max_size - iocb->ki_pos);
 760
 761	ret = iomap_file_buffered_write(iocb, from, &zonefs_iomap_ops);
 762	if (ret > 0)
 763		iocb->ki_pos += ret;
 764	else if (ret == -EIO)
 765		zonefs_io_error(inode, true);
 766
 767inode_unlock:
 768	inode_unlock(inode);
 769	if (ret > 0)
 770		ret = generic_write_sync(iocb, ret);
 771
 772	return ret;
 773}
 774
 775static ssize_t zonefs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
 776{
 777	struct inode *inode = file_inode(iocb->ki_filp);
 778
 779	if (unlikely(IS_IMMUTABLE(inode)))
 780		return -EPERM;
 781
 782	if (sb_rdonly(inode->i_sb))
 783		return -EROFS;
 784
 785	/* Write operations beyond the zone size are not allowed */
 786	if (iocb->ki_pos >= ZONEFS_I(inode)->i_max_size)
 787		return -EFBIG;
 788
 789	if (iocb->ki_flags & IOCB_DIRECT) {
 790		ssize_t ret = zonefs_file_dio_write(iocb, from);
 791		if (ret != -ENOTBLK)
 792			return ret;
 793	}
 794
 795	return zonefs_file_buffered_write(iocb, from);
 796}
 797
 798static int zonefs_file_read_dio_end_io(struct kiocb *iocb, ssize_t size,
 799				       int error, unsigned int flags)
 800{
 801	if (error) {
 802		zonefs_io_error(file_inode(iocb->ki_filp), false);
 803		return error;
 804	}
 805
 806	return 0;
 807}
 808
 809static const struct iomap_dio_ops zonefs_read_dio_ops = {
 810	.end_io			= zonefs_file_read_dio_end_io,
 811};
 812
 813static ssize_t zonefs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
 814{
 815	struct inode *inode = file_inode(iocb->ki_filp);
 816	struct zonefs_inode_info *zi = ZONEFS_I(inode);
 817	struct super_block *sb = inode->i_sb;
 818	loff_t isize;
 819	ssize_t ret;
 820
 821	/* Offline zones cannot be read */
 822	if (unlikely(IS_IMMUTABLE(inode) && !(inode->i_mode & 0777)))
 823		return -EPERM;
 824
 825	if (iocb->ki_pos >= zi->i_max_size)
 826		return 0;
 827
 828	if (iocb->ki_flags & IOCB_NOWAIT) {
 829		if (!inode_trylock_shared(inode))
 830			return -EAGAIN;
 831	} else {
 832		inode_lock_shared(inode);
 833	}
 834
 835	/* Limit read operations to written data */
 836	mutex_lock(&zi->i_truncate_mutex);
 837	isize = i_size_read(inode);
 838	if (iocb->ki_pos >= isize) {
 839		mutex_unlock(&zi->i_truncate_mutex);
 840		ret = 0;
 841		goto inode_unlock;
 842	}
 843	iov_iter_truncate(to, isize - iocb->ki_pos);
 844	mutex_unlock(&zi->i_truncate_mutex);
 845
 846	if (iocb->ki_flags & IOCB_DIRECT) {
 847		size_t count = iov_iter_count(to);
 848
 849		if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) {
 850			ret = -EINVAL;
 851			goto inode_unlock;
 852		}
 853		file_accessed(iocb->ki_filp);
 854		ret = iomap_dio_rw(iocb, to, &zonefs_iomap_ops,
 855				   &zonefs_read_dio_ops, is_sync_kiocb(iocb));
 856	} else {
 857		ret = generic_file_read_iter(iocb, to);
 858		if (ret == -EIO)
 859			zonefs_io_error(inode, false);
 860	}
 861
 862inode_unlock:
 863	inode_unlock_shared(inode);
 864
 865	return ret;
 866}
 867
 868static const struct file_operations zonefs_file_operations = {
 869	.open		= generic_file_open,
 870	.fsync		= zonefs_file_fsync,
 871	.mmap		= zonefs_file_mmap,
 872	.llseek		= zonefs_file_llseek,
 873	.read_iter	= zonefs_file_read_iter,
 874	.write_iter	= zonefs_file_write_iter,
 875	.splice_read	= generic_file_splice_read,
 876	.splice_write	= iter_file_splice_write,
 877	.iopoll		= iomap_dio_iopoll,
 878};
 879
 880static struct kmem_cache *zonefs_inode_cachep;
 881
 882static struct inode *zonefs_alloc_inode(struct super_block *sb)
 883{
 884	struct zonefs_inode_info *zi;
 885
 886	zi = kmem_cache_alloc(zonefs_inode_cachep, GFP_KERNEL);
 887	if (!zi)
 888		return NULL;
 889
 890	inode_init_once(&zi->i_vnode);
 891	mutex_init(&zi->i_truncate_mutex);
 892	init_rwsem(&zi->i_mmap_sem);
 893
 894	return &zi->i_vnode;
 895}
 896
 897static void zonefs_free_inode(struct inode *inode)
 898{
 899	kmem_cache_free(zonefs_inode_cachep, ZONEFS_I(inode));
 900}
 901
 902/*
 903 * File system stat.
 904 */
 905static int zonefs_statfs(struct dentry *dentry, struct kstatfs *buf)
 906{
 907	struct super_block *sb = dentry->d_sb;
 908	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
 909	enum zonefs_ztype t;
 910	u64 fsid;
 911
 912	buf->f_type = ZONEFS_MAGIC;
 913	buf->f_bsize = sb->s_blocksize;
 914	buf->f_namelen = ZONEFS_NAME_MAX;
 915
 916	spin_lock(&sbi->s_lock);
 917
 918	buf->f_blocks = sbi->s_blocks;
 919	if (WARN_ON(sbi->s_used_blocks > sbi->s_blocks))
 920		buf->f_bfree = 0;
 921	else
 922		buf->f_bfree = buf->f_blocks - sbi->s_used_blocks;
 923	buf->f_bavail = buf->f_bfree;
 924
 925	for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) {
 926		if (sbi->s_nr_files[t])
 927			buf->f_files += sbi->s_nr_files[t] + 1;
 928	}
 929	buf->f_ffree = 0;
 930
 931	spin_unlock(&sbi->s_lock);
 932
 933	fsid = le64_to_cpup((void *)sbi->s_uuid.b) ^
 934		le64_to_cpup((void *)sbi->s_uuid.b + sizeof(u64));
 935	buf->f_fsid.val[0] = (u32)fsid;
 936	buf->f_fsid.val[1] = (u32)(fsid >> 32);
 937
 938	return 0;
 939}
 940
 941enum {
 942	Opt_errors_ro, Opt_errors_zro, Opt_errors_zol, Opt_errors_repair,
 943	Opt_err,
 944};
 945
 946static const match_table_t tokens = {
 947	{ Opt_errors_ro,	"errors=remount-ro"},
 948	{ Opt_errors_zro,	"errors=zone-ro"},
 949	{ Opt_errors_zol,	"errors=zone-offline"},
 950	{ Opt_errors_repair,	"errors=repair"},
 
 951	{ Opt_err,		NULL}
 952};
 953
 954static int zonefs_parse_options(struct super_block *sb, char *options)
 955{
 956	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
 957	substring_t args[MAX_OPT_ARGS];
 958	char *p;
 959
 960	if (!options)
 961		return 0;
 962
 963	while ((p = strsep(&options, ",")) != NULL) {
 964		int token;
 965
 966		if (!*p)
 967			continue;
 968
 969		token = match_token(p, tokens, args);
 970		switch (token) {
 971		case Opt_errors_ro:
 972			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
 973			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_RO;
 974			break;
 975		case Opt_errors_zro:
 976			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
 977			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZRO;
 978			break;
 979		case Opt_errors_zol:
 980			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
 981			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZOL;
 982			break;
 983		case Opt_errors_repair:
 984			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
 985			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_REPAIR;
 986			break;
 
 
 
 987		default:
 988			return -EINVAL;
 989		}
 990	}
 991
 992	return 0;
 993}
 994
 995static int zonefs_show_options(struct seq_file *seq, struct dentry *root)
 996{
 997	struct zonefs_sb_info *sbi = ZONEFS_SB(root->d_sb);
 998
 999	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO)
1000		seq_puts(seq, ",errors=remount-ro");
1001	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO)
1002		seq_puts(seq, ",errors=zone-ro");
1003	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL)
1004		seq_puts(seq, ",errors=zone-offline");
1005	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_REPAIR)
1006		seq_puts(seq, ",errors=repair");
1007
1008	return 0;
1009}
1010
1011static int zonefs_remount(struct super_block *sb, int *flags, char *data)
1012{
1013	sync_filesystem(sb);
1014
1015	return zonefs_parse_options(sb, data);
1016}
1017
1018static const struct super_operations zonefs_sops = {
1019	.alloc_inode	= zonefs_alloc_inode,
1020	.free_inode	= zonefs_free_inode,
1021	.statfs		= zonefs_statfs,
1022	.remount_fs	= zonefs_remount,
1023	.show_options	= zonefs_show_options,
1024};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1025
1026static const struct inode_operations zonefs_dir_inode_operations = {
1027	.lookup		= simple_lookup,
1028	.setattr	= zonefs_inode_setattr,
1029};
1030
1031static void zonefs_init_dir_inode(struct inode *parent, struct inode *inode,
1032				  enum zonefs_ztype type)
1033{
1034	struct super_block *sb = parent->i_sb;
 
 
 
 
 
1035
1036	inode->i_ino = blkdev_nr_zones(sb->s_bdev->bd_disk) + type + 1;
1037	inode_init_owner(inode, parent, S_IFDIR | 0555);
1038	inode->i_op = &zonefs_dir_inode_operations;
1039	inode->i_fop = &simple_dir_operations;
1040	set_nlink(inode, 2);
1041	inc_nlink(parent);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1042}
1043
1044static void zonefs_init_file_inode(struct inode *inode, struct blk_zone *zone,
1045				   enum zonefs_ztype type)
1046{
1047	struct super_block *sb = inode->i_sb;
 
1048	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1049	struct zonefs_inode_info *zi = ZONEFS_I(inode);
 
 
 
1050
1051	inode->i_ino = zone->start >> sbi->s_zone_sectors_shift;
1052	inode->i_mode = S_IFREG | sbi->s_perm;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1053
1054	zi->i_ztype = type;
1055	zi->i_zsector = zone->start;
1056	zi->i_zone_size = zone->len << SECTOR_SHIFT;
1057
1058	zi->i_max_size = min_t(loff_t, MAX_LFS_FILESIZE,
1059			       zone->capacity << SECTOR_SHIFT);
1060	zi->i_wpoffset = zonefs_check_zone_condition(inode, zone, true, true);
1061
1062	inode->i_uid = sbi->s_uid;
1063	inode->i_gid = sbi->s_gid;
1064	inode->i_size = zi->i_wpoffset;
1065	inode->i_blocks = zi->i_max_size >> SECTOR_SHIFT;
1066
1067	inode->i_op = &zonefs_file_inode_operations;
1068	inode->i_fop = &zonefs_file_operations;
1069	inode->i_mapping->a_ops = &zonefs_file_aops;
1070
1071	sb->s_maxbytes = max(zi->i_max_size, sb->s_maxbytes);
1072	sbi->s_blocks += zi->i_max_size >> sb->s_blocksize_bits;
1073	sbi->s_used_blocks += zi->i_wpoffset >> sb->s_blocksize_bits;
 
 
 
1074}
1075
1076static struct dentry *zonefs_create_inode(struct dentry *parent,
1077					const char *name, struct blk_zone *zone,
1078					enum zonefs_ztype type)
1079{
1080	struct inode *dir = d_inode(parent);
1081	struct dentry *dentry;
1082	struct inode *inode;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1083
1084	dentry = d_alloc_name(parent, name);
1085	if (!dentry)
1086		return NULL;
 
 
 
 
 
 
 
 
 
 
 
 
1087
1088	inode = new_inode(parent->d_sb);
1089	if (!inode)
1090		goto dput;
1091
1092	inode->i_ctime = inode->i_mtime = inode->i_atime = dir->i_ctime;
1093	if (zone)
1094		zonefs_init_file_inode(inode, zone, type);
1095	else
1096		zonefs_init_dir_inode(dir, inode, type);
1097	d_add(dentry, inode);
1098	dir->i_size++;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1099
1100	return dentry;
 
 
1101
1102dput:
1103	dput(dentry);
 
 
 
 
 
 
 
 
 
 
 
1104
1105	return NULL;
1106}
1107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1108struct zonefs_zone_data {
1109	struct super_block	*sb;
1110	unsigned int		nr_zones[ZONEFS_ZTYPE_MAX];
 
1111	struct blk_zone		*zones;
1112};
1113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1114/*
1115 * Create a zone group and populate it with zone files.
1116 */
1117static int zonefs_create_zgroup(struct zonefs_zone_data *zd,
1118				enum zonefs_ztype type)
 
1119{
1120	struct super_block *sb = zd->sb;
1121	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
 
1122	struct blk_zone *zone, *next, *end;
1123	const char *zgroup_name;
1124	char *file_name;
1125	struct dentry *dir;
1126	unsigned int n = 0;
1127	int ret;
1128
1129	/* If the group is empty, there is nothing to do */
1130	if (!zd->nr_zones[type])
1131		return 0;
1132
1133	file_name = kmalloc(ZONEFS_NAME_MAX, GFP_KERNEL);
1134	if (!file_name)
 
1135		return -ENOMEM;
1136
1137	if (type == ZONEFS_ZTYPE_CNV)
1138		zgroup_name = "cnv";
1139	else
1140		zgroup_name = "seq";
1141
1142	dir = zonefs_create_inode(sb->s_root, zgroup_name, NULL, type);
1143	if (!dir) {
1144		ret = -ENOMEM;
1145		goto free;
1146	}
1147
1148	/*
1149	 * The first zone contains the super block: skip it.
 
 
1150	 */
1151	end = zd->zones + blkdev_nr_zones(sb->s_bdev->bd_disk);
1152	for (zone = &zd->zones[1]; zone < end; zone = next) {
1153
1154		next = zone + 1;
1155		if (zonefs_zone_type(zone) != type)
1156			continue;
1157
 
 
 
1158		/*
1159		 * For conventional zones, contiguous zones can be aggregated
1160		 * together to form larger files. Note that this overwrites the
1161		 * length of the first zone of the set of contiguous zones
1162		 * aggregated together. If one offline or read-only zone is
1163		 * found, assume that all zones aggregated have the same
1164		 * condition.
1165		 */
1166		if (type == ZONEFS_ZTYPE_CNV &&
1167		    (sbi->s_features & ZONEFS_F_AGGRCNV)) {
1168			for (; next < end; next++) {
1169				if (zonefs_zone_type(next) != type)
1170					break;
1171				zone->len += next->len;
1172				zone->capacity += next->capacity;
1173				if (next->cond == BLK_ZONE_COND_READONLY &&
1174				    zone->cond != BLK_ZONE_COND_OFFLINE)
1175					zone->cond = BLK_ZONE_COND_READONLY;
1176				else if (next->cond == BLK_ZONE_COND_OFFLINE)
1177					zone->cond = BLK_ZONE_COND_OFFLINE;
1178			}
1179			if (zone->capacity != zone->len) {
1180				zonefs_err(sb, "Invalid conventional zone capacity\n");
1181				ret = -EINVAL;
1182				goto free;
1183			}
1184		}
1185
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1186		/*
1187		 * Use the file number within its group as file name.
 
 
 
1188		 */
1189		snprintf(file_name, ZONEFS_NAME_MAX - 1, "%u", n);
1190		if (!zonefs_create_inode(dir, file_name, zone, type)) {
1191			ret = -ENOMEM;
1192			goto free;
 
 
1193		}
1194
 
 
1195		n++;
1196	}
1197
 
 
 
1198	zonefs_info(sb, "Zone group \"%s\" has %u file%s\n",
1199		    zgroup_name, n, n > 1 ? "s" : "");
 
 
1200
1201	sbi->s_nr_files[type] = n;
1202	ret = 0;
1203
1204free:
1205	kfree(file_name);
1206
1207	return ret;
1208}
1209
1210static int zonefs_get_zone_info_cb(struct blk_zone *zone, unsigned int idx,
1211				   void *data)
1212{
1213	struct zonefs_zone_data *zd = data;
 
 
 
 
1214
1215	/*
1216	 * Count the number of usable zones: the first zone at index 0 contains
1217	 * the super block and is ignored.
1218	 */
1219	switch (zone->type) {
1220	case BLK_ZONE_TYPE_CONVENTIONAL:
1221		zone->wp = zone->start + zone->len;
1222		if (idx)
1223			zd->nr_zones[ZONEFS_ZTYPE_CNV]++;
1224		break;
1225	case BLK_ZONE_TYPE_SEQWRITE_REQ:
1226	case BLK_ZONE_TYPE_SEQWRITE_PREF:
1227		if (idx)
1228			zd->nr_zones[ZONEFS_ZTYPE_SEQ]++;
1229		break;
1230	default:
1231		zonefs_err(zd->sb, "Unsupported zone type 0x%x\n",
1232			   zone->type);
1233		return -EIO;
1234	}
1235
1236	memcpy(&zd->zones[idx], zone, sizeof(struct blk_zone));
1237
1238	return 0;
1239}
1240
1241static int zonefs_get_zone_info(struct zonefs_zone_data *zd)
 
 
 
1242{
1243	struct block_device *bdev = zd->sb->s_bdev;
 
1244	int ret;
1245
1246	zd->zones = kvcalloc(blkdev_nr_zones(bdev->bd_disk),
1247			     sizeof(struct blk_zone), GFP_KERNEL);
1248	if (!zd->zones)
1249		return -ENOMEM;
 
 
1250
1251	/* Get zones information from the device */
1252	ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES,
1253				  zonefs_get_zone_info_cb, zd);
1254	if (ret < 0) {
1255		zonefs_err(zd->sb, "Zone report failed %d\n", ret);
1256		return ret;
 
 
 
1257	}
1258
1259	if (ret != blkdev_nr_zones(bdev->bd_disk)) {
1260		zonefs_err(zd->sb, "Invalid zone report (%d/%u zones)\n",
1261			   ret, blkdev_nr_zones(bdev->bd_disk));
1262		return -EIO;
1263	}
1264
1265	return 0;
1266}
1267
1268static inline void zonefs_cleanup_zone_info(struct zonefs_zone_data *zd)
1269{
1270	kvfree(zd->zones);
1271}
1272
1273/*
1274 * Read super block information from the device.
1275 */
1276static int zonefs_read_super(struct super_block *sb)
1277{
1278	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1279	struct zonefs_super *super;
1280	u32 crc, stored_crc;
1281	struct page *page;
1282	struct bio_vec bio_vec;
1283	struct bio bio;
1284	int ret;
1285
1286	page = alloc_page(GFP_KERNEL);
1287	if (!page)
1288		return -ENOMEM;
1289
1290	bio_init(&bio, &bio_vec, 1);
1291	bio.bi_iter.bi_sector = 0;
1292	bio.bi_opf = REQ_OP_READ;
1293	bio_set_dev(&bio, sb->s_bdev);
1294	bio_add_page(&bio, page, PAGE_SIZE, 0);
1295
1296	ret = submit_bio_wait(&bio);
1297	if (ret)
1298		goto free_page;
1299
1300	super = kmap(page);
1301
1302	ret = -EINVAL;
1303	if (le32_to_cpu(super->s_magic) != ZONEFS_MAGIC)
1304		goto unmap;
1305
1306	stored_crc = le32_to_cpu(super->s_crc);
1307	super->s_crc = 0;
1308	crc = crc32(~0U, (unsigned char *)super, sizeof(struct zonefs_super));
1309	if (crc != stored_crc) {
1310		zonefs_err(sb, "Invalid checksum (Expected 0x%08x, got 0x%08x)",
1311			   crc, stored_crc);
1312		goto unmap;
1313	}
1314
1315	sbi->s_features = le64_to_cpu(super->s_features);
1316	if (sbi->s_features & ~ZONEFS_F_DEFINED_FEATURES) {
1317		zonefs_err(sb, "Unknown features set 0x%llx\n",
1318			   sbi->s_features);
1319		goto unmap;
1320	}
1321
1322	if (sbi->s_features & ZONEFS_F_UID) {
1323		sbi->s_uid = make_kuid(current_user_ns(),
1324				       le32_to_cpu(super->s_uid));
1325		if (!uid_valid(sbi->s_uid)) {
1326			zonefs_err(sb, "Invalid UID feature\n");
1327			goto unmap;
1328		}
1329	}
1330
1331	if (sbi->s_features & ZONEFS_F_GID) {
1332		sbi->s_gid = make_kgid(current_user_ns(),
1333				       le32_to_cpu(super->s_gid));
1334		if (!gid_valid(sbi->s_gid)) {
1335			zonefs_err(sb, "Invalid GID feature\n");
1336			goto unmap;
1337		}
1338	}
1339
1340	if (sbi->s_features & ZONEFS_F_PERM)
1341		sbi->s_perm = le32_to_cpu(super->s_perm);
1342
1343	if (memchr_inv(super->s_reserved, 0, sizeof(super->s_reserved))) {
1344		zonefs_err(sb, "Reserved area is being used\n");
1345		goto unmap;
1346	}
1347
1348	import_uuid(&sbi->s_uuid, super->s_uuid);
1349	ret = 0;
1350
1351unmap:
1352	kunmap(page);
1353free_page:
1354	__free_page(page);
1355
1356	return ret;
1357}
1358
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1359/*
1360 * Check that the device is zoned. If it is, get the list of zones and create
1361 * sub-directories and files according to the device zone configuration and
1362 * format options.
1363 */
1364static int zonefs_fill_super(struct super_block *sb, void *data, int silent)
1365{
1366	struct zonefs_zone_data zd;
1367	struct zonefs_sb_info *sbi;
1368	struct inode *inode;
1369	enum zonefs_ztype t;
1370	int ret;
1371
1372	if (!bdev_is_zoned(sb->s_bdev)) {
1373		zonefs_err(sb, "Not a zoned block device\n");
1374		return -EINVAL;
1375	}
1376
1377	/*
1378	 * Initialize super block information: the maximum file size is updated
1379	 * when the zone files are created so that the format option
1380	 * ZONEFS_F_AGGRCNV which increases the maximum file size of a file
1381	 * beyond the zone size is taken into account.
1382	 */
1383	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1384	if (!sbi)
1385		return -ENOMEM;
1386
1387	spin_lock_init(&sbi->s_lock);
1388	sb->s_fs_info = sbi;
1389	sb->s_magic = ZONEFS_MAGIC;
1390	sb->s_maxbytes = 0;
1391	sb->s_op = &zonefs_sops;
1392	sb->s_time_gran	= 1;
1393
1394	/*
1395	 * The block size is set to the device physical sector size to ensure
1396	 * that write operations on 512e devices (512B logical block and 4KB
1397	 * physical block) are always aligned to the device physical blocks,
1398	 * as mandated by the ZBC/ZAC specifications.
1399	 */
1400	sb_set_blocksize(sb, bdev_physical_block_size(sb->s_bdev));
1401	sbi->s_zone_sectors_shift = ilog2(bdev_zone_sectors(sb->s_bdev));
1402	sbi->s_uid = GLOBAL_ROOT_UID;
1403	sbi->s_gid = GLOBAL_ROOT_GID;
1404	sbi->s_perm = 0640;
1405	sbi->s_mount_opts = ZONEFS_MNTOPT_ERRORS_RO;
1406
 
 
 
 
 
1407	ret = zonefs_read_super(sb);
1408	if (ret)
1409		return ret;
1410
1411	ret = zonefs_parse_options(sb, data);
1412	if (ret)
1413		return ret;
1414
1415	memset(&zd, 0, sizeof(struct zonefs_zone_data));
1416	zd.sb = sb;
1417	ret = zonefs_get_zone_info(&zd);
 
 
 
 
 
 
 
 
 
1418	if (ret)
1419		goto cleanup;
1420
1421	zonefs_info(sb, "Mounting %u zones",
1422		    blkdev_nr_zones(sb->s_bdev->bd_disk));
1423
1424	/* Create root directory inode */
1425	ret = -ENOMEM;
1426	inode = new_inode(sb);
1427	if (!inode)
1428		goto cleanup;
1429
1430	inode->i_ino = blkdev_nr_zones(sb->s_bdev->bd_disk);
1431	inode->i_mode = S_IFDIR | 0555;
1432	inode->i_ctime = inode->i_mtime = inode->i_atime = current_time(inode);
1433	inode->i_op = &zonefs_dir_inode_operations;
1434	inode->i_fop = &simple_dir_operations;
 
1435	set_nlink(inode, 2);
 
 
 
 
 
 
1436
1437	sb->s_root = d_make_root(inode);
1438	if (!sb->s_root)
1439		goto cleanup;
1440
1441	/* Create and populate files in zone groups directories */
1442	for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) {
1443		ret = zonefs_create_zgroup(&zd, t);
1444		if (ret)
1445			break;
1446	}
 
 
 
 
 
 
 
1447
1448cleanup:
1449	zonefs_cleanup_zone_info(&zd);
 
1450
1451	return ret;
1452}
1453
1454static struct dentry *zonefs_mount(struct file_system_type *fs_type,
1455				   int flags, const char *dev_name, void *data)
1456{
1457	return mount_bdev(fs_type, flags, dev_name, data, zonefs_fill_super);
1458}
1459
1460static void zonefs_kill_super(struct super_block *sb)
1461{
1462	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1463
1464	if (sb->s_root)
1465		d_genocide(sb->s_root);
 
1466	kill_block_super(sb);
 
 
 
1467	kfree(sbi);
1468}
1469
1470/*
1471 * File system definition and registration.
1472 */
1473static struct file_system_type zonefs_type = {
1474	.owner		= THIS_MODULE,
1475	.name		= "zonefs",
1476	.mount		= zonefs_mount,
1477	.kill_sb	= zonefs_kill_super,
1478	.fs_flags	= FS_REQUIRES_DEV,
1479};
1480
1481static int __init zonefs_init_inodecache(void)
1482{
1483	zonefs_inode_cachep = kmem_cache_create("zonefs_inode_cache",
1484			sizeof(struct zonefs_inode_info), 0,
1485			(SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT),
1486			NULL);
1487	if (zonefs_inode_cachep == NULL)
1488		return -ENOMEM;
1489	return 0;
1490}
1491
1492static void zonefs_destroy_inodecache(void)
1493{
1494	/*
1495	 * Make sure all delayed rcu free inodes are flushed before we
1496	 * destroy the inode cache.
1497	 */
1498	rcu_barrier();
1499	kmem_cache_destroy(zonefs_inode_cachep);
1500}
1501
1502static int __init zonefs_init(void)
1503{
1504	int ret;
1505
1506	BUILD_BUG_ON(sizeof(struct zonefs_super) != ZONEFS_SUPER_SIZE);
1507
1508	ret = zonefs_init_inodecache();
1509	if (ret)
1510		return ret;
1511
 
 
 
 
1512	ret = register_filesystem(&zonefs_type);
1513	if (ret) {
1514		zonefs_destroy_inodecache();
1515		return ret;
1516	}
1517
1518	return 0;
 
 
 
 
 
 
 
1519}
1520
1521static void __exit zonefs_exit(void)
1522{
 
 
1523	zonefs_destroy_inodecache();
1524	unregister_filesystem(&zonefs_type);
1525}
1526
1527MODULE_AUTHOR("Damien Le Moal");
1528MODULE_DESCRIPTION("Zone file system for zoned block devices");
1529MODULE_LICENSE("GPL");
 
1530module_init(zonefs_init);
1531module_exit(zonefs_exit);