file.c 18 KB

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