relay.c 10 KB

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