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_read_iomap_begin(struct inode *inode, loff_t offset,
114 loff_t length, unsigned int flags,
115 struct iomap *iomap, struct iomap *srcmap)
117 struct zonefs_inode_info *zi = ZONEFS_I(inode);
118 struct super_block *sb = inode->i_sb;
122 * All blocks are always mapped below EOF. If reading past EOF,
123 * act as if there is a hole up to the file maximum size.
125 mutex_lock(&zi->i_truncate_mutex);
126 iomap->bdev = inode->i_sb->s_bdev;
127 iomap->offset = ALIGN_DOWN(offset, sb->s_blocksize);
128 isize = i_size_read(inode);
129 if (iomap->offset >= isize) {
130 iomap->type = IOMAP_HOLE;
131 iomap->addr = IOMAP_NULL_ADDR;
132 iomap->length = length;
134 iomap->type = IOMAP_MAPPED;
135 iomap->addr = (zi->i_zsector << SECTOR_SHIFT) + iomap->offset;
136 iomap->length = isize - iomap->offset;
138 mutex_unlock(&zi->i_truncate_mutex);
140 trace_zonefs_iomap_begin(inode, iomap);
145 static const struct iomap_ops zonefs_read_iomap_ops = {
146 .iomap_begin = zonefs_read_iomap_begin,
149 static int zonefs_write_iomap_begin(struct inode *inode, loff_t offset,
150 loff_t length, unsigned int flags,
151 struct iomap *iomap, struct iomap *srcmap)
153 struct zonefs_inode_info *zi = ZONEFS_I(inode);
154 struct super_block *sb = inode->i_sb;
157 /* All write I/Os should always be within the file maximum size */
158 if (WARN_ON_ONCE(offset + length > zi->i_max_size))
162 * Sequential zones can only accept direct writes. This is already
163 * checked when writes are issued, so warn if we see a page writeback
166 if (WARN_ON_ONCE(zi->i_ztype == ZONEFS_ZTYPE_SEQ &&
167 !(flags & IOMAP_DIRECT)))
171 * For conventional zones, all blocks are always mapped. For sequential
172 * zones, all blocks after always mapped below the inode size (zone
173 * write pointer) and unwriten beyond.
175 mutex_lock(&zi->i_truncate_mutex);
176 iomap->bdev = inode->i_sb->s_bdev;
177 iomap->offset = ALIGN_DOWN(offset, sb->s_blocksize);
178 iomap->addr = (zi->i_zsector << SECTOR_SHIFT) + iomap->offset;
179 isize = i_size_read(inode);
180 if (iomap->offset >= isize) {
181 iomap->type = IOMAP_UNWRITTEN;
182 iomap->length = zi->i_max_size - iomap->offset;
184 iomap->type = IOMAP_MAPPED;
185 iomap->length = isize - iomap->offset;
187 mutex_unlock(&zi->i_truncate_mutex);
189 trace_zonefs_iomap_begin(inode, iomap);
194 static const struct iomap_ops zonefs_write_iomap_ops = {
195 .iomap_begin = zonefs_write_iomap_begin,
198 static int zonefs_read_folio(struct file *unused, struct folio *folio)
200 return iomap_read_folio(folio, &zonefs_read_iomap_ops);
203 static void zonefs_readahead(struct readahead_control *rac)
205 iomap_readahead(rac, &zonefs_read_iomap_ops);
209 * Map blocks for page writeback. This is used only on conventional zone files,
210 * which implies that the page range can only be within the fixed inode size.
212 static int zonefs_write_map_blocks(struct iomap_writepage_ctx *wpc,
213 struct inode *inode, loff_t offset)
215 struct zonefs_inode_info *zi = ZONEFS_I(inode);
217 if (WARN_ON_ONCE(zi->i_ztype != ZONEFS_ZTYPE_CNV))
219 if (WARN_ON_ONCE(offset >= i_size_read(inode)))
222 /* If the mapping is already OK, nothing needs to be done */
223 if (offset >= wpc->iomap.offset &&
224 offset < wpc->iomap.offset + wpc->iomap.length)
227 return zonefs_write_iomap_begin(inode, offset, zi->i_max_size - offset,
228 IOMAP_WRITE, &wpc->iomap, NULL);
231 static const struct iomap_writeback_ops zonefs_writeback_ops = {
232 .map_blocks = zonefs_write_map_blocks,
235 static int zonefs_writepage(struct page *page, struct writeback_control *wbc)
237 struct iomap_writepage_ctx wpc = { };
239 return iomap_writepage(page, wbc, &wpc, &zonefs_writeback_ops);
242 static int zonefs_writepages(struct address_space *mapping,
243 struct writeback_control *wbc)
245 struct iomap_writepage_ctx wpc = { };
247 return iomap_writepages(mapping, wbc, &wpc, &zonefs_writeback_ops);
250 static int zonefs_swap_activate(struct swap_info_struct *sis,
251 struct file *swap_file, sector_t *span)
253 struct inode *inode = file_inode(swap_file);
254 struct zonefs_inode_info *zi = ZONEFS_I(inode);
256 if (zi->i_ztype != ZONEFS_ZTYPE_CNV) {
257 zonefs_err(inode->i_sb,
258 "swap file: not a conventional zone file\n");
262 return iomap_swapfile_activate(sis, swap_file, span,
263 &zonefs_read_iomap_ops);
266 static const struct address_space_operations zonefs_file_aops = {
267 .read_folio = zonefs_read_folio,
268 .readahead = zonefs_readahead,
269 .writepage = zonefs_writepage,
270 .writepages = zonefs_writepages,
271 .dirty_folio = filemap_dirty_folio,
272 .release_folio = iomap_release_folio,
273 .invalidate_folio = iomap_invalidate_folio,
274 .migratepage = iomap_migrate_page,
275 .is_partially_uptodate = iomap_is_partially_uptodate,
276 .error_remove_page = generic_error_remove_page,
277 .direct_IO = noop_direct_IO,
278 .swap_activate = zonefs_swap_activate,
281 static void zonefs_update_stats(struct inode *inode, loff_t new_isize)
283 struct super_block *sb = inode->i_sb;
284 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
285 loff_t old_isize = i_size_read(inode);
288 if (new_isize == old_isize)
291 spin_lock(&sbi->s_lock);
294 * This may be called for an update after an IO error.
295 * So beware of the values seen.
297 if (new_isize < old_isize) {
298 nr_blocks = (old_isize - new_isize) >> sb->s_blocksize_bits;
299 if (sbi->s_used_blocks > nr_blocks)
300 sbi->s_used_blocks -= nr_blocks;
302 sbi->s_used_blocks = 0;
304 sbi->s_used_blocks +=
305 (new_isize - old_isize) >> sb->s_blocksize_bits;
306 if (sbi->s_used_blocks > sbi->s_blocks)
307 sbi->s_used_blocks = sbi->s_blocks;
310 spin_unlock(&sbi->s_lock);
314 * Check a zone condition and adjust its file inode access permissions for
315 * offline and readonly zones. Return the inode size corresponding to the
316 * amount of readable data in the zone.
318 static loff_t zonefs_check_zone_condition(struct inode *inode,
319 struct blk_zone *zone, bool warn,
322 struct zonefs_inode_info *zi = ZONEFS_I(inode);
324 switch (zone->cond) {
325 case BLK_ZONE_COND_OFFLINE:
327 * Dead zone: make the inode immutable, disable all accesses
328 * and set the file size to 0 (zone wp set to zone start).
331 zonefs_warn(inode->i_sb, "inode %lu: offline zone\n",
333 inode->i_flags |= S_IMMUTABLE;
334 inode->i_mode &= ~0777;
335 zone->wp = zone->start;
337 case BLK_ZONE_COND_READONLY:
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.
347 zonefs_warn(inode->i_sb, "inode %lu: read-only zone\n",
349 inode->i_flags |= S_IMMUTABLE;
351 zone->cond = BLK_ZONE_COND_OFFLINE;
352 inode->i_mode &= ~0777;
353 zone->wp = zone->start;
356 inode->i_mode &= ~0222;
357 return i_size_read(inode);
358 case BLK_ZONE_COND_FULL:
359 /* The write pointer of full zones is invalid. */
360 return zi->i_max_size;
362 if (zi->i_ztype == ZONEFS_ZTYPE_CNV)
363 return zi->i_max_size;
364 return (zone->wp - zone->start) << SECTOR_SHIFT;
368 struct zonefs_ioerr_data {
373 static int zonefs_io_error_cb(struct blk_zone *zone, unsigned int idx,
376 struct zonefs_ioerr_data *err = data;
377 struct inode *inode = err->inode;
378 struct zonefs_inode_info *zi = ZONEFS_I(inode);
379 struct super_block *sb = inode->i_sb;
380 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
381 loff_t isize, data_size;
384 * Check the zone condition: if the zone is not "bad" (offline or
385 * read-only), read errors are simply signaled to the IO issuer as long
386 * as there is no inconsistency between the inode size and the amount of
387 * data writen in the zone (data_size).
389 data_size = zonefs_check_zone_condition(inode, zone, true, false);
390 isize = i_size_read(inode);
391 if (zone->cond != BLK_ZONE_COND_OFFLINE &&
392 zone->cond != BLK_ZONE_COND_READONLY &&
393 !err->write && isize == data_size)
397 * At this point, we detected either a bad zone or an inconsistency
398 * between the inode size and the amount of data written in the zone.
399 * For the latter case, the cause may be a write IO error or an external
400 * action on the device. Two error patterns exist:
401 * 1) The inode size is lower than the amount of data in the zone:
402 * a write operation partially failed and data was writen at the end
403 * of the file. This can happen in the case of a large direct IO
404 * needing several BIOs and/or write requests to be processed.
405 * 2) The inode size is larger than the amount of data in the zone:
406 * this can happen with a deferred write error with the use of the
407 * device side write cache after getting successful write IO
408 * completions. Other possibilities are (a) an external corruption,
409 * e.g. an application reset the zone directly, or (b) the device
410 * has a serious problem (e.g. firmware bug).
412 * In all cases, warn about inode size inconsistency and handle the
413 * IO error according to the zone condition and to the mount options.
415 if (zi->i_ztype == ZONEFS_ZTYPE_SEQ && isize != data_size)
416 zonefs_warn(sb, "inode %lu: invalid size %lld (should be %lld)\n",
417 inode->i_ino, isize, data_size);
420 * First handle bad zones signaled by hardware. The mount options
421 * errors=zone-ro and errors=zone-offline result in changing the
422 * zone condition to read-only and offline respectively, as if the
423 * condition was signaled by the hardware.
425 if (zone->cond == BLK_ZONE_COND_OFFLINE ||
426 sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL) {
427 zonefs_warn(sb, "inode %lu: read/write access disabled\n",
429 if (zone->cond != BLK_ZONE_COND_OFFLINE) {
430 zone->cond = BLK_ZONE_COND_OFFLINE;
431 data_size = zonefs_check_zone_condition(inode, zone,
434 } else if (zone->cond == BLK_ZONE_COND_READONLY ||
435 sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO) {
436 zonefs_warn(sb, "inode %lu: write access disabled\n",
438 if (zone->cond != BLK_ZONE_COND_READONLY) {
439 zone->cond = BLK_ZONE_COND_READONLY;
440 data_size = zonefs_check_zone_condition(inode, zone,
446 * If the filesystem is mounted with the explicit-open mount option, we
447 * need to clear the ZONEFS_ZONE_OPEN flag if the zone transitioned to
448 * the read-only or offline condition, to avoid attempting an explicit
449 * close of the zone when the inode file is closed.
451 if ((sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) &&
452 (zone->cond == BLK_ZONE_COND_OFFLINE ||
453 zone->cond == BLK_ZONE_COND_READONLY))
454 zi->i_flags &= ~ZONEFS_ZONE_OPEN;
457 * If error=remount-ro was specified, any error result in remounting
458 * the volume as read-only.
460 if ((sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO) && !sb_rdonly(sb)) {
461 zonefs_warn(sb, "remounting filesystem read-only\n");
462 sb->s_flags |= SB_RDONLY;
466 * Update block usage stats and the inode size to prevent access to
469 zonefs_update_stats(inode, data_size);
470 zonefs_i_size_write(inode, data_size);
471 zi->i_wpoffset = data_size;
472 zonefs_account_active(inode);
478 * When an file IO error occurs, check the file zone to see if there is a change
479 * in the zone condition (e.g. offline or read-only). For a failed write to a
480 * sequential zone, the zone write pointer position must also be checked to
481 * eventually correct the file size and zonefs inode write pointer offset
482 * (which can be out of sync with the drive due to partial write failures).
484 static void __zonefs_io_error(struct inode *inode, bool write)
486 struct zonefs_inode_info *zi = ZONEFS_I(inode);
487 struct super_block *sb = inode->i_sb;
488 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
489 unsigned int noio_flag;
490 unsigned int nr_zones =
491 zi->i_zone_size >> (sbi->s_zone_sectors_shift + SECTOR_SHIFT);
492 struct zonefs_ioerr_data err = {
499 * Memory allocations in blkdev_report_zones() can trigger a memory
500 * reclaim which may in turn cause a recursion into zonefs as well as
501 * struct request allocations for the same device. The former case may
502 * end up in a deadlock on the inode truncate mutex, while the latter
503 * may prevent IO forward progress. Executing the report zones under
504 * the GFP_NOIO context avoids both problems.
506 noio_flag = memalloc_noio_save();
507 ret = blkdev_report_zones(sb->s_bdev, zi->i_zsector, nr_zones,
508 zonefs_io_error_cb, &err);
510 zonefs_err(sb, "Get inode %lu zone information failed %d\n",
512 memalloc_noio_restore(noio_flag);
515 static void zonefs_io_error(struct inode *inode, bool write)
517 struct zonefs_inode_info *zi = ZONEFS_I(inode);
519 mutex_lock(&zi->i_truncate_mutex);
520 __zonefs_io_error(inode, write);
521 mutex_unlock(&zi->i_truncate_mutex);
524 static int zonefs_file_truncate(struct inode *inode, loff_t isize)
526 struct zonefs_inode_info *zi = ZONEFS_I(inode);
532 * Only sequential zone files can be truncated and truncation is allowed
533 * only down to a 0 size, which is equivalent to a zone reset, and to
534 * the maximum file size, which is equivalent to a zone finish.
536 if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
540 op = REQ_OP_ZONE_RESET;
541 else if (isize == zi->i_max_size)
542 op = REQ_OP_ZONE_FINISH;
546 inode_dio_wait(inode);
548 /* Serialize against page faults */
549 filemap_invalidate_lock(inode->i_mapping);
551 /* Serialize against zonefs_iomap_begin() */
552 mutex_lock(&zi->i_truncate_mutex);
554 old_isize = i_size_read(inode);
555 if (isize == old_isize)
558 ret = zonefs_zone_mgmt(inode, op);
563 * If the mount option ZONEFS_MNTOPT_EXPLICIT_OPEN is set,
564 * take care of open zones.
566 if (zi->i_flags & ZONEFS_ZONE_OPEN) {
568 * Truncating a zone to EMPTY or FULL is the equivalent of
569 * closing the zone. For a truncation to 0, we need to
570 * re-open the zone to ensure new writes can be processed.
571 * For a truncation to the maximum file size, the zone is
572 * closed and writes cannot be accepted anymore, so clear
576 ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_OPEN);
578 zi->i_flags &= ~ZONEFS_ZONE_OPEN;
581 zonefs_update_stats(inode, isize);
582 truncate_setsize(inode, isize);
583 zi->i_wpoffset = isize;
584 zonefs_account_active(inode);
587 mutex_unlock(&zi->i_truncate_mutex);
588 filemap_invalidate_unlock(inode->i_mapping);
593 static int zonefs_inode_setattr(struct user_namespace *mnt_userns,
594 struct dentry *dentry, struct iattr *iattr)
596 struct inode *inode = d_inode(dentry);
599 if (unlikely(IS_IMMUTABLE(inode)))
602 ret = setattr_prepare(&init_user_ns, dentry, iattr);
607 * Since files and directories cannot be created nor deleted, do not
608 * allow setting any write attributes on the sub-directories grouping
609 * files by zone type.
611 if ((iattr->ia_valid & ATTR_MODE) && S_ISDIR(inode->i_mode) &&
612 (iattr->ia_mode & 0222))
615 if (((iattr->ia_valid & ATTR_UID) &&
616 !uid_eq(iattr->ia_uid, inode->i_uid)) ||
617 ((iattr->ia_valid & ATTR_GID) &&
618 !gid_eq(iattr->ia_gid, inode->i_gid))) {
619 ret = dquot_transfer(mnt_userns, inode, iattr);
624 if (iattr->ia_valid & ATTR_SIZE) {
625 ret = zonefs_file_truncate(inode, iattr->ia_size);
630 setattr_copy(&init_user_ns, inode, iattr);
635 static const struct inode_operations zonefs_file_inode_operations = {
636 .setattr = zonefs_inode_setattr,
639 static int zonefs_file_fsync(struct file *file, loff_t start, loff_t end,
642 struct inode *inode = file_inode(file);
645 if (unlikely(IS_IMMUTABLE(inode)))
649 * Since only direct writes are allowed in sequential files, page cache
650 * flush is needed only for conventional zone files.
652 if (ZONEFS_I(inode)->i_ztype == ZONEFS_ZTYPE_CNV)
653 ret = file_write_and_wait_range(file, start, end);
655 ret = blkdev_issue_flush(inode->i_sb->s_bdev);
658 zonefs_io_error(inode, true);
663 static vm_fault_t zonefs_filemap_page_mkwrite(struct vm_fault *vmf)
665 struct inode *inode = file_inode(vmf->vma->vm_file);
666 struct zonefs_inode_info *zi = ZONEFS_I(inode);
669 if (unlikely(IS_IMMUTABLE(inode)))
670 return VM_FAULT_SIGBUS;
673 * Sanity check: only conventional zone files can have shared
674 * writeable mappings.
676 if (WARN_ON_ONCE(zi->i_ztype != ZONEFS_ZTYPE_CNV))
677 return VM_FAULT_NOPAGE;
679 sb_start_pagefault(inode->i_sb);
680 file_update_time(vmf->vma->vm_file);
682 /* Serialize against truncates */
683 filemap_invalidate_lock_shared(inode->i_mapping);
684 ret = iomap_page_mkwrite(vmf, &zonefs_write_iomap_ops);
685 filemap_invalidate_unlock_shared(inode->i_mapping);
687 sb_end_pagefault(inode->i_sb);
691 static const struct vm_operations_struct zonefs_file_vm_ops = {
692 .fault = filemap_fault,
693 .map_pages = filemap_map_pages,
694 .page_mkwrite = zonefs_filemap_page_mkwrite,
697 static int zonefs_file_mmap(struct file *file, struct vm_area_struct *vma)
700 * Conventional zones accept random writes, so their files can support
701 * shared writable mappings. For sequential zone files, only read
702 * mappings are possible since there are no guarantees for write
703 * ordering between msync() and page cache writeback.
705 if (ZONEFS_I(file_inode(file))->i_ztype == ZONEFS_ZTYPE_SEQ &&
706 (vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
710 vma->vm_ops = &zonefs_file_vm_ops;
715 static loff_t zonefs_file_llseek(struct file *file, loff_t offset, int whence)
717 loff_t isize = i_size_read(file_inode(file));
720 * Seeks are limited to below the zone size for conventional zones
721 * and below the zone write pointer for sequential zones. In both
722 * cases, this limit is the inode size.
724 return generic_file_llseek_size(file, offset, whence, isize, isize);
727 static int zonefs_file_write_dio_end_io(struct kiocb *iocb, ssize_t size,
728 int error, unsigned int flags)
730 struct inode *inode = file_inode(iocb->ki_filp);
731 struct zonefs_inode_info *zi = ZONEFS_I(inode);
734 zonefs_io_error(inode, true);
738 if (size && zi->i_ztype != ZONEFS_ZTYPE_CNV) {
740 * Note that we may be seeing completions out of order,
741 * but that is not a problem since a write completed
742 * successfully necessarily means that all preceding writes
743 * were also successful. So we can safely increase the inode
744 * size to the write end location.
746 mutex_lock(&zi->i_truncate_mutex);
747 if (i_size_read(inode) < iocb->ki_pos + size) {
748 zonefs_update_stats(inode, iocb->ki_pos + size);
749 zonefs_i_size_write(inode, iocb->ki_pos + size);
751 mutex_unlock(&zi->i_truncate_mutex);
757 static const struct iomap_dio_ops zonefs_write_dio_ops = {
758 .end_io = zonefs_file_write_dio_end_io,
761 static ssize_t zonefs_file_dio_append(struct kiocb *iocb, struct iov_iter *from)
763 struct inode *inode = file_inode(iocb->ki_filp);
764 struct zonefs_inode_info *zi = ZONEFS_I(inode);
765 struct block_device *bdev = inode->i_sb->s_bdev;
766 unsigned int max = bdev_max_zone_append_sectors(bdev);
772 max = ALIGN_DOWN(max << SECTOR_SHIFT, inode->i_sb->s_blocksize);
773 iov_iter_truncate(from, max);
775 nr_pages = iov_iter_npages(from, BIO_MAX_VECS);
779 bio = bio_alloc(bdev, nr_pages,
780 REQ_OP_ZONE_APPEND | REQ_SYNC | REQ_IDLE, GFP_NOFS);
781 bio->bi_iter.bi_sector = zi->i_zsector;
782 bio->bi_ioprio = iocb->ki_ioprio;
783 if (iocb->ki_flags & IOCB_DSYNC)
784 bio->bi_opf |= REQ_FUA;
786 ret = bio_iov_iter_get_pages(bio, from);
790 size = bio->bi_iter.bi_size;
791 task_io_account_write(size);
793 if (iocb->ki_flags & IOCB_HIPRI)
794 bio_set_polled(bio, iocb);
796 ret = submit_bio_wait(bio);
798 zonefs_file_write_dio_end_io(iocb, size, ret, 0);
799 trace_zonefs_file_dio_append(inode, size, ret);
802 bio_release_pages(bio, false);
806 iocb->ki_pos += size;
814 * Do not exceed the LFS limits nor the file zone size. If pos is under the
815 * limit it becomes a short access. If it exceeds the limit, return -EFBIG.
817 static loff_t zonefs_write_check_limits(struct file *file, loff_t pos,
820 struct inode *inode = file_inode(file);
821 struct zonefs_inode_info *zi = ZONEFS_I(inode);
822 loff_t limit = rlimit(RLIMIT_FSIZE);
823 loff_t max_size = zi->i_max_size;
825 if (limit != RLIM_INFINITY) {
827 send_sig(SIGXFSZ, current, 0);
830 count = min(count, limit - pos);
833 if (!(file->f_flags & O_LARGEFILE))
834 max_size = min_t(loff_t, MAX_NON_LFS, max_size);
836 if (unlikely(pos >= max_size))
839 return min(count, max_size - pos);
842 static ssize_t zonefs_write_checks(struct kiocb *iocb, struct iov_iter *from)
844 struct file *file = iocb->ki_filp;
845 struct inode *inode = file_inode(file);
846 struct zonefs_inode_info *zi = ZONEFS_I(inode);
849 if (IS_SWAPFILE(inode))
852 if (!iov_iter_count(from))
855 if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
858 if (iocb->ki_flags & IOCB_APPEND) {
859 if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
861 mutex_lock(&zi->i_truncate_mutex);
862 iocb->ki_pos = zi->i_wpoffset;
863 mutex_unlock(&zi->i_truncate_mutex);
866 count = zonefs_write_check_limits(file, iocb->ki_pos,
867 iov_iter_count(from));
871 iov_iter_truncate(from, count);
872 return iov_iter_count(from);
876 * Handle direct writes. For sequential zone files, this is the only possible
877 * write path. For these files, check that the user is issuing writes
878 * sequentially from the end of the file. This code assumes that the block layer
879 * delivers write requests to the device in sequential order. This is always the
880 * case if a block IO scheduler implementing the ELEVATOR_F_ZBD_SEQ_WRITE
881 * elevator feature is being used (e.g. mq-deadline). The block layer always
882 * automatically select such an elevator for zoned block devices during the
883 * device initialization.
885 static ssize_t zonefs_file_dio_write(struct kiocb *iocb, struct iov_iter *from)
887 struct inode *inode = file_inode(iocb->ki_filp);
888 struct zonefs_inode_info *zi = ZONEFS_I(inode);
889 struct super_block *sb = inode->i_sb;
890 bool sync = is_sync_kiocb(iocb);
895 * For async direct IOs to sequential zone files, refuse IOCB_NOWAIT
896 * as this can cause write reordering (e.g. the first aio gets EAGAIN
897 * on the inode lock but the second goes through but is now unaligned).
899 if (zi->i_ztype == ZONEFS_ZTYPE_SEQ && !sync &&
900 (iocb->ki_flags & IOCB_NOWAIT))
903 if (iocb->ki_flags & IOCB_NOWAIT) {
904 if (!inode_trylock(inode))
910 count = zonefs_write_checks(iocb, from);
916 if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) {
921 /* Enforce sequential writes (append only) in sequential zones */
922 if (zi->i_ztype == ZONEFS_ZTYPE_SEQ) {
923 mutex_lock(&zi->i_truncate_mutex);
924 if (iocb->ki_pos != zi->i_wpoffset) {
925 mutex_unlock(&zi->i_truncate_mutex);
929 mutex_unlock(&zi->i_truncate_mutex);
934 ret = zonefs_file_dio_append(iocb, from);
936 ret = iomap_dio_rw(iocb, from, &zonefs_write_iomap_ops,
937 &zonefs_write_dio_ops, 0, NULL, 0);
938 if (zi->i_ztype == ZONEFS_ZTYPE_SEQ &&
939 (ret > 0 || ret == -EIOCBQUEUED)) {
944 * Update the zone write pointer offset assuming the write
945 * operation succeeded. If it did not, the error recovery path
946 * will correct it. Also do active seq file accounting.
948 mutex_lock(&zi->i_truncate_mutex);
949 zi->i_wpoffset += count;
950 zonefs_account_active(inode);
951 mutex_unlock(&zi->i_truncate_mutex);
960 static ssize_t zonefs_file_buffered_write(struct kiocb *iocb,
961 struct iov_iter *from)
963 struct inode *inode = file_inode(iocb->ki_filp);
964 struct zonefs_inode_info *zi = ZONEFS_I(inode);
968 * Direct IO writes are mandatory for sequential zone files so that the
969 * write IO issuing order is preserved.
971 if (zi->i_ztype != ZONEFS_ZTYPE_CNV)
974 if (iocb->ki_flags & IOCB_NOWAIT) {
975 if (!inode_trylock(inode))
981 ret = zonefs_write_checks(iocb, from);
985 ret = iomap_file_buffered_write(iocb, from, &zonefs_write_iomap_ops);
988 else if (ret == -EIO)
989 zonefs_io_error(inode, true);
994 ret = generic_write_sync(iocb, ret);
999 static ssize_t zonefs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1001 struct inode *inode = file_inode(iocb->ki_filp);
1003 if (unlikely(IS_IMMUTABLE(inode)))
1006 if (sb_rdonly(inode->i_sb))
1009 /* Write operations beyond the zone size are not allowed */
1010 if (iocb->ki_pos >= ZONEFS_I(inode)->i_max_size)
1013 if (iocb->ki_flags & IOCB_DIRECT) {
1014 ssize_t ret = zonefs_file_dio_write(iocb, from);
1015 if (ret != -ENOTBLK)
1019 return zonefs_file_buffered_write(iocb, from);
1022 static int zonefs_file_read_dio_end_io(struct kiocb *iocb, ssize_t size,
1023 int error, unsigned int flags)
1026 zonefs_io_error(file_inode(iocb->ki_filp), false);
1033 static const struct iomap_dio_ops zonefs_read_dio_ops = {
1034 .end_io = zonefs_file_read_dio_end_io,
1037 static ssize_t zonefs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1039 struct inode *inode = file_inode(iocb->ki_filp);
1040 struct zonefs_inode_info *zi = ZONEFS_I(inode);
1041 struct super_block *sb = inode->i_sb;
1045 /* Offline zones cannot be read */
1046 if (unlikely(IS_IMMUTABLE(inode) && !(inode->i_mode & 0777)))
1049 if (iocb->ki_pos >= zi->i_max_size)
1052 if (iocb->ki_flags & IOCB_NOWAIT) {
1053 if (!inode_trylock_shared(inode))
1056 inode_lock_shared(inode);
1059 /* Limit read operations to written data */
1060 mutex_lock(&zi->i_truncate_mutex);
1061 isize = i_size_read(inode);
1062 if (iocb->ki_pos >= isize) {
1063 mutex_unlock(&zi->i_truncate_mutex);
1067 iov_iter_truncate(to, isize - iocb->ki_pos);
1068 mutex_unlock(&zi->i_truncate_mutex);
1070 if (iocb->ki_flags & IOCB_DIRECT) {
1071 size_t count = iov_iter_count(to);
1073 if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) {
1077 file_accessed(iocb->ki_filp);
1078 ret = iomap_dio_rw(iocb, to, &zonefs_read_iomap_ops,
1079 &zonefs_read_dio_ops, 0, NULL, 0);
1081 ret = generic_file_read_iter(iocb, to);
1083 zonefs_io_error(inode, false);
1087 inode_unlock_shared(inode);
1093 * Write open accounting is done only for sequential files.
1095 static inline bool zonefs_seq_file_need_wro(struct inode *inode,
1098 struct zonefs_inode_info *zi = ZONEFS_I(inode);
1100 if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
1103 if (!(file->f_mode & FMODE_WRITE))
1109 static int zonefs_seq_file_write_open(struct inode *inode)
1111 struct zonefs_inode_info *zi = ZONEFS_I(inode);
1114 mutex_lock(&zi->i_truncate_mutex);
1116 if (!zi->i_wr_refcnt) {
1117 struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
1118 unsigned int wro = atomic_inc_return(&sbi->s_wro_seq_files);
1120 if (sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) {
1122 if (sbi->s_max_wro_seq_files
1123 && wro > sbi->s_max_wro_seq_files) {
1124 atomic_dec(&sbi->s_wro_seq_files);
1129 if (i_size_read(inode) < zi->i_max_size) {
1130 ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_OPEN);
1132 atomic_dec(&sbi->s_wro_seq_files);
1135 zi->i_flags |= ZONEFS_ZONE_OPEN;
1136 zonefs_account_active(inode);
1144 mutex_unlock(&zi->i_truncate_mutex);
1149 static int zonefs_file_open(struct inode *inode, struct file *file)
1153 ret = generic_file_open(inode, file);
1157 if (zonefs_seq_file_need_wro(inode, file))
1158 return zonefs_seq_file_write_open(inode);
1163 static void zonefs_seq_file_write_close(struct inode *inode)
1165 struct zonefs_inode_info *zi = ZONEFS_I(inode);
1166 struct super_block *sb = inode->i_sb;
1167 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1170 mutex_lock(&zi->i_truncate_mutex);
1173 if (zi->i_wr_refcnt)
1177 * The file zone may not be open anymore (e.g. the file was truncated to
1178 * its maximum size or it was fully written). For this case, we only
1179 * need to decrement the write open count.
1181 if (zi->i_flags & ZONEFS_ZONE_OPEN) {
1182 ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_CLOSE);
1184 __zonefs_io_error(inode, false);
1186 * Leaving zones explicitly open may lead to a state
1187 * where most zones cannot be written (zone resources
1188 * exhausted). So take preventive action by remounting
1191 if (zi->i_flags & ZONEFS_ZONE_OPEN &&
1192 !(sb->s_flags & SB_RDONLY)) {
1194 "closing zone at %llu failed %d\n",
1195 zi->i_zsector, ret);
1197 "remounting filesystem read-only\n");
1198 sb->s_flags |= SB_RDONLY;
1203 zi->i_flags &= ~ZONEFS_ZONE_OPEN;
1204 zonefs_account_active(inode);
1207 atomic_dec(&sbi->s_wro_seq_files);
1210 mutex_unlock(&zi->i_truncate_mutex);
1213 static int zonefs_file_release(struct inode *inode, struct file *file)
1216 * If we explicitly open a zone we must close it again as well, but the
1217 * zone management operation can fail (either due to an IO error or as
1218 * the zone has gone offline or read-only). Make sure we don't fail the
1219 * close(2) for user-space.
1221 if (zonefs_seq_file_need_wro(inode, file))
1222 zonefs_seq_file_write_close(inode);
1227 static const struct file_operations zonefs_file_operations = {
1228 .open = zonefs_file_open,
1229 .release = zonefs_file_release,
1230 .fsync = zonefs_file_fsync,
1231 .mmap = zonefs_file_mmap,
1232 .llseek = zonefs_file_llseek,
1233 .read_iter = zonefs_file_read_iter,
1234 .write_iter = zonefs_file_write_iter,
1235 .splice_read = generic_file_splice_read,
1236 .splice_write = iter_file_splice_write,
1237 .iopoll = iocb_bio_iopoll,
1240 static struct kmem_cache *zonefs_inode_cachep;
1242 static struct inode *zonefs_alloc_inode(struct super_block *sb)
1244 struct zonefs_inode_info *zi;
1246 zi = alloc_inode_sb(sb, zonefs_inode_cachep, GFP_KERNEL);
1250 inode_init_once(&zi->i_vnode);
1251 mutex_init(&zi->i_truncate_mutex);
1252 zi->i_wr_refcnt = 0;
1255 return &zi->i_vnode;
1258 static void zonefs_free_inode(struct inode *inode)
1260 kmem_cache_free(zonefs_inode_cachep, ZONEFS_I(inode));
1266 static int zonefs_statfs(struct dentry *dentry, struct kstatfs *buf)
1268 struct super_block *sb = dentry->d_sb;
1269 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1270 enum zonefs_ztype t;
1272 buf->f_type = ZONEFS_MAGIC;
1273 buf->f_bsize = sb->s_blocksize;
1274 buf->f_namelen = ZONEFS_NAME_MAX;
1276 spin_lock(&sbi->s_lock);
1278 buf->f_blocks = sbi->s_blocks;
1279 if (WARN_ON(sbi->s_used_blocks > sbi->s_blocks))
1282 buf->f_bfree = buf->f_blocks - sbi->s_used_blocks;
1283 buf->f_bavail = buf->f_bfree;
1285 for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) {
1286 if (sbi->s_nr_files[t])
1287 buf->f_files += sbi->s_nr_files[t] + 1;
1291 spin_unlock(&sbi->s_lock);
1293 buf->f_fsid = uuid_to_fsid(sbi->s_uuid.b);
1299 Opt_errors_ro, Opt_errors_zro, Opt_errors_zol, Opt_errors_repair,
1300 Opt_explicit_open, Opt_err,
1303 static const match_table_t tokens = {
1304 { Opt_errors_ro, "errors=remount-ro"},
1305 { Opt_errors_zro, "errors=zone-ro"},
1306 { Opt_errors_zol, "errors=zone-offline"},
1307 { Opt_errors_repair, "errors=repair"},
1308 { Opt_explicit_open, "explicit-open" },
1312 static int zonefs_parse_options(struct super_block *sb, char *options)
1314 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1315 substring_t args[MAX_OPT_ARGS];
1321 while ((p = strsep(&options, ",")) != NULL) {
1327 token = match_token(p, tokens, args);
1330 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
1331 sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_RO;
1333 case Opt_errors_zro:
1334 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
1335 sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZRO;
1337 case Opt_errors_zol:
1338 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
1339 sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZOL;
1341 case Opt_errors_repair:
1342 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
1343 sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_REPAIR;
1345 case Opt_explicit_open:
1346 sbi->s_mount_opts |= ZONEFS_MNTOPT_EXPLICIT_OPEN;
1356 static int zonefs_show_options(struct seq_file *seq, struct dentry *root)
1358 struct zonefs_sb_info *sbi = ZONEFS_SB(root->d_sb);
1360 if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO)
1361 seq_puts(seq, ",errors=remount-ro");
1362 if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO)
1363 seq_puts(seq, ",errors=zone-ro");
1364 if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL)
1365 seq_puts(seq, ",errors=zone-offline");
1366 if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_REPAIR)
1367 seq_puts(seq, ",errors=repair");
1372 static int zonefs_remount(struct super_block *sb, int *flags, char *data)
1374 sync_filesystem(sb);
1376 return zonefs_parse_options(sb, data);
1379 static const struct super_operations zonefs_sops = {
1380 .alloc_inode = zonefs_alloc_inode,
1381 .free_inode = zonefs_free_inode,
1382 .statfs = zonefs_statfs,
1383 .remount_fs = zonefs_remount,
1384 .show_options = zonefs_show_options,
1387 static const struct inode_operations zonefs_dir_inode_operations = {
1388 .lookup = simple_lookup,
1389 .setattr = zonefs_inode_setattr,
1392 static void zonefs_init_dir_inode(struct inode *parent, struct inode *inode,
1393 enum zonefs_ztype type)
1395 struct super_block *sb = parent->i_sb;
1397 inode->i_ino = blkdev_nr_zones(sb->s_bdev->bd_disk) + type + 1;
1398 inode_init_owner(&init_user_ns, inode, parent, S_IFDIR | 0555);
1399 inode->i_op = &zonefs_dir_inode_operations;
1400 inode->i_fop = &simple_dir_operations;
1401 set_nlink(inode, 2);
1405 static int zonefs_init_file_inode(struct inode *inode, struct blk_zone *zone,
1406 enum zonefs_ztype type)
1408 struct super_block *sb = inode->i_sb;
1409 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1410 struct zonefs_inode_info *zi = ZONEFS_I(inode);
1413 inode->i_ino = zone->start >> sbi->s_zone_sectors_shift;
1414 inode->i_mode = S_IFREG | sbi->s_perm;
1417 zi->i_zsector = zone->start;
1418 zi->i_zone_size = zone->len << SECTOR_SHIFT;
1420 zi->i_max_size = min_t(loff_t, MAX_LFS_FILESIZE,
1421 zone->capacity << SECTOR_SHIFT);
1422 zi->i_wpoffset = zonefs_check_zone_condition(inode, zone, true, true);
1424 inode->i_uid = sbi->s_uid;
1425 inode->i_gid = sbi->s_gid;
1426 inode->i_size = zi->i_wpoffset;
1427 inode->i_blocks = zi->i_max_size >> SECTOR_SHIFT;
1429 inode->i_op = &zonefs_file_inode_operations;
1430 inode->i_fop = &zonefs_file_operations;
1431 inode->i_mapping->a_ops = &zonefs_file_aops;
1433 sb->s_maxbytes = max(zi->i_max_size, sb->s_maxbytes);
1434 sbi->s_blocks += zi->i_max_size >> sb->s_blocksize_bits;
1435 sbi->s_used_blocks += zi->i_wpoffset >> sb->s_blocksize_bits;
1437 mutex_lock(&zi->i_truncate_mutex);
1440 * For sequential zones, make sure that any open zone is closed first
1441 * to ensure that the initial number of open zones is 0, in sync with
1442 * the open zone accounting done when the mount option
1443 * ZONEFS_MNTOPT_EXPLICIT_OPEN is used.
1445 if (type == ZONEFS_ZTYPE_SEQ &&
1446 (zone->cond == BLK_ZONE_COND_IMP_OPEN ||
1447 zone->cond == BLK_ZONE_COND_EXP_OPEN)) {
1448 ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_CLOSE);
1453 zonefs_account_active(inode);
1456 mutex_unlock(&zi->i_truncate_mutex);
1461 static struct dentry *zonefs_create_inode(struct dentry *parent,
1462 const char *name, struct blk_zone *zone,
1463 enum zonefs_ztype type)
1465 struct inode *dir = d_inode(parent);
1466 struct dentry *dentry;
1467 struct inode *inode;
1470 dentry = d_alloc_name(parent, name);
1474 inode = new_inode(parent->d_sb);
1478 inode->i_ctime = inode->i_mtime = inode->i_atime = dir->i_ctime;
1480 ret = zonefs_init_file_inode(inode, zone, type);
1486 zonefs_init_dir_inode(dir, inode, type);
1489 d_add(dentry, inode);
1500 struct zonefs_zone_data {
1501 struct super_block *sb;
1502 unsigned int nr_zones[ZONEFS_ZTYPE_MAX];
1503 struct blk_zone *zones;
1507 * Create a zone group and populate it with zone files.
1509 static int zonefs_create_zgroup(struct zonefs_zone_data *zd,
1510 enum zonefs_ztype type)
1512 struct super_block *sb = zd->sb;
1513 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1514 struct blk_zone *zone, *next, *end;
1515 const char *zgroup_name;
1521 /* If the group is empty, there is nothing to do */
1522 if (!zd->nr_zones[type])
1525 file_name = kmalloc(ZONEFS_NAME_MAX, GFP_KERNEL);
1529 if (type == ZONEFS_ZTYPE_CNV)
1530 zgroup_name = "cnv";
1532 zgroup_name = "seq";
1534 dir = zonefs_create_inode(sb->s_root, zgroup_name, NULL, type);
1541 * The first zone contains the super block: skip it.
1543 end = zd->zones + blkdev_nr_zones(sb->s_bdev->bd_disk);
1544 for (zone = &zd->zones[1]; zone < end; zone = next) {
1547 if (zonefs_zone_type(zone) != type)
1551 * For conventional zones, contiguous zones can be aggregated
1552 * together to form larger files. Note that this overwrites the
1553 * length of the first zone of the set of contiguous zones
1554 * aggregated together. If one offline or read-only zone is
1555 * found, assume that all zones aggregated have the same
1558 if (type == ZONEFS_ZTYPE_CNV &&
1559 (sbi->s_features & ZONEFS_F_AGGRCNV)) {
1560 for (; next < end; next++) {
1561 if (zonefs_zone_type(next) != type)
1563 zone->len += next->len;
1564 zone->capacity += next->capacity;
1565 if (next->cond == BLK_ZONE_COND_READONLY &&
1566 zone->cond != BLK_ZONE_COND_OFFLINE)
1567 zone->cond = BLK_ZONE_COND_READONLY;
1568 else if (next->cond == BLK_ZONE_COND_OFFLINE)
1569 zone->cond = BLK_ZONE_COND_OFFLINE;
1571 if (zone->capacity != zone->len) {
1572 zonefs_err(sb, "Invalid conventional zone capacity\n");
1579 * Use the file number within its group as file name.
1581 snprintf(file_name, ZONEFS_NAME_MAX - 1, "%u", n);
1582 if (!zonefs_create_inode(dir, file_name, zone, type)) {
1590 zonefs_info(sb, "Zone group \"%s\" has %u file%s\n",
1591 zgroup_name, n, n > 1 ? "s" : "");
1593 sbi->s_nr_files[type] = n;
1602 static int zonefs_get_zone_info_cb(struct blk_zone *zone, unsigned int idx,
1605 struct zonefs_zone_data *zd = data;
1608 * Count the number of usable zones: the first zone at index 0 contains
1609 * the super block and is ignored.
1611 switch (zone->type) {
1612 case BLK_ZONE_TYPE_CONVENTIONAL:
1613 zone->wp = zone->start + zone->len;
1615 zd->nr_zones[ZONEFS_ZTYPE_CNV]++;
1617 case BLK_ZONE_TYPE_SEQWRITE_REQ:
1618 case BLK_ZONE_TYPE_SEQWRITE_PREF:
1620 zd->nr_zones[ZONEFS_ZTYPE_SEQ]++;
1623 zonefs_err(zd->sb, "Unsupported zone type 0x%x\n",
1628 memcpy(&zd->zones[idx], zone, sizeof(struct blk_zone));
1633 static int zonefs_get_zone_info(struct zonefs_zone_data *zd)
1635 struct block_device *bdev = zd->sb->s_bdev;
1638 zd->zones = kvcalloc(blkdev_nr_zones(bdev->bd_disk),
1639 sizeof(struct blk_zone), GFP_KERNEL);
1643 /* Get zones information from the device */
1644 ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES,
1645 zonefs_get_zone_info_cb, zd);
1647 zonefs_err(zd->sb, "Zone report failed %d\n", ret);
1651 if (ret != blkdev_nr_zones(bdev->bd_disk)) {
1652 zonefs_err(zd->sb, "Invalid zone report (%d/%u zones)\n",
1653 ret, blkdev_nr_zones(bdev->bd_disk));
1660 static inline void zonefs_cleanup_zone_info(struct zonefs_zone_data *zd)
1666 * Read super block information from the device.
1668 static int zonefs_read_super(struct super_block *sb)
1670 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1671 struct zonefs_super *super;
1672 u32 crc, stored_crc;
1674 struct bio_vec bio_vec;
1678 page = alloc_page(GFP_KERNEL);
1682 bio_init(&bio, sb->s_bdev, &bio_vec, 1, REQ_OP_READ);
1683 bio.bi_iter.bi_sector = 0;
1684 bio_add_page(&bio, page, PAGE_SIZE, 0);
1686 ret = submit_bio_wait(&bio);
1693 if (le32_to_cpu(super->s_magic) != ZONEFS_MAGIC)
1696 stored_crc = le32_to_cpu(super->s_crc);
1698 crc = crc32(~0U, (unsigned char *)super, sizeof(struct zonefs_super));
1699 if (crc != stored_crc) {
1700 zonefs_err(sb, "Invalid checksum (Expected 0x%08x, got 0x%08x)",
1705 sbi->s_features = le64_to_cpu(super->s_features);
1706 if (sbi->s_features & ~ZONEFS_F_DEFINED_FEATURES) {
1707 zonefs_err(sb, "Unknown features set 0x%llx\n",
1712 if (sbi->s_features & ZONEFS_F_UID) {
1713 sbi->s_uid = make_kuid(current_user_ns(),
1714 le32_to_cpu(super->s_uid));
1715 if (!uid_valid(sbi->s_uid)) {
1716 zonefs_err(sb, "Invalid UID feature\n");
1721 if (sbi->s_features & ZONEFS_F_GID) {
1722 sbi->s_gid = make_kgid(current_user_ns(),
1723 le32_to_cpu(super->s_gid));
1724 if (!gid_valid(sbi->s_gid)) {
1725 zonefs_err(sb, "Invalid GID feature\n");
1730 if (sbi->s_features & ZONEFS_F_PERM)
1731 sbi->s_perm = le32_to_cpu(super->s_perm);
1733 if (memchr_inv(super->s_reserved, 0, sizeof(super->s_reserved))) {
1734 zonefs_err(sb, "Reserved area is being used\n");
1738 import_uuid(&sbi->s_uuid, super->s_uuid);
1750 * Check that the device is zoned. If it is, get the list of zones and create
1751 * sub-directories and files according to the device zone configuration and
1754 static int zonefs_fill_super(struct super_block *sb, void *data, int silent)
1756 struct zonefs_zone_data zd;
1757 struct zonefs_sb_info *sbi;
1758 struct inode *inode;
1759 enum zonefs_ztype t;
1762 if (!bdev_is_zoned(sb->s_bdev)) {
1763 zonefs_err(sb, "Not a zoned block device\n");
1768 * Initialize super block information: the maximum file size is updated
1769 * when the zone files are created so that the format option
1770 * ZONEFS_F_AGGRCNV which increases the maximum file size of a file
1771 * beyond the zone size is taken into account.
1773 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1777 spin_lock_init(&sbi->s_lock);
1778 sb->s_fs_info = sbi;
1779 sb->s_magic = ZONEFS_MAGIC;
1781 sb->s_op = &zonefs_sops;
1782 sb->s_time_gran = 1;
1785 * The block size is set to the device zone write granularity to ensure
1786 * that write operations are always aligned according to the device
1787 * interface constraints.
1789 sb_set_blocksize(sb, bdev_zone_write_granularity(sb->s_bdev));
1790 sbi->s_zone_sectors_shift = ilog2(bdev_zone_sectors(sb->s_bdev));
1791 sbi->s_uid = GLOBAL_ROOT_UID;
1792 sbi->s_gid = GLOBAL_ROOT_GID;
1794 sbi->s_mount_opts = ZONEFS_MNTOPT_ERRORS_RO;
1796 atomic_set(&sbi->s_wro_seq_files, 0);
1797 sbi->s_max_wro_seq_files = bdev_max_open_zones(sb->s_bdev);
1798 atomic_set(&sbi->s_active_seq_files, 0);
1799 sbi->s_max_active_seq_files = bdev_max_active_zones(sb->s_bdev);
1801 ret = zonefs_read_super(sb);
1805 ret = zonefs_parse_options(sb, data);
1809 memset(&zd, 0, sizeof(struct zonefs_zone_data));
1811 ret = zonefs_get_zone_info(&zd);
1815 ret = zonefs_sysfs_register(sb);
1819 zonefs_info(sb, "Mounting %u zones",
1820 blkdev_nr_zones(sb->s_bdev->bd_disk));
1822 if (!sbi->s_max_wro_seq_files &&
1823 !sbi->s_max_active_seq_files &&
1824 sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) {
1826 "No open and active zone limits. Ignoring explicit_open mount option\n");
1827 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_EXPLICIT_OPEN;
1830 /* Create root directory inode */
1832 inode = new_inode(sb);
1836 inode->i_ino = blkdev_nr_zones(sb->s_bdev->bd_disk);
1837 inode->i_mode = S_IFDIR | 0555;
1838 inode->i_ctime = inode->i_mtime = inode->i_atime = current_time(inode);
1839 inode->i_op = &zonefs_dir_inode_operations;
1840 inode->i_fop = &simple_dir_operations;
1841 set_nlink(inode, 2);
1843 sb->s_root = d_make_root(inode);
1847 /* Create and populate files in zone groups directories */
1848 for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) {
1849 ret = zonefs_create_zgroup(&zd, t);
1855 zonefs_cleanup_zone_info(&zd);
1860 static struct dentry *zonefs_mount(struct file_system_type *fs_type,
1861 int flags, const char *dev_name, void *data)
1863 return mount_bdev(fs_type, flags, dev_name, data, zonefs_fill_super);
1866 static void zonefs_kill_super(struct super_block *sb)
1868 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1871 d_genocide(sb->s_root);
1873 zonefs_sysfs_unregister(sb);
1874 kill_block_super(sb);
1879 * File system definition and registration.
1881 static struct file_system_type zonefs_type = {
1882 .owner = THIS_MODULE,
1884 .mount = zonefs_mount,
1885 .kill_sb = zonefs_kill_super,
1886 .fs_flags = FS_REQUIRES_DEV,
1889 static int __init zonefs_init_inodecache(void)
1891 zonefs_inode_cachep = kmem_cache_create("zonefs_inode_cache",
1892 sizeof(struct zonefs_inode_info), 0,
1893 (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT),
1895 if (zonefs_inode_cachep == NULL)
1900 static void zonefs_destroy_inodecache(void)
1903 * Make sure all delayed rcu free inodes are flushed before we
1904 * destroy the inode cache.
1907 kmem_cache_destroy(zonefs_inode_cachep);
1910 static int __init zonefs_init(void)
1914 BUILD_BUG_ON(sizeof(struct zonefs_super) != ZONEFS_SUPER_SIZE);
1916 ret = zonefs_init_inodecache();
1920 ret = register_filesystem(&zonefs_type);
1922 goto destroy_inodecache;
1924 ret = zonefs_sysfs_init();
1931 unregister_filesystem(&zonefs_type);
1933 zonefs_destroy_inodecache();
1938 static void __exit zonefs_exit(void)
1940 zonefs_sysfs_exit();
1941 zonefs_destroy_inodecache();
1942 unregister_filesystem(&zonefs_type);
1945 MODULE_AUTHOR("Damien Le Moal");
1946 MODULE_DESCRIPTION("Zone file system for zoned block devices");
1947 MODULE_LICENSE("GPL");
1948 MODULE_ALIAS_FS("zonefs");
1949 module_init(zonefs_init);
1950 module_exit(zonefs_exit);