file.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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 read callback for regular sysfs files with pre-alloc */
  92. static ssize_t sysfs_kf_read(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. size_t len;
  98. /*
  99. * If buf != of->prealloc_buf, we don't know how
  100. * large it is, so cannot safely pass it to ->show
  101. */
  102. if (pos || WARN_ON_ONCE(buf != of->prealloc_buf))
  103. return 0;
  104. len = ops->show(kobj, of->kn->priv, buf);
  105. return min(count, len);
  106. }
  107. /* kernfs write callback for regular sysfs files */
  108. static ssize_t sysfs_kf_write(struct kernfs_open_file *of, char *buf,
  109. size_t count, loff_t pos)
  110. {
  111. const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
  112. struct kobject *kobj = of->kn->parent->priv;
  113. if (!count)
  114. return 0;
  115. return ops->store(kobj, of->kn->priv, buf, count);
  116. }
  117. /* kernfs write callback for bin sysfs files */
  118. static ssize_t sysfs_kf_bin_write(struct kernfs_open_file *of, char *buf,
  119. size_t count, loff_t pos)
  120. {
  121. struct bin_attribute *battr = of->kn->priv;
  122. struct kobject *kobj = of->kn->parent->priv;
  123. loff_t size = file_inode(of->file)->i_size;
  124. if (size) {
  125. if (size <= pos)
  126. return -EFBIG;
  127. count = min_t(ssize_t, count, size - pos);
  128. }
  129. if (!count)
  130. return 0;
  131. if (!battr->write)
  132. return -EIO;
  133. return battr->write(of->file, kobj, battr, buf, pos, count);
  134. }
  135. static int sysfs_kf_bin_mmap(struct kernfs_open_file *of,
  136. struct vm_area_struct *vma)
  137. {
  138. struct bin_attribute *battr = of->kn->priv;
  139. struct kobject *kobj = of->kn->parent->priv;
  140. return battr->mmap(of->file, kobj, battr, vma);
  141. }
  142. void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr)
  143. {
  144. struct kernfs_node *kn = kobj->sd, *tmp;
  145. if (kn && dir)
  146. kn = kernfs_find_and_get(kn, dir);
  147. else
  148. kernfs_get(kn);
  149. if (kn && attr) {
  150. tmp = kernfs_find_and_get(kn, attr);
  151. kernfs_put(kn);
  152. kn = tmp;
  153. }
  154. if (kn) {
  155. kernfs_notify(kn);
  156. kernfs_put(kn);
  157. }
  158. }
  159. EXPORT_SYMBOL_GPL(sysfs_notify);
  160. static const struct kernfs_ops sysfs_file_kfops_empty = {
  161. };
  162. static const struct kernfs_ops sysfs_file_kfops_ro = {
  163. .seq_show = sysfs_kf_seq_show,
  164. };
  165. static const struct kernfs_ops sysfs_file_kfops_wo = {
  166. .write = sysfs_kf_write,
  167. };
  168. static const struct kernfs_ops sysfs_file_kfops_rw = {
  169. .seq_show = sysfs_kf_seq_show,
  170. .write = sysfs_kf_write,
  171. };
  172. static const struct kernfs_ops sysfs_prealloc_kfops_ro = {
  173. .read = sysfs_kf_read,
  174. .prealloc = true,
  175. };
  176. static const struct kernfs_ops sysfs_prealloc_kfops_wo = {
  177. .write = sysfs_kf_write,
  178. .prealloc = true,
  179. };
  180. static const struct kernfs_ops sysfs_prealloc_kfops_rw = {
  181. .read = sysfs_kf_read,
  182. .write = sysfs_kf_write,
  183. .prealloc = true,
  184. };
  185. static const struct kernfs_ops sysfs_bin_kfops_ro = {
  186. .read = sysfs_kf_bin_read,
  187. };
  188. static const struct kernfs_ops sysfs_bin_kfops_wo = {
  189. .write = sysfs_kf_bin_write,
  190. };
  191. static const struct kernfs_ops sysfs_bin_kfops_rw = {
  192. .read = sysfs_kf_bin_read,
  193. .write = sysfs_kf_bin_write,
  194. };
  195. static const struct kernfs_ops sysfs_bin_kfops_mmap = {
  196. .read = sysfs_kf_bin_read,
  197. .write = sysfs_kf_bin_write,
  198. .mmap = sysfs_kf_bin_mmap,
  199. };
  200. int sysfs_add_file_mode_ns(struct kernfs_node *parent,
  201. const struct attribute *attr, bool is_bin,
  202. umode_t mode, const void *ns)
  203. {
  204. struct lock_class_key *key = NULL;
  205. const struct kernfs_ops *ops;
  206. struct kernfs_node *kn;
  207. loff_t size;
  208. if (!is_bin) {
  209. struct kobject *kobj = parent->priv;
  210. const struct sysfs_ops *sysfs_ops = kobj->ktype->sysfs_ops;
  211. /* every kobject with an attribute needs a ktype assigned */
  212. if (WARN(!sysfs_ops, KERN_ERR
  213. "missing sysfs attribute operations for kobject: %s\n",
  214. kobject_name(kobj)))
  215. return -EINVAL;
  216. if (sysfs_ops->show && sysfs_ops->store) {
  217. if (mode & SYSFS_PREALLOC)
  218. ops = &sysfs_prealloc_kfops_rw;
  219. else
  220. ops = &sysfs_file_kfops_rw;
  221. } else if (sysfs_ops->show) {
  222. if (mode & SYSFS_PREALLOC)
  223. ops = &sysfs_prealloc_kfops_ro;
  224. else
  225. ops = &sysfs_file_kfops_ro;
  226. } else if (sysfs_ops->store) {
  227. if (mode & SYSFS_PREALLOC)
  228. ops = &sysfs_prealloc_kfops_wo;
  229. else
  230. ops = &sysfs_file_kfops_wo;
  231. } else
  232. ops = &sysfs_file_kfops_empty;
  233. size = PAGE_SIZE;
  234. } else {
  235. struct bin_attribute *battr = (void *)attr;
  236. if (battr->mmap)
  237. ops = &sysfs_bin_kfops_mmap;
  238. else if (battr->read && battr->write)
  239. ops = &sysfs_bin_kfops_rw;
  240. else if (battr->read)
  241. ops = &sysfs_bin_kfops_ro;
  242. else if (battr->write)
  243. ops = &sysfs_bin_kfops_wo;
  244. else
  245. ops = &sysfs_file_kfops_empty;
  246. size = battr->size;
  247. }
  248. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  249. if (!attr->ignore_lockdep)
  250. key = attr->key ?: (struct lock_class_key *)&attr->skey;
  251. #endif
  252. kn = __kernfs_create_file(parent, attr->name, mode & 0777, size, ops,
  253. (void *)attr, ns, key);
  254. if (IS_ERR(kn)) {
  255. if (PTR_ERR(kn) == -EEXIST)
  256. sysfs_warn_dup(parent, attr->name);
  257. return PTR_ERR(kn);
  258. }
  259. return 0;
  260. }
  261. int sysfs_add_file(struct kernfs_node *parent, const struct attribute *attr,
  262. bool is_bin)
  263. {
  264. return sysfs_add_file_mode_ns(parent, attr, is_bin, attr->mode, NULL);
  265. }
  266. /**
  267. * sysfs_create_file_ns - create an attribute file for an object with custom ns
  268. * @kobj: object we're creating for
  269. * @attr: attribute descriptor
  270. * @ns: namespace the new file should belong to
  271. */
  272. int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
  273. const void *ns)
  274. {
  275. BUG_ON(!kobj || !kobj->sd || !attr);
  276. return sysfs_add_file_mode_ns(kobj->sd, attr, false, attr->mode, ns);
  277. }
  278. EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
  279. int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
  280. {
  281. int err = 0;
  282. int i;
  283. for (i = 0; ptr[i] && !err; i++)
  284. err = sysfs_create_file(kobj, ptr[i]);
  285. if (err)
  286. while (--i >= 0)
  287. sysfs_remove_file(kobj, ptr[i]);
  288. return err;
  289. }
  290. EXPORT_SYMBOL_GPL(sysfs_create_files);
  291. /**
  292. * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
  293. * @kobj: object we're acting for.
  294. * @attr: attribute descriptor.
  295. * @group: group name.
  296. */
  297. int sysfs_add_file_to_group(struct kobject *kobj,
  298. const struct attribute *attr, const char *group)
  299. {
  300. struct kernfs_node *parent;
  301. int error;
  302. if (group) {
  303. parent = kernfs_find_and_get(kobj->sd, group);
  304. } else {
  305. parent = kobj->sd;
  306. kernfs_get(parent);
  307. }
  308. if (!parent)
  309. return -ENOENT;
  310. error = sysfs_add_file(parent, attr, false);
  311. kernfs_put(parent);
  312. return error;
  313. }
  314. EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
  315. /**
  316. * sysfs_chmod_file - update the modified mode value on an object attribute.
  317. * @kobj: object we're acting for.
  318. * @attr: attribute descriptor.
  319. * @mode: file permissions.
  320. *
  321. */
  322. int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
  323. umode_t mode)
  324. {
  325. struct kernfs_node *kn;
  326. struct iattr newattrs;
  327. int rc;
  328. kn = kernfs_find_and_get(kobj->sd, attr->name);
  329. if (!kn)
  330. return -ENOENT;
  331. newattrs.ia_mode = (mode & S_IALLUGO) | (kn->mode & ~S_IALLUGO);
  332. newattrs.ia_valid = ATTR_MODE;
  333. rc = kernfs_setattr(kn, &newattrs);
  334. kernfs_put(kn);
  335. return rc;
  336. }
  337. EXPORT_SYMBOL_GPL(sysfs_chmod_file);
  338. /**
  339. * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
  340. * @kobj: object we're acting for
  341. * @attr: attribute descriptor
  342. * @ns: namespace tag of the file to remove
  343. *
  344. * Hash the attribute name and namespace tag and kill the victim.
  345. */
  346. void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
  347. const void *ns)
  348. {
  349. struct kernfs_node *parent = kobj->sd;
  350. kernfs_remove_by_name_ns(parent, attr->name, ns);
  351. }
  352. EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
  353. /**
  354. * sysfs_remove_file_self - remove an object attribute from its own method
  355. * @kobj: object we're acting for
  356. * @attr: attribute descriptor
  357. *
  358. * See kernfs_remove_self() for details.
  359. */
  360. bool sysfs_remove_file_self(struct kobject *kobj, const struct attribute *attr)
  361. {
  362. struct kernfs_node *parent = kobj->sd;
  363. struct kernfs_node *kn;
  364. bool ret;
  365. kn = kernfs_find_and_get(parent, attr->name);
  366. if (WARN_ON_ONCE(!kn))
  367. return false;
  368. ret = kernfs_remove_self(kn);
  369. kernfs_put(kn);
  370. return ret;
  371. }
  372. void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
  373. {
  374. int i;
  375. for (i = 0; ptr[i]; i++)
  376. sysfs_remove_file(kobj, ptr[i]);
  377. }
  378. EXPORT_SYMBOL_GPL(sysfs_remove_files);
  379. /**
  380. * sysfs_remove_file_from_group - remove an attribute file from a group.
  381. * @kobj: object we're acting for.
  382. * @attr: attribute descriptor.
  383. * @group: group name.
  384. */
  385. void sysfs_remove_file_from_group(struct kobject *kobj,
  386. const struct attribute *attr, const char *group)
  387. {
  388. struct kernfs_node *parent;
  389. if (group) {
  390. parent = kernfs_find_and_get(kobj->sd, group);
  391. } else {
  392. parent = kobj->sd;
  393. kernfs_get(parent);
  394. }
  395. if (parent) {
  396. kernfs_remove_by_name(parent, attr->name);
  397. kernfs_put(parent);
  398. }
  399. }
  400. EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
  401. /**
  402. * sysfs_create_bin_file - create binary file for object.
  403. * @kobj: object.
  404. * @attr: attribute descriptor.
  405. */
  406. int sysfs_create_bin_file(struct kobject *kobj,
  407. const struct bin_attribute *attr)
  408. {
  409. BUG_ON(!kobj || !kobj->sd || !attr);
  410. return sysfs_add_file(kobj->sd, &attr->attr, true);
  411. }
  412. EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
  413. /**
  414. * sysfs_remove_bin_file - remove binary file for object.
  415. * @kobj: object.
  416. * @attr: attribute descriptor.
  417. */
  418. void sysfs_remove_bin_file(struct kobject *kobj,
  419. const struct bin_attribute *attr)
  420. {
  421. kernfs_remove_by_name(kobj->sd, attr->attr.name);
  422. }
  423. EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);