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