RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / arch / s390 / hypfs / inode.c
blob8e1ea1c4012846a8a8d2f31f45f289ceabf39e6f
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 <asm/ebcdic.h>
21 #include "hypfs.h"
23 #define HYPFS_MAGIC 0x687970 /* ASCII 'hyp' */
24 #define TMP_SIZE 64 /* size of temporary buffers */
26 static struct dentry *hypfs_create_update_file(struct super_block *sb,
27 struct dentry *dir);
29 struct 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 */
37 static const struct file_operations hypfs_file_ops;
38 static struct file_system_type hypfs_type;
39 static struct super_operations hypfs_s_ops;
41 /* start of list of all dentries, which have to be deleted on update */
42 static struct dentry *hypfs_last_dentry;
44 static void hypfs_update_update(struct super_block *sb)
46 struct hypfs_sb_info *sb_info = sb->s_fs_info;
47 struct inode *inode = sb_info->update_file->d_inode;
49 sb_info->last_update = get_seconds();
50 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
53 /* directory tree removal functions */
55 static void hypfs_add_dentry(struct dentry *dentry)
57 dentry->d_fsdata = hypfs_last_dentry;
58 hypfs_last_dentry = dentry;
61 static void hypfs_remove(struct dentry *dentry)
63 struct dentry *parent;
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);
74 static void hypfs_delete_tree(struct dentry *root)
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;
84 static struct inode *hypfs_make_inode(struct super_block *sb, int mode)
86 struct inode *ret = new_inode(sb);
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;
93 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;
100 return ret;
103 static void hypfs_drop_inode(struct inode *inode)
105 kfree(inode->i_private);
106 generic_delete_inode(inode);
109 static int hypfs_open(struct inode *inode, struct file *filp)
111 char *data = filp->f_path.dentry->d_inode->i_private;
112 struct hypfs_sb_info *fs_info;
114 if (filp->f_mode & FMODE_WRITE) {
115 if (!(inode->i_mode & S_IWUGO))
116 return -EACCES;
118 if (filp->f_mode & FMODE_READ) {
119 if (!(inode->i_mode & S_IRUGO))
120 return -EACCES;
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;
131 mutex_unlock(&fs_info->lock);
133 return 0;
136 static ssize_t hypfs_aio_read(struct kiocb *iocb, const struct iovec *iov,
137 unsigned long nr_segs, loff_t offset)
139 char *data;
140 size_t len;
141 struct file *filp = iocb->ki_filp;
142 /* XXX: temporary */
143 char __user *buf = iov[0].iov_base;
144 size_t count = iov[0].iov_len;
146 if (nr_segs != 1) {
147 count = -EINVAL;
148 goto out;
151 data = filp->private_data;
152 len = strlen(data);
153 if (offset > len) {
154 count = 0;
155 goto out;
157 if (count > len - offset)
158 count = len - offset;
159 if (copy_to_user(buf, data + offset, count)) {
160 count = -EFAULT;
161 goto out;
163 iocb->ki_pos += count;
164 file_accessed(filp);
165 out:
166 return count;
168 static ssize_t hypfs_aio_write(struct kiocb *iocb, const struct iovec *iov,
169 unsigned long nr_segs, loff_t offset)
171 int rc;
172 struct super_block *sb;
173 struct hypfs_sb_info *fs_info;
174 size_t count = iov_length(iov, nr_segs);
176 sb = iocb->ki_filp->f_path.dentry->d_inode->i_sb;
177 fs_info = sb->s_fs_info;
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.
188 mutex_lock(&fs_info->lock);
189 if (fs_info->last_update == get_seconds()) {
190 rc = -EBUSY;
191 goto out;
193 hypfs_delete_tree(sb->s_root);
194 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);
198 if (rc) {
199 printk(KERN_ERR "hypfs: Update failed\n");
200 hypfs_delete_tree(sb->s_root);
201 goto out;
203 hypfs_update_update(sb);
204 rc = count;
205 out:
206 mutex_unlock(&fs_info->lock);
207 return rc;
210 static int hypfs_release(struct inode *inode, struct file *filp)
212 kfree(filp->private_data);
213 return 0;
216 enum { opt_uid, opt_gid, opt_err };
218 static match_table_t hypfs_tokens = {
219 {opt_uid, "uid=%u"},
220 {opt_gid, "gid=%u"},
221 {opt_err, NULL}
224 static int hypfs_parse_options(char *options, struct super_block *sb)
226 char *str;
227 substring_t args[MAX_OPT_ARGS];
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;
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;
256 return 0;
259 static int hypfs_fill_super(struct super_block *sb, void *data, int silent)
261 struct inode *root_inode;
262 struct dentry *root_dentry;
263 int rc = 0;
264 struct hypfs_sb_info *sbi;
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;
281 root_inode = hypfs_make_inode(sb, S_IFDIR | 0755);
282 if (!root_inode) {
283 rc = -ENOMEM;
284 goto err_alloc;
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;
294 if (MACHINE_IS_VM)
295 rc = hypfs_vm_create_files(sb, root_dentry);
296 else
297 rc = hypfs_diag_create_files(sb, root_dentry);
298 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;
305 hypfs_update_update(sb);
306 sb->s_root = root_dentry;
307 return 0;
309 err_tree:
310 hypfs_delete_tree(root_dentry);
311 d_genocide(root_dentry);
312 dput(root_dentry);
313 err_alloc:
314 kfree(sbi);
315 return rc;
318 static int hypfs_get_super(struct file_system_type *fst, int flags,
319 const char *devname, void *data, struct vfsmount *mnt)
321 return get_sb_single(fst, flags, data, hypfs_fill_super, mnt);
324 static void hypfs_kill_super(struct super_block *sb)
326 struct hypfs_sb_info *sb_info = sb->s_fs_info;
328 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;
334 kill_litter_super(sb);
337 static struct dentry *hypfs_create_file(struct super_block *sb,
338 struct dentry *parent, const char *name,
339 char *data, mode_t mode)
341 struct dentry *dentry;
342 struct inode *inode;
343 struct qstr qname;
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);
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();
368 inode->i_private = data;
369 d_instantiate(dentry, inode);
370 dget(dentry);
371 return dentry;
374 struct dentry *hypfs_mkdir(struct super_block *sb, struct dentry *parent,
375 const char *name)
377 struct dentry *dentry;
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;
387 static struct dentry *hypfs_create_update_file(struct super_block *sb,
388 struct dentry *dir)
390 struct dentry *dentry;
392 dentry = hypfs_create_file(sb, dir, "update", NULL,
393 S_IFREG | UPDATE_FILE_MODE);
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.
399 return dentry;
402 struct dentry *hypfs_create_u64(struct super_block *sb, struct dentry *dir,
403 const char *name, __u64 value)
405 char *buffer;
406 char tmp[TMP_SIZE];
407 struct dentry *dentry;
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);
419 hypfs_add_dentry(dentry);
420 return dentry;
423 struct dentry *hypfs_create_str(struct super_block *sb, struct dentry *dir,
424 const char *name, char *string)
426 char *buffer;
427 struct dentry *dentry;
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);
439 hypfs_add_dentry(dentry);
440 return dentry;
443 static const struct file_operations hypfs_file_ops = {
444 .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,
452 static 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
459 static struct super_operations hypfs_s_ops = {
460 .statfs = simple_statfs,
461 .drop_inode = hypfs_drop_inode,
464 static decl_subsys(s390, NULL, NULL);
466 static int __init hypfs_init(void)
468 int rc;
470 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;
480 kobj_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;
489 fail_filesystem:
490 subsystem_unregister(&s390_subsys);
491 fail_sysfs:
492 if (!MACHINE_IS_VM)
493 hypfs_diag_exit();
494 fail_diag:
495 printk(KERN_ERR "hypfs: Initialization failed with rc = %i.\n", rc);
496 return rc;
499 static void __exit hypfs_exit(void)
501 if (!MACHINE_IS_VM)
502 hypfs_diag_exit();
503 unregister_filesystem(&hypfs_type);
504 subsystem_unregister(&s390_subsys);
507 module_init(hypfs_init)
508 module_exit(hypfs_exit)
510 MODULE_LICENSE("GPL");
511 MODULE_AUTHOR("Michael Holzheu <holzheu@de.ibm.com>");
512 MODULE_DESCRIPTION("s390 Hypervisor Filesystem");