file.c 19 KB

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