file.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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 "kernfs-internal.h"
  17. /*
  18. * There's one kernfs_open_file for each open file and one kernfs_open_node
  19. * for each kernfs_node with one or more open files.
  20. *
  21. * kernfs_node->attr.open points to kernfs_open_node. attr.open is
  22. * protected by kernfs_open_node_lock.
  23. *
  24. * filp->private_data points to seq_file whose ->private points to
  25. * kernfs_open_file. kernfs_open_files are chained at
  26. * kernfs_open_node->files, which is protected by kernfs_open_file_mutex.
  27. */
  28. static DEFINE_SPINLOCK(kernfs_open_node_lock);
  29. static DEFINE_MUTEX(kernfs_open_file_mutex);
  30. struct kernfs_open_node {
  31. atomic_t refcnt;
  32. atomic_t event;
  33. wait_queue_head_t poll;
  34. struct list_head files; /* goes through kernfs_open_file.list */
  35. };
  36. static struct kernfs_open_file *kernfs_of(struct file *file)
  37. {
  38. return ((struct seq_file *)file->private_data)->private;
  39. }
  40. /*
  41. * Determine the kernfs_ops for the given kernfs_node. This function must
  42. * be called while holding an active reference.
  43. */
  44. static const struct kernfs_ops *kernfs_ops(struct kernfs_node *kn)
  45. {
  46. if (kn->flags & KERNFS_LOCKDEP)
  47. lockdep_assert_held(kn);
  48. return kn->attr.ops;
  49. }
  50. static void *kernfs_seq_start(struct seq_file *sf, loff_t *ppos)
  51. {
  52. struct kernfs_open_file *of = sf->private;
  53. const struct kernfs_ops *ops;
  54. /*
  55. * @of->mutex nests outside active ref and is just to ensure that
  56. * the ops aren't called concurrently for the same open file.
  57. */
  58. mutex_lock(&of->mutex);
  59. if (!sysfs_get_active(of->kn))
  60. return ERR_PTR(-ENODEV);
  61. ops = kernfs_ops(of->kn);
  62. if (ops->seq_start) {
  63. return ops->seq_start(sf, ppos);
  64. } else {
  65. /*
  66. * The same behavior and code as single_open(). Returns
  67. * !NULL if pos is at the beginning; otherwise, NULL.
  68. */
  69. return NULL + !*ppos;
  70. }
  71. }
  72. static void *kernfs_seq_next(struct seq_file *sf, void *v, loff_t *ppos)
  73. {
  74. struct kernfs_open_file *of = sf->private;
  75. const struct kernfs_ops *ops = kernfs_ops(of->kn);
  76. if (ops->seq_next) {
  77. return ops->seq_next(sf, v, ppos);
  78. } else {
  79. /*
  80. * The same behavior and code as single_open(), always
  81. * terminate after the initial read.
  82. */
  83. ++*ppos;
  84. return NULL;
  85. }
  86. }
  87. static void kernfs_seq_stop(struct seq_file *sf, void *v)
  88. {
  89. struct kernfs_open_file *of = sf->private;
  90. const struct kernfs_ops *ops = kernfs_ops(of->kn);
  91. if (ops->seq_stop)
  92. ops->seq_stop(sf, v);
  93. sysfs_put_active(of->kn);
  94. mutex_unlock(&of->mutex);
  95. }
  96. static int kernfs_seq_show(struct seq_file *sf, void *v)
  97. {
  98. struct kernfs_open_file *of = sf->private;
  99. of->event = atomic_read(&of->kn->attr.open->event);
  100. return of->kn->attr.ops->seq_show(sf, v);
  101. }
  102. static const struct seq_operations kernfs_seq_ops = {
  103. .start = kernfs_seq_start,
  104. .next = kernfs_seq_next,
  105. .stop = kernfs_seq_stop,
  106. .show = kernfs_seq_show,
  107. };
  108. /*
  109. * As reading a bin file can have side-effects, the exact offset and bytes
  110. * specified in read(2) call should be passed to the read callback making
  111. * it difficult to use seq_file. Implement simplistic custom buffering for
  112. * bin files.
  113. */
  114. static ssize_t kernfs_file_direct_read(struct kernfs_open_file *of,
  115. char __user *user_buf, size_t count,
  116. loff_t *ppos)
  117. {
  118. ssize_t len = min_t(size_t, count, PAGE_SIZE);
  119. const struct kernfs_ops *ops;
  120. char *buf;
  121. buf = kmalloc(len, GFP_KERNEL);
  122. if (!buf)
  123. return -ENOMEM;
  124. /*
  125. * @of->mutex nests outside active ref and is just to ensure that
  126. * the ops aren't called concurrently for the same open file.
  127. */
  128. mutex_lock(&of->mutex);
  129. if (!sysfs_get_active(of->kn)) {
  130. len = -ENODEV;
  131. mutex_unlock(&of->mutex);
  132. goto out_free;
  133. }
  134. ops = kernfs_ops(of->kn);
  135. if (ops->read)
  136. len = ops->read(of, buf, len, *ppos);
  137. else
  138. len = -EINVAL;
  139. sysfs_put_active(of->kn);
  140. mutex_unlock(&of->mutex);
  141. if (len < 0)
  142. goto out_free;
  143. if (copy_to_user(user_buf, buf, len)) {
  144. len = -EFAULT;
  145. goto out_free;
  146. }
  147. *ppos += len;
  148. out_free:
  149. kfree(buf);
  150. return len;
  151. }
  152. /**
  153. * kernfs_file_read - kernfs vfs read callback
  154. * @file: file pointer
  155. * @user_buf: data to write
  156. * @count: number of bytes
  157. * @ppos: starting offset
  158. */
  159. static ssize_t kernfs_file_read(struct file *file, char __user *user_buf,
  160. size_t count, loff_t *ppos)
  161. {
  162. struct kernfs_open_file *of = kernfs_of(file);
  163. if (of->kn->flags & KERNFS_HAS_SEQ_SHOW)
  164. return seq_read(file, user_buf, count, ppos);
  165. else
  166. return kernfs_file_direct_read(of, user_buf, count, ppos);
  167. }
  168. /**
  169. * kernfs_file_write - kernfs vfs write callback
  170. * @file: file pointer
  171. * @user_buf: data to write
  172. * @count: number of bytes
  173. * @ppos: starting offset
  174. *
  175. * Copy data in from userland and pass it to the matching kernfs write
  176. * operation.
  177. *
  178. * There is no easy way for us to know if userspace is only doing a partial
  179. * write, so we don't support them. We expect the entire buffer to come on
  180. * the first write. Hint: if you're writing a value, first read the file,
  181. * modify only the the value you're changing, then write entire buffer
  182. * back.
  183. */
  184. static ssize_t kernfs_file_write(struct file *file, const char __user *user_buf,
  185. size_t count, loff_t *ppos)
  186. {
  187. struct kernfs_open_file *of = kernfs_of(file);
  188. ssize_t len = min_t(size_t, count, PAGE_SIZE);
  189. const struct kernfs_ops *ops;
  190. char *buf;
  191. buf = kmalloc(len + 1, GFP_KERNEL);
  192. if (!buf)
  193. return -ENOMEM;
  194. if (copy_from_user(buf, user_buf, len)) {
  195. len = -EFAULT;
  196. goto out_free;
  197. }
  198. buf[len] = '\0'; /* guarantee string termination */
  199. /*
  200. * @of->mutex nests outside active ref and is just to ensure that
  201. * the ops aren't called concurrently for the same open file.
  202. */
  203. mutex_lock(&of->mutex);
  204. if (!sysfs_get_active(of->kn)) {
  205. mutex_unlock(&of->mutex);
  206. len = -ENODEV;
  207. goto out_free;
  208. }
  209. ops = kernfs_ops(of->kn);
  210. if (ops->write)
  211. len = ops->write(of, buf, len, *ppos);
  212. else
  213. len = -EINVAL;
  214. sysfs_put_active(of->kn);
  215. mutex_unlock(&of->mutex);
  216. if (len > 0)
  217. *ppos += len;
  218. out_free:
  219. kfree(buf);
  220. return len;
  221. }
  222. static void kernfs_vma_open(struct vm_area_struct *vma)
  223. {
  224. struct file *file = vma->vm_file;
  225. struct kernfs_open_file *of = kernfs_of(file);
  226. if (!of->vm_ops)
  227. return;
  228. if (!sysfs_get_active(of->kn))
  229. return;
  230. if (of->vm_ops->open)
  231. of->vm_ops->open(vma);
  232. sysfs_put_active(of->kn);
  233. }
  234. static int kernfs_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  235. {
  236. struct file *file = vma->vm_file;
  237. struct kernfs_open_file *of = kernfs_of(file);
  238. int ret;
  239. if (!of->vm_ops)
  240. return VM_FAULT_SIGBUS;
  241. if (!sysfs_get_active(of->kn))
  242. return VM_FAULT_SIGBUS;
  243. ret = VM_FAULT_SIGBUS;
  244. if (of->vm_ops->fault)
  245. ret = of->vm_ops->fault(vma, vmf);
  246. sysfs_put_active(of->kn);
  247. return ret;
  248. }
  249. static int kernfs_vma_page_mkwrite(struct vm_area_struct *vma,
  250. struct vm_fault *vmf)
  251. {
  252. struct file *file = vma->vm_file;
  253. struct kernfs_open_file *of = kernfs_of(file);
  254. int ret;
  255. if (!of->vm_ops)
  256. return VM_FAULT_SIGBUS;
  257. if (!sysfs_get_active(of->kn))
  258. return VM_FAULT_SIGBUS;
  259. ret = 0;
  260. if (of->vm_ops->page_mkwrite)
  261. ret = of->vm_ops->page_mkwrite(vma, vmf);
  262. else
  263. file_update_time(file);
  264. sysfs_put_active(of->kn);
  265. return ret;
  266. }
  267. static int kernfs_vma_access(struct vm_area_struct *vma, unsigned long addr,
  268. void *buf, int len, int write)
  269. {
  270. struct file *file = vma->vm_file;
  271. struct kernfs_open_file *of = kernfs_of(file);
  272. int ret;
  273. if (!of->vm_ops)
  274. return -EINVAL;
  275. if (!sysfs_get_active(of->kn))
  276. return -EINVAL;
  277. ret = -EINVAL;
  278. if (of->vm_ops->access)
  279. ret = of->vm_ops->access(vma, addr, buf, len, write);
  280. sysfs_put_active(of->kn);
  281. return ret;
  282. }
  283. #ifdef CONFIG_NUMA
  284. static int kernfs_vma_set_policy(struct vm_area_struct *vma,
  285. struct mempolicy *new)
  286. {
  287. struct file *file = vma->vm_file;
  288. struct kernfs_open_file *of = kernfs_of(file);
  289. int ret;
  290. if (!of->vm_ops)
  291. return 0;
  292. if (!sysfs_get_active(of->kn))
  293. return -EINVAL;
  294. ret = 0;
  295. if (of->vm_ops->set_policy)
  296. ret = of->vm_ops->set_policy(vma, new);
  297. sysfs_put_active(of->kn);
  298. return ret;
  299. }
  300. static struct mempolicy *kernfs_vma_get_policy(struct vm_area_struct *vma,
  301. unsigned long addr)
  302. {
  303. struct file *file = vma->vm_file;
  304. struct kernfs_open_file *of = kernfs_of(file);
  305. struct mempolicy *pol;
  306. if (!of->vm_ops)
  307. return vma->vm_policy;
  308. if (!sysfs_get_active(of->kn))
  309. return vma->vm_policy;
  310. pol = vma->vm_policy;
  311. if (of->vm_ops->get_policy)
  312. pol = of->vm_ops->get_policy(vma, addr);
  313. sysfs_put_active(of->kn);
  314. return pol;
  315. }
  316. static int kernfs_vma_migrate(struct vm_area_struct *vma,
  317. const nodemask_t *from, const nodemask_t *to,
  318. unsigned long flags)
  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 0;
  325. if (!sysfs_get_active(of->kn))
  326. return 0;
  327. ret = 0;
  328. if (of->vm_ops->migrate)
  329. ret = of->vm_ops->migrate(vma, from, to, flags);
  330. sysfs_put_active(of->kn);
  331. return ret;
  332. }
  333. #endif
  334. static const struct vm_operations_struct kernfs_vm_ops = {
  335. .open = kernfs_vma_open,
  336. .fault = kernfs_vma_fault,
  337. .page_mkwrite = kernfs_vma_page_mkwrite,
  338. .access = kernfs_vma_access,
  339. #ifdef CONFIG_NUMA
  340. .set_policy = kernfs_vma_set_policy,
  341. .get_policy = kernfs_vma_get_policy,
  342. .migrate = kernfs_vma_migrate,
  343. #endif
  344. };
  345. static int kernfs_file_mmap(struct file *file, struct vm_area_struct *vma)
  346. {
  347. struct kernfs_open_file *of = kernfs_of(file);
  348. const struct kernfs_ops *ops;
  349. int rc;
  350. /*
  351. * mmap path and of->mutex are prone to triggering spurious lockdep
  352. * warnings and we don't want to add spurious locking dependency
  353. * between the two. Check whether mmap is actually implemented
  354. * without grabbing @of->mutex by testing HAS_MMAP flag. See the
  355. * comment in kernfs_file_open() for more details.
  356. */
  357. if (!(of->kn->flags & KERNFS_HAS_MMAP))
  358. return -ENODEV;
  359. mutex_lock(&of->mutex);
  360. rc = -ENODEV;
  361. if (!sysfs_get_active(of->kn))
  362. goto out_unlock;
  363. ops = kernfs_ops(of->kn);
  364. rc = ops->mmap(of, vma);
  365. /*
  366. * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
  367. * to satisfy versions of X which crash if the mmap fails: that
  368. * substitutes a new vm_file, and we don't then want bin_vm_ops.
  369. */
  370. if (vma->vm_file != file)
  371. goto out_put;
  372. rc = -EINVAL;
  373. if (of->mmapped && of->vm_ops != vma->vm_ops)
  374. goto out_put;
  375. /*
  376. * It is not possible to successfully wrap close.
  377. * So error if someone is trying to use close.
  378. */
  379. rc = -EINVAL;
  380. if (vma->vm_ops && vma->vm_ops->close)
  381. goto out_put;
  382. rc = 0;
  383. of->mmapped = 1;
  384. of->vm_ops = vma->vm_ops;
  385. vma->vm_ops = &kernfs_vm_ops;
  386. out_put:
  387. sysfs_put_active(of->kn);
  388. out_unlock:
  389. mutex_unlock(&of->mutex);
  390. return rc;
  391. }
  392. /**
  393. * sysfs_get_open_dirent - get or create kernfs_open_node
  394. * @kn: target kernfs_node
  395. * @of: kernfs_open_file for this instance of open
  396. *
  397. * If @kn->attr.open exists, increment its reference count; otherwise,
  398. * create one. @of is chained to the files list.
  399. *
  400. * LOCKING:
  401. * Kernel thread context (may sleep).
  402. *
  403. * RETURNS:
  404. * 0 on success, -errno on failure.
  405. */
  406. static int sysfs_get_open_dirent(struct kernfs_node *kn,
  407. struct kernfs_open_file *of)
  408. {
  409. struct kernfs_open_node *on, *new_on = NULL;
  410. retry:
  411. mutex_lock(&kernfs_open_file_mutex);
  412. spin_lock_irq(&kernfs_open_node_lock);
  413. if (!kn->attr.open && new_on) {
  414. kn->attr.open = new_on;
  415. new_on = NULL;
  416. }
  417. on = kn->attr.open;
  418. if (on) {
  419. atomic_inc(&on->refcnt);
  420. list_add_tail(&of->list, &on->files);
  421. }
  422. spin_unlock_irq(&kernfs_open_node_lock);
  423. mutex_unlock(&kernfs_open_file_mutex);
  424. if (on) {
  425. kfree(new_on);
  426. return 0;
  427. }
  428. /* not there, initialize a new one and retry */
  429. new_on = kmalloc(sizeof(*new_on), GFP_KERNEL);
  430. if (!new_on)
  431. return -ENOMEM;
  432. atomic_set(&new_on->refcnt, 0);
  433. atomic_set(&new_on->event, 1);
  434. init_waitqueue_head(&new_on->poll);
  435. INIT_LIST_HEAD(&new_on->files);
  436. goto retry;
  437. }
  438. /**
  439. * sysfs_put_open_dirent - put kernfs_open_node
  440. * @kn: target kernfs_nodet
  441. * @of: associated kernfs_open_file
  442. *
  443. * Put @kn->attr.open and unlink @of from the files list. If
  444. * reference count reaches zero, disassociate and free it.
  445. *
  446. * LOCKING:
  447. * None.
  448. */
  449. static void sysfs_put_open_dirent(struct kernfs_node *kn,
  450. struct kernfs_open_file *of)
  451. {
  452. struct kernfs_open_node *on = kn->attr.open;
  453. unsigned long flags;
  454. mutex_lock(&kernfs_open_file_mutex);
  455. spin_lock_irqsave(&kernfs_open_node_lock, flags);
  456. if (of)
  457. list_del(&of->list);
  458. if (atomic_dec_and_test(&on->refcnt))
  459. kn->attr.open = NULL;
  460. else
  461. on = NULL;
  462. spin_unlock_irqrestore(&kernfs_open_node_lock, flags);
  463. mutex_unlock(&kernfs_open_file_mutex);
  464. kfree(on);
  465. }
  466. static int kernfs_file_open(struct inode *inode, struct file *file)
  467. {
  468. struct kernfs_node *kn = file->f_path.dentry->d_fsdata;
  469. const struct kernfs_ops *ops;
  470. struct kernfs_open_file *of;
  471. bool has_read, has_write, has_mmap;
  472. int error = -EACCES;
  473. if (!sysfs_get_active(kn))
  474. return -ENODEV;
  475. ops = kernfs_ops(kn);
  476. has_read = ops->seq_show || ops->read || ops->mmap;
  477. has_write = ops->write || ops->mmap;
  478. has_mmap = ops->mmap;
  479. /* check perms and supported operations */
  480. if ((file->f_mode & FMODE_WRITE) &&
  481. (!(inode->i_mode & S_IWUGO) || !has_write))
  482. goto err_out;
  483. if ((file->f_mode & FMODE_READ) &&
  484. (!(inode->i_mode & S_IRUGO) || !has_read))
  485. goto err_out;
  486. /* allocate a kernfs_open_file for the file */
  487. error = -ENOMEM;
  488. of = kzalloc(sizeof(struct kernfs_open_file), GFP_KERNEL);
  489. if (!of)
  490. goto err_out;
  491. /*
  492. * The following is done to give a different lockdep key to
  493. * @of->mutex for files which implement mmap. This is a rather
  494. * crude way to avoid false positive lockdep warning around
  495. * mm->mmap_sem - mmap nests @of->mutex under mm->mmap_sem and
  496. * reading /sys/block/sda/trace/act_mask grabs sr_mutex, under
  497. * which mm->mmap_sem nests, while holding @of->mutex. As each
  498. * open file has a separate mutex, it's okay as long as those don't
  499. * happen on the same file. At this point, we can't easily give
  500. * each file a separate locking class. Let's differentiate on
  501. * whether the file has mmap or not for now.
  502. *
  503. * Both paths of the branch look the same. They're supposed to
  504. * look that way and give @of->mutex different static lockdep keys.
  505. */
  506. if (has_mmap)
  507. mutex_init(&of->mutex);
  508. else
  509. mutex_init(&of->mutex);
  510. of->kn = kn;
  511. of->file = file;
  512. /*
  513. * Always instantiate seq_file even if read access doesn't use
  514. * seq_file or is not requested. This unifies private data access
  515. * and readable regular files are the vast majority anyway.
  516. */
  517. if (ops->seq_show)
  518. error = seq_open(file, &kernfs_seq_ops);
  519. else
  520. error = seq_open(file, NULL);
  521. if (error)
  522. goto err_free;
  523. ((struct seq_file *)file->private_data)->private = of;
  524. /* seq_file clears PWRITE unconditionally, restore it if WRITE */
  525. if (file->f_mode & FMODE_WRITE)
  526. file->f_mode |= FMODE_PWRITE;
  527. /* make sure we have open dirent struct */
  528. error = sysfs_get_open_dirent(kn, of);
  529. if (error)
  530. goto err_close;
  531. /* open succeeded, put active references */
  532. sysfs_put_active(kn);
  533. return 0;
  534. err_close:
  535. seq_release(inode, file);
  536. err_free:
  537. kfree(of);
  538. err_out:
  539. sysfs_put_active(kn);
  540. return error;
  541. }
  542. static int kernfs_file_release(struct inode *inode, struct file *filp)
  543. {
  544. struct kernfs_node *kn = filp->f_path.dentry->d_fsdata;
  545. struct kernfs_open_file *of = kernfs_of(filp);
  546. sysfs_put_open_dirent(kn, of);
  547. seq_release(inode, filp);
  548. kfree(of);
  549. return 0;
  550. }
  551. void sysfs_unmap_bin_file(struct kernfs_node *kn)
  552. {
  553. struct kernfs_open_node *on;
  554. struct kernfs_open_file *of;
  555. if (!(kn->flags & KERNFS_HAS_MMAP))
  556. return;
  557. spin_lock_irq(&kernfs_open_node_lock);
  558. on = kn->attr.open;
  559. if (on)
  560. atomic_inc(&on->refcnt);
  561. spin_unlock_irq(&kernfs_open_node_lock);
  562. if (!on)
  563. return;
  564. mutex_lock(&kernfs_open_file_mutex);
  565. list_for_each_entry(of, &on->files, list) {
  566. struct inode *inode = file_inode(of->file);
  567. unmap_mapping_range(inode->i_mapping, 0, 0, 1);
  568. }
  569. mutex_unlock(&kernfs_open_file_mutex);
  570. sysfs_put_open_dirent(kn, NULL);
  571. }
  572. /* Sysfs attribute files are pollable. The idea is that you read
  573. * the content and then you use 'poll' or 'select' to wait for
  574. * the content to change. When the content changes (assuming the
  575. * manager for the kobject supports notification), poll will
  576. * return POLLERR|POLLPRI, and select will return the fd whether
  577. * it is waiting for read, write, or exceptions.
  578. * Once poll/select indicates that the value has changed, you
  579. * need to close and re-open the file, or seek to 0 and read again.
  580. * Reminder: this only works for attributes which actively support
  581. * it, and it is not possible to test an attribute from userspace
  582. * to see if it supports poll (Neither 'poll' nor 'select' return
  583. * an appropriate error code). When in doubt, set a suitable timeout value.
  584. */
  585. static unsigned int kernfs_file_poll(struct file *filp, poll_table *wait)
  586. {
  587. struct kernfs_open_file *of = kernfs_of(filp);
  588. struct kernfs_node *kn = filp->f_path.dentry->d_fsdata;
  589. struct kernfs_open_node *on = kn->attr.open;
  590. /* need parent for the kobj, grab both */
  591. if (!sysfs_get_active(kn))
  592. goto trigger;
  593. poll_wait(filp, &on->poll, wait);
  594. sysfs_put_active(kn);
  595. if (of->event != atomic_read(&on->event))
  596. goto trigger;
  597. return DEFAULT_POLLMASK;
  598. trigger:
  599. return DEFAULT_POLLMASK|POLLERR|POLLPRI;
  600. }
  601. /**
  602. * kernfs_notify - notify a kernfs file
  603. * @kn: file to notify
  604. *
  605. * Notify @kn such that poll(2) on @kn wakes up.
  606. */
  607. void kernfs_notify(struct kernfs_node *kn)
  608. {
  609. struct kernfs_open_node *on;
  610. unsigned long flags;
  611. spin_lock_irqsave(&kernfs_open_node_lock, flags);
  612. if (!WARN_ON(kernfs_type(kn) != KERNFS_FILE)) {
  613. on = kn->attr.open;
  614. if (on) {
  615. atomic_inc(&on->event);
  616. wake_up_interruptible(&on->poll);
  617. }
  618. }
  619. spin_unlock_irqrestore(&kernfs_open_node_lock, flags);
  620. }
  621. EXPORT_SYMBOL_GPL(kernfs_notify);
  622. const struct file_operations kernfs_file_operations = {
  623. .read = kernfs_file_read,
  624. .write = kernfs_file_write,
  625. .llseek = generic_file_llseek,
  626. .mmap = kernfs_file_mmap,
  627. .open = kernfs_file_open,
  628. .release = kernfs_file_release,
  629. .poll = kernfs_file_poll,
  630. };
  631. /**
  632. * kernfs_create_file_ns_key - create a file
  633. * @parent: directory to create the file in
  634. * @name: name of the file
  635. * @mode: mode of the file
  636. * @size: size of the file
  637. * @ops: kernfs operations for the file
  638. * @priv: private data for the file
  639. * @ns: optional namespace tag of the file
  640. * @key: lockdep key for the file's active_ref, %NULL to disable lockdep
  641. *
  642. * Returns the created node on success, ERR_PTR() value on error.
  643. */
  644. struct kernfs_node *kernfs_create_file_ns_key(struct kernfs_node *parent,
  645. const char *name,
  646. umode_t mode, loff_t size,
  647. const struct kernfs_ops *ops,
  648. void *priv, const void *ns,
  649. struct lock_class_key *key)
  650. {
  651. struct kernfs_addrm_cxt acxt;
  652. struct kernfs_node *kn;
  653. int rc;
  654. kn = sysfs_new_dirent(kernfs_root(parent), name,
  655. (mode & S_IALLUGO) | S_IFREG, KERNFS_FILE);
  656. if (!kn)
  657. return ERR_PTR(-ENOMEM);
  658. kn->attr.ops = ops;
  659. kn->attr.size = size;
  660. kn->ns = ns;
  661. kn->priv = priv;
  662. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  663. if (key) {
  664. lockdep_init_map(&kn->dep_map, "s_active", key, 0);
  665. kn->flags |= KERNFS_LOCKDEP;
  666. }
  667. #endif
  668. /*
  669. * kn->attr.ops is accesible only while holding active ref. We
  670. * need to know whether some ops are implemented outside active
  671. * ref. Cache their existence in flags.
  672. */
  673. if (ops->seq_show)
  674. kn->flags |= KERNFS_HAS_SEQ_SHOW;
  675. if (ops->mmap)
  676. kn->flags |= KERNFS_HAS_MMAP;
  677. sysfs_addrm_start(&acxt);
  678. rc = sysfs_add_one(&acxt, kn, parent);
  679. sysfs_addrm_finish(&acxt);
  680. if (rc) {
  681. kernfs_put(kn);
  682. return ERR_PTR(rc);
  683. }
  684. return kn;
  685. }