file.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. /*
  2. * fs/kernfs/file.c - kernfs file implementation
  3. *
  4. * Copyright (c) 2001-3 Patrick Mochel
  5. * Copyright (c) 2007 SUSE Linux Products GmbH
  6. * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
  7. *
  8. * This file is released under the GPLv2.
  9. */
  10. #include <linux/fs.h>
  11. #include <linux/seq_file.h>
  12. #include <linux/slab.h>
  13. #include <linux/poll.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/sched/mm.h>
  16. #include <linux/fsnotify.h>
  17. #include "kernfs-internal.h"
  18. /*
  19. * There's one kernfs_open_file for each open file and one kernfs_open_node
  20. * for each kernfs_node with one or more open files.
  21. *
  22. * kernfs_node->attr.open points to kernfs_open_node. attr.open is
  23. * protected by kernfs_open_node_lock.
  24. *
  25. * filp->private_data points to seq_file whose ->private points to
  26. * kernfs_open_file. kernfs_open_files are chained at
  27. * kernfs_open_node->files, which is protected by kernfs_open_file_mutex.
  28. */
  29. static DEFINE_SPINLOCK(kernfs_open_node_lock);
  30. static DEFINE_MUTEX(kernfs_open_file_mutex);
  31. struct kernfs_open_node {
  32. atomic_t refcnt;
  33. atomic_t event;
  34. wait_queue_head_t poll;
  35. struct list_head files; /* goes through kernfs_open_file.list */
  36. };
  37. /*
  38. * kernfs_notify() may be called from any context and bounces notifications
  39. * through a work item. To minimize space overhead in kernfs_node, the
  40. * pending queue is implemented as a singly linked list of kernfs_nodes.
  41. * The list is terminated with the self pointer so that whether a
  42. * kernfs_node is on the list or not can be determined by testing the next
  43. * pointer for NULL.
  44. */
  45. #define KERNFS_NOTIFY_EOL ((void *)&kernfs_notify_list)
  46. static DEFINE_SPINLOCK(kernfs_notify_lock);
  47. static struct kernfs_node *kernfs_notify_list = KERNFS_NOTIFY_EOL;
  48. static struct kernfs_open_file *kernfs_of(struct file *file)
  49. {
  50. return ((struct seq_file *)file->private_data)->private;
  51. }
  52. /*
  53. * Determine the kernfs_ops for the given kernfs_node. This function must
  54. * be called while holding an active reference.
  55. */
  56. static const struct kernfs_ops *kernfs_ops(struct kernfs_node *kn)
  57. {
  58. if (kn->flags & KERNFS_LOCKDEP)
  59. lockdep_assert_held(kn);
  60. return kn->attr.ops;
  61. }
  62. /*
  63. * As kernfs_seq_stop() is also called after kernfs_seq_start() or
  64. * kernfs_seq_next() failure, it needs to distinguish whether it's stopping
  65. * a seq_file iteration which is fully initialized with an active reference
  66. * or an aborted kernfs_seq_start() due to get_active failure. The
  67. * position pointer is the only context for each seq_file iteration and
  68. * thus the stop condition should be encoded in it. As the return value is
  69. * directly visible to userland, ERR_PTR(-ENODEV) is the only acceptable
  70. * choice to indicate get_active failure.
  71. *
  72. * Unfortunately, this is complicated due to the optional custom seq_file
  73. * operations which may return ERR_PTR(-ENODEV) too. kernfs_seq_stop()
  74. * can't distinguish whether ERR_PTR(-ENODEV) is from get_active failure or
  75. * custom seq_file operations and thus can't decide whether put_active
  76. * should be performed or not only on ERR_PTR(-ENODEV).
  77. *
  78. * This is worked around by factoring out the custom seq_stop() and
  79. * put_active part into kernfs_seq_stop_active(), skipping it from
  80. * kernfs_seq_stop() if ERR_PTR(-ENODEV) while invoking it directly after
  81. * custom seq_file operations fail with ERR_PTR(-ENODEV) - this ensures
  82. * that kernfs_seq_stop_active() is skipped only after get_active failure.
  83. */
  84. static void kernfs_seq_stop_active(struct seq_file *sf, void *v)
  85. {
  86. struct kernfs_open_file *of = sf->private;
  87. const struct kernfs_ops *ops = kernfs_ops(of->kn);
  88. if (ops->seq_stop)
  89. ops->seq_stop(sf, v);
  90. kernfs_put_active(of->kn);
  91. }
  92. static void *kernfs_seq_start(struct seq_file *sf, loff_t *ppos)
  93. {
  94. struct kernfs_open_file *of = sf->private;
  95. const struct kernfs_ops *ops;
  96. /*
  97. * @of->mutex nests outside active ref and is primarily to ensure that
  98. * the ops aren't called concurrently for the same open file.
  99. */
  100. mutex_lock(&of->mutex);
  101. if (!kernfs_get_active(of->kn))
  102. return ERR_PTR(-ENODEV);
  103. ops = kernfs_ops(of->kn);
  104. if (ops->seq_start) {
  105. void *next = ops->seq_start(sf, ppos);
  106. /* see the comment above kernfs_seq_stop_active() */
  107. if (next == ERR_PTR(-ENODEV))
  108. kernfs_seq_stop_active(sf, next);
  109. return next;
  110. } else {
  111. /*
  112. * The same behavior and code as single_open(). Returns
  113. * !NULL if pos is at the beginning; otherwise, NULL.
  114. */
  115. return NULL + !*ppos;
  116. }
  117. }
  118. static void *kernfs_seq_next(struct seq_file *sf, void *v, loff_t *ppos)
  119. {
  120. struct kernfs_open_file *of = sf->private;
  121. const struct kernfs_ops *ops = kernfs_ops(of->kn);
  122. if (ops->seq_next) {
  123. void *next = ops->seq_next(sf, v, ppos);
  124. /* see the comment above kernfs_seq_stop_active() */
  125. if (next == ERR_PTR(-ENODEV))
  126. kernfs_seq_stop_active(sf, next);
  127. return next;
  128. } else {
  129. /*
  130. * The same behavior and code as single_open(), always
  131. * terminate after the initial read.
  132. */
  133. ++*ppos;
  134. return NULL;
  135. }
  136. }
  137. static void kernfs_seq_stop(struct seq_file *sf, void *v)
  138. {
  139. struct kernfs_open_file *of = sf->private;
  140. if (v != ERR_PTR(-ENODEV))
  141. kernfs_seq_stop_active(sf, v);
  142. mutex_unlock(&of->mutex);
  143. }
  144. static int kernfs_seq_show(struct seq_file *sf, void *v)
  145. {
  146. struct kernfs_open_file *of = sf->private;
  147. of->event = atomic_read(&of->kn->attr.open->event);
  148. return of->kn->attr.ops->seq_show(sf, v);
  149. }
  150. static const struct seq_operations kernfs_seq_ops = {
  151. .start = kernfs_seq_start,
  152. .next = kernfs_seq_next,
  153. .stop = kernfs_seq_stop,
  154. .show = kernfs_seq_show,
  155. };
  156. /*
  157. * As reading a bin file can have side-effects, the exact offset and bytes
  158. * specified in read(2) call should be passed to the read callback making
  159. * it difficult to use seq_file. Implement simplistic custom buffering for
  160. * bin files.
  161. */
  162. static ssize_t kernfs_file_direct_read(struct kernfs_open_file *of,
  163. char __user *user_buf, size_t count,
  164. loff_t *ppos)
  165. {
  166. ssize_t len = min_t(size_t, count, PAGE_SIZE);
  167. const struct kernfs_ops *ops;
  168. char *buf;
  169. buf = of->prealloc_buf;
  170. if (buf)
  171. mutex_lock(&of->prealloc_mutex);
  172. else
  173. buf = kmalloc(len, GFP_KERNEL);
  174. if (!buf)
  175. return -ENOMEM;
  176. /*
  177. * @of->mutex nests outside active ref and is used both to ensure that
  178. * the ops aren't called concurrently for the same open file.
  179. */
  180. mutex_lock(&of->mutex);
  181. if (!kernfs_get_active(of->kn)) {
  182. len = -ENODEV;
  183. mutex_unlock(&of->mutex);
  184. goto out_free;
  185. }
  186. of->event = atomic_read(&of->kn->attr.open->event);
  187. ops = kernfs_ops(of->kn);
  188. if (ops->read)
  189. len = ops->read(of, buf, len, *ppos);
  190. else
  191. len = -EINVAL;
  192. kernfs_put_active(of->kn);
  193. mutex_unlock(&of->mutex);
  194. if (len < 0)
  195. goto out_free;
  196. if (copy_to_user(user_buf, buf, len)) {
  197. len = -EFAULT;
  198. goto out_free;
  199. }
  200. *ppos += len;
  201. out_free:
  202. if (buf == of->prealloc_buf)
  203. mutex_unlock(&of->prealloc_mutex);
  204. else
  205. kfree(buf);
  206. return len;
  207. }
  208. /**
  209. * kernfs_fop_read - kernfs vfs read callback
  210. * @file: file pointer
  211. * @user_buf: data to write
  212. * @count: number of bytes
  213. * @ppos: starting offset
  214. */
  215. static ssize_t kernfs_fop_read(struct file *file, char __user *user_buf,
  216. size_t count, loff_t *ppos)
  217. {
  218. struct kernfs_open_file *of = kernfs_of(file);
  219. if (of->kn->flags & KERNFS_HAS_SEQ_SHOW)
  220. return seq_read(file, user_buf, count, ppos);
  221. else
  222. return kernfs_file_direct_read(of, user_buf, count, ppos);
  223. }
  224. /**
  225. * kernfs_fop_write - kernfs vfs write callback
  226. * @file: file pointer
  227. * @user_buf: data to write
  228. * @count: number of bytes
  229. * @ppos: starting offset
  230. *
  231. * Copy data in from userland and pass it to the matching kernfs write
  232. * operation.
  233. *
  234. * There is no easy way for us to know if userspace is only doing a partial
  235. * write, so we don't support them. We expect the entire buffer to come on
  236. * the first write. Hint: if you're writing a value, first read the file,
  237. * modify only the the value you're changing, then write entire buffer
  238. * back.
  239. */
  240. static ssize_t kernfs_fop_write(struct file *file, const char __user *user_buf,
  241. size_t count, loff_t *ppos)
  242. {
  243. struct kernfs_open_file *of = kernfs_of(file);
  244. const struct kernfs_ops *ops;
  245. ssize_t len;
  246. char *buf;
  247. if (of->atomic_write_len) {
  248. len = count;
  249. if (len > of->atomic_write_len)
  250. return -E2BIG;
  251. } else {
  252. len = min_t(size_t, count, PAGE_SIZE);
  253. }
  254. buf = of->prealloc_buf;
  255. if (buf)
  256. mutex_lock(&of->prealloc_mutex);
  257. else
  258. buf = kmalloc(len + 1, GFP_KERNEL);
  259. if (!buf)
  260. return -ENOMEM;
  261. if (copy_from_user(buf, user_buf, len)) {
  262. len = -EFAULT;
  263. goto out_free;
  264. }
  265. buf[len] = '\0'; /* guarantee string termination */
  266. /*
  267. * @of->mutex nests outside active ref and is used both to ensure that
  268. * the ops aren't called concurrently for the same open file.
  269. */
  270. mutex_lock(&of->mutex);
  271. if (!kernfs_get_active(of->kn)) {
  272. mutex_unlock(&of->mutex);
  273. len = -ENODEV;
  274. goto out_free;
  275. }
  276. ops = kernfs_ops(of->kn);
  277. if (ops->write)
  278. len = ops->write(of, buf, len, *ppos);
  279. else
  280. len = -EINVAL;
  281. kernfs_put_active(of->kn);
  282. mutex_unlock(&of->mutex);
  283. if (len > 0)
  284. *ppos += len;
  285. out_free:
  286. if (buf == of->prealloc_buf)
  287. mutex_unlock(&of->prealloc_mutex);
  288. else
  289. kfree(buf);
  290. return len;
  291. }
  292. static void kernfs_vma_open(struct vm_area_struct *vma)
  293. {
  294. struct file *file = vma->vm_file;
  295. struct kernfs_open_file *of = kernfs_of(file);
  296. if (!of->vm_ops)
  297. return;
  298. if (!kernfs_get_active(of->kn))
  299. return;
  300. if (of->vm_ops->open)
  301. of->vm_ops->open(vma);
  302. kernfs_put_active(of->kn);
  303. }
  304. static vm_fault_t kernfs_vma_fault(struct vm_fault *vmf)
  305. {
  306. struct file *file = vmf->vma->vm_file;
  307. struct kernfs_open_file *of = kernfs_of(file);
  308. vm_fault_t ret;
  309. if (!of->vm_ops)
  310. return VM_FAULT_SIGBUS;
  311. if (!kernfs_get_active(of->kn))
  312. return VM_FAULT_SIGBUS;
  313. ret = VM_FAULT_SIGBUS;
  314. if (of->vm_ops->fault)
  315. ret = of->vm_ops->fault(vmf);
  316. kernfs_put_active(of->kn);
  317. return ret;
  318. }
  319. static vm_fault_t kernfs_vma_page_mkwrite(struct vm_fault *vmf)
  320. {
  321. struct file *file = vmf->vma->vm_file;
  322. struct kernfs_open_file *of = kernfs_of(file);
  323. vm_fault_t ret;
  324. if (!of->vm_ops)
  325. return VM_FAULT_SIGBUS;
  326. if (!kernfs_get_active(of->kn))
  327. return VM_FAULT_SIGBUS;
  328. ret = 0;
  329. if (of->vm_ops->page_mkwrite)
  330. ret = of->vm_ops->page_mkwrite(vmf);
  331. else
  332. file_update_time(file);
  333. kernfs_put_active(of->kn);
  334. return ret;
  335. }
  336. static int kernfs_vma_access(struct vm_area_struct *vma, unsigned long addr,
  337. void *buf, int len, int write)
  338. {
  339. struct file *file = vma->vm_file;
  340. struct kernfs_open_file *of = kernfs_of(file);
  341. int ret;
  342. if (!of->vm_ops)
  343. return -EINVAL;
  344. if (!kernfs_get_active(of->kn))
  345. return -EINVAL;
  346. ret = -EINVAL;
  347. if (of->vm_ops->access)
  348. ret = of->vm_ops->access(vma, addr, buf, len, write);
  349. kernfs_put_active(of->kn);
  350. return ret;
  351. }
  352. #ifdef CONFIG_NUMA
  353. static int kernfs_vma_set_policy(struct vm_area_struct *vma,
  354. struct mempolicy *new)
  355. {
  356. struct file *file = vma->vm_file;
  357. struct kernfs_open_file *of = kernfs_of(file);
  358. int ret;
  359. if (!of->vm_ops)
  360. return 0;
  361. if (!kernfs_get_active(of->kn))
  362. return -EINVAL;
  363. ret = 0;
  364. if (of->vm_ops->set_policy)
  365. ret = of->vm_ops->set_policy(vma, new);
  366. kernfs_put_active(of->kn);
  367. return ret;
  368. }
  369. static struct mempolicy *kernfs_vma_get_policy(struct vm_area_struct *vma,
  370. unsigned long addr)
  371. {
  372. struct file *file = vma->vm_file;
  373. struct kernfs_open_file *of = kernfs_of(file);
  374. struct mempolicy *pol;
  375. if (!of->vm_ops)
  376. return vma->vm_policy;
  377. if (!kernfs_get_active(of->kn))
  378. return vma->vm_policy;
  379. pol = vma->vm_policy;
  380. if (of->vm_ops->get_policy)
  381. pol = of->vm_ops->get_policy(vma, addr);
  382. kernfs_put_active(of->kn);
  383. return pol;
  384. }
  385. #endif
  386. static const struct vm_operations_struct kernfs_vm_ops = {
  387. .open = kernfs_vma_open,
  388. .fault = kernfs_vma_fault,
  389. .page_mkwrite = kernfs_vma_page_mkwrite,
  390. .access = kernfs_vma_access,
  391. #ifdef CONFIG_NUMA
  392. .set_policy = kernfs_vma_set_policy,
  393. .get_policy = kernfs_vma_get_policy,
  394. #endif
  395. };
  396. static int kernfs_fop_mmap(struct file *file, struct vm_area_struct *vma)
  397. {
  398. struct kernfs_open_file *of = kernfs_of(file);
  399. const struct kernfs_ops *ops;
  400. int rc;
  401. /*
  402. * mmap path and of->mutex are prone to triggering spurious lockdep
  403. * warnings and we don't want to add spurious locking dependency
  404. * between the two. Check whether mmap is actually implemented
  405. * without grabbing @of->mutex by testing HAS_MMAP flag. See the
  406. * comment in kernfs_file_open() for more details.
  407. */
  408. if (!(of->kn->flags & KERNFS_HAS_MMAP))
  409. return -ENODEV;
  410. mutex_lock(&of->mutex);
  411. rc = -ENODEV;
  412. if (!kernfs_get_active(of->kn))
  413. goto out_unlock;
  414. ops = kernfs_ops(of->kn);
  415. rc = ops->mmap(of, vma);
  416. if (rc)
  417. goto out_put;
  418. /*
  419. * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
  420. * to satisfy versions of X which crash if the mmap fails: that
  421. * substitutes a new vm_file, and we don't then want bin_vm_ops.
  422. */
  423. if (vma->vm_file != file)
  424. goto out_put;
  425. rc = -EINVAL;
  426. if (of->mmapped && of->vm_ops != vma->vm_ops)
  427. goto out_put;
  428. /*
  429. * It is not possible to successfully wrap close.
  430. * So error if someone is trying to use close.
  431. */
  432. rc = -EINVAL;
  433. if (vma->vm_ops && vma->vm_ops->close)
  434. goto out_put;
  435. rc = 0;
  436. of->mmapped = true;
  437. of->vm_ops = vma->vm_ops;
  438. vma->vm_ops = &kernfs_vm_ops;
  439. out_put:
  440. kernfs_put_active(of->kn);
  441. out_unlock:
  442. mutex_unlock(&of->mutex);
  443. return rc;
  444. }
  445. /**
  446. * kernfs_get_open_node - get or create kernfs_open_node
  447. * @kn: target kernfs_node
  448. * @of: kernfs_open_file for this instance of open
  449. *
  450. * If @kn->attr.open exists, increment its reference count; otherwise,
  451. * create one. @of is chained to the files list.
  452. *
  453. * LOCKING:
  454. * Kernel thread context (may sleep).
  455. *
  456. * RETURNS:
  457. * 0 on success, -errno on failure.
  458. */
  459. static int kernfs_get_open_node(struct kernfs_node *kn,
  460. struct kernfs_open_file *of)
  461. {
  462. struct kernfs_open_node *on, *new_on = NULL;
  463. retry:
  464. mutex_lock(&kernfs_open_file_mutex);
  465. spin_lock_irq(&kernfs_open_node_lock);
  466. if (!kn->attr.open && new_on) {
  467. kn->attr.open = new_on;
  468. new_on = NULL;
  469. }
  470. on = kn->attr.open;
  471. if (on) {
  472. atomic_inc(&on->refcnt);
  473. list_add_tail(&of->list, &on->files);
  474. }
  475. spin_unlock_irq(&kernfs_open_node_lock);
  476. mutex_unlock(&kernfs_open_file_mutex);
  477. if (on) {
  478. kfree(new_on);
  479. return 0;
  480. }
  481. /* not there, initialize a new one and retry */
  482. new_on = kmalloc(sizeof(*new_on), GFP_KERNEL);
  483. if (!new_on)
  484. return -ENOMEM;
  485. atomic_set(&new_on->refcnt, 0);
  486. atomic_set(&new_on->event, 1);
  487. init_waitqueue_head(&new_on->poll);
  488. INIT_LIST_HEAD(&new_on->files);
  489. goto retry;
  490. }
  491. /**
  492. * kernfs_put_open_node - put kernfs_open_node
  493. * @kn: target kernfs_nodet
  494. * @of: associated kernfs_open_file
  495. *
  496. * Put @kn->attr.open and unlink @of from the files list. If
  497. * reference count reaches zero, disassociate and free it.
  498. *
  499. * LOCKING:
  500. * None.
  501. */
  502. static void kernfs_put_open_node(struct kernfs_node *kn,
  503. struct kernfs_open_file *of)
  504. {
  505. struct kernfs_open_node *on = kn->attr.open;
  506. unsigned long flags;
  507. mutex_lock(&kernfs_open_file_mutex);
  508. spin_lock_irqsave(&kernfs_open_node_lock, flags);
  509. if (of)
  510. list_del(&of->list);
  511. if (atomic_dec_and_test(&on->refcnt))
  512. kn->attr.open = NULL;
  513. else
  514. on = NULL;
  515. spin_unlock_irqrestore(&kernfs_open_node_lock, flags);
  516. mutex_unlock(&kernfs_open_file_mutex);
  517. kfree(on);
  518. }
  519. static int kernfs_fop_open(struct inode *inode, struct file *file)
  520. {
  521. struct kernfs_node *kn = inode->i_private;
  522. struct kernfs_root *root = kernfs_root(kn);
  523. const struct kernfs_ops *ops;
  524. struct kernfs_open_file *of;
  525. bool has_read, has_write, has_mmap;
  526. int error = -EACCES;
  527. if (!kernfs_get_active(kn))
  528. return -ENODEV;
  529. ops = kernfs_ops(kn);
  530. has_read = ops->seq_show || ops->read || ops->mmap;
  531. has_write = ops->write || ops->mmap;
  532. has_mmap = ops->mmap;
  533. /* see the flag definition for details */
  534. if (root->flags & KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK) {
  535. if ((file->f_mode & FMODE_WRITE) &&
  536. (!(inode->i_mode & S_IWUGO) || !has_write))
  537. goto err_out;
  538. if ((file->f_mode & FMODE_READ) &&
  539. (!(inode->i_mode & S_IRUGO) || !has_read))
  540. goto err_out;
  541. }
  542. /* allocate a kernfs_open_file for the file */
  543. error = -ENOMEM;
  544. of = kzalloc(sizeof(struct kernfs_open_file), GFP_KERNEL);
  545. if (!of)
  546. goto err_out;
  547. /*
  548. * The following is done to give a different lockdep key to
  549. * @of->mutex for files which implement mmap. This is a rather
  550. * crude way to avoid false positive lockdep warning around
  551. * mm->mmap_sem - mmap nests @of->mutex under mm->mmap_sem and
  552. * reading /sys/block/sda/trace/act_mask grabs sr_mutex, under
  553. * which mm->mmap_sem nests, while holding @of->mutex. As each
  554. * open file has a separate mutex, it's okay as long as those don't
  555. * happen on the same file. At this point, we can't easily give
  556. * each file a separate locking class. Let's differentiate on
  557. * whether the file has mmap or not for now.
  558. *
  559. * Both paths of the branch look the same. They're supposed to
  560. * look that way and give @of->mutex different static lockdep keys.
  561. */
  562. if (has_mmap)
  563. mutex_init(&of->mutex);
  564. else
  565. mutex_init(&of->mutex);
  566. of->kn = kn;
  567. of->file = file;
  568. /*
  569. * Write path needs to atomic_write_len outside active reference.
  570. * Cache it in open_file. See kernfs_fop_write() for details.
  571. */
  572. of->atomic_write_len = ops->atomic_write_len;
  573. error = -EINVAL;
  574. /*
  575. * ->seq_show is incompatible with ->prealloc,
  576. * as seq_read does its own allocation.
  577. * ->read must be used instead.
  578. */
  579. if (ops->prealloc && ops->seq_show)
  580. goto err_free;
  581. if (ops->prealloc) {
  582. int len = of->atomic_write_len ?: PAGE_SIZE;
  583. of->prealloc_buf = kmalloc(len + 1, GFP_KERNEL);
  584. error = -ENOMEM;
  585. if (!of->prealloc_buf)
  586. goto err_free;
  587. mutex_init(&of->prealloc_mutex);
  588. }
  589. /*
  590. * Always instantiate seq_file even if read access doesn't use
  591. * seq_file or is not requested. This unifies private data access
  592. * and readable regular files are the vast majority anyway.
  593. */
  594. if (ops->seq_show)
  595. error = seq_open(file, &kernfs_seq_ops);
  596. else
  597. error = seq_open(file, NULL);
  598. if (error)
  599. goto err_free;
  600. of->seq_file = file->private_data;
  601. of->seq_file->private = of;
  602. /* seq_file clears PWRITE unconditionally, restore it if WRITE */
  603. if (file->f_mode & FMODE_WRITE)
  604. file->f_mode |= FMODE_PWRITE;
  605. /* make sure we have open node struct */
  606. error = kernfs_get_open_node(kn, of);
  607. if (error)
  608. goto err_seq_release;
  609. if (ops->open) {
  610. /* nobody has access to @of yet, skip @of->mutex */
  611. error = ops->open(of);
  612. if (error)
  613. goto err_put_node;
  614. }
  615. /* open succeeded, put active references */
  616. kernfs_put_active(kn);
  617. return 0;
  618. err_put_node:
  619. kernfs_put_open_node(kn, of);
  620. err_seq_release:
  621. seq_release(inode, file);
  622. err_free:
  623. kfree(of->prealloc_buf);
  624. kfree(of);
  625. err_out:
  626. kernfs_put_active(kn);
  627. return error;
  628. }
  629. /* used from release/drain to ensure that ->release() is called exactly once */
  630. static void kernfs_release_file(struct kernfs_node *kn,
  631. struct kernfs_open_file *of)
  632. {
  633. /*
  634. * @of is guaranteed to have no other file operations in flight and
  635. * we just want to synchronize release and drain paths.
  636. * @kernfs_open_file_mutex is enough. @of->mutex can't be used
  637. * here because drain path may be called from places which can
  638. * cause circular dependency.
  639. */
  640. lockdep_assert_held(&kernfs_open_file_mutex);
  641. if (!of->released) {
  642. /*
  643. * A file is never detached without being released and we
  644. * need to be able to release files which are deactivated
  645. * and being drained. Don't use kernfs_ops().
  646. */
  647. kn->attr.ops->release(of);
  648. of->released = true;
  649. }
  650. }
  651. static int kernfs_fop_release(struct inode *inode, struct file *filp)
  652. {
  653. struct kernfs_node *kn = inode->i_private;
  654. struct kernfs_open_file *of = kernfs_of(filp);
  655. if (kn->flags & KERNFS_HAS_RELEASE) {
  656. mutex_lock(&kernfs_open_file_mutex);
  657. kernfs_release_file(kn, of);
  658. mutex_unlock(&kernfs_open_file_mutex);
  659. }
  660. kernfs_put_open_node(kn, of);
  661. seq_release(inode, filp);
  662. kfree(of->prealloc_buf);
  663. kfree(of);
  664. return 0;
  665. }
  666. void kernfs_drain_open_files(struct kernfs_node *kn)
  667. {
  668. struct kernfs_open_node *on;
  669. struct kernfs_open_file *of;
  670. if (!(kn->flags & (KERNFS_HAS_MMAP | KERNFS_HAS_RELEASE)))
  671. return;
  672. spin_lock_irq(&kernfs_open_node_lock);
  673. on = kn->attr.open;
  674. if (on)
  675. atomic_inc(&on->refcnt);
  676. spin_unlock_irq(&kernfs_open_node_lock);
  677. if (!on)
  678. return;
  679. mutex_lock(&kernfs_open_file_mutex);
  680. list_for_each_entry(of, &on->files, list) {
  681. struct inode *inode = file_inode(of->file);
  682. if (kn->flags & KERNFS_HAS_MMAP)
  683. unmap_mapping_range(inode->i_mapping, 0, 0, 1);
  684. if (kn->flags & KERNFS_HAS_RELEASE)
  685. kernfs_release_file(kn, of);
  686. }
  687. mutex_unlock(&kernfs_open_file_mutex);
  688. kernfs_put_open_node(kn, NULL);
  689. }
  690. /*
  691. * Kernfs attribute files are pollable. The idea is that you read
  692. * the content and then you use 'poll' or 'select' to wait for
  693. * the content to change. When the content changes (assuming the
  694. * manager for the kobject supports notification), poll will
  695. * return EPOLLERR|EPOLLPRI, and select will return the fd whether
  696. * it is waiting for read, write, or exceptions.
  697. * Once poll/select indicates that the value has changed, you
  698. * need to close and re-open the file, or seek to 0 and read again.
  699. * Reminder: this only works for attributes which actively support
  700. * it, and it is not possible to test an attribute from userspace
  701. * to see if it supports poll (Neither 'poll' nor 'select' return
  702. * an appropriate error code). When in doubt, set a suitable timeout value.
  703. */
  704. static __poll_t kernfs_fop_poll(struct file *filp, poll_table *wait)
  705. {
  706. struct kernfs_open_file *of = kernfs_of(filp);
  707. struct kernfs_node *kn = kernfs_dentry_node(filp->f_path.dentry);
  708. struct kernfs_open_node *on = kn->attr.open;
  709. if (!kernfs_get_active(kn))
  710. goto trigger;
  711. poll_wait(filp, &on->poll, wait);
  712. kernfs_put_active(kn);
  713. if (of->event != atomic_read(&on->event))
  714. goto trigger;
  715. return DEFAULT_POLLMASK;
  716. trigger:
  717. return DEFAULT_POLLMASK|EPOLLERR|EPOLLPRI;
  718. }
  719. static void kernfs_notify_workfn(struct work_struct *work)
  720. {
  721. struct kernfs_node *kn;
  722. struct kernfs_open_node *on;
  723. struct kernfs_super_info *info;
  724. repeat:
  725. /* pop one off the notify_list */
  726. spin_lock_irq(&kernfs_notify_lock);
  727. kn = kernfs_notify_list;
  728. if (kn == KERNFS_NOTIFY_EOL) {
  729. spin_unlock_irq(&kernfs_notify_lock);
  730. return;
  731. }
  732. kernfs_notify_list = kn->attr.notify_next;
  733. kn->attr.notify_next = NULL;
  734. spin_unlock_irq(&kernfs_notify_lock);
  735. /* kick poll */
  736. spin_lock_irq(&kernfs_open_node_lock);
  737. on = kn->attr.open;
  738. if (on) {
  739. atomic_inc(&on->event);
  740. wake_up_interruptible(&on->poll);
  741. }
  742. spin_unlock_irq(&kernfs_open_node_lock);
  743. /* kick fsnotify */
  744. mutex_lock(&kernfs_mutex);
  745. list_for_each_entry(info, &kernfs_root(kn)->supers, node) {
  746. struct kernfs_node *parent;
  747. struct inode *inode;
  748. /*
  749. * We want fsnotify_modify() on @kn but as the
  750. * modifications aren't originating from userland don't
  751. * have the matching @file available. Look up the inodes
  752. * and generate the events manually.
  753. */
  754. inode = ilookup(info->sb, kn->id.ino);
  755. if (!inode)
  756. continue;
  757. parent = kernfs_get_parent(kn);
  758. if (parent) {
  759. struct inode *p_inode;
  760. p_inode = ilookup(info->sb, parent->id.ino);
  761. if (p_inode) {
  762. fsnotify(p_inode, FS_MODIFY | FS_EVENT_ON_CHILD,
  763. inode, FSNOTIFY_EVENT_INODE, kn->name, 0);
  764. iput(p_inode);
  765. }
  766. kernfs_put(parent);
  767. }
  768. fsnotify(inode, FS_MODIFY, inode, FSNOTIFY_EVENT_INODE,
  769. kn->name, 0);
  770. iput(inode);
  771. }
  772. mutex_unlock(&kernfs_mutex);
  773. kernfs_put(kn);
  774. goto repeat;
  775. }
  776. /**
  777. * kernfs_notify - notify a kernfs file
  778. * @kn: file to notify
  779. *
  780. * Notify @kn such that poll(2) on @kn wakes up. Maybe be called from any
  781. * context.
  782. */
  783. void kernfs_notify(struct kernfs_node *kn)
  784. {
  785. static DECLARE_WORK(kernfs_notify_work, kernfs_notify_workfn);
  786. unsigned long flags;
  787. if (WARN_ON(kernfs_type(kn) != KERNFS_FILE))
  788. return;
  789. spin_lock_irqsave(&kernfs_notify_lock, flags);
  790. if (!kn->attr.notify_next) {
  791. kernfs_get(kn);
  792. kn->attr.notify_next = kernfs_notify_list;
  793. kernfs_notify_list = kn;
  794. schedule_work(&kernfs_notify_work);
  795. }
  796. spin_unlock_irqrestore(&kernfs_notify_lock, flags);
  797. }
  798. EXPORT_SYMBOL_GPL(kernfs_notify);
  799. const struct file_operations kernfs_file_fops = {
  800. .read = kernfs_fop_read,
  801. .write = kernfs_fop_write,
  802. .llseek = generic_file_llseek,
  803. .mmap = kernfs_fop_mmap,
  804. .open = kernfs_fop_open,
  805. .release = kernfs_fop_release,
  806. .poll = kernfs_fop_poll,
  807. .fsync = noop_fsync,
  808. };
  809. /**
  810. * __kernfs_create_file - kernfs internal function to create a file
  811. * @parent: directory to create the file in
  812. * @name: name of the file
  813. * @mode: mode of the file
  814. * @uid: uid of the file
  815. * @gid: gid of the file
  816. * @size: size of the file
  817. * @ops: kernfs operations for the file
  818. * @priv: private data for the file
  819. * @ns: optional namespace tag of the file
  820. * @key: lockdep key for the file's active_ref, %NULL to disable lockdep
  821. *
  822. * Returns the created node on success, ERR_PTR() value on error.
  823. */
  824. struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent,
  825. const char *name,
  826. umode_t mode, kuid_t uid, kgid_t gid,
  827. loff_t size,
  828. const struct kernfs_ops *ops,
  829. void *priv, const void *ns,
  830. struct lock_class_key *key)
  831. {
  832. struct kernfs_node *kn;
  833. unsigned flags;
  834. int rc;
  835. flags = KERNFS_FILE;
  836. kn = kernfs_new_node(parent, name, (mode & S_IALLUGO) | S_IFREG,
  837. uid, gid, flags);
  838. if (!kn)
  839. return ERR_PTR(-ENOMEM);
  840. kn->attr.ops = ops;
  841. kn->attr.size = size;
  842. kn->ns = ns;
  843. kn->priv = priv;
  844. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  845. if (key) {
  846. lockdep_init_map(&kn->dep_map, "kn->count", key, 0);
  847. kn->flags |= KERNFS_LOCKDEP;
  848. }
  849. #endif
  850. /*
  851. * kn->attr.ops is accesible only while holding active ref. We
  852. * need to know whether some ops are implemented outside active
  853. * ref. Cache their existence in flags.
  854. */
  855. if (ops->seq_show)
  856. kn->flags |= KERNFS_HAS_SEQ_SHOW;
  857. if (ops->mmap)
  858. kn->flags |= KERNFS_HAS_MMAP;
  859. if (ops->release)
  860. kn->flags |= KERNFS_HAS_RELEASE;
  861. rc = kernfs_add_one(kn);
  862. if (rc) {
  863. kernfs_put(kn);
  864. return ERR_PTR(rc);
  865. }
  866. return kn;
  867. }