file.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * fs/sysfs/file.c - sysfs regular (text) file implementation
  3. *
  4. * Copyright (c) 2001-3 Patrick Mochel
  5. * Copyright (c) 2007 SUSE Linux Products GmbH
  6. * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
  7. *
  8. * This file is released under the GPLv2.
  9. *
  10. * Please see Documentation/filesystems/sysfs.txt for more information.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kobject.h>
  14. #include <linux/kallsyms.h>
  15. #include <linux/slab.h>
  16. #include <linux/list.h>
  17. #include <linux/mutex.h>
  18. #include <linux/seq_file.h>
  19. #include "sysfs.h"
  20. #include "../kernfs/kernfs-internal.h"
  21. /*
  22. * Determine ktype->sysfs_ops for the given kernfs_node. This function
  23. * must be called while holding an active reference.
  24. */
  25. static const struct sysfs_ops *sysfs_file_ops(struct kernfs_node *kn)
  26. {
  27. struct kobject *kobj = kn->parent->priv;
  28. if (kn->flags & KERNFS_LOCKDEP)
  29. lockdep_assert_held(kn);
  30. return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
  31. }
  32. /*
  33. * Reads on sysfs are handled through seq_file, which takes care of hairy
  34. * details like buffering and seeking. The following function pipes
  35. * sysfs_ops->show() result through seq_file.
  36. */
  37. static int sysfs_kf_seq_show(struct seq_file *sf, void *v)
  38. {
  39. struct kernfs_open_file *of = sf->private;
  40. struct kobject *kobj = of->kn->parent->priv;
  41. const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
  42. ssize_t count;
  43. char *buf;
  44. /* acquire buffer and ensure that it's >= PAGE_SIZE and clear */
  45. count = seq_get_buf(sf, &buf);
  46. if (count < PAGE_SIZE) {
  47. seq_commit(sf, -1);
  48. return 0;
  49. }
  50. memset(buf, 0, PAGE_SIZE);
  51. /*
  52. * Invoke show(). Control may reach here via seq file lseek even
  53. * if @ops->show() isn't implemented.
  54. */
  55. if (ops->show) {
  56. count = ops->show(kobj, of->kn->priv, buf);
  57. if (count < 0)
  58. return count;
  59. }
  60. /*
  61. * The code works fine with PAGE_SIZE return but it's likely to
  62. * indicate truncated result or overflow in normal use cases.
  63. */
  64. if (count >= (ssize_t)PAGE_SIZE) {
  65. print_symbol("fill_read_buffer: %s returned bad count\n",
  66. (unsigned long)ops->show);
  67. /* Try to struggle along */
  68. count = PAGE_SIZE - 1;
  69. }
  70. seq_commit(sf, count);
  71. return 0;
  72. }
  73. static ssize_t sysfs_kf_bin_read(struct kernfs_open_file *of, char *buf,
  74. size_t count, loff_t pos)
  75. {
  76. struct bin_attribute *battr = of->kn->priv;
  77. struct kobject *kobj = of->kn->parent->priv;
  78. loff_t size = file_inode(of->file)->i_size;
  79. if (!count)
  80. return 0;
  81. if (size) {
  82. if (pos > size)
  83. return 0;
  84. if (pos + count > size)
  85. count = size - pos;
  86. }
  87. if (!battr->read)
  88. return -EIO;
  89. return battr->read(of->file, kobj, battr, buf, pos, count);
  90. }
  91. /* kernfs write callback for regular sysfs files */
  92. static ssize_t sysfs_kf_write(struct kernfs_open_file *of, char *buf,
  93. size_t count, loff_t pos)
  94. {
  95. const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
  96. struct kobject *kobj = of->kn->parent->priv;
  97. if (!count)
  98. return 0;
  99. return ops->store(kobj, of->kn->priv, buf, count);
  100. }
  101. /* kernfs write callback for bin sysfs files */
  102. static ssize_t sysfs_kf_bin_write(struct kernfs_open_file *of, char *buf,
  103. size_t count, loff_t pos)
  104. {
  105. struct bin_attribute *battr = of->kn->priv;
  106. struct kobject *kobj = of->kn->parent->priv;
  107. loff_t size = file_inode(of->file)->i_size;
  108. if (size) {
  109. if (size <= pos)
  110. return 0;
  111. count = min_t(ssize_t, count, size - pos);
  112. }
  113. if (!count)
  114. return 0;
  115. if (!battr->write)
  116. return -EIO;
  117. return battr->write(of->file, kobj, battr, buf, pos, count);
  118. }
  119. static int sysfs_kf_bin_mmap(struct kernfs_open_file *of,
  120. struct vm_area_struct *vma)
  121. {
  122. struct bin_attribute *battr = of->kn->priv;
  123. struct kobject *kobj = of->kn->parent->priv;
  124. return battr->mmap(of->file, kobj, battr, vma);
  125. }
  126. void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr)
  127. {
  128. struct kernfs_node *kn = kobj->sd, *tmp;
  129. if (kn && dir)
  130. kn = kernfs_find_and_get(kn, dir);
  131. else
  132. kernfs_get(kn);
  133. if (kn && attr) {
  134. tmp = kernfs_find_and_get(kn, attr);
  135. kernfs_put(kn);
  136. kn = tmp;
  137. }
  138. if (kn) {
  139. kernfs_notify(kn);
  140. kernfs_put(kn);
  141. }
  142. }
  143. EXPORT_SYMBOL_GPL(sysfs_notify);
  144. static const struct kernfs_ops sysfs_file_kfops_empty = {
  145. };
  146. static const struct kernfs_ops sysfs_file_kfops_ro = {
  147. .seq_show = sysfs_kf_seq_show,
  148. };
  149. static const struct kernfs_ops sysfs_file_kfops_wo = {
  150. .write = sysfs_kf_write,
  151. };
  152. static const struct kernfs_ops sysfs_file_kfops_rw = {
  153. .seq_show = sysfs_kf_seq_show,
  154. .write = sysfs_kf_write,
  155. };
  156. static const struct kernfs_ops sysfs_bin_kfops_ro = {
  157. .read = sysfs_kf_bin_read,
  158. };
  159. static const struct kernfs_ops sysfs_bin_kfops_wo = {
  160. .write = sysfs_kf_bin_write,
  161. };
  162. static const struct kernfs_ops sysfs_bin_kfops_rw = {
  163. .read = sysfs_kf_bin_read,
  164. .write = sysfs_kf_bin_write,
  165. };
  166. static const struct kernfs_ops sysfs_bin_kfops_mmap = {
  167. .read = sysfs_kf_bin_read,
  168. .write = sysfs_kf_bin_write,
  169. .mmap = sysfs_kf_bin_mmap,
  170. };
  171. int sysfs_add_file_mode_ns(struct kernfs_node *parent,
  172. const struct attribute *attr, bool is_bin,
  173. umode_t mode, const void *ns)
  174. {
  175. struct lock_class_key *key = NULL;
  176. const struct kernfs_ops *ops;
  177. struct kernfs_node *kn;
  178. loff_t size;
  179. if (!is_bin) {
  180. struct kobject *kobj = parent->priv;
  181. const struct sysfs_ops *sysfs_ops = kobj->ktype->sysfs_ops;
  182. /* every kobject with an attribute needs a ktype assigned */
  183. if (WARN(!sysfs_ops, KERN_ERR
  184. "missing sysfs attribute operations for kobject: %s\n",
  185. kobject_name(kobj)))
  186. return -EINVAL;
  187. if (sysfs_ops->show && sysfs_ops->store)
  188. ops = &sysfs_file_kfops_rw;
  189. else if (sysfs_ops->show)
  190. ops = &sysfs_file_kfops_ro;
  191. else if (sysfs_ops->store)
  192. ops = &sysfs_file_kfops_wo;
  193. else
  194. ops = &sysfs_file_kfops_empty;
  195. size = PAGE_SIZE;
  196. } else {
  197. struct bin_attribute *battr = (void *)attr;
  198. if (battr->mmap)
  199. ops = &sysfs_bin_kfops_mmap;
  200. else if (battr->read && battr->write)
  201. ops = &sysfs_bin_kfops_rw;
  202. else if (battr->read)
  203. ops = &sysfs_bin_kfops_ro;
  204. else if (battr->write)
  205. ops = &sysfs_bin_kfops_wo;
  206. else
  207. ops = &sysfs_file_kfops_empty;
  208. size = battr->size;
  209. }
  210. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  211. if (!attr->ignore_lockdep)
  212. key = attr->key ?: (struct lock_class_key *)&attr->skey;
  213. #endif
  214. kn = __kernfs_create_file(parent, attr->name, mode, size, ops,
  215. (void *)attr, ns, true, key);
  216. if (IS_ERR(kn)) {
  217. if (PTR_ERR(kn) == -EEXIST)
  218. sysfs_warn_dup(parent, attr->name);
  219. return PTR_ERR(kn);
  220. }
  221. return 0;
  222. }
  223. int sysfs_add_file(struct kernfs_node *parent, const struct attribute *attr,
  224. bool is_bin)
  225. {
  226. return sysfs_add_file_mode_ns(parent, attr, is_bin, attr->mode, NULL);
  227. }
  228. /**
  229. * sysfs_create_file_ns - create an attribute file for an object with custom ns
  230. * @kobj: object we're creating for
  231. * @attr: attribute descriptor
  232. * @ns: namespace the new file should belong to
  233. */
  234. int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
  235. const void *ns)
  236. {
  237. BUG_ON(!kobj || !kobj->sd || !attr);
  238. return sysfs_add_file_mode_ns(kobj->sd, attr, false, attr->mode, ns);
  239. }
  240. EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
  241. int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
  242. {
  243. int err = 0;
  244. int i;
  245. for (i = 0; ptr[i] && !err; i++)
  246. err = sysfs_create_file(kobj, ptr[i]);
  247. if (err)
  248. while (--i >= 0)
  249. sysfs_remove_file(kobj, ptr[i]);
  250. return err;
  251. }
  252. EXPORT_SYMBOL_GPL(sysfs_create_files);
  253. /**
  254. * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
  255. * @kobj: object we're acting for.
  256. * @attr: attribute descriptor.
  257. * @group: group name.
  258. */
  259. int sysfs_add_file_to_group(struct kobject *kobj,
  260. const struct attribute *attr, const char *group)
  261. {
  262. struct kernfs_node *parent;
  263. int error;
  264. if (group) {
  265. parent = kernfs_find_and_get(kobj->sd, group);
  266. } else {
  267. parent = kobj->sd;
  268. kernfs_get(parent);
  269. }
  270. if (!parent)
  271. return -ENOENT;
  272. error = sysfs_add_file(parent, attr, false);
  273. kernfs_put(parent);
  274. return error;
  275. }
  276. EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
  277. /**
  278. * sysfs_chmod_file - update the modified mode value on an object attribute.
  279. * @kobj: object we're acting for.
  280. * @attr: attribute descriptor.
  281. * @mode: file permissions.
  282. *
  283. */
  284. int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
  285. umode_t mode)
  286. {
  287. struct kernfs_node *kn;
  288. struct iattr newattrs;
  289. int rc;
  290. kn = kernfs_find_and_get(kobj->sd, attr->name);
  291. if (!kn)
  292. return -ENOENT;
  293. newattrs.ia_mode = (mode & S_IALLUGO) | (kn->mode & ~S_IALLUGO);
  294. newattrs.ia_valid = ATTR_MODE;
  295. rc = kernfs_setattr(kn, &newattrs);
  296. kernfs_put(kn);
  297. return rc;
  298. }
  299. EXPORT_SYMBOL_GPL(sysfs_chmod_file);
  300. /**
  301. * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
  302. * @kobj: object we're acting for
  303. * @attr: attribute descriptor
  304. * @ns: namespace tag of the file to remove
  305. *
  306. * Hash the attribute name and namespace tag and kill the victim.
  307. */
  308. void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
  309. const void *ns)
  310. {
  311. struct kernfs_node *parent = kobj->sd;
  312. kernfs_remove_by_name_ns(parent, attr->name, ns);
  313. }
  314. EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
  315. /**
  316. * sysfs_remove_file_self - remove an object attribute from its own method
  317. * @kobj: object we're acting for
  318. * @attr: attribute descriptor
  319. *
  320. * See kernfs_remove_self() for details.
  321. */
  322. bool sysfs_remove_file_self(struct kobject *kobj, const struct attribute *attr)
  323. {
  324. struct kernfs_node *parent = kobj->sd;
  325. struct kernfs_node *kn;
  326. bool ret;
  327. kn = kernfs_find_and_get(parent, attr->name);
  328. if (WARN_ON_ONCE(!kn))
  329. return false;
  330. ret = kernfs_remove_self(kn);
  331. kernfs_put(kn);
  332. return ret;
  333. }
  334. void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
  335. {
  336. int i;
  337. for (i = 0; ptr[i]; i++)
  338. sysfs_remove_file(kobj, ptr[i]);
  339. }
  340. EXPORT_SYMBOL_GPL(sysfs_remove_files);
  341. /**
  342. * sysfs_remove_file_from_group - remove an attribute file from a group.
  343. * @kobj: object we're acting for.
  344. * @attr: attribute descriptor.
  345. * @group: group name.
  346. */
  347. void sysfs_remove_file_from_group(struct kobject *kobj,
  348. const struct attribute *attr, const char *group)
  349. {
  350. struct kernfs_node *parent;
  351. if (group) {
  352. parent = kernfs_find_and_get(kobj->sd, group);
  353. } else {
  354. parent = kobj->sd;
  355. kernfs_get(parent);
  356. }
  357. if (parent) {
  358. kernfs_remove_by_name(parent, attr->name);
  359. kernfs_put(parent);
  360. }
  361. }
  362. EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
  363. /**
  364. * sysfs_create_bin_file - create binary file for object.
  365. * @kobj: object.
  366. * @attr: attribute descriptor.
  367. */
  368. int sysfs_create_bin_file(struct kobject *kobj,
  369. const struct bin_attribute *attr)
  370. {
  371. BUG_ON(!kobj || !kobj->sd || !attr);
  372. return sysfs_add_file(kobj->sd, &attr->attr, true);
  373. }
  374. EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
  375. /**
  376. * sysfs_remove_bin_file - remove binary file for object.
  377. * @kobj: object.
  378. * @attr: attribute descriptor.
  379. */
  380. void sysfs_remove_bin_file(struct kobject *kobj,
  381. const struct bin_attribute *attr)
  382. {
  383. kernfs_remove_by_name(kobj->sd, attr->attr.name);
  384. }
  385. EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);