file.c 19 KB

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