file.c 30 KB

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