oprofilefs.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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/fs_context.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/uaccess.h>
  19. #include "oprof.h"
  20. #define OPROFILEFS_MAGIC 0x6f70726f
  21. DEFINE_RAW_SPINLOCK(oprofilefs_lock);
  22. static struct inode *oprofilefs_get_inode(struct super_block *sb, int mode)
  23. {
  24. struct inode *inode = new_inode(sb);
  25. if (inode) {
  26. inode->i_ino = get_next_ino();
  27. inode->i_mode = mode;
  28. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  29. }
  30. return inode;
  31. }
  32. static const struct super_operations s_ops = {
  33. .statfs = simple_statfs,
  34. .drop_inode = generic_delete_inode,
  35. };
  36. ssize_t oprofilefs_str_to_user(char const *str, char __user *buf, size_t count, loff_t *offset)
  37. {
  38. return simple_read_from_buffer(buf, count, offset, str, strlen(str));
  39. }
  40. #define TMPBUFSIZE 50
  41. ssize_t oprofilefs_ulong_to_user(unsigned long val, char __user *buf, size_t count, loff_t *offset)
  42. {
  43. char tmpbuf[TMPBUFSIZE];
  44. size_t maxlen = snprintf(tmpbuf, TMPBUFSIZE, "%lu\n", val);
  45. if (maxlen > TMPBUFSIZE)
  46. maxlen = TMPBUFSIZE;
  47. return simple_read_from_buffer(buf, count, offset, tmpbuf, maxlen);
  48. }
  49. /*
  50. * Note: If oprofilefs_ulong_from_user() returns 0, then *val remains
  51. * unchanged and might be uninitialized. This follows write syscall
  52. * implementation when count is zero: "If count is zero ... [and if]
  53. * no errors are detected, 0 will be returned without causing any
  54. * other effect." (man 2 write)
  55. */
  56. int oprofilefs_ulong_from_user(unsigned long *val, char const __user *buf, size_t count)
  57. {
  58. char tmpbuf[TMPBUFSIZE];
  59. unsigned long flags;
  60. if (!count)
  61. return 0;
  62. if (count > TMPBUFSIZE - 1)
  63. return -EINVAL;
  64. memset(tmpbuf, 0x0, TMPBUFSIZE);
  65. if (copy_from_user(tmpbuf, buf, count))
  66. return -EFAULT;
  67. raw_spin_lock_irqsave(&oprofilefs_lock, flags);
  68. *val = simple_strtoul(tmpbuf, NULL, 0);
  69. raw_spin_unlock_irqrestore(&oprofilefs_lock, flags);
  70. return count;
  71. }
  72. static ssize_t ulong_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
  73. {
  74. unsigned long *val = file->private_data;
  75. return oprofilefs_ulong_to_user(*val, buf, count, offset);
  76. }
  77. static ssize_t ulong_write_file(struct file *file, char const __user *buf, size_t count, loff_t *offset)
  78. {
  79. unsigned long value;
  80. int retval;
  81. if (*offset)
  82. return -EINVAL;
  83. retval = oprofilefs_ulong_from_user(&value, buf, count);
  84. if (retval <= 0)
  85. return retval;
  86. retval = oprofile_set_ulong(file->private_data, value);
  87. if (retval)
  88. return retval;
  89. return count;
  90. }
  91. static const struct file_operations ulong_fops = {
  92. .read = ulong_read_file,
  93. .write = ulong_write_file,
  94. .open = simple_open,
  95. .llseek = default_llseek,
  96. };
  97. static const struct file_operations ulong_ro_fops = {
  98. .read = ulong_read_file,
  99. .open = simple_open,
  100. .llseek = default_llseek,
  101. };
  102. static int __oprofilefs_create_file(struct dentry *root, char const *name,
  103. const struct file_operations *fops, int perm, void *priv)
  104. {
  105. struct dentry *dentry;
  106. struct inode *inode;
  107. if (!root)
  108. return -ENOMEM;
  109. inode_lock(d_inode(root));
  110. dentry = d_alloc_name(root, name);
  111. if (!dentry) {
  112. inode_unlock(d_inode(root));
  113. return -ENOMEM;
  114. }
  115. inode = oprofilefs_get_inode(root->d_sb, S_IFREG | perm);
  116. if (!inode) {
  117. dput(dentry);
  118. inode_unlock(d_inode(root));
  119. return -ENOMEM;
  120. }
  121. inode->i_fop = fops;
  122. inode->i_private = priv;
  123. d_add(dentry, inode);
  124. inode_unlock(d_inode(root));
  125. return 0;
  126. }
  127. int oprofilefs_create_ulong(struct dentry *root,
  128. char const *name, unsigned long *val)
  129. {
  130. return __oprofilefs_create_file(root, name,
  131. &ulong_fops, 0644, val);
  132. }
  133. int oprofilefs_create_ro_ulong(struct dentry *root,
  134. char const *name, unsigned long *val)
  135. {
  136. return __oprofilefs_create_file(root, name,
  137. &ulong_ro_fops, 0444, val);
  138. }
  139. static ssize_t atomic_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
  140. {
  141. atomic_t *val = file->private_data;
  142. return oprofilefs_ulong_to_user(atomic_read(val), buf, count, offset);
  143. }
  144. static const struct file_operations atomic_ro_fops = {
  145. .read = atomic_read_file,
  146. .open = simple_open,
  147. .llseek = default_llseek,
  148. };
  149. int oprofilefs_create_ro_atomic(struct dentry *root,
  150. char const *name, atomic_t *val)
  151. {
  152. return __oprofilefs_create_file(root, name,
  153. &atomic_ro_fops, 0444, val);
  154. }
  155. int oprofilefs_create_file(struct dentry *root,
  156. char const *name, const struct file_operations *fops)
  157. {
  158. return __oprofilefs_create_file(root, name, fops, 0644, NULL);
  159. }
  160. int oprofilefs_create_file_perm(struct dentry *root,
  161. char const *name, const struct file_operations *fops, int perm)
  162. {
  163. return __oprofilefs_create_file(root, name, fops, perm, NULL);
  164. }
  165. struct dentry *oprofilefs_mkdir(struct dentry *parent, char const *name)
  166. {
  167. struct dentry *dentry;
  168. struct inode *inode;
  169. inode_lock(d_inode(parent));
  170. dentry = d_alloc_name(parent, name);
  171. if (!dentry) {
  172. inode_unlock(d_inode(parent));
  173. return NULL;
  174. }
  175. inode = oprofilefs_get_inode(parent->d_sb, S_IFDIR | 0755);
  176. if (!inode) {
  177. dput(dentry);
  178. inode_unlock(d_inode(parent));
  179. return NULL;
  180. }
  181. inode->i_op = &simple_dir_inode_operations;
  182. inode->i_fop = &simple_dir_operations;
  183. d_add(dentry, inode);
  184. inode_unlock(d_inode(parent));
  185. return dentry;
  186. }
  187. static int oprofilefs_fill_super(struct super_block *sb, struct fs_context *fc)
  188. {
  189. struct inode *root_inode;
  190. sb->s_blocksize = PAGE_SIZE;
  191. sb->s_blocksize_bits = PAGE_SHIFT;
  192. sb->s_magic = OPROFILEFS_MAGIC;
  193. sb->s_op = &s_ops;
  194. sb->s_time_gran = 1;
  195. root_inode = oprofilefs_get_inode(sb, S_IFDIR | 0755);
  196. if (!root_inode)
  197. return -ENOMEM;
  198. root_inode->i_op = &simple_dir_inode_operations;
  199. root_inode->i_fop = &simple_dir_operations;
  200. sb->s_root = d_make_root(root_inode);
  201. if (!sb->s_root)
  202. return -ENOMEM;
  203. oprofile_create_files(sb->s_root);
  204. // FIXME: verify kill_litter_super removes our dentries
  205. return 0;
  206. }
  207. static int oprofilefs_get_tree(struct fs_context *fc)
  208. {
  209. return get_tree_single(fc, oprofilefs_fill_super);
  210. }
  211. static const struct fs_context_operations oprofilefs_context_ops = {
  212. .get_tree = oprofilefs_get_tree,
  213. };
  214. static int oprofilefs_init_fs_context(struct fs_context *fc)
  215. {
  216. fc->ops = &oprofilefs_context_ops;
  217. return 0;
  218. }
  219. static struct file_system_type oprofilefs_type = {
  220. .owner = THIS_MODULE,
  221. .name = "oprofilefs",
  222. .init_fs_context = oprofilefs_init_fs_context,
  223. .kill_sb = kill_litter_super,
  224. };
  225. MODULE_ALIAS_FS("oprofilefs");
  226. int __init oprofilefs_register(void)
  227. {
  228. return register_filesystem(&oprofilefs_type);
  229. }
  230. void __exit oprofilefs_unregister(void)
  231. {
  232. unregister_filesystem(&oprofilefs_type);
  233. }