2 * arch/s390/hypfs/inode.c
3 * Hypervisor filesystem for Linux on s390.
5 * Copyright (C) IBM Corp. 2006
6 * Author(s): Michael Holzheu <holzheu@de.ibm.com>
9 #include <linux/types.h>
10 #include <linux/errno.h>
12 #include <linux/namei.h>
13 #include <linux/vfs.h>
14 #include <linux/pagemap.h>
15 #include <linux/gfp.h>
16 #include <linux/time.h>
17 #include <linux/parser.h>
18 #include <linux/sysfs.h>
19 #include <linux/module.h>
20 #include <linux/seq_file.h>
21 #include <linux/mount.h>
22 #include <asm/ebcdic.h>
25 #define HYPFS_MAGIC 0x687970 /* ASCII 'hyp' */
26 #define TMP_SIZE 64 /* size of temporary buffers */
28 static struct dentry *hypfs_create_update_file(struct super_block *sb,
31 struct hypfs_sb_info {
32 uid_t uid; /* uid used for files and dirs */
33 gid_t gid; /* gid used for files and dirs */
34 struct dentry *update_file; /* file to trigger update */
35 time_t last_update; /* last update time in secs since 1970 */
36 struct mutex lock; /* lock to protect update process */
39 static const struct file_operations hypfs_file_ops;
40 static struct file_system_type hypfs_type;
41 static struct super_operations hypfs_s_ops;
43 /* start of list of all dentries, which have to be deleted on update */
44 static struct dentry *hypfs_last_dentry;
46 static void hypfs_update_update(struct super_block *sb)
48 struct hypfs_sb_info *sb_info = sb->s_fs_info;
49 struct inode *inode = sb_info->update_file->d_inode;
51 sb_info->last_update = get_seconds();
52 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
55 /* directory tree removal functions */
57 static void hypfs_add_dentry(struct dentry *dentry)
59 dentry->d_fsdata = hypfs_last_dentry;
60 hypfs_last_dentry = dentry;
63 static inline int hypfs_positive(struct dentry *dentry)
65 return dentry->d_inode && !d_unhashed(dentry);
68 static void hypfs_remove(struct dentry *dentry)
70 struct dentry *parent;
72 parent = dentry->d_parent;
73 if (!parent || !parent->d_inode)
75 mutex_lock(&parent->d_inode->i_mutex);
76 if (hypfs_positive(dentry)) {
77 if (S_ISDIR(dentry->d_inode->i_mode))
78 simple_rmdir(parent->d_inode, dentry);
80 simple_unlink(parent->d_inode, dentry);
84 mutex_unlock(&parent->d_inode->i_mutex);
87 static void hypfs_delete_tree(struct dentry *root)
89 while (hypfs_last_dentry) {
90 struct dentry *next_dentry;
91 next_dentry = hypfs_last_dentry->d_fsdata;
92 hypfs_remove(hypfs_last_dentry);
93 hypfs_last_dentry = next_dentry;
97 static struct inode *hypfs_make_inode(struct super_block *sb, int mode)
99 struct inode *ret = new_inode(sb);
102 struct hypfs_sb_info *hypfs_info = sb->s_fs_info;
104 ret->i_uid = hypfs_info->uid;
105 ret->i_gid = hypfs_info->gid;
107 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
116 static void hypfs_drop_inode(struct inode *inode)
118 kfree(inode->i_private);
119 generic_delete_inode(inode);
122 static int hypfs_open(struct inode *inode, struct file *filp)
124 char *data = filp->f_path.dentry->d_inode->i_private;
125 struct hypfs_sb_info *fs_info;
127 if (filp->f_mode & FMODE_WRITE) {
128 if (!(inode->i_mode & S_IWUGO))
131 if (filp->f_mode & FMODE_READ) {
132 if (!(inode->i_mode & S_IRUGO))
136 fs_info = inode->i_sb->s_fs_info;
138 mutex_lock(&fs_info->lock);
139 filp->private_data = kstrdup(data, GFP_KERNEL);
140 if (!filp->private_data) {
141 mutex_unlock(&fs_info->lock);
144 mutex_unlock(&fs_info->lock);
149 static ssize_t hypfs_aio_read(struct kiocb *iocb, const struct iovec *iov,
150 unsigned long nr_segs, loff_t offset)
154 struct file *filp = iocb->ki_filp;
156 char __user *buf = iov[0].iov_base;
157 size_t count = iov[0].iov_len;
164 data = filp->private_data;
170 if (count > len - offset)
171 count = len - offset;
172 if (copy_to_user(buf, data + offset, count)) {
176 iocb->ki_pos += count;
181 static ssize_t hypfs_aio_write(struct kiocb *iocb, const struct iovec *iov,
182 unsigned long nr_segs, loff_t offset)
185 struct super_block *sb;
186 struct hypfs_sb_info *fs_info;
187 size_t count = iov_length(iov, nr_segs);
189 sb = iocb->ki_filp->f_path.dentry->d_inode->i_sb;
190 fs_info = sb->s_fs_info;
192 * Currently we only allow one update per second for two reasons:
193 * 1. diag 204 is VERY expensive
194 * 2. If several processes do updates in parallel and then read the
195 * hypfs data, the likelihood of collisions is reduced, if we restrict
196 * the minimum update interval. A collision occurs, if during the
197 * data gathering of one process another process triggers an update
198 * If the first process wants to ensure consistent data, it has
199 * to restart data collection in this case.
201 mutex_lock(&fs_info->lock);
202 if (fs_info->last_update == get_seconds()) {
206 hypfs_delete_tree(sb->s_root);
208 rc = hypfs_vm_create_files(sb, sb->s_root);
210 rc = hypfs_diag_create_files(sb, sb->s_root);
212 printk(KERN_ERR "hypfs: Update failed\n");
213 hypfs_delete_tree(sb->s_root);
216 hypfs_update_update(sb);
219 mutex_unlock(&fs_info->lock);
223 static int hypfs_release(struct inode *inode, struct file *filp)
225 kfree(filp->private_data);
229 enum { opt_uid, opt_gid, opt_err };
231 static match_table_t hypfs_tokens = {
237 static int hypfs_parse_options(char *options, struct super_block *sb)
240 substring_t args[MAX_OPT_ARGS];
244 while ((str = strsep(&options, ",")) != NULL) {
246 struct hypfs_sb_info *hypfs_info = sb->s_fs_info;
250 token = match_token(str, hypfs_tokens, args);
253 if (match_int(&args[0], &option))
255 hypfs_info->uid = option;
258 if (match_int(&args[0], &option))
260 hypfs_info->gid = option;
264 printk(KERN_ERR "hypfs: Unrecognized mount option "
265 "\"%s\" or missing value\n", str);
272 static int hypfs_show_options(struct seq_file *s, struct vfsmount *mnt)
274 struct hypfs_sb_info *hypfs_info = mnt->mnt_sb->s_fs_info;
276 seq_printf(s, ",uid=%u", hypfs_info->uid);
277 seq_printf(s, ",gid=%u", hypfs_info->gid);
281 static int hypfs_fill_super(struct super_block *sb, void *data, int silent)
283 struct inode *root_inode;
284 struct dentry *root_dentry;
286 struct hypfs_sb_info *sbi;
288 sbi = kzalloc(sizeof(struct hypfs_sb_info), GFP_KERNEL);
291 mutex_init(&sbi->lock);
292 sbi->uid = current->uid;
293 sbi->gid = current->gid;
295 sb->s_blocksize = PAGE_CACHE_SIZE;
296 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
297 sb->s_magic = HYPFS_MAGIC;
298 sb->s_op = &hypfs_s_ops;
299 if (hypfs_parse_options(data, sb)) {
303 root_inode = hypfs_make_inode(sb, S_IFDIR | 0755);
308 root_inode->i_op = &simple_dir_inode_operations;
309 root_inode->i_fop = &simple_dir_operations;
310 root_dentry = d_alloc_root(root_inode);
317 rc = hypfs_vm_create_files(sb, root_dentry);
319 rc = hypfs_diag_create_files(sb, root_dentry);
322 sbi->update_file = hypfs_create_update_file(sb, root_dentry);
323 if (IS_ERR(sbi->update_file)) {
324 rc = PTR_ERR(sbi->update_file);
327 hypfs_update_update(sb);
328 sb->s_root = root_dentry;
329 printk(KERN_INFO "hypfs: Hypervisor filesystem mounted\n");
333 hypfs_delete_tree(root_dentry);
334 d_genocide(root_dentry);
341 static int hypfs_get_super(struct file_system_type *fst, int flags,
342 const char *devname, void *data, struct vfsmount *mnt)
344 return get_sb_single(fst, flags, data, hypfs_fill_super, mnt);
347 static void hypfs_kill_super(struct super_block *sb)
349 struct hypfs_sb_info *sb_info = sb->s_fs_info;
352 hypfs_delete_tree(sb->s_root);
353 hypfs_remove(sb_info->update_file);
354 kfree(sb->s_fs_info);
355 sb->s_fs_info = NULL;
357 kill_litter_super(sb);
360 static struct dentry *hypfs_create_file(struct super_block *sb,
361 struct dentry *parent, const char *name,
362 char *data, mode_t mode)
364 struct dentry *dentry;
369 qname.len = strlen(name);
370 qname.hash = full_name_hash(name, qname.len);
371 mutex_lock(&parent->d_inode->i_mutex);
372 dentry = lookup_one_len(name, parent, strlen(name));
373 if (IS_ERR(dentry)) {
374 dentry = ERR_PTR(-ENOMEM);
377 inode = hypfs_make_inode(sb, mode);
380 dentry = ERR_PTR(-ENOMEM);
383 if (mode & S_IFREG) {
384 inode->i_fop = &hypfs_file_ops;
386 inode->i_size = strlen(data);
389 } else if (mode & S_IFDIR) {
390 inode->i_op = &simple_dir_inode_operations;
391 inode->i_fop = &simple_dir_operations;
392 parent->d_inode->i_nlink++;
395 inode->i_private = data;
396 d_instantiate(dentry, inode);
399 mutex_unlock(&parent->d_inode->i_mutex);
403 struct dentry *hypfs_mkdir(struct super_block *sb, struct dentry *parent,
406 struct dentry *dentry;
408 dentry = hypfs_create_file(sb, parent, name, NULL, S_IFDIR | DIR_MODE);
411 hypfs_add_dentry(dentry);
415 static struct dentry *hypfs_create_update_file(struct super_block *sb,
418 struct dentry *dentry;
420 dentry = hypfs_create_file(sb, dir, "update", NULL,
421 S_IFREG | UPDATE_FILE_MODE);
423 * We do not put the update file on the 'delete' list with
424 * hypfs_add_dentry(), since it should not be removed when the tree
430 struct dentry *hypfs_create_u64(struct super_block *sb, struct dentry *dir,
431 const char *name, __u64 value)
435 struct dentry *dentry;
437 snprintf(tmp, TMP_SIZE, "%lld\n", (unsigned long long int)value);
438 buffer = kstrdup(tmp, GFP_KERNEL);
440 return ERR_PTR(-ENOMEM);
442 hypfs_create_file(sb, dir, name, buffer, S_IFREG | REG_FILE_MODE);
443 if (IS_ERR(dentry)) {
445 return ERR_PTR(-ENOMEM);
447 hypfs_add_dentry(dentry);
451 struct dentry *hypfs_create_str(struct super_block *sb, struct dentry *dir,
452 const char *name, char *string)
455 struct dentry *dentry;
457 buffer = kmalloc(strlen(string) + 2, GFP_KERNEL);
459 return ERR_PTR(-ENOMEM);
460 sprintf(buffer, "%s\n", string);
462 hypfs_create_file(sb, dir, name, buffer, S_IFREG | REG_FILE_MODE);
463 if (IS_ERR(dentry)) {
465 return ERR_PTR(-ENOMEM);
467 hypfs_add_dentry(dentry);
471 static const struct file_operations hypfs_file_ops = {
473 .release = hypfs_release,
474 .read = do_sync_read,
475 .write = do_sync_write,
476 .aio_read = hypfs_aio_read,
477 .aio_write = hypfs_aio_write,
480 static struct file_system_type hypfs_type = {
481 .owner = THIS_MODULE,
482 .name = "s390_hypfs",
483 .get_sb = hypfs_get_super,
484 .kill_sb = hypfs_kill_super
487 static struct super_operations hypfs_s_ops = {
488 .statfs = simple_statfs,
489 .drop_inode = hypfs_drop_inode,
490 .show_options = hypfs_show_options,
493 static decl_subsys(s390, NULL, NULL);
495 static int __init hypfs_init(void)
501 /* no diag 2fc, just exit */
504 if (hypfs_diag_init()) {
509 kobj_set_kset_s(&s390_subsys, hypervisor_subsys);
510 rc = subsystem_register(&s390_subsys);
513 rc = register_filesystem(&hypfs_type);
515 goto fail_filesystem;
519 subsystem_unregister(&s390_subsys);
524 printk(KERN_ERR "hypfs: Initialization failed with rc = %i.\n", rc);
528 static void __exit hypfs_exit(void)
532 unregister_filesystem(&hypfs_type);
533 subsystem_unregister(&s390_subsys);
536 module_init(hypfs_init)
537 module_exit(hypfs_exit)
539 MODULE_LICENSE("GPL");
540 MODULE_AUTHOR("Michael Holzheu <holzheu@de.ibm.com>");
541 MODULE_DESCRIPTION("s390 Hypervisor Filesystem");