s390/hypfs: Add missing get_next_ino()
[deliverable/linux.git] / arch / s390 / hypfs / inode.c
1 /*
2 * arch/s390/hypfs/inode.c
3 * Hypervisor filesystem for Linux on s390.
4 *
5 * Copyright IBM Corp. 2006, 2008
6 * Author(s): Michael Holzheu <holzheu@de.ibm.com>
7 */
8
9 #define KMSG_COMPONENT "hypfs"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
12 #include <linux/types.h>
13 #include <linux/errno.h>
14 #include <linux/fs.h>
15 #include <linux/namei.h>
16 #include <linux/vfs.h>
17 #include <linux/slab.h>
18 #include <linux/pagemap.h>
19 #include <linux/time.h>
20 #include <linux/parser.h>
21 #include <linux/sysfs.h>
22 #include <linux/module.h>
23 #include <linux/seq_file.h>
24 #include <linux/mount.h>
25 #include <asm/ebcdic.h>
26 #include "hypfs.h"
27
28 #define HYPFS_MAGIC 0x687970 /* ASCII 'hyp' */
29 #define TMP_SIZE 64 /* size of temporary buffers */
30
31 static struct dentry *hypfs_create_update_file(struct super_block *sb,
32 struct dentry *dir);
33
34 struct hypfs_sb_info {
35 uid_t uid; /* uid used for files and dirs */
36 gid_t gid; /* gid used for files and dirs */
37 struct dentry *update_file; /* file to trigger update */
38 time_t last_update; /* last update time in secs since 1970 */
39 struct mutex lock; /* lock to protect update process */
40 };
41
42 static const struct file_operations hypfs_file_ops;
43 static struct file_system_type hypfs_type;
44 static const struct super_operations hypfs_s_ops;
45
46 /* start of list of all dentries, which have to be deleted on update */
47 static struct dentry *hypfs_last_dentry;
48
49 static void hypfs_update_update(struct super_block *sb)
50 {
51 struct hypfs_sb_info *sb_info = sb->s_fs_info;
52 struct inode *inode = sb_info->update_file->d_inode;
53
54 sb_info->last_update = get_seconds();
55 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
56 }
57
58 /* directory tree removal functions */
59
60 static void hypfs_add_dentry(struct dentry *dentry)
61 {
62 dentry->d_fsdata = hypfs_last_dentry;
63 hypfs_last_dentry = dentry;
64 }
65
66 static inline int hypfs_positive(struct dentry *dentry)
67 {
68 return dentry->d_inode && !d_unhashed(dentry);
69 }
70
71 static void hypfs_remove(struct dentry *dentry)
72 {
73 struct dentry *parent;
74
75 parent = dentry->d_parent;
76 if (!parent || !parent->d_inode)
77 return;
78 mutex_lock(&parent->d_inode->i_mutex);
79 if (hypfs_positive(dentry)) {
80 if (S_ISDIR(dentry->d_inode->i_mode))
81 simple_rmdir(parent->d_inode, dentry);
82 else
83 simple_unlink(parent->d_inode, dentry);
84 }
85 d_delete(dentry);
86 dput(dentry);
87 mutex_unlock(&parent->d_inode->i_mutex);
88 }
89
90 static void hypfs_delete_tree(struct dentry *root)
91 {
92 while (hypfs_last_dentry) {
93 struct dentry *next_dentry;
94 next_dentry = hypfs_last_dentry->d_fsdata;
95 hypfs_remove(hypfs_last_dentry);
96 hypfs_last_dentry = next_dentry;
97 }
98 }
99
100 static struct inode *hypfs_make_inode(struct super_block *sb, umode_t mode)
101 {
102 struct inode *ret = new_inode(sb);
103
104 if (ret) {
105 struct hypfs_sb_info *hypfs_info = sb->s_fs_info;
106 ret->i_ino = get_next_ino();
107 ret->i_mode = mode;
108 ret->i_uid = hypfs_info->uid;
109 ret->i_gid = hypfs_info->gid;
110 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
111 if (S_ISDIR(mode))
112 set_nlink(ret, 2);
113 }
114 return ret;
115 }
116
117 static void hypfs_evict_inode(struct inode *inode)
118 {
119 clear_inode(inode);
120 kfree(inode->i_private);
121 }
122
123 static int hypfs_open(struct inode *inode, struct file *filp)
124 {
125 char *data = filp->f_path.dentry->d_inode->i_private;
126 struct hypfs_sb_info *fs_info;
127
128 if (filp->f_mode & FMODE_WRITE) {
129 if (!(inode->i_mode & S_IWUGO))
130 return -EACCES;
131 }
132 if (filp->f_mode & FMODE_READ) {
133 if (!(inode->i_mode & S_IRUGO))
134 return -EACCES;
135 }
136
137 fs_info = inode->i_sb->s_fs_info;
138 if(data) {
139 mutex_lock(&fs_info->lock);
140 filp->private_data = kstrdup(data, GFP_KERNEL);
141 if (!filp->private_data) {
142 mutex_unlock(&fs_info->lock);
143 return -ENOMEM;
144 }
145 mutex_unlock(&fs_info->lock);
146 }
147 return nonseekable_open(inode, filp);
148 }
149
150 static ssize_t hypfs_aio_read(struct kiocb *iocb, const struct iovec *iov,
151 unsigned long nr_segs, loff_t offset)
152 {
153 char *data;
154 ssize_t ret;
155 struct file *filp = iocb->ki_filp;
156 /* XXX: temporary */
157 char __user *buf = iov[0].iov_base;
158 size_t count = iov[0].iov_len;
159
160 if (nr_segs != 1)
161 return -EINVAL;
162
163 data = filp->private_data;
164 ret = simple_read_from_buffer(buf, count, &offset, data, strlen(data));
165 if (ret <= 0)
166 return ret;
167
168 iocb->ki_pos += ret;
169 file_accessed(filp);
170
171 return ret;
172 }
173 static ssize_t hypfs_aio_write(struct kiocb *iocb, const struct iovec *iov,
174 unsigned long nr_segs, loff_t offset)
175 {
176 int rc;
177 struct super_block *sb;
178 struct hypfs_sb_info *fs_info;
179 size_t count = iov_length(iov, nr_segs);
180
181 sb = iocb->ki_filp->f_path.dentry->d_inode->i_sb;
182 fs_info = sb->s_fs_info;
183 /*
184 * Currently we only allow one update per second for two reasons:
185 * 1. diag 204 is VERY expensive
186 * 2. If several processes do updates in parallel and then read the
187 * hypfs data, the likelihood of collisions is reduced, if we restrict
188 * the minimum update interval. A collision occurs, if during the
189 * data gathering of one process another process triggers an update
190 * If the first process wants to ensure consistent data, it has
191 * to restart data collection in this case.
192 */
193 mutex_lock(&fs_info->lock);
194 if (fs_info->last_update == get_seconds()) {
195 rc = -EBUSY;
196 goto out;
197 }
198 hypfs_delete_tree(sb->s_root);
199 if (MACHINE_IS_VM)
200 rc = hypfs_vm_create_files(sb, sb->s_root);
201 else
202 rc = hypfs_diag_create_files(sb, sb->s_root);
203 if (rc) {
204 pr_err("Updating the hypfs tree failed\n");
205 hypfs_delete_tree(sb->s_root);
206 goto out;
207 }
208 hypfs_update_update(sb);
209 rc = count;
210 out:
211 mutex_unlock(&fs_info->lock);
212 return rc;
213 }
214
215 static int hypfs_release(struct inode *inode, struct file *filp)
216 {
217 kfree(filp->private_data);
218 return 0;
219 }
220
221 enum { opt_uid, opt_gid, opt_err };
222
223 static const match_table_t hypfs_tokens = {
224 {opt_uid, "uid=%u"},
225 {opt_gid, "gid=%u"},
226 {opt_err, NULL}
227 };
228
229 static int hypfs_parse_options(char *options, struct super_block *sb)
230 {
231 char *str;
232 substring_t args[MAX_OPT_ARGS];
233
234 if (!options)
235 return 0;
236 while ((str = strsep(&options, ",")) != NULL) {
237 int token, option;
238 struct hypfs_sb_info *hypfs_info = sb->s_fs_info;
239
240 if (!*str)
241 continue;
242 token = match_token(str, hypfs_tokens, args);
243 switch (token) {
244 case opt_uid:
245 if (match_int(&args[0], &option))
246 return -EINVAL;
247 hypfs_info->uid = option;
248 break;
249 case opt_gid:
250 if (match_int(&args[0], &option))
251 return -EINVAL;
252 hypfs_info->gid = option;
253 break;
254 case opt_err:
255 default:
256 pr_err("%s is not a valid mount option\n", str);
257 return -EINVAL;
258 }
259 }
260 return 0;
261 }
262
263 static int hypfs_show_options(struct seq_file *s, struct dentry *root)
264 {
265 struct hypfs_sb_info *hypfs_info = root->d_sb->s_fs_info;
266
267 seq_printf(s, ",uid=%u", hypfs_info->uid);
268 seq_printf(s, ",gid=%u", hypfs_info->gid);
269 return 0;
270 }
271
272 static int hypfs_fill_super(struct super_block *sb, void *data, int silent)
273 {
274 struct inode *root_inode;
275 struct dentry *root_dentry;
276 int rc = 0;
277 struct hypfs_sb_info *sbi;
278
279 sbi = kzalloc(sizeof(struct hypfs_sb_info), GFP_KERNEL);
280 if (!sbi)
281 return -ENOMEM;
282 mutex_init(&sbi->lock);
283 sbi->uid = current_uid();
284 sbi->gid = current_gid();
285 sb->s_fs_info = sbi;
286 sb->s_blocksize = PAGE_CACHE_SIZE;
287 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
288 sb->s_magic = HYPFS_MAGIC;
289 sb->s_op = &hypfs_s_ops;
290 if (hypfs_parse_options(data, sb))
291 return -EINVAL;
292 root_inode = hypfs_make_inode(sb, S_IFDIR | 0755);
293 if (!root_inode)
294 return -ENOMEM;
295 root_inode->i_op = &simple_dir_inode_operations;
296 root_inode->i_fop = &simple_dir_operations;
297 sb->s_root = root_dentry = d_make_root(root_inode);
298 if (!root_dentry)
299 return -ENOMEM;
300 if (MACHINE_IS_VM)
301 rc = hypfs_vm_create_files(sb, root_dentry);
302 else
303 rc = hypfs_diag_create_files(sb, root_dentry);
304 if (rc)
305 return rc;
306 sbi->update_file = hypfs_create_update_file(sb, root_dentry);
307 if (IS_ERR(sbi->update_file))
308 return PTR_ERR(sbi->update_file);
309 hypfs_update_update(sb);
310 pr_info("Hypervisor filesystem mounted\n");
311 return 0;
312 }
313
314 static struct dentry *hypfs_mount(struct file_system_type *fst, int flags,
315 const char *devname, void *data)
316 {
317 return mount_single(fst, flags, data, hypfs_fill_super);
318 }
319
320 static void hypfs_kill_super(struct super_block *sb)
321 {
322 struct hypfs_sb_info *sb_info = sb->s_fs_info;
323
324 if (sb->s_root)
325 hypfs_delete_tree(sb->s_root);
326 if (sb_info->update_file)
327 hypfs_remove(sb_info->update_file);
328 kfree(sb->s_fs_info);
329 sb->s_fs_info = NULL;
330 kill_litter_super(sb);
331 }
332
333 static struct dentry *hypfs_create_file(struct super_block *sb,
334 struct dentry *parent, const char *name,
335 char *data, umode_t mode)
336 {
337 struct dentry *dentry;
338 struct inode *inode;
339
340 mutex_lock(&parent->d_inode->i_mutex);
341 dentry = lookup_one_len(name, parent, strlen(name));
342 if (IS_ERR(dentry)) {
343 dentry = ERR_PTR(-ENOMEM);
344 goto fail;
345 }
346 inode = hypfs_make_inode(sb, mode);
347 if (!inode) {
348 dput(dentry);
349 dentry = ERR_PTR(-ENOMEM);
350 goto fail;
351 }
352 if (S_ISREG(mode)) {
353 inode->i_fop = &hypfs_file_ops;
354 if (data)
355 inode->i_size = strlen(data);
356 else
357 inode->i_size = 0;
358 } else if (S_ISDIR(mode)) {
359 inode->i_op = &simple_dir_inode_operations;
360 inode->i_fop = &simple_dir_operations;
361 inc_nlink(parent->d_inode);
362 } else
363 BUG();
364 inode->i_private = data;
365 d_instantiate(dentry, inode);
366 dget(dentry);
367 fail:
368 mutex_unlock(&parent->d_inode->i_mutex);
369 return dentry;
370 }
371
372 struct dentry *hypfs_mkdir(struct super_block *sb, struct dentry *parent,
373 const char *name)
374 {
375 struct dentry *dentry;
376
377 dentry = hypfs_create_file(sb, parent, name, NULL, S_IFDIR | DIR_MODE);
378 if (IS_ERR(dentry))
379 return dentry;
380 hypfs_add_dentry(dentry);
381 return dentry;
382 }
383
384 static struct dentry *hypfs_create_update_file(struct super_block *sb,
385 struct dentry *dir)
386 {
387 struct dentry *dentry;
388
389 dentry = hypfs_create_file(sb, dir, "update", NULL,
390 S_IFREG | UPDATE_FILE_MODE);
391 /*
392 * We do not put the update file on the 'delete' list with
393 * hypfs_add_dentry(), since it should not be removed when the tree
394 * is updated.
395 */
396 return dentry;
397 }
398
399 struct dentry *hypfs_create_u64(struct super_block *sb, struct dentry *dir,
400 const char *name, __u64 value)
401 {
402 char *buffer;
403 char tmp[TMP_SIZE];
404 struct dentry *dentry;
405
406 snprintf(tmp, TMP_SIZE, "%llu\n", (unsigned long long int)value);
407 buffer = kstrdup(tmp, GFP_KERNEL);
408 if (!buffer)
409 return ERR_PTR(-ENOMEM);
410 dentry =
411 hypfs_create_file(sb, dir, name, buffer, S_IFREG | REG_FILE_MODE);
412 if (IS_ERR(dentry)) {
413 kfree(buffer);
414 return ERR_PTR(-ENOMEM);
415 }
416 hypfs_add_dentry(dentry);
417 return dentry;
418 }
419
420 struct dentry *hypfs_create_str(struct super_block *sb, struct dentry *dir,
421 const char *name, char *string)
422 {
423 char *buffer;
424 struct dentry *dentry;
425
426 buffer = kmalloc(strlen(string) + 2, GFP_KERNEL);
427 if (!buffer)
428 return ERR_PTR(-ENOMEM);
429 sprintf(buffer, "%s\n", string);
430 dentry =
431 hypfs_create_file(sb, dir, name, buffer, S_IFREG | REG_FILE_MODE);
432 if (IS_ERR(dentry)) {
433 kfree(buffer);
434 return ERR_PTR(-ENOMEM);
435 }
436 hypfs_add_dentry(dentry);
437 return dentry;
438 }
439
440 static const struct file_operations hypfs_file_ops = {
441 .open = hypfs_open,
442 .release = hypfs_release,
443 .read = do_sync_read,
444 .write = do_sync_write,
445 .aio_read = hypfs_aio_read,
446 .aio_write = hypfs_aio_write,
447 .llseek = no_llseek,
448 };
449
450 static struct file_system_type hypfs_type = {
451 .owner = THIS_MODULE,
452 .name = "s390_hypfs",
453 .mount = hypfs_mount,
454 .kill_sb = hypfs_kill_super
455 };
456
457 static const struct super_operations hypfs_s_ops = {
458 .statfs = simple_statfs,
459 .evict_inode = hypfs_evict_inode,
460 .show_options = hypfs_show_options,
461 };
462
463 static struct kobject *s390_kobj;
464
465 static int __init hypfs_init(void)
466 {
467 int rc;
468
469 rc = hypfs_dbfs_init();
470 if (rc)
471 return rc;
472 if (hypfs_diag_init()) {
473 rc = -ENODATA;
474 goto fail_dbfs_exit;
475 }
476 if (hypfs_vm_init()) {
477 rc = -ENODATA;
478 goto fail_hypfs_diag_exit;
479 }
480 s390_kobj = kobject_create_and_add("s390", hypervisor_kobj);
481 if (!s390_kobj) {
482 rc = -ENOMEM;
483 goto fail_hypfs_vm_exit;
484 }
485 rc = register_filesystem(&hypfs_type);
486 if (rc)
487 goto fail_filesystem;
488 return 0;
489
490 fail_filesystem:
491 kobject_put(s390_kobj);
492 fail_hypfs_vm_exit:
493 hypfs_vm_exit();
494 fail_hypfs_diag_exit:
495 hypfs_diag_exit();
496 fail_dbfs_exit:
497 hypfs_dbfs_exit();
498 pr_err("Initialization of hypfs failed with rc=%i\n", rc);
499 return rc;
500 }
501
502 static void __exit hypfs_exit(void)
503 {
504 hypfs_diag_exit();
505 hypfs_vm_exit();
506 hypfs_dbfs_exit();
507 unregister_filesystem(&hypfs_type);
508 kobject_put(s390_kobj);
509 }
510
511 module_init(hypfs_init)
512 module_exit(hypfs_exit)
513
514 MODULE_LICENSE("GPL");
515 MODULE_AUTHOR("Michael Holzheu <holzheu@de.ibm.com>");
516 MODULE_DESCRIPTION("s390 Hypervisor Filesystem");
This page took 0.047092 seconds and 5 git commands to generate.