file.c 19 KB

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