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