blob: ba5d3167df0db3175fb76525843094ac653582e6 [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>
20#include <asm/ebcdic.h>
21#include "hypfs.h"
Michael Holzheu24bbb1f2006-06-23 02:05:06 -070022
23#define HYPFS_MAGIC 0x687970 /* ASCII 'hyp' */
24#define TMP_SIZE 64 /* size of temporary buffers */
25
26static struct dentry *hypfs_create_update_file(struct super_block *sb,
27 struct dentry *dir);
28
29struct hypfs_sb_info {
30 uid_t uid; /* uid used for files and dirs */
31 gid_t gid; /* gid used for files and dirs */
32 struct dentry *update_file; /* file to trigger update */
33 time_t last_update; /* last update time in secs since 1970 */
34 struct mutex lock; /* lock to protect update process */
35};
36
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -080037static const struct file_operations hypfs_file_ops;
Michael Holzheu24bbb1f2006-06-23 02:05:06 -070038static struct file_system_type hypfs_type;
39static struct super_operations hypfs_s_ops;
40
41/* start of list of all dentries, which have to be deleted on update */
42static struct dentry *hypfs_last_dentry;
43
44static void hypfs_update_update(struct super_block *sb)
45{
46 struct hypfs_sb_info *sb_info = sb->s_fs_info;
47 struct inode *inode = sb_info->update_file->d_inode;
48
49 sb_info->last_update = get_seconds();
50 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
51}
52
53/* directory tree removal functions */
54
55static void hypfs_add_dentry(struct dentry *dentry)
56{
57 dentry->d_fsdata = hypfs_last_dentry;
58 hypfs_last_dentry = dentry;
59}
60
61static void hypfs_remove(struct dentry *dentry)
62{
63 struct dentry *parent;
64
65 parent = dentry->d_parent;
66 if (S_ISDIR(dentry->d_inode->i_mode))
67 simple_rmdir(parent->d_inode, dentry);
68 else
69 simple_unlink(parent->d_inode, dentry);
70 d_delete(dentry);
71 dput(dentry);
72}
73
74static void hypfs_delete_tree(struct dentry *root)
75{
76 while (hypfs_last_dentry) {
77 struct dentry *next_dentry;
78 next_dentry = hypfs_last_dentry->d_fsdata;
79 hypfs_remove(hypfs_last_dentry);
80 hypfs_last_dentry = next_dentry;
81 }
82}
83
84static struct inode *hypfs_make_inode(struct super_block *sb, int mode)
85{
86 struct inode *ret = new_inode(sb);
87
88 if (ret) {
89 struct hypfs_sb_info *hypfs_info = sb->s_fs_info;
90 ret->i_mode = mode;
91 ret->i_uid = hypfs_info->uid;
92 ret->i_gid = hypfs_info->gid;
Michael Holzheu24bbb1f2006-06-23 02:05:06 -070093 ret->i_blocks = 0;
94 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
95 if (mode & S_IFDIR)
96 ret->i_nlink = 2;
97 else
98 ret->i_nlink = 1;
99 }
100 return ret;
101}
102
103static void hypfs_drop_inode(struct inode *inode)
104{
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700105 kfree(inode->i_private);
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700106 generic_delete_inode(inode);
107}
108
109static int hypfs_open(struct inode *inode, struct file *filp)
110{
Josef Sipekd20343e2006-12-08 02:37:35 -0800111 char *data = filp->f_path.dentry->d_inode->i_private;
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700112 struct hypfs_sb_info *fs_info;
113
114 if (filp->f_mode & FMODE_WRITE) {
115 if (!(inode->i_mode & S_IWUGO))
116 return -EACCES;
117 }
118 if (filp->f_mode & FMODE_READ) {
119 if (!(inode->i_mode & S_IRUGO))
120 return -EACCES;
121 }
122
123 fs_info = inode->i_sb->s_fs_info;
124 if(data) {
125 mutex_lock(&fs_info->lock);
126 filp->private_data = kstrdup(data, GFP_KERNEL);
127 if (!filp->private_data) {
128 mutex_unlock(&fs_info->lock);
129 return -ENOMEM;
130 }
131 mutex_unlock(&fs_info->lock);
132 }
133 return 0;
134}
135
Badari Pulavarty027445c2006-09-30 23:28:46 -0700136static ssize_t hypfs_aio_read(struct kiocb *iocb, const struct iovec *iov,
137 unsigned long nr_segs, loff_t offset)
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700138{
139 char *data;
140 size_t len;
141 struct file *filp = iocb->ki_filp;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700142 /* XXX: temporary */
143 char __user *buf = iov[0].iov_base;
144 size_t count = iov[0].iov_len;
145
146 if (nr_segs != 1) {
147 count = -EINVAL;
148 goto out;
149 }
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700150
151 data = filp->private_data;
152 len = strlen(data);
153 if (offset > len) {
154 count = 0;
155 goto out;
156 }
157 if (count > len - offset)
158 count = len - offset;
159 if (copy_to_user(buf, data + offset, count)) {
160 count = -EFAULT;
161 goto out;
162 }
163 iocb->ki_pos += count;
164 file_accessed(filp);
165out:
166 return count;
167}
Badari Pulavarty027445c2006-09-30 23:28:46 -0700168static ssize_t hypfs_aio_write(struct kiocb *iocb, const struct iovec *iov,
169 unsigned long nr_segs, loff_t offset)
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700170{
171 int rc;
172 struct super_block *sb;
173 struct hypfs_sb_info *fs_info;
Badari Pulavarty027445c2006-09-30 23:28:46 -0700174 size_t count = iov_length(iov, nr_segs);
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700175
Josef Sipekd20343e2006-12-08 02:37:35 -0800176 sb = iocb->ki_filp->f_path.dentry->d_inode->i_sb;
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700177 fs_info = sb->s_fs_info;
178 /*
179 * Currently we only allow one update per second for two reasons:
180 * 1. diag 204 is VERY expensive
181 * 2. If several processes do updates in parallel and then read the
182 * hypfs data, the likelihood of collisions is reduced, if we restrict
183 * the minimum update interval. A collision occurs, if during the
184 * data gathering of one process another process triggers an update
185 * If the first process wants to ensure consistent data, it has
186 * to restart data collection in this case.
187 */
188 mutex_lock(&fs_info->lock);
189 if (fs_info->last_update == get_seconds()) {
190 rc = -EBUSY;
191 goto out;
192 }
193 hypfs_delete_tree(sb->s_root);
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100194 if (MACHINE_IS_VM)
195 rc = hypfs_vm_create_files(sb, sb->s_root);
196 else
197 rc = hypfs_diag_create_files(sb, sb->s_root);
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700198 if (rc) {
199 printk(KERN_ERR "hypfs: Update failed\n");
200 hypfs_delete_tree(sb->s_root);
201 goto out;
202 }
203 hypfs_update_update(sb);
204 rc = count;
205out:
206 mutex_unlock(&fs_info->lock);
207 return rc;
208}
209
210static int hypfs_release(struct inode *inode, struct file *filp)
211{
212 kfree(filp->private_data);
213 return 0;
214}
215
216enum { opt_uid, opt_gid, opt_err };
217
218static match_table_t hypfs_tokens = {
219 {opt_uid, "uid=%u"},
220 {opt_gid, "gid=%u"},
221 {opt_err, NULL}
222};
223
224static int hypfs_parse_options(char *options, struct super_block *sb)
225{
226 char *str;
227 substring_t args[MAX_OPT_ARGS];
228
229 if (!options)
230 return 0;
231 while ((str = strsep(&options, ",")) != NULL) {
232 int token, option;
233 struct hypfs_sb_info *hypfs_info = sb->s_fs_info;
234
235 if (!*str)
236 continue;
237 token = match_token(str, hypfs_tokens, args);
238 switch (token) {
239 case opt_uid:
240 if (match_int(&args[0], &option))
241 return -EINVAL;
242 hypfs_info->uid = option;
243 break;
244 case opt_gid:
245 if (match_int(&args[0], &option))
246 return -EINVAL;
247 hypfs_info->gid = option;
248 break;
249 case opt_err:
250 default:
251 printk(KERN_ERR "hypfs: Unrecognized mount option "
252 "\"%s\" or missing value\n", str);
253 return -EINVAL;
254 }
255 }
256 return 0;
257}
258
259static int hypfs_fill_super(struct super_block *sb, void *data, int silent)
260{
261 struct inode *root_inode;
262 struct dentry *root_dentry;
263 int rc = 0;
264 struct hypfs_sb_info *sbi;
265
266 sbi = kzalloc(sizeof(struct hypfs_sb_info), GFP_KERNEL);
267 if (!sbi)
268 return -ENOMEM;
269 mutex_init(&sbi->lock);
270 sbi->uid = current->uid;
271 sbi->gid = current->gid;
272 sb->s_fs_info = sbi;
273 sb->s_blocksize = PAGE_CACHE_SIZE;
274 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
275 sb->s_magic = HYPFS_MAGIC;
276 sb->s_op = &hypfs_s_ops;
277 if (hypfs_parse_options(data, sb)) {
278 rc = -EINVAL;
279 goto err_alloc;
280 }
281 root_inode = hypfs_make_inode(sb, S_IFDIR | 0755);
282 if (!root_inode) {
283 rc = -ENOMEM;
284 goto err_alloc;
285 }
286 root_inode->i_op = &simple_dir_inode_operations;
287 root_inode->i_fop = &simple_dir_operations;
288 root_dentry = d_alloc_root(root_inode);
289 if (!root_dentry) {
290 iput(root_inode);
291 rc = -ENOMEM;
292 goto err_alloc;
293 }
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100294 if (MACHINE_IS_VM)
295 rc = hypfs_vm_create_files(sb, root_dentry);
296 else
297 rc = hypfs_diag_create_files(sb, root_dentry);
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700298 if (rc)
299 goto err_tree;
300 sbi->update_file = hypfs_create_update_file(sb, root_dentry);
301 if (IS_ERR(sbi->update_file)) {
302 rc = PTR_ERR(sbi->update_file);
303 goto err_tree;
304 }
305 hypfs_update_update(sb);
306 sb->s_root = root_dentry;
307 return 0;
308
309err_tree:
310 hypfs_delete_tree(root_dentry);
311 d_genocide(root_dentry);
312 dput(root_dentry);
313err_alloc:
314 kfree(sbi);
315 return rc;
316}
317
Andrew Mortona5cf4b92006-06-23 04:01:35 -0700318static int hypfs_get_super(struct file_system_type *fst, int flags,
319 const char *devname, void *data, struct vfsmount *mnt)
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700320{
Andrew Mortona5cf4b92006-06-23 04:01:35 -0700321 return get_sb_single(fst, flags, data, hypfs_fill_super, mnt);
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700322}
323
324static void hypfs_kill_super(struct super_block *sb)
325{
326 struct hypfs_sb_info *sb_info = sb->s_fs_info;
327
Michael Holzheu388c5712006-09-20 16:00:04 +0200328 if (sb->s_root) {
329 hypfs_delete_tree(sb->s_root);
330 hypfs_remove(sb_info->update_file);
331 kfree(sb->s_fs_info);
332 sb->s_fs_info = NULL;
333 }
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700334 kill_litter_super(sb);
335}
336
337static struct dentry *hypfs_create_file(struct super_block *sb,
338 struct dentry *parent, const char *name,
339 char *data, mode_t mode)
340{
341 struct dentry *dentry;
342 struct inode *inode;
343 struct qstr qname;
344
345 qname.name = name;
346 qname.len = strlen(name);
347 qname.hash = full_name_hash(name, qname.len);
348 dentry = lookup_one_len(name, parent, strlen(name));
349 if (IS_ERR(dentry))
350 return ERR_PTR(-ENOMEM);
351 inode = hypfs_make_inode(sb, mode);
352 if (!inode) {
353 dput(dentry);
354 return ERR_PTR(-ENOMEM);
355 }
356 if (mode & S_IFREG) {
357 inode->i_fop = &hypfs_file_ops;
358 if (data)
359 inode->i_size = strlen(data);
360 else
361 inode->i_size = 0;
362 } else if (mode & S_IFDIR) {
363 inode->i_op = &simple_dir_inode_operations;
364 inode->i_fop = &simple_dir_operations;
365 parent->d_inode->i_nlink++;
366 } else
367 BUG();
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700368 inode->i_private = data;
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700369 d_instantiate(dentry, inode);
370 dget(dentry);
371 return dentry;
372}
373
374struct dentry *hypfs_mkdir(struct super_block *sb, struct dentry *parent,
375 const char *name)
376{
377 struct dentry *dentry;
378
379 dentry = hypfs_create_file(sb, parent, name, NULL, S_IFDIR | DIR_MODE);
380 if (IS_ERR(dentry))
381 return dentry;
382 hypfs_add_dentry(dentry);
383 parent->d_inode->i_nlink++;
384 return dentry;
385}
386
387static struct dentry *hypfs_create_update_file(struct super_block *sb,
388 struct dentry *dir)
389{
390 struct dentry *dentry;
391
392 dentry = hypfs_create_file(sb, dir, "update", NULL,
393 S_IFREG | UPDATE_FILE_MODE);
394 /*
395 * We do not put the update file on the 'delete' list with
396 * hypfs_add_dentry(), since it should not be removed when the tree
397 * is updated.
398 */
399 return dentry;
400}
401
402struct dentry *hypfs_create_u64(struct super_block *sb, struct dentry *dir,
403 const char *name, __u64 value)
404{
405 char *buffer;
406 char tmp[TMP_SIZE];
407 struct dentry *dentry;
408
409 snprintf(tmp, TMP_SIZE, "%lld\n", (unsigned long long int)value);
410 buffer = kstrdup(tmp, GFP_KERNEL);
411 if (!buffer)
412 return ERR_PTR(-ENOMEM);
413 dentry =
414 hypfs_create_file(sb, dir, name, buffer, S_IFREG | REG_FILE_MODE);
415 if (IS_ERR(dentry)) {
416 kfree(buffer);
417 return ERR_PTR(-ENOMEM);
418 }
419 hypfs_add_dentry(dentry);
420 return dentry;
421}
422
423struct dentry *hypfs_create_str(struct super_block *sb, struct dentry *dir,
424 const char *name, char *string)
425{
426 char *buffer;
427 struct dentry *dentry;
428
429 buffer = kmalloc(strlen(string) + 2, GFP_KERNEL);
430 if (!buffer)
431 return ERR_PTR(-ENOMEM);
432 sprintf(buffer, "%s\n", string);
433 dentry =
434 hypfs_create_file(sb, dir, name, buffer, S_IFREG | REG_FILE_MODE);
435 if (IS_ERR(dentry)) {
436 kfree(buffer);
437 return ERR_PTR(-ENOMEM);
438 }
439 hypfs_add_dentry(dentry);
440 return dentry;
441}
442
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800443static const struct file_operations hypfs_file_ops = {
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700444 .open = hypfs_open,
445 .release = hypfs_release,
446 .read = do_sync_read,
447 .write = do_sync_write,
448 .aio_read = hypfs_aio_read,
449 .aio_write = hypfs_aio_write,
450};
451
452static struct file_system_type hypfs_type = {
453 .owner = THIS_MODULE,
454 .name = "s390_hypfs",
455 .get_sb = hypfs_get_super,
456 .kill_sb = hypfs_kill_super
457};
458
459static struct super_operations hypfs_s_ops = {
460 .statfs = simple_statfs,
461 .drop_inode = hypfs_drop_inode,
462};
463
464static decl_subsys(s390, NULL, NULL);
465
466static int __init hypfs_init(void)
467{
468 int rc;
469
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100470 if (MACHINE_IS_VM) {
471 if (hypfs_vm_init())
472 /* no diag 2fc, just exit */
473 return -ENODATA;
474 } else {
475 if (hypfs_diag_init()) {
476 rc = -ENODATA;
477 goto fail_diag;
478 }
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700479 }
480 kset_set_kset_s(&s390_subsys, hypervisor_subsys);
481 rc = subsystem_register(&s390_subsys);
482 if (rc)
483 goto fail_sysfs;
484 rc = register_filesystem(&hypfs_type);
485 if (rc)
486 goto fail_filesystem;
487 return 0;
488
489fail_filesystem:
490 subsystem_unregister(&s390_subsys);
491fail_sysfs:
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100492 if (!MACHINE_IS_VM)
493 hypfs_diag_exit();
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700494fail_diag:
495 printk(KERN_ERR "hypfs: Initialization failed with rc = %i.\n", rc);
496 return rc;
497}
498
499static void __exit hypfs_exit(void)
500{
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100501 if (!MACHINE_IS_VM)
502 hypfs_diag_exit();
Michael Holzheu24bbb1f2006-06-23 02:05:06 -0700503 unregister_filesystem(&hypfs_type);
504 subsystem_unregister(&s390_subsys);
505}
506
507module_init(hypfs_init)
508module_exit(hypfs_exit)
509
510MODULE_LICENSE("GPL");
511MODULE_AUTHOR("Michael Holzheu <holzheu@de.ibm.com>");
512MODULE_DESCRIPTION("s390 Hypervisor Filesystem");