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