1 // SPDX-License-Identifier: GPL-2.0
3 * Simple file system for zoned block devices exposing zones as files.
5 * Copyright (C) 2019 Western Digital Corporation or its affiliates.
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>
27 #define CREATE_TRACE_POINTS
31 * Manage the active zone count. Called with zi->i_truncate_mutex held.
33 static void zonefs_account_active(struct inode *inode)
35 struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
36 struct zonefs_inode_info *zi = ZONEFS_I(inode);
38 lockdep_assert_held(&zi->i_truncate_mutex);
40 if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
44 * If the zone is active, that is, if it is explicitly open or
45 * partially written, check if it was already accounted as active.
47 if ((zi->i_flags & ZONEFS_ZONE_OPEN) ||
48 (zi->i_wpoffset > 0 && zi->i_wpoffset < zi->i_max_size)) {
49 if (!(zi->i_flags & ZONEFS_ZONE_ACTIVE)) {
50 zi->i_flags |= ZONEFS_ZONE_ACTIVE;
51 atomic_inc(&sbi->s_active_seq_files);
56 /* The zone is not active. If it was, update the active count */
57 if (zi->i_flags & ZONEFS_ZONE_ACTIVE) {
58 zi->i_flags &= ~ZONEFS_ZONE_ACTIVE;
59 atomic_dec(&sbi->s_active_seq_files);
63 static inline int zonefs_zone_mgmt(struct inode *inode,
66 struct zonefs_inode_info *zi = ZONEFS_I(inode);
69 lockdep_assert_held(&zi->i_truncate_mutex);
72 * With ZNS drives, closing an explicitly open zone that has not been
73 * written will change the zone state to "closed", that is, the zone
74 * will remain active. Since this can then cause failure of explicit
75 * open operation on other zones if the drive active zone resources
76 * are exceeded, make sure that the zone does not remain active by
79 if (op == REQ_OP_ZONE_CLOSE && !zi->i_wpoffset)
80 op = REQ_OP_ZONE_RESET;
82 trace_zonefs_zone_mgmt(inode, op);
83 ret = blkdev_zone_mgmt(inode->i_sb->s_bdev, op, zi->i_zsector,
84 zi->i_zone_size >> SECTOR_SHIFT, GFP_NOFS);
86 zonefs_err(inode->i_sb,
87 "Zone management operation %s at %llu failed %d\n",
88 blk_op_str(op), zi->i_zsector, ret);
95 static inline void zonefs_i_size_write(struct inode *inode, loff_t isize)
97 struct zonefs_inode_info *zi = ZONEFS_I(inode);
99 i_size_write(inode, isize);
101 * A full zone is no longer open/active and does not need
104 if (isize >= zi->i_max_size) {
105 struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
107 if (zi->i_flags & ZONEFS_ZONE_ACTIVE)
108 atomic_dec(&sbi->s_active_seq_files);
109 zi->i_flags &= ~(ZONEFS_ZONE_OPEN | ZONEFS_ZONE_ACTIVE);
113 static int zonefs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
114 unsigned int flags, struct iomap *iomap,
115 struct iomap *srcmap)
117 struct zonefs_inode_info *zi = ZONEFS_I(inode);
118 struct super_block *sb = inode->i_sb;
121 /* All I/Os should always be within the file maximum size */
122 if (WARN_ON_ONCE(offset + length > zi->i_max_size))
126 * Sequential zones can only accept direct writes. This is already
127 * checked when writes are issued, so warn if we see a page writeback
130 if (WARN_ON_ONCE(zi->i_ztype == ZONEFS_ZTYPE_SEQ &&
131 (flags & IOMAP_WRITE) && !(flags & IOMAP_DIRECT)))
135 * For conventional zones, all blocks are always mapped. For sequential
136 * zones, all blocks after always mapped below the inode size (zone
137 * write pointer) and unwriten beyond.
139 mutex_lock(&zi->i_truncate_mutex);
140 isize = i_size_read(inode);
142 iomap->type = IOMAP_UNWRITTEN;
144 iomap->type = IOMAP_MAPPED;
145 if (flags & IOMAP_WRITE)
146 length = zi->i_max_size - offset;
148 length = min(length, isize - offset);
149 mutex_unlock(&zi->i_truncate_mutex);
151 iomap->offset = ALIGN_DOWN(offset, sb->s_blocksize);
152 iomap->length = ALIGN(offset + length, sb->s_blocksize) - iomap->offset;
153 iomap->bdev = inode->i_sb->s_bdev;
154 iomap->addr = (zi->i_zsector << SECTOR_SHIFT) + iomap->offset;
156 trace_zonefs_iomap_begin(inode, iomap);
161 static const struct iomap_ops zonefs_iomap_ops = {
162 .iomap_begin = zonefs_iomap_begin,
165 static int zonefs_read_folio(struct file *unused, struct folio *folio)
167 return iomap_read_folio(folio, &zonefs_iomap_ops);
170 static void zonefs_readahead(struct readahead_control *rac)
172 iomap_readahead(rac, &zonefs_iomap_ops);
176 * Map blocks for page writeback. This is used only on conventional zone files,
177 * which implies that the page range can only be within the fixed inode size.
179 static int zonefs_map_blocks(struct iomap_writepage_ctx *wpc,
180 struct inode *inode, loff_t offset)
182 struct zonefs_inode_info *zi = ZONEFS_I(inode);
184 if (WARN_ON_ONCE(zi->i_ztype != ZONEFS_ZTYPE_CNV))
186 if (WARN_ON_ONCE(offset >= i_size_read(inode)))
189 /* If the mapping is already OK, nothing needs to be done */
190 if (offset >= wpc->iomap.offset &&
191 offset < wpc->iomap.offset + wpc->iomap.length)
194 return zonefs_iomap_begin(inode, offset, zi->i_max_size - offset,
195 IOMAP_WRITE, &wpc->iomap, NULL);
198 static const struct iomap_writeback_ops zonefs_writeback_ops = {
199 .map_blocks = zonefs_map_blocks,
202 static int zonefs_writepage(struct page *page, struct writeback_control *wbc)
204 struct iomap_writepage_ctx wpc = { };
206 return iomap_writepage(page, wbc, &wpc, &zonefs_writeback_ops);
209 static int zonefs_writepages(struct address_space *mapping,
210 struct writeback_control *wbc)
212 struct iomap_writepage_ctx wpc = { };
214 return iomap_writepages(mapping, wbc, &wpc, &zonefs_writeback_ops);
217 static int zonefs_swap_activate(struct swap_info_struct *sis,
218 struct file *swap_file, sector_t *span)
220 struct inode *inode = file_inode(swap_file);
221 struct zonefs_inode_info *zi = ZONEFS_I(inode);
223 if (zi->i_ztype != ZONEFS_ZTYPE_CNV) {
224 zonefs_err(inode->i_sb,
225 "swap file: not a conventional zone file\n");
229 return iomap_swapfile_activate(sis, swap_file, span, &zonefs_iomap_ops);
232 static const struct address_space_operations zonefs_file_aops = {
233 .read_folio = zonefs_read_folio,
234 .readahead = zonefs_readahead,
235 .writepage = zonefs_writepage,
236 .writepages = zonefs_writepages,
237 .dirty_folio = filemap_dirty_folio,
238 .release_folio = iomap_release_folio,
239 .invalidate_folio = iomap_invalidate_folio,
240 .migratepage = iomap_migrate_page,
241 .is_partially_uptodate = iomap_is_partially_uptodate,
242 .error_remove_page = generic_error_remove_page,
243 .direct_IO = noop_direct_IO,
244 .swap_activate = zonefs_swap_activate,
247 static void zonefs_update_stats(struct inode *inode, loff_t new_isize)
249 struct super_block *sb = inode->i_sb;
250 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
251 loff_t old_isize = i_size_read(inode);
254 if (new_isize == old_isize)
257 spin_lock(&sbi->s_lock);
260 * This may be called for an update after an IO error.
261 * So beware of the values seen.
263 if (new_isize < old_isize) {
264 nr_blocks = (old_isize - new_isize) >> sb->s_blocksize_bits;
265 if (sbi->s_used_blocks > nr_blocks)
266 sbi->s_used_blocks -= nr_blocks;
268 sbi->s_used_blocks = 0;
270 sbi->s_used_blocks +=
271 (new_isize - old_isize) >> sb->s_blocksize_bits;
272 if (sbi->s_used_blocks > sbi->s_blocks)
273 sbi->s_used_blocks = sbi->s_blocks;
276 spin_unlock(&sbi->s_lock);
280 * Check a zone condition and adjust its file inode access permissions for
281 * offline and readonly zones. Return the inode size corresponding to the
282 * amount of readable data in the zone.
284 static loff_t zonefs_check_zone_condition(struct inode *inode,
285 struct blk_zone *zone, bool warn,
288 struct zonefs_inode_info *zi = ZONEFS_I(inode);
290 switch (zone->cond) {
291 case BLK_ZONE_COND_OFFLINE:
293 * Dead zone: make the inode immutable, disable all accesses
294 * and set the file size to 0 (zone wp set to zone start).
297 zonefs_warn(inode->i_sb, "inode %lu: offline zone\n",
299 inode->i_flags |= S_IMMUTABLE;
300 inode->i_mode &= ~0777;
301 zone->wp = zone->start;
303 case BLK_ZONE_COND_READONLY:
305 * The write pointer of read-only zones is invalid. If such a
306 * zone is found during mount, the file size cannot be retrieved
307 * so we treat the zone as offline (mount == true case).
308 * Otherwise, keep the file size as it was when last updated
309 * so that the user can recover data. In both cases, writes are
310 * always disabled for the zone.
313 zonefs_warn(inode->i_sb, "inode %lu: read-only zone\n",
315 inode->i_flags |= S_IMMUTABLE;
317 zone->cond = BLK_ZONE_COND_OFFLINE;
318 inode->i_mode &= ~0777;
319 zone->wp = zone->start;
322 inode->i_mode &= ~0222;
323 return i_size_read(inode);
324 case BLK_ZONE_COND_FULL:
325 /* The write pointer of full zones is invalid. */
326 return zi->i_max_size;
328 if (zi->i_ztype == ZONEFS_ZTYPE_CNV)
329 return zi->i_max_size;
330 return (zone->wp - zone->start) << SECTOR_SHIFT;
334 struct zonefs_ioerr_data {
339 static int zonefs_io_error_cb(struct blk_zone *zone, unsigned int idx,
342 struct zonefs_ioerr_data *err = data;
343 struct inode *inode = err->inode;
344 struct zonefs_inode_info *zi = ZONEFS_I(inode);
345 struct super_block *sb = inode->i_sb;
346 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
347 loff_t isize, data_size;
350 * Check the zone condition: if the zone is not "bad" (offline or
351 * read-only), read errors are simply signaled to the IO issuer as long
352 * as there is no inconsistency between the inode size and the amount of
353 * data writen in the zone (data_size).
355 data_size = zonefs_check_zone_condition(inode, zone, true, false);
356 isize = i_size_read(inode);
357 if (zone->cond != BLK_ZONE_COND_OFFLINE &&
358 zone->cond != BLK_ZONE_COND_READONLY &&
359 !err->write && isize == data_size)
363 * At this point, we detected either a bad zone or an inconsistency
364 * between the inode size and the amount of data written in the zone.
365 * For the latter case, the cause may be a write IO error or an external
366 * action on the device. Two error patterns exist:
367 * 1) The inode size is lower than the amount of data in the zone:
368 * a write operation partially failed and data was writen at the end
369 * of the file. This can happen in the case of a large direct IO
370 * needing several BIOs and/or write requests to be processed.
371 * 2) The inode size is larger than the amount of data in the zone:
372 * this can happen with a deferred write error with the use of the
373 * device side write cache after getting successful write IO
374 * completions. Other possibilities are (a) an external corruption,
375 * e.g. an application reset the zone directly, or (b) the device
376 * has a serious problem (e.g. firmware bug).
378 * In all cases, warn about inode size inconsistency and handle the
379 * IO error according to the zone condition and to the mount options.
381 if (zi->i_ztype == ZONEFS_ZTYPE_SEQ && isize != data_size)
382 zonefs_warn(sb, "inode %lu: invalid size %lld (should be %lld)\n",
383 inode->i_ino, isize, data_size);
386 * First handle bad zones signaled by hardware. The mount options
387 * errors=zone-ro and errors=zone-offline result in changing the
388 * zone condition to read-only and offline respectively, as if the
389 * condition was signaled by the hardware.
391 if (zone->cond == BLK_ZONE_COND_OFFLINE ||
392 sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL) {
393 zonefs_warn(sb, "inode %lu: read/write access disabled\n",
395 if (zone->cond != BLK_ZONE_COND_OFFLINE) {
396 zone->cond = BLK_ZONE_COND_OFFLINE;
397 data_size = zonefs_check_zone_condition(inode, zone,
400 } else if (zone->cond == BLK_ZONE_COND_READONLY ||
401 sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO) {
402 zonefs_warn(sb, "inode %lu: write access disabled\n",
404 if (zone->cond != BLK_ZONE_COND_READONLY) {
405 zone->cond = BLK_ZONE_COND_READONLY;
406 data_size = zonefs_check_zone_condition(inode, zone,
412 * If the filesystem is mounted with the explicit-open mount option, we
413 * need to clear the ZONEFS_ZONE_OPEN flag if the zone transitioned to
414 * the read-only or offline condition, to avoid attempting an explicit
415 * close of the zone when the inode file is closed.
417 if ((sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) &&
418 (zone->cond == BLK_ZONE_COND_OFFLINE ||
419 zone->cond == BLK_ZONE_COND_READONLY))
420 zi->i_flags &= ~ZONEFS_ZONE_OPEN;
423 * If error=remount-ro was specified, any error result in remounting
424 * the volume as read-only.
426 if ((sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO) && !sb_rdonly(sb)) {
427 zonefs_warn(sb, "remounting filesystem read-only\n");
428 sb->s_flags |= SB_RDONLY;
432 * Update block usage stats and the inode size to prevent access to
435 zonefs_update_stats(inode, data_size);
436 zonefs_i_size_write(inode, data_size);
437 zi->i_wpoffset = data_size;
438 zonefs_account_active(inode);
444 * When an file IO error occurs, check the file zone to see if there is a change
445 * in the zone condition (e.g. offline or read-only). For a failed write to a
446 * sequential zone, the zone write pointer position must also be checked to
447 * eventually correct the file size and zonefs inode write pointer offset
448 * (which can be out of sync with the drive due to partial write failures).
450 static void __zonefs_io_error(struct inode *inode, bool write)
452 struct zonefs_inode_info *zi = ZONEFS_I(inode);
453 struct super_block *sb = inode->i_sb;
454 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
455 unsigned int noio_flag;
456 unsigned int nr_zones =
457 zi->i_zone_size >> (sbi->s_zone_sectors_shift + SECTOR_SHIFT);
458 struct zonefs_ioerr_data err = {
465 * Memory allocations in blkdev_report_zones() can trigger a memory
466 * reclaim which may in turn cause a recursion into zonefs as well as
467 * struct request allocations for the same device. The former case may
468 * end up in a deadlock on the inode truncate mutex, while the latter
469 * may prevent IO forward progress. Executing the report zones under
470 * the GFP_NOIO context avoids both problems.
472 noio_flag = memalloc_noio_save();
473 ret = blkdev_report_zones(sb->s_bdev, zi->i_zsector, nr_zones,
474 zonefs_io_error_cb, &err);
476 zonefs_err(sb, "Get inode %lu zone information failed %d\n",
478 memalloc_noio_restore(noio_flag);
481 static void zonefs_io_error(struct inode *inode, bool write)
483 struct zonefs_inode_info *zi = ZONEFS_I(inode);
485 mutex_lock(&zi->i_truncate_mutex);
486 __zonefs_io_error(inode, write);
487 mutex_unlock(&zi->i_truncate_mutex);
490 static int zonefs_file_truncate(struct inode *inode, loff_t isize)
492 struct zonefs_inode_info *zi = ZONEFS_I(inode);
498 * Only sequential zone files can be truncated and truncation is allowed
499 * only down to a 0 size, which is equivalent to a zone reset, and to
500 * the maximum file size, which is equivalent to a zone finish.
502 if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
506 op = REQ_OP_ZONE_RESET;
507 else if (isize == zi->i_max_size)
508 op = REQ_OP_ZONE_FINISH;
512 inode_dio_wait(inode);
514 /* Serialize against page faults */
515 filemap_invalidate_lock(inode->i_mapping);
517 /* Serialize against zonefs_iomap_begin() */
518 mutex_lock(&zi->i_truncate_mutex);
520 old_isize = i_size_read(inode);
521 if (isize == old_isize)
524 ret = zonefs_zone_mgmt(inode, op);
529 * If the mount option ZONEFS_MNTOPT_EXPLICIT_OPEN is set,
530 * take care of open zones.
532 if (zi->i_flags & ZONEFS_ZONE_OPEN) {
534 * Truncating a zone to EMPTY or FULL is the equivalent of
535 * closing the zone. For a truncation to 0, we need to
536 * re-open the zone to ensure new writes can be processed.
537 * For a truncation to the maximum file size, the zone is
538 * closed and writes cannot be accepted anymore, so clear
542 ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_OPEN);
544 zi->i_flags &= ~ZONEFS_ZONE_OPEN;
547 zonefs_update_stats(inode, isize);
548 truncate_setsize(inode, isize);
549 zi->i_wpoffset = isize;
550 zonefs_account_active(inode);
553 mutex_unlock(&zi->i_truncate_mutex);
554 filemap_invalidate_unlock(inode->i_mapping);
559 static int zonefs_inode_setattr(struct user_namespace *mnt_userns,
560 struct dentry *dentry, struct iattr *iattr)
562 struct inode *inode = d_inode(dentry);
565 if (unlikely(IS_IMMUTABLE(inode)))
568 ret = setattr_prepare(&init_user_ns, dentry, iattr);
573 * Since files and directories cannot be created nor deleted, do not
574 * allow setting any write attributes on the sub-directories grouping
575 * files by zone type.
577 if ((iattr->ia_valid & ATTR_MODE) && S_ISDIR(inode->i_mode) &&
578 (iattr->ia_mode & 0222))
581 if (((iattr->ia_valid & ATTR_UID) &&
582 !uid_eq(iattr->ia_uid, inode->i_uid)) ||
583 ((iattr->ia_valid & ATTR_GID) &&
584 !gid_eq(iattr->ia_gid, inode->i_gid))) {
585 ret = dquot_transfer(inode, iattr);
590 if (iattr->ia_valid & ATTR_SIZE) {
591 ret = zonefs_file_truncate(inode, iattr->ia_size);
596 setattr_copy(&init_user_ns, inode, iattr);
601 static const struct inode_operations zonefs_file_inode_operations = {
602 .setattr = zonefs_inode_setattr,
605 static int zonefs_file_fsync(struct file *file, loff_t start, loff_t end,
608 struct inode *inode = file_inode(file);
611 if (unlikely(IS_IMMUTABLE(inode)))
615 * Since only direct writes are allowed in sequential files, page cache
616 * flush is needed only for conventional zone files.
618 if (ZONEFS_I(inode)->i_ztype == ZONEFS_ZTYPE_CNV)
619 ret = file_write_and_wait_range(file, start, end);
621 ret = blkdev_issue_flush(inode->i_sb->s_bdev);
624 zonefs_io_error(inode, true);
629 static vm_fault_t zonefs_filemap_page_mkwrite(struct vm_fault *vmf)
631 struct inode *inode = file_inode(vmf->vma->vm_file);
632 struct zonefs_inode_info *zi = ZONEFS_I(inode);
635 if (unlikely(IS_IMMUTABLE(inode)))
636 return VM_FAULT_SIGBUS;
639 * Sanity check: only conventional zone files can have shared
640 * writeable mappings.
642 if (WARN_ON_ONCE(zi->i_ztype != ZONEFS_ZTYPE_CNV))
643 return VM_FAULT_NOPAGE;
645 sb_start_pagefault(inode->i_sb);
646 file_update_time(vmf->vma->vm_file);
648 /* Serialize against truncates */
649 filemap_invalidate_lock_shared(inode->i_mapping);
650 ret = iomap_page_mkwrite(vmf, &zonefs_iomap_ops);
651 filemap_invalidate_unlock_shared(inode->i_mapping);
653 sb_end_pagefault(inode->i_sb);
657 static const struct vm_operations_struct zonefs_file_vm_ops = {
658 .fault = filemap_fault,
659 .map_pages = filemap_map_pages,
660 .page_mkwrite = zonefs_filemap_page_mkwrite,
663 static int zonefs_file_mmap(struct file *file, struct vm_area_struct *vma)
666 * Conventional zones accept random writes, so their files can support
667 * shared writable mappings. For sequential zone files, only read
668 * mappings are possible since there are no guarantees for write
669 * ordering between msync() and page cache writeback.
671 if (ZONEFS_I(file_inode(file))->i_ztype == ZONEFS_ZTYPE_SEQ &&
672 (vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
676 vma->vm_ops = &zonefs_file_vm_ops;
681 static loff_t zonefs_file_llseek(struct file *file, loff_t offset, int whence)
683 loff_t isize = i_size_read(file_inode(file));
686 * Seeks are limited to below the zone size for conventional zones
687 * and below the zone write pointer for sequential zones. In both
688 * cases, this limit is the inode size.
690 return generic_file_llseek_size(file, offset, whence, isize, isize);
693 static int zonefs_file_write_dio_end_io(struct kiocb *iocb, ssize_t size,
694 int error, unsigned int flags)
696 struct inode *inode = file_inode(iocb->ki_filp);
697 struct zonefs_inode_info *zi = ZONEFS_I(inode);
700 zonefs_io_error(inode, true);
704 if (size && zi->i_ztype != ZONEFS_ZTYPE_CNV) {
706 * Note that we may be seeing completions out of order,
707 * but that is not a problem since a write completed
708 * successfully necessarily means that all preceding writes
709 * were also successful. So we can safely increase the inode
710 * size to the write end location.
712 mutex_lock(&zi->i_truncate_mutex);
713 if (i_size_read(inode) < iocb->ki_pos + size) {
714 zonefs_update_stats(inode, iocb->ki_pos + size);
715 zonefs_i_size_write(inode, iocb->ki_pos + size);
717 mutex_unlock(&zi->i_truncate_mutex);
723 static const struct iomap_dio_ops zonefs_write_dio_ops = {
724 .end_io = zonefs_file_write_dio_end_io,
727 static ssize_t zonefs_file_dio_append(struct kiocb *iocb, struct iov_iter *from)
729 struct inode *inode = file_inode(iocb->ki_filp);
730 struct zonefs_inode_info *zi = ZONEFS_I(inode);
731 struct block_device *bdev = inode->i_sb->s_bdev;
732 unsigned int max = bdev_max_zone_append_sectors(bdev);
738 max = ALIGN_DOWN(max << SECTOR_SHIFT, inode->i_sb->s_blocksize);
739 iov_iter_truncate(from, max);
741 nr_pages = iov_iter_npages(from, BIO_MAX_VECS);
745 bio = bio_alloc(bdev, nr_pages,
746 REQ_OP_ZONE_APPEND | REQ_SYNC | REQ_IDLE, GFP_NOFS);
747 bio->bi_iter.bi_sector = zi->i_zsector;
748 bio->bi_ioprio = iocb->ki_ioprio;
749 if (iocb_is_dsync(iocb))
750 bio->bi_opf |= REQ_FUA;
752 ret = bio_iov_iter_get_pages(bio, from);
756 size = bio->bi_iter.bi_size;
757 task_io_account_write(size);
759 if (iocb->ki_flags & IOCB_HIPRI)
760 bio_set_polled(bio, iocb);
762 ret = submit_bio_wait(bio);
764 zonefs_file_write_dio_end_io(iocb, size, ret, 0);
765 trace_zonefs_file_dio_append(inode, size, ret);
768 bio_release_pages(bio, false);
772 iocb->ki_pos += size;
780 * Do not exceed the LFS limits nor the file zone size. If pos is under the
781 * limit it becomes a short access. If it exceeds the limit, return -EFBIG.
783 static loff_t zonefs_write_check_limits(struct file *file, loff_t pos,
786 struct inode *inode = file_inode(file);
787 struct zonefs_inode_info *zi = ZONEFS_I(inode);
788 loff_t limit = rlimit(RLIMIT_FSIZE);
789 loff_t max_size = zi->i_max_size;
791 if (limit != RLIM_INFINITY) {
793 send_sig(SIGXFSZ, current, 0);
796 count = min(count, limit - pos);
799 if (!(file->f_flags & O_LARGEFILE))
800 max_size = min_t(loff_t, MAX_NON_LFS, max_size);
802 if (unlikely(pos >= max_size))
805 return min(count, max_size - pos);
808 static ssize_t zonefs_write_checks(struct kiocb *iocb, struct iov_iter *from)
810 struct file *file = iocb->ki_filp;
811 struct inode *inode = file_inode(file);
812 struct zonefs_inode_info *zi = ZONEFS_I(inode);
815 if (IS_SWAPFILE(inode))
818 if (!iov_iter_count(from))
821 if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
824 if (iocb->ki_flags & IOCB_APPEND) {
825 if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
827 mutex_lock(&zi->i_truncate_mutex);
828 iocb->ki_pos = zi->i_wpoffset;
829 mutex_unlock(&zi->i_truncate_mutex);
832 count = zonefs_write_check_limits(file, iocb->ki_pos,
833 iov_iter_count(from));
837 iov_iter_truncate(from, count);
838 return iov_iter_count(from);
842 * Handle direct writes. For sequential zone files, this is the only possible
843 * write path. For these files, check that the user is issuing writes
844 * sequentially from the end of the file. This code assumes that the block layer
845 * delivers write requests to the device in sequential order. This is always the
846 * case if a block IO scheduler implementing the ELEVATOR_F_ZBD_SEQ_WRITE
847 * elevator feature is being used (e.g. mq-deadline). The block layer always
848 * automatically select such an elevator for zoned block devices during the
849 * device initialization.
851 static ssize_t zonefs_file_dio_write(struct kiocb *iocb, struct iov_iter *from)
853 struct inode *inode = file_inode(iocb->ki_filp);
854 struct zonefs_inode_info *zi = ZONEFS_I(inode);
855 struct super_block *sb = inode->i_sb;
856 bool sync = is_sync_kiocb(iocb);
861 * For async direct IOs to sequential zone files, refuse IOCB_NOWAIT
862 * as this can cause write reordering (e.g. the first aio gets EAGAIN
863 * on the inode lock but the second goes through but is now unaligned).
865 if (zi->i_ztype == ZONEFS_ZTYPE_SEQ && !sync &&
866 (iocb->ki_flags & IOCB_NOWAIT))
869 if (iocb->ki_flags & IOCB_NOWAIT) {
870 if (!inode_trylock(inode))
876 count = zonefs_write_checks(iocb, from);
882 if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) {
887 /* Enforce sequential writes (append only) in sequential zones */
888 if (zi->i_ztype == ZONEFS_ZTYPE_SEQ) {
889 mutex_lock(&zi->i_truncate_mutex);
890 if (iocb->ki_pos != zi->i_wpoffset) {
891 mutex_unlock(&zi->i_truncate_mutex);
895 mutex_unlock(&zi->i_truncate_mutex);
900 ret = zonefs_file_dio_append(iocb, from);
902 ret = iomap_dio_rw(iocb, from, &zonefs_iomap_ops,
903 &zonefs_write_dio_ops, 0, NULL, 0);
904 if (zi->i_ztype == ZONEFS_ZTYPE_SEQ &&
905 (ret > 0 || ret == -EIOCBQUEUED)) {
910 * Update the zone write pointer offset assuming the write
911 * operation succeeded. If it did not, the error recovery path
912 * will correct it. Also do active seq file accounting.
914 mutex_lock(&zi->i_truncate_mutex);
915 zi->i_wpoffset += count;
916 zonefs_account_active(inode);
917 mutex_unlock(&zi->i_truncate_mutex);
926 static ssize_t zonefs_file_buffered_write(struct kiocb *iocb,
927 struct iov_iter *from)
929 struct inode *inode = file_inode(iocb->ki_filp);
930 struct zonefs_inode_info *zi = ZONEFS_I(inode);
934 * Direct IO writes are mandatory for sequential zone files so that the
935 * write IO issuing order is preserved.
937 if (zi->i_ztype != ZONEFS_ZTYPE_CNV)
940 if (iocb->ki_flags & IOCB_NOWAIT) {
941 if (!inode_trylock(inode))
947 ret = zonefs_write_checks(iocb, from);
951 ret = iomap_file_buffered_write(iocb, from, &zonefs_iomap_ops);
954 else if (ret == -EIO)
955 zonefs_io_error(inode, true);
960 ret = generic_write_sync(iocb, ret);
965 static ssize_t zonefs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
967 struct inode *inode = file_inode(iocb->ki_filp);
969 if (unlikely(IS_IMMUTABLE(inode)))
972 if (sb_rdonly(inode->i_sb))
975 /* Write operations beyond the zone size are not allowed */
976 if (iocb->ki_pos >= ZONEFS_I(inode)->i_max_size)
979 if (iocb->ki_flags & IOCB_DIRECT) {
980 ssize_t ret = zonefs_file_dio_write(iocb, from);
985 return zonefs_file_buffered_write(iocb, from);
988 static int zonefs_file_read_dio_end_io(struct kiocb *iocb, ssize_t size,
989 int error, unsigned int flags)
992 zonefs_io_error(file_inode(iocb->ki_filp), false);
999 static const struct iomap_dio_ops zonefs_read_dio_ops = {
1000 .end_io = zonefs_file_read_dio_end_io,
1003 static ssize_t zonefs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1005 struct inode *inode = file_inode(iocb->ki_filp);
1006 struct zonefs_inode_info *zi = ZONEFS_I(inode);
1007 struct super_block *sb = inode->i_sb;
1011 /* Offline zones cannot be read */
1012 if (unlikely(IS_IMMUTABLE(inode) && !(inode->i_mode & 0777)))
1015 if (iocb->ki_pos >= zi->i_max_size)
1018 if (iocb->ki_flags & IOCB_NOWAIT) {
1019 if (!inode_trylock_shared(inode))
1022 inode_lock_shared(inode);
1025 /* Limit read operations to written data */
1026 mutex_lock(&zi->i_truncate_mutex);
1027 isize = i_size_read(inode);
1028 if (iocb->ki_pos >= isize) {
1029 mutex_unlock(&zi->i_truncate_mutex);
1033 iov_iter_truncate(to, isize - iocb->ki_pos);
1034 mutex_unlock(&zi->i_truncate_mutex);
1036 if (iocb->ki_flags & IOCB_DIRECT) {
1037 size_t count = iov_iter_count(to);
1039 if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) {
1043 file_accessed(iocb->ki_filp);
1044 ret = iomap_dio_rw(iocb, to, &zonefs_iomap_ops,
1045 &zonefs_read_dio_ops, 0, NULL, 0);
1047 ret = generic_file_read_iter(iocb, to);
1049 zonefs_io_error(inode, false);
1053 inode_unlock_shared(inode);
1059 * Write open accounting is done only for sequential files.
1061 static inline bool zonefs_seq_file_need_wro(struct inode *inode,
1064 struct zonefs_inode_info *zi = ZONEFS_I(inode);
1066 if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
1069 if (!(file->f_mode & FMODE_WRITE))
1075 static int zonefs_seq_file_write_open(struct inode *inode)
1077 struct zonefs_inode_info *zi = ZONEFS_I(inode);
1080 mutex_lock(&zi->i_truncate_mutex);
1082 if (!zi->i_wr_refcnt) {
1083 struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
1084 unsigned int wro = atomic_inc_return(&sbi->s_wro_seq_files);
1086 if (sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) {
1088 if (wro > sbi->s_max_wro_seq_files) {
1089 atomic_dec(&sbi->s_wro_seq_files);
1094 if (i_size_read(inode) < zi->i_max_size) {
1095 ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_OPEN);
1097 atomic_dec(&sbi->s_wro_seq_files);
1100 zi->i_flags |= ZONEFS_ZONE_OPEN;
1101 zonefs_account_active(inode);
1109 mutex_unlock(&zi->i_truncate_mutex);
1114 static int zonefs_file_open(struct inode *inode, struct file *file)
1118 ret = generic_file_open(inode, file);
1122 if (zonefs_seq_file_need_wro(inode, file))
1123 return zonefs_seq_file_write_open(inode);
1128 static void zonefs_seq_file_write_close(struct inode *inode)
1130 struct zonefs_inode_info *zi = ZONEFS_I(inode);
1131 struct super_block *sb = inode->i_sb;
1132 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1135 mutex_lock(&zi->i_truncate_mutex);
1138 if (zi->i_wr_refcnt)
1142 * The file zone may not be open anymore (e.g. the file was truncated to
1143 * its maximum size or it was fully written). For this case, we only
1144 * need to decrement the write open count.
1146 if (zi->i_flags & ZONEFS_ZONE_OPEN) {
1147 ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_CLOSE);
1149 __zonefs_io_error(inode, false);
1151 * Leaving zones explicitly open may lead to a state
1152 * where most zones cannot be written (zone resources
1153 * exhausted). So take preventive action by remounting
1156 if (zi->i_flags & ZONEFS_ZONE_OPEN &&
1157 !(sb->s_flags & SB_RDONLY)) {
1159 "closing zone at %llu failed %d\n",
1160 zi->i_zsector, ret);
1162 "remounting filesystem read-only\n");
1163 sb->s_flags |= SB_RDONLY;
1168 zi->i_flags &= ~ZONEFS_ZONE_OPEN;
1169 zonefs_account_active(inode);
1172 atomic_dec(&sbi->s_wro_seq_files);
1175 mutex_unlock(&zi->i_truncate_mutex);
1178 static int zonefs_file_release(struct inode *inode, struct file *file)
1181 * If we explicitly open a zone we must close it again as well, but the
1182 * zone management operation can fail (either due to an IO error or as
1183 * the zone has gone offline or read-only). Make sure we don't fail the
1184 * close(2) for user-space.
1186 if (zonefs_seq_file_need_wro(inode, file))
1187 zonefs_seq_file_write_close(inode);
1192 static const struct file_operations zonefs_file_operations = {
1193 .open = zonefs_file_open,
1194 .release = zonefs_file_release,
1195 .fsync = zonefs_file_fsync,
1196 .mmap = zonefs_file_mmap,
1197 .llseek = zonefs_file_llseek,
1198 .read_iter = zonefs_file_read_iter,
1199 .write_iter = zonefs_file_write_iter,
1200 .splice_read = generic_file_splice_read,
1201 .splice_write = iter_file_splice_write,
1202 .iopoll = iocb_bio_iopoll,
1205 static struct kmem_cache *zonefs_inode_cachep;
1207 static struct inode *zonefs_alloc_inode(struct super_block *sb)
1209 struct zonefs_inode_info *zi;
1211 zi = alloc_inode_sb(sb, zonefs_inode_cachep, GFP_KERNEL);
1215 inode_init_once(&zi->i_vnode);
1216 mutex_init(&zi->i_truncate_mutex);
1217 zi->i_wr_refcnt = 0;
1220 return &zi->i_vnode;
1223 static void zonefs_free_inode(struct inode *inode)
1225 kmem_cache_free(zonefs_inode_cachep, ZONEFS_I(inode));
1231 static int zonefs_statfs(struct dentry *dentry, struct kstatfs *buf)
1233 struct super_block *sb = dentry->d_sb;
1234 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1235 enum zonefs_ztype t;
1237 buf->f_type = ZONEFS_MAGIC;
1238 buf->f_bsize = sb->s_blocksize;
1239 buf->f_namelen = ZONEFS_NAME_MAX;
1241 spin_lock(&sbi->s_lock);
1243 buf->f_blocks = sbi->s_blocks;
1244 if (WARN_ON(sbi->s_used_blocks > sbi->s_blocks))
1247 buf->f_bfree = buf->f_blocks - sbi->s_used_blocks;
1248 buf->f_bavail = buf->f_bfree;
1250 for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) {
1251 if (sbi->s_nr_files[t])
1252 buf->f_files += sbi->s_nr_files[t] + 1;
1256 spin_unlock(&sbi->s_lock);
1258 buf->f_fsid = uuid_to_fsid(sbi->s_uuid.b);
1264 Opt_errors_ro, Opt_errors_zro, Opt_errors_zol, Opt_errors_repair,
1265 Opt_explicit_open, Opt_err,
1268 static const match_table_t tokens = {
1269 { Opt_errors_ro, "errors=remount-ro"},
1270 { Opt_errors_zro, "errors=zone-ro"},
1271 { Opt_errors_zol, "errors=zone-offline"},
1272 { Opt_errors_repair, "errors=repair"},
1273 { Opt_explicit_open, "explicit-open" },
1277 static int zonefs_parse_options(struct super_block *sb, char *options)
1279 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1280 substring_t args[MAX_OPT_ARGS];
1286 while ((p = strsep(&options, ",")) != NULL) {
1292 token = match_token(p, tokens, args);
1295 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
1296 sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_RO;
1298 case Opt_errors_zro:
1299 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
1300 sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZRO;
1302 case Opt_errors_zol:
1303 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
1304 sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZOL;
1306 case Opt_errors_repair:
1307 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
1308 sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_REPAIR;
1310 case Opt_explicit_open:
1311 sbi->s_mount_opts |= ZONEFS_MNTOPT_EXPLICIT_OPEN;
1321 static int zonefs_show_options(struct seq_file *seq, struct dentry *root)
1323 struct zonefs_sb_info *sbi = ZONEFS_SB(root->d_sb);
1325 if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO)
1326 seq_puts(seq, ",errors=remount-ro");
1327 if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO)
1328 seq_puts(seq, ",errors=zone-ro");
1329 if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL)
1330 seq_puts(seq, ",errors=zone-offline");
1331 if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_REPAIR)
1332 seq_puts(seq, ",errors=repair");
1337 static int zonefs_remount(struct super_block *sb, int *flags, char *data)
1339 sync_filesystem(sb);
1341 return zonefs_parse_options(sb, data);
1344 static const struct super_operations zonefs_sops = {
1345 .alloc_inode = zonefs_alloc_inode,
1346 .free_inode = zonefs_free_inode,
1347 .statfs = zonefs_statfs,
1348 .remount_fs = zonefs_remount,
1349 .show_options = zonefs_show_options,
1352 static const struct inode_operations zonefs_dir_inode_operations = {
1353 .lookup = simple_lookup,
1354 .setattr = zonefs_inode_setattr,
1357 static void zonefs_init_dir_inode(struct inode *parent, struct inode *inode,
1358 enum zonefs_ztype type)
1360 struct super_block *sb = parent->i_sb;
1362 inode->i_ino = blkdev_nr_zones(sb->s_bdev->bd_disk) + type + 1;
1363 inode_init_owner(&init_user_ns, inode, parent, S_IFDIR | 0555);
1364 inode->i_op = &zonefs_dir_inode_operations;
1365 inode->i_fop = &simple_dir_operations;
1366 set_nlink(inode, 2);
1370 static int zonefs_init_file_inode(struct inode *inode, struct blk_zone *zone,
1371 enum zonefs_ztype type)
1373 struct super_block *sb = inode->i_sb;
1374 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1375 struct zonefs_inode_info *zi = ZONEFS_I(inode);
1378 inode->i_ino = zone->start >> sbi->s_zone_sectors_shift;
1379 inode->i_mode = S_IFREG | sbi->s_perm;
1382 zi->i_zsector = zone->start;
1383 zi->i_zone_size = zone->len << SECTOR_SHIFT;
1385 zi->i_max_size = min_t(loff_t, MAX_LFS_FILESIZE,
1386 zone->capacity << SECTOR_SHIFT);
1387 zi->i_wpoffset = zonefs_check_zone_condition(inode, zone, true, true);
1389 inode->i_uid = sbi->s_uid;
1390 inode->i_gid = sbi->s_gid;
1391 inode->i_size = zi->i_wpoffset;
1392 inode->i_blocks = zi->i_max_size >> SECTOR_SHIFT;
1394 inode->i_op = &zonefs_file_inode_operations;
1395 inode->i_fop = &zonefs_file_operations;
1396 inode->i_mapping->a_ops = &zonefs_file_aops;
1398 sb->s_maxbytes = max(zi->i_max_size, sb->s_maxbytes);
1399 sbi->s_blocks += zi->i_max_size >> sb->s_blocksize_bits;
1400 sbi->s_used_blocks += zi->i_wpoffset >> sb->s_blocksize_bits;
1402 mutex_lock(&zi->i_truncate_mutex);
1405 * For sequential zones, make sure that any open zone is closed first
1406 * to ensure that the initial number of open zones is 0, in sync with
1407 * the open zone accounting done when the mount option
1408 * ZONEFS_MNTOPT_EXPLICIT_OPEN is used.
1410 if (type == ZONEFS_ZTYPE_SEQ &&
1411 (zone->cond == BLK_ZONE_COND_IMP_OPEN ||
1412 zone->cond == BLK_ZONE_COND_EXP_OPEN)) {
1413 ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_CLOSE);
1418 zonefs_account_active(inode);
1421 mutex_unlock(&zi->i_truncate_mutex);
1426 static struct dentry *zonefs_create_inode(struct dentry *parent,
1427 const char *name, struct blk_zone *zone,
1428 enum zonefs_ztype type)
1430 struct inode *dir = d_inode(parent);
1431 struct dentry *dentry;
1432 struct inode *inode;
1435 dentry = d_alloc_name(parent, name);
1439 inode = new_inode(parent->d_sb);
1443 inode->i_ctime = inode->i_mtime = inode->i_atime = dir->i_ctime;
1445 ret = zonefs_init_file_inode(inode, zone, type);
1451 zonefs_init_dir_inode(dir, inode, type);
1454 d_add(dentry, inode);
1465 struct zonefs_zone_data {
1466 struct super_block *sb;
1467 unsigned int nr_zones[ZONEFS_ZTYPE_MAX];
1468 struct blk_zone *zones;
1472 * Create a zone group and populate it with zone files.
1474 static int zonefs_create_zgroup(struct zonefs_zone_data *zd,
1475 enum zonefs_ztype type)
1477 struct super_block *sb = zd->sb;
1478 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1479 struct blk_zone *zone, *next, *end;
1480 const char *zgroup_name;
1486 /* If the group is empty, there is nothing to do */
1487 if (!zd->nr_zones[type])
1490 file_name = kmalloc(ZONEFS_NAME_MAX, GFP_KERNEL);
1494 if (type == ZONEFS_ZTYPE_CNV)
1495 zgroup_name = "cnv";
1497 zgroup_name = "seq";
1499 dir = zonefs_create_inode(sb->s_root, zgroup_name, NULL, type);
1506 * The first zone contains the super block: skip it.
1508 end = zd->zones + blkdev_nr_zones(sb->s_bdev->bd_disk);
1509 for (zone = &zd->zones[1]; zone < end; zone = next) {
1512 if (zonefs_zone_type(zone) != type)
1516 * For conventional zones, contiguous zones can be aggregated
1517 * together to form larger files. Note that this overwrites the
1518 * length of the first zone of the set of contiguous zones
1519 * aggregated together. If one offline or read-only zone is
1520 * found, assume that all zones aggregated have the same
1523 if (type == ZONEFS_ZTYPE_CNV &&
1524 (sbi->s_features & ZONEFS_F_AGGRCNV)) {
1525 for (; next < end; next++) {
1526 if (zonefs_zone_type(next) != type)
1528 zone->len += next->len;
1529 zone->capacity += next->capacity;
1530 if (next->cond == BLK_ZONE_COND_READONLY &&
1531 zone->cond != BLK_ZONE_COND_OFFLINE)
1532 zone->cond = BLK_ZONE_COND_READONLY;
1533 else if (next->cond == BLK_ZONE_COND_OFFLINE)
1534 zone->cond = BLK_ZONE_COND_OFFLINE;
1536 if (zone->capacity != zone->len) {
1537 zonefs_err(sb, "Invalid conventional zone capacity\n");
1544 * Use the file number within its group as file name.
1546 snprintf(file_name, ZONEFS_NAME_MAX - 1, "%u", n);
1547 if (!zonefs_create_inode(dir, file_name, zone, type)) {
1555 zonefs_info(sb, "Zone group \"%s\" has %u file%s\n",
1556 zgroup_name, n, n > 1 ? "s" : "");
1558 sbi->s_nr_files[type] = n;
1567 static int zonefs_get_zone_info_cb(struct blk_zone *zone, unsigned int idx,
1570 struct zonefs_zone_data *zd = data;
1573 * Count the number of usable zones: the first zone at index 0 contains
1574 * the super block and is ignored.
1576 switch (zone->type) {
1577 case BLK_ZONE_TYPE_CONVENTIONAL:
1578 zone->wp = zone->start + zone->len;
1580 zd->nr_zones[ZONEFS_ZTYPE_CNV]++;
1582 case BLK_ZONE_TYPE_SEQWRITE_REQ:
1583 case BLK_ZONE_TYPE_SEQWRITE_PREF:
1585 zd->nr_zones[ZONEFS_ZTYPE_SEQ]++;
1588 zonefs_err(zd->sb, "Unsupported zone type 0x%x\n",
1593 memcpy(&zd->zones[idx], zone, sizeof(struct blk_zone));
1598 static int zonefs_get_zone_info(struct zonefs_zone_data *zd)
1600 struct block_device *bdev = zd->sb->s_bdev;
1603 zd->zones = kvcalloc(blkdev_nr_zones(bdev->bd_disk),
1604 sizeof(struct blk_zone), GFP_KERNEL);
1608 /* Get zones information from the device */
1609 ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES,
1610 zonefs_get_zone_info_cb, zd);
1612 zonefs_err(zd->sb, "Zone report failed %d\n", ret);
1616 if (ret != blkdev_nr_zones(bdev->bd_disk)) {
1617 zonefs_err(zd->sb, "Invalid zone report (%d/%u zones)\n",
1618 ret, blkdev_nr_zones(bdev->bd_disk));
1625 static inline void zonefs_cleanup_zone_info(struct zonefs_zone_data *zd)
1631 * Read super block information from the device.
1633 static int zonefs_read_super(struct super_block *sb)
1635 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1636 struct zonefs_super *super;
1637 u32 crc, stored_crc;
1639 struct bio_vec bio_vec;
1643 page = alloc_page(GFP_KERNEL);
1647 bio_init(&bio, sb->s_bdev, &bio_vec, 1, REQ_OP_READ);
1648 bio.bi_iter.bi_sector = 0;
1649 bio_add_page(&bio, page, PAGE_SIZE, 0);
1651 ret = submit_bio_wait(&bio);
1658 if (le32_to_cpu(super->s_magic) != ZONEFS_MAGIC)
1661 stored_crc = le32_to_cpu(super->s_crc);
1663 crc = crc32(~0U, (unsigned char *)super, sizeof(struct zonefs_super));
1664 if (crc != stored_crc) {
1665 zonefs_err(sb, "Invalid checksum (Expected 0x%08x, got 0x%08x)",
1670 sbi->s_features = le64_to_cpu(super->s_features);
1671 if (sbi->s_features & ~ZONEFS_F_DEFINED_FEATURES) {
1672 zonefs_err(sb, "Unknown features set 0x%llx\n",
1677 if (sbi->s_features & ZONEFS_F_UID) {
1678 sbi->s_uid = make_kuid(current_user_ns(),
1679 le32_to_cpu(super->s_uid));
1680 if (!uid_valid(sbi->s_uid)) {
1681 zonefs_err(sb, "Invalid UID feature\n");
1686 if (sbi->s_features & ZONEFS_F_GID) {
1687 sbi->s_gid = make_kgid(current_user_ns(),
1688 le32_to_cpu(super->s_gid));
1689 if (!gid_valid(sbi->s_gid)) {
1690 zonefs_err(sb, "Invalid GID feature\n");
1695 if (sbi->s_features & ZONEFS_F_PERM)
1696 sbi->s_perm = le32_to_cpu(super->s_perm);
1698 if (memchr_inv(super->s_reserved, 0, sizeof(super->s_reserved))) {
1699 zonefs_err(sb, "Reserved area is being used\n");
1703 import_uuid(&sbi->s_uuid, super->s_uuid);
1715 * Check that the device is zoned. If it is, get the list of zones and create
1716 * sub-directories and files according to the device zone configuration and
1719 static int zonefs_fill_super(struct super_block *sb, void *data, int silent)
1721 struct zonefs_zone_data zd;
1722 struct zonefs_sb_info *sbi;
1723 struct inode *inode;
1724 enum zonefs_ztype t;
1727 if (!bdev_is_zoned(sb->s_bdev)) {
1728 zonefs_err(sb, "Not a zoned block device\n");
1733 * Initialize super block information: the maximum file size is updated
1734 * when the zone files are created so that the format option
1735 * ZONEFS_F_AGGRCNV which increases the maximum file size of a file
1736 * beyond the zone size is taken into account.
1738 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1742 spin_lock_init(&sbi->s_lock);
1743 sb->s_fs_info = sbi;
1744 sb->s_magic = ZONEFS_MAGIC;
1746 sb->s_op = &zonefs_sops;
1747 sb->s_time_gran = 1;
1750 * The block size is set to the device zone write granularity to ensure
1751 * that write operations are always aligned according to the device
1752 * interface constraints.
1754 sb_set_blocksize(sb, bdev_zone_write_granularity(sb->s_bdev));
1755 sbi->s_zone_sectors_shift = ilog2(bdev_zone_sectors(sb->s_bdev));
1756 sbi->s_uid = GLOBAL_ROOT_UID;
1757 sbi->s_gid = GLOBAL_ROOT_GID;
1759 sbi->s_mount_opts = ZONEFS_MNTOPT_ERRORS_RO;
1761 atomic_set(&sbi->s_wro_seq_files, 0);
1762 sbi->s_max_wro_seq_files = bdev_max_open_zones(sb->s_bdev);
1763 if (!sbi->s_max_wro_seq_files &&
1764 sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) {
1765 zonefs_info(sb, "No open zones limit. Ignoring explicit_open mount option\n");
1766 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_EXPLICIT_OPEN;
1769 atomic_set(&sbi->s_active_seq_files, 0);
1770 sbi->s_max_active_seq_files = bdev_max_active_zones(sb->s_bdev);
1772 ret = zonefs_read_super(sb);
1776 ret = zonefs_parse_options(sb, data);
1780 memset(&zd, 0, sizeof(struct zonefs_zone_data));
1782 ret = zonefs_get_zone_info(&zd);
1786 ret = zonefs_sysfs_register(sb);
1790 zonefs_info(sb, "Mounting %u zones",
1791 blkdev_nr_zones(sb->s_bdev->bd_disk));
1793 /* Create root directory inode */
1795 inode = new_inode(sb);
1799 inode->i_ino = blkdev_nr_zones(sb->s_bdev->bd_disk);
1800 inode->i_mode = S_IFDIR | 0555;
1801 inode->i_ctime = inode->i_mtime = inode->i_atime = current_time(inode);
1802 inode->i_op = &zonefs_dir_inode_operations;
1803 inode->i_fop = &simple_dir_operations;
1804 set_nlink(inode, 2);
1806 sb->s_root = d_make_root(inode);
1810 /* Create and populate files in zone groups directories */
1811 for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) {
1812 ret = zonefs_create_zgroup(&zd, t);
1818 zonefs_cleanup_zone_info(&zd);
1823 static struct dentry *zonefs_mount(struct file_system_type *fs_type,
1824 int flags, const char *dev_name, void *data)
1826 return mount_bdev(fs_type, flags, dev_name, data, zonefs_fill_super);
1829 static void zonefs_kill_super(struct super_block *sb)
1831 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1834 d_genocide(sb->s_root);
1836 zonefs_sysfs_unregister(sb);
1837 kill_block_super(sb);
1842 * File system definition and registration.
1844 static struct file_system_type zonefs_type = {
1845 .owner = THIS_MODULE,
1847 .mount = zonefs_mount,
1848 .kill_sb = zonefs_kill_super,
1849 .fs_flags = FS_REQUIRES_DEV,
1852 static int __init zonefs_init_inodecache(void)
1854 zonefs_inode_cachep = kmem_cache_create("zonefs_inode_cache",
1855 sizeof(struct zonefs_inode_info), 0,
1856 (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT),
1858 if (zonefs_inode_cachep == NULL)
1863 static void zonefs_destroy_inodecache(void)
1866 * Make sure all delayed rcu free inodes are flushed before we
1867 * destroy the inode cache.
1870 kmem_cache_destroy(zonefs_inode_cachep);
1873 static int __init zonefs_init(void)
1877 BUILD_BUG_ON(sizeof(struct zonefs_super) != ZONEFS_SUPER_SIZE);
1879 ret = zonefs_init_inodecache();
1883 ret = register_filesystem(&zonefs_type);
1885 goto destroy_inodecache;
1887 ret = zonefs_sysfs_init();
1894 unregister_filesystem(&zonefs_type);
1896 zonefs_destroy_inodecache();
1901 static void __exit zonefs_exit(void)
1903 zonefs_sysfs_exit();
1904 zonefs_destroy_inodecache();
1905 unregister_filesystem(&zonefs_type);
1908 MODULE_AUTHOR("Damien Le Moal");
1909 MODULE_DESCRIPTION("Zone file system for zoned block devices");
1910 MODULE_LICENSE("GPL");
1911 MODULE_ALIAS_FS("zonefs");
1912 module_init(zonefs_init);
1913 module_exit(zonefs_exit);