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