file.c 26 KB

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