^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Simple file system for zoned block devices exposing zones as files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2019 Western Digital Corporation or its affiliates.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/magic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/iomap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/statfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/writeback.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/quotaops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/parser.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/uio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/sched/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/crc32.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/task_io_accounting_ops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include "zonefs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static inline int zonefs_zone_mgmt(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) enum req_opf op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct zonefs_inode_info *zi = ZONEFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) lockdep_assert_held(&zi->i_truncate_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) ret = blkdev_zone_mgmt(inode->i_sb->s_bdev, op, zi->i_zsector,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) zi->i_zone_size >> SECTOR_SHIFT, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) zonefs_err(inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) "Zone management operation %s at %llu failed %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) blk_op_str(op), zi->i_zsector, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static inline void zonefs_i_size_write(struct inode *inode, loff_t isize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct zonefs_inode_info *zi = ZONEFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) i_size_write(inode, isize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * A full zone is no longer open/active and does not need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * explicit closing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (isize >= zi->i_max_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) zi->i_flags &= ~ZONEFS_ZONE_OPEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static int zonefs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) unsigned int flags, struct iomap *iomap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct iomap *srcmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct zonefs_inode_info *zi = ZONEFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) loff_t isize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) /* All I/Os should always be within the file maximum size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (WARN_ON_ONCE(offset + length > zi->i_max_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * Sequential zones can only accept direct writes. This is already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * checked when writes are issued, so warn if we see a page writeback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (WARN_ON_ONCE(zi->i_ztype == ZONEFS_ZTYPE_SEQ &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) (flags & IOMAP_WRITE) && !(flags & IOMAP_DIRECT)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * For conventional zones, all blocks are always mapped. For sequential
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * zones, all blocks after always mapped below the inode size (zone
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * write pointer) and unwriten beyond.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) mutex_lock(&zi->i_truncate_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) isize = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (offset >= isize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) iomap->type = IOMAP_UNWRITTEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) iomap->type = IOMAP_MAPPED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (flags & IOMAP_WRITE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) length = zi->i_max_size - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) length = min(length, isize - offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) mutex_unlock(&zi->i_truncate_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) iomap->offset = ALIGN_DOWN(offset, sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) iomap->length = ALIGN(offset + length, sb->s_blocksize) - iomap->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) iomap->bdev = inode->i_sb->s_bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) iomap->addr = (zi->i_zsector << SECTOR_SHIFT) + iomap->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static const struct iomap_ops zonefs_iomap_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) .iomap_begin = zonefs_iomap_begin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static int zonefs_readpage(struct file *unused, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return iomap_readpage(page, &zonefs_iomap_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static void zonefs_readahead(struct readahead_control *rac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) iomap_readahead(rac, &zonefs_iomap_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * Map blocks for page writeback. This is used only on conventional zone files,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * which implies that the page range can only be within the fixed inode size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static int zonefs_map_blocks(struct iomap_writepage_ctx *wpc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct inode *inode, loff_t offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct zonefs_inode_info *zi = ZONEFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (WARN_ON_ONCE(zi->i_ztype != ZONEFS_ZTYPE_CNV))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (WARN_ON_ONCE(offset >= i_size_read(inode)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) /* If the mapping is already OK, nothing needs to be done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (offset >= wpc->iomap.offset &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) offset < wpc->iomap.offset + wpc->iomap.length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return zonefs_iomap_begin(inode, offset, zi->i_max_size - offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) IOMAP_WRITE, &wpc->iomap, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static const struct iomap_writeback_ops zonefs_writeback_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) .map_blocks = zonefs_map_blocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static int zonefs_writepage(struct page *page, struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) struct iomap_writepage_ctx wpc = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return iomap_writepage(page, wbc, &wpc, &zonefs_writeback_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) static int zonefs_writepages(struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) struct iomap_writepage_ctx wpc = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return iomap_writepages(mapping, wbc, &wpc, &zonefs_writeback_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static int zonefs_swap_activate(struct swap_info_struct *sis,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) struct file *swap_file, sector_t *span)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) struct inode *inode = file_inode(swap_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct zonefs_inode_info *zi = ZONEFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (zi->i_ztype != ZONEFS_ZTYPE_CNV) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) zonefs_err(inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) "swap file: not a conventional zone file\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return iomap_swapfile_activate(sis, swap_file, span, &zonefs_iomap_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) static const struct address_space_operations zonefs_file_aops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) .readpage = zonefs_readpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) .readahead = zonefs_readahead,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) .writepage = zonefs_writepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) .writepages = zonefs_writepages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) .set_page_dirty = iomap_set_page_dirty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) .releasepage = iomap_releasepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) .invalidatepage = iomap_invalidatepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) .migratepage = iomap_migrate_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) .is_partially_uptodate = iomap_is_partially_uptodate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) .error_remove_page = generic_error_remove_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) .direct_IO = noop_direct_IO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) .swap_activate = zonefs_swap_activate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) static void zonefs_update_stats(struct inode *inode, loff_t new_isize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) loff_t old_isize = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) loff_t nr_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) if (new_isize == old_isize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) spin_lock(&sbi->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * This may be called for an update after an IO error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * So beware of the values seen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if (new_isize < old_isize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) nr_blocks = (old_isize - new_isize) >> sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) if (sbi->s_used_blocks > nr_blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) sbi->s_used_blocks -= nr_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) sbi->s_used_blocks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) sbi->s_used_blocks +=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) (new_isize - old_isize) >> sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (sbi->s_used_blocks > sbi->s_blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) sbi->s_used_blocks = sbi->s_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) spin_unlock(&sbi->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * Check a zone condition and adjust its file inode access permissions for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * offline and readonly zones. Return the inode size corresponding to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * amount of readable data in the zone.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) static loff_t zonefs_check_zone_condition(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) struct blk_zone *zone, bool warn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) bool mount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) struct zonefs_inode_info *zi = ZONEFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) switch (zone->cond) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) case BLK_ZONE_COND_OFFLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) * Dead zone: make the inode immutable, disable all accesses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * and set the file size to 0 (zone wp set to zone start).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) if (warn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) zonefs_warn(inode->i_sb, "inode %lu: offline zone\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) inode->i_flags |= S_IMMUTABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) inode->i_mode &= ~0777;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) zone->wp = zone->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) case BLK_ZONE_COND_READONLY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * The write pointer of read-only zones is invalid. If such a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * zone is found during mount, the file size cannot be retrieved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * so we treat the zone as offline (mount == true case).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) * Otherwise, keep the file size as it was when last updated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) * so that the user can recover data. In both cases, writes are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) * always disabled for the zone.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (warn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) zonefs_warn(inode->i_sb, "inode %lu: read-only zone\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) inode->i_flags |= S_IMMUTABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) if (mount) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) zone->cond = BLK_ZONE_COND_OFFLINE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) inode->i_mode &= ~0777;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) zone->wp = zone->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) inode->i_mode &= ~0222;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) return i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) case BLK_ZONE_COND_FULL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) /* The write pointer of full zones is invalid. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return zi->i_max_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) if (zi->i_ztype == ZONEFS_ZTYPE_CNV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) return zi->i_max_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) return (zone->wp - zone->start) << SECTOR_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) struct zonefs_ioerr_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) bool write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) static int zonefs_io_error_cb(struct blk_zone *zone, unsigned int idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) struct zonefs_ioerr_data *err = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) struct inode *inode = err->inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) struct zonefs_inode_info *zi = ZONEFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) loff_t isize, data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * Check the zone condition: if the zone is not "bad" (offline or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) * read-only), read errors are simply signaled to the IO issuer as long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) * as there is no inconsistency between the inode size and the amount of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) * data writen in the zone (data_size).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) data_size = zonefs_check_zone_condition(inode, zone, true, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) isize = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (zone->cond != BLK_ZONE_COND_OFFLINE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) zone->cond != BLK_ZONE_COND_READONLY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) !err->write && isize == data_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * At this point, we detected either a bad zone or an inconsistency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) * between the inode size and the amount of data written in the zone.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) * For the latter case, the cause may be a write IO error or an external
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) * action on the device. Two error patterns exist:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) * 1) The inode size is lower than the amount of data in the zone:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) * a write operation partially failed and data was writen at the end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) * of the file. This can happen in the case of a large direct IO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) * needing several BIOs and/or write requests to be processed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) * 2) The inode size is larger than the amount of data in the zone:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * this can happen with a deferred write error with the use of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * device side write cache after getting successful write IO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) * completions. Other possibilities are (a) an external corruption,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * e.g. an application reset the zone directly, or (b) the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * has a serious problem (e.g. firmware bug).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * In all cases, warn about inode size inconsistency and handle the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * IO error according to the zone condition and to the mount options.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (zi->i_ztype == ZONEFS_ZTYPE_SEQ && isize != data_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) zonefs_warn(sb, "inode %lu: invalid size %lld (should be %lld)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) inode->i_ino, isize, data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) * First handle bad zones signaled by hardware. The mount options
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) * errors=zone-ro and errors=zone-offline result in changing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) * zone condition to read-only and offline respectively, as if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) * condition was signaled by the hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) if (zone->cond == BLK_ZONE_COND_OFFLINE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) zonefs_warn(sb, "inode %lu: read/write access disabled\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (zone->cond != BLK_ZONE_COND_OFFLINE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) zone->cond = BLK_ZONE_COND_OFFLINE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) data_size = zonefs_check_zone_condition(inode, zone,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) false, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) } else if (zone->cond == BLK_ZONE_COND_READONLY ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) zonefs_warn(sb, "inode %lu: write access disabled\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) if (zone->cond != BLK_ZONE_COND_READONLY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) zone->cond = BLK_ZONE_COND_READONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) data_size = zonefs_check_zone_condition(inode, zone,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) false, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) * If the filesystem is mounted with the explicit-open mount option, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) * need to clear the ZONEFS_ZONE_OPEN flag if the zone transitioned to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) * the read-only or offline condition, to avoid attempting an explicit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) * close of the zone when the inode file is closed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) if ((sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) (zone->cond == BLK_ZONE_COND_OFFLINE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) zone->cond == BLK_ZONE_COND_READONLY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) zi->i_flags &= ~ZONEFS_ZONE_OPEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) * If error=remount-ro was specified, any error result in remounting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) * the volume as read-only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if ((sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO) && !sb_rdonly(sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) zonefs_warn(sb, "remounting filesystem read-only\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) sb->s_flags |= SB_RDONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) * Update block usage stats and the inode size to prevent access to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) * invalid data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) zonefs_update_stats(inode, data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) zonefs_i_size_write(inode, data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) zi->i_wpoffset = data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) * When an file IO error occurs, check the file zone to see if there is a change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) * in the zone condition (e.g. offline or read-only). For a failed write to a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) * sequential zone, the zone write pointer position must also be checked to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) * eventually correct the file size and zonefs inode write pointer offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) * (which can be out of sync with the drive due to partial write failures).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) static void __zonefs_io_error(struct inode *inode, bool write)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) struct zonefs_inode_info *zi = ZONEFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) unsigned int noio_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) unsigned int nr_zones =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) zi->i_zone_size >> (sbi->s_zone_sectors_shift + SECTOR_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) struct zonefs_ioerr_data err = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) .inode = inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) .write = write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) * Memory allocations in blkdev_report_zones() can trigger a memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) * reclaim which may in turn cause a recursion into zonefs as well as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) * struct request allocations for the same device. The former case may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) * end up in a deadlock on the inode truncate mutex, while the latter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) * may prevent IO forward progress. Executing the report zones under
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) * the GFP_NOIO context avoids both problems.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) noio_flag = memalloc_noio_save();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) ret = blkdev_report_zones(sb->s_bdev, zi->i_zsector, nr_zones,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) zonefs_io_error_cb, &err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) if (ret != nr_zones)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) zonefs_err(sb, "Get inode %lu zone information failed %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) inode->i_ino, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) memalloc_noio_restore(noio_flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) static void zonefs_io_error(struct inode *inode, bool write)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) struct zonefs_inode_info *zi = ZONEFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) mutex_lock(&zi->i_truncate_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) __zonefs_io_error(inode, write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) mutex_unlock(&zi->i_truncate_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) static int zonefs_file_truncate(struct inode *inode, loff_t isize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) struct zonefs_inode_info *zi = ZONEFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) loff_t old_isize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) enum req_opf op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) * Only sequential zone files can be truncated and truncation is allowed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) * only down to a 0 size, which is equivalent to a zone reset, and to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) * the maximum file size, which is equivalent to a zone finish.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) if (!isize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) op = REQ_OP_ZONE_RESET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) else if (isize == zi->i_max_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) op = REQ_OP_ZONE_FINISH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) inode_dio_wait(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) /* Serialize against page faults */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) down_write(&zi->i_mmap_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) /* Serialize against zonefs_iomap_begin() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) mutex_lock(&zi->i_truncate_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) old_isize = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) if (isize == old_isize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) ret = zonefs_zone_mgmt(inode, op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) * If the mount option ZONEFS_MNTOPT_EXPLICIT_OPEN is set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) * take care of open zones.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) if (zi->i_flags & ZONEFS_ZONE_OPEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) * Truncating a zone to EMPTY or FULL is the equivalent of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) * closing the zone. For a truncation to 0, we need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) * re-open the zone to ensure new writes can be processed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) * For a truncation to the maximum file size, the zone is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) * closed and writes cannot be accepted anymore, so clear
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) * the open flag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) if (!isize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_OPEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) zi->i_flags &= ~ZONEFS_ZONE_OPEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) zonefs_update_stats(inode, isize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) truncate_setsize(inode, isize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) zi->i_wpoffset = isize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) mutex_unlock(&zi->i_truncate_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) up_write(&zi->i_mmap_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) static int zonefs_inode_setattr(struct dentry *dentry, struct iattr *iattr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) if (unlikely(IS_IMMUTABLE(inode)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) ret = setattr_prepare(dentry, iattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) * Since files and directories cannot be created nor deleted, do not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) * allow setting any write attributes on the sub-directories grouping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) * files by zone type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) if ((iattr->ia_valid & ATTR_MODE) && S_ISDIR(inode->i_mode) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) (iattr->ia_mode & 0222))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) if (((iattr->ia_valid & ATTR_UID) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) !uid_eq(iattr->ia_uid, inode->i_uid)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) ((iattr->ia_valid & ATTR_GID) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) !gid_eq(iattr->ia_gid, inode->i_gid))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) ret = dquot_transfer(inode, iattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) if (iattr->ia_valid & ATTR_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) ret = zonefs_file_truncate(inode, iattr->ia_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) setattr_copy(inode, iattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) static const struct inode_operations zonefs_file_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) .setattr = zonefs_inode_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) static int zonefs_file_fsync(struct file *file, loff_t start, loff_t end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) int datasync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) if (unlikely(IS_IMMUTABLE(inode)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) * Since only direct writes are allowed in sequential files, page cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) * flush is needed only for conventional zone files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) if (ZONEFS_I(inode)->i_ztype == ZONEFS_ZTYPE_CNV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) ret = file_write_and_wait_range(file, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) ret = blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) zonefs_io_error(inode, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) static vm_fault_t zonefs_filemap_fault(struct vm_fault *vmf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) struct zonefs_inode_info *zi = ZONEFS_I(file_inode(vmf->vma->vm_file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) vm_fault_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) down_read(&zi->i_mmap_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) ret = filemap_fault(vmf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) up_read(&zi->i_mmap_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) static vm_fault_t zonefs_filemap_page_mkwrite(struct vm_fault *vmf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) struct inode *inode = file_inode(vmf->vma->vm_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) struct zonefs_inode_info *zi = ZONEFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) vm_fault_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) if (unlikely(IS_IMMUTABLE(inode)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) return VM_FAULT_SIGBUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) * Sanity check: only conventional zone files can have shared
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) * writeable mappings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) if (WARN_ON_ONCE(zi->i_ztype != ZONEFS_ZTYPE_CNV))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) return VM_FAULT_NOPAGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) sb_start_pagefault(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) file_update_time(vmf->vma->vm_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) /* Serialize against truncates */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) down_read(&zi->i_mmap_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) ret = iomap_page_mkwrite(vmf, &zonefs_iomap_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) up_read(&zi->i_mmap_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) sb_end_pagefault(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) static const struct vm_operations_struct zonefs_file_vm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) .fault = zonefs_filemap_fault,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) .map_pages = filemap_map_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) .page_mkwrite = zonefs_filemap_page_mkwrite,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) static int zonefs_file_mmap(struct file *file, struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) * Conventional zones accept random writes, so their files can support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) * shared writable mappings. For sequential zone files, only read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) * mappings are possible since there are no guarantees for write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) * ordering between msync() and page cache writeback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) if (ZONEFS_I(file_inode(file))->i_ztype == ZONEFS_ZTYPE_SEQ &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) (vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) file_accessed(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) vma->vm_ops = &zonefs_file_vm_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) static loff_t zonefs_file_llseek(struct file *file, loff_t offset, int whence)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) loff_t isize = i_size_read(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) * Seeks are limited to below the zone size for conventional zones
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) * and below the zone write pointer for sequential zones. In both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) * cases, this limit is the inode size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) return generic_file_llseek_size(file, offset, whence, isize, isize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) static int zonefs_file_write_dio_end_io(struct kiocb *iocb, ssize_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) int error, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) struct inode *inode = file_inode(iocb->ki_filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) struct zonefs_inode_info *zi = ZONEFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) zonefs_io_error(inode, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) if (size && zi->i_ztype != ZONEFS_ZTYPE_CNV) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) * Note that we may be seeing completions out of order,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) * but that is not a problem since a write completed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) * successfully necessarily means that all preceding writes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) * were also successful. So we can safely increase the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) * size to the write end location.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) mutex_lock(&zi->i_truncate_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) if (i_size_read(inode) < iocb->ki_pos + size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) zonefs_update_stats(inode, iocb->ki_pos + size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) zonefs_i_size_write(inode, iocb->ki_pos + size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) mutex_unlock(&zi->i_truncate_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) static const struct iomap_dio_ops zonefs_write_dio_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) .end_io = zonefs_file_write_dio_end_io,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) static ssize_t zonefs_file_dio_append(struct kiocb *iocb, struct iov_iter *from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) struct inode *inode = file_inode(iocb->ki_filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) struct zonefs_inode_info *zi = ZONEFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) struct block_device *bdev = inode->i_sb->s_bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) unsigned int max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) struct bio *bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) ssize_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) int nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) max = queue_max_zone_append_sectors(bdev_get_queue(bdev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) max = ALIGN_DOWN(max << SECTOR_SHIFT, inode->i_sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) iov_iter_truncate(from, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) nr_pages = iov_iter_npages(from, BIO_MAX_PAGES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) if (!nr_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) bio = bio_alloc_bioset(GFP_NOFS, nr_pages, &fs_bio_set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) if (!bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) bio_set_dev(bio, bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) bio->bi_iter.bi_sector = zi->i_zsector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) bio->bi_write_hint = iocb->ki_hint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) bio->bi_ioprio = iocb->ki_ioprio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) bio->bi_opf = REQ_OP_ZONE_APPEND | REQ_SYNC | REQ_IDLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) if (iocb->ki_flags & IOCB_DSYNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) bio->bi_opf |= REQ_FUA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) ret = bio_iov_iter_get_pages(bio, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) if (unlikely(ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) goto out_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) size = bio->bi_iter.bi_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) task_io_account_write(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) if (iocb->ki_flags & IOCB_HIPRI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) bio_set_polled(bio, iocb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) ret = submit_bio_wait(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) zonefs_file_write_dio_end_io(iocb, size, ret, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) out_release:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) bio_release_pages(bio, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) bio_put(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) if (ret >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) iocb->ki_pos += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) * Do not exceed the LFS limits nor the file zone size. If pos is under the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) * limit it becomes a short access. If it exceeds the limit, return -EFBIG.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) static loff_t zonefs_write_check_limits(struct file *file, loff_t pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) loff_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) struct zonefs_inode_info *zi = ZONEFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) loff_t limit = rlimit(RLIMIT_FSIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) loff_t max_size = zi->i_max_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) if (limit != RLIM_INFINITY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) if (pos >= limit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) send_sig(SIGXFSZ, current, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) return -EFBIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) count = min(count, limit - pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) if (!(file->f_flags & O_LARGEFILE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) max_size = min_t(loff_t, MAX_NON_LFS, max_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) if (unlikely(pos >= max_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) return -EFBIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) return min(count, max_size - pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) static ssize_t zonefs_write_checks(struct kiocb *iocb, struct iov_iter *from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) struct file *file = iocb->ki_filp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) struct zonefs_inode_info *zi = ZONEFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) loff_t count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) if (IS_SWAPFILE(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) return -ETXTBSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) if (!iov_iter_count(from))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) if (iocb->ki_flags & IOCB_APPEND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) mutex_lock(&zi->i_truncate_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) iocb->ki_pos = zi->i_wpoffset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) mutex_unlock(&zi->i_truncate_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) count = zonefs_write_check_limits(file, iocb->ki_pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) iov_iter_count(from));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) if (count < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) iov_iter_truncate(from, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) return iov_iter_count(from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) * Handle direct writes. For sequential zone files, this is the only possible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) * write path. For these files, check that the user is issuing writes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) * sequentially from the end of the file. This code assumes that the block layer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) * delivers write requests to the device in sequential order. This is always the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) * case if a block IO scheduler implementing the ELEVATOR_F_ZBD_SEQ_WRITE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) * elevator feature is being used (e.g. mq-deadline). The block layer always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) * automatically select such an elevator for zoned block devices during the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) * device initialization.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) static ssize_t zonefs_file_dio_write(struct kiocb *iocb, struct iov_iter *from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) struct inode *inode = file_inode(iocb->ki_filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) struct zonefs_inode_info *zi = ZONEFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) bool sync = is_sync_kiocb(iocb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) bool append = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) ssize_t ret, count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) * For async direct IOs to sequential zone files, refuse IOCB_NOWAIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) * as this can cause write reordering (e.g. the first aio gets EAGAIN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) * on the inode lock but the second goes through but is now unaligned).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) if (zi->i_ztype == ZONEFS_ZTYPE_SEQ && !sync &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) (iocb->ki_flags & IOCB_NOWAIT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) if (iocb->ki_flags & IOCB_NOWAIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) if (!inode_trylock(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) count = zonefs_write_checks(iocb, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) if (count <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) goto inode_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) goto inode_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) /* Enforce sequential writes (append only) in sequential zones */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) if (zi->i_ztype == ZONEFS_ZTYPE_SEQ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) mutex_lock(&zi->i_truncate_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) if (iocb->ki_pos != zi->i_wpoffset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) mutex_unlock(&zi->i_truncate_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) goto inode_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) mutex_unlock(&zi->i_truncate_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) append = sync;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) if (append)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) ret = zonefs_file_dio_append(iocb, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) ret = iomap_dio_rw(iocb, from, &zonefs_iomap_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) &zonefs_write_dio_ops, sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) if (zi->i_ztype == ZONEFS_ZTYPE_SEQ &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) (ret > 0 || ret == -EIOCBQUEUED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) count = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) mutex_lock(&zi->i_truncate_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) zi->i_wpoffset += count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) mutex_unlock(&zi->i_truncate_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) inode_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) static ssize_t zonefs_file_buffered_write(struct kiocb *iocb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) struct iov_iter *from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) struct inode *inode = file_inode(iocb->ki_filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) struct zonefs_inode_info *zi = ZONEFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) * Direct IO writes are mandatory for sequential zone files so that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) * write IO issuing order is preserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) if (zi->i_ztype != ZONEFS_ZTYPE_CNV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) if (iocb->ki_flags & IOCB_NOWAIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) if (!inode_trylock(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) ret = zonefs_write_checks(iocb, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) if (ret <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) goto inode_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) ret = iomap_file_buffered_write(iocb, from, &zonefs_iomap_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) iocb->ki_pos += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) else if (ret == -EIO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) zonefs_io_error(inode, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) inode_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) ret = generic_write_sync(iocb, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) static ssize_t zonefs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) struct inode *inode = file_inode(iocb->ki_filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) if (unlikely(IS_IMMUTABLE(inode)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) if (sb_rdonly(inode->i_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) return -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) /* Write operations beyond the zone size are not allowed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) if (iocb->ki_pos >= ZONEFS_I(inode)->i_max_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) return -EFBIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) if (iocb->ki_flags & IOCB_DIRECT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) ssize_t ret = zonefs_file_dio_write(iocb, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) if (ret != -ENOTBLK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) return zonefs_file_buffered_write(iocb, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) static int zonefs_file_read_dio_end_io(struct kiocb *iocb, ssize_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) int error, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) zonefs_io_error(file_inode(iocb->ki_filp), false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) static const struct iomap_dio_ops zonefs_read_dio_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) .end_io = zonefs_file_read_dio_end_io,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) static ssize_t zonefs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) struct inode *inode = file_inode(iocb->ki_filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) struct zonefs_inode_info *zi = ZONEFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) loff_t isize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) /* Offline zones cannot be read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) if (unlikely(IS_IMMUTABLE(inode) && !(inode->i_mode & 0777)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) if (iocb->ki_pos >= zi->i_max_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) if (iocb->ki_flags & IOCB_NOWAIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) if (!inode_trylock_shared(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) inode_lock_shared(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) /* Limit read operations to written data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) mutex_lock(&zi->i_truncate_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) isize = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) if (iocb->ki_pos >= isize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) mutex_unlock(&zi->i_truncate_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) goto inode_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) iov_iter_truncate(to, isize - iocb->ki_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) mutex_unlock(&zi->i_truncate_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) if (iocb->ki_flags & IOCB_DIRECT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) size_t count = iov_iter_count(to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) goto inode_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) file_accessed(iocb->ki_filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) ret = iomap_dio_rw(iocb, to, &zonefs_iomap_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) &zonefs_read_dio_ops, is_sync_kiocb(iocb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) ret = generic_file_read_iter(iocb, to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) if (ret == -EIO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) zonefs_io_error(inode, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) inode_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) inode_unlock_shared(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) static inline bool zonefs_file_use_exp_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) struct zonefs_inode_info *zi = ZONEFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) if (!(sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) if (zi->i_ztype != ZONEFS_ZTYPE_SEQ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) if (!(file->f_mode & FMODE_WRITE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) static int zonefs_open_zone(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) struct zonefs_inode_info *zi = ZONEFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) mutex_lock(&zi->i_truncate_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) if (!zi->i_wr_refcnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) if (atomic_inc_return(&sbi->s_open_zones) > sbi->s_max_open_zones) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) atomic_dec(&sbi->s_open_zones);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) if (i_size_read(inode) < zi->i_max_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_OPEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) atomic_dec(&sbi->s_open_zones);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) zi->i_flags |= ZONEFS_ZONE_OPEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) zi->i_wr_refcnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) mutex_unlock(&zi->i_truncate_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) static int zonefs_file_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) ret = generic_file_open(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) if (zonefs_file_use_exp_open(inode, file))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) return zonefs_open_zone(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) static void zonefs_close_zone(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) struct zonefs_inode_info *zi = ZONEFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) mutex_lock(&zi->i_truncate_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) zi->i_wr_refcnt--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) if (!zi->i_wr_refcnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) * If the file zone is full, it is not open anymore and we only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) * need to decrement the open count.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) if (!(zi->i_flags & ZONEFS_ZONE_OPEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) goto dec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_CLOSE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) __zonefs_io_error(inode, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) * Leaving zones explicitly open may lead to a state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) * where most zones cannot be written (zone resources
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) * exhausted). So take preventive action by remounting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) * read-only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) if (zi->i_flags & ZONEFS_ZONE_OPEN &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) !(sb->s_flags & SB_RDONLY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) zonefs_warn(sb, "closing zone failed, remounting filesystem read-only\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) sb->s_flags |= SB_RDONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) zi->i_flags &= ~ZONEFS_ZONE_OPEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) dec:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) atomic_dec(&sbi->s_open_zones);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) mutex_unlock(&zi->i_truncate_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) static int zonefs_file_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) * If we explicitly open a zone we must close it again as well, but the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) * zone management operation can fail (either due to an IO error or as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) * the zone has gone offline or read-only). Make sure we don't fail the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) * close(2) for user-space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) if (zonefs_file_use_exp_open(inode, file))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) zonefs_close_zone(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) static const struct file_operations zonefs_file_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) .open = zonefs_file_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) .release = zonefs_file_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) .fsync = zonefs_file_fsync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) .mmap = zonefs_file_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) .llseek = zonefs_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) .read_iter = zonefs_file_read_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) .write_iter = zonefs_file_write_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) .splice_read = generic_file_splice_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) .splice_write = iter_file_splice_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) .iopoll = iomap_dio_iopoll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) static struct kmem_cache *zonefs_inode_cachep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) static struct inode *zonefs_alloc_inode(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) struct zonefs_inode_info *zi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) zi = kmem_cache_alloc(zonefs_inode_cachep, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) if (!zi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) inode_init_once(&zi->i_vnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) mutex_init(&zi->i_truncate_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) init_rwsem(&zi->i_mmap_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) zi->i_wr_refcnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) return &zi->i_vnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) static void zonefs_free_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) kmem_cache_free(zonefs_inode_cachep, ZONEFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) * File system stat.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) static int zonefs_statfs(struct dentry *dentry, struct kstatfs *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) struct super_block *sb = dentry->d_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) enum zonefs_ztype t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) u64 fsid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) buf->f_type = ZONEFS_MAGIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) buf->f_bsize = sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) buf->f_namelen = ZONEFS_NAME_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) spin_lock(&sbi->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) buf->f_blocks = sbi->s_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) if (WARN_ON(sbi->s_used_blocks > sbi->s_blocks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) buf->f_bfree = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) buf->f_bfree = buf->f_blocks - sbi->s_used_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) buf->f_bavail = buf->f_bfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) if (sbi->s_nr_files[t])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) buf->f_files += sbi->s_nr_files[t] + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) buf->f_ffree = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) spin_unlock(&sbi->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) fsid = le64_to_cpup((void *)sbi->s_uuid.b) ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) le64_to_cpup((void *)sbi->s_uuid.b + sizeof(u64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) buf->f_fsid = u64_to_fsid(fsid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) Opt_errors_ro, Opt_errors_zro, Opt_errors_zol, Opt_errors_repair,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) Opt_explicit_open, Opt_err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) static const match_table_t tokens = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) { Opt_errors_ro, "errors=remount-ro"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) { Opt_errors_zro, "errors=zone-ro"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) { Opt_errors_zol, "errors=zone-offline"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) { Opt_errors_repair, "errors=repair"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) { Opt_explicit_open, "explicit-open" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) { Opt_err, NULL}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) static int zonefs_parse_options(struct super_block *sb, char *options)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) substring_t args[MAX_OPT_ARGS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) if (!options)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) while ((p = strsep(&options, ",")) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) int token;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) if (!*p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) token = match_token(p, tokens, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) switch (token) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) case Opt_errors_ro:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_RO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) case Opt_errors_zro:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZRO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) case Opt_errors_zol:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZOL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) case Opt_errors_repair:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_REPAIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) case Opt_explicit_open:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) sbi->s_mount_opts |= ZONEFS_MNTOPT_EXPLICIT_OPEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) static int zonefs_show_options(struct seq_file *seq, struct dentry *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) struct zonefs_sb_info *sbi = ZONEFS_SB(root->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) seq_puts(seq, ",errors=remount-ro");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) seq_puts(seq, ",errors=zone-ro");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) seq_puts(seq, ",errors=zone-offline");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_REPAIR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) seq_puts(seq, ",errors=repair");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) static int zonefs_remount(struct super_block *sb, int *flags, char *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) sync_filesystem(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) return zonefs_parse_options(sb, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) static const struct super_operations zonefs_sops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) .alloc_inode = zonefs_alloc_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) .free_inode = zonefs_free_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) .statfs = zonefs_statfs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) .remount_fs = zonefs_remount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) .show_options = zonefs_show_options,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) static const struct inode_operations zonefs_dir_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) .lookup = simple_lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) .setattr = zonefs_inode_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) static void zonefs_init_dir_inode(struct inode *parent, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) enum zonefs_ztype type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) struct super_block *sb = parent->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) inode->i_ino = blkdev_nr_zones(sb->s_bdev->bd_disk) + type + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) inode_init_owner(inode, parent, S_IFDIR | 0555);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) inode->i_op = &zonefs_dir_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) inode->i_fop = &simple_dir_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) set_nlink(inode, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) inc_nlink(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) static void zonefs_init_file_inode(struct inode *inode, struct blk_zone *zone,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) enum zonefs_ztype type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) struct zonefs_inode_info *zi = ZONEFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) inode->i_ino = zone->start >> sbi->s_zone_sectors_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) inode->i_mode = S_IFREG | sbi->s_perm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) zi->i_ztype = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) zi->i_zsector = zone->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) zi->i_zone_size = zone->len << SECTOR_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) zi->i_max_size = min_t(loff_t, MAX_LFS_FILESIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) zone->capacity << SECTOR_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) zi->i_wpoffset = zonefs_check_zone_condition(inode, zone, true, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) inode->i_uid = sbi->s_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) inode->i_gid = sbi->s_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) inode->i_size = zi->i_wpoffset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) inode->i_blocks = zi->i_max_size >> SECTOR_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) inode->i_op = &zonefs_file_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) inode->i_fop = &zonefs_file_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) inode->i_mapping->a_ops = &zonefs_file_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) sb->s_maxbytes = max(zi->i_max_size, sb->s_maxbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) sbi->s_blocks += zi->i_max_size >> sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) sbi->s_used_blocks += zi->i_wpoffset >> sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) static struct dentry *zonefs_create_inode(struct dentry *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) const char *name, struct blk_zone *zone,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) enum zonefs_ztype type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) struct inode *dir = d_inode(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) struct dentry *dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) dentry = d_alloc_name(parent, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) if (!dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) inode = new_inode(parent->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) if (!inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) goto dput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) inode->i_ctime = inode->i_mtime = inode->i_atime = dir->i_ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) if (zone)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) zonefs_init_file_inode(inode, zone, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) zonefs_init_dir_inode(dir, inode, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) d_add(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) dir->i_size++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) return dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) dput:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) struct zonefs_zone_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) struct super_block *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) unsigned int nr_zones[ZONEFS_ZTYPE_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) struct blk_zone *zones;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) * Create a zone group and populate it with zone files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) static int zonefs_create_zgroup(struct zonefs_zone_data *zd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) enum zonefs_ztype type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) struct super_block *sb = zd->sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) struct blk_zone *zone, *next, *end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) const char *zgroup_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) char *file_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) struct dentry *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) unsigned int n = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) /* If the group is empty, there is nothing to do */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) if (!zd->nr_zones[type])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) file_name = kmalloc(ZONEFS_NAME_MAX, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) if (!file_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) if (type == ZONEFS_ZTYPE_CNV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) zgroup_name = "cnv";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) zgroup_name = "seq";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) dir = zonefs_create_inode(sb->s_root, zgroup_name, NULL, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) if (!dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) goto free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) * The first zone contains the super block: skip it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) end = zd->zones + blkdev_nr_zones(sb->s_bdev->bd_disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) for (zone = &zd->zones[1]; zone < end; zone = next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) next = zone + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) if (zonefs_zone_type(zone) != type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) * For conventional zones, contiguous zones can be aggregated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) * together to form larger files. Note that this overwrites the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) * length of the first zone of the set of contiguous zones
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) * aggregated together. If one offline or read-only zone is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) * found, assume that all zones aggregated have the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) * condition.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) if (type == ZONEFS_ZTYPE_CNV &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) (sbi->s_features & ZONEFS_F_AGGRCNV)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) for (; next < end; next++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) if (zonefs_zone_type(next) != type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) zone->len += next->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) zone->capacity += next->capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) if (next->cond == BLK_ZONE_COND_READONLY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) zone->cond != BLK_ZONE_COND_OFFLINE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) zone->cond = BLK_ZONE_COND_READONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) else if (next->cond == BLK_ZONE_COND_OFFLINE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) zone->cond = BLK_ZONE_COND_OFFLINE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) if (zone->capacity != zone->len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) zonefs_err(sb, "Invalid conventional zone capacity\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) goto free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) * Use the file number within its group as file name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) snprintf(file_name, ZONEFS_NAME_MAX - 1, "%u", n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) if (!zonefs_create_inode(dir, file_name, zone, type)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) goto free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) n++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) zonefs_info(sb, "Zone group \"%s\" has %u file%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) zgroup_name, n, n > 1 ? "s" : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) sbi->s_nr_files[type] = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) kfree(file_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) static int zonefs_get_zone_info_cb(struct blk_zone *zone, unsigned int idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) struct zonefs_zone_data *zd = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) * Count the number of usable zones: the first zone at index 0 contains
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) * the super block and is ignored.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) switch (zone->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) case BLK_ZONE_TYPE_CONVENTIONAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) zone->wp = zone->start + zone->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) if (idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) zd->nr_zones[ZONEFS_ZTYPE_CNV]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) case BLK_ZONE_TYPE_SEQWRITE_REQ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) case BLK_ZONE_TYPE_SEQWRITE_PREF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) if (idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) zd->nr_zones[ZONEFS_ZTYPE_SEQ]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) zonefs_err(zd->sb, "Unsupported zone type 0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) zone->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) memcpy(&zd->zones[idx], zone, sizeof(struct blk_zone));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) static int zonefs_get_zone_info(struct zonefs_zone_data *zd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) struct block_device *bdev = zd->sb->s_bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) zd->zones = kvcalloc(blkdev_nr_zones(bdev->bd_disk),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) sizeof(struct blk_zone), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) if (!zd->zones)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) /* Get zones information from the device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) zonefs_get_zone_info_cb, zd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) zonefs_err(zd->sb, "Zone report failed %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) if (ret != blkdev_nr_zones(bdev->bd_disk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) zonefs_err(zd->sb, "Invalid zone report (%d/%u zones)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) ret, blkdev_nr_zones(bdev->bd_disk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) static inline void zonefs_cleanup_zone_info(struct zonefs_zone_data *zd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) kvfree(zd->zones);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) * Read super block information from the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) static int zonefs_read_super(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) struct zonefs_super *super;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) u32 crc, stored_crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) struct bio_vec bio_vec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) struct bio bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) page = alloc_page(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) bio_init(&bio, &bio_vec, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) bio.bi_iter.bi_sector = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) bio.bi_opf = REQ_OP_READ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) bio_set_dev(&bio, sb->s_bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) bio_add_page(&bio, page, PAGE_SIZE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) ret = submit_bio_wait(&bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) goto free_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) super = kmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) if (le32_to_cpu(super->s_magic) != ZONEFS_MAGIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) goto unmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) stored_crc = le32_to_cpu(super->s_crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) super->s_crc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) crc = crc32(~0U, (unsigned char *)super, sizeof(struct zonefs_super));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) if (crc != stored_crc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) zonefs_err(sb, "Invalid checksum (Expected 0x%08x, got 0x%08x)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) crc, stored_crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) goto unmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) sbi->s_features = le64_to_cpu(super->s_features);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) if (sbi->s_features & ~ZONEFS_F_DEFINED_FEATURES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) zonefs_err(sb, "Unknown features set 0x%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) sbi->s_features);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) goto unmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) if (sbi->s_features & ZONEFS_F_UID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) sbi->s_uid = make_kuid(current_user_ns(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) le32_to_cpu(super->s_uid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) if (!uid_valid(sbi->s_uid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) zonefs_err(sb, "Invalid UID feature\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) goto unmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) if (sbi->s_features & ZONEFS_F_GID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) sbi->s_gid = make_kgid(current_user_ns(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) le32_to_cpu(super->s_gid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) if (!gid_valid(sbi->s_gid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) zonefs_err(sb, "Invalid GID feature\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) goto unmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) if (sbi->s_features & ZONEFS_F_PERM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) sbi->s_perm = le32_to_cpu(super->s_perm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) if (memchr_inv(super->s_reserved, 0, sizeof(super->s_reserved))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) zonefs_err(sb, "Reserved area is being used\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) goto unmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) import_uuid(&sbi->s_uuid, super->s_uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) unmap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) kunmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) free_page:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) __free_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) * Check that the device is zoned. If it is, get the list of zones and create
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) * sub-directories and files according to the device zone configuration and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) * format options.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) static int zonefs_fill_super(struct super_block *sb, void *data, int silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) struct zonefs_zone_data zd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) struct zonefs_sb_info *sbi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) enum zonefs_ztype t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) if (!bdev_is_zoned(sb->s_bdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) zonefs_err(sb, "Not a zoned block device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) * Initialize super block information: the maximum file size is updated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) * when the zone files are created so that the format option
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) * ZONEFS_F_AGGRCNV which increases the maximum file size of a file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) * beyond the zone size is taken into account.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) if (!sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) spin_lock_init(&sbi->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) sb->s_fs_info = sbi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) sb->s_magic = ZONEFS_MAGIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) sb->s_maxbytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) sb->s_op = &zonefs_sops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) sb->s_time_gran = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) * The block size is set to the device physical sector size to ensure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) * that write operations on 512e devices (512B logical block and 4KB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) * physical block) are always aligned to the device physical blocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) * as mandated by the ZBC/ZAC specifications.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) sb_set_blocksize(sb, bdev_physical_block_size(sb->s_bdev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) sbi->s_zone_sectors_shift = ilog2(bdev_zone_sectors(sb->s_bdev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) sbi->s_uid = GLOBAL_ROOT_UID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) sbi->s_gid = GLOBAL_ROOT_GID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) sbi->s_perm = 0640;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) sbi->s_mount_opts = ZONEFS_MNTOPT_ERRORS_RO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) sbi->s_max_open_zones = bdev_max_open_zones(sb->s_bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) atomic_set(&sbi->s_open_zones, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) if (!sbi->s_max_open_zones &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) zonefs_info(sb, "No open zones limit. Ignoring explicit_open mount option\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) sbi->s_mount_opts &= ~ZONEFS_MNTOPT_EXPLICIT_OPEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) ret = zonefs_read_super(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) ret = zonefs_parse_options(sb, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) memset(&zd, 0, sizeof(struct zonefs_zone_data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) zd.sb = sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) ret = zonefs_get_zone_info(&zd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) zonefs_info(sb, "Mounting %u zones",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) blkdev_nr_zones(sb->s_bdev->bd_disk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) /* Create root directory inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) inode = new_inode(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) if (!inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) inode->i_ino = blkdev_nr_zones(sb->s_bdev->bd_disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) inode->i_mode = S_IFDIR | 0555;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) inode->i_ctime = inode->i_mtime = inode->i_atime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) inode->i_op = &zonefs_dir_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) inode->i_fop = &simple_dir_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) set_nlink(inode, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) sb->s_root = d_make_root(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) if (!sb->s_root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) /* Create and populate files in zone groups directories */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) ret = zonefs_create_zgroup(&zd, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) zonefs_cleanup_zone_info(&zd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) static struct dentry *zonefs_mount(struct file_system_type *fs_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) int flags, const char *dev_name, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) return mount_bdev(fs_type, flags, dev_name, data, zonefs_fill_super);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) static void zonefs_kill_super(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) if (sb->s_root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) d_genocide(sb->s_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) kill_block_super(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) kfree(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) * File system definition and registration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) static struct file_system_type zonefs_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) .name = "zonefs",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) .mount = zonefs_mount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) .kill_sb = zonefs_kill_super,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) .fs_flags = FS_REQUIRES_DEV,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) static int __init zonefs_init_inodecache(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) zonefs_inode_cachep = kmem_cache_create("zonefs_inode_cache",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) sizeof(struct zonefs_inode_info), 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) if (zonefs_inode_cachep == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) static void zonefs_destroy_inodecache(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) * Make sure all delayed rcu free inodes are flushed before we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) * destroy the inode cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) rcu_barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) kmem_cache_destroy(zonefs_inode_cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) static int __init zonefs_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) BUILD_BUG_ON(sizeof(struct zonefs_super) != ZONEFS_SUPER_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) ret = zonefs_init_inodecache();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) ret = register_filesystem(&zonefs_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) zonefs_destroy_inodecache();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) static void __exit zonefs_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) zonefs_destroy_inodecache();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) unregister_filesystem(&zonefs_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) MODULE_AUTHOR("Damien Le Moal");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) MODULE_DESCRIPTION("Zone file system for zoned block devices");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) MODULE_ALIAS_FS("zonefs");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) module_init(zonefs_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) module_exit(zonefs_exit);