watch_queue.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* User-mappable watch queue
  3. *
  4. * Copyright (C) 2020 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells ([email protected])
  6. *
  7. * See Documentation/core-api/watch_queue.rst
  8. */
  9. #ifndef _LINUX_WATCH_QUEUE_H
  10. #define _LINUX_WATCH_QUEUE_H
  11. #include <uapi/linux/watch_queue.h>
  12. #include <linux/kref.h>
  13. #include <linux/rcupdate.h>
  14. #ifdef CONFIG_WATCH_QUEUE
  15. struct cred;
  16. struct watch_type_filter {
  17. enum watch_notification_type type;
  18. __u32 subtype_filter[1]; /* Bitmask of subtypes to filter on */
  19. __u32 info_filter; /* Filter on watch_notification::info */
  20. __u32 info_mask; /* Mask of relevant bits in info_filter */
  21. };
  22. struct watch_filter {
  23. union {
  24. struct rcu_head rcu;
  25. /* Bitmask of accepted types */
  26. DECLARE_BITMAP(type_filter, WATCH_TYPE__NR);
  27. };
  28. u32 nr_filters; /* Number of filters */
  29. struct watch_type_filter filters[];
  30. };
  31. struct watch_queue {
  32. struct rcu_head rcu;
  33. struct watch_filter __rcu *filter;
  34. struct pipe_inode_info *pipe; /* Pipe we use as a buffer, NULL if queue closed */
  35. struct hlist_head watches; /* Contributory watches */
  36. struct page **notes; /* Preallocated notifications */
  37. unsigned long *notes_bitmap; /* Allocation bitmap for notes */
  38. struct kref usage; /* Object usage count */
  39. spinlock_t lock;
  40. unsigned int nr_notes; /* Number of notes */
  41. unsigned int nr_pages; /* Number of pages in notes[] */
  42. };
  43. /*
  44. * Representation of a watch on an object.
  45. */
  46. struct watch {
  47. union {
  48. struct rcu_head rcu;
  49. u32 info_id; /* ID to be OR'd in to info field */
  50. };
  51. struct watch_queue __rcu *queue; /* Queue to post events to */
  52. struct hlist_node queue_node; /* Link in queue->watches */
  53. struct watch_list __rcu *watch_list;
  54. struct hlist_node list_node; /* Link in watch_list->watchers */
  55. const struct cred *cred; /* Creds of the owner of the watch */
  56. void *private; /* Private data for the watched object */
  57. u64 id; /* Internal identifier */
  58. struct kref usage; /* Object usage count */
  59. };
  60. /*
  61. * List of watches on an object.
  62. */
  63. struct watch_list {
  64. struct rcu_head rcu;
  65. struct hlist_head watchers;
  66. void (*release_watch)(struct watch *);
  67. spinlock_t lock;
  68. };
  69. extern void __post_watch_notification(struct watch_list *,
  70. struct watch_notification *,
  71. const struct cred *,
  72. u64);
  73. extern struct watch_queue *get_watch_queue(int);
  74. extern void put_watch_queue(struct watch_queue *);
  75. extern void init_watch(struct watch *, struct watch_queue *);
  76. extern int add_watch_to_object(struct watch *, struct watch_list *);
  77. extern int remove_watch_from_object(struct watch_list *, struct watch_queue *, u64, bool);
  78. extern long watch_queue_set_size(struct pipe_inode_info *, unsigned int);
  79. extern long watch_queue_set_filter(struct pipe_inode_info *,
  80. struct watch_notification_filter __user *);
  81. extern int watch_queue_init(struct pipe_inode_info *);
  82. extern void watch_queue_clear(struct watch_queue *);
  83. static inline void init_watch_list(struct watch_list *wlist,
  84. void (*release_watch)(struct watch *))
  85. {
  86. INIT_HLIST_HEAD(&wlist->watchers);
  87. spin_lock_init(&wlist->lock);
  88. wlist->release_watch = release_watch;
  89. }
  90. static inline void post_watch_notification(struct watch_list *wlist,
  91. struct watch_notification *n,
  92. const struct cred *cred,
  93. u64 id)
  94. {
  95. if (unlikely(wlist))
  96. __post_watch_notification(wlist, n, cred, id);
  97. }
  98. static inline void remove_watch_list(struct watch_list *wlist, u64 id)
  99. {
  100. if (wlist) {
  101. remove_watch_from_object(wlist, NULL, id, true);
  102. kfree_rcu(wlist, rcu);
  103. }
  104. }
  105. /**
  106. * watch_sizeof - Calculate the information part of the size of a watch record,
  107. * given the structure size.
  108. */
  109. #define watch_sizeof(STRUCT) (sizeof(STRUCT) << WATCH_INFO_LENGTH__SHIFT)
  110. #else
  111. static inline int watch_queue_init(struct pipe_inode_info *pipe)
  112. {
  113. return -ENOPKG;
  114. }
  115. #endif
  116. #endif /* _LINUX_WATCH_QUEUE_H */