bin.c 5.6 KB

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