oprofilefs.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 const 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 int __oprofilefs_create_file(struct super_block *sb,
  95. struct dentry *root, char const *name, const struct file_operations *fops,
  96. int perm, void *priv)
  97. {
  98. struct dentry *dentry;
  99. struct inode *inode;
  100. dentry = d_alloc_name(root, name);
  101. if (!dentry)
  102. return -ENOMEM;
  103. inode = oprofilefs_get_inode(sb, S_IFREG | perm);
  104. if (!inode) {
  105. dput(dentry);
  106. return -ENOMEM;
  107. }
  108. inode->i_fop = fops;
  109. d_add(dentry, inode);
  110. dentry->d_inode->i_private = priv;
  111. return 0;
  112. }
  113. int oprofilefs_create_ulong(struct super_block *sb, struct dentry *root,
  114. char const *name, unsigned long *val)
  115. {
  116. return __oprofilefs_create_file(sb, root, name,
  117. &ulong_fops, 0644, val);
  118. }
  119. int oprofilefs_create_ro_ulong(struct super_block *sb, struct dentry *root,
  120. char const *name, unsigned long *val)
  121. {
  122. return __oprofilefs_create_file(sb, root, name,
  123. &ulong_ro_fops, 0444, val);
  124. }
  125. static ssize_t atomic_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
  126. {
  127. atomic_t *val = file->private_data;
  128. return oprofilefs_ulong_to_user(atomic_read(val), buf, count, offset);
  129. }
  130. static const struct file_operations atomic_ro_fops = {
  131. .read = atomic_read_file,
  132. .open = default_open,
  133. };
  134. int oprofilefs_create_ro_atomic(struct super_block *sb, struct dentry *root,
  135. char const *name, atomic_t *val)
  136. {
  137. return __oprofilefs_create_file(sb, root, name,
  138. &atomic_ro_fops, 0444, val);
  139. }
  140. int oprofilefs_create_file(struct super_block *sb, struct dentry *root,
  141. char const *name, const struct file_operations *fops)
  142. {
  143. return __oprofilefs_create_file(sb, root, name, fops, 0644, NULL);
  144. }
  145. int oprofilefs_create_file_perm(struct super_block *sb, struct dentry *root,
  146. char const *name, const struct file_operations *fops, int perm)
  147. {
  148. return __oprofilefs_create_file(sb, root, name, fops, perm, NULL);
  149. }
  150. struct dentry *oprofilefs_mkdir(struct super_block *sb,
  151. struct dentry *root, char const *name)
  152. {
  153. struct dentry *dentry;
  154. struct inode *inode;
  155. dentry = d_alloc_name(root, name);
  156. if (!dentry)
  157. return NULL;
  158. inode = oprofilefs_get_inode(sb, S_IFDIR | 0755);
  159. if (!inode) {
  160. dput(dentry);
  161. return NULL;
  162. }
  163. inode->i_op = &simple_dir_inode_operations;
  164. inode->i_fop = &simple_dir_operations;
  165. d_add(dentry, inode);
  166. return dentry;
  167. }
  168. static int oprofilefs_fill_super(struct super_block *sb, void *data, int silent)
  169. {
  170. struct inode *root_inode;
  171. struct dentry *root_dentry;
  172. sb->s_blocksize = PAGE_CACHE_SIZE;
  173. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  174. sb->s_magic = OPROFILEFS_MAGIC;
  175. sb->s_op = &s_ops;
  176. sb->s_time_gran = 1;
  177. root_inode = oprofilefs_get_inode(sb, S_IFDIR | 0755);
  178. if (!root_inode)
  179. return -ENOMEM;
  180. root_inode->i_op = &simple_dir_inode_operations;
  181. root_inode->i_fop = &simple_dir_operations;
  182. root_dentry = d_alloc_root(root_inode);
  183. if (!root_dentry) {
  184. iput(root_inode);
  185. return -ENOMEM;
  186. }
  187. sb->s_root = root_dentry;
  188. oprofile_create_files(sb, root_dentry);
  189. // FIXME: verify kill_litter_super removes our dentries
  190. return 0;
  191. }
  192. static int oprofilefs_get_sb(struct file_system_type *fs_type,
  193. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  194. {
  195. return get_sb_single(fs_type, flags, data, oprofilefs_fill_super, mnt);
  196. }
  197. static struct file_system_type oprofilefs_type = {
  198. .owner = THIS_MODULE,
  199. .name = "oprofilefs",
  200. .get_sb = oprofilefs_get_sb,
  201. .kill_sb = kill_litter_super,
  202. };
  203. int __init oprofilefs_register(void)
  204. {
  205. return register_filesystem(&oprofilefs_type);
  206. }
  207. void __exit oprofilefs_unregister(void)
  208. {
  209. unregister_filesystem(&oprofilefs_type);
  210. }