file.c 18 KB

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