Linux 6.16-rc6
[linux-2.6-block.git] / arch / s390 / hypfs / inode.c
CommitLineData
0b73214f 1// SPDX-License-Identifier: GPL-1.0+
24bbb1fa 2/*
24bbb1fa
MH
3 * Hypervisor filesystem for Linux on s390.
4 *
f55495ba 5 * Copyright IBM Corp. 2006, 2008
24bbb1fa
MH
6 * Author(s): Michael Holzheu <holzheu@de.ibm.com>
7 */
8
f55495ba
MH
9#define KMSG_COMPONENT "hypfs"
10#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
24bbb1fa
MH
12#include <linux/types.h>
13#include <linux/errno.h>
14#include <linux/fs.h>
8f8d8d11
DH
15#include <linux/fs_context.h>
16#include <linux/fs_parser.h>
24bbb1fa
MH
17#include <linux/namei.h>
18#include <linux/vfs.h>
5a0e3ad6 19#include <linux/slab.h>
24bbb1fa 20#include <linux/pagemap.h>
24bbb1fa 21#include <linux/time.h>
24bbb1fa 22#include <linux/sysfs.h>
cee672e1
PG
23#include <linux/init.h>
24#include <linux/kobject.h>
b01af5ba 25#include <linux/seq_file.h>
e2e40f2c 26#include <linux/uio.h>
52109a06 27#include <asm/machine.h>
24bbb1fa
MH
28#include <asm/ebcdic.h>
29#include "hypfs.h"
24bbb1fa
MH
30
31#define HYPFS_MAGIC 0x687970 /* ASCII 'hyp' */
32#define TMP_SIZE 64 /* size of temporary buffers */
33
a118cfdf 34static struct dentry *hypfs_create_update_file(struct dentry *dir);
24bbb1fa
MH
35
36struct hypfs_sb_info {
6a62a216
EB
37 kuid_t uid; /* uid used for files and dirs */
38 kgid_t gid; /* gid used for files and dirs */
24bbb1fa 39 struct dentry *update_file; /* file to trigger update */
6075e4ae 40 time64_t last_update; /* last update, CLOCK_MONOTONIC time */
24bbb1fa
MH
41 struct mutex lock; /* lock to protect update process */
42};
43
5dfe4c96 44static const struct file_operations hypfs_file_ops;
24bbb1fa 45static struct file_system_type hypfs_type;
b87221de 46static const struct super_operations hypfs_s_ops;
24bbb1fa
MH
47
48/* start of list of all dentries, which have to be deleted on update */
49static struct dentry *hypfs_last_dentry;
50
51static void hypfs_update_update(struct super_block *sb)
52{
53 struct hypfs_sb_info *sb_info = sb->s_fs_info;
75c3cfa8 54 struct inode *inode = d_inode(sb_info->update_file);
24bbb1fa 55
6075e4ae 56 sb_info->last_update = ktime_get_seconds();
9304a99e 57 simple_inode_init_ts(inode);
24bbb1fa
MH
58}
59
60/* directory tree removal functions */
61
62static void hypfs_add_dentry(struct dentry *dentry)
63{
64 dentry->d_fsdata = hypfs_last_dentry;
65 hypfs_last_dentry = dentry;
66}
67
68static void hypfs_remove(struct dentry *dentry)
69{
70 struct dentry *parent;
71
72 parent = dentry->d_parent;
5955102c 73 inode_lock(d_inode(parent));
dc3f4198 74 if (simple_positive(dentry)) {
e36cb0b8 75 if (d_is_dir(dentry))
75c3cfa8 76 simple_rmdir(d_inode(parent), dentry);
9b5a03e1 77 else
75c3cfa8 78 simple_unlink(d_inode(parent), dentry);
9b5a03e1 79 }
29dfeb0b 80 d_drop(dentry);
24bbb1fa 81 dput(dentry);
5955102c 82 inode_unlock(d_inode(parent));
24bbb1fa
MH
83}
84
85static 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
fec0ebae 95static struct inode *hypfs_make_inode(struct super_block *sb, umode_t mode)
24bbb1fa
MH
96{
97 struct inode *ret = new_inode(sb);
98
99 if (ret) {
100 struct hypfs_sb_info *hypfs_info = sb->s_fs_info;
c960bec4 101 ret->i_ino = get_next_ino();
24bbb1fa
MH
102 ret->i_mode = mode;
103 ret->i_uid = hypfs_info->uid;
104 ret->i_gid = hypfs_info->gid;
9304a99e 105 simple_inode_init_ts(ret);
fec0ebae 106 if (S_ISDIR(mode))
bfe86848 107 set_nlink(ret, 2);
24bbb1fa
MH
108 }
109 return ret;
110}
111
b69257f2 112static void hypfs_evict_inode(struct inode *inode)
24bbb1fa 113{
dbd5768f 114 clear_inode(inode);
8e18e294 115 kfree(inode->i_private);
24bbb1fa
MH
116}
117
118static int hypfs_open(struct inode *inode, struct file *filp)
119{
496ad9aa 120 char *data = file_inode(filp)->i_private;
24bbb1fa
MH
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 }
58ea91c0 142 return nonseekable_open(inode, filp);
24bbb1fa
MH
143}
144
a457ac28 145static ssize_t hypfs_read_iter(struct kiocb *iocb, struct iov_iter *to)
24bbb1fa 146{
a457ac28
AV
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;
a29591c4 152
a457ac28
AV
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;
24bbb1fa 163}
a457ac28
AV
164
165static ssize_t hypfs_write_iter(struct kiocb *iocb, struct iov_iter *from)
24bbb1fa
MH
166{
167 int rc;
6131ffaa
AV
168 struct super_block *sb = file_inode(iocb->ki_filp)->i_sb;
169 struct hypfs_sb_info *fs_info = sb->s_fs_info;
a457ac28 170 size_t count = iov_iter_count(from);
24bbb1fa 171
24bbb1fa
MH
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);
6075e4ae 183 if (fs_info->last_update == ktime_get_seconds()) {
24bbb1fa
MH
184 rc = -EBUSY;
185 goto out;
186 }
187 hypfs_delete_tree(sb->s_root);
52109a06 188 if (machine_is_vm())
f78e41e3 189 rc = hypfs_vm_create_files(sb->s_root);
31cb4bd3 190 else
e334cf4f 191 rc = hypfs_diag_create_files(sb->s_root);
24bbb1fa 192 if (rc) {
f55495ba 193 pr_err("Updating the hypfs tree failed\n");
24bbb1fa
MH
194 hypfs_delete_tree(sb->s_root);
195 goto out;
196 }
197 hypfs_update_update(sb);
198 rc = count;
a457ac28 199 iov_iter_advance(from, count);
24bbb1fa
MH
200out:
201 mutex_unlock(&fs_info->lock);
202 return rc;
203}
204
205static int hypfs_release(struct inode *inode, struct file *filp)
206{
207 kfree(filp->private_data);
208 return 0;
209}
210
8f8d8d11 211enum { Opt_uid, Opt_gid, };
24bbb1fa 212
d7167b14 213static const struct fs_parameter_spec hypfs_fs_parameters[] = {
8f8d8d11
DH
214 fsparam_u32("gid", Opt_gid),
215 fsparam_u32("uid", Opt_uid),
216 {}
24bbb1fa
MH
217};
218
8f8d8d11 219static int hypfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
24bbb1fa 220{
8f8d8d11
DH
221 struct hypfs_sb_info *hypfs_info = fc->s_fs_info;
222 struct fs_parse_result result;
6a62a216
EB
223 kuid_t uid;
224 kgid_t gid;
8f8d8d11
DH
225 int opt;
226
d7167b14 227 opt = fs_parse(fc, hypfs_fs_parameters, param, &result);
8f8d8d11
DH
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;
24bbb1fa
MH
244 }
245 return 0;
246}
247
34c80b1d 248static int hypfs_show_options(struct seq_file *s, struct dentry *root)
b01af5ba 249{
34c80b1d 250 struct hypfs_sb_info *hypfs_info = root->d_sb->s_fs_info;
b01af5ba 251
6a62a216
EB
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));
b01af5ba
MH
254 return 0;
255}
256
8f8d8d11 257static int hypfs_fill_super(struct super_block *sb, struct fs_context *fc)
24bbb1fa 258{
8f8d8d11 259 struct hypfs_sb_info *sbi = sb->s_fs_info;
24bbb1fa 260 struct inode *root_inode;
b54c64f7 261 struct dentry *root_dentry, *update_file;
8f8d8d11 262 int rc;
24bbb1fa 263
09cbfeaf
KS
264 sb->s_blocksize = PAGE_SIZE;
265 sb->s_blocksize_bits = PAGE_SHIFT;
24bbb1fa
MH
266 sb->s_magic = HYPFS_MAGIC;
267 sb->s_op = &hypfs_s_ops;
8f8d8d11 268
24bbb1fa 269 root_inode = hypfs_make_inode(sb, S_IFDIR | 0755);
f1771ffa
AV
270 if (!root_inode)
271 return -ENOMEM;
24bbb1fa
MH
272 root_inode->i_op = &simple_dir_inode_operations;
273 root_inode->i_fop = &simple_dir_operations;
48fde701
AV
274 sb->s_root = root_dentry = d_make_root(root_inode);
275 if (!root_dentry)
f1771ffa 276 return -ENOMEM;
52109a06 277 if (machine_is_vm())
f78e41e3 278 rc = hypfs_vm_create_files(root_dentry);
31cb4bd3 279 else
e334cf4f 280 rc = hypfs_diag_create_files(root_dentry);
24bbb1fa 281 if (rc)
f1771ffa 282 return rc;
b54c64f7
DH
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;
24bbb1fa 287 hypfs_update_update(sb);
f55495ba 288 pr_info("Hypervisor filesystem mounted\n");
24bbb1fa 289 return 0;
24bbb1fa
MH
290}
291
8f8d8d11
DH
292static int hypfs_get_tree(struct fs_context *fc)
293{
294 return get_tree_single(fc, hypfs_fill_super);
295}
296
297static void hypfs_free_fc(struct fs_context *fc)
24bbb1fa 298{
8f8d8d11
DH
299 kfree(fc->s_fs_info);
300}
301
302static 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
308static 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;
24bbb1fa
MH
323}
324
325static void hypfs_kill_super(struct super_block *sb)
326{
327 struct hypfs_sb_info *sb_info = sb->s_fs_info;
328
f1771ffa 329 if (sb->s_root)
388c571c 330 hypfs_delete_tree(sb->s_root);
a24cd490 331 if (sb_info && sb_info->update_file)
388c571c 332 hypfs_remove(sb_info->update_file);
f1771ffa
AV
333 kfree(sb->s_fs_info);
334 sb->s_fs_info = NULL;
24bbb1fa
MH
335 kill_litter_super(sb);
336}
337
a118cfdf 338static struct dentry *hypfs_create_file(struct dentry *parent, const char *name,
fec0ebae 339 char *data, umode_t mode)
24bbb1fa
MH
340{
341 struct dentry *dentry;
342 struct inode *inode;
24bbb1fa 343
5955102c 344 inode_lock(d_inode(parent));
fa6fe07d 345 dentry = lookup_noperm(&QSTR(name), parent);
9b5a03e1
MH
346 if (IS_ERR(dentry)) {
347 dentry = ERR_PTR(-ENOMEM);
348 goto fail;
349 }
a118cfdf 350 inode = hypfs_make_inode(parent->d_sb, mode);
24bbb1fa
MH
351 if (!inode) {
352 dput(dentry);
9b5a03e1
MH
353 dentry = ERR_PTR(-ENOMEM);
354 goto fail;
24bbb1fa 355 }
fec0ebae 356 if (S_ISREG(mode)) {
24bbb1fa
MH
357 inode->i_fop = &hypfs_file_ops;
358 if (data)
359 inode->i_size = strlen(data);
360 else
361 inode->i_size = 0;
fec0ebae 362 } else if (S_ISDIR(mode)) {
24bbb1fa
MH
363 inode->i_op = &simple_dir_inode_operations;
364 inode->i_fop = &simple_dir_operations;
75c3cfa8 365 inc_nlink(d_inode(parent));
24bbb1fa
MH
366 } else
367 BUG();
8e18e294 368 inode->i_private = data;
24bbb1fa
MH
369 d_instantiate(dentry, inode);
370 dget(dentry);
9b5a03e1 371fail:
5955102c 372 inode_unlock(d_inode(parent));
24bbb1fa
MH
373 return dentry;
374}
375
a118cfdf 376struct dentry *hypfs_mkdir(struct dentry *parent, const char *name)
24bbb1fa
MH
377{
378 struct dentry *dentry;
379
a118cfdf 380 dentry = hypfs_create_file(parent, name, NULL, S_IFDIR | DIR_MODE);
24bbb1fa
MH
381 if (IS_ERR(dentry))
382 return dentry;
383 hypfs_add_dentry(dentry);
24bbb1fa
MH
384 return dentry;
385}
386
a118cfdf 387static struct dentry *hypfs_create_update_file(struct dentry *dir)
24bbb1fa
MH
388{
389 struct dentry *dentry;
390
a118cfdf 391 dentry = hypfs_create_file(dir, "update", NULL,
24bbb1fa
MH
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
a118cfdf 401struct dentry *hypfs_create_u64(struct dentry *dir,
24bbb1fa
MH
402 const char *name, __u64 value)
403{
404 char *buffer;
405 char tmp[TMP_SIZE];
406 struct dentry *dentry;
407
ad2a5d8e 408 snprintf(tmp, TMP_SIZE, "%llu\n", (unsigned long long int)value);
24bbb1fa
MH
409 buffer = kstrdup(tmp, GFP_KERNEL);
410 if (!buffer)
411 return ERR_PTR(-ENOMEM);
412 dentry =
a118cfdf 413 hypfs_create_file(dir, name, buffer, S_IFREG | REG_FILE_MODE);
24bbb1fa
MH
414 if (IS_ERR(dentry)) {
415 kfree(buffer);
416 return ERR_PTR(-ENOMEM);
417 }
418 hypfs_add_dentry(dentry);
419 return dentry;
420}
421
a118cfdf 422struct dentry *hypfs_create_str(struct dentry *dir,
24bbb1fa
MH
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 =
a118cfdf 433 hypfs_create_file(dir, name, buffer, S_IFREG | REG_FILE_MODE);
24bbb1fa
MH
434 if (IS_ERR(dentry)) {
435 kfree(buffer);
436 return ERR_PTR(-ENOMEM);
437 }
438 hypfs_add_dentry(dentry);
439 return dentry;
440}
441
5dfe4c96 442static const struct file_operations hypfs_file_ops = {
24bbb1fa
MH
443 .open = hypfs_open,
444 .release = hypfs_release,
a457ac28
AV
445 .read_iter = hypfs_read_iter,
446 .write_iter = hypfs_write_iter,
24bbb1fa
MH
447};
448
449static struct file_system_type hypfs_type = {
450 .owner = THIS_MODULE,
451 .name = "s390_hypfs",
8f8d8d11 452 .init_fs_context = hypfs_init_fs_context,
d7167b14 453 .parameters = hypfs_fs_parameters,
24bbb1fa
MH
454 .kill_sb = hypfs_kill_super
455};
456
b87221de 457static const struct super_operations hypfs_s_ops = {
24bbb1fa 458 .statfs = simple_statfs,
b69257f2 459 .evict_inode = hypfs_evict_inode,
b01af5ba 460 .show_options = hypfs_show_options,
24bbb1fa
MH
461};
462
3325b4d8 463int __init __hypfs_fs_init(void)
24bbb1fa
MH
464{
465 int rc;
466
f9bb4882
EB
467 rc = sysfs_create_mount_point(hypervisor_kobj, "s390");
468 if (rc)
3325b4d8 469 return rc;
24bbb1fa
MH
470 rc = register_filesystem(&hypfs_type);
471 if (rc)
3325b4d8 472 goto fail;
24bbb1fa 473 return 0;
3325b4d8 474fail:
f9bb4882 475 sysfs_remove_mount_point(hypervisor_kobj, "s390");
24bbb1fa
MH
476 return rc;
477}