xref: /linux/fs/freevxfs/vxfs_super.c (revision d53b8e36925256097a08d7cb749198d85cbf9b2b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2000-2001 Christoph Hellwig.
4  * Copyright (c) 2016 Krzysztof Blaszkowski
5  */
6 
7 /*
8  * Veritas filesystem driver - superblock related routines.
9  */
10 #include <linux/init.h>
11 #include <linux/module.h>
12 
13 #include <linux/blkdev.h>
14 #include <linux/fs.h>
15 #include <linux/buffer_head.h>
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
18 #include <linux/stat.h>
19 #include <linux/vfs.h>
20 #include <linux/fs_context.h>
21 
22 #include "vxfs.h"
23 #include "vxfs_extern.h"
24 #include "vxfs_dir.h"
25 #include "vxfs_inode.h"
26 
27 
28 MODULE_AUTHOR("Christoph Hellwig, Krzysztof Blaszkowski");
29 MODULE_DESCRIPTION("Veritas Filesystem (VxFS) driver");
30 MODULE_LICENSE("Dual BSD/GPL");
31 
32 static struct kmem_cache *vxfs_inode_cachep;
33 
34 /**
35  * vxfs_put_super - free superblock resources
36  * @sbp:	VFS superblock.
37  *
38  * Description:
39  *   vxfs_put_super frees all resources allocated for @sbp
40  *   after the last instance of the filesystem is unmounted.
41  */
42 
43 static void
44 vxfs_put_super(struct super_block *sbp)
45 {
46 	struct vxfs_sb_info	*infp = VXFS_SBI(sbp);
47 
48 	iput(infp->vsi_fship);
49 	iput(infp->vsi_ilist);
50 	iput(infp->vsi_stilist);
51 
52 	brelse(infp->vsi_bp);
53 	kfree(infp);
54 }
55 
56 /**
57  * vxfs_statfs - get filesystem information
58  * @dentry:	VFS dentry to locate superblock
59  * @bufp:	output buffer
60  *
61  * Description:
62  *   vxfs_statfs fills the statfs buffer @bufp with information
63  *   about the filesystem described by @dentry.
64  *
65  * Returns:
66  *   Zero.
67  *
68  * Locking:
69  *   No locks held.
70  *
71  * Notes:
72  *   This is everything but complete...
73  */
74 static int
75 vxfs_statfs(struct dentry *dentry, struct kstatfs *bufp)
76 {
77 	struct vxfs_sb_info		*infp = VXFS_SBI(dentry->d_sb);
78 	struct vxfs_sb *raw_sb = infp->vsi_raw;
79 	u64 id = huge_encode_dev(dentry->d_sb->s_bdev->bd_dev);
80 
81 	bufp->f_type = VXFS_SUPER_MAGIC;
82 	bufp->f_bsize = dentry->d_sb->s_blocksize;
83 	bufp->f_blocks = fs32_to_cpu(infp, raw_sb->vs_dsize);
84 	bufp->f_bfree = fs32_to_cpu(infp, raw_sb->vs_free);
85 	bufp->f_bavail = 0;
86 	bufp->f_files = 0;
87 	bufp->f_ffree = fs32_to_cpu(infp, raw_sb->vs_ifree);
88 	bufp->f_fsid = u64_to_fsid(id);
89 	bufp->f_namelen = VXFS_NAMELEN;
90 
91 	return 0;
92 }
93 
94 static int vxfs_reconfigure(struct fs_context *fc)
95 {
96 	sync_filesystem(fc->root->d_sb);
97 	fc->sb_flags |= SB_RDONLY;
98 	return 0;
99 }
100 
101 static struct inode *vxfs_alloc_inode(struct super_block *sb)
102 {
103 	struct vxfs_inode_info *vi;
104 
105 	vi = alloc_inode_sb(sb, vxfs_inode_cachep, GFP_KERNEL);
106 	if (!vi)
107 		return NULL;
108 	inode_init_once(&vi->vfs_inode);
109 	return &vi->vfs_inode;
110 }
111 
112 static void vxfs_free_inode(struct inode *inode)
113 {
114 	kmem_cache_free(vxfs_inode_cachep, VXFS_INO(inode));
115 }
116 
117 static const struct super_operations vxfs_super_ops = {
118 	.alloc_inode		= vxfs_alloc_inode,
119 	.free_inode		= vxfs_free_inode,
120 	.evict_inode		= vxfs_evict_inode,
121 	.put_super		= vxfs_put_super,
122 	.statfs			= vxfs_statfs,
123 };
124 
125 static int vxfs_try_sb_magic(struct super_block *sbp, struct fs_context *fc,
126 		unsigned blk, __fs32 magic)
127 {
128 	struct buffer_head *bp;
129 	struct vxfs_sb *rsbp;
130 	struct vxfs_sb_info *infp = VXFS_SBI(sbp);
131 	int silent = fc->sb_flags & SB_SILENT;
132 	int rc = -ENOMEM;
133 
134 	bp = sb_bread(sbp, blk);
135 	do {
136 		if (!bp || !buffer_mapped(bp)) {
137 			if (!silent) {
138 				warnf(fc,
139 				      "vxfs: unable to read disk superblock at %u",
140 				      blk);
141 			}
142 			break;
143 		}
144 
145 		rc = -EINVAL;
146 		rsbp = (struct vxfs_sb *)bp->b_data;
147 		if (rsbp->vs_magic != magic) {
148 			if (!silent)
149 				infof(fc,
150 				      "vxfs: WRONG superblock magic %08x at %u",
151 				      rsbp->vs_magic, blk);
152 			break;
153 		}
154 
155 		rc = 0;
156 		infp->vsi_raw = rsbp;
157 		infp->vsi_bp = bp;
158 	} while (0);
159 
160 	if (rc) {
161 		infp->vsi_raw = NULL;
162 		infp->vsi_bp = NULL;
163 		brelse(bp);
164 	}
165 
166 	return rc;
167 }
168 
169 /**
170  * vxfs_fill_super - read superblock into memory and initialize filesystem
171  * @sbp:		VFS superblock (to fill)
172  * @fc:			filesytem context
173  *
174  * Description:
175  *   We are called on the first mount of a filesystem to read the
176  *   superblock into memory and do some basic setup.
177  *
178  * Returns:
179  *   The superblock on success, else %NULL.
180  *
181  * Locking:
182  *   We are under @sbp->s_lock.
183  */
184 static int vxfs_fill_super(struct super_block *sbp, struct fs_context *fc)
185 {
186 	struct vxfs_sb_info	*infp;
187 	struct vxfs_sb		*rsbp;
188 	u_long			bsize;
189 	struct inode *root;
190 	int ret = -EINVAL;
191 	int silent = fc->sb_flags & SB_SILENT;
192 	u32 j;
193 
194 	sbp->s_flags |= SB_RDONLY;
195 
196 	infp = kzalloc(sizeof(*infp), GFP_KERNEL);
197 	if (!infp) {
198 		warnf(fc, "vxfs: unable to allocate incore superblock");
199 		return -ENOMEM;
200 	}
201 
202 	bsize = sb_min_blocksize(sbp, BLOCK_SIZE);
203 	if (!bsize) {
204 		warnf(fc, "vxfs: unable to set blocksize");
205 		goto out;
206 	}
207 
208 	sbp->s_op = &vxfs_super_ops;
209 	sbp->s_fs_info = infp;
210 	sbp->s_time_min = 0;
211 	sbp->s_time_max = U32_MAX;
212 
213 	if (!vxfs_try_sb_magic(sbp, fc, 1,
214 			(__force __fs32)cpu_to_le32(VXFS_SUPER_MAGIC))) {
215 		/* Unixware, x86 */
216 		infp->byte_order = VXFS_BO_LE;
217 	} else if (!vxfs_try_sb_magic(sbp, fc, 8,
218 			(__force __fs32)cpu_to_be32(VXFS_SUPER_MAGIC))) {
219 		/* HP-UX, parisc */
220 		infp->byte_order = VXFS_BO_BE;
221 	} else {
222 		if (!silent)
223 			infof(fc, "vxfs: can't find superblock.");
224 		goto out;
225 	}
226 
227 	rsbp = infp->vsi_raw;
228 	j = fs32_to_cpu(infp, rsbp->vs_version);
229 	if ((j < 2 || j > 4) && !silent) {
230 		infof(fc, "vxfs: unsupported VxFS version (%d)", j);
231 		goto out;
232 	}
233 
234 #ifdef DIAGNOSTIC
235 	printk(KERN_DEBUG "vxfs: supported VxFS version (%d)\n", j);
236 	printk(KERN_DEBUG "vxfs: blocksize: %d\n",
237 		fs32_to_cpu(infp, rsbp->vs_bsize));
238 #endif
239 
240 	sbp->s_magic = fs32_to_cpu(infp, rsbp->vs_magic);
241 
242 	infp->vsi_oltext = fs32_to_cpu(infp, rsbp->vs_oltext[0]);
243 	infp->vsi_oltsize = fs32_to_cpu(infp, rsbp->vs_oltsize);
244 
245 	j = fs32_to_cpu(infp, rsbp->vs_bsize);
246 	if (!sb_set_blocksize(sbp, j)) {
247 		warnf(fc, "vxfs: unable to set final block size");
248 		goto out;
249 	}
250 
251 	if (vxfs_read_olt(sbp, bsize)) {
252 		warnf(fc, "vxfs: unable to read olt");
253 		goto out;
254 	}
255 
256 	if (vxfs_read_fshead(sbp)) {
257 		warnf(fc, "vxfs: unable to read fshead");
258 		goto out;
259 	}
260 
261 	root = vxfs_iget(sbp, VXFS_ROOT_INO);
262 	if (IS_ERR(root)) {
263 		ret = PTR_ERR(root);
264 		goto out;
265 	}
266 	sbp->s_root = d_make_root(root);
267 	if (!sbp->s_root) {
268 		warnf(fc, "vxfs: unable to get root dentry.");
269 		goto out_free_ilist;
270 	}
271 
272 	return 0;
273 
274 out_free_ilist:
275 	iput(infp->vsi_fship);
276 	iput(infp->vsi_ilist);
277 	iput(infp->vsi_stilist);
278 out:
279 	brelse(infp->vsi_bp);
280 	kfree(infp);
281 	return ret;
282 }
283 
284 /*
285  * The usual module blurb.
286  */
287 static int vxfs_get_tree(struct fs_context *fc)
288 {
289 	return get_tree_bdev(fc, vxfs_fill_super);
290 }
291 
292 static const struct fs_context_operations vxfs_context_ops = {
293 	.get_tree	= vxfs_get_tree,
294 	.reconfigure	= vxfs_reconfigure,
295 };
296 
297 static int vxfs_init_fs_context(struct fs_context *fc)
298 {
299 	fc->ops = &vxfs_context_ops;
300 
301 	return 0;
302 }
303 
304 static struct file_system_type vxfs_fs_type = {
305 	.owner		= THIS_MODULE,
306 	.name		= "vxfs",
307 	.kill_sb	= kill_block_super,
308 	.fs_flags	= FS_REQUIRES_DEV,
309 	.init_fs_context = vxfs_init_fs_context,
310 };
311 MODULE_ALIAS_FS("vxfs"); /* makes mount -t vxfs autoload the module */
312 MODULE_ALIAS("vxfs");
313 
314 static int __init
315 vxfs_init(void)
316 {
317 	int rv;
318 
319 	vxfs_inode_cachep = kmem_cache_create_usercopy("vxfs_inode",
320 			sizeof(struct vxfs_inode_info), 0,
321 			SLAB_RECLAIM_ACCOUNT,
322 			offsetof(struct vxfs_inode_info, vii_immed.vi_immed),
323 			sizeof_field(struct vxfs_inode_info,
324 				vii_immed.vi_immed),
325 			NULL);
326 	if (!vxfs_inode_cachep)
327 		return -ENOMEM;
328 	rv = register_filesystem(&vxfs_fs_type);
329 	if (rv < 0)
330 		kmem_cache_destroy(vxfs_inode_cachep);
331 	return rv;
332 }
333 
334 static void __exit
335 vxfs_cleanup(void)
336 {
337 	unregister_filesystem(&vxfs_fs_type);
338 	/*
339 	 * Make sure all delayed rcu free inodes are flushed before we
340 	 * destroy cache.
341 	 */
342 	rcu_barrier();
343 	kmem_cache_destroy(vxfs_inode_cachep);
344 }
345 
346 module_init(vxfs_init);
347 module_exit(vxfs_cleanup);
348