bin.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 (!bytes)
  55. return 0;
  56. if (size) {
  57. if (offs > size)
  58. return 0;
  59. if (offs + count > size)
  60. count = size - offs;
  61. }
  62. temp = kmalloc(count, GFP_KERNEL);
  63. if (!temp)
  64. return -ENOMEM;
  65. mutex_lock(&bb->mutex);
  66. count = fill_read(dentry, bb->buffer, offs, count);
  67. if (count < 0) {
  68. mutex_unlock(&bb->mutex);
  69. goto out_free;
  70. }
  71. memcpy(temp, bb->buffer, count);
  72. mutex_unlock(&bb->mutex);
  73. if (copy_to_user(userbuf, temp, count)) {
  74. count = -EFAULT;
  75. goto out_free;
  76. }
  77. pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count);
  78. *off = offs + count;
  79. out_free:
  80. kfree(temp);
  81. return count;
  82. }
  83. static int
  84. flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count)
  85. {
  86. struct sysfs_dirent *attr_sd = dentry->d_fsdata;
  87. struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
  88. struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
  89. int rc;
  90. /* need attr_sd for attr, its parent for kobj */
  91. if (!sysfs_get_active_two(attr_sd))
  92. return -ENODEV;
  93. rc = -EIO;
  94. if (attr->write)
  95. rc = attr->write(kobj, attr, buffer, offset, count);
  96. sysfs_put_active_two(attr_sd);
  97. return rc;
  98. }
  99. static ssize_t write(struct file *file, const char __user *userbuf,
  100. size_t bytes, loff_t *off)
  101. {
  102. struct bin_buffer *bb = file->private_data;
  103. struct dentry *dentry = file->f_path.dentry;
  104. int size = dentry->d_inode->i_size;
  105. loff_t offs = *off;
  106. int count = min_t(size_t, bytes, PAGE_SIZE);
  107. char *temp;
  108. if (!bytes)
  109. return 0;
  110. if (size) {
  111. if (offs > size)
  112. return 0;
  113. if (offs + count > size)
  114. count = size - offs;
  115. }
  116. temp = kmalloc(count, GFP_KERNEL);
  117. if (!temp)
  118. return -ENOMEM;
  119. if (copy_from_user(temp, userbuf, count)) {
  120. count = -EFAULT;
  121. goto out_free;
  122. }
  123. mutex_lock(&bb->mutex);
  124. memcpy(bb->buffer, temp, count);
  125. count = flush_write(dentry, bb->buffer, offs, count);
  126. mutex_unlock(&bb->mutex);
  127. if (count > 0)
  128. *off = offs + count;
  129. out_free:
  130. kfree(temp);
  131. return count;
  132. }
  133. static int mmap(struct file *file, struct vm_area_struct *vma)
  134. {
  135. struct bin_buffer *bb = file->private_data;
  136. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  137. struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
  138. struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
  139. int rc;
  140. mutex_lock(&bb->mutex);
  141. /* need attr_sd for attr, its parent for kobj */
  142. if (!sysfs_get_active_two(attr_sd))
  143. return -ENODEV;
  144. rc = -EINVAL;
  145. if (attr->mmap)
  146. rc = attr->mmap(kobj, attr, vma);
  147. if (rc == 0 && !bb->mmapped)
  148. bb->mmapped = 1;
  149. else
  150. sysfs_put_active_two(attr_sd);
  151. mutex_unlock(&bb->mutex);
  152. return rc;
  153. }
  154. static int open(struct inode * inode, struct file * file)
  155. {
  156. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  157. struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
  158. struct bin_buffer *bb = NULL;
  159. int error;
  160. /* binary file operations requires both @sd and its parent */
  161. if (!sysfs_get_active_two(attr_sd))
  162. return -ENODEV;
  163. error = -EACCES;
  164. if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap))
  165. goto err_out;
  166. if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap))
  167. goto err_out;
  168. error = -ENOMEM;
  169. bb = kzalloc(sizeof(*bb), GFP_KERNEL);
  170. if (!bb)
  171. goto err_out;
  172. bb->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
  173. if (!bb->buffer)
  174. goto err_out;
  175. mutex_init(&bb->mutex);
  176. file->private_data = bb;
  177. /* open succeeded, put active references */
  178. sysfs_put_active_two(attr_sd);
  179. return 0;
  180. err_out:
  181. sysfs_put_active_two(attr_sd);
  182. kfree(bb);
  183. return error;
  184. }
  185. static int release(struct inode * inode, struct file * file)
  186. {
  187. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  188. struct bin_buffer *bb = file->private_data;
  189. if (bb->mmapped)
  190. sysfs_put_active_two(attr_sd);
  191. kfree(bb->buffer);
  192. kfree(bb);
  193. return 0;
  194. }
  195. const struct file_operations bin_fops = {
  196. .read = read,
  197. .write = write,
  198. .mmap = mmap,
  199. .llseek = generic_file_llseek,
  200. .open = open,
  201. .release = release,
  202. };
  203. /**
  204. * sysfs_create_bin_file - create binary file for object.
  205. * @kobj: object.
  206. * @attr: attribute descriptor.
  207. */
  208. int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr)
  209. {
  210. BUG_ON(!kobj || !kobj->sd || !attr);
  211. return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
  212. }
  213. /**
  214. * sysfs_remove_bin_file - remove binary file for object.
  215. * @kobj: object.
  216. * @attr: attribute descriptor.
  217. */
  218. void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr)
  219. {
  220. sysfs_hash_and_remove(kobj->sd, attr->attr.name);
  221. }
  222. EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
  223. EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);