1 // SPDX-License-Identifier: GPL-1.0+
2 /*
3  *    Hypervisor filesystem for Linux on s390.
4  *
5  *    Copyright IBM Corp. 2006, 2008
6  *    Author(s): Michael Holzheu <holzheu@de.ibm.com>
7  */
8 
9 #define KMSG_COMPONENT "hypfs"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11 
12 #include <linux/types.h>
13 #include <linux/errno.h>
14 #include <linux/fs.h>
15 #include <linux/fs_context.h>
16 #include <linux/fs_parser.h>
17 #include <linux/namei.h>
18 #include <linux/vfs.h>
19 #include <linux/slab.h>
20 #include <linux/pagemap.h>
21 #include <linux/time.h>
22 #include <linux/sysfs.h>
23 #include <linux/init.h>
24 #include <linux/kobject.h>
25 #include <linux/seq_file.h>
26 #include <linux/uio.h>
27 #include <asm/machine.h>
28 #include <asm/ebcdic.h>
29 #include "hypfs.h"
30 
31 #define HYPFS_MAGIC 0x687970	/* ASCII 'hyp' */
32 #define TMP_SIZE 64		/* size of temporary buffers */
33 
34 static struct dentry *hypfs_create_update_file(struct dentry *dir);
35 
36 struct hypfs_sb_info {
37 	kuid_t uid;			/* uid used for files and dirs */
38 	kgid_t gid;			/* gid used for files and dirs */
39 	struct dentry *update_file;	/* file to trigger update */
40 	time64_t last_update;		/* last update, CLOCK_MONOTONIC time */
41 	struct mutex lock;		/* lock to protect update process */
42 };
43 
44 static const struct file_operations hypfs_file_ops;
45 static struct file_system_type hypfs_type;
46 static const struct super_operations hypfs_s_ops;
47 
48 /* start of list of all dentries, which have to be deleted on update */
49 static struct dentry *hypfs_last_dentry;
50 
51 static void hypfs_update_update(struct super_block *sb)
52 {
53 	struct hypfs_sb_info *sb_info = sb->s_fs_info;
54 	struct inode *inode = d_inode(sb_info->update_file);
55 
56 	sb_info->last_update = ktime_get_seconds();
57 	simple_inode_init_ts(inode);
58 }
59 
60 /* directory tree removal functions */
61 
62 static void hypfs_add_dentry(struct dentry *dentry)
63 {
64 	dentry->d_fsdata = hypfs_last_dentry;
65 	hypfs_last_dentry = dentry;
66 }
67 
68 static void hypfs_remove(struct dentry *dentry)
69 {
70 	struct dentry *parent;
71 
72 	parent = dentry->d_parent;
73 	inode_lock(d_inode(parent));
74 	if (simple_positive(dentry)) {
75 		if (d_is_dir(dentry))
76 			simple_rmdir(d_inode(parent), dentry);
77 		else
78 			simple_unlink(d_inode(parent), dentry);
79 	}
80 	d_drop(dentry);
81 	dput(dentry);
82 	inode_unlock(d_inode(parent));
83 }
84 
85 static void hypfs_delete_tree(struct dentry *root)
86 {
87 	while (hypfs_last_dentry) {
88 		struct dentry *next_dentry;
89 		next_dentry = hypfs_last_dentry->d_fsdata;
90 		hypfs_remove(hypfs_last_dentry);
91 		hypfs_last_dentry = next_dentry;
92 	}
93 }
94 
95 static struct inode *hypfs_make_inode(struct super_block *sb, umode_t mode)
96 {
97 	struct inode *ret = new_inode(sb);
98 
99 	if (ret) {
100 		struct hypfs_sb_info *hypfs_info = sb->s_fs_info;
101 		ret->i_ino = get_next_ino();
102 		ret->i_mode = mode;
103 		ret->i_uid = hypfs_info->uid;
104 		ret->i_gid = hypfs_info->gid;
105 		simple_inode_init_ts(ret);
106 		if (S_ISDIR(mode))
107 			set_nlink(ret, 2);
108 	}
109 	return ret;
110 }
111 
112 static void hypfs_evict_inode(struct inode *inode)
113 {
114 	clear_inode(inode);
115 	kfree(inode->i_private);
116 }
117 
118 static int hypfs_open(struct inode *inode, struct file *filp)
119 {
120 	char *data = file_inode(filp)->i_private;
121 	struct hypfs_sb_info *fs_info;
122 
123 	if (filp->f_mode & FMODE_WRITE) {
124 		if (!(inode->i_mode & S_IWUGO))
125 			return -EACCES;
126 	}
127 	if (filp->f_mode & FMODE_READ) {
128 		if (!(inode->i_mode & S_IRUGO))
129 			return -EACCES;
130 	}
131 
132 	fs_info = inode->i_sb->s_fs_info;
133 	if(data) {
134 		mutex_lock(&fs_info->lock);
135 		filp->private_data = kstrdup(data, GFP_KERNEL);
136 		if (!filp->private_data) {
137 			mutex_unlock(&fs_info->lock);
138 			return -ENOMEM;
139 		}
140 		mutex_unlock(&fs_info->lock);
141 	}
142 	return nonseekable_open(inode, filp);
143 }
144 
145 static ssize_t hypfs_read_iter(struct kiocb *iocb, struct iov_iter *to)
146 {
147 	struct file *file = iocb->ki_filp;
148 	char *data = file->private_data;
149 	size_t available = strlen(data);
150 	loff_t pos = iocb->ki_pos;
151 	size_t count;
152 
153 	if (pos < 0)
154 		return -EINVAL;
155 	if (pos >= available || !iov_iter_count(to))
156 		return 0;
157 	count = copy_to_iter(data + pos, available - pos, to);
158 	if (!count)
159 		return -EFAULT;
160 	iocb->ki_pos = pos + count;
161 	file_accessed(file);
162 	return count;
163 }
164 
165 static ssize_t hypfs_write_iter(struct kiocb *iocb, struct iov_iter *from)
166 {
167 	int rc;
168 	struct super_block *sb = file_inode(iocb->ki_filp)->i_sb;
169 	struct hypfs_sb_info *fs_info = sb->s_fs_info;
170 	size_t count = iov_iter_count(from);
171 
172 	/*
173 	 * Currently we only allow one update per second for two reasons:
174 	 * 1. diag 204 is VERY expensive
175 	 * 2. If several processes do updates in parallel and then read the
176 	 *    hypfs data, the likelihood of collisions is reduced, if we restrict
177 	 *    the minimum update interval. A collision occurs, if during the
178 	 *    data gathering of one process another process triggers an update
179 	 *    If the first process wants to ensure consistent data, it has
180 	 *    to restart data collection in this case.
181 	 */
182 	mutex_lock(&fs_info->lock);
183 	if (fs_info->last_update == ktime_get_seconds()) {
184 		rc = -EBUSY;
185 		goto out;
186 	}
187 	hypfs_delete_tree(sb->s_root);
188 	if (machine_is_vm())
189 		rc = hypfs_vm_create_files(sb->s_root);
190 	else
191 		rc = hypfs_diag_create_files(sb->s_root);
192 	if (rc) {
193 		pr_err("Updating the hypfs tree failed\n");
194 		hypfs_delete_tree(sb->s_root);
195 		goto out;
196 	}
197 	hypfs_update_update(sb);
198 	rc = count;
199 	iov_iter_advance(from, count);
200 out:
201 	mutex_unlock(&fs_info->lock);
202 	return rc;
203 }
204 
205 static int hypfs_release(struct inode *inode, struct file *filp)
206 {
207 	kfree(filp->private_data);
208 	return 0;
209 }
210 
211 enum { Opt_uid, Opt_gid, };
212 
213 static const struct fs_parameter_spec hypfs_fs_parameters[] = {
214 	fsparam_u32("gid", Opt_gid),
215 	fsparam_u32("uid", Opt_uid),
216 	{}
217 };
218 
219 static int hypfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
220 {
221 	struct hypfs_sb_info *hypfs_info = fc->s_fs_info;
222 	struct fs_parse_result result;
223 	kuid_t uid;
224 	kgid_t gid;
225 	int opt;
226 
227 	opt = fs_parse(fc, hypfs_fs_parameters, param, &result);
228 	if (opt < 0)
229 		return opt;
230 
231 	switch (opt) {
232 	case Opt_uid:
233 		uid = make_kuid(current_user_ns(), result.uint_32);
234 		if (!uid_valid(uid))
235 			return invalf(fc, "Unknown uid");
236 		hypfs_info->uid = uid;
237 		break;
238 	case Opt_gid:
239 		gid = make_kgid(current_user_ns(), result.uint_32);
240 		if (!gid_valid(gid))
241 			return invalf(fc, "Unknown gid");
242 		hypfs_info->gid = gid;
243 		break;
244 	}
245 	return 0;
246 }
247 
248 static int hypfs_show_options(struct seq_file *s, struct dentry *root)
249 {
250 	struct hypfs_sb_info *hypfs_info = root->d_sb->s_fs_info;
251 
252 	seq_printf(s, ",uid=%u", from_kuid_munged(&init_user_ns, hypfs_info->uid));
253 	seq_printf(s, ",gid=%u", from_kgid_munged(&init_user_ns, hypfs_info->gid));
254 	return 0;
255 }
256 
257 static int hypfs_fill_super(struct super_block *sb, struct fs_context *fc)
258 {
259 	struct hypfs_sb_info *sbi = sb->s_fs_info;
260 	struct inode *root_inode;
261 	struct dentry *root_dentry, *update_file;
262 	int rc;
263 
264 	sb->s_blocksize = PAGE_SIZE;
265 	sb->s_blocksize_bits = PAGE_SHIFT;
266 	sb->s_magic = HYPFS_MAGIC;
267 	sb->s_op = &hypfs_s_ops;
268 
269 	root_inode = hypfs_make_inode(sb, S_IFDIR | 0755);
270 	if (!root_inode)
271 		return -ENOMEM;
272 	root_inode->i_op = &simple_dir_inode_operations;
273 	root_inode->i_fop = &simple_dir_operations;
274 	sb->s_root = root_dentry = d_make_root(root_inode);
275 	if (!root_dentry)
276 		return -ENOMEM;
277 	if (machine_is_vm())
278 		rc = hypfs_vm_create_files(root_dentry);
279 	else
280 		rc = hypfs_diag_create_files(root_dentry);
281 	if (rc)
282 		return rc;
283 	update_file = hypfs_create_update_file(root_dentry);
284 	if (IS_ERR(update_file))
285 		return PTR_ERR(update_file);
286 	sbi->update_file = update_file;
287 	hypfs_update_update(sb);
288 	pr_info("Hypervisor filesystem mounted\n");
289 	return 0;
290 }
291 
292 static int hypfs_get_tree(struct fs_context *fc)
293 {
294 	return get_tree_single(fc, hypfs_fill_super);
295 }
296 
297 static void hypfs_free_fc(struct fs_context *fc)
298 {
299 	kfree(fc->s_fs_info);
300 }
301 
302 static const struct fs_context_operations hypfs_context_ops = {
303 	.free		= hypfs_free_fc,
304 	.parse_param	= hypfs_parse_param,
305 	.get_tree	= hypfs_get_tree,
306 };
307 
308 static int hypfs_init_fs_context(struct fs_context *fc)
309 {
310 	struct hypfs_sb_info *sbi;
311 
312 	sbi = kzalloc(sizeof(struct hypfs_sb_info), GFP_KERNEL);
313 	if (!sbi)
314 		return -ENOMEM;
315 
316 	mutex_init(&sbi->lock);
317 	sbi->uid = current_uid();
318 	sbi->gid = current_gid();
319 
320 	fc->s_fs_info = sbi;
321 	fc->ops = &hypfs_context_ops;
322 	return 0;
323 }
324 
325 static void hypfs_kill_super(struct super_block *sb)
326 {
327 	struct hypfs_sb_info *sb_info = sb->s_fs_info;
328 
329 	if (sb->s_root)
330 		hypfs_delete_tree(sb->s_root);
331 	if (sb_info && sb_info->update_file)
332 		hypfs_remove(sb_info->update_file);
333 	kfree(sb->s_fs_info);
334 	sb->s_fs_info = NULL;
335 	kill_litter_super(sb);
336 }
337 
338 static struct dentry *hypfs_create_file(struct dentry *parent, const char *name,
339 					char *data, umode_t mode)
340 {
341 	struct dentry *dentry;
342 	struct inode *inode;
343 
344 	inode_lock(d_inode(parent));
345 	dentry = lookup_noperm(&QSTR(name), parent);
346 	if (IS_ERR(dentry)) {
347 		dentry = ERR_PTR(-ENOMEM);
348 		goto fail;
349 	}
350 	inode = hypfs_make_inode(parent->d_sb, mode);
351 	if (!inode) {
352 		dput(dentry);
353 		dentry = ERR_PTR(-ENOMEM);
354 		goto fail;
355 	}
356 	if (S_ISREG(mode)) {
357 		inode->i_fop = &hypfs_file_ops;
358 		if (data)
359 			inode->i_size = strlen(data);
360 		else
361 			inode->i_size = 0;
362 	} else if (S_ISDIR(mode)) {
363 		inode->i_op = &simple_dir_inode_operations;
364 		inode->i_fop = &simple_dir_operations;
365 		inc_nlink(d_inode(parent));
366 	} else
367 		BUG();
368 	inode->i_private = data;
369 	d_instantiate(dentry, inode);
370 	dget(dentry);
371 fail:
372 	inode_unlock(d_inode(parent));
373 	return dentry;
374 }
375 
376 struct dentry *hypfs_mkdir(struct dentry *parent, const char *name)
377 {
378 	struct dentry *dentry;
379 
380 	dentry = hypfs_create_file(parent, name, NULL, S_IFDIR | DIR_MODE);
381 	if (IS_ERR(dentry))
382 		return dentry;
383 	hypfs_add_dentry(dentry);
384 	return dentry;
385 }
386 
387 static struct dentry *hypfs_create_update_file(struct dentry *dir)
388 {
389 	struct dentry *dentry;
390 
391 	dentry = hypfs_create_file(dir, "update", NULL,
392 				   S_IFREG | UPDATE_FILE_MODE);
393 	/*
394 	 * We do not put the update file on the 'delete' list with
395 	 * hypfs_add_dentry(), since it should not be removed when the tree
396 	 * is updated.
397 	 */
398 	return dentry;
399 }
400 
401 struct dentry *hypfs_create_u64(struct dentry *dir,
402 				const char *name, __u64 value)
403 {
404 	char *buffer;
405 	char tmp[TMP_SIZE];
406 	struct dentry *dentry;
407 
408 	snprintf(tmp, TMP_SIZE, "%llu\n", (unsigned long long int)value);
409 	buffer = kstrdup(tmp, GFP_KERNEL);
410 	if (!buffer)
411 		return ERR_PTR(-ENOMEM);
412 	dentry =
413 	    hypfs_create_file(dir, name, buffer, S_IFREG | REG_FILE_MODE);
414 	if (IS_ERR(dentry)) {
415 		kfree(buffer);
416 		return ERR_PTR(-ENOMEM);
417 	}
418 	hypfs_add_dentry(dentry);
419 	return dentry;
420 }
421 
422 struct dentry *hypfs_create_str(struct dentry *dir,
423 				const char *name, char *string)
424 {
425 	char *buffer;
426 	struct dentry *dentry;
427 
428 	buffer = kmalloc(strlen(string) + 2, GFP_KERNEL);
429 	if (!buffer)
430 		return ERR_PTR(-ENOMEM);
431 	sprintf(buffer, "%s\n", string);
432 	dentry =
433 	    hypfs_create_file(dir, name, buffer, S_IFREG | REG_FILE_MODE);
434 	if (IS_ERR(dentry)) {
435 		kfree(buffer);
436 		return ERR_PTR(-ENOMEM);
437 	}
438 	hypfs_add_dentry(dentry);
439 	return dentry;
440 }
441 
442 static const struct file_operations hypfs_file_ops = {
443 	.open		= hypfs_open,
444 	.release	= hypfs_release,
445 	.read_iter	= hypfs_read_iter,
446 	.write_iter	= hypfs_write_iter,
447 };
448 
449 static struct file_system_type hypfs_type = {
450 	.owner		= THIS_MODULE,
451 	.name		= "s390_hypfs",
452 	.init_fs_context = hypfs_init_fs_context,
453 	.parameters	= hypfs_fs_parameters,
454 	.kill_sb	= hypfs_kill_super
455 };
456 
457 static const struct super_operations hypfs_s_ops = {
458 	.statfs		= simple_statfs,
459 	.evict_inode	= hypfs_evict_inode,
460 	.show_options	= hypfs_show_options,
461 };
462 
463 int __init __hypfs_fs_init(void)
464 {
465 	int rc;
466 
467 	rc = sysfs_create_mount_point(hypervisor_kobj, "s390");
468 	if (rc)
469 		return rc;
470 	rc = register_filesystem(&hypfs_type);
471 	if (rc)
472 		goto fail;
473 	return 0;
474 fail:
475 	sysfs_remove_mount_point(hypervisor_kobj, "s390");
476 	return rc;
477 }
478