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