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/namei.h>
  16. #include <linux/poll.h>
  17. #include <linux/list.h>
  18. #include <linux/mutex.h>
  19. #include <asm/uaccess.h>
  20. #include "sysfs.h"
  21. /*
  22. * There's one sysfs_buffer for each open file and one
  23. * sysfs_open_dirent for each sysfs_dirent with one or more open
  24. * files.
  25. *
  26. * filp->private_data points to sysfs_buffer and
  27. * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open
  28. * is protected by sysfs_open_dirent_lock.
  29. */
  30. static DEFINE_SPINLOCK(sysfs_open_dirent_lock);
  31. struct sysfs_open_dirent {
  32. atomic_t refcnt;
  33. atomic_t event;
  34. wait_queue_head_t poll;
  35. struct list_head buffers; /* goes through sysfs_buffer.list */
  36. };
  37. struct sysfs_buffer {
  38. size_t count;
  39. loff_t pos;
  40. char * page;
  41. struct sysfs_ops * ops;
  42. struct mutex mutex;
  43. int needs_read_fill;
  44. int event;
  45. struct list_head list;
  46. };
  47. /**
  48. * fill_read_buffer - allocate and fill buffer from object.
  49. * @dentry: dentry pointer.
  50. * @buffer: data buffer for file.
  51. *
  52. * Allocate @buffer->page, if it hasn't been already, then call the
  53. * kobject's show() method to fill the buffer with this attribute's
  54. * data.
  55. * This is called only once, on the file's first read unless an error
  56. * is returned.
  57. */
  58. static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer)
  59. {
  60. struct sysfs_dirent *attr_sd = dentry->d_fsdata;
  61. struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
  62. struct sysfs_ops * ops = buffer->ops;
  63. int ret = 0;
  64. ssize_t count;
  65. if (!buffer->page)
  66. buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
  67. if (!buffer->page)
  68. return -ENOMEM;
  69. /* need attr_sd for attr and ops, its parent for kobj */
  70. if (!sysfs_get_active_two(attr_sd))
  71. return -ENODEV;
  72. buffer->event = atomic_read(&attr_sd->s_attr.open->event);
  73. count = ops->show(kobj, attr_sd->s_attr.attr, buffer->page);
  74. sysfs_put_active_two(attr_sd);
  75. /*
  76. * The code works fine with PAGE_SIZE return but it's likely to
  77. * indicate truncated result or overflow in normal use cases.
  78. */
  79. if (count >= (ssize_t)PAGE_SIZE) {
  80. print_symbol("fill_read_buffer: %s returned bad count\n",
  81. (unsigned long)ops->show);
  82. /* Try to struggle along */
  83. count = PAGE_SIZE - 1;
  84. }
  85. if (count >= 0) {
  86. buffer->needs_read_fill = 0;
  87. buffer->count = count;
  88. } else {
  89. ret = count;
  90. }
  91. return ret;
  92. }
  93. /**
  94. * sysfs_read_file - read an attribute.
  95. * @file: file pointer.
  96. * @buf: buffer to fill.
  97. * @count: number of bytes to read.
  98. * @ppos: starting offset in file.
  99. *
  100. * Userspace wants to read an attribute file. The attribute descriptor
  101. * is in the file's ->d_fsdata. The target object is in the directory's
  102. * ->d_fsdata.
  103. *
  104. * We call fill_read_buffer() to allocate and fill the buffer from the
  105. * object's show() method exactly once (if the read is happening from
  106. * the beginning of the file). That should fill the entire buffer with
  107. * all the data the object has to offer for that attribute.
  108. * We then call flush_read_buffer() to copy the buffer to userspace
  109. * in the increments specified.
  110. */
  111. static ssize_t
  112. sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  113. {
  114. struct sysfs_buffer * buffer = file->private_data;
  115. ssize_t retval = 0;
  116. mutex_lock(&buffer->mutex);
  117. if (buffer->needs_read_fill) {
  118. retval = fill_read_buffer(file->f_path.dentry,buffer);
  119. if (retval)
  120. goto out;
  121. }
  122. pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
  123. __FUNCTION__, count, *ppos, buffer->page);
  124. retval = simple_read_from_buffer(buf, count, ppos, buffer->page,
  125. buffer->count);
  126. out:
  127. mutex_unlock(&buffer->mutex);
  128. return retval;
  129. }
  130. /**
  131. * fill_write_buffer - copy buffer from userspace.
  132. * @buffer: data buffer for file.
  133. * @buf: data from user.
  134. * @count: number of bytes in @userbuf.
  135. *
  136. * Allocate @buffer->page if it hasn't been already, then
  137. * copy the user-supplied buffer into it.
  138. */
  139. static int
  140. fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count)
  141. {
  142. int error;
  143. if (!buffer->page)
  144. buffer->page = (char *)get_zeroed_page(GFP_KERNEL);
  145. if (!buffer->page)
  146. return -ENOMEM;
  147. if (count >= PAGE_SIZE)
  148. count = PAGE_SIZE - 1;
  149. error = copy_from_user(buffer->page,buf,count);
  150. buffer->needs_read_fill = 1;
  151. /* if buf is assumed to contain a string, terminate it by \0,
  152. so e.g. sscanf() can scan the string easily */
  153. buffer->page[count] = 0;
  154. return error ? -EFAULT : count;
  155. }
  156. /**
  157. * flush_write_buffer - push buffer to kobject.
  158. * @dentry: dentry to the attribute
  159. * @buffer: data buffer for file.
  160. * @count: number of bytes
  161. *
  162. * Get the correct pointers for the kobject and the attribute we're
  163. * dealing with, then call the store() method for the attribute,
  164. * passing the buffer that we acquired in fill_write_buffer().
  165. */
  166. static int
  167. flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count)
  168. {
  169. struct sysfs_dirent *attr_sd = dentry->d_fsdata;
  170. struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
  171. struct sysfs_ops * ops = buffer->ops;
  172. int rc;
  173. /* need attr_sd for attr and ops, its parent for kobj */
  174. if (!sysfs_get_active_two(attr_sd))
  175. return -ENODEV;
  176. rc = ops->store(kobj, attr_sd->s_attr.attr, buffer->page, count);
  177. sysfs_put_active_two(attr_sd);
  178. return rc;
  179. }
  180. /**
  181. * sysfs_write_file - write an attribute.
  182. * @file: file pointer
  183. * @buf: data to write
  184. * @count: number of bytes
  185. * @ppos: starting offset
  186. *
  187. * Similar to sysfs_read_file(), though working in the opposite direction.
  188. * We allocate and fill the data from the user in fill_write_buffer(),
  189. * then push it to the kobject in flush_write_buffer().
  190. * There is no easy way for us to know if userspace is only doing a partial
  191. * write, so we don't support them. We expect the entire buffer to come
  192. * on the first write.
  193. * Hint: if you're writing a value, first read the file, modify only the
  194. * the value you're changing, then write entire buffer back.
  195. */
  196. static ssize_t
  197. sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  198. {
  199. struct sysfs_buffer * buffer = file->private_data;
  200. ssize_t len;
  201. mutex_lock(&buffer->mutex);
  202. len = fill_write_buffer(buffer, buf, count);
  203. if (len > 0)
  204. len = flush_write_buffer(file->f_path.dentry, buffer, len);
  205. if (len > 0)
  206. *ppos += len;
  207. mutex_unlock(&buffer->mutex);
  208. return len;
  209. }
  210. /**
  211. * sysfs_get_open_dirent - get or create sysfs_open_dirent
  212. * @sd: target sysfs_dirent
  213. * @buffer: sysfs_buffer for this instance of open
  214. *
  215. * If @sd->s_attr.open exists, increment its reference count;
  216. * otherwise, create one. @buffer is chained to the buffers
  217. * list.
  218. *
  219. * LOCKING:
  220. * Kernel thread context (may sleep).
  221. *
  222. * RETURNS:
  223. * 0 on success, -errno on failure.
  224. */
  225. static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
  226. struct sysfs_buffer *buffer)
  227. {
  228. struct sysfs_open_dirent *od, *new_od = NULL;
  229. retry:
  230. spin_lock(&sysfs_open_dirent_lock);
  231. if (!sd->s_attr.open && new_od) {
  232. sd->s_attr.open = new_od;
  233. new_od = NULL;
  234. }
  235. od = sd->s_attr.open;
  236. if (od) {
  237. atomic_inc(&od->refcnt);
  238. list_add_tail(&buffer->list, &od->buffers);
  239. }
  240. spin_unlock(&sysfs_open_dirent_lock);
  241. if (od) {
  242. kfree(new_od);
  243. return 0;
  244. }
  245. /* not there, initialize a new one and retry */
  246. new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
  247. if (!new_od)
  248. return -ENOMEM;
  249. atomic_set(&new_od->refcnt, 0);
  250. atomic_set(&new_od->event, 1);
  251. init_waitqueue_head(&new_od->poll);
  252. INIT_LIST_HEAD(&new_od->buffers);
  253. goto retry;
  254. }
  255. /**
  256. * sysfs_put_open_dirent - put sysfs_open_dirent
  257. * @sd: target sysfs_dirent
  258. * @buffer: associated sysfs_buffer
  259. *
  260. * Put @sd->s_attr.open and unlink @buffer from the buffers list.
  261. * If reference count reaches zero, disassociate and free it.
  262. *
  263. * LOCKING:
  264. * None.
  265. */
  266. static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
  267. struct sysfs_buffer *buffer)
  268. {
  269. struct sysfs_open_dirent *od = sd->s_attr.open;
  270. spin_lock(&sysfs_open_dirent_lock);
  271. list_del(&buffer->list);
  272. if (atomic_dec_and_test(&od->refcnt))
  273. sd->s_attr.open = NULL;
  274. else
  275. od = NULL;
  276. spin_unlock(&sysfs_open_dirent_lock);
  277. kfree(od);
  278. }
  279. static int sysfs_open_file(struct inode *inode, struct file *file)
  280. {
  281. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  282. struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
  283. struct sysfs_buffer *buffer;
  284. struct sysfs_ops *ops;
  285. int error = -EACCES;
  286. /* need attr_sd for attr and ops, its parent for kobj */
  287. if (!sysfs_get_active_two(attr_sd))
  288. return -ENODEV;
  289. /* every kobject with an attribute needs a ktype assigned */
  290. if (kobj->ktype && kobj->ktype->sysfs_ops)
  291. ops = kobj->ktype->sysfs_ops;
  292. else {
  293. printk(KERN_ERR "missing sysfs attribute operations for "
  294. "kobject: %s\n", kobject_name(kobj));
  295. WARN_ON(1);
  296. goto err_out;
  297. }
  298. /* File needs write support.
  299. * The inode's perms must say it's ok,
  300. * and we must have a store method.
  301. */
  302. if (file->f_mode & FMODE_WRITE) {
  303. if (!(inode->i_mode & S_IWUGO) || !ops->store)
  304. goto err_out;
  305. }
  306. /* File needs read support.
  307. * The inode's perms must say it's ok, and we there
  308. * must be a show method for it.
  309. */
  310. if (file->f_mode & FMODE_READ) {
  311. if (!(inode->i_mode & S_IRUGO) || !ops->show)
  312. goto err_out;
  313. }
  314. /* No error? Great, allocate a buffer for the file, and store it
  315. * it in file->private_data for easy access.
  316. */
  317. error = -ENOMEM;
  318. buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL);
  319. if (!buffer)
  320. goto err_out;
  321. mutex_init(&buffer->mutex);
  322. buffer->needs_read_fill = 1;
  323. buffer->ops = ops;
  324. file->private_data = buffer;
  325. /* make sure we have open dirent struct */
  326. error = sysfs_get_open_dirent(attr_sd, buffer);
  327. if (error)
  328. goto err_free;
  329. /* open succeeded, put active references */
  330. sysfs_put_active_two(attr_sd);
  331. return 0;
  332. err_free:
  333. kfree(buffer);
  334. err_out:
  335. sysfs_put_active_two(attr_sd);
  336. return error;
  337. }
  338. static int sysfs_release(struct inode *inode, struct file *filp)
  339. {
  340. struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
  341. struct sysfs_buffer *buffer = filp->private_data;
  342. sysfs_put_open_dirent(sd, buffer);
  343. if (buffer->page)
  344. free_page((unsigned long)buffer->page);
  345. kfree(buffer);
  346. return 0;
  347. }
  348. /* Sysfs attribute files are pollable. The idea is that you read
  349. * the content and then you use 'poll' or 'select' to wait for
  350. * the content to change. When the content changes (assuming the
  351. * manager for the kobject supports notification), poll will
  352. * return POLLERR|POLLPRI, and select will return the fd whether
  353. * it is waiting for read, write, or exceptions.
  354. * Once poll/select indicates that the value has changed, you
  355. * need to close and re-open the file, as simply seeking and reading
  356. * again will not get new data, or reset the state of 'poll'.
  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);