bin.c 11 KB

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