blob: 4b010ff814c9caa3f439cb7e849aa8c501bdace1 [file] [log] [blame]
Michael Holzheu24bbb1f2006-06-23 02:05:06 -07001/*
Michael Holzheuf19bfb22006-09-20 15:58:44 +02002 * arch/s390/hypfs/inode.c
Michael Holzheu24bbb1f2006-06-23 02:05:06 -07003 * Hypervisor filesystem for Linux on s390.
4 *
5 * Copyright (C) IBM Corp. 2006
6 * Author(s): Michael Holzheu <holzheu@de.ibm.com>
7 */
8
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>
Michael Holzheub01af5b2007-08-10 14:32:27 +020020#include <linux/seq_file.h>
21#include <linux/mount.h>
Michael Holzheu24bbb1f2006-06-23 02:05:06 -070022#include <asm/ebcdic.h>
23#include "hypfs.h"
Michael Holzheu24bbb1f2006-06-23 02:05:06 -070024
25#define HYPFS_MAGIC 0x687970 /* ASCII 'hyp' */
26#define TMP_SIZE 64 /* size of temporary buffers */
27
28static struct dentry *hypfs_create_update_file(struct super_block *sb,
29 struct dentry *dir);
30
31struct 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 */
37};
38
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -080039static const struct file_operations hypfs_file_ops;
Michael Holzheu24bbb1f2006-06-23 02:05:06 -070040static struct file_system_type hypfs_type;
41static struct super_operations hypfs_s_ops;
42
43/* start of list of all dentries, which have to be deleted on update */
44static struct dentry *hypfs_last_dentry;
45
46static void hypfs_update_update(struct super_block *sb)
47{
48 struct hypfs_sb_info *sb_info = sb->s_fs_info;
49 struct inode *inode = sb_info->update_file->d_inode;
50
51 sb_info->last_update = get_seconds();
52 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
53}
54
55/* directory tree removal functions */
56
57static void hypfs_add_dentry(struct dentry *dentry)
58{
59 dentry->d_fsdata = hypfs_last_dentry;
60 hypfs_last_dentry = dentry;
61}
62
Michael Holzheu9b5a03e2007-08-22 13:51:43 +020063static inline int hypfs_positive(struct dentry *dentry)
64{
65 return dentry->d_inode && !d_unhashed(dentry);
66}
67
Michael Holzheu24bbb1f2006-06-23 02:05:06 -070068static void hypfs_remove(struct dentry *dentry)
69{
70 struct dentry *parent;
71
72 parent = dentry->d_parent;
Michael Holzheu9b5a03e2007-08-22 13:51:43 +020073 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);
81 }
Michael Holzheu24bbb1f2006-06-23 02:05:06 -070082 d_delete(dentry);
83 dput(dentry);
Michael Holzheu9b5a03e2007-08-22 13:51:43 +020084 mutex_unlock(&parent->d_inode->i_mutex);
Michael Holzheu24bbb1f2006-06-23 02:05:06 -070085}
86
87static void hypfs_delete_tree(struct dentry *root)
88{
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;
94 }
95}
96
97static struct inode *hypfs_make_inode(struct super_block *sb, int mode)
98{
99 struct inode *ret = new_inode(sb);
100
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;
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700106 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;
112 }
113 return ret;
114}
115
116static void hypfs_drop_inode(struct inode *inode)
117{
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700118 kfree(inode->i_private);
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700119 generic_delete_inode(inode);
120}
121
122static int hypfs_open(struct inode *inode, struct file *filp)
123{
Josef Sipekd20343e2006-12-08 02:37:35 -0800124 char *data = filp->f_path.dentry->d_inode->i_private;
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700125 struct hypfs_sb_info *fs_info;
126
127 if (filp->f_mode & FMODE_WRITE) {
128 if (!(inode->i_mode & S_IWUGO))
129 return -EACCES;
130 }
131 if (filp->f_mode & FMODE_READ) {
132 if (!(inode->i_mode & S_IRUGO))
133 return -EACCES;
134 }
135
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;
143 }
144 mutex_unlock(&fs_info->lock);
145 }
146 return 0;
147}
148
Badari Pulavarty027445c2006-09-30 23:28:46 -0700149static ssize_t hypfs_aio_read(struct kiocb *iocb, const struct iovec *iov,
150 unsigned long nr_segs, loff_t offset)
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700151{
152 char *data;
153 size_t len;
154 struct file *filp = iocb->ki_filp;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700155 /* XXX: temporary */
156 char __user *buf = iov[0].iov_base;
157 size_t count = iov[0].iov_len;
158
159 if (nr_segs != 1) {
160 count = -EINVAL;
161 goto out;
162 }
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700163
164 data = filp->private_data;
165 len = strlen(data);
166 if (offset > len) {
167 count = 0;
168 goto out;
169 }
170 if (count > len - offset)
171 count = len - offset;
172 if (copy_to_user(buf, data + offset, count)) {
173 count = -EFAULT;
174 goto out;
175 }
176 iocb->ki_pos += count;
177 file_accessed(filp);
178out:
179 return count;
180}
Badari Pulavarty027445c2006-09-30 23:28:46 -0700181static ssize_t hypfs_aio_write(struct kiocb *iocb, const struct iovec *iov,
182 unsigned long nr_segs, loff_t offset)
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700183{
184 int rc;
185 struct super_block *sb;
186 struct hypfs_sb_info *fs_info;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700187 size_t count = iov_length(iov, nr_segs);
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700188
Josef Sipekd20343e2006-12-08 02:37:35 -0800189 sb = iocb->ki_filp->f_path.dentry->d_inode->i_sb;
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700190 fs_info = sb->s_fs_info;
191 /*
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.
200 */
201 mutex_lock(&fs_info->lock);
202 if (fs_info->last_update == get_seconds()) {
203 rc = -EBUSY;
204 goto out;
205 }
206 hypfs_delete_tree(sb->s_root);
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100207 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);
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700211 if (rc) {
212 printk(KERN_ERR "hypfs: Update failed\n");
213 hypfs_delete_tree(sb->s_root);
214 goto out;
215 }
216 hypfs_update_update(sb);
217 rc = count;
218out:
219 mutex_unlock(&fs_info->lock);
220 return rc;
221}
222
223static int hypfs_release(struct inode *inode, struct file *filp)
224{
225 kfree(filp->private_data);
226 return 0;
227}
228
229enum { opt_uid, opt_gid, opt_err };
230
231static match_table_t hypfs_tokens = {
232 {opt_uid, "uid=%u"},
233 {opt_gid, "gid=%u"},
234 {opt_err, NULL}
235};
236
237static int hypfs_parse_options(char *options, struct super_block *sb)
238{
239 char *str;
240 substring_t args[MAX_OPT_ARGS];
241
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;
247
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;
267 }
268 }
269 return 0;
270}
271
Michael Holzheub01af5b2007-08-10 14:32:27 +0200272static int hypfs_show_options(struct seq_file *s, struct vfsmount *mnt)
273{
274 struct hypfs_sb_info *hypfs_info = mnt->mnt_sb->s_fs_info;
275
276 seq_printf(s, ",uid=%u", hypfs_info->uid);
277 seq_printf(s, ",gid=%u", hypfs_info->gid);
278 return 0;
279}
280
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700281static int hypfs_fill_super(struct super_block *sb, void *data, int silent)
282{
283 struct inode *root_inode;
284 struct dentry *root_dentry;
285 int rc = 0;
286 struct hypfs_sb_info *sbi;
287
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;
302 }
303 root_inode = hypfs_make_inode(sb, S_IFDIR | 0755);
304 if (!root_inode) {
305 rc = -ENOMEM;
306 goto err_alloc;
307 }
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;
315 }
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100316 if (MACHINE_IS_VM)
317 rc = hypfs_vm_create_files(sb, root_dentry);
318 else
319 rc = hypfs_diag_create_files(sb, root_dentry);
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700320 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;
326 }
327 hypfs_update_update(sb);
328 sb->s_root = root_dentry;
Michael Holzheu9b5a03e2007-08-22 13:51:43 +0200329 printk(KERN_INFO "hypfs: Hypervisor filesystem mounted\n");
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700330 return 0;
331
332err_tree:
333 hypfs_delete_tree(root_dentry);
334 d_genocide(root_dentry);
335 dput(root_dentry);
336err_alloc:
337 kfree(sbi);
338 return rc;
339}
340
Andrew Mortona5cf4b9a2006-06-23 04:01:35 -0700341static int hypfs_get_super(struct file_system_type *fst, int flags,
342 const char *devname, void *data, struct vfsmount *mnt)
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700343{
Andrew Mortona5cf4b9a2006-06-23 04:01:35 -0700344 return get_sb_single(fst, flags, data, hypfs_fill_super, mnt);
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700345}
346
347static void hypfs_kill_super(struct super_block *sb)
348{
349 struct hypfs_sb_info *sb_info = sb->s_fs_info;
350
Michael Holzheu388c5712006-09-20 16:00:04 +0200351 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;
356 }
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700357 kill_litter_super(sb);
358}
359
360static struct dentry *hypfs_create_file(struct super_block *sb,
361 struct dentry *parent, const char *name,
362 char *data, mode_t mode)
363{
364 struct dentry *dentry;
365 struct inode *inode;
366 struct qstr qname;
367
368 qname.name = name;
369 qname.len = strlen(name);
370 qname.hash = full_name_hash(name, qname.len);
Michael Holzheu9b5a03e2007-08-22 13:51:43 +0200371 mutex_lock(&parent->d_inode->i_mutex);
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700372 dentry = lookup_one_len(name, parent, strlen(name));
Michael Holzheu9b5a03e2007-08-22 13:51:43 +0200373 if (IS_ERR(dentry)) {
374 dentry = ERR_PTR(-ENOMEM);
375 goto fail;
376 }
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700377 inode = hypfs_make_inode(sb, mode);
378 if (!inode) {
379 dput(dentry);
Michael Holzheu9b5a03e2007-08-22 13:51:43 +0200380 dentry = ERR_PTR(-ENOMEM);
381 goto fail;
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700382 }
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();
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700395 inode->i_private = data;
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700396 d_instantiate(dentry, inode);
397 dget(dentry);
Michael Holzheu9b5a03e2007-08-22 13:51:43 +0200398fail:
399 mutex_unlock(&parent->d_inode->i_mutex);
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700400 return dentry;
401}
402
403struct dentry *hypfs_mkdir(struct super_block *sb, struct dentry *parent,
404 const char *name)
405{
406 struct dentry *dentry;
407
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);
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700412 return dentry;
413}
414
415static struct dentry *hypfs_create_update_file(struct super_block *sb,
416 struct dentry *dir)
417{
418 struct dentry *dentry;
419
420 dentry = hypfs_create_file(sb, dir, "update", NULL,
421 S_IFREG | UPDATE_FILE_MODE);
422 /*
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.
426 */
427 return dentry;
428}
429
430struct dentry *hypfs_create_u64(struct super_block *sb, struct dentry *dir,
431 const char *name, __u64 value)
432{
433 char *buffer;
434 char tmp[TMP_SIZE];
435 struct dentry *dentry;
436
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);
446 }
447 hypfs_add_dentry(dentry);
448 return dentry;
449}
450
451struct dentry *hypfs_create_str(struct super_block *sb, struct dentry *dir,
452 const char *name, char *string)
453{
454 char *buffer;
455 struct dentry *dentry;
456
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);
466 }
467 hypfs_add_dentry(dentry);
468 return dentry;
469}
470
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800471static const struct file_operations hypfs_file_ops = {
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700472 .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,
478};
479
480static 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
485};
486
487static struct super_operations hypfs_s_ops = {
488 .statfs = simple_statfs,
489 .drop_inode = hypfs_drop_inode,
Michael Holzheub01af5b2007-08-10 14:32:27 +0200490 .show_options = hypfs_show_options,
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700491};
492
Greg Kroah-Hartman9b477702007-11-01 09:29:06 -0600493static struct kobject *s390_kobj;
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700494
495static int __init hypfs_init(void)
496{
497 int rc;
498
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100499 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;
507 }
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700508 }
Greg Kroah-Hartman9b477702007-11-01 09:29:06 -0600509 s390_kobj = kobject_create_and_add("s390", hypervisor_kobj);
510 if (!s390_kobj) {
511 rc = -ENOMEM;;
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700512 goto fail_sysfs;
Greg Kroah-Hartman9b477702007-11-01 09:29:06 -0600513 }
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700514 rc = register_filesystem(&hypfs_type);
515 if (rc)
516 goto fail_filesystem;
517 return 0;
518
519fail_filesystem:
Greg Kroah-Hartman38a382a2007-12-20 08:13:05 -0800520 kobject_put(s390_kobj);
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700521fail_sysfs:
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100522 if (!MACHINE_IS_VM)
523 hypfs_diag_exit();
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700524fail_diag:
525 printk(KERN_ERR "hypfs: Initialization failed with rc = %i.\n", rc);
526 return rc;
527}
528
529static void __exit hypfs_exit(void)
530{
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100531 if (!MACHINE_IS_VM)
532 hypfs_diag_exit();
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700533 unregister_filesystem(&hypfs_type);
Greg Kroah-Hartman38a382a2007-12-20 08:13:05 -0800534 kobject_put(s390_kobj);
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700535}
536
537module_init(hypfs_init)
538module_exit(hypfs_exit)
539
540MODULE_LICENSE("GPL");
541MODULE_AUTHOR("Michael Holzheu <holzheu@de.ibm.com>");
542MODULE_DESCRIPTION("s390 Hypervisor Filesystem");