file.c 12 KB

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