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