file.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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/fsnotify.h>
  17. #include <linux/namei.h>
  18. #include <linux/poll.h>
  19. #include <linux/list.h>
  20. #include <linux/mutex.h>
  21. #include <linux/limits.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/seq_file.h>
  24. #include "sysfs.h"
  25. /*
  26. * There's one sysfs_open_file for each open file and one sysfs_open_dirent
  27. * for each sysfs_dirent with one or more open files.
  28. *
  29. * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open is
  30. * protected by sysfs_open_dirent_lock.
  31. *
  32. * filp->private_data points to seq_file whose ->private points to
  33. * sysfs_open_file. sysfs_open_files are chained at
  34. * sysfs_open_dirent->files, which is protected by sysfs_open_file_mutex.
  35. */
  36. static DEFINE_SPINLOCK(sysfs_open_dirent_lock);
  37. static DEFINE_MUTEX(sysfs_open_file_mutex);
  38. struct sysfs_open_dirent {
  39. atomic_t refcnt;
  40. atomic_t event;
  41. wait_queue_head_t poll;
  42. struct list_head files; /* goes through sysfs_open_file.list */
  43. };
  44. struct sysfs_open_file {
  45. struct sysfs_dirent *sd;
  46. struct file *file;
  47. struct mutex mutex;
  48. int event;
  49. struct list_head list;
  50. };
  51. static struct sysfs_open_file *sysfs_of(struct file *file)
  52. {
  53. return ((struct seq_file *)file->private_data)->private;
  54. }
  55. /*
  56. * Determine ktype->sysfs_ops for the given sysfs_dirent. This function
  57. * must be called while holding an active reference.
  58. */
  59. static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd)
  60. {
  61. struct kobject *kobj = sd->s_parent->s_dir.kobj;
  62. lockdep_assert_held(sd);
  63. return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
  64. }
  65. /*
  66. * Reads on sysfs are handled through seq_file, which takes care of hairy
  67. * details like buffering and seeking. The following function pipes
  68. * sysfs_ops->show() result through seq_file.
  69. */
  70. static int sysfs_seq_show(struct seq_file *sf, void *v)
  71. {
  72. struct sysfs_open_file *of = sf->private;
  73. struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
  74. const struct sysfs_ops *ops;
  75. char *buf;
  76. ssize_t count;
  77. /* acquire buffer and ensure that it's >= PAGE_SIZE */
  78. count = seq_get_buf(sf, &buf);
  79. if (count < PAGE_SIZE) {
  80. seq_commit(sf, -1);
  81. return 0;
  82. }
  83. /*
  84. * Need @of->sd for attr and ops, its parent for kobj. @of->mutex
  85. * nests outside active ref and is just to ensure that the ops
  86. * aren't called concurrently for the same open file.
  87. */
  88. mutex_lock(&of->mutex);
  89. if (!sysfs_get_active(of->sd)) {
  90. mutex_unlock(&of->mutex);
  91. return -ENODEV;
  92. }
  93. of->event = atomic_read(&of->sd->s_attr.open->event);
  94. /*
  95. * Lookup @ops and invoke show(). Control may reach here via seq
  96. * file lseek even if @ops->show() isn't implemented.
  97. */
  98. ops = sysfs_file_ops(of->sd);
  99. if (ops->show)
  100. count = ops->show(kobj, of->sd->s_attr.attr, buf);
  101. else
  102. count = 0;
  103. sysfs_put_active(of->sd);
  104. mutex_unlock(&of->mutex);
  105. if (count < 0)
  106. return count;
  107. /*
  108. * The code works fine with PAGE_SIZE return but it's likely to
  109. * indicate truncated result or overflow in normal use cases.
  110. */
  111. if (count >= (ssize_t)PAGE_SIZE) {
  112. print_symbol("fill_read_buffer: %s returned bad count\n",
  113. (unsigned long)ops->show);
  114. /* Try to struggle along */
  115. count = PAGE_SIZE - 1;
  116. }
  117. seq_commit(sf, count);
  118. return 0;
  119. }
  120. /**
  121. * flush_write_buffer - push buffer to kobject
  122. * @of: open file
  123. * @buf: data buffer for file
  124. * @count: number of bytes
  125. *
  126. * Get the correct pointers for the kobject and the attribute we're dealing
  127. * with, then call the store() method for it with @buf.
  128. */
  129. static int flush_write_buffer(struct sysfs_open_file *of, char *buf,
  130. size_t count)
  131. {
  132. struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
  133. const struct sysfs_ops *ops;
  134. int rc = 0;
  135. /*
  136. * Need @of->sd for attr and ops, its parent for kobj. @of->mutex
  137. * nests outside active ref and is just to ensure that the ops
  138. * aren't called concurrently for the same open file.
  139. */
  140. mutex_lock(&of->mutex);
  141. if (!sysfs_get_active(of->sd)) {
  142. mutex_unlock(&of->mutex);
  143. return -ENODEV;
  144. }
  145. ops = sysfs_file_ops(of->sd);
  146. rc = ops->store(kobj, of->sd->s_attr.attr, buf, count);
  147. sysfs_put_active(of->sd);
  148. mutex_unlock(&of->mutex);
  149. return rc;
  150. }
  151. /**
  152. * sysfs_write_file - write an attribute
  153. * @file: file pointer
  154. * @user_buf: data to write
  155. * @count: number of bytes
  156. * @ppos: starting offset
  157. *
  158. * Copy data in from userland and pass it to the matching
  159. * sysfs_ops->store() by invoking flush_write_buffer().
  160. *
  161. * There is no easy way for us to know if userspace is only doing a partial
  162. * write, so we don't support them. We expect the entire buffer to come on
  163. * the first write. Hint: if you're writing a value, first read the file,
  164. * modify only the the value you're changing, then write entire buffer
  165. * back.
  166. */
  167. static ssize_t sysfs_write_file(struct file *file, const char __user *user_buf,
  168. size_t count, loff_t *ppos)
  169. {
  170. struct sysfs_open_file *of = sysfs_of(file);
  171. ssize_t len = min_t(size_t, count, PAGE_SIZE - 1);
  172. char *buf;
  173. if (!len)
  174. return 0;
  175. buf = kmalloc(len + 1, GFP_KERNEL);
  176. if (!buf)
  177. return -ENOMEM;
  178. if (copy_from_user(buf, user_buf, len)) {
  179. len = -EFAULT;
  180. goto out_free;
  181. }
  182. buf[len] = '\0'; /* guarantee string termination */
  183. len = flush_write_buffer(of, buf, len);
  184. if (len > 0)
  185. *ppos += len;
  186. out_free:
  187. kfree(buf);
  188. return len;
  189. }
  190. /**
  191. * sysfs_get_open_dirent - get or create sysfs_open_dirent
  192. * @sd: target sysfs_dirent
  193. * @of: sysfs_open_file for this instance of open
  194. *
  195. * If @sd->s_attr.open exists, increment its reference count;
  196. * otherwise, create one. @of is chained to the files list.
  197. *
  198. * LOCKING:
  199. * Kernel thread context (may sleep).
  200. *
  201. * RETURNS:
  202. * 0 on success, -errno on failure.
  203. */
  204. static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
  205. struct sysfs_open_file *of)
  206. {
  207. struct sysfs_open_dirent *od, *new_od = NULL;
  208. retry:
  209. mutex_lock(&sysfs_open_file_mutex);
  210. spin_lock_irq(&sysfs_open_dirent_lock);
  211. if (!sd->s_attr.open && new_od) {
  212. sd->s_attr.open = new_od;
  213. new_od = NULL;
  214. }
  215. od = sd->s_attr.open;
  216. if (od) {
  217. atomic_inc(&od->refcnt);
  218. list_add_tail(&of->list, &od->files);
  219. }
  220. spin_unlock_irq(&sysfs_open_dirent_lock);
  221. mutex_unlock(&sysfs_open_file_mutex);
  222. if (od) {
  223. kfree(new_od);
  224. return 0;
  225. }
  226. /* not there, initialize a new one and retry */
  227. new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
  228. if (!new_od)
  229. return -ENOMEM;
  230. atomic_set(&new_od->refcnt, 0);
  231. atomic_set(&new_od->event, 1);
  232. init_waitqueue_head(&new_od->poll);
  233. INIT_LIST_HEAD(&new_od->files);
  234. goto retry;
  235. }
  236. /**
  237. * sysfs_put_open_dirent - put sysfs_open_dirent
  238. * @sd: target sysfs_dirent
  239. * @of: associated sysfs_open_file
  240. *
  241. * Put @sd->s_attr.open and unlink @of from the files list. If
  242. * reference count reaches zero, disassociate and free it.
  243. *
  244. * LOCKING:
  245. * None.
  246. */
  247. static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
  248. struct sysfs_open_file *of)
  249. {
  250. struct sysfs_open_dirent *od = sd->s_attr.open;
  251. unsigned long flags;
  252. mutex_lock(&sysfs_open_file_mutex);
  253. spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
  254. list_del(&of->list);
  255. if (atomic_dec_and_test(&od->refcnt))
  256. sd->s_attr.open = NULL;
  257. else
  258. od = NULL;
  259. spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
  260. mutex_unlock(&sysfs_open_file_mutex);
  261. kfree(od);
  262. }
  263. static int sysfs_open_file(struct inode *inode, struct file *file)
  264. {
  265. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  266. struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
  267. struct sysfs_open_file *of;
  268. const struct sysfs_ops *ops;
  269. int error = -EACCES;
  270. /* need attr_sd for attr and ops, its parent for kobj */
  271. if (!sysfs_get_active(attr_sd))
  272. return -ENODEV;
  273. /* every kobject with an attribute needs a ktype assigned */
  274. ops = sysfs_file_ops(attr_sd);
  275. if (WARN(!ops, KERN_ERR
  276. "missing sysfs attribute operations for kobject: %s\n",
  277. kobject_name(kobj)))
  278. goto err_out;
  279. /* File needs write support.
  280. * The inode's perms must say it's ok,
  281. * and we must have a store method.
  282. */
  283. if (file->f_mode & FMODE_WRITE) {
  284. if (!(inode->i_mode & S_IWUGO) || !ops->store)
  285. goto err_out;
  286. }
  287. /* File needs read support.
  288. * The inode's perms must say it's ok, and we there
  289. * must be a show method for it.
  290. */
  291. if (file->f_mode & FMODE_READ) {
  292. if (!(inode->i_mode & S_IRUGO) || !ops->show)
  293. goto err_out;
  294. }
  295. /* allocate a sysfs_open_file for the file */
  296. error = -ENOMEM;
  297. of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL);
  298. if (!of)
  299. goto err_out;
  300. mutex_init(&of->mutex);
  301. of->sd = attr_sd;
  302. of->file = file;
  303. /*
  304. * Always instantiate seq_file even if read access is not
  305. * implemented or requested. This unifies private data access and
  306. * most files are readable anyway.
  307. */
  308. error = single_open(file, sysfs_seq_show, of);
  309. if (error)
  310. goto err_free;
  311. /* seq_file clears PWRITE unconditionally, restore it if WRITE */
  312. if (file->f_mode & FMODE_WRITE)
  313. file->f_mode |= FMODE_PWRITE;
  314. /* make sure we have open dirent struct */
  315. error = sysfs_get_open_dirent(attr_sd, of);
  316. if (error)
  317. goto err_close;
  318. /* open succeeded, put active references */
  319. sysfs_put_active(attr_sd);
  320. return 0;
  321. err_close:
  322. single_release(inode, file);
  323. err_free:
  324. kfree(of);
  325. err_out:
  326. sysfs_put_active(attr_sd);
  327. return error;
  328. }
  329. static int sysfs_release(struct inode *inode, struct file *filp)
  330. {
  331. struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
  332. struct sysfs_open_file *of = sysfs_of(filp);
  333. sysfs_put_open_dirent(sd, of);
  334. single_release(inode, filp);
  335. kfree(of);
  336. return 0;
  337. }
  338. /* Sysfs attribute files are pollable. The idea is that you read
  339. * the content and then you use 'poll' or 'select' to wait for
  340. * the content to change. When the content changes (assuming the
  341. * manager for the kobject supports notification), poll will
  342. * return POLLERR|POLLPRI, and select will return the fd whether
  343. * it is waiting for read, write, or exceptions.
  344. * Once poll/select indicates that the value has changed, you
  345. * need to close and re-open the file, or seek to 0 and read again.
  346. * Reminder: this only works for attributes which actively support
  347. * it, and it is not possible to test an attribute from userspace
  348. * to see if it supports poll (Neither 'poll' nor 'select' return
  349. * an appropriate error code). When in doubt, set a suitable timeout value.
  350. */
  351. static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
  352. {
  353. struct sysfs_open_file *of = sysfs_of(filp);
  354. struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
  355. struct sysfs_open_dirent *od = attr_sd->s_attr.open;
  356. /* need parent for the kobj, grab both */
  357. if (!sysfs_get_active(attr_sd))
  358. goto trigger;
  359. poll_wait(filp, &od->poll, wait);
  360. sysfs_put_active(attr_sd);
  361. if (of->event != atomic_read(&od->event))
  362. goto trigger;
  363. return DEFAULT_POLLMASK;
  364. trigger:
  365. return DEFAULT_POLLMASK|POLLERR|POLLPRI;
  366. }
  367. void sysfs_notify_dirent(struct sysfs_dirent *sd)
  368. {
  369. struct sysfs_open_dirent *od;
  370. unsigned long flags;
  371. spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
  372. if (!WARN_ON(sysfs_type(sd) != SYSFS_KOBJ_ATTR)) {
  373. od = sd->s_attr.open;
  374. if (od) {
  375. atomic_inc(&od->event);
  376. wake_up_interruptible(&od->poll);
  377. }
  378. }
  379. spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
  380. }
  381. EXPORT_SYMBOL_GPL(sysfs_notify_dirent);
  382. void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
  383. {
  384. struct sysfs_dirent *sd = k->sd;
  385. mutex_lock(&sysfs_mutex);
  386. if (sd && dir)
  387. sd = sysfs_find_dirent(sd, dir, NULL);
  388. if (sd && attr)
  389. sd = sysfs_find_dirent(sd, attr, NULL);
  390. if (sd)
  391. sysfs_notify_dirent(sd);
  392. mutex_unlock(&sysfs_mutex);
  393. }
  394. EXPORT_SYMBOL_GPL(sysfs_notify);
  395. const struct file_operations sysfs_file_operations = {
  396. .read = seq_read,
  397. .write = sysfs_write_file,
  398. .llseek = seq_lseek,
  399. .open = sysfs_open_file,
  400. .release = sysfs_release,
  401. .poll = sysfs_poll,
  402. };
  403. int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
  404. const struct attribute *attr, int type,
  405. umode_t amode, const void *ns)
  406. {
  407. umode_t mode = (amode & S_IALLUGO) | S_IFREG;
  408. struct sysfs_addrm_cxt acxt;
  409. struct sysfs_dirent *sd;
  410. int rc;
  411. sd = sysfs_new_dirent(attr->name, mode, type);
  412. if (!sd)
  413. return -ENOMEM;
  414. sd->s_ns = ns;
  415. sd->s_attr.attr = (void *)attr;
  416. sysfs_dirent_init_lockdep(sd);
  417. sysfs_addrm_start(&acxt);
  418. rc = sysfs_add_one(&acxt, sd, dir_sd);
  419. sysfs_addrm_finish(&acxt);
  420. if (rc)
  421. sysfs_put(sd);
  422. return rc;
  423. }
  424. int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
  425. int type)
  426. {
  427. return sysfs_add_file_mode_ns(dir_sd, attr, type, attr->mode, NULL);
  428. }
  429. /**
  430. * sysfs_create_file_ns - create an attribute file for an object with custom ns
  431. * @kobj: object we're creating for
  432. * @attr: attribute descriptor
  433. * @ns: namespace the new file should belong to
  434. */
  435. int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
  436. const void *ns)
  437. {
  438. BUG_ON(!kobj || !kobj->sd || !attr);
  439. return sysfs_add_file_mode_ns(kobj->sd, attr, SYSFS_KOBJ_ATTR,
  440. attr->mode, ns);
  441. }
  442. EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
  443. int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
  444. {
  445. int err = 0;
  446. int i;
  447. for (i = 0; ptr[i] && !err; i++)
  448. err = sysfs_create_file(kobj, ptr[i]);
  449. if (err)
  450. while (--i >= 0)
  451. sysfs_remove_file(kobj, ptr[i]);
  452. return err;
  453. }
  454. EXPORT_SYMBOL_GPL(sysfs_create_files);
  455. /**
  456. * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
  457. * @kobj: object we're acting for.
  458. * @attr: attribute descriptor.
  459. * @group: group name.
  460. */
  461. int sysfs_add_file_to_group(struct kobject *kobj,
  462. const struct attribute *attr, const char *group)
  463. {
  464. struct sysfs_dirent *dir_sd;
  465. int error;
  466. if (group)
  467. dir_sd = sysfs_get_dirent(kobj->sd, group);
  468. else
  469. dir_sd = sysfs_get(kobj->sd);
  470. if (!dir_sd)
  471. return -ENOENT;
  472. error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR);
  473. sysfs_put(dir_sd);
  474. return error;
  475. }
  476. EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
  477. /**
  478. * sysfs_chmod_file - update the modified mode value on an object attribute.
  479. * @kobj: object we're acting for.
  480. * @attr: attribute descriptor.
  481. * @mode: file permissions.
  482. *
  483. */
  484. int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
  485. umode_t mode)
  486. {
  487. struct sysfs_dirent *sd;
  488. struct iattr newattrs;
  489. int rc;
  490. mutex_lock(&sysfs_mutex);
  491. rc = -ENOENT;
  492. sd = sysfs_find_dirent(kobj->sd, attr->name, NULL);
  493. if (!sd)
  494. goto out;
  495. newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO);
  496. newattrs.ia_valid = ATTR_MODE;
  497. rc = sysfs_sd_setattr(sd, &newattrs);
  498. out:
  499. mutex_unlock(&sysfs_mutex);
  500. return rc;
  501. }
  502. EXPORT_SYMBOL_GPL(sysfs_chmod_file);
  503. /**
  504. * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
  505. * @kobj: object we're acting for
  506. * @attr: attribute descriptor
  507. * @ns: namespace tag of the file to remove
  508. *
  509. * Hash the attribute name and namespace tag and kill the victim.
  510. */
  511. void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
  512. const void *ns)
  513. {
  514. struct sysfs_dirent *dir_sd = kobj->sd;
  515. sysfs_hash_and_remove(dir_sd, attr->name, ns);
  516. }
  517. EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
  518. void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
  519. {
  520. int i;
  521. for (i = 0; ptr[i]; i++)
  522. sysfs_remove_file(kobj, ptr[i]);
  523. }
  524. EXPORT_SYMBOL_GPL(sysfs_remove_files);
  525. /**
  526. * sysfs_remove_file_from_group - remove an attribute file from a group.
  527. * @kobj: object we're acting for.
  528. * @attr: attribute descriptor.
  529. * @group: group name.
  530. */
  531. void sysfs_remove_file_from_group(struct kobject *kobj,
  532. const struct attribute *attr, const char *group)
  533. {
  534. struct sysfs_dirent *dir_sd;
  535. if (group)
  536. dir_sd = sysfs_get_dirent(kobj->sd, group);
  537. else
  538. dir_sd = sysfs_get(kobj->sd);
  539. if (dir_sd) {
  540. sysfs_hash_and_remove(dir_sd, attr->name, NULL);
  541. sysfs_put(dir_sd);
  542. }
  543. }
  544. EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
  545. struct sysfs_schedule_callback_struct {
  546. struct list_head workq_list;
  547. struct kobject *kobj;
  548. void (*func)(void *);
  549. void *data;
  550. struct module *owner;
  551. struct work_struct work;
  552. };
  553. static struct workqueue_struct *sysfs_workqueue;
  554. static DEFINE_MUTEX(sysfs_workq_mutex);
  555. static LIST_HEAD(sysfs_workq);
  556. static void sysfs_schedule_callback_work(struct work_struct *work)
  557. {
  558. struct sysfs_schedule_callback_struct *ss = container_of(work,
  559. struct sysfs_schedule_callback_struct, work);
  560. (ss->func)(ss->data);
  561. kobject_put(ss->kobj);
  562. module_put(ss->owner);
  563. mutex_lock(&sysfs_workq_mutex);
  564. list_del(&ss->workq_list);
  565. mutex_unlock(&sysfs_workq_mutex);
  566. kfree(ss);
  567. }
  568. /**
  569. * sysfs_schedule_callback - helper to schedule a callback for a kobject
  570. * @kobj: object we're acting for.
  571. * @func: callback function to invoke later.
  572. * @data: argument to pass to @func.
  573. * @owner: module owning the callback code
  574. *
  575. * sysfs attribute methods must not unregister themselves or their parent
  576. * kobject (which would amount to the same thing). Attempts to do so will
  577. * deadlock, since unregistration is mutually exclusive with driver
  578. * callbacks.
  579. *
  580. * Instead methods can call this routine, which will attempt to allocate
  581. * and schedule a workqueue request to call back @func with @data as its
  582. * argument in the workqueue's process context. @kobj will be pinned
  583. * until @func returns.
  584. *
  585. * Returns 0 if the request was submitted, -ENOMEM if storage could not
  586. * be allocated, -ENODEV if a reference to @owner isn't available,
  587. * -EAGAIN if a callback has already been scheduled for @kobj.
  588. */
  589. int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
  590. void *data, struct module *owner)
  591. {
  592. struct sysfs_schedule_callback_struct *ss, *tmp;
  593. if (!try_module_get(owner))
  594. return -ENODEV;
  595. mutex_lock(&sysfs_workq_mutex);
  596. list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
  597. if (ss->kobj == kobj) {
  598. module_put(owner);
  599. mutex_unlock(&sysfs_workq_mutex);
  600. return -EAGAIN;
  601. }
  602. mutex_unlock(&sysfs_workq_mutex);
  603. if (sysfs_workqueue == NULL) {
  604. sysfs_workqueue = create_singlethread_workqueue("sysfsd");
  605. if (sysfs_workqueue == NULL) {
  606. module_put(owner);
  607. return -ENOMEM;
  608. }
  609. }
  610. ss = kmalloc(sizeof(*ss), GFP_KERNEL);
  611. if (!ss) {
  612. module_put(owner);
  613. return -ENOMEM;
  614. }
  615. kobject_get(kobj);
  616. ss->kobj = kobj;
  617. ss->func = func;
  618. ss->data = data;
  619. ss->owner = owner;
  620. INIT_WORK(&ss->work, sysfs_schedule_callback_work);
  621. INIT_LIST_HEAD(&ss->workq_list);
  622. mutex_lock(&sysfs_workq_mutex);
  623. list_add_tail(&ss->workq_list, &sysfs_workq);
  624. mutex_unlock(&sysfs_workq_mutex);
  625. queue_work(sysfs_workqueue, &ss->work);
  626. return 0;
  627. }
  628. EXPORT_SYMBOL_GPL(sysfs_schedule_callback);