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>
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 static inline int zonefs_zone_mgmt(struct inode *inode,
30 struct zonefs_inode_info *zi = ZONEFS_I(inode);
33 lockdep_assert_held(&zi->i_truncate_mutex);
35 ret = blkdev_zone_mgmt(inode->i_sb->s_bdev, op, zi->i_zsector,
36 zi->i_zone_size >> SECTOR_SHIFT, GFP_NOFS);
38 zonefs_err(inode->i_sb,
39 "Zone management operation %s at %llu failed %d\n",
40 blk_op_str(op), zi->i_zsector, ret);
47 static inline void zonefs_i_size_write(struct inode *inode, loff_t isize)
49 struct zonefs_inode_info *zi = ZONEFS_I(inode);
51 i_size_write(inode, isize);
53 * A full zone is no longer open/active and does not need
56 if (isize >= zi->i_max_size)
57 zi->i_flags &= ~ZONEFS_ZONE_OPEN;
60 static int zonefs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
61 unsigned int flags, struct iomap *iomap,
64 struct zonefs_inode_info *zi = ZONEFS_I(inode);
65 struct super_block *sb = inode->i_sb;
68 /* All I/Os should always be within the file maximum size */
69 if (WARN_ON_ONCE(offset + length > zi->i_max_size))
73 * Sequential zones can only accept direct writes. This is already
74 * checked when writes are issued, so warn if we see a page writeback
77 if (WARN_ON_ONCE(zi->i_ztype == ZONEFS_ZTYPE_SEQ &&
78 (flags & IOMAP_WRITE) && !(flags & IOMAP_DIRECT)))
82 * For conventional zones, all blocks are always mapped. For sequential
83 * zones, all blocks after always mapped below the inode size (zone
84 * write pointer) and unwriten beyond.
86 mutex_lock(&zi->i_truncate_mutex);
87 isize = i_size_read(inode);
89 iomap->type = IOMAP_UNWRITTEN;
91 iomap->type = IOMAP_MAPPED;
92 if (flags & IOMAP_WRITE)
93 length = zi->i_max_size - offset;
95 length = min(length, isize - offset);
96 mutex_unlock(&zi->i_truncate_mutex);
98 iomap->offset = ALIGN_DOWN(offset, sb->s_blocksize);
99 iomap->length = ALIGN(offset + length, sb->s_blocksize) - iomap->offset;
100 iomap->bdev = inode->i_sb->s_bdev;
101 iomap->addr = (zi->i_zsector << SECTOR_SHIFT) + iomap->offset;
106 static const struct iomap_ops zonefs_iomap_ops = {
107 .iomap_begin = zonefs_iomap_begin,
110 static int zonefs_readpage(struct file *unused, struct page *page)
112 return iomap_readpage(page, &zonefs_iomap_ops);
115 static void zonefs_readahead(struct readahead_control *rac)
117 iomap_readahead(rac, &zonefs_iomap_ops);
121 * Map blocks for page writeback. This is used only on conventional zone files,
122 * which implies that the page range can only be within the fixed inode size.
124 static int zonefs_map_blocks(struct iomap_writepage_ctx *wpc,
125 struct inode *inode, loff_t offset)
127 struct zonefs_inode_info *zi = ZONEFS_I(inode);
129 if (WARN_ON_ONCE(zi->i_ztype != ZONEFS_ZTYPE_CNV))
131 if (WARN_ON_ONCE(offset >= i_size_read(inode)))
134 /* If the mapping is already OK, nothing needs to be done */
135 if (offset >= wpc->iomap.offset &&
136 offset < wpc->iomap.offset + wpc->iomap.length)
139 return zonefs_iomap_begin(inode, offset, zi->i_max_size - offset,
140 IOMAP_WRITE, &wpc->iomap, NULL);
143 static const struct iomap_writeback_ops zonefs_writeback_ops = {
144 .map_blocks = zonefs_map_blocks,
147 static int zonefs_writepage(struct page *page, struct writeback_control *wbc)
149 struct iomap_writepage_ctx wpc = { };
151 return iomap_writepage(page, wbc, &wpc, &zonefs_writeback_ops);
154 static int zonefs_writepages(struct address_space *mapping,
155 struct writeback_control *wbc)
157 struct iomap_writepage_ctx wpc = { };
159 return iomap_writepages(mapping, wbc, &wpc, &zonefs_writeback_ops);
162 static const struct address_space_operations zonefs_file_aops = {
163 .readpage = zonefs_readpage,
164 .readahead = zonefs_readahead,
165 .writepage = zonefs_writepage,
166 .writepages = zonefs_writepages,
167 .set_page_dirty = iomap_set_page_dirty,
168 .releasepage = iomap_releasepage,
169 .invalidatepage = iomap_invalidatepage,
170 .migratepage = iomap_migrate_page,
171 .is_partially_uptodate = iomap_is_partially_uptodate,
172 .error_remove_page = generic_error_remove_page,
173 .direct_IO = noop_direct_IO,
176 static void zonefs_update_stats(struct inode *inode, loff_t new_isize)
178 struct super_block *sb = inode->i_sb;
179 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
180 loff_t old_isize = i_size_read(inode);
183 if (new_isize == old_isize)
186 spin_lock(&sbi->s_lock);
189 * This may be called for an update after an IO error.
190 * So beware of the values seen.
192 if (new_isize < old_isize) {
193 nr_blocks = (old_isize - new_isize) >> sb->s_blocksize_bits;
194 if (sbi->s_used_blocks > nr_blocks)
195 sbi->s_used_blocks -= nr_blocks;
197 sbi->s_used_blocks = 0;
199 sbi->s_used_blocks +=
200 (new_isize - old_isize) >> sb->s_blocksize_bits;
201 if (sbi->s_used_blocks > sbi->s_blocks)
202 sbi->s_used_blocks = sbi->s_blocks;
205 spin_unlock(&sbi->s_lock);
209 * Check a zone condition and adjust its file inode access permissions for
210 * offline and readonly zones. Return the inode size corresponding to the
211 * amount of readable data in the zone.
213 static loff_t zonefs_check_zone_condition(struct inode *inode,
214 struct blk_zone *zone, bool warn,
217 struct zonefs_inode_info *zi = ZONEFS_I(inode);
219 switch (zone->cond) {
220 case BLK_ZONE_COND_OFFLINE:
222 * Dead zone: make the inode immutable, disable all accesses
223 * and set the file size to 0 (zone wp set to zone start).
226 zonefs_warn(inode->i_sb, "inode %lu: offline zone\n",
228 inode->i_flags |= S_IMMUTABLE;
229 inode->i_mode &= ~0777;
230 zone->wp = zone->start;
232 case BLK_ZONE_COND_READONLY:
234 * The write pointer of read-only zones is invalid. If such a
235 * zone is found during mount, the file size cannot be retrieved
236 * so we treat the zone as offline (mount == true case).
237 * Otherwise, keep the file size as it was when last updated
238 * so that the user can recover data. In both cases, writes are
239 * always disabled for the zone.
242 zonefs_warn(inode->i_sb, "inode %lu: read-only zone\n",
244 inode->i_flags |= S_IMMUTABLE;
246 zone->cond = BLK_ZONE_COND_OFFLINE;
247 inode->i_mode &= ~0777;
248 zone->wp = zone->start;
251 inode->i_mode &= ~0222;
252 return i_size_read(inode);
254 if (zi->i_ztype == ZONEFS_ZTYPE_CNV)
255 return zi->i_max_size;
256 return (zone->wp - zone->start) << SECTOR_SHIFT;
260 struct zonefs_ioerr_data {
265 static int zonefs_io_error_cb(struct blk_zone *zone, unsigned int idx,
268 struct zonefs_ioerr_data *err = data;
269 struct inode *inode = err->inode;
270 struct zonefs_inode_info *zi = ZONEFS_I(inode);
271 struct super_block *sb = inode->i_sb;
272 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
273 loff_t isize, data_size;
276 * Check the zone condition: if the zone is not "bad" (offline or
277 * read-only), read errors are simply signaled to the IO issuer as long
278 * as there is no inconsistency between the inode size and the amount of
279 * data writen in the zone (data_size).
281 data_size = zonefs_check_zone_condition(inode, zone, true, false);
282 isize = i_size_read(inode);
283 if (zone->cond != BLK_ZONE_COND_OFFLINE &&
284 zone->cond != BLK_ZONE_COND_READONLY &&
285 !err->write && isize == data_size)
289 * At this point, we detected either a bad zone or an inconsistency
290 * between the inode size and the amount of data written in the zone.
291 * For the latter case, the cause may be a write IO error or an external
292 * action on the device. Two error patterns exist:
293 * 1) The inode size is lower than the amount of data in the zone:
294 * a write operation partially failed and data was writen at the end
295 * of the file. This can happen in the case of a large direct IO
296 * needing several BIOs and/or write requests to be processed.
297 * 2) The inode size is larger than the amount of data in the zone:
298 * this can happen with a deferred write error with the use of the
299 * device side write cache after getting successful write IO
300 * completions. Other possibilities are (a) an external corruption,
301 * e.g. an application reset the zone directly, or (b) the device
302 * has a serious problem (e.g. firmware bug).
304 * In all cases, warn about inode size inconsistency and handle the
305 * IO error according to the zone condition and to the mount options.
307 if (zi->i_ztype == ZONEFS_ZTYPE_SEQ && isize != data_size)
308 zonefs_warn(sb, "inode %lu: invalid size %lld (should be %lld)\n",
309 inode->i_ino, isize, data_size);
312 * First handle bad zones signaled by hardware. The mount options
313 * errors=zone-ro and errors=zone-offline result in changing the
314 * zone condition to read-only and offline respectively, as if the
315 * condition was signaled by the hardware.
317 if (zone->cond == BLK_ZONE_COND_OFFLINE ||
318 sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL) {
319 zonefs_warn(sb, "inode %lu: read/write access disabled\n",
321 if (zone->cond != BLK_ZONE_COND_OFFLINE) {
322 zone->cond = BLK_ZONE_COND_OFFLINE;
323 data_size = zonefs_check_zone_condition(inode, zone,
326 } else if (zone->cond == BLK_ZONE_COND_READONLY ||
327 sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO) {
328 zonefs_warn(sb, "inode %lu: write access disabled\n",
330 if (zone->cond != BLK_ZONE_COND_READONLY) {
331 zone->cond = BLK_ZONE_COND_READONLY;
332 data_size = zonefs_check_zone_condition(inode, zone,
338 * If the filesystem is mounted with the explicit-open mount option, we
339 * need to clear the ZONEFS_ZONE_OPEN flag if the zone transitioned to
340 * the read-only or offline condition, to avoid attempting an explicit
341 * close of the zone when the inode file is closed.
343 if ((sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) &&
344 (zone->cond == BLK_ZONE_COND_OFFLINE ||
345 zone->cond == BLK_ZONE_COND_READONLY))
346 zi->i_flags &= ~ZONEFS_ZONE_OPEN;
349 * If error=remount-ro was specified, any error result in remounting
350 * the volume as read-only.
352 if ((sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO) && !sb_rdonly(sb)) {
353 zonefs_warn(sb, "remounting filesystem read-only\n");
354 sb->s_flags |= SB_RDONLY;
358 * Update block usage stats and the inode size to prevent access to
361 zonefs_update_stats(inode, data_size);
362 zonefs_i_size_write(inode, data_size);
363 zi->i_wpoffset = data_size;
369 * When an file IO error occurs, check the file zone to see if there is a change
370 * in the zone condition (e.g. offline or read-only). For a failed write to a
371 * sequential zone, the zone write pointer position must also be checked to
372 * eventually correct the file size and zonefs inode write pointer offset
373 * (which can be out of sync with the drive due to partial write failures).
375 static void __zonefs_io_error(struct inode *inode, bool write)
377 struct zonefs_inode_info *zi = ZONEFS_I(inode);
378 struct super_block *sb = inode->i_sb;
379 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
380 unsigned int noio_flag;
381 unsigned int nr_zones =
382 zi->i_zone_size >> (sbi->s_zone_sectors_shift + SECTOR_SHIFT);
383 struct zonefs_ioerr_data err = {
390 * Memory allocations in blkdev_report_zones() can trigger a memory
391 * reclaim which may in turn cause a recursion into zonefs as well as
392 * struct request allocations for the same device. The former case may
393 * end up in a deadlock on the inode truncate mutex, while the latter
394 * may prevent IO forward progress. Executing the report zones under
395 * the GFP_NOIO context avoids both problems.
397 noio_flag = memalloc_noio_save();
398 ret = blkdev_report_zones(sb->s_bdev, zi->i_zsector, nr_zones,
399 zonefs_io_error_cb, &err);
401 zonefs_err(sb, "Get inode %lu zone information failed %d\n",
403 memalloc_noio_restore(noio_flag);
406 static void zonefs_io_error(struct inode *inode, bool write)
408 struct zonefs_inode_info *zi = ZONEFS_I(inode);
410 mutex_lock(&zi->i_truncate_mutex);
411 __zonefs_io_error(inode, write);
412 mutex_unlock(&zi->i_truncate_mutex);
415 static int zonefs_file_truncate(struct inode *inode, loff_t isize)
417 struct zonefs_inode_info *zi = ZONEFS_I(inode);
423 * Only sequential zone files can be truncated and truncation is allowed
424 * only down to a 0 size, which is equivalent to a zone reset, and to
425 * the maximum file size, which is equivalent to a zone finish.
427 if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
431 op = REQ_OP_ZONE_RESET;
432 else if (isize == zi->i_max_size)
433 op = REQ_OP_ZONE_FINISH;
437 inode_dio_wait(inode);
439 /* Serialize against page faults */
440 down_write(&zi->i_mmap_sem);
442 /* Serialize against zonefs_iomap_begin() */
443 mutex_lock(&zi->i_truncate_mutex);
445 old_isize = i_size_read(inode);
446 if (isize == old_isize)
449 ret = zonefs_zone_mgmt(inode, op);
454 * If the mount option ZONEFS_MNTOPT_EXPLICIT_OPEN is set,
455 * take care of open zones.
457 if (zi->i_flags & ZONEFS_ZONE_OPEN) {
459 * Truncating a zone to EMPTY or FULL is the equivalent of
460 * closing the zone. For a truncation to 0, we need to
461 * re-open the zone to ensure new writes can be processed.
462 * For a truncation to the maximum file size, the zone is
463 * closed and writes cannot be accepted anymore, so clear
467 ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_OPEN);
469 zi->i_flags &= ~ZONEFS_ZONE_OPEN;
472 zonefs_update_stats(inode, isize);
473 truncate_setsize(inode, isize);
474 zi->i_wpoffset = isize;
477 mutex_unlock(&zi->i_truncate_mutex);
478 up_write(&zi->i_mmap_sem);
483 static int zonefs_inode_setattr(struct user_namespace *mnt_userns,
484 struct dentry *dentry, struct iattr *iattr)
486 struct inode *inode = d_inode(dentry);
489 if (unlikely(IS_IMMUTABLE(inode)))
492 ret = setattr_prepare(&init_user_ns, dentry, iattr);
497 * Since files and directories cannot be created nor deleted, do not
498 * allow setting any write attributes on the sub-directories grouping
499 * files by zone type.
501 if ((iattr->ia_valid & ATTR_MODE) && S_ISDIR(inode->i_mode) &&
502 (iattr->ia_mode & 0222))
505 if (((iattr->ia_valid & ATTR_UID) &&
506 !uid_eq(iattr->ia_uid, inode->i_uid)) ||
507 ((iattr->ia_valid & ATTR_GID) &&
508 !gid_eq(iattr->ia_gid, inode->i_gid))) {
509 ret = dquot_transfer(inode, iattr);
514 if (iattr->ia_valid & ATTR_SIZE) {
515 ret = zonefs_file_truncate(inode, iattr->ia_size);
520 setattr_copy(&init_user_ns, inode, iattr);
525 static const struct inode_operations zonefs_file_inode_operations = {
526 .setattr = zonefs_inode_setattr,
529 static int zonefs_file_fsync(struct file *file, loff_t start, loff_t end,
532 struct inode *inode = file_inode(file);
535 if (unlikely(IS_IMMUTABLE(inode)))
539 * Since only direct writes are allowed in sequential files, page cache
540 * flush is needed only for conventional zone files.
542 if (ZONEFS_I(inode)->i_ztype == ZONEFS_ZTYPE_CNV)
543 ret = file_write_and_wait_range(file, start, end);
545 ret = blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL);
548 zonefs_io_error(inode, true);
553 static vm_fault_t zonefs_filemap_fault(struct vm_fault *vmf)
555 struct zonefs_inode_info *zi = ZONEFS_I(file_inode(vmf->vma->vm_file));
558 down_read(&zi->i_mmap_sem);
559 ret = filemap_fault(vmf);
560 up_read(&zi->i_mmap_sem);
565 static vm_fault_t zonefs_filemap_page_mkwrite(struct vm_fault *vmf)
567 struct inode *inode = file_inode(vmf->vma->vm_file);
568 struct zonefs_inode_info *zi = ZONEFS_I(inode);
571 if (unlikely(IS_IMMUTABLE(inode)))
572 return VM_FAULT_SIGBUS;
575 * Sanity check: only conventional zone files can have shared
576 * writeable mappings.
578 if (WARN_ON_ONCE(zi->i_ztype != ZONEFS_ZTYPE_CNV))
579 return VM_FAULT_NOPAGE;
581 sb_start_pagefault(inode->i_sb);
582 file_update_time(vmf->vma->vm_file);
584 /* Serialize against truncates */
585 down_read(&zi->i_mmap_sem);
586 ret = iomap_page_mkwrite(vmf, &zonefs_iomap_ops);
587 up_read(&zi->i_mmap_sem);
589 sb_end_pagefault(inode->i_sb);
593 static const struct vm_operations_struct zonefs_file_vm_ops = {
594 .fault = zonefs_filemap_fault,
595 .map_pages = filemap_map_pages,
596 .page_mkwrite = zonefs_filemap_page_mkwrite,
599 static int zonefs_file_mmap(struct file *file, struct vm_area_struct *vma)
602 * Conventional zones accept random writes, so their files can support
603 * shared writable mappings. For sequential zone files, only read
604 * mappings are possible since there are no guarantees for write
605 * ordering between msync() and page cache writeback.
607 if (ZONEFS_I(file_inode(file))->i_ztype == ZONEFS_ZTYPE_SEQ &&
608 (vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
612 vma->vm_ops = &zonefs_file_vm_ops;
617 static loff_t zonefs_file_llseek(struct file *file, loff_t offset, int whence)
619 loff_t isize = i_size_read(file_inode(file));
622 * Seeks are limited to below the zone size for conventional zones
623 * and below the zone write pointer for sequential zones. In both
624 * cases, this limit is the inode size.
626 return generic_file_llseek_size(file, offset, whence, isize, isize);
629 static int zonefs_file_write_dio_end_io(struct kiocb *iocb, ssize_t size,
630 int error, unsigned int flags)
632 struct inode *inode = file_inode(iocb->ki_filp);
633 struct zonefs_inode_info *zi = ZONEFS_I(inode);
636 zonefs_io_error(inode, true);
640 if (size && zi->i_ztype != ZONEFS_ZTYPE_CNV) {
642 * Note that we may be seeing completions out of order,
643 * but that is not a problem since a write completed
644 * successfully necessarily means that all preceding writes
645 * were also successful. So we can safely increase the inode
646 * size to the write end location.
648 mutex_lock(&zi->i_truncate_mutex);
649 if (i_size_read(inode) < iocb->ki_pos + size) {
650 zonefs_update_stats(inode, iocb->ki_pos + size);
651 zonefs_i_size_write(inode, iocb->ki_pos + size);
653 mutex_unlock(&zi->i_truncate_mutex);
659 static const struct iomap_dio_ops zonefs_write_dio_ops = {
660 .end_io = zonefs_file_write_dio_end_io,
663 static ssize_t zonefs_file_dio_append(struct kiocb *iocb, struct iov_iter *from)
665 struct inode *inode = file_inode(iocb->ki_filp);
666 struct zonefs_inode_info *zi = ZONEFS_I(inode);
667 struct block_device *bdev = inode->i_sb->s_bdev;
674 max = queue_max_zone_append_sectors(bdev_get_queue(bdev));
675 max = ALIGN_DOWN(max << SECTOR_SHIFT, inode->i_sb->s_blocksize);
676 iov_iter_truncate(from, max);
678 nr_pages = iov_iter_npages(from, BIO_MAX_PAGES);
682 bio = bio_alloc_bioset(GFP_NOFS, nr_pages, &fs_bio_set);
686 bio_set_dev(bio, bdev);
687 bio->bi_iter.bi_sector = zi->i_zsector;
688 bio->bi_write_hint = iocb->ki_hint;
689 bio->bi_ioprio = iocb->ki_ioprio;
690 bio->bi_opf = REQ_OP_ZONE_APPEND | REQ_SYNC | REQ_IDLE;
691 if (iocb->ki_flags & IOCB_DSYNC)
692 bio->bi_opf |= REQ_FUA;
694 ret = bio_iov_iter_get_pages(bio, from);
698 size = bio->bi_iter.bi_size;
699 task_io_account_write(size);
701 if (iocb->ki_flags & IOCB_HIPRI)
702 bio_set_polled(bio, iocb);
704 ret = submit_bio_wait(bio);
706 zonefs_file_write_dio_end_io(iocb, size, ret, 0);
709 bio_release_pages(bio, false);
713 iocb->ki_pos += size;
721 * Handle direct writes. For sequential zone files, this is the only possible
722 * write path. For these files, check that the user is issuing writes
723 * sequentially from the end of the file. This code assumes that the block layer
724 * delivers write requests to the device in sequential order. This is always the
725 * case if a block IO scheduler implementing the ELEVATOR_F_ZBD_SEQ_WRITE
726 * elevator feature is being used (e.g. mq-deadline). The block layer always
727 * automatically select such an elevator for zoned block devices during the
728 * device initialization.
730 static ssize_t zonefs_file_dio_write(struct kiocb *iocb, struct iov_iter *from)
732 struct inode *inode = file_inode(iocb->ki_filp);
733 struct zonefs_inode_info *zi = ZONEFS_I(inode);
734 struct super_block *sb = inode->i_sb;
735 bool sync = is_sync_kiocb(iocb);
741 * For async direct IOs to sequential zone files, refuse IOCB_NOWAIT
742 * as this can cause write reordering (e.g. the first aio gets EAGAIN
743 * on the inode lock but the second goes through but is now unaligned).
745 if (zi->i_ztype == ZONEFS_ZTYPE_SEQ && !sync &&
746 (iocb->ki_flags & IOCB_NOWAIT))
749 if (iocb->ki_flags & IOCB_NOWAIT) {
750 if (!inode_trylock(inode))
756 ret = generic_write_checks(iocb, from);
760 iov_iter_truncate(from, zi->i_max_size - iocb->ki_pos);
761 count = iov_iter_count(from);
763 if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) {
768 /* Enforce sequential writes (append only) in sequential zones */
769 if (zi->i_ztype == ZONEFS_ZTYPE_SEQ) {
770 mutex_lock(&zi->i_truncate_mutex);
771 if (iocb->ki_pos != zi->i_wpoffset) {
772 mutex_unlock(&zi->i_truncate_mutex);
776 mutex_unlock(&zi->i_truncate_mutex);
781 ret = zonefs_file_dio_append(iocb, from);
783 ret = iomap_dio_rw(iocb, from, &zonefs_iomap_ops,
784 &zonefs_write_dio_ops, sync);
785 if (zi->i_ztype == ZONEFS_ZTYPE_SEQ &&
786 (ret > 0 || ret == -EIOCBQUEUED)) {
789 mutex_lock(&zi->i_truncate_mutex);
790 zi->i_wpoffset += count;
791 mutex_unlock(&zi->i_truncate_mutex);
800 static ssize_t zonefs_file_buffered_write(struct kiocb *iocb,
801 struct iov_iter *from)
803 struct inode *inode = file_inode(iocb->ki_filp);
804 struct zonefs_inode_info *zi = ZONEFS_I(inode);
808 * Direct IO writes are mandatory for sequential zone files so that the
809 * write IO issuing order is preserved.
811 if (zi->i_ztype != ZONEFS_ZTYPE_CNV)
814 if (iocb->ki_flags & IOCB_NOWAIT) {
815 if (!inode_trylock(inode))
821 ret = generic_write_checks(iocb, from);
825 iov_iter_truncate(from, zi->i_max_size - iocb->ki_pos);
827 ret = iomap_file_buffered_write(iocb, from, &zonefs_iomap_ops);
830 else if (ret == -EIO)
831 zonefs_io_error(inode, true);
836 ret = generic_write_sync(iocb, ret);
841 static ssize_t zonefs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
843 struct inode *inode = file_inode(iocb->ki_filp);
845 if (unlikely(IS_IMMUTABLE(inode)))
848 if (sb_rdonly(inode->i_sb))
851 /* Write operations beyond the zone size are not allowed */
852 if (iocb->ki_pos >= ZONEFS_I(inode)->i_max_size)
855 if (iocb->ki_flags & IOCB_DIRECT) {
856 ssize_t ret = zonefs_file_dio_write(iocb, from);
861 return zonefs_file_buffered_write(iocb, from);
864 static int zonefs_file_read_dio_end_io(struct kiocb *iocb, ssize_t size,
865 int error, unsigned int flags)
868 zonefs_io_error(file_inode(iocb->ki_filp), false);
875 static const struct iomap_dio_ops zonefs_read_dio_ops = {
876 .end_io = zonefs_file_read_dio_end_io,
879 static ssize_t zonefs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
881 struct inode *inode = file_inode(iocb->ki_filp);
882 struct zonefs_inode_info *zi = ZONEFS_I(inode);
883 struct super_block *sb = inode->i_sb;
887 /* Offline zones cannot be read */
888 if (unlikely(IS_IMMUTABLE(inode) && !(inode->i_mode & 0777)))
891 if (iocb->ki_pos >= zi->i_max_size)
894 if (iocb->ki_flags & IOCB_NOWAIT) {
895 if (!inode_trylock_shared(inode))
898 inode_lock_shared(inode);
901 /* Limit read operations to written data */
902 mutex_lock(&zi->i_truncate_mutex);
903 isize = i_size_read(inode);
904 if (iocb->ki_pos >= isize) {
905 mutex_unlock(&zi->i_truncate_mutex);
909 iov_iter_truncate(to, isize - iocb->ki_pos);
910 mutex_unlock(&zi->i_truncate_mutex);
912 if (iocb->ki_flags & IOCB_DIRECT) {
913 size_t count = iov_iter_count(to);
915 if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) {
919 file_accessed(iocb->ki_filp);
920 ret = iomap_dio_rw(iocb, to, &zonefs_iomap_ops,
921 &zonefs_read_dio_ops, is_sync_kiocb(iocb));
923 ret = generic_file_read_iter(iocb, to);
925 zonefs_io_error(inode, false);
929 inode_unlock_shared(inode);
934 static inline bool zonefs_file_use_exp_open(struct inode *inode, struct file *file)
936 struct zonefs_inode_info *zi = ZONEFS_I(inode);
937 struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
939 if (!(sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN))
942 if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
945 if (!(file->f_mode & FMODE_WRITE))
951 static int zonefs_open_zone(struct inode *inode)
953 struct zonefs_inode_info *zi = ZONEFS_I(inode);
954 struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
957 mutex_lock(&zi->i_truncate_mutex);
960 if (zi->i_wr_refcnt == 1) {
962 if (atomic_inc_return(&sbi->s_open_zones) > sbi->s_max_open_zones) {
963 atomic_dec(&sbi->s_open_zones);
968 if (i_size_read(inode) < zi->i_max_size) {
969 ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_OPEN);
972 atomic_dec(&sbi->s_open_zones);
975 zi->i_flags |= ZONEFS_ZONE_OPEN;
980 mutex_unlock(&zi->i_truncate_mutex);
985 static int zonefs_file_open(struct inode *inode, struct file *file)
989 ret = generic_file_open(inode, file);
993 if (zonefs_file_use_exp_open(inode, file))
994 return zonefs_open_zone(inode);
999 static void zonefs_close_zone(struct inode *inode)
1001 struct zonefs_inode_info *zi = ZONEFS_I(inode);
1004 mutex_lock(&zi->i_truncate_mutex);
1006 if (!zi->i_wr_refcnt) {
1007 struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
1008 struct super_block *sb = inode->i_sb;
1011 * If the file zone is full, it is not open anymore and we only
1012 * need to decrement the open count.
1014 if (!(zi->i_flags & ZONEFS_ZONE_OPEN))
1017 ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_CLOSE);
1019 __zonefs_io_error(inode, false);
1021 * Leaving zones explicitly open may lead to a state
1022 * where most zones cannot be written (zone resources
1023 * exhausted). So take preventive action by remounting
1026 if (zi->i_flags & ZONEFS_ZONE_OPEN &&
1027 !(sb->s_flags & SB_RDONLY)) {
1028 zonefs_warn(sb, "closing zone failed, remounting filesystem read-only\n");
1029 sb->s_flags |= SB_RDONLY;
1032 zi->i_flags &= ~ZONEFS_ZONE_OPEN;
1034 atomic_dec(&sbi->s_open_zones);
1036 mutex_unlock(&zi->i_truncate_mutex);
1039 static int zonefs_file_release(struct inode *inode, struct file *file)
1042 * If we explicitly open a zone we must close it again as well, but the
1043 * zone management operation can fail (either due to an IO error or as
1044 * the zone has gone offline or read-only). Make sure we don't fail the
1045 * close(2) for user-space.
1047 if (zonefs_file_use_exp_open(inode, file))
1048 zonefs_close_zone(inode);
1053 static const struct file_operations zonefs_file_operations = {
1054 .open = zonefs_file_open,
1055 .release = zonefs_file_release,
1056 .fsync = zonefs_file_fsync,
1057 .mmap = zonefs_file_mmap,
1058 .llseek = zonefs_file_llseek,
1059 .read_iter = zonefs_file_read_iter,
1060 .write_iter = zonefs_file_write_iter,
1061 .splice_read = generic_file_splice_read,
1062 .splice_write = iter_file_splice_write,
1063 .iopoll = iomap_dio_iopoll,
1066 static struct kmem_cache *zonefs_inode_cachep;
1068 static struct inode *zonefs_alloc_inode(struct super_block *sb)
1070 struct zonefs_inode_info *zi;
1072 zi = kmem_cache_alloc(zonefs_inode_cachep, GFP_KERNEL);
1076 inode_init_once(&zi->i_vnode);
1077 mutex_init(&zi->i_truncate_mutex);
1078 init_rwsem(&zi->i_mmap_sem);
1079 zi->i_wr_refcnt = 0;
1081 return &zi->i_vnode;
1084 static void zonefs_free_inode(struct inode *inode)
1086 kmem_cache_free(zonefs_inode_cachep, ZONEFS_I(inode));
1092 static int zonefs_statfs(struct dentry *dentry, struct kstatfs *buf)
1094 struct super_block *sb = dentry->d_sb;
1095 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1096 enum zonefs_ztype t;
1099 buf->f_type = ZONEFS_MAGIC;
1100 buf->f_bsize = sb->s_blocksize;
1101 buf->f_namelen = ZONEFS_NAME_MAX;
1103 spin_lock(&sbi->s_lock);
1105 buf->f_blocks = sbi->s_blocks;
1106 if (WARN_ON(sbi->s_used_blocks > sbi->s_blocks))
1109 buf->f_bfree = buf->f_blocks - sbi->s_used_blocks;
1110 buf->f_bavail = buf->f_bfree;
1112 for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) {
1113 if (sbi->s_nr_files[t])
1114 buf->f_files += sbi->s_nr_files[t] + 1;
1118 spin_unlock(&sbi->s_lock);
1120 fsid = le64_to_cpup((void *)sbi->s_uuid.b) ^
1121 le64_to_cpup((void *)sbi->s_uuid.b + sizeof(u64));
1122 buf->f_fsid = u64_to_fsid(fsid);
1128 Opt_errors_ro, Opt_errors_zro, Opt_errors_zol, Opt_errors_repair,
1129 Opt_explicit_open, Opt_err,
1132 static const match_table_t tokens = {
1133 { Opt_errors_ro, "errors=remount-ro"},
1134 { Opt_errors_zro, "errors=zone-ro"},
1135 { Opt_errors_zol, "errors=zone-offline"},
1136 { Opt_errors_repair, "errors=repair"},
1137 { Opt_explicit_open, "explicit-open" },
1141 static int zonefs_parse_options(struct super_block *sb, char *options)
1143 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1144 substring_t args[MAX_OPT_ARGS];
1150 while ((p = strsep(&options, ",")) != NULL) {
1156 token = match_token(p, tokens, args);
1159 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
1160 sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_RO;
1162 case Opt_errors_zro:
1163 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
1164 sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZRO;
1166 case Opt_errors_zol:
1167 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
1168 sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZOL;
1170 case Opt_errors_repair:
1171 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
1172 sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_REPAIR;
1174 case Opt_explicit_open:
1175 sbi->s_mount_opts |= ZONEFS_MNTOPT_EXPLICIT_OPEN;
1185 static int zonefs_show_options(struct seq_file *seq, struct dentry *root)
1187 struct zonefs_sb_info *sbi = ZONEFS_SB(root->d_sb);
1189 if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO)
1190 seq_puts(seq, ",errors=remount-ro");
1191 if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO)
1192 seq_puts(seq, ",errors=zone-ro");
1193 if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL)
1194 seq_puts(seq, ",errors=zone-offline");
1195 if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_REPAIR)
1196 seq_puts(seq, ",errors=repair");
1201 static int zonefs_remount(struct super_block *sb, int *flags, char *data)
1203 sync_filesystem(sb);
1205 return zonefs_parse_options(sb, data);
1208 static const struct super_operations zonefs_sops = {
1209 .alloc_inode = zonefs_alloc_inode,
1210 .free_inode = zonefs_free_inode,
1211 .statfs = zonefs_statfs,
1212 .remount_fs = zonefs_remount,
1213 .show_options = zonefs_show_options,
1216 static const struct inode_operations zonefs_dir_inode_operations = {
1217 .lookup = simple_lookup,
1218 .setattr = zonefs_inode_setattr,
1221 static void zonefs_init_dir_inode(struct inode *parent, struct inode *inode,
1222 enum zonefs_ztype type)
1224 struct super_block *sb = parent->i_sb;
1226 inode->i_ino = blkdev_nr_zones(sb->s_bdev->bd_disk) + type + 1;
1227 inode_init_owner(&init_user_ns, inode, parent, S_IFDIR | 0555);
1228 inode->i_op = &zonefs_dir_inode_operations;
1229 inode->i_fop = &simple_dir_operations;
1230 set_nlink(inode, 2);
1234 static void zonefs_init_file_inode(struct inode *inode, struct blk_zone *zone,
1235 enum zonefs_ztype type)
1237 struct super_block *sb = inode->i_sb;
1238 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1239 struct zonefs_inode_info *zi = ZONEFS_I(inode);
1241 inode->i_ino = zone->start >> sbi->s_zone_sectors_shift;
1242 inode->i_mode = S_IFREG | sbi->s_perm;
1245 zi->i_zsector = zone->start;
1246 zi->i_zone_size = zone->len << SECTOR_SHIFT;
1248 zi->i_max_size = min_t(loff_t, MAX_LFS_FILESIZE,
1249 zone->capacity << SECTOR_SHIFT);
1250 zi->i_wpoffset = zonefs_check_zone_condition(inode, zone, true, true);
1252 inode->i_uid = sbi->s_uid;
1253 inode->i_gid = sbi->s_gid;
1254 inode->i_size = zi->i_wpoffset;
1255 inode->i_blocks = zi->i_max_size >> SECTOR_SHIFT;
1257 inode->i_op = &zonefs_file_inode_operations;
1258 inode->i_fop = &zonefs_file_operations;
1259 inode->i_mapping->a_ops = &zonefs_file_aops;
1261 sb->s_maxbytes = max(zi->i_max_size, sb->s_maxbytes);
1262 sbi->s_blocks += zi->i_max_size >> sb->s_blocksize_bits;
1263 sbi->s_used_blocks += zi->i_wpoffset >> sb->s_blocksize_bits;
1266 static struct dentry *zonefs_create_inode(struct dentry *parent,
1267 const char *name, struct blk_zone *zone,
1268 enum zonefs_ztype type)
1270 struct inode *dir = d_inode(parent);
1271 struct dentry *dentry;
1272 struct inode *inode;
1274 dentry = d_alloc_name(parent, name);
1278 inode = new_inode(parent->d_sb);
1282 inode->i_ctime = inode->i_mtime = inode->i_atime = dir->i_ctime;
1284 zonefs_init_file_inode(inode, zone, type);
1286 zonefs_init_dir_inode(dir, inode, type);
1287 d_add(dentry, inode);
1298 struct zonefs_zone_data {
1299 struct super_block *sb;
1300 unsigned int nr_zones[ZONEFS_ZTYPE_MAX];
1301 struct blk_zone *zones;
1305 * Create a zone group and populate it with zone files.
1307 static int zonefs_create_zgroup(struct zonefs_zone_data *zd,
1308 enum zonefs_ztype type)
1310 struct super_block *sb = zd->sb;
1311 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1312 struct blk_zone *zone, *next, *end;
1313 const char *zgroup_name;
1319 /* If the group is empty, there is nothing to do */
1320 if (!zd->nr_zones[type])
1323 file_name = kmalloc(ZONEFS_NAME_MAX, GFP_KERNEL);
1327 if (type == ZONEFS_ZTYPE_CNV)
1328 zgroup_name = "cnv";
1330 zgroup_name = "seq";
1332 dir = zonefs_create_inode(sb->s_root, zgroup_name, NULL, type);
1339 * The first zone contains the super block: skip it.
1341 end = zd->zones + blkdev_nr_zones(sb->s_bdev->bd_disk);
1342 for (zone = &zd->zones[1]; zone < end; zone = next) {
1345 if (zonefs_zone_type(zone) != type)
1349 * For conventional zones, contiguous zones can be aggregated
1350 * together to form larger files. Note that this overwrites the
1351 * length of the first zone of the set of contiguous zones
1352 * aggregated together. If one offline or read-only zone is
1353 * found, assume that all zones aggregated have the same
1356 if (type == ZONEFS_ZTYPE_CNV &&
1357 (sbi->s_features & ZONEFS_F_AGGRCNV)) {
1358 for (; next < end; next++) {
1359 if (zonefs_zone_type(next) != type)
1361 zone->len += next->len;
1362 zone->capacity += next->capacity;
1363 if (next->cond == BLK_ZONE_COND_READONLY &&
1364 zone->cond != BLK_ZONE_COND_OFFLINE)
1365 zone->cond = BLK_ZONE_COND_READONLY;
1366 else if (next->cond == BLK_ZONE_COND_OFFLINE)
1367 zone->cond = BLK_ZONE_COND_OFFLINE;
1369 if (zone->capacity != zone->len) {
1370 zonefs_err(sb, "Invalid conventional zone capacity\n");
1377 * Use the file number within its group as file name.
1379 snprintf(file_name, ZONEFS_NAME_MAX - 1, "%u", n);
1380 if (!zonefs_create_inode(dir, file_name, zone, type)) {
1388 zonefs_info(sb, "Zone group \"%s\" has %u file%s\n",
1389 zgroup_name, n, n > 1 ? "s" : "");
1391 sbi->s_nr_files[type] = n;
1400 static int zonefs_get_zone_info_cb(struct blk_zone *zone, unsigned int idx,
1403 struct zonefs_zone_data *zd = data;
1406 * Count the number of usable zones: the first zone at index 0 contains
1407 * the super block and is ignored.
1409 switch (zone->type) {
1410 case BLK_ZONE_TYPE_CONVENTIONAL:
1411 zone->wp = zone->start + zone->len;
1413 zd->nr_zones[ZONEFS_ZTYPE_CNV]++;
1415 case BLK_ZONE_TYPE_SEQWRITE_REQ:
1416 case BLK_ZONE_TYPE_SEQWRITE_PREF:
1418 zd->nr_zones[ZONEFS_ZTYPE_SEQ]++;
1421 zonefs_err(zd->sb, "Unsupported zone type 0x%x\n",
1426 memcpy(&zd->zones[idx], zone, sizeof(struct blk_zone));
1431 static int zonefs_get_zone_info(struct zonefs_zone_data *zd)
1433 struct block_device *bdev = zd->sb->s_bdev;
1436 zd->zones = kvcalloc(blkdev_nr_zones(bdev->bd_disk),
1437 sizeof(struct blk_zone), GFP_KERNEL);
1441 /* Get zones information from the device */
1442 ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES,
1443 zonefs_get_zone_info_cb, zd);
1445 zonefs_err(zd->sb, "Zone report failed %d\n", ret);
1449 if (ret != blkdev_nr_zones(bdev->bd_disk)) {
1450 zonefs_err(zd->sb, "Invalid zone report (%d/%u zones)\n",
1451 ret, blkdev_nr_zones(bdev->bd_disk));
1458 static inline void zonefs_cleanup_zone_info(struct zonefs_zone_data *zd)
1464 * Read super block information from the device.
1466 static int zonefs_read_super(struct super_block *sb)
1468 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1469 struct zonefs_super *super;
1470 u32 crc, stored_crc;
1472 struct bio_vec bio_vec;
1476 page = alloc_page(GFP_KERNEL);
1480 bio_init(&bio, &bio_vec, 1);
1481 bio.bi_iter.bi_sector = 0;
1482 bio.bi_opf = REQ_OP_READ;
1483 bio_set_dev(&bio, sb->s_bdev);
1484 bio_add_page(&bio, page, PAGE_SIZE, 0);
1486 ret = submit_bio_wait(&bio);
1493 if (le32_to_cpu(super->s_magic) != ZONEFS_MAGIC)
1496 stored_crc = le32_to_cpu(super->s_crc);
1498 crc = crc32(~0U, (unsigned char *)super, sizeof(struct zonefs_super));
1499 if (crc != stored_crc) {
1500 zonefs_err(sb, "Invalid checksum (Expected 0x%08x, got 0x%08x)",
1505 sbi->s_features = le64_to_cpu(super->s_features);
1506 if (sbi->s_features & ~ZONEFS_F_DEFINED_FEATURES) {
1507 zonefs_err(sb, "Unknown features set 0x%llx\n",
1512 if (sbi->s_features & ZONEFS_F_UID) {
1513 sbi->s_uid = make_kuid(current_user_ns(),
1514 le32_to_cpu(super->s_uid));
1515 if (!uid_valid(sbi->s_uid)) {
1516 zonefs_err(sb, "Invalid UID feature\n");
1521 if (sbi->s_features & ZONEFS_F_GID) {
1522 sbi->s_gid = make_kgid(current_user_ns(),
1523 le32_to_cpu(super->s_gid));
1524 if (!gid_valid(sbi->s_gid)) {
1525 zonefs_err(sb, "Invalid GID feature\n");
1530 if (sbi->s_features & ZONEFS_F_PERM)
1531 sbi->s_perm = le32_to_cpu(super->s_perm);
1533 if (memchr_inv(super->s_reserved, 0, sizeof(super->s_reserved))) {
1534 zonefs_err(sb, "Reserved area is being used\n");
1538 import_uuid(&sbi->s_uuid, super->s_uuid);
1550 * Check that the device is zoned. If it is, get the list of zones and create
1551 * sub-directories and files according to the device zone configuration and
1554 static int zonefs_fill_super(struct super_block *sb, void *data, int silent)
1556 struct zonefs_zone_data zd;
1557 struct zonefs_sb_info *sbi;
1558 struct inode *inode;
1559 enum zonefs_ztype t;
1562 if (!bdev_is_zoned(sb->s_bdev)) {
1563 zonefs_err(sb, "Not a zoned block device\n");
1568 * Initialize super block information: the maximum file size is updated
1569 * when the zone files are created so that the format option
1570 * ZONEFS_F_AGGRCNV which increases the maximum file size of a file
1571 * beyond the zone size is taken into account.
1573 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1577 spin_lock_init(&sbi->s_lock);
1578 sb->s_fs_info = sbi;
1579 sb->s_magic = ZONEFS_MAGIC;
1581 sb->s_op = &zonefs_sops;
1582 sb->s_time_gran = 1;
1585 * The block size is set to the device physical sector size to ensure
1586 * that write operations on 512e devices (512B logical block and 4KB
1587 * physical block) are always aligned to the device physical blocks,
1588 * as mandated by the ZBC/ZAC specifications.
1590 sb_set_blocksize(sb, bdev_physical_block_size(sb->s_bdev));
1591 sbi->s_zone_sectors_shift = ilog2(bdev_zone_sectors(sb->s_bdev));
1592 sbi->s_uid = GLOBAL_ROOT_UID;
1593 sbi->s_gid = GLOBAL_ROOT_GID;
1595 sbi->s_mount_opts = ZONEFS_MNTOPT_ERRORS_RO;
1596 sbi->s_max_open_zones = bdev_max_open_zones(sb->s_bdev);
1597 atomic_set(&sbi->s_open_zones, 0);
1598 if (!sbi->s_max_open_zones &&
1599 sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) {
1600 zonefs_info(sb, "No open zones limit. Ignoring explicit_open mount option\n");
1601 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_EXPLICIT_OPEN;
1604 ret = zonefs_read_super(sb);
1608 ret = zonefs_parse_options(sb, data);
1612 memset(&zd, 0, sizeof(struct zonefs_zone_data));
1614 ret = zonefs_get_zone_info(&zd);
1618 zonefs_info(sb, "Mounting %u zones",
1619 blkdev_nr_zones(sb->s_bdev->bd_disk));
1621 /* Create root directory inode */
1623 inode = new_inode(sb);
1627 inode->i_ino = blkdev_nr_zones(sb->s_bdev->bd_disk);
1628 inode->i_mode = S_IFDIR | 0555;
1629 inode->i_ctime = inode->i_mtime = inode->i_atime = current_time(inode);
1630 inode->i_op = &zonefs_dir_inode_operations;
1631 inode->i_fop = &simple_dir_operations;
1632 set_nlink(inode, 2);
1634 sb->s_root = d_make_root(inode);
1638 /* Create and populate files in zone groups directories */
1639 for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) {
1640 ret = zonefs_create_zgroup(&zd, t);
1646 zonefs_cleanup_zone_info(&zd);
1651 static struct dentry *zonefs_mount(struct file_system_type *fs_type,
1652 int flags, const char *dev_name, void *data)
1654 return mount_bdev(fs_type, flags, dev_name, data, zonefs_fill_super);
1657 static void zonefs_kill_super(struct super_block *sb)
1659 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1662 d_genocide(sb->s_root);
1663 kill_block_super(sb);
1668 * File system definition and registration.
1670 static struct file_system_type zonefs_type = {
1671 .owner = THIS_MODULE,
1673 .mount = zonefs_mount,
1674 .kill_sb = zonefs_kill_super,
1675 .fs_flags = FS_REQUIRES_DEV,
1678 static int __init zonefs_init_inodecache(void)
1680 zonefs_inode_cachep = kmem_cache_create("zonefs_inode_cache",
1681 sizeof(struct zonefs_inode_info), 0,
1682 (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT),
1684 if (zonefs_inode_cachep == NULL)
1689 static void zonefs_destroy_inodecache(void)
1692 * Make sure all delayed rcu free inodes are flushed before we
1693 * destroy the inode cache.
1696 kmem_cache_destroy(zonefs_inode_cachep);
1699 static int __init zonefs_init(void)
1703 BUILD_BUG_ON(sizeof(struct zonefs_super) != ZONEFS_SUPER_SIZE);
1705 ret = zonefs_init_inodecache();
1709 ret = register_filesystem(&zonefs_type);
1711 zonefs_destroy_inodecache();
1718 static void __exit zonefs_exit(void)
1720 zonefs_destroy_inodecache();
1721 unregister_filesystem(&zonefs_type);
1724 MODULE_AUTHOR("Damien Le Moal");
1725 MODULE_DESCRIPTION("Zone file system for zoned block devices");
1726 MODULE_LICENSE("GPL");
1727 module_init(zonefs_init);
1728 module_exit(zonefs_exit);