relay.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. /*
  74. * create_buf_file_create() default callback. Creates file to represent buf.
  75. */
  76. static struct dentry *create_buf_file_default_callback(const char *filename,
  77. struct dentry *parent,
  78. int mode,
  79. struct rchan_buf *buf,
  80. int *is_global)
  81. {
  82. return relayfs_create_file(filename, parent, mode,
  83. &relay_file_operations, buf);
  84. }
  85. /*
  86. * remove_buf_file() default callback. Removes file representing relay buffer.
  87. */
  88. static int remove_buf_file_default_callback(struct dentry *dentry)
  89. {
  90. return relayfs_remove(dentry);
  91. }
  92. /* relay channel default callbacks */
  93. static struct rchan_callbacks default_channel_callbacks = {
  94. .subbuf_start = subbuf_start_default_callback,
  95. .buf_mapped = buf_mapped_default_callback,
  96. .buf_unmapped = buf_unmapped_default_callback,
  97. .create_buf_file = create_buf_file_default_callback,
  98. .remove_buf_file = remove_buf_file_default_callback,
  99. };
  100. /**
  101. * wakeup_readers - wake up readers waiting on a channel
  102. * @private: the channel buffer
  103. *
  104. * This is the work function used to defer reader waking. The
  105. * reason waking is deferred is that calling directly from write
  106. * causes problems if you're writing from say the scheduler.
  107. */
  108. static void wakeup_readers(void *private)
  109. {
  110. struct rchan_buf *buf = private;
  111. wake_up_interruptible(&buf->read_wait);
  112. }
  113. /**
  114. * __relay_reset - reset a channel buffer
  115. * @buf: the channel buffer
  116. * @init: 1 if this is a first-time initialization
  117. *
  118. * See relay_reset for description of effect.
  119. */
  120. static inline void __relay_reset(struct rchan_buf *buf, unsigned int init)
  121. {
  122. size_t i;
  123. if (init) {
  124. init_waitqueue_head(&buf->read_wait);
  125. kref_init(&buf->kref);
  126. INIT_WORK(&buf->wake_readers, NULL, NULL);
  127. } else {
  128. cancel_delayed_work(&buf->wake_readers);
  129. flush_scheduled_work();
  130. }
  131. buf->subbufs_produced = 0;
  132. buf->subbufs_consumed = 0;
  133. buf->bytes_consumed = 0;
  134. buf->finalized = 0;
  135. buf->data = buf->start;
  136. buf->offset = 0;
  137. for (i = 0; i < buf->chan->n_subbufs; i++)
  138. buf->padding[i] = 0;
  139. buf->chan->cb->subbuf_start(buf, buf->data, NULL, 0);
  140. }
  141. /**
  142. * relay_reset - reset the channel
  143. * @chan: the channel
  144. *
  145. * This has the effect of erasing all data from all channel buffers
  146. * and restarting the channel in its initial state. The buffers
  147. * are not freed, so any mappings are still in effect.
  148. *
  149. * NOTE: Care should be taken that the channel isn't actually
  150. * being used by anything when this call is made.
  151. */
  152. void relay_reset(struct rchan *chan)
  153. {
  154. unsigned int i;
  155. struct rchan_buf *prev = NULL;
  156. if (!chan)
  157. return;
  158. for (i = 0; i < NR_CPUS; i++) {
  159. if (!chan->buf[i] || chan->buf[i] == prev)
  160. break;
  161. __relay_reset(chan->buf[i], 0);
  162. prev = chan->buf[i];
  163. }
  164. }
  165. /**
  166. * relay_open_buf - create a new channel buffer in relayfs
  167. *
  168. * Internal - used by relay_open().
  169. */
  170. static struct rchan_buf *relay_open_buf(struct rchan *chan,
  171. const char *filename,
  172. struct dentry *parent,
  173. int *is_global)
  174. {
  175. struct rchan_buf *buf;
  176. struct dentry *dentry;
  177. if (*is_global)
  178. return chan->buf[0];
  179. buf = relay_create_buf(chan);
  180. if (!buf)
  181. return NULL;
  182. /* Create file in fs */
  183. dentry = chan->cb->create_buf_file(filename, parent, S_IRUSR,
  184. buf, is_global);
  185. if (!dentry) {
  186. relay_destroy_buf(buf);
  187. return NULL;
  188. }
  189. buf->dentry = dentry;
  190. __relay_reset(buf, 1);
  191. return buf;
  192. }
  193. /**
  194. * relay_close_buf - close a channel buffer
  195. * @buf: channel buffer
  196. *
  197. * Marks the buffer finalized and restores the default callbacks.
  198. * The channel buffer and channel buffer data structure are then freed
  199. * automatically when the last reference is given up.
  200. */
  201. static inline void relay_close_buf(struct rchan_buf *buf)
  202. {
  203. buf->finalized = 1;
  204. buf->chan->cb = &default_channel_callbacks;
  205. cancel_delayed_work(&buf->wake_readers);
  206. flush_scheduled_work();
  207. kref_put(&buf->kref, relay_remove_buf);
  208. }
  209. static inline void setup_callbacks(struct rchan *chan,
  210. struct rchan_callbacks *cb)
  211. {
  212. if (!cb) {
  213. chan->cb = &default_channel_callbacks;
  214. return;
  215. }
  216. if (!cb->subbuf_start)
  217. cb->subbuf_start = subbuf_start_default_callback;
  218. if (!cb->buf_mapped)
  219. cb->buf_mapped = buf_mapped_default_callback;
  220. if (!cb->buf_unmapped)
  221. cb->buf_unmapped = buf_unmapped_default_callback;
  222. if (!cb->create_buf_file)
  223. cb->create_buf_file = create_buf_file_default_callback;
  224. if (!cb->remove_buf_file)
  225. cb->remove_buf_file = remove_buf_file_default_callback;
  226. chan->cb = cb;
  227. }
  228. /**
  229. * relay_open - create a new relayfs channel
  230. * @base_filename: base name of files to create
  231. * @parent: dentry of parent directory, NULL for root directory
  232. * @subbuf_size: size of sub-buffers
  233. * @n_subbufs: number of sub-buffers
  234. * @cb: client callback functions
  235. *
  236. * Returns channel pointer if successful, NULL otherwise.
  237. *
  238. * Creates a channel buffer for each cpu using the sizes and
  239. * attributes specified. The created channel buffer files
  240. * will be named base_filename0...base_filenameN-1. File
  241. * permissions will be S_IRUSR.
  242. */
  243. struct rchan *relay_open(const char *base_filename,
  244. struct dentry *parent,
  245. size_t subbuf_size,
  246. size_t n_subbufs,
  247. struct rchan_callbacks *cb)
  248. {
  249. unsigned int i;
  250. struct rchan *chan;
  251. char *tmpname;
  252. int is_global = 0;
  253. if (!base_filename)
  254. return NULL;
  255. if (!(subbuf_size && n_subbufs))
  256. return NULL;
  257. chan = kcalloc(1, sizeof(struct rchan), GFP_KERNEL);
  258. if (!chan)
  259. return NULL;
  260. chan->version = RELAYFS_CHANNEL_VERSION;
  261. chan->n_subbufs = n_subbufs;
  262. chan->subbuf_size = subbuf_size;
  263. chan->alloc_size = FIX_SIZE(subbuf_size * n_subbufs);
  264. setup_callbacks(chan, cb);
  265. kref_init(&chan->kref);
  266. tmpname = kmalloc(NAME_MAX + 1, GFP_KERNEL);
  267. if (!tmpname)
  268. goto free_chan;
  269. for_each_online_cpu(i) {
  270. sprintf(tmpname, "%s%d", base_filename, i);
  271. chan->buf[i] = relay_open_buf(chan, tmpname, parent,
  272. &is_global);
  273. chan->buf[i]->cpu = i;
  274. if (!chan->buf[i])
  275. goto free_bufs;
  276. }
  277. kfree(tmpname);
  278. return chan;
  279. free_bufs:
  280. for (i = 0; i < NR_CPUS; i++) {
  281. if (!chan->buf[i])
  282. break;
  283. relay_close_buf(chan->buf[i]);
  284. if (is_global)
  285. break;
  286. }
  287. kfree(tmpname);
  288. free_chan:
  289. kref_put(&chan->kref, relay_destroy_channel);
  290. return NULL;
  291. }
  292. /**
  293. * relay_switch_subbuf - switch to a new sub-buffer
  294. * @buf: channel buffer
  295. * @length: size of current event
  296. *
  297. * Returns either the length passed in or 0 if full.
  298. * Performs sub-buffer-switch tasks such as invoking callbacks,
  299. * updating padding counts, waking up readers, etc.
  300. */
  301. size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
  302. {
  303. void *old, *new;
  304. size_t old_subbuf, new_subbuf;
  305. if (unlikely(length > buf->chan->subbuf_size))
  306. goto toobig;
  307. if (buf->offset != buf->chan->subbuf_size + 1) {
  308. buf->prev_padding = buf->chan->subbuf_size - buf->offset;
  309. old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
  310. buf->padding[old_subbuf] = buf->prev_padding;
  311. buf->subbufs_produced++;
  312. if (waitqueue_active(&buf->read_wait)) {
  313. PREPARE_WORK(&buf->wake_readers, wakeup_readers, buf);
  314. schedule_delayed_work(&buf->wake_readers, 1);
  315. }
  316. }
  317. old = buf->data;
  318. new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
  319. new = buf->start + new_subbuf * buf->chan->subbuf_size;
  320. buf->offset = 0;
  321. if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) {
  322. buf->offset = buf->chan->subbuf_size + 1;
  323. return 0;
  324. }
  325. buf->data = new;
  326. buf->padding[new_subbuf] = 0;
  327. if (unlikely(length + buf->offset > buf->chan->subbuf_size))
  328. goto toobig;
  329. return length;
  330. toobig:
  331. buf->chan->last_toobig = length;
  332. return 0;
  333. }
  334. /**
  335. * relay_subbufs_consumed - update the buffer's sub-buffers-consumed count
  336. * @chan: the channel
  337. * @cpu: the cpu associated with the channel buffer to update
  338. * @subbufs_consumed: number of sub-buffers to add to current buf's count
  339. *
  340. * Adds to the channel buffer's consumed sub-buffer count.
  341. * subbufs_consumed should be the number of sub-buffers newly consumed,
  342. * not the total consumed.
  343. *
  344. * NOTE: kernel clients don't need to call this function if the channel
  345. * mode is 'overwrite'.
  346. */
  347. void relay_subbufs_consumed(struct rchan *chan,
  348. unsigned int cpu,
  349. size_t subbufs_consumed)
  350. {
  351. struct rchan_buf *buf;
  352. if (!chan)
  353. return;
  354. if (cpu >= NR_CPUS || !chan->buf[cpu])
  355. return;
  356. buf = chan->buf[cpu];
  357. buf->subbufs_consumed += subbufs_consumed;
  358. if (buf->subbufs_consumed > buf->subbufs_produced)
  359. buf->subbufs_consumed = buf->subbufs_produced;
  360. }
  361. /**
  362. * relay_destroy_channel - free the channel struct
  363. *
  364. * Should only be called from kref_put().
  365. */
  366. void relay_destroy_channel(struct kref *kref)
  367. {
  368. struct rchan *chan = container_of(kref, struct rchan, kref);
  369. kfree(chan);
  370. }
  371. /**
  372. * relay_close - close the channel
  373. * @chan: the channel
  374. *
  375. * Closes all channel buffers and frees the channel.
  376. */
  377. void relay_close(struct rchan *chan)
  378. {
  379. unsigned int i;
  380. struct rchan_buf *prev = NULL;
  381. if (!chan)
  382. return;
  383. for (i = 0; i < NR_CPUS; i++) {
  384. if (!chan->buf[i] || chan->buf[i] == prev)
  385. break;
  386. relay_close_buf(chan->buf[i]);
  387. prev = chan->buf[i];
  388. }
  389. if (chan->last_toobig)
  390. printk(KERN_WARNING "relayfs: one or more items not logged "
  391. "[item size (%Zd) > sub-buffer size (%Zd)]\n",
  392. chan->last_toobig, chan->subbuf_size);
  393. kref_put(&chan->kref, relay_destroy_channel);
  394. }
  395. /**
  396. * relay_flush - close the channel
  397. * @chan: the channel
  398. *
  399. * Flushes all channel buffers i.e. forces buffer switch.
  400. */
  401. void relay_flush(struct rchan *chan)
  402. {
  403. unsigned int i;
  404. struct rchan_buf *prev = NULL;
  405. if (!chan)
  406. return;
  407. for (i = 0; i < NR_CPUS; i++) {
  408. if (!chan->buf[i] || chan->buf[i] == prev)
  409. break;
  410. relay_switch_subbuf(chan->buf[i], 0);
  411. prev = chan->buf[i];
  412. }
  413. }
  414. EXPORT_SYMBOL_GPL(relay_open);
  415. EXPORT_SYMBOL_GPL(relay_close);
  416. EXPORT_SYMBOL_GPL(relay_flush);
  417. EXPORT_SYMBOL_GPL(relay_reset);
  418. EXPORT_SYMBOL_GPL(relay_subbufs_consumed);
  419. EXPORT_SYMBOL_GPL(relay_switch_subbuf);
  420. EXPORT_SYMBOL_GPL(relay_buf_full);