file.c 12 KB

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