bin.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * bin.c - binary file operations for sysfs.
  3. *
  4. * Copyright (c) 2003 Patrick Mochel
  5. * Copyright (c) 2003 Matthew Wilcox
  6. * Copyright (c) 2004 Silicon Graphics, Inc.
  7. */
  8. #undef DEBUG
  9. #include <linux/errno.h>
  10. #include <linux/fs.h>
  11. #include <linux/kernel.h>
  12. #include <linux/kobject.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <asm/uaccess.h>
  16. #include <asm/semaphore.h>
  17. #include "sysfs.h"
  18. struct bin_buffer {
  19. struct mutex mutex;
  20. void *buffer;
  21. int mmapped;
  22. };
  23. static int
  24. fill_read(struct dentry *dentry, char *buffer, loff_t off, size_t count)
  25. {
  26. struct sysfs_dirent *attr_sd = dentry->d_fsdata;
  27. struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr;
  28. struct kobject *kobj = attr_sd->s_parent->s_elem.dir.kobj;
  29. int rc;
  30. /* need attr_sd for attr, its parent for kobj */
  31. if (!sysfs_get_active_two(attr_sd))
  32. return -ENODEV;
  33. rc = -EIO;
  34. if (attr->read)
  35. rc = attr->read(kobj, buffer, off, count);
  36. sysfs_put_active_two(attr_sd);
  37. return rc;
  38. }
  39. static ssize_t
  40. read(struct file *file, char __user *userbuf, size_t bytes, loff_t *off)
  41. {
  42. struct bin_buffer *bb = file->private_data;
  43. struct dentry *dentry = file->f_path.dentry;
  44. int size = dentry->d_inode->i_size;
  45. loff_t offs = *off;
  46. int count = min_t(size_t, bytes, PAGE_SIZE);
  47. if (size) {
  48. if (offs > size)
  49. return 0;
  50. if (offs + count > size)
  51. count = size - offs;
  52. }
  53. mutex_lock(&bb->mutex);
  54. count = fill_read(dentry, bb->buffer, offs, count);
  55. if (count < 0)
  56. goto out_unlock;
  57. if (copy_to_user(userbuf, bb->buffer, count)) {
  58. count = -EFAULT;
  59. goto out_unlock;
  60. }
  61. pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count);
  62. *off = offs + count;
  63. out_unlock:
  64. mutex_unlock(&bb->mutex);
  65. return count;
  66. }
  67. static int
  68. flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count)
  69. {
  70. struct sysfs_dirent *attr_sd = dentry->d_fsdata;
  71. struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr;
  72. struct kobject *kobj = attr_sd->s_parent->s_elem.dir.kobj;
  73. int rc;
  74. /* need attr_sd for attr, its parent for kobj */
  75. if (!sysfs_get_active_two(attr_sd))
  76. return -ENODEV;
  77. rc = -EIO;
  78. if (attr->write)
  79. rc = attr->write(kobj, buffer, offset, count);
  80. sysfs_put_active_two(attr_sd);
  81. return rc;
  82. }
  83. static ssize_t write(struct file *file, const char __user *userbuf,
  84. size_t bytes, loff_t *off)
  85. {
  86. struct bin_buffer *bb = file->private_data;
  87. struct dentry *dentry = file->f_path.dentry;
  88. int size = dentry->d_inode->i_size;
  89. loff_t offs = *off;
  90. int count = min_t(size_t, bytes, PAGE_SIZE);
  91. if (size) {
  92. if (offs > size)
  93. return 0;
  94. if (offs + count > size)
  95. count = size - offs;
  96. }
  97. mutex_lock(&bb->mutex);
  98. if (copy_from_user(bb->buffer, userbuf, count)) {
  99. count = -EFAULT;
  100. goto out_unlock;
  101. }
  102. count = flush_write(dentry, bb->buffer, offs, count);
  103. if (count > 0)
  104. *off = offs + count;
  105. out_unlock:
  106. mutex_unlock(&bb->mutex);
  107. return count;
  108. }
  109. static int mmap(struct file *file, struct vm_area_struct *vma)
  110. {
  111. struct bin_buffer *bb = file->private_data;
  112. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  113. struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr;
  114. struct kobject *kobj = attr_sd->s_parent->s_elem.dir.kobj;
  115. int rc;
  116. mutex_lock(&bb->mutex);
  117. /* need attr_sd for attr, its parent for kobj */
  118. if (!sysfs_get_active_two(attr_sd))
  119. return -ENODEV;
  120. rc = -EINVAL;
  121. if (attr->mmap)
  122. rc = attr->mmap(kobj, attr, vma);
  123. if (rc == 0 && !bb->mmapped)
  124. bb->mmapped = 1;
  125. else
  126. sysfs_put_active_two(attr_sd);
  127. mutex_unlock(&bb->mutex);
  128. return rc;
  129. }
  130. static int open(struct inode * inode, struct file * file)
  131. {
  132. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  133. struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr;
  134. struct bin_buffer *bb = NULL;
  135. int error;
  136. /* need attr_sd for attr */
  137. if (!sysfs_get_active(attr_sd))
  138. return -ENODEV;
  139. error = -EACCES;
  140. if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap))
  141. goto err_out;
  142. if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap))
  143. goto err_out;
  144. error = -ENOMEM;
  145. bb = kzalloc(sizeof(*bb), GFP_KERNEL);
  146. if (!bb)
  147. goto err_out;
  148. bb->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
  149. if (!bb->buffer)
  150. goto err_out;
  151. mutex_init(&bb->mutex);
  152. file->private_data = bb;
  153. /* open succeeded, put active reference and pin attr_sd */
  154. sysfs_put_active(attr_sd);
  155. sysfs_get(attr_sd);
  156. return 0;
  157. err_out:
  158. sysfs_put_active(attr_sd);
  159. kfree(bb);
  160. return error;
  161. }
  162. static int release(struct inode * inode, struct file * file)
  163. {
  164. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  165. struct bin_buffer *bb = file->private_data;
  166. if (bb->mmapped)
  167. sysfs_put_active_two(attr_sd);
  168. sysfs_put(attr_sd);
  169. kfree(bb->buffer);
  170. kfree(bb);
  171. return 0;
  172. }
  173. const struct file_operations bin_fops = {
  174. .read = read,
  175. .write = write,
  176. .mmap = mmap,
  177. .llseek = generic_file_llseek,
  178. .open = open,
  179. .release = release,
  180. };
  181. /**
  182. * sysfs_create_bin_file - create binary file for object.
  183. * @kobj: object.
  184. * @attr: attribute descriptor.
  185. */
  186. int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr)
  187. {
  188. BUG_ON(!kobj || !kobj->dentry || !attr);
  189. return sysfs_add_file(kobj->dentry, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
  190. }
  191. /**
  192. * sysfs_remove_bin_file - remove binary file for object.
  193. * @kobj: object.
  194. * @attr: attribute descriptor.
  195. */
  196. void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr)
  197. {
  198. if (sysfs_hash_and_remove(kobj->dentry, attr->attr.name) < 0) {
  199. printk(KERN_ERR "%s: "
  200. "bad dentry or inode or no such file: \"%s\"\n",
  201. __FUNCTION__, attr->attr.name);
  202. dump_stack();
  203. }
  204. }
  205. EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
  206. EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);