relay.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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,
  158. &relayfs_file_operations, buf);
  159. if (!dentry) {
  160. relay_destroy_buf(buf);
  161. return NULL;
  162. }
  163. buf->dentry = dentry;
  164. __relay_reset(buf, 1);
  165. return buf;
  166. }
  167. /**
  168. * relay_close_buf - close a channel buffer
  169. * @buf: channel buffer
  170. *
  171. * Marks the buffer finalized and restores the default callbacks.
  172. * The channel buffer and channel buffer data structure are then freed
  173. * automatically when the last reference is given up.
  174. */
  175. static inline void relay_close_buf(struct rchan_buf *buf)
  176. {
  177. buf->finalized = 1;
  178. buf->chan->cb = &default_channel_callbacks;
  179. cancel_delayed_work(&buf->wake_readers);
  180. flush_scheduled_work();
  181. kref_put(&buf->kref, relay_remove_buf);
  182. }
  183. static inline void setup_callbacks(struct rchan *chan,
  184. struct rchan_callbacks *cb)
  185. {
  186. if (!cb) {
  187. chan->cb = &default_channel_callbacks;
  188. return;
  189. }
  190. if (!cb->subbuf_start)
  191. cb->subbuf_start = subbuf_start_default_callback;
  192. if (!cb->buf_mapped)
  193. cb->buf_mapped = buf_mapped_default_callback;
  194. if (!cb->buf_unmapped)
  195. cb->buf_unmapped = buf_unmapped_default_callback;
  196. chan->cb = cb;
  197. }
  198. /**
  199. * relay_open - create a new relayfs channel
  200. * @base_filename: base name of files to create
  201. * @parent: dentry of parent directory, NULL for root directory
  202. * @subbuf_size: size of sub-buffers
  203. * @n_subbufs: number of sub-buffers
  204. * @cb: client callback functions
  205. *
  206. * Returns channel pointer if successful, NULL otherwise.
  207. *
  208. * Creates a channel buffer for each cpu using the sizes and
  209. * attributes specified. The created channel buffer files
  210. * will be named base_filename0...base_filenameN-1. File
  211. * permissions will be S_IRUSR.
  212. */
  213. struct rchan *relay_open(const char *base_filename,
  214. struct dentry *parent,
  215. size_t subbuf_size,
  216. size_t n_subbufs,
  217. struct rchan_callbacks *cb)
  218. {
  219. unsigned int i;
  220. struct rchan *chan;
  221. char *tmpname;
  222. if (!base_filename)
  223. return NULL;
  224. if (!(subbuf_size && n_subbufs))
  225. return NULL;
  226. chan = kcalloc(1, sizeof(struct rchan), GFP_KERNEL);
  227. if (!chan)
  228. return NULL;
  229. chan->version = RELAYFS_CHANNEL_VERSION;
  230. chan->n_subbufs = n_subbufs;
  231. chan->subbuf_size = subbuf_size;
  232. chan->alloc_size = FIX_SIZE(subbuf_size * n_subbufs);
  233. setup_callbacks(chan, cb);
  234. kref_init(&chan->kref);
  235. tmpname = kmalloc(NAME_MAX + 1, GFP_KERNEL);
  236. if (!tmpname)
  237. goto free_chan;
  238. for_each_online_cpu(i) {
  239. sprintf(tmpname, "%s%d", base_filename, i);
  240. chan->buf[i] = relay_open_buf(chan, tmpname, parent);
  241. chan->buf[i]->cpu = i;
  242. if (!chan->buf[i])
  243. goto free_bufs;
  244. }
  245. kfree(tmpname);
  246. return chan;
  247. free_bufs:
  248. for (i = 0; i < NR_CPUS; i++) {
  249. if (!chan->buf[i])
  250. break;
  251. relay_close_buf(chan->buf[i]);
  252. }
  253. kfree(tmpname);
  254. free_chan:
  255. kref_put(&chan->kref, relay_destroy_channel);
  256. return NULL;
  257. }
  258. /**
  259. * relay_switch_subbuf - switch to a new sub-buffer
  260. * @buf: channel buffer
  261. * @length: size of current event
  262. *
  263. * Returns either the length passed in or 0 if full.
  264. * Performs sub-buffer-switch tasks such as invoking callbacks,
  265. * updating padding counts, waking up readers, etc.
  266. */
  267. size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
  268. {
  269. void *old, *new;
  270. size_t old_subbuf, new_subbuf;
  271. if (unlikely(length > buf->chan->subbuf_size))
  272. goto toobig;
  273. if (buf->offset != buf->chan->subbuf_size + 1) {
  274. buf->prev_padding = buf->chan->subbuf_size - buf->offset;
  275. old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
  276. buf->padding[old_subbuf] = buf->prev_padding;
  277. buf->subbufs_produced++;
  278. if (waitqueue_active(&buf->read_wait)) {
  279. PREPARE_WORK(&buf->wake_readers, wakeup_readers, buf);
  280. schedule_delayed_work(&buf->wake_readers, 1);
  281. }
  282. }
  283. old = buf->data;
  284. new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
  285. new = buf->start + new_subbuf * buf->chan->subbuf_size;
  286. buf->offset = 0;
  287. if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) {
  288. buf->offset = buf->chan->subbuf_size + 1;
  289. return 0;
  290. }
  291. buf->data = new;
  292. buf->padding[new_subbuf] = 0;
  293. if (unlikely(length + buf->offset > buf->chan->subbuf_size))
  294. goto toobig;
  295. return length;
  296. toobig:
  297. buf->chan->last_toobig = length;
  298. return 0;
  299. }
  300. /**
  301. * relay_subbufs_consumed - update the buffer's sub-buffers-consumed count
  302. * @chan: the channel
  303. * @cpu: the cpu associated with the channel buffer to update
  304. * @subbufs_consumed: number of sub-buffers to add to current buf's count
  305. *
  306. * Adds to the channel buffer's consumed sub-buffer count.
  307. * subbufs_consumed should be the number of sub-buffers newly consumed,
  308. * not the total consumed.
  309. *
  310. * NOTE: kernel clients don't need to call this function if the channel
  311. * mode is 'overwrite'.
  312. */
  313. void relay_subbufs_consumed(struct rchan *chan,
  314. unsigned int cpu,
  315. size_t subbufs_consumed)
  316. {
  317. struct rchan_buf *buf;
  318. if (!chan)
  319. return;
  320. if (cpu >= NR_CPUS || !chan->buf[cpu])
  321. return;
  322. buf = chan->buf[cpu];
  323. buf->subbufs_consumed += subbufs_consumed;
  324. if (buf->subbufs_consumed > buf->subbufs_produced)
  325. buf->subbufs_consumed = buf->subbufs_produced;
  326. }
  327. /**
  328. * relay_destroy_channel - free the channel struct
  329. *
  330. * Should only be called from kref_put().
  331. */
  332. void relay_destroy_channel(struct kref *kref)
  333. {
  334. struct rchan *chan = container_of(kref, struct rchan, kref);
  335. kfree(chan);
  336. }
  337. /**
  338. * relay_close - close the channel
  339. * @chan: the channel
  340. *
  341. * Closes all channel buffers and frees the channel.
  342. */
  343. void relay_close(struct rchan *chan)
  344. {
  345. unsigned int i;
  346. if (!chan)
  347. return;
  348. for (i = 0; i < NR_CPUS; i++) {
  349. if (!chan->buf[i])
  350. continue;
  351. relay_close_buf(chan->buf[i]);
  352. }
  353. if (chan->last_toobig)
  354. printk(KERN_WARNING "relayfs: one or more items not logged "
  355. "[item size (%Zd) > sub-buffer size (%Zd)]\n",
  356. chan->last_toobig, chan->subbuf_size);
  357. kref_put(&chan->kref, relay_destroy_channel);
  358. }
  359. /**
  360. * relay_flush - close the channel
  361. * @chan: the channel
  362. *
  363. * Flushes all channel buffers i.e. forces buffer switch.
  364. */
  365. void relay_flush(struct rchan *chan)
  366. {
  367. unsigned int i;
  368. if (!chan)
  369. return;
  370. for (i = 0; i < NR_CPUS; i++) {
  371. if (!chan->buf[i])
  372. continue;
  373. relay_switch_subbuf(chan->buf[i], 0);
  374. }
  375. }
  376. EXPORT_SYMBOL_GPL(relay_open);
  377. EXPORT_SYMBOL_GPL(relay_close);
  378. EXPORT_SYMBOL_GPL(relay_flush);
  379. EXPORT_SYMBOL_GPL(relay_reset);
  380. EXPORT_SYMBOL_GPL(relay_subbufs_consumed);
  381. EXPORT_SYMBOL_GPL(relay_switch_subbuf);
  382. EXPORT_SYMBOL_GPL(relay_buf_full);