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