Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-1.0+
0002 /*
0003  *    Hypervisor filesystem for Linux on s390.
0004  *
0005  *    Copyright IBM Corp. 2006, 2008
0006  *    Author(s): Michael Holzheu <holzheu@de.ibm.com>
0007  */
0008 
0009 #define KMSG_COMPONENT "hypfs"
0010 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
0011 
0012 #include <linux/types.h>
0013 #include <linux/errno.h>
0014 #include <linux/fs.h>
0015 #include <linux/fs_context.h>
0016 #include <linux/fs_parser.h>
0017 #include <linux/namei.h>
0018 #include <linux/vfs.h>
0019 #include <linux/slab.h>
0020 #include <linux/pagemap.h>
0021 #include <linux/time.h>
0022 #include <linux/sysfs.h>
0023 #include <linux/init.h>
0024 #include <linux/kobject.h>
0025 #include <linux/seq_file.h>
0026 #include <linux/uio.h>
0027 #include <asm/ebcdic.h>
0028 #include "hypfs.h"
0029 
0030 #define HYPFS_MAGIC 0x687970    /* ASCII 'hyp' */
0031 #define TMP_SIZE 64     /* size of temporary buffers */
0032 
0033 static struct dentry *hypfs_create_update_file(struct dentry *dir);
0034 
0035 struct hypfs_sb_info {
0036     kuid_t uid;         /* uid used for files and dirs */
0037     kgid_t gid;         /* gid used for files and dirs */
0038     struct dentry *update_file; /* file to trigger update */
0039     time64_t last_update;       /* last update, CLOCK_MONOTONIC time */
0040     struct mutex lock;      /* lock to protect update process */
0041 };
0042 
0043 static const struct file_operations hypfs_file_ops;
0044 static struct file_system_type hypfs_type;
0045 static const struct super_operations hypfs_s_ops;
0046 
0047 /* start of list of all dentries, which have to be deleted on update */
0048 static struct dentry *hypfs_last_dentry;
0049 
0050 static void hypfs_update_update(struct super_block *sb)
0051 {
0052     struct hypfs_sb_info *sb_info = sb->s_fs_info;
0053     struct inode *inode = d_inode(sb_info->update_file);
0054 
0055     sb_info->last_update = ktime_get_seconds();
0056     inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
0057 }
0058 
0059 /* directory tree removal functions */
0060 
0061 static void hypfs_add_dentry(struct dentry *dentry)
0062 {
0063     dentry->d_fsdata = hypfs_last_dentry;
0064     hypfs_last_dentry = dentry;
0065 }
0066 
0067 static void hypfs_remove(struct dentry *dentry)
0068 {
0069     struct dentry *parent;
0070 
0071     parent = dentry->d_parent;
0072     inode_lock(d_inode(parent));
0073     if (simple_positive(dentry)) {
0074         if (d_is_dir(dentry))
0075             simple_rmdir(d_inode(parent), dentry);
0076         else
0077             simple_unlink(d_inode(parent), dentry);
0078     }
0079     d_drop(dentry);
0080     dput(dentry);
0081     inode_unlock(d_inode(parent));
0082 }
0083 
0084 static void hypfs_delete_tree(struct dentry *root)
0085 {
0086     while (hypfs_last_dentry) {
0087         struct dentry *next_dentry;
0088         next_dentry = hypfs_last_dentry->d_fsdata;
0089         hypfs_remove(hypfs_last_dentry);
0090         hypfs_last_dentry = next_dentry;
0091     }
0092 }
0093 
0094 static struct inode *hypfs_make_inode(struct super_block *sb, umode_t mode)
0095 {
0096     struct inode *ret = new_inode(sb);
0097 
0098     if (ret) {
0099         struct hypfs_sb_info *hypfs_info = sb->s_fs_info;
0100         ret->i_ino = get_next_ino();
0101         ret->i_mode = mode;
0102         ret->i_uid = hypfs_info->uid;
0103         ret->i_gid = hypfs_info->gid;
0104         ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
0105         if (S_ISDIR(mode))
0106             set_nlink(ret, 2);
0107     }
0108     return ret;
0109 }
0110 
0111 static void hypfs_evict_inode(struct inode *inode)
0112 {
0113     clear_inode(inode);
0114     kfree(inode->i_private);
0115 }
0116 
0117 static int hypfs_open(struct inode *inode, struct file *filp)
0118 {
0119     char *data = file_inode(filp)->i_private;
0120     struct hypfs_sb_info *fs_info;
0121 
0122     if (filp->f_mode & FMODE_WRITE) {
0123         if (!(inode->i_mode & S_IWUGO))
0124             return -EACCES;
0125     }
0126     if (filp->f_mode & FMODE_READ) {
0127         if (!(inode->i_mode & S_IRUGO))
0128             return -EACCES;
0129     }
0130 
0131     fs_info = inode->i_sb->s_fs_info;
0132     if(data) {
0133         mutex_lock(&fs_info->lock);
0134         filp->private_data = kstrdup(data, GFP_KERNEL);
0135         if (!filp->private_data) {
0136             mutex_unlock(&fs_info->lock);
0137             return -ENOMEM;
0138         }
0139         mutex_unlock(&fs_info->lock);
0140     }
0141     return nonseekable_open(inode, filp);
0142 }
0143 
0144 static ssize_t hypfs_read_iter(struct kiocb *iocb, struct iov_iter *to)
0145 {
0146     struct file *file = iocb->ki_filp;
0147     char *data = file->private_data;
0148     size_t available = strlen(data);
0149     loff_t pos = iocb->ki_pos;
0150     size_t count;
0151 
0152     if (pos < 0)
0153         return -EINVAL;
0154     if (pos >= available || !iov_iter_count(to))
0155         return 0;
0156     count = copy_to_iter(data + pos, available - pos, to);
0157     if (!count)
0158         return -EFAULT;
0159     iocb->ki_pos = pos + count;
0160     file_accessed(file);
0161     return count;
0162 }
0163 
0164 static ssize_t hypfs_write_iter(struct kiocb *iocb, struct iov_iter *from)
0165 {
0166     int rc;
0167     struct super_block *sb = file_inode(iocb->ki_filp)->i_sb;
0168     struct hypfs_sb_info *fs_info = sb->s_fs_info;
0169     size_t count = iov_iter_count(from);
0170 
0171     /*
0172      * Currently we only allow one update per second for two reasons:
0173      * 1. diag 204 is VERY expensive
0174      * 2. If several processes do updates in parallel and then read the
0175      *    hypfs data, the likelihood of collisions is reduced, if we restrict
0176      *    the minimum update interval. A collision occurs, if during the
0177      *    data gathering of one process another process triggers an update
0178      *    If the first process wants to ensure consistent data, it has
0179      *    to restart data collection in this case.
0180      */
0181     mutex_lock(&fs_info->lock);
0182     if (fs_info->last_update == ktime_get_seconds()) {
0183         rc = -EBUSY;
0184         goto out;
0185     }
0186     hypfs_delete_tree(sb->s_root);
0187     if (MACHINE_IS_VM)
0188         rc = hypfs_vm_create_files(sb->s_root);
0189     else
0190         rc = hypfs_diag_create_files(sb->s_root);
0191     if (rc) {
0192         pr_err("Updating the hypfs tree failed\n");
0193         hypfs_delete_tree(sb->s_root);
0194         goto out;
0195     }
0196     hypfs_update_update(sb);
0197     rc = count;
0198     iov_iter_advance(from, count);
0199 out:
0200     mutex_unlock(&fs_info->lock);
0201     return rc;
0202 }
0203 
0204 static int hypfs_release(struct inode *inode, struct file *filp)
0205 {
0206     kfree(filp->private_data);
0207     return 0;
0208 }
0209 
0210 enum { Opt_uid, Opt_gid, };
0211 
0212 static const struct fs_parameter_spec hypfs_fs_parameters[] = {
0213     fsparam_u32("gid", Opt_gid),
0214     fsparam_u32("uid", Opt_uid),
0215     {}
0216 };
0217 
0218 static int hypfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
0219 {
0220     struct hypfs_sb_info *hypfs_info = fc->s_fs_info;
0221     struct fs_parse_result result;
0222     kuid_t uid;
0223     kgid_t gid;
0224     int opt;
0225 
0226     opt = fs_parse(fc, hypfs_fs_parameters, param, &result);
0227     if (opt < 0)
0228         return opt;
0229 
0230     switch (opt) {
0231     case Opt_uid:
0232         uid = make_kuid(current_user_ns(), result.uint_32);
0233         if (!uid_valid(uid))
0234             return invalf(fc, "Unknown uid");
0235         hypfs_info->uid = uid;
0236         break;
0237     case Opt_gid:
0238         gid = make_kgid(current_user_ns(), result.uint_32);
0239         if (!gid_valid(gid))
0240             return invalf(fc, "Unknown gid");
0241         hypfs_info->gid = gid;
0242         break;
0243     }
0244     return 0;
0245 }
0246 
0247 static int hypfs_show_options(struct seq_file *s, struct dentry *root)
0248 {
0249     struct hypfs_sb_info *hypfs_info = root->d_sb->s_fs_info;
0250 
0251     seq_printf(s, ",uid=%u", from_kuid_munged(&init_user_ns, hypfs_info->uid));
0252     seq_printf(s, ",gid=%u", from_kgid_munged(&init_user_ns, hypfs_info->gid));
0253     return 0;
0254 }
0255 
0256 static int hypfs_fill_super(struct super_block *sb, struct fs_context *fc)
0257 {
0258     struct hypfs_sb_info *sbi = sb->s_fs_info;
0259     struct inode *root_inode;
0260     struct dentry *root_dentry, *update_file;
0261     int rc;
0262 
0263     sb->s_blocksize = PAGE_SIZE;
0264     sb->s_blocksize_bits = PAGE_SHIFT;
0265     sb->s_magic = HYPFS_MAGIC;
0266     sb->s_op = &hypfs_s_ops;
0267 
0268     root_inode = hypfs_make_inode(sb, S_IFDIR | 0755);
0269     if (!root_inode)
0270         return -ENOMEM;
0271     root_inode->i_op = &simple_dir_inode_operations;
0272     root_inode->i_fop = &simple_dir_operations;
0273     sb->s_root = root_dentry = d_make_root(root_inode);
0274     if (!root_dentry)
0275         return -ENOMEM;
0276     if (MACHINE_IS_VM)
0277         rc = hypfs_vm_create_files(root_dentry);
0278     else
0279         rc = hypfs_diag_create_files(root_dentry);
0280     if (rc)
0281         return rc;
0282     update_file = hypfs_create_update_file(root_dentry);
0283     if (IS_ERR(update_file))
0284         return PTR_ERR(update_file);
0285     sbi->update_file = update_file;
0286     hypfs_update_update(sb);
0287     pr_info("Hypervisor filesystem mounted\n");
0288     return 0;
0289 }
0290 
0291 static int hypfs_get_tree(struct fs_context *fc)
0292 {
0293     return get_tree_single(fc, hypfs_fill_super);
0294 }
0295 
0296 static void hypfs_free_fc(struct fs_context *fc)
0297 {
0298     kfree(fc->s_fs_info);
0299 }
0300 
0301 static const struct fs_context_operations hypfs_context_ops = {
0302     .free       = hypfs_free_fc,
0303     .parse_param    = hypfs_parse_param,
0304     .get_tree   = hypfs_get_tree,
0305 };
0306 
0307 static int hypfs_init_fs_context(struct fs_context *fc)
0308 {
0309     struct hypfs_sb_info *sbi;
0310 
0311     sbi = kzalloc(sizeof(struct hypfs_sb_info), GFP_KERNEL);
0312     if (!sbi)
0313         return -ENOMEM;
0314 
0315     mutex_init(&sbi->lock);
0316     sbi->uid = current_uid();
0317     sbi->gid = current_gid();
0318 
0319     fc->s_fs_info = sbi;
0320     fc->ops = &hypfs_context_ops;
0321     return 0;
0322 }
0323 
0324 static void hypfs_kill_super(struct super_block *sb)
0325 {
0326     struct hypfs_sb_info *sb_info = sb->s_fs_info;
0327 
0328     if (sb->s_root)
0329         hypfs_delete_tree(sb->s_root);
0330     if (sb_info && sb_info->update_file)
0331         hypfs_remove(sb_info->update_file);
0332     kfree(sb->s_fs_info);
0333     sb->s_fs_info = NULL;
0334     kill_litter_super(sb);
0335 }
0336 
0337 static struct dentry *hypfs_create_file(struct dentry *parent, const char *name,
0338                     char *data, umode_t mode)
0339 {
0340     struct dentry *dentry;
0341     struct inode *inode;
0342 
0343     inode_lock(d_inode(parent));
0344     dentry = lookup_one_len(name, parent, strlen(name));
0345     if (IS_ERR(dentry)) {
0346         dentry = ERR_PTR(-ENOMEM);
0347         goto fail;
0348     }
0349     inode = hypfs_make_inode(parent->d_sb, mode);
0350     if (!inode) {
0351         dput(dentry);
0352         dentry = ERR_PTR(-ENOMEM);
0353         goto fail;
0354     }
0355     if (S_ISREG(mode)) {
0356         inode->i_fop = &hypfs_file_ops;
0357         if (data)
0358             inode->i_size = strlen(data);
0359         else
0360             inode->i_size = 0;
0361     } else if (S_ISDIR(mode)) {
0362         inode->i_op = &simple_dir_inode_operations;
0363         inode->i_fop = &simple_dir_operations;
0364         inc_nlink(d_inode(parent));
0365     } else
0366         BUG();
0367     inode->i_private = data;
0368     d_instantiate(dentry, inode);
0369     dget(dentry);
0370 fail:
0371     inode_unlock(d_inode(parent));
0372     return dentry;
0373 }
0374 
0375 struct dentry *hypfs_mkdir(struct dentry *parent, const char *name)
0376 {
0377     struct dentry *dentry;
0378 
0379     dentry = hypfs_create_file(parent, name, NULL, S_IFDIR | DIR_MODE);
0380     if (IS_ERR(dentry))
0381         return dentry;
0382     hypfs_add_dentry(dentry);
0383     return dentry;
0384 }
0385 
0386 static struct dentry *hypfs_create_update_file(struct dentry *dir)
0387 {
0388     struct dentry *dentry;
0389 
0390     dentry = hypfs_create_file(dir, "update", NULL,
0391                    S_IFREG | UPDATE_FILE_MODE);
0392     /*
0393      * We do not put the update file on the 'delete' list with
0394      * hypfs_add_dentry(), since it should not be removed when the tree
0395      * is updated.
0396      */
0397     return dentry;
0398 }
0399 
0400 struct dentry *hypfs_create_u64(struct dentry *dir,
0401                 const char *name, __u64 value)
0402 {
0403     char *buffer;
0404     char tmp[TMP_SIZE];
0405     struct dentry *dentry;
0406 
0407     snprintf(tmp, TMP_SIZE, "%llu\n", (unsigned long long int)value);
0408     buffer = kstrdup(tmp, GFP_KERNEL);
0409     if (!buffer)
0410         return ERR_PTR(-ENOMEM);
0411     dentry =
0412         hypfs_create_file(dir, name, buffer, S_IFREG | REG_FILE_MODE);
0413     if (IS_ERR(dentry)) {
0414         kfree(buffer);
0415         return ERR_PTR(-ENOMEM);
0416     }
0417     hypfs_add_dentry(dentry);
0418     return dentry;
0419 }
0420 
0421 struct dentry *hypfs_create_str(struct dentry *dir,
0422                 const char *name, char *string)
0423 {
0424     char *buffer;
0425     struct dentry *dentry;
0426 
0427     buffer = kmalloc(strlen(string) + 2, GFP_KERNEL);
0428     if (!buffer)
0429         return ERR_PTR(-ENOMEM);
0430     sprintf(buffer, "%s\n", string);
0431     dentry =
0432         hypfs_create_file(dir, name, buffer, S_IFREG | REG_FILE_MODE);
0433     if (IS_ERR(dentry)) {
0434         kfree(buffer);
0435         return ERR_PTR(-ENOMEM);
0436     }
0437     hypfs_add_dentry(dentry);
0438     return dentry;
0439 }
0440 
0441 static const struct file_operations hypfs_file_ops = {
0442     .open       = hypfs_open,
0443     .release    = hypfs_release,
0444     .read_iter  = hypfs_read_iter,
0445     .write_iter = hypfs_write_iter,
0446     .llseek     = no_llseek,
0447 };
0448 
0449 static struct file_system_type hypfs_type = {
0450     .owner      = THIS_MODULE,
0451     .name       = "s390_hypfs",
0452     .init_fs_context = hypfs_init_fs_context,
0453     .parameters = hypfs_fs_parameters,
0454     .kill_sb    = hypfs_kill_super
0455 };
0456 
0457 static const struct super_operations hypfs_s_ops = {
0458     .statfs     = simple_statfs,
0459     .evict_inode    = hypfs_evict_inode,
0460     .show_options   = hypfs_show_options,
0461 };
0462 
0463 static int __init hypfs_init(void)
0464 {
0465     int rc;
0466 
0467     hypfs_dbfs_init();
0468 
0469     if (hypfs_diag_init()) {
0470         rc = -ENODATA;
0471         goto fail_dbfs_exit;
0472     }
0473     if (hypfs_vm_init()) {
0474         rc = -ENODATA;
0475         goto fail_hypfs_diag_exit;
0476     }
0477     hypfs_sprp_init();
0478     if (hypfs_diag0c_init()) {
0479         rc = -ENODATA;
0480         goto fail_hypfs_sprp_exit;
0481     }
0482     rc = sysfs_create_mount_point(hypervisor_kobj, "s390");
0483     if (rc)
0484         goto fail_hypfs_diag0c_exit;
0485     rc = register_filesystem(&hypfs_type);
0486     if (rc)
0487         goto fail_filesystem;
0488     return 0;
0489 
0490 fail_filesystem:
0491     sysfs_remove_mount_point(hypervisor_kobj, "s390");
0492 fail_hypfs_diag0c_exit:
0493     hypfs_diag0c_exit();
0494 fail_hypfs_sprp_exit:
0495     hypfs_sprp_exit();
0496     hypfs_vm_exit();
0497 fail_hypfs_diag_exit:
0498     hypfs_diag_exit();
0499     pr_err("Initialization of hypfs failed with rc=%i\n", rc);
0500 fail_dbfs_exit:
0501     hypfs_dbfs_exit();
0502     return rc;
0503 }
0504 device_initcall(hypfs_init)