file.c 19 KB

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