file.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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/slab.h>
  14. #include <linux/list.h>
  15. #include <linux/mutex.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/mm.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. printk("fill_read_buffer: %pS returned bad count\n",
  65. 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, kuid_t uid, kgid_t gid, 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, uid, gid,
  260. size, ops, (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. /**
  269. * sysfs_create_file_ns - create an attribute file for an object with custom ns
  270. * @kobj: object we're creating for
  271. * @attr: attribute descriptor
  272. * @ns: namespace the new file should belong to
  273. */
  274. int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
  275. const void *ns)
  276. {
  277. kuid_t uid;
  278. kgid_t gid;
  279. BUG_ON(!kobj || !kobj->sd || !attr);
  280. kobject_get_ownership(kobj, &uid, &gid);
  281. return sysfs_add_file_mode_ns(kobj->sd, attr, false, attr->mode,
  282. uid, gid, ns);
  283. }
  284. EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
  285. int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
  286. {
  287. int err = 0;
  288. int i;
  289. for (i = 0; ptr[i] && !err; i++)
  290. err = sysfs_create_file(kobj, ptr[i]);
  291. if (err)
  292. while (--i >= 0)
  293. sysfs_remove_file(kobj, ptr[i]);
  294. return err;
  295. }
  296. EXPORT_SYMBOL_GPL(sysfs_create_files);
  297. /**
  298. * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
  299. * @kobj: object we're acting for.
  300. * @attr: attribute descriptor.
  301. * @group: group name.
  302. */
  303. int sysfs_add_file_to_group(struct kobject *kobj,
  304. const struct attribute *attr, const char *group)
  305. {
  306. struct kernfs_node *parent;
  307. kuid_t uid;
  308. kgid_t gid;
  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. kobject_get_ownership(kobj, &uid, &gid);
  319. error = sysfs_add_file_mode_ns(parent, attr, false,
  320. attr->mode, uid, gid, NULL);
  321. kernfs_put(parent);
  322. return error;
  323. }
  324. EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
  325. /**
  326. * sysfs_chmod_file - update the modified mode value on an object attribute.
  327. * @kobj: object we're acting for.
  328. * @attr: attribute descriptor.
  329. * @mode: file permissions.
  330. *
  331. */
  332. int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
  333. umode_t mode)
  334. {
  335. struct kernfs_node *kn;
  336. struct iattr newattrs;
  337. int rc;
  338. kn = kernfs_find_and_get(kobj->sd, attr->name);
  339. if (!kn)
  340. return -ENOENT;
  341. newattrs.ia_mode = (mode & S_IALLUGO) | (kn->mode & ~S_IALLUGO);
  342. newattrs.ia_valid = ATTR_MODE;
  343. rc = kernfs_setattr(kn, &newattrs);
  344. kernfs_put(kn);
  345. return rc;
  346. }
  347. EXPORT_SYMBOL_GPL(sysfs_chmod_file);
  348. /**
  349. * sysfs_break_active_protection - break "active" protection
  350. * @kobj: The kernel object @attr is associated with.
  351. * @attr: The attribute to break the "active" protection for.
  352. *
  353. * With sysfs, just like kernfs, deletion of an attribute is postponed until
  354. * all active .show() and .store() callbacks have finished unless this function
  355. * is called. Hence this function is useful in methods that implement self
  356. * deletion.
  357. */
  358. struct kernfs_node *sysfs_break_active_protection(struct kobject *kobj,
  359. const struct attribute *attr)
  360. {
  361. struct kernfs_node *kn;
  362. kobject_get(kobj);
  363. kn = kernfs_find_and_get(kobj->sd, attr->name);
  364. if (kn)
  365. kernfs_break_active_protection(kn);
  366. return kn;
  367. }
  368. EXPORT_SYMBOL_GPL(sysfs_break_active_protection);
  369. /**
  370. * sysfs_unbreak_active_protection - restore "active" protection
  371. * @kn: Pointer returned by sysfs_break_active_protection().
  372. *
  373. * Undo the effects of sysfs_break_active_protection(). Since this function
  374. * calls kernfs_put() on the kernfs node that corresponds to the 'attr'
  375. * argument passed to sysfs_break_active_protection() that attribute may have
  376. * been removed between the sysfs_break_active_protection() and
  377. * sysfs_unbreak_active_protection() calls, it is not safe to access @kn after
  378. * this function has returned.
  379. */
  380. void sysfs_unbreak_active_protection(struct kernfs_node *kn)
  381. {
  382. struct kobject *kobj = kn->parent->priv;
  383. kernfs_unbreak_active_protection(kn);
  384. kernfs_put(kn);
  385. kobject_put(kobj);
  386. }
  387. EXPORT_SYMBOL_GPL(sysfs_unbreak_active_protection);
  388. /**
  389. * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
  390. * @kobj: object we're acting for
  391. * @attr: attribute descriptor
  392. * @ns: namespace tag of the file to remove
  393. *
  394. * Hash the attribute name and namespace tag and kill the victim.
  395. */
  396. void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
  397. const void *ns)
  398. {
  399. struct kernfs_node *parent = kobj->sd;
  400. kernfs_remove_by_name_ns(parent, attr->name, ns);
  401. }
  402. EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
  403. /**
  404. * sysfs_remove_file_self - remove an object attribute from its own method
  405. * @kobj: object we're acting for
  406. * @attr: attribute descriptor
  407. *
  408. * See kernfs_remove_self() for details.
  409. */
  410. bool sysfs_remove_file_self(struct kobject *kobj, const struct attribute *attr)
  411. {
  412. struct kernfs_node *parent = kobj->sd;
  413. struct kernfs_node *kn;
  414. bool ret;
  415. kn = kernfs_find_and_get(parent, attr->name);
  416. if (WARN_ON_ONCE(!kn))
  417. return false;
  418. ret = kernfs_remove_self(kn);
  419. kernfs_put(kn);
  420. return ret;
  421. }
  422. void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
  423. {
  424. int i;
  425. for (i = 0; ptr[i]; i++)
  426. sysfs_remove_file(kobj, ptr[i]);
  427. }
  428. EXPORT_SYMBOL_GPL(sysfs_remove_files);
  429. /**
  430. * sysfs_remove_file_from_group - remove an attribute file from a group.
  431. * @kobj: object we're acting for.
  432. * @attr: attribute descriptor.
  433. * @group: group name.
  434. */
  435. void sysfs_remove_file_from_group(struct kobject *kobj,
  436. const struct attribute *attr, const char *group)
  437. {
  438. struct kernfs_node *parent;
  439. if (group) {
  440. parent = kernfs_find_and_get(kobj->sd, group);
  441. } else {
  442. parent = kobj->sd;
  443. kernfs_get(parent);
  444. }
  445. if (parent) {
  446. kernfs_remove_by_name(parent, attr->name);
  447. kernfs_put(parent);
  448. }
  449. }
  450. EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
  451. /**
  452. * sysfs_create_bin_file - create binary file for object.
  453. * @kobj: object.
  454. * @attr: attribute descriptor.
  455. */
  456. int sysfs_create_bin_file(struct kobject *kobj,
  457. const struct bin_attribute *attr)
  458. {
  459. kuid_t uid;
  460. kgid_t gid;
  461. BUG_ON(!kobj || !kobj->sd || !attr);
  462. kobject_get_ownership(kobj, &uid, &gid);
  463. return sysfs_add_file_mode_ns(kobj->sd, &attr->attr, true,
  464. attr->attr.mode, uid, gid, NULL);
  465. }
  466. EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
  467. /**
  468. * sysfs_remove_bin_file - remove binary file for object.
  469. * @kobj: object.
  470. * @attr: attribute descriptor.
  471. */
  472. void sysfs_remove_bin_file(struct kobject *kobj,
  473. const struct bin_attribute *attr)
  474. {
  475. kernfs_remove_by_name(kobj->sd, attr->attr.name);
  476. }
  477. EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
  478. /**
  479. * sysfs_emit - scnprintf equivalent, aware of PAGE_SIZE buffer.
  480. * @buf: start of PAGE_SIZE buffer.
  481. * @fmt: format
  482. * @...: optional arguments to @format
  483. *
  484. *
  485. * Returns number of characters written to @buf.
  486. */
  487. int sysfs_emit(char *buf, const char *fmt, ...)
  488. {
  489. va_list args;
  490. int len;
  491. if (WARN(!buf || offset_in_page(buf),
  492. "invalid sysfs_emit: buf:%p\n", buf))
  493. return 0;
  494. va_start(args, fmt);
  495. len = vscnprintf(buf, PAGE_SIZE, fmt, args);
  496. va_end(args);
  497. return len;
  498. }
  499. EXPORT_SYMBOL_GPL(sysfs_emit);
  500. /**
  501. * sysfs_emit_at - scnprintf equivalent, aware of PAGE_SIZE buffer.
  502. * @buf: start of PAGE_SIZE buffer.
  503. * @at: offset in @buf to start write in bytes
  504. * @at must be >= 0 && < PAGE_SIZE
  505. * @fmt: format
  506. * @...: optional arguments to @fmt
  507. *
  508. *
  509. * Returns number of characters written starting at &@buf[@at].
  510. */
  511. int sysfs_emit_at(char *buf, int at, const char *fmt, ...)
  512. {
  513. va_list args;
  514. int len;
  515. if (WARN(!buf || offset_in_page(buf) || at < 0 || at >= PAGE_SIZE,
  516. "invalid sysfs_emit_at: buf:%p at:%d\n", buf, at))
  517. return 0;
  518. va_start(args, fmt);
  519. len = vscnprintf(buf + at, PAGE_SIZE - at, fmt, args);
  520. va_end(args);
  521. return len;
  522. }
  523. EXPORT_SYMBOL_GPL(sysfs_emit_at);