file.c 14 KB

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