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