oprofilefs.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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_ino = get_next_ino();
  26. inode->i_mode = mode;
  27. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  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. int oprofilefs_ulong_from_user(unsigned long *val, char const __user *buf, size_t count)
  49. {
  50. char tmpbuf[TMPBUFSIZE];
  51. unsigned long flags;
  52. if (!count)
  53. return 0;
  54. if (count > TMPBUFSIZE - 1)
  55. return -EINVAL;
  56. memset(tmpbuf, 0x0, TMPBUFSIZE);
  57. if (copy_from_user(tmpbuf, buf, count))
  58. return -EFAULT;
  59. spin_lock_irqsave(&oprofilefs_lock, flags);
  60. *val = simple_strtoul(tmpbuf, NULL, 0);
  61. spin_unlock_irqrestore(&oprofilefs_lock, flags);
  62. return 0;
  63. }
  64. static ssize_t ulong_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
  65. {
  66. unsigned long *val = file->private_data;
  67. return oprofilefs_ulong_to_user(*val, buf, count, offset);
  68. }
  69. static ssize_t ulong_write_file(struct file *file, char const __user *buf, size_t count, loff_t *offset)
  70. {
  71. unsigned long value;
  72. int retval;
  73. if (*offset)
  74. return -EINVAL;
  75. retval = oprofilefs_ulong_from_user(&value, buf, count);
  76. if (retval)
  77. return retval;
  78. retval = oprofile_set_ulong(file->private_data, value);
  79. if (retval)
  80. return retval;
  81. return count;
  82. }
  83. static int default_open(struct inode *inode, struct file *filp)
  84. {
  85. if (inode->i_private)
  86. filp->private_data = inode->i_private;
  87. return 0;
  88. }
  89. static const struct file_operations ulong_fops = {
  90. .read = ulong_read_file,
  91. .write = ulong_write_file,
  92. .open = default_open,
  93. .llseek = default_llseek,
  94. };
  95. static const struct file_operations ulong_ro_fops = {
  96. .read = ulong_read_file,
  97. .open = default_open,
  98. .llseek = default_llseek,
  99. };
  100. static int __oprofilefs_create_file(struct super_block *sb,
  101. struct dentry *root, char const *name, const struct file_operations *fops,
  102. int perm, void *priv)
  103. {
  104. struct dentry *dentry;
  105. struct inode *inode;
  106. dentry = d_alloc_name(root, name);
  107. if (!dentry)
  108. return -ENOMEM;
  109. inode = oprofilefs_get_inode(sb, S_IFREG | perm);
  110. if (!inode) {
  111. dput(dentry);
  112. return -ENOMEM;
  113. }
  114. inode->i_fop = fops;
  115. d_add(dentry, inode);
  116. dentry->d_inode->i_private = priv;
  117. return 0;
  118. }
  119. int oprofilefs_create_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_fops, 0644, val);
  124. }
  125. int oprofilefs_create_ro_ulong(struct super_block *sb, struct dentry *root,
  126. char const *name, unsigned long *val)
  127. {
  128. return __oprofilefs_create_file(sb, root, name,
  129. &ulong_ro_fops, 0444, val);
  130. }
  131. static ssize_t atomic_read_file(struct file *file, char __user *buf, size_t count, loff_t *offset)
  132. {
  133. atomic_t *val = file->private_data;
  134. return oprofilefs_ulong_to_user(atomic_read(val), buf, count, offset);
  135. }
  136. static const struct file_operations atomic_ro_fops = {
  137. .read = atomic_read_file,
  138. .open = default_open,
  139. .llseek = default_llseek,
  140. };
  141. int oprofilefs_create_ro_atomic(struct super_block *sb, struct dentry *root,
  142. char const *name, atomic_t *val)
  143. {
  144. return __oprofilefs_create_file(sb, root, name,
  145. &atomic_ro_fops, 0444, val);
  146. }
  147. int oprofilefs_create_file(struct super_block *sb, struct dentry *root,
  148. char const *name, const struct file_operations *fops)
  149. {
  150. return __oprofilefs_create_file(sb, root, name, fops, 0644, NULL);
  151. }
  152. int oprofilefs_create_file_perm(struct super_block *sb, struct dentry *root,
  153. char const *name, const struct file_operations *fops, int perm)
  154. {
  155. return __oprofilefs_create_file(sb, root, name, fops, perm, NULL);
  156. }
  157. struct dentry *oprofilefs_mkdir(struct super_block *sb,
  158. struct dentry *root, char const *name)
  159. {
  160. struct dentry *dentry;
  161. struct inode *inode;
  162. dentry = d_alloc_name(root, name);
  163. if (!dentry)
  164. return NULL;
  165. inode = oprofilefs_get_inode(sb, S_IFDIR | 0755);
  166. if (!inode) {
  167. dput(dentry);
  168. return NULL;
  169. }
  170. inode->i_op = &simple_dir_inode_operations;
  171. inode->i_fop = &simple_dir_operations;
  172. d_add(dentry, inode);
  173. return dentry;
  174. }
  175. static int oprofilefs_fill_super(struct super_block *sb, void *data, int silent)
  176. {
  177. struct inode *root_inode;
  178. struct dentry *root_dentry;
  179. sb->s_blocksize = PAGE_CACHE_SIZE;
  180. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  181. sb->s_magic = OPROFILEFS_MAGIC;
  182. sb->s_op = &s_ops;
  183. sb->s_time_gran = 1;
  184. root_inode = oprofilefs_get_inode(sb, S_IFDIR | 0755);
  185. if (!root_inode)
  186. return -ENOMEM;
  187. root_inode->i_op = &simple_dir_inode_operations;
  188. root_inode->i_fop = &simple_dir_operations;
  189. root_dentry = d_alloc_root(root_inode);
  190. if (!root_dentry) {
  191. iput(root_inode);
  192. return -ENOMEM;
  193. }
  194. sb->s_root = root_dentry;
  195. oprofile_create_files(sb, root_dentry);
  196. // FIXME: verify kill_litter_super removes our dentries
  197. return 0;
  198. }
  199. static struct dentry *oprofilefs_mount(struct file_system_type *fs_type,
  200. int flags, const char *dev_name, void *data)
  201. {
  202. return mount_single(fs_type, flags, data, oprofilefs_fill_super);
  203. }
  204. static struct file_system_type oprofilefs_type = {
  205. .owner = THIS_MODULE,
  206. .name = "oprofilefs",
  207. .mount = oprofilefs_mount,
  208. .kill_sb = kill_litter_super,
  209. };
  210. int __init oprofilefs_register(void)
  211. {
  212. return register_filesystem(&oprofilefs_type);
  213. }
  214. void __exit oprofilefs_unregister(void)
  215. {
  216. unregister_filesystem(&oprofilefs_type);
  217. }