xref: /openbmc/linux/fs/zonefs/super.c (revision 4652ae7a)
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  * Manage the active zone count. Called with zi->i_truncate_mutex held.
32  */
33 static void zonefs_account_active(struct inode *inode)
34 {
35 	struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
36 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
37 
38 	lockdep_assert_held(&zi->i_truncate_mutex);
39 
40 	if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
41 		return;
42 
43 	/*
44 	 * For zones that transitioned to the offline or readonly condition,
45 	 * we only need to clear the active state.
46 	 */
47 	if (zi->i_flags & (ZONEFS_ZONE_OFFLINE | ZONEFS_ZONE_READONLY))
48 		goto out;
49 
50 	/*
51 	 * If the zone is active, that is, if it is explicitly open or
52 	 * partially written, check if it was already accounted as active.
53 	 */
54 	if ((zi->i_flags & ZONEFS_ZONE_OPEN) ||
55 	    (zi->i_wpoffset > 0 && zi->i_wpoffset < zi->i_max_size)) {
56 		if (!(zi->i_flags & ZONEFS_ZONE_ACTIVE)) {
57 			zi->i_flags |= ZONEFS_ZONE_ACTIVE;
58 			atomic_inc(&sbi->s_active_seq_files);
59 		}
60 		return;
61 	}
62 
63 out:
64 	/* The zone is not active. If it was, update the active count */
65 	if (zi->i_flags & ZONEFS_ZONE_ACTIVE) {
66 		zi->i_flags &= ~ZONEFS_ZONE_ACTIVE;
67 		atomic_dec(&sbi->s_active_seq_files);
68 	}
69 }
70 
71 static inline int zonefs_zone_mgmt(struct inode *inode, enum req_op op)
72 {
73 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
74 	int ret;
75 
76 	lockdep_assert_held(&zi->i_truncate_mutex);
77 
78 	/*
79 	 * With ZNS drives, closing an explicitly open zone that has not been
80 	 * written will change the zone state to "closed", that is, the zone
81 	 * will remain active. Since this can then cause failure of explicit
82 	 * open operation on other zones if the drive active zone resources
83 	 * are exceeded, make sure that the zone does not remain active by
84 	 * resetting it.
85 	 */
86 	if (op == REQ_OP_ZONE_CLOSE && !zi->i_wpoffset)
87 		op = REQ_OP_ZONE_RESET;
88 
89 	trace_zonefs_zone_mgmt(inode, op);
90 	ret = blkdev_zone_mgmt(inode->i_sb->s_bdev, op, zi->i_zsector,
91 			       zi->i_zone_size >> SECTOR_SHIFT, GFP_NOFS);
92 	if (ret) {
93 		zonefs_err(inode->i_sb,
94 			   "Zone management operation %s at %llu failed %d\n",
95 			   blk_op_str(op), zi->i_zsector, ret);
96 		return ret;
97 	}
98 
99 	return 0;
100 }
101 
102 static inline void zonefs_i_size_write(struct inode *inode, loff_t isize)
103 {
104 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
105 
106 	i_size_write(inode, isize);
107 	/*
108 	 * A full zone is no longer open/active and does not need
109 	 * explicit closing.
110 	 */
111 	if (isize >= zi->i_max_size) {
112 		struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
113 
114 		if (zi->i_flags & ZONEFS_ZONE_ACTIVE)
115 			atomic_dec(&sbi->s_active_seq_files);
116 		zi->i_flags &= ~(ZONEFS_ZONE_OPEN | ZONEFS_ZONE_ACTIVE);
117 	}
118 }
119 
120 static int zonefs_read_iomap_begin(struct inode *inode, loff_t offset,
121 				   loff_t length, unsigned int flags,
122 				   struct iomap *iomap, struct iomap *srcmap)
123 {
124 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
125 	struct super_block *sb = inode->i_sb;
126 	loff_t isize;
127 
128 	/*
129 	 * All blocks are always mapped below EOF. If reading past EOF,
130 	 * act as if there is a hole up to the file maximum size.
131 	 */
132 	mutex_lock(&zi->i_truncate_mutex);
133 	iomap->bdev = inode->i_sb->s_bdev;
134 	iomap->offset = ALIGN_DOWN(offset, sb->s_blocksize);
135 	isize = i_size_read(inode);
136 	if (iomap->offset >= isize) {
137 		iomap->type = IOMAP_HOLE;
138 		iomap->addr = IOMAP_NULL_ADDR;
139 		iomap->length = length;
140 	} else {
141 		iomap->type = IOMAP_MAPPED;
142 		iomap->addr = (zi->i_zsector << SECTOR_SHIFT) + iomap->offset;
143 		iomap->length = isize - iomap->offset;
144 	}
145 	mutex_unlock(&zi->i_truncate_mutex);
146 
147 	trace_zonefs_iomap_begin(inode, iomap);
148 
149 	return 0;
150 }
151 
152 static const struct iomap_ops zonefs_read_iomap_ops = {
153 	.iomap_begin	= zonefs_read_iomap_begin,
154 };
155 
156 static int zonefs_write_iomap_begin(struct inode *inode, loff_t offset,
157 				    loff_t length, unsigned int flags,
158 				    struct iomap *iomap, struct iomap *srcmap)
159 {
160 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
161 	struct super_block *sb = inode->i_sb;
162 	loff_t isize;
163 
164 	/* All write I/Os should always be within the file maximum size */
165 	if (WARN_ON_ONCE(offset + length > zi->i_max_size))
166 		return -EIO;
167 
168 	/*
169 	 * Sequential zones can only accept direct writes. This is already
170 	 * checked when writes are issued, so warn if we see a page writeback
171 	 * operation.
172 	 */
173 	if (WARN_ON_ONCE(zi->i_ztype == ZONEFS_ZTYPE_SEQ &&
174 			 !(flags & IOMAP_DIRECT)))
175 		return -EIO;
176 
177 	/*
178 	 * For conventional zones, all blocks are always mapped. For sequential
179 	 * zones, all blocks after always mapped below the inode size (zone
180 	 * write pointer) and unwriten beyond.
181 	 */
182 	mutex_lock(&zi->i_truncate_mutex);
183 	iomap->bdev = inode->i_sb->s_bdev;
184 	iomap->offset = ALIGN_DOWN(offset, sb->s_blocksize);
185 	iomap->addr = (zi->i_zsector << SECTOR_SHIFT) + iomap->offset;
186 	isize = i_size_read(inode);
187 	if (iomap->offset >= isize) {
188 		iomap->type = IOMAP_UNWRITTEN;
189 		iomap->length = zi->i_max_size - iomap->offset;
190 	} else {
191 		iomap->type = IOMAP_MAPPED;
192 		iomap->length = isize - iomap->offset;
193 	}
194 	mutex_unlock(&zi->i_truncate_mutex);
195 
196 	trace_zonefs_iomap_begin(inode, iomap);
197 
198 	return 0;
199 }
200 
201 static const struct iomap_ops zonefs_write_iomap_ops = {
202 	.iomap_begin	= zonefs_write_iomap_begin,
203 };
204 
205 static int zonefs_read_folio(struct file *unused, struct folio *folio)
206 {
207 	return iomap_read_folio(folio, &zonefs_read_iomap_ops);
208 }
209 
210 static void zonefs_readahead(struct readahead_control *rac)
211 {
212 	iomap_readahead(rac, &zonefs_read_iomap_ops);
213 }
214 
215 /*
216  * Map blocks for page writeback. This is used only on conventional zone files,
217  * which implies that the page range can only be within the fixed inode size.
218  */
219 static int zonefs_write_map_blocks(struct iomap_writepage_ctx *wpc,
220 				   struct inode *inode, loff_t offset)
221 {
222 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
223 
224 	if (WARN_ON_ONCE(zi->i_ztype != ZONEFS_ZTYPE_CNV))
225 		return -EIO;
226 	if (WARN_ON_ONCE(offset >= i_size_read(inode)))
227 		return -EIO;
228 
229 	/* If the mapping is already OK, nothing needs to be done */
230 	if (offset >= wpc->iomap.offset &&
231 	    offset < wpc->iomap.offset + wpc->iomap.length)
232 		return 0;
233 
234 	return zonefs_write_iomap_begin(inode, offset, zi->i_max_size - offset,
235 					IOMAP_WRITE, &wpc->iomap, NULL);
236 }
237 
238 static const struct iomap_writeback_ops zonefs_writeback_ops = {
239 	.map_blocks		= zonefs_write_map_blocks,
240 };
241 
242 static int zonefs_writepages(struct address_space *mapping,
243 			     struct writeback_control *wbc)
244 {
245 	struct iomap_writepage_ctx wpc = { };
246 
247 	return iomap_writepages(mapping, wbc, &wpc, &zonefs_writeback_ops);
248 }
249 
250 static int zonefs_swap_activate(struct swap_info_struct *sis,
251 				struct file *swap_file, sector_t *span)
252 {
253 	struct inode *inode = file_inode(swap_file);
254 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
255 
256 	if (zi->i_ztype != ZONEFS_ZTYPE_CNV) {
257 		zonefs_err(inode->i_sb,
258 			   "swap file: not a conventional zone file\n");
259 		return -EINVAL;
260 	}
261 
262 	return iomap_swapfile_activate(sis, swap_file, span,
263 				       &zonefs_read_iomap_ops);
264 }
265 
266 static const struct address_space_operations zonefs_file_aops = {
267 	.read_folio		= zonefs_read_folio,
268 	.readahead		= zonefs_readahead,
269 	.writepages		= zonefs_writepages,
270 	.dirty_folio		= filemap_dirty_folio,
271 	.release_folio		= iomap_release_folio,
272 	.invalidate_folio	= iomap_invalidate_folio,
273 	.migrate_folio		= filemap_migrate_folio,
274 	.is_partially_uptodate	= iomap_is_partially_uptodate,
275 	.error_remove_page	= generic_error_remove_page,
276 	.direct_IO		= noop_direct_IO,
277 	.swap_activate		= zonefs_swap_activate,
278 };
279 
280 static void zonefs_update_stats(struct inode *inode, loff_t new_isize)
281 {
282 	struct super_block *sb = inode->i_sb;
283 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
284 	loff_t old_isize = i_size_read(inode);
285 	loff_t nr_blocks;
286 
287 	if (new_isize == old_isize)
288 		return;
289 
290 	spin_lock(&sbi->s_lock);
291 
292 	/*
293 	 * This may be called for an update after an IO error.
294 	 * So beware of the values seen.
295 	 */
296 	if (new_isize < old_isize) {
297 		nr_blocks = (old_isize - new_isize) >> sb->s_blocksize_bits;
298 		if (sbi->s_used_blocks > nr_blocks)
299 			sbi->s_used_blocks -= nr_blocks;
300 		else
301 			sbi->s_used_blocks = 0;
302 	} else {
303 		sbi->s_used_blocks +=
304 			(new_isize - old_isize) >> sb->s_blocksize_bits;
305 		if (sbi->s_used_blocks > sbi->s_blocks)
306 			sbi->s_used_blocks = sbi->s_blocks;
307 	}
308 
309 	spin_unlock(&sbi->s_lock);
310 }
311 
312 /*
313  * Check a zone condition and adjust its file inode access permissions for
314  * offline and readonly zones. Return the inode size corresponding to the
315  * amount of readable data in the zone.
316  */
317 static loff_t zonefs_check_zone_condition(struct inode *inode,
318 					  struct blk_zone *zone, bool warn,
319 					  bool mount)
320 {
321 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
322 
323 	switch (zone->cond) {
324 	case BLK_ZONE_COND_OFFLINE:
325 		/*
326 		 * Dead zone: make the inode immutable, disable all accesses
327 		 * and set the file size to 0 (zone wp set to zone start).
328 		 */
329 		if (warn)
330 			zonefs_warn(inode->i_sb, "inode %lu: offline zone\n",
331 				    inode->i_ino);
332 		inode->i_flags |= S_IMMUTABLE;
333 		inode->i_mode &= ~0777;
334 		zone->wp = zone->start;
335 		zi->i_flags |= ZONEFS_ZONE_OFFLINE;
336 		return 0;
337 	case BLK_ZONE_COND_READONLY:
338 		/*
339 		 * The write pointer of read-only zones is invalid. If such a
340 		 * zone is found during mount, the file size cannot be retrieved
341 		 * so we treat the zone as offline (mount == true case).
342 		 * Otherwise, keep the file size as it was when last updated
343 		 * so that the user can recover data. In both cases, writes are
344 		 * always disabled for the zone.
345 		 */
346 		if (warn)
347 			zonefs_warn(inode->i_sb, "inode %lu: read-only zone\n",
348 				    inode->i_ino);
349 		inode->i_flags |= S_IMMUTABLE;
350 		if (mount) {
351 			zone->cond = BLK_ZONE_COND_OFFLINE;
352 			inode->i_mode &= ~0777;
353 			zone->wp = zone->start;
354 			zi->i_flags |= ZONEFS_ZONE_OFFLINE;
355 			return 0;
356 		}
357 		zi->i_flags |= ZONEFS_ZONE_READONLY;
358 		inode->i_mode &= ~0222;
359 		return i_size_read(inode);
360 	case BLK_ZONE_COND_FULL:
361 		/* The write pointer of full zones is invalid. */
362 		return zi->i_max_size;
363 	default:
364 		if (zi->i_ztype == ZONEFS_ZTYPE_CNV)
365 			return zi->i_max_size;
366 		return (zone->wp - zone->start) << SECTOR_SHIFT;
367 	}
368 }
369 
370 struct zonefs_ioerr_data {
371 	struct inode	*inode;
372 	bool		write;
373 };
374 
375 static int zonefs_io_error_cb(struct blk_zone *zone, unsigned int idx,
376 			      void *data)
377 {
378 	struct zonefs_ioerr_data *err = data;
379 	struct inode *inode = err->inode;
380 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
381 	struct super_block *sb = inode->i_sb;
382 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
383 	loff_t isize, data_size;
384 
385 	/*
386 	 * Check the zone condition: if the zone is not "bad" (offline or
387 	 * read-only), read errors are simply signaled to the IO issuer as long
388 	 * as there is no inconsistency between the inode size and the amount of
389 	 * data writen in the zone (data_size).
390 	 */
391 	data_size = zonefs_check_zone_condition(inode, zone, true, false);
392 	isize = i_size_read(inode);
393 	if (zone->cond != BLK_ZONE_COND_OFFLINE &&
394 	    zone->cond != BLK_ZONE_COND_READONLY &&
395 	    !err->write && isize == data_size)
396 		return 0;
397 
398 	/*
399 	 * At this point, we detected either a bad zone or an inconsistency
400 	 * between the inode size and the amount of data written in the zone.
401 	 * For the latter case, the cause may be a write IO error or an external
402 	 * action on the device. Two error patterns exist:
403 	 * 1) The inode size is lower than the amount of data in the zone:
404 	 *    a write operation partially failed and data was writen at the end
405 	 *    of the file. This can happen in the case of a large direct IO
406 	 *    needing several BIOs and/or write requests to be processed.
407 	 * 2) The inode size is larger than the amount of data in the zone:
408 	 *    this can happen with a deferred write error with the use of the
409 	 *    device side write cache after getting successful write IO
410 	 *    completions. Other possibilities are (a) an external corruption,
411 	 *    e.g. an application reset the zone directly, or (b) the device
412 	 *    has a serious problem (e.g. firmware bug).
413 	 *
414 	 * In all cases, warn about inode size inconsistency and handle the
415 	 * IO error according to the zone condition and to the mount options.
416 	 */
417 	if (zi->i_ztype == ZONEFS_ZTYPE_SEQ && isize != data_size)
418 		zonefs_warn(sb, "inode %lu: invalid size %lld (should be %lld)\n",
419 			    inode->i_ino, isize, data_size);
420 
421 	/*
422 	 * First handle bad zones signaled by hardware. The mount options
423 	 * errors=zone-ro and errors=zone-offline result in changing the
424 	 * zone condition to read-only and offline respectively, as if the
425 	 * condition was signaled by the hardware.
426 	 */
427 	if (zone->cond == BLK_ZONE_COND_OFFLINE ||
428 	    sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL) {
429 		zonefs_warn(sb, "inode %lu: read/write access disabled\n",
430 			    inode->i_ino);
431 		if (zone->cond != BLK_ZONE_COND_OFFLINE) {
432 			zone->cond = BLK_ZONE_COND_OFFLINE;
433 			data_size = zonefs_check_zone_condition(inode, zone,
434 								false, false);
435 		}
436 	} else if (zone->cond == BLK_ZONE_COND_READONLY ||
437 		   sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO) {
438 		zonefs_warn(sb, "inode %lu: write access disabled\n",
439 			    inode->i_ino);
440 		if (zone->cond != BLK_ZONE_COND_READONLY) {
441 			zone->cond = BLK_ZONE_COND_READONLY;
442 			data_size = zonefs_check_zone_condition(inode, zone,
443 								false, false);
444 		}
445 	} else if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO &&
446 		   data_size > isize) {
447 		/* Do not expose garbage data */
448 		data_size = isize;
449 	}
450 
451 	/*
452 	 * If the filesystem is mounted with the explicit-open mount option, we
453 	 * need to clear the ZONEFS_ZONE_OPEN flag if the zone transitioned to
454 	 * the read-only or offline condition, to avoid attempting an explicit
455 	 * close of the zone when the inode file is closed.
456 	 */
457 	if ((sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) &&
458 	    (zone->cond == BLK_ZONE_COND_OFFLINE ||
459 	     zone->cond == BLK_ZONE_COND_READONLY))
460 		zi->i_flags &= ~ZONEFS_ZONE_OPEN;
461 
462 	/*
463 	 * If error=remount-ro was specified, any error result in remounting
464 	 * the volume as read-only.
465 	 */
466 	if ((sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO) && !sb_rdonly(sb)) {
467 		zonefs_warn(sb, "remounting filesystem read-only\n");
468 		sb->s_flags |= SB_RDONLY;
469 	}
470 
471 	/*
472 	 * Update block usage stats and the inode size  to prevent access to
473 	 * invalid data.
474 	 */
475 	zonefs_update_stats(inode, data_size);
476 	zonefs_i_size_write(inode, data_size);
477 	zi->i_wpoffset = data_size;
478 	zonefs_account_active(inode);
479 
480 	return 0;
481 }
482 
483 /*
484  * When an file IO error occurs, check the file zone to see if there is a change
485  * in the zone condition (e.g. offline or read-only). For a failed write to a
486  * sequential zone, the zone write pointer position must also be checked to
487  * eventually correct the file size and zonefs inode write pointer offset
488  * (which can be out of sync with the drive due to partial write failures).
489  */
490 static void __zonefs_io_error(struct inode *inode, bool write)
491 {
492 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
493 	struct super_block *sb = inode->i_sb;
494 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
495 	unsigned int noio_flag;
496 	unsigned int nr_zones = 1;
497 	struct zonefs_ioerr_data err = {
498 		.inode = inode,
499 		.write = write,
500 	};
501 	int ret;
502 
503 	/*
504 	 * The only files that have more than one zone are conventional zone
505 	 * files with aggregated conventional zones, for which the inode zone
506 	 * size is always larger than the device zone size.
507 	 */
508 	if (zi->i_zone_size > bdev_zone_sectors(sb->s_bdev))
509 		nr_zones = zi->i_zone_size >>
510 			(sbi->s_zone_sectors_shift + SECTOR_SHIFT);
511 
512 	/*
513 	 * Memory allocations in blkdev_report_zones() can trigger a memory
514 	 * reclaim which may in turn cause a recursion into zonefs as well as
515 	 * struct request allocations for the same device. The former case may
516 	 * end up in a deadlock on the inode truncate mutex, while the latter
517 	 * may prevent IO forward progress. Executing the report zones under
518 	 * the GFP_NOIO context avoids both problems.
519 	 */
520 	noio_flag = memalloc_noio_save();
521 	ret = blkdev_report_zones(sb->s_bdev, zi->i_zsector, nr_zones,
522 				  zonefs_io_error_cb, &err);
523 	if (ret != nr_zones)
524 		zonefs_err(sb, "Get inode %lu zone information failed %d\n",
525 			   inode->i_ino, ret);
526 	memalloc_noio_restore(noio_flag);
527 }
528 
529 static void zonefs_io_error(struct inode *inode, bool write)
530 {
531 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
532 
533 	mutex_lock(&zi->i_truncate_mutex);
534 	__zonefs_io_error(inode, write);
535 	mutex_unlock(&zi->i_truncate_mutex);
536 }
537 
538 static int zonefs_file_truncate(struct inode *inode, loff_t isize)
539 {
540 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
541 	loff_t old_isize;
542 	enum req_op op;
543 	int ret = 0;
544 
545 	/*
546 	 * Only sequential zone files can be truncated and truncation is allowed
547 	 * only down to a 0 size, which is equivalent to a zone reset, and to
548 	 * the maximum file size, which is equivalent to a zone finish.
549 	 */
550 	if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
551 		return -EPERM;
552 
553 	if (!isize)
554 		op = REQ_OP_ZONE_RESET;
555 	else if (isize == zi->i_max_size)
556 		op = REQ_OP_ZONE_FINISH;
557 	else
558 		return -EPERM;
559 
560 	inode_dio_wait(inode);
561 
562 	/* Serialize against page faults */
563 	filemap_invalidate_lock(inode->i_mapping);
564 
565 	/* Serialize against zonefs_iomap_begin() */
566 	mutex_lock(&zi->i_truncate_mutex);
567 
568 	old_isize = i_size_read(inode);
569 	if (isize == old_isize)
570 		goto unlock;
571 
572 	ret = zonefs_zone_mgmt(inode, op);
573 	if (ret)
574 		goto unlock;
575 
576 	/*
577 	 * If the mount option ZONEFS_MNTOPT_EXPLICIT_OPEN is set,
578 	 * take care of open zones.
579 	 */
580 	if (zi->i_flags & ZONEFS_ZONE_OPEN) {
581 		/*
582 		 * Truncating a zone to EMPTY or FULL is the equivalent of
583 		 * closing the zone. For a truncation to 0, we need to
584 		 * re-open the zone to ensure new writes can be processed.
585 		 * For a truncation to the maximum file size, the zone is
586 		 * closed and writes cannot be accepted anymore, so clear
587 		 * the open flag.
588 		 */
589 		if (!isize)
590 			ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_OPEN);
591 		else
592 			zi->i_flags &= ~ZONEFS_ZONE_OPEN;
593 	}
594 
595 	zonefs_update_stats(inode, isize);
596 	truncate_setsize(inode, isize);
597 	zi->i_wpoffset = isize;
598 	zonefs_account_active(inode);
599 
600 unlock:
601 	mutex_unlock(&zi->i_truncate_mutex);
602 	filemap_invalidate_unlock(inode->i_mapping);
603 
604 	return ret;
605 }
606 
607 static int zonefs_inode_setattr(struct user_namespace *mnt_userns,
608 				struct dentry *dentry, struct iattr *iattr)
609 {
610 	struct inode *inode = d_inode(dentry);
611 	int ret;
612 
613 	if (unlikely(IS_IMMUTABLE(inode)))
614 		return -EPERM;
615 
616 	ret = setattr_prepare(&init_user_ns, dentry, iattr);
617 	if (ret)
618 		return ret;
619 
620 	/*
621 	 * Since files and directories cannot be created nor deleted, do not
622 	 * allow setting any write attributes on the sub-directories grouping
623 	 * files by zone type.
624 	 */
625 	if ((iattr->ia_valid & ATTR_MODE) && S_ISDIR(inode->i_mode) &&
626 	    (iattr->ia_mode & 0222))
627 		return -EPERM;
628 
629 	if (((iattr->ia_valid & ATTR_UID) &&
630 	     !uid_eq(iattr->ia_uid, inode->i_uid)) ||
631 	    ((iattr->ia_valid & ATTR_GID) &&
632 	     !gid_eq(iattr->ia_gid, inode->i_gid))) {
633 		ret = dquot_transfer(mnt_userns, inode, iattr);
634 		if (ret)
635 			return ret;
636 	}
637 
638 	if (iattr->ia_valid & ATTR_SIZE) {
639 		ret = zonefs_file_truncate(inode, iattr->ia_size);
640 		if (ret)
641 			return ret;
642 	}
643 
644 	setattr_copy(&init_user_ns, inode, iattr);
645 
646 	return 0;
647 }
648 
649 static const struct inode_operations zonefs_file_inode_operations = {
650 	.setattr	= zonefs_inode_setattr,
651 };
652 
653 static int zonefs_file_fsync(struct file *file, loff_t start, loff_t end,
654 			     int datasync)
655 {
656 	struct inode *inode = file_inode(file);
657 	int ret = 0;
658 
659 	if (unlikely(IS_IMMUTABLE(inode)))
660 		return -EPERM;
661 
662 	/*
663 	 * Since only direct writes are allowed in sequential files, page cache
664 	 * flush is needed only for conventional zone files.
665 	 */
666 	if (ZONEFS_I(inode)->i_ztype == ZONEFS_ZTYPE_CNV)
667 		ret = file_write_and_wait_range(file, start, end);
668 	if (!ret)
669 		ret = blkdev_issue_flush(inode->i_sb->s_bdev);
670 
671 	if (ret)
672 		zonefs_io_error(inode, true);
673 
674 	return ret;
675 }
676 
677 static vm_fault_t zonefs_filemap_page_mkwrite(struct vm_fault *vmf)
678 {
679 	struct inode *inode = file_inode(vmf->vma->vm_file);
680 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
681 	vm_fault_t ret;
682 
683 	if (unlikely(IS_IMMUTABLE(inode)))
684 		return VM_FAULT_SIGBUS;
685 
686 	/*
687 	 * Sanity check: only conventional zone files can have shared
688 	 * writeable mappings.
689 	 */
690 	if (WARN_ON_ONCE(zi->i_ztype != ZONEFS_ZTYPE_CNV))
691 		return VM_FAULT_NOPAGE;
692 
693 	sb_start_pagefault(inode->i_sb);
694 	file_update_time(vmf->vma->vm_file);
695 
696 	/* Serialize against truncates */
697 	filemap_invalidate_lock_shared(inode->i_mapping);
698 	ret = iomap_page_mkwrite(vmf, &zonefs_write_iomap_ops);
699 	filemap_invalidate_unlock_shared(inode->i_mapping);
700 
701 	sb_end_pagefault(inode->i_sb);
702 	return ret;
703 }
704 
705 static const struct vm_operations_struct zonefs_file_vm_ops = {
706 	.fault		= filemap_fault,
707 	.map_pages	= filemap_map_pages,
708 	.page_mkwrite	= zonefs_filemap_page_mkwrite,
709 };
710 
711 static int zonefs_file_mmap(struct file *file, struct vm_area_struct *vma)
712 {
713 	/*
714 	 * Conventional zones accept random writes, so their files can support
715 	 * shared writable mappings. For sequential zone files, only read
716 	 * mappings are possible since there are no guarantees for write
717 	 * ordering between msync() and page cache writeback.
718 	 */
719 	if (ZONEFS_I(file_inode(file))->i_ztype == ZONEFS_ZTYPE_SEQ &&
720 	    (vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
721 		return -EINVAL;
722 
723 	file_accessed(file);
724 	vma->vm_ops = &zonefs_file_vm_ops;
725 
726 	return 0;
727 }
728 
729 static loff_t zonefs_file_llseek(struct file *file, loff_t offset, int whence)
730 {
731 	loff_t isize = i_size_read(file_inode(file));
732 
733 	/*
734 	 * Seeks are limited to below the zone size for conventional zones
735 	 * and below the zone write pointer for sequential zones. In both
736 	 * cases, this limit is the inode size.
737 	 */
738 	return generic_file_llseek_size(file, offset, whence, isize, isize);
739 }
740 
741 static int zonefs_file_write_dio_end_io(struct kiocb *iocb, ssize_t size,
742 					int error, unsigned int flags)
743 {
744 	struct inode *inode = file_inode(iocb->ki_filp);
745 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
746 
747 	if (error) {
748 		zonefs_io_error(inode, true);
749 		return error;
750 	}
751 
752 	if (size && zi->i_ztype != ZONEFS_ZTYPE_CNV) {
753 		/*
754 		 * Note that we may be seeing completions out of order,
755 		 * but that is not a problem since a write completed
756 		 * successfully necessarily means that all preceding writes
757 		 * were also successful. So we can safely increase the inode
758 		 * size to the write end location.
759 		 */
760 		mutex_lock(&zi->i_truncate_mutex);
761 		if (i_size_read(inode) < iocb->ki_pos + size) {
762 			zonefs_update_stats(inode, iocb->ki_pos + size);
763 			zonefs_i_size_write(inode, iocb->ki_pos + size);
764 		}
765 		mutex_unlock(&zi->i_truncate_mutex);
766 	}
767 
768 	return 0;
769 }
770 
771 static const struct iomap_dio_ops zonefs_write_dio_ops = {
772 	.end_io			= zonefs_file_write_dio_end_io,
773 };
774 
775 static ssize_t zonefs_file_dio_append(struct kiocb *iocb, struct iov_iter *from)
776 {
777 	struct inode *inode = file_inode(iocb->ki_filp);
778 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
779 	struct block_device *bdev = inode->i_sb->s_bdev;
780 	unsigned int max = bdev_max_zone_append_sectors(bdev);
781 	struct bio *bio;
782 	ssize_t size;
783 	int nr_pages;
784 	ssize_t ret;
785 
786 	max = ALIGN_DOWN(max << SECTOR_SHIFT, inode->i_sb->s_blocksize);
787 	iov_iter_truncate(from, max);
788 
789 	nr_pages = iov_iter_npages(from, BIO_MAX_VECS);
790 	if (!nr_pages)
791 		return 0;
792 
793 	bio = bio_alloc(bdev, nr_pages,
794 			REQ_OP_ZONE_APPEND | REQ_SYNC | REQ_IDLE, GFP_NOFS);
795 	bio->bi_iter.bi_sector = zi->i_zsector;
796 	bio->bi_ioprio = iocb->ki_ioprio;
797 	if (iocb_is_dsync(iocb))
798 		bio->bi_opf |= REQ_FUA;
799 
800 	ret = bio_iov_iter_get_pages(bio, from);
801 	if (unlikely(ret))
802 		goto out_release;
803 
804 	size = bio->bi_iter.bi_size;
805 	task_io_account_write(size);
806 
807 	if (iocb->ki_flags & IOCB_HIPRI)
808 		bio_set_polled(bio, iocb);
809 
810 	ret = submit_bio_wait(bio);
811 
812 	/*
813 	 * If the file zone was written underneath the file system, the zone
814 	 * write pointer may not be where we expect it to be, but the zone
815 	 * append write can still succeed. So check manually that we wrote where
816 	 * we intended to, that is, at zi->i_wpoffset.
817 	 */
818 	if (!ret) {
819 		sector_t wpsector =
820 			zi->i_zsector + (zi->i_wpoffset >> SECTOR_SHIFT);
821 
822 		if (bio->bi_iter.bi_sector != wpsector) {
823 			zonefs_warn(inode->i_sb,
824 				"Corrupted write pointer %llu for zone at %llu\n",
825 				wpsector, zi->i_zsector);
826 			ret = -EIO;
827 		}
828 	}
829 
830 	zonefs_file_write_dio_end_io(iocb, size, ret, 0);
831 	trace_zonefs_file_dio_append(inode, size, ret);
832 
833 out_release:
834 	bio_release_pages(bio, false);
835 	bio_put(bio);
836 
837 	if (ret >= 0) {
838 		iocb->ki_pos += size;
839 		return size;
840 	}
841 
842 	return ret;
843 }
844 
845 /*
846  * Do not exceed the LFS limits nor the file zone size. If pos is under the
847  * limit it becomes a short access. If it exceeds the limit, return -EFBIG.
848  */
849 static loff_t zonefs_write_check_limits(struct file *file, loff_t pos,
850 					loff_t count)
851 {
852 	struct inode *inode = file_inode(file);
853 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
854 	loff_t limit = rlimit(RLIMIT_FSIZE);
855 	loff_t max_size = zi->i_max_size;
856 
857 	if (limit != RLIM_INFINITY) {
858 		if (pos >= limit) {
859 			send_sig(SIGXFSZ, current, 0);
860 			return -EFBIG;
861 		}
862 		count = min(count, limit - pos);
863 	}
864 
865 	if (!(file->f_flags & O_LARGEFILE))
866 		max_size = min_t(loff_t, MAX_NON_LFS, max_size);
867 
868 	if (unlikely(pos >= max_size))
869 		return -EFBIG;
870 
871 	return min(count, max_size - pos);
872 }
873 
874 static ssize_t zonefs_write_checks(struct kiocb *iocb, struct iov_iter *from)
875 {
876 	struct file *file = iocb->ki_filp;
877 	struct inode *inode = file_inode(file);
878 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
879 	loff_t count;
880 
881 	if (IS_SWAPFILE(inode))
882 		return -ETXTBSY;
883 
884 	if (!iov_iter_count(from))
885 		return 0;
886 
887 	if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
888 		return -EINVAL;
889 
890 	if (iocb->ki_flags & IOCB_APPEND) {
891 		if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
892 			return -EINVAL;
893 		mutex_lock(&zi->i_truncate_mutex);
894 		iocb->ki_pos = zi->i_wpoffset;
895 		mutex_unlock(&zi->i_truncate_mutex);
896 	}
897 
898 	count = zonefs_write_check_limits(file, iocb->ki_pos,
899 					  iov_iter_count(from));
900 	if (count < 0)
901 		return count;
902 
903 	iov_iter_truncate(from, count);
904 	return iov_iter_count(from);
905 }
906 
907 /*
908  * Handle direct writes. For sequential zone files, this is the only possible
909  * write path. For these files, check that the user is issuing writes
910  * sequentially from the end of the file. This code assumes that the block layer
911  * delivers write requests to the device in sequential order. This is always the
912  * case if a block IO scheduler implementing the ELEVATOR_F_ZBD_SEQ_WRITE
913  * elevator feature is being used (e.g. mq-deadline). The block layer always
914  * automatically select such an elevator for zoned block devices during the
915  * device initialization.
916  */
917 static ssize_t zonefs_file_dio_write(struct kiocb *iocb, struct iov_iter *from)
918 {
919 	struct inode *inode = file_inode(iocb->ki_filp);
920 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
921 	struct super_block *sb = inode->i_sb;
922 	bool sync = is_sync_kiocb(iocb);
923 	bool append = false;
924 	ssize_t ret, count;
925 
926 	/*
927 	 * For async direct IOs to sequential zone files, refuse IOCB_NOWAIT
928 	 * as this can cause write reordering (e.g. the first aio gets EAGAIN
929 	 * on the inode lock but the second goes through but is now unaligned).
930 	 */
931 	if (zi->i_ztype == ZONEFS_ZTYPE_SEQ && !sync &&
932 	    (iocb->ki_flags & IOCB_NOWAIT))
933 		return -EOPNOTSUPP;
934 
935 	if (iocb->ki_flags & IOCB_NOWAIT) {
936 		if (!inode_trylock(inode))
937 			return -EAGAIN;
938 	} else {
939 		inode_lock(inode);
940 	}
941 
942 	count = zonefs_write_checks(iocb, from);
943 	if (count <= 0) {
944 		ret = count;
945 		goto inode_unlock;
946 	}
947 
948 	if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) {
949 		ret = -EINVAL;
950 		goto inode_unlock;
951 	}
952 
953 	/* Enforce sequential writes (append only) in sequential zones */
954 	if (zi->i_ztype == ZONEFS_ZTYPE_SEQ) {
955 		mutex_lock(&zi->i_truncate_mutex);
956 		if (iocb->ki_pos != zi->i_wpoffset) {
957 			mutex_unlock(&zi->i_truncate_mutex);
958 			ret = -EINVAL;
959 			goto inode_unlock;
960 		}
961 		mutex_unlock(&zi->i_truncate_mutex);
962 		append = sync;
963 	}
964 
965 	if (append)
966 		ret = zonefs_file_dio_append(iocb, from);
967 	else
968 		ret = iomap_dio_rw(iocb, from, &zonefs_write_iomap_ops,
969 				   &zonefs_write_dio_ops, 0, NULL, 0);
970 	if (zi->i_ztype == ZONEFS_ZTYPE_SEQ &&
971 	    (ret > 0 || ret == -EIOCBQUEUED)) {
972 		if (ret > 0)
973 			count = ret;
974 
975 		/*
976 		 * Update the zone write pointer offset assuming the write
977 		 * operation succeeded. If it did not, the error recovery path
978 		 * will correct it. Also do active seq file accounting.
979 		 */
980 		mutex_lock(&zi->i_truncate_mutex);
981 		zi->i_wpoffset += count;
982 		zonefs_account_active(inode);
983 		mutex_unlock(&zi->i_truncate_mutex);
984 	}
985 
986 inode_unlock:
987 	inode_unlock(inode);
988 
989 	return ret;
990 }
991 
992 static ssize_t zonefs_file_buffered_write(struct kiocb *iocb,
993 					  struct iov_iter *from)
994 {
995 	struct inode *inode = file_inode(iocb->ki_filp);
996 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
997 	ssize_t ret;
998 
999 	/*
1000 	 * Direct IO writes are mandatory for sequential zone files so that the
1001 	 * write IO issuing order is preserved.
1002 	 */
1003 	if (zi->i_ztype != ZONEFS_ZTYPE_CNV)
1004 		return -EIO;
1005 
1006 	if (iocb->ki_flags & IOCB_NOWAIT) {
1007 		if (!inode_trylock(inode))
1008 			return -EAGAIN;
1009 	} else {
1010 		inode_lock(inode);
1011 	}
1012 
1013 	ret = zonefs_write_checks(iocb, from);
1014 	if (ret <= 0)
1015 		goto inode_unlock;
1016 
1017 	ret = iomap_file_buffered_write(iocb, from, &zonefs_write_iomap_ops);
1018 	if (ret > 0)
1019 		iocb->ki_pos += ret;
1020 	else if (ret == -EIO)
1021 		zonefs_io_error(inode, true);
1022 
1023 inode_unlock:
1024 	inode_unlock(inode);
1025 	if (ret > 0)
1026 		ret = generic_write_sync(iocb, ret);
1027 
1028 	return ret;
1029 }
1030 
1031 static ssize_t zonefs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1032 {
1033 	struct inode *inode = file_inode(iocb->ki_filp);
1034 
1035 	if (unlikely(IS_IMMUTABLE(inode)))
1036 		return -EPERM;
1037 
1038 	if (sb_rdonly(inode->i_sb))
1039 		return -EROFS;
1040 
1041 	/* Write operations beyond the zone size are not allowed */
1042 	if (iocb->ki_pos >= ZONEFS_I(inode)->i_max_size)
1043 		return -EFBIG;
1044 
1045 	if (iocb->ki_flags & IOCB_DIRECT) {
1046 		ssize_t ret = zonefs_file_dio_write(iocb, from);
1047 		if (ret != -ENOTBLK)
1048 			return ret;
1049 	}
1050 
1051 	return zonefs_file_buffered_write(iocb, from);
1052 }
1053 
1054 static int zonefs_file_read_dio_end_io(struct kiocb *iocb, ssize_t size,
1055 				       int error, unsigned int flags)
1056 {
1057 	if (error) {
1058 		zonefs_io_error(file_inode(iocb->ki_filp), false);
1059 		return error;
1060 	}
1061 
1062 	return 0;
1063 }
1064 
1065 static const struct iomap_dio_ops zonefs_read_dio_ops = {
1066 	.end_io			= zonefs_file_read_dio_end_io,
1067 };
1068 
1069 static ssize_t zonefs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1070 {
1071 	struct inode *inode = file_inode(iocb->ki_filp);
1072 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
1073 	struct super_block *sb = inode->i_sb;
1074 	loff_t isize;
1075 	ssize_t ret;
1076 
1077 	/* Offline zones cannot be read */
1078 	if (unlikely(IS_IMMUTABLE(inode) && !(inode->i_mode & 0777)))
1079 		return -EPERM;
1080 
1081 	if (iocb->ki_pos >= zi->i_max_size)
1082 		return 0;
1083 
1084 	if (iocb->ki_flags & IOCB_NOWAIT) {
1085 		if (!inode_trylock_shared(inode))
1086 			return -EAGAIN;
1087 	} else {
1088 		inode_lock_shared(inode);
1089 	}
1090 
1091 	/* Limit read operations to written data */
1092 	mutex_lock(&zi->i_truncate_mutex);
1093 	isize = i_size_read(inode);
1094 	if (iocb->ki_pos >= isize) {
1095 		mutex_unlock(&zi->i_truncate_mutex);
1096 		ret = 0;
1097 		goto inode_unlock;
1098 	}
1099 	iov_iter_truncate(to, isize - iocb->ki_pos);
1100 	mutex_unlock(&zi->i_truncate_mutex);
1101 
1102 	if (iocb->ki_flags & IOCB_DIRECT) {
1103 		size_t count = iov_iter_count(to);
1104 
1105 		if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) {
1106 			ret = -EINVAL;
1107 			goto inode_unlock;
1108 		}
1109 		file_accessed(iocb->ki_filp);
1110 		ret = iomap_dio_rw(iocb, to, &zonefs_read_iomap_ops,
1111 				   &zonefs_read_dio_ops, 0, NULL, 0);
1112 	} else {
1113 		ret = generic_file_read_iter(iocb, to);
1114 		if (ret == -EIO)
1115 			zonefs_io_error(inode, false);
1116 	}
1117 
1118 inode_unlock:
1119 	inode_unlock_shared(inode);
1120 
1121 	return ret;
1122 }
1123 
1124 /*
1125  * Write open accounting is done only for sequential files.
1126  */
1127 static inline bool zonefs_seq_file_need_wro(struct inode *inode,
1128 					    struct file *file)
1129 {
1130 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
1131 
1132 	if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
1133 		return false;
1134 
1135 	if (!(file->f_mode & FMODE_WRITE))
1136 		return false;
1137 
1138 	return true;
1139 }
1140 
1141 static int zonefs_seq_file_write_open(struct inode *inode)
1142 {
1143 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
1144 	int ret = 0;
1145 
1146 	mutex_lock(&zi->i_truncate_mutex);
1147 
1148 	if (!zi->i_wr_refcnt) {
1149 		struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
1150 		unsigned int wro = atomic_inc_return(&sbi->s_wro_seq_files);
1151 
1152 		if (sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) {
1153 
1154 			if (sbi->s_max_wro_seq_files
1155 			    && wro > sbi->s_max_wro_seq_files) {
1156 				atomic_dec(&sbi->s_wro_seq_files);
1157 				ret = -EBUSY;
1158 				goto unlock;
1159 			}
1160 
1161 			if (i_size_read(inode) < zi->i_max_size) {
1162 				ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_OPEN);
1163 				if (ret) {
1164 					atomic_dec(&sbi->s_wro_seq_files);
1165 					goto unlock;
1166 				}
1167 				zi->i_flags |= ZONEFS_ZONE_OPEN;
1168 				zonefs_account_active(inode);
1169 			}
1170 		}
1171 	}
1172 
1173 	zi->i_wr_refcnt++;
1174 
1175 unlock:
1176 	mutex_unlock(&zi->i_truncate_mutex);
1177 
1178 	return ret;
1179 }
1180 
1181 static int zonefs_file_open(struct inode *inode, struct file *file)
1182 {
1183 	int ret;
1184 
1185 	ret = generic_file_open(inode, file);
1186 	if (ret)
1187 		return ret;
1188 
1189 	if (zonefs_seq_file_need_wro(inode, file))
1190 		return zonefs_seq_file_write_open(inode);
1191 
1192 	return 0;
1193 }
1194 
1195 static void zonefs_seq_file_write_close(struct inode *inode)
1196 {
1197 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
1198 	struct super_block *sb = inode->i_sb;
1199 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1200 	int ret = 0;
1201 
1202 	mutex_lock(&zi->i_truncate_mutex);
1203 
1204 	zi->i_wr_refcnt--;
1205 	if (zi->i_wr_refcnt)
1206 		goto unlock;
1207 
1208 	/*
1209 	 * The file zone may not be open anymore (e.g. the file was truncated to
1210 	 * its maximum size or it was fully written). For this case, we only
1211 	 * need to decrement the write open count.
1212 	 */
1213 	if (zi->i_flags & ZONEFS_ZONE_OPEN) {
1214 		ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_CLOSE);
1215 		if (ret) {
1216 			__zonefs_io_error(inode, false);
1217 			/*
1218 			 * Leaving zones explicitly open may lead to a state
1219 			 * where most zones cannot be written (zone resources
1220 			 * exhausted). So take preventive action by remounting
1221 			 * read-only.
1222 			 */
1223 			if (zi->i_flags & ZONEFS_ZONE_OPEN &&
1224 			    !(sb->s_flags & SB_RDONLY)) {
1225 				zonefs_warn(sb,
1226 					"closing zone at %llu failed %d\n",
1227 					zi->i_zsector, ret);
1228 				zonefs_warn(sb,
1229 					"remounting filesystem read-only\n");
1230 				sb->s_flags |= SB_RDONLY;
1231 			}
1232 			goto unlock;
1233 		}
1234 
1235 		zi->i_flags &= ~ZONEFS_ZONE_OPEN;
1236 		zonefs_account_active(inode);
1237 	}
1238 
1239 	atomic_dec(&sbi->s_wro_seq_files);
1240 
1241 unlock:
1242 	mutex_unlock(&zi->i_truncate_mutex);
1243 }
1244 
1245 static int zonefs_file_release(struct inode *inode, struct file *file)
1246 {
1247 	/*
1248 	 * If we explicitly open a zone we must close it again as well, but the
1249 	 * zone management operation can fail (either due to an IO error or as
1250 	 * the zone has gone offline or read-only). Make sure we don't fail the
1251 	 * close(2) for user-space.
1252 	 */
1253 	if (zonefs_seq_file_need_wro(inode, file))
1254 		zonefs_seq_file_write_close(inode);
1255 
1256 	return 0;
1257 }
1258 
1259 static const struct file_operations zonefs_file_operations = {
1260 	.open		= zonefs_file_open,
1261 	.release	= zonefs_file_release,
1262 	.fsync		= zonefs_file_fsync,
1263 	.mmap		= zonefs_file_mmap,
1264 	.llseek		= zonefs_file_llseek,
1265 	.read_iter	= zonefs_file_read_iter,
1266 	.write_iter	= zonefs_file_write_iter,
1267 	.splice_read	= generic_file_splice_read,
1268 	.splice_write	= iter_file_splice_write,
1269 	.iopoll		= iocb_bio_iopoll,
1270 };
1271 
1272 static struct kmem_cache *zonefs_inode_cachep;
1273 
1274 static struct inode *zonefs_alloc_inode(struct super_block *sb)
1275 {
1276 	struct zonefs_inode_info *zi;
1277 
1278 	zi = alloc_inode_sb(sb, zonefs_inode_cachep, GFP_KERNEL);
1279 	if (!zi)
1280 		return NULL;
1281 
1282 	inode_init_once(&zi->i_vnode);
1283 	mutex_init(&zi->i_truncate_mutex);
1284 	zi->i_wr_refcnt = 0;
1285 	zi->i_flags = 0;
1286 
1287 	return &zi->i_vnode;
1288 }
1289 
1290 static void zonefs_free_inode(struct inode *inode)
1291 {
1292 	kmem_cache_free(zonefs_inode_cachep, ZONEFS_I(inode));
1293 }
1294 
1295 /*
1296  * File system stat.
1297  */
1298 static int zonefs_statfs(struct dentry *dentry, struct kstatfs *buf)
1299 {
1300 	struct super_block *sb = dentry->d_sb;
1301 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1302 	enum zonefs_ztype t;
1303 
1304 	buf->f_type = ZONEFS_MAGIC;
1305 	buf->f_bsize = sb->s_blocksize;
1306 	buf->f_namelen = ZONEFS_NAME_MAX;
1307 
1308 	spin_lock(&sbi->s_lock);
1309 
1310 	buf->f_blocks = sbi->s_blocks;
1311 	if (WARN_ON(sbi->s_used_blocks > sbi->s_blocks))
1312 		buf->f_bfree = 0;
1313 	else
1314 		buf->f_bfree = buf->f_blocks - sbi->s_used_blocks;
1315 	buf->f_bavail = buf->f_bfree;
1316 
1317 	for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) {
1318 		if (sbi->s_nr_files[t])
1319 			buf->f_files += sbi->s_nr_files[t] + 1;
1320 	}
1321 	buf->f_ffree = 0;
1322 
1323 	spin_unlock(&sbi->s_lock);
1324 
1325 	buf->f_fsid = uuid_to_fsid(sbi->s_uuid.b);
1326 
1327 	return 0;
1328 }
1329 
1330 enum {
1331 	Opt_errors_ro, Opt_errors_zro, Opt_errors_zol, Opt_errors_repair,
1332 	Opt_explicit_open, Opt_err,
1333 };
1334 
1335 static const match_table_t tokens = {
1336 	{ Opt_errors_ro,	"errors=remount-ro"},
1337 	{ Opt_errors_zro,	"errors=zone-ro"},
1338 	{ Opt_errors_zol,	"errors=zone-offline"},
1339 	{ Opt_errors_repair,	"errors=repair"},
1340 	{ Opt_explicit_open,	"explicit-open" },
1341 	{ Opt_err,		NULL}
1342 };
1343 
1344 static int zonefs_parse_options(struct super_block *sb, char *options)
1345 {
1346 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1347 	substring_t args[MAX_OPT_ARGS];
1348 	char *p;
1349 
1350 	if (!options)
1351 		return 0;
1352 
1353 	while ((p = strsep(&options, ",")) != NULL) {
1354 		int token;
1355 
1356 		if (!*p)
1357 			continue;
1358 
1359 		token = match_token(p, tokens, args);
1360 		switch (token) {
1361 		case Opt_errors_ro:
1362 			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
1363 			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_RO;
1364 			break;
1365 		case Opt_errors_zro:
1366 			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
1367 			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZRO;
1368 			break;
1369 		case Opt_errors_zol:
1370 			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
1371 			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZOL;
1372 			break;
1373 		case Opt_errors_repair:
1374 			sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
1375 			sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_REPAIR;
1376 			break;
1377 		case Opt_explicit_open:
1378 			sbi->s_mount_opts |= ZONEFS_MNTOPT_EXPLICIT_OPEN;
1379 			break;
1380 		default:
1381 			return -EINVAL;
1382 		}
1383 	}
1384 
1385 	return 0;
1386 }
1387 
1388 static int zonefs_show_options(struct seq_file *seq, struct dentry *root)
1389 {
1390 	struct zonefs_sb_info *sbi = ZONEFS_SB(root->d_sb);
1391 
1392 	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO)
1393 		seq_puts(seq, ",errors=remount-ro");
1394 	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO)
1395 		seq_puts(seq, ",errors=zone-ro");
1396 	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL)
1397 		seq_puts(seq, ",errors=zone-offline");
1398 	if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_REPAIR)
1399 		seq_puts(seq, ",errors=repair");
1400 
1401 	return 0;
1402 }
1403 
1404 static int zonefs_remount(struct super_block *sb, int *flags, char *data)
1405 {
1406 	sync_filesystem(sb);
1407 
1408 	return zonefs_parse_options(sb, data);
1409 }
1410 
1411 static const struct super_operations zonefs_sops = {
1412 	.alloc_inode	= zonefs_alloc_inode,
1413 	.free_inode	= zonefs_free_inode,
1414 	.statfs		= zonefs_statfs,
1415 	.remount_fs	= zonefs_remount,
1416 	.show_options	= zonefs_show_options,
1417 };
1418 
1419 static const struct inode_operations zonefs_dir_inode_operations = {
1420 	.lookup		= simple_lookup,
1421 	.setattr	= zonefs_inode_setattr,
1422 };
1423 
1424 static void zonefs_init_dir_inode(struct inode *parent, struct inode *inode,
1425 				  enum zonefs_ztype type)
1426 {
1427 	struct super_block *sb = parent->i_sb;
1428 
1429 	inode->i_ino = bdev_nr_zones(sb->s_bdev) + type + 1;
1430 	inode_init_owner(&init_user_ns, inode, parent, S_IFDIR | 0555);
1431 	inode->i_op = &zonefs_dir_inode_operations;
1432 	inode->i_fop = &simple_dir_operations;
1433 	set_nlink(inode, 2);
1434 	inc_nlink(parent);
1435 }
1436 
1437 static int zonefs_init_file_inode(struct inode *inode, struct blk_zone *zone,
1438 				  enum zonefs_ztype type)
1439 {
1440 	struct super_block *sb = inode->i_sb;
1441 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1442 	struct zonefs_inode_info *zi = ZONEFS_I(inode);
1443 	int ret = 0;
1444 
1445 	inode->i_ino = zone->start >> sbi->s_zone_sectors_shift;
1446 	inode->i_mode = S_IFREG | sbi->s_perm;
1447 
1448 	zi->i_ztype = type;
1449 	zi->i_zsector = zone->start;
1450 	zi->i_zone_size = zone->len << SECTOR_SHIFT;
1451 	if (zi->i_zone_size > bdev_zone_sectors(sb->s_bdev) << SECTOR_SHIFT &&
1452 	    !(sbi->s_features & ZONEFS_F_AGGRCNV)) {
1453 		zonefs_err(sb,
1454 			   "zone size %llu doesn't match device's zone sectors %llu\n",
1455 			   zi->i_zone_size,
1456 			   bdev_zone_sectors(sb->s_bdev) << SECTOR_SHIFT);
1457 		return -EINVAL;
1458 	}
1459 
1460 	zi->i_max_size = min_t(loff_t, MAX_LFS_FILESIZE,
1461 			       zone->capacity << SECTOR_SHIFT);
1462 	zi->i_wpoffset = zonefs_check_zone_condition(inode, zone, true, true);
1463 
1464 	inode->i_uid = sbi->s_uid;
1465 	inode->i_gid = sbi->s_gid;
1466 	inode->i_size = zi->i_wpoffset;
1467 	inode->i_blocks = zi->i_max_size >> SECTOR_SHIFT;
1468 
1469 	inode->i_op = &zonefs_file_inode_operations;
1470 	inode->i_fop = &zonefs_file_operations;
1471 	inode->i_mapping->a_ops = &zonefs_file_aops;
1472 
1473 	sb->s_maxbytes = max(zi->i_max_size, sb->s_maxbytes);
1474 	sbi->s_blocks += zi->i_max_size >> sb->s_blocksize_bits;
1475 	sbi->s_used_blocks += zi->i_wpoffset >> sb->s_blocksize_bits;
1476 
1477 	mutex_lock(&zi->i_truncate_mutex);
1478 
1479 	/*
1480 	 * For sequential zones, make sure that any open zone is closed first
1481 	 * to ensure that the initial number of open zones is 0, in sync with
1482 	 * the open zone accounting done when the mount option
1483 	 * ZONEFS_MNTOPT_EXPLICIT_OPEN is used.
1484 	 */
1485 	if (type == ZONEFS_ZTYPE_SEQ &&
1486 	    (zone->cond == BLK_ZONE_COND_IMP_OPEN ||
1487 	     zone->cond == BLK_ZONE_COND_EXP_OPEN)) {
1488 		ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_CLOSE);
1489 		if (ret)
1490 			goto unlock;
1491 	}
1492 
1493 	zonefs_account_active(inode);
1494 
1495 unlock:
1496 	mutex_unlock(&zi->i_truncate_mutex);
1497 
1498 	return ret;
1499 }
1500 
1501 static struct dentry *zonefs_create_inode(struct dentry *parent,
1502 					const char *name, struct blk_zone *zone,
1503 					enum zonefs_ztype type)
1504 {
1505 	struct inode *dir = d_inode(parent);
1506 	struct dentry *dentry;
1507 	struct inode *inode;
1508 	int ret = -ENOMEM;
1509 
1510 	dentry = d_alloc_name(parent, name);
1511 	if (!dentry)
1512 		return ERR_PTR(ret);
1513 
1514 	inode = new_inode(parent->d_sb);
1515 	if (!inode)
1516 		goto dput;
1517 
1518 	inode->i_ctime = inode->i_mtime = inode->i_atime = dir->i_ctime;
1519 	if (zone) {
1520 		ret = zonefs_init_file_inode(inode, zone, type);
1521 		if (ret) {
1522 			iput(inode);
1523 			goto dput;
1524 		}
1525 	} else {
1526 		zonefs_init_dir_inode(dir, inode, type);
1527 	}
1528 
1529 	d_add(dentry, inode);
1530 	dir->i_size++;
1531 
1532 	return dentry;
1533 
1534 dput:
1535 	dput(dentry);
1536 
1537 	return ERR_PTR(ret);
1538 }
1539 
1540 struct zonefs_zone_data {
1541 	struct super_block	*sb;
1542 	unsigned int		nr_zones[ZONEFS_ZTYPE_MAX];
1543 	struct blk_zone		*zones;
1544 };
1545 
1546 /*
1547  * Create a zone group and populate it with zone files.
1548  */
1549 static int zonefs_create_zgroup(struct zonefs_zone_data *zd,
1550 				enum zonefs_ztype type)
1551 {
1552 	struct super_block *sb = zd->sb;
1553 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1554 	struct blk_zone *zone, *next, *end;
1555 	const char *zgroup_name;
1556 	char *file_name;
1557 	struct dentry *dir, *dent;
1558 	unsigned int n = 0;
1559 	int ret;
1560 
1561 	/* If the group is empty, there is nothing to do */
1562 	if (!zd->nr_zones[type])
1563 		return 0;
1564 
1565 	file_name = kmalloc(ZONEFS_NAME_MAX, GFP_KERNEL);
1566 	if (!file_name)
1567 		return -ENOMEM;
1568 
1569 	if (type == ZONEFS_ZTYPE_CNV)
1570 		zgroup_name = "cnv";
1571 	else
1572 		zgroup_name = "seq";
1573 
1574 	dir = zonefs_create_inode(sb->s_root, zgroup_name, NULL, type);
1575 	if (IS_ERR(dir)) {
1576 		ret = PTR_ERR(dir);
1577 		goto free;
1578 	}
1579 
1580 	/*
1581 	 * The first zone contains the super block: skip it.
1582 	 */
1583 	end = zd->zones + bdev_nr_zones(sb->s_bdev);
1584 	for (zone = &zd->zones[1]; zone < end; zone = next) {
1585 
1586 		next = zone + 1;
1587 		if (zonefs_zone_type(zone) != type)
1588 			continue;
1589 
1590 		/*
1591 		 * For conventional zones, contiguous zones can be aggregated
1592 		 * together to form larger files. Note that this overwrites the
1593 		 * length of the first zone of the set of contiguous zones
1594 		 * aggregated together. If one offline or read-only zone is
1595 		 * found, assume that all zones aggregated have the same
1596 		 * condition.
1597 		 */
1598 		if (type == ZONEFS_ZTYPE_CNV &&
1599 		    (sbi->s_features & ZONEFS_F_AGGRCNV)) {
1600 			for (; next < end; next++) {
1601 				if (zonefs_zone_type(next) != type)
1602 					break;
1603 				zone->len += next->len;
1604 				zone->capacity += next->capacity;
1605 				if (next->cond == BLK_ZONE_COND_READONLY &&
1606 				    zone->cond != BLK_ZONE_COND_OFFLINE)
1607 					zone->cond = BLK_ZONE_COND_READONLY;
1608 				else if (next->cond == BLK_ZONE_COND_OFFLINE)
1609 					zone->cond = BLK_ZONE_COND_OFFLINE;
1610 			}
1611 			if (zone->capacity != zone->len) {
1612 				zonefs_err(sb, "Invalid conventional zone capacity\n");
1613 				ret = -EINVAL;
1614 				goto free;
1615 			}
1616 		}
1617 
1618 		/*
1619 		 * Use the file number within its group as file name.
1620 		 */
1621 		snprintf(file_name, ZONEFS_NAME_MAX - 1, "%u", n);
1622 		dent = zonefs_create_inode(dir, file_name, zone, type);
1623 		if (IS_ERR(dent)) {
1624 			ret = PTR_ERR(dent);
1625 			goto free;
1626 		}
1627 
1628 		n++;
1629 	}
1630 
1631 	zonefs_info(sb, "Zone group \"%s\" has %u file%s\n",
1632 		    zgroup_name, n, n > 1 ? "s" : "");
1633 
1634 	sbi->s_nr_files[type] = n;
1635 	ret = 0;
1636 
1637 free:
1638 	kfree(file_name);
1639 
1640 	return ret;
1641 }
1642 
1643 static int zonefs_get_zone_info_cb(struct blk_zone *zone, unsigned int idx,
1644 				   void *data)
1645 {
1646 	struct zonefs_zone_data *zd = data;
1647 
1648 	/*
1649 	 * Count the number of usable zones: the first zone at index 0 contains
1650 	 * the super block and is ignored.
1651 	 */
1652 	switch (zone->type) {
1653 	case BLK_ZONE_TYPE_CONVENTIONAL:
1654 		zone->wp = zone->start + zone->len;
1655 		if (idx)
1656 			zd->nr_zones[ZONEFS_ZTYPE_CNV]++;
1657 		break;
1658 	case BLK_ZONE_TYPE_SEQWRITE_REQ:
1659 	case BLK_ZONE_TYPE_SEQWRITE_PREF:
1660 		if (idx)
1661 			zd->nr_zones[ZONEFS_ZTYPE_SEQ]++;
1662 		break;
1663 	default:
1664 		zonefs_err(zd->sb, "Unsupported zone type 0x%x\n",
1665 			   zone->type);
1666 		return -EIO;
1667 	}
1668 
1669 	memcpy(&zd->zones[idx], zone, sizeof(struct blk_zone));
1670 
1671 	return 0;
1672 }
1673 
1674 static int zonefs_get_zone_info(struct zonefs_zone_data *zd)
1675 {
1676 	struct block_device *bdev = zd->sb->s_bdev;
1677 	int ret;
1678 
1679 	zd->zones = kvcalloc(bdev_nr_zones(bdev), sizeof(struct blk_zone),
1680 			     GFP_KERNEL);
1681 	if (!zd->zones)
1682 		return -ENOMEM;
1683 
1684 	/* Get zones information from the device */
1685 	ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES,
1686 				  zonefs_get_zone_info_cb, zd);
1687 	if (ret < 0) {
1688 		zonefs_err(zd->sb, "Zone report failed %d\n", ret);
1689 		return ret;
1690 	}
1691 
1692 	if (ret != bdev_nr_zones(bdev)) {
1693 		zonefs_err(zd->sb, "Invalid zone report (%d/%u zones)\n",
1694 			   ret, bdev_nr_zones(bdev));
1695 		return -EIO;
1696 	}
1697 
1698 	return 0;
1699 }
1700 
1701 static inline void zonefs_cleanup_zone_info(struct zonefs_zone_data *zd)
1702 {
1703 	kvfree(zd->zones);
1704 }
1705 
1706 /*
1707  * Read super block information from the device.
1708  */
1709 static int zonefs_read_super(struct super_block *sb)
1710 {
1711 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1712 	struct zonefs_super *super;
1713 	u32 crc, stored_crc;
1714 	struct page *page;
1715 	struct bio_vec bio_vec;
1716 	struct bio bio;
1717 	int ret;
1718 
1719 	page = alloc_page(GFP_KERNEL);
1720 	if (!page)
1721 		return -ENOMEM;
1722 
1723 	bio_init(&bio, sb->s_bdev, &bio_vec, 1, REQ_OP_READ);
1724 	bio.bi_iter.bi_sector = 0;
1725 	bio_add_page(&bio, page, PAGE_SIZE, 0);
1726 
1727 	ret = submit_bio_wait(&bio);
1728 	if (ret)
1729 		goto free_page;
1730 
1731 	super = page_address(page);
1732 
1733 	ret = -EINVAL;
1734 	if (le32_to_cpu(super->s_magic) != ZONEFS_MAGIC)
1735 		goto free_page;
1736 
1737 	stored_crc = le32_to_cpu(super->s_crc);
1738 	super->s_crc = 0;
1739 	crc = crc32(~0U, (unsigned char *)super, sizeof(struct zonefs_super));
1740 	if (crc != stored_crc) {
1741 		zonefs_err(sb, "Invalid checksum (Expected 0x%08x, got 0x%08x)",
1742 			   crc, stored_crc);
1743 		goto free_page;
1744 	}
1745 
1746 	sbi->s_features = le64_to_cpu(super->s_features);
1747 	if (sbi->s_features & ~ZONEFS_F_DEFINED_FEATURES) {
1748 		zonefs_err(sb, "Unknown features set 0x%llx\n",
1749 			   sbi->s_features);
1750 		goto free_page;
1751 	}
1752 
1753 	if (sbi->s_features & ZONEFS_F_UID) {
1754 		sbi->s_uid = make_kuid(current_user_ns(),
1755 				       le32_to_cpu(super->s_uid));
1756 		if (!uid_valid(sbi->s_uid)) {
1757 			zonefs_err(sb, "Invalid UID feature\n");
1758 			goto free_page;
1759 		}
1760 	}
1761 
1762 	if (sbi->s_features & ZONEFS_F_GID) {
1763 		sbi->s_gid = make_kgid(current_user_ns(),
1764 				       le32_to_cpu(super->s_gid));
1765 		if (!gid_valid(sbi->s_gid)) {
1766 			zonefs_err(sb, "Invalid GID feature\n");
1767 			goto free_page;
1768 		}
1769 	}
1770 
1771 	if (sbi->s_features & ZONEFS_F_PERM)
1772 		sbi->s_perm = le32_to_cpu(super->s_perm);
1773 
1774 	if (memchr_inv(super->s_reserved, 0, sizeof(super->s_reserved))) {
1775 		zonefs_err(sb, "Reserved area is being used\n");
1776 		goto free_page;
1777 	}
1778 
1779 	import_uuid(&sbi->s_uuid, super->s_uuid);
1780 	ret = 0;
1781 
1782 free_page:
1783 	__free_page(page);
1784 
1785 	return ret;
1786 }
1787 
1788 /*
1789  * Check that the device is zoned. If it is, get the list of zones and create
1790  * sub-directories and files according to the device zone configuration and
1791  * format options.
1792  */
1793 static int zonefs_fill_super(struct super_block *sb, void *data, int silent)
1794 {
1795 	struct zonefs_zone_data zd;
1796 	struct zonefs_sb_info *sbi;
1797 	struct inode *inode;
1798 	enum zonefs_ztype t;
1799 	int ret;
1800 
1801 	if (!bdev_is_zoned(sb->s_bdev)) {
1802 		zonefs_err(sb, "Not a zoned block device\n");
1803 		return -EINVAL;
1804 	}
1805 
1806 	/*
1807 	 * Initialize super block information: the maximum file size is updated
1808 	 * when the zone files are created so that the format option
1809 	 * ZONEFS_F_AGGRCNV which increases the maximum file size of a file
1810 	 * beyond the zone size is taken into account.
1811 	 */
1812 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1813 	if (!sbi)
1814 		return -ENOMEM;
1815 
1816 	spin_lock_init(&sbi->s_lock);
1817 	sb->s_fs_info = sbi;
1818 	sb->s_magic = ZONEFS_MAGIC;
1819 	sb->s_maxbytes = 0;
1820 	sb->s_op = &zonefs_sops;
1821 	sb->s_time_gran	= 1;
1822 
1823 	/*
1824 	 * The block size is set to the device zone write granularity to ensure
1825 	 * that write operations are always aligned according to the device
1826 	 * interface constraints.
1827 	 */
1828 	sb_set_blocksize(sb, bdev_zone_write_granularity(sb->s_bdev));
1829 	sbi->s_zone_sectors_shift = ilog2(bdev_zone_sectors(sb->s_bdev));
1830 	sbi->s_uid = GLOBAL_ROOT_UID;
1831 	sbi->s_gid = GLOBAL_ROOT_GID;
1832 	sbi->s_perm = 0640;
1833 	sbi->s_mount_opts = ZONEFS_MNTOPT_ERRORS_RO;
1834 
1835 	atomic_set(&sbi->s_wro_seq_files, 0);
1836 	sbi->s_max_wro_seq_files = bdev_max_open_zones(sb->s_bdev);
1837 	atomic_set(&sbi->s_active_seq_files, 0);
1838 	sbi->s_max_active_seq_files = bdev_max_active_zones(sb->s_bdev);
1839 
1840 	ret = zonefs_read_super(sb);
1841 	if (ret)
1842 		return ret;
1843 
1844 	ret = zonefs_parse_options(sb, data);
1845 	if (ret)
1846 		return ret;
1847 
1848 	memset(&zd, 0, sizeof(struct zonefs_zone_data));
1849 	zd.sb = sb;
1850 	ret = zonefs_get_zone_info(&zd);
1851 	if (ret)
1852 		goto cleanup;
1853 
1854 	ret = zonefs_sysfs_register(sb);
1855 	if (ret)
1856 		goto cleanup;
1857 
1858 	zonefs_info(sb, "Mounting %u zones", bdev_nr_zones(sb->s_bdev));
1859 
1860 	if (!sbi->s_max_wro_seq_files &&
1861 	    !sbi->s_max_active_seq_files &&
1862 	    sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) {
1863 		zonefs_info(sb,
1864 			"No open and active zone limits. Ignoring explicit_open mount option\n");
1865 		sbi->s_mount_opts &= ~ZONEFS_MNTOPT_EXPLICIT_OPEN;
1866 	}
1867 
1868 	/* Create root directory inode */
1869 	ret = -ENOMEM;
1870 	inode = new_inode(sb);
1871 	if (!inode)
1872 		goto cleanup;
1873 
1874 	inode->i_ino = bdev_nr_zones(sb->s_bdev);
1875 	inode->i_mode = S_IFDIR | 0555;
1876 	inode->i_ctime = inode->i_mtime = inode->i_atime = current_time(inode);
1877 	inode->i_op = &zonefs_dir_inode_operations;
1878 	inode->i_fop = &simple_dir_operations;
1879 	set_nlink(inode, 2);
1880 
1881 	sb->s_root = d_make_root(inode);
1882 	if (!sb->s_root)
1883 		goto cleanup;
1884 
1885 	/* Create and populate files in zone groups directories */
1886 	for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) {
1887 		ret = zonefs_create_zgroup(&zd, t);
1888 		if (ret)
1889 			break;
1890 	}
1891 
1892 cleanup:
1893 	zonefs_cleanup_zone_info(&zd);
1894 
1895 	return ret;
1896 }
1897 
1898 static struct dentry *zonefs_mount(struct file_system_type *fs_type,
1899 				   int flags, const char *dev_name, void *data)
1900 {
1901 	return mount_bdev(fs_type, flags, dev_name, data, zonefs_fill_super);
1902 }
1903 
1904 static void zonefs_kill_super(struct super_block *sb)
1905 {
1906 	struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1907 
1908 	if (sb->s_root)
1909 		d_genocide(sb->s_root);
1910 
1911 	zonefs_sysfs_unregister(sb);
1912 	kill_block_super(sb);
1913 	kfree(sbi);
1914 }
1915 
1916 /*
1917  * File system definition and registration.
1918  */
1919 static struct file_system_type zonefs_type = {
1920 	.owner		= THIS_MODULE,
1921 	.name		= "zonefs",
1922 	.mount		= zonefs_mount,
1923 	.kill_sb	= zonefs_kill_super,
1924 	.fs_flags	= FS_REQUIRES_DEV,
1925 };
1926 
1927 static int __init zonefs_init_inodecache(void)
1928 {
1929 	zonefs_inode_cachep = kmem_cache_create("zonefs_inode_cache",
1930 			sizeof(struct zonefs_inode_info), 0,
1931 			(SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT),
1932 			NULL);
1933 	if (zonefs_inode_cachep == NULL)
1934 		return -ENOMEM;
1935 	return 0;
1936 }
1937 
1938 static void zonefs_destroy_inodecache(void)
1939 {
1940 	/*
1941 	 * Make sure all delayed rcu free inodes are flushed before we
1942 	 * destroy the inode cache.
1943 	 */
1944 	rcu_barrier();
1945 	kmem_cache_destroy(zonefs_inode_cachep);
1946 }
1947 
1948 static int __init zonefs_init(void)
1949 {
1950 	int ret;
1951 
1952 	BUILD_BUG_ON(sizeof(struct zonefs_super) != ZONEFS_SUPER_SIZE);
1953 
1954 	ret = zonefs_init_inodecache();
1955 	if (ret)
1956 		return ret;
1957 
1958 	ret = zonefs_sysfs_init();
1959 	if (ret)
1960 		goto destroy_inodecache;
1961 
1962 	ret = register_filesystem(&zonefs_type);
1963 	if (ret)
1964 		goto sysfs_exit;
1965 
1966 	return 0;
1967 
1968 sysfs_exit:
1969 	zonefs_sysfs_exit();
1970 destroy_inodecache:
1971 	zonefs_destroy_inodecache();
1972 
1973 	return ret;
1974 }
1975 
1976 static void __exit zonefs_exit(void)
1977 {
1978 	unregister_filesystem(&zonefs_type);
1979 	zonefs_sysfs_exit();
1980 	zonefs_destroy_inodecache();
1981 }
1982 
1983 MODULE_AUTHOR("Damien Le Moal");
1984 MODULE_DESCRIPTION("Zone file system for zoned block devices");
1985 MODULE_LICENSE("GPL");
1986 MODULE_ALIAS_FS("zonefs");
1987 module_init(zonefs_init);
1988 module_exit(zonefs_exit);
1989