Blame view

drivers/oprofile/oprofilefs.c 6.64 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  /**
   * @file oprofilefs.c
   *
   * @remark Copyright 2002 OProfile authors
   * @remark Read the file COPYING
   *
   * @author John Levon
   *
   * A simple filesystem for configuration and
   * access of oprofile.
   */
  
  #include <linux/init.h>
  #include <linux/module.h>
  #include <linux/oprofile.h>
  #include <linux/fs.h>
c6a2c720d   David Howells   vfs: Convert opro...
17
  #include <linux/fs_context.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
  #include <linux/pagemap.h>
7c0f6ba68   Linus Torvalds   Replace <asm/uacc...
19
  #include <linux/uaccess.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20
21
22
23
  
  #include "oprof.h"
  
  #define OPROFILEFS_MAGIC 0x6f70726f
2d21a29fb   Thomas Gleixner   locking, oprofile...
24
  DEFINE_RAW_SPINLOCK(oprofilefs_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
25

25ad2913c   Robert Richter   oprofile: more wh...
26
  static struct inode *oprofilefs_get_inode(struct super_block *sb, int mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
27
  {
25ad2913c   Robert Richter   oprofile: more wh...
28
  	struct inode *inode = new_inode(sb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
29
30
  
  	if (inode) {
85fe4025c   Christoph Hellwig   fs: do not assign...
31
  		inode->i_ino = get_next_ino();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32
  		inode->i_mode = mode;
078cd8279   Deepa Dinamani   fs: Replace CURRE...
33
  		inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
34
35
36
  	}
  	return inode;
  }
b87221de6   Alexey Dobriyan   const: mark remai...
37
  static const struct super_operations s_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
38
39
40
  	.statfs		= simple_statfs,
  	.drop_inode 	= generic_delete_inode,
  };
25ad2913c   Robert Richter   oprofile: more wh...
41
  ssize_t oprofilefs_str_to_user(char const *str, char __user *buf, size_t count, loff_t *offset)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
42
43
44
45
46
47
  {
  	return simple_read_from_buffer(buf, count, offset, str, strlen(str));
  }
  
  
  #define TMPBUFSIZE 50
25ad2913c   Robert Richter   oprofile: more wh...
48
  ssize_t oprofilefs_ulong_to_user(unsigned long val, char __user *buf, size_t count, loff_t *offset)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
49
50
51
52
53
54
55
56
  {
  	char tmpbuf[TMPBUFSIZE];
  	size_t maxlen = snprintf(tmpbuf, TMPBUFSIZE, "%lu
  ", val);
  	if (maxlen > TMPBUFSIZE)
  		maxlen = TMPBUFSIZE;
  	return simple_read_from_buffer(buf, count, offset, tmpbuf, maxlen);
  }
913050b91   Robert Richter   oprofile: Fix uni...
57
58
59
60
61
62
63
  /*
   * Note: If oprofilefs_ulong_from_user() returns 0, then *val remains
   * unchanged and might be uninitialized. This follows write syscall
   * implementation when count is zero: "If count is zero ... [and if]
   * no errors are detected, 0 will be returned without causing any
   * other effect." (man 2 write)
   */
25ad2913c   Robert Richter   oprofile: more wh...
64
  int oprofilefs_ulong_from_user(unsigned long *val, char const __user *buf, size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
65
66
  {
  	char tmpbuf[TMPBUFSIZE];
4dfc896e9   Jiri Kosina   [PATCH] oprofile:...
67
  	unsigned long flags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
68
69
70
71
72
73
74
75
76
77
78
  
  	if (!count)
  		return 0;
  
  	if (count > TMPBUFSIZE - 1)
  		return -EINVAL;
  
  	memset(tmpbuf, 0x0, TMPBUFSIZE);
  
  	if (copy_from_user(tmpbuf, buf, count))
  		return -EFAULT;
2d21a29fb   Thomas Gleixner   locking, oprofile...
79
  	raw_spin_lock_irqsave(&oprofilefs_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
80
  	*val = simple_strtoul(tmpbuf, NULL, 0);
2d21a29fb   Thomas Gleixner   locking, oprofile...
81
  	raw_spin_unlock_irqrestore(&oprofilefs_lock, flags);
913050b91   Robert Richter   oprofile: Fix uni...
82
  	return count;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
83
  }
25ad2913c   Robert Richter   oprofile: more wh...
84
  static ssize_t ulong_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
85
  {
25ad2913c   Robert Richter   oprofile: more wh...
86
  	unsigned long *val = file->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
87
88
  	return oprofilefs_ulong_to_user(*val, buf, count, offset);
  }
25ad2913c   Robert Richter   oprofile: more wh...
89
  static ssize_t ulong_write_file(struct file *file, char const __user *buf, size_t count, loff_t *offset)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
90
  {
7df01d96b   Robert Richter   oprofile: disable...
91
  	unsigned long value;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
92
93
94
95
  	int retval;
  
  	if (*offset)
  		return -EINVAL;
7df01d96b   Robert Richter   oprofile: disable...
96
  	retval = oprofilefs_ulong_from_user(&value, buf, count);
913050b91   Robert Richter   oprofile: Fix uni...
97
  	if (retval <= 0)
7df01d96b   Robert Richter   oprofile: disable...
98
  		return retval;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
99

7df01d96b   Robert Richter   oprofile: disable...
100
  	retval = oprofile_set_ulong(file->private_data, value);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
101
102
  	if (retval)
  		return retval;
7df01d96b   Robert Richter   oprofile: disable...
103

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
104
105
  	return count;
  }
d54b1fdb1   Arjan van de Ven   [PATCH] mark stru...
106
  static const struct file_operations ulong_fops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
107
108
  	.read		= ulong_read_file,
  	.write		= ulong_write_file,
234e34058   Stephen Boyd   simple_open: auto...
109
  	.open		= simple_open,
6038f373a   Arnd Bergmann   llseek: automatic...
110
  	.llseek		= default_llseek,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
111
  };
d54b1fdb1   Arjan van de Ven   [PATCH] mark stru...
112
  static const struct file_operations ulong_ro_fops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
113
  	.read		= ulong_read_file,
234e34058   Stephen Boyd   simple_open: auto...
114
  	.open		= simple_open,
6038f373a   Arnd Bergmann   llseek: automatic...
115
  	.llseek		= default_llseek,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
116
  };
6af4ea0ba   Al Viro   oprofilefs_create...
117
118
  static int __oprofilefs_create_file(struct dentry *root, char const *name,
  	const struct file_operations *fops, int perm, void *priv)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
119
  {
25ad2913c   Robert Richter   oprofile: more wh...
120
121
  	struct dentry *dentry;
  	struct inode *inode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
122

a74989683   Al Viro   oprofilefs: don't...
123
124
  	if (!root)
  		return -ENOMEM;
5955102c9   Al Viro   wrappers for ->i_...
125
  	inode_lock(d_inode(root));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
126
  	dentry = d_alloc_name(root, name);
3f3834c35   Al Viro   oprofilefs: add m...
127
  	if (!dentry) {
5955102c9   Al Viro   wrappers for ->i_...
128
  		inode_unlock(d_inode(root));
4fdaa7b68   Robert Richter   oprofile: Remove ...
129
  		return -ENOMEM;
3f3834c35   Al Viro   oprofilefs: add m...
130
  	}
6af4ea0ba   Al Viro   oprofilefs_create...
131
  	inode = oprofilefs_get_inode(root->d_sb, S_IFREG | perm);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
132
133
  	if (!inode) {
  		dput(dentry);
5955102c9   Al Viro   wrappers for ->i_...
134
  		inode_unlock(d_inode(root));
4fdaa7b68   Robert Richter   oprofile: Remove ...
135
  		return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
136
137
  	}
  	inode->i_fop = fops;
3f3834c35   Al Viro   oprofilefs: add m...
138
  	inode->i_private = priv;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
139
  	d_add(dentry, inode);
5955102c9   Al Viro   wrappers for ->i_...
140
  	inode_unlock(d_inode(root));
4fdaa7b68   Robert Richter   oprofile: Remove ...
141
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
142
  }
6af4ea0ba   Al Viro   oprofilefs_create...
143
  int oprofilefs_create_ulong(struct dentry *root,
25ad2913c   Robert Richter   oprofile: more wh...
144
  	char const *name, unsigned long *val)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
145
  {
6af4ea0ba   Al Viro   oprofilefs_create...
146
  	return __oprofilefs_create_file(root, name,
4fdaa7b68   Robert Richter   oprofile: Remove ...
147
  					&ulong_fops, 0644, val);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
148
  }
6af4ea0ba   Al Viro   oprofilefs_create...
149
  int oprofilefs_create_ro_ulong(struct dentry *root,
25ad2913c   Robert Richter   oprofile: more wh...
150
  	char const *name, unsigned long *val)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
151
  {
6af4ea0ba   Al Viro   oprofilefs_create...
152
  	return __oprofilefs_create_file(root, name,
4fdaa7b68   Robert Richter   oprofile: Remove ...
153
  					&ulong_ro_fops, 0444, val);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
154
  }
25ad2913c   Robert Richter   oprofile: more wh...
155
  static ssize_t atomic_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
156
  {
25ad2913c   Robert Richter   oprofile: more wh...
157
  	atomic_t *val = file->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
158
159
  	return oprofilefs_ulong_to_user(atomic_read(val), buf, count, offset);
  }
6a18037d4   Robert Richter   oprofile: fixing ...
160

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
161

d54b1fdb1   Arjan van de Ven   [PATCH] mark stru...
162
  static const struct file_operations atomic_ro_fops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
163
  	.read		= atomic_read_file,
234e34058   Stephen Boyd   simple_open: auto...
164
  	.open		= simple_open,
6038f373a   Arnd Bergmann   llseek: automatic...
165
  	.llseek		= default_llseek,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
166
  };
6a18037d4   Robert Richter   oprofile: fixing ...
167

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
168

6af4ea0ba   Al Viro   oprofilefs_create...
169
  int oprofilefs_create_ro_atomic(struct dentry *root,
25ad2913c   Robert Richter   oprofile: more wh...
170
  	char const *name, atomic_t *val)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
171
  {
6af4ea0ba   Al Viro   oprofilefs_create...
172
  	return __oprofilefs_create_file(root, name,
4fdaa7b68   Robert Richter   oprofile: Remove ...
173
  					&atomic_ro_fops, 0444, val);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
174
  }
6a18037d4   Robert Richter   oprofile: fixing ...
175

6af4ea0ba   Al Viro   oprofilefs_create...
176
  int oprofilefs_create_file(struct dentry *root,
25ad2913c   Robert Richter   oprofile: more wh...
177
  	char const *name, const struct file_operations *fops)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
178
  {
6af4ea0ba   Al Viro   oprofilefs_create...
179
  	return __oprofilefs_create_file(root, name, fops, 0644, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
180
  }
6af4ea0ba   Al Viro   oprofilefs_create...
181
  int oprofilefs_create_file_perm(struct dentry *root,
25ad2913c   Robert Richter   oprofile: more wh...
182
  	char const *name, const struct file_operations *fops, int perm)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
183
  {
6af4ea0ba   Al Viro   oprofilefs_create...
184
  	return __oprofilefs_create_file(root, name, fops, perm, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
185
  }
ecde28237   Al Viro   oprofilefs_mkdir(...
186
  struct dentry *oprofilefs_mkdir(struct dentry *parent, char const *name)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
187
  {
25ad2913c   Robert Richter   oprofile: more wh...
188
189
  	struct dentry *dentry;
  	struct inode *inode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
190

5955102c9   Al Viro   wrappers for ->i_...
191
  	inode_lock(d_inode(parent));
ecde28237   Al Viro   oprofilefs_mkdir(...
192
  	dentry = d_alloc_name(parent, name);
3f3834c35   Al Viro   oprofilefs: add m...
193
  	if (!dentry) {
5955102c9   Al Viro   wrappers for ->i_...
194
  		inode_unlock(d_inode(parent));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
195
  		return NULL;
3f3834c35   Al Viro   oprofilefs: add m...
196
  	}
ecde28237   Al Viro   oprofilefs_mkdir(...
197
  	inode = oprofilefs_get_inode(parent->d_sb, S_IFDIR | 0755);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
198
199
  	if (!inode) {
  		dput(dentry);
5955102c9   Al Viro   wrappers for ->i_...
200
  		inode_unlock(d_inode(parent));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
201
202
203
204
205
  		return NULL;
  	}
  	inode->i_op = &simple_dir_inode_operations;
  	inode->i_fop = &simple_dir_operations;
  	d_add(dentry, inode);
5955102c9   Al Viro   wrappers for ->i_...
206
  	inode_unlock(d_inode(parent));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
207
208
  	return dentry;
  }
c6a2c720d   David Howells   vfs: Convert opro...
209
  static int oprofilefs_fill_super(struct super_block *sb, struct fs_context *fc)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
210
  {
25ad2913c   Robert Richter   oprofile: more wh...
211
  	struct inode *root_inode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
212

09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
213
214
  	sb->s_blocksize = PAGE_SIZE;
  	sb->s_blocksize_bits = PAGE_SHIFT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
215
216
217
218
219
220
221
222
223
  	sb->s_magic = OPROFILEFS_MAGIC;
  	sb->s_op = &s_ops;
  	sb->s_time_gran = 1;
  
  	root_inode = oprofilefs_get_inode(sb, S_IFDIR | 0755);
  	if (!root_inode)
  		return -ENOMEM;
  	root_inode->i_op = &simple_dir_inode_operations;
  	root_inode->i_fop = &simple_dir_operations;
318ceed08   Al Viro   tidy up after d_m...
224
225
  	sb->s_root = d_make_root(root_inode);
  	if (!sb->s_root)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
226
  		return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
227

a9e599e55   Al Viro   don't bother pass...
228
  	oprofile_create_files(sb->s_root);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
229
230
231
232
  
  	// FIXME: verify kill_litter_super removes our dentries
  	return 0;
  }
c6a2c720d   David Howells   vfs: Convert opro...
233
  static int oprofilefs_get_tree(struct fs_context *fc)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
234
  {
c6a2c720d   David Howells   vfs: Convert opro...
235
  	return get_tree_single(fc, oprofilefs_fill_super);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
236
  }
c6a2c720d   David Howells   vfs: Convert opro...
237
238
239
240
241
242
243
244
245
  static const struct fs_context_operations oprofilefs_context_ops = {
  	.get_tree	= oprofilefs_get_tree,
  };
  
  static int oprofilefs_init_fs_context(struct fs_context *fc)
  {
  	fc->ops = &oprofilefs_context_ops;
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
246
247
248
249
  
  static struct file_system_type oprofilefs_type = {
  	.owner		= THIS_MODULE,
  	.name		= "oprofilefs",
c6a2c720d   David Howells   vfs: Convert opro...
250
  	.init_fs_context = oprofilefs_init_fs_context,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
251
252
  	.kill_sb	= kill_litter_super,
  };
7f78e0351   Eric W. Biederman   fs: Limit sys_mou...
253
  MODULE_ALIAS_FS("oprofilefs");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
254
255
256
257
258
259
260
261
262
263
264
265
  
  
  int __init oprofilefs_register(void)
  {
  	return register_filesystem(&oprofilefs_type);
  }
  
  
  void __exit oprofilefs_unregister(void)
  {
  	unregister_filesystem(&oprofilefs_type);
  }