file.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  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, has_mmap;
  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. has_mmap = battr->mmap;
  518. } else {
  519. const struct sysfs_ops *ops = sysfs_file_ops(attr_sd);
  520. /* every kobject with an attribute needs a ktype assigned */
  521. if (WARN(!ops, KERN_ERR
  522. "missing sysfs attribute operations for kobject: %s\n",
  523. kobject_name(kobj)))
  524. goto err_out;
  525. has_read = ops->show;
  526. has_write = ops->store;
  527. has_mmap = false;
  528. }
  529. /* check perms and supported operations */
  530. if ((file->f_mode & FMODE_WRITE) &&
  531. (!(inode->i_mode & S_IWUGO) || !has_write))
  532. goto err_out;
  533. if ((file->f_mode & FMODE_READ) &&
  534. (!(inode->i_mode & S_IRUGO) || !has_read))
  535. goto err_out;
  536. /* allocate a sysfs_open_file for the file */
  537. error = -ENOMEM;
  538. of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL);
  539. if (!of)
  540. goto err_out;
  541. /*
  542. * The following is done to give a different lockdep key to
  543. * @of->mutex for files which implement mmap. This is a rather
  544. * crude way to avoid false positive lockdep warning around
  545. * mm->mmap_sem - mmap nests @of->mutex under mm->mmap_sem and
  546. * reading /sys/block/sda/trace/act_mask grabs sr_mutex, under
  547. * which mm->mmap_sem nests, while holding @of->mutex. As each
  548. * open file has a separate mutex, it's okay as long as those don't
  549. * happen on the same file. At this point, we can't easily give
  550. * each file a separate locking class. Let's differentiate on
  551. * whether the file has mmap or not for now.
  552. */
  553. if (has_mmap)
  554. mutex_init(&of->mutex);
  555. else
  556. mutex_init(&of->mutex);
  557. of->sd = attr_sd;
  558. of->file = file;
  559. /*
  560. * Always instantiate seq_file even if read access doesn't use
  561. * seq_file or is not requested. This unifies private data access
  562. * and readable regular files are the vast majority anyway.
  563. */
  564. if (sysfs_is_bin(attr_sd))
  565. error = single_open(file, NULL, of);
  566. else
  567. error = single_open(file, sysfs_seq_show, of);
  568. if (error)
  569. goto err_free;
  570. /* seq_file clears PWRITE unconditionally, restore it if WRITE */
  571. if (file->f_mode & FMODE_WRITE)
  572. file->f_mode |= FMODE_PWRITE;
  573. /* make sure we have open dirent struct */
  574. error = sysfs_get_open_dirent(attr_sd, of);
  575. if (error)
  576. goto err_close;
  577. /* open succeeded, put active references */
  578. sysfs_put_active(attr_sd);
  579. return 0;
  580. err_close:
  581. single_release(inode, file);
  582. err_free:
  583. kfree(of);
  584. err_out:
  585. sysfs_put_active(attr_sd);
  586. return error;
  587. }
  588. static int sysfs_release(struct inode *inode, struct file *filp)
  589. {
  590. struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
  591. struct sysfs_open_file *of = sysfs_of(filp);
  592. sysfs_put_open_dirent(sd, of);
  593. single_release(inode, filp);
  594. kfree(of);
  595. return 0;
  596. }
  597. void sysfs_unmap_bin_file(struct sysfs_dirent *sd)
  598. {
  599. struct sysfs_open_dirent *od;
  600. struct sysfs_open_file *of;
  601. if (!sysfs_is_bin(sd))
  602. return;
  603. spin_lock_irq(&sysfs_open_dirent_lock);
  604. od = sd->s_attr.open;
  605. if (od)
  606. atomic_inc(&od->refcnt);
  607. spin_unlock_irq(&sysfs_open_dirent_lock);
  608. if (!od)
  609. return;
  610. mutex_lock(&sysfs_open_file_mutex);
  611. list_for_each_entry(of, &od->files, list) {
  612. struct inode *inode = file_inode(of->file);
  613. unmap_mapping_range(inode->i_mapping, 0, 0, 1);
  614. }
  615. mutex_unlock(&sysfs_open_file_mutex);
  616. sysfs_put_open_dirent(sd, NULL);
  617. }
  618. /* Sysfs attribute files are pollable. The idea is that you read
  619. * the content and then you use 'poll' or 'select' to wait for
  620. * the content to change. When the content changes (assuming the
  621. * manager for the kobject supports notification), poll will
  622. * return POLLERR|POLLPRI, and select will return the fd whether
  623. * it is waiting for read, write, or exceptions.
  624. * Once poll/select indicates that the value has changed, you
  625. * need to close and re-open the file, or seek to 0 and read again.
  626. * Reminder: this only works for attributes which actively support
  627. * it, and it is not possible to test an attribute from userspace
  628. * to see if it supports poll (Neither 'poll' nor 'select' return
  629. * an appropriate error code). When in doubt, set a suitable timeout value.
  630. */
  631. static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
  632. {
  633. struct sysfs_open_file *of = sysfs_of(filp);
  634. struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
  635. struct sysfs_open_dirent *od = attr_sd->s_attr.open;
  636. /* need parent for the kobj, grab both */
  637. if (!sysfs_get_active(attr_sd))
  638. goto trigger;
  639. poll_wait(filp, &od->poll, wait);
  640. sysfs_put_active(attr_sd);
  641. if (of->event != atomic_read(&od->event))
  642. goto trigger;
  643. return DEFAULT_POLLMASK;
  644. trigger:
  645. return DEFAULT_POLLMASK|POLLERR|POLLPRI;
  646. }
  647. void sysfs_notify_dirent(struct sysfs_dirent *sd)
  648. {
  649. struct sysfs_open_dirent *od;
  650. unsigned long flags;
  651. spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
  652. if (!WARN_ON(sysfs_type(sd) != SYSFS_KOBJ_ATTR)) {
  653. od = sd->s_attr.open;
  654. if (od) {
  655. atomic_inc(&od->event);
  656. wake_up_interruptible(&od->poll);
  657. }
  658. }
  659. spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
  660. }
  661. EXPORT_SYMBOL_GPL(sysfs_notify_dirent);
  662. void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
  663. {
  664. struct sysfs_dirent *sd = k->sd;
  665. mutex_lock(&sysfs_mutex);
  666. if (sd && dir)
  667. sd = sysfs_find_dirent(sd, dir, NULL);
  668. if (sd && attr)
  669. sd = sysfs_find_dirent(sd, attr, NULL);
  670. if (sd)
  671. sysfs_notify_dirent(sd);
  672. mutex_unlock(&sysfs_mutex);
  673. }
  674. EXPORT_SYMBOL_GPL(sysfs_notify);
  675. const struct file_operations sysfs_file_operations = {
  676. .read = seq_read,
  677. .write = sysfs_write_file,
  678. .llseek = generic_file_llseek,
  679. .open = sysfs_open_file,
  680. .release = sysfs_release,
  681. .poll = sysfs_poll,
  682. };
  683. const struct file_operations sysfs_bin_operations = {
  684. .read = sysfs_bin_read,
  685. .write = sysfs_write_file,
  686. .llseek = generic_file_llseek,
  687. .mmap = sysfs_bin_mmap,
  688. .open = sysfs_open_file,
  689. .release = sysfs_release,
  690. .poll = sysfs_poll,
  691. };
  692. int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
  693. const struct attribute *attr, int type,
  694. umode_t amode, const void *ns)
  695. {
  696. umode_t mode = (amode & S_IALLUGO) | S_IFREG;
  697. struct sysfs_addrm_cxt acxt;
  698. struct sysfs_dirent *sd;
  699. int rc;
  700. sd = sysfs_new_dirent(attr->name, mode, type);
  701. if (!sd)
  702. return -ENOMEM;
  703. sd->s_ns = ns;
  704. sd->s_attr.attr = (void *)attr;
  705. sysfs_dirent_init_lockdep(sd);
  706. sysfs_addrm_start(&acxt);
  707. rc = sysfs_add_one(&acxt, sd, dir_sd);
  708. sysfs_addrm_finish(&acxt);
  709. if (rc)
  710. sysfs_put(sd);
  711. return rc;
  712. }
  713. int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
  714. int type)
  715. {
  716. return sysfs_add_file_mode_ns(dir_sd, attr, type, attr->mode, NULL);
  717. }
  718. /**
  719. * sysfs_create_file_ns - create an attribute file for an object with custom ns
  720. * @kobj: object we're creating for
  721. * @attr: attribute descriptor
  722. * @ns: namespace the new file should belong to
  723. */
  724. int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
  725. const void *ns)
  726. {
  727. BUG_ON(!kobj || !kobj->sd || !attr);
  728. return sysfs_add_file_mode_ns(kobj->sd, attr, SYSFS_KOBJ_ATTR,
  729. attr->mode, ns);
  730. }
  731. EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
  732. int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
  733. {
  734. int err = 0;
  735. int i;
  736. for (i = 0; ptr[i] && !err; i++)
  737. err = sysfs_create_file(kobj, ptr[i]);
  738. if (err)
  739. while (--i >= 0)
  740. sysfs_remove_file(kobj, ptr[i]);
  741. return err;
  742. }
  743. EXPORT_SYMBOL_GPL(sysfs_create_files);
  744. /**
  745. * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
  746. * @kobj: object we're acting for.
  747. * @attr: attribute descriptor.
  748. * @group: group name.
  749. */
  750. int sysfs_add_file_to_group(struct kobject *kobj,
  751. const struct attribute *attr, const char *group)
  752. {
  753. struct sysfs_dirent *dir_sd;
  754. int error;
  755. if (group)
  756. dir_sd = sysfs_get_dirent(kobj->sd, group);
  757. else
  758. dir_sd = sysfs_get(kobj->sd);
  759. if (!dir_sd)
  760. return -ENOENT;
  761. error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR);
  762. sysfs_put(dir_sd);
  763. return error;
  764. }
  765. EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
  766. /**
  767. * sysfs_chmod_file - update the modified mode value on an object attribute.
  768. * @kobj: object we're acting for.
  769. * @attr: attribute descriptor.
  770. * @mode: file permissions.
  771. *
  772. */
  773. int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
  774. umode_t mode)
  775. {
  776. struct sysfs_dirent *sd;
  777. struct iattr newattrs;
  778. int rc;
  779. mutex_lock(&sysfs_mutex);
  780. rc = -ENOENT;
  781. sd = sysfs_find_dirent(kobj->sd, attr->name, NULL);
  782. if (!sd)
  783. goto out;
  784. newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO);
  785. newattrs.ia_valid = ATTR_MODE;
  786. rc = sysfs_sd_setattr(sd, &newattrs);
  787. out:
  788. mutex_unlock(&sysfs_mutex);
  789. return rc;
  790. }
  791. EXPORT_SYMBOL_GPL(sysfs_chmod_file);
  792. /**
  793. * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
  794. * @kobj: object we're acting for
  795. * @attr: attribute descriptor
  796. * @ns: namespace tag of the file to remove
  797. *
  798. * Hash the attribute name and namespace tag and kill the victim.
  799. */
  800. void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
  801. const void *ns)
  802. {
  803. struct sysfs_dirent *dir_sd = kobj->sd;
  804. sysfs_hash_and_remove(dir_sd, attr->name, ns);
  805. }
  806. EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
  807. void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
  808. {
  809. int i;
  810. for (i = 0; ptr[i]; i++)
  811. sysfs_remove_file(kobj, ptr[i]);
  812. }
  813. EXPORT_SYMBOL_GPL(sysfs_remove_files);
  814. /**
  815. * sysfs_remove_file_from_group - remove an attribute file from a group.
  816. * @kobj: object we're acting for.
  817. * @attr: attribute descriptor.
  818. * @group: group name.
  819. */
  820. void sysfs_remove_file_from_group(struct kobject *kobj,
  821. const struct attribute *attr, const char *group)
  822. {
  823. struct sysfs_dirent *dir_sd;
  824. if (group)
  825. dir_sd = sysfs_get_dirent(kobj->sd, group);
  826. else
  827. dir_sd = sysfs_get(kobj->sd);
  828. if (dir_sd) {
  829. sysfs_hash_and_remove(dir_sd, attr->name, NULL);
  830. sysfs_put(dir_sd);
  831. }
  832. }
  833. EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
  834. /**
  835. * sysfs_create_bin_file - create binary file for object.
  836. * @kobj: object.
  837. * @attr: attribute descriptor.
  838. */
  839. int sysfs_create_bin_file(struct kobject *kobj,
  840. const struct bin_attribute *attr)
  841. {
  842. BUG_ON(!kobj || !kobj->sd || !attr);
  843. return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
  844. }
  845. EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
  846. /**
  847. * sysfs_remove_bin_file - remove binary file for object.
  848. * @kobj: object.
  849. * @attr: attribute descriptor.
  850. */
  851. void sysfs_remove_bin_file(struct kobject *kobj,
  852. const struct bin_attribute *attr)
  853. {
  854. sysfs_hash_and_remove(kobj->sd, attr->attr.name, NULL);
  855. }
  856. EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
  857. struct sysfs_schedule_callback_struct {
  858. struct list_head workq_list;
  859. struct kobject *kobj;
  860. void (*func)(void *);
  861. void *data;
  862. struct module *owner;
  863. struct work_struct work;
  864. };
  865. static struct workqueue_struct *sysfs_workqueue;
  866. static DEFINE_MUTEX(sysfs_workq_mutex);
  867. static LIST_HEAD(sysfs_workq);
  868. static void sysfs_schedule_callback_work(struct work_struct *work)
  869. {
  870. struct sysfs_schedule_callback_struct *ss = container_of(work,
  871. struct sysfs_schedule_callback_struct, work);
  872. (ss->func)(ss->data);
  873. kobject_put(ss->kobj);
  874. module_put(ss->owner);
  875. mutex_lock(&sysfs_workq_mutex);
  876. list_del(&ss->workq_list);
  877. mutex_unlock(&sysfs_workq_mutex);
  878. kfree(ss);
  879. }
  880. /**
  881. * sysfs_schedule_callback - helper to schedule a callback for a kobject
  882. * @kobj: object we're acting for.
  883. * @func: callback function to invoke later.
  884. * @data: argument to pass to @func.
  885. * @owner: module owning the callback code
  886. *
  887. * sysfs attribute methods must not unregister themselves or their parent
  888. * kobject (which would amount to the same thing). Attempts to do so will
  889. * deadlock, since unregistration is mutually exclusive with driver
  890. * callbacks.
  891. *
  892. * Instead methods can call this routine, which will attempt to allocate
  893. * and schedule a workqueue request to call back @func with @data as its
  894. * argument in the workqueue's process context. @kobj will be pinned
  895. * until @func returns.
  896. *
  897. * Returns 0 if the request was submitted, -ENOMEM if storage could not
  898. * be allocated, -ENODEV if a reference to @owner isn't available,
  899. * -EAGAIN if a callback has already been scheduled for @kobj.
  900. */
  901. int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
  902. void *data, struct module *owner)
  903. {
  904. struct sysfs_schedule_callback_struct *ss, *tmp;
  905. if (!try_module_get(owner))
  906. return -ENODEV;
  907. mutex_lock(&sysfs_workq_mutex);
  908. list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
  909. if (ss->kobj == kobj) {
  910. module_put(owner);
  911. mutex_unlock(&sysfs_workq_mutex);
  912. return -EAGAIN;
  913. }
  914. mutex_unlock(&sysfs_workq_mutex);
  915. if (sysfs_workqueue == NULL) {
  916. sysfs_workqueue = create_singlethread_workqueue("sysfsd");
  917. if (sysfs_workqueue == NULL) {
  918. module_put(owner);
  919. return -ENOMEM;
  920. }
  921. }
  922. ss = kmalloc(sizeof(*ss), GFP_KERNEL);
  923. if (!ss) {
  924. module_put(owner);
  925. return -ENOMEM;
  926. }
  927. kobject_get(kobj);
  928. ss->kobj = kobj;
  929. ss->func = func;
  930. ss->data = data;
  931. ss->owner = owner;
  932. INIT_WORK(&ss->work, sysfs_schedule_callback_work);
  933. INIT_LIST_HEAD(&ss->workq_list);
  934. mutex_lock(&sysfs_workq_mutex);
  935. list_add_tail(&ss->workq_list, &sysfs_workq);
  936. mutex_unlock(&sysfs_workq_mutex);
  937. queue_work(sysfs_workqueue, &ss->work);
  938. return 0;
  939. }
  940. EXPORT_SYMBOL_GPL(sysfs_schedule_callback);