Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Simple file system for zoned block devices exposing zones as files. |
| 4 | * |
| 5 | * Copyright (C) 2019 Western Digital Corporation or its affiliates. |
| 6 | */ |
| 7 | #include <linux/module.h> |
Matthew Wilcox (Oracle) | 3a6b216 | 2021-06-28 19:36:30 -0700 | [diff] [blame] | 8 | #include <linux/pagemap.h> |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 9 | #include <linux/magic.h> |
| 10 | #include <linux/iomap.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/blkdev.h> |
| 14 | #include <linux/statfs.h> |
| 15 | #include <linux/writeback.h> |
| 16 | #include <linux/quotaops.h> |
| 17 | #include <linux/seq_file.h> |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 18 | #include <linux/uio.h> |
| 19 | #include <linux/mman.h> |
| 20 | #include <linux/sched/mm.h> |
| 21 | #include <linux/crc32.h> |
Johannes Thumshirn | 02ef12a | 2020-05-12 17:55:54 +0900 | [diff] [blame] | 22 | #include <linux/task_io_accounting_ops.h> |
Bill O'Donnell | 567e629 | 2024-02-09 13:36:59 -0600 | [diff] [blame] | 23 | #include <linux/fs_parser.h> |
| 24 | #include <linux/fs_context.h> |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 25 | |
| 26 | #include "zonefs.h" |
| 27 | |
Johannes Thumshirn | 62ab1aa | 2021-01-27 05:21:15 +0900 | [diff] [blame] | 28 | #define CREATE_TRACE_POINTS |
| 29 | #include "trace.h" |
| 30 | |
Damien Le Moal | 87c9ce3f | 2022-04-12 18:54:39 +0900 | [diff] [blame] | 31 | /* |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 32 | * Get the name of a zone group directory. |
Damien Le Moal | 87c9ce3f | 2022-04-12 18:54:39 +0900 | [diff] [blame] | 33 | */ |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 34 | static const char *zonefs_zgroup_name(enum zonefs_ztype ztype) |
Damien Le Moal | 87c9ce3f | 2022-04-12 18:54:39 +0900 | [diff] [blame] | 35 | { |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 36 | switch (ztype) { |
| 37 | case ZONEFS_ZTYPE_CNV: |
| 38 | return "cnv"; |
| 39 | case ZONEFS_ZTYPE_SEQ: |
| 40 | return "seq"; |
| 41 | default: |
| 42 | WARN_ON_ONCE(1); |
| 43 | return "???"; |
| 44 | } |
| 45 | } |
Damien Le Moal | 87c9ce3f | 2022-04-12 18:54:39 +0900 | [diff] [blame] | 46 | |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 47 | /* |
| 48 | * Manage the active zone count. |
| 49 | */ |
| 50 | static void zonefs_account_active(struct super_block *sb, |
| 51 | struct zonefs_zone *z) |
| 52 | { |
| 53 | struct zonefs_sb_info *sbi = ZONEFS_SB(sb); |
Damien Le Moal | 87c9ce3f | 2022-04-12 18:54:39 +0900 | [diff] [blame] | 54 | |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 55 | if (zonefs_zone_is_cnv(z)) |
Damien Le Moal | 87c9ce3f | 2022-04-12 18:54:39 +0900 | [diff] [blame] | 56 | return; |
| 57 | |
| 58 | /* |
Damien Le Moal | db58653c | 2022-11-21 16:29:37 +0900 | [diff] [blame] | 59 | * For zones that transitioned to the offline or readonly condition, |
| 60 | * we only need to clear the active state. |
| 61 | */ |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 62 | if (z->z_flags & (ZONEFS_ZONE_OFFLINE | ZONEFS_ZONE_READONLY)) |
Damien Le Moal | db58653c | 2022-11-21 16:29:37 +0900 | [diff] [blame] | 63 | goto out; |
| 64 | |
| 65 | /* |
Damien Le Moal | 87c9ce3f | 2022-04-12 18:54:39 +0900 | [diff] [blame] | 66 | * If the zone is active, that is, if it is explicitly open or |
| 67 | * partially written, check if it was already accounted as active. |
| 68 | */ |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 69 | if ((z->z_flags & ZONEFS_ZONE_OPEN) || |
| 70 | (z->z_wpoffset > 0 && z->z_wpoffset < z->z_capacity)) { |
| 71 | if (!(z->z_flags & ZONEFS_ZONE_ACTIVE)) { |
| 72 | z->z_flags |= ZONEFS_ZONE_ACTIVE; |
Damien Le Moal | 87c9ce3f | 2022-04-12 18:54:39 +0900 | [diff] [blame] | 73 | atomic_inc(&sbi->s_active_seq_files); |
| 74 | } |
| 75 | return; |
| 76 | } |
| 77 | |
Damien Le Moal | db58653c | 2022-11-21 16:29:37 +0900 | [diff] [blame] | 78 | out: |
Damien Le Moal | 87c9ce3f | 2022-04-12 18:54:39 +0900 | [diff] [blame] | 79 | /* The zone is not active. If it was, update the active count */ |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 80 | if (z->z_flags & ZONEFS_ZONE_ACTIVE) { |
| 81 | z->z_flags &= ~ZONEFS_ZONE_ACTIVE; |
Damien Le Moal | 87c9ce3f | 2022-04-12 18:54:39 +0900 | [diff] [blame] | 82 | atomic_dec(&sbi->s_active_seq_files); |
| 83 | } |
| 84 | } |
| 85 | |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 86 | /* |
| 87 | * Manage the active zone count. Called with zi->i_truncate_mutex held. |
| 88 | */ |
| 89 | void zonefs_inode_account_active(struct inode *inode) |
Johannes Thumshirn | 5498d5f | 2020-09-11 17:56:48 +0900 | [diff] [blame] | 90 | { |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 91 | lockdep_assert_held(&ZONEFS_I(inode)->i_truncate_mutex); |
Johannes Thumshirn | 5498d5f | 2020-09-11 17:56:48 +0900 | [diff] [blame] | 92 | |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 93 | return zonefs_account_active(inode->i_sb, zonefs_inode_zone(inode)); |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * Execute a zone management operation. |
| 98 | */ |
| 99 | static int zonefs_zone_mgmt(struct super_block *sb, |
| 100 | struct zonefs_zone *z, enum req_op op) |
| 101 | { |
| 102 | int ret; |
Johannes Thumshirn | 5498d5f | 2020-09-11 17:56:48 +0900 | [diff] [blame] | 103 | |
Damien Le Moal | 1da18a2 | 2022-04-12 17:41:37 +0900 | [diff] [blame] | 104 | /* |
| 105 | * With ZNS drives, closing an explicitly open zone that has not been |
| 106 | * written will change the zone state to "closed", that is, the zone |
| 107 | * will remain active. Since this can then cause failure of explicit |
| 108 | * open operation on other zones if the drive active zone resources |
| 109 | * are exceeded, make sure that the zone does not remain active by |
| 110 | * resetting it. |
| 111 | */ |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 112 | if (op == REQ_OP_ZONE_CLOSE && !z->z_wpoffset) |
Damien Le Moal | 1da18a2 | 2022-04-12 17:41:37 +0900 | [diff] [blame] | 113 | op = REQ_OP_ZONE_RESET; |
| 114 | |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 115 | trace_zonefs_zone_mgmt(sb, z, op); |
| 116 | ret = blkdev_zone_mgmt(sb->s_bdev, op, z->z_sector, |
Johannes Thumshirn | 71f4ecdb | 2024-01-28 23:52:20 -0800 | [diff] [blame] | 117 | z->z_size >> SECTOR_SHIFT); |
Johannes Thumshirn | 5498d5f | 2020-09-11 17:56:48 +0900 | [diff] [blame] | 118 | if (ret) { |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 119 | zonefs_err(sb, |
Johannes Thumshirn | 5498d5f | 2020-09-11 17:56:48 +0900 | [diff] [blame] | 120 | "Zone management operation %s at %llu failed %d\n", |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 121 | blk_op_str(op), z->z_sector, ret); |
Johannes Thumshirn | 5498d5f | 2020-09-11 17:56:48 +0900 | [diff] [blame] | 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 128 | int zonefs_inode_zone_mgmt(struct inode *inode, enum req_op op) |
Johannes Thumshirn | b5c00e9 | 2020-09-11 17:56:50 +0900 | [diff] [blame] | 129 | { |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 130 | lockdep_assert_held(&ZONEFS_I(inode)->i_truncate_mutex); |
| 131 | |
| 132 | return zonefs_zone_mgmt(inode->i_sb, zonefs_inode_zone(inode), op); |
| 133 | } |
| 134 | |
Damien Le Moal | 4008e2a | 2022-11-25 09:39:33 +0900 | [diff] [blame] | 135 | void zonefs_i_size_write(struct inode *inode, loff_t isize) |
Johannes Thumshirn | b5c00e9 | 2020-09-11 17:56:50 +0900 | [diff] [blame] | 136 | { |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 137 | struct zonefs_zone *z = zonefs_inode_zone(inode); |
Johannes Thumshirn | b5c00e9 | 2020-09-11 17:56:50 +0900 | [diff] [blame] | 138 | |
| 139 | i_size_write(inode, isize); |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 140 | |
Johannes Thumshirn | b5c00e9 | 2020-09-11 17:56:50 +0900 | [diff] [blame] | 141 | /* |
| 142 | * A full zone is no longer open/active and does not need |
| 143 | * explicit closing. |
| 144 | */ |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 145 | if (isize >= z->z_capacity) { |
Damien Le Moal | 87c9ce3f | 2022-04-12 18:54:39 +0900 | [diff] [blame] | 146 | struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb); |
| 147 | |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 148 | if (z->z_flags & ZONEFS_ZONE_ACTIVE) |
Damien Le Moal | 87c9ce3f | 2022-04-12 18:54:39 +0900 | [diff] [blame] | 149 | atomic_dec(&sbi->s_active_seq_files); |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 150 | z->z_flags &= ~(ZONEFS_ZONE_OPEN | ZONEFS_ZONE_ACTIVE); |
Damien Le Moal | 87c9ce3f | 2022-04-12 18:54:39 +0900 | [diff] [blame] | 151 | } |
Johannes Thumshirn | b5c00e9 | 2020-09-11 17:56:50 +0900 | [diff] [blame] | 152 | } |
| 153 | |
Damien Le Moal | 4008e2a | 2022-11-25 09:39:33 +0900 | [diff] [blame] | 154 | void zonefs_update_stats(struct inode *inode, loff_t new_isize) |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 155 | { |
| 156 | struct super_block *sb = inode->i_sb; |
| 157 | struct zonefs_sb_info *sbi = ZONEFS_SB(sb); |
| 158 | loff_t old_isize = i_size_read(inode); |
| 159 | loff_t nr_blocks; |
| 160 | |
| 161 | if (new_isize == old_isize) |
| 162 | return; |
| 163 | |
| 164 | spin_lock(&sbi->s_lock); |
| 165 | |
| 166 | /* |
| 167 | * This may be called for an update after an IO error. |
| 168 | * So beware of the values seen. |
| 169 | */ |
| 170 | if (new_isize < old_isize) { |
| 171 | nr_blocks = (old_isize - new_isize) >> sb->s_blocksize_bits; |
| 172 | if (sbi->s_used_blocks > nr_blocks) |
| 173 | sbi->s_used_blocks -= nr_blocks; |
| 174 | else |
| 175 | sbi->s_used_blocks = 0; |
| 176 | } else { |
| 177 | sbi->s_used_blocks += |
| 178 | (new_isize - old_isize) >> sb->s_blocksize_bits; |
| 179 | if (sbi->s_used_blocks > sbi->s_blocks) |
| 180 | sbi->s_used_blocks = sbi->s_blocks; |
| 181 | } |
| 182 | |
| 183 | spin_unlock(&sbi->s_lock); |
| 184 | } |
| 185 | |
| 186 | /* |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 187 | * Check a zone condition. Return the amount of written (and still readable) |
| 188 | * data in the zone. |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 189 | */ |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 190 | static loff_t zonefs_check_zone_condition(struct super_block *sb, |
| 191 | struct zonefs_zone *z, |
Damien Le Moal | 46a9c52 | 2022-11-25 11:06:20 +0900 | [diff] [blame] | 192 | struct blk_zone *zone) |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 193 | { |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 194 | switch (zone->cond) { |
| 195 | case BLK_ZONE_COND_OFFLINE: |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 196 | zonefs_warn(sb, "Zone %llu: offline zone\n", |
| 197 | z->z_sector); |
| 198 | z->z_flags |= ZONEFS_ZONE_OFFLINE; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 199 | return 0; |
| 200 | case BLK_ZONE_COND_READONLY: |
Damien Le Moal | ccf4ad7 | 2020-03-20 21:36:54 +0900 | [diff] [blame] | 201 | /* |
Damien Le Moal | 46a9c52 | 2022-11-25 11:06:20 +0900 | [diff] [blame] | 202 | * The write pointer of read-only zones is invalid, so we cannot |
| 203 | * determine the zone wpoffset (inode size). We thus keep the |
| 204 | * zone wpoffset as is, which leads to an empty file |
| 205 | * (wpoffset == 0) on mount. For a runtime error, this keeps |
| 206 | * the inode size as it was when last updated so that the user |
| 207 | * can recover data. |
Damien Le Moal | ccf4ad7 | 2020-03-20 21:36:54 +0900 | [diff] [blame] | 208 | */ |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 209 | zonefs_warn(sb, "Zone %llu: read-only zone\n", |
| 210 | z->z_sector); |
| 211 | z->z_flags |= ZONEFS_ZONE_READONLY; |
| 212 | if (zonefs_zone_is_cnv(z)) |
| 213 | return z->z_capacity; |
| 214 | return z->z_wpoffset; |
Shin'ichiro Kawasaki | 059c010 | 2021-02-17 18:58:11 +0900 | [diff] [blame] | 215 | case BLK_ZONE_COND_FULL: |
| 216 | /* The write pointer of full zones is invalid. */ |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 217 | return z->z_capacity; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 218 | default: |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 219 | if (zonefs_zone_is_cnv(z)) |
| 220 | return z->z_capacity; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 221 | return (zone->wp - zone->start) << SECTOR_SHIFT; |
| 222 | } |
| 223 | } |
| 224 | |
Damien Le Moal | 46a9c52 | 2022-11-25 11:06:20 +0900 | [diff] [blame] | 225 | /* |
| 226 | * Check a zone condition and adjust its inode access permissions for |
| 227 | * offline and readonly zones. |
| 228 | */ |
| 229 | static void zonefs_inode_update_mode(struct inode *inode) |
| 230 | { |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 231 | struct zonefs_zone *z = zonefs_inode_zone(inode); |
Damien Le Moal | 46a9c52 | 2022-11-25 11:06:20 +0900 | [diff] [blame] | 232 | |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 233 | if (z->z_flags & ZONEFS_ZONE_OFFLINE) { |
Damien Le Moal | 46a9c52 | 2022-11-25 11:06:20 +0900 | [diff] [blame] | 234 | /* Offline zones cannot be read nor written */ |
| 235 | inode->i_flags |= S_IMMUTABLE; |
| 236 | inode->i_mode &= ~0777; |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 237 | } else if (z->z_flags & ZONEFS_ZONE_READONLY) { |
Damien Le Moal | 46a9c52 | 2022-11-25 11:06:20 +0900 | [diff] [blame] | 238 | /* Readonly zones cannot be written */ |
| 239 | inode->i_flags |= S_IMMUTABLE; |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 240 | if (z->z_flags & ZONEFS_ZONE_INIT_MODE) |
Damien Le Moal | 46a9c52 | 2022-11-25 11:06:20 +0900 | [diff] [blame] | 241 | inode->i_mode &= ~0777; |
| 242 | else |
| 243 | inode->i_mode &= ~0222; |
| 244 | } |
| 245 | |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 246 | z->z_flags &= ~ZONEFS_ZONE_INIT_MODE; |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 247 | z->z_mode = inode->i_mode; |
Damien Le Moal | 46a9c52 | 2022-11-25 11:06:20 +0900 | [diff] [blame] | 248 | } |
| 249 | |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 250 | static int zonefs_io_error_cb(struct blk_zone *zone, unsigned int idx, |
| 251 | void *data) |
| 252 | { |
Damien Le Moal | 14db5f6 | 2024-02-08 17:26:59 +0900 | [diff] [blame] | 253 | struct blk_zone *z = data; |
| 254 | |
| 255 | *z = *zone; |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | static void zonefs_handle_io_error(struct inode *inode, struct blk_zone *zone, |
| 260 | bool write) |
| 261 | { |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 262 | struct zonefs_zone *z = zonefs_inode_zone(inode); |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 263 | struct super_block *sb = inode->i_sb; |
| 264 | struct zonefs_sb_info *sbi = ZONEFS_SB(sb); |
| 265 | loff_t isize, data_size; |
| 266 | |
| 267 | /* |
| 268 | * Check the zone condition: if the zone is not "bad" (offline or |
| 269 | * read-only), read errors are simply signaled to the IO issuer as long |
| 270 | * as there is no inconsistency between the inode size and the amount of |
| 271 | * data writen in the zone (data_size). |
| 272 | */ |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 273 | data_size = zonefs_check_zone_condition(sb, z, zone); |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 274 | isize = i_size_read(inode); |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 275 | if (!(z->z_flags & (ZONEFS_ZONE_READONLY | ZONEFS_ZONE_OFFLINE)) && |
Damien Le Moal | 14db5f6 | 2024-02-08 17:26:59 +0900 | [diff] [blame] | 276 | !write && isize == data_size) |
| 277 | return; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 278 | |
| 279 | /* |
| 280 | * At this point, we detected either a bad zone or an inconsistency |
| 281 | * between the inode size and the amount of data written in the zone. |
| 282 | * For the latter case, the cause may be a write IO error or an external |
| 283 | * action on the device. Two error patterns exist: |
| 284 | * 1) The inode size is lower than the amount of data in the zone: |
| 285 | * a write operation partially failed and data was writen at the end |
| 286 | * of the file. This can happen in the case of a large direct IO |
| 287 | * needing several BIOs and/or write requests to be processed. |
| 288 | * 2) The inode size is larger than the amount of data in the zone: |
| 289 | * this can happen with a deferred write error with the use of the |
| 290 | * device side write cache after getting successful write IO |
| 291 | * completions. Other possibilities are (a) an external corruption, |
| 292 | * e.g. an application reset the zone directly, or (b) the device |
| 293 | * has a serious problem (e.g. firmware bug). |
| 294 | * |
| 295 | * In all cases, warn about inode size inconsistency and handle the |
| 296 | * IO error according to the zone condition and to the mount options. |
| 297 | */ |
Damien Le Moal | 14db5f6 | 2024-02-08 17:26:59 +0900 | [diff] [blame] | 298 | if (isize != data_size) |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 299 | zonefs_warn(sb, |
| 300 | "inode %lu: invalid size %lld (should be %lld)\n", |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 301 | inode->i_ino, isize, data_size); |
| 302 | |
| 303 | /* |
| 304 | * First handle bad zones signaled by hardware. The mount options |
| 305 | * errors=zone-ro and errors=zone-offline result in changing the |
| 306 | * zone condition to read-only and offline respectively, as if the |
| 307 | * condition was signaled by the hardware. |
| 308 | */ |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 309 | if ((z->z_flags & ZONEFS_ZONE_OFFLINE) || |
Damien Le Moal | 46a9c52 | 2022-11-25 11:06:20 +0900 | [diff] [blame] | 310 | (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL)) { |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 311 | zonefs_warn(sb, "inode %lu: read/write access disabled\n", |
| 312 | inode->i_ino); |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 313 | if (!(z->z_flags & ZONEFS_ZONE_OFFLINE)) |
| 314 | z->z_flags |= ZONEFS_ZONE_OFFLINE; |
Damien Le Moal | 46a9c52 | 2022-11-25 11:06:20 +0900 | [diff] [blame] | 315 | zonefs_inode_update_mode(inode); |
| 316 | data_size = 0; |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 317 | } else if ((z->z_flags & ZONEFS_ZONE_READONLY) || |
Damien Le Moal | 46a9c52 | 2022-11-25 11:06:20 +0900 | [diff] [blame] | 318 | (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO)) { |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 319 | zonefs_warn(sb, "inode %lu: write access disabled\n", |
| 320 | inode->i_ino); |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 321 | if (!(z->z_flags & ZONEFS_ZONE_READONLY)) |
| 322 | z->z_flags |= ZONEFS_ZONE_READONLY; |
Damien Le Moal | 46a9c52 | 2022-11-25 11:06:20 +0900 | [diff] [blame] | 323 | zonefs_inode_update_mode(inode); |
| 324 | data_size = isize; |
Damien Le Moal | a608da3 | 2023-01-06 17:43:06 +0900 | [diff] [blame] | 325 | } else if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO && |
| 326 | data_size > isize) { |
| 327 | /* Do not expose garbage data */ |
| 328 | data_size = isize; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /* |
Johannes Thumshirn | b5c00e9 | 2020-09-11 17:56:50 +0900 | [diff] [blame] | 332 | * If the filesystem is mounted with the explicit-open mount option, we |
| 333 | * need to clear the ZONEFS_ZONE_OPEN flag if the zone transitioned to |
| 334 | * the read-only or offline condition, to avoid attempting an explicit |
| 335 | * close of the zone when the inode file is closed. |
| 336 | */ |
| 337 | if ((sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) && |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 338 | (z->z_flags & (ZONEFS_ZONE_READONLY | ZONEFS_ZONE_OFFLINE))) |
| 339 | z->z_flags &= ~ZONEFS_ZONE_OPEN; |
Johannes Thumshirn | b5c00e9 | 2020-09-11 17:56:50 +0900 | [diff] [blame] | 340 | |
| 341 | /* |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 342 | * If error=remount-ro was specified, any error result in remounting |
| 343 | * the volume as read-only. |
| 344 | */ |
| 345 | if ((sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO) && !sb_rdonly(sb)) { |
| 346 | zonefs_warn(sb, "remounting filesystem read-only\n"); |
| 347 | sb->s_flags |= SB_RDONLY; |
| 348 | } |
| 349 | |
| 350 | /* |
| 351 | * Update block usage stats and the inode size to prevent access to |
| 352 | * invalid data. |
| 353 | */ |
| 354 | zonefs_update_stats(inode, data_size); |
Johannes Thumshirn | b5c00e9 | 2020-09-11 17:56:50 +0900 | [diff] [blame] | 355 | zonefs_i_size_write(inode, data_size); |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 356 | z->z_wpoffset = data_size; |
| 357 | zonefs_inode_account_active(inode); |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | /* |
| 361 | * When an file IO error occurs, check the file zone to see if there is a change |
| 362 | * in the zone condition (e.g. offline or read-only). For a failed write to a |
| 363 | * sequential zone, the zone write pointer position must also be checked to |
| 364 | * eventually correct the file size and zonefs inode write pointer offset |
| 365 | * (which can be out of sync with the drive due to partial write failures). |
| 366 | */ |
Damien Le Moal | 4008e2a | 2022-11-25 09:39:33 +0900 | [diff] [blame] | 367 | void __zonefs_io_error(struct inode *inode, bool write) |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 368 | { |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 369 | struct zonefs_zone *z = zonefs_inode_zone(inode); |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 370 | struct super_block *sb = inode->i_sb; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 371 | unsigned int noio_flag; |
Damien Le Moal | 14db5f6 | 2024-02-08 17:26:59 +0900 | [diff] [blame] | 372 | struct blk_zone zone; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 373 | int ret; |
| 374 | |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 375 | /* |
Damien Le Moal | 14db5f6 | 2024-02-08 17:26:59 +0900 | [diff] [blame] | 376 | * Conventional zone have no write pointer and cannot become read-only |
| 377 | * or offline. So simply fake a report for a single or aggregated zone |
| 378 | * and let zonefs_handle_io_error() correct the zone inode information |
| 379 | * according to the mount options. |
Damien Le Moal | 7dd12d6 | 2022-10-25 13:39:31 +0900 | [diff] [blame] | 380 | */ |
Damien Le Moal | 14db5f6 | 2024-02-08 17:26:59 +0900 | [diff] [blame] | 381 | if (!zonefs_zone_is_seq(z)) { |
| 382 | zone.start = z->z_sector; |
| 383 | zone.len = z->z_size >> SECTOR_SHIFT; |
| 384 | zone.wp = zone.start + zone.len; |
| 385 | zone.type = BLK_ZONE_TYPE_CONVENTIONAL; |
| 386 | zone.cond = BLK_ZONE_COND_NOT_WP; |
| 387 | zone.capacity = zone.len; |
| 388 | goto handle_io_error; |
| 389 | } |
Damien Le Moal | 7dd12d6 | 2022-10-25 13:39:31 +0900 | [diff] [blame] | 390 | |
| 391 | /* |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 392 | * Memory allocations in blkdev_report_zones() can trigger a memory |
| 393 | * reclaim which may in turn cause a recursion into zonefs as well as |
| 394 | * struct request allocations for the same device. The former case may |
| 395 | * end up in a deadlock on the inode truncate mutex, while the latter |
| 396 | * may prevent IO forward progress. Executing the report zones under |
| 397 | * the GFP_NOIO context avoids both problems. |
| 398 | */ |
| 399 | noio_flag = memalloc_noio_save(); |
Damien Le Moal | 14db5f6 | 2024-02-08 17:26:59 +0900 | [diff] [blame] | 400 | ret = blkdev_report_zones(sb->s_bdev, z->z_sector, 1, |
| 401 | zonefs_io_error_cb, &zone); |
| 402 | memalloc_noio_restore(noio_flag); |
| 403 | |
| 404 | if (ret != 1) { |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 405 | zonefs_err(sb, "Get inode %lu zone information failed %d\n", |
| 406 | inode->i_ino, ret); |
Damien Le Moal | 14db5f6 | 2024-02-08 17:26:59 +0900 | [diff] [blame] | 407 | zonefs_warn(sb, "remounting filesystem read-only\n"); |
| 408 | sb->s_flags |= SB_RDONLY; |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | handle_io_error: |
| 413 | zonefs_handle_io_error(inode, &zone, write); |
Johannes Thumshirn | 48d546a | 2020-09-11 17:56:49 +0900 | [diff] [blame] | 414 | } |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 415 | |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 416 | static struct kmem_cache *zonefs_inode_cachep; |
| 417 | |
| 418 | static struct inode *zonefs_alloc_inode(struct super_block *sb) |
| 419 | { |
| 420 | struct zonefs_inode_info *zi; |
| 421 | |
Muchun Song | fd60b28 | 2022-03-22 14:41:03 -0700 | [diff] [blame] | 422 | zi = alloc_inode_sb(sb, zonefs_inode_cachep, GFP_KERNEL); |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 423 | if (!zi) |
| 424 | return NULL; |
| 425 | |
| 426 | inode_init_once(&zi->i_vnode); |
| 427 | mutex_init(&zi->i_truncate_mutex); |
Johannes Thumshirn | b5c00e9 | 2020-09-11 17:56:50 +0900 | [diff] [blame] | 428 | zi->i_wr_refcnt = 0; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 429 | |
| 430 | return &zi->i_vnode; |
| 431 | } |
| 432 | |
| 433 | static void zonefs_free_inode(struct inode *inode) |
| 434 | { |
| 435 | kmem_cache_free(zonefs_inode_cachep, ZONEFS_I(inode)); |
| 436 | } |
| 437 | |
| 438 | /* |
| 439 | * File system stat. |
| 440 | */ |
| 441 | static int zonefs_statfs(struct dentry *dentry, struct kstatfs *buf) |
| 442 | { |
| 443 | struct super_block *sb = dentry->d_sb; |
| 444 | struct zonefs_sb_info *sbi = ZONEFS_SB(sb); |
| 445 | enum zonefs_ztype t; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 446 | |
| 447 | buf->f_type = ZONEFS_MAGIC; |
| 448 | buf->f_bsize = sb->s_blocksize; |
| 449 | buf->f_namelen = ZONEFS_NAME_MAX; |
| 450 | |
| 451 | spin_lock(&sbi->s_lock); |
| 452 | |
| 453 | buf->f_blocks = sbi->s_blocks; |
| 454 | if (WARN_ON(sbi->s_used_blocks > sbi->s_blocks)) |
| 455 | buf->f_bfree = 0; |
| 456 | else |
| 457 | buf->f_bfree = buf->f_blocks - sbi->s_used_blocks; |
| 458 | buf->f_bavail = buf->f_bfree; |
| 459 | |
| 460 | for (t = 0; t < ZONEFS_ZTYPE_MAX; t++) { |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 461 | if (sbi->s_zgroup[t].g_nr_zones) |
| 462 | buf->f_files += sbi->s_zgroup[t].g_nr_zones + 1; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 463 | } |
| 464 | buf->f_ffree = 0; |
| 465 | |
| 466 | spin_unlock(&sbi->s_lock); |
| 467 | |
Amir Goldstein | 9591c3a | 2021-03-22 19:39:43 +0200 | [diff] [blame] | 468 | buf->f_fsid = uuid_to_fsid(sbi->s_uuid.b); |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 469 | |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | enum { |
Bill O'Donnell | 567e629 | 2024-02-09 13:36:59 -0600 | [diff] [blame] | 474 | Opt_errors, Opt_explicit_open, |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 475 | }; |
| 476 | |
Bill O'Donnell | 567e629 | 2024-02-09 13:36:59 -0600 | [diff] [blame] | 477 | struct zonefs_context { |
| 478 | unsigned long s_mount_opts; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 479 | }; |
| 480 | |
Bill O'Donnell | 567e629 | 2024-02-09 13:36:59 -0600 | [diff] [blame] | 481 | static const struct constant_table zonefs_param_errors[] = { |
| 482 | {"remount-ro", ZONEFS_MNTOPT_ERRORS_RO}, |
| 483 | {"zone-ro", ZONEFS_MNTOPT_ERRORS_ZRO}, |
| 484 | {"zone-offline", ZONEFS_MNTOPT_ERRORS_ZOL}, |
| 485 | {"repair", ZONEFS_MNTOPT_ERRORS_REPAIR}, |
| 486 | {} |
| 487 | }; |
| 488 | |
| 489 | static const struct fs_parameter_spec zonefs_param_spec[] = { |
| 490 | fsparam_enum ("errors", Opt_errors, zonefs_param_errors), |
| 491 | fsparam_flag ("explicit-open", Opt_explicit_open), |
| 492 | {} |
| 493 | }; |
| 494 | |
| 495 | static int zonefs_parse_param(struct fs_context *fc, struct fs_parameter *param) |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 496 | { |
Bill O'Donnell | 567e629 | 2024-02-09 13:36:59 -0600 | [diff] [blame] | 497 | struct zonefs_context *ctx = fc->fs_private; |
| 498 | struct fs_parse_result result; |
| 499 | int opt; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 500 | |
Bill O'Donnell | 567e629 | 2024-02-09 13:36:59 -0600 | [diff] [blame] | 501 | opt = fs_parse(fc, zonefs_param_spec, param, &result); |
| 502 | if (opt < 0) |
| 503 | return opt; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 504 | |
Bill O'Donnell | 567e629 | 2024-02-09 13:36:59 -0600 | [diff] [blame] | 505 | switch (opt) { |
| 506 | case Opt_errors: |
| 507 | ctx->s_mount_opts &= ~ZONEFS_MNTOPT_ERRORS_MASK; |
| 508 | ctx->s_mount_opts |= result.uint_32; |
| 509 | break; |
| 510 | case Opt_explicit_open: |
| 511 | ctx->s_mount_opts |= ZONEFS_MNTOPT_EXPLICIT_OPEN; |
| 512 | break; |
| 513 | default: |
| 514 | return -EINVAL; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | static int zonefs_show_options(struct seq_file *seq, struct dentry *root) |
| 521 | { |
| 522 | struct zonefs_sb_info *sbi = ZONEFS_SB(root->d_sb); |
| 523 | |
| 524 | if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_RO) |
| 525 | seq_puts(seq, ",errors=remount-ro"); |
| 526 | if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZRO) |
| 527 | seq_puts(seq, ",errors=zone-ro"); |
| 528 | if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_ZOL) |
| 529 | seq_puts(seq, ",errors=zone-offline"); |
| 530 | if (sbi->s_mount_opts & ZONEFS_MNTOPT_ERRORS_REPAIR) |
| 531 | seq_puts(seq, ",errors=repair"); |
| 532 | |
| 533 | return 0; |
| 534 | } |
| 535 | |
Linus Torvalds | 232dd59 | 2023-02-22 14:11:54 -0800 | [diff] [blame] | 536 | static int zonefs_inode_setattr(struct mnt_idmap *idmap, |
Damien Le Moal | 4008e2a | 2022-11-25 09:39:33 +0900 | [diff] [blame] | 537 | struct dentry *dentry, struct iattr *iattr) |
| 538 | { |
| 539 | struct inode *inode = d_inode(dentry); |
| 540 | int ret; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 541 | |
Damien Le Moal | 4008e2a | 2022-11-25 09:39:33 +0900 | [diff] [blame] | 542 | if (unlikely(IS_IMMUTABLE(inode))) |
| 543 | return -EPERM; |
| 544 | |
Linus Torvalds | 232dd59 | 2023-02-22 14:11:54 -0800 | [diff] [blame] | 545 | ret = setattr_prepare(&nop_mnt_idmap, dentry, iattr); |
Damien Le Moal | 4008e2a | 2022-11-25 09:39:33 +0900 | [diff] [blame] | 546 | if (ret) |
| 547 | return ret; |
| 548 | |
| 549 | /* |
| 550 | * Since files and directories cannot be created nor deleted, do not |
| 551 | * allow setting any write attributes on the sub-directories grouping |
| 552 | * files by zone type. |
| 553 | */ |
| 554 | if ((iattr->ia_valid & ATTR_MODE) && S_ISDIR(inode->i_mode) && |
| 555 | (iattr->ia_mode & 0222)) |
| 556 | return -EPERM; |
| 557 | |
| 558 | if (((iattr->ia_valid & ATTR_UID) && |
| 559 | !uid_eq(iattr->ia_uid, inode->i_uid)) || |
| 560 | ((iattr->ia_valid & ATTR_GID) && |
| 561 | !gid_eq(iattr->ia_gid, inode->i_gid))) { |
Linus Torvalds | 232dd59 | 2023-02-22 14:11:54 -0800 | [diff] [blame] | 562 | ret = dquot_transfer(&nop_mnt_idmap, inode, iattr); |
Damien Le Moal | 4008e2a | 2022-11-25 09:39:33 +0900 | [diff] [blame] | 563 | if (ret) |
| 564 | return ret; |
| 565 | } |
| 566 | |
| 567 | if (iattr->ia_valid & ATTR_SIZE) { |
| 568 | ret = zonefs_file_truncate(inode, iattr->ia_size); |
| 569 | if (ret) |
| 570 | return ret; |
| 571 | } |
| 572 | |
Linus Torvalds | 232dd59 | 2023-02-22 14:11:54 -0800 | [diff] [blame] | 573 | setattr_copy(&nop_mnt_idmap, inode, iattr); |
Damien Le Moal | 4008e2a | 2022-11-25 09:39:33 +0900 | [diff] [blame] | 574 | |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 575 | if (S_ISREG(inode->i_mode)) { |
| 576 | struct zonefs_zone *z = zonefs_inode_zone(inode); |
| 577 | |
| 578 | z->z_mode = inode->i_mode; |
| 579 | z->z_uid = inode->i_uid; |
| 580 | z->z_gid = inode->i_gid; |
| 581 | } |
| 582 | |
Damien Le Moal | 4008e2a | 2022-11-25 09:39:33 +0900 | [diff] [blame] | 583 | return 0; |
| 584 | } |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 585 | |
Damien Le Moal | 4008e2a | 2022-11-25 09:39:33 +0900 | [diff] [blame] | 586 | static const struct inode_operations zonefs_file_inode_operations = { |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 587 | .setattr = zonefs_inode_setattr, |
| 588 | }; |
| 589 | |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 590 | static long zonefs_fname_to_fno(const struct qstr *fname) |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 591 | { |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 592 | const char *name = fname->name; |
| 593 | unsigned int len = fname->len; |
| 594 | long fno = 0, shift = 1; |
| 595 | const char *rname; |
| 596 | char c = *name; |
| 597 | unsigned int i; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 598 | |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 599 | /* |
| 600 | * File names are always a base-10 number string without any |
| 601 | * leading 0s. |
| 602 | */ |
| 603 | if (!isdigit(c)) |
| 604 | return -ENOENT; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 605 | |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 606 | if (len > 1 && c == '0') |
| 607 | return -ENOENT; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 608 | |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 609 | if (len == 1) |
| 610 | return c - '0'; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 611 | |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 612 | for (i = 0, rname = name + len - 1; i < len; i++, rname--) { |
| 613 | c = *rname; |
| 614 | if (!isdigit(c)) |
| 615 | return -ENOENT; |
| 616 | fno += (c - '0') * shift; |
| 617 | shift *= 10; |
Damien Le Moal | 7dd12d6 | 2022-10-25 13:39:31 +0900 | [diff] [blame] | 618 | } |
Johannes Thumshirn | e3c3155 | 2020-07-21 21:10:26 +0900 | [diff] [blame] | 619 | |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 620 | return fno; |
| 621 | } |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 622 | |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 623 | static struct inode *zonefs_get_file_inode(struct inode *dir, |
| 624 | struct dentry *dentry) |
| 625 | { |
| 626 | struct zonefs_zone_group *zgroup = dir->i_private; |
| 627 | struct super_block *sb = dir->i_sb; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 628 | struct zonefs_sb_info *sbi = ZONEFS_SB(sb); |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 629 | struct zonefs_zone *z; |
| 630 | struct inode *inode; |
| 631 | ino_t ino; |
| 632 | long fno; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 633 | |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 634 | /* Get the file number from the file name */ |
| 635 | fno = zonefs_fname_to_fno(&dentry->d_name); |
| 636 | if (fno < 0) |
| 637 | return ERR_PTR(fno); |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 638 | |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 639 | if (!zgroup->g_nr_zones || fno >= zgroup->g_nr_zones) |
| 640 | return ERR_PTR(-ENOENT); |
| 641 | |
| 642 | z = &zgroup->g_zones[fno]; |
| 643 | ino = z->z_sector >> sbi->s_zone_sectors_shift; |
| 644 | inode = iget_locked(sb, ino); |
| 645 | if (!inode) |
| 646 | return ERR_PTR(-ENOMEM); |
| 647 | if (!(inode->i_state & I_NEW)) { |
| 648 | WARN_ON_ONCE(inode->i_private != z); |
| 649 | return inode; |
| 650 | } |
| 651 | |
| 652 | inode->i_ino = ino; |
| 653 | inode->i_mode = z->z_mode; |
Jeff Layton | 8df379a | 2023-10-04 14:53:03 -0400 | [diff] [blame] | 654 | inode_set_mtime_to_ts(inode, |
| 655 | inode_set_atime_to_ts(inode, inode_set_ctime_to_ts(inode, inode_get_ctime(dir)))); |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 656 | inode->i_uid = z->z_uid; |
| 657 | inode->i_gid = z->z_gid; |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 658 | inode->i_size = z->z_wpoffset; |
| 659 | inode->i_blocks = z->z_capacity >> SECTOR_SHIFT; |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 660 | inode->i_private = z; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 661 | |
| 662 | inode->i_op = &zonefs_file_inode_operations; |
| 663 | inode->i_fop = &zonefs_file_operations; |
| 664 | inode->i_mapping->a_ops = &zonefs_file_aops; |
Johannes Thumshirn | df2f970 | 2024-05-14 00:37:18 +0200 | [diff] [blame] | 665 | mapping_set_large_folios(inode->i_mapping); |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 666 | |
Damien Le Moal | 46a9c52 | 2022-11-25 11:06:20 +0900 | [diff] [blame] | 667 | /* Update the inode access rights depending on the zone condition */ |
Damien Le Moal | 46a9c52 | 2022-11-25 11:06:20 +0900 | [diff] [blame] | 668 | zonefs_inode_update_mode(inode); |
Damien Le Moal | 1da18a2 | 2022-04-12 17:41:37 +0900 | [diff] [blame] | 669 | |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 670 | unlock_new_inode(inode); |
| 671 | |
| 672 | return inode; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 673 | } |
| 674 | |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 675 | static struct inode *zonefs_get_zgroup_inode(struct super_block *sb, |
| 676 | enum zonefs_ztype ztype) |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 677 | { |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 678 | struct inode *root = d_inode(sb->s_root); |
| 679 | struct zonefs_sb_info *sbi = ZONEFS_SB(sb); |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 680 | struct inode *inode; |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 681 | ino_t ino = bdev_nr_zones(sb->s_bdev) + ztype + 1; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 682 | |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 683 | inode = iget_locked(sb, ino); |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 684 | if (!inode) |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 685 | return ERR_PTR(-ENOMEM); |
| 686 | if (!(inode->i_state & I_NEW)) |
| 687 | return inode; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 688 | |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 689 | inode->i_ino = ino; |
Linus Torvalds | 232dd59 | 2023-02-22 14:11:54 -0800 | [diff] [blame] | 690 | inode_init_owner(&nop_mnt_idmap, inode, root, S_IFDIR | 0555); |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 691 | inode->i_size = sbi->s_zgroup[ztype].g_nr_zones; |
Jeff Layton | 8df379a | 2023-10-04 14:53:03 -0400 | [diff] [blame] | 692 | inode_set_mtime_to_ts(inode, |
| 693 | inode_set_atime_to_ts(inode, inode_set_ctime_to_ts(inode, inode_get_ctime(root)))); |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 694 | inode->i_private = &sbi->s_zgroup[ztype]; |
| 695 | set_nlink(inode, 2); |
Damien Le Moal | 1da18a2 | 2022-04-12 17:41:37 +0900 | [diff] [blame] | 696 | |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 697 | inode->i_op = &zonefs_dir_inode_operations; |
| 698 | inode->i_fop = &zonefs_dir_operations; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 699 | |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 700 | unlock_new_inode(inode); |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 701 | |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 702 | return inode; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 703 | } |
| 704 | |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 705 | |
| 706 | static struct inode *zonefs_get_dir_inode(struct inode *dir, |
| 707 | struct dentry *dentry) |
| 708 | { |
| 709 | struct super_block *sb = dir->i_sb; |
| 710 | struct zonefs_sb_info *sbi = ZONEFS_SB(sb); |
| 711 | const char *name = dentry->d_name.name; |
| 712 | enum zonefs_ztype ztype; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 713 | |
| 714 | /* |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 715 | * We only need to check for the "seq" directory and |
| 716 | * the "cnv" directory if we have conventional zones. |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 717 | */ |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 718 | if (dentry->d_name.len != 3) |
| 719 | return ERR_PTR(-ENOENT); |
| 720 | |
| 721 | for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) { |
| 722 | if (sbi->s_zgroup[ztype].g_nr_zones && |
| 723 | memcmp(name, zonefs_zgroup_name(ztype), 3) == 0) |
| 724 | break; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 725 | } |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 726 | if (ztype == ZONEFS_ZTYPE_MAX) |
| 727 | return ERR_PTR(-ENOENT); |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 728 | |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 729 | return zonefs_get_zgroup_inode(sb, ztype); |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 730 | } |
| 731 | |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 732 | static struct dentry *zonefs_lookup(struct inode *dir, struct dentry *dentry, |
| 733 | unsigned int flags) |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 734 | { |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 735 | struct inode *inode; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 736 | |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 737 | if (dentry->d_name.len > ZONEFS_NAME_MAX) |
| 738 | return ERR_PTR(-ENAMETOOLONG); |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 739 | |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 740 | if (dir == d_inode(dir->i_sb->s_root)) |
| 741 | inode = zonefs_get_dir_inode(dir, dentry); |
| 742 | else |
| 743 | inode = zonefs_get_file_inode(dir, dentry); |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 744 | |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 745 | return d_splice_alias(inode, dentry); |
| 746 | } |
| 747 | |
| 748 | static int zonefs_readdir_root(struct file *file, struct dir_context *ctx) |
| 749 | { |
| 750 | struct inode *inode = file_inode(file); |
| 751 | struct super_block *sb = inode->i_sb; |
| 752 | struct zonefs_sb_info *sbi = ZONEFS_SB(sb); |
| 753 | enum zonefs_ztype ztype = ZONEFS_ZTYPE_CNV; |
| 754 | ino_t base_ino = bdev_nr_zones(sb->s_bdev) + 1; |
| 755 | |
| 756 | if (ctx->pos >= inode->i_size) |
| 757 | return 0; |
| 758 | |
| 759 | if (!dir_emit_dots(file, ctx)) |
| 760 | return 0; |
| 761 | |
| 762 | if (ctx->pos == 2) { |
| 763 | if (!sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones) |
| 764 | ztype = ZONEFS_ZTYPE_SEQ; |
| 765 | |
| 766 | if (!dir_emit(ctx, zonefs_zgroup_name(ztype), 3, |
| 767 | base_ino + ztype, DT_DIR)) |
| 768 | return 0; |
| 769 | ctx->pos++; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 770 | } |
| 771 | |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 772 | if (ctx->pos == 3 && ztype != ZONEFS_ZTYPE_SEQ) { |
| 773 | ztype = ZONEFS_ZTYPE_SEQ; |
| 774 | if (!dir_emit(ctx, zonefs_zgroup_name(ztype), 3, |
| 775 | base_ino + ztype, DT_DIR)) |
| 776 | return 0; |
| 777 | ctx->pos++; |
| 778 | } |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 779 | |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 780 | return 0; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 781 | } |
| 782 | |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 783 | static int zonefs_readdir_zgroup(struct file *file, |
| 784 | struct dir_context *ctx) |
| 785 | { |
| 786 | struct inode *inode = file_inode(file); |
| 787 | struct zonefs_zone_group *zgroup = inode->i_private; |
| 788 | struct super_block *sb = inode->i_sb; |
| 789 | struct zonefs_sb_info *sbi = ZONEFS_SB(sb); |
| 790 | struct zonefs_zone *z; |
| 791 | int fname_len; |
| 792 | char *fname; |
| 793 | ino_t ino; |
| 794 | int f; |
| 795 | |
| 796 | /* |
| 797 | * The size of zone group directories is equal to the number |
| 798 | * of zone files in the group and does note include the "." and |
| 799 | * ".." entries. Hence the "+ 2" here. |
| 800 | */ |
| 801 | if (ctx->pos >= inode->i_size + 2) |
| 802 | return 0; |
| 803 | |
| 804 | if (!dir_emit_dots(file, ctx)) |
| 805 | return 0; |
| 806 | |
| 807 | fname = kmalloc(ZONEFS_NAME_MAX, GFP_KERNEL); |
| 808 | if (!fname) |
| 809 | return -ENOMEM; |
| 810 | |
| 811 | for (f = ctx->pos - 2; f < zgroup->g_nr_zones; f++) { |
| 812 | z = &zgroup->g_zones[f]; |
| 813 | ino = z->z_sector >> sbi->s_zone_sectors_shift; |
| 814 | fname_len = snprintf(fname, ZONEFS_NAME_MAX - 1, "%u", f); |
| 815 | if (!dir_emit(ctx, fname, fname_len, ino, DT_REG)) |
| 816 | break; |
| 817 | ctx->pos++; |
| 818 | } |
| 819 | |
| 820 | kfree(fname); |
| 821 | |
| 822 | return 0; |
| 823 | } |
| 824 | |
| 825 | static int zonefs_readdir(struct file *file, struct dir_context *ctx) |
| 826 | { |
| 827 | struct inode *inode = file_inode(file); |
| 828 | |
| 829 | if (inode == d_inode(inode->i_sb->s_root)) |
| 830 | return zonefs_readdir_root(file, ctx); |
| 831 | |
| 832 | return zonefs_readdir_zgroup(file, ctx); |
| 833 | } |
| 834 | |
| 835 | const struct inode_operations zonefs_dir_inode_operations = { |
| 836 | .lookup = zonefs_lookup, |
| 837 | .setattr = zonefs_inode_setattr, |
| 838 | }; |
| 839 | |
| 840 | const struct file_operations zonefs_dir_operations = { |
| 841 | .llseek = generic_file_llseek, |
| 842 | .read = generic_read_dir, |
| 843 | .iterate_shared = zonefs_readdir, |
| 844 | }; |
| 845 | |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 846 | struct zonefs_zone_data { |
| 847 | struct super_block *sb; |
| 848 | unsigned int nr_zones[ZONEFS_ZTYPE_MAX]; |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 849 | sector_t cnv_zone_start; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 850 | struct blk_zone *zones; |
| 851 | }; |
| 852 | |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 853 | static int zonefs_get_zone_info_cb(struct blk_zone *zone, unsigned int idx, |
| 854 | void *data) |
| 855 | { |
| 856 | struct zonefs_zone_data *zd = data; |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 857 | struct super_block *sb = zd->sb; |
| 858 | struct zonefs_sb_info *sbi = ZONEFS_SB(sb); |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 859 | |
| 860 | /* |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 861 | * We do not care about the first zone: it contains the super block |
| 862 | * and not exposed as a file. |
| 863 | */ |
| 864 | if (!idx) |
| 865 | return 0; |
| 866 | |
| 867 | /* |
| 868 | * Count the number of zones that will be exposed as files. |
| 869 | * For sequential zones, we always have as many files as zones. |
| 870 | * FOr conventional zones, the number of files depends on if we have |
| 871 | * conventional zones aggregation enabled. |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 872 | */ |
| 873 | switch (zone->type) { |
| 874 | case BLK_ZONE_TYPE_CONVENTIONAL: |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 875 | if (sbi->s_features & ZONEFS_F_AGGRCNV) { |
| 876 | /* One file per set of contiguous conventional zones */ |
| 877 | if (!(sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones) || |
| 878 | zone->start != zd->cnv_zone_start) |
| 879 | sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones++; |
| 880 | zd->cnv_zone_start = zone->start + zone->len; |
| 881 | } else { |
| 882 | /* One file per zone */ |
| 883 | sbi->s_zgroup[ZONEFS_ZTYPE_CNV].g_nr_zones++; |
| 884 | } |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 885 | break; |
| 886 | case BLK_ZONE_TYPE_SEQWRITE_REQ: |
| 887 | case BLK_ZONE_TYPE_SEQWRITE_PREF: |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 888 | sbi->s_zgroup[ZONEFS_ZTYPE_SEQ].g_nr_zones++; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 889 | break; |
| 890 | default: |
| 891 | zonefs_err(zd->sb, "Unsupported zone type 0x%x\n", |
| 892 | zone->type); |
| 893 | return -EIO; |
| 894 | } |
| 895 | |
| 896 | memcpy(&zd->zones[idx], zone, sizeof(struct blk_zone)); |
| 897 | |
| 898 | return 0; |
| 899 | } |
| 900 | |
| 901 | static int zonefs_get_zone_info(struct zonefs_zone_data *zd) |
| 902 | { |
| 903 | struct block_device *bdev = zd->sb->s_bdev; |
| 904 | int ret; |
| 905 | |
Christoph Hellwig | b623e34 | 2022-07-06 09:03:45 +0200 | [diff] [blame] | 906 | zd->zones = kvcalloc(bdev_nr_zones(bdev), sizeof(struct blk_zone), |
| 907 | GFP_KERNEL); |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 908 | if (!zd->zones) |
| 909 | return -ENOMEM; |
| 910 | |
| 911 | /* Get zones information from the device */ |
| 912 | ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES, |
| 913 | zonefs_get_zone_info_cb, zd); |
| 914 | if (ret < 0) { |
| 915 | zonefs_err(zd->sb, "Zone report failed %d\n", ret); |
| 916 | return ret; |
| 917 | } |
| 918 | |
Christoph Hellwig | b623e34 | 2022-07-06 09:03:45 +0200 | [diff] [blame] | 919 | if (ret != bdev_nr_zones(bdev)) { |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 920 | zonefs_err(zd->sb, "Invalid zone report (%d/%u zones)\n", |
Christoph Hellwig | b623e34 | 2022-07-06 09:03:45 +0200 | [diff] [blame] | 921 | ret, bdev_nr_zones(bdev)); |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 922 | return -EIO; |
| 923 | } |
| 924 | |
| 925 | return 0; |
| 926 | } |
| 927 | |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 928 | static inline void zonefs_free_zone_info(struct zonefs_zone_data *zd) |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 929 | { |
| 930 | kvfree(zd->zones); |
| 931 | } |
| 932 | |
| 933 | /* |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 934 | * Create a zone group and populate it with zone files. |
| 935 | */ |
| 936 | static int zonefs_init_zgroup(struct super_block *sb, |
| 937 | struct zonefs_zone_data *zd, |
| 938 | enum zonefs_ztype ztype) |
| 939 | { |
| 940 | struct zonefs_sb_info *sbi = ZONEFS_SB(sb); |
| 941 | struct zonefs_zone_group *zgroup = &sbi->s_zgroup[ztype]; |
| 942 | struct blk_zone *zone, *next, *end; |
| 943 | struct zonefs_zone *z; |
| 944 | unsigned int n = 0; |
| 945 | int ret; |
| 946 | |
| 947 | /* Allocate the zone group. If it is empty, we have nothing to do. */ |
| 948 | if (!zgroup->g_nr_zones) |
| 949 | return 0; |
| 950 | |
| 951 | zgroup->g_zones = kvcalloc(zgroup->g_nr_zones, |
| 952 | sizeof(struct zonefs_zone), GFP_KERNEL); |
| 953 | if (!zgroup->g_zones) |
| 954 | return -ENOMEM; |
| 955 | |
| 956 | /* |
| 957 | * Initialize the zone groups using the device zone information. |
| 958 | * We always skip the first zone as it contains the super block |
| 959 | * and is not use to back a file. |
| 960 | */ |
| 961 | end = zd->zones + bdev_nr_zones(sb->s_bdev); |
| 962 | for (zone = &zd->zones[1]; zone < end; zone = next) { |
| 963 | |
| 964 | next = zone + 1; |
| 965 | if (zonefs_zone_type(zone) != ztype) |
| 966 | continue; |
| 967 | |
| 968 | if (WARN_ON_ONCE(n >= zgroup->g_nr_zones)) |
| 969 | return -EINVAL; |
| 970 | |
| 971 | /* |
| 972 | * For conventional zones, contiguous zones can be aggregated |
| 973 | * together to form larger files. Note that this overwrites the |
| 974 | * length of the first zone of the set of contiguous zones |
| 975 | * aggregated together. If one offline or read-only zone is |
| 976 | * found, assume that all zones aggregated have the same |
| 977 | * condition. |
| 978 | */ |
| 979 | if (ztype == ZONEFS_ZTYPE_CNV && |
| 980 | (sbi->s_features & ZONEFS_F_AGGRCNV)) { |
| 981 | for (; next < end; next++) { |
| 982 | if (zonefs_zone_type(next) != ztype) |
| 983 | break; |
| 984 | zone->len += next->len; |
| 985 | zone->capacity += next->capacity; |
| 986 | if (next->cond == BLK_ZONE_COND_READONLY && |
| 987 | zone->cond != BLK_ZONE_COND_OFFLINE) |
| 988 | zone->cond = BLK_ZONE_COND_READONLY; |
| 989 | else if (next->cond == BLK_ZONE_COND_OFFLINE) |
| 990 | zone->cond = BLK_ZONE_COND_OFFLINE; |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | z = &zgroup->g_zones[n]; |
| 995 | if (ztype == ZONEFS_ZTYPE_CNV) |
| 996 | z->z_flags |= ZONEFS_ZONE_CNV; |
| 997 | z->z_sector = zone->start; |
| 998 | z->z_size = zone->len << SECTOR_SHIFT; |
| 999 | if (z->z_size > bdev_zone_sectors(sb->s_bdev) << SECTOR_SHIFT && |
| 1000 | !(sbi->s_features & ZONEFS_F_AGGRCNV)) { |
| 1001 | zonefs_err(sb, |
| 1002 | "Invalid zone size %llu (device zone sectors %llu)\n", |
| 1003 | z->z_size, |
| 1004 | bdev_zone_sectors(sb->s_bdev) << SECTOR_SHIFT); |
| 1005 | return -EINVAL; |
| 1006 | } |
| 1007 | |
| 1008 | z->z_capacity = min_t(loff_t, MAX_LFS_FILESIZE, |
| 1009 | zone->capacity << SECTOR_SHIFT); |
| 1010 | z->z_wpoffset = zonefs_check_zone_condition(sb, z, zone); |
| 1011 | |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 1012 | z->z_mode = S_IFREG | sbi->s_perm; |
| 1013 | z->z_uid = sbi->s_uid; |
| 1014 | z->z_gid = sbi->s_gid; |
| 1015 | |
| 1016 | /* |
| 1017 | * Let zonefs_inode_update_mode() know that we will need |
| 1018 | * special initialization of the inode mode the first time |
| 1019 | * it is accessed. |
| 1020 | */ |
| 1021 | z->z_flags |= ZONEFS_ZONE_INIT_MODE; |
| 1022 | |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 1023 | sb->s_maxbytes = max(z->z_capacity, sb->s_maxbytes); |
| 1024 | sbi->s_blocks += z->z_capacity >> sb->s_blocksize_bits; |
| 1025 | sbi->s_used_blocks += z->z_wpoffset >> sb->s_blocksize_bits; |
| 1026 | |
| 1027 | /* |
| 1028 | * For sequential zones, make sure that any open zone is closed |
| 1029 | * first to ensure that the initial number of open zones is 0, |
| 1030 | * in sync with the open zone accounting done when the mount |
| 1031 | * option ZONEFS_MNTOPT_EXPLICIT_OPEN is used. |
| 1032 | */ |
| 1033 | if (ztype == ZONEFS_ZTYPE_SEQ && |
| 1034 | (zone->cond == BLK_ZONE_COND_IMP_OPEN || |
| 1035 | zone->cond == BLK_ZONE_COND_EXP_OPEN)) { |
| 1036 | ret = zonefs_zone_mgmt(sb, z, REQ_OP_ZONE_CLOSE); |
| 1037 | if (ret) |
| 1038 | return ret; |
| 1039 | } |
| 1040 | |
| 1041 | zonefs_account_active(sb, z); |
| 1042 | |
| 1043 | n++; |
| 1044 | } |
| 1045 | |
| 1046 | if (WARN_ON_ONCE(n != zgroup->g_nr_zones)) |
| 1047 | return -EINVAL; |
| 1048 | |
| 1049 | zonefs_info(sb, "Zone group \"%s\" has %u file%s\n", |
| 1050 | zonefs_zgroup_name(ztype), |
| 1051 | zgroup->g_nr_zones, |
Thorsten Blum | 60b703c | 2024-04-02 12:17:16 +0200 | [diff] [blame] | 1052 | str_plural(zgroup->g_nr_zones)); |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 1053 | |
| 1054 | return 0; |
| 1055 | } |
| 1056 | |
| 1057 | static void zonefs_free_zgroups(struct super_block *sb) |
| 1058 | { |
| 1059 | struct zonefs_sb_info *sbi = ZONEFS_SB(sb); |
| 1060 | enum zonefs_ztype ztype; |
| 1061 | |
| 1062 | if (!sbi) |
| 1063 | return; |
| 1064 | |
| 1065 | for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) { |
| 1066 | kvfree(sbi->s_zgroup[ztype].g_zones); |
| 1067 | sbi->s_zgroup[ztype].g_zones = NULL; |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | /* |
| 1072 | * Create a zone group and populate it with zone files. |
| 1073 | */ |
| 1074 | static int zonefs_init_zgroups(struct super_block *sb) |
| 1075 | { |
| 1076 | struct zonefs_zone_data zd; |
| 1077 | enum zonefs_ztype ztype; |
| 1078 | int ret; |
| 1079 | |
| 1080 | /* First get the device zone information */ |
| 1081 | memset(&zd, 0, sizeof(struct zonefs_zone_data)); |
| 1082 | zd.sb = sb; |
| 1083 | ret = zonefs_get_zone_info(&zd); |
| 1084 | if (ret) |
| 1085 | goto cleanup; |
| 1086 | |
| 1087 | /* Allocate and initialize the zone groups */ |
| 1088 | for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) { |
| 1089 | ret = zonefs_init_zgroup(sb, &zd, ztype); |
| 1090 | if (ret) { |
| 1091 | zonefs_info(sb, |
| 1092 | "Zone group \"%s\" initialization failed\n", |
| 1093 | zonefs_zgroup_name(ztype)); |
| 1094 | break; |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | cleanup: |
| 1099 | zonefs_free_zone_info(&zd); |
| 1100 | if (ret) |
| 1101 | zonefs_free_zgroups(sb); |
| 1102 | |
| 1103 | return ret; |
| 1104 | } |
| 1105 | |
| 1106 | /* |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1107 | * Read super block information from the device. |
| 1108 | */ |
| 1109 | static int zonefs_read_super(struct super_block *sb) |
| 1110 | { |
| 1111 | struct zonefs_sb_info *sbi = ZONEFS_SB(sb); |
| 1112 | struct zonefs_super *super; |
| 1113 | u32 crc, stored_crc; |
| 1114 | struct page *page; |
| 1115 | struct bio_vec bio_vec; |
| 1116 | struct bio bio; |
| 1117 | int ret; |
| 1118 | |
| 1119 | page = alloc_page(GFP_KERNEL); |
| 1120 | if (!page) |
| 1121 | return -ENOMEM; |
| 1122 | |
Christoph Hellwig | 49add49 | 2022-01-24 10:11:06 +0100 | [diff] [blame] | 1123 | bio_init(&bio, sb->s_bdev, &bio_vec, 1, REQ_OP_READ); |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1124 | bio.bi_iter.bi_sector = 0; |
Johannes Thumshirn | 0fa5b08 | 2023-05-31 04:50:33 -0700 | [diff] [blame] | 1125 | __bio_add_page(&bio, page, PAGE_SIZE, 0); |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1126 | |
| 1127 | ret = submit_bio_wait(&bio); |
| 1128 | if (ret) |
| 1129 | goto free_page; |
| 1130 | |
Fabio M. De Francesco | 6bac30b | 2022-07-05 16:22:02 +0200 | [diff] [blame] | 1131 | super = page_address(page); |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1132 | |
| 1133 | ret = -EINVAL; |
| 1134 | if (le32_to_cpu(super->s_magic) != ZONEFS_MAGIC) |
Fabio M. De Francesco | 6bac30b | 2022-07-05 16:22:02 +0200 | [diff] [blame] | 1135 | goto free_page; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1136 | |
| 1137 | stored_crc = le32_to_cpu(super->s_crc); |
| 1138 | super->s_crc = 0; |
| 1139 | crc = crc32(~0U, (unsigned char *)super, sizeof(struct zonefs_super)); |
| 1140 | if (crc != stored_crc) { |
| 1141 | zonefs_err(sb, "Invalid checksum (Expected 0x%08x, got 0x%08x)", |
| 1142 | crc, stored_crc); |
Fabio M. De Francesco | 6bac30b | 2022-07-05 16:22:02 +0200 | [diff] [blame] | 1143 | goto free_page; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | sbi->s_features = le64_to_cpu(super->s_features); |
| 1147 | if (sbi->s_features & ~ZONEFS_F_DEFINED_FEATURES) { |
| 1148 | zonefs_err(sb, "Unknown features set 0x%llx\n", |
| 1149 | sbi->s_features); |
Fabio M. De Francesco | 6bac30b | 2022-07-05 16:22:02 +0200 | [diff] [blame] | 1150 | goto free_page; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | if (sbi->s_features & ZONEFS_F_UID) { |
| 1154 | sbi->s_uid = make_kuid(current_user_ns(), |
| 1155 | le32_to_cpu(super->s_uid)); |
| 1156 | if (!uid_valid(sbi->s_uid)) { |
| 1157 | zonefs_err(sb, "Invalid UID feature\n"); |
Fabio M. De Francesco | 6bac30b | 2022-07-05 16:22:02 +0200 | [diff] [blame] | 1158 | goto free_page; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | if (sbi->s_features & ZONEFS_F_GID) { |
| 1163 | sbi->s_gid = make_kgid(current_user_ns(), |
| 1164 | le32_to_cpu(super->s_gid)); |
| 1165 | if (!gid_valid(sbi->s_gid)) { |
| 1166 | zonefs_err(sb, "Invalid GID feature\n"); |
Fabio M. De Francesco | 6bac30b | 2022-07-05 16:22:02 +0200 | [diff] [blame] | 1167 | goto free_page; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | if (sbi->s_features & ZONEFS_F_PERM) |
| 1172 | sbi->s_perm = le32_to_cpu(super->s_perm); |
| 1173 | |
| 1174 | if (memchr_inv(super->s_reserved, 0, sizeof(super->s_reserved))) { |
| 1175 | zonefs_err(sb, "Reserved area is being used\n"); |
Fabio M. De Francesco | 6bac30b | 2022-07-05 16:22:02 +0200 | [diff] [blame] | 1176 | goto free_page; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1177 | } |
| 1178 | |
Andy Shevchenko | 568776f | 2020-04-23 18:32:11 +0300 | [diff] [blame] | 1179 | import_uuid(&sbi->s_uuid, super->s_uuid); |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1180 | ret = 0; |
| 1181 | |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1182 | free_page: |
| 1183 | __free_page(page); |
| 1184 | |
| 1185 | return ret; |
| 1186 | } |
| 1187 | |
Damien Le Moal | 4008e2a | 2022-11-25 09:39:33 +0900 | [diff] [blame] | 1188 | static const struct super_operations zonefs_sops = { |
| 1189 | .alloc_inode = zonefs_alloc_inode, |
| 1190 | .free_inode = zonefs_free_inode, |
| 1191 | .statfs = zonefs_statfs, |
Damien Le Moal | 4008e2a | 2022-11-25 09:39:33 +0900 | [diff] [blame] | 1192 | .show_options = zonefs_show_options, |
| 1193 | }; |
| 1194 | |
Damien Le Moal | 43592c4 | 2023-01-04 17:20:55 +0900 | [diff] [blame] | 1195 | static int zonefs_get_zgroup_inodes(struct super_block *sb) |
| 1196 | { |
| 1197 | struct zonefs_sb_info *sbi = ZONEFS_SB(sb); |
| 1198 | struct inode *dir_inode; |
| 1199 | enum zonefs_ztype ztype; |
| 1200 | |
| 1201 | for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) { |
| 1202 | if (!sbi->s_zgroup[ztype].g_nr_zones) |
| 1203 | continue; |
| 1204 | |
| 1205 | dir_inode = zonefs_get_zgroup_inode(sb, ztype); |
| 1206 | if (IS_ERR(dir_inode)) |
| 1207 | return PTR_ERR(dir_inode); |
| 1208 | |
| 1209 | sbi->s_zgroup[ztype].g_inode = dir_inode; |
| 1210 | } |
| 1211 | |
| 1212 | return 0; |
| 1213 | } |
| 1214 | |
| 1215 | static void zonefs_release_zgroup_inodes(struct super_block *sb) |
| 1216 | { |
| 1217 | struct zonefs_sb_info *sbi = ZONEFS_SB(sb); |
| 1218 | enum zonefs_ztype ztype; |
| 1219 | |
| 1220 | if (!sbi) |
| 1221 | return; |
| 1222 | |
| 1223 | for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) { |
| 1224 | if (sbi->s_zgroup[ztype].g_inode) { |
| 1225 | iput(sbi->s_zgroup[ztype].g_inode); |
| 1226 | sbi->s_zgroup[ztype].g_inode = NULL; |
| 1227 | } |
| 1228 | } |
| 1229 | } |
| 1230 | |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1231 | /* |
| 1232 | * Check that the device is zoned. If it is, get the list of zones and create |
| 1233 | * sub-directories and files according to the device zone configuration and |
| 1234 | * format options. |
| 1235 | */ |
Bill O'Donnell | 567e629 | 2024-02-09 13:36:59 -0600 | [diff] [blame] | 1236 | static int zonefs_fill_super(struct super_block *sb, struct fs_context *fc) |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1237 | { |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1238 | struct zonefs_sb_info *sbi; |
Bill O'Donnell | 567e629 | 2024-02-09 13:36:59 -0600 | [diff] [blame] | 1239 | struct zonefs_context *ctx = fc->fs_private; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1240 | struct inode *inode; |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 1241 | enum zonefs_ztype ztype; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1242 | int ret; |
| 1243 | |
| 1244 | if (!bdev_is_zoned(sb->s_bdev)) { |
| 1245 | zonefs_err(sb, "Not a zoned block device\n"); |
| 1246 | return -EINVAL; |
| 1247 | } |
| 1248 | |
| 1249 | /* |
| 1250 | * Initialize super block information: the maximum file size is updated |
| 1251 | * when the zone files are created so that the format option |
| 1252 | * ZONEFS_F_AGGRCNV which increases the maximum file size of a file |
| 1253 | * beyond the zone size is taken into account. |
| 1254 | */ |
| 1255 | sbi = kzalloc(sizeof(*sbi), GFP_KERNEL); |
| 1256 | if (!sbi) |
| 1257 | return -ENOMEM; |
| 1258 | |
| 1259 | spin_lock_init(&sbi->s_lock); |
| 1260 | sb->s_fs_info = sbi; |
| 1261 | sb->s_magic = ZONEFS_MAGIC; |
| 1262 | sb->s_maxbytes = 0; |
| 1263 | sb->s_op = &zonefs_sops; |
| 1264 | sb->s_time_gran = 1; |
| 1265 | |
| 1266 | /* |
Damien Le Moal | 0f1ba5f | 2021-01-28 13:47:31 +0900 | [diff] [blame] | 1267 | * The block size is set to the device zone write granularity to ensure |
| 1268 | * that write operations are always aligned according to the device |
| 1269 | * interface constraints. |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1270 | */ |
Damien Le Moal | 0f1ba5f | 2021-01-28 13:47:31 +0900 | [diff] [blame] | 1271 | sb_set_blocksize(sb, bdev_zone_write_granularity(sb->s_bdev)); |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1272 | sbi->s_zone_sectors_shift = ilog2(bdev_zone_sectors(sb->s_bdev)); |
| 1273 | sbi->s_uid = GLOBAL_ROOT_UID; |
| 1274 | sbi->s_gid = GLOBAL_ROOT_GID; |
| 1275 | sbi->s_perm = 0640; |
Bill O'Donnell | 567e629 | 2024-02-09 13:36:59 -0600 | [diff] [blame] | 1276 | sbi->s_mount_opts = ctx->s_mount_opts; |
Damien Le Moal | 2b95a23 | 2022-04-12 17:00:13 +0900 | [diff] [blame] | 1277 | |
| 1278 | atomic_set(&sbi->s_wro_seq_files, 0); |
| 1279 | sbi->s_max_wro_seq_files = bdev_max_open_zones(sb->s_bdev); |
Damien Le Moal | 87c9ce3f | 2022-04-12 18:54:39 +0900 | [diff] [blame] | 1280 | atomic_set(&sbi->s_active_seq_files, 0); |
| 1281 | sbi->s_max_active_seq_files = bdev_max_active_zones(sb->s_bdev); |
| 1282 | |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1283 | ret = zonefs_read_super(sb); |
| 1284 | if (ret) |
| 1285 | return ret; |
| 1286 | |
Christoph Hellwig | b623e34 | 2022-07-06 09:03:45 +0200 | [diff] [blame] | 1287 | zonefs_info(sb, "Mounting %u zones", bdev_nr_zones(sb->s_bdev)); |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1288 | |
Damien Le Moal | a2a513b | 2022-06-02 23:16:57 +0900 | [diff] [blame] | 1289 | if (!sbi->s_max_wro_seq_files && |
Damien Le Moal | 96eca14 | 2022-06-02 21:33:25 +0900 | [diff] [blame] | 1290 | !sbi->s_max_active_seq_files && |
Damien Le Moal | a2a513b | 2022-06-02 23:16:57 +0900 | [diff] [blame] | 1291 | sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) { |
Damien Le Moal | 96eca14 | 2022-06-02 21:33:25 +0900 | [diff] [blame] | 1292 | zonefs_info(sb, |
| 1293 | "No open and active zone limits. Ignoring explicit_open mount option\n"); |
Damien Le Moal | a2a513b | 2022-06-02 23:16:57 +0900 | [diff] [blame] | 1294 | sbi->s_mount_opts &= ~ZONEFS_MNTOPT_EXPLICIT_OPEN; |
| 1295 | } |
| 1296 | |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 1297 | /* Initialize the zone groups */ |
| 1298 | ret = zonefs_init_zgroups(sb); |
| 1299 | if (ret) |
| 1300 | goto cleanup; |
| 1301 | |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 1302 | /* Create the root directory inode */ |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1303 | ret = -ENOMEM; |
| 1304 | inode = new_inode(sb); |
| 1305 | if (!inode) |
| 1306 | goto cleanup; |
| 1307 | |
Christoph Hellwig | b623e34 | 2022-07-06 09:03:45 +0200 | [diff] [blame] | 1308 | inode->i_ino = bdev_nr_zones(sb->s_bdev); |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1309 | inode->i_mode = S_IFDIR | 0555; |
Jeff Layton | 8df379a | 2023-10-04 14:53:03 -0400 | [diff] [blame] | 1310 | simple_inode_init_ts(inode); |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1311 | inode->i_op = &zonefs_dir_inode_operations; |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 1312 | inode->i_fop = &zonefs_dir_operations; |
| 1313 | inode->i_size = 2; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1314 | set_nlink(inode, 2); |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 1315 | for (ztype = 0; ztype < ZONEFS_ZTYPE_MAX; ztype++) { |
| 1316 | if (sbi->s_zgroup[ztype].g_nr_zones) { |
| 1317 | inc_nlink(inode); |
| 1318 | inode->i_size++; |
| 1319 | } |
| 1320 | } |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1321 | |
| 1322 | sb->s_root = d_make_root(inode); |
| 1323 | if (!sb->s_root) |
| 1324 | goto cleanup; |
| 1325 | |
Damien Le Moal | 43592c4 | 2023-01-04 17:20:55 +0900 | [diff] [blame] | 1326 | /* |
| 1327 | * Take a reference on the zone groups directory inodes |
| 1328 | * to keep them in the inode cache. |
| 1329 | */ |
| 1330 | ret = zonefs_get_zgroup_inodes(sb); |
| 1331 | if (ret) |
| 1332 | goto cleanup; |
| 1333 | |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 1334 | ret = zonefs_sysfs_register(sb); |
| 1335 | if (ret) |
| 1336 | goto cleanup; |
| 1337 | |
| 1338 | return 0; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1339 | |
| 1340 | cleanup: |
Damien Le Moal | 43592c4 | 2023-01-04 17:20:55 +0900 | [diff] [blame] | 1341 | zonefs_release_zgroup_inodes(sb); |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 1342 | zonefs_free_zgroups(sb); |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1343 | |
| 1344 | return ret; |
| 1345 | } |
| 1346 | |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1347 | static void zonefs_kill_super(struct super_block *sb) |
| 1348 | { |
| 1349 | struct zonefs_sb_info *sbi = ZONEFS_SB(sb); |
| 1350 | |
Damien Le Moal | 43592c4 | 2023-01-04 17:20:55 +0900 | [diff] [blame] | 1351 | /* Release the reference on the zone group directory inodes */ |
| 1352 | zonefs_release_zgroup_inodes(sb); |
| 1353 | |
Damien Le Moal | d207794 | 2022-11-30 11:01:09 +0900 | [diff] [blame] | 1354 | kill_block_super(sb); |
Damien Le Moal | 9277a6d | 2022-04-12 16:25:34 +0900 | [diff] [blame] | 1355 | |
| 1356 | zonefs_sysfs_unregister(sb); |
Damien Le Moal | aa7f243 | 2022-11-16 18:15:40 +0900 | [diff] [blame] | 1357 | zonefs_free_zgroups(sb); |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1358 | kfree(sbi); |
| 1359 | } |
| 1360 | |
Bill O'Donnell | 567e629 | 2024-02-09 13:36:59 -0600 | [diff] [blame] | 1361 | static void zonefs_free_fc(struct fs_context *fc) |
| 1362 | { |
| 1363 | struct zonefs_context *ctx = fc->fs_private; |
| 1364 | |
| 1365 | kfree(ctx); |
| 1366 | } |
| 1367 | |
| 1368 | static int zonefs_get_tree(struct fs_context *fc) |
| 1369 | { |
| 1370 | return get_tree_bdev(fc, zonefs_fill_super); |
| 1371 | } |
| 1372 | |
| 1373 | static int zonefs_reconfigure(struct fs_context *fc) |
| 1374 | { |
| 1375 | struct zonefs_context *ctx = fc->fs_private; |
| 1376 | struct super_block *sb = fc->root->d_sb; |
| 1377 | struct zonefs_sb_info *sbi = sb->s_fs_info; |
| 1378 | |
| 1379 | sync_filesystem(fc->root->d_sb); |
| 1380 | /* Copy new options from ctx into sbi. */ |
| 1381 | sbi->s_mount_opts = ctx->s_mount_opts; |
| 1382 | |
| 1383 | return 0; |
| 1384 | } |
| 1385 | |
| 1386 | static const struct fs_context_operations zonefs_context_ops = { |
| 1387 | .parse_param = zonefs_parse_param, |
| 1388 | .get_tree = zonefs_get_tree, |
| 1389 | .reconfigure = zonefs_reconfigure, |
| 1390 | .free = zonefs_free_fc, |
| 1391 | }; |
| 1392 | |
| 1393 | /* |
| 1394 | * Set up the filesystem mount context. |
| 1395 | */ |
| 1396 | static int zonefs_init_fs_context(struct fs_context *fc) |
| 1397 | { |
| 1398 | struct zonefs_context *ctx; |
| 1399 | |
| 1400 | ctx = kzalloc(sizeof(struct zonefs_context), GFP_KERNEL); |
| 1401 | if (!ctx) |
| 1402 | return -ENOMEM; |
| 1403 | ctx->s_mount_opts = ZONEFS_MNTOPT_ERRORS_RO; |
| 1404 | fc->ops = &zonefs_context_ops; |
| 1405 | fc->fs_private = ctx; |
| 1406 | |
| 1407 | return 0; |
| 1408 | } |
| 1409 | |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1410 | /* |
| 1411 | * File system definition and registration. |
| 1412 | */ |
| 1413 | static struct file_system_type zonefs_type = { |
Bill O'Donnell | 567e629 | 2024-02-09 13:36:59 -0600 | [diff] [blame] | 1414 | .owner = THIS_MODULE, |
| 1415 | .name = "zonefs", |
| 1416 | .kill_sb = zonefs_kill_super, |
| 1417 | .fs_flags = FS_REQUIRES_DEV, |
| 1418 | .init_fs_context = zonefs_init_fs_context, |
| 1419 | .parameters = zonefs_param_spec, |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1420 | }; |
| 1421 | |
| 1422 | static int __init zonefs_init_inodecache(void) |
| 1423 | { |
| 1424 | zonefs_inode_cachep = kmem_cache_create("zonefs_inode_cache", |
| 1425 | sizeof(struct zonefs_inode_info), 0, |
Linus Torvalds | f88c3fb | 2024-03-12 20:32:19 -0700 | [diff] [blame] | 1426 | SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT, |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1427 | NULL); |
| 1428 | if (zonefs_inode_cachep == NULL) |
| 1429 | return -ENOMEM; |
| 1430 | return 0; |
| 1431 | } |
| 1432 | |
| 1433 | static void zonefs_destroy_inodecache(void) |
| 1434 | { |
| 1435 | /* |
| 1436 | * Make sure all delayed rcu free inodes are flushed before we |
| 1437 | * destroy the inode cache. |
| 1438 | */ |
| 1439 | rcu_barrier(); |
| 1440 | kmem_cache_destroy(zonefs_inode_cachep); |
| 1441 | } |
| 1442 | |
| 1443 | static int __init zonefs_init(void) |
| 1444 | { |
| 1445 | int ret; |
| 1446 | |
| 1447 | BUILD_BUG_ON(sizeof(struct zonefs_super) != ZONEFS_SUPER_SIZE); |
| 1448 | |
Damien Le Moal | 16d7fd3 | 2023-06-01 17:15:41 +0900 | [diff] [blame] | 1449 | ret = zonefs_init_inodecache(); |
| 1450 | if (ret) |
Damien Le Moal | fe9da61 | 2023-08-07 13:11:48 +0900 | [diff] [blame] | 1451 | return ret; |
Damien Le Moal | 16d7fd3 | 2023-06-01 17:15:41 +0900 | [diff] [blame] | 1452 | |
Zhang Xiaoxu | 4e45886 | 2022-11-20 18:57:59 +0800 | [diff] [blame] | 1453 | ret = zonefs_sysfs_init(); |
Damien Le Moal | 9277a6d | 2022-04-12 16:25:34 +0900 | [diff] [blame] | 1454 | if (ret) |
| 1455 | goto destroy_inodecache; |
| 1456 | |
Zhang Xiaoxu | 4e45886 | 2022-11-20 18:57:59 +0800 | [diff] [blame] | 1457 | ret = register_filesystem(&zonefs_type); |
Damien Le Moal | 9277a6d | 2022-04-12 16:25:34 +0900 | [diff] [blame] | 1458 | if (ret) |
Zhang Xiaoxu | 4e45886 | 2022-11-20 18:57:59 +0800 | [diff] [blame] | 1459 | goto sysfs_exit; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1460 | |
| 1461 | return 0; |
Damien Le Moal | 9277a6d | 2022-04-12 16:25:34 +0900 | [diff] [blame] | 1462 | |
Zhang Xiaoxu | 4e45886 | 2022-11-20 18:57:59 +0800 | [diff] [blame] | 1463 | sysfs_exit: |
| 1464 | zonefs_sysfs_exit(); |
Damien Le Moal | 9277a6d | 2022-04-12 16:25:34 +0900 | [diff] [blame] | 1465 | destroy_inodecache: |
| 1466 | zonefs_destroy_inodecache(); |
| 1467 | |
| 1468 | return ret; |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | static void __exit zonefs_exit(void) |
| 1472 | { |
Zhang Xiaoxu | 4e45886 | 2022-11-20 18:57:59 +0800 | [diff] [blame] | 1473 | unregister_filesystem(&zonefs_type); |
Damien Le Moal | 9277a6d | 2022-04-12 16:25:34 +0900 | [diff] [blame] | 1474 | zonefs_sysfs_exit(); |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1475 | zonefs_destroy_inodecache(); |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | MODULE_AUTHOR("Damien Le Moal"); |
| 1479 | MODULE_DESCRIPTION("Zone file system for zoned block devices"); |
| 1480 | MODULE_LICENSE("GPL"); |
Naohiro Aota | 8ffea25 | 2021-12-17 15:15:45 +0900 | [diff] [blame] | 1481 | MODULE_ALIAS_FS("zonefs"); |
Damien Le Moal | 8dcc1a9 | 2019-12-25 16:07:44 +0900 | [diff] [blame] | 1482 | module_init(zonefs_init); |
| 1483 | module_exit(zonefs_exit); |