file.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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. * fill_write_buffer - copy buffer from userspace.
  148. * @of: open file struct.
  149. * @buf: data from user.
  150. * @count: number of bytes in @userbuf.
  151. *
  152. * Allocate @of->page if it hasn't been already, then copy the
  153. * user-supplied buffer into it.
  154. */
  155. static int fill_write_buffer(struct sysfs_open_file *of,
  156. const char __user *buf, size_t count)
  157. {
  158. int error;
  159. if (!of->page)
  160. of->page = (char *)get_zeroed_page(GFP_KERNEL);
  161. if (!of->page)
  162. return -ENOMEM;
  163. if (count >= PAGE_SIZE)
  164. count = PAGE_SIZE - 1;
  165. error = copy_from_user(of->page, buf, count);
  166. /*
  167. * If buf is assumed to contain a string, terminate it by \0, so
  168. * e.g. sscanf() can scan the string easily.
  169. */
  170. of->page[count] = 0;
  171. return error ? -EFAULT : count;
  172. }
  173. /**
  174. * flush_write_buffer - push buffer to kobject.
  175. * @of: open file
  176. * @count: number of bytes
  177. *
  178. * Get the correct pointers for the kobject and the attribute we're
  179. * dealing with, then call the store() method for the attribute,
  180. * passing the buffer that we acquired in fill_write_buffer().
  181. */
  182. static int flush_write_buffer(struct sysfs_open_file *of, size_t count)
  183. {
  184. struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
  185. const struct sysfs_ops *ops;
  186. int rc;
  187. /* need @of->sd for attr and ops, its parent for kobj */
  188. if (!sysfs_get_active(of->sd))
  189. return -ENODEV;
  190. ops = sysfs_file_ops(of->sd);
  191. rc = ops->store(kobj, of->sd->s_attr.attr, of->page, count);
  192. sysfs_put_active(of->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_open_file *of = file->private_data;
  215. ssize_t len;
  216. mutex_lock(&of->mutex);
  217. len = fill_write_buffer(of, buf, count);
  218. if (len > 0)
  219. len = flush_write_buffer(of, len);
  220. if (len > 0)
  221. *ppos += len;
  222. mutex_unlock(&of->mutex);
  223. return len;
  224. }
  225. /**
  226. * sysfs_get_open_dirent - get or create sysfs_open_dirent
  227. * @sd: target sysfs_dirent
  228. * @of: sysfs_open_file for this instance of open
  229. *
  230. * If @sd->s_attr.open exists, increment its reference count;
  231. * otherwise, create one. @of is chained to the files list.
  232. *
  233. * LOCKING:
  234. * Kernel thread context (may sleep).
  235. *
  236. * RETURNS:
  237. * 0 on success, -errno on failure.
  238. */
  239. static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
  240. struct sysfs_open_file *of)
  241. {
  242. struct sysfs_open_dirent *od, *new_od = NULL;
  243. retry:
  244. mutex_lock(&sysfs_open_file_mutex);
  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(&of->list, &od->files);
  254. }
  255. spin_unlock_irq(&sysfs_open_dirent_lock);
  256. mutex_unlock(&sysfs_open_file_mutex);
  257. if (od) {
  258. kfree(new_od);
  259. return 0;
  260. }
  261. /* not there, initialize a new one and retry */
  262. new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
  263. if (!new_od)
  264. return -ENOMEM;
  265. atomic_set(&new_od->refcnt, 0);
  266. atomic_set(&new_od->event, 1);
  267. init_waitqueue_head(&new_od->poll);
  268. INIT_LIST_HEAD(&new_od->files);
  269. goto retry;
  270. }
  271. /**
  272. * sysfs_put_open_dirent - put sysfs_open_dirent
  273. * @sd: target sysfs_dirent
  274. * @of: associated sysfs_open_file
  275. *
  276. * Put @sd->s_attr.open and unlink @of from the files list. If
  277. * reference count reaches zero, disassociate and free it.
  278. *
  279. * LOCKING:
  280. * None.
  281. */
  282. static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
  283. struct sysfs_open_file *of)
  284. {
  285. struct sysfs_open_dirent *od = sd->s_attr.open;
  286. unsigned long flags;
  287. mutex_lock(&sysfs_open_file_mutex);
  288. spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
  289. list_del(&of->list);
  290. if (atomic_dec_and_test(&od->refcnt))
  291. sd->s_attr.open = NULL;
  292. else
  293. od = NULL;
  294. spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
  295. mutex_unlock(&sysfs_open_file_mutex);
  296. kfree(od);
  297. }
  298. static int sysfs_open_file(struct inode *inode, struct file *file)
  299. {
  300. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  301. struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
  302. struct sysfs_open_file *of;
  303. const struct sysfs_ops *ops;
  304. int error = -EACCES;
  305. /* need attr_sd for attr and ops, its parent for kobj */
  306. if (!sysfs_get_active(attr_sd))
  307. return -ENODEV;
  308. /* every kobject with an attribute needs a ktype assigned */
  309. ops = sysfs_file_ops(attr_sd);
  310. if (WARN(!ops, KERN_ERR
  311. "missing sysfs attribute operations for kobject: %s\n",
  312. kobject_name(kobj)))
  313. goto err_out;
  314. /* File needs write support.
  315. * The inode's perms must say it's ok,
  316. * and we must have a store method.
  317. */
  318. if (file->f_mode & FMODE_WRITE) {
  319. if (!(inode->i_mode & S_IWUGO) || !ops->store)
  320. goto err_out;
  321. }
  322. /* File needs read support.
  323. * The inode's perms must say it's ok, and we there
  324. * must be a show method for it.
  325. */
  326. if (file->f_mode & FMODE_READ) {
  327. if (!(inode->i_mode & S_IRUGO) || !ops->show)
  328. goto err_out;
  329. }
  330. /*
  331. * No error? Great, allocate a sysfs_open_file for the file, and
  332. * store it it in file->private_data for easy access.
  333. */
  334. error = -ENOMEM;
  335. of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL);
  336. if (!of)
  337. goto err_out;
  338. mutex_init(&of->mutex);
  339. of->sd = attr_sd;
  340. of->file = file;
  341. file->private_data = of;
  342. /* make sure we have open dirent struct */
  343. error = sysfs_get_open_dirent(attr_sd, of);
  344. if (error)
  345. goto err_free;
  346. /* open succeeded, put active references */
  347. sysfs_put_active(attr_sd);
  348. return 0;
  349. err_free:
  350. kfree(of);
  351. err_out:
  352. sysfs_put_active(attr_sd);
  353. return error;
  354. }
  355. static int sysfs_release(struct inode *inode, struct file *filp)
  356. {
  357. struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
  358. struct sysfs_open_file *of = filp->private_data;
  359. sysfs_put_open_dirent(sd, of);
  360. if (of->page)
  361. free_page((unsigned long)of->page);
  362. kfree(of);
  363. return 0;
  364. }
  365. /* Sysfs attribute files are pollable. The idea is that you read
  366. * the content and then you use 'poll' or 'select' to wait for
  367. * the content to change. When the content changes (assuming the
  368. * manager for the kobject supports notification), poll will
  369. * return POLLERR|POLLPRI, and select will return the fd whether
  370. * it is waiting for read, write, or exceptions.
  371. * Once poll/select indicates that the value has changed, you
  372. * need to close and re-open the file, or seek to 0 and read again.
  373. * Reminder: this only works for attributes which actively support
  374. * it, and it is not possible to test an attribute from userspace
  375. * to see if it supports poll (Neither 'poll' nor 'select' return
  376. * an appropriate error code). When in doubt, set a suitable timeout value.
  377. */
  378. static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
  379. {
  380. struct sysfs_open_file *of = filp->private_data;
  381. struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
  382. struct sysfs_open_dirent *od = attr_sd->s_attr.open;
  383. /* need parent for the kobj, grab both */
  384. if (!sysfs_get_active(attr_sd))
  385. goto trigger;
  386. poll_wait(filp, &od->poll, wait);
  387. sysfs_put_active(attr_sd);
  388. if (of->event != atomic_read(&od->event))
  389. goto trigger;
  390. return DEFAULT_POLLMASK;
  391. trigger:
  392. return DEFAULT_POLLMASK|POLLERR|POLLPRI;
  393. }
  394. void sysfs_notify_dirent(struct sysfs_dirent *sd)
  395. {
  396. struct sysfs_open_dirent *od;
  397. unsigned long flags;
  398. spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
  399. if (!WARN_ON(sysfs_type(sd) != SYSFS_KOBJ_ATTR)) {
  400. od = sd->s_attr.open;
  401. if (od) {
  402. atomic_inc(&od->event);
  403. wake_up_interruptible(&od->poll);
  404. }
  405. }
  406. spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
  407. }
  408. EXPORT_SYMBOL_GPL(sysfs_notify_dirent);
  409. void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
  410. {
  411. struct sysfs_dirent *sd = k->sd;
  412. mutex_lock(&sysfs_mutex);
  413. if (sd && dir)
  414. sd = sysfs_find_dirent(sd, dir, NULL);
  415. if (sd && attr)
  416. sd = sysfs_find_dirent(sd, attr, NULL);
  417. if (sd)
  418. sysfs_notify_dirent(sd);
  419. mutex_unlock(&sysfs_mutex);
  420. }
  421. EXPORT_SYMBOL_GPL(sysfs_notify);
  422. const struct file_operations sysfs_file_operations = {
  423. .read = sysfs_read_file,
  424. .write = sysfs_write_file,
  425. .llseek = generic_file_llseek,
  426. .open = sysfs_open_file,
  427. .release = sysfs_release,
  428. .poll = sysfs_poll,
  429. };
  430. int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
  431. const struct attribute *attr, int type,
  432. umode_t amode, const void *ns)
  433. {
  434. umode_t mode = (amode & S_IALLUGO) | S_IFREG;
  435. struct sysfs_addrm_cxt acxt;
  436. struct sysfs_dirent *sd;
  437. int rc;
  438. sd = sysfs_new_dirent(attr->name, mode, type);
  439. if (!sd)
  440. return -ENOMEM;
  441. sd->s_ns = ns;
  442. sd->s_attr.attr = (void *)attr;
  443. sysfs_dirent_init_lockdep(sd);
  444. sysfs_addrm_start(&acxt);
  445. rc = sysfs_add_one(&acxt, sd, dir_sd);
  446. sysfs_addrm_finish(&acxt);
  447. if (rc)
  448. sysfs_put(sd);
  449. return rc;
  450. }
  451. int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
  452. int type)
  453. {
  454. return sysfs_add_file_mode_ns(dir_sd, attr, type, attr->mode, NULL);
  455. }
  456. /**
  457. * sysfs_create_file_ns - create an attribute file for an object with custom ns
  458. * @kobj: object we're creating for
  459. * @attr: attribute descriptor
  460. * @ns: namespace the new file should belong to
  461. */
  462. int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
  463. const void *ns)
  464. {
  465. BUG_ON(!kobj || !kobj->sd || !attr);
  466. return sysfs_add_file_mode_ns(kobj->sd, attr, SYSFS_KOBJ_ATTR,
  467. attr->mode, ns);
  468. }
  469. EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
  470. int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
  471. {
  472. int err = 0;
  473. int i;
  474. for (i = 0; ptr[i] && !err; i++)
  475. err = sysfs_create_file(kobj, ptr[i]);
  476. if (err)
  477. while (--i >= 0)
  478. sysfs_remove_file(kobj, ptr[i]);
  479. return err;
  480. }
  481. EXPORT_SYMBOL_GPL(sysfs_create_files);
  482. /**
  483. * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
  484. * @kobj: object we're acting for.
  485. * @attr: attribute descriptor.
  486. * @group: group name.
  487. */
  488. int sysfs_add_file_to_group(struct kobject *kobj,
  489. const struct attribute *attr, const char *group)
  490. {
  491. struct sysfs_dirent *dir_sd;
  492. int error;
  493. if (group)
  494. dir_sd = sysfs_get_dirent(kobj->sd, group);
  495. else
  496. dir_sd = sysfs_get(kobj->sd);
  497. if (!dir_sd)
  498. return -ENOENT;
  499. error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR);
  500. sysfs_put(dir_sd);
  501. return error;
  502. }
  503. EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
  504. /**
  505. * sysfs_chmod_file - update the modified mode value on an object attribute.
  506. * @kobj: object we're acting for.
  507. * @attr: attribute descriptor.
  508. * @mode: file permissions.
  509. *
  510. */
  511. int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
  512. umode_t mode)
  513. {
  514. struct sysfs_dirent *sd;
  515. struct iattr newattrs;
  516. int rc;
  517. mutex_lock(&sysfs_mutex);
  518. rc = -ENOENT;
  519. sd = sysfs_find_dirent(kobj->sd, attr->name, NULL);
  520. if (!sd)
  521. goto out;
  522. newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO);
  523. newattrs.ia_valid = ATTR_MODE;
  524. rc = sysfs_sd_setattr(sd, &newattrs);
  525. out:
  526. mutex_unlock(&sysfs_mutex);
  527. return rc;
  528. }
  529. EXPORT_SYMBOL_GPL(sysfs_chmod_file);
  530. /**
  531. * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
  532. * @kobj: object we're acting for
  533. * @attr: attribute descriptor
  534. * @ns: namespace tag of the file to remove
  535. *
  536. * Hash the attribute name and namespace tag and kill the victim.
  537. */
  538. void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
  539. const void *ns)
  540. {
  541. struct sysfs_dirent *dir_sd = kobj->sd;
  542. sysfs_hash_and_remove(dir_sd, attr->name, ns);
  543. }
  544. EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
  545. void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
  546. {
  547. int i;
  548. for (i = 0; ptr[i]; i++)
  549. sysfs_remove_file(kobj, ptr[i]);
  550. }
  551. EXPORT_SYMBOL_GPL(sysfs_remove_files);
  552. /**
  553. * sysfs_remove_file_from_group - remove an attribute file from a group.
  554. * @kobj: object we're acting for.
  555. * @attr: attribute descriptor.
  556. * @group: group name.
  557. */
  558. void sysfs_remove_file_from_group(struct kobject *kobj,
  559. const struct attribute *attr, const char *group)
  560. {
  561. struct sysfs_dirent *dir_sd;
  562. if (group)
  563. dir_sd = sysfs_get_dirent(kobj->sd, group);
  564. else
  565. dir_sd = sysfs_get(kobj->sd);
  566. if (dir_sd) {
  567. sysfs_hash_and_remove(dir_sd, attr->name, NULL);
  568. sysfs_put(dir_sd);
  569. }
  570. }
  571. EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
  572. struct sysfs_schedule_callback_struct {
  573. struct list_head workq_list;
  574. struct kobject *kobj;
  575. void (*func)(void *);
  576. void *data;
  577. struct module *owner;
  578. struct work_struct work;
  579. };
  580. static struct workqueue_struct *sysfs_workqueue;
  581. static DEFINE_MUTEX(sysfs_workq_mutex);
  582. static LIST_HEAD(sysfs_workq);
  583. static void sysfs_schedule_callback_work(struct work_struct *work)
  584. {
  585. struct sysfs_schedule_callback_struct *ss = container_of(work,
  586. struct sysfs_schedule_callback_struct, work);
  587. (ss->func)(ss->data);
  588. kobject_put(ss->kobj);
  589. module_put(ss->owner);
  590. mutex_lock(&sysfs_workq_mutex);
  591. list_del(&ss->workq_list);
  592. mutex_unlock(&sysfs_workq_mutex);
  593. kfree(ss);
  594. }
  595. /**
  596. * sysfs_schedule_callback - helper to schedule a callback for a kobject
  597. * @kobj: object we're acting for.
  598. * @func: callback function to invoke later.
  599. * @data: argument to pass to @func.
  600. * @owner: module owning the callback code
  601. *
  602. * sysfs attribute methods must not unregister themselves or their parent
  603. * kobject (which would amount to the same thing). Attempts to do so will
  604. * deadlock, since unregistration is mutually exclusive with driver
  605. * callbacks.
  606. *
  607. * Instead methods can call this routine, which will attempt to allocate
  608. * and schedule a workqueue request to call back @func with @data as its
  609. * argument in the workqueue's process context. @kobj will be pinned
  610. * until @func returns.
  611. *
  612. * Returns 0 if the request was submitted, -ENOMEM if storage could not
  613. * be allocated, -ENODEV if a reference to @owner isn't available,
  614. * -EAGAIN if a callback has already been scheduled for @kobj.
  615. */
  616. int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
  617. void *data, struct module *owner)
  618. {
  619. struct sysfs_schedule_callback_struct *ss, *tmp;
  620. if (!try_module_get(owner))
  621. return -ENODEV;
  622. mutex_lock(&sysfs_workq_mutex);
  623. list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
  624. if (ss->kobj == kobj) {
  625. module_put(owner);
  626. mutex_unlock(&sysfs_workq_mutex);
  627. return -EAGAIN;
  628. }
  629. mutex_unlock(&sysfs_workq_mutex);
  630. if (sysfs_workqueue == NULL) {
  631. sysfs_workqueue = create_singlethread_workqueue("sysfsd");
  632. if (sysfs_workqueue == NULL) {
  633. module_put(owner);
  634. return -ENOMEM;
  635. }
  636. }
  637. ss = kmalloc(sizeof(*ss), GFP_KERNEL);
  638. if (!ss) {
  639. module_put(owner);
  640. return -ENOMEM;
  641. }
  642. kobject_get(kobj);
  643. ss->kobj = kobj;
  644. ss->func = func;
  645. ss->data = data;
  646. ss->owner = owner;
  647. INIT_WORK(&ss->work, sysfs_schedule_callback_work);
  648. INIT_LIST_HEAD(&ss->workq_list);
  649. mutex_lock(&sysfs_workq_mutex);
  650. list_add_tail(&ss->workq_list, &sysfs_workq);
  651. mutex_unlock(&sysfs_workq_mutex);
  652. queue_work(sysfs_workqueue, &ss->work);
  653. return 0;
  654. }
  655. EXPORT_SYMBOL_GPL(sysfs_schedule_callback);