bin.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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 <linux/mm.h>
  23. #include <asm/uaccess.h>
  24. #include "sysfs.h"
  25. /*
  26. * There's one bin_buffer for each open file.
  27. *
  28. * filp->private_data points to bin_buffer and
  29. * sysfs_dirent->s_bin_attr.buffers points to a the bin_buffer s
  30. * sysfs_dirent->s_bin_attr.buffers is protected by sysfs_bin_lock
  31. */
  32. static DEFINE_MUTEX(sysfs_bin_lock);
  33. struct bin_buffer {
  34. struct mutex mutex;
  35. void *buffer;
  36. int mmapped;
  37. const struct vm_operations_struct *vm_ops;
  38. struct file *file;
  39. struct hlist_node list;
  40. };
  41. static int
  42. fill_read(struct file *file, char *buffer, loff_t off, size_t count)
  43. {
  44. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  45. struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
  46. struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
  47. int rc;
  48. /* need attr_sd for attr, its parent for kobj */
  49. if (!sysfs_get_active(attr_sd))
  50. return -ENODEV;
  51. rc = -EIO;
  52. if (attr->read)
  53. rc = attr->read(file, kobj, attr, buffer, off, count);
  54. sysfs_put_active(attr_sd);
  55. return rc;
  56. }
  57. static ssize_t
  58. read(struct file *file, char __user *userbuf, size_t bytes, loff_t *off)
  59. {
  60. struct bin_buffer *bb = file->private_data;
  61. int size = file->f_path.dentry->d_inode->i_size;
  62. loff_t offs = *off;
  63. int count = min_t(size_t, bytes, PAGE_SIZE);
  64. char *temp;
  65. if (!bytes)
  66. return 0;
  67. if (size) {
  68. if (offs > size)
  69. return 0;
  70. if (offs + count > size)
  71. count = size - offs;
  72. }
  73. temp = kmalloc(count, GFP_KERNEL);
  74. if (!temp)
  75. return -ENOMEM;
  76. mutex_lock(&bb->mutex);
  77. count = fill_read(file, bb->buffer, offs, count);
  78. if (count < 0) {
  79. mutex_unlock(&bb->mutex);
  80. goto out_free;
  81. }
  82. memcpy(temp, bb->buffer, count);
  83. mutex_unlock(&bb->mutex);
  84. if (copy_to_user(userbuf, temp, count)) {
  85. count = -EFAULT;
  86. goto out_free;
  87. }
  88. pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count);
  89. *off = offs + count;
  90. out_free:
  91. kfree(temp);
  92. return count;
  93. }
  94. static int
  95. flush_write(struct file *file, char *buffer, loff_t offset, size_t count)
  96. {
  97. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  98. struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
  99. struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
  100. int rc;
  101. /* need attr_sd for attr, its parent for kobj */
  102. if (!sysfs_get_active(attr_sd))
  103. return -ENODEV;
  104. rc = -EIO;
  105. if (attr->write)
  106. rc = attr->write(file, kobj, attr, buffer, offset, count);
  107. sysfs_put_active(attr_sd);
  108. return rc;
  109. }
  110. static ssize_t write(struct file *file, const char __user *userbuf,
  111. size_t bytes, loff_t *off)
  112. {
  113. struct bin_buffer *bb = file->private_data;
  114. int size = file->f_path.dentry->d_inode->i_size;
  115. loff_t offs = *off;
  116. int count = min_t(size_t, bytes, PAGE_SIZE);
  117. char *temp;
  118. if (!bytes)
  119. return 0;
  120. if (size) {
  121. if (offs > size)
  122. return 0;
  123. if (offs + count > size)
  124. count = size - offs;
  125. }
  126. temp = memdup_user(userbuf, count);
  127. if (IS_ERR(temp))
  128. return PTR_ERR(temp);
  129. mutex_lock(&bb->mutex);
  130. memcpy(bb->buffer, temp, count);
  131. count = flush_write(file, bb->buffer, offs, count);
  132. mutex_unlock(&bb->mutex);
  133. if (count > 0)
  134. *off = offs + count;
  135. kfree(temp);
  136. return count;
  137. }
  138. static void bin_vma_open(struct vm_area_struct *vma)
  139. {
  140. struct file *file = vma->vm_file;
  141. struct bin_buffer *bb = file->private_data;
  142. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  143. if (!bb->vm_ops || !bb->vm_ops->open)
  144. return;
  145. if (!sysfs_get_active(attr_sd))
  146. return;
  147. bb->vm_ops->open(vma);
  148. sysfs_put_active(attr_sd);
  149. }
  150. static void bin_vma_close(struct vm_area_struct *vma)
  151. {
  152. struct file *file = vma->vm_file;
  153. struct bin_buffer *bb = file->private_data;
  154. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  155. if (!bb->vm_ops || !bb->vm_ops->close)
  156. return;
  157. if (!sysfs_get_active(attr_sd))
  158. return;
  159. bb->vm_ops->close(vma);
  160. sysfs_put_active(attr_sd);
  161. }
  162. static int bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  163. {
  164. struct file *file = vma->vm_file;
  165. struct bin_buffer *bb = file->private_data;
  166. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  167. int ret;
  168. if (!bb->vm_ops || !bb->vm_ops->fault)
  169. return VM_FAULT_SIGBUS;
  170. if (!sysfs_get_active(attr_sd))
  171. return VM_FAULT_SIGBUS;
  172. ret = bb->vm_ops->fault(vma, vmf);
  173. sysfs_put_active(attr_sd);
  174. return ret;
  175. }
  176. static int bin_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  177. {
  178. struct file *file = vma->vm_file;
  179. struct bin_buffer *bb = file->private_data;
  180. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  181. int ret;
  182. if (!bb->vm_ops)
  183. return VM_FAULT_SIGBUS;
  184. if (!bb->vm_ops->page_mkwrite)
  185. return 0;
  186. if (!sysfs_get_active(attr_sd))
  187. return VM_FAULT_SIGBUS;
  188. ret = bb->vm_ops->page_mkwrite(vma, vmf);
  189. sysfs_put_active(attr_sd);
  190. return ret;
  191. }
  192. static int bin_access(struct vm_area_struct *vma, unsigned long addr,
  193. void *buf, int len, int write)
  194. {
  195. struct file *file = vma->vm_file;
  196. struct bin_buffer *bb = file->private_data;
  197. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  198. int ret;
  199. if (!bb->vm_ops || !bb->vm_ops->access)
  200. return -EINVAL;
  201. if (!sysfs_get_active(attr_sd))
  202. return -EINVAL;
  203. ret = bb->vm_ops->access(vma, addr, buf, len, write);
  204. sysfs_put_active(attr_sd);
  205. return ret;
  206. }
  207. #ifdef CONFIG_NUMA
  208. static int bin_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
  209. {
  210. struct file *file = vma->vm_file;
  211. struct bin_buffer *bb = file->private_data;
  212. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  213. int ret;
  214. if (!bb->vm_ops || !bb->vm_ops->set_policy)
  215. return 0;
  216. if (!sysfs_get_active(attr_sd))
  217. return -EINVAL;
  218. ret = bb->vm_ops->set_policy(vma, new);
  219. sysfs_put_active(attr_sd);
  220. return ret;
  221. }
  222. static struct mempolicy *bin_get_policy(struct vm_area_struct *vma,
  223. unsigned long addr)
  224. {
  225. struct file *file = vma->vm_file;
  226. struct bin_buffer *bb = file->private_data;
  227. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  228. struct mempolicy *pol;
  229. if (!bb->vm_ops || !bb->vm_ops->get_policy)
  230. return vma->vm_policy;
  231. if (!sysfs_get_active(attr_sd))
  232. return vma->vm_policy;
  233. pol = bb->vm_ops->get_policy(vma, addr);
  234. sysfs_put_active(attr_sd);
  235. return pol;
  236. }
  237. static int bin_migrate(struct vm_area_struct *vma, const nodemask_t *from,
  238. const nodemask_t *to, unsigned long flags)
  239. {
  240. struct file *file = vma->vm_file;
  241. struct bin_buffer *bb = file->private_data;
  242. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  243. int ret;
  244. if (!bb->vm_ops || !bb->vm_ops->migrate)
  245. return 0;
  246. if (!sysfs_get_active(attr_sd))
  247. return 0;
  248. ret = bb->vm_ops->migrate(vma, from, to, flags);
  249. sysfs_put_active(attr_sd);
  250. return ret;
  251. }
  252. #endif
  253. static const struct vm_operations_struct bin_vm_ops = {
  254. .open = bin_vma_open,
  255. .close = bin_vma_close,
  256. .fault = bin_fault,
  257. .page_mkwrite = bin_page_mkwrite,
  258. .access = bin_access,
  259. #ifdef CONFIG_NUMA
  260. .set_policy = bin_set_policy,
  261. .get_policy = bin_get_policy,
  262. .migrate = bin_migrate,
  263. #endif
  264. };
  265. static int mmap(struct file *file, struct vm_area_struct *vma)
  266. {
  267. struct bin_buffer *bb = file->private_data;
  268. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  269. struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
  270. struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
  271. int rc;
  272. mutex_lock(&bb->mutex);
  273. /* need attr_sd for attr, its parent for kobj */
  274. rc = -ENODEV;
  275. if (!sysfs_get_active(attr_sd))
  276. goto out_unlock;
  277. rc = -EINVAL;
  278. if (!attr->mmap)
  279. goto out_put;
  280. rc = attr->mmap(file, kobj, attr, vma);
  281. if (rc)
  282. goto out_put;
  283. /*
  284. * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
  285. * to satisfy versions of X which crash if the mmap fails: that
  286. * substitutes a new vm_file, and we don't then want bin_vm_ops.
  287. */
  288. if (vma->vm_file != file)
  289. goto out_put;
  290. rc = -EINVAL;
  291. if (bb->mmapped && bb->vm_ops != vma->vm_ops)
  292. goto out_put;
  293. rc = 0;
  294. bb->mmapped = 1;
  295. bb->vm_ops = vma->vm_ops;
  296. vma->vm_ops = &bin_vm_ops;
  297. out_put:
  298. sysfs_put_active(attr_sd);
  299. out_unlock:
  300. mutex_unlock(&bb->mutex);
  301. return rc;
  302. }
  303. static int open(struct inode * inode, struct file * file)
  304. {
  305. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  306. struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
  307. struct bin_buffer *bb = NULL;
  308. int error;
  309. /* binary file operations requires both @sd and its parent */
  310. if (!sysfs_get_active(attr_sd))
  311. return -ENODEV;
  312. error = -EACCES;
  313. if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap))
  314. goto err_out;
  315. if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap))
  316. goto err_out;
  317. error = -ENOMEM;
  318. bb = kzalloc(sizeof(*bb), GFP_KERNEL);
  319. if (!bb)
  320. goto err_out;
  321. bb->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
  322. if (!bb->buffer)
  323. goto err_out;
  324. mutex_init(&bb->mutex);
  325. bb->file = file;
  326. file->private_data = bb;
  327. mutex_lock(&sysfs_bin_lock);
  328. hlist_add_head(&bb->list, &attr_sd->s_bin_attr.buffers);
  329. mutex_unlock(&sysfs_bin_lock);
  330. /* open succeeded, put active references */
  331. sysfs_put_active(attr_sd);
  332. return 0;
  333. err_out:
  334. sysfs_put_active(attr_sd);
  335. kfree(bb);
  336. return error;
  337. }
  338. static int release(struct inode * inode, struct file * file)
  339. {
  340. struct bin_buffer *bb = file->private_data;
  341. mutex_lock(&sysfs_bin_lock);
  342. hlist_del(&bb->list);
  343. mutex_unlock(&sysfs_bin_lock);
  344. kfree(bb->buffer);
  345. kfree(bb);
  346. return 0;
  347. }
  348. const struct file_operations bin_fops = {
  349. .read = read,
  350. .write = write,
  351. .mmap = mmap,
  352. .llseek = generic_file_llseek,
  353. .open = open,
  354. .release = release,
  355. };
  356. void unmap_bin_file(struct sysfs_dirent *attr_sd)
  357. {
  358. struct bin_buffer *bb;
  359. struct hlist_node *tmp;
  360. if (sysfs_type(attr_sd) != SYSFS_KOBJ_BIN_ATTR)
  361. return;
  362. mutex_lock(&sysfs_bin_lock);
  363. hlist_for_each_entry(bb, tmp, &attr_sd->s_bin_attr.buffers, list) {
  364. struct inode *inode = bb->file->f_path.dentry->d_inode;
  365. unmap_mapping_range(inode->i_mapping, 0, 0, 1);
  366. }
  367. mutex_unlock(&sysfs_bin_lock);
  368. }
  369. /**
  370. * sysfs_create_bin_file - create binary file for object.
  371. * @kobj: object.
  372. * @attr: attribute descriptor.
  373. */
  374. int sysfs_create_bin_file(struct kobject *kobj,
  375. const struct bin_attribute *attr)
  376. {
  377. BUG_ON(!kobj || !kobj->sd || !attr);
  378. return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
  379. }
  380. /**
  381. * sysfs_remove_bin_file - remove binary file for object.
  382. * @kobj: object.
  383. * @attr: attribute descriptor.
  384. */
  385. void sysfs_remove_bin_file(struct kobject *kobj,
  386. const struct bin_attribute *attr)
  387. {
  388. sysfs_hash_and_remove(kobj->sd, NULL, attr->attr.name);
  389. }
  390. EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
  391. EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);