file.c 20 KB

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