oprofilefs.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /**
  2. * @file oprofilefs.c
  3. *
  4. * @remark Copyright 2002 OProfile authors
  5. * @remark Read the file COPYING
  6. *
  7. * @author John Levon
  8. *
  9. * A simple filesystem for configuration and
  10. * access of oprofile.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/oprofile.h>
  15. #include <linux/fs.h>
  16. #include <linux/pagemap.h>
  17. #include <asm/uaccess.h>
  18. #include "oprof.h"
  19. #define OPROFILEFS_MAGIC 0x6f70726f
  20. DEFINE_SPINLOCK(oprofilefs_lock);
  21. static struct inode *oprofilefs_get_inode(struct super_block *sb, int mode)
  22. {
  23. struct inode *inode = new_inode(sb);
  24. if (inode) {
  25. inode->i_mode = mode;
  26. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  27. }
  28. return inode;
  29. }
  30. static struct super_operations s_ops = {
  31. .statfs = simple_statfs,
  32. .drop_inode = generic_delete_inode,
  33. };
  34. ssize_t oprofilefs_str_to_user(char const *str, char __user *buf, size_t count, loff_t *offset)
  35. {
  36. return simple_read_from_buffer(buf, count, offset, str, strlen(str));
  37. }
  38. #define TMPBUFSIZE 50
  39. ssize_t oprofilefs_ulong_to_user(unsigned long val, char __user *buf, size_t count, loff_t *offset)
  40. {
  41. char tmpbuf[TMPBUFSIZE];
  42. size_t maxlen = snprintf(tmpbuf, TMPBUFSIZE, "%lu\n", val);
  43. if (maxlen > TMPBUFSIZE)
  44. maxlen = TMPBUFSIZE;
  45. return simple_read_from_buffer(buf, count, offset, tmpbuf, maxlen);
  46. }
  47. int oprofilefs_ulong_from_user(unsigned long *val, char const __user *buf, size_t count)
  48. {
  49. char tmpbuf[TMPBUFSIZE];
  50. unsigned long flags;
  51. if (!count)
  52. return 0;
  53. if (count > TMPBUFSIZE - 1)
  54. return -EINVAL;
  55. memset(tmpbuf, 0x0, TMPBUFSIZE);
  56. if (copy_from_user(tmpbuf, buf, count))
  57. return -EFAULT;
  58. spin_lock_irqsave(&oprofilefs_lock, flags);
  59. *val = simple_strtoul(tmpbuf, NULL, 0);
  60. spin_unlock_irqrestore(&oprofilefs_lock, flags);
  61. return 0;
  62. }
  63. static ssize_t ulong_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
  64. {
  65. unsigned long *val = file->private_data;
  66. return oprofilefs_ulong_to_user(*val, buf, count, offset);
  67. }
  68. static ssize_t ulong_write_file(struct file *file, char const __user *buf, size_t count, loff_t *offset)
  69. {
  70. unsigned long *value = file->private_data;
  71. int retval;
  72. if (*offset)
  73. return -EINVAL;
  74. retval = oprofilefs_ulong_from_user(value, buf, count);
  75. if (retval)
  76. return retval;
  77. return count;
  78. }
  79. static int default_open(struct inode *inode, struct file *filp)
  80. {
  81. if (inode->i_private)
  82. filp->private_data = inode->i_private;
  83. return 0;
  84. }
  85. static const struct file_operations ulong_fops = {
  86. .read = ulong_read_file,
  87. .write = ulong_write_file,
  88. .open = default_open,
  89. };
  90. static const struct file_operations ulong_ro_fops = {
  91. .read = ulong_read_file,
  92. .open = default_open,
  93. };
  94. static struct dentry *__oprofilefs_create_file(struct super_block *sb,
  95. struct dentry *root, char const *name, const struct file_operations *fops,
  96. int perm)
  97. {
  98. struct dentry *dentry;
  99. struct inode *inode;
  100. dentry = d_alloc_name(root, name);
  101. if (!dentry)
  102. return NULL;
  103. inode = oprofilefs_get_inode(sb, S_IFREG | perm);
  104. if (!inode) {
  105. dput(dentry);
  106. return NULL;
  107. }
  108. inode->i_fop = fops;
  109. d_add(dentry, inode);
  110. return dentry;
  111. }
  112. int oprofilefs_create_ulong(struct super_block *sb, struct dentry *root,
  113. char const *name, unsigned long *val)
  114. {
  115. struct dentry *d = __oprofilefs_create_file(sb, root, name,
  116. &ulong_fops, 0644);
  117. if (!d)
  118. return -EFAULT;
  119. d->d_inode->i_private = val;
  120. return 0;
  121. }
  122. int oprofilefs_create_ro_ulong(struct super_block *sb, struct dentry *root,
  123. char const *name, unsigned long *val)
  124. {
  125. struct dentry *d = __oprofilefs_create_file(sb, root, name,
  126. &ulong_ro_fops, 0444);
  127. if (!d)
  128. return -EFAULT;
  129. d->d_inode->i_private = val;
  130. return 0;
  131. }
  132. static ssize_t atomic_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
  133. {
  134. atomic_t *val = file->private_data;
  135. return oprofilefs_ulong_to_user(atomic_read(val), buf, count, offset);
  136. }
  137. static const struct file_operations atomic_ro_fops = {
  138. .read = atomic_read_file,
  139. .open = default_open,
  140. };
  141. int oprofilefs_create_ro_atomic(struct super_block *sb, struct dentry *root,
  142. char const *name, atomic_t *val)
  143. {
  144. struct dentry *d = __oprofilefs_create_file(sb, root, name,
  145. &atomic_ro_fops, 0444);
  146. if (!d)
  147. return -EFAULT;
  148. d->d_inode->i_private = val;
  149. return 0;
  150. }
  151. int oprofilefs_create_file(struct super_block *sb, struct dentry *root,
  152. char const *name, const struct file_operations *fops)
  153. {
  154. if (!__oprofilefs_create_file(sb, root, name, fops, 0644))
  155. return -EFAULT;
  156. return 0;
  157. }
  158. int oprofilefs_create_file_perm(struct super_block *sb, struct dentry *root,
  159. char const *name, const struct file_operations *fops, int perm)
  160. {
  161. if (!__oprofilefs_create_file(sb, root, name, fops, perm))
  162. return -EFAULT;
  163. return 0;
  164. }
  165. struct dentry *oprofilefs_mkdir(struct super_block *sb,
  166. struct dentry *root, char const *name)
  167. {
  168. struct dentry *dentry;
  169. struct inode *inode;
  170. dentry = d_alloc_name(root, name);
  171. if (!dentry)
  172. return NULL;
  173. inode = oprofilefs_get_inode(sb, S_IFDIR | 0755);
  174. if (!inode) {
  175. dput(dentry);
  176. return NULL;
  177. }
  178. inode->i_op = &simple_dir_inode_operations;
  179. inode->i_fop = &simple_dir_operations;
  180. d_add(dentry, inode);
  181. return dentry;
  182. }
  183. static int oprofilefs_fill_super(struct super_block *sb, void *data, int silent)
  184. {
  185. struct inode *root_inode;
  186. struct dentry *root_dentry;
  187. sb->s_blocksize = PAGE_CACHE_SIZE;
  188. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  189. sb->s_magic = OPROFILEFS_MAGIC;
  190. sb->s_op = &s_ops;
  191. sb->s_time_gran = 1;
  192. root_inode = oprofilefs_get_inode(sb, S_IFDIR | 0755);
  193. if (!root_inode)
  194. return -ENOMEM;
  195. root_inode->i_op = &simple_dir_inode_operations;
  196. root_inode->i_fop = &simple_dir_operations;
  197. root_dentry = d_alloc_root(root_inode);
  198. if (!root_dentry) {
  199. iput(root_inode);
  200. return -ENOMEM;
  201. }
  202. sb->s_root = root_dentry;
  203. oprofile_create_files(sb, root_dentry);
  204. // FIXME: verify kill_litter_super removes our dentries
  205. return 0;
  206. }
  207. static int oprofilefs_get_sb(struct file_system_type *fs_type,
  208. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  209. {
  210. return get_sb_single(fs_type, flags, data, oprofilefs_fill_super, mnt);
  211. }
  212. static struct file_system_type oprofilefs_type = {
  213. .owner = THIS_MODULE,
  214. .name = "oprofilefs",
  215. .get_sb = oprofilefs_get_sb,
  216. .kill_sb = kill_litter_super,
  217. };
  218. int __init oprofilefs_register(void)
  219. {
  220. return register_filesystem(&oprofilefs_type);
  221. }
  222. void __exit oprofilefs_unregister(void)
  223. {
  224. unregister_filesystem(&oprofilefs_type);
  225. }