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