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