notifier.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Routines to manage notifier chains for passing status changes to any
  3. * interested routines. We need this instead of hard coded call lists so
  4. * that modules can poke their nose into the innards. The network devices
  5. * needed them so here they are for the rest of you.
  6. *
  7. * Alan Cox <Alan.Cox@linux.org>
  8. */
  9. #ifndef _LINUX_NOTIFIER_H
  10. #define _LINUX_NOTIFIER_H
  11. #include <linux/errno.h>
  12. #include <linux/mutex.h>
  13. #include <linux/rwsem.h>
  14. /*
  15. * Notifier chains are of three types:
  16. *
  17. * Atomic notifier chains: Chain callbacks run in interrupt/atomic
  18. * context. Callouts are not allowed to block.
  19. * Blocking notifier chains: Chain callbacks run in process context.
  20. * Callouts are allowed to block.
  21. * Raw notifier chains: There are no restrictions on callbacks,
  22. * registration, or unregistration. All locking and protection
  23. * must be provided by the caller.
  24. *
  25. * atomic_notifier_chain_register() may be called from an atomic context,
  26. * but blocking_notifier_chain_register() must be called from a process
  27. * context. Ditto for the corresponding _unregister() routines.
  28. *
  29. * atomic_notifier_chain_unregister() and blocking_notifier_chain_unregister()
  30. * _must not_ be called from within the call chain.
  31. */
  32. struct notifier_block {
  33. int (*notifier_call)(struct notifier_block *, unsigned long, void *);
  34. struct notifier_block *next;
  35. int priority;
  36. };
  37. struct atomic_notifier_head {
  38. spinlock_t lock;
  39. struct notifier_block *head;
  40. };
  41. struct blocking_notifier_head {
  42. struct rw_semaphore rwsem;
  43. struct notifier_block *head;
  44. };
  45. struct raw_notifier_head {
  46. struct notifier_block *head;
  47. };
  48. #define ATOMIC_INIT_NOTIFIER_HEAD(name) do { \
  49. spin_lock_init(&(name)->lock); \
  50. (name)->head = NULL; \
  51. } while (0)
  52. #define BLOCKING_INIT_NOTIFIER_HEAD(name) do { \
  53. init_rwsem(&(name)->rwsem); \
  54. (name)->head = NULL; \
  55. } while (0)
  56. #define RAW_INIT_NOTIFIER_HEAD(name) do { \
  57. (name)->head = NULL; \
  58. } while (0)
  59. #define ATOMIC_NOTIFIER_INIT(name) { \
  60. .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
  61. .head = NULL }
  62. #define BLOCKING_NOTIFIER_INIT(name) { \
  63. .rwsem = __RWSEM_INITIALIZER((name).rwsem), \
  64. .head = NULL }
  65. #define RAW_NOTIFIER_INIT(name) { \
  66. .head = NULL }
  67. #define ATOMIC_NOTIFIER_HEAD(name) \
  68. struct atomic_notifier_head name = \
  69. ATOMIC_NOTIFIER_INIT(name)
  70. #define BLOCKING_NOTIFIER_HEAD(name) \
  71. struct blocking_notifier_head name = \
  72. BLOCKING_NOTIFIER_INIT(name)
  73. #define RAW_NOTIFIER_HEAD(name) \
  74. struct raw_notifier_head name = \
  75. RAW_NOTIFIER_INIT(name)
  76. #ifdef __KERNEL__
  77. extern int atomic_notifier_chain_register(struct atomic_notifier_head *,
  78. struct notifier_block *);
  79. extern int blocking_notifier_chain_register(struct blocking_notifier_head *,
  80. struct notifier_block *);
  81. extern int raw_notifier_chain_register(struct raw_notifier_head *,
  82. struct notifier_block *);
  83. extern int atomic_notifier_chain_unregister(struct atomic_notifier_head *,
  84. struct notifier_block *);
  85. extern int blocking_notifier_chain_unregister(struct blocking_notifier_head *,
  86. struct notifier_block *);
  87. extern int raw_notifier_chain_unregister(struct raw_notifier_head *,
  88. struct notifier_block *);
  89. extern int atomic_notifier_call_chain(struct atomic_notifier_head *,
  90. unsigned long val, void *v);
  91. extern int blocking_notifier_call_chain(struct blocking_notifier_head *,
  92. unsigned long val, void *v);
  93. extern int raw_notifier_call_chain(struct raw_notifier_head *,
  94. unsigned long val, void *v);
  95. #define NOTIFY_DONE 0x0000 /* Don't care */
  96. #define NOTIFY_OK 0x0001 /* Suits me */
  97. #define NOTIFY_STOP_MASK 0x8000 /* Don't call further */
  98. #define NOTIFY_BAD (NOTIFY_STOP_MASK|0x0002)
  99. /* Bad/Veto action */
  100. /*
  101. * Clean way to return from the notifier and stop further calls.
  102. */
  103. #define NOTIFY_STOP (NOTIFY_OK|NOTIFY_STOP_MASK)
  104. /*
  105. * Declared notifiers so far. I can imagine quite a few more chains
  106. * over time (eg laptop power reset chains, reboot chain (to clean
  107. * device units up), device [un]mount chain, module load/unload chain,
  108. * low memory chain, screenblank chain (for plug in modular screenblankers)
  109. * VC switch chains (for loadable kernel svgalib VC switch helpers) etc...
  110. */
  111. /* netdevice notifier chain */
  112. #define NETDEV_UP 0x0001 /* For now you can't veto a device up/down */
  113. #define NETDEV_DOWN 0x0002
  114. #define NETDEV_REBOOT 0x0003 /* Tell a protocol stack a network interface
  115. detected a hardware crash and restarted
  116. - we can use this eg to kick tcp sessions
  117. once done */
  118. #define NETDEV_CHANGE 0x0004 /* Notify device state change */
  119. #define NETDEV_REGISTER 0x0005
  120. #define NETDEV_UNREGISTER 0x0006
  121. #define NETDEV_CHANGEMTU 0x0007
  122. #define NETDEV_CHANGEADDR 0x0008
  123. #define NETDEV_GOING_DOWN 0x0009
  124. #define NETDEV_CHANGENAME 0x000A
  125. #define NETDEV_FEAT_CHANGE 0x000B
  126. #define SYS_DOWN 0x0001 /* Notify of system down */
  127. #define SYS_RESTART SYS_DOWN
  128. #define SYS_HALT 0x0002 /* Notify of system halt */
  129. #define SYS_POWER_OFF 0x0003 /* Notify of system power off */
  130. #define NETLINK_URELEASE 0x0001 /* Unicast netlink socket released */
  131. #define CPU_ONLINE 0x0002 /* CPU (unsigned)v is up */
  132. #define CPU_UP_PREPARE 0x0003 /* CPU (unsigned)v coming up */
  133. #define CPU_UP_CANCELED 0x0004 /* CPU (unsigned)v NOT coming up */
  134. #define CPU_DOWN_PREPARE 0x0005 /* CPU (unsigned)v going down */
  135. #define CPU_DOWN_FAILED 0x0006 /* CPU (unsigned)v NOT going down */
  136. #define CPU_DEAD 0x0007 /* CPU (unsigned)v dead */
  137. #endif /* __KERNEL__ */
  138. #endif /* _LINUX_NOTIFIER_H */