file.c 21 KB

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