Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / arch / s390 / hypfs / inode.c
blob4b010ff814c9caa3f439cb7e849aa8c501bdace1
1 /*
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>
7 */
9 #include <linux/types.h>
10 #include <linux/errno.h>
11 #include <linux/fs.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>
23 #include "hypfs.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,
29 struct dentry *dir);
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)
74 return;
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);
79 else
80 simple_unlink(parent->d_inode, dentry);
82 d_delete(dentry);
83 dput(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);
101 if (ret) {
102 struct hypfs_sb_info *hypfs_info = sb->s_fs_info;
103 ret->i_mode = mode;
104 ret->i_uid = hypfs_info->uid;
105 ret->i_gid = hypfs_info->gid;
106 ret->i_blocks = 0;
107 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
108 if (mode & S_IFDIR)
109 ret->i_nlink = 2;
110 else
111 ret->i_nlink = 1;
113 return ret;
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))
129 return -EACCES;
131 if (filp->f_mode & FMODE_READ) {
132 if (!(inode->i_mode & S_IRUGO))
133 return -EACCES;
136 fs_info = inode->i_sb->s_fs_info;
137 if(data) {
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);
142 return -ENOMEM;
144 mutex_unlock(&fs_info->lock);
146 return 0;
149 static ssize_t hypfs_aio_read(struct kiocb *iocb, const struct iovec *iov,
150 unsigned long nr_segs, loff_t offset)
152 char *data;
153 size_t len;
154 struct file *filp = iocb->ki_filp;
155 /* XXX: temporary */
156 char __user *buf = iov[0].iov_base;
157 size_t count = iov[0].iov_len;
159 if (nr_segs != 1) {
160 count = -EINVAL;
161 goto out;
164 data = filp->private_data;
165 len = strlen(data);
166 if (offset > len) {
167 count = 0;
168 goto out;
170 if (count > len - offset)
171 count = len - offset;
172 if (copy_to_user(buf, data + offset, count)) {
173 count = -EFAULT;
174 goto out;
176 iocb->ki_pos += count;
177 file_accessed(filp);
178 out:
179 return count;
181 static ssize_t hypfs_aio_write(struct kiocb *iocb, const struct iovec *iov,
182 unsigned long nr_segs, loff_t offset)
184 int rc;
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()) {
203 rc = -EBUSY;
204 goto out;
206 hypfs_delete_tree(sb->s_root);
207 if (MACHINE_IS_VM)
208 rc = hypfs_vm_create_files(sb, sb->s_root);
209 else
210 rc = hypfs_diag_create_files(sb, sb->s_root);
211 if (rc) {
212 printk(KERN_ERR "hypfs: Update failed\n");
213 hypfs_delete_tree(sb->s_root);
214 goto out;
216 hypfs_update_update(sb);
217 rc = count;
218 out:
219 mutex_unlock(&fs_info->lock);
220 return rc;
223 static int hypfs_release(struct inode *inode, struct file *filp)
225 kfree(filp->private_data);
226 return 0;
229 enum { opt_uid, opt_gid, opt_err };
231 static match_table_t hypfs_tokens = {
232 {opt_uid, "uid=%u"},
233 {opt_gid, "gid=%u"},
234 {opt_err, NULL}
237 static int hypfs_parse_options(char *options, struct super_block *sb)
239 char *str;
240 substring_t args[MAX_OPT_ARGS];
242 if (!options)
243 return 0;
244 while ((str = strsep(&options, ",")) != NULL) {
245 int token, option;
246 struct hypfs_sb_info *hypfs_info = sb->s_fs_info;
248 if (!*str)
249 continue;
250 token = match_token(str, hypfs_tokens, args);
251 switch (token) {
252 case opt_uid:
253 if (match_int(&args[0], &option))
254 return -EINVAL;
255 hypfs_info->uid = option;
256 break;
257 case opt_gid:
258 if (match_int(&args[0], &option))
259 return -EINVAL;
260 hypfs_info->gid = option;
261 break;
262 case opt_err:
263 default:
264 printk(KERN_ERR "hypfs: Unrecognized mount option "
265 "\"%s\" or missing value\n", str);
266 return -EINVAL;
269 return 0;
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);
278 return 0;
281 static int hypfs_fill_super(struct super_block *sb, void *data, int silent)
283 struct inode *root_inode;
284 struct dentry *root_dentry;
285 int rc = 0;
286 struct hypfs_sb_info *sbi;
288 sbi = kzalloc(sizeof(struct hypfs_sb_info), GFP_KERNEL);
289 if (!sbi)
290 return -ENOMEM;
291 mutex_init(&sbi->lock);
292 sbi->uid = current->uid;
293 sbi->gid = current->gid;
294 sb->s_fs_info = sbi;
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)) {
300 rc = -EINVAL;
301 goto err_alloc;
303 root_inode = hypfs_make_inode(sb, S_IFDIR | 0755);
304 if (!root_inode) {
305 rc = -ENOMEM;
306 goto err_alloc;
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);
311 if (!root_dentry) {
312 iput(root_inode);
313 rc = -ENOMEM;
314 goto err_alloc;
316 if (MACHINE_IS_VM)
317 rc = hypfs_vm_create_files(sb, root_dentry);
318 else
319 rc = hypfs_diag_create_files(sb, root_dentry);
320 if (rc)
321 goto err_tree;
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);
325 goto err_tree;
327 hypfs_update_update(sb);
328 sb->s_root = root_dentry;
329 printk(KERN_INFO "hypfs: Hypervisor filesystem mounted\n");
330 return 0;
332 err_tree:
333 hypfs_delete_tree(root_dentry);
334 d_genocide(root_dentry);
335 dput(root_dentry);
336 err_alloc:
337 kfree(sbi);
338 return rc;
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;
351 if (sb->s_root) {
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;
365 struct inode *inode;
366 struct qstr qname;
368 qname.name = name;
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);
375 goto fail;
377 inode = hypfs_make_inode(sb, mode);
378 if (!inode) {
379 dput(dentry);
380 dentry = ERR_PTR(-ENOMEM);
381 goto fail;
383 if (mode & S_IFREG) {
384 inode->i_fop = &hypfs_file_ops;
385 if (data)
386 inode->i_size = strlen(data);
387 else
388 inode->i_size = 0;
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++;
393 } else
394 BUG();
395 inode->i_private = data;
396 d_instantiate(dentry, inode);
397 dget(dentry);
398 fail:
399 mutex_unlock(&parent->d_inode->i_mutex);
400 return dentry;
403 struct dentry *hypfs_mkdir(struct super_block *sb, struct dentry *parent,
404 const char *name)
406 struct dentry *dentry;
408 dentry = hypfs_create_file(sb, parent, name, NULL, S_IFDIR | DIR_MODE);
409 if (IS_ERR(dentry))
410 return dentry;
411 hypfs_add_dentry(dentry);
412 return dentry;
415 static struct dentry *hypfs_create_update_file(struct super_block *sb,
416 struct dentry *dir)
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
425 * is updated.
427 return dentry;
430 struct dentry *hypfs_create_u64(struct super_block *sb, struct dentry *dir,
431 const char *name, __u64 value)
433 char *buffer;
434 char tmp[TMP_SIZE];
435 struct dentry *dentry;
437 snprintf(tmp, TMP_SIZE, "%lld\n", (unsigned long long int)value);
438 buffer = kstrdup(tmp, GFP_KERNEL);
439 if (!buffer)
440 return ERR_PTR(-ENOMEM);
441 dentry =
442 hypfs_create_file(sb, dir, name, buffer, S_IFREG | REG_FILE_MODE);
443 if (IS_ERR(dentry)) {
444 kfree(buffer);
445 return ERR_PTR(-ENOMEM);
447 hypfs_add_dentry(dentry);
448 return dentry;
451 struct dentry *hypfs_create_str(struct super_block *sb, struct dentry *dir,
452 const char *name, char *string)
454 char *buffer;
455 struct dentry *dentry;
457 buffer = kmalloc(strlen(string) + 2, GFP_KERNEL);
458 if (!buffer)
459 return ERR_PTR(-ENOMEM);
460 sprintf(buffer, "%s\n", string);
461 dentry =
462 hypfs_create_file(sb, dir, name, buffer, S_IFREG | REG_FILE_MODE);
463 if (IS_ERR(dentry)) {
464 kfree(buffer);
465 return ERR_PTR(-ENOMEM);
467 hypfs_add_dentry(dentry);
468 return dentry;
471 static const struct file_operations hypfs_file_ops = {
472 .open = hypfs_open,
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 struct kobject *s390_kobj;
495 static int __init hypfs_init(void)
497 int rc;
499 if (MACHINE_IS_VM) {
500 if (hypfs_vm_init())
501 /* no diag 2fc, just exit */
502 return -ENODATA;
503 } else {
504 if (hypfs_diag_init()) {
505 rc = -ENODATA;
506 goto fail_diag;
509 s390_kobj = kobject_create_and_add("s390", hypervisor_kobj);
510 if (!s390_kobj) {
511 rc = -ENOMEM;;
512 goto fail_sysfs;
514 rc = register_filesystem(&hypfs_type);
515 if (rc)
516 goto fail_filesystem;
517 return 0;
519 fail_filesystem:
520 kobject_put(s390_kobj);
521 fail_sysfs:
522 if (!MACHINE_IS_VM)
523 hypfs_diag_exit();
524 fail_diag:
525 printk(KERN_ERR "hypfs: Initialization failed with rc = %i.\n", rc);
526 return rc;
529 static void __exit hypfs_exit(void)
531 if (!MACHINE_IS_VM)
532 hypfs_diag_exit();
533 unregister_filesystem(&hypfs_type);
534 kobject_put(s390_kobj);
537 module_init(hypfs_init)
538 module_exit(hypfs_exit)
540 MODULE_LICENSE("GPL");
541 MODULE_AUTHOR("Michael Holzheu <holzheu@de.ibm.com>");
542 MODULE_DESCRIPTION("s390 Hypervisor Filesystem");