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