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