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