relay.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * Public API and common code for RelayFS.
  3. *
  4. * See Documentation/filesystems/relayfs.txt for an overview of relayfs.
  5. *
  6. * Copyright (C) 2002-2005 - Tom Zanussi (zanussi@us.ibm.com), IBM Corp
  7. * Copyright (C) 1999-2005 - Karim Yaghmour (karim@opersys.com)
  8. *
  9. * This file is released under the GPL.
  10. */
  11. #include <linux/errno.h>
  12. #include <linux/stddef.h>
  13. #include <linux/slab.h>
  14. #include <linux/module.h>
  15. #include <linux/string.h>
  16. #include <linux/relayfs_fs.h>
  17. #include "relay.h"
  18. #include "buffers.h"
  19. /**
  20. * relay_buf_empty - boolean, is the channel buffer empty?
  21. * @buf: channel buffer
  22. *
  23. * Returns 1 if the buffer is empty, 0 otherwise.
  24. */
  25. int relay_buf_empty(struct rchan_buf *buf)
  26. {
  27. return (buf->subbufs_produced - buf->subbufs_consumed) ? 0 : 1;
  28. }
  29. /**
  30. * relay_buf_full - boolean, is the channel buffer full?
  31. * @buf: channel buffer
  32. *
  33. * Returns 1 if the buffer is full, 0 otherwise.
  34. */
  35. int relay_buf_full(struct rchan_buf *buf)
  36. {
  37. size_t ready = buf->subbufs_produced - buf->subbufs_consumed;
  38. return (ready >= buf->chan->n_subbufs) ? 1 : 0;
  39. }
  40. /*
  41. * High-level relayfs kernel API and associated functions.
  42. */
  43. /*
  44. * rchan_callback implementations defining default channel behavior. Used
  45. * in place of corresponding NULL values in client callback struct.
  46. */
  47. /*
  48. * subbuf_start() default callback. Does nothing.
  49. */
  50. static int subbuf_start_default_callback (struct rchan_buf *buf,
  51. void *subbuf,
  52. void *prev_subbuf,
  53. size_t prev_padding)
  54. {
  55. if (relay_buf_full(buf))
  56. return 0;
  57. return 1;
  58. }
  59. /*
  60. * buf_mapped() default callback. Does nothing.
  61. */
  62. static void buf_mapped_default_callback(struct rchan_buf *buf,
  63. struct file *filp)
  64. {
  65. }
  66. /*
  67. * buf_unmapped() default callback. Does nothing.
  68. */
  69. static void buf_unmapped_default_callback(struct rchan_buf *buf,
  70. struct file *filp)
  71. {
  72. }
  73. /* relay channel default callbacks */
  74. static struct rchan_callbacks default_channel_callbacks = {
  75. .subbuf_start = subbuf_start_default_callback,
  76. .buf_mapped = buf_mapped_default_callback,
  77. .buf_unmapped = buf_unmapped_default_callback,
  78. };
  79. /**
  80. * wakeup_readers - wake up readers waiting on a channel
  81. * @private: the channel buffer
  82. *
  83. * This is the work function used to defer reader waking. The
  84. * reason waking is deferred is that calling directly from write
  85. * causes problems if you're writing from say the scheduler.
  86. */
  87. static void wakeup_readers(void *private)
  88. {
  89. struct rchan_buf *buf = private;
  90. wake_up_interruptible(&buf->read_wait);
  91. }
  92. /**
  93. * __relay_reset - reset a channel buffer
  94. * @buf: the channel buffer
  95. * @init: 1 if this is a first-time initialization
  96. *
  97. * See relay_reset for description of effect.
  98. */
  99. static inline void __relay_reset(struct rchan_buf *buf, unsigned int init)
  100. {
  101. size_t i;
  102. if (init) {
  103. init_waitqueue_head(&buf->read_wait);
  104. kref_init(&buf->kref);
  105. INIT_WORK(&buf->wake_readers, NULL, NULL);
  106. } else {
  107. cancel_delayed_work(&buf->wake_readers);
  108. flush_scheduled_work();
  109. }
  110. buf->subbufs_produced = 0;
  111. buf->subbufs_consumed = 0;
  112. buf->bytes_consumed = 0;
  113. buf->finalized = 0;
  114. buf->data = buf->start;
  115. buf->offset = 0;
  116. for (i = 0; i < buf->chan->n_subbufs; i++)
  117. buf->padding[i] = 0;
  118. buf->chan->cb->subbuf_start(buf, buf->data, NULL, 0);
  119. }
  120. /**
  121. * relay_reset - reset the channel
  122. * @chan: the channel
  123. *
  124. * This has the effect of erasing all data from all channel buffers
  125. * and restarting the channel in its initial state. The buffers
  126. * are not freed, so any mappings are still in effect.
  127. *
  128. * NOTE: Care should be taken that the channel isn't actually
  129. * being used by anything when this call is made.
  130. */
  131. void relay_reset(struct rchan *chan)
  132. {
  133. unsigned int i;
  134. if (!chan)
  135. return;
  136. for (i = 0; i < NR_CPUS; i++) {
  137. if (!chan->buf[i])
  138. continue;
  139. __relay_reset(chan->buf[i], 0);
  140. }
  141. }
  142. /**
  143. * relay_open_buf - create a new channel buffer in relayfs
  144. *
  145. * Internal - used by relay_open().
  146. */
  147. static struct rchan_buf *relay_open_buf(struct rchan *chan,
  148. const char *filename,
  149. struct dentry *parent)
  150. {
  151. struct rchan_buf *buf;
  152. struct dentry *dentry;
  153. buf = relay_create_buf(chan);
  154. if (!buf)
  155. return NULL;
  156. /* Create file in fs */
  157. dentry = relayfs_create_file(filename, parent, S_IRUSR, buf);
  158. if (!dentry) {
  159. relay_destroy_buf(buf);
  160. return NULL;
  161. }
  162. buf->dentry = dentry;
  163. __relay_reset(buf, 1);
  164. return buf;
  165. }
  166. /**
  167. * relay_close_buf - close a channel buffer
  168. * @buf: channel buffer
  169. *
  170. * Marks the buffer finalized and restores the default callbacks.
  171. * The channel buffer and channel buffer data structure are then freed
  172. * automatically when the last reference is given up.
  173. */
  174. static inline void relay_close_buf(struct rchan_buf *buf)
  175. {
  176. buf->finalized = 1;
  177. buf->chan->cb = &default_channel_callbacks;
  178. cancel_delayed_work(&buf->wake_readers);
  179. flush_scheduled_work();
  180. kref_put(&buf->kref, relay_remove_buf);
  181. }
  182. static inline void setup_callbacks(struct rchan *chan,
  183. struct rchan_callbacks *cb)
  184. {
  185. if (!cb) {
  186. chan->cb = &default_channel_callbacks;
  187. return;
  188. }
  189. if (!cb->subbuf_start)
  190. cb->subbuf_start = subbuf_start_default_callback;
  191. if (!cb->buf_mapped)
  192. cb->buf_mapped = buf_mapped_default_callback;
  193. if (!cb->buf_unmapped)
  194. cb->buf_unmapped = buf_unmapped_default_callback;
  195. chan->cb = cb;
  196. }
  197. /**
  198. * relay_open - create a new relayfs channel
  199. * @base_filename: base name of files to create
  200. * @parent: dentry of parent directory, NULL for root directory
  201. * @subbuf_size: size of sub-buffers
  202. * @n_subbufs: number of sub-buffers
  203. * @cb: client callback functions
  204. *
  205. * Returns channel pointer if successful, NULL otherwise.
  206. *
  207. * Creates a channel buffer for each cpu using the sizes and
  208. * attributes specified. The created channel buffer files
  209. * will be named base_filename0...base_filenameN-1. File
  210. * permissions will be S_IRUSR.
  211. */
  212. struct rchan *relay_open(const char *base_filename,
  213. struct dentry *parent,
  214. size_t subbuf_size,
  215. size_t n_subbufs,
  216. struct rchan_callbacks *cb)
  217. {
  218. unsigned int i;
  219. struct rchan *chan;
  220. char *tmpname;
  221. if (!base_filename)
  222. return NULL;
  223. if (!(subbuf_size && n_subbufs))
  224. return NULL;
  225. chan = kcalloc(1, sizeof(struct rchan), GFP_KERNEL);
  226. if (!chan)
  227. return NULL;
  228. chan->version = RELAYFS_CHANNEL_VERSION;
  229. chan->n_subbufs = n_subbufs;
  230. chan->subbuf_size = subbuf_size;
  231. chan->alloc_size = FIX_SIZE(subbuf_size * n_subbufs);
  232. setup_callbacks(chan, cb);
  233. kref_init(&chan->kref);
  234. tmpname = kmalloc(NAME_MAX + 1, GFP_KERNEL);
  235. if (!tmpname)
  236. goto free_chan;
  237. for_each_online_cpu(i) {
  238. sprintf(tmpname, "%s%d", base_filename, i);
  239. chan->buf[i] = relay_open_buf(chan, tmpname, parent);
  240. chan->buf[i]->cpu = i;
  241. if (!chan->buf[i])
  242. goto free_bufs;
  243. }
  244. kfree(tmpname);
  245. return chan;
  246. free_bufs:
  247. for (i = 0; i < NR_CPUS; i++) {
  248. if (!chan->buf[i])
  249. break;
  250. relay_close_buf(chan->buf[i]);
  251. }
  252. kfree(tmpname);
  253. free_chan:
  254. kref_put(&chan->kref, relay_destroy_channel);
  255. return NULL;
  256. }
  257. /**
  258. * relay_switch_subbuf - switch to a new sub-buffer
  259. * @buf: channel buffer
  260. * @length: size of current event
  261. *
  262. * Returns either the length passed in or 0 if full.
  263. * Performs sub-buffer-switch tasks such as invoking callbacks,
  264. * updating padding counts, waking up readers, etc.
  265. */
  266. size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
  267. {
  268. void *old, *new;
  269. size_t old_subbuf, new_subbuf;
  270. if (unlikely(length > buf->chan->subbuf_size))
  271. goto toobig;
  272. if (buf->offset != buf->chan->subbuf_size + 1) {
  273. buf->prev_padding = buf->chan->subbuf_size - buf->offset;
  274. old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
  275. buf->padding[old_subbuf] = buf->prev_padding;
  276. buf->subbufs_produced++;
  277. if (waitqueue_active(&buf->read_wait)) {
  278. PREPARE_WORK(&buf->wake_readers, wakeup_readers, buf);
  279. schedule_delayed_work(&buf->wake_readers, 1);
  280. }
  281. }
  282. old = buf->data;
  283. new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
  284. new = buf->start + new_subbuf * buf->chan->subbuf_size;
  285. buf->offset = 0;
  286. if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) {
  287. buf->offset = buf->chan->subbuf_size + 1;
  288. return 0;
  289. }
  290. buf->data = new;
  291. buf->padding[new_subbuf] = 0;
  292. if (unlikely(length + buf->offset > buf->chan->subbuf_size))
  293. goto toobig;
  294. return length;
  295. toobig:
  296. buf->chan->last_toobig = length;
  297. return 0;
  298. }
  299. /**
  300. * relay_subbufs_consumed - update the buffer's sub-buffers-consumed count
  301. * @chan: the channel
  302. * @cpu: the cpu associated with the channel buffer to update
  303. * @subbufs_consumed: number of sub-buffers to add to current buf's count
  304. *
  305. * Adds to the channel buffer's consumed sub-buffer count.
  306. * subbufs_consumed should be the number of sub-buffers newly consumed,
  307. * not the total consumed.
  308. *
  309. * NOTE: kernel clients don't need to call this function if the channel
  310. * mode is 'overwrite'.
  311. */
  312. void relay_subbufs_consumed(struct rchan *chan,
  313. unsigned int cpu,
  314. size_t subbufs_consumed)
  315. {
  316. struct rchan_buf *buf;
  317. if (!chan)
  318. return;
  319. if (cpu >= NR_CPUS || !chan->buf[cpu])
  320. return;
  321. buf = chan->buf[cpu];
  322. buf->subbufs_consumed += subbufs_consumed;
  323. if (buf->subbufs_consumed > buf->subbufs_produced)
  324. buf->subbufs_consumed = buf->subbufs_produced;
  325. }
  326. /**
  327. * relay_destroy_channel - free the channel struct
  328. *
  329. * Should only be called from kref_put().
  330. */
  331. void relay_destroy_channel(struct kref *kref)
  332. {
  333. struct rchan *chan = container_of(kref, struct rchan, kref);
  334. kfree(chan);
  335. }
  336. /**
  337. * relay_close - close the channel
  338. * @chan: the channel
  339. *
  340. * Closes all channel buffers and frees the channel.
  341. */
  342. void relay_close(struct rchan *chan)
  343. {
  344. unsigned int i;
  345. if (!chan)
  346. return;
  347. for (i = 0; i < NR_CPUS; i++) {
  348. if (!chan->buf[i])
  349. continue;
  350. relay_close_buf(chan->buf[i]);
  351. }
  352. if (chan->last_toobig)
  353. printk(KERN_WARNING "relayfs: one or more items not logged "
  354. "[item size (%Zd) > sub-buffer size (%Zd)]\n",
  355. chan->last_toobig, chan->subbuf_size);
  356. kref_put(&chan->kref, relay_destroy_channel);
  357. }
  358. /**
  359. * relay_flush - close the channel
  360. * @chan: the channel
  361. *
  362. * Flushes all channel buffers i.e. forces buffer switch.
  363. */
  364. void relay_flush(struct rchan *chan)
  365. {
  366. unsigned int i;
  367. if (!chan)
  368. return;
  369. for (i = 0; i < NR_CPUS; i++) {
  370. if (!chan->buf[i])
  371. continue;
  372. relay_switch_subbuf(chan->buf[i], 0);
  373. }
  374. }
  375. EXPORT_SYMBOL_GPL(relay_open);
  376. EXPORT_SYMBOL_GPL(relay_close);
  377. EXPORT_SYMBOL_GPL(relay_flush);
  378. EXPORT_SYMBOL_GPL(relay_reset);
  379. EXPORT_SYMBOL_GPL(relay_subbufs_consumed);
  380. EXPORT_SYMBOL_GPL(relay_switch_subbuf);
  381. EXPORT_SYMBOL_GPL(relay_buf_full);