xref: /linux/fs/efs/super.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * super.c
4  *
5  * Copyright (c) 1999 Al Smith
6  *
7  * Portions derived from work (c) 1995,1996 Christian Vogelgsang.
8  */
9 
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/exportfs.h>
13 #include <linux/slab.h>
14 #include <linux/buffer_head.h>
15 #include <linux/vfs.h>
16 #include <linux/blkdev.h>
17 #include <linux/fs_context.h>
18 #include "efs.h"
19 #include <linux/efs_vh.h>
20 #include <linux/efs_fs_sb.h>
21 
22 static int efs_statfs(struct dentry *dentry, struct kstatfs *buf);
23 static int efs_init_fs_context(struct fs_context *fc);
24 
25 static void efs_kill_sb(struct super_block *s)
26 {
27 	struct efs_sb_info *sbi = SUPER_INFO(s);
28 	kill_block_super(s);
29 	kfree(sbi);
30 }
31 
32 static struct pt_types sgi_pt_types[] = {
33 	{0x00,		"SGI vh"},
34 	{0x01,		"SGI trkrepl"},
35 	{0x02,		"SGI secrepl"},
36 	{0x03,		"SGI raw"},
37 	{0x04,		"SGI bsd"},
38 	{SGI_SYSV,	"SGI sysv"},
39 	{0x06,		"SGI vol"},
40 	{SGI_EFS,	"SGI efs"},
41 	{0x08,		"SGI lv"},
42 	{0x09,		"SGI rlv"},
43 	{0x0A,		"SGI xfs"},
44 	{0x0B,		"SGI xfslog"},
45 	{0x0C,		"SGI xlv"},
46 	{0x82,		"Linux swap"},
47 	{0x83,		"Linux native"},
48 	{0,		NULL}
49 };
50 
51 /*
52  * File system definition and registration.
53  */
54 static struct file_system_type efs_fs_type = {
55 	.owner			= THIS_MODULE,
56 	.name			= "efs",
57 	.kill_sb		= efs_kill_sb,
58 	.fs_flags		= FS_REQUIRES_DEV,
59 	.init_fs_context	= efs_init_fs_context,
60 };
61 MODULE_ALIAS_FS("efs");
62 
63 static struct kmem_cache * efs_inode_cachep;
64 
65 static struct inode *efs_alloc_inode(struct super_block *sb)
66 {
67 	struct efs_inode_info *ei;
68 	ei = alloc_inode_sb(sb, efs_inode_cachep, GFP_KERNEL);
69 	if (!ei)
70 		return NULL;
71 	return &ei->vfs_inode;
72 }
73 
74 static void efs_free_inode(struct inode *inode)
75 {
76 	kmem_cache_free(efs_inode_cachep, INODE_INFO(inode));
77 }
78 
79 static void init_once(void *foo)
80 {
81 	struct efs_inode_info *ei = (struct efs_inode_info *) foo;
82 
83 	inode_init_once(&ei->vfs_inode);
84 }
85 
86 static int __init init_inodecache(void)
87 {
88 	efs_inode_cachep = kmem_cache_create("efs_inode_cache",
89 				sizeof(struct efs_inode_info), 0,
90 				SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT,
91 				init_once);
92 	if (efs_inode_cachep == NULL)
93 		return -ENOMEM;
94 	return 0;
95 }
96 
97 static void destroy_inodecache(void)
98 {
99 	/*
100 	 * Make sure all delayed rcu free inodes are flushed before we
101 	 * destroy cache.
102 	 */
103 	rcu_barrier();
104 	kmem_cache_destroy(efs_inode_cachep);
105 }
106 
107 static const struct super_operations efs_superblock_operations = {
108 	.alloc_inode	= efs_alloc_inode,
109 	.free_inode	= efs_free_inode,
110 	.statfs		= efs_statfs,
111 };
112 
113 static const struct export_operations efs_export_ops = {
114 	.encode_fh	= generic_encode_ino32_fh,
115 	.fh_to_dentry	= efs_fh_to_dentry,
116 	.fh_to_parent	= efs_fh_to_parent,
117 	.get_parent	= efs_get_parent,
118 };
119 
120 static int __init init_efs_fs(void) {
121 	int err;
122 	pr_info(EFS_VERSION" - http://aeschi.ch.eu.org/efs/\n");
123 	err = init_inodecache();
124 	if (err)
125 		goto out1;
126 	err = register_filesystem(&efs_fs_type);
127 	if (err)
128 		goto out;
129 	return 0;
130 out:
131 	destroy_inodecache();
132 out1:
133 	return err;
134 }
135 
136 static void __exit exit_efs_fs(void) {
137 	unregister_filesystem(&efs_fs_type);
138 	destroy_inodecache();
139 }
140 
141 module_init(init_efs_fs)
142 module_exit(exit_efs_fs)
143 
144 static efs_block_t efs_validate_vh(struct volume_header *vh) {
145 	int		i;
146 	__be32		cs, *ui;
147 	int		csum;
148 	efs_block_t	sblock = 0; /* shuts up gcc */
149 	struct pt_types	*pt_entry;
150 	int		pt_type, slice = -1;
151 
152 	if (be32_to_cpu(vh->vh_magic) != VHMAGIC) {
153 		/*
154 		 * assume that we're dealing with a partition and allow
155 		 * read_super() to try and detect a valid superblock
156 		 * on the next block.
157 		 */
158 		return 0;
159 	}
160 
161 	ui = ((__be32 *) (vh + 1)) - 1;
162 	for(csum = 0; ui >= ((__be32 *) vh);) {
163 		cs = *ui--;
164 		csum += be32_to_cpu(cs);
165 	}
166 	if (csum) {
167 		pr_warn("SGI disklabel: checksum bad, label corrupted\n");
168 		return 0;
169 	}
170 
171 #ifdef DEBUG
172 	pr_debug("bf: \"%16s\"\n", vh->vh_bootfile);
173 
174 	for(i = 0; i < NVDIR; i++) {
175 		int	j;
176 		char	name[VDNAMESIZE+1];
177 
178 		for(j = 0; j < VDNAMESIZE; j++) {
179 			name[j] = vh->vh_vd[i].vd_name[j];
180 		}
181 		name[j] = (char) 0;
182 
183 		if (name[0]) {
184 			pr_debug("vh: %8s block: 0x%08x size: 0x%08x\n",
185 				name, (int) be32_to_cpu(vh->vh_vd[i].vd_lbn),
186 				(int) be32_to_cpu(vh->vh_vd[i].vd_nbytes));
187 		}
188 	}
189 #endif
190 
191 	for(i = 0; i < NPARTAB; i++) {
192 		pt_type = (int) be32_to_cpu(vh->vh_pt[i].pt_type);
193 		for(pt_entry = sgi_pt_types; pt_entry->pt_name; pt_entry++) {
194 			if (pt_type == pt_entry->pt_type) break;
195 		}
196 #ifdef DEBUG
197 		if (be32_to_cpu(vh->vh_pt[i].pt_nblks)) {
198 			pr_debug("pt %2d: start: %08d size: %08d type: 0x%02x (%s)\n",
199 				 i, (int)be32_to_cpu(vh->vh_pt[i].pt_firstlbn),
200 				 (int)be32_to_cpu(vh->vh_pt[i].pt_nblks),
201 				 pt_type, (pt_entry->pt_name) ?
202 				 pt_entry->pt_name : "unknown");
203 		}
204 #endif
205 		if (IS_EFS(pt_type)) {
206 			sblock = be32_to_cpu(vh->vh_pt[i].pt_firstlbn);
207 			slice = i;
208 		}
209 	}
210 
211 	if (slice == -1) {
212 		pr_notice("partition table contained no EFS partitions\n");
213 #ifdef DEBUG
214 	} else {
215 		pr_info("using slice %d (type %s, offset 0x%x)\n", slice,
216 			(pt_entry->pt_name) ? pt_entry->pt_name : "unknown",
217 			sblock);
218 #endif
219 	}
220 	return sblock;
221 }
222 
223 static int efs_validate_super(struct efs_sb_info *sb, struct efs_super *super) {
224 
225 	if (!IS_EFS_MAGIC(be32_to_cpu(super->fs_magic)))
226 		return -1;
227 
228 	sb->fs_magic     = be32_to_cpu(super->fs_magic);
229 	sb->total_blocks = be32_to_cpu(super->fs_size);
230 	sb->first_block  = be32_to_cpu(super->fs_firstcg);
231 	sb->group_size   = be32_to_cpu(super->fs_cgfsize);
232 	sb->data_free    = be32_to_cpu(super->fs_tfree);
233 	sb->inode_free   = be32_to_cpu(super->fs_tinode);
234 	sb->inode_blocks = be16_to_cpu(super->fs_cgisize);
235 	sb->total_groups = be16_to_cpu(super->fs_ncg);
236 
237 	return 0;
238 }
239 
240 static int efs_fill_super(struct super_block *s, struct fs_context *fc)
241 {
242 	struct efs_sb_info *sb;
243 	struct buffer_head *bh;
244 	struct inode *root;
245 
246 	sb = kzalloc(sizeof(struct efs_sb_info), GFP_KERNEL);
247 	if (!sb)
248 		return -ENOMEM;
249 	s->s_fs_info = sb;
250 	s->s_time_min = 0;
251 	s->s_time_max = U32_MAX;
252 
253 	s->s_magic		= EFS_SUPER_MAGIC;
254 	if (!sb_set_blocksize(s, EFS_BLOCKSIZE)) {
255 		pr_err("device does not support %d byte blocks\n",
256 			EFS_BLOCKSIZE);
257 		return invalf(fc, "device does not support %d byte blocks\n",
258 			      EFS_BLOCKSIZE);
259 	}
260 
261 	/* read the vh (volume header) block */
262 	bh = sb_bread(s, 0);
263 
264 	if (!bh) {
265 		pr_err("cannot read volume header\n");
266 		return -EIO;
267 	}
268 
269 	/*
270 	 * if this returns zero then we didn't find any partition table.
271 	 * this isn't (yet) an error - just assume for the moment that
272 	 * the device is valid and go on to search for a superblock.
273 	 */
274 	sb->fs_start = efs_validate_vh((struct volume_header *) bh->b_data);
275 	brelse(bh);
276 
277 	if (sb->fs_start == -1) {
278 		return -EINVAL;
279 	}
280 
281 	bh = sb_bread(s, sb->fs_start + EFS_SUPER);
282 	if (!bh) {
283 		pr_err("cannot read superblock\n");
284 		return -EIO;
285 	}
286 
287 	if (efs_validate_super(sb, (struct efs_super *) bh->b_data)) {
288 #ifdef DEBUG
289 		pr_warn("invalid superblock at block %u\n",
290 			sb->fs_start + EFS_SUPER);
291 #endif
292 		brelse(bh);
293 		return -EINVAL;
294 	}
295 	brelse(bh);
296 
297 	if (!sb_rdonly(s)) {
298 #ifdef DEBUG
299 		pr_info("forcing read-only mode\n");
300 #endif
301 		s->s_flags |= SB_RDONLY;
302 	}
303 	s->s_op   = &efs_superblock_operations;
304 	s->s_export_op = &efs_export_ops;
305 	root = efs_iget(s, EFS_ROOTINODE);
306 	if (IS_ERR(root)) {
307 		pr_err("get root inode failed\n");
308 		return PTR_ERR(root);
309 	}
310 
311 	s->s_root = d_make_root(root);
312 	if (!(s->s_root)) {
313 		pr_err("get root dentry failed\n");
314 		return -ENOMEM;
315 	}
316 
317 	return 0;
318 }
319 
320 static int efs_get_tree(struct fs_context *fc)
321 {
322 	return get_tree_bdev(fc, efs_fill_super);
323 }
324 
325 static int efs_reconfigure(struct fs_context *fc)
326 {
327 	sync_filesystem(fc->root->d_sb);
328 	fc->sb_flags |= SB_RDONLY;
329 
330 	return 0;
331 }
332 
333 static const struct fs_context_operations efs_context_opts = {
334 	.get_tree	= efs_get_tree,
335 	.reconfigure	= efs_reconfigure,
336 };
337 
338 /*
339  * Set up the filesystem mount context.
340  */
341 static int efs_init_fs_context(struct fs_context *fc)
342 {
343 	fc->ops = &efs_context_opts;
344 
345 	return 0;
346 }
347 
348 static int efs_statfs(struct dentry *dentry, struct kstatfs *buf) {
349 	struct super_block *sb = dentry->d_sb;
350 	struct efs_sb_info *sbi = SUPER_INFO(sb);
351 	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
352 
353 	buf->f_type    = EFS_SUPER_MAGIC;	/* efs magic number */
354 	buf->f_bsize   = EFS_BLOCKSIZE;		/* blocksize */
355 	buf->f_blocks  = sbi->total_groups *	/* total data blocks */
356 			(sbi->group_size - sbi->inode_blocks);
357 	buf->f_bfree   = sbi->data_free;	/* free data blocks */
358 	buf->f_bavail  = sbi->data_free;	/* free blocks for non-root */
359 	buf->f_files   = sbi->total_groups *	/* total inodes */
360 			sbi->inode_blocks *
361 			(EFS_BLOCKSIZE / sizeof(struct efs_dinode));
362 	buf->f_ffree   = sbi->inode_free;	/* free inodes */
363 	buf->f_fsid    = u64_to_fsid(id);
364 	buf->f_namelen = EFS_MAXNAMELEN;	/* max filename length */
365 
366 	return 0;
367 }
368 
369