oprofilefs.c 6.2 KB

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