1 // SPDX-License-Identifier: GPL-2.0
3 * Simple file system for zoned block devices exposing zones as files.
5 * Copyright (C) 2019 Western Digital Corporation or its affiliates.
7 #include <linux/module.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>
26 static int zonefs_iomap_begin(struct inode
*inode
, loff_t offset
, loff_t length
,
27 unsigned int flags
, struct iomap
*iomap
,
30 struct zonefs_inode_info
*zi
= ZONEFS_I(inode
);
31 struct super_block
*sb
= inode
->i_sb
;
34 /* All I/Os should always be within the file maximum size */
35 if (WARN_ON_ONCE(offset
+ length
> zi
->i_max_size
))
39 * Sequential zones can only accept direct writes. This is already
40 * checked when writes are issued, so warn if we see a page writeback
43 if (WARN_ON_ONCE(zi
->i_ztype
== ZONEFS_ZTYPE_SEQ
&&
44 (flags
& IOMAP_WRITE
) && !(flags
& IOMAP_DIRECT
)))
48 * For conventional zones, all blocks are always mapped. For sequential
49 * zones, all blocks after always mapped below the inode size (zone
50 * write pointer) and unwriten beyond.
52 mutex_lock(&zi
->i_truncate_mutex
);
53 isize
= i_size_read(inode
);
55 iomap
->type
= IOMAP_UNWRITTEN
;
57 iomap
->type
= IOMAP_MAPPED
;
58 if (flags
& IOMAP_WRITE
)
59 length
= zi
->i_max_size
- offset
;
61 length
= min(length
, isize
- offset
);
62 mutex_unlock(&zi
->i_truncate_mutex
);
64 iomap
->offset
= ALIGN_DOWN(offset
, sb
->s_blocksize
);
65 iomap
->length
= ALIGN(offset
+ length
, sb
->s_blocksize
) - iomap
->offset
;
66 iomap
->bdev
= inode
->i_sb
->s_bdev
;
67 iomap
->addr
= (zi
->i_zsector
<< SECTOR_SHIFT
) + iomap
->offset
;
72 static const struct iomap_ops zonefs_iomap_ops
= {
73 .iomap_begin
= zonefs_iomap_begin
,
76 static int zonefs_readpage(struct file
*unused
, struct page
*page
)
78 return iomap_readpage(page
, &zonefs_iomap_ops
);
81 static int zonefs_readpages(struct file
*unused
, struct address_space
*mapping
,
82 struct list_head
*pages
, unsigned int nr_pages
)
84 return iomap_readpages(mapping
, pages
, nr_pages
, &zonefs_iomap_ops
);
88 * Map blocks for page writeback. This is used only on conventional zone files,
89 * which implies that the page range can only be within the fixed inode size.
91 static int zonefs_map_blocks(struct iomap_writepage_ctx
*wpc
,
92 struct inode
*inode
, loff_t offset
)
94 struct zonefs_inode_info
*zi
= ZONEFS_I(inode
);
96 if (WARN_ON_ONCE(zi
->i_ztype
!= ZONEFS_ZTYPE_CNV
))
98 if (WARN_ON_ONCE(offset
>= i_size_read(inode
)))
101 /* If the mapping is already OK, nothing needs to be done */
102 if (offset
>= wpc
->iomap
.offset
&&
103 offset
< wpc
->iomap
.offset
+ wpc
->iomap
.length
)
106 return zonefs_iomap_begin(inode
, offset
, zi
->i_max_size
- offset
,
107 IOMAP_WRITE
, &wpc
->iomap
, NULL
);
110 static const struct iomap_writeback_ops zonefs_writeback_ops
= {
111 .map_blocks
= zonefs_map_blocks
,
114 static int zonefs_writepage(struct page
*page
, struct writeback_control
*wbc
)
116 struct iomap_writepage_ctx wpc
= { };
118 return iomap_writepage(page
, wbc
, &wpc
, &zonefs_writeback_ops
);
121 static int zonefs_writepages(struct address_space
*mapping
,
122 struct writeback_control
*wbc
)
124 struct iomap_writepage_ctx wpc
= { };
126 return iomap_writepages(mapping
, wbc
, &wpc
, &zonefs_writeback_ops
);
129 static const struct address_space_operations zonefs_file_aops
= {
130 .readpage
= zonefs_readpage
,
131 .readpages
= zonefs_readpages
,
132 .writepage
= zonefs_writepage
,
133 .writepages
= zonefs_writepages
,
134 .set_page_dirty
= iomap_set_page_dirty
,
135 .releasepage
= iomap_releasepage
,
136 .invalidatepage
= iomap_invalidatepage
,
137 .migratepage
= iomap_migrate_page
,
138 .is_partially_uptodate
= iomap_is_partially_uptodate
,
139 .error_remove_page
= generic_error_remove_page
,
140 .direct_IO
= noop_direct_IO
,
143 static void zonefs_update_stats(struct inode
*inode
, loff_t new_isize
)
145 struct super_block
*sb
= inode
->i_sb
;
146 struct zonefs_sb_info
*sbi
= ZONEFS_SB(sb
);
147 loff_t old_isize
= i_size_read(inode
);
150 if (new_isize
== old_isize
)
153 spin_lock(&sbi
->s_lock
);
156 * This may be called for an update after an IO error.
157 * So beware of the values seen.
159 if (new_isize
< old_isize
) {
160 nr_blocks
= (old_isize
- new_isize
) >> sb
->s_blocksize_bits
;
161 if (sbi
->s_used_blocks
> nr_blocks
)
162 sbi
->s_used_blocks
-= nr_blocks
;
164 sbi
->s_used_blocks
= 0;
166 sbi
->s_used_blocks
+=
167 (new_isize
- old_isize
) >> sb
->s_blocksize_bits
;
168 if (sbi
->s_used_blocks
> sbi
->s_blocks
)
169 sbi
->s_used_blocks
= sbi
->s_blocks
;
172 spin_unlock(&sbi
->s_lock
);
176 * Check a zone condition and adjust its file inode access permissions for
177 * offline and readonly zones. Return the inode size corresponding to the
178 * amount of readable data in the zone.
180 static loff_t
zonefs_check_zone_condition(struct inode
*inode
,
181 struct blk_zone
*zone
, bool warn
,
184 struct zonefs_inode_info
*zi
= ZONEFS_I(inode
);
186 switch (zone
->cond
) {
187 case BLK_ZONE_COND_OFFLINE
:
189 * Dead zone: make the inode immutable, disable all accesses
190 * and set the file size to 0 (zone wp set to zone start).
193 zonefs_warn(inode
->i_sb
, "inode %lu: offline zone\n",
195 inode
->i_flags
|= S_IMMUTABLE
;
196 inode
->i_mode
&= ~0777;
197 zone
->wp
= zone
->start
;
199 case BLK_ZONE_COND_READONLY
:
201 * The write pointer of read-only zones is invalid. If such a
202 * zone is found during mount, the file size cannot be retrieved
203 * so we treat the zone as offline (mount == true case).
204 * Otherwise, keep the file size as it was when last updated
205 * so that the user can recover data. In both cases, writes are
206 * always disabled for the zone.
209 zonefs_warn(inode
->i_sb
, "inode %lu: read-only zone\n",
211 inode
->i_flags
|= S_IMMUTABLE
;
213 zone
->cond
= BLK_ZONE_COND_OFFLINE
;
214 inode
->i_mode
&= ~0777;
215 zone
->wp
= zone
->start
;
218 inode
->i_mode
&= ~0222;
219 return i_size_read(inode
);
221 if (zi
->i_ztype
== ZONEFS_ZTYPE_CNV
)
222 return zi
->i_max_size
;
223 return (zone
->wp
- zone
->start
) << SECTOR_SHIFT
;
227 struct zonefs_ioerr_data
{
232 static int zonefs_io_error_cb(struct blk_zone
*zone
, unsigned int idx
,
235 struct zonefs_ioerr_data
*err
= data
;
236 struct inode
*inode
= err
->inode
;
237 struct zonefs_inode_info
*zi
= ZONEFS_I(inode
);
238 struct super_block
*sb
= inode
->i_sb
;
239 struct zonefs_sb_info
*sbi
= ZONEFS_SB(sb
);
240 loff_t isize
, data_size
;
243 * Check the zone condition: if the zone is not "bad" (offline or
244 * read-only), read errors are simply signaled to the IO issuer as long
245 * as there is no inconsistency between the inode size and the amount of
246 * data writen in the zone (data_size).
248 data_size
= zonefs_check_zone_condition(inode
, zone
, true, false);
249 isize
= i_size_read(inode
);
250 if (zone
->cond
!= BLK_ZONE_COND_OFFLINE
&&
251 zone
->cond
!= BLK_ZONE_COND_READONLY
&&
252 !err
->write
&& isize
== data_size
)
256 * At this point, we detected either a bad zone or an inconsistency
257 * between the inode size and the amount of data written in the zone.
258 * For the latter case, the cause may be a write IO error or an external
259 * action on the device. Two error patterns exist:
260 * 1) The inode size is lower than the amount of data in the zone:
261 * a write operation partially failed and data was writen at the end
262 * of the file. This can happen in the case of a large direct IO
263 * needing several BIOs and/or write requests to be processed.
264 * 2) The inode size is larger than the amount of data in the zone:
265 * this can happen with a deferred write error with the use of the
266 * device side write cache after getting successful write IO
267 * completions. Other possibilities are (a) an external corruption,
268 * e.g. an application reset the zone directly, or (b) the device
269 * has a serious problem (e.g. firmware bug).
271 * In all cases, warn about inode size inconsistency and handle the
272 * IO error according to the zone condition and to the mount options.
274 if (zi
->i_ztype
== ZONEFS_ZTYPE_SEQ
&& isize
!= data_size
)
275 zonefs_warn(sb
, "inode %lu: invalid size %lld (should be %lld)\n",
276 inode
->i_ino
, isize
, data_size
);
279 * First handle bad zones signaled by hardware. The mount options
280 * errors=zone-ro and errors=zone-offline result in changing the
281 * zone condition to read-only and offline respectively, as if the
282 * condition was signaled by the hardware.
284 if (zone
->cond
== BLK_ZONE_COND_OFFLINE
||
285 sbi
->s_mount_opts
& ZONEFS_MNTOPT_ERRORS_ZOL
) {
286 zonefs_warn(sb
, "inode %lu: read/write access disabled\n",
288 if (zone
->cond
!= BLK_ZONE_COND_OFFLINE
) {
289 zone
->cond
= BLK_ZONE_COND_OFFLINE
;
290 data_size
= zonefs_check_zone_condition(inode
, zone
,
293 } else if (zone
->cond
== BLK_ZONE_COND_READONLY
||
294 sbi
->s_mount_opts
& ZONEFS_MNTOPT_ERRORS_ZRO
) {
295 zonefs_warn(sb
, "inode %lu: write access disabled\n",
297 if (zone
->cond
!= BLK_ZONE_COND_READONLY
) {
298 zone
->cond
= BLK_ZONE_COND_READONLY
;
299 data_size
= zonefs_check_zone_condition(inode
, zone
,
305 * If error=remount-ro was specified, any error result in remounting
306 * the volume as read-only.
308 if ((sbi
->s_mount_opts
& ZONEFS_MNTOPT_ERRORS_RO
) && !sb_rdonly(sb
)) {
309 zonefs_warn(sb
, "remounting filesystem read-only\n");
310 sb
->s_flags
|= SB_RDONLY
;
314 * Update block usage stats and the inode size to prevent access to
317 zonefs_update_stats(inode
, data_size
);
318 i_size_write(inode
, data_size
);
319 zi
->i_wpoffset
= data_size
;
325 * When an file IO error occurs, check the file zone to see if there is a change
326 * in the zone condition (e.g. offline or read-only). For a failed write to a
327 * sequential zone, the zone write pointer position must also be checked to
328 * eventually correct the file size and zonefs inode write pointer offset
329 * (which can be out of sync with the drive due to partial write failures).
331 static void zonefs_io_error(struct inode
*inode
, bool write
)
333 struct zonefs_inode_info
*zi
= ZONEFS_I(inode
);
334 struct super_block
*sb
= inode
->i_sb
;
335 struct zonefs_sb_info
*sbi
= ZONEFS_SB(sb
);
336 unsigned int noio_flag
;
337 unsigned int nr_zones
=
338 zi
->i_max_size
>> (sbi
->s_zone_sectors_shift
+ SECTOR_SHIFT
);
339 struct zonefs_ioerr_data err
= {
345 mutex_lock(&zi
->i_truncate_mutex
);
348 * Memory allocations in blkdev_report_zones() can trigger a memory
349 * reclaim which may in turn cause a recursion into zonefs as well as
350 * struct request allocations for the same device. The former case may
351 * end up in a deadlock on the inode truncate mutex, while the latter
352 * may prevent IO forward progress. Executing the report zones under
353 * the GFP_NOIO context avoids both problems.
355 noio_flag
= memalloc_noio_save();
356 ret
= blkdev_report_zones(sb
->s_bdev
, zi
->i_zsector
, nr_zones
,
357 zonefs_io_error_cb
, &err
);
359 zonefs_err(sb
, "Get inode %lu zone information failed %d\n",
361 memalloc_noio_restore(noio_flag
);
363 mutex_unlock(&zi
->i_truncate_mutex
);
366 static int zonefs_file_truncate(struct inode
*inode
, loff_t isize
)
368 struct zonefs_inode_info
*zi
= ZONEFS_I(inode
);
374 * Only sequential zone files can be truncated and truncation is allowed
375 * only down to a 0 size, which is equivalent to a zone reset, and to
376 * the maximum file size, which is equivalent to a zone finish.
378 if (zi
->i_ztype
!= ZONEFS_ZTYPE_SEQ
)
382 op
= REQ_OP_ZONE_RESET
;
383 else if (isize
== zi
->i_max_size
)
384 op
= REQ_OP_ZONE_FINISH
;
388 inode_dio_wait(inode
);
390 /* Serialize against page faults */
391 down_write(&zi
->i_mmap_sem
);
393 /* Serialize against zonefs_iomap_begin() */
394 mutex_lock(&zi
->i_truncate_mutex
);
396 old_isize
= i_size_read(inode
);
397 if (isize
== old_isize
)
400 ret
= blkdev_zone_mgmt(inode
->i_sb
->s_bdev
, op
, zi
->i_zsector
,
401 zi
->i_max_size
>> SECTOR_SHIFT
, GFP_NOFS
);
403 zonefs_err(inode
->i_sb
,
404 "Zone management operation at %llu failed %d",
409 zonefs_update_stats(inode
, isize
);
410 truncate_setsize(inode
, isize
);
411 zi
->i_wpoffset
= isize
;
414 mutex_unlock(&zi
->i_truncate_mutex
);
415 up_write(&zi
->i_mmap_sem
);
420 static int zonefs_inode_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
422 struct inode
*inode
= d_inode(dentry
);
425 if (unlikely(IS_IMMUTABLE(inode
)))
428 ret
= setattr_prepare(dentry
, iattr
);
433 * Since files and directories cannot be created nor deleted, do not
434 * allow setting any write attributes on the sub-directories grouping
435 * files by zone type.
437 if ((iattr
->ia_valid
& ATTR_MODE
) && S_ISDIR(inode
->i_mode
) &&
438 (iattr
->ia_mode
& 0222))
441 if (((iattr
->ia_valid
& ATTR_UID
) &&
442 !uid_eq(iattr
->ia_uid
, inode
->i_uid
)) ||
443 ((iattr
->ia_valid
& ATTR_GID
) &&
444 !gid_eq(iattr
->ia_gid
, inode
->i_gid
))) {
445 ret
= dquot_transfer(inode
, iattr
);
450 if (iattr
->ia_valid
& ATTR_SIZE
) {
451 ret
= zonefs_file_truncate(inode
, iattr
->ia_size
);
456 setattr_copy(inode
, iattr
);
461 static const struct inode_operations zonefs_file_inode_operations
= {
462 .setattr
= zonefs_inode_setattr
,
465 static int zonefs_file_fsync(struct file
*file
, loff_t start
, loff_t end
,
468 struct inode
*inode
= file_inode(file
);
471 if (unlikely(IS_IMMUTABLE(inode
)))
475 * Since only direct writes are allowed in sequential files, page cache
476 * flush is needed only for conventional zone files.
478 if (ZONEFS_I(inode
)->i_ztype
== ZONEFS_ZTYPE_CNV
)
479 ret
= file_write_and_wait_range(file
, start
, end
);
481 ret
= blkdev_issue_flush(inode
->i_sb
->s_bdev
, GFP_KERNEL
, NULL
);
484 zonefs_io_error(inode
, true);
489 static vm_fault_t
zonefs_filemap_fault(struct vm_fault
*vmf
)
491 struct zonefs_inode_info
*zi
= ZONEFS_I(file_inode(vmf
->vma
->vm_file
));
494 down_read(&zi
->i_mmap_sem
);
495 ret
= filemap_fault(vmf
);
496 up_read(&zi
->i_mmap_sem
);
501 static vm_fault_t
zonefs_filemap_page_mkwrite(struct vm_fault
*vmf
)
503 struct inode
*inode
= file_inode(vmf
->vma
->vm_file
);
504 struct zonefs_inode_info
*zi
= ZONEFS_I(inode
);
507 if (unlikely(IS_IMMUTABLE(inode
)))
508 return VM_FAULT_SIGBUS
;
511 * Sanity check: only conventional zone files can have shared
512 * writeable mappings.
514 if (WARN_ON_ONCE(zi
->i_ztype
!= ZONEFS_ZTYPE_CNV
))
515 return VM_FAULT_NOPAGE
;
517 sb_start_pagefault(inode
->i_sb
);
518 file_update_time(vmf
->vma
->vm_file
);
520 /* Serialize against truncates */
521 down_read(&zi
->i_mmap_sem
);
522 ret
= iomap_page_mkwrite(vmf
, &zonefs_iomap_ops
);
523 up_read(&zi
->i_mmap_sem
);
525 sb_end_pagefault(inode
->i_sb
);
529 static const struct vm_operations_struct zonefs_file_vm_ops
= {
530 .fault
= zonefs_filemap_fault
,
531 .map_pages
= filemap_map_pages
,
532 .page_mkwrite
= zonefs_filemap_page_mkwrite
,
535 static int zonefs_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
538 * Conventional zones accept random writes, so their files can support
539 * shared writable mappings. For sequential zone files, only read
540 * mappings are possible since there are no guarantees for write
541 * ordering between msync() and page cache writeback.
543 if (ZONEFS_I(file_inode(file
))->i_ztype
== ZONEFS_ZTYPE_SEQ
&&
544 (vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
))
548 vma
->vm_ops
= &zonefs_file_vm_ops
;
553 static loff_t
zonefs_file_llseek(struct file
*file
, loff_t offset
, int whence
)
555 loff_t isize
= i_size_read(file_inode(file
));
558 * Seeks are limited to below the zone size for conventional zones
559 * and below the zone write pointer for sequential zones. In both
560 * cases, this limit is the inode size.
562 return generic_file_llseek_size(file
, offset
, whence
, isize
, isize
);
565 static int zonefs_file_write_dio_end_io(struct kiocb
*iocb
, ssize_t size
,
566 int error
, unsigned int flags
)
568 struct inode
*inode
= file_inode(iocb
->ki_filp
);
569 struct zonefs_inode_info
*zi
= ZONEFS_I(inode
);
572 zonefs_io_error(inode
, true);
576 if (size
&& zi
->i_ztype
!= ZONEFS_ZTYPE_CNV
) {
578 * Note that we may be seeing completions out of order,
579 * but that is not a problem since a write completed
580 * successfully necessarily means that all preceding writes
581 * were also successful. So we can safely increase the inode
582 * size to the write end location.
584 mutex_lock(&zi
->i_truncate_mutex
);
585 if (i_size_read(inode
) < iocb
->ki_pos
+ size
) {
586 zonefs_update_stats(inode
, iocb
->ki_pos
+ size
);
587 i_size_write(inode
, iocb
->ki_pos
+ size
);
589 mutex_unlock(&zi
->i_truncate_mutex
);
595 static const struct iomap_dio_ops zonefs_write_dio_ops
= {
596 .end_io
= zonefs_file_write_dio_end_io
,
600 * Handle direct writes. For sequential zone files, this is the only possible
601 * write path. For these files, check that the user is issuing writes
602 * sequentially from the end of the file. This code assumes that the block layer
603 * delivers write requests to the device in sequential order. This is always the
604 * case if a block IO scheduler implementing the ELEVATOR_F_ZBD_SEQ_WRITE
605 * elevator feature is being used (e.g. mq-deadline). The block layer always
606 * automatically select such an elevator for zoned block devices during the
607 * device initialization.
609 static ssize_t
zonefs_file_dio_write(struct kiocb
*iocb
, struct iov_iter
*from
)
611 struct inode
*inode
= file_inode(iocb
->ki_filp
);
612 struct zonefs_inode_info
*zi
= ZONEFS_I(inode
);
613 struct super_block
*sb
= inode
->i_sb
;
618 * For async direct IOs to sequential zone files, refuse IOCB_NOWAIT
619 * as this can cause write reordering (e.g. the first aio gets EAGAIN
620 * on the inode lock but the second goes through but is now unaligned).
622 if (zi
->i_ztype
== ZONEFS_ZTYPE_SEQ
&& !is_sync_kiocb(iocb
) &&
623 (iocb
->ki_flags
& IOCB_NOWAIT
))
626 if (iocb
->ki_flags
& IOCB_NOWAIT
) {
627 if (!inode_trylock(inode
))
633 ret
= generic_write_checks(iocb
, from
);
637 iov_iter_truncate(from
, zi
->i_max_size
- iocb
->ki_pos
);
638 count
= iov_iter_count(from
);
640 if ((iocb
->ki_pos
| count
) & (sb
->s_blocksize
- 1)) {
645 /* Enforce sequential writes (append only) in sequential zones */
646 mutex_lock(&zi
->i_truncate_mutex
);
647 if (zi
->i_ztype
== ZONEFS_ZTYPE_SEQ
&& iocb
->ki_pos
!= zi
->i_wpoffset
) {
648 mutex_unlock(&zi
->i_truncate_mutex
);
652 mutex_unlock(&zi
->i_truncate_mutex
);
654 ret
= iomap_dio_rw(iocb
, from
, &zonefs_iomap_ops
,
655 &zonefs_write_dio_ops
, is_sync_kiocb(iocb
));
656 if (zi
->i_ztype
== ZONEFS_ZTYPE_SEQ
&&
657 (ret
> 0 || ret
== -EIOCBQUEUED
)) {
660 mutex_lock(&zi
->i_truncate_mutex
);
661 zi
->i_wpoffset
+= count
;
662 mutex_unlock(&zi
->i_truncate_mutex
);
671 static ssize_t
zonefs_file_buffered_write(struct kiocb
*iocb
,
672 struct iov_iter
*from
)
674 struct inode
*inode
= file_inode(iocb
->ki_filp
);
675 struct zonefs_inode_info
*zi
= ZONEFS_I(inode
);
679 * Direct IO writes are mandatory for sequential zone files so that the
680 * write IO issuing order is preserved.
682 if (zi
->i_ztype
!= ZONEFS_ZTYPE_CNV
)
685 if (iocb
->ki_flags
& IOCB_NOWAIT
) {
686 if (!inode_trylock(inode
))
692 ret
= generic_write_checks(iocb
, from
);
696 iov_iter_truncate(from
, zi
->i_max_size
- iocb
->ki_pos
);
698 ret
= iomap_file_buffered_write(iocb
, from
, &zonefs_iomap_ops
);
701 else if (ret
== -EIO
)
702 zonefs_io_error(inode
, true);
707 ret
= generic_write_sync(iocb
, ret
);
712 static ssize_t
zonefs_file_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
714 struct inode
*inode
= file_inode(iocb
->ki_filp
);
716 if (unlikely(IS_IMMUTABLE(inode
)))
719 if (sb_rdonly(inode
->i_sb
))
722 /* Write operations beyond the zone size are not allowed */
723 if (iocb
->ki_pos
>= ZONEFS_I(inode
)->i_max_size
)
726 if (iocb
->ki_flags
& IOCB_DIRECT
)
727 return zonefs_file_dio_write(iocb
, from
);
729 return zonefs_file_buffered_write(iocb
, from
);
732 static int zonefs_file_read_dio_end_io(struct kiocb
*iocb
, ssize_t size
,
733 int error
, unsigned int flags
)
736 zonefs_io_error(file_inode(iocb
->ki_filp
), false);
743 static const struct iomap_dio_ops zonefs_read_dio_ops
= {
744 .end_io
= zonefs_file_read_dio_end_io
,
747 static ssize_t
zonefs_file_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
749 struct inode
*inode
= file_inode(iocb
->ki_filp
);
750 struct zonefs_inode_info
*zi
= ZONEFS_I(inode
);
751 struct super_block
*sb
= inode
->i_sb
;
755 /* Offline zones cannot be read */
756 if (unlikely(IS_IMMUTABLE(inode
) && !(inode
->i_mode
& 0777)))
759 if (iocb
->ki_pos
>= zi
->i_max_size
)
762 if (iocb
->ki_flags
& IOCB_NOWAIT
) {
763 if (!inode_trylock_shared(inode
))
766 inode_lock_shared(inode
);
769 /* Limit read operations to written data */
770 mutex_lock(&zi
->i_truncate_mutex
);
771 isize
= i_size_read(inode
);
772 if (iocb
->ki_pos
>= isize
) {
773 mutex_unlock(&zi
->i_truncate_mutex
);
777 iov_iter_truncate(to
, isize
- iocb
->ki_pos
);
778 mutex_unlock(&zi
->i_truncate_mutex
);
780 if (iocb
->ki_flags
& IOCB_DIRECT
) {
781 size_t count
= iov_iter_count(to
);
783 if ((iocb
->ki_pos
| count
) & (sb
->s_blocksize
- 1)) {
787 file_accessed(iocb
->ki_filp
);
788 ret
= iomap_dio_rw(iocb
, to
, &zonefs_iomap_ops
,
789 &zonefs_read_dio_ops
, is_sync_kiocb(iocb
));
791 ret
= generic_file_read_iter(iocb
, to
);
793 zonefs_io_error(inode
, false);
797 inode_unlock_shared(inode
);
802 static const struct file_operations zonefs_file_operations
= {
803 .open
= generic_file_open
,
804 .fsync
= zonefs_file_fsync
,
805 .mmap
= zonefs_file_mmap
,
806 .llseek
= zonefs_file_llseek
,
807 .read_iter
= zonefs_file_read_iter
,
808 .write_iter
= zonefs_file_write_iter
,
809 .splice_read
= generic_file_splice_read
,
810 .splice_write
= iter_file_splice_write
,
811 .iopoll
= iomap_dio_iopoll
,
814 static struct kmem_cache
*zonefs_inode_cachep
;
816 static struct inode
*zonefs_alloc_inode(struct super_block
*sb
)
818 struct zonefs_inode_info
*zi
;
820 zi
= kmem_cache_alloc(zonefs_inode_cachep
, GFP_KERNEL
);
824 inode_init_once(&zi
->i_vnode
);
825 mutex_init(&zi
->i_truncate_mutex
);
826 init_rwsem(&zi
->i_mmap_sem
);
831 static void zonefs_free_inode(struct inode
*inode
)
833 kmem_cache_free(zonefs_inode_cachep
, ZONEFS_I(inode
));
839 static int zonefs_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
841 struct super_block
*sb
= dentry
->d_sb
;
842 struct zonefs_sb_info
*sbi
= ZONEFS_SB(sb
);
846 buf
->f_type
= ZONEFS_MAGIC
;
847 buf
->f_bsize
= sb
->s_blocksize
;
848 buf
->f_namelen
= ZONEFS_NAME_MAX
;
850 spin_lock(&sbi
->s_lock
);
852 buf
->f_blocks
= sbi
->s_blocks
;
853 if (WARN_ON(sbi
->s_used_blocks
> sbi
->s_blocks
))
856 buf
->f_bfree
= buf
->f_blocks
- sbi
->s_used_blocks
;
857 buf
->f_bavail
= buf
->f_bfree
;
859 for (t
= 0; t
< ZONEFS_ZTYPE_MAX
; t
++) {
860 if (sbi
->s_nr_files
[t
])
861 buf
->f_files
+= sbi
->s_nr_files
[t
] + 1;
865 spin_unlock(&sbi
->s_lock
);
867 fsid
= le64_to_cpup((void *)sbi
->s_uuid
.b
) ^
868 le64_to_cpup((void *)sbi
->s_uuid
.b
+ sizeof(u64
));
869 buf
->f_fsid
.val
[0] = (u32
)fsid
;
870 buf
->f_fsid
.val
[1] = (u32
)(fsid
>> 32);
876 Opt_errors_ro
, Opt_errors_zro
, Opt_errors_zol
, Opt_errors_repair
,
880 static const match_table_t tokens
= {
881 { Opt_errors_ro
, "errors=remount-ro"},
882 { Opt_errors_zro
, "errors=zone-ro"},
883 { Opt_errors_zol
, "errors=zone-offline"},
884 { Opt_errors_repair
, "errors=repair"},
888 static int zonefs_parse_options(struct super_block
*sb
, char *options
)
890 struct zonefs_sb_info
*sbi
= ZONEFS_SB(sb
);
891 substring_t args
[MAX_OPT_ARGS
];
897 while ((p
= strsep(&options
, ",")) != NULL
) {
903 token
= match_token(p
, tokens
, args
);
906 sbi
->s_mount_opts
&= ~ZONEFS_MNTOPT_ERRORS_MASK
;
907 sbi
->s_mount_opts
|= ZONEFS_MNTOPT_ERRORS_RO
;
910 sbi
->s_mount_opts
&= ~ZONEFS_MNTOPT_ERRORS_MASK
;
911 sbi
->s_mount_opts
|= ZONEFS_MNTOPT_ERRORS_ZRO
;
914 sbi
->s_mount_opts
&= ~ZONEFS_MNTOPT_ERRORS_MASK
;
915 sbi
->s_mount_opts
|= ZONEFS_MNTOPT_ERRORS_ZOL
;
917 case Opt_errors_repair
:
918 sbi
->s_mount_opts
&= ~ZONEFS_MNTOPT_ERRORS_MASK
;
919 sbi
->s_mount_opts
|= ZONEFS_MNTOPT_ERRORS_REPAIR
;
929 static int zonefs_show_options(struct seq_file
*seq
, struct dentry
*root
)
931 struct zonefs_sb_info
*sbi
= ZONEFS_SB(root
->d_sb
);
933 if (sbi
->s_mount_opts
& ZONEFS_MNTOPT_ERRORS_RO
)
934 seq_puts(seq
, ",errors=remount-ro");
935 if (sbi
->s_mount_opts
& ZONEFS_MNTOPT_ERRORS_ZRO
)
936 seq_puts(seq
, ",errors=zone-ro");
937 if (sbi
->s_mount_opts
& ZONEFS_MNTOPT_ERRORS_ZOL
)
938 seq_puts(seq
, ",errors=zone-offline");
939 if (sbi
->s_mount_opts
& ZONEFS_MNTOPT_ERRORS_REPAIR
)
940 seq_puts(seq
, ",errors=repair");
945 static int zonefs_remount(struct super_block
*sb
, int *flags
, char *data
)
949 return zonefs_parse_options(sb
, data
);
952 static const struct super_operations zonefs_sops
= {
953 .alloc_inode
= zonefs_alloc_inode
,
954 .free_inode
= zonefs_free_inode
,
955 .statfs
= zonefs_statfs
,
956 .remount_fs
= zonefs_remount
,
957 .show_options
= zonefs_show_options
,
960 static const struct inode_operations zonefs_dir_inode_operations
= {
961 .lookup
= simple_lookup
,
962 .setattr
= zonefs_inode_setattr
,
965 static void zonefs_init_dir_inode(struct inode
*parent
, struct inode
*inode
,
966 enum zonefs_ztype type
)
968 struct super_block
*sb
= parent
->i_sb
;
970 inode
->i_ino
= blkdev_nr_zones(sb
->s_bdev
->bd_disk
) + type
+ 1;
971 inode_init_owner(inode
, parent
, S_IFDIR
| 0555);
972 inode
->i_op
= &zonefs_dir_inode_operations
;
973 inode
->i_fop
= &simple_dir_operations
;
978 static void zonefs_init_file_inode(struct inode
*inode
, struct blk_zone
*zone
,
979 enum zonefs_ztype type
)
981 struct super_block
*sb
= inode
->i_sb
;
982 struct zonefs_sb_info
*sbi
= ZONEFS_SB(sb
);
983 struct zonefs_inode_info
*zi
= ZONEFS_I(inode
);
985 inode
->i_ino
= zone
->start
>> sbi
->s_zone_sectors_shift
;
986 inode
->i_mode
= S_IFREG
| sbi
->s_perm
;
989 zi
->i_zsector
= zone
->start
;
990 zi
->i_max_size
= min_t(loff_t
, MAX_LFS_FILESIZE
,
991 zone
->len
<< SECTOR_SHIFT
);
992 zi
->i_wpoffset
= zonefs_check_zone_condition(inode
, zone
, true, true);
994 inode
->i_uid
= sbi
->s_uid
;
995 inode
->i_gid
= sbi
->s_gid
;
996 inode
->i_size
= zi
->i_wpoffset
;
997 inode
->i_blocks
= zone
->len
;
999 inode
->i_op
= &zonefs_file_inode_operations
;
1000 inode
->i_fop
= &zonefs_file_operations
;
1001 inode
->i_mapping
->a_ops
= &zonefs_file_aops
;
1003 sb
->s_maxbytes
= max(zi
->i_max_size
, sb
->s_maxbytes
);
1004 sbi
->s_blocks
+= zi
->i_max_size
>> sb
->s_blocksize_bits
;
1005 sbi
->s_used_blocks
+= zi
->i_wpoffset
>> sb
->s_blocksize_bits
;
1008 static struct dentry
*zonefs_create_inode(struct dentry
*parent
,
1009 const char *name
, struct blk_zone
*zone
,
1010 enum zonefs_ztype type
)
1012 struct inode
*dir
= d_inode(parent
);
1013 struct dentry
*dentry
;
1014 struct inode
*inode
;
1016 dentry
= d_alloc_name(parent
, name
);
1020 inode
= new_inode(parent
->d_sb
);
1024 inode
->i_ctime
= inode
->i_mtime
= inode
->i_atime
= dir
->i_ctime
;
1026 zonefs_init_file_inode(inode
, zone
, type
);
1028 zonefs_init_dir_inode(dir
, inode
, type
);
1029 d_add(dentry
, inode
);
1040 struct zonefs_zone_data
{
1041 struct super_block
*sb
;
1042 unsigned int nr_zones
[ZONEFS_ZTYPE_MAX
];
1043 struct blk_zone
*zones
;
1047 * Create a zone group and populate it with zone files.
1049 static int zonefs_create_zgroup(struct zonefs_zone_data
*zd
,
1050 enum zonefs_ztype type
)
1052 struct super_block
*sb
= zd
->sb
;
1053 struct zonefs_sb_info
*sbi
= ZONEFS_SB(sb
);
1054 struct blk_zone
*zone
, *next
, *end
;
1055 const char *zgroup_name
;
1061 /* If the group is empty, there is nothing to do */
1062 if (!zd
->nr_zones
[type
])
1065 file_name
= kmalloc(ZONEFS_NAME_MAX
, GFP_KERNEL
);
1069 if (type
== ZONEFS_ZTYPE_CNV
)
1070 zgroup_name
= "cnv";
1072 zgroup_name
= "seq";
1074 dir
= zonefs_create_inode(sb
->s_root
, zgroup_name
, NULL
, type
);
1079 * The first zone contains the super block: skip it.
1081 end
= zd
->zones
+ blkdev_nr_zones(sb
->s_bdev
->bd_disk
);
1082 for (zone
= &zd
->zones
[1]; zone
< end
; zone
= next
) {
1085 if (zonefs_zone_type(zone
) != type
)
1089 * For conventional zones, contiguous zones can be aggregated
1090 * together to form larger files. Note that this overwrites the
1091 * length of the first zone of the set of contiguous zones
1092 * aggregated together. If one offline or read-only zone is
1093 * found, assume that all zones aggregated have the same
1096 if (type
== ZONEFS_ZTYPE_CNV
&&
1097 (sbi
->s_features
& ZONEFS_F_AGGRCNV
)) {
1098 for (; next
< end
; next
++) {
1099 if (zonefs_zone_type(next
) != type
)
1101 zone
->len
+= next
->len
;
1102 if (next
->cond
== BLK_ZONE_COND_READONLY
&&
1103 zone
->cond
!= BLK_ZONE_COND_OFFLINE
)
1104 zone
->cond
= BLK_ZONE_COND_READONLY
;
1105 else if (next
->cond
== BLK_ZONE_COND_OFFLINE
)
1106 zone
->cond
= BLK_ZONE_COND_OFFLINE
;
1111 * Use the file number within its group as file name.
1113 snprintf(file_name
, ZONEFS_NAME_MAX
- 1, "%u", n
);
1114 if (!zonefs_create_inode(dir
, file_name
, zone
, type
))
1120 zonefs_info(sb
, "Zone group \"%s\" has %u file%s\n",
1121 zgroup_name
, n
, n
> 1 ? "s" : "");
1123 sbi
->s_nr_files
[type
] = n
;
1132 static int zonefs_get_zone_info_cb(struct blk_zone
*zone
, unsigned int idx
,
1135 struct zonefs_zone_data
*zd
= data
;
1138 * Count the number of usable zones: the first zone at index 0 contains
1139 * the super block and is ignored.
1141 switch (zone
->type
) {
1142 case BLK_ZONE_TYPE_CONVENTIONAL
:
1143 zone
->wp
= zone
->start
+ zone
->len
;
1145 zd
->nr_zones
[ZONEFS_ZTYPE_CNV
]++;
1147 case BLK_ZONE_TYPE_SEQWRITE_REQ
:
1148 case BLK_ZONE_TYPE_SEQWRITE_PREF
:
1150 zd
->nr_zones
[ZONEFS_ZTYPE_SEQ
]++;
1153 zonefs_err(zd
->sb
, "Unsupported zone type 0x%x\n",
1158 memcpy(&zd
->zones
[idx
], zone
, sizeof(struct blk_zone
));
1163 static int zonefs_get_zone_info(struct zonefs_zone_data
*zd
)
1165 struct block_device
*bdev
= zd
->sb
->s_bdev
;
1168 zd
->zones
= kvcalloc(blkdev_nr_zones(bdev
->bd_disk
),
1169 sizeof(struct blk_zone
), GFP_KERNEL
);
1173 /* Get zones information from the device */
1174 ret
= blkdev_report_zones(bdev
, 0, BLK_ALL_ZONES
,
1175 zonefs_get_zone_info_cb
, zd
);
1177 zonefs_err(zd
->sb
, "Zone report failed %d\n", ret
);
1181 if (ret
!= blkdev_nr_zones(bdev
->bd_disk
)) {
1182 zonefs_err(zd
->sb
, "Invalid zone report (%d/%u zones)\n",
1183 ret
, blkdev_nr_zones(bdev
->bd_disk
));
1190 static inline void zonefs_cleanup_zone_info(struct zonefs_zone_data
*zd
)
1196 * Read super block information from the device.
1198 static int zonefs_read_super(struct super_block
*sb
)
1200 struct zonefs_sb_info
*sbi
= ZONEFS_SB(sb
);
1201 struct zonefs_super
*super
;
1202 u32 crc
, stored_crc
;
1204 struct bio_vec bio_vec
;
1208 page
= alloc_page(GFP_KERNEL
);
1212 bio_init(&bio
, &bio_vec
, 1);
1213 bio
.bi_iter
.bi_sector
= 0;
1214 bio
.bi_opf
= REQ_OP_READ
;
1215 bio_set_dev(&bio
, sb
->s_bdev
);
1216 bio_add_page(&bio
, page
, PAGE_SIZE
, 0);
1218 ret
= submit_bio_wait(&bio
);
1225 if (le32_to_cpu(super
->s_magic
) != ZONEFS_MAGIC
)
1228 stored_crc
= le32_to_cpu(super
->s_crc
);
1230 crc
= crc32(~0U, (unsigned char *)super
, sizeof(struct zonefs_super
));
1231 if (crc
!= stored_crc
) {
1232 zonefs_err(sb
, "Invalid checksum (Expected 0x%08x, got 0x%08x)",
1237 sbi
->s_features
= le64_to_cpu(super
->s_features
);
1238 if (sbi
->s_features
& ~ZONEFS_F_DEFINED_FEATURES
) {
1239 zonefs_err(sb
, "Unknown features set 0x%llx\n",
1244 if (sbi
->s_features
& ZONEFS_F_UID
) {
1245 sbi
->s_uid
= make_kuid(current_user_ns(),
1246 le32_to_cpu(super
->s_uid
));
1247 if (!uid_valid(sbi
->s_uid
)) {
1248 zonefs_err(sb
, "Invalid UID feature\n");
1253 if (sbi
->s_features
& ZONEFS_F_GID
) {
1254 sbi
->s_gid
= make_kgid(current_user_ns(),
1255 le32_to_cpu(super
->s_gid
));
1256 if (!gid_valid(sbi
->s_gid
)) {
1257 zonefs_err(sb
, "Invalid GID feature\n");
1262 if (sbi
->s_features
& ZONEFS_F_PERM
)
1263 sbi
->s_perm
= le32_to_cpu(super
->s_perm
);
1265 if (memchr_inv(super
->s_reserved
, 0, sizeof(super
->s_reserved
))) {
1266 zonefs_err(sb
, "Reserved area is being used\n");
1270 uuid_copy(&sbi
->s_uuid
, (uuid_t
*)super
->s_uuid
);
1282 * Check that the device is zoned. If it is, get the list of zones and create
1283 * sub-directories and files according to the device zone configuration and
1286 static int zonefs_fill_super(struct super_block
*sb
, void *data
, int silent
)
1288 struct zonefs_zone_data zd
;
1289 struct zonefs_sb_info
*sbi
;
1290 struct inode
*inode
;
1291 enum zonefs_ztype t
;
1294 if (!bdev_is_zoned(sb
->s_bdev
)) {
1295 zonefs_err(sb
, "Not a zoned block device\n");
1300 * Initialize super block information: the maximum file size is updated
1301 * when the zone files are created so that the format option
1302 * ZONEFS_F_AGGRCNV which increases the maximum file size of a file
1303 * beyond the zone size is taken into account.
1305 sbi
= kzalloc(sizeof(*sbi
), GFP_KERNEL
);
1309 spin_lock_init(&sbi
->s_lock
);
1310 sb
->s_fs_info
= sbi
;
1311 sb
->s_magic
= ZONEFS_MAGIC
;
1313 sb
->s_op
= &zonefs_sops
;
1314 sb
->s_time_gran
= 1;
1317 * The block size is set to the device physical sector size to ensure
1318 * that write operations on 512e devices (512B logical block and 4KB
1319 * physical block) are always aligned to the device physical blocks,
1320 * as mandated by the ZBC/ZAC specifications.
1322 sb_set_blocksize(sb
, bdev_physical_block_size(sb
->s_bdev
));
1323 sbi
->s_zone_sectors_shift
= ilog2(bdev_zone_sectors(sb
->s_bdev
));
1324 sbi
->s_uid
= GLOBAL_ROOT_UID
;
1325 sbi
->s_gid
= GLOBAL_ROOT_GID
;
1327 sbi
->s_mount_opts
= ZONEFS_MNTOPT_ERRORS_RO
;
1329 ret
= zonefs_read_super(sb
);
1333 ret
= zonefs_parse_options(sb
, data
);
1337 memset(&zd
, 0, sizeof(struct zonefs_zone_data
));
1339 ret
= zonefs_get_zone_info(&zd
);
1343 zonefs_info(sb
, "Mounting %u zones",
1344 blkdev_nr_zones(sb
->s_bdev
->bd_disk
));
1346 /* Create root directory inode */
1348 inode
= new_inode(sb
);
1352 inode
->i_ino
= blkdev_nr_zones(sb
->s_bdev
->bd_disk
);
1353 inode
->i_mode
= S_IFDIR
| 0555;
1354 inode
->i_ctime
= inode
->i_mtime
= inode
->i_atime
= current_time(inode
);
1355 inode
->i_op
= &zonefs_dir_inode_operations
;
1356 inode
->i_fop
= &simple_dir_operations
;
1357 set_nlink(inode
, 2);
1359 sb
->s_root
= d_make_root(inode
);
1363 /* Create and populate files in zone groups directories */
1364 for (t
= 0; t
< ZONEFS_ZTYPE_MAX
; t
++) {
1365 ret
= zonefs_create_zgroup(&zd
, t
);
1371 zonefs_cleanup_zone_info(&zd
);
1376 static struct dentry
*zonefs_mount(struct file_system_type
*fs_type
,
1377 int flags
, const char *dev_name
, void *data
)
1379 return mount_bdev(fs_type
, flags
, dev_name
, data
, zonefs_fill_super
);
1382 static void zonefs_kill_super(struct super_block
*sb
)
1384 struct zonefs_sb_info
*sbi
= ZONEFS_SB(sb
);
1387 d_genocide(sb
->s_root
);
1388 kill_block_super(sb
);
1393 * File system definition and registration.
1395 static struct file_system_type zonefs_type
= {
1396 .owner
= THIS_MODULE
,
1398 .mount
= zonefs_mount
,
1399 .kill_sb
= zonefs_kill_super
,
1400 .fs_flags
= FS_REQUIRES_DEV
,
1403 static int __init
zonefs_init_inodecache(void)
1405 zonefs_inode_cachep
= kmem_cache_create("zonefs_inode_cache",
1406 sizeof(struct zonefs_inode_info
), 0,
1407 (SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
| SLAB_ACCOUNT
),
1409 if (zonefs_inode_cachep
== NULL
)
1414 static void zonefs_destroy_inodecache(void)
1417 * Make sure all delayed rcu free inodes are flushed before we
1418 * destroy the inode cache.
1421 kmem_cache_destroy(zonefs_inode_cachep
);
1424 static int __init
zonefs_init(void)
1428 BUILD_BUG_ON(sizeof(struct zonefs_super
) != ZONEFS_SUPER_SIZE
);
1430 ret
= zonefs_init_inodecache();
1434 ret
= register_filesystem(&zonefs_type
);
1436 zonefs_destroy_inodecache();
1443 static void __exit
zonefs_exit(void)
1445 zonefs_destroy_inodecache();
1446 unregister_filesystem(&zonefs_type
);
1449 MODULE_AUTHOR("Damien Le Moal");
1450 MODULE_DESCRIPTION("Zone file system for zoned block devices");
1451 MODULE_LICENSE("GPL");
1452 module_init(zonefs_init
);
1453 module_exit(zonefs_exit
);