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
30 static inline int zonefs_zone_mgmt(struct inode *inode,
33 struct zonefs_inode_info *zi = ZONEFS_I(inode);
36 lockdep_assert_held(&zi->i_truncate_mutex);
38 trace_zonefs_zone_mgmt(inode, op);
39 ret = blkdev_zone_mgmt(inode->i_sb->s_bdev, op, zi->i_zsector,
40 zi->i_zone_size >> SECTOR_SHIFT, GFP_NOFS);
42 zonefs_err(inode->i_sb,
43 "Zone management operation %s at %llu failed %d\n",
44 blk_op_str(op), zi->i_zsector, ret);
51 static inline void zonefs_i_size_write(struct inode *inode, loff_t isize)
53 struct zonefs_inode_info *zi = ZONEFS_I(inode);
55 i_size_write(inode, isize);
57 * A full zone is no longer open/active and does not need
60 if (isize >= zi->i_max_size)
61 zi->i_flags &= ~ZONEFS_ZONE_OPEN;
64 static int zonefs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
65 unsigned int flags, struct iomap *iomap,
68 struct zonefs_inode_info *zi = ZONEFS_I(inode);
69 struct super_block *sb = inode->i_sb;
72 /* All I/Os should always be within the file maximum size */
73 if (WARN_ON_ONCE(offset + length > zi->i_max_size))
77 * Sequential zones can only accept direct writes. This is already
78 * checked when writes are issued, so warn if we see a page writeback
81 if (WARN_ON_ONCE(zi->i_ztype == ZONEFS_ZTYPE_SEQ &&
82 (flags & IOMAP_WRITE) && !(flags & IOMAP_DIRECT)))
86 * For conventional zones, all blocks are always mapped. For sequential
87 * zones, all blocks after always mapped below the inode size (zone
88 * write pointer) and unwriten beyond.
90 mutex_lock(&zi->i_truncate_mutex);
91 isize = i_size_read(inode);
93 iomap->type = IOMAP_UNWRITTEN;
95 iomap->type = IOMAP_MAPPED;
96 if (flags & IOMAP_WRITE)
97 length = zi->i_max_size - offset;
99 length = min(length, isize - offset);
100 mutex_unlock(&zi->i_truncate_mutex);
102 iomap->offset = ALIGN_DOWN(offset, sb->s_blocksize);
103 iomap->length = ALIGN(offset + length, sb->s_blocksize) - iomap->offset;
104 iomap->bdev = inode->i_sb->s_bdev;
105 iomap->addr = (zi->i_zsector << SECTOR_SHIFT) + iomap->offset;
107 trace_zonefs_iomap_begin(inode, iomap);
112 static const struct iomap_ops zonefs_iomap_ops = {
113 .iomap_begin = zonefs_iomap_begin,
116 static int zonefs_readpage(struct file *unused, struct page *page)
118 return iomap_readpage(page, &zonefs_iomap_ops);
121 static void zonefs_readahead(struct readahead_control *rac)
123 iomap_readahead(rac, &zonefs_iomap_ops);
127 * Map blocks for page writeback. This is used only on conventional zone files,
128 * which implies that the page range can only be within the fixed inode size.
130 static int zonefs_map_blocks(struct iomap_writepage_ctx *wpc,
131 struct inode *inode, loff_t offset)
133 struct zonefs_inode_info *zi = ZONEFS_I(inode);
135 if (WARN_ON_ONCE(zi->i_ztype != ZONEFS_ZTYPE_CNV))
137 if (WARN_ON_ONCE(offset >= i_size_read(inode)))
140 /* If the mapping is already OK, nothing needs to be done */
141 if (offset >= wpc->iomap.offset &&
142 offset < wpc->iomap.offset + wpc->iomap.length)
145 return zonefs_iomap_begin(inode, offset, zi->i_max_size - offset,
146 IOMAP_WRITE, &wpc->iomap, NULL);
149 static const struct iomap_writeback_ops zonefs_writeback_ops = {
150 .map_blocks = zonefs_map_blocks,
153 static int zonefs_writepage(struct page *page, struct writeback_control *wbc)
155 struct iomap_writepage_ctx wpc = { };
157 return iomap_writepage(page, wbc, &wpc, &zonefs_writeback_ops);
160 static int zonefs_writepages(struct address_space *mapping,
161 struct writeback_control *wbc)
163 struct iomap_writepage_ctx wpc = { };
165 return iomap_writepages(mapping, wbc, &wpc, &zonefs_writeback_ops);
168 static int zonefs_swap_activate(struct swap_info_struct *sis,
169 struct file *swap_file, sector_t *span)
171 struct inode *inode = file_inode(swap_file);
172 struct zonefs_inode_info *zi = ZONEFS_I(inode);
174 if (zi->i_ztype != ZONEFS_ZTYPE_CNV) {
175 zonefs_err(inode->i_sb,
176 "swap file: not a conventional zone file\n");
180 return iomap_swapfile_activate(sis, swap_file, span, &zonefs_iomap_ops);
183 static const struct address_space_operations zonefs_file_aops = {
184 .readpage = zonefs_readpage,
185 .readahead = zonefs_readahead,
186 .writepage = zonefs_writepage,
187 .writepages = zonefs_writepages,
188 .dirty_folio = filemap_dirty_folio,
189 .releasepage = iomap_releasepage,
190 .invalidate_folio = iomap_invalidate_folio,
191 .migratepage = iomap_migrate_page,
192 .is_partially_uptodate = iomap_is_partially_uptodate,
193 .error_remove_page = generic_error_remove_page,
194 .direct_IO = noop_direct_IO,
195 .swap_activate = zonefs_swap_activate,
198 static void zonefs_update_stats(struct inode *inode, loff_t new_isize)
200 struct super_block *sb = inode->i_sb;
201 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
202 loff_t old_isize = i_size_read(inode);
205 if (new_isize == old_isize)
208 spin_lock(&sbi->s_lock);
211 * This may be called for an update after an IO error.
212 * So beware of the values seen.
214 if (new_isize < old_isize) {
215 nr_blocks = (old_isize - new_isize) >> sb->s_blocksize_bits;
216 if (sbi->s_used_blocks > nr_blocks)
217 sbi->s_used_blocks -= nr_blocks;
219 sbi->s_used_blocks = 0;
221 sbi->s_used_blocks +=
222 (new_isize - old_isize) >> sb->s_blocksize_bits;
223 if (sbi->s_used_blocks > sbi->s_blocks)
224 sbi->s_used_blocks = sbi->s_blocks;
227 spin_unlock(&sbi->s_lock);
231 * Check a zone condition and adjust its file inode access permissions for
232 * offline and readonly zones. Return the inode size corresponding to the
233 * amount of readable data in the zone.
235 static loff_t zonefs_check_zone_condition(struct inode *inode,
236 struct blk_zone *zone, bool warn,
239 struct zonefs_inode_info *zi = ZONEFS_I(inode);
241 switch (zone->cond) {
242 case BLK_ZONE_COND_OFFLINE:
244 * Dead zone: make the inode immutable, disable all accesses
245 * and set the file size to 0 (zone wp set to zone start).
248 zonefs_warn(inode->i_sb, "inode %lu: offline zone\n",
250 inode->i_flags |= S_IMMUTABLE;
251 inode->i_mode &= ~0777;
252 zone->wp = zone->start;
254 case BLK_ZONE_COND_READONLY:
256 * The write pointer of read-only zones is invalid. If such a
257 * zone is found during mount, the file size cannot be retrieved
258 * so we treat the zone as offline (mount == true case).
259 * Otherwise, keep the file size as it was when last updated
260 * so that the user can recover data. In both cases, writes are
261 * always disabled for the zone.
264 zonefs_warn(inode->i_sb, "inode %lu: read-only zone\n",
266 inode->i_flags |= S_IMMUTABLE;
268 zone->cond = BLK_ZONE_COND_OFFLINE;
269 inode->i_mode &= ~0777;
270 zone->wp = zone->start;
273 inode->i_mode &= ~0222;
274 return i_size_read(inode);
275 case BLK_ZONE_COND_FULL:
276 /* The write pointer of full zones is invalid. */
277 return zi->i_max_size;
279 if (zi->i_ztype == ZONEFS_ZTYPE_CNV)
280 return zi->i_max_size;
281 return (zone->wp - zone->start) << SECTOR_SHIFT;
285 struct zonefs_ioerr_data {
290 static int zonefs_io_error_cb(struct blk_zone *zone, unsigned int idx,
293 struct zonefs_ioerr_data *err = data;
294 struct inode *inode = err->inode;
295 struct zonefs_inode_info *zi = ZONEFS_I(inode);
296 struct super_block *sb = inode->i_sb;
297 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
298 loff_t isize, data_size;
301 * Check the zone condition: if the zone is not "bad" (offline or
302 * read-only), read errors are simply signaled to the IO issuer as long
303 * as there is no inconsistency between the inode size and the amount of
304 * data writen in the zone (data_size).
306 data_size = zonefs_check_zone_condition(inode, zone, true, false);
307 isize = i_size_read(inode);
308 if (zone->cond != BLK_ZONE_COND_OFFLINE &&
309 zone->cond != BLK_ZONE_COND_READONLY &&
310 !err->write && isize == data_size)
314 * At this point, we detected either a bad zone or an inconsistency
315 * between the inode size and the amount of data written in the zone.
316 * For the latter case, the cause may be a write IO error or an external
317 * action on the device. Two error patterns exist:
318 * 1) The inode size is lower than the amount of data in the zone:
319 * a write operation partially failed and data was writen at the end
320 * of the file. This can happen in the case of a large direct IO
321 * needing several BIOs and/or write requests to be processed.
322 * 2) The inode size is larger than the amount of data in the zone:
323 * this can happen with a deferred write error with the use of the
324 * device side write cache after getting successful write IO
325 * completions. Other possibilities are (a) an external corruption,
326 * e.g. an application reset the zone directly, or (b) the device
327 * has a serious problem (e.g. firmware bug).
329 * In all cases, warn about inode size inconsistency and handle the
330 * IO error according to the zone condition and to the mount options.
332 if (zi->i_ztype == ZONEFS_ZTYPE_SEQ && isize != data_size)
333 zonefs_warn(sb, "inode %lu: invalid size %lld (should be %lld)\n",
334 inode->i_ino, isize, data_size);
337 * First handle bad zones signaled by hardware. The mount options
338 * errors=zone-ro and errors=zone-offline result in changing the
339 * zone condition to read-only and offline respectively, as if the
340 * condition was signaled by the hardware.
342 if (zone->cond == BLK_ZONE_COND_OFFLINE ||
343 sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL) {
344 zonefs_warn(sb, "inode %lu: read/write access disabled\n",
346 if (zone->cond != BLK_ZONE_COND_OFFLINE) {
347 zone->cond = BLK_ZONE_COND_OFFLINE;
348 data_size = zonefs_check_zone_condition(inode, zone,
351 } else if (zone->cond == BLK_ZONE_COND_READONLY ||
352 sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO) {
353 zonefs_warn(sb, "inode %lu: write access disabled\n",
355 if (zone->cond != BLK_ZONE_COND_READONLY) {
356 zone->cond = BLK_ZONE_COND_READONLY;
357 data_size = zonefs_check_zone_condition(inode, zone,
363 * If the filesystem is mounted with the explicit-open mount option, we
364 * need to clear the ZONEFS_ZONE_OPEN flag if the zone transitioned to
365 * the read-only or offline condition, to avoid attempting an explicit
366 * close of the zone when the inode file is closed.
368 if ((sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) &&
369 (zone->cond == BLK_ZONE_COND_OFFLINE ||
370 zone->cond == BLK_ZONE_COND_READONLY))
371 zi->i_flags &= ~ZONEFS_ZONE_OPEN;
374 * If error=remount-ro was specified, any error result in remounting
375 * the volume as read-only.
377 if ((sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO) && !sb_rdonly(sb)) {
378 zonefs_warn(sb, "remounting filesystem read-only\n");
379 sb->s_flags |= SB_RDONLY;
383 * Update block usage stats and the inode size to prevent access to
386 zonefs_update_stats(inode, data_size);
387 zonefs_i_size_write(inode, data_size);
388 zi->i_wpoffset = data_size;
394 * When an file IO error occurs, check the file zone to see if there is a change
395 * in the zone condition (e.g. offline or read-only). For a failed write to a
396 * sequential zone, the zone write pointer position must also be checked to
397 * eventually correct the file size and zonefs inode write pointer offset
398 * (which can be out of sync with the drive due to partial write failures).
400 static void __zonefs_io_error(struct inode *inode, bool write)
402 struct zonefs_inode_info *zi = ZONEFS_I(inode);
403 struct super_block *sb = inode->i_sb;
404 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
405 unsigned int noio_flag;
406 unsigned int nr_zones =
407 zi->i_zone_size >> (sbi->s_zone_sectors_shift + SECTOR_SHIFT);
408 struct zonefs_ioerr_data err = {
415 * Memory allocations in blkdev_report_zones() can trigger a memory
416 * reclaim which may in turn cause a recursion into zonefs as well as
417 * struct request allocations for the same device. The former case may
418 * end up in a deadlock on the inode truncate mutex, while the latter
419 * may prevent IO forward progress. Executing the report zones under
420 * the GFP_NOIO context avoids both problems.
422 noio_flag = memalloc_noio_save();
423 ret = blkdev_report_zones(sb->s_bdev, zi->i_zsector, nr_zones,
424 zonefs_io_error_cb, &err);
426 zonefs_err(sb, "Get inode %lu zone information failed %d\n",
428 memalloc_noio_restore(noio_flag);
431 static void zonefs_io_error(struct inode *inode, bool write)
433 struct zonefs_inode_info *zi = ZONEFS_I(inode);
435 mutex_lock(&zi->i_truncate_mutex);
436 __zonefs_io_error(inode, write);
437 mutex_unlock(&zi->i_truncate_mutex);
440 static int zonefs_file_truncate(struct inode *inode, loff_t isize)
442 struct zonefs_inode_info *zi = ZONEFS_I(inode);
448 * Only sequential zone files can be truncated and truncation is allowed
449 * only down to a 0 size, which is equivalent to a zone reset, and to
450 * the maximum file size, which is equivalent to a zone finish.
452 if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
456 op = REQ_OP_ZONE_RESET;
457 else if (isize == zi->i_max_size)
458 op = REQ_OP_ZONE_FINISH;
462 inode_dio_wait(inode);
464 /* Serialize against page faults */
465 filemap_invalidate_lock(inode->i_mapping);
467 /* Serialize against zonefs_iomap_begin() */
468 mutex_lock(&zi->i_truncate_mutex);
470 old_isize = i_size_read(inode);
471 if (isize == old_isize)
474 ret = zonefs_zone_mgmt(inode, op);
479 * If the mount option ZONEFS_MNTOPT_EXPLICIT_OPEN is set,
480 * take care of open zones.
482 if (zi->i_flags & ZONEFS_ZONE_OPEN) {
484 * Truncating a zone to EMPTY or FULL is the equivalent of
485 * closing the zone. For a truncation to 0, we need to
486 * re-open the zone to ensure new writes can be processed.
487 * For a truncation to the maximum file size, the zone is
488 * closed and writes cannot be accepted anymore, so clear
492 ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_OPEN);
494 zi->i_flags &= ~ZONEFS_ZONE_OPEN;
497 zonefs_update_stats(inode, isize);
498 truncate_setsize(inode, isize);
499 zi->i_wpoffset = isize;
502 mutex_unlock(&zi->i_truncate_mutex);
503 filemap_invalidate_unlock(inode->i_mapping);
508 static int zonefs_inode_setattr(struct user_namespace *mnt_userns,
509 struct dentry *dentry, struct iattr *iattr)
511 struct inode *inode = d_inode(dentry);
514 if (unlikely(IS_IMMUTABLE(inode)))
517 ret = setattr_prepare(&init_user_ns, dentry, iattr);
522 * Since files and directories cannot be created nor deleted, do not
523 * allow setting any write attributes on the sub-directories grouping
524 * files by zone type.
526 if ((iattr->ia_valid & ATTR_MODE) && S_ISDIR(inode->i_mode) &&
527 (iattr->ia_mode & 0222))
530 if (((iattr->ia_valid & ATTR_UID) &&
531 !uid_eq(iattr->ia_uid, inode->i_uid)) ||
532 ((iattr->ia_valid & ATTR_GID) &&
533 !gid_eq(iattr->ia_gid, inode->i_gid))) {
534 ret = dquot_transfer(inode, iattr);
539 if (iattr->ia_valid & ATTR_SIZE) {
540 ret = zonefs_file_truncate(inode, iattr->ia_size);
545 setattr_copy(&init_user_ns, inode, iattr);
550 static const struct inode_operations zonefs_file_inode_operations = {
551 .setattr = zonefs_inode_setattr,
554 static int zonefs_file_fsync(struct file *file, loff_t start, loff_t end,
557 struct inode *inode = file_inode(file);
560 if (unlikely(IS_IMMUTABLE(inode)))
564 * Since only direct writes are allowed in sequential files, page cache
565 * flush is needed only for conventional zone files.
567 if (ZONEFS_I(inode)->i_ztype == ZONEFS_ZTYPE_CNV)
568 ret = file_write_and_wait_range(file, start, end);
570 ret = blkdev_issue_flush(inode->i_sb->s_bdev);
573 zonefs_io_error(inode, true);
578 static vm_fault_t zonefs_filemap_page_mkwrite(struct vm_fault *vmf)
580 struct inode *inode = file_inode(vmf->vma->vm_file);
581 struct zonefs_inode_info *zi = ZONEFS_I(inode);
584 if (unlikely(IS_IMMUTABLE(inode)))
585 return VM_FAULT_SIGBUS;
588 * Sanity check: only conventional zone files can have shared
589 * writeable mappings.
591 if (WARN_ON_ONCE(zi->i_ztype != ZONEFS_ZTYPE_CNV))
592 return VM_FAULT_NOPAGE;
594 sb_start_pagefault(inode->i_sb);
595 file_update_time(vmf->vma->vm_file);
597 /* Serialize against truncates */
598 filemap_invalidate_lock_shared(inode->i_mapping);
599 ret = iomap_page_mkwrite(vmf, &zonefs_iomap_ops);
600 filemap_invalidate_unlock_shared(inode->i_mapping);
602 sb_end_pagefault(inode->i_sb);
606 static const struct vm_operations_struct zonefs_file_vm_ops = {
607 .fault = filemap_fault,
608 .map_pages = filemap_map_pages,
609 .page_mkwrite = zonefs_filemap_page_mkwrite,
612 static int zonefs_file_mmap(struct file *file, struct vm_area_struct *vma)
615 * Conventional zones accept random writes, so their files can support
616 * shared writable mappings. For sequential zone files, only read
617 * mappings are possible since there are no guarantees for write
618 * ordering between msync() and page cache writeback.
620 if (ZONEFS_I(file_inode(file))->i_ztype == ZONEFS_ZTYPE_SEQ &&
621 (vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
625 vma->vm_ops = &zonefs_file_vm_ops;
630 static loff_t zonefs_file_llseek(struct file *file, loff_t offset, int whence)
632 loff_t isize = i_size_read(file_inode(file));
635 * Seeks are limited to below the zone size for conventional zones
636 * and below the zone write pointer for sequential zones. In both
637 * cases, this limit is the inode size.
639 return generic_file_llseek_size(file, offset, whence, isize, isize);
642 static int zonefs_file_write_dio_end_io(struct kiocb *iocb, ssize_t size,
643 int error, unsigned int flags)
645 struct inode *inode = file_inode(iocb->ki_filp);
646 struct zonefs_inode_info *zi = ZONEFS_I(inode);
649 zonefs_io_error(inode, true);
653 if (size && zi->i_ztype != ZONEFS_ZTYPE_CNV) {
655 * Note that we may be seeing completions out of order,
656 * but that is not a problem since a write completed
657 * successfully necessarily means that all preceding writes
658 * were also successful. So we can safely increase the inode
659 * size to the write end location.
661 mutex_lock(&zi->i_truncate_mutex);
662 if (i_size_read(inode) < iocb->ki_pos + size) {
663 zonefs_update_stats(inode, iocb->ki_pos + size);
664 zonefs_i_size_write(inode, iocb->ki_pos + size);
666 mutex_unlock(&zi->i_truncate_mutex);
672 static const struct iomap_dio_ops zonefs_write_dio_ops = {
673 .end_io = zonefs_file_write_dio_end_io,
676 static ssize_t zonefs_file_dio_append(struct kiocb *iocb, struct iov_iter *from)
678 struct inode *inode = file_inode(iocb->ki_filp);
679 struct zonefs_inode_info *zi = ZONEFS_I(inode);
680 struct block_device *bdev = inode->i_sb->s_bdev;
681 unsigned int max = bdev_max_zone_append_sectors(bdev);
687 max = ALIGN_DOWN(max << SECTOR_SHIFT, inode->i_sb->s_blocksize);
688 iov_iter_truncate(from, max);
690 nr_pages = iov_iter_npages(from, BIO_MAX_VECS);
694 bio = bio_alloc(bdev, nr_pages,
695 REQ_OP_ZONE_APPEND | REQ_SYNC | REQ_IDLE, GFP_NOFS);
696 bio->bi_iter.bi_sector = zi->i_zsector;
697 bio->bi_ioprio = iocb->ki_ioprio;
698 if (iocb->ki_flags & IOCB_DSYNC)
699 bio->bi_opf |= REQ_FUA;
701 ret = bio_iov_iter_get_pages(bio, from);
705 size = bio->bi_iter.bi_size;
706 task_io_account_write(size);
708 if (iocb->ki_flags & IOCB_HIPRI)
709 bio_set_polled(bio, iocb);
711 ret = submit_bio_wait(bio);
713 zonefs_file_write_dio_end_io(iocb, size, ret, 0);
714 trace_zonefs_file_dio_append(inode, size, ret);
717 bio_release_pages(bio, false);
721 iocb->ki_pos += size;
729 * Do not exceed the LFS limits nor the file zone size. If pos is under the
730 * limit it becomes a short access. If it exceeds the limit, return -EFBIG.
732 static loff_t zonefs_write_check_limits(struct file *file, loff_t pos,
735 struct inode *inode = file_inode(file);
736 struct zonefs_inode_info *zi = ZONEFS_I(inode);
737 loff_t limit = rlimit(RLIMIT_FSIZE);
738 loff_t max_size = zi->i_max_size;
740 if (limit != RLIM_INFINITY) {
742 send_sig(SIGXFSZ, current, 0);
745 count = min(count, limit - pos);
748 if (!(file->f_flags & O_LARGEFILE))
749 max_size = min_t(loff_t, MAX_NON_LFS, max_size);
751 if (unlikely(pos >= max_size))
754 return min(count, max_size - pos);
757 static ssize_t zonefs_write_checks(struct kiocb *iocb, struct iov_iter *from)
759 struct file *file = iocb->ki_filp;
760 struct inode *inode = file_inode(file);
761 struct zonefs_inode_info *zi = ZONEFS_I(inode);
764 if (IS_SWAPFILE(inode))
767 if (!iov_iter_count(from))
770 if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
773 if (iocb->ki_flags & IOCB_APPEND) {
774 if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
776 mutex_lock(&zi->i_truncate_mutex);
777 iocb->ki_pos = zi->i_wpoffset;
778 mutex_unlock(&zi->i_truncate_mutex);
781 count = zonefs_write_check_limits(file, iocb->ki_pos,
782 iov_iter_count(from));
786 iov_iter_truncate(from, count);
787 return iov_iter_count(from);
791 * Handle direct writes. For sequential zone files, this is the only possible
792 * write path. For these files, check that the user is issuing writes
793 * sequentially from the end of the file. This code assumes that the block layer
794 * delivers write requests to the device in sequential order. This is always the
795 * case if a block IO scheduler implementing the ELEVATOR_F_ZBD_SEQ_WRITE
796 * elevator feature is being used (e.g. mq-deadline). The block layer always
797 * automatically select such an elevator for zoned block devices during the
798 * device initialization.
800 static ssize_t zonefs_file_dio_write(struct kiocb *iocb, struct iov_iter *from)
802 struct inode *inode = file_inode(iocb->ki_filp);
803 struct zonefs_inode_info *zi = ZONEFS_I(inode);
804 struct super_block *sb = inode->i_sb;
805 bool sync = is_sync_kiocb(iocb);
810 * For async direct IOs to sequential zone files, refuse IOCB_NOWAIT
811 * as this can cause write reordering (e.g. the first aio gets EAGAIN
812 * on the inode lock but the second goes through but is now unaligned).
814 if (zi->i_ztype == ZONEFS_ZTYPE_SEQ && !sync &&
815 (iocb->ki_flags & IOCB_NOWAIT))
818 if (iocb->ki_flags & IOCB_NOWAIT) {
819 if (!inode_trylock(inode))
825 count = zonefs_write_checks(iocb, from);
831 if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) {
836 /* Enforce sequential writes (append only) in sequential zones */
837 if (zi->i_ztype == ZONEFS_ZTYPE_SEQ) {
838 mutex_lock(&zi->i_truncate_mutex);
839 if (iocb->ki_pos != zi->i_wpoffset) {
840 mutex_unlock(&zi->i_truncate_mutex);
844 mutex_unlock(&zi->i_truncate_mutex);
849 ret = zonefs_file_dio_append(iocb, from);
851 ret = iomap_dio_rw(iocb, from, &zonefs_iomap_ops,
852 &zonefs_write_dio_ops, 0, 0);
853 if (zi->i_ztype == ZONEFS_ZTYPE_SEQ &&
854 (ret > 0 || ret == -EIOCBQUEUED)) {
857 mutex_lock(&zi->i_truncate_mutex);
858 zi->i_wpoffset += count;
859 mutex_unlock(&zi->i_truncate_mutex);
868 static ssize_t zonefs_file_buffered_write(struct kiocb *iocb,
869 struct iov_iter *from)
871 struct inode *inode = file_inode(iocb->ki_filp);
872 struct zonefs_inode_info *zi = ZONEFS_I(inode);
876 * Direct IO writes are mandatory for sequential zone files so that the
877 * write IO issuing order is preserved.
879 if (zi->i_ztype != ZONEFS_ZTYPE_CNV)
882 if (iocb->ki_flags & IOCB_NOWAIT) {
883 if (!inode_trylock(inode))
889 ret = zonefs_write_checks(iocb, from);
893 ret = iomap_file_buffered_write(iocb, from, &zonefs_iomap_ops);
896 else if (ret == -EIO)
897 zonefs_io_error(inode, true);
902 ret = generic_write_sync(iocb, ret);
907 static ssize_t zonefs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
909 struct inode *inode = file_inode(iocb->ki_filp);
911 if (unlikely(IS_IMMUTABLE(inode)))
914 if (sb_rdonly(inode->i_sb))
917 /* Write operations beyond the zone size are not allowed */
918 if (iocb->ki_pos >= ZONEFS_I(inode)->i_max_size)
921 if (iocb->ki_flags & IOCB_DIRECT) {
922 ssize_t ret = zonefs_file_dio_write(iocb, from);
927 return zonefs_file_buffered_write(iocb, from);
930 static int zonefs_file_read_dio_end_io(struct kiocb *iocb, ssize_t size,
931 int error, unsigned int flags)
934 zonefs_io_error(file_inode(iocb->ki_filp), false);
941 static const struct iomap_dio_ops zonefs_read_dio_ops = {
942 .end_io = zonefs_file_read_dio_end_io,
945 static ssize_t zonefs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
947 struct inode *inode = file_inode(iocb->ki_filp);
948 struct zonefs_inode_info *zi = ZONEFS_I(inode);
949 struct super_block *sb = inode->i_sb;
953 /* Offline zones cannot be read */
954 if (unlikely(IS_IMMUTABLE(inode) && !(inode->i_mode & 0777)))
957 if (iocb->ki_pos >= zi->i_max_size)
960 if (iocb->ki_flags & IOCB_NOWAIT) {
961 if (!inode_trylock_shared(inode))
964 inode_lock_shared(inode);
967 /* Limit read operations to written data */
968 mutex_lock(&zi->i_truncate_mutex);
969 isize = i_size_read(inode);
970 if (iocb->ki_pos >= isize) {
971 mutex_unlock(&zi->i_truncate_mutex);
975 iov_iter_truncate(to, isize - iocb->ki_pos);
976 mutex_unlock(&zi->i_truncate_mutex);
978 if (iocb->ki_flags & IOCB_DIRECT) {
979 size_t count = iov_iter_count(to);
981 if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) {
985 file_accessed(iocb->ki_filp);
986 ret = iomap_dio_rw(iocb, to, &zonefs_iomap_ops,
987 &zonefs_read_dio_ops, 0, 0);
989 ret = generic_file_read_iter(iocb, to);
991 zonefs_io_error(inode, false);
995 inode_unlock_shared(inode);
1000 static inline bool zonefs_file_use_exp_open(struct inode *inode, struct file *file)
1002 struct zonefs_inode_info *zi = ZONEFS_I(inode);
1003 struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
1005 if (!(sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN))
1008 if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
1011 if (!(file->f_mode & FMODE_WRITE))
1017 static int zonefs_open_zone(struct inode *inode)
1019 struct zonefs_inode_info *zi = ZONEFS_I(inode);
1020 struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
1023 mutex_lock(&zi->i_truncate_mutex);
1025 if (!zi->i_wr_refcnt) {
1026 if (atomic_inc_return(&sbi->s_open_zones) > sbi->s_max_open_zones) {
1027 atomic_dec(&sbi->s_open_zones);
1032 if (i_size_read(inode) < zi->i_max_size) {
1033 ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_OPEN);
1035 atomic_dec(&sbi->s_open_zones);
1038 zi->i_flags |= ZONEFS_ZONE_OPEN;
1045 mutex_unlock(&zi->i_truncate_mutex);
1050 static int zonefs_file_open(struct inode *inode, struct file *file)
1054 ret = generic_file_open(inode, file);
1058 if (zonefs_file_use_exp_open(inode, file))
1059 return zonefs_open_zone(inode);
1064 static void zonefs_close_zone(struct inode *inode)
1066 struct zonefs_inode_info *zi = ZONEFS_I(inode);
1069 mutex_lock(&zi->i_truncate_mutex);
1071 if (!zi->i_wr_refcnt) {
1072 struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
1073 struct super_block *sb = inode->i_sb;
1076 * If the file zone is full, it is not open anymore and we only
1077 * need to decrement the open count.
1079 if (!(zi->i_flags & ZONEFS_ZONE_OPEN))
1082 ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_CLOSE);
1084 __zonefs_io_error(inode, false);
1086 * Leaving zones explicitly open may lead to a state
1087 * where most zones cannot be written (zone resources
1088 * exhausted). So take preventive action by remounting
1091 if (zi->i_flags & ZONEFS_ZONE_OPEN &&
1092 !(sb->s_flags & SB_RDONLY)) {
1093 zonefs_warn(sb, "closing zone failed, remounting filesystem read-only\n");
1094 sb->s_flags |= SB_RDONLY;
1097 zi->i_flags &= ~ZONEFS_ZONE_OPEN;
1099 atomic_dec(&sbi->s_open_zones);
1101 mutex_unlock(&zi->i_truncate_mutex);
1104 static int zonefs_file_release(struct inode *inode, struct file *file)
1107 * If we explicitly open a zone we must close it again as well, but the
1108 * zone management operation can fail (either due to an IO error or as
1109 * the zone has gone offline or read-only). Make sure we don't fail the
1110 * close(2) for user-space.
1112 if (zonefs_file_use_exp_open(inode, file))
1113 zonefs_close_zone(inode);
1118 static const struct file_operations zonefs_file_operations = {
1119 .open = zonefs_file_open,
1120 .release = zonefs_file_release,
1121 .fsync = zonefs_file_fsync,
1122 .mmap = zonefs_file_mmap,
1123 .llseek = zonefs_file_llseek,
1124 .read_iter = zonefs_file_read_iter,
1125 .write_iter = zonefs_file_write_iter,
1126 .splice_read = generic_file_splice_read,
1127 .splice_write = iter_file_splice_write,
1128 .iopoll = iocb_bio_iopoll,
1131 static struct kmem_cache *zonefs_inode_cachep;
1133 static struct inode *zonefs_alloc_inode(struct super_block *sb)
1135 struct zonefs_inode_info *zi;
1137 zi = alloc_inode_sb(sb, zonefs_inode_cachep, GFP_KERNEL);
1141 inode_init_once(&zi->i_vnode);
1142 mutex_init(&zi->i_truncate_mutex);
1143 zi->i_wr_refcnt = 0;
1145 return &zi->i_vnode;
1148 static void zonefs_free_inode(struct inode *inode)
1150 kmem_cache_free(zonefs_inode_cachep, ZONEFS_I(inode));
1156 static int zonefs_statfs(struct dentry *dentry, struct kstatfs *buf)
1158 struct super_block *sb = dentry->d_sb;
1159 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1160 enum zonefs_ztype t;
1162 buf->f_type = ZONEFS_MAGIC;
1163 buf->f_bsize = sb->s_blocksize;
1164 buf->f_namelen = ZONEFS_NAME_MAX;
1166 spin_lock(&sbi->s_lock);
1168 buf->f_blocks = sbi->s_blocks;
1169 if (WARN_ON(sbi->s_used_blocks > sbi->s_blocks))
1172 buf->f_bfree = buf->f_blocks - sbi->s_used_blocks;
1173 buf->f_bavail = buf->f_bfree;
1175 for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) {
1176 if (sbi->s_nr_files[t])
1177 buf->f_files += sbi->s_nr_files[t] + 1;
1181 spin_unlock(&sbi->s_lock);
1183 buf->f_fsid = uuid_to_fsid(sbi->s_uuid.b);
1189 Opt_errors_ro, Opt_errors_zro, Opt_errors_zol, Opt_errors_repair,
1190 Opt_explicit_open, Opt_err,
1193 static const match_table_t tokens = {
1194 { Opt_errors_ro, "errors=remount-ro"},
1195 { Opt_errors_zro, "errors=zone-ro"},
1196 { Opt_errors_zol, "errors=zone-offline"},
1197 { Opt_errors_repair, "errors=repair"},
1198 { Opt_explicit_open, "explicit-open" },
1202 static int zonefs_parse_options(struct super_block *sb, char *options)
1204 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1205 substring_t args[MAX_OPT_ARGS];
1211 while ((p = strsep(&options, ",")) != NULL) {
1217 token = match_token(p, tokens, args);
1220 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
1221 sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_RO;
1223 case Opt_errors_zro:
1224 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
1225 sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZRO;
1227 case Opt_errors_zol:
1228 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
1229 sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZOL;
1231 case Opt_errors_repair:
1232 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
1233 sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_REPAIR;
1235 case Opt_explicit_open:
1236 sbi->s_mount_opts |= ZONEFS_MNTOPT_EXPLICIT_OPEN;
1246 static int zonefs_show_options(struct seq_file *seq, struct dentry *root)
1248 struct zonefs_sb_info *sbi = ZONEFS_SB(root->d_sb);
1250 if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO)
1251 seq_puts(seq, ",errors=remount-ro");
1252 if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO)
1253 seq_puts(seq, ",errors=zone-ro");
1254 if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL)
1255 seq_puts(seq, ",errors=zone-offline");
1256 if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_REPAIR)
1257 seq_puts(seq, ",errors=repair");
1262 static int zonefs_remount(struct super_block *sb, int *flags, char *data)
1264 sync_filesystem(sb);
1266 return zonefs_parse_options(sb, data);
1269 static const struct super_operations zonefs_sops = {
1270 .alloc_inode = zonefs_alloc_inode,
1271 .free_inode = zonefs_free_inode,
1272 .statfs = zonefs_statfs,
1273 .remount_fs = zonefs_remount,
1274 .show_options = zonefs_show_options,
1277 static const struct inode_operations zonefs_dir_inode_operations = {
1278 .lookup = simple_lookup,
1279 .setattr = zonefs_inode_setattr,
1282 static void zonefs_init_dir_inode(struct inode *parent, struct inode *inode,
1283 enum zonefs_ztype type)
1285 struct super_block *sb = parent->i_sb;
1287 inode->i_ino = blkdev_nr_zones(sb->s_bdev->bd_disk) + type + 1;
1288 inode_init_owner(&init_user_ns, inode, parent, S_IFDIR | 0555);
1289 inode->i_op = &zonefs_dir_inode_operations;
1290 inode->i_fop = &simple_dir_operations;
1291 set_nlink(inode, 2);
1295 static void zonefs_init_file_inode(struct inode *inode, struct blk_zone *zone,
1296 enum zonefs_ztype type)
1298 struct super_block *sb = inode->i_sb;
1299 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1300 struct zonefs_inode_info *zi = ZONEFS_I(inode);
1302 inode->i_ino = zone->start >> sbi->s_zone_sectors_shift;
1303 inode->i_mode = S_IFREG | sbi->s_perm;
1306 zi->i_zsector = zone->start;
1307 zi->i_zone_size = zone->len << SECTOR_SHIFT;
1309 zi->i_max_size = min_t(loff_t, MAX_LFS_FILESIZE,
1310 zone->capacity << SECTOR_SHIFT);
1311 zi->i_wpoffset = zonefs_check_zone_condition(inode, zone, true, true);
1313 inode->i_uid = sbi->s_uid;
1314 inode->i_gid = sbi->s_gid;
1315 inode->i_size = zi->i_wpoffset;
1316 inode->i_blocks = zi->i_max_size >> SECTOR_SHIFT;
1318 inode->i_op = &zonefs_file_inode_operations;
1319 inode->i_fop = &zonefs_file_operations;
1320 inode->i_mapping->a_ops = &zonefs_file_aops;
1322 sb->s_maxbytes = max(zi->i_max_size, sb->s_maxbytes);
1323 sbi->s_blocks += zi->i_max_size >> sb->s_blocksize_bits;
1324 sbi->s_used_blocks += zi->i_wpoffset >> sb->s_blocksize_bits;
1327 static struct dentry *zonefs_create_inode(struct dentry *parent,
1328 const char *name, struct blk_zone *zone,
1329 enum zonefs_ztype type)
1331 struct inode *dir = d_inode(parent);
1332 struct dentry *dentry;
1333 struct inode *inode;
1335 dentry = d_alloc_name(parent, name);
1339 inode = new_inode(parent->d_sb);
1343 inode->i_ctime = inode->i_mtime = inode->i_atime = dir->i_ctime;
1345 zonefs_init_file_inode(inode, zone, type);
1347 zonefs_init_dir_inode(dir, inode, type);
1348 d_add(dentry, inode);
1359 struct zonefs_zone_data {
1360 struct super_block *sb;
1361 unsigned int nr_zones[ZONEFS_ZTYPE_MAX];
1362 struct blk_zone *zones;
1366 * Create a zone group and populate it with zone files.
1368 static int zonefs_create_zgroup(struct zonefs_zone_data *zd,
1369 enum zonefs_ztype type)
1371 struct super_block *sb = zd->sb;
1372 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1373 struct blk_zone *zone, *next, *end;
1374 const char *zgroup_name;
1380 /* If the group is empty, there is nothing to do */
1381 if (!zd->nr_zones[type])
1384 file_name = kmalloc(ZONEFS_NAME_MAX, GFP_KERNEL);
1388 if (type == ZONEFS_ZTYPE_CNV)
1389 zgroup_name = "cnv";
1391 zgroup_name = "seq";
1393 dir = zonefs_create_inode(sb->s_root, zgroup_name, NULL, type);
1400 * The first zone contains the super block: skip it.
1402 end = zd->zones + blkdev_nr_zones(sb->s_bdev->bd_disk);
1403 for (zone = &zd->zones[1]; zone < end; zone = next) {
1406 if (zonefs_zone_type(zone) != type)
1410 * For conventional zones, contiguous zones can be aggregated
1411 * together to form larger files. Note that this overwrites the
1412 * length of the first zone of the set of contiguous zones
1413 * aggregated together. If one offline or read-only zone is
1414 * found, assume that all zones aggregated have the same
1417 if (type == ZONEFS_ZTYPE_CNV &&
1418 (sbi->s_features & ZONEFS_F_AGGRCNV)) {
1419 for (; next < end; next++) {
1420 if (zonefs_zone_type(next) != type)
1422 zone->len += next->len;
1423 zone->capacity += next->capacity;
1424 if (next->cond == BLK_ZONE_COND_READONLY &&
1425 zone->cond != BLK_ZONE_COND_OFFLINE)
1426 zone->cond = BLK_ZONE_COND_READONLY;
1427 else if (next->cond == BLK_ZONE_COND_OFFLINE)
1428 zone->cond = BLK_ZONE_COND_OFFLINE;
1430 if (zone->capacity != zone->len) {
1431 zonefs_err(sb, "Invalid conventional zone capacity\n");
1438 * Use the file number within its group as file name.
1440 snprintf(file_name, ZONEFS_NAME_MAX - 1, "%u", n);
1441 if (!zonefs_create_inode(dir, file_name, zone, type)) {
1449 zonefs_info(sb, "Zone group \"%s\" has %u file%s\n",
1450 zgroup_name, n, n > 1 ? "s" : "");
1452 sbi->s_nr_files[type] = n;
1461 static int zonefs_get_zone_info_cb(struct blk_zone *zone, unsigned int idx,
1464 struct zonefs_zone_data *zd = data;
1467 * Count the number of usable zones: the first zone at index 0 contains
1468 * the super block and is ignored.
1470 switch (zone->type) {
1471 case BLK_ZONE_TYPE_CONVENTIONAL:
1472 zone->wp = zone->start + zone->len;
1474 zd->nr_zones[ZONEFS_ZTYPE_CNV]++;
1476 case BLK_ZONE_TYPE_SEQWRITE_REQ:
1477 case BLK_ZONE_TYPE_SEQWRITE_PREF:
1479 zd->nr_zones[ZONEFS_ZTYPE_SEQ]++;
1482 zonefs_err(zd->sb, "Unsupported zone type 0x%x\n",
1487 memcpy(&zd->zones[idx], zone, sizeof(struct blk_zone));
1492 static int zonefs_get_zone_info(struct zonefs_zone_data *zd)
1494 struct block_device *bdev = zd->sb->s_bdev;
1497 zd->zones = kvcalloc(blkdev_nr_zones(bdev->bd_disk),
1498 sizeof(struct blk_zone), GFP_KERNEL);
1502 /* Get zones information from the device */
1503 ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES,
1504 zonefs_get_zone_info_cb, zd);
1506 zonefs_err(zd->sb, "Zone report failed %d\n", ret);
1510 if (ret != blkdev_nr_zones(bdev->bd_disk)) {
1511 zonefs_err(zd->sb, "Invalid zone report (%d/%u zones)\n",
1512 ret, blkdev_nr_zones(bdev->bd_disk));
1519 static inline void zonefs_cleanup_zone_info(struct zonefs_zone_data *zd)
1525 * Read super block information from the device.
1527 static int zonefs_read_super(struct super_block *sb)
1529 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1530 struct zonefs_super *super;
1531 u32 crc, stored_crc;
1533 struct bio_vec bio_vec;
1537 page = alloc_page(GFP_KERNEL);
1541 bio_init(&bio, sb->s_bdev, &bio_vec, 1, REQ_OP_READ);
1542 bio.bi_iter.bi_sector = 0;
1543 bio_add_page(&bio, page, PAGE_SIZE, 0);
1545 ret = submit_bio_wait(&bio);
1552 if (le32_to_cpu(super->s_magic) != ZONEFS_MAGIC)
1555 stored_crc = le32_to_cpu(super->s_crc);
1557 crc = crc32(~0U, (unsigned char *)super, sizeof(struct zonefs_super));
1558 if (crc != stored_crc) {
1559 zonefs_err(sb, "Invalid checksum (Expected 0x%08x, got 0x%08x)",
1564 sbi->s_features = le64_to_cpu(super->s_features);
1565 if (sbi->s_features & ~ZONEFS_F_DEFINED_FEATURES) {
1566 zonefs_err(sb, "Unknown features set 0x%llx\n",
1571 if (sbi->s_features & ZONEFS_F_UID) {
1572 sbi->s_uid = make_kuid(current_user_ns(),
1573 le32_to_cpu(super->s_uid));
1574 if (!uid_valid(sbi->s_uid)) {
1575 zonefs_err(sb, "Invalid UID feature\n");
1580 if (sbi->s_features & ZONEFS_F_GID) {
1581 sbi->s_gid = make_kgid(current_user_ns(),
1582 le32_to_cpu(super->s_gid));
1583 if (!gid_valid(sbi->s_gid)) {
1584 zonefs_err(sb, "Invalid GID feature\n");
1589 if (sbi->s_features & ZONEFS_F_PERM)
1590 sbi->s_perm = le32_to_cpu(super->s_perm);
1592 if (memchr_inv(super->s_reserved, 0, sizeof(super->s_reserved))) {
1593 zonefs_err(sb, "Reserved area is being used\n");
1597 import_uuid(&sbi->s_uuid, super->s_uuid);
1609 * Check that the device is zoned. If it is, get the list of zones and create
1610 * sub-directories and files according to the device zone configuration and
1613 static int zonefs_fill_super(struct super_block *sb, void *data, int silent)
1615 struct zonefs_zone_data zd;
1616 struct zonefs_sb_info *sbi;
1617 struct inode *inode;
1618 enum zonefs_ztype t;
1621 if (!bdev_is_zoned(sb->s_bdev)) {
1622 zonefs_err(sb, "Not a zoned block device\n");
1627 * Initialize super block information: the maximum file size is updated
1628 * when the zone files are created so that the format option
1629 * ZONEFS_F_AGGRCNV which increases the maximum file size of a file
1630 * beyond the zone size is taken into account.
1632 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1636 spin_lock_init(&sbi->s_lock);
1637 sb->s_fs_info = sbi;
1638 sb->s_magic = ZONEFS_MAGIC;
1640 sb->s_op = &zonefs_sops;
1641 sb->s_time_gran = 1;
1644 * The block size is set to the device zone write granularity to ensure
1645 * that write operations are always aligned according to the device
1646 * interface constraints.
1648 sb_set_blocksize(sb, bdev_zone_write_granularity(sb->s_bdev));
1649 sbi->s_zone_sectors_shift = ilog2(bdev_zone_sectors(sb->s_bdev));
1650 sbi->s_uid = GLOBAL_ROOT_UID;
1651 sbi->s_gid = GLOBAL_ROOT_GID;
1653 sbi->s_mount_opts = ZONEFS_MNTOPT_ERRORS_RO;
1654 sbi->s_max_open_zones = bdev_max_open_zones(sb->s_bdev);
1655 atomic_set(&sbi->s_open_zones, 0);
1656 if (!sbi->s_max_open_zones &&
1657 sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) {
1658 zonefs_info(sb, "No open zones limit. Ignoring explicit_open mount option\n");
1659 sbi->s_mount_opts &= ~ZONEFS_MNTOPT_EXPLICIT_OPEN;
1662 ret = zonefs_read_super(sb);
1666 ret = zonefs_parse_options(sb, data);
1670 memset(&zd, 0, sizeof(struct zonefs_zone_data));
1672 ret = zonefs_get_zone_info(&zd);
1676 zonefs_info(sb, "Mounting %u zones",
1677 blkdev_nr_zones(sb->s_bdev->bd_disk));
1679 /* Create root directory inode */
1681 inode = new_inode(sb);
1685 inode->i_ino = blkdev_nr_zones(sb->s_bdev->bd_disk);
1686 inode->i_mode = S_IFDIR | 0555;
1687 inode->i_ctime = inode->i_mtime = inode->i_atime = current_time(inode);
1688 inode->i_op = &zonefs_dir_inode_operations;
1689 inode->i_fop = &simple_dir_operations;
1690 set_nlink(inode, 2);
1692 sb->s_root = d_make_root(inode);
1696 /* Create and populate files in zone groups directories */
1697 for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) {
1698 ret = zonefs_create_zgroup(&zd, t);
1704 zonefs_cleanup_zone_info(&zd);
1709 static struct dentry *zonefs_mount(struct file_system_type *fs_type,
1710 int flags, const char *dev_name, void *data)
1712 return mount_bdev(fs_type, flags, dev_name, data, zonefs_fill_super);
1715 static void zonefs_kill_super(struct super_block *sb)
1717 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
1720 d_genocide(sb->s_root);
1721 kill_block_super(sb);
1726 * File system definition and registration.
1728 static struct file_system_type zonefs_type = {
1729 .owner = THIS_MODULE,
1731 .mount = zonefs_mount,
1732 .kill_sb = zonefs_kill_super,
1733 .fs_flags = FS_REQUIRES_DEV,
1736 static int __init zonefs_init_inodecache(void)
1738 zonefs_inode_cachep = kmem_cache_create("zonefs_inode_cache",
1739 sizeof(struct zonefs_inode_info), 0,
1740 (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT),
1742 if (zonefs_inode_cachep == NULL)
1747 static void zonefs_destroy_inodecache(void)
1750 * Make sure all delayed rcu free inodes are flushed before we
1751 * destroy the inode cache.
1754 kmem_cache_destroy(zonefs_inode_cachep);
1757 static int __init zonefs_init(void)
1761 BUILD_BUG_ON(sizeof(struct zonefs_super) != ZONEFS_SUPER_SIZE);
1763 ret = zonefs_init_inodecache();
1767 ret = register_filesystem(&zonefs_type);
1769 zonefs_destroy_inodecache();
1776 static void __exit zonefs_exit(void)
1778 zonefs_destroy_inodecache();
1779 unregister_filesystem(&zonefs_type);
1782 MODULE_AUTHOR("Damien Le Moal");
1783 MODULE_DESCRIPTION("Zone file system for zoned block devices");
1784 MODULE_LICENSE("GPL");
1785 MODULE_ALIAS_FS("zonefs");
1786 module_init(zonefs_init);
1787 module_exit(zonefs_exit);