file.c 26 KB

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