projects/linux·blob master · refs

878 lines · 22.8 KiB

1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Simple file system for zoned block devices exposing zones as files.
4 *
5 * Copyright (C) 2022 Western Digital Corporation or its affiliates.
6 */
7#include <linux/module.h>
8#include <linux/pagemap.h>
9#include <linux/iomap.h>
10#include <linux/init.h>
11#include <linux/slab.h>
12#include <linux/blkdev.h>
13#include <linux/statfs.h>
14#include <linux/writeback.h>
15#include <linux/quotaops.h>
16#include <linux/seq_file.h>
17#include <linux/parser.h>
18#include <linux/uio.h>
19#include <linux/mman.h>
20#include <linux/sched/mm.h>
21#include <linux/task_io_accounting_ops.h>
22
23#include "zonefs.h"
24
25#include "trace.h"
26
27static int zonefs_read_iomap_begin(struct inode *inode, loff_t offset,
28 loff_t length, unsigned int flags,
29 struct iomap *iomap, struct iomap *srcmap)
30{
31 struct zonefs_inode_info *zi = ZONEFS_I(inode);
32 struct zonefs_zone *z = zonefs_inode_zone(inode);
33 struct super_block *sb = inode->i_sb;
34 loff_t isize;
35
36 /*
37 * All blocks are always mapped below EOF. If reading past EOF,
38 * act as if there is a hole up to the file maximum size.
39 */
40 mutex_lock(&zi->i_truncate_mutex);
41 iomap->bdev = inode->i_sb->s_bdev;
42 iomap->offset = ALIGN_DOWN(offset, sb->s_blocksize);
43 isize = i_size_read(inode);
44 if (iomap->offset >= isize) {
45 iomap->type = IOMAP_HOLE;
46 iomap->addr = IOMAP_NULL_ADDR;
47 iomap->length = length;
48 } else {
49 iomap->type = IOMAP_MAPPED;
50 iomap->addr = (z->z_sector << SECTOR_SHIFT) + iomap->offset;
51 iomap->length = isize - iomap->offset;
52 }
53 mutex_unlock(&zi->i_truncate_mutex);
54
55 trace_zonefs_iomap_begin(inode, iomap);
56
57 return 0;
58}
59
60static DEFINE_IOMAP_ITER_NEXT(zonefs_read_iomap_next, zonefs_read_iomap_begin);
61
62static const struct iomap_ops zonefs_read_iomap_ops = {
63 .iomap_next = zonefs_read_iomap_next,
64};
65
66static int zonefs_write_iomap_begin(struct inode *inode, loff_t offset,
67 loff_t length, unsigned int flags,
68 struct iomap *iomap, struct iomap *srcmap)
69{
70 struct zonefs_inode_info *zi = ZONEFS_I(inode);
71 struct zonefs_zone *z = zonefs_inode_zone(inode);
72 struct super_block *sb = inode->i_sb;
73 loff_t isize;
74
75 /* All write I/Os should always be within the file maximum size */
76 if (WARN_ON_ONCE(offset + length > z->z_capacity))
77 return -EIO;
78
79 /*
80 * Sequential zones can only accept direct writes. This is already
81 * checked when writes are issued, so warn if we see a page writeback
82 * operation.
83 */
84 if (WARN_ON_ONCE(zonefs_zone_is_seq(z) && !(flags & IOMAP_DIRECT)))
85 return -EIO;
86
87 /*
88 * For conventional zones, all blocks are always mapped. For sequential
89 * zones, all blocks after always mapped below the inode size (zone
90 * write pointer) and unwritten beyond.
91 */
92 mutex_lock(&zi->i_truncate_mutex);
93 iomap->bdev = inode->i_sb->s_bdev;
94 iomap->offset = ALIGN_DOWN(offset, sb->s_blocksize);
95 iomap->addr = (z->z_sector << SECTOR_SHIFT) + iomap->offset;
96 isize = i_size_read(inode);
97 if (iomap->offset >= isize) {
98 iomap->type = IOMAP_UNWRITTEN;
99 iomap->length = z->z_capacity - iomap->offset;
100 } else {
101 iomap->type = IOMAP_MAPPED;
102 iomap->length = isize - iomap->offset;
103 }
104 mutex_unlock(&zi->i_truncate_mutex);
105
106 trace_zonefs_iomap_begin(inode, iomap);
107
108 return 0;
109}
110
111static DEFINE_IOMAP_ITER_NEXT(zonefs_write_iomap_next,
112 zonefs_write_iomap_begin);
113
114static const struct iomap_ops zonefs_write_iomap_ops = {
115 .iomap_next = zonefs_write_iomap_next,
116};
117
118static int zonefs_read_folio(struct file *unused, struct folio *folio)
119{
120 iomap_bio_read_folio(folio, &zonefs_read_iomap_ops);
121 return 0;
122}
123
124static void zonefs_readahead(struct readahead_control *rac)
125{
126 iomap_bio_readahead(rac, &zonefs_read_iomap_ops);
127}
128
129/*
130 * Map blocks for page writeback. This is used only on conventional zone files,
131 * which implies that the page range can only be within the fixed inode size.
132 */
133static ssize_t zonefs_writeback_range(struct iomap_writepage_ctx *wpc,
134 struct folio *folio, u64 offset, unsigned len, u64 end_pos)
135{
136 struct zonefs_zone *z = zonefs_inode_zone(wpc->inode);
137
138 if (WARN_ON_ONCE(zonefs_zone_is_seq(z)))
139 return -EIO;
140 if (WARN_ON_ONCE(offset >= i_size_read(wpc->inode)))
141 return -EIO;
142
143 /* If the mapping is already OK, nothing needs to be done */
144 if (offset < wpc->iomap.offset ||
145 offset >= wpc->iomap.offset + wpc->iomap.length) {
146 int error;
147
148 error = zonefs_write_iomap_begin(wpc->inode, offset,
149 z->z_capacity - offset, IOMAP_WRITE,
150 &wpc->iomap, NULL);
151 if (error)
152 return error;
153 }
154
155 return iomap_add_to_ioend(wpc, folio, offset, end_pos, len);
156}
157
158static const struct iomap_writeback_ops zonefs_writeback_ops = {
159 .writeback_range = zonefs_writeback_range,
160 .writeback_submit = iomap_ioend_writeback_submit,
161};
162
163static int zonefs_writepages(struct address_space *mapping,
164 struct writeback_control *wbc)
165{
166 struct iomap_writepage_ctx wpc = {
167 .inode = mapping->host,
168 .wbc = wbc,
169 .ops = &zonefs_writeback_ops,
170 };
171
172 return iomap_writepages(&wpc);
173}
174
175static int zonefs_swap_activate(struct swap_info_struct *sis,
176 struct file *swap_file, sector_t *span)
177{
178 struct inode *inode = file_inode(swap_file);
179
180 if (zonefs_inode_is_seq(inode)) {
181 zonefs_err(inode->i_sb,
182 "swap file: not a conventional zone file\n");
183 return -EINVAL;
184 }
185
186 return iomap_swapfile_activate(sis, swap_file, span,
187 &zonefs_read_iomap_ops);
188}
189
190const struct address_space_operations zonefs_file_aops = {
191 .read_folio = zonefs_read_folio,
192 .readahead = zonefs_readahead,
193 .writepages = zonefs_writepages,
194 .dirty_folio = iomap_dirty_folio,
195 .release_folio = iomap_release_folio,
196 .invalidate_folio = iomap_invalidate_folio,
197 .migrate_folio = filemap_migrate_folio,
198 .is_partially_uptodate = iomap_is_partially_uptodate,
199 .error_remove_folio = generic_error_remove_folio,
200 .swap_activate = zonefs_swap_activate,
201};
202
203int zonefs_file_truncate(struct inode *inode, loff_t isize)
204{
205 struct zonefs_inode_info *zi = ZONEFS_I(inode);
206 struct zonefs_zone *z = zonefs_inode_zone(inode);
207 loff_t old_isize;
208 enum req_op op;
209 int ret = 0;
210
211 /*
212 * Only sequential zone files can be truncated and truncation is allowed
213 * only down to a 0 size, which is equivalent to a zone reset, and to
214 * the maximum file size, which is equivalent to a zone finish.
215 */
216 if (!zonefs_zone_is_seq(z))
217 return -EPERM;
218
219 if (!isize)
220 op = REQ_OP_ZONE_RESET;
221 else if (isize == z->z_capacity)
222 op = REQ_OP_ZONE_FINISH;
223 else
224 return -EPERM;
225
226 inode_dio_wait(inode);
227
228 /* Serialize against page faults */
229 filemap_invalidate_lock(inode->i_mapping);
230
231 /* Serialize against zonefs_iomap_begin() */
232 mutex_lock(&zi->i_truncate_mutex);
233
234 old_isize = i_size_read(inode);
235 if (isize == old_isize)
236 goto unlock;
237
238 ret = zonefs_inode_zone_mgmt(inode, op);
239 if (ret)
240 goto unlock;
241
242 /*
243 * If the mount option ZONEFS_MNTOPT_EXPLICIT_OPEN is set,
244 * take care of open zones.
245 */
246 if (z->z_flags & ZONEFS_ZONE_OPEN) {
247 /*
248 * Truncating a zone to EMPTY or FULL is the equivalent of
249 * closing the zone. For a truncation to 0, we need to
250 * re-open the zone to ensure new writes can be processed.
251 * For a truncation to the maximum file size, the zone is
252 * closed and writes cannot be accepted anymore, so clear
253 * the open flag.
254 */
255 if (!isize)
256 ret = zonefs_inode_zone_mgmt(inode, REQ_OP_ZONE_OPEN);
257 else
258 z->z_flags &= ~ZONEFS_ZONE_OPEN;
259 }
260
261 zonefs_update_stats(inode, isize);
262 truncate_setsize(inode, isize);
263 z->z_wpoffset = isize;
264 zonefs_inode_account_active(inode);
265
266unlock:
267 mutex_unlock(&zi->i_truncate_mutex);
268 filemap_invalidate_unlock(inode->i_mapping);
269
270 return ret;
271}
272
273static int zonefs_file_fsync(struct file *file, loff_t start, loff_t end,
274 int datasync)
275{
276 struct inode *inode = file_inode(file);
277 int ret = 0;
278
279 if (unlikely(IS_IMMUTABLE(inode)))
280 return -EPERM;
281
282 /*
283 * Since only direct writes are allowed in sequential files, page cache
284 * flush is needed only for conventional zone files.
285 */
286 if (zonefs_inode_is_cnv(inode))
287 ret = file_write_and_wait_range(file, start, end);
288 if (!ret)
289 ret = blkdev_issue_flush(inode->i_sb->s_bdev);
290
291 if (ret)
292 zonefs_io_error(inode, true);
293
294 return ret;
295}
296
297static vm_fault_t zonefs_filemap_page_mkwrite(struct vm_fault *vmf)
298{
299 struct inode *inode = file_inode(vmf->vma->vm_file);
300 vm_fault_t ret;
301
302 if (unlikely(IS_IMMUTABLE(inode)))
303 return VM_FAULT_SIGBUS;
304
305 /*
306 * Sanity check: only conventional zone files can have shared
307 * writeable mappings.
308 */
309 if (zonefs_inode_is_seq(inode))
310 return VM_FAULT_NOPAGE;
311
312 sb_start_pagefault(inode->i_sb);
313 file_update_time(vmf->vma->vm_file);
314
315 /* Serialize against truncates */
316 filemap_invalidate_lock_shared(inode->i_mapping);
317 ret = iomap_page_mkwrite(vmf, &zonefs_write_iomap_ops, NULL);
318 filemap_invalidate_unlock_shared(inode->i_mapping);
319
320 sb_end_pagefault(inode->i_sb);
321 return ret;
322}
323
324static const struct vm_operations_struct zonefs_file_vm_ops = {
325 .fault = filemap_fault,
326 .map_pages = filemap_map_pages,
327 .page_mkwrite = zonefs_filemap_page_mkwrite,
328};
329
330static int zonefs_file_mmap_prepare(struct vm_area_desc *desc)
331{
332 struct file *file = desc->file;
333
334 /*
335 * Conventional zones accept random writes, so their files can support
336 * shared writable mappings. For sequential zone files, only read
337 * mappings are possible since there are no guarantees for write
338 * ordering between msync() and page cache writeback.
339 */
340 if (zonefs_inode_is_seq(file_inode(file)) &&
341 vma_desc_test_all(desc, VMA_SHARED_BIT, VMA_MAYWRITE_BIT))
342 return -EINVAL;
343
344 file_accessed(file);
345 desc->vm_ops = &zonefs_file_vm_ops;
346
347 return 0;
348}
349
350static loff_t zonefs_file_llseek(struct file *file, loff_t offset, int whence)
351{
352 loff_t isize = i_size_read(file_inode(file));
353
354 /*
355 * Seeks are limited to below the zone size for conventional zones
356 * and below the zone write pointer for sequential zones. In both
357 * cases, this limit is the inode size.
358 */
359 return generic_file_llseek_size(file, offset, whence, isize, isize);
360}
361
362static int zonefs_file_write_dio_end_io(struct kiocb *iocb, ssize_t size,
363 int error, unsigned int flags)
364{
365 struct inode *inode = file_inode(iocb->ki_filp);
366 struct zonefs_inode_info *zi = ZONEFS_I(inode);
367
368 if (error) {
369 /*
370 * For Sync IOs, error recovery is called from
371 * zonefs_file_dio_write().
372 */
373 if (!is_sync_kiocb(iocb))
374 zonefs_io_error(inode, true);
375 return error;
376 }
377
378 if (size && zonefs_inode_is_seq(inode)) {
379 /*
380 * Note that we may be seeing completions out of order,
381 * but that is not a problem since a write completed
382 * successfully necessarily means that all preceding writes
383 * were also successful. So we can safely increase the inode
384 * size to the write end location.
385 */
386 mutex_lock(&zi->i_truncate_mutex);
387 if (i_size_read(inode) < iocb->ki_pos + size) {
388 zonefs_update_stats(inode, iocb->ki_pos + size);
389 zonefs_i_size_write(inode, iocb->ki_pos + size);
390 }
391 mutex_unlock(&zi->i_truncate_mutex);
392 }
393
394 return 0;
395}
396
397static const struct iomap_dio_ops zonefs_write_dio_ops = {
398 .end_io = zonefs_file_write_dio_end_io,
399};
400
401/*
402 * Do not exceed the LFS limits nor the file zone size. If pos is under the
403 * limit it becomes a short access. If it exceeds the limit, return -EFBIG.
404 */
405static loff_t zonefs_write_check_limits(struct file *file, loff_t pos,
406 loff_t count)
407{
408 struct inode *inode = file_inode(file);
409 struct zonefs_zone *z = zonefs_inode_zone(inode);
410 loff_t limit = rlimit(RLIMIT_FSIZE);
411 loff_t max_size = z->z_capacity;
412
413 if (limit != RLIM_INFINITY) {
414 if (pos >= limit) {
415 send_sig(SIGXFSZ, current, 0);
416 return -EFBIG;
417 }
418 count = min(count, limit - pos);
419 }
420
421 if (!(file->f_flags & O_LARGEFILE))
422 max_size = min_t(loff_t, MAX_NON_LFS, max_size);
423
424 if (unlikely(pos >= max_size))
425 return -EFBIG;
426
427 return min(count, max_size - pos);
428}
429
430static ssize_t zonefs_write_checks(struct kiocb *iocb, struct iov_iter *from)
431{
432 struct file *file = iocb->ki_filp;
433 struct inode *inode = file_inode(file);
434 struct zonefs_inode_info *zi = ZONEFS_I(inode);
435 struct zonefs_zone *z = zonefs_inode_zone(inode);
436 loff_t count;
437
438 if (IS_SWAPFILE(inode))
439 return -ETXTBSY;
440
441 if (!iov_iter_count(from))
442 return 0;
443
444 if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
445 return -EINVAL;
446
447 if (iocb->ki_flags & IOCB_APPEND) {
448 if (zonefs_zone_is_cnv(z))
449 return -EINVAL;
450 mutex_lock(&zi->i_truncate_mutex);
451 iocb->ki_pos = z->z_wpoffset;
452 mutex_unlock(&zi->i_truncate_mutex);
453 }
454
455 count = zonefs_write_check_limits(file, iocb->ki_pos,
456 iov_iter_count(from));
457 if (count < 0)
458 return count;
459
460 iov_iter_truncate(from, count);
461 return iov_iter_count(from);
462}
463
464/*
465 * Handle direct writes. For sequential zone files, this is the only possible
466 * write path. For these files, check that the user is issuing writes
467 * sequentially from the end of the file. This code assumes that the block layer
468 * delivers write requests to the device in sequential order. This is always the
469 * case if a block IO scheduler implementing the ELEVATOR_F_ZBD_SEQ_WRITE
470 * elevator feature is being used (e.g. mq-deadline). The block layer always
471 * automatically select such an elevator for zoned block devices during the
472 * device initialization.
473 */
474static ssize_t zonefs_file_dio_write(struct kiocb *iocb, struct iov_iter *from)
475{
476 struct inode *inode = file_inode(iocb->ki_filp);
477 struct zonefs_inode_info *zi = ZONEFS_I(inode);
478 struct zonefs_zone *z = zonefs_inode_zone(inode);
479 struct super_block *sb = inode->i_sb;
480 ssize_t ret, count;
481
482 /*
483 * For async direct IOs to sequential zone files, refuse IOCB_NOWAIT
484 * as this can cause write reordering (e.g. the first aio gets EAGAIN
485 * on the inode lock but the second goes through but is now unaligned).
486 */
487 if (zonefs_zone_is_seq(z) && !is_sync_kiocb(iocb) &&
488 (iocb->ki_flags & IOCB_NOWAIT))
489 return -EOPNOTSUPP;
490
491 if (iocb->ki_flags & IOCB_NOWAIT) {
492 if (!inode_trylock(inode))
493 return -EAGAIN;
494 } else {
495 inode_lock(inode);
496 }
497
498 count = zonefs_write_checks(iocb, from);
499 if (count <= 0) {
500 ret = count;
501 goto inode_unlock;
502 }
503
504 if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) {
505 ret = -EINVAL;
506 goto inode_unlock;
507 }
508
509 /* Enforce sequential writes (append only) in sequential zones */
510 if (zonefs_zone_is_seq(z)) {
511 mutex_lock(&zi->i_truncate_mutex);
512 if (iocb->ki_pos != z->z_wpoffset) {
513 mutex_unlock(&zi->i_truncate_mutex);
514 ret = -EINVAL;
515 goto inode_unlock;
516 }
517 /*
518 * Advance the zone write pointer offset. This assumes that the
519 * IO will succeed, which is OK to do because we do not allow
520 * partial writes (IOMAP_DIO_PARTIAL is not set) and if the IO
521 * fails, the error path will correct the write pointer offset.
522 */
523 z->z_wpoffset += count;
524 zonefs_inode_account_active(inode);
525 mutex_unlock(&zi->i_truncate_mutex);
526 }
527
528 /*
529 * iomap_dio_rw() may return ENOTBLK if there was an issue with
530 * page invalidation. Overwrite that error code with EBUSY so that
531 * the user can make sense of the error.
532 */
533 ret = iomap_dio_rw(iocb, from, &zonefs_write_iomap_ops,
534 &zonefs_write_dio_ops, 0, NULL, 0);
535 if (ret == -ENOTBLK)
536 ret = -EBUSY;
537
538 /*
539 * For a failed IO or partial completion, trigger error recovery
540 * to update the zone write pointer offset to a correct value.
541 * For asynchronous IOs, zonefs_file_write_dio_end_io() may already
542 * have executed error recovery if the IO already completed when we
543 * reach here. However, we cannot know that and execute error recovery
544 * again (that will not change anything).
545 */
546 if (zonefs_zone_is_seq(z)) {
547 if (ret > 0 && ret != count)
548 ret = -EIO;
549 if (ret < 0 && ret != -EIOCBQUEUED)
550 zonefs_io_error(inode, true);
551 }
552
553inode_unlock:
554 inode_unlock(inode);
555
556 return ret;
557}
558
559static ssize_t zonefs_file_buffered_write(struct kiocb *iocb,
560 struct iov_iter *from)
561{
562 struct inode *inode = file_inode(iocb->ki_filp);
563 ssize_t ret;
564
565 /*
566 * Direct IO writes are mandatory for sequential zone files so that the
567 * write IO issuing order is preserved.
568 */
569 if (zonefs_inode_is_seq(inode))
570 return -EIO;
571
572 if (iocb->ki_flags & IOCB_NOWAIT) {
573 if (!inode_trylock(inode))
574 return -EAGAIN;
575 } else {
576 inode_lock(inode);
577 }
578
579 ret = zonefs_write_checks(iocb, from);
580 if (ret <= 0)
581 goto inode_unlock;
582
583 ret = iomap_file_buffered_write(iocb, from, &zonefs_write_iomap_ops,
584 NULL, NULL);
585 if (ret == -EIO)
586 zonefs_io_error(inode, true);
587
588inode_unlock:
589 inode_unlock(inode);
590 if (ret > 0)
591 ret = generic_write_sync(iocb, ret);
592
593 return ret;
594}
595
596static ssize_t zonefs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
597{
598 struct inode *inode = file_inode(iocb->ki_filp);
599 struct zonefs_zone *z = zonefs_inode_zone(inode);
600
601 if (unlikely(IS_IMMUTABLE(inode)))
602 return -EPERM;
603
604 if (sb_rdonly(inode->i_sb))
605 return -EROFS;
606
607 /* Write operations beyond the zone capacity are not allowed */
608 if (iocb->ki_pos >= z->z_capacity)
609 return -EFBIG;
610
611 if (iocb->ki_flags & IOCB_DIRECT) {
612 ssize_t ret = zonefs_file_dio_write(iocb, from);
613
614 if (ret != -ENOTBLK)
615 return ret;
616 }
617
618 return zonefs_file_buffered_write(iocb, from);
619}
620
621static int zonefs_file_read_dio_end_io(struct kiocb *iocb, ssize_t size,
622 int error, unsigned int flags)
623{
624 if (error) {
625 zonefs_io_error(file_inode(iocb->ki_filp), false);
626 return error;
627 }
628
629 return 0;
630}
631
632static const struct iomap_dio_ops zonefs_read_dio_ops = {
633 .end_io = zonefs_file_read_dio_end_io,
634};
635
636static ssize_t zonefs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
637{
638 struct inode *inode = file_inode(iocb->ki_filp);
639 struct zonefs_inode_info *zi = ZONEFS_I(inode);
640 struct zonefs_zone *z = zonefs_inode_zone(inode);
641 struct super_block *sb = inode->i_sb;
642 loff_t isize;
643 ssize_t ret;
644
645 /* Offline zones cannot be read */
646 if (unlikely(IS_IMMUTABLE(inode) && !(inode->i_mode & 0777)))
647 return -EPERM;
648
649 if (iocb->ki_pos >= z->z_capacity)
650 return 0;
651
652 if (iocb->ki_flags & IOCB_NOWAIT) {
653 if (!inode_trylock_shared(inode))
654 return -EAGAIN;
655 } else {
656 inode_lock_shared(inode);
657 }
658
659 /* Limit read operations to written data */
660 mutex_lock(&zi->i_truncate_mutex);
661 isize = i_size_read(inode);
662 if (iocb->ki_pos >= isize) {
663 mutex_unlock(&zi->i_truncate_mutex);
664 ret = 0;
665 goto inode_unlock;
666 }
667 iov_iter_truncate(to, isize - iocb->ki_pos);
668 mutex_unlock(&zi->i_truncate_mutex);
669
670 if (iocb->ki_flags & IOCB_DIRECT) {
671 size_t count = iov_iter_count(to);
672
673 if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) {
674 ret = -EINVAL;
675 goto inode_unlock;
676 }
677 file_accessed(iocb->ki_filp);
678 ret = iomap_dio_rw(iocb, to, &zonefs_read_iomap_ops,
679 &zonefs_read_dio_ops, 0, NULL, 0);
680 } else {
681 ret = generic_file_read_iter(iocb, to);
682 if (ret == -EIO)
683 zonefs_io_error(inode, false);
684 }
685
686inode_unlock:
687 inode_unlock_shared(inode);
688
689 return ret;
690}
691
692static ssize_t zonefs_file_splice_read(struct file *in, loff_t *ppos,
693 struct pipe_inode_info *pipe,
694 size_t len, unsigned int flags)
695{
696 struct inode *inode = file_inode(in);
697 struct zonefs_inode_info *zi = ZONEFS_I(inode);
698 struct zonefs_zone *z = zonefs_inode_zone(inode);
699 loff_t isize;
700 ssize_t ret = 0;
701
702 /* Offline zones cannot be read */
703 if (unlikely(IS_IMMUTABLE(inode) && !(inode->i_mode & 0777)))
704 return -EPERM;
705
706 if (*ppos >= z->z_capacity)
707 return 0;
708
709 inode_lock_shared(inode);
710
711 /* Limit read operations to written data */
712 mutex_lock(&zi->i_truncate_mutex);
713 isize = i_size_read(inode);
714 if (*ppos >= isize)
715 len = 0;
716 else
717 len = min_t(loff_t, len, isize - *ppos);
718 mutex_unlock(&zi->i_truncate_mutex);
719
720 if (len > 0) {
721 ret = filemap_splice_read(in, ppos, pipe, len, flags);
722 if (ret == -EIO)
723 zonefs_io_error(inode, false);
724 }
725
726 inode_unlock_shared(inode);
727 return ret;
728}
729
730/*
731 * Write open accounting is done only for sequential files.
732 */
733static inline bool zonefs_seq_file_need_wro(struct inode *inode,
734 struct file *file)
735{
736 if (zonefs_inode_is_cnv(inode))
737 return false;
738
739 if (!(file->f_mode & FMODE_WRITE))
740 return false;
741
742 return true;
743}
744
745static int zonefs_seq_file_write_open(struct inode *inode)
746{
747 struct zonefs_inode_info *zi = ZONEFS_I(inode);
748 struct zonefs_zone *z = zonefs_inode_zone(inode);
749 int ret = 0;
750
751 mutex_lock(&zi->i_truncate_mutex);
752
753 if (!zi->i_wr_refcnt) {
754 struct zonefs_sb_info *sbi = ZONEFS_SB(inode->i_sb);
755 unsigned int wro = atomic_inc_return(&sbi->s_wro_seq_files);
756
757 if (sbi->s_mount_opts & ZONEFS_MNTOPT_EXPLICIT_OPEN) {
758
759 if (sbi->s_max_wro_seq_files
760 && wro > sbi->s_max_wro_seq_files) {
761 atomic_dec(&sbi->s_wro_seq_files);
762 ret = -EBUSY;
763 goto unlock;
764 }
765
766 if (i_size_read(inode) < z->z_capacity) {
767 ret = zonefs_inode_zone_mgmt(inode,
768 REQ_OP_ZONE_OPEN);
769 if (ret) {
770 atomic_dec(&sbi->s_wro_seq_files);
771 goto unlock;
772 }
773 z->z_flags |= ZONEFS_ZONE_OPEN;
774 zonefs_inode_account_active(inode);
775 }
776 }
777 }
778
779 zi->i_wr_refcnt++;
780
781unlock:
782 mutex_unlock(&zi->i_truncate_mutex);
783
784 return ret;
785}
786
787static int zonefs_file_open(struct inode *inode, struct file *file)
788{
789 int ret;
790
791 file->f_mode |= FMODE_CAN_ODIRECT;
792 ret = generic_file_open(inode, file);
793 if (ret)
794 return ret;
795
796 if (zonefs_seq_file_need_wro(inode, file))
797 return zonefs_seq_file_write_open(inode);
798
799 return 0;
800}
801
802static void zonefs_seq_file_write_close(struct inode *inode)
803{
804 struct zonefs_inode_info *zi = ZONEFS_I(inode);
805 struct zonefs_zone *z = zonefs_inode_zone(inode);
806 struct super_block *sb = inode->i_sb;
807 struct zonefs_sb_info *sbi = ZONEFS_SB(sb);
808 int ret = 0;
809
810 mutex_lock(&zi->i_truncate_mutex);
811
812 zi->i_wr_refcnt--;
813 if (zi->i_wr_refcnt)
814 goto unlock;
815
816 /*
817 * The file zone may not be open anymore (e.g. the file was truncated to
818 * its maximum size or it was fully written). For this case, we only
819 * need to decrement the write open count.
820 */
821 if (z->z_flags & ZONEFS_ZONE_OPEN) {
822 ret = zonefs_inode_zone_mgmt(inode, REQ_OP_ZONE_CLOSE);
823 if (ret) {
824 __zonefs_io_error(inode, false);
825 /*
826 * Leaving zones explicitly open may lead to a state
827 * where most zones cannot be written (zone resources
828 * exhausted). So take preventive action by remounting
829 * read-only.
830 */
831 if (z->z_flags & ZONEFS_ZONE_OPEN &&
832 !(sb->s_flags & SB_RDONLY)) {
833 zonefs_warn(sb,
834 "closing zone at %llu failed %d\n",
835 z->z_sector, ret);
836 zonefs_warn(sb,
837 "remounting filesystem read-only\n");
838 sb->s_flags |= SB_RDONLY;
839 }
840 goto unlock;
841 }
842
843 z->z_flags &= ~ZONEFS_ZONE_OPEN;
844 zonefs_inode_account_active(inode);
845 }
846
847 atomic_dec(&sbi->s_wro_seq_files);
848
849unlock:
850 mutex_unlock(&zi->i_truncate_mutex);
851}
852
853static int zonefs_file_release(struct inode *inode, struct file *file)
854{
855 /*
856 * If we explicitly open a zone we must close it again as well, but the
857 * zone management operation can fail (either due to an IO error or as
858 * the zone has gone offline or read-only). Make sure we don't fail the
859 * close(2) for user-space.
860 */
861 if (zonefs_seq_file_need_wro(inode, file))
862 zonefs_seq_file_write_close(inode);
863
864 return 0;
865}
866
867const struct file_operations zonefs_file_operations = {
868 .open = zonefs_file_open,
869 .release = zonefs_file_release,
870 .fsync = zonefs_file_fsync,
871 .mmap_prepare = zonefs_file_mmap_prepare,
872 .llseek = zonefs_file_llseek,
873 .read_iter = zonefs_file_read_iter,
874 .write_iter = zonefs_file_write_iter,
875 .splice_read = zonefs_file_splice_read,
876 .splice_write = iter_file_splice_write,
877 .iopoll = iocb_bio_iopoll,
878};