file.c 12 KB

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