18dcc1a9dSDamien Le Moal // SPDX-License-Identifier: GPL-2.0 28dcc1a9dSDamien Le Moal /* 38dcc1a9dSDamien Le Moal * Simple file system for zoned block devices exposing zones as files. 48dcc1a9dSDamien Le Moal * 58dcc1a9dSDamien Le Moal * Copyright (C) 2019 Western Digital Corporation or its affiliates. 68dcc1a9dSDamien Le Moal */ 78dcc1a9dSDamien Le Moal #include <linux/module.h> 83a6b2162SMatthew Wilcox (Oracle) #include <linux/pagemap.h> 98dcc1a9dSDamien Le Moal #include <linux/magic.h> 108dcc1a9dSDamien Le Moal #include <linux/iomap.h> 118dcc1a9dSDamien Le Moal #include <linux/init.h> 128dcc1a9dSDamien Le Moal #include <linux/slab.h> 138dcc1a9dSDamien Le Moal #include <linux/blkdev.h> 148dcc1a9dSDamien Le Moal #include <linux/statfs.h> 158dcc1a9dSDamien Le Moal #include <linux/writeback.h> 168dcc1a9dSDamien Le Moal #include <linux/quotaops.h> 178dcc1a9dSDamien Le Moal #include <linux/seq_file.h> 188dcc1a9dSDamien Le Moal #include <linux/parser.h> 198dcc1a9dSDamien Le Moal #include <linux/uio.h> 208dcc1a9dSDamien Le Moal #include <linux/mman.h> 218dcc1a9dSDamien Le Moal #include <linux/sched/mm.h> 228dcc1a9dSDamien Le Moal #include <linux/crc32.h> 2302ef12a6SJohannes Thumshirn #include <linux/task_io_accounting_ops.h> 248dcc1a9dSDamien Le Moal 258dcc1a9dSDamien Le Moal #include "zonefs.h" 268dcc1a9dSDamien Le Moal 2762ab1aadSJohannes Thumshirn #define CREATE_TRACE_POINTS 2862ab1aadSJohannes Thumshirn #include "trace.h" 2962ab1aadSJohannes Thumshirn 3087c9ce3fSDamien Le Moal /* 3187c9ce3fSDamien Le Moal * Manage the active zone count. Called with zi->i_truncate_mutex held. 3287c9ce3fSDamien Le Moal */ 3387c9ce3fSDamien Le Moal static void zonefs_account_active(struct inode *inode) 3487c9ce3fSDamien Le Moal { 3587c9ce3fSDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb); 3687c9ce3fSDamien Le Moal struct zonefs_inode_info *zi = ZONEFS_I(inode); 3787c9ce3fSDamien Le Moal 3887c9ce3fSDamien Le Moal lockdep_assert_held(&zi->i_truncate_mutex); 3987c9ce3fSDamien Le Moal 4087c9ce3fSDamien Le Moal if (zi->i_ztype != ZONEFS_ZTYPE_SEQ) 4187c9ce3fSDamien Le Moal return; 4287c9ce3fSDamien Le Moal 4387c9ce3fSDamien Le Moal /* 4487c9ce3fSDamien Le Moal * If the zone is active, that is, if it is explicitly open or 4587c9ce3fSDamien Le Moal * partially written, check if it was already accounted as active. 4687c9ce3fSDamien Le Moal */ 4787c9ce3fSDamien Le Moal if ((zi->i_flags & ZONEFS_ZONE_OPEN) || 4887c9ce3fSDamien Le Moal (zi->i_wpoffset > 0 && zi->i_wpoffset < zi->i_max_size)) { 4987c9ce3fSDamien Le Moal if (!(zi->i_flags & ZONEFS_ZONE_ACTIVE)) { 5087c9ce3fSDamien Le Moal zi->i_flags |= ZONEFS_ZONE_ACTIVE; 5187c9ce3fSDamien Le Moal atomic_inc(&sbi->s_active_seq_files); 5287c9ce3fSDamien Le Moal } 5387c9ce3fSDamien Le Moal return; 5487c9ce3fSDamien Le Moal } 5587c9ce3fSDamien Le Moal 5687c9ce3fSDamien Le Moal /* The zone is not active. If it was, update the active count */ 5787c9ce3fSDamien Le Moal if (zi->i_flags & ZONEFS_ZONE_ACTIVE) { 5887c9ce3fSDamien Le Moal zi->i_flags &= ~ZONEFS_ZONE_ACTIVE; 5987c9ce3fSDamien Le Moal atomic_dec(&sbi->s_active_seq_files); 6087c9ce3fSDamien Le Moal } 6187c9ce3fSDamien Le Moal } 6287c9ce3fSDamien Le Moal 63ff07a02eSBart Van Assche static inline int zonefs_zone_mgmt(struct inode *inode, enum req_op op) 645498d5f9SJohannes Thumshirn { 655498d5f9SJohannes Thumshirn struct zonefs_inode_info *zi = ZONEFS_I(inode); 665498d5f9SJohannes Thumshirn int ret; 675498d5f9SJohannes Thumshirn 685498d5f9SJohannes Thumshirn lockdep_assert_held(&zi->i_truncate_mutex); 695498d5f9SJohannes Thumshirn 701da18a29SDamien Le Moal /* 711da18a29SDamien Le Moal * With ZNS drives, closing an explicitly open zone that has not been 721da18a29SDamien Le Moal * written will change the zone state to "closed", that is, the zone 731da18a29SDamien Le Moal * will remain active. Since this can then cause failure of explicit 741da18a29SDamien Le Moal * open operation on other zones if the drive active zone resources 751da18a29SDamien Le Moal * are exceeded, make sure that the zone does not remain active by 761da18a29SDamien Le Moal * resetting it. 771da18a29SDamien Le Moal */ 781da18a29SDamien Le Moal if (op == REQ_OP_ZONE_CLOSE && !zi->i_wpoffset) 791da18a29SDamien Le Moal op = REQ_OP_ZONE_RESET; 801da18a29SDamien Le Moal 8162ab1aadSJohannes Thumshirn trace_zonefs_zone_mgmt(inode, op); 825498d5f9SJohannes Thumshirn ret = blkdev_zone_mgmt(inode->i_sb->s_bdev, op, zi->i_zsector, 835498d5f9SJohannes Thumshirn zi->i_zone_size >> SECTOR_SHIFT, GFP_NOFS); 845498d5f9SJohannes Thumshirn if (ret) { 855498d5f9SJohannes Thumshirn zonefs_err(inode->i_sb, 865498d5f9SJohannes Thumshirn "Zone management operation %s at %llu failed %d\n", 875498d5f9SJohannes Thumshirn blk_op_str(op), zi->i_zsector, ret); 885498d5f9SJohannes Thumshirn return ret; 895498d5f9SJohannes Thumshirn } 905498d5f9SJohannes Thumshirn 915498d5f9SJohannes Thumshirn return 0; 925498d5f9SJohannes Thumshirn } 935498d5f9SJohannes Thumshirn 94b5c00e97SJohannes Thumshirn static inline void zonefs_i_size_write(struct inode *inode, loff_t isize) 95b5c00e97SJohannes Thumshirn { 96b5c00e97SJohannes Thumshirn struct zonefs_inode_info *zi = ZONEFS_I(inode); 97b5c00e97SJohannes Thumshirn 98b5c00e97SJohannes Thumshirn i_size_write(inode, isize); 99b5c00e97SJohannes Thumshirn /* 100b5c00e97SJohannes Thumshirn * A full zone is no longer open/active and does not need 101b5c00e97SJohannes Thumshirn * explicit closing. 102b5c00e97SJohannes Thumshirn */ 10387c9ce3fSDamien Le Moal if (isize >= zi->i_max_size) { 10487c9ce3fSDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb); 10587c9ce3fSDamien Le Moal 10687c9ce3fSDamien Le Moal if (zi->i_flags & ZONEFS_ZONE_ACTIVE) 10787c9ce3fSDamien Le Moal atomic_dec(&sbi->s_active_seq_files); 10887c9ce3fSDamien Le Moal zi->i_flags &= ~(ZONEFS_ZONE_OPEN | ZONEFS_ZONE_ACTIVE); 10987c9ce3fSDamien Le Moal } 110b5c00e97SJohannes Thumshirn } 111b5c00e97SJohannes Thumshirn 112c1c1204cSDamien Le Moal static int zonefs_read_iomap_begin(struct inode *inode, loff_t offset, 113c1c1204cSDamien Le Moal loff_t length, unsigned int flags, 114c1c1204cSDamien Le Moal struct iomap *iomap, struct iomap *srcmap) 1158dcc1a9dSDamien Le Moal { 1168dcc1a9dSDamien Le Moal struct zonefs_inode_info *zi = ZONEFS_I(inode); 1178dcc1a9dSDamien Le Moal struct super_block *sb = inode->i_sb; 1188dcc1a9dSDamien Le Moal loff_t isize; 1198dcc1a9dSDamien Le Moal 120c1c1204cSDamien Le Moal /* 121c1c1204cSDamien Le Moal * All blocks are always mapped below EOF. If reading past EOF, 122c1c1204cSDamien Le Moal * act as if there is a hole up to the file maximum size. 123c1c1204cSDamien Le Moal */ 124c1c1204cSDamien Le Moal mutex_lock(&zi->i_truncate_mutex); 125c1c1204cSDamien Le Moal iomap->bdev = inode->i_sb->s_bdev; 126c1c1204cSDamien Le Moal iomap->offset = ALIGN_DOWN(offset, sb->s_blocksize); 127c1c1204cSDamien Le Moal isize = i_size_read(inode); 128c1c1204cSDamien Le Moal if (iomap->offset >= isize) { 129c1c1204cSDamien Le Moal iomap->type = IOMAP_HOLE; 130c1c1204cSDamien Le Moal iomap->addr = IOMAP_NULL_ADDR; 131c1c1204cSDamien Le Moal iomap->length = length; 132c1c1204cSDamien Le Moal } else { 133c1c1204cSDamien Le Moal iomap->type = IOMAP_MAPPED; 134c1c1204cSDamien Le Moal iomap->addr = (zi->i_zsector << SECTOR_SHIFT) + iomap->offset; 135c1c1204cSDamien Le Moal iomap->length = isize - iomap->offset; 136c1c1204cSDamien Le Moal } 137c1c1204cSDamien Le Moal mutex_unlock(&zi->i_truncate_mutex); 138c1c1204cSDamien Le Moal 139c1c1204cSDamien Le Moal trace_zonefs_iomap_begin(inode, iomap); 140c1c1204cSDamien Le Moal 141c1c1204cSDamien Le Moal return 0; 142c1c1204cSDamien Le Moal } 143c1c1204cSDamien Le Moal 144c1c1204cSDamien Le Moal static const struct iomap_ops zonefs_read_iomap_ops = { 145c1c1204cSDamien Le Moal .iomap_begin = zonefs_read_iomap_begin, 146c1c1204cSDamien Le Moal }; 147c1c1204cSDamien Le Moal 148c1c1204cSDamien Le Moal static int zonefs_write_iomap_begin(struct inode *inode, loff_t offset, 149c1c1204cSDamien Le Moal loff_t length, unsigned int flags, 150c1c1204cSDamien Le Moal struct iomap *iomap, struct iomap *srcmap) 151c1c1204cSDamien Le Moal { 152c1c1204cSDamien Le Moal struct zonefs_inode_info *zi = ZONEFS_I(inode); 153c1c1204cSDamien Le Moal struct super_block *sb = inode->i_sb; 154c1c1204cSDamien Le Moal loff_t isize; 155c1c1204cSDamien Le Moal 156c1c1204cSDamien Le Moal /* All write I/Os should always be within the file maximum size */ 1578dcc1a9dSDamien Le Moal if (WARN_ON_ONCE(offset + length > zi->i_max_size)) 1588dcc1a9dSDamien Le Moal return -EIO; 1598dcc1a9dSDamien Le Moal 1608dcc1a9dSDamien Le Moal /* 1618dcc1a9dSDamien Le Moal * Sequential zones can only accept direct writes. This is already 1628dcc1a9dSDamien Le Moal * checked when writes are issued, so warn if we see a page writeback 1638dcc1a9dSDamien Le Moal * operation. 1648dcc1a9dSDamien Le Moal */ 1658dcc1a9dSDamien Le Moal if (WARN_ON_ONCE(zi->i_ztype == ZONEFS_ZTYPE_SEQ && 166c1c1204cSDamien Le Moal !(flags & IOMAP_DIRECT))) 1678dcc1a9dSDamien Le Moal return -EIO; 1688dcc1a9dSDamien Le Moal 1698dcc1a9dSDamien Le Moal /* 1708dcc1a9dSDamien Le Moal * For conventional zones, all blocks are always mapped. For sequential 1718dcc1a9dSDamien Le Moal * zones, all blocks after always mapped below the inode size (zone 1728dcc1a9dSDamien Le Moal * write pointer) and unwriten beyond. 1738dcc1a9dSDamien Le Moal */ 1748dcc1a9dSDamien Le Moal mutex_lock(&zi->i_truncate_mutex); 1758dcc1a9dSDamien Le Moal iomap->bdev = inode->i_sb->s_bdev; 176c1c1204cSDamien Le Moal iomap->offset = ALIGN_DOWN(offset, sb->s_blocksize); 1778dcc1a9dSDamien Le Moal iomap->addr = (zi->i_zsector << SECTOR_SHIFT) + iomap->offset; 178c1c1204cSDamien Le Moal isize = i_size_read(inode); 179c1c1204cSDamien Le Moal if (iomap->offset >= isize) { 180c1c1204cSDamien Le Moal iomap->type = IOMAP_UNWRITTEN; 181c1c1204cSDamien Le Moal iomap->length = zi->i_max_size - iomap->offset; 182c1c1204cSDamien Le Moal } else { 183c1c1204cSDamien Le Moal iomap->type = IOMAP_MAPPED; 184c1c1204cSDamien Le Moal iomap->length = isize - iomap->offset; 185c1c1204cSDamien Le Moal } 186c1c1204cSDamien Le Moal mutex_unlock(&zi->i_truncate_mutex); 1878dcc1a9dSDamien Le Moal 18862ab1aadSJohannes Thumshirn trace_zonefs_iomap_begin(inode, iomap); 18962ab1aadSJohannes Thumshirn 1908dcc1a9dSDamien Le Moal return 0; 1918dcc1a9dSDamien Le Moal } 1928dcc1a9dSDamien Le Moal 193c1c1204cSDamien Le Moal static const struct iomap_ops zonefs_write_iomap_ops = { 194c1c1204cSDamien Le Moal .iomap_begin = zonefs_write_iomap_begin, 1958dcc1a9dSDamien Le Moal }; 1968dcc1a9dSDamien Le Moal 1977479c505SMatthew Wilcox (Oracle) static int zonefs_read_folio(struct file *unused, struct folio *folio) 1988dcc1a9dSDamien Le Moal { 199c1c1204cSDamien Le Moal return iomap_read_folio(folio, &zonefs_read_iomap_ops); 2008dcc1a9dSDamien Le Moal } 2018dcc1a9dSDamien Le Moal 2029d24a13aSMatthew Wilcox (Oracle) static void zonefs_readahead(struct readahead_control *rac) 2038dcc1a9dSDamien Le Moal { 204c1c1204cSDamien Le Moal iomap_readahead(rac, &zonefs_read_iomap_ops); 2058dcc1a9dSDamien Le Moal } 2068dcc1a9dSDamien Le Moal 2078dcc1a9dSDamien Le Moal /* 2088dcc1a9dSDamien Le Moal * Map blocks for page writeback. This is used only on conventional zone files, 2098dcc1a9dSDamien Le Moal * which implies that the page range can only be within the fixed inode size. 2108dcc1a9dSDamien Le Moal */ 211c1c1204cSDamien Le Moal static int zonefs_write_map_blocks(struct iomap_writepage_ctx *wpc, 2128dcc1a9dSDamien Le Moal struct inode *inode, loff_t offset) 2138dcc1a9dSDamien Le Moal { 2148dcc1a9dSDamien Le Moal struct zonefs_inode_info *zi = ZONEFS_I(inode); 2158dcc1a9dSDamien Le Moal 2168dcc1a9dSDamien Le Moal if (WARN_ON_ONCE(zi->i_ztype != ZONEFS_ZTYPE_CNV)) 2178dcc1a9dSDamien Le Moal return -EIO; 2188dcc1a9dSDamien Le Moal if (WARN_ON_ONCE(offset >= i_size_read(inode))) 2198dcc1a9dSDamien Le Moal return -EIO; 2208dcc1a9dSDamien Le Moal 2218dcc1a9dSDamien Le Moal /* If the mapping is already OK, nothing needs to be done */ 2228dcc1a9dSDamien Le Moal if (offset >= wpc->iomap.offset && 2238dcc1a9dSDamien Le Moal offset < wpc->iomap.offset + wpc->iomap.length) 2248dcc1a9dSDamien Le Moal return 0; 2258dcc1a9dSDamien Le Moal 226c1c1204cSDamien Le Moal return zonefs_write_iomap_begin(inode, offset, zi->i_max_size - offset, 2278dcc1a9dSDamien Le Moal IOMAP_WRITE, &wpc->iomap, NULL); 2288dcc1a9dSDamien Le Moal } 2298dcc1a9dSDamien Le Moal 2308dcc1a9dSDamien Le Moal static const struct iomap_writeback_ops zonefs_writeback_ops = { 231c1c1204cSDamien Le Moal .map_blocks = zonefs_write_map_blocks, 2328dcc1a9dSDamien Le Moal }; 2338dcc1a9dSDamien Le Moal 2348dcc1a9dSDamien Le Moal static int zonefs_writepages(struct address_space *mapping, 2358dcc1a9dSDamien Le Moal struct writeback_control *wbc) 2368dcc1a9dSDamien Le Moal { 2378dcc1a9dSDamien Le Moal struct iomap_writepage_ctx wpc = { }; 2388dcc1a9dSDamien Le Moal 2398dcc1a9dSDamien Le Moal return iomap_writepages(mapping, wbc, &wpc, &zonefs_writeback_ops); 2408dcc1a9dSDamien Le Moal } 2418dcc1a9dSDamien Le Moal 2421601ea06SDamien Le Moal static int zonefs_swap_activate(struct swap_info_struct *sis, 2431601ea06SDamien Le Moal struct file *swap_file, sector_t *span) 2441601ea06SDamien Le Moal { 2451601ea06SDamien Le Moal struct inode *inode = file_inode(swap_file); 2461601ea06SDamien Le Moal struct zonefs_inode_info *zi = ZONEFS_I(inode); 2471601ea06SDamien Le Moal 2481601ea06SDamien Le Moal if (zi->i_ztype != ZONEFS_ZTYPE_CNV) { 2491601ea06SDamien Le Moal zonefs_err(inode->i_sb, 2501601ea06SDamien Le Moal "swap file: not a conventional zone file\n"); 2511601ea06SDamien Le Moal return -EINVAL; 2521601ea06SDamien Le Moal } 2531601ea06SDamien Le Moal 254c1c1204cSDamien Le Moal return iomap_swapfile_activate(sis, swap_file, span, 255c1c1204cSDamien Le Moal &zonefs_read_iomap_ops); 2561601ea06SDamien Le Moal } 2571601ea06SDamien Le Moal 2588dcc1a9dSDamien Le Moal static const struct address_space_operations zonefs_file_aops = { 2597479c505SMatthew Wilcox (Oracle) .read_folio = zonefs_read_folio, 2609d24a13aSMatthew Wilcox (Oracle) .readahead = zonefs_readahead, 2618dcc1a9dSDamien Le Moal .writepages = zonefs_writepages, 262187c82cbSMatthew Wilcox (Oracle) .dirty_folio = filemap_dirty_folio, 2638597447dSMatthew Wilcox (Oracle) .release_folio = iomap_release_folio, 264d82354f6SMatthew Wilcox (Oracle) .invalidate_folio = iomap_invalidate_folio, 2652ec810d5SMatthew Wilcox (Oracle) .migrate_folio = filemap_migrate_folio, 2668dcc1a9dSDamien Le Moal .is_partially_uptodate = iomap_is_partially_uptodate, 2678dcc1a9dSDamien Le Moal .error_remove_page = generic_error_remove_page, 2688dcc1a9dSDamien Le Moal .direct_IO = noop_direct_IO, 2691601ea06SDamien Le Moal .swap_activate = zonefs_swap_activate, 2708dcc1a9dSDamien Le Moal }; 2718dcc1a9dSDamien Le Moal 2728dcc1a9dSDamien Le Moal static void zonefs_update_stats(struct inode *inode, loff_t new_isize) 2738dcc1a9dSDamien Le Moal { 2748dcc1a9dSDamien Le Moal struct super_block *sb = inode->i_sb; 2758dcc1a9dSDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 2768dcc1a9dSDamien Le Moal loff_t old_isize = i_size_read(inode); 2778dcc1a9dSDamien Le Moal loff_t nr_blocks; 2788dcc1a9dSDamien Le Moal 2798dcc1a9dSDamien Le Moal if (new_isize == old_isize) 2808dcc1a9dSDamien Le Moal return; 2818dcc1a9dSDamien Le Moal 2828dcc1a9dSDamien Le Moal spin_lock(&sbi->s_lock); 2838dcc1a9dSDamien Le Moal 2848dcc1a9dSDamien Le Moal /* 2858dcc1a9dSDamien Le Moal * This may be called for an update after an IO error. 2868dcc1a9dSDamien Le Moal * So beware of the values seen. 2878dcc1a9dSDamien Le Moal */ 2888dcc1a9dSDamien Le Moal if (new_isize < old_isize) { 2898dcc1a9dSDamien Le Moal nr_blocks = (old_isize - new_isize) >> sb->s_blocksize_bits; 2908dcc1a9dSDamien Le Moal if (sbi->s_used_blocks > nr_blocks) 2918dcc1a9dSDamien Le Moal sbi->s_used_blocks -= nr_blocks; 2928dcc1a9dSDamien Le Moal else 2938dcc1a9dSDamien Le Moal sbi->s_used_blocks = 0; 2948dcc1a9dSDamien Le Moal } else { 2958dcc1a9dSDamien Le Moal sbi->s_used_blocks += 2968dcc1a9dSDamien Le Moal (new_isize - old_isize) >> sb->s_blocksize_bits; 2978dcc1a9dSDamien Le Moal if (sbi->s_used_blocks > sbi->s_blocks) 2988dcc1a9dSDamien Le Moal sbi->s_used_blocks = sbi->s_blocks; 2998dcc1a9dSDamien Le Moal } 3008dcc1a9dSDamien Le Moal 3018dcc1a9dSDamien Le Moal spin_unlock(&sbi->s_lock); 3028dcc1a9dSDamien Le Moal } 3038dcc1a9dSDamien Le Moal 3048dcc1a9dSDamien Le Moal /* 3058dcc1a9dSDamien Le Moal * Check a zone condition and adjust its file inode access permissions for 3068dcc1a9dSDamien Le Moal * offline and readonly zones. Return the inode size corresponding to the 3078dcc1a9dSDamien Le Moal * amount of readable data in the zone. 3088dcc1a9dSDamien Le Moal */ 3098dcc1a9dSDamien Le Moal static loff_t zonefs_check_zone_condition(struct inode *inode, 310ccf4ad7dSDamien Le Moal struct blk_zone *zone, bool warn, 311ccf4ad7dSDamien Le Moal bool mount) 3128dcc1a9dSDamien Le Moal { 3138dcc1a9dSDamien Le Moal struct zonefs_inode_info *zi = ZONEFS_I(inode); 3148dcc1a9dSDamien Le Moal 3158dcc1a9dSDamien Le Moal switch (zone->cond) { 3168dcc1a9dSDamien Le Moal case BLK_ZONE_COND_OFFLINE: 3178dcc1a9dSDamien Le Moal /* 3188dcc1a9dSDamien Le Moal * Dead zone: make the inode immutable, disable all accesses 3198dcc1a9dSDamien Le Moal * and set the file size to 0 (zone wp set to zone start). 3208dcc1a9dSDamien Le Moal */ 3218dcc1a9dSDamien Le Moal if (warn) 3228dcc1a9dSDamien Le Moal zonefs_warn(inode->i_sb, "inode %lu: offline zone\n", 3238dcc1a9dSDamien Le Moal inode->i_ino); 3248dcc1a9dSDamien Le Moal inode->i_flags |= S_IMMUTABLE; 3258dcc1a9dSDamien Le Moal inode->i_mode &= ~0777; 3268dcc1a9dSDamien Le Moal zone->wp = zone->start; 3278dcc1a9dSDamien Le Moal return 0; 3288dcc1a9dSDamien Le Moal case BLK_ZONE_COND_READONLY: 329ccf4ad7dSDamien Le Moal /* 330ccf4ad7dSDamien Le Moal * The write pointer of read-only zones is invalid. If such a 331ccf4ad7dSDamien Le Moal * zone is found during mount, the file size cannot be retrieved 332ccf4ad7dSDamien Le Moal * so we treat the zone as offline (mount == true case). 333ccf4ad7dSDamien Le Moal * Otherwise, keep the file size as it was when last updated 334ccf4ad7dSDamien Le Moal * so that the user can recover data. In both cases, writes are 335ccf4ad7dSDamien Le Moal * always disabled for the zone. 336ccf4ad7dSDamien Le Moal */ 3378dcc1a9dSDamien Le Moal if (warn) 3388dcc1a9dSDamien Le Moal zonefs_warn(inode->i_sb, "inode %lu: read-only zone\n", 3398dcc1a9dSDamien Le Moal inode->i_ino); 3408dcc1a9dSDamien Le Moal inode->i_flags |= S_IMMUTABLE; 341ccf4ad7dSDamien Le Moal if (mount) { 342ccf4ad7dSDamien Le Moal zone->cond = BLK_ZONE_COND_OFFLINE; 343ccf4ad7dSDamien Le Moal inode->i_mode &= ~0777; 344ccf4ad7dSDamien Le Moal zone->wp = zone->start; 345ccf4ad7dSDamien Le Moal return 0; 346ccf4ad7dSDamien Le Moal } 3478dcc1a9dSDamien Le Moal inode->i_mode &= ~0222; 348ccf4ad7dSDamien Le Moal return i_size_read(inode); 349059c0103SShin'ichiro Kawasaki case BLK_ZONE_COND_FULL: 350059c0103SShin'ichiro Kawasaki /* The write pointer of full zones is invalid. */ 351059c0103SShin'ichiro Kawasaki return zi->i_max_size; 3528dcc1a9dSDamien Le Moal default: 3538dcc1a9dSDamien Le Moal if (zi->i_ztype == ZONEFS_ZTYPE_CNV) 3548dcc1a9dSDamien Le Moal return zi->i_max_size; 3558dcc1a9dSDamien Le Moal return (zone->wp - zone->start) << SECTOR_SHIFT; 3568dcc1a9dSDamien Le Moal } 3578dcc1a9dSDamien Le Moal } 3588dcc1a9dSDamien Le Moal 3598dcc1a9dSDamien Le Moal struct zonefs_ioerr_data { 3608dcc1a9dSDamien Le Moal struct inode *inode; 3618dcc1a9dSDamien Le Moal bool write; 3628dcc1a9dSDamien Le Moal }; 3638dcc1a9dSDamien Le Moal 3648dcc1a9dSDamien Le Moal static int zonefs_io_error_cb(struct blk_zone *zone, unsigned int idx, 3658dcc1a9dSDamien Le Moal void *data) 3668dcc1a9dSDamien Le Moal { 3678dcc1a9dSDamien Le Moal struct zonefs_ioerr_data *err = data; 3688dcc1a9dSDamien Le Moal struct inode *inode = err->inode; 3698dcc1a9dSDamien Le Moal struct zonefs_inode_info *zi = ZONEFS_I(inode); 3708dcc1a9dSDamien Le Moal struct super_block *sb = inode->i_sb; 3718dcc1a9dSDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 3728dcc1a9dSDamien Le Moal loff_t isize, data_size; 3738dcc1a9dSDamien Le Moal 3748dcc1a9dSDamien Le Moal /* 3758dcc1a9dSDamien Le Moal * Check the zone condition: if the zone is not "bad" (offline or 3768dcc1a9dSDamien Le Moal * read-only), read errors are simply signaled to the IO issuer as long 3778dcc1a9dSDamien Le Moal * as there is no inconsistency between the inode size and the amount of 3788dcc1a9dSDamien Le Moal * data writen in the zone (data_size). 3798dcc1a9dSDamien Le Moal */ 380ccf4ad7dSDamien Le Moal data_size = zonefs_check_zone_condition(inode, zone, true, false); 3818dcc1a9dSDamien Le Moal isize = i_size_read(inode); 3828dcc1a9dSDamien Le Moal if (zone->cond != BLK_ZONE_COND_OFFLINE && 3838dcc1a9dSDamien Le Moal zone->cond != BLK_ZONE_COND_READONLY && 3848dcc1a9dSDamien Le Moal !err->write && isize == data_size) 3858dcc1a9dSDamien Le Moal return 0; 3868dcc1a9dSDamien Le Moal 3878dcc1a9dSDamien Le Moal /* 3888dcc1a9dSDamien Le Moal * At this point, we detected either a bad zone or an inconsistency 3898dcc1a9dSDamien Le Moal * between the inode size and the amount of data written in the zone. 3908dcc1a9dSDamien Le Moal * For the latter case, the cause may be a write IO error or an external 3918dcc1a9dSDamien Le Moal * action on the device. Two error patterns exist: 3928dcc1a9dSDamien Le Moal * 1) The inode size is lower than the amount of data in the zone: 3938dcc1a9dSDamien Le Moal * a write operation partially failed and data was writen at the end 3948dcc1a9dSDamien Le Moal * of the file. This can happen in the case of a large direct IO 3958dcc1a9dSDamien Le Moal * needing several BIOs and/or write requests to be processed. 3968dcc1a9dSDamien Le Moal * 2) The inode size is larger than the amount of data in the zone: 3978dcc1a9dSDamien Le Moal * this can happen with a deferred write error with the use of the 3988dcc1a9dSDamien Le Moal * device side write cache after getting successful write IO 3998dcc1a9dSDamien Le Moal * completions. Other possibilities are (a) an external corruption, 4008dcc1a9dSDamien Le Moal * e.g. an application reset the zone directly, or (b) the device 4018dcc1a9dSDamien Le Moal * has a serious problem (e.g. firmware bug). 4028dcc1a9dSDamien Le Moal * 4038dcc1a9dSDamien Le Moal * In all cases, warn about inode size inconsistency and handle the 4048dcc1a9dSDamien Le Moal * IO error according to the zone condition and to the mount options. 4058dcc1a9dSDamien Le Moal */ 4068dcc1a9dSDamien Le Moal if (zi->i_ztype == ZONEFS_ZTYPE_SEQ && isize != data_size) 4078dcc1a9dSDamien Le Moal zonefs_warn(sb, "inode %lu: invalid size %lld (should be %lld)\n", 4088dcc1a9dSDamien Le Moal inode->i_ino, isize, data_size); 4098dcc1a9dSDamien Le Moal 4108dcc1a9dSDamien Le Moal /* 4118dcc1a9dSDamien Le Moal * First handle bad zones signaled by hardware. The mount options 4128dcc1a9dSDamien Le Moal * errors=zone-ro and errors=zone-offline result in changing the 4138dcc1a9dSDamien Le Moal * zone condition to read-only and offline respectively, as if the 4148dcc1a9dSDamien Le Moal * condition was signaled by the hardware. 4158dcc1a9dSDamien Le Moal */ 4168dcc1a9dSDamien Le Moal if (zone->cond == BLK_ZONE_COND_OFFLINE || 4178dcc1a9dSDamien Le Moal sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL) { 4188dcc1a9dSDamien Le Moal zonefs_warn(sb, "inode %lu: read/write access disabled\n", 4198dcc1a9dSDamien Le Moal inode->i_ino); 4208dcc1a9dSDamien Le Moal if (zone->cond != BLK_ZONE_COND_OFFLINE) { 4218dcc1a9dSDamien Le Moal zone->cond = BLK_ZONE_COND_OFFLINE; 4228dcc1a9dSDamien Le Moal data_size = zonefs_check_zone_condition(inode, zone, 423ccf4ad7dSDamien Le Moal false, false); 4248dcc1a9dSDamien Le Moal } 4258dcc1a9dSDamien Le Moal } else if (zone->cond == BLK_ZONE_COND_READONLY || 4268dcc1a9dSDamien Le Moal sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO) { 4278dcc1a9dSDamien Le Moal zonefs_warn(sb, "inode %lu: write access disabled\n", 4288dcc1a9dSDamien Le Moal inode->i_ino); 4298dcc1a9dSDamien Le Moal if (zone->cond != BLK_ZONE_COND_READONLY) { 4308dcc1a9dSDamien Le Moal zone->cond = BLK_ZONE_COND_READONLY; 4318dcc1a9dSDamien Le Moal data_size = zonefs_check_zone_condition(inode, zone, 432ccf4ad7dSDamien Le Moal false, false); 4338dcc1a9dSDamien Le Moal } 4348dcc1a9dSDamien Le Moal } 4358dcc1a9dSDamien Le Moal 4368dcc1a9dSDamien Le Moal /* 437b5c00e97SJohannes Thumshirn * If the filesystem is mounted with the explicit-open mount option, we 438b5c00e97SJohannes Thumshirn * need to clear the ZONEFS_ZONE_OPEN flag if the zone transitioned to 439b5c00e97SJohannes Thumshirn * the read-only or offline condition, to avoid attempting an explicit 440b5c00e97SJohannes Thumshirn * close of the zone when the inode file is closed. 441b5c00e97SJohannes Thumshirn */ 442b5c00e97SJohannes Thumshirn if ((sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) && 443b5c00e97SJohannes Thumshirn (zone->cond == BLK_ZONE_COND_OFFLINE || 444b5c00e97SJohannes Thumshirn zone->cond == BLK_ZONE_COND_READONLY)) 445b5c00e97SJohannes Thumshirn zi->i_flags &= ~ZONEFS_ZONE_OPEN; 446b5c00e97SJohannes Thumshirn 447b5c00e97SJohannes Thumshirn /* 4488dcc1a9dSDamien Le Moal * If error=remount-ro was specified, any error result in remounting 4498dcc1a9dSDamien Le Moal * the volume as read-only. 4508dcc1a9dSDamien Le Moal */ 4518dcc1a9dSDamien Le Moal if ((sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO) && !sb_rdonly(sb)) { 4528dcc1a9dSDamien Le Moal zonefs_warn(sb, "remounting filesystem read-only\n"); 4538dcc1a9dSDamien Le Moal sb->s_flags |= SB_RDONLY; 4548dcc1a9dSDamien Le Moal } 4558dcc1a9dSDamien Le Moal 4568dcc1a9dSDamien Le Moal /* 4578dcc1a9dSDamien Le Moal * Update block usage stats and the inode size to prevent access to 4588dcc1a9dSDamien Le Moal * invalid data. 4598dcc1a9dSDamien Le Moal */ 4608dcc1a9dSDamien Le Moal zonefs_update_stats(inode, data_size); 461b5c00e97SJohannes Thumshirn zonefs_i_size_write(inode, data_size); 4628dcc1a9dSDamien Le Moal zi->i_wpoffset = data_size; 46387c9ce3fSDamien Le Moal zonefs_account_active(inode); 4648dcc1a9dSDamien Le Moal 4658dcc1a9dSDamien Le Moal return 0; 4668dcc1a9dSDamien Le Moal } 4678dcc1a9dSDamien Le Moal 4688dcc1a9dSDamien Le Moal /* 4698dcc1a9dSDamien Le Moal * When an file IO error occurs, check the file zone to see if there is a change 4708dcc1a9dSDamien Le Moal * in the zone condition (e.g. offline or read-only). For a failed write to a 4718dcc1a9dSDamien Le Moal * sequential zone, the zone write pointer position must also be checked to 4728dcc1a9dSDamien Le Moal * eventually correct the file size and zonefs inode write pointer offset 4738dcc1a9dSDamien Le Moal * (which can be out of sync with the drive due to partial write failures). 4748dcc1a9dSDamien Le Moal */ 47548d546a8SJohannes Thumshirn static void __zonefs_io_error(struct inode *inode, bool write) 4768dcc1a9dSDamien Le Moal { 4778dcc1a9dSDamien Le Moal struct zonefs_inode_info *zi = ZONEFS_I(inode); 4788dcc1a9dSDamien Le Moal struct super_block *sb = inode->i_sb; 4798dcc1a9dSDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 4808dcc1a9dSDamien Le Moal unsigned int noio_flag; 481*7dd12d65SDamien Le Moal unsigned int nr_zones = 1; 4828dcc1a9dSDamien Le Moal struct zonefs_ioerr_data err = { 4838dcc1a9dSDamien Le Moal .inode = inode, 4848dcc1a9dSDamien Le Moal .write = write, 4858dcc1a9dSDamien Le Moal }; 4868dcc1a9dSDamien Le Moal int ret; 4878dcc1a9dSDamien Le Moal 4888dcc1a9dSDamien Le Moal /* 489*7dd12d65SDamien Le Moal * The only files that have more than one zone are conventional zone 490*7dd12d65SDamien Le Moal * files with aggregated conventional zones, for which the inode zone 491*7dd12d65SDamien Le Moal * size is always larger than the device zone size. 492*7dd12d65SDamien Le Moal */ 493*7dd12d65SDamien Le Moal if (zi->i_zone_size > bdev_zone_sectors(sb->s_bdev)) 494*7dd12d65SDamien Le Moal nr_zones = zi->i_zone_size >> 495*7dd12d65SDamien Le Moal (sbi->s_zone_sectors_shift + SECTOR_SHIFT); 496*7dd12d65SDamien Le Moal 497*7dd12d65SDamien Le Moal /* 4988dcc1a9dSDamien Le Moal * Memory allocations in blkdev_report_zones() can trigger a memory 4998dcc1a9dSDamien Le Moal * reclaim which may in turn cause a recursion into zonefs as well as 5008dcc1a9dSDamien Le Moal * struct request allocations for the same device. The former case may 5018dcc1a9dSDamien Le Moal * end up in a deadlock on the inode truncate mutex, while the latter 5028dcc1a9dSDamien Le Moal * may prevent IO forward progress. Executing the report zones under 5038dcc1a9dSDamien Le Moal * the GFP_NOIO context avoids both problems. 5048dcc1a9dSDamien Le Moal */ 5058dcc1a9dSDamien Le Moal noio_flag = memalloc_noio_save(); 5068dcc1a9dSDamien Le Moal ret = blkdev_report_zones(sb->s_bdev, zi->i_zsector, nr_zones, 5078dcc1a9dSDamien Le Moal zonefs_io_error_cb, &err); 5088dcc1a9dSDamien Le Moal if (ret != nr_zones) 5098dcc1a9dSDamien Le Moal zonefs_err(sb, "Get inode %lu zone information failed %d\n", 5108dcc1a9dSDamien Le Moal inode->i_ino, ret); 5118dcc1a9dSDamien Le Moal memalloc_noio_restore(noio_flag); 51248d546a8SJohannes Thumshirn } 5138dcc1a9dSDamien Le Moal 51448d546a8SJohannes Thumshirn static void zonefs_io_error(struct inode *inode, bool write) 51548d546a8SJohannes Thumshirn { 51648d546a8SJohannes Thumshirn struct zonefs_inode_info *zi = ZONEFS_I(inode); 51748d546a8SJohannes Thumshirn 51848d546a8SJohannes Thumshirn mutex_lock(&zi->i_truncate_mutex); 51948d546a8SJohannes Thumshirn __zonefs_io_error(inode, write); 5208dcc1a9dSDamien Le Moal mutex_unlock(&zi->i_truncate_mutex); 5218dcc1a9dSDamien Le Moal } 5228dcc1a9dSDamien Le Moal 5238dcc1a9dSDamien Le Moal static int zonefs_file_truncate(struct inode *inode, loff_t isize) 5248dcc1a9dSDamien Le Moal { 5258dcc1a9dSDamien Le Moal struct zonefs_inode_info *zi = ZONEFS_I(inode); 5268dcc1a9dSDamien Le Moal loff_t old_isize; 527ff07a02eSBart Van Assche enum req_op op; 5288dcc1a9dSDamien Le Moal int ret = 0; 5298dcc1a9dSDamien Le Moal 5308dcc1a9dSDamien Le Moal /* 5318dcc1a9dSDamien Le Moal * Only sequential zone files can be truncated and truncation is allowed 5328dcc1a9dSDamien Le Moal * only down to a 0 size, which is equivalent to a zone reset, and to 5338dcc1a9dSDamien Le Moal * the maximum file size, which is equivalent to a zone finish. 5348dcc1a9dSDamien Le Moal */ 5358dcc1a9dSDamien Le Moal if (zi->i_ztype != ZONEFS_ZTYPE_SEQ) 5368dcc1a9dSDamien Le Moal return -EPERM; 5378dcc1a9dSDamien Le Moal 5388dcc1a9dSDamien Le Moal if (!isize) 5398dcc1a9dSDamien Le Moal op = REQ_OP_ZONE_RESET; 5408dcc1a9dSDamien Le Moal else if (isize == zi->i_max_size) 5418dcc1a9dSDamien Le Moal op = REQ_OP_ZONE_FINISH; 5428dcc1a9dSDamien Le Moal else 5438dcc1a9dSDamien Le Moal return -EPERM; 5448dcc1a9dSDamien Le Moal 5458dcc1a9dSDamien Le Moal inode_dio_wait(inode); 5468dcc1a9dSDamien Le Moal 5478dcc1a9dSDamien Le Moal /* Serialize against page faults */ 548448f9490SJan Kara filemap_invalidate_lock(inode->i_mapping); 5498dcc1a9dSDamien Le Moal 5508dcc1a9dSDamien Le Moal /* Serialize against zonefs_iomap_begin() */ 5518dcc1a9dSDamien Le Moal mutex_lock(&zi->i_truncate_mutex); 5528dcc1a9dSDamien Le Moal 5538dcc1a9dSDamien Le Moal old_isize = i_size_read(inode); 5548dcc1a9dSDamien Le Moal if (isize == old_isize) 5558dcc1a9dSDamien Le Moal goto unlock; 5568dcc1a9dSDamien Le Moal 5575498d5f9SJohannes Thumshirn ret = zonefs_zone_mgmt(inode, op); 5585498d5f9SJohannes Thumshirn if (ret) 5598dcc1a9dSDamien Le Moal goto unlock; 5608dcc1a9dSDamien Le Moal 561b5c00e97SJohannes Thumshirn /* 562b5c00e97SJohannes Thumshirn * If the mount option ZONEFS_MNTOPT_EXPLICIT_OPEN is set, 563b5c00e97SJohannes Thumshirn * take care of open zones. 564b5c00e97SJohannes Thumshirn */ 565b5c00e97SJohannes Thumshirn if (zi->i_flags & ZONEFS_ZONE_OPEN) { 566b5c00e97SJohannes Thumshirn /* 567b5c00e97SJohannes Thumshirn * Truncating a zone to EMPTY or FULL is the equivalent of 568b5c00e97SJohannes Thumshirn * closing the zone. For a truncation to 0, we need to 569b5c00e97SJohannes Thumshirn * re-open the zone to ensure new writes can be processed. 570b5c00e97SJohannes Thumshirn * For a truncation to the maximum file size, the zone is 571b5c00e97SJohannes Thumshirn * closed and writes cannot be accepted anymore, so clear 572b5c00e97SJohannes Thumshirn * the open flag. 573b5c00e97SJohannes Thumshirn */ 574b5c00e97SJohannes Thumshirn if (!isize) 575b5c00e97SJohannes Thumshirn ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_OPEN); 576b5c00e97SJohannes Thumshirn else 577b5c00e97SJohannes Thumshirn zi->i_flags &= ~ZONEFS_ZONE_OPEN; 578b5c00e97SJohannes Thumshirn } 579b5c00e97SJohannes Thumshirn 5808dcc1a9dSDamien Le Moal zonefs_update_stats(inode, isize); 5818dcc1a9dSDamien Le Moal truncate_setsize(inode, isize); 5828dcc1a9dSDamien Le Moal zi->i_wpoffset = isize; 58387c9ce3fSDamien Le Moal zonefs_account_active(inode); 5848dcc1a9dSDamien Le Moal 5858dcc1a9dSDamien Le Moal unlock: 5868dcc1a9dSDamien Le Moal mutex_unlock(&zi->i_truncate_mutex); 587448f9490SJan Kara filemap_invalidate_unlock(inode->i_mapping); 5888dcc1a9dSDamien Le Moal 5898dcc1a9dSDamien Le Moal return ret; 5908dcc1a9dSDamien Le Moal } 5918dcc1a9dSDamien Le Moal 592549c7297SChristian Brauner static int zonefs_inode_setattr(struct user_namespace *mnt_userns, 593549c7297SChristian Brauner struct dentry *dentry, struct iattr *iattr) 5948dcc1a9dSDamien Le Moal { 5958dcc1a9dSDamien Le Moal struct inode *inode = d_inode(dentry); 5968dcc1a9dSDamien Le Moal int ret; 5978dcc1a9dSDamien Le Moal 5988dcc1a9dSDamien Le Moal if (unlikely(IS_IMMUTABLE(inode))) 5998dcc1a9dSDamien Le Moal return -EPERM; 6008dcc1a9dSDamien Le Moal 6012f221d6fSChristian Brauner ret = setattr_prepare(&init_user_ns, dentry, iattr); 6028dcc1a9dSDamien Le Moal if (ret) 6038dcc1a9dSDamien Le Moal return ret; 6048dcc1a9dSDamien Le Moal 6058dcc1a9dSDamien Le Moal /* 6068dcc1a9dSDamien Le Moal * Since files and directories cannot be created nor deleted, do not 6078dcc1a9dSDamien Le Moal * allow setting any write attributes on the sub-directories grouping 6088dcc1a9dSDamien Le Moal * files by zone type. 6098dcc1a9dSDamien Le Moal */ 6108dcc1a9dSDamien Le Moal if ((iattr->ia_valid & ATTR_MODE) && S_ISDIR(inode->i_mode) && 6118dcc1a9dSDamien Le Moal (iattr->ia_mode & 0222)) 6128dcc1a9dSDamien Le Moal return -EPERM; 6138dcc1a9dSDamien Le Moal 6148dcc1a9dSDamien Le Moal if (((iattr->ia_valid & ATTR_UID) && 6158dcc1a9dSDamien Le Moal !uid_eq(iattr->ia_uid, inode->i_uid)) || 6168dcc1a9dSDamien Le Moal ((iattr->ia_valid & ATTR_GID) && 6178dcc1a9dSDamien Le Moal !gid_eq(iattr->ia_gid, inode->i_gid))) { 618b27c82e1SChristian Brauner ret = dquot_transfer(mnt_userns, inode, iattr); 6198dcc1a9dSDamien Le Moal if (ret) 6208dcc1a9dSDamien Le Moal return ret; 6218dcc1a9dSDamien Le Moal } 6228dcc1a9dSDamien Le Moal 6238dcc1a9dSDamien Le Moal if (iattr->ia_valid & ATTR_SIZE) { 6248dcc1a9dSDamien Le Moal ret = zonefs_file_truncate(inode, iattr->ia_size); 6258dcc1a9dSDamien Le Moal if (ret) 6268dcc1a9dSDamien Le Moal return ret; 6278dcc1a9dSDamien Le Moal } 6288dcc1a9dSDamien Le Moal 6292f221d6fSChristian Brauner setattr_copy(&init_user_ns, inode, iattr); 6308dcc1a9dSDamien Le Moal 6318dcc1a9dSDamien Le Moal return 0; 6328dcc1a9dSDamien Le Moal } 6338dcc1a9dSDamien Le Moal 6348dcc1a9dSDamien Le Moal static const struct inode_operations zonefs_file_inode_operations = { 6358dcc1a9dSDamien Le Moal .setattr = zonefs_inode_setattr, 6368dcc1a9dSDamien Le Moal }; 6378dcc1a9dSDamien Le Moal 6388dcc1a9dSDamien Le Moal static int zonefs_file_fsync(struct file *file, loff_t start, loff_t end, 6398dcc1a9dSDamien Le Moal int datasync) 6408dcc1a9dSDamien Le Moal { 6418dcc1a9dSDamien Le Moal struct inode *inode = file_inode(file); 6428dcc1a9dSDamien Le Moal int ret = 0; 6438dcc1a9dSDamien Le Moal 6448dcc1a9dSDamien Le Moal if (unlikely(IS_IMMUTABLE(inode))) 6458dcc1a9dSDamien Le Moal return -EPERM; 6468dcc1a9dSDamien Le Moal 6478dcc1a9dSDamien Le Moal /* 6488dcc1a9dSDamien Le Moal * Since only direct writes are allowed in sequential files, page cache 6498dcc1a9dSDamien Le Moal * flush is needed only for conventional zone files. 6508dcc1a9dSDamien Le Moal */ 6518dcc1a9dSDamien Le Moal if (ZONEFS_I(inode)->i_ztype == ZONEFS_ZTYPE_CNV) 6528dcc1a9dSDamien Le Moal ret = file_write_and_wait_range(file, start, end); 6538dcc1a9dSDamien Le Moal if (!ret) 654c6bf3f0eSChristoph Hellwig ret = blkdev_issue_flush(inode->i_sb->s_bdev); 6558dcc1a9dSDamien Le Moal 6568dcc1a9dSDamien Le Moal if (ret) 6578dcc1a9dSDamien Le Moal zonefs_io_error(inode, true); 6588dcc1a9dSDamien Le Moal 6598dcc1a9dSDamien Le Moal return ret; 6608dcc1a9dSDamien Le Moal } 6618dcc1a9dSDamien Le Moal 6628dcc1a9dSDamien Le Moal static vm_fault_t zonefs_filemap_page_mkwrite(struct vm_fault *vmf) 6638dcc1a9dSDamien Le Moal { 6648dcc1a9dSDamien Le Moal struct inode *inode = file_inode(vmf->vma->vm_file); 6658dcc1a9dSDamien Le Moal struct zonefs_inode_info *zi = ZONEFS_I(inode); 6668dcc1a9dSDamien Le Moal vm_fault_t ret; 6678dcc1a9dSDamien Le Moal 6688dcc1a9dSDamien Le Moal if (unlikely(IS_IMMUTABLE(inode))) 6698dcc1a9dSDamien Le Moal return VM_FAULT_SIGBUS; 6708dcc1a9dSDamien Le Moal 6718dcc1a9dSDamien Le Moal /* 6728dcc1a9dSDamien Le Moal * Sanity check: only conventional zone files can have shared 6738dcc1a9dSDamien Le Moal * writeable mappings. 6748dcc1a9dSDamien Le Moal */ 6758dcc1a9dSDamien Le Moal if (WARN_ON_ONCE(zi->i_ztype != ZONEFS_ZTYPE_CNV)) 6768dcc1a9dSDamien Le Moal return VM_FAULT_NOPAGE; 6778dcc1a9dSDamien Le Moal 6788dcc1a9dSDamien Le Moal sb_start_pagefault(inode->i_sb); 6798dcc1a9dSDamien Le Moal file_update_time(vmf->vma->vm_file); 6808dcc1a9dSDamien Le Moal 6818dcc1a9dSDamien Le Moal /* Serialize against truncates */ 682448f9490SJan Kara filemap_invalidate_lock_shared(inode->i_mapping); 683c1c1204cSDamien Le Moal ret = iomap_page_mkwrite(vmf, &zonefs_write_iomap_ops); 684448f9490SJan Kara filemap_invalidate_unlock_shared(inode->i_mapping); 6858dcc1a9dSDamien Le Moal 6868dcc1a9dSDamien Le Moal sb_end_pagefault(inode->i_sb); 6878dcc1a9dSDamien Le Moal return ret; 6888dcc1a9dSDamien Le Moal } 6898dcc1a9dSDamien Le Moal 6908dcc1a9dSDamien Le Moal static const struct vm_operations_struct zonefs_file_vm_ops = { 691448f9490SJan Kara .fault = filemap_fault, 6928dcc1a9dSDamien Le Moal .map_pages = filemap_map_pages, 6938dcc1a9dSDamien Le Moal .page_mkwrite = zonefs_filemap_page_mkwrite, 6948dcc1a9dSDamien Le Moal }; 6958dcc1a9dSDamien Le Moal 6968dcc1a9dSDamien Le Moal static int zonefs_file_mmap(struct file *file, struct vm_area_struct *vma) 6978dcc1a9dSDamien Le Moal { 6988dcc1a9dSDamien Le Moal /* 6998dcc1a9dSDamien Le Moal * Conventional zones accept random writes, so their files can support 7008dcc1a9dSDamien Le Moal * shared writable mappings. For sequential zone files, only read 7018dcc1a9dSDamien Le Moal * mappings are possible since there are no guarantees for write 7028dcc1a9dSDamien Le Moal * ordering between msync() and page cache writeback. 7038dcc1a9dSDamien Le Moal */ 7048dcc1a9dSDamien Le Moal if (ZONEFS_I(file_inode(file))->i_ztype == ZONEFS_ZTYPE_SEQ && 7058dcc1a9dSDamien Le Moal (vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) 7068dcc1a9dSDamien Le Moal return -EINVAL; 7078dcc1a9dSDamien Le Moal 7088dcc1a9dSDamien Le Moal file_accessed(file); 7098dcc1a9dSDamien Le Moal vma->vm_ops = &zonefs_file_vm_ops; 7108dcc1a9dSDamien Le Moal 7118dcc1a9dSDamien Le Moal return 0; 7128dcc1a9dSDamien Le Moal } 7138dcc1a9dSDamien Le Moal 7148dcc1a9dSDamien Le Moal static loff_t zonefs_file_llseek(struct file *file, loff_t offset, int whence) 7158dcc1a9dSDamien Le Moal { 7168dcc1a9dSDamien Le Moal loff_t isize = i_size_read(file_inode(file)); 7178dcc1a9dSDamien Le Moal 7188dcc1a9dSDamien Le Moal /* 7198dcc1a9dSDamien Le Moal * Seeks are limited to below the zone size for conventional zones 7208dcc1a9dSDamien Le Moal * and below the zone write pointer for sequential zones. In both 7218dcc1a9dSDamien Le Moal * cases, this limit is the inode size. 7228dcc1a9dSDamien Le Moal */ 7238dcc1a9dSDamien Le Moal return generic_file_llseek_size(file, offset, whence, isize, isize); 7248dcc1a9dSDamien Le Moal } 7258dcc1a9dSDamien Le Moal 7268dcc1a9dSDamien Le Moal static int zonefs_file_write_dio_end_io(struct kiocb *iocb, ssize_t size, 7278dcc1a9dSDamien Le Moal int error, unsigned int flags) 7288dcc1a9dSDamien Le Moal { 7298dcc1a9dSDamien Le Moal struct inode *inode = file_inode(iocb->ki_filp); 7308dcc1a9dSDamien Le Moal struct zonefs_inode_info *zi = ZONEFS_I(inode); 7318dcc1a9dSDamien Le Moal 7328dcc1a9dSDamien Le Moal if (error) { 7338dcc1a9dSDamien Le Moal zonefs_io_error(inode, true); 7348dcc1a9dSDamien Le Moal return error; 7358dcc1a9dSDamien Le Moal } 7368dcc1a9dSDamien Le Moal 7378dcc1a9dSDamien Le Moal if (size && zi->i_ztype != ZONEFS_ZTYPE_CNV) { 7388dcc1a9dSDamien Le Moal /* 7398dcc1a9dSDamien Le Moal * Note that we may be seeing completions out of order, 7408dcc1a9dSDamien Le Moal * but that is not a problem since a write completed 7418dcc1a9dSDamien Le Moal * successfully necessarily means that all preceding writes 7428dcc1a9dSDamien Le Moal * were also successful. So we can safely increase the inode 7438dcc1a9dSDamien Le Moal * size to the write end location. 7448dcc1a9dSDamien Le Moal */ 7458dcc1a9dSDamien Le Moal mutex_lock(&zi->i_truncate_mutex); 7468dcc1a9dSDamien Le Moal if (i_size_read(inode) < iocb->ki_pos + size) { 7478dcc1a9dSDamien Le Moal zonefs_update_stats(inode, iocb->ki_pos + size); 748b5c00e97SJohannes Thumshirn zonefs_i_size_write(inode, iocb->ki_pos + size); 7498dcc1a9dSDamien Le Moal } 7508dcc1a9dSDamien Le Moal mutex_unlock(&zi->i_truncate_mutex); 7518dcc1a9dSDamien Le Moal } 7528dcc1a9dSDamien Le Moal 7538dcc1a9dSDamien Le Moal return 0; 7548dcc1a9dSDamien Le Moal } 7558dcc1a9dSDamien Le Moal 7568dcc1a9dSDamien Le Moal static const struct iomap_dio_ops zonefs_write_dio_ops = { 7578dcc1a9dSDamien Le Moal .end_io = zonefs_file_write_dio_end_io, 7588dcc1a9dSDamien Le Moal }; 7598dcc1a9dSDamien Le Moal 76002ef12a6SJohannes Thumshirn static ssize_t zonefs_file_dio_append(struct kiocb *iocb, struct iov_iter *from) 76102ef12a6SJohannes Thumshirn { 76202ef12a6SJohannes Thumshirn struct inode *inode = file_inode(iocb->ki_filp); 76302ef12a6SJohannes Thumshirn struct zonefs_inode_info *zi = ZONEFS_I(inode); 76402ef12a6SJohannes Thumshirn struct block_device *bdev = inode->i_sb->s_bdev; 7652aba0d19SChristoph Hellwig unsigned int max = bdev_max_zone_append_sectors(bdev); 76602ef12a6SJohannes Thumshirn struct bio *bio; 76702ef12a6SJohannes Thumshirn ssize_t size; 76802ef12a6SJohannes Thumshirn int nr_pages; 76902ef12a6SJohannes Thumshirn ssize_t ret; 77002ef12a6SJohannes Thumshirn 77102ef12a6SJohannes Thumshirn max = ALIGN_DOWN(max << SECTOR_SHIFT, inode->i_sb->s_blocksize); 77202ef12a6SJohannes Thumshirn iov_iter_truncate(from, max); 77302ef12a6SJohannes Thumshirn 774a8affc03SChristoph Hellwig nr_pages = iov_iter_npages(from, BIO_MAX_VECS); 77589ee7237SJohannes Thumshirn if (!nr_pages) 77689ee7237SJohannes Thumshirn return 0; 77789ee7237SJohannes Thumshirn 77807888c66SChristoph Hellwig bio = bio_alloc(bdev, nr_pages, 77907888c66SChristoph Hellwig REQ_OP_ZONE_APPEND | REQ_SYNC | REQ_IDLE, GFP_NOFS); 78002ef12a6SJohannes Thumshirn bio->bi_iter.bi_sector = zi->i_zsector; 78102ef12a6SJohannes Thumshirn bio->bi_ioprio = iocb->ki_ioprio; 78291b94c5dSAl Viro if (iocb_is_dsync(iocb)) 78302ef12a6SJohannes Thumshirn bio->bi_opf |= REQ_FUA; 78402ef12a6SJohannes Thumshirn 78502ef12a6SJohannes Thumshirn ret = bio_iov_iter_get_pages(bio, from); 7866bea0225SDamien Le Moal if (unlikely(ret)) 7876bea0225SDamien Le Moal goto out_release; 7886bea0225SDamien Le Moal 78902ef12a6SJohannes Thumshirn size = bio->bi_iter.bi_size; 7906bea0225SDamien Le Moal task_io_account_write(size); 79102ef12a6SJohannes Thumshirn 79202ef12a6SJohannes Thumshirn if (iocb->ki_flags & IOCB_HIPRI) 79302ef12a6SJohannes Thumshirn bio_set_polled(bio, iocb); 79402ef12a6SJohannes Thumshirn 79502ef12a6SJohannes Thumshirn ret = submit_bio_wait(bio); 79602ef12a6SJohannes Thumshirn 7976bea0225SDamien Le Moal zonefs_file_write_dio_end_io(iocb, size, ret, 0); 79862ab1aadSJohannes Thumshirn trace_zonefs_file_dio_append(inode, size, ret); 7996bea0225SDamien Le Moal 8006bea0225SDamien Le Moal out_release: 8016bea0225SDamien Le Moal bio_release_pages(bio, false); 80202ef12a6SJohannes Thumshirn bio_put(bio); 80302ef12a6SJohannes Thumshirn 80402ef12a6SJohannes Thumshirn if (ret >= 0) { 80502ef12a6SJohannes Thumshirn iocb->ki_pos += size; 80602ef12a6SJohannes Thumshirn return size; 80702ef12a6SJohannes Thumshirn } 80802ef12a6SJohannes Thumshirn 80902ef12a6SJohannes Thumshirn return ret; 81002ef12a6SJohannes Thumshirn } 81102ef12a6SJohannes Thumshirn 8128dcc1a9dSDamien Le Moal /* 813ebfd68cdSDamien Le Moal * Do not exceed the LFS limits nor the file zone size. If pos is under the 814ebfd68cdSDamien Le Moal * limit it becomes a short access. If it exceeds the limit, return -EFBIG. 815ebfd68cdSDamien Le Moal */ 816ebfd68cdSDamien Le Moal static loff_t zonefs_write_check_limits(struct file *file, loff_t pos, 817ebfd68cdSDamien Le Moal loff_t count) 818ebfd68cdSDamien Le Moal { 819ebfd68cdSDamien Le Moal struct inode *inode = file_inode(file); 820ebfd68cdSDamien Le Moal struct zonefs_inode_info *zi = ZONEFS_I(inode); 821ebfd68cdSDamien Le Moal loff_t limit = rlimit(RLIMIT_FSIZE); 822ebfd68cdSDamien Le Moal loff_t max_size = zi->i_max_size; 823ebfd68cdSDamien Le Moal 824ebfd68cdSDamien Le Moal if (limit != RLIM_INFINITY) { 825ebfd68cdSDamien Le Moal if (pos >= limit) { 826ebfd68cdSDamien Le Moal send_sig(SIGXFSZ, current, 0); 827ebfd68cdSDamien Le Moal return -EFBIG; 828ebfd68cdSDamien Le Moal } 829ebfd68cdSDamien Le Moal count = min(count, limit - pos); 830ebfd68cdSDamien Le Moal } 831ebfd68cdSDamien Le Moal 832ebfd68cdSDamien Le Moal if (!(file->f_flags & O_LARGEFILE)) 833ebfd68cdSDamien Le Moal max_size = min_t(loff_t, MAX_NON_LFS, max_size); 834ebfd68cdSDamien Le Moal 835ebfd68cdSDamien Le Moal if (unlikely(pos >= max_size)) 836ebfd68cdSDamien Le Moal return -EFBIG; 837ebfd68cdSDamien Le Moal 838ebfd68cdSDamien Le Moal return min(count, max_size - pos); 839ebfd68cdSDamien Le Moal } 840ebfd68cdSDamien Le Moal 841ebfd68cdSDamien Le Moal static ssize_t zonefs_write_checks(struct kiocb *iocb, struct iov_iter *from) 842ebfd68cdSDamien Le Moal { 843ebfd68cdSDamien Le Moal struct file *file = iocb->ki_filp; 844ebfd68cdSDamien Le Moal struct inode *inode = file_inode(file); 845ebfd68cdSDamien Le Moal struct zonefs_inode_info *zi = ZONEFS_I(inode); 846ebfd68cdSDamien Le Moal loff_t count; 847ebfd68cdSDamien Le Moal 848ebfd68cdSDamien Le Moal if (IS_SWAPFILE(inode)) 849ebfd68cdSDamien Le Moal return -ETXTBSY; 850ebfd68cdSDamien Le Moal 851ebfd68cdSDamien Le Moal if (!iov_iter_count(from)) 852ebfd68cdSDamien Le Moal return 0; 853ebfd68cdSDamien Le Moal 854ebfd68cdSDamien Le Moal if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT)) 855ebfd68cdSDamien Le Moal return -EINVAL; 856ebfd68cdSDamien Le Moal 857ebfd68cdSDamien Le Moal if (iocb->ki_flags & IOCB_APPEND) { 858ebfd68cdSDamien Le Moal if (zi->i_ztype != ZONEFS_ZTYPE_SEQ) 859ebfd68cdSDamien Le Moal return -EINVAL; 860ebfd68cdSDamien Le Moal mutex_lock(&zi->i_truncate_mutex); 861ebfd68cdSDamien Le Moal iocb->ki_pos = zi->i_wpoffset; 862ebfd68cdSDamien Le Moal mutex_unlock(&zi->i_truncate_mutex); 863ebfd68cdSDamien Le Moal } 864ebfd68cdSDamien Le Moal 865ebfd68cdSDamien Le Moal count = zonefs_write_check_limits(file, iocb->ki_pos, 866ebfd68cdSDamien Le Moal iov_iter_count(from)); 867ebfd68cdSDamien Le Moal if (count < 0) 868ebfd68cdSDamien Le Moal return count; 869ebfd68cdSDamien Le Moal 870ebfd68cdSDamien Le Moal iov_iter_truncate(from, count); 871ebfd68cdSDamien Le Moal return iov_iter_count(from); 872ebfd68cdSDamien Le Moal } 873ebfd68cdSDamien Le Moal 874ebfd68cdSDamien Le Moal /* 8758dcc1a9dSDamien Le Moal * Handle direct writes. For sequential zone files, this is the only possible 8768dcc1a9dSDamien Le Moal * write path. For these files, check that the user is issuing writes 8778dcc1a9dSDamien Le Moal * sequentially from the end of the file. This code assumes that the block layer 8788dcc1a9dSDamien Le Moal * delivers write requests to the device in sequential order. This is always the 8798dcc1a9dSDamien Le Moal * case if a block IO scheduler implementing the ELEVATOR_F_ZBD_SEQ_WRITE 8808dcc1a9dSDamien Le Moal * elevator feature is being used (e.g. mq-deadline). The block layer always 8818dcc1a9dSDamien Le Moal * automatically select such an elevator for zoned block devices during the 8828dcc1a9dSDamien Le Moal * device initialization. 8838dcc1a9dSDamien Le Moal */ 8848dcc1a9dSDamien Le Moal static ssize_t zonefs_file_dio_write(struct kiocb *iocb, struct iov_iter *from) 8858dcc1a9dSDamien Le Moal { 8868dcc1a9dSDamien Le Moal struct inode *inode = file_inode(iocb->ki_filp); 8878dcc1a9dSDamien Le Moal struct zonefs_inode_info *zi = ZONEFS_I(inode); 8888dcc1a9dSDamien Le Moal struct super_block *sb = inode->i_sb; 88902ef12a6SJohannes Thumshirn bool sync = is_sync_kiocb(iocb); 89002ef12a6SJohannes Thumshirn bool append = false; 891ebfd68cdSDamien Le Moal ssize_t ret, count; 8928dcc1a9dSDamien Le Moal 8938dcc1a9dSDamien Le Moal /* 8947c69eb84SChristoph Hellwig * For async direct IOs to sequential zone files, refuse IOCB_NOWAIT 8958dcc1a9dSDamien Le Moal * as this can cause write reordering (e.g. the first aio gets EAGAIN 8968dcc1a9dSDamien Le Moal * on the inode lock but the second goes through but is now unaligned). 8978dcc1a9dSDamien Le Moal */ 89802ef12a6SJohannes Thumshirn if (zi->i_ztype == ZONEFS_ZTYPE_SEQ && !sync && 8997c69eb84SChristoph Hellwig (iocb->ki_flags & IOCB_NOWAIT)) 9007c69eb84SChristoph Hellwig return -EOPNOTSUPP; 9018dcc1a9dSDamien Le Moal 9028dcc1a9dSDamien Le Moal if (iocb->ki_flags & IOCB_NOWAIT) { 9038dcc1a9dSDamien Le Moal if (!inode_trylock(inode)) 9048dcc1a9dSDamien Le Moal return -EAGAIN; 9058dcc1a9dSDamien Le Moal } else { 9068dcc1a9dSDamien Le Moal inode_lock(inode); 9078dcc1a9dSDamien Le Moal } 9088dcc1a9dSDamien Le Moal 909ebfd68cdSDamien Le Moal count = zonefs_write_checks(iocb, from); 910ebfd68cdSDamien Le Moal if (count <= 0) { 911ebfd68cdSDamien Le Moal ret = count; 9128dcc1a9dSDamien Le Moal goto inode_unlock; 913ebfd68cdSDamien Le Moal } 9148dcc1a9dSDamien Le Moal 9158dcc1a9dSDamien Le Moal if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) { 9168dcc1a9dSDamien Le Moal ret = -EINVAL; 9178dcc1a9dSDamien Le Moal goto inode_unlock; 9188dcc1a9dSDamien Le Moal } 9198dcc1a9dSDamien Le Moal 9208dcc1a9dSDamien Le Moal /* Enforce sequential writes (append only) in sequential zones */ 92102ef12a6SJohannes Thumshirn if (zi->i_ztype == ZONEFS_ZTYPE_SEQ) { 9228dcc1a9dSDamien Le Moal mutex_lock(&zi->i_truncate_mutex); 92302ef12a6SJohannes Thumshirn if (iocb->ki_pos != zi->i_wpoffset) { 9248dcc1a9dSDamien Le Moal mutex_unlock(&zi->i_truncate_mutex); 9258dcc1a9dSDamien Le Moal ret = -EINVAL; 9268dcc1a9dSDamien Le Moal goto inode_unlock; 9278dcc1a9dSDamien Le Moal } 9288dcc1a9dSDamien Le Moal mutex_unlock(&zi->i_truncate_mutex); 92902ef12a6SJohannes Thumshirn append = sync; 93002ef12a6SJohannes Thumshirn } 9318dcc1a9dSDamien Le Moal 93202ef12a6SJohannes Thumshirn if (append) 93302ef12a6SJohannes Thumshirn ret = zonefs_file_dio_append(iocb, from); 93402ef12a6SJohannes Thumshirn else 935c1c1204cSDamien Le Moal ret = iomap_dio_rw(iocb, from, &zonefs_write_iomap_ops, 936786f847fSChristoph Hellwig &zonefs_write_dio_ops, 0, NULL, 0); 9378dcc1a9dSDamien Le Moal if (zi->i_ztype == ZONEFS_ZTYPE_SEQ && 9388dcc1a9dSDamien Le Moal (ret > 0 || ret == -EIOCBQUEUED)) { 9398dcc1a9dSDamien Le Moal if (ret > 0) 9408dcc1a9dSDamien Le Moal count = ret; 94187c9ce3fSDamien Le Moal 94287c9ce3fSDamien Le Moal /* 94387c9ce3fSDamien Le Moal * Update the zone write pointer offset assuming the write 94487c9ce3fSDamien Le Moal * operation succeeded. If it did not, the error recovery path 94587c9ce3fSDamien Le Moal * will correct it. Also do active seq file accounting. 94687c9ce3fSDamien Le Moal */ 9478dcc1a9dSDamien Le Moal mutex_lock(&zi->i_truncate_mutex); 9488dcc1a9dSDamien Le Moal zi->i_wpoffset += count; 94987c9ce3fSDamien Le Moal zonefs_account_active(inode); 9508dcc1a9dSDamien Le Moal mutex_unlock(&zi->i_truncate_mutex); 9518dcc1a9dSDamien Le Moal } 9528dcc1a9dSDamien Le Moal 9538dcc1a9dSDamien Le Moal inode_unlock: 9548dcc1a9dSDamien Le Moal inode_unlock(inode); 9558dcc1a9dSDamien Le Moal 9568dcc1a9dSDamien Le Moal return ret; 9578dcc1a9dSDamien Le Moal } 9588dcc1a9dSDamien Le Moal 9598dcc1a9dSDamien Le Moal static ssize_t zonefs_file_buffered_write(struct kiocb *iocb, 9608dcc1a9dSDamien Le Moal struct iov_iter *from) 9618dcc1a9dSDamien Le Moal { 9628dcc1a9dSDamien Le Moal struct inode *inode = file_inode(iocb->ki_filp); 9638dcc1a9dSDamien Le Moal struct zonefs_inode_info *zi = ZONEFS_I(inode); 9648dcc1a9dSDamien Le Moal ssize_t ret; 9658dcc1a9dSDamien Le Moal 9668dcc1a9dSDamien Le Moal /* 9678dcc1a9dSDamien Le Moal * Direct IO writes are mandatory for sequential zone files so that the 9688dcc1a9dSDamien Le Moal * write IO issuing order is preserved. 9698dcc1a9dSDamien Le Moal */ 9708dcc1a9dSDamien Le Moal if (zi->i_ztype != ZONEFS_ZTYPE_CNV) 9718dcc1a9dSDamien Le Moal return -EIO; 9728dcc1a9dSDamien Le Moal 9738dcc1a9dSDamien Le Moal if (iocb->ki_flags & IOCB_NOWAIT) { 9748dcc1a9dSDamien Le Moal if (!inode_trylock(inode)) 9758dcc1a9dSDamien Le Moal return -EAGAIN; 9768dcc1a9dSDamien Le Moal } else { 9778dcc1a9dSDamien Le Moal inode_lock(inode); 9788dcc1a9dSDamien Le Moal } 9798dcc1a9dSDamien Le Moal 980ebfd68cdSDamien Le Moal ret = zonefs_write_checks(iocb, from); 9818dcc1a9dSDamien Le Moal if (ret <= 0) 9828dcc1a9dSDamien Le Moal goto inode_unlock; 9838dcc1a9dSDamien Le Moal 984c1c1204cSDamien Le Moal ret = iomap_file_buffered_write(iocb, from, &zonefs_write_iomap_ops); 9858dcc1a9dSDamien Le Moal if (ret > 0) 9868dcc1a9dSDamien Le Moal iocb->ki_pos += ret; 9878dcc1a9dSDamien Le Moal else if (ret == -EIO) 9888dcc1a9dSDamien Le Moal zonefs_io_error(inode, true); 9898dcc1a9dSDamien Le Moal 9908dcc1a9dSDamien Le Moal inode_unlock: 9918dcc1a9dSDamien Le Moal inode_unlock(inode); 9928dcc1a9dSDamien Le Moal if (ret > 0) 9938dcc1a9dSDamien Le Moal ret = generic_write_sync(iocb, ret); 9948dcc1a9dSDamien Le Moal 9958dcc1a9dSDamien Le Moal return ret; 9968dcc1a9dSDamien Le Moal } 9978dcc1a9dSDamien Le Moal 9988dcc1a9dSDamien Le Moal static ssize_t zonefs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) 9998dcc1a9dSDamien Le Moal { 10008dcc1a9dSDamien Le Moal struct inode *inode = file_inode(iocb->ki_filp); 10018dcc1a9dSDamien Le Moal 10028dcc1a9dSDamien Le Moal if (unlikely(IS_IMMUTABLE(inode))) 10038dcc1a9dSDamien Le Moal return -EPERM; 10048dcc1a9dSDamien Le Moal 10058dcc1a9dSDamien Le Moal if (sb_rdonly(inode->i_sb)) 10068dcc1a9dSDamien Le Moal return -EROFS; 10078dcc1a9dSDamien Le Moal 10088dcc1a9dSDamien Le Moal /* Write operations beyond the zone size are not allowed */ 10098dcc1a9dSDamien Le Moal if (iocb->ki_pos >= ZONEFS_I(inode)->i_max_size) 10108dcc1a9dSDamien Le Moal return -EFBIG; 10118dcc1a9dSDamien Le Moal 101260263d58SChristoph Hellwig if (iocb->ki_flags & IOCB_DIRECT) { 101360263d58SChristoph Hellwig ssize_t ret = zonefs_file_dio_write(iocb, from); 101460263d58SChristoph Hellwig if (ret != -ENOTBLK) 101560263d58SChristoph Hellwig return ret; 101660263d58SChristoph Hellwig } 10178dcc1a9dSDamien Le Moal 10188dcc1a9dSDamien Le Moal return zonefs_file_buffered_write(iocb, from); 10198dcc1a9dSDamien Le Moal } 10208dcc1a9dSDamien Le Moal 10218dcc1a9dSDamien Le Moal static int zonefs_file_read_dio_end_io(struct kiocb *iocb, ssize_t size, 10228dcc1a9dSDamien Le Moal int error, unsigned int flags) 10238dcc1a9dSDamien Le Moal { 10248dcc1a9dSDamien Le Moal if (error) { 10258dcc1a9dSDamien Le Moal zonefs_io_error(file_inode(iocb->ki_filp), false); 10268dcc1a9dSDamien Le Moal return error; 10278dcc1a9dSDamien Le Moal } 10288dcc1a9dSDamien Le Moal 10298dcc1a9dSDamien Le Moal return 0; 10308dcc1a9dSDamien Le Moal } 10318dcc1a9dSDamien Le Moal 10328dcc1a9dSDamien Le Moal static const struct iomap_dio_ops zonefs_read_dio_ops = { 10338dcc1a9dSDamien Le Moal .end_io = zonefs_file_read_dio_end_io, 10348dcc1a9dSDamien Le Moal }; 10358dcc1a9dSDamien Le Moal 10368dcc1a9dSDamien Le Moal static ssize_t zonefs_file_read_iter(struct kiocb *iocb, struct iov_iter *to) 10378dcc1a9dSDamien Le Moal { 10388dcc1a9dSDamien Le Moal struct inode *inode = file_inode(iocb->ki_filp); 10398dcc1a9dSDamien Le Moal struct zonefs_inode_info *zi = ZONEFS_I(inode); 10408dcc1a9dSDamien Le Moal struct super_block *sb = inode->i_sb; 10418dcc1a9dSDamien Le Moal loff_t isize; 10428dcc1a9dSDamien Le Moal ssize_t ret; 10438dcc1a9dSDamien Le Moal 10448dcc1a9dSDamien Le Moal /* Offline zones cannot be read */ 10458dcc1a9dSDamien Le Moal if (unlikely(IS_IMMUTABLE(inode) && !(inode->i_mode & 0777))) 10468dcc1a9dSDamien Le Moal return -EPERM; 10478dcc1a9dSDamien Le Moal 10488dcc1a9dSDamien Le Moal if (iocb->ki_pos >= zi->i_max_size) 10498dcc1a9dSDamien Le Moal return 0; 10508dcc1a9dSDamien Le Moal 10518dcc1a9dSDamien Le Moal if (iocb->ki_flags & IOCB_NOWAIT) { 10528dcc1a9dSDamien Le Moal if (!inode_trylock_shared(inode)) 10538dcc1a9dSDamien Le Moal return -EAGAIN; 10548dcc1a9dSDamien Le Moal } else { 10558dcc1a9dSDamien Le Moal inode_lock_shared(inode); 10568dcc1a9dSDamien Le Moal } 10578dcc1a9dSDamien Le Moal 10588dcc1a9dSDamien Le Moal /* Limit read operations to written data */ 10598dcc1a9dSDamien Le Moal mutex_lock(&zi->i_truncate_mutex); 10608dcc1a9dSDamien Le Moal isize = i_size_read(inode); 10618dcc1a9dSDamien Le Moal if (iocb->ki_pos >= isize) { 10628dcc1a9dSDamien Le Moal mutex_unlock(&zi->i_truncate_mutex); 10638dcc1a9dSDamien Le Moal ret = 0; 10648dcc1a9dSDamien Le Moal goto inode_unlock; 10658dcc1a9dSDamien Le Moal } 10668dcc1a9dSDamien Le Moal iov_iter_truncate(to, isize - iocb->ki_pos); 10678dcc1a9dSDamien Le Moal mutex_unlock(&zi->i_truncate_mutex); 10688dcc1a9dSDamien Le Moal 10698dcc1a9dSDamien Le Moal if (iocb->ki_flags & IOCB_DIRECT) { 10708dcc1a9dSDamien Le Moal size_t count = iov_iter_count(to); 10718dcc1a9dSDamien Le Moal 10728dcc1a9dSDamien Le Moal if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) { 10738dcc1a9dSDamien Le Moal ret = -EINVAL; 10748dcc1a9dSDamien Le Moal goto inode_unlock; 10758dcc1a9dSDamien Le Moal } 10768dcc1a9dSDamien Le Moal file_accessed(iocb->ki_filp); 1077c1c1204cSDamien Le Moal ret = iomap_dio_rw(iocb, to, &zonefs_read_iomap_ops, 1078786f847fSChristoph Hellwig &zonefs_read_dio_ops, 0, NULL, 0); 10798dcc1a9dSDamien Le Moal } else { 10808dcc1a9dSDamien Le Moal ret = generic_file_read_iter(iocb, to); 10818dcc1a9dSDamien Le Moal if (ret == -EIO) 10828dcc1a9dSDamien Le Moal zonefs_io_error(inode, false); 10838dcc1a9dSDamien Le Moal } 10848dcc1a9dSDamien Le Moal 10858dcc1a9dSDamien Le Moal inode_unlock: 10868dcc1a9dSDamien Le Moal inode_unlock_shared(inode); 10878dcc1a9dSDamien Le Moal 10888dcc1a9dSDamien Le Moal return ret; 10898dcc1a9dSDamien Le Moal } 10908dcc1a9dSDamien Le Moal 10917d6dfbe0SDamien Le Moal /* 10927d6dfbe0SDamien Le Moal * Write open accounting is done only for sequential files. 10937d6dfbe0SDamien Le Moal */ 10947d6dfbe0SDamien Le Moal static inline bool zonefs_seq_file_need_wro(struct inode *inode, 10957d6dfbe0SDamien Le Moal struct file *file) 1096b5c00e97SJohannes Thumshirn { 1097b5c00e97SJohannes Thumshirn struct zonefs_inode_info *zi = ZONEFS_I(inode); 1098b5c00e97SJohannes Thumshirn 1099b5c00e97SJohannes Thumshirn if (zi->i_ztype != ZONEFS_ZTYPE_SEQ) 1100b5c00e97SJohannes Thumshirn return false; 1101b5c00e97SJohannes Thumshirn 1102b5c00e97SJohannes Thumshirn if (!(file->f_mode & FMODE_WRITE)) 1103b5c00e97SJohannes Thumshirn return false; 1104b5c00e97SJohannes Thumshirn 1105b5c00e97SJohannes Thumshirn return true; 1106b5c00e97SJohannes Thumshirn } 1107b5c00e97SJohannes Thumshirn 11087d6dfbe0SDamien Le Moal static int zonefs_seq_file_write_open(struct inode *inode) 1109b5c00e97SJohannes Thumshirn { 1110b5c00e97SJohannes Thumshirn struct zonefs_inode_info *zi = ZONEFS_I(inode); 1111b5c00e97SJohannes Thumshirn int ret = 0; 1112b5c00e97SJohannes Thumshirn 1113b5c00e97SJohannes Thumshirn mutex_lock(&zi->i_truncate_mutex); 1114b5c00e97SJohannes Thumshirn 11156980d29cSChao Yu if (!zi->i_wr_refcnt) { 11167d6dfbe0SDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb); 11172b95a23cSDamien Le Moal unsigned int wro = atomic_inc_return(&sbi->s_wro_seq_files); 11182b95a23cSDamien Le Moal 11197d6dfbe0SDamien Le Moal if (sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) { 11207d6dfbe0SDamien Le Moal 112196eca145SDamien Le Moal if (sbi->s_max_wro_seq_files 112296eca145SDamien Le Moal && wro > sbi->s_max_wro_seq_files) { 11232b95a23cSDamien Le Moal atomic_dec(&sbi->s_wro_seq_files); 1124b5c00e97SJohannes Thumshirn ret = -EBUSY; 1125b5c00e97SJohannes Thumshirn goto unlock; 1126b5c00e97SJohannes Thumshirn } 1127b5c00e97SJohannes Thumshirn 1128b5c00e97SJohannes Thumshirn if (i_size_read(inode) < zi->i_max_size) { 1129b5c00e97SJohannes Thumshirn ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_OPEN); 1130b5c00e97SJohannes Thumshirn if (ret) { 11312b95a23cSDamien Le Moal atomic_dec(&sbi->s_wro_seq_files); 1132b5c00e97SJohannes Thumshirn goto unlock; 1133b5c00e97SJohannes Thumshirn } 1134b5c00e97SJohannes Thumshirn zi->i_flags |= ZONEFS_ZONE_OPEN; 113587c9ce3fSDamien Le Moal zonefs_account_active(inode); 1136b5c00e97SJohannes Thumshirn } 1137b5c00e97SJohannes Thumshirn } 1138b5c00e97SJohannes Thumshirn } 1139b5c00e97SJohannes Thumshirn 11406980d29cSChao Yu zi->i_wr_refcnt++; 11416980d29cSChao Yu 1142b5c00e97SJohannes Thumshirn unlock: 1143b5c00e97SJohannes Thumshirn mutex_unlock(&zi->i_truncate_mutex); 1144b5c00e97SJohannes Thumshirn 1145b5c00e97SJohannes Thumshirn return ret; 1146b5c00e97SJohannes Thumshirn } 1147b5c00e97SJohannes Thumshirn 1148b5c00e97SJohannes Thumshirn static int zonefs_file_open(struct inode *inode, struct file *file) 1149b5c00e97SJohannes Thumshirn { 1150b5c00e97SJohannes Thumshirn int ret; 1151b5c00e97SJohannes Thumshirn 1152b5c00e97SJohannes Thumshirn ret = generic_file_open(inode, file); 1153b5c00e97SJohannes Thumshirn if (ret) 1154b5c00e97SJohannes Thumshirn return ret; 1155b5c00e97SJohannes Thumshirn 11567d6dfbe0SDamien Le Moal if (zonefs_seq_file_need_wro(inode, file)) 11577d6dfbe0SDamien Le Moal return zonefs_seq_file_write_open(inode); 1158b5c00e97SJohannes Thumshirn 1159b5c00e97SJohannes Thumshirn return 0; 1160b5c00e97SJohannes Thumshirn } 1161b5c00e97SJohannes Thumshirn 11627d6dfbe0SDamien Le Moal static void zonefs_seq_file_write_close(struct inode *inode) 1163b5c00e97SJohannes Thumshirn { 1164b5c00e97SJohannes Thumshirn struct zonefs_inode_info *zi = ZONEFS_I(inode); 11657d6dfbe0SDamien Le Moal struct super_block *sb = inode->i_sb; 11667d6dfbe0SDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 1167b5c00e97SJohannes Thumshirn int ret = 0; 1168b5c00e97SJohannes Thumshirn 1169b5c00e97SJohannes Thumshirn mutex_lock(&zi->i_truncate_mutex); 11707d6dfbe0SDamien Le Moal 1171b5c00e97SJohannes Thumshirn zi->i_wr_refcnt--; 11727d6dfbe0SDamien Le Moal if (zi->i_wr_refcnt) 11737d6dfbe0SDamien Le Moal goto unlock; 1174b5c00e97SJohannes Thumshirn 1175b5c00e97SJohannes Thumshirn /* 11767d6dfbe0SDamien Le Moal * The file zone may not be open anymore (e.g. the file was truncated to 11777d6dfbe0SDamien Le Moal * its maximum size or it was fully written). For this case, we only 11787d6dfbe0SDamien Le Moal * need to decrement the write open count. 1179b5c00e97SJohannes Thumshirn */ 11807d6dfbe0SDamien Le Moal if (zi->i_flags & ZONEFS_ZONE_OPEN) { 1181b5c00e97SJohannes Thumshirn ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_CLOSE); 1182b5c00e97SJohannes Thumshirn if (ret) { 1183b5c00e97SJohannes Thumshirn __zonefs_io_error(inode, false); 1184b5c00e97SJohannes Thumshirn /* 1185b5c00e97SJohannes Thumshirn * Leaving zones explicitly open may lead to a state 1186b5c00e97SJohannes Thumshirn * where most zones cannot be written (zone resources 1187b5c00e97SJohannes Thumshirn * exhausted). So take preventive action by remounting 1188b5c00e97SJohannes Thumshirn * read-only. 1189b5c00e97SJohannes Thumshirn */ 1190b5c00e97SJohannes Thumshirn if (zi->i_flags & ZONEFS_ZONE_OPEN && 1191b5c00e97SJohannes Thumshirn !(sb->s_flags & SB_RDONLY)) { 11927d6dfbe0SDamien Le Moal zonefs_warn(sb, 11937d6dfbe0SDamien Le Moal "closing zone at %llu failed %d\n", 11947d6dfbe0SDamien Le Moal zi->i_zsector, ret); 11957d6dfbe0SDamien Le Moal zonefs_warn(sb, 11967d6dfbe0SDamien Le Moal "remounting filesystem read-only\n"); 1197b5c00e97SJohannes Thumshirn sb->s_flags |= SB_RDONLY; 1198b5c00e97SJohannes Thumshirn } 11997d6dfbe0SDamien Le Moal goto unlock; 1200b5c00e97SJohannes Thumshirn } 12017d6dfbe0SDamien Le Moal 1202b5c00e97SJohannes Thumshirn zi->i_flags &= ~ZONEFS_ZONE_OPEN; 120387c9ce3fSDamien Le Moal zonefs_account_active(inode); 1204b5c00e97SJohannes Thumshirn } 12057d6dfbe0SDamien Le Moal 12067d6dfbe0SDamien Le Moal atomic_dec(&sbi->s_wro_seq_files); 12077d6dfbe0SDamien Le Moal 12087d6dfbe0SDamien Le Moal unlock: 1209b5c00e97SJohannes Thumshirn mutex_unlock(&zi->i_truncate_mutex); 1210b5c00e97SJohannes Thumshirn } 1211b5c00e97SJohannes Thumshirn 1212b5c00e97SJohannes Thumshirn static int zonefs_file_release(struct inode *inode, struct file *file) 1213b5c00e97SJohannes Thumshirn { 1214b5c00e97SJohannes Thumshirn /* 1215b5c00e97SJohannes Thumshirn * If we explicitly open a zone we must close it again as well, but the 1216b5c00e97SJohannes Thumshirn * zone management operation can fail (either due to an IO error or as 1217b5c00e97SJohannes Thumshirn * the zone has gone offline or read-only). Make sure we don't fail the 1218b5c00e97SJohannes Thumshirn * close(2) for user-space. 1219b5c00e97SJohannes Thumshirn */ 12207d6dfbe0SDamien Le Moal if (zonefs_seq_file_need_wro(inode, file)) 12217d6dfbe0SDamien Le Moal zonefs_seq_file_write_close(inode); 1222b5c00e97SJohannes Thumshirn 1223b5c00e97SJohannes Thumshirn return 0; 1224b5c00e97SJohannes Thumshirn } 1225b5c00e97SJohannes Thumshirn 12268dcc1a9dSDamien Le Moal static const struct file_operations zonefs_file_operations = { 1227b5c00e97SJohannes Thumshirn .open = zonefs_file_open, 1228b5c00e97SJohannes Thumshirn .release = zonefs_file_release, 12298dcc1a9dSDamien Le Moal .fsync = zonefs_file_fsync, 12308dcc1a9dSDamien Le Moal .mmap = zonefs_file_mmap, 12318dcc1a9dSDamien Le Moal .llseek = zonefs_file_llseek, 12328dcc1a9dSDamien Le Moal .read_iter = zonefs_file_read_iter, 12338dcc1a9dSDamien Le Moal .write_iter = zonefs_file_write_iter, 12348dcc1a9dSDamien Le Moal .splice_read = generic_file_splice_read, 12358dcc1a9dSDamien Le Moal .splice_write = iter_file_splice_write, 12363e08773cSChristoph Hellwig .iopoll = iocb_bio_iopoll, 12378dcc1a9dSDamien Le Moal }; 12388dcc1a9dSDamien Le Moal 12398dcc1a9dSDamien Le Moal static struct kmem_cache *zonefs_inode_cachep; 12408dcc1a9dSDamien Le Moal 12418dcc1a9dSDamien Le Moal static struct inode *zonefs_alloc_inode(struct super_block *sb) 12428dcc1a9dSDamien Le Moal { 12438dcc1a9dSDamien Le Moal struct zonefs_inode_info *zi; 12448dcc1a9dSDamien Le Moal 1245fd60b288SMuchun Song zi = alloc_inode_sb(sb, zonefs_inode_cachep, GFP_KERNEL); 12468dcc1a9dSDamien Le Moal if (!zi) 12478dcc1a9dSDamien Le Moal return NULL; 12488dcc1a9dSDamien Le Moal 12498dcc1a9dSDamien Le Moal inode_init_once(&zi->i_vnode); 12508dcc1a9dSDamien Le Moal mutex_init(&zi->i_truncate_mutex); 1251b5c00e97SJohannes Thumshirn zi->i_wr_refcnt = 0; 1252694852eaSDamien Le Moal zi->i_flags = 0; 12538dcc1a9dSDamien Le Moal 12548dcc1a9dSDamien Le Moal return &zi->i_vnode; 12558dcc1a9dSDamien Le Moal } 12568dcc1a9dSDamien Le Moal 12578dcc1a9dSDamien Le Moal static void zonefs_free_inode(struct inode *inode) 12588dcc1a9dSDamien Le Moal { 12598dcc1a9dSDamien Le Moal kmem_cache_free(zonefs_inode_cachep, ZONEFS_I(inode)); 12608dcc1a9dSDamien Le Moal } 12618dcc1a9dSDamien Le Moal 12628dcc1a9dSDamien Le Moal /* 12638dcc1a9dSDamien Le Moal * File system stat. 12648dcc1a9dSDamien Le Moal */ 12658dcc1a9dSDamien Le Moal static int zonefs_statfs(struct dentry *dentry, struct kstatfs *buf) 12668dcc1a9dSDamien Le Moal { 12678dcc1a9dSDamien Le Moal struct super_block *sb = dentry->d_sb; 12688dcc1a9dSDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 12698dcc1a9dSDamien Le Moal enum zonefs_ztype t; 12708dcc1a9dSDamien Le Moal 12718dcc1a9dSDamien Le Moal buf->f_type = ZONEFS_MAGIC; 12728dcc1a9dSDamien Le Moal buf->f_bsize = sb->s_blocksize; 12738dcc1a9dSDamien Le Moal buf->f_namelen = ZONEFS_NAME_MAX; 12748dcc1a9dSDamien Le Moal 12758dcc1a9dSDamien Le Moal spin_lock(&sbi->s_lock); 12768dcc1a9dSDamien Le Moal 12778dcc1a9dSDamien Le Moal buf->f_blocks = sbi->s_blocks; 12788dcc1a9dSDamien Le Moal if (WARN_ON(sbi->s_used_blocks > sbi->s_blocks)) 12798dcc1a9dSDamien Le Moal buf->f_bfree = 0; 12808dcc1a9dSDamien Le Moal else 12818dcc1a9dSDamien Le Moal buf->f_bfree = buf->f_blocks - sbi->s_used_blocks; 12828dcc1a9dSDamien Le Moal buf->f_bavail = buf->f_bfree; 12838dcc1a9dSDamien Le Moal 12848dcc1a9dSDamien Le Moal for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) { 12858dcc1a9dSDamien Le Moal if (sbi->s_nr_files[t]) 12868dcc1a9dSDamien Le Moal buf->f_files += sbi->s_nr_files[t] + 1; 12878dcc1a9dSDamien Le Moal } 12888dcc1a9dSDamien Le Moal buf->f_ffree = 0; 12898dcc1a9dSDamien Le Moal 12908dcc1a9dSDamien Le Moal spin_unlock(&sbi->s_lock); 12918dcc1a9dSDamien Le Moal 12929591c3a3SAmir Goldstein buf->f_fsid = uuid_to_fsid(sbi->s_uuid.b); 12938dcc1a9dSDamien Le Moal 12948dcc1a9dSDamien Le Moal return 0; 12958dcc1a9dSDamien Le Moal } 12968dcc1a9dSDamien Le Moal 12978dcc1a9dSDamien Le Moal enum { 12988dcc1a9dSDamien Le Moal Opt_errors_ro, Opt_errors_zro, Opt_errors_zol, Opt_errors_repair, 1299b5c00e97SJohannes Thumshirn Opt_explicit_open, Opt_err, 13008dcc1a9dSDamien Le Moal }; 13018dcc1a9dSDamien Le Moal 13028dcc1a9dSDamien Le Moal static const match_table_t tokens = { 13038dcc1a9dSDamien Le Moal { Opt_errors_ro, "errors=remount-ro"}, 13048dcc1a9dSDamien Le Moal { Opt_errors_zro, "errors=zone-ro"}, 13058dcc1a9dSDamien Le Moal { Opt_errors_zol, "errors=zone-offline"}, 13068dcc1a9dSDamien Le Moal { Opt_errors_repair, "errors=repair"}, 1307b5c00e97SJohannes Thumshirn { Opt_explicit_open, "explicit-open" }, 13088dcc1a9dSDamien Le Moal { Opt_err, NULL} 13098dcc1a9dSDamien Le Moal }; 13108dcc1a9dSDamien Le Moal 13118dcc1a9dSDamien Le Moal static int zonefs_parse_options(struct super_block *sb, char *options) 13128dcc1a9dSDamien Le Moal { 13138dcc1a9dSDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 13148dcc1a9dSDamien Le Moal substring_t args[MAX_OPT_ARGS]; 13158dcc1a9dSDamien Le Moal char *p; 13168dcc1a9dSDamien Le Moal 13178dcc1a9dSDamien Le Moal if (!options) 13188dcc1a9dSDamien Le Moal return 0; 13198dcc1a9dSDamien Le Moal 13208dcc1a9dSDamien Le Moal while ((p = strsep(&options, ",")) != NULL) { 13218dcc1a9dSDamien Le Moal int token; 13228dcc1a9dSDamien Le Moal 13238dcc1a9dSDamien Le Moal if (!*p) 13248dcc1a9dSDamien Le Moal continue; 13258dcc1a9dSDamien Le Moal 13268dcc1a9dSDamien Le Moal token = match_token(p, tokens, args); 13278dcc1a9dSDamien Le Moal switch (token) { 13288dcc1a9dSDamien Le Moal case Opt_errors_ro: 13298dcc1a9dSDamien Le Moal sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK; 13308dcc1a9dSDamien Le Moal sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_RO; 13318dcc1a9dSDamien Le Moal break; 13328dcc1a9dSDamien Le Moal case Opt_errors_zro: 13338dcc1a9dSDamien Le Moal sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK; 13348dcc1a9dSDamien Le Moal sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZRO; 13358dcc1a9dSDamien Le Moal break; 13368dcc1a9dSDamien Le Moal case Opt_errors_zol: 13378dcc1a9dSDamien Le Moal sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK; 13388dcc1a9dSDamien Le Moal sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_ZOL; 13398dcc1a9dSDamien Le Moal break; 13408dcc1a9dSDamien Le Moal case Opt_errors_repair: 13418dcc1a9dSDamien Le Moal sbi->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK; 13428dcc1a9dSDamien Le Moal sbi->s_mount_opts |= ZONEFS_MNTOPT_ERRORS_REPAIR; 13438dcc1a9dSDamien Le Moal break; 1344b5c00e97SJohannes Thumshirn case Opt_explicit_open: 1345b5c00e97SJohannes Thumshirn sbi->s_mount_opts |= ZONEFS_MNTOPT_EXPLICIT_OPEN; 1346b5c00e97SJohannes Thumshirn break; 13478dcc1a9dSDamien Le Moal default: 13488dcc1a9dSDamien Le Moal return -EINVAL; 13498dcc1a9dSDamien Le Moal } 13508dcc1a9dSDamien Le Moal } 13518dcc1a9dSDamien Le Moal 13528dcc1a9dSDamien Le Moal return 0; 13538dcc1a9dSDamien Le Moal } 13548dcc1a9dSDamien Le Moal 13558dcc1a9dSDamien Le Moal static int zonefs_show_options(struct seq_file *seq, struct dentry *root) 13568dcc1a9dSDamien Le Moal { 13578dcc1a9dSDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(root->d_sb); 13588dcc1a9dSDamien Le Moal 13598dcc1a9dSDamien Le Moal if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO) 13608dcc1a9dSDamien Le Moal seq_puts(seq, ",errors=remount-ro"); 13618dcc1a9dSDamien Le Moal if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO) 13628dcc1a9dSDamien Le Moal seq_puts(seq, ",errors=zone-ro"); 13638dcc1a9dSDamien Le Moal if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL) 13648dcc1a9dSDamien Le Moal seq_puts(seq, ",errors=zone-offline"); 13658dcc1a9dSDamien Le Moal if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_REPAIR) 13668dcc1a9dSDamien Le Moal seq_puts(seq, ",errors=repair"); 13678dcc1a9dSDamien Le Moal 13688dcc1a9dSDamien Le Moal return 0; 13698dcc1a9dSDamien Le Moal } 13708dcc1a9dSDamien Le Moal 13718dcc1a9dSDamien Le Moal static int zonefs_remount(struct super_block *sb, int *flags, char *data) 13728dcc1a9dSDamien Le Moal { 13738dcc1a9dSDamien Le Moal sync_filesystem(sb); 13748dcc1a9dSDamien Le Moal 13758dcc1a9dSDamien Le Moal return zonefs_parse_options(sb, data); 13768dcc1a9dSDamien Le Moal } 13778dcc1a9dSDamien Le Moal 13788dcc1a9dSDamien Le Moal static const struct super_operations zonefs_sops = { 13798dcc1a9dSDamien Le Moal .alloc_inode = zonefs_alloc_inode, 13808dcc1a9dSDamien Le Moal .free_inode = zonefs_free_inode, 13818dcc1a9dSDamien Le Moal .statfs = zonefs_statfs, 13828dcc1a9dSDamien Le Moal .remount_fs = zonefs_remount, 13838dcc1a9dSDamien Le Moal .show_options = zonefs_show_options, 13848dcc1a9dSDamien Le Moal }; 13858dcc1a9dSDamien Le Moal 13868dcc1a9dSDamien Le Moal static const struct inode_operations zonefs_dir_inode_operations = { 13878dcc1a9dSDamien Le Moal .lookup = simple_lookup, 13888dcc1a9dSDamien Le Moal .setattr = zonefs_inode_setattr, 13898dcc1a9dSDamien Le Moal }; 13908dcc1a9dSDamien Le Moal 13918dcc1a9dSDamien Le Moal static void zonefs_init_dir_inode(struct inode *parent, struct inode *inode, 13928dcc1a9dSDamien Le Moal enum zonefs_ztype type) 13938dcc1a9dSDamien Le Moal { 13948dcc1a9dSDamien Le Moal struct super_block *sb = parent->i_sb; 13958dcc1a9dSDamien Le Moal 1396b623e347SChristoph Hellwig inode->i_ino = bdev_nr_zones(sb->s_bdev) + type + 1; 139721cb47beSChristian Brauner inode_init_owner(&init_user_ns, inode, parent, S_IFDIR | 0555); 13988dcc1a9dSDamien Le Moal inode->i_op = &zonefs_dir_inode_operations; 13998dcc1a9dSDamien Le Moal inode->i_fop = &simple_dir_operations; 14008dcc1a9dSDamien Le Moal set_nlink(inode, 2); 14018dcc1a9dSDamien Le Moal inc_nlink(parent); 14028dcc1a9dSDamien Le Moal } 14038dcc1a9dSDamien Le Moal 14041da18a29SDamien Le Moal static int zonefs_init_file_inode(struct inode *inode, struct blk_zone *zone, 14058dcc1a9dSDamien Le Moal enum zonefs_ztype type) 14068dcc1a9dSDamien Le Moal { 14078dcc1a9dSDamien Le Moal struct super_block *sb = inode->i_sb; 14088dcc1a9dSDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 14098dcc1a9dSDamien Le Moal struct zonefs_inode_info *zi = ZONEFS_I(inode); 141014bdb047SDamien Le Moal int ret = 0; 14118dcc1a9dSDamien Le Moal 14128dcc1a9dSDamien Le Moal inode->i_ino = zone->start >> sbi->s_zone_sectors_shift; 14138dcc1a9dSDamien Le Moal inode->i_mode = S_IFREG | sbi->s_perm; 14148dcc1a9dSDamien Le Moal 14158dcc1a9dSDamien Le Moal zi->i_ztype = type; 14168dcc1a9dSDamien Le Moal zi->i_zsector = zone->start; 1417e3c3155bSJohannes Thumshirn zi->i_zone_size = zone->len << SECTOR_SHIFT; 1418*7dd12d65SDamien Le Moal if (zi->i_zone_size > bdev_zone_sectors(sb->s_bdev) << SECTOR_SHIFT && 1419*7dd12d65SDamien Le Moal !(sbi->s_features & ZONEFS_F_AGGRCNV)) { 1420*7dd12d65SDamien Le Moal zonefs_err(sb, 1421*7dd12d65SDamien Le Moal "zone size %llu doesn't match device's zone sectors %llu\n", 1422*7dd12d65SDamien Le Moal zi->i_zone_size, 1423*7dd12d65SDamien Le Moal bdev_zone_sectors(sb->s_bdev) << SECTOR_SHIFT); 1424*7dd12d65SDamien Le Moal return -EINVAL; 1425*7dd12d65SDamien Le Moal } 1426e3c3155bSJohannes Thumshirn 14278dcc1a9dSDamien Le Moal zi->i_max_size = min_t(loff_t, MAX_LFS_FILESIZE, 1428e3c3155bSJohannes Thumshirn zone->capacity << SECTOR_SHIFT); 1429ccf4ad7dSDamien Le Moal zi->i_wpoffset = zonefs_check_zone_condition(inode, zone, true, true); 14308dcc1a9dSDamien Le Moal 14318dcc1a9dSDamien Le Moal inode->i_uid = sbi->s_uid; 14328dcc1a9dSDamien Le Moal inode->i_gid = sbi->s_gid; 14338dcc1a9dSDamien Le Moal inode->i_size = zi->i_wpoffset; 1434e3c3155bSJohannes Thumshirn inode->i_blocks = zi->i_max_size >> SECTOR_SHIFT; 14358dcc1a9dSDamien Le Moal 14368dcc1a9dSDamien Le Moal inode->i_op = &zonefs_file_inode_operations; 14378dcc1a9dSDamien Le Moal inode->i_fop = &zonefs_file_operations; 14388dcc1a9dSDamien Le Moal inode->i_mapping->a_ops = &zonefs_file_aops; 14398dcc1a9dSDamien Le Moal 14408dcc1a9dSDamien Le Moal sb->s_maxbytes = max(zi->i_max_size, sb->s_maxbytes); 14418dcc1a9dSDamien Le Moal sbi->s_blocks += zi->i_max_size >> sb->s_blocksize_bits; 14428dcc1a9dSDamien Le Moal sbi->s_used_blocks += zi->i_wpoffset >> sb->s_blocksize_bits; 14431da18a29SDamien Le Moal 144487c9ce3fSDamien Le Moal mutex_lock(&zi->i_truncate_mutex); 144587c9ce3fSDamien Le Moal 14461da18a29SDamien Le Moal /* 14471da18a29SDamien Le Moal * For sequential zones, make sure that any open zone is closed first 14481da18a29SDamien Le Moal * to ensure that the initial number of open zones is 0, in sync with 14491da18a29SDamien Le Moal * the open zone accounting done when the mount option 14501da18a29SDamien Le Moal * ZONEFS_MNTOPT_EXPLICIT_OPEN is used. 14511da18a29SDamien Le Moal */ 14521da18a29SDamien Le Moal if (type == ZONEFS_ZTYPE_SEQ && 14531da18a29SDamien Le Moal (zone->cond == BLK_ZONE_COND_IMP_OPEN || 14541da18a29SDamien Le Moal zone->cond == BLK_ZONE_COND_EXP_OPEN)) { 14551da18a29SDamien Le Moal ret = zonefs_zone_mgmt(inode, REQ_OP_ZONE_CLOSE); 145687c9ce3fSDamien Le Moal if (ret) 145787c9ce3fSDamien Le Moal goto unlock; 14581da18a29SDamien Le Moal } 14591da18a29SDamien Le Moal 146087c9ce3fSDamien Le Moal zonefs_account_active(inode); 146187c9ce3fSDamien Le Moal 146287c9ce3fSDamien Le Moal unlock: 146387c9ce3fSDamien Le Moal mutex_unlock(&zi->i_truncate_mutex); 146487c9ce3fSDamien Le Moal 146514bdb047SDamien Le Moal return ret; 14668dcc1a9dSDamien Le Moal } 14678dcc1a9dSDamien Le Moal 14688dcc1a9dSDamien Le Moal static struct dentry *zonefs_create_inode(struct dentry *parent, 14698dcc1a9dSDamien Le Moal const char *name, struct blk_zone *zone, 14708dcc1a9dSDamien Le Moal enum zonefs_ztype type) 14718dcc1a9dSDamien Le Moal { 14728dcc1a9dSDamien Le Moal struct inode *dir = d_inode(parent); 14738dcc1a9dSDamien Le Moal struct dentry *dentry; 14748dcc1a9dSDamien Le Moal struct inode *inode; 1475*7dd12d65SDamien Le Moal int ret = -ENOMEM; 14768dcc1a9dSDamien Le Moal 14778dcc1a9dSDamien Le Moal dentry = d_alloc_name(parent, name); 14788dcc1a9dSDamien Le Moal if (!dentry) 1479*7dd12d65SDamien Le Moal return ERR_PTR(ret); 14808dcc1a9dSDamien Le Moal 14818dcc1a9dSDamien Le Moal inode = new_inode(parent->d_sb); 14828dcc1a9dSDamien Le Moal if (!inode) 14838dcc1a9dSDamien Le Moal goto dput; 14848dcc1a9dSDamien Le Moal 14858dcc1a9dSDamien Le Moal inode->i_ctime = inode->i_mtime = inode->i_atime = dir->i_ctime; 14861da18a29SDamien Le Moal if (zone) { 14871da18a29SDamien Le Moal ret = zonefs_init_file_inode(inode, zone, type); 14881da18a29SDamien Le Moal if (ret) { 14891da18a29SDamien Le Moal iput(inode); 14901da18a29SDamien Le Moal goto dput; 14911da18a29SDamien Le Moal } 14921da18a29SDamien Le Moal } else { 14938dcc1a9dSDamien Le Moal zonefs_init_dir_inode(dir, inode, type); 14941da18a29SDamien Le Moal } 14951da18a29SDamien Le Moal 14968dcc1a9dSDamien Le Moal d_add(dentry, inode); 14978dcc1a9dSDamien Le Moal dir->i_size++; 14988dcc1a9dSDamien Le Moal 14998dcc1a9dSDamien Le Moal return dentry; 15008dcc1a9dSDamien Le Moal 15018dcc1a9dSDamien Le Moal dput: 15028dcc1a9dSDamien Le Moal dput(dentry); 15038dcc1a9dSDamien Le Moal 1504*7dd12d65SDamien Le Moal return ERR_PTR(ret); 15058dcc1a9dSDamien Le Moal } 15068dcc1a9dSDamien Le Moal 15078dcc1a9dSDamien Le Moal struct zonefs_zone_data { 15088dcc1a9dSDamien Le Moal struct super_block *sb; 15098dcc1a9dSDamien Le Moal unsigned int nr_zones[ZONEFS_ZTYPE_MAX]; 15108dcc1a9dSDamien Le Moal struct blk_zone *zones; 15118dcc1a9dSDamien Le Moal }; 15128dcc1a9dSDamien Le Moal 15138dcc1a9dSDamien Le Moal /* 15148dcc1a9dSDamien Le Moal * Create a zone group and populate it with zone files. 15158dcc1a9dSDamien Le Moal */ 15168dcc1a9dSDamien Le Moal static int zonefs_create_zgroup(struct zonefs_zone_data *zd, 15178dcc1a9dSDamien Le Moal enum zonefs_ztype type) 15188dcc1a9dSDamien Le Moal { 15198dcc1a9dSDamien Le Moal struct super_block *sb = zd->sb; 15208dcc1a9dSDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 15218dcc1a9dSDamien Le Moal struct blk_zone *zone, *next, *end; 15228dcc1a9dSDamien Le Moal const char *zgroup_name; 15238dcc1a9dSDamien Le Moal char *file_name; 1524*7dd12d65SDamien Le Moal struct dentry *dir, *dent; 15258dcc1a9dSDamien Le Moal unsigned int n = 0; 152601b2651cSDamien Le Moal int ret; 15278dcc1a9dSDamien Le Moal 15288dcc1a9dSDamien Le Moal /* If the group is empty, there is nothing to do */ 15298dcc1a9dSDamien Le Moal if (!zd->nr_zones[type]) 15308dcc1a9dSDamien Le Moal return 0; 15318dcc1a9dSDamien Le Moal 15328dcc1a9dSDamien Le Moal file_name = kmalloc(ZONEFS_NAME_MAX, GFP_KERNEL); 15338dcc1a9dSDamien Le Moal if (!file_name) 15348dcc1a9dSDamien Le Moal return -ENOMEM; 15358dcc1a9dSDamien Le Moal 15368dcc1a9dSDamien Le Moal if (type == ZONEFS_ZTYPE_CNV) 15378dcc1a9dSDamien Le Moal zgroup_name = "cnv"; 15388dcc1a9dSDamien Le Moal else 15398dcc1a9dSDamien Le Moal zgroup_name = "seq"; 15408dcc1a9dSDamien Le Moal 15418dcc1a9dSDamien Le Moal dir = zonefs_create_inode(sb->s_root, zgroup_name, NULL, type); 1542*7dd12d65SDamien Le Moal if (IS_ERR(dir)) { 1543*7dd12d65SDamien Le Moal ret = PTR_ERR(dir); 15448dcc1a9dSDamien Le Moal goto free; 154501b2651cSDamien Le Moal } 15468dcc1a9dSDamien Le Moal 15478dcc1a9dSDamien Le Moal /* 15488dcc1a9dSDamien Le Moal * The first zone contains the super block: skip it. 15498dcc1a9dSDamien Le Moal */ 1550b623e347SChristoph Hellwig end = zd->zones + bdev_nr_zones(sb->s_bdev); 15518dcc1a9dSDamien Le Moal for (zone = &zd->zones[1]; zone < end; zone = next) { 15528dcc1a9dSDamien Le Moal 15538dcc1a9dSDamien Le Moal next = zone + 1; 15548dcc1a9dSDamien Le Moal if (zonefs_zone_type(zone) != type) 15558dcc1a9dSDamien Le Moal continue; 15568dcc1a9dSDamien Le Moal 15578dcc1a9dSDamien Le Moal /* 15588dcc1a9dSDamien Le Moal * For conventional zones, contiguous zones can be aggregated 15598dcc1a9dSDamien Le Moal * together to form larger files. Note that this overwrites the 15608dcc1a9dSDamien Le Moal * length of the first zone of the set of contiguous zones 15618dcc1a9dSDamien Le Moal * aggregated together. If one offline or read-only zone is 15628dcc1a9dSDamien Le Moal * found, assume that all zones aggregated have the same 15638dcc1a9dSDamien Le Moal * condition. 15648dcc1a9dSDamien Le Moal */ 15658dcc1a9dSDamien Le Moal if (type == ZONEFS_ZTYPE_CNV && 15668dcc1a9dSDamien Le Moal (sbi->s_features & ZONEFS_F_AGGRCNV)) { 15678dcc1a9dSDamien Le Moal for (; next < end; next++) { 15688dcc1a9dSDamien Le Moal if (zonefs_zone_type(next) != type) 15698dcc1a9dSDamien Le Moal break; 15708dcc1a9dSDamien Le Moal zone->len += next->len; 1571e3c3155bSJohannes Thumshirn zone->capacity += next->capacity; 15728dcc1a9dSDamien Le Moal if (next->cond == BLK_ZONE_COND_READONLY && 15738dcc1a9dSDamien Le Moal zone->cond != BLK_ZONE_COND_OFFLINE) 15748dcc1a9dSDamien Le Moal zone->cond = BLK_ZONE_COND_READONLY; 15758dcc1a9dSDamien Le Moal else if (next->cond == BLK_ZONE_COND_OFFLINE) 15768dcc1a9dSDamien Le Moal zone->cond = BLK_ZONE_COND_OFFLINE; 15778dcc1a9dSDamien Le Moal } 1578e3c3155bSJohannes Thumshirn if (zone->capacity != zone->len) { 1579e3c3155bSJohannes Thumshirn zonefs_err(sb, "Invalid conventional zone capacity\n"); 1580e3c3155bSJohannes Thumshirn ret = -EINVAL; 1581e3c3155bSJohannes Thumshirn goto free; 1582e3c3155bSJohannes Thumshirn } 15838dcc1a9dSDamien Le Moal } 15848dcc1a9dSDamien Le Moal 15858dcc1a9dSDamien Le Moal /* 15868dcc1a9dSDamien Le Moal * Use the file number within its group as file name. 15878dcc1a9dSDamien Le Moal */ 15888dcc1a9dSDamien Le Moal snprintf(file_name, ZONEFS_NAME_MAX - 1, "%u", n); 1589*7dd12d65SDamien Le Moal dent = zonefs_create_inode(dir, file_name, zone, type); 1590*7dd12d65SDamien Le Moal if (IS_ERR(dent)) { 1591*7dd12d65SDamien Le Moal ret = PTR_ERR(dent); 15928dcc1a9dSDamien Le Moal goto free; 159301b2651cSDamien Le Moal } 15948dcc1a9dSDamien Le Moal 15958dcc1a9dSDamien Le Moal n++; 15968dcc1a9dSDamien Le Moal } 15978dcc1a9dSDamien Le Moal 15988dcc1a9dSDamien Le Moal zonefs_info(sb, "Zone group \"%s\" has %u file%s\n", 15998dcc1a9dSDamien Le Moal zgroup_name, n, n > 1 ? "s" : ""); 16008dcc1a9dSDamien Le Moal 16018dcc1a9dSDamien Le Moal sbi->s_nr_files[type] = n; 16028dcc1a9dSDamien Le Moal ret = 0; 16038dcc1a9dSDamien Le Moal 16048dcc1a9dSDamien Le Moal free: 16058dcc1a9dSDamien Le Moal kfree(file_name); 16068dcc1a9dSDamien Le Moal 16078dcc1a9dSDamien Le Moal return ret; 16088dcc1a9dSDamien Le Moal } 16098dcc1a9dSDamien Le Moal 16108dcc1a9dSDamien Le Moal static int zonefs_get_zone_info_cb(struct blk_zone *zone, unsigned int idx, 16118dcc1a9dSDamien Le Moal void *data) 16128dcc1a9dSDamien Le Moal { 16138dcc1a9dSDamien Le Moal struct zonefs_zone_data *zd = data; 16148dcc1a9dSDamien Le Moal 16158dcc1a9dSDamien Le Moal /* 16168dcc1a9dSDamien Le Moal * Count the number of usable zones: the first zone at index 0 contains 16178dcc1a9dSDamien Le Moal * the super block and is ignored. 16188dcc1a9dSDamien Le Moal */ 16198dcc1a9dSDamien Le Moal switch (zone->type) { 16208dcc1a9dSDamien Le Moal case BLK_ZONE_TYPE_CONVENTIONAL: 16218dcc1a9dSDamien Le Moal zone->wp = zone->start + zone->len; 16228dcc1a9dSDamien Le Moal if (idx) 16238dcc1a9dSDamien Le Moal zd->nr_zones[ZONEFS_ZTYPE_CNV]++; 16248dcc1a9dSDamien Le Moal break; 16258dcc1a9dSDamien Le Moal case BLK_ZONE_TYPE_SEQWRITE_REQ: 16268dcc1a9dSDamien Le Moal case BLK_ZONE_TYPE_SEQWRITE_PREF: 16278dcc1a9dSDamien Le Moal if (idx) 16288dcc1a9dSDamien Le Moal zd->nr_zones[ZONEFS_ZTYPE_SEQ]++; 16298dcc1a9dSDamien Le Moal break; 16308dcc1a9dSDamien Le Moal default: 16318dcc1a9dSDamien Le Moal zonefs_err(zd->sb, "Unsupported zone type 0x%x\n", 16328dcc1a9dSDamien Le Moal zone->type); 16338dcc1a9dSDamien Le Moal return -EIO; 16348dcc1a9dSDamien Le Moal } 16358dcc1a9dSDamien Le Moal 16368dcc1a9dSDamien Le Moal memcpy(&zd->zones[idx], zone, sizeof(struct blk_zone)); 16378dcc1a9dSDamien Le Moal 16388dcc1a9dSDamien Le Moal return 0; 16398dcc1a9dSDamien Le Moal } 16408dcc1a9dSDamien Le Moal 16418dcc1a9dSDamien Le Moal static int zonefs_get_zone_info(struct zonefs_zone_data *zd) 16428dcc1a9dSDamien Le Moal { 16438dcc1a9dSDamien Le Moal struct block_device *bdev = zd->sb->s_bdev; 16448dcc1a9dSDamien Le Moal int ret; 16458dcc1a9dSDamien Le Moal 1646b623e347SChristoph Hellwig zd->zones = kvcalloc(bdev_nr_zones(bdev), sizeof(struct blk_zone), 1647b623e347SChristoph Hellwig GFP_KERNEL); 16488dcc1a9dSDamien Le Moal if (!zd->zones) 16498dcc1a9dSDamien Le Moal return -ENOMEM; 16508dcc1a9dSDamien Le Moal 16518dcc1a9dSDamien Le Moal /* Get zones information from the device */ 16528dcc1a9dSDamien Le Moal ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES, 16538dcc1a9dSDamien Le Moal zonefs_get_zone_info_cb, zd); 16548dcc1a9dSDamien Le Moal if (ret < 0) { 16558dcc1a9dSDamien Le Moal zonefs_err(zd->sb, "Zone report failed %d\n", ret); 16568dcc1a9dSDamien Le Moal return ret; 16578dcc1a9dSDamien Le Moal } 16588dcc1a9dSDamien Le Moal 1659b623e347SChristoph Hellwig if (ret != bdev_nr_zones(bdev)) { 16608dcc1a9dSDamien Le Moal zonefs_err(zd->sb, "Invalid zone report (%d/%u zones)\n", 1661b623e347SChristoph Hellwig ret, bdev_nr_zones(bdev)); 16628dcc1a9dSDamien Le Moal return -EIO; 16638dcc1a9dSDamien Le Moal } 16648dcc1a9dSDamien Le Moal 16658dcc1a9dSDamien Le Moal return 0; 16668dcc1a9dSDamien Le Moal } 16678dcc1a9dSDamien Le Moal 16688dcc1a9dSDamien Le Moal static inline void zonefs_cleanup_zone_info(struct zonefs_zone_data *zd) 16698dcc1a9dSDamien Le Moal { 16708dcc1a9dSDamien Le Moal kvfree(zd->zones); 16718dcc1a9dSDamien Le Moal } 16728dcc1a9dSDamien Le Moal 16738dcc1a9dSDamien Le Moal /* 16748dcc1a9dSDamien Le Moal * Read super block information from the device. 16758dcc1a9dSDamien Le Moal */ 16768dcc1a9dSDamien Le Moal static int zonefs_read_super(struct super_block *sb) 16778dcc1a9dSDamien Le Moal { 16788dcc1a9dSDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 16798dcc1a9dSDamien Le Moal struct zonefs_super *super; 16808dcc1a9dSDamien Le Moal u32 crc, stored_crc; 16818dcc1a9dSDamien Le Moal struct page *page; 16828dcc1a9dSDamien Le Moal struct bio_vec bio_vec; 16838dcc1a9dSDamien Le Moal struct bio bio; 16848dcc1a9dSDamien Le Moal int ret; 16858dcc1a9dSDamien Le Moal 16868dcc1a9dSDamien Le Moal page = alloc_page(GFP_KERNEL); 16878dcc1a9dSDamien Le Moal if (!page) 16888dcc1a9dSDamien Le Moal return -ENOMEM; 16898dcc1a9dSDamien Le Moal 169049add496SChristoph Hellwig bio_init(&bio, sb->s_bdev, &bio_vec, 1, REQ_OP_READ); 16918dcc1a9dSDamien Le Moal bio.bi_iter.bi_sector = 0; 16928dcc1a9dSDamien Le Moal bio_add_page(&bio, page, PAGE_SIZE, 0); 16938dcc1a9dSDamien Le Moal 16948dcc1a9dSDamien Le Moal ret = submit_bio_wait(&bio); 16958dcc1a9dSDamien Le Moal if (ret) 16968dcc1a9dSDamien Le Moal goto free_page; 16978dcc1a9dSDamien Le Moal 16986bac30bbSFabio M. De Francesco super = page_address(page); 16998dcc1a9dSDamien Le Moal 17008dcc1a9dSDamien Le Moal ret = -EINVAL; 17018dcc1a9dSDamien Le Moal if (le32_to_cpu(super->s_magic) != ZONEFS_MAGIC) 17026bac30bbSFabio M. De Francesco goto free_page; 17038dcc1a9dSDamien Le Moal 17048dcc1a9dSDamien Le Moal stored_crc = le32_to_cpu(super->s_crc); 17058dcc1a9dSDamien Le Moal super->s_crc = 0; 17068dcc1a9dSDamien Le Moal crc = crc32(~0U, (unsigned char *)super, sizeof(struct zonefs_super)); 17078dcc1a9dSDamien Le Moal if (crc != stored_crc) { 17088dcc1a9dSDamien Le Moal zonefs_err(sb, "Invalid checksum (Expected 0x%08x, got 0x%08x)", 17098dcc1a9dSDamien Le Moal crc, stored_crc); 17106bac30bbSFabio M. De Francesco goto free_page; 17118dcc1a9dSDamien Le Moal } 17128dcc1a9dSDamien Le Moal 17138dcc1a9dSDamien Le Moal sbi->s_features = le64_to_cpu(super->s_features); 17148dcc1a9dSDamien Le Moal if (sbi->s_features & ~ZONEFS_F_DEFINED_FEATURES) { 17158dcc1a9dSDamien Le Moal zonefs_err(sb, "Unknown features set 0x%llx\n", 17168dcc1a9dSDamien Le Moal sbi->s_features); 17176bac30bbSFabio M. De Francesco goto free_page; 17188dcc1a9dSDamien Le Moal } 17198dcc1a9dSDamien Le Moal 17208dcc1a9dSDamien Le Moal if (sbi->s_features & ZONEFS_F_UID) { 17218dcc1a9dSDamien Le Moal sbi->s_uid = make_kuid(current_user_ns(), 17228dcc1a9dSDamien Le Moal le32_to_cpu(super->s_uid)); 17238dcc1a9dSDamien Le Moal if (!uid_valid(sbi->s_uid)) { 17248dcc1a9dSDamien Le Moal zonefs_err(sb, "Invalid UID feature\n"); 17256bac30bbSFabio M. De Francesco goto free_page; 17268dcc1a9dSDamien Le Moal } 17278dcc1a9dSDamien Le Moal } 17288dcc1a9dSDamien Le Moal 17298dcc1a9dSDamien Le Moal if (sbi->s_features & ZONEFS_F_GID) { 17308dcc1a9dSDamien Le Moal sbi->s_gid = make_kgid(current_user_ns(), 17318dcc1a9dSDamien Le Moal le32_to_cpu(super->s_gid)); 17328dcc1a9dSDamien Le Moal if (!gid_valid(sbi->s_gid)) { 17338dcc1a9dSDamien Le Moal zonefs_err(sb, "Invalid GID feature\n"); 17346bac30bbSFabio M. De Francesco goto free_page; 17358dcc1a9dSDamien Le Moal } 17368dcc1a9dSDamien Le Moal } 17378dcc1a9dSDamien Le Moal 17388dcc1a9dSDamien Le Moal if (sbi->s_features & ZONEFS_F_PERM) 17398dcc1a9dSDamien Le Moal sbi->s_perm = le32_to_cpu(super->s_perm); 17408dcc1a9dSDamien Le Moal 17418dcc1a9dSDamien Le Moal if (memchr_inv(super->s_reserved, 0, sizeof(super->s_reserved))) { 17428dcc1a9dSDamien Le Moal zonefs_err(sb, "Reserved area is being used\n"); 17436bac30bbSFabio M. De Francesco goto free_page; 17448dcc1a9dSDamien Le Moal } 17458dcc1a9dSDamien Le Moal 1746568776f9SAndy Shevchenko import_uuid(&sbi->s_uuid, super->s_uuid); 17478dcc1a9dSDamien Le Moal ret = 0; 17488dcc1a9dSDamien Le Moal 17498dcc1a9dSDamien Le Moal free_page: 17508dcc1a9dSDamien Le Moal __free_page(page); 17518dcc1a9dSDamien Le Moal 17528dcc1a9dSDamien Le Moal return ret; 17538dcc1a9dSDamien Le Moal } 17548dcc1a9dSDamien Le Moal 17558dcc1a9dSDamien Le Moal /* 17568dcc1a9dSDamien Le Moal * Check that the device is zoned. If it is, get the list of zones and create 17578dcc1a9dSDamien Le Moal * sub-directories and files according to the device zone configuration and 17588dcc1a9dSDamien Le Moal * format options. 17598dcc1a9dSDamien Le Moal */ 17608dcc1a9dSDamien Le Moal static int zonefs_fill_super(struct super_block *sb, void *data, int silent) 17618dcc1a9dSDamien Le Moal { 17628dcc1a9dSDamien Le Moal struct zonefs_zone_data zd; 17638dcc1a9dSDamien Le Moal struct zonefs_sb_info *sbi; 17648dcc1a9dSDamien Le Moal struct inode *inode; 17658dcc1a9dSDamien Le Moal enum zonefs_ztype t; 17668dcc1a9dSDamien Le Moal int ret; 17678dcc1a9dSDamien Le Moal 17688dcc1a9dSDamien Le Moal if (!bdev_is_zoned(sb->s_bdev)) { 17698dcc1a9dSDamien Le Moal zonefs_err(sb, "Not a zoned block device\n"); 17708dcc1a9dSDamien Le Moal return -EINVAL; 17718dcc1a9dSDamien Le Moal } 17728dcc1a9dSDamien Le Moal 17738dcc1a9dSDamien Le Moal /* 17748dcc1a9dSDamien Le Moal * Initialize super block information: the maximum file size is updated 17758dcc1a9dSDamien Le Moal * when the zone files are created so that the format option 17768dcc1a9dSDamien Le Moal * ZONEFS_F_AGGRCNV which increases the maximum file size of a file 17778dcc1a9dSDamien Le Moal * beyond the zone size is taken into account. 17788dcc1a9dSDamien Le Moal */ 17798dcc1a9dSDamien Le Moal sbi = kzalloc(sizeof(*sbi), GFP_KERNEL); 17808dcc1a9dSDamien Le Moal if (!sbi) 17818dcc1a9dSDamien Le Moal return -ENOMEM; 17828dcc1a9dSDamien Le Moal 17838dcc1a9dSDamien Le Moal spin_lock_init(&sbi->s_lock); 17848dcc1a9dSDamien Le Moal sb->s_fs_info = sbi; 17858dcc1a9dSDamien Le Moal sb->s_magic = ZONEFS_MAGIC; 17868dcc1a9dSDamien Le Moal sb->s_maxbytes = 0; 17878dcc1a9dSDamien Le Moal sb->s_op = &zonefs_sops; 17888dcc1a9dSDamien Le Moal sb->s_time_gran = 1; 17898dcc1a9dSDamien Le Moal 17908dcc1a9dSDamien Le Moal /* 17910f1ba5f5SDamien Le Moal * The block size is set to the device zone write granularity to ensure 17920f1ba5f5SDamien Le Moal * that write operations are always aligned according to the device 17930f1ba5f5SDamien Le Moal * interface constraints. 17948dcc1a9dSDamien Le Moal */ 17950f1ba5f5SDamien Le Moal sb_set_blocksize(sb, bdev_zone_write_granularity(sb->s_bdev)); 17968dcc1a9dSDamien Le Moal sbi->s_zone_sectors_shift = ilog2(bdev_zone_sectors(sb->s_bdev)); 17978dcc1a9dSDamien Le Moal sbi->s_uid = GLOBAL_ROOT_UID; 17988dcc1a9dSDamien Le Moal sbi->s_gid = GLOBAL_ROOT_GID; 17998dcc1a9dSDamien Le Moal sbi->s_perm = 0640; 18008dcc1a9dSDamien Le Moal sbi->s_mount_opts = ZONEFS_MNTOPT_ERRORS_RO; 18012b95a23cSDamien Le Moal 18022b95a23cSDamien Le Moal atomic_set(&sbi->s_wro_seq_files, 0); 18032b95a23cSDamien Le Moal sbi->s_max_wro_seq_files = bdev_max_open_zones(sb->s_bdev); 180487c9ce3fSDamien Le Moal atomic_set(&sbi->s_active_seq_files, 0); 180587c9ce3fSDamien Le Moal sbi->s_max_active_seq_files = bdev_max_active_zones(sb->s_bdev); 180687c9ce3fSDamien Le Moal 18078dcc1a9dSDamien Le Moal ret = zonefs_read_super(sb); 18088dcc1a9dSDamien Le Moal if (ret) 18098dcc1a9dSDamien Le Moal return ret; 18108dcc1a9dSDamien Le Moal 18118dcc1a9dSDamien Le Moal ret = zonefs_parse_options(sb, data); 18128dcc1a9dSDamien Le Moal if (ret) 18138dcc1a9dSDamien Le Moal return ret; 18148dcc1a9dSDamien Le Moal 18158dcc1a9dSDamien Le Moal memset(&zd, 0, sizeof(struct zonefs_zone_data)); 18168dcc1a9dSDamien Le Moal zd.sb = sb; 18178dcc1a9dSDamien Le Moal ret = zonefs_get_zone_info(&zd); 18188dcc1a9dSDamien Le Moal if (ret) 18198dcc1a9dSDamien Le Moal goto cleanup; 18208dcc1a9dSDamien Le Moal 18219277a6d4SDamien Le Moal ret = zonefs_sysfs_register(sb); 18229277a6d4SDamien Le Moal if (ret) 18239277a6d4SDamien Le Moal goto cleanup; 18249277a6d4SDamien Le Moal 1825b623e347SChristoph Hellwig zonefs_info(sb, "Mounting %u zones", bdev_nr_zones(sb->s_bdev)); 18268dcc1a9dSDamien Le Moal 1827a2a513beSDamien Le Moal if (!sbi->s_max_wro_seq_files && 182896eca145SDamien Le Moal !sbi->s_max_active_seq_files && 1829a2a513beSDamien Le Moal sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) { 183096eca145SDamien Le Moal zonefs_info(sb, 183196eca145SDamien Le Moal "No open and active zone limits. Ignoring explicit_open mount option\n"); 1832a2a513beSDamien Le Moal sbi->s_mount_opts &= ~ZONEFS_MNTOPT_EXPLICIT_OPEN; 1833a2a513beSDamien Le Moal } 1834a2a513beSDamien Le Moal 18358dcc1a9dSDamien Le Moal /* Create root directory inode */ 18368dcc1a9dSDamien Le Moal ret = -ENOMEM; 18378dcc1a9dSDamien Le Moal inode = new_inode(sb); 18388dcc1a9dSDamien Le Moal if (!inode) 18398dcc1a9dSDamien Le Moal goto cleanup; 18408dcc1a9dSDamien Le Moal 1841b623e347SChristoph Hellwig inode->i_ino = bdev_nr_zones(sb->s_bdev); 18428dcc1a9dSDamien Le Moal inode->i_mode = S_IFDIR | 0555; 18438dcc1a9dSDamien Le Moal inode->i_ctime = inode->i_mtime = inode->i_atime = current_time(inode); 18448dcc1a9dSDamien Le Moal inode->i_op = &zonefs_dir_inode_operations; 18458dcc1a9dSDamien Le Moal inode->i_fop = &simple_dir_operations; 18468dcc1a9dSDamien Le Moal set_nlink(inode, 2); 18478dcc1a9dSDamien Le Moal 18488dcc1a9dSDamien Le Moal sb->s_root = d_make_root(inode); 18498dcc1a9dSDamien Le Moal if (!sb->s_root) 18508dcc1a9dSDamien Le Moal goto cleanup; 18518dcc1a9dSDamien Le Moal 18528dcc1a9dSDamien Le Moal /* Create and populate files in zone groups directories */ 18538dcc1a9dSDamien Le Moal for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) { 18548dcc1a9dSDamien Le Moal ret = zonefs_create_zgroup(&zd, t); 18558dcc1a9dSDamien Le Moal if (ret) 18568dcc1a9dSDamien Le Moal break; 18578dcc1a9dSDamien Le Moal } 18588dcc1a9dSDamien Le Moal 18598dcc1a9dSDamien Le Moal cleanup: 18608dcc1a9dSDamien Le Moal zonefs_cleanup_zone_info(&zd); 18618dcc1a9dSDamien Le Moal 18628dcc1a9dSDamien Le Moal return ret; 18638dcc1a9dSDamien Le Moal } 18648dcc1a9dSDamien Le Moal 18658dcc1a9dSDamien Le Moal static struct dentry *zonefs_mount(struct file_system_type *fs_type, 18668dcc1a9dSDamien Le Moal int flags, const char *dev_name, void *data) 18678dcc1a9dSDamien Le Moal { 18688dcc1a9dSDamien Le Moal return mount_bdev(fs_type, flags, dev_name, data, zonefs_fill_super); 18698dcc1a9dSDamien Le Moal } 18708dcc1a9dSDamien Le Moal 18718dcc1a9dSDamien Le Moal static void zonefs_kill_super(struct super_block *sb) 18728dcc1a9dSDamien Le Moal { 18738dcc1a9dSDamien Le Moal struct zonefs_sb_info *sbi = ZONEFS_SB(sb); 18748dcc1a9dSDamien Le Moal 18758dcc1a9dSDamien Le Moal if (sb->s_root) 18768dcc1a9dSDamien Le Moal d_genocide(sb->s_root); 18779277a6d4SDamien Le Moal 18789277a6d4SDamien Le Moal zonefs_sysfs_unregister(sb); 18798dcc1a9dSDamien Le Moal kill_block_super(sb); 18808dcc1a9dSDamien Le Moal kfree(sbi); 18818dcc1a9dSDamien Le Moal } 18828dcc1a9dSDamien Le Moal 18838dcc1a9dSDamien Le Moal /* 18848dcc1a9dSDamien Le Moal * File system definition and registration. 18858dcc1a9dSDamien Le Moal */ 18868dcc1a9dSDamien Le Moal static struct file_system_type zonefs_type = { 18878dcc1a9dSDamien Le Moal .owner = THIS_MODULE, 18888dcc1a9dSDamien Le Moal .name = "zonefs", 18898dcc1a9dSDamien Le Moal .mount = zonefs_mount, 18908dcc1a9dSDamien Le Moal .kill_sb = zonefs_kill_super, 18918dcc1a9dSDamien Le Moal .fs_flags = FS_REQUIRES_DEV, 18928dcc1a9dSDamien Le Moal }; 18938dcc1a9dSDamien Le Moal 18948dcc1a9dSDamien Le Moal static int __init zonefs_init_inodecache(void) 18958dcc1a9dSDamien Le Moal { 18968dcc1a9dSDamien Le Moal zonefs_inode_cachep = kmem_cache_create("zonefs_inode_cache", 18978dcc1a9dSDamien Le Moal sizeof(struct zonefs_inode_info), 0, 18988dcc1a9dSDamien Le Moal (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT), 18998dcc1a9dSDamien Le Moal NULL); 19008dcc1a9dSDamien Le Moal if (zonefs_inode_cachep == NULL) 19018dcc1a9dSDamien Le Moal return -ENOMEM; 19028dcc1a9dSDamien Le Moal return 0; 19038dcc1a9dSDamien Le Moal } 19048dcc1a9dSDamien Le Moal 19058dcc1a9dSDamien Le Moal static void zonefs_destroy_inodecache(void) 19068dcc1a9dSDamien Le Moal { 19078dcc1a9dSDamien Le Moal /* 19088dcc1a9dSDamien Le Moal * Make sure all delayed rcu free inodes are flushed before we 19098dcc1a9dSDamien Le Moal * destroy the inode cache. 19108dcc1a9dSDamien Le Moal */ 19118dcc1a9dSDamien Le Moal rcu_barrier(); 19128dcc1a9dSDamien Le Moal kmem_cache_destroy(zonefs_inode_cachep); 19138dcc1a9dSDamien Le Moal } 19148dcc1a9dSDamien Le Moal 19158dcc1a9dSDamien Le Moal static int __init zonefs_init(void) 19168dcc1a9dSDamien Le Moal { 19178dcc1a9dSDamien Le Moal int ret; 19188dcc1a9dSDamien Le Moal 19198dcc1a9dSDamien Le Moal BUILD_BUG_ON(sizeof(struct zonefs_super) != ZONEFS_SUPER_SIZE); 19208dcc1a9dSDamien Le Moal 19218dcc1a9dSDamien Le Moal ret = zonefs_init_inodecache(); 19228dcc1a9dSDamien Le Moal if (ret) 19238dcc1a9dSDamien Le Moal return ret; 19248dcc1a9dSDamien Le Moal 19258dcc1a9dSDamien Le Moal ret = register_filesystem(&zonefs_type); 19269277a6d4SDamien Le Moal if (ret) 19279277a6d4SDamien Le Moal goto destroy_inodecache; 19289277a6d4SDamien Le Moal 19299277a6d4SDamien Le Moal ret = zonefs_sysfs_init(); 19309277a6d4SDamien Le Moal if (ret) 19319277a6d4SDamien Le Moal goto unregister_fs; 19328dcc1a9dSDamien Le Moal 19338dcc1a9dSDamien Le Moal return 0; 19349277a6d4SDamien Le Moal 19359277a6d4SDamien Le Moal unregister_fs: 19369277a6d4SDamien Le Moal unregister_filesystem(&zonefs_type); 19379277a6d4SDamien Le Moal destroy_inodecache: 19389277a6d4SDamien Le Moal zonefs_destroy_inodecache(); 19399277a6d4SDamien Le Moal 19409277a6d4SDamien Le Moal return ret; 19418dcc1a9dSDamien Le Moal } 19428dcc1a9dSDamien Le Moal 19438dcc1a9dSDamien Le Moal static void __exit zonefs_exit(void) 19448dcc1a9dSDamien Le Moal { 19459277a6d4SDamien Le Moal zonefs_sysfs_exit(); 19468dcc1a9dSDamien Le Moal zonefs_destroy_inodecache(); 19478dcc1a9dSDamien Le Moal unregister_filesystem(&zonefs_type); 19488dcc1a9dSDamien Le Moal } 19498dcc1a9dSDamien Le Moal 19508dcc1a9dSDamien Le Moal MODULE_AUTHOR("Damien Le Moal"); 19518dcc1a9dSDamien Le Moal MODULE_DESCRIPTION("Zone file system for zoned block devices"); 19528dcc1a9dSDamien Le Moal MODULE_LICENSE("GPL"); 19538ffea259SNaohiro Aota MODULE_ALIAS_FS("zonefs"); 19548dcc1a9dSDamien Le Moal module_init(zonefs_init); 19558dcc1a9dSDamien Le Moal module_exit(zonefs_exit); 1956