bin.c 5.9 KB

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