file.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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 */
  45. count = seq_get_buf(sf, &buf);
  46. if (count < PAGE_SIZE) {
  47. seq_commit(sf, -1);
  48. return 0;
  49. }
  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. print_symbol("fill_read_buffer: %s returned bad count\n",
  65. (unsigned long)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 write callback for regular sysfs files */
  91. static ssize_t sysfs_kf_write(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. if (!count)
  97. return 0;
  98. return ops->store(kobj, of->kn->priv, buf, count);
  99. }
  100. /* kernfs write callback for bin sysfs files */
  101. static ssize_t sysfs_kf_bin_write(struct kernfs_open_file *of, char *buf,
  102. size_t count, loff_t pos)
  103. {
  104. struct bin_attribute *battr = of->kn->priv;
  105. struct kobject *kobj = of->kn->parent->priv;
  106. loff_t size = file_inode(of->file)->i_size;
  107. if (size) {
  108. if (size <= pos)
  109. return 0;
  110. count = min_t(ssize_t, count, size - pos);
  111. }
  112. if (!count)
  113. return 0;
  114. if (!battr->write)
  115. return -EIO;
  116. return battr->write(of->file, kobj, battr, buf, pos, count);
  117. }
  118. static int sysfs_kf_bin_mmap(struct kernfs_open_file *of,
  119. struct vm_area_struct *vma)
  120. {
  121. struct bin_attribute *battr = of->kn->priv;
  122. struct kobject *kobj = of->kn->parent->priv;
  123. return battr->mmap(of->file, kobj, battr, vma);
  124. }
  125. void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr)
  126. {
  127. struct kernfs_node *kn = kobj->sd, *tmp;
  128. if (kn && dir)
  129. kn = kernfs_find_and_get(kn, dir);
  130. else
  131. kernfs_get(kn);
  132. if (kn && attr) {
  133. tmp = kernfs_find_and_get(kn, attr);
  134. kernfs_put(kn);
  135. kn = tmp;
  136. }
  137. if (kn) {
  138. kernfs_notify(kn);
  139. kernfs_put(kn);
  140. }
  141. }
  142. EXPORT_SYMBOL_GPL(sysfs_notify);
  143. static const struct kernfs_ops sysfs_file_kfops_empty = {
  144. };
  145. static const struct kernfs_ops sysfs_file_kfops_ro = {
  146. .seq_show = sysfs_kf_seq_show,
  147. };
  148. static const struct kernfs_ops sysfs_file_kfops_wo = {
  149. .write = sysfs_kf_write,
  150. };
  151. static const struct kernfs_ops sysfs_file_kfops_rw = {
  152. .seq_show = sysfs_kf_seq_show,
  153. .write = sysfs_kf_write,
  154. };
  155. static const struct kernfs_ops sysfs_bin_kfops_ro = {
  156. .read = sysfs_kf_bin_read,
  157. };
  158. static const struct kernfs_ops sysfs_bin_kfops_wo = {
  159. .write = sysfs_kf_bin_write,
  160. };
  161. static const struct kernfs_ops sysfs_bin_kfops_rw = {
  162. .read = sysfs_kf_bin_read,
  163. .write = sysfs_kf_bin_write,
  164. };
  165. static const struct kernfs_ops sysfs_bin_kfops_mmap = {
  166. .read = sysfs_kf_bin_read,
  167. .write = sysfs_kf_bin_write,
  168. .mmap = sysfs_kf_bin_mmap,
  169. };
  170. int sysfs_add_file_mode_ns(struct kernfs_node *parent,
  171. const struct attribute *attr, bool is_bin,
  172. umode_t mode, const void *ns)
  173. {
  174. struct lock_class_key *key = NULL;
  175. const struct kernfs_ops *ops;
  176. struct kernfs_node *kn;
  177. loff_t size;
  178. if (!is_bin) {
  179. struct kobject *kobj = parent->priv;
  180. const struct sysfs_ops *sysfs_ops = kobj->ktype->sysfs_ops;
  181. /* every kobject with an attribute needs a ktype assigned */
  182. if (WARN(!sysfs_ops, KERN_ERR
  183. "missing sysfs attribute operations for kobject: %s\n",
  184. kobject_name(kobj)))
  185. return -EINVAL;
  186. if (sysfs_ops->show && sysfs_ops->store)
  187. ops = &sysfs_file_kfops_rw;
  188. else if (sysfs_ops->show)
  189. ops = &sysfs_file_kfops_ro;
  190. else if (sysfs_ops->store)
  191. ops = &sysfs_file_kfops_wo;
  192. else
  193. ops = &sysfs_file_kfops_empty;
  194. size = PAGE_SIZE;
  195. } else {
  196. struct bin_attribute *battr = (void *)attr;
  197. if (battr->mmap)
  198. ops = &sysfs_bin_kfops_mmap;
  199. else if (battr->read && battr->write)
  200. ops = &sysfs_bin_kfops_rw;
  201. else if (battr->read)
  202. ops = &sysfs_bin_kfops_ro;
  203. else if (battr->write)
  204. ops = &sysfs_bin_kfops_wo;
  205. else
  206. ops = &sysfs_file_kfops_empty;
  207. size = battr->size;
  208. }
  209. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  210. if (!attr->ignore_lockdep)
  211. key = attr->key ?: (struct lock_class_key *)&attr->skey;
  212. #endif
  213. kn = __kernfs_create_file(parent, attr->name, mode, size, ops,
  214. (void *)attr, ns, true, key);
  215. if (IS_ERR(kn)) {
  216. if (PTR_ERR(kn) == -EEXIST)
  217. sysfs_warn_dup(parent, attr->name);
  218. return PTR_ERR(kn);
  219. }
  220. return 0;
  221. }
  222. int sysfs_add_file(struct kernfs_node *parent, const struct attribute *attr,
  223. bool is_bin)
  224. {
  225. return sysfs_add_file_mode_ns(parent, attr, is_bin, attr->mode, NULL);
  226. }
  227. /**
  228. * sysfs_create_file_ns - create an attribute file for an object with custom ns
  229. * @kobj: object we're creating for
  230. * @attr: attribute descriptor
  231. * @ns: namespace the new file should belong to
  232. */
  233. int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
  234. const void *ns)
  235. {
  236. BUG_ON(!kobj || !kobj->sd || !attr);
  237. return sysfs_add_file_mode_ns(kobj->sd, attr, false, attr->mode, ns);
  238. }
  239. EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
  240. int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
  241. {
  242. int err = 0;
  243. int i;
  244. for (i = 0; ptr[i] && !err; i++)
  245. err = sysfs_create_file(kobj, ptr[i]);
  246. if (err)
  247. while (--i >= 0)
  248. sysfs_remove_file(kobj, ptr[i]);
  249. return err;
  250. }
  251. EXPORT_SYMBOL_GPL(sysfs_create_files);
  252. /**
  253. * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
  254. * @kobj: object we're acting for.
  255. * @attr: attribute descriptor.
  256. * @group: group name.
  257. */
  258. int sysfs_add_file_to_group(struct kobject *kobj,
  259. const struct attribute *attr, const char *group)
  260. {
  261. struct kernfs_node *parent;
  262. int error;
  263. if (group) {
  264. parent = kernfs_find_and_get(kobj->sd, group);
  265. } else {
  266. parent = kobj->sd;
  267. kernfs_get(parent);
  268. }
  269. if (!parent)
  270. return -ENOENT;
  271. error = sysfs_add_file(parent, attr, false);
  272. kernfs_put(parent);
  273. return error;
  274. }
  275. EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
  276. /**
  277. * sysfs_chmod_file - update the modified mode value on an object attribute.
  278. * @kobj: object we're acting for.
  279. * @attr: attribute descriptor.
  280. * @mode: file permissions.
  281. *
  282. */
  283. int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
  284. umode_t mode)
  285. {
  286. struct kernfs_node *kn;
  287. struct iattr newattrs;
  288. int rc;
  289. kn = kernfs_find_and_get(kobj->sd, attr->name);
  290. if (!kn)
  291. return -ENOENT;
  292. newattrs.ia_mode = (mode & S_IALLUGO) | (kn->mode & ~S_IALLUGO);
  293. newattrs.ia_valid = ATTR_MODE;
  294. rc = kernfs_setattr(kn, &newattrs);
  295. kernfs_put(kn);
  296. return rc;
  297. }
  298. EXPORT_SYMBOL_GPL(sysfs_chmod_file);
  299. /**
  300. * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
  301. * @kobj: object we're acting for
  302. * @attr: attribute descriptor
  303. * @ns: namespace tag of the file to remove
  304. *
  305. * Hash the attribute name and namespace tag and kill the victim.
  306. */
  307. void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
  308. const void *ns)
  309. {
  310. struct kernfs_node *parent = kobj->sd;
  311. kernfs_remove_by_name_ns(parent, attr->name, ns);
  312. }
  313. EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
  314. void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
  315. {
  316. int i;
  317. for (i = 0; ptr[i]; i++)
  318. sysfs_remove_file(kobj, ptr[i]);
  319. }
  320. EXPORT_SYMBOL_GPL(sysfs_remove_files);
  321. /**
  322. * sysfs_remove_file_from_group - remove an attribute file from a group.
  323. * @kobj: object we're acting for.
  324. * @attr: attribute descriptor.
  325. * @group: group name.
  326. */
  327. void sysfs_remove_file_from_group(struct kobject *kobj,
  328. const struct attribute *attr, const char *group)
  329. {
  330. struct kernfs_node *parent;
  331. if (group) {
  332. parent = kernfs_find_and_get(kobj->sd, group);
  333. } else {
  334. parent = kobj->sd;
  335. kernfs_get(parent);
  336. }
  337. if (parent) {
  338. kernfs_remove_by_name(parent, attr->name);
  339. kernfs_put(parent);
  340. }
  341. }
  342. EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
  343. /**
  344. * sysfs_create_bin_file - create binary file for object.
  345. * @kobj: object.
  346. * @attr: attribute descriptor.
  347. */
  348. int sysfs_create_bin_file(struct kobject *kobj,
  349. const struct bin_attribute *attr)
  350. {
  351. BUG_ON(!kobj || !kobj->sd || !attr);
  352. return sysfs_add_file(kobj->sd, &attr->attr, true);
  353. }
  354. EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
  355. /**
  356. * sysfs_remove_bin_file - remove binary file for object.
  357. * @kobj: object.
  358. * @attr: attribute descriptor.
  359. */
  360. void sysfs_remove_bin_file(struct kobject *kobj,
  361. const struct bin_attribute *attr)
  362. {
  363. kernfs_remove_by_name(kobj->sd, attr->attr.name);
  364. }
  365. EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
  366. struct sysfs_schedule_callback_struct {
  367. struct list_head workq_list;
  368. struct kobject *kobj;
  369. void (*func)(void *);
  370. void *data;
  371. struct module *owner;
  372. struct work_struct work;
  373. };
  374. static struct workqueue_struct *sysfs_workqueue;
  375. static DEFINE_MUTEX(sysfs_workq_mutex);
  376. static LIST_HEAD(sysfs_workq);
  377. static void sysfs_schedule_callback_work(struct work_struct *work)
  378. {
  379. struct sysfs_schedule_callback_struct *ss = container_of(work,
  380. struct sysfs_schedule_callback_struct, work);
  381. (ss->func)(ss->data);
  382. kobject_put(ss->kobj);
  383. module_put(ss->owner);
  384. mutex_lock(&sysfs_workq_mutex);
  385. list_del(&ss->workq_list);
  386. mutex_unlock(&sysfs_workq_mutex);
  387. kfree(ss);
  388. }
  389. /**
  390. * sysfs_schedule_callback - helper to schedule a callback for a kobject
  391. * @kobj: object we're acting for.
  392. * @func: callback function to invoke later.
  393. * @data: argument to pass to @func.
  394. * @owner: module owning the callback code
  395. *
  396. * sysfs attribute methods must not unregister themselves or their parent
  397. * kobject (which would amount to the same thing). Attempts to do so will
  398. * deadlock, since unregistration is mutually exclusive with driver
  399. * callbacks.
  400. *
  401. * Instead methods can call this routine, which will attempt to allocate
  402. * and schedule a workqueue request to call back @func with @data as its
  403. * argument in the workqueue's process context. @kobj will be pinned
  404. * until @func returns.
  405. *
  406. * Returns 0 if the request was submitted, -ENOMEM if storage could not
  407. * be allocated, -ENODEV if a reference to @owner isn't available,
  408. * -EAGAIN if a callback has already been scheduled for @kobj.
  409. */
  410. int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
  411. void *data, struct module *owner)
  412. {
  413. struct sysfs_schedule_callback_struct *ss, *tmp;
  414. if (!try_module_get(owner))
  415. return -ENODEV;
  416. mutex_lock(&sysfs_workq_mutex);
  417. list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
  418. if (ss->kobj == kobj) {
  419. module_put(owner);
  420. mutex_unlock(&sysfs_workq_mutex);
  421. return -EAGAIN;
  422. }
  423. mutex_unlock(&sysfs_workq_mutex);
  424. if (sysfs_workqueue == NULL) {
  425. sysfs_workqueue = create_singlethread_workqueue("sysfsd");
  426. if (sysfs_workqueue == NULL) {
  427. module_put(owner);
  428. return -ENOMEM;
  429. }
  430. }
  431. ss = kmalloc(sizeof(*ss), GFP_KERNEL);
  432. if (!ss) {
  433. module_put(owner);
  434. return -ENOMEM;
  435. }
  436. kobject_get(kobj);
  437. ss->kobj = kobj;
  438. ss->func = func;
  439. ss->data = data;
  440. ss->owner = owner;
  441. INIT_WORK(&ss->work, sysfs_schedule_callback_work);
  442. INIT_LIST_HEAD(&ss->workq_list);
  443. mutex_lock(&sysfs_workq_mutex);
  444. list_add_tail(&ss->workq_list, &sysfs_workq);
  445. mutex_unlock(&sysfs_workq_mutex);
  446. queue_work(sysfs_workqueue, &ss->work);
  447. return 0;
  448. }
  449. EXPORT_SYMBOL_GPL(sysfs_schedule_callback);