file.c 17 KB

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