oprofilefs.c 6.4 KB

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