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