file.c 20 KB

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