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>
9 #include <linux/types.h>
10 #include <linux/errno.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>
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
,
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
);
69 simple_unlink(parent
->d_inode
, 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
);
89 struct hypfs_sb_info
*hypfs_info
= sb
->s_fs_info
;
91 ret
->i_uid
= hypfs_info
->uid
;
92 ret
->i_gid
= hypfs_info
->gid
;
94 ret
->i_atime
= ret
->i_mtime
= ret
->i_ctime
= CURRENT_TIME
;
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
))
118 if (filp
->f_mode
& FMODE_READ
) {
119 if (!(inode
->i_mode
& S_IRUGO
))
123 fs_info
= inode
->i_sb
->s_fs_info
;
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
);
131 mutex_unlock(&fs_info
->lock
);
136 static ssize_t
hypfs_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
137 unsigned long nr_segs
, loff_t offset
)
141 struct file
*filp
= iocb
->ki_filp
;
143 char __user
*buf
= iov
[0].iov_base
;
144 size_t count
= iov
[0].iov_len
;
151 data
= filp
->private_data
;
157 if (count
> len
- offset
)
158 count
= len
- offset
;
159 if (copy_to_user(buf
, data
+ offset
, count
)) {
163 iocb
->ki_pos
+= count
;
168 static ssize_t
hypfs_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
169 unsigned long nr_segs
, loff_t offset
)
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()) {
193 hypfs_delete_tree(sb
->s_root
);
195 rc
= hypfs_vm_create_files(sb
, sb
->s_root
);
197 rc
= hypfs_diag_create_files(sb
, sb
->s_root
);
199 printk(KERN_ERR
"hypfs: Update failed\n");
200 hypfs_delete_tree(sb
->s_root
);
203 hypfs_update_update(sb
);
206 mutex_unlock(&fs_info
->lock
);
210 static int hypfs_release(struct inode
*inode
, struct file
*filp
)
212 kfree(filp
->private_data
);
216 enum { opt_uid
, opt_gid
, opt_err
};
218 static match_table_t hypfs_tokens
= {
224 static int hypfs_parse_options(char *options
, struct super_block
*sb
)
227 substring_t args
[MAX_OPT_ARGS
];
231 while ((str
= strsep(&options
, ",")) != NULL
) {
233 struct hypfs_sb_info
*hypfs_info
= sb
->s_fs_info
;
237 token
= match_token(str
, hypfs_tokens
, args
);
240 if (match_int(&args
[0], &option
))
242 hypfs_info
->uid
= option
;
245 if (match_int(&args
[0], &option
))
247 hypfs_info
->gid
= option
;
251 printk(KERN_ERR
"hypfs: Unrecognized mount option "
252 "\"%s\" or missing value\n", str
);
259 static int hypfs_fill_super(struct super_block
*sb
, void *data
, int silent
)
261 struct inode
*root_inode
;
262 struct dentry
*root_dentry
;
264 struct hypfs_sb_info
*sbi
;
266 sbi
= kzalloc(sizeof(struct hypfs_sb_info
), GFP_KERNEL
);
269 mutex_init(&sbi
->lock
);
270 sbi
->uid
= current
->uid
;
271 sbi
->gid
= current
->gid
;
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
)) {
281 root_inode
= hypfs_make_inode(sb
, S_IFDIR
| 0755);
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
);
295 rc
= hypfs_vm_create_files(sb
, root_dentry
);
297 rc
= hypfs_diag_create_files(sb
, root_dentry
);
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
);
305 hypfs_update_update(sb
);
306 sb
->s_root
= root_dentry
;
310 hypfs_delete_tree(root_dentry
);
311 d_genocide(root_dentry
);
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
;
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
;
346 qname
.len
= strlen(name
);
347 qname
.hash
= full_name_hash(name
, qname
.len
);
348 dentry
= lookup_one_len(name
, parent
, strlen(name
));
350 return ERR_PTR(-ENOMEM
);
351 inode
= hypfs_make_inode(sb
, mode
);
354 return ERR_PTR(-ENOMEM
);
356 if (mode
& S_IFREG
) {
357 inode
->i_fop
= &hypfs_file_ops
;
359 inode
->i_size
= strlen(data
);
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
++;
368 inode
->i_private
= data
;
369 d_instantiate(dentry
, inode
);
374 struct dentry
*hypfs_mkdir(struct super_block
*sb
, struct dentry
*parent
,
377 struct dentry
*dentry
;
379 dentry
= hypfs_create_file(sb
, parent
, name
, NULL
, S_IFDIR
| DIR_MODE
);
382 hypfs_add_dentry(dentry
);
383 parent
->d_inode
->i_nlink
++;
387 static struct dentry
*hypfs_create_update_file(struct super_block
*sb
,
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
402 struct dentry
*hypfs_create_u64(struct super_block
*sb
, struct dentry
*dir
,
403 const char *name
, __u64 value
)
407 struct dentry
*dentry
;
409 snprintf(tmp
, TMP_SIZE
, "%lld\n", (unsigned long long int)value
);
410 buffer
= kstrdup(tmp
, GFP_KERNEL
);
412 return ERR_PTR(-ENOMEM
);
414 hypfs_create_file(sb
, dir
, name
, buffer
, S_IFREG
| REG_FILE_MODE
);
415 if (IS_ERR(dentry
)) {
417 return ERR_PTR(-ENOMEM
);
419 hypfs_add_dentry(dentry
);
423 struct dentry
*hypfs_create_str(struct super_block
*sb
, struct dentry
*dir
,
424 const char *name
, char *string
)
427 struct dentry
*dentry
;
429 buffer
= kmalloc(strlen(string
) + 2, GFP_KERNEL
);
431 return ERR_PTR(-ENOMEM
);
432 sprintf(buffer
, "%s\n", string
);
434 hypfs_create_file(sb
, dir
, name
, buffer
, S_IFREG
| REG_FILE_MODE
);
435 if (IS_ERR(dentry
)) {
437 return ERR_PTR(-ENOMEM
);
439 hypfs_add_dentry(dentry
);
443 static const struct file_operations hypfs_file_ops
= {
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)
472 /* no diag 2fc, just exit */
475 if (hypfs_diag_init()) {
480 kobj_set_kset_s(&s390_subsys
, hypervisor_subsys
);
481 rc
= subsystem_register(&s390_subsys
);
484 rc
= register_filesystem(&hypfs_type
);
486 goto fail_filesystem
;
490 subsystem_unregister(&s390_subsys
);
495 printk(KERN_ERR
"hypfs: Initialization failed with rc = %i.\n", rc
);
499 static void __exit
hypfs_exit(void)
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");