bin.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 ssize_t
  42. read(struct file *file, char __user *userbuf, size_t bytes, loff_t *off)
  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. struct bin_buffer *bb = file->private_data;
  48. int size = file_inode(file)->i_size;
  49. loff_t offs = *off;
  50. int count = min_t(size_t, bytes, PAGE_SIZE);
  51. char *buf;
  52. if (!bytes)
  53. return 0;
  54. if (size) {
  55. if (offs > size)
  56. return 0;
  57. if (offs + count > size)
  58. count = size - offs;
  59. }
  60. buf = kmalloc(count, GFP_KERNEL);
  61. if (!buf)
  62. return -ENOMEM;
  63. /* need attr_sd for attr, its parent for kobj */
  64. mutex_lock(&bb->mutex);
  65. if (!sysfs_get_active(attr_sd)) {
  66. count = -ENODEV;
  67. mutex_unlock(&bb->mutex);
  68. goto out_free;
  69. }
  70. if (attr->read)
  71. count = attr->read(file, kobj, attr, buf, offs, count);
  72. else
  73. count = -EIO;
  74. sysfs_put_active(attr_sd);
  75. mutex_unlock(&bb->mutex);
  76. if (count < 0)
  77. goto out_free;
  78. if (copy_to_user(userbuf, buf, count)) {
  79. count = -EFAULT;
  80. goto out_free;
  81. }
  82. pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count);
  83. *off = offs + count;
  84. out_free:
  85. kfree(buf);
  86. return count;
  87. }
  88. static int
  89. flush_write(struct file *file, char *buffer, loff_t offset, size_t count)
  90. {
  91. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  92. struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
  93. struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
  94. int rc;
  95. /* need attr_sd for attr, its parent for kobj */
  96. if (!sysfs_get_active(attr_sd))
  97. return -ENODEV;
  98. rc = -EIO;
  99. if (attr->write)
  100. rc = attr->write(file, kobj, attr, buffer, offset, count);
  101. sysfs_put_active(attr_sd);
  102. return rc;
  103. }
  104. static ssize_t write(struct file *file, const char __user *userbuf,
  105. size_t bytes, loff_t *off)
  106. {
  107. struct bin_buffer *bb = file->private_data;
  108. int size = file_inode(file)->i_size;
  109. loff_t offs = *off;
  110. int count = min_t(size_t, bytes, PAGE_SIZE);
  111. char *temp;
  112. if (!bytes)
  113. return 0;
  114. if (size) {
  115. if (offs > size)
  116. return 0;
  117. if (offs + count > size)
  118. count = size - offs;
  119. }
  120. temp = memdup_user(userbuf, count);
  121. if (IS_ERR(temp))
  122. return PTR_ERR(temp);
  123. mutex_lock(&bb->mutex);
  124. memcpy(bb->buffer, temp, count);
  125. count = flush_write(file, bb->buffer, offs, count);
  126. mutex_unlock(&bb->mutex);
  127. if (count > 0)
  128. *off = offs + count;
  129. kfree(temp);
  130. return count;
  131. }
  132. static void bin_vma_open(struct vm_area_struct *vma)
  133. {
  134. struct file *file = vma->vm_file;
  135. struct bin_buffer *bb = file->private_data;
  136. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  137. if (!bb->vm_ops)
  138. return;
  139. if (!sysfs_get_active(attr_sd))
  140. return;
  141. if (bb->vm_ops->open)
  142. bb->vm_ops->open(vma);
  143. sysfs_put_active(attr_sd);
  144. }
  145. static int bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  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. int ret;
  151. if (!bb->vm_ops)
  152. return VM_FAULT_SIGBUS;
  153. if (!sysfs_get_active(attr_sd))
  154. return VM_FAULT_SIGBUS;
  155. ret = VM_FAULT_SIGBUS;
  156. if (bb->vm_ops->fault)
  157. ret = bb->vm_ops->fault(vma, vmf);
  158. sysfs_put_active(attr_sd);
  159. return ret;
  160. }
  161. static int bin_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  162. {
  163. struct file *file = vma->vm_file;
  164. struct bin_buffer *bb = file->private_data;
  165. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  166. int ret;
  167. if (!bb->vm_ops)
  168. return VM_FAULT_SIGBUS;
  169. if (!sysfs_get_active(attr_sd))
  170. return VM_FAULT_SIGBUS;
  171. ret = 0;
  172. if (bb->vm_ops->page_mkwrite)
  173. ret = bb->vm_ops->page_mkwrite(vma, vmf);
  174. else
  175. file_update_time(file);
  176. sysfs_put_active(attr_sd);
  177. return ret;
  178. }
  179. static int bin_access(struct vm_area_struct *vma, unsigned long addr,
  180. void *buf, int len, int write)
  181. {
  182. struct file *file = vma->vm_file;
  183. struct bin_buffer *bb = file->private_data;
  184. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  185. int ret;
  186. if (!bb->vm_ops)
  187. return -EINVAL;
  188. if (!sysfs_get_active(attr_sd))
  189. return -EINVAL;
  190. ret = -EINVAL;
  191. if (bb->vm_ops->access)
  192. ret = bb->vm_ops->access(vma, addr, buf, len, write);
  193. sysfs_put_active(attr_sd);
  194. return ret;
  195. }
  196. #ifdef CONFIG_NUMA
  197. static int bin_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
  198. {
  199. struct file *file = vma->vm_file;
  200. struct bin_buffer *bb = file->private_data;
  201. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  202. int ret;
  203. if (!bb->vm_ops)
  204. return 0;
  205. if (!sysfs_get_active(attr_sd))
  206. return -EINVAL;
  207. ret = 0;
  208. if (bb->vm_ops->set_policy)
  209. ret = bb->vm_ops->set_policy(vma, new);
  210. sysfs_put_active(attr_sd);
  211. return ret;
  212. }
  213. static struct mempolicy *bin_get_policy(struct vm_area_struct *vma,
  214. unsigned long addr)
  215. {
  216. struct file *file = vma->vm_file;
  217. struct bin_buffer *bb = file->private_data;
  218. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  219. struct mempolicy *pol;
  220. if (!bb->vm_ops)
  221. return vma->vm_policy;
  222. if (!sysfs_get_active(attr_sd))
  223. return vma->vm_policy;
  224. pol = vma->vm_policy;
  225. if (bb->vm_ops->get_policy)
  226. pol = bb->vm_ops->get_policy(vma, addr);
  227. sysfs_put_active(attr_sd);
  228. return pol;
  229. }
  230. static int bin_migrate(struct vm_area_struct *vma, const nodemask_t *from,
  231. const nodemask_t *to, unsigned long flags)
  232. {
  233. struct file *file = vma->vm_file;
  234. struct bin_buffer *bb = file->private_data;
  235. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  236. int ret;
  237. if (!bb->vm_ops)
  238. return 0;
  239. if (!sysfs_get_active(attr_sd))
  240. return 0;
  241. ret = 0;
  242. if (bb->vm_ops->migrate)
  243. ret = bb->vm_ops->migrate(vma, from, to, flags);
  244. sysfs_put_active(attr_sd);
  245. return ret;
  246. }
  247. #endif
  248. static const struct vm_operations_struct bin_vm_ops = {
  249. .open = bin_vma_open,
  250. .fault = bin_fault,
  251. .page_mkwrite = bin_page_mkwrite,
  252. .access = bin_access,
  253. #ifdef CONFIG_NUMA
  254. .set_policy = bin_set_policy,
  255. .get_policy = bin_get_policy,
  256. .migrate = bin_migrate,
  257. #endif
  258. };
  259. static int mmap(struct file *file, struct vm_area_struct *vma)
  260. {
  261. struct bin_buffer *bb = file->private_data;
  262. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  263. struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
  264. struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
  265. int rc;
  266. mutex_lock(&bb->mutex);
  267. /* need attr_sd for attr, its parent for kobj */
  268. rc = -ENODEV;
  269. if (!sysfs_get_active(attr_sd))
  270. goto out_unlock;
  271. rc = -EINVAL;
  272. if (!attr->mmap)
  273. goto out_put;
  274. rc = attr->mmap(file, kobj, attr, vma);
  275. if (rc)
  276. goto out_put;
  277. /*
  278. * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
  279. * to satisfy versions of X which crash if the mmap fails: that
  280. * substitutes a new vm_file, and we don't then want bin_vm_ops.
  281. */
  282. if (vma->vm_file != file)
  283. goto out_put;
  284. rc = -EINVAL;
  285. if (bb->mmapped && bb->vm_ops != vma->vm_ops)
  286. goto out_put;
  287. /*
  288. * It is not possible to successfully wrap close.
  289. * So error if someone is trying to use close.
  290. */
  291. rc = -EINVAL;
  292. if (vma->vm_ops && vma->vm_ops->close)
  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(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(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(attr_sd);
  333. return 0;
  334. err_out:
  335. sysfs_put_active(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. if (sysfs_type(attr_sd) != SYSFS_KOBJ_BIN_ATTR)
  361. return;
  362. mutex_lock(&sysfs_bin_lock);
  363. hlist_for_each_entry(bb, &attr_sd->s_bin_attr.buffers, list) {
  364. struct inode *inode = file_inode(bb->file);
  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. EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
  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,
  387. const struct bin_attribute *attr)
  388. {
  389. sysfs_hash_and_remove(kobj->sd, attr->attr.name, NULL);
  390. }
  391. EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);