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>
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>
27 #define CREATE_TRACE_POINTS
30 static inline int zonefs_zone_mgmt(struct inode
*inode
,
33 struct zonefs_inode_info
*zi
= ZONEFS_I(inode
);
36 lockdep_assert_held(&zi
->i_truncate_mutex
);
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
46 if (op
== REQ_OP_ZONE_CLOSE
&& !zi
->i_wpoffset
)
47 op
= REQ_OP_ZONE_RESET
;
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
);
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
);
62 static inline void zonefs_i_size_write(struct inode
*inode
, loff_t isize
)
64 struct zonefs_inode_info
*zi
= ZONEFS_I(inode
);
66 i_size_write(inode
, isize
);
68 * A full zone is no longer open/active and does not need
71 if (isize
>= zi
->i_max_size
)
72 zi
->i_flags
&= ~ZONEFS_ZONE_OPEN
;
75 static int zonefs_iomap_begin(struct inode
*inode
, loff_t offset
, loff_t length
,
76 unsigned int flags
, struct iomap
*iomap
,
79 struct zonefs_inode_info
*zi
= ZONEFS_I(inode
);
80 struct super_block
*sb
= inode
->i_sb
;
83 /* All I/Os should always be within the file maximum size */
84 if (WARN_ON_ONCE(offset
+ length
> zi
->i_max_size
))
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
92 if (WARN_ON_ONCE(zi
->i_ztype
== ZONEFS_ZTYPE_SEQ
&&
93 (flags
& IOMAP_WRITE
) && !(flags
& IOMAP_DIRECT
)))
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.
101 mutex_lock(&zi
->i_truncate_mutex
);
102 isize
= i_size_read(inode
);
104 iomap
->type
= IOMAP_UNWRITTEN
;
106 iomap
->type
= IOMAP_MAPPED
;
107 if (flags
& IOMAP_WRITE
)
108 length
= zi
->i_max_size
- offset
;
110 length
= min(length
, isize
- offset
);
111 mutex_unlock(&zi
->i_truncate_mutex
);
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
;
118 trace_zonefs_iomap_begin(inode
, iomap
);
123 static const struct iomap_ops zonefs_iomap_ops
= {
124 .iomap_begin
= zonefs_iomap_begin
,
127 static int zonefs_readpage(struct file
*unused
, struct page
*page
)
129 return iomap_readpage(page
, &zonefs_iomap_ops
);
132 static void zonefs_readahead(struct readahead_control
*rac
)
134 iomap_readahead(rac
, &zonefs_iomap_ops
);
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.
141 static int zonefs_map_blocks(struct iomap_writepage_ctx
*wpc
,
142 struct inode
*inode
, loff_t offset
)
144 struct zonefs_inode_info
*zi
= ZONEFS_I(inode
);
146 if (WARN_ON_ONCE(zi
->i_ztype
!= ZONEFS_ZTYPE_CNV
))
148 if (WARN_ON_ONCE(offset
>= i_size_read(inode
)))
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
)
156 return zonefs_iomap_begin(inode
, offset
, zi
->i_max_size
- offset
,
157 IOMAP_WRITE
, &wpc
->iomap
, NULL
);
160 static const struct iomap_writeback_ops zonefs_writeback_ops
= {
161 .map_blocks
= zonefs_map_blocks
,
164 static int zonefs_writepage(struct page
*page
, struct writeback_control
*wbc
)
166 struct iomap_writepage_ctx wpc
= { };
168 return iomap_writepage(page
, wbc
, &wpc
, &zonefs_writeback_ops
);
171 static int zonefs_writepages(struct address_space
*mapping
,
172 struct writeback_control
*wbc
)
174 struct iomap_writepage_ctx wpc
= { };
176 return iomap_writepages(mapping
, wbc
, &wpc
, &zonefs_writeback_ops
);
179 static int zonefs_swap_activate(struct swap_info_struct
*sis
,
180 struct file
*swap_file
, sector_t
*span
)
182 struct inode
*inode
= file_inode(swap_file
);
183 struct zonefs_inode_info
*zi
= ZONEFS_I(inode
);
185 if (zi
->i_ztype
!= ZONEFS_ZTYPE_CNV
) {
186 zonefs_err(inode
->i_sb
,
187 "swap file: not a conventional zone file\n");
191 return iomap_swapfile_activate(sis
, swap_file
, span
, &zonefs_iomap_ops
);
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
,
209 static void zonefs_update_stats(struct inode
*inode
, loff_t new_isize
)
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
);
216 if (new_isize
== old_isize
)
219 spin_lock(&sbi
->s_lock
);
222 * This may be called for an update after an IO error.
223 * So beware of the values seen.
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
;
230 sbi
->s_used_blocks
= 0;
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
;
238 spin_unlock(&sbi
->s_lock
);
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.
246 static loff_t
zonefs_check_zone_condition(struct inode
*inode
,
247 struct blk_zone
*zone
, bool warn
,
250 struct zonefs_inode_info
*zi
= ZONEFS_I(inode
);
252 switch (zone
->cond
) {
253 case BLK_ZONE_COND_OFFLINE
:
255 * Dead zone: make the inode immutable, disable all accesses
256 * and set the file size to 0 (zone wp set to zone start).
259 zonefs_warn(inode
->i_sb
, "inode %lu: offline zone\n",
261 inode
->i_flags
|= S_IMMUTABLE
;
262 inode
->i_mode
&= ~0777;
263 zone
->wp
= zone
->start
;
265 case BLK_ZONE_COND_READONLY
:
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.
275 zonefs_warn(inode
->i_sb
, "inode %lu: read-only zone\n",
277 inode
->i_flags
|= S_IMMUTABLE
;
279 zone
->cond
= BLK_ZONE_COND_OFFLINE
;
280 inode
->i_mode
&= ~0777;
281 zone
->wp
= zone
->start
;
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
;
290 if (zi
->i_ztype
== ZONEFS_ZTYPE_CNV
)
291 return zi
->i_max_size
;
292 return (zone
->wp
- zone
->start
) << SECTOR_SHIFT
;
296 struct zonefs_ioerr_data
{
301 static int zonefs_io_error_cb(struct blk_zone
*zone
, unsigned int idx
,
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
;
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).
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
)
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).
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.
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
);
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.
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",
357 if (zone
->cond
!= BLK_ZONE_COND_OFFLINE
) {
358 zone
->cond
= BLK_ZONE_COND_OFFLINE
;
359 data_size
= zonefs_check_zone_condition(inode
, zone
,
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",
366 if (zone
->cond
!= BLK_ZONE_COND_READONLY
) {
367 zone
->cond
= BLK_ZONE_COND_READONLY
;
368 data_size
= zonefs_check_zone_condition(inode
, zone
,
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.
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
;
385 * If error=remount-ro was specified, any error result in remounting
386 * the volume as read-only.
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
;
394 * Update block usage stats and the inode size to prevent access to
397 zonefs_update_stats(inode
, data_size
);
398 zonefs_i_size_write(inode
, data_size
);
399 zi
->i_wpoffset
= data_size
;
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).
411 static void __zonefs_io_error(struct inode
*inode
, bool write
)
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
= {
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.
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
);
437 zonefs_err(sb
, "Get inode %lu zone information failed %d\n",
439 memalloc_noio_restore(noio_flag
);
442 static void zonefs_io_error(struct inode
*inode
, bool write
)
444 struct zonefs_inode_info
*zi
= ZONEFS_I(inode
);
446 mutex_lock(&zi
->i_truncate_mutex
);
447 __zonefs_io_error(inode
, write
);
448 mutex_unlock(&zi
->i_truncate_mutex
);
451 static int zonefs_file_truncate(struct inode
*inode
, loff_t isize
)
453 struct zonefs_inode_info
*zi
= ZONEFS_I(inode
);
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.
463 if (zi
->i_ztype
!= ZONEFS_ZTYPE_SEQ
)
467 op
= REQ_OP_ZONE_RESET
;
468 else if (isize
== zi
->i_max_size
)
469 op
= REQ_OP_ZONE_FINISH
;
473 inode_dio_wait(inode
);
475 /* Serialize against page faults */
476 filemap_invalidate_lock(inode
->i_mapping
);
478 /* Serialize against zonefs_iomap_begin() */
479 mutex_lock(&zi
->i_truncate_mutex
);
481 old_isize
= i_size_read(inode
);
482 if (isize
== old_isize
)
485 ret
= zonefs_zone_mgmt(inode
, op
);
490 * If the mount option ZONEFS_MNTOPT_EXPLICIT_OPEN is set,
491 * take care of open zones.
493 if (zi
->i_flags
& ZONEFS_ZONE_OPEN
) {
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
503 ret
= zonefs_zone_mgmt(inode
, REQ_OP_ZONE_OPEN
);
505 zi
->i_flags
&= ~ZONEFS_ZONE_OPEN
;
508 zonefs_update_stats(inode
, isize
);
509 truncate_setsize(inode
, isize
);
510 zi
->i_wpoffset
= isize
;
513 mutex_unlock(&zi
->i_truncate_mutex
);
514 filemap_invalidate_unlock(inode
->i_mapping
);
519 static int zonefs_inode_setattr(struct user_namespace
*mnt_userns
,
520 struct dentry
*dentry
, struct iattr
*iattr
)
522 struct inode
*inode
= d_inode(dentry
);
525 if (unlikely(IS_IMMUTABLE(inode
)))
528 ret
= setattr_prepare(&init_user_ns
, dentry
, iattr
);
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.
537 if ((iattr
->ia_valid
& ATTR_MODE
) && S_ISDIR(inode
->i_mode
) &&
538 (iattr
->ia_mode
& 0222))
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
);
550 if (iattr
->ia_valid
& ATTR_SIZE
) {
551 ret
= zonefs_file_truncate(inode
, iattr
->ia_size
);
556 setattr_copy(&init_user_ns
, inode
, iattr
);
561 static const struct inode_operations zonefs_file_inode_operations
= {
562 .setattr
= zonefs_inode_setattr
,
565 static int zonefs_file_fsync(struct file
*file
, loff_t start
, loff_t end
,
568 struct inode
*inode
= file_inode(file
);
571 if (unlikely(IS_IMMUTABLE(inode
)))
575 * Since only direct writes are allowed in sequential files, page cache
576 * flush is needed only for conventional zone files.
578 if (ZONEFS_I(inode
)->i_ztype
== ZONEFS_ZTYPE_CNV
)
579 ret
= file_write_and_wait_range(file
, start
, end
);
581 ret
= blkdev_issue_flush(inode
->i_sb
->s_bdev
);
584 zonefs_io_error(inode
, true);
589 static vm_fault_t
zonefs_filemap_page_mkwrite(struct vm_fault
*vmf
)
591 struct inode
*inode
= file_inode(vmf
->vma
->vm_file
);
592 struct zonefs_inode_info
*zi
= ZONEFS_I(inode
);
595 if (unlikely(IS_IMMUTABLE(inode
)))
596 return VM_FAULT_SIGBUS
;
599 * Sanity check: only conventional zone files can have shared
600 * writeable mappings.
602 if (WARN_ON_ONCE(zi
->i_ztype
!= ZONEFS_ZTYPE_CNV
))
603 return VM_FAULT_NOPAGE
;
605 sb_start_pagefault(inode
->i_sb
);
606 file_update_time(vmf
->vma
->vm_file
);
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
);
613 sb_end_pagefault(inode
->i_sb
);
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
,
623 static int zonefs_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
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.
631 if (ZONEFS_I(file_inode(file
))->i_ztype
== ZONEFS_ZTYPE_SEQ
&&
632 (vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
))
636 vma
->vm_ops
= &zonefs_file_vm_ops
;
641 static loff_t
zonefs_file_llseek(struct file
*file
, loff_t offset
, int whence
)
643 loff_t isize
= i_size_read(file_inode(file
));
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.
650 return generic_file_llseek_size(file
, offset
, whence
, isize
, isize
);
653 static int zonefs_file_write_dio_end_io(struct kiocb
*iocb
, ssize_t size
,
654 int error
, unsigned int flags
)
656 struct inode
*inode
= file_inode(iocb
->ki_filp
);
657 struct zonefs_inode_info
*zi
= ZONEFS_I(inode
);
660 zonefs_io_error(inode
, true);
664 if (size
&& zi
->i_ztype
!= ZONEFS_ZTYPE_CNV
) {
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.
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
);
677 mutex_unlock(&zi
->i_truncate_mutex
);
683 static const struct iomap_dio_ops zonefs_write_dio_ops
= {
684 .end_io
= zonefs_file_write_dio_end_io
,
687 static ssize_t
zonefs_file_dio_append(struct kiocb
*iocb
, struct iov_iter
*from
)
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
;
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
);
702 nr_pages
= iov_iter_npages(from
, BIO_MAX_VECS
);
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
;
715 ret
= bio_iov_iter_get_pages(bio
, from
);
719 size
= bio
->bi_iter
.bi_size
;
720 task_io_account_write(size
);
722 if (iocb
->ki_flags
& IOCB_HIPRI
)
723 bio_set_polled(bio
, iocb
);
725 ret
= submit_bio_wait(bio
);
727 zonefs_file_write_dio_end_io(iocb
, size
, ret
, 0);
728 trace_zonefs_file_dio_append(inode
, size
, ret
);
731 bio_release_pages(bio
, false);
735 iocb
->ki_pos
+= size
;
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.
746 static loff_t
zonefs_write_check_limits(struct file
*file
, loff_t pos
,
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
;
754 if (limit
!= RLIM_INFINITY
) {
756 send_sig(SIGXFSZ
, current
, 0);
759 count
= min(count
, limit
- pos
);
762 if (!(file
->f_flags
& O_LARGEFILE
))
763 max_size
= min_t(loff_t
, MAX_NON_LFS
, max_size
);
765 if (unlikely(pos
>= max_size
))
768 return min(count
, max_size
- pos
);
771 static ssize_t
zonefs_write_checks(struct kiocb
*iocb
, struct iov_iter
*from
)
773 struct file
*file
= iocb
->ki_filp
;
774 struct inode
*inode
= file_inode(file
);
775 struct zonefs_inode_info
*zi
= ZONEFS_I(inode
);
778 if (IS_SWAPFILE(inode
))
781 if (!iov_iter_count(from
))
784 if ((iocb
->ki_flags
& IOCB_NOWAIT
) && !(iocb
->ki_flags
& IOCB_DIRECT
))
787 if (iocb
->ki_flags
& IOCB_APPEND
) {
788 if (zi
->i_ztype
!= ZONEFS_ZTYPE_SEQ
)
790 mutex_lock(&zi
->i_truncate_mutex
);
791 iocb
->ki_pos
= zi
->i_wpoffset
;
792 mutex_unlock(&zi
->i_truncate_mutex
);
795 count
= zonefs_write_check_limits(file
, iocb
->ki_pos
,
796 iov_iter_count(from
));
800 iov_iter_truncate(from
, count
);
801 return iov_iter_count(from
);
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.
814 static ssize_t
zonefs_file_dio_write(struct kiocb
*iocb
, struct iov_iter
*from
)
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
);
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).
828 if (zi
->i_ztype
== ZONEFS_ZTYPE_SEQ
&& !sync
&&
829 (iocb
->ki_flags
& IOCB_NOWAIT
))
832 if (iocb
->ki_flags
& IOCB_NOWAIT
) {
833 if (!inode_trylock(inode
))
839 count
= zonefs_write_checks(iocb
, from
);
845 if ((iocb
->ki_pos
| count
) & (sb
->s_blocksize
- 1)) {
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
);
858 mutex_unlock(&zi
->i_truncate_mutex
);
863 ret
= zonefs_file_dio_append(iocb
, from
);
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
)) {
871 mutex_lock(&zi
->i_truncate_mutex
);
872 zi
->i_wpoffset
+= count
;
873 mutex_unlock(&zi
->i_truncate_mutex
);
882 static ssize_t
zonefs_file_buffered_write(struct kiocb
*iocb
,
883 struct iov_iter
*from
)
885 struct inode
*inode
= file_inode(iocb
->ki_filp
);
886 struct zonefs_inode_info
*zi
= ZONEFS_I(inode
);
890 * Direct IO writes are mandatory for sequential zone files so that the
891 * write IO issuing order is preserved.
893 if (zi
->i_ztype
!= ZONEFS_ZTYPE_CNV
)
896 if (iocb
->ki_flags
& IOCB_NOWAIT
) {
897 if (!inode_trylock(inode
))
903 ret
= zonefs_write_checks(iocb
, from
);
907 ret
= iomap_file_buffered_write(iocb
, from
, &zonefs_iomap_ops
);
910 else if (ret
== -EIO
)
911 zonefs_io_error(inode
, true);
916 ret
= generic_write_sync(iocb
, ret
);
921 static ssize_t
zonefs_file_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
923 struct inode
*inode
= file_inode(iocb
->ki_filp
);
925 if (unlikely(IS_IMMUTABLE(inode
)))
928 if (sb_rdonly(inode
->i_sb
))
931 /* Write operations beyond the zone size are not allowed */
932 if (iocb
->ki_pos
>= ZONEFS_I(inode
)->i_max_size
)
935 if (iocb
->ki_flags
& IOCB_DIRECT
) {
936 ssize_t ret
= zonefs_file_dio_write(iocb
, from
);
941 return zonefs_file_buffered_write(iocb
, from
);
944 static int zonefs_file_read_dio_end_io(struct kiocb
*iocb
, ssize_t size
,
945 int error
, unsigned int flags
)
948 zonefs_io_error(file_inode(iocb
->ki_filp
), false);
955 static const struct iomap_dio_ops zonefs_read_dio_ops
= {
956 .end_io
= zonefs_file_read_dio_end_io
,
959 static ssize_t
zonefs_file_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
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
;
967 /* Offline zones cannot be read */
968 if (unlikely(IS_IMMUTABLE(inode
) && !(inode
->i_mode
& 0777)))
971 if (iocb
->ki_pos
>= zi
->i_max_size
)
974 if (iocb
->ki_flags
& IOCB_NOWAIT
) {
975 if (!inode_trylock_shared(inode
))
978 inode_lock_shared(inode
);
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
);
989 iov_iter_truncate(to
, isize
- iocb
->ki_pos
);
990 mutex_unlock(&zi
->i_truncate_mutex
);
992 if (iocb
->ki_flags
& IOCB_DIRECT
) {
993 size_t count
= iov_iter_count(to
);
995 if ((iocb
->ki_pos
| count
) & (sb
->s_blocksize
- 1)) {
999 file_accessed(iocb
->ki_filp
);
1000 ret
= iomap_dio_rw(iocb
, to
, &zonefs_iomap_ops
,
1001 &zonefs_read_dio_ops
, 0, 0);
1003 ret
= generic_file_read_iter(iocb
, to
);
1005 zonefs_io_error(inode
, false);
1009 inode_unlock_shared(inode
);
1014 static inline bool zonefs_file_use_exp_open(struct inode
*inode
, struct file
*file
)
1016 struct zonefs_inode_info
*zi
= ZONEFS_I(inode
);
1017 struct zonefs_sb_info
*sbi
= ZONEFS_SB(inode
->i_sb
);
1019 if (!(sbi
->s_mount_opts
& ZONEFS_MNTOPT_EXPLICIT_OPEN
))
1022 if (zi
->i_ztype
!= ZONEFS_ZTYPE_SEQ
)
1025 if (!(file
->f_mode
& FMODE_WRITE
))
1031 static int zonefs_open_zone(struct inode
*inode
)
1033 struct zonefs_inode_info
*zi
= ZONEFS_I(inode
);
1034 struct zonefs_sb_info
*sbi
= ZONEFS_SB(inode
->i_sb
);
1037 mutex_lock(&zi
->i_truncate_mutex
);
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
);
1046 if (i_size_read(inode
) < zi
->i_max_size
) {
1047 ret
= zonefs_zone_mgmt(inode
, REQ_OP_ZONE_OPEN
);
1049 atomic_dec(&sbi
->s_open_zones
);
1052 zi
->i_flags
|= ZONEFS_ZONE_OPEN
;
1059 mutex_unlock(&zi
->i_truncate_mutex
);
1064 static int zonefs_file_open(struct inode
*inode
, struct file
*file
)
1068 ret
= generic_file_open(inode
, file
);
1072 if (zonefs_file_use_exp_open(inode
, file
))
1073 return zonefs_open_zone(inode
);
1078 static void zonefs_close_zone(struct inode
*inode
)
1080 struct zonefs_inode_info
*zi
= ZONEFS_I(inode
);
1083 mutex_lock(&zi
->i_truncate_mutex
);
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
;
1090 * If the file zone is full, it is not open anymore and we only
1091 * need to decrement the open count.
1093 if (!(zi
->i_flags
& ZONEFS_ZONE_OPEN
))
1096 ret
= zonefs_zone_mgmt(inode
, REQ_OP_ZONE_CLOSE
);
1098 __zonefs_io_error(inode
, false);
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
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
;
1111 zi
->i_flags
&= ~ZONEFS_ZONE_OPEN
;
1113 atomic_dec(&sbi
->s_open_zones
);
1115 mutex_unlock(&zi
->i_truncate_mutex
);
1118 static int zonefs_file_release(struct inode
*inode
, struct file
*file
)
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.
1126 if (zonefs_file_use_exp_open(inode
, file
))
1127 zonefs_close_zone(inode
);
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
,
1145 static struct kmem_cache
*zonefs_inode_cachep
;
1147 static struct inode
*zonefs_alloc_inode(struct super_block
*sb
)
1149 struct zonefs_inode_info
*zi
;
1151 zi
= kmem_cache_alloc(zonefs_inode_cachep
, GFP_KERNEL
);
1155 inode_init_once(&zi
->i_vnode
);
1156 mutex_init(&zi
->i_truncate_mutex
);
1157 zi
->i_wr_refcnt
= 0;
1160 return &zi
->i_vnode
;
1163 static void zonefs_free_inode(struct inode
*inode
)
1165 kmem_cache_free(zonefs_inode_cachep
, ZONEFS_I(inode
));
1171 static int zonefs_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
1173 struct super_block
*sb
= dentry
->d_sb
;
1174 struct zonefs_sb_info
*sbi
= ZONEFS_SB(sb
);
1175 enum zonefs_ztype t
;
1177 buf
->f_type
= ZONEFS_MAGIC
;
1178 buf
->f_bsize
= sb
->s_blocksize
;
1179 buf
->f_namelen
= ZONEFS_NAME_MAX
;
1181 spin_lock(&sbi
->s_lock
);
1183 buf
->f_blocks
= sbi
->s_blocks
;
1184 if (WARN_ON(sbi
->s_used_blocks
> sbi
->s_blocks
))
1187 buf
->f_bfree
= buf
->f_blocks
- sbi
->s_used_blocks
;
1188 buf
->f_bavail
= buf
->f_bfree
;
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;
1196 spin_unlock(&sbi
->s_lock
);
1198 buf
->f_fsid
= uuid_to_fsid(sbi
->s_uuid
.b
);
1204 Opt_errors_ro
, Opt_errors_zro
, Opt_errors_zol
, Opt_errors_repair
,
1205 Opt_explicit_open
, Opt_err
,
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" },
1217 static int zonefs_parse_options(struct super_block
*sb
, char *options
)
1219 struct zonefs_sb_info
*sbi
= ZONEFS_SB(sb
);
1220 substring_t args
[MAX_OPT_ARGS
];
1226 while ((p
= strsep(&options
, ",")) != NULL
) {
1232 token
= match_token(p
, tokens
, args
);
1235 sbi
->s_mount_opts
&= ~ZONEFS_MNTOPT_ERRORS_MASK
;
1236 sbi
->s_mount_opts
|= ZONEFS_MNTOPT_ERRORS_RO
;
1238 case Opt_errors_zro
:
1239 sbi
->s_mount_opts
&= ~ZONEFS_MNTOPT_ERRORS_MASK
;
1240 sbi
->s_mount_opts
|= ZONEFS_MNTOPT_ERRORS_ZRO
;
1242 case Opt_errors_zol
:
1243 sbi
->s_mount_opts
&= ~ZONEFS_MNTOPT_ERRORS_MASK
;
1244 sbi
->s_mount_opts
|= ZONEFS_MNTOPT_ERRORS_ZOL
;
1246 case Opt_errors_repair
:
1247 sbi
->s_mount_opts
&= ~ZONEFS_MNTOPT_ERRORS_MASK
;
1248 sbi
->s_mount_opts
|= ZONEFS_MNTOPT_ERRORS_REPAIR
;
1250 case Opt_explicit_open
:
1251 sbi
->s_mount_opts
|= ZONEFS_MNTOPT_EXPLICIT_OPEN
;
1261 static int zonefs_show_options(struct seq_file
*seq
, struct dentry
*root
)
1263 struct zonefs_sb_info
*sbi
= ZONEFS_SB(root
->d_sb
);
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");
1277 static int zonefs_remount(struct super_block
*sb
, int *flags
, char *data
)
1279 sync_filesystem(sb
);
1281 return zonefs_parse_options(sb
, data
);
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
,
1292 static const struct inode_operations zonefs_dir_inode_operations
= {
1293 .lookup
= simple_lookup
,
1294 .setattr
= zonefs_inode_setattr
,
1297 static void zonefs_init_dir_inode(struct inode
*parent
, struct inode
*inode
,
1298 enum zonefs_ztype type
)
1300 struct super_block
*sb
= parent
->i_sb
;
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);
1310 static int zonefs_init_file_inode(struct inode
*inode
, struct blk_zone
*zone
,
1311 enum zonefs_ztype type
)
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
);
1318 inode
->i_ino
= zone
->start
>> sbi
->s_zone_sectors_shift
;
1319 inode
->i_mode
= S_IFREG
| sbi
->s_perm
;
1322 zi
->i_zsector
= zone
->start
;
1323 zi
->i_zone_size
= zone
->len
<< SECTOR_SHIFT
;
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);
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
;
1334 inode
->i_op
= &zonefs_file_inode_operations
;
1335 inode
->i_fop
= &zonefs_file_operations
;
1336 inode
->i_mapping
->a_ops
= &zonefs_file_aops
;
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
;
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.
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
);
1359 static struct dentry
*zonefs_create_inode(struct dentry
*parent
,
1360 const char *name
, struct blk_zone
*zone
,
1361 enum zonefs_ztype type
)
1363 struct inode
*dir
= d_inode(parent
);
1364 struct dentry
*dentry
;
1365 struct inode
*inode
;
1368 dentry
= d_alloc_name(parent
, name
);
1372 inode
= new_inode(parent
->d_sb
);
1376 inode
->i_ctime
= inode
->i_mtime
= inode
->i_atime
= dir
->i_ctime
;
1378 ret
= zonefs_init_file_inode(inode
, zone
, type
);
1384 zonefs_init_dir_inode(dir
, inode
, type
);
1387 d_add(dentry
, inode
);
1398 struct zonefs_zone_data
{
1399 struct super_block
*sb
;
1400 unsigned int nr_zones
[ZONEFS_ZTYPE_MAX
];
1401 struct blk_zone
*zones
;
1405 * Create a zone group and populate it with zone files.
1407 static int zonefs_create_zgroup(struct zonefs_zone_data
*zd
,
1408 enum zonefs_ztype type
)
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
;
1419 /* If the group is empty, there is nothing to do */
1420 if (!zd
->nr_zones
[type
])
1423 file_name
= kmalloc(ZONEFS_NAME_MAX
, GFP_KERNEL
);
1427 if (type
== ZONEFS_ZTYPE_CNV
)
1428 zgroup_name
= "cnv";
1430 zgroup_name
= "seq";
1432 dir
= zonefs_create_inode(sb
->s_root
, zgroup_name
, NULL
, type
);
1439 * The first zone contains the super block: skip it.
1441 end
= zd
->zones
+ blkdev_nr_zones(sb
->s_bdev
->bd_disk
);
1442 for (zone
= &zd
->zones
[1]; zone
< end
; zone
= next
) {
1445 if (zonefs_zone_type(zone
) != type
)
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
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
)
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
;
1469 if (zone
->capacity
!= zone
->len
) {
1470 zonefs_err(sb
, "Invalid conventional zone capacity\n");
1477 * Use the file number within its group as file name.
1479 snprintf(file_name
, ZONEFS_NAME_MAX
- 1, "%u", n
);
1480 if (!zonefs_create_inode(dir
, file_name
, zone
, type
)) {
1488 zonefs_info(sb
, "Zone group \"%s\" has %u file%s\n",
1489 zgroup_name
, n
, n
> 1 ? "s" : "");
1491 sbi
->s_nr_files
[type
] = n
;
1500 static int zonefs_get_zone_info_cb(struct blk_zone
*zone
, unsigned int idx
,
1503 struct zonefs_zone_data
*zd
= data
;
1506 * Count the number of usable zones: the first zone at index 0 contains
1507 * the super block and is ignored.
1509 switch (zone
->type
) {
1510 case BLK_ZONE_TYPE_CONVENTIONAL
:
1511 zone
->wp
= zone
->start
+ zone
->len
;
1513 zd
->nr_zones
[ZONEFS_ZTYPE_CNV
]++;
1515 case BLK_ZONE_TYPE_SEQWRITE_REQ
:
1516 case BLK_ZONE_TYPE_SEQWRITE_PREF
:
1518 zd
->nr_zones
[ZONEFS_ZTYPE_SEQ
]++;
1521 zonefs_err(zd
->sb
, "Unsupported zone type 0x%x\n",
1526 memcpy(&zd
->zones
[idx
], zone
, sizeof(struct blk_zone
));
1531 static int zonefs_get_zone_info(struct zonefs_zone_data
*zd
)
1533 struct block_device
*bdev
= zd
->sb
->s_bdev
;
1536 zd
->zones
= kvcalloc(blkdev_nr_zones(bdev
->bd_disk
),
1537 sizeof(struct blk_zone
), GFP_KERNEL
);
1541 /* Get zones information from the device */
1542 ret
= blkdev_report_zones(bdev
, 0, BLK_ALL_ZONES
,
1543 zonefs_get_zone_info_cb
, zd
);
1545 zonefs_err(zd
->sb
, "Zone report failed %d\n", ret
);
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
));
1558 static inline void zonefs_cleanup_zone_info(struct zonefs_zone_data
*zd
)
1564 * Read super block information from the device.
1566 static int zonefs_read_super(struct super_block
*sb
)
1568 struct zonefs_sb_info
*sbi
= ZONEFS_SB(sb
);
1569 struct zonefs_super
*super
;
1570 u32 crc
, stored_crc
;
1572 struct bio_vec bio_vec
;
1576 page
= alloc_page(GFP_KERNEL
);
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);
1586 ret
= submit_bio_wait(&bio
);
1593 if (le32_to_cpu(super
->s_magic
) != ZONEFS_MAGIC
)
1596 stored_crc
= le32_to_cpu(super
->s_crc
);
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)",
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",
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");
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");
1630 if (sbi
->s_features
& ZONEFS_F_PERM
)
1631 sbi
->s_perm
= le32_to_cpu(super
->s_perm
);
1633 if (memchr_inv(super
->s_reserved
, 0, sizeof(super
->s_reserved
))) {
1634 zonefs_err(sb
, "Reserved area is being used\n");
1638 import_uuid(&sbi
->s_uuid
, super
->s_uuid
);
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
1654 static int zonefs_fill_super(struct super_block
*sb
, void *data
, int silent
)
1656 struct zonefs_zone_data zd
;
1657 struct zonefs_sb_info
*sbi
;
1658 struct inode
*inode
;
1659 enum zonefs_ztype t
;
1662 if (!bdev_is_zoned(sb
->s_bdev
)) {
1663 zonefs_err(sb
, "Not a zoned block device\n");
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.
1673 sbi
= kzalloc(sizeof(*sbi
), GFP_KERNEL
);
1677 spin_lock_init(&sbi
->s_lock
);
1678 sb
->s_fs_info
= sbi
;
1679 sb
->s_magic
= ZONEFS_MAGIC
;
1681 sb
->s_op
= &zonefs_sops
;
1682 sb
->s_time_gran
= 1;
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.
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
;
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
;
1703 ret
= zonefs_read_super(sb
);
1707 ret
= zonefs_parse_options(sb
, data
);
1711 memset(&zd
, 0, sizeof(struct zonefs_zone_data
));
1713 ret
= zonefs_get_zone_info(&zd
);
1717 zonefs_info(sb
, "Mounting %u zones",
1718 blkdev_nr_zones(sb
->s_bdev
->bd_disk
));
1720 /* Create root directory inode */
1722 inode
= new_inode(sb
);
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);
1733 sb
->s_root
= d_make_root(inode
);
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
);
1745 zonefs_cleanup_zone_info(&zd
);
1750 static struct dentry
*zonefs_mount(struct file_system_type
*fs_type
,
1751 int flags
, const char *dev_name
, void *data
)
1753 return mount_bdev(fs_type
, flags
, dev_name
, data
, zonefs_fill_super
);
1756 static void zonefs_kill_super(struct super_block
*sb
)
1758 struct zonefs_sb_info
*sbi
= ZONEFS_SB(sb
);
1761 d_genocide(sb
->s_root
);
1762 kill_block_super(sb
);
1767 * File system definition and registration.
1769 static struct file_system_type zonefs_type
= {
1770 .owner
= THIS_MODULE
,
1772 .mount
= zonefs_mount
,
1773 .kill_sb
= zonefs_kill_super
,
1774 .fs_flags
= FS_REQUIRES_DEV
,
1777 static int __init
zonefs_init_inodecache(void)
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
),
1783 if (zonefs_inode_cachep
== NULL
)
1788 static void zonefs_destroy_inodecache(void)
1791 * Make sure all delayed rcu free inodes are flushed before we
1792 * destroy the inode cache.
1795 kmem_cache_destroy(zonefs_inode_cachep
);
1798 static int __init
zonefs_init(void)
1802 BUILD_BUG_ON(sizeof(struct zonefs_super
) != ZONEFS_SUPER_SIZE
);
1804 ret
= zonefs_init_inodecache();
1808 ret
= register_filesystem(&zonefs_type
);
1810 zonefs_destroy_inodecache();
1817 static void __exit
zonefs_exit(void)
1819 zonefs_destroy_inodecache();
1820 unregister_filesystem(&zonefs_type
);
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
);