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