oprofilefs.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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_uid = 0;
  27. inode->i_gid = 0;
  28. inode->i_blocks = 0;
  29. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  30. }
  31. return inode;
  32. }
  33. static struct super_operations s_ops = {
  34. .statfs = simple_statfs,
  35. .drop_inode = generic_delete_inode,
  36. };
  37. ssize_t oprofilefs_str_to_user(char const * str, char __user * buf, size_t count, loff_t * offset)
  38. {
  39. return simple_read_from_buffer(buf, count, offset, str, strlen(str));
  40. }
  41. #define TMPBUFSIZE 50
  42. ssize_t oprofilefs_ulong_to_user(unsigned long val, char __user * buf, size_t count, loff_t * offset)
  43. {
  44. char tmpbuf[TMPBUFSIZE];
  45. size_t maxlen = snprintf(tmpbuf, TMPBUFSIZE, "%lu\n", val);
  46. if (maxlen > TMPBUFSIZE)
  47. maxlen = TMPBUFSIZE;
  48. return simple_read_from_buffer(buf, count, offset, tmpbuf, maxlen);
  49. }
  50. int oprofilefs_ulong_from_user(unsigned long * val, char const __user * buf, size_t count)
  51. {
  52. char tmpbuf[TMPBUFSIZE];
  53. if (!count)
  54. return 0;
  55. if (count > TMPBUFSIZE - 1)
  56. return -EINVAL;
  57. memset(tmpbuf, 0x0, TMPBUFSIZE);
  58. if (copy_from_user(tmpbuf, buf, count))
  59. return -EFAULT;
  60. spin_lock(&oprofilefs_lock);
  61. *val = simple_strtoul(tmpbuf, NULL, 0);
  62. spin_unlock(&oprofilefs_lock);
  63. return 0;
  64. }
  65. static ssize_t ulong_read_file(struct file * file, char __user * buf, size_t count, loff_t * offset)
  66. {
  67. unsigned long * val = file->private_data;
  68. return oprofilefs_ulong_to_user(*val, buf, count, offset);
  69. }
  70. static ssize_t ulong_write_file(struct file * file, char const __user * buf, size_t count, loff_t * offset)
  71. {
  72. unsigned long * value = file->private_data;
  73. int retval;
  74. if (*offset)
  75. return -EINVAL;
  76. retval = oprofilefs_ulong_from_user(value, buf, count);
  77. if (retval)
  78. return retval;
  79. return count;
  80. }
  81. static int default_open(struct inode * inode, struct file * filp)
  82. {
  83. if (inode->i_private)
  84. filp->private_data = inode->i_private;
  85. return 0;
  86. }
  87. static struct file_operations ulong_fops = {
  88. .read = ulong_read_file,
  89. .write = ulong_write_file,
  90. .open = default_open,
  91. };
  92. static struct file_operations ulong_ro_fops = {
  93. .read = ulong_read_file,
  94. .open = default_open,
  95. };
  96. static struct dentry * __oprofilefs_create_file(struct super_block * sb,
  97. struct dentry * root, char const * name, const struct file_operations * fops,
  98. int perm)
  99. {
  100. struct dentry * dentry;
  101. struct inode * inode;
  102. dentry = d_alloc_name(root, name);
  103. if (!dentry)
  104. return NULL;
  105. inode = oprofilefs_get_inode(sb, S_IFREG | perm);
  106. if (!inode) {
  107. dput(dentry);
  108. return NULL;
  109. }
  110. inode->i_fop = fops;
  111. d_add(dentry, inode);
  112. return dentry;
  113. }
  114. int oprofilefs_create_ulong(struct super_block * sb, struct dentry * root,
  115. char const * name, unsigned long * val)
  116. {
  117. struct dentry * d = __oprofilefs_create_file(sb, root, name,
  118. &ulong_fops, 0644);
  119. if (!d)
  120. return -EFAULT;
  121. d->d_inode->i_private = val;
  122. return 0;
  123. }
  124. int oprofilefs_create_ro_ulong(struct super_block * sb, struct dentry * root,
  125. char const * name, unsigned long * val)
  126. {
  127. struct dentry * d = __oprofilefs_create_file(sb, root, name,
  128. &ulong_ro_fops, 0444);
  129. if (!d)
  130. return -EFAULT;
  131. d->d_inode->i_private = val;
  132. return 0;
  133. }
  134. static ssize_t atomic_read_file(struct file * file, char __user * buf, size_t count, loff_t * offset)
  135. {
  136. atomic_t * val = file->private_data;
  137. return oprofilefs_ulong_to_user(atomic_read(val), buf, count, offset);
  138. }
  139. static struct file_operations atomic_ro_fops = {
  140. .read = atomic_read_file,
  141. .open = default_open,
  142. };
  143. int oprofilefs_create_ro_atomic(struct super_block * sb, struct dentry * root,
  144. char const * name, atomic_t * val)
  145. {
  146. struct dentry * d = __oprofilefs_create_file(sb, root, name,
  147. &atomic_ro_fops, 0444);
  148. if (!d)
  149. return -EFAULT;
  150. d->d_inode->i_private = val;
  151. return 0;
  152. }
  153. int oprofilefs_create_file(struct super_block * sb, struct dentry * root,
  154. char const * name, const struct file_operations * fops)
  155. {
  156. if (!__oprofilefs_create_file(sb, root, name, fops, 0644))
  157. return -EFAULT;
  158. return 0;
  159. }
  160. int oprofilefs_create_file_perm(struct super_block * sb, struct dentry * root,
  161. char const * name, const struct file_operations * fops, int perm)
  162. {
  163. if (!__oprofilefs_create_file(sb, root, name, fops, perm))
  164. return -EFAULT;
  165. return 0;
  166. }
  167. struct dentry * oprofilefs_mkdir(struct super_block * sb,
  168. struct dentry * root, char const * name)
  169. {
  170. struct dentry * dentry;
  171. struct inode * inode;
  172. dentry = d_alloc_name(root, name);
  173. if (!dentry)
  174. return NULL;
  175. inode = oprofilefs_get_inode(sb, S_IFDIR | 0755);
  176. if (!inode) {
  177. dput(dentry);
  178. return NULL;
  179. }
  180. inode->i_op = &simple_dir_inode_operations;
  181. inode->i_fop = &simple_dir_operations;
  182. d_add(dentry, inode);
  183. return dentry;
  184. }
  185. static int oprofilefs_fill_super(struct super_block * sb, void * data, int silent)
  186. {
  187. struct inode * root_inode;
  188. struct dentry * root_dentry;
  189. sb->s_blocksize = PAGE_CACHE_SIZE;
  190. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  191. sb->s_magic = OPROFILEFS_MAGIC;
  192. sb->s_op = &s_ops;
  193. sb->s_time_gran = 1;
  194. root_inode = oprofilefs_get_inode(sb, S_IFDIR | 0755);
  195. if (!root_inode)
  196. return -ENOMEM;
  197. root_inode->i_op = &simple_dir_inode_operations;
  198. root_inode->i_fop = &simple_dir_operations;
  199. root_dentry = d_alloc_root(root_inode);
  200. if (!root_dentry) {
  201. iput(root_inode);
  202. return -ENOMEM;
  203. }
  204. sb->s_root = root_dentry;
  205. oprofile_create_files(sb, root_dentry);
  206. // FIXME: verify kill_litter_super removes our dentries
  207. return 0;
  208. }
  209. static int oprofilefs_get_sb(struct file_system_type *fs_type,
  210. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  211. {
  212. return get_sb_single(fs_type, flags, data, oprofilefs_fill_super, mnt);
  213. }
  214. static struct file_system_type oprofilefs_type = {
  215. .owner = THIS_MODULE,
  216. .name = "oprofilefs",
  217. .get_sb = oprofilefs_get_sb,
  218. .kill_sb = kill_litter_super,
  219. };
  220. int __init oprofilefs_register(void)
  221. {
  222. return register_filesystem(&oprofilefs_type);
  223. }
  224. void __exit oprofilefs_unregister(void)
  225. {
  226. unregister_filesystem(&oprofilefs_type);
  227. }