file.c 20 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_buffer 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_buffer which is chained at
  32. * sysfs_open_dirent->buffers, 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 buffers; /* goes through sysfs_buffer.list */
  41. };
  42. struct sysfs_buffer {
  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_buffer *buffer)
  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 (!buffer->page)
  78. buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
  79. if (!buffer->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. buffer->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, buffer->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. buffer->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_buffer *buffer = file->private_data;
  126. ssize_t retval = 0;
  127. mutex_lock(&buffer->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 || !buffer->page) {
  133. retval = fill_read_buffer(file->f_path.dentry, buffer);
  134. if (retval)
  135. goto out;
  136. }
  137. pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
  138. __func__, count, *ppos, buffer->page);
  139. retval = simple_read_from_buffer(buf, count, ppos, buffer->page,
  140. buffer->count);
  141. out:
  142. mutex_unlock(&buffer->mutex);
  143. return retval;
  144. }
  145. /**
  146. * fill_write_buffer - copy buffer from userspace.
  147. * @buffer: data buffer for file.
  148. * @buf: data from user.
  149. * @count: number of bytes in @userbuf.
  150. *
  151. * Allocate @buffer->page if it hasn't been already, then
  152. * copy the user-supplied buffer into it.
  153. */
  154. static int fill_write_buffer(struct sysfs_buffer *buffer,
  155. const char __user *buf, size_t count)
  156. {
  157. int error;
  158. if (!buffer->page)
  159. buffer->page = (char *)get_zeroed_page(GFP_KERNEL);
  160. if (!buffer->page)
  161. return -ENOMEM;
  162. if (count >= PAGE_SIZE)
  163. count = PAGE_SIZE - 1;
  164. error = copy_from_user(buffer->page, buf, count);
  165. /*
  166. * If buf is assumed to contain a string, terminate it by \0, so
  167. * e.g. sscanf() can scan the string easily.
  168. */
  169. buffer->page[count] = 0;
  170. return error ? -EFAULT : count;
  171. }
  172. /**
  173. * flush_write_buffer - push buffer to kobject.
  174. * @dentry: dentry to the attribute
  175. * @buffer: data buffer for 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 dentry *dentry,
  183. struct sysfs_buffer *buffer, size_t count)
  184. {
  185. struct sysfs_dirent *attr_sd = dentry->d_fsdata;
  186. struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
  187. const struct sysfs_ops *ops;
  188. int rc;
  189. /* need attr_sd for attr and ops, its parent for kobj */
  190. if (!sysfs_get_active(attr_sd))
  191. return -ENODEV;
  192. ops = sysfs_file_ops(attr_sd);
  193. rc = ops->store(kobj, attr_sd->s_attr.attr, buffer->page, count);
  194. sysfs_put_active(attr_sd);
  195. return rc;
  196. }
  197. /**
  198. * sysfs_write_file - write an attribute.
  199. * @file: file pointer
  200. * @buf: data to write
  201. * @count: number of bytes
  202. * @ppos: starting offset
  203. *
  204. * Similar to sysfs_read_file(), though working in the opposite direction.
  205. * We allocate and fill the data from the user in fill_write_buffer(),
  206. * then push it to the kobject in flush_write_buffer().
  207. * There is no easy way for us to know if userspace is only doing a partial
  208. * write, so we don't support them. We expect the entire buffer to come
  209. * on the first write.
  210. * Hint: if you're writing a value, first read the file, modify only the
  211. * the value you're changing, then write entire buffer back.
  212. */
  213. static ssize_t sysfs_write_file(struct file *file, const char __user *buf,
  214. size_t count, loff_t *ppos)
  215. {
  216. struct sysfs_buffer *buffer = file->private_data;
  217. ssize_t len;
  218. mutex_lock(&buffer->mutex);
  219. len = fill_write_buffer(buffer, buf, count);
  220. if (len > 0)
  221. len = flush_write_buffer(file->f_path.dentry, buffer, len);
  222. if (len > 0)
  223. *ppos += len;
  224. mutex_unlock(&buffer->mutex);
  225. return len;
  226. }
  227. /**
  228. * sysfs_get_open_dirent - get or create sysfs_open_dirent
  229. * @sd: target sysfs_dirent
  230. * @buffer: sysfs_buffer for this instance of open
  231. *
  232. * If @sd->s_attr.open exists, increment its reference count;
  233. * otherwise, create one. @buffer is chained to the buffers
  234. * list.
  235. *
  236. * LOCKING:
  237. * Kernel thread context (may sleep).
  238. *
  239. * RETURNS:
  240. * 0 on success, -errno on failure.
  241. */
  242. static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
  243. struct sysfs_buffer *buffer)
  244. {
  245. struct sysfs_open_dirent *od, *new_od = NULL;
  246. retry:
  247. mutex_lock(&sysfs_open_file_mutex);
  248. spin_lock_irq(&sysfs_open_dirent_lock);
  249. if (!sd->s_attr.open && new_od) {
  250. sd->s_attr.open = new_od;
  251. new_od = NULL;
  252. }
  253. od = sd->s_attr.open;
  254. if (od) {
  255. atomic_inc(&od->refcnt);
  256. list_add_tail(&buffer->list, &od->buffers);
  257. }
  258. spin_unlock_irq(&sysfs_open_dirent_lock);
  259. mutex_unlock(&sysfs_open_file_mutex);
  260. if (od) {
  261. kfree(new_od);
  262. return 0;
  263. }
  264. /* not there, initialize a new one and retry */
  265. new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
  266. if (!new_od)
  267. return -ENOMEM;
  268. atomic_set(&new_od->refcnt, 0);
  269. atomic_set(&new_od->event, 1);
  270. init_waitqueue_head(&new_od->poll);
  271. INIT_LIST_HEAD(&new_od->buffers);
  272. goto retry;
  273. }
  274. /**
  275. * sysfs_put_open_dirent - put sysfs_open_dirent
  276. * @sd: target sysfs_dirent
  277. * @buffer: associated sysfs_buffer
  278. *
  279. * Put @sd->s_attr.open and unlink @buffer from the buffers list.
  280. * If reference count reaches zero, disassociate and free it.
  281. *
  282. * LOCKING:
  283. * None.
  284. */
  285. static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
  286. struct sysfs_buffer *buffer)
  287. {
  288. struct sysfs_open_dirent *od = sd->s_attr.open;
  289. unsigned long flags;
  290. mutex_lock(&sysfs_open_file_mutex);
  291. spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
  292. list_del(&buffer->list);
  293. if (atomic_dec_and_test(&od->refcnt))
  294. sd->s_attr.open = NULL;
  295. else
  296. od = NULL;
  297. spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
  298. mutex_unlock(&sysfs_open_file_mutex);
  299. kfree(od);
  300. }
  301. static int sysfs_open_file(struct inode *inode, struct file *file)
  302. {
  303. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  304. struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
  305. struct sysfs_buffer *buffer;
  306. const struct sysfs_ops *ops;
  307. int error = -EACCES;
  308. /* need attr_sd for attr and ops, its parent for kobj */
  309. if (!sysfs_get_active(attr_sd))
  310. return -ENODEV;
  311. /* every kobject with an attribute needs a ktype assigned */
  312. ops = sysfs_file_ops(attr_sd);
  313. if (WARN(!ops, KERN_ERR
  314. "missing sysfs attribute operations for kobject: %s\n",
  315. kobject_name(kobj)))
  316. goto err_out;
  317. /* File needs write support.
  318. * The inode's perms must say it's ok,
  319. * and we must have a store method.
  320. */
  321. if (file->f_mode & FMODE_WRITE) {
  322. if (!(inode->i_mode & S_IWUGO) || !ops->store)
  323. goto err_out;
  324. }
  325. /* File needs read support.
  326. * The inode's perms must say it's ok, and we there
  327. * must be a show method for it.
  328. */
  329. if (file->f_mode & FMODE_READ) {
  330. if (!(inode->i_mode & S_IRUGO) || !ops->show)
  331. goto err_out;
  332. }
  333. /* No error? Great, allocate a buffer for the file, and store it
  334. * it in file->private_data for easy access.
  335. */
  336. error = -ENOMEM;
  337. buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL);
  338. if (!buffer)
  339. goto err_out;
  340. mutex_init(&buffer->mutex);
  341. file->private_data = buffer;
  342. /* make sure we have open dirent struct */
  343. error = sysfs_get_open_dirent(attr_sd, buffer);
  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(buffer);
  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_buffer *buffer = filp->private_data;
  359. sysfs_put_open_dirent(sd, buffer);
  360. if (buffer->page)
  361. free_page((unsigned long)buffer->page);
  362. kfree(buffer);
  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_buffer *buffer = 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 (buffer->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);