1
0

file.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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.rst 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. static struct kobject *sysfs_file_kobj(struct kernfs_node *kn)
  20. {
  21. guard(rcu)();
  22. return rcu_dereference(kn->__parent)->priv;
  23. }
  24. /*
  25. * Determine ktype->sysfs_ops for the given kernfs_node. This function
  26. * must be called while holding an active reference.
  27. */
  28. static const struct sysfs_ops *sysfs_file_ops(struct kernfs_node *kn)
  29. {
  30. struct kobject *kobj = sysfs_file_kobj(kn);
  31. if (kn->flags & KERNFS_LOCKDEP)
  32. lockdep_assert_held(kn);
  33. return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
  34. }
  35. /*
  36. * Reads on sysfs are handled through seq_file, which takes care of hairy
  37. * details like buffering and seeking. The following function pipes
  38. * sysfs_ops->show() result through seq_file.
  39. */
  40. static int sysfs_kf_seq_show(struct seq_file *sf, void *v)
  41. {
  42. struct kernfs_open_file *of = sf->private;
  43. struct kobject *kobj = sysfs_file_kobj(of->kn);
  44. const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
  45. ssize_t count;
  46. char *buf;
  47. if (WARN_ON_ONCE(!ops->show))
  48. return -EINVAL;
  49. /* acquire buffer and ensure that it's >= PAGE_SIZE and clear */
  50. count = seq_get_buf(sf, &buf);
  51. if (count < PAGE_SIZE) {
  52. seq_commit(sf, -1);
  53. return 0;
  54. }
  55. memset(buf, 0, PAGE_SIZE);
  56. count = ops->show(kobj, of->kn->priv, buf);
  57. if (count < 0)
  58. return count;
  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. const struct bin_attribute *battr = of->kn->priv;
  76. struct kobject *kobj = sysfs_file_kobj(of->kn);
  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 = sysfs_file_kobj(of->kn);
  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 = sysfs_file_kobj(of->kn);
  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. const struct bin_attribute *battr = of->kn->priv;
  129. struct kobject *kobj = sysfs_file_kobj(of->kn);
  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. const struct bin_attribute *battr = of->kn->priv;
  146. struct kobject *kobj = sysfs_file_kobj(of->kn);
  147. return battr->mmap(of->file, kobj, battr, vma);
  148. }
  149. static loff_t sysfs_kf_bin_llseek(struct kernfs_open_file *of, loff_t offset,
  150. int whence)
  151. {
  152. const struct bin_attribute *battr = of->kn->priv;
  153. struct kobject *kobj = sysfs_file_kobj(of->kn);
  154. if (battr->llseek)
  155. return battr->llseek(of->file, kobj, battr, offset, whence);
  156. else
  157. return generic_file_llseek(of->file, offset, whence);
  158. }
  159. static int sysfs_kf_bin_open(struct kernfs_open_file *of)
  160. {
  161. const struct bin_attribute *battr = of->kn->priv;
  162. if (battr->f_mapping)
  163. of->file->f_mapping = battr->f_mapping();
  164. return 0;
  165. }
  166. void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr)
  167. {
  168. struct kernfs_node *kn = kobj->sd, *tmp;
  169. if (kn && dir)
  170. kn = kernfs_find_and_get(kn, dir);
  171. else
  172. kernfs_get(kn);
  173. if (kn && attr) {
  174. tmp = kernfs_find_and_get(kn, attr);
  175. kernfs_put(kn);
  176. kn = tmp;
  177. }
  178. if (kn) {
  179. kernfs_notify(kn);
  180. kernfs_put(kn);
  181. }
  182. }
  183. EXPORT_SYMBOL_GPL(sysfs_notify);
  184. static const struct kernfs_ops sysfs_file_kfops_empty = {
  185. };
  186. static const struct kernfs_ops sysfs_file_kfops_ro = {
  187. .seq_show = sysfs_kf_seq_show,
  188. };
  189. static const struct kernfs_ops sysfs_file_kfops_wo = {
  190. .write = sysfs_kf_write,
  191. };
  192. static const struct kernfs_ops sysfs_file_kfops_rw = {
  193. .seq_show = sysfs_kf_seq_show,
  194. .write = sysfs_kf_write,
  195. };
  196. static const struct kernfs_ops sysfs_prealloc_kfops_ro = {
  197. .read = sysfs_kf_read,
  198. .prealloc = true,
  199. };
  200. static const struct kernfs_ops sysfs_prealloc_kfops_wo = {
  201. .write = sysfs_kf_write,
  202. .prealloc = true,
  203. };
  204. static const struct kernfs_ops sysfs_prealloc_kfops_rw = {
  205. .read = sysfs_kf_read,
  206. .write = sysfs_kf_write,
  207. .prealloc = true,
  208. };
  209. static const struct kernfs_ops sysfs_bin_kfops_ro = {
  210. .read = sysfs_kf_bin_read,
  211. };
  212. static const struct kernfs_ops sysfs_bin_kfops_wo = {
  213. .write = sysfs_kf_bin_write,
  214. };
  215. static const struct kernfs_ops sysfs_bin_kfops_rw = {
  216. .read = sysfs_kf_bin_read,
  217. .write = sysfs_kf_bin_write,
  218. };
  219. static const struct kernfs_ops sysfs_bin_kfops_mmap = {
  220. .read = sysfs_kf_bin_read,
  221. .write = sysfs_kf_bin_write,
  222. .mmap = sysfs_kf_bin_mmap,
  223. .open = sysfs_kf_bin_open,
  224. .llseek = sysfs_kf_bin_llseek,
  225. };
  226. int sysfs_add_file_mode_ns(struct kernfs_node *parent,
  227. const struct attribute *attr, umode_t mode, kuid_t uid,
  228. kgid_t gid, const struct ns_common *ns)
  229. {
  230. struct kobject *kobj = parent->priv;
  231. const struct sysfs_ops *sysfs_ops = kobj->ktype->sysfs_ops;
  232. struct lock_class_key *key = NULL;
  233. const struct kernfs_ops *ops = NULL;
  234. struct kernfs_node *kn;
  235. /* every kobject with an attribute needs a ktype assigned */
  236. if (WARN(!sysfs_ops, KERN_ERR
  237. "missing sysfs attribute operations for kobject: %s\n",
  238. kobject_name(kobj)))
  239. return -EINVAL;
  240. if (mode & SYSFS_PREALLOC) {
  241. if (sysfs_ops->show && sysfs_ops->store)
  242. ops = &sysfs_prealloc_kfops_rw;
  243. else if (sysfs_ops->show)
  244. ops = &sysfs_prealloc_kfops_ro;
  245. else if (sysfs_ops->store)
  246. ops = &sysfs_prealloc_kfops_wo;
  247. } else {
  248. if (sysfs_ops->show && sysfs_ops->store)
  249. ops = &sysfs_file_kfops_rw;
  250. else if (sysfs_ops->show)
  251. ops = &sysfs_file_kfops_ro;
  252. else if (sysfs_ops->store)
  253. ops = &sysfs_file_kfops_wo;
  254. }
  255. if (!ops)
  256. ops = &sysfs_file_kfops_empty;
  257. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  258. if (!attr->ignore_lockdep)
  259. key = attr->key ?: (struct lock_class_key *)&attr->skey;
  260. #endif
  261. kn = __kernfs_create_file(parent, attr->name, mode & 0777, uid, gid,
  262. PAGE_SIZE, ops, (void *)attr, ns, key);
  263. if (IS_ERR(kn)) {
  264. if (PTR_ERR(kn) == -EEXIST)
  265. sysfs_warn_dup(parent, attr->name);
  266. return PTR_ERR(kn);
  267. }
  268. return 0;
  269. }
  270. int sysfs_add_bin_file_mode_ns(struct kernfs_node *parent,
  271. const struct bin_attribute *battr, umode_t mode, size_t size,
  272. kuid_t uid, kgid_t gid, const struct ns_common *ns)
  273. {
  274. const struct attribute *attr = &battr->attr;
  275. struct lock_class_key *key = NULL;
  276. const struct kernfs_ops *ops;
  277. struct kernfs_node *kn;
  278. if (battr->mmap)
  279. ops = &sysfs_bin_kfops_mmap;
  280. else if (battr->read && battr->write)
  281. ops = &sysfs_bin_kfops_rw;
  282. else if (battr->read)
  283. ops = &sysfs_bin_kfops_ro;
  284. else if (battr->write)
  285. ops = &sysfs_bin_kfops_wo;
  286. else
  287. ops = &sysfs_file_kfops_empty;
  288. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  289. if (!attr->ignore_lockdep)
  290. key = attr->key ?: (struct lock_class_key *)&attr->skey;
  291. #endif
  292. kn = __kernfs_create_file(parent, attr->name, mode & 0777, uid, gid,
  293. size, ops, (void *)attr, ns, key);
  294. if (IS_ERR(kn)) {
  295. if (PTR_ERR(kn) == -EEXIST)
  296. sysfs_warn_dup(parent, attr->name);
  297. return PTR_ERR(kn);
  298. }
  299. return 0;
  300. }
  301. /**
  302. * sysfs_create_file_ns - create an attribute file for an object with custom ns
  303. * @kobj: object we're creating for
  304. * @attr: attribute descriptor
  305. * @ns: namespace the new file should belong to
  306. */
  307. int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
  308. const struct ns_common *ns)
  309. {
  310. kuid_t uid;
  311. kgid_t gid;
  312. if (WARN_ON(!kobj || !kobj->sd || !attr))
  313. return -EINVAL;
  314. kobject_get_ownership(kobj, &uid, &gid);
  315. return sysfs_add_file_mode_ns(kobj->sd, attr, attr->mode, uid, gid, ns);
  316. }
  317. EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
  318. int sysfs_create_files(struct kobject *kobj, const struct attribute * const *ptr)
  319. {
  320. int err = 0;
  321. int i;
  322. for (i = 0; ptr[i] && !err; i++)
  323. err = sysfs_create_file(kobj, ptr[i]);
  324. if (err)
  325. while (--i >= 0)
  326. sysfs_remove_file(kobj, ptr[i]);
  327. return err;
  328. }
  329. EXPORT_SYMBOL_GPL(sysfs_create_files);
  330. /**
  331. * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
  332. * @kobj: object we're acting for.
  333. * @attr: attribute descriptor.
  334. * @group: group name.
  335. */
  336. int sysfs_add_file_to_group(struct kobject *kobj,
  337. const struct attribute *attr, const char *group)
  338. {
  339. struct kernfs_node *parent;
  340. kuid_t uid;
  341. kgid_t gid;
  342. int error;
  343. if (group) {
  344. parent = kernfs_find_and_get(kobj->sd, group);
  345. } else {
  346. parent = kobj->sd;
  347. kernfs_get(parent);
  348. }
  349. if (!parent)
  350. return -ENOENT;
  351. kobject_get_ownership(kobj, &uid, &gid);
  352. error = sysfs_add_file_mode_ns(parent, attr, attr->mode, uid, gid,
  353. NULL);
  354. kernfs_put(parent);
  355. return error;
  356. }
  357. EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
  358. /**
  359. * sysfs_chmod_file - update the modified mode value on an object attribute.
  360. * @kobj: object we're acting for.
  361. * @attr: attribute descriptor.
  362. * @mode: file permissions.
  363. *
  364. */
  365. int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
  366. umode_t mode)
  367. {
  368. struct kernfs_node *kn;
  369. struct iattr newattrs;
  370. int rc;
  371. kn = kernfs_find_and_get(kobj->sd, attr->name);
  372. if (!kn)
  373. return -ENOENT;
  374. newattrs.ia_mode = (mode & S_IALLUGO) | (kn->mode & ~S_IALLUGO);
  375. newattrs.ia_valid = ATTR_MODE;
  376. rc = kernfs_setattr(kn, &newattrs);
  377. kernfs_put(kn);
  378. return rc;
  379. }
  380. EXPORT_SYMBOL_GPL(sysfs_chmod_file);
  381. /**
  382. * sysfs_break_active_protection - break "active" protection
  383. * @kobj: The kernel object @attr is associated with.
  384. * @attr: The attribute to break the "active" protection for.
  385. *
  386. * With sysfs, just like kernfs, deletion of an attribute is postponed until
  387. * all active .show() and .store() callbacks have finished unless this function
  388. * is called. Hence this function is useful in methods that implement self
  389. * deletion.
  390. */
  391. struct kernfs_node *sysfs_break_active_protection(struct kobject *kobj,
  392. const struct attribute *attr)
  393. {
  394. struct kernfs_node *kn;
  395. kobject_get(kobj);
  396. kn = kernfs_find_and_get(kobj->sd, attr->name);
  397. if (kn)
  398. kernfs_break_active_protection(kn);
  399. else
  400. kobject_put(kobj);
  401. return kn;
  402. }
  403. EXPORT_SYMBOL_GPL(sysfs_break_active_protection);
  404. /**
  405. * sysfs_unbreak_active_protection - restore "active" protection
  406. * @kn: Pointer returned by sysfs_break_active_protection().
  407. *
  408. * Undo the effects of sysfs_break_active_protection(). Since this function
  409. * calls kernfs_put() on the kernfs node that corresponds to the 'attr'
  410. * argument passed to sysfs_break_active_protection() that attribute may have
  411. * been removed between the sysfs_break_active_protection() and
  412. * sysfs_unbreak_active_protection() calls, it is not safe to access @kn after
  413. * this function has returned.
  414. */
  415. void sysfs_unbreak_active_protection(struct kernfs_node *kn)
  416. {
  417. struct kobject *kobj = sysfs_file_kobj(kn);
  418. kernfs_unbreak_active_protection(kn);
  419. kernfs_put(kn);
  420. kobject_put(kobj);
  421. }
  422. EXPORT_SYMBOL_GPL(sysfs_unbreak_active_protection);
  423. /**
  424. * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
  425. * @kobj: object we're acting for
  426. * @attr: attribute descriptor
  427. * @ns: namespace tag of the file to remove
  428. *
  429. * Hash the attribute name and namespace tag and kill the victim.
  430. */
  431. void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
  432. const struct ns_common *ns)
  433. {
  434. struct kernfs_node *parent = kobj->sd;
  435. kernfs_remove_by_name_ns(parent, attr->name, ns);
  436. }
  437. EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
  438. /**
  439. * sysfs_remove_file_self - remove an object attribute from its own method
  440. * @kobj: object we're acting for
  441. * @attr: attribute descriptor
  442. *
  443. * See kernfs_remove_self() for details.
  444. */
  445. bool sysfs_remove_file_self(struct kobject *kobj, const struct attribute *attr)
  446. {
  447. struct kernfs_node *parent = kobj->sd;
  448. struct kernfs_node *kn;
  449. bool ret;
  450. kn = kernfs_find_and_get(parent, attr->name);
  451. if (WARN_ON_ONCE(!kn))
  452. return false;
  453. ret = kernfs_remove_self(kn);
  454. kernfs_put(kn);
  455. return ret;
  456. }
  457. EXPORT_SYMBOL_GPL(sysfs_remove_file_self);
  458. void sysfs_remove_files(struct kobject *kobj, const struct attribute * const *ptr)
  459. {
  460. int i;
  461. for (i = 0; ptr[i]; i++)
  462. sysfs_remove_file(kobj, ptr[i]);
  463. }
  464. EXPORT_SYMBOL_GPL(sysfs_remove_files);
  465. /**
  466. * sysfs_remove_file_from_group - remove an attribute file from a group.
  467. * @kobj: object we're acting for.
  468. * @attr: attribute descriptor.
  469. * @group: group name.
  470. */
  471. void sysfs_remove_file_from_group(struct kobject *kobj,
  472. const struct attribute *attr, const char *group)
  473. {
  474. struct kernfs_node *parent;
  475. if (group) {
  476. parent = kernfs_find_and_get(kobj->sd, group);
  477. } else {
  478. parent = kobj->sd;
  479. kernfs_get(parent);
  480. }
  481. if (parent) {
  482. kernfs_remove_by_name(parent, attr->name);
  483. kernfs_put(parent);
  484. }
  485. }
  486. EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
  487. /**
  488. * sysfs_create_bin_file - create binary file for object.
  489. * @kobj: object.
  490. * @attr: attribute descriptor.
  491. */
  492. int sysfs_create_bin_file(struct kobject *kobj,
  493. const struct bin_attribute *attr)
  494. {
  495. kuid_t uid;
  496. kgid_t gid;
  497. if (WARN_ON(!kobj || !kobj->sd || !attr))
  498. return -EINVAL;
  499. kobject_get_ownership(kobj, &uid, &gid);
  500. return sysfs_add_bin_file_mode_ns(kobj->sd, attr, attr->attr.mode,
  501. attr->size, uid, gid, NULL);
  502. }
  503. EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
  504. /**
  505. * sysfs_remove_bin_file - remove binary file for object.
  506. * @kobj: object.
  507. * @attr: attribute descriptor.
  508. */
  509. void sysfs_remove_bin_file(struct kobject *kobj,
  510. const struct bin_attribute *attr)
  511. {
  512. kernfs_remove_by_name(kobj->sd, attr->attr.name);
  513. }
  514. EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
  515. static int internal_change_owner(struct kernfs_node *kn, kuid_t kuid,
  516. kgid_t kgid)
  517. {
  518. struct iattr newattrs = {
  519. .ia_valid = ATTR_UID | ATTR_GID,
  520. .ia_uid = kuid,
  521. .ia_gid = kgid,
  522. };
  523. return kernfs_setattr(kn, &newattrs);
  524. }
  525. /**
  526. * sysfs_link_change_owner - change owner of a sysfs file.
  527. * @kobj: object of the kernfs_node the symlink is located in.
  528. * @targ: object of the kernfs_node the symlink points to.
  529. * @name: name of the link.
  530. * @kuid: new owner's kuid
  531. * @kgid: new owner's kgid
  532. *
  533. * This function looks up the sysfs symlink entry @name under @kobj and changes
  534. * the ownership to @kuid/@kgid. The symlink is looked up in the namespace of
  535. * @targ.
  536. *
  537. * Returns 0 on success or error code on failure.
  538. */
  539. int sysfs_link_change_owner(struct kobject *kobj, struct kobject *targ,
  540. const char *name, kuid_t kuid, kgid_t kgid)
  541. {
  542. struct kernfs_node *kn = NULL;
  543. int error;
  544. if (!name || !kobj->state_in_sysfs || !targ->state_in_sysfs)
  545. return -EINVAL;
  546. error = -ENOENT;
  547. kn = kernfs_find_and_get_ns(kobj->sd, name, targ->sd->ns);
  548. if (!kn)
  549. goto out;
  550. error = -EINVAL;
  551. if (kernfs_type(kn) != KERNFS_LINK)
  552. goto out;
  553. if (kn->symlink.target_kn->priv != targ)
  554. goto out;
  555. error = internal_change_owner(kn, kuid, kgid);
  556. out:
  557. kernfs_put(kn);
  558. return error;
  559. }
  560. /**
  561. * sysfs_file_change_owner - change owner of a sysfs file.
  562. * @kobj: object.
  563. * @name: name of the file to change.
  564. * @kuid: new owner's kuid
  565. * @kgid: new owner's kgid
  566. *
  567. * This function looks up the sysfs entry @name under @kobj and changes the
  568. * ownership to @kuid/@kgid.
  569. *
  570. * Returns 0 on success or error code on failure.
  571. */
  572. int sysfs_file_change_owner(struct kobject *kobj, const char *name, kuid_t kuid,
  573. kgid_t kgid)
  574. {
  575. struct kernfs_node *kn;
  576. int error;
  577. if (!name)
  578. return -EINVAL;
  579. if (!kobj->state_in_sysfs)
  580. return -EINVAL;
  581. kn = kernfs_find_and_get(kobj->sd, name);
  582. if (!kn)
  583. return -ENOENT;
  584. error = internal_change_owner(kn, kuid, kgid);
  585. kernfs_put(kn);
  586. return error;
  587. }
  588. /**
  589. * sysfs_change_owner - change owner of the given object.
  590. * @kobj: object.
  591. * @kuid: new owner's kuid
  592. * @kgid: new owner's kgid
  593. *
  594. * Change the owner of the default directory, files, groups, and attributes of
  595. * @kobj to @kuid/@kgid. Note that sysfs_change_owner mirrors how the sysfs
  596. * entries for a kobject are added by driver core. In summary,
  597. * sysfs_change_owner() takes care of the default directory entry for @kobj,
  598. * the default attributes associated with the ktype of @kobj and the default
  599. * attributes associated with the ktype of @kobj.
  600. * Additional properties not added by driver core have to be changed by the
  601. * driver or subsystem which created them. This is similar to how
  602. * driver/subsystem specific entries are removed.
  603. *
  604. * Returns 0 on success or error code on failure.
  605. */
  606. int sysfs_change_owner(struct kobject *kobj, kuid_t kuid, kgid_t kgid)
  607. {
  608. int error;
  609. const struct kobj_type *ktype;
  610. if (!kobj->state_in_sysfs)
  611. return -EINVAL;
  612. /* Change the owner of the kobject itself. */
  613. error = internal_change_owner(kobj->sd, kuid, kgid);
  614. if (error)
  615. return error;
  616. ktype = get_ktype(kobj);
  617. if (ktype) {
  618. /*
  619. * Change owner of the default groups associated with the
  620. * ktype of @kobj.
  621. */
  622. error = sysfs_groups_change_owner(kobj, ktype->default_groups,
  623. kuid, kgid);
  624. if (error)
  625. return error;
  626. }
  627. return 0;
  628. }
  629. /**
  630. * sysfs_emit - scnprintf equivalent, aware of PAGE_SIZE buffer.
  631. * @buf: start of PAGE_SIZE buffer.
  632. * @fmt: format
  633. * @...: optional arguments to @format
  634. *
  635. *
  636. * Returns number of characters written to @buf.
  637. */
  638. int sysfs_emit(char *buf, const char *fmt, ...)
  639. {
  640. va_list args;
  641. int len;
  642. if (WARN(!buf || offset_in_page(buf),
  643. "invalid sysfs_emit: buf:%p\n", buf))
  644. return 0;
  645. va_start(args, fmt);
  646. len = vscnprintf(buf, PAGE_SIZE, fmt, args);
  647. va_end(args);
  648. return len;
  649. }
  650. EXPORT_SYMBOL_GPL(sysfs_emit);
  651. /**
  652. * sysfs_emit_at - scnprintf equivalent, aware of PAGE_SIZE buffer.
  653. * @buf: start of PAGE_SIZE buffer.
  654. * @at: offset in @buf to start write in bytes
  655. * @at must be >= 0 && < PAGE_SIZE
  656. * @fmt: format
  657. * @...: optional arguments to @fmt
  658. *
  659. *
  660. * Returns number of characters written starting at &@buf[@at].
  661. */
  662. int sysfs_emit_at(char *buf, int at, const char *fmt, ...)
  663. {
  664. va_list args;
  665. int len;
  666. if (WARN(!buf || offset_in_page(buf) || at < 0 || at >= PAGE_SIZE,
  667. "invalid sysfs_emit_at: buf:%p at:%d\n", buf, at))
  668. return 0;
  669. va_start(args, fmt);
  670. len = vscnprintf(buf + at, PAGE_SIZE - at, fmt, args);
  671. va_end(args);
  672. return len;
  673. }
  674. EXPORT_SYMBOL_GPL(sysfs_emit_at);
  675. /**
  676. * sysfs_bin_attr_simple_read - read callback to simply copy from memory.
  677. * @file: attribute file which is being read.
  678. * @kobj: object to which the attribute belongs.
  679. * @attr: attribute descriptor.
  680. * @buf: destination buffer.
  681. * @off: offset in bytes from which to read.
  682. * @count: maximum number of bytes to read.
  683. *
  684. * Simple ->read() callback for bin_attributes backed by a buffer in memory.
  685. * The @private and @size members in struct bin_attribute must be set to the
  686. * buffer's location and size before the bin_attribute is created in sysfs.
  687. *
  688. * Bounds check for @off and @count is done in sysfs_kf_bin_read().
  689. * Negative value check for @off is done in vfs_setpos() and default_llseek().
  690. *
  691. * Returns number of bytes written to @buf.
  692. */
  693. ssize_t sysfs_bin_attr_simple_read(struct file *file, struct kobject *kobj,
  694. const struct bin_attribute *attr, char *buf,
  695. loff_t off, size_t count)
  696. {
  697. memcpy(buf, attr->private + off, count);
  698. return count;
  699. }
  700. EXPORT_SYMBOL_GPL(sysfs_bin_attr_simple_read);