bin.c 11 KB

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