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