thread_with_file.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _BCACHEFS_THREAD_WITH_FILE_H
  3. #define _BCACHEFS_THREAD_WITH_FILE_H
  4. #include "thread_with_file_types.h"
  5. /*
  6. * Thread with file: Run a kthread and connect it to a file descriptor, so that
  7. * it can be interacted with via fd read/write methods and closing the file
  8. * descriptor stops the kthread.
  9. *
  10. * We have two different APIs:
  11. *
  12. * thread_with_file, the low level version.
  13. * You get to define the full file_operations, including your release function,
  14. * which means that you must call bch2_thread_with_file_exit() from your
  15. * .release method
  16. *
  17. * thread_with_stdio, the higher level version
  18. * This implements full piping of input and output, including .poll.
  19. *
  20. * Notes on behaviour:
  21. * - kthread shutdown behaves like writing or reading from a pipe that has been
  22. * closed
  23. * - Input and output buffers are 4096 bytes, although buffers may in some
  24. * situations slightly exceed that limit so as to avoid chopping off a
  25. * message in the middle in nonblocking mode.
  26. * - Input/output buffers are lazily allocated, with GFP_NOWAIT allocations -
  27. * should be fine but might change in future revisions.
  28. * - Output buffer may grow past 4096 bytes to deal with messages that are
  29. * bigger than 4096 bytes
  30. * - Writing may be done blocking or nonblocking; in nonblocking mode, we only
  31. * drop entire messages.
  32. *
  33. * To write, use stdio_redirect_printf()
  34. * To read, use stdio_redirect_read() or stdio_redirect_readline()
  35. */
  36. struct task_struct;
  37. struct thread_with_file {
  38. struct task_struct *task;
  39. int ret;
  40. bool done;
  41. };
  42. void bch2_thread_with_file_exit(struct thread_with_file *);
  43. int bch2_run_thread_with_file(struct thread_with_file *,
  44. const struct file_operations *,
  45. int (*fn)(void *));
  46. struct thread_with_stdio;
  47. struct thread_with_stdio_ops {
  48. void (*exit)(struct thread_with_stdio *);
  49. int (*fn)(struct thread_with_stdio *);
  50. long (*unlocked_ioctl)(struct thread_with_stdio *, unsigned int, unsigned long);
  51. };
  52. struct thread_with_stdio {
  53. struct thread_with_file thr;
  54. struct stdio_redirect stdio;
  55. const struct thread_with_stdio_ops *ops;
  56. };
  57. void bch2_thread_with_stdio_init(struct thread_with_stdio *,
  58. const struct thread_with_stdio_ops *);
  59. int __bch2_run_thread_with_stdio(struct thread_with_stdio *);
  60. int bch2_run_thread_with_stdio(struct thread_with_stdio *,
  61. const struct thread_with_stdio_ops *);
  62. int bch2_run_thread_with_stdout(struct thread_with_stdio *,
  63. const struct thread_with_stdio_ops *);
  64. int bch2_stdio_redirect_read(struct stdio_redirect *, char *, size_t);
  65. int bch2_stdio_redirect_readline_timeout(struct stdio_redirect *, darray_char *, unsigned long);
  66. int bch2_stdio_redirect_readline(struct stdio_redirect *, darray_char *);
  67. __printf(3, 0) ssize_t bch2_stdio_redirect_vprintf(struct stdio_redirect *, bool, const char *, va_list);
  68. __printf(3, 4) ssize_t bch2_stdio_redirect_printf(struct stdio_redirect *, bool, const char *, ...);
  69. #endif /* _BCACHEFS_THREAD_WITH_FILE_H */