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