Blame view

drivers/oprofile/oprofilefs.c 6.56 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
17
18
19
20
21
22
  /**
   * @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>
  #include <linux/pagemap.h>
  #include <asm/uaccess.h>
  
  #include "oprof.h"
  
  #define OPROFILEFS_MAGIC 0x6f70726f
2d21a29fb   Thomas Gleixner   locking, oprofile...
23
  DEFINE_RAW_SPINLOCK(oprofilefs_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
24

25ad2913c   Robert Richter   oprofile: more wh...
25
  static struct inode *oprofilefs_get_inode(struct super_block *sb, int mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
  {
25ad2913c   Robert Richter   oprofile: more wh...
27
  	struct inode *inode = new_inode(sb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
28
29
  
  	if (inode) {
85fe4025c   Christoph Hellwig   fs: do not assign...
30
  		inode->i_ino = get_next_ino();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31
  		inode->i_mode = mode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32
33
34
35
  		inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  	}
  	return inode;
  }
b87221de6   Alexey Dobriyan   const: mark remai...
36
  static const struct super_operations s_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
37
38
39
  	.statfs		= simple_statfs,
  	.drop_inode 	= generic_delete_inode,
  };
25ad2913c   Robert Richter   oprofile: more wh...
40
  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
41
42
43
44
45
46
  {
  	return simple_read_from_buffer(buf, count, offset, str, strlen(str));
  }
  
  
  #define TMPBUFSIZE 50
25ad2913c   Robert Richter   oprofile: more wh...
47
  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
48
49
50
51
52
53
54
55
  {
  	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...
56
57
58
59
60
61
62
  /*
   * 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...
63
  int oprofilefs_ulong_from_user(unsigned long *val, char const __user *buf, size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
64
65
  {
  	char tmpbuf[TMPBUFSIZE];
4dfc896e9   Jiri Kosina   [PATCH] oprofile:...
66
  	unsigned long flags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
67
68
69
70
71
72
73
74
75
76
77
  
  	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...
78
  	raw_spin_lock_irqsave(&oprofilefs_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
79
  	*val = simple_strtoul(tmpbuf, NULL, 0);
2d21a29fb   Thomas Gleixner   locking, oprofile...
80
  	raw_spin_unlock_irqrestore(&oprofilefs_lock, flags);
913050b91   Robert Richter   oprofile: Fix uni...
81
  	return count;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
82
  }
25ad2913c   Robert Richter   oprofile: more wh...
83
  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
84
  {
25ad2913c   Robert Richter   oprofile: more wh...
85
  	unsigned long *val = file->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86
87
  	return oprofilefs_ulong_to_user(*val, buf, count, offset);
  }
25ad2913c   Robert Richter   oprofile: more wh...
88
  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
89
  {
7df01d96b   Robert Richter   oprofile: disable...
90
  	unsigned long value;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
91
92
93
94
  	int retval;
  
  	if (*offset)
  		return -EINVAL;
7df01d96b   Robert Richter   oprofile: disable...
95
  	retval = oprofilefs_ulong_from_user(&value, buf, count);
913050b91   Robert Richter   oprofile: Fix uni...
96
  	if (retval <= 0)
7df01d96b   Robert Richter   oprofile: disable...
97
  		return retval;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
98

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

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
103
104
  	return count;
  }
25ad2913c   Robert Richter   oprofile: more wh...
105
  static int default_open(struct inode *inode, struct file *filp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
106
  {
8e18e2941   Theodore Ts'o   [PATCH] inode_die...
107
108
  	if (inode->i_private)
  		filp->private_data = inode->i_private;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
110
  	return 0;
  }
d54b1fdb1   Arjan van de Ven   [PATCH] mark stru...
111
  static const struct file_operations ulong_fops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
112
113
114
  	.read		= ulong_read_file,
  	.write		= ulong_write_file,
  	.open		= default_open,
6038f373a   Arnd Bergmann   llseek: automatic...
115
  	.llseek		= default_llseek,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
116
  };
d54b1fdb1   Arjan van de Ven   [PATCH] mark stru...
117
  static const struct file_operations ulong_ro_fops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
118
119
  	.read		= ulong_read_file,
  	.open		= default_open,
6038f373a   Arnd Bergmann   llseek: automatic...
120
  	.llseek		= default_llseek,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
121
  };
4fdaa7b68   Robert Richter   oprofile: Remove ...
122
  static int __oprofilefs_create_file(struct super_block *sb,
25ad2913c   Robert Richter   oprofile: more wh...
123
  	struct dentry *root, char const *name, const struct file_operations *fops,
4fdaa7b68   Robert Richter   oprofile: Remove ...
124
  	int perm, void *priv)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
125
  {
25ad2913c   Robert Richter   oprofile: more wh...
126
127
  	struct dentry *dentry;
  	struct inode *inode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
128
129
130
  
  	dentry = d_alloc_name(root, name);
  	if (!dentry)
4fdaa7b68   Robert Richter   oprofile: Remove ...
131
  		return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
132
133
134
  	inode = oprofilefs_get_inode(sb, S_IFREG | perm);
  	if (!inode) {
  		dput(dentry);
4fdaa7b68   Robert Richter   oprofile: Remove ...
135
  		return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
136
137
138
  	}
  	inode->i_fop = fops;
  	d_add(dentry, inode);
4fdaa7b68   Robert Richter   oprofile: Remove ...
139
140
  	dentry->d_inode->i_private = priv;
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
141
  }
25ad2913c   Robert Richter   oprofile: more wh...
142
143
  int oprofilefs_create_ulong(struct super_block *sb, struct dentry *root,
  	char const *name, unsigned long *val)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
144
  {
4fdaa7b68   Robert Richter   oprofile: Remove ...
145
146
  	return __oprofilefs_create_file(sb, root, name,
  					&ulong_fops, 0644, val);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
147
  }
25ad2913c   Robert Richter   oprofile: more wh...
148
149
  int oprofilefs_create_ro_ulong(struct super_block *sb, struct dentry *root,
  	char const *name, unsigned long *val)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
150
  {
4fdaa7b68   Robert Richter   oprofile: Remove ...
151
152
  	return __oprofilefs_create_file(sb, root, name,
  					&ulong_ro_fops, 0444, val);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
153
  }
25ad2913c   Robert Richter   oprofile: more wh...
154
  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
155
  {
25ad2913c   Robert Richter   oprofile: more wh...
156
  	atomic_t *val = file->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
157
158
  	return oprofilefs_ulong_to_user(atomic_read(val), buf, count, offset);
  }
6a18037d4   Robert Richter   oprofile: fixing ...
159

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
160

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

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
167

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

25ad2913c   Robert Richter   oprofile: more wh...
175
176
  int oprofilefs_create_file(struct super_block *sb, struct dentry *root,
  	char const *name, const struct file_operations *fops)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
177
  {
4fdaa7b68   Robert Richter   oprofile: Remove ...
178
  	return __oprofilefs_create_file(sb, root, name, fops, 0644, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
179
  }
25ad2913c   Robert Richter   oprofile: more wh...
180
181
  int oprofilefs_create_file_perm(struct super_block *sb, struct dentry *root,
  	char const *name, const struct file_operations *fops, int perm)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
182
  {
4fdaa7b68   Robert Richter   oprofile: Remove ...
183
  	return __oprofilefs_create_file(sb, root, name, fops, perm, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
184
  }
25ad2913c   Robert Richter   oprofile: more wh...
185
186
  struct dentry *oprofilefs_mkdir(struct super_block *sb,
  	struct dentry *root, 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
191
192
193
194
195
196
197
198
199
200
201
202
203
  
  	dentry = d_alloc_name(root, name);
  	if (!dentry)
  		return NULL;
  	inode = oprofilefs_get_inode(sb, S_IFDIR | 0755);
  	if (!inode) {
  		dput(dentry);
  		return NULL;
  	}
  	inode->i_op = &simple_dir_inode_operations;
  	inode->i_fop = &simple_dir_operations;
  	d_add(dentry, inode);
  	return dentry;
  }
25ad2913c   Robert Richter   oprofile: more wh...
204
  static int oprofilefs_fill_super(struct super_block *sb, void *data, int silent)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
205
  {
25ad2913c   Robert Richter   oprofile: more wh...
206
207
  	struct inode *root_inode;
  	struct dentry *root_dentry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
  
  	sb->s_blocksize = PAGE_CACHE_SIZE;
  	sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  	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;
  	root_dentry = d_alloc_root(root_inode);
  	if (!root_dentry) {
  		iput(root_inode);
  		return -ENOMEM;
  	}
  
  	sb->s_root = root_dentry;
  
  	oprofile_create_files(sb, root_dentry);
  
  	// FIXME: verify kill_litter_super removes our dentries
  	return 0;
  }
fc14f2fef   Al Viro   convert get_sb_si...
233
234
  static struct dentry *oprofilefs_mount(struct file_system_type *fs_type,
  	int flags, const char *dev_name, void *data)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
235
  {
fc14f2fef   Al Viro   convert get_sb_si...
236
  	return mount_single(fs_type, flags, data, oprofilefs_fill_super);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
237
238
239
240
241
242
  }
  
  
  static struct file_system_type oprofilefs_type = {
  	.owner		= THIS_MODULE,
  	.name		= "oprofilefs",
fc14f2fef   Al Viro   convert get_sb_si...
243
  	.mount		= oprofilefs_mount,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
244
245
246
247
248
249
250
251
252
253
254
255
256
257
  	.kill_sb	= kill_litter_super,
  };
  
  
  int __init oprofilefs_register(void)
  {
  	return register_filesystem(&oprofilefs_type);
  }
  
  
  void __exit oprofilefs_unregister(void)
  {
  	unregister_filesystem(&oprofilefs_type);
  }