file.c 28 KB

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