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