file.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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 <linux/mm.h>
  25. #include "sysfs.h"
  26. /*
  27. * There's one sysfs_open_file for each open file and one sysfs_open_dirent
  28. * for each sysfs_dirent with one or more open files.
  29. *
  30. * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open is
  31. * protected by sysfs_open_dirent_lock.
  32. *
  33. * filp->private_data points to seq_file whose ->private points to
  34. * sysfs_open_file. sysfs_open_files are chained at
  35. * sysfs_open_dirent->files, which is protected by sysfs_open_file_mutex.
  36. */
  37. static DEFINE_SPINLOCK(sysfs_open_dirent_lock);
  38. static DEFINE_MUTEX(sysfs_open_file_mutex);
  39. struct sysfs_open_dirent {
  40. atomic_t refcnt;
  41. atomic_t event;
  42. wait_queue_head_t poll;
  43. struct list_head files; /* goes through sysfs_open_file.list */
  44. };
  45. struct sysfs_open_file {
  46. struct sysfs_dirent *sd;
  47. struct file *file;
  48. struct mutex mutex;
  49. int event;
  50. struct list_head list;
  51. bool mmapped;
  52. const struct vm_operations_struct *vm_ops;
  53. };
  54. static bool sysfs_is_bin(struct sysfs_dirent *sd)
  55. {
  56. return sysfs_type(sd) == SYSFS_KOBJ_BIN_ATTR;
  57. }
  58. static struct sysfs_open_file *sysfs_of(struct file *file)
  59. {
  60. return ((struct seq_file *)file->private_data)->private;
  61. }
  62. /*
  63. * Determine ktype->sysfs_ops for the given sysfs_dirent. This function
  64. * must be called while holding an active reference.
  65. */
  66. static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd)
  67. {
  68. struct kobject *kobj = sd->s_parent->s_dir.kobj;
  69. lockdep_assert_held(sd);
  70. return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
  71. }
  72. /*
  73. * Reads on sysfs are handled through seq_file, which takes care of hairy
  74. * details like buffering and seeking. The following function pipes
  75. * sysfs_ops->show() result through seq_file.
  76. */
  77. static int sysfs_seq_show(struct seq_file *sf, void *v)
  78. {
  79. struct sysfs_open_file *of = sf->private;
  80. struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
  81. const struct sysfs_ops *ops;
  82. char *buf;
  83. ssize_t count;
  84. /* acquire buffer and ensure that it's >= PAGE_SIZE */
  85. count = seq_get_buf(sf, &buf);
  86. if (count < PAGE_SIZE) {
  87. seq_commit(sf, -1);
  88. return 0;
  89. }
  90. /*
  91. * Need @of->sd for attr and ops, its parent for kobj. @of->mutex
  92. * nests outside active ref and is just to ensure that the ops
  93. * aren't called concurrently for the same open file.
  94. */
  95. mutex_lock(&of->mutex);
  96. if (!sysfs_get_active(of->sd)) {
  97. mutex_unlock(&of->mutex);
  98. return -ENODEV;
  99. }
  100. of->event = atomic_read(&of->sd->s_attr.open->event);
  101. /*
  102. * Lookup @ops and invoke show(). Control may reach here via seq
  103. * file lseek even if @ops->show() isn't implemented.
  104. */
  105. ops = sysfs_file_ops(of->sd);
  106. if (ops->show)
  107. count = ops->show(kobj, of->sd->s_attr.attr, buf);
  108. else
  109. count = 0;
  110. sysfs_put_active(of->sd);
  111. mutex_unlock(&of->mutex);
  112. if (count < 0)
  113. return count;
  114. /*
  115. * The code works fine with PAGE_SIZE return but it's likely to
  116. * indicate truncated result or overflow in normal use cases.
  117. */
  118. if (count >= (ssize_t)PAGE_SIZE) {
  119. print_symbol("fill_read_buffer: %s returned bad count\n",
  120. (unsigned long)ops->show);
  121. /* Try to struggle along */
  122. count = PAGE_SIZE - 1;
  123. }
  124. seq_commit(sf, count);
  125. return 0;
  126. }
  127. /*
  128. * Read method for bin files. As reading a bin file can have side-effects,
  129. * the exact offset and bytes specified in read(2) call should be passed to
  130. * the read callback making it difficult to use seq_file. Implement
  131. * simplistic custom buffering for bin files.
  132. */
  133. static ssize_t sysfs_bin_read(struct file *file, char __user *userbuf,
  134. size_t bytes, loff_t *off)
  135. {
  136. struct sysfs_open_file *of = sysfs_of(file);
  137. struct bin_attribute *battr = of->sd->s_bin_attr.bin_attr;
  138. struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
  139. int size = file_inode(file)->i_size;
  140. int count = min_t(size_t, bytes, PAGE_SIZE);
  141. loff_t offs = *off;
  142. char *buf;
  143. if (!bytes)
  144. return 0;
  145. if (size) {
  146. if (offs > size)
  147. return 0;
  148. if (offs + count > size)
  149. count = size - offs;
  150. }
  151. buf = kmalloc(count, GFP_KERNEL);
  152. if (!buf)
  153. return -ENOMEM;
  154. /* need of->sd for battr, its parent for kobj */
  155. mutex_lock(&of->mutex);
  156. if (!sysfs_get_active(of->sd)) {
  157. count = -ENODEV;
  158. mutex_unlock(&of->mutex);
  159. goto out_free;
  160. }
  161. if (battr->read)
  162. count = battr->read(file, kobj, battr, buf, offs, count);
  163. else
  164. count = -EIO;
  165. sysfs_put_active(of->sd);
  166. mutex_unlock(&of->mutex);
  167. if (count < 0)
  168. goto out_free;
  169. if (copy_to_user(userbuf, buf, count)) {
  170. count = -EFAULT;
  171. goto out_free;
  172. }
  173. pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count);
  174. *off = offs + count;
  175. out_free:
  176. kfree(buf);
  177. return count;
  178. }
  179. /**
  180. * flush_write_buffer - push buffer to kobject
  181. * @of: open file
  182. * @buf: data buffer for file
  183. * @off: file offset to write to
  184. * @count: number of bytes
  185. *
  186. * Get the correct pointers for the kobject and the attribute we're dealing
  187. * with, then call the store() method for it with @buf.
  188. */
  189. static int flush_write_buffer(struct sysfs_open_file *of, char *buf, loff_t off,
  190. size_t count)
  191. {
  192. struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
  193. int rc = 0;
  194. /*
  195. * Need @of->sd for attr and ops, its parent for kobj. @of->mutex
  196. * nests outside active ref and is just to ensure that the ops
  197. * aren't called concurrently for the same open file.
  198. */
  199. mutex_lock(&of->mutex);
  200. if (!sysfs_get_active(of->sd)) {
  201. mutex_unlock(&of->mutex);
  202. return -ENODEV;
  203. }
  204. if (sysfs_is_bin(of->sd)) {
  205. struct bin_attribute *battr = of->sd->s_bin_attr.bin_attr;
  206. rc = -EIO;
  207. if (battr->write)
  208. rc = battr->write(of->file, kobj, battr, buf, off,
  209. count);
  210. } else {
  211. const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
  212. rc = ops->store(kobj, of->sd->s_attr.attr, buf, count);
  213. }
  214. sysfs_put_active(of->sd);
  215. mutex_unlock(&of->mutex);
  216. return rc;
  217. }
  218. /**
  219. * sysfs_write_file - write an attribute
  220. * @file: file pointer
  221. * @user_buf: data to write
  222. * @count: number of bytes
  223. * @ppos: starting offset
  224. *
  225. * Copy data in from userland and pass it to the matching
  226. * sysfs_ops->store() by invoking flush_write_buffer().
  227. *
  228. * There is no easy way for us to know if userspace is only doing a partial
  229. * write, so we don't support them. We expect the entire buffer to come on
  230. * the first write. Hint: if you're writing a value, first read the file,
  231. * modify only the the value you're changing, then write entire buffer
  232. * back.
  233. */
  234. static ssize_t sysfs_write_file(struct file *file, const char __user *user_buf,
  235. size_t count, loff_t *ppos)
  236. {
  237. struct sysfs_open_file *of = sysfs_of(file);
  238. ssize_t len = min_t(size_t, count, PAGE_SIZE);
  239. char *buf;
  240. if (sysfs_is_bin(of->sd)) {
  241. loff_t size = file_inode(file)->i_size;
  242. if (size <= *ppos)
  243. return 0;
  244. len = min_t(ssize_t, len, size - *ppos);
  245. }
  246. if (!len)
  247. return 0;
  248. buf = kmalloc(len + 1, GFP_KERNEL);
  249. if (!buf)
  250. return -ENOMEM;
  251. if (copy_from_user(buf, user_buf, len)) {
  252. len = -EFAULT;
  253. goto out_free;
  254. }
  255. buf[len] = '\0'; /* guarantee string termination */
  256. len = flush_write_buffer(of, buf, *ppos, len);
  257. if (len > 0)
  258. *ppos += len;
  259. out_free:
  260. kfree(buf);
  261. return len;
  262. }
  263. static void sysfs_bin_vma_open(struct vm_area_struct *vma)
  264. {
  265. struct file *file = vma->vm_file;
  266. struct sysfs_open_file *of = sysfs_of(file);
  267. if (!of->vm_ops)
  268. return;
  269. if (!sysfs_get_active(of->sd))
  270. return;
  271. if (of->vm_ops->open)
  272. of->vm_ops->open(vma);
  273. sysfs_put_active(of->sd);
  274. }
  275. static int sysfs_bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  276. {
  277. struct file *file = vma->vm_file;
  278. struct sysfs_open_file *of = sysfs_of(file);
  279. int ret;
  280. if (!of->vm_ops)
  281. return VM_FAULT_SIGBUS;
  282. if (!sysfs_get_active(of->sd))
  283. return VM_FAULT_SIGBUS;
  284. ret = VM_FAULT_SIGBUS;
  285. if (of->vm_ops->fault)
  286. ret = of->vm_ops->fault(vma, vmf);
  287. sysfs_put_active(of->sd);
  288. return ret;
  289. }
  290. static int sysfs_bin_page_mkwrite(struct vm_area_struct *vma,
  291. struct vm_fault *vmf)
  292. {
  293. struct file *file = vma->vm_file;
  294. struct sysfs_open_file *of = sysfs_of(file);
  295. int ret;
  296. if (!of->vm_ops)
  297. return VM_FAULT_SIGBUS;
  298. if (!sysfs_get_active(of->sd))
  299. return VM_FAULT_SIGBUS;
  300. ret = 0;
  301. if (of->vm_ops->page_mkwrite)
  302. ret = of->vm_ops->page_mkwrite(vma, vmf);
  303. else
  304. file_update_time(file);
  305. sysfs_put_active(of->sd);
  306. return ret;
  307. }
  308. static int sysfs_bin_access(struct vm_area_struct *vma, unsigned long addr,
  309. void *buf, int len, int write)
  310. {
  311. struct file *file = vma->vm_file;
  312. struct sysfs_open_file *of = sysfs_of(file);
  313. int ret;
  314. if (!of->vm_ops)
  315. return -EINVAL;
  316. if (!sysfs_get_active(of->sd))
  317. return -EINVAL;
  318. ret = -EINVAL;
  319. if (of->vm_ops->access)
  320. ret = of->vm_ops->access(vma, addr, buf, len, write);
  321. sysfs_put_active(of->sd);
  322. return ret;
  323. }
  324. #ifdef CONFIG_NUMA
  325. static int sysfs_bin_set_policy(struct vm_area_struct *vma,
  326. struct mempolicy *new)
  327. {
  328. struct file *file = vma->vm_file;
  329. struct sysfs_open_file *of = sysfs_of(file);
  330. int ret;
  331. if (!of->vm_ops)
  332. return 0;
  333. if (!sysfs_get_active(of->sd))
  334. return -EINVAL;
  335. ret = 0;
  336. if (of->vm_ops->set_policy)
  337. ret = of->vm_ops->set_policy(vma, new);
  338. sysfs_put_active(of->sd);
  339. return ret;
  340. }
  341. static struct mempolicy *sysfs_bin_get_policy(struct vm_area_struct *vma,
  342. unsigned long addr)
  343. {
  344. struct file *file = vma->vm_file;
  345. struct sysfs_open_file *of = sysfs_of(file);
  346. struct mempolicy *pol;
  347. if (!of->vm_ops)
  348. return vma->vm_policy;
  349. if (!sysfs_get_active(of->sd))
  350. return vma->vm_policy;
  351. pol = vma->vm_policy;
  352. if (of->vm_ops->get_policy)
  353. pol = of->vm_ops->get_policy(vma, addr);
  354. sysfs_put_active(of->sd);
  355. return pol;
  356. }
  357. static int sysfs_bin_migrate(struct vm_area_struct *vma, const nodemask_t *from,
  358. const nodemask_t *to, unsigned long flags)
  359. {
  360. struct file *file = vma->vm_file;
  361. struct sysfs_open_file *of = sysfs_of(file);
  362. int ret;
  363. if (!of->vm_ops)
  364. return 0;
  365. if (!sysfs_get_active(of->sd))
  366. return 0;
  367. ret = 0;
  368. if (of->vm_ops->migrate)
  369. ret = of->vm_ops->migrate(vma, from, to, flags);
  370. sysfs_put_active(of->sd);
  371. return ret;
  372. }
  373. #endif
  374. static const struct vm_operations_struct sysfs_bin_vm_ops = {
  375. .open = sysfs_bin_vma_open,
  376. .fault = sysfs_bin_fault,
  377. .page_mkwrite = sysfs_bin_page_mkwrite,
  378. .access = sysfs_bin_access,
  379. #ifdef CONFIG_NUMA
  380. .set_policy = sysfs_bin_set_policy,
  381. .get_policy = sysfs_bin_get_policy,
  382. .migrate = sysfs_bin_migrate,
  383. #endif
  384. };
  385. static int sysfs_bin_mmap(struct file *file, struct vm_area_struct *vma)
  386. {
  387. struct sysfs_open_file *of = sysfs_of(file);
  388. struct bin_attribute *battr = of->sd->s_bin_attr.bin_attr;
  389. struct kobject *kobj = of->sd->s_parent->s_dir.kobj;
  390. int rc;
  391. mutex_lock(&of->mutex);
  392. /* need of->sd for battr, its parent for kobj */
  393. rc = -ENODEV;
  394. if (!sysfs_get_active(of->sd))
  395. goto out_unlock;
  396. rc = -EINVAL;
  397. if (!battr->mmap)
  398. goto out_put;
  399. rc = battr->mmap(file, kobj, battr, vma);
  400. if (rc)
  401. goto out_put;
  402. /*
  403. * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
  404. * to satisfy versions of X which crash if the mmap fails: that
  405. * substitutes a new vm_file, and we don't then want bin_vm_ops.
  406. */
  407. if (vma->vm_file != file)
  408. goto out_put;
  409. rc = -EINVAL;
  410. if (of->mmapped && of->vm_ops != vma->vm_ops)
  411. goto out_put;
  412. /*
  413. * It is not possible to successfully wrap close.
  414. * So error if someone is trying to use close.
  415. */
  416. rc = -EINVAL;
  417. if (vma->vm_ops && vma->vm_ops->close)
  418. goto out_put;
  419. rc = 0;
  420. of->mmapped = 1;
  421. of->vm_ops = vma->vm_ops;
  422. vma->vm_ops = &sysfs_bin_vm_ops;
  423. out_put:
  424. sysfs_put_active(of->sd);
  425. out_unlock:
  426. mutex_unlock(&of->mutex);
  427. return rc;
  428. }
  429. /**
  430. * sysfs_get_open_dirent - get or create sysfs_open_dirent
  431. * @sd: target sysfs_dirent
  432. * @of: sysfs_open_file for this instance of open
  433. *
  434. * If @sd->s_attr.open exists, increment its reference count;
  435. * otherwise, create one. @of is chained to the files list.
  436. *
  437. * LOCKING:
  438. * Kernel thread context (may sleep).
  439. *
  440. * RETURNS:
  441. * 0 on success, -errno on failure.
  442. */
  443. static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
  444. struct sysfs_open_file *of)
  445. {
  446. struct sysfs_open_dirent *od, *new_od = NULL;
  447. retry:
  448. mutex_lock(&sysfs_open_file_mutex);
  449. spin_lock_irq(&sysfs_open_dirent_lock);
  450. if (!sd->s_attr.open && new_od) {
  451. sd->s_attr.open = new_od;
  452. new_od = NULL;
  453. }
  454. od = sd->s_attr.open;
  455. if (od) {
  456. atomic_inc(&od->refcnt);
  457. list_add_tail(&of->list, &od->files);
  458. }
  459. spin_unlock_irq(&sysfs_open_dirent_lock);
  460. mutex_unlock(&sysfs_open_file_mutex);
  461. if (od) {
  462. kfree(new_od);
  463. return 0;
  464. }
  465. /* not there, initialize a new one and retry */
  466. new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
  467. if (!new_od)
  468. return -ENOMEM;
  469. atomic_set(&new_od->refcnt, 0);
  470. atomic_set(&new_od->event, 1);
  471. init_waitqueue_head(&new_od->poll);
  472. INIT_LIST_HEAD(&new_od->files);
  473. goto retry;
  474. }
  475. /**
  476. * sysfs_put_open_dirent - put sysfs_open_dirent
  477. * @sd: target sysfs_dirent
  478. * @of: associated sysfs_open_file
  479. *
  480. * Put @sd->s_attr.open and unlink @of from the files list. If
  481. * reference count reaches zero, disassociate and free it.
  482. *
  483. * LOCKING:
  484. * None.
  485. */
  486. static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
  487. struct sysfs_open_file *of)
  488. {
  489. struct sysfs_open_dirent *od = sd->s_attr.open;
  490. unsigned long flags;
  491. mutex_lock(&sysfs_open_file_mutex);
  492. spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
  493. if (of)
  494. list_del(&of->list);
  495. if (atomic_dec_and_test(&od->refcnt))
  496. sd->s_attr.open = NULL;
  497. else
  498. od = NULL;
  499. spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
  500. mutex_unlock(&sysfs_open_file_mutex);
  501. kfree(od);
  502. }
  503. static int sysfs_open_file(struct inode *inode, struct file *file)
  504. {
  505. struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
  506. struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
  507. struct sysfs_open_file *of;
  508. const struct sysfs_ops *ops;
  509. int error = -EACCES;
  510. /* need attr_sd for attr and ops, its parent for kobj */
  511. if (!sysfs_get_active(attr_sd))
  512. return -ENODEV;
  513. /* every kobject with an attribute needs a ktype assigned */
  514. ops = sysfs_file_ops(attr_sd);
  515. if (WARN(!ops, KERN_ERR
  516. "missing sysfs attribute operations for kobject: %s\n",
  517. kobject_name(kobj)))
  518. goto err_out;
  519. /* File needs write support.
  520. * The inode's perms must say it's ok,
  521. * and we must have a store method.
  522. */
  523. if (file->f_mode & FMODE_WRITE) {
  524. if (!(inode->i_mode & S_IWUGO) || !ops->store)
  525. goto err_out;
  526. }
  527. /* File needs read support.
  528. * The inode's perms must say it's ok, and we there
  529. * must be a show method for it.
  530. */
  531. if (file->f_mode & FMODE_READ) {
  532. if (!(inode->i_mode & S_IRUGO) || !ops->show)
  533. goto err_out;
  534. }
  535. /* allocate a sysfs_open_file for the file */
  536. error = -ENOMEM;
  537. of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL);
  538. if (!of)
  539. goto err_out;
  540. mutex_init(&of->mutex);
  541. of->sd = attr_sd;
  542. of->file = file;
  543. /*
  544. * Always instantiate seq_file even if read access is not
  545. * implemented or requested. This unifies private data access and
  546. * most files are readable anyway.
  547. */
  548. error = single_open(file, sysfs_seq_show, of);
  549. if (error)
  550. goto err_free;
  551. /* seq_file clears PWRITE unconditionally, restore it if WRITE */
  552. if (file->f_mode & FMODE_WRITE)
  553. file->f_mode |= FMODE_PWRITE;
  554. /* make sure we have open dirent struct */
  555. error = sysfs_get_open_dirent(attr_sd, of);
  556. if (error)
  557. goto err_close;
  558. /* open succeeded, put active references */
  559. sysfs_put_active(attr_sd);
  560. return 0;
  561. err_close:
  562. single_release(inode, file);
  563. err_free:
  564. kfree(of);
  565. err_out:
  566. sysfs_put_active(attr_sd);
  567. return error;
  568. }
  569. static int sysfs_release(struct inode *inode, struct file *filp)
  570. {
  571. struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
  572. struct sysfs_open_file *of = sysfs_of(filp);
  573. sysfs_put_open_dirent(sd, of);
  574. single_release(inode, filp);
  575. kfree(of);
  576. return 0;
  577. }
  578. void sysfs_unmap_bin_file(struct sysfs_dirent *sd)
  579. {
  580. struct sysfs_open_dirent *od;
  581. struct sysfs_open_file *of;
  582. if (!sysfs_is_bin(sd))
  583. return;
  584. spin_lock_irq(&sysfs_open_dirent_lock);
  585. od = sd->s_attr.open;
  586. if (od)
  587. atomic_inc(&od->refcnt);
  588. spin_unlock_irq(&sysfs_open_dirent_lock);
  589. if (!od)
  590. return;
  591. mutex_lock(&sysfs_open_file_mutex);
  592. list_for_each_entry(of, &od->files, list) {
  593. struct inode *inode = file_inode(of->file);
  594. unmap_mapping_range(inode->i_mapping, 0, 0, 1);
  595. }
  596. mutex_unlock(&sysfs_open_file_mutex);
  597. sysfs_put_open_dirent(sd, NULL);
  598. }
  599. /* Sysfs attribute files are pollable. The idea is that you read
  600. * the content and then you use 'poll' or 'select' to wait for
  601. * the content to change. When the content changes (assuming the
  602. * manager for the kobject supports notification), poll will
  603. * return POLLERR|POLLPRI, and select will return the fd whether
  604. * it is waiting for read, write, or exceptions.
  605. * Once poll/select indicates that the value has changed, you
  606. * need to close and re-open the file, or seek to 0 and read again.
  607. * Reminder: this only works for attributes which actively support
  608. * it, and it is not possible to test an attribute from userspace
  609. * to see if it supports poll (Neither 'poll' nor 'select' return
  610. * an appropriate error code). When in doubt, set a suitable timeout value.
  611. */
  612. static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
  613. {
  614. struct sysfs_open_file *of = sysfs_of(filp);
  615. struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
  616. struct sysfs_open_dirent *od = attr_sd->s_attr.open;
  617. /* need parent for the kobj, grab both */
  618. if (!sysfs_get_active(attr_sd))
  619. goto trigger;
  620. poll_wait(filp, &od->poll, wait);
  621. sysfs_put_active(attr_sd);
  622. if (of->event != atomic_read(&od->event))
  623. goto trigger;
  624. return DEFAULT_POLLMASK;
  625. trigger:
  626. return DEFAULT_POLLMASK|POLLERR|POLLPRI;
  627. }
  628. void sysfs_notify_dirent(struct sysfs_dirent *sd)
  629. {
  630. struct sysfs_open_dirent *od;
  631. unsigned long flags;
  632. spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
  633. if (!WARN_ON(sysfs_type(sd) != SYSFS_KOBJ_ATTR)) {
  634. od = sd->s_attr.open;
  635. if (od) {
  636. atomic_inc(&od->event);
  637. wake_up_interruptible(&od->poll);
  638. }
  639. }
  640. spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
  641. }
  642. EXPORT_SYMBOL_GPL(sysfs_notify_dirent);
  643. void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
  644. {
  645. struct sysfs_dirent *sd = k->sd;
  646. mutex_lock(&sysfs_mutex);
  647. if (sd && dir)
  648. sd = sysfs_find_dirent(sd, dir, NULL);
  649. if (sd && attr)
  650. sd = sysfs_find_dirent(sd, attr, NULL);
  651. if (sd)
  652. sysfs_notify_dirent(sd);
  653. mutex_unlock(&sysfs_mutex);
  654. }
  655. EXPORT_SYMBOL_GPL(sysfs_notify);
  656. const struct file_operations sysfs_file_operations = {
  657. .read = seq_read,
  658. .write = sysfs_write_file,
  659. .llseek = seq_lseek,
  660. .open = sysfs_open_file,
  661. .release = sysfs_release,
  662. .poll = sysfs_poll,
  663. };
  664. const struct file_operations sysfs_bin_operations = {
  665. .read = sysfs_bin_read,
  666. .write = sysfs_write_file,
  667. .llseek = generic_file_llseek,
  668. .mmap = sysfs_bin_mmap,
  669. };
  670. int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
  671. const struct attribute *attr, int type,
  672. umode_t amode, const void *ns)
  673. {
  674. umode_t mode = (amode & S_IALLUGO) | S_IFREG;
  675. struct sysfs_addrm_cxt acxt;
  676. struct sysfs_dirent *sd;
  677. int rc;
  678. sd = sysfs_new_dirent(attr->name, mode, type);
  679. if (!sd)
  680. return -ENOMEM;
  681. sd->s_ns = ns;
  682. sd->s_attr.attr = (void *)attr;
  683. sysfs_dirent_init_lockdep(sd);
  684. sysfs_addrm_start(&acxt);
  685. rc = sysfs_add_one(&acxt, sd, dir_sd);
  686. sysfs_addrm_finish(&acxt);
  687. if (rc)
  688. sysfs_put(sd);
  689. return rc;
  690. }
  691. int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
  692. int type)
  693. {
  694. return sysfs_add_file_mode_ns(dir_sd, attr, type, attr->mode, NULL);
  695. }
  696. /**
  697. * sysfs_create_file_ns - create an attribute file for an object with custom ns
  698. * @kobj: object we're creating for
  699. * @attr: attribute descriptor
  700. * @ns: namespace the new file should belong to
  701. */
  702. int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
  703. const void *ns)
  704. {
  705. BUG_ON(!kobj || !kobj->sd || !attr);
  706. return sysfs_add_file_mode_ns(kobj->sd, attr, SYSFS_KOBJ_ATTR,
  707. attr->mode, ns);
  708. }
  709. EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
  710. int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
  711. {
  712. int err = 0;
  713. int i;
  714. for (i = 0; ptr[i] && !err; i++)
  715. err = sysfs_create_file(kobj, ptr[i]);
  716. if (err)
  717. while (--i >= 0)
  718. sysfs_remove_file(kobj, ptr[i]);
  719. return err;
  720. }
  721. EXPORT_SYMBOL_GPL(sysfs_create_files);
  722. /**
  723. * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
  724. * @kobj: object we're acting for.
  725. * @attr: attribute descriptor.
  726. * @group: group name.
  727. */
  728. int sysfs_add_file_to_group(struct kobject *kobj,
  729. const struct attribute *attr, const char *group)
  730. {
  731. struct sysfs_dirent *dir_sd;
  732. int error;
  733. if (group)
  734. dir_sd = sysfs_get_dirent(kobj->sd, group);
  735. else
  736. dir_sd = sysfs_get(kobj->sd);
  737. if (!dir_sd)
  738. return -ENOENT;
  739. error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR);
  740. sysfs_put(dir_sd);
  741. return error;
  742. }
  743. EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
  744. /**
  745. * sysfs_chmod_file - update the modified mode value on an object attribute.
  746. * @kobj: object we're acting for.
  747. * @attr: attribute descriptor.
  748. * @mode: file permissions.
  749. *
  750. */
  751. int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
  752. umode_t mode)
  753. {
  754. struct sysfs_dirent *sd;
  755. struct iattr newattrs;
  756. int rc;
  757. mutex_lock(&sysfs_mutex);
  758. rc = -ENOENT;
  759. sd = sysfs_find_dirent(kobj->sd, attr->name, NULL);
  760. if (!sd)
  761. goto out;
  762. newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO);
  763. newattrs.ia_valid = ATTR_MODE;
  764. rc = sysfs_sd_setattr(sd, &newattrs);
  765. out:
  766. mutex_unlock(&sysfs_mutex);
  767. return rc;
  768. }
  769. EXPORT_SYMBOL_GPL(sysfs_chmod_file);
  770. /**
  771. * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
  772. * @kobj: object we're acting for
  773. * @attr: attribute descriptor
  774. * @ns: namespace tag of the file to remove
  775. *
  776. * Hash the attribute name and namespace tag and kill the victim.
  777. */
  778. void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
  779. const void *ns)
  780. {
  781. struct sysfs_dirent *dir_sd = kobj->sd;
  782. sysfs_hash_and_remove(dir_sd, attr->name, ns);
  783. }
  784. EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
  785. void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
  786. {
  787. int i;
  788. for (i = 0; ptr[i]; i++)
  789. sysfs_remove_file(kobj, ptr[i]);
  790. }
  791. EXPORT_SYMBOL_GPL(sysfs_remove_files);
  792. /**
  793. * sysfs_remove_file_from_group - remove an attribute file from a group.
  794. * @kobj: object we're acting for.
  795. * @attr: attribute descriptor.
  796. * @group: group name.
  797. */
  798. void sysfs_remove_file_from_group(struct kobject *kobj,
  799. const struct attribute *attr, const char *group)
  800. {
  801. struct sysfs_dirent *dir_sd;
  802. if (group)
  803. dir_sd = sysfs_get_dirent(kobj->sd, group);
  804. else
  805. dir_sd = sysfs_get(kobj->sd);
  806. if (dir_sd) {
  807. sysfs_hash_and_remove(dir_sd, attr->name, NULL);
  808. sysfs_put(dir_sd);
  809. }
  810. }
  811. EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
  812. struct sysfs_schedule_callback_struct {
  813. struct list_head workq_list;
  814. struct kobject *kobj;
  815. void (*func)(void *);
  816. void *data;
  817. struct module *owner;
  818. struct work_struct work;
  819. };
  820. static struct workqueue_struct *sysfs_workqueue;
  821. static DEFINE_MUTEX(sysfs_workq_mutex);
  822. static LIST_HEAD(sysfs_workq);
  823. static void sysfs_schedule_callback_work(struct work_struct *work)
  824. {
  825. struct sysfs_schedule_callback_struct *ss = container_of(work,
  826. struct sysfs_schedule_callback_struct, work);
  827. (ss->func)(ss->data);
  828. kobject_put(ss->kobj);
  829. module_put(ss->owner);
  830. mutex_lock(&sysfs_workq_mutex);
  831. list_del(&ss->workq_list);
  832. mutex_unlock(&sysfs_workq_mutex);
  833. kfree(ss);
  834. }
  835. /**
  836. * sysfs_schedule_callback - helper to schedule a callback for a kobject
  837. * @kobj: object we're acting for.
  838. * @func: callback function to invoke later.
  839. * @data: argument to pass to @func.
  840. * @owner: module owning the callback code
  841. *
  842. * sysfs attribute methods must not unregister themselves or their parent
  843. * kobject (which would amount to the same thing). Attempts to do so will
  844. * deadlock, since unregistration is mutually exclusive with driver
  845. * callbacks.
  846. *
  847. * Instead methods can call this routine, which will attempt to allocate
  848. * and schedule a workqueue request to call back @func with @data as its
  849. * argument in the workqueue's process context. @kobj will be pinned
  850. * until @func returns.
  851. *
  852. * Returns 0 if the request was submitted, -ENOMEM if storage could not
  853. * be allocated, -ENODEV if a reference to @owner isn't available,
  854. * -EAGAIN if a callback has already been scheduled for @kobj.
  855. */
  856. int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
  857. void *data, struct module *owner)
  858. {
  859. struct sysfs_schedule_callback_struct *ss, *tmp;
  860. if (!try_module_get(owner))
  861. return -ENODEV;
  862. mutex_lock(&sysfs_workq_mutex);
  863. list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
  864. if (ss->kobj == kobj) {
  865. module_put(owner);
  866. mutex_unlock(&sysfs_workq_mutex);
  867. return -EAGAIN;
  868. }
  869. mutex_unlock(&sysfs_workq_mutex);
  870. if (sysfs_workqueue == NULL) {
  871. sysfs_workqueue = create_singlethread_workqueue("sysfsd");
  872. if (sysfs_workqueue == NULL) {
  873. module_put(owner);
  874. return -ENOMEM;
  875. }
  876. }
  877. ss = kmalloc(sizeof(*ss), GFP_KERNEL);
  878. if (!ss) {
  879. module_put(owner);
  880. return -ENOMEM;
  881. }
  882. kobject_get(kobj);
  883. ss->kobj = kobj;
  884. ss->func = func;
  885. ss->data = data;
  886. ss->owner = owner;
  887. INIT_WORK(&ss->work, sysfs_schedule_callback_work);
  888. INIT_LIST_HEAD(&ss->workq_list);
  889. mutex_lock(&sysfs_workq_mutex);
  890. list_add_tail(&ss->workq_list, &sysfs_workq);
  891. mutex_unlock(&sysfs_workq_mutex);
  892. queue_work(sysfs_workqueue, &ss->work);
  893. return 0;
  894. }
  895. EXPORT_SYMBOL_GPL(sysfs_schedule_callback);