1 // SPDX-License-Identifier: GPL-1.0+
3 * Hypervisor filesystem for Linux on s390.
5 * Copyright IBM Corp. 2006, 2008
6 * Author(s): Michael Holzheu <holzheu@de.ibm.com>
9 #define KMSG_COMPONENT "hypfs"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 #include <linux/types.h>
13 #include <linux/errno.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>
31 #define HYPFS_MAGIC 0x687970 /* ASCII 'hyp' */
32 #define TMP_SIZE 64 /* size of temporary buffers */
34 static struct dentry *hypfs_create_update_file(struct dentry *dir);
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 */
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;
48 /* start of list of all dentries, which have to be deleted on update */
49 static struct dentry *hypfs_last_dentry;
51 static void hypfs_update_update(struct super_block *sb)
53 struct hypfs_sb_info *sb_info = sb->s_fs_info;
54 struct inode *inode = d_inode(sb_info->update_file);
56 sb_info->last_update = ktime_get_seconds();
57 simple_inode_init_ts(inode);
60 /* directory tree removal functions */
62 static void hypfs_add_dentry(struct dentry *dentry)
64 dentry->d_fsdata = hypfs_last_dentry;
65 hypfs_last_dentry = dentry;
68 static void hypfs_remove(struct dentry *dentry)
70 struct dentry *parent;
72 parent = dentry->d_parent;
73 inode_lock(d_inode(parent));
74 if (simple_positive(dentry)) {
76 simple_rmdir(d_inode(parent), dentry);
78 simple_unlink(d_inode(parent), dentry);
82 inode_unlock(d_inode(parent));
85 static void hypfs_delete_tree(struct dentry *root)
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;
95 static struct inode *hypfs_make_inode(struct super_block *sb, umode_t mode)
97 struct inode *ret = new_inode(sb);
100 struct hypfs_sb_info *hypfs_info = sb->s_fs_info;
101 ret->i_ino = get_next_ino();
103 ret->i_uid = hypfs_info->uid;
104 ret->i_gid = hypfs_info->gid;
105 simple_inode_init_ts(ret);
112 static void hypfs_evict_inode(struct inode *inode)
115 kfree(inode->i_private);
118 static int hypfs_open(struct inode *inode, struct file *filp)
120 char *data = file_inode(filp)->i_private;
121 struct hypfs_sb_info *fs_info;
123 if (filp->f_mode & FMODE_WRITE) {
124 if (!(inode->i_mode & S_IWUGO))
127 if (filp->f_mode & FMODE_READ) {
128 if (!(inode->i_mode & S_IRUGO))
132 fs_info = inode->i_sb->s_fs_info;
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);
140 mutex_unlock(&fs_info->lock);
142 return nonseekable_open(inode, filp);
145 static ssize_t hypfs_read_iter(struct kiocb *iocb, struct iov_iter *to)
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;
155 if (pos >= available || !iov_iter_count(to))
157 count = copy_to_iter(data + pos, available - pos, to);
160 iocb->ki_pos = pos + count;
165 static ssize_t hypfs_write_iter(struct kiocb *iocb, struct iov_iter *from)
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);
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.
182 mutex_lock(&fs_info->lock);
183 if (fs_info->last_update == ktime_get_seconds()) {
187 hypfs_delete_tree(sb->s_root);
189 rc = hypfs_vm_create_files(sb->s_root);
191 rc = hypfs_diag_create_files(sb->s_root);
193 pr_err("Updating the hypfs tree failed\n");
194 hypfs_delete_tree(sb->s_root);
197 hypfs_update_update(sb);
199 iov_iter_advance(from, count);
201 mutex_unlock(&fs_info->lock);
205 static int hypfs_release(struct inode *inode, struct file *filp)
207 kfree(filp->private_data);
211 enum { Opt_uid, Opt_gid, };
213 static const struct fs_parameter_spec hypfs_fs_parameters[] = {
214 fsparam_u32("gid", Opt_gid),
215 fsparam_u32("uid", Opt_uid),
219 static int hypfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
221 struct hypfs_sb_info *hypfs_info = fc->s_fs_info;
222 struct fs_parse_result result;
227 opt = fs_parse(fc, hypfs_fs_parameters, param, &result);
233 uid = make_kuid(current_user_ns(), result.uint_32);
235 return invalf(fc, "Unknown uid");
236 hypfs_info->uid = uid;
239 gid = make_kgid(current_user_ns(), result.uint_32);
241 return invalf(fc, "Unknown gid");
242 hypfs_info->gid = gid;
248 static int hypfs_show_options(struct seq_file *s, struct dentry *root)
250 struct hypfs_sb_info *hypfs_info = root->d_sb->s_fs_info;
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));
257 static int hypfs_fill_super(struct super_block *sb, struct fs_context *fc)
259 struct hypfs_sb_info *sbi = sb->s_fs_info;
260 struct inode *root_inode;
261 struct dentry *root_dentry, *update_file;
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;
269 root_inode = hypfs_make_inode(sb, S_IFDIR | 0755);
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);
278 rc = hypfs_vm_create_files(root_dentry);
280 rc = hypfs_diag_create_files(root_dentry);
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");
292 static int hypfs_get_tree(struct fs_context *fc)
294 return get_tree_single(fc, hypfs_fill_super);
297 static void hypfs_free_fc(struct fs_context *fc)
299 kfree(fc->s_fs_info);
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,
308 static int hypfs_init_fs_context(struct fs_context *fc)
310 struct hypfs_sb_info *sbi;
312 sbi = kzalloc(sizeof(struct hypfs_sb_info), GFP_KERNEL);
316 mutex_init(&sbi->lock);
317 sbi->uid = current_uid();
318 sbi->gid = current_gid();
321 fc->ops = &hypfs_context_ops;
325 static void hypfs_kill_super(struct super_block *sb)
327 struct hypfs_sb_info *sb_info = sb->s_fs_info;
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);
338 static struct dentry *hypfs_create_file(struct dentry *parent, const char *name,
339 char *data, umode_t mode)
341 struct dentry *dentry;
344 inode_lock(d_inode(parent));
345 dentry = lookup_noperm(&QSTR(name), parent);
346 if (IS_ERR(dentry)) {
347 dentry = ERR_PTR(-ENOMEM);
350 inode = hypfs_make_inode(parent->d_sb, mode);
353 dentry = ERR_PTR(-ENOMEM);
357 inode->i_fop = &hypfs_file_ops;
359 inode->i_size = strlen(data);
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));
368 inode->i_private = data;
369 d_instantiate(dentry, inode);
372 inode_unlock(d_inode(parent));
376 struct dentry *hypfs_mkdir(struct dentry *parent, const char *name)
378 struct dentry *dentry;
380 dentry = hypfs_create_file(parent, name, NULL, S_IFDIR | DIR_MODE);
383 hypfs_add_dentry(dentry);
387 static struct dentry *hypfs_create_update_file(struct dentry *dir)
389 struct dentry *dentry;
391 dentry = hypfs_create_file(dir, "update", NULL,
392 S_IFREG | UPDATE_FILE_MODE);
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
401 struct dentry *hypfs_create_u64(struct dentry *dir,
402 const char *name, __u64 value)
406 struct dentry *dentry;
408 snprintf(tmp, TMP_SIZE, "%llu\n", (unsigned long long int)value);
409 buffer = kstrdup(tmp, GFP_KERNEL);
411 return ERR_PTR(-ENOMEM);
413 hypfs_create_file(dir, name, buffer, S_IFREG | REG_FILE_MODE);
414 if (IS_ERR(dentry)) {
416 return ERR_PTR(-ENOMEM);
418 hypfs_add_dentry(dentry);
422 struct dentry *hypfs_create_str(struct dentry *dir,
423 const char *name, char *string)
426 struct dentry *dentry;
428 buffer = kmalloc(strlen(string) + 2, GFP_KERNEL);
430 return ERR_PTR(-ENOMEM);
431 sprintf(buffer, "%s\n", string);
433 hypfs_create_file(dir, name, buffer, S_IFREG | REG_FILE_MODE);
434 if (IS_ERR(dentry)) {
436 return ERR_PTR(-ENOMEM);
438 hypfs_add_dentry(dentry);
442 static const struct file_operations hypfs_file_ops = {
444 .release = hypfs_release,
445 .read_iter = hypfs_read_iter,
446 .write_iter = hypfs_write_iter,
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
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,
463 int __init __hypfs_fs_init(void)
467 rc = sysfs_create_mount_point(hypervisor_kobj, "s390");
470 rc = register_filesystem(&hypfs_type);
475 sysfs_remove_mount_point(hypervisor_kobj, "s390");