oprofilefs.c 6.4 KB

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