file.c 18 KB

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