file.c 24 KB

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