bin.c 11 KB

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