relay.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. /*
  2. * Public API and common code for kernel->userspace relay file support.
  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. * Moved to kernel/relay.c by Paul Mundt, 2006.
  10. *
  11. * This file is released under the GPL.
  12. */
  13. #include <linux/errno.h>
  14. #include <linux/stddef.h>
  15. #include <linux/slab.h>
  16. #include <linux/module.h>
  17. #include <linux/string.h>
  18. #include <linux/relay.h>
  19. #include <linux/vmalloc.h>
  20. #include <linux/mm.h>
  21. /*
  22. * close() vm_op implementation for relay file mapping.
  23. */
  24. static void relay_file_mmap_close(struct vm_area_struct *vma)
  25. {
  26. struct rchan_buf *buf = vma->vm_private_data;
  27. buf->chan->cb->buf_unmapped(buf, vma->vm_file);
  28. }
  29. /*
  30. * nopage() vm_op implementation for relay file mapping.
  31. */
  32. static struct page *relay_buf_nopage(struct vm_area_struct *vma,
  33. unsigned long address,
  34. int *type)
  35. {
  36. struct page *page;
  37. struct rchan_buf *buf = vma->vm_private_data;
  38. unsigned long offset = address - vma->vm_start;
  39. if (address > vma->vm_end)
  40. return NOPAGE_SIGBUS; /* Disallow mremap */
  41. if (!buf)
  42. return NOPAGE_OOM;
  43. page = vmalloc_to_page(buf->start + offset);
  44. if (!page)
  45. return NOPAGE_OOM;
  46. get_page(page);
  47. if (type)
  48. *type = VM_FAULT_MINOR;
  49. return page;
  50. }
  51. /*
  52. * vm_ops for relay file mappings.
  53. */
  54. static struct vm_operations_struct relay_file_mmap_ops = {
  55. .nopage = relay_buf_nopage,
  56. .close = relay_file_mmap_close,
  57. };
  58. /**
  59. * relay_mmap_buf: - mmap channel buffer to process address space
  60. * @buf: relay channel buffer
  61. * @vma: vm_area_struct describing memory to be mapped
  62. *
  63. * Returns 0 if ok, negative on error
  64. *
  65. * Caller should already have grabbed mmap_sem.
  66. */
  67. int relay_mmap_buf(struct rchan_buf *buf, struct vm_area_struct *vma)
  68. {
  69. unsigned long length = vma->vm_end - vma->vm_start;
  70. struct file *filp = vma->vm_file;
  71. if (!buf)
  72. return -EBADF;
  73. if (length != (unsigned long)buf->chan->alloc_size)
  74. return -EINVAL;
  75. vma->vm_ops = &relay_file_mmap_ops;
  76. vma->vm_private_data = buf;
  77. buf->chan->cb->buf_mapped(buf, filp);
  78. return 0;
  79. }
  80. /**
  81. * relay_alloc_buf - allocate a channel buffer
  82. * @buf: the buffer struct
  83. * @size: total size of the buffer
  84. *
  85. * Returns a pointer to the resulting buffer, %NULL if unsuccessful. The
  86. * passed in size will get page aligned, if it isn't already.
  87. */
  88. static void *relay_alloc_buf(struct rchan_buf *buf, size_t *size)
  89. {
  90. void *mem;
  91. unsigned int i, j, n_pages;
  92. *size = PAGE_ALIGN(*size);
  93. n_pages = *size >> PAGE_SHIFT;
  94. buf->page_array = kcalloc(n_pages, sizeof(struct page *), GFP_KERNEL);
  95. if (!buf->page_array)
  96. return NULL;
  97. for (i = 0; i < n_pages; i++) {
  98. buf->page_array[i] = alloc_page(GFP_KERNEL);
  99. if (unlikely(!buf->page_array[i]))
  100. goto depopulate;
  101. }
  102. mem = vmap(buf->page_array, n_pages, VM_MAP, PAGE_KERNEL);
  103. if (!mem)
  104. goto depopulate;
  105. memset(mem, 0, *size);
  106. buf->page_count = n_pages;
  107. return mem;
  108. depopulate:
  109. for (j = 0; j < i; j++)
  110. __free_page(buf->page_array[j]);
  111. kfree(buf->page_array);
  112. return NULL;
  113. }
  114. /**
  115. * relay_create_buf - allocate and initialize a channel buffer
  116. * @chan: the relay channel
  117. *
  118. * Returns channel buffer if successful, %NULL otherwise.
  119. */
  120. struct rchan_buf *relay_create_buf(struct rchan *chan)
  121. {
  122. struct rchan_buf *buf = kcalloc(1, sizeof(struct rchan_buf), GFP_KERNEL);
  123. if (!buf)
  124. return NULL;
  125. buf->padding = kmalloc(chan->n_subbufs * sizeof(size_t *), GFP_KERNEL);
  126. if (!buf->padding)
  127. goto free_buf;
  128. buf->start = relay_alloc_buf(buf, &chan->alloc_size);
  129. if (!buf->start)
  130. goto free_buf;
  131. buf->chan = chan;
  132. kref_get(&buf->chan->kref);
  133. return buf;
  134. free_buf:
  135. kfree(buf->padding);
  136. kfree(buf);
  137. return NULL;
  138. }
  139. /**
  140. * relay_destroy_channel - free the channel struct
  141. * @kref: target kernel reference that contains the relay channel
  142. *
  143. * Should only be called from kref_put().
  144. */
  145. void relay_destroy_channel(struct kref *kref)
  146. {
  147. struct rchan *chan = container_of(kref, struct rchan, kref);
  148. kfree(chan);
  149. }
  150. /**
  151. * relay_destroy_buf - destroy an rchan_buf struct and associated buffer
  152. * @buf: the buffer struct
  153. */
  154. void relay_destroy_buf(struct rchan_buf *buf)
  155. {
  156. struct rchan *chan = buf->chan;
  157. unsigned int i;
  158. if (likely(buf->start)) {
  159. vunmap(buf->start);
  160. for (i = 0; i < buf->page_count; i++)
  161. __free_page(buf->page_array[i]);
  162. kfree(buf->page_array);
  163. }
  164. kfree(buf->padding);
  165. kfree(buf);
  166. kref_put(&chan->kref, relay_destroy_channel);
  167. }
  168. /**
  169. * relay_remove_buf - remove a channel buffer
  170. * @kref: target kernel reference that contains the relay buffer
  171. *
  172. * Removes the file from the fileystem, which also frees the
  173. * rchan_buf_struct and the channel buffer. Should only be called from
  174. * kref_put().
  175. */
  176. void relay_remove_buf(struct kref *kref)
  177. {
  178. struct rchan_buf *buf = container_of(kref, struct rchan_buf, kref);
  179. buf->chan->cb->remove_buf_file(buf->dentry);
  180. relay_destroy_buf(buf);
  181. }
  182. /**
  183. * relay_buf_empty - boolean, is the channel buffer empty?
  184. * @buf: channel buffer
  185. *
  186. * Returns 1 if the buffer is empty, 0 otherwise.
  187. */
  188. int relay_buf_empty(struct rchan_buf *buf)
  189. {
  190. return (buf->subbufs_produced - buf->subbufs_consumed) ? 0 : 1;
  191. }
  192. EXPORT_SYMBOL_GPL(relay_buf_empty);
  193. /**
  194. * relay_buf_full - boolean, is the channel buffer full?
  195. * @buf: channel buffer
  196. *
  197. * Returns 1 if the buffer is full, 0 otherwise.
  198. */
  199. int relay_buf_full(struct rchan_buf *buf)
  200. {
  201. size_t ready = buf->subbufs_produced - buf->subbufs_consumed;
  202. return (ready >= buf->chan->n_subbufs) ? 1 : 0;
  203. }
  204. EXPORT_SYMBOL_GPL(relay_buf_full);
  205. /*
  206. * High-level relay kernel API and associated functions.
  207. */
  208. /*
  209. * rchan_callback implementations defining default channel behavior. Used
  210. * in place of corresponding NULL values in client callback struct.
  211. */
  212. /*
  213. * subbuf_start() default callback. Does nothing.
  214. */
  215. static int subbuf_start_default_callback (struct rchan_buf *buf,
  216. void *subbuf,
  217. void *prev_subbuf,
  218. size_t prev_padding)
  219. {
  220. if (relay_buf_full(buf))
  221. return 0;
  222. return 1;
  223. }
  224. /*
  225. * buf_mapped() default callback. Does nothing.
  226. */
  227. static void buf_mapped_default_callback(struct rchan_buf *buf,
  228. struct file *filp)
  229. {
  230. }
  231. /*
  232. * buf_unmapped() default callback. Does nothing.
  233. */
  234. static void buf_unmapped_default_callback(struct rchan_buf *buf,
  235. struct file *filp)
  236. {
  237. }
  238. /*
  239. * create_buf_file_create() default callback. Does nothing.
  240. */
  241. static struct dentry *create_buf_file_default_callback(const char *filename,
  242. struct dentry *parent,
  243. int mode,
  244. struct rchan_buf *buf,
  245. int *is_global)
  246. {
  247. return NULL;
  248. }
  249. /*
  250. * remove_buf_file() default callback. Does nothing.
  251. */
  252. static int remove_buf_file_default_callback(struct dentry *dentry)
  253. {
  254. return -EINVAL;
  255. }
  256. /* relay channel default callbacks */
  257. static struct rchan_callbacks default_channel_callbacks = {
  258. .subbuf_start = subbuf_start_default_callback,
  259. .buf_mapped = buf_mapped_default_callback,
  260. .buf_unmapped = buf_unmapped_default_callback,
  261. .create_buf_file = create_buf_file_default_callback,
  262. .remove_buf_file = remove_buf_file_default_callback,
  263. };
  264. /**
  265. * wakeup_readers - wake up readers waiting on a channel
  266. * @private: the channel buffer
  267. *
  268. * This is the work function used to defer reader waking. The
  269. * reason waking is deferred is that calling directly from write
  270. * causes problems if you're writing from say the scheduler.
  271. */
  272. static void wakeup_readers(void *private)
  273. {
  274. struct rchan_buf *buf = private;
  275. wake_up_interruptible(&buf->read_wait);
  276. }
  277. /**
  278. * __relay_reset - reset a channel buffer
  279. * @buf: the channel buffer
  280. * @init: 1 if this is a first-time initialization
  281. *
  282. * See relay_reset for description of effect.
  283. */
  284. static inline void __relay_reset(struct rchan_buf *buf, unsigned int init)
  285. {
  286. size_t i;
  287. if (init) {
  288. init_waitqueue_head(&buf->read_wait);
  289. kref_init(&buf->kref);
  290. INIT_WORK(&buf->wake_readers, NULL, NULL);
  291. } else {
  292. cancel_delayed_work(&buf->wake_readers);
  293. flush_scheduled_work();
  294. }
  295. buf->subbufs_produced = 0;
  296. buf->subbufs_consumed = 0;
  297. buf->bytes_consumed = 0;
  298. buf->finalized = 0;
  299. buf->data = buf->start;
  300. buf->offset = 0;
  301. for (i = 0; i < buf->chan->n_subbufs; i++)
  302. buf->padding[i] = 0;
  303. buf->chan->cb->subbuf_start(buf, buf->data, NULL, 0);
  304. }
  305. /**
  306. * relay_reset - reset the channel
  307. * @chan: the channel
  308. *
  309. * This has the effect of erasing all data from all channel buffers
  310. * and restarting the channel in its initial state. The buffers
  311. * are not freed, so any mappings are still in effect.
  312. *
  313. * NOTE: Care should be taken that the channel isn't actually
  314. * being used by anything when this call is made.
  315. */
  316. void relay_reset(struct rchan *chan)
  317. {
  318. unsigned int i;
  319. struct rchan_buf *prev = NULL;
  320. if (!chan)
  321. return;
  322. for (i = 0; i < NR_CPUS; i++) {
  323. if (!chan->buf[i] || chan->buf[i] == prev)
  324. break;
  325. __relay_reset(chan->buf[i], 0);
  326. prev = chan->buf[i];
  327. }
  328. }
  329. EXPORT_SYMBOL_GPL(relay_reset);
  330. /*
  331. * relay_open_buf - create a new relay channel buffer
  332. *
  333. * Internal - used by relay_open().
  334. */
  335. static struct rchan_buf *relay_open_buf(struct rchan *chan,
  336. const char *filename,
  337. struct dentry *parent,
  338. int *is_global)
  339. {
  340. struct rchan_buf *buf;
  341. struct dentry *dentry;
  342. if (*is_global)
  343. return chan->buf[0];
  344. buf = relay_create_buf(chan);
  345. if (!buf)
  346. return NULL;
  347. /* Create file in fs */
  348. dentry = chan->cb->create_buf_file(filename, parent, S_IRUSR,
  349. buf, is_global);
  350. if (!dentry) {
  351. relay_destroy_buf(buf);
  352. return NULL;
  353. }
  354. buf->dentry = dentry;
  355. __relay_reset(buf, 1);
  356. return buf;
  357. }
  358. /**
  359. * relay_close_buf - close a channel buffer
  360. * @buf: channel buffer
  361. *
  362. * Marks the buffer finalized and restores the default callbacks.
  363. * The channel buffer and channel buffer data structure are then freed
  364. * automatically when the last reference is given up.
  365. */
  366. static inline void relay_close_buf(struct rchan_buf *buf)
  367. {
  368. buf->finalized = 1;
  369. cancel_delayed_work(&buf->wake_readers);
  370. flush_scheduled_work();
  371. kref_put(&buf->kref, relay_remove_buf);
  372. }
  373. static inline void setup_callbacks(struct rchan *chan,
  374. struct rchan_callbacks *cb)
  375. {
  376. if (!cb) {
  377. chan->cb = &default_channel_callbacks;
  378. return;
  379. }
  380. if (!cb->subbuf_start)
  381. cb->subbuf_start = subbuf_start_default_callback;
  382. if (!cb->buf_mapped)
  383. cb->buf_mapped = buf_mapped_default_callback;
  384. if (!cb->buf_unmapped)
  385. cb->buf_unmapped = buf_unmapped_default_callback;
  386. if (!cb->create_buf_file)
  387. cb->create_buf_file = create_buf_file_default_callback;
  388. if (!cb->remove_buf_file)
  389. cb->remove_buf_file = remove_buf_file_default_callback;
  390. chan->cb = cb;
  391. }
  392. /**
  393. * relay_open - create a new relay channel
  394. * @base_filename: base name of files to create
  395. * @parent: dentry of parent directory, %NULL for root directory
  396. * @subbuf_size: size of sub-buffers
  397. * @n_subbufs: number of sub-buffers
  398. * @cb: client callback functions
  399. *
  400. * Returns channel pointer if successful, %NULL otherwise.
  401. *
  402. * Creates a channel buffer for each cpu using the sizes and
  403. * attributes specified. The created channel buffer files
  404. * will be named base_filename0...base_filenameN-1. File
  405. * permissions will be S_IRUSR.
  406. */
  407. struct rchan *relay_open(const char *base_filename,
  408. struct dentry *parent,
  409. size_t subbuf_size,
  410. size_t n_subbufs,
  411. struct rchan_callbacks *cb)
  412. {
  413. unsigned int i;
  414. struct rchan *chan;
  415. char *tmpname;
  416. int is_global = 0;
  417. if (!base_filename)
  418. return NULL;
  419. if (!(subbuf_size && n_subbufs))
  420. return NULL;
  421. chan = kcalloc(1, sizeof(struct rchan), GFP_KERNEL);
  422. if (!chan)
  423. return NULL;
  424. chan->version = RELAYFS_CHANNEL_VERSION;
  425. chan->n_subbufs = n_subbufs;
  426. chan->subbuf_size = subbuf_size;
  427. chan->alloc_size = FIX_SIZE(subbuf_size * n_subbufs);
  428. setup_callbacks(chan, cb);
  429. kref_init(&chan->kref);
  430. tmpname = kmalloc(NAME_MAX + 1, GFP_KERNEL);
  431. if (!tmpname)
  432. goto free_chan;
  433. for_each_online_cpu(i) {
  434. sprintf(tmpname, "%s%d", base_filename, i);
  435. chan->buf[i] = relay_open_buf(chan, tmpname, parent,
  436. &is_global);
  437. if (!chan->buf[i])
  438. goto free_bufs;
  439. chan->buf[i]->cpu = i;
  440. }
  441. kfree(tmpname);
  442. return chan;
  443. free_bufs:
  444. for (i = 0; i < NR_CPUS; i++) {
  445. if (!chan->buf[i])
  446. break;
  447. relay_close_buf(chan->buf[i]);
  448. if (is_global)
  449. break;
  450. }
  451. kfree(tmpname);
  452. free_chan:
  453. kref_put(&chan->kref, relay_destroy_channel);
  454. return NULL;
  455. }
  456. EXPORT_SYMBOL_GPL(relay_open);
  457. /**
  458. * relay_switch_subbuf - switch to a new sub-buffer
  459. * @buf: channel buffer
  460. * @length: size of current event
  461. *
  462. * Returns either the length passed in or 0 if full.
  463. *
  464. * Performs sub-buffer-switch tasks such as invoking callbacks,
  465. * updating padding counts, waking up readers, etc.
  466. */
  467. size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
  468. {
  469. void *old, *new;
  470. size_t old_subbuf, new_subbuf;
  471. if (unlikely(length > buf->chan->subbuf_size))
  472. goto toobig;
  473. if (buf->offset != buf->chan->subbuf_size + 1) {
  474. buf->prev_padding = buf->chan->subbuf_size - buf->offset;
  475. old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
  476. buf->padding[old_subbuf] = buf->prev_padding;
  477. buf->subbufs_produced++;
  478. buf->dentry->d_inode->i_size += buf->chan->subbuf_size -
  479. buf->padding[old_subbuf];
  480. smp_mb();
  481. if (waitqueue_active(&buf->read_wait)) {
  482. PREPARE_WORK(&buf->wake_readers, wakeup_readers, buf);
  483. schedule_delayed_work(&buf->wake_readers, 1);
  484. }
  485. }
  486. old = buf->data;
  487. new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
  488. new = buf->start + new_subbuf * buf->chan->subbuf_size;
  489. buf->offset = 0;
  490. if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) {
  491. buf->offset = buf->chan->subbuf_size + 1;
  492. return 0;
  493. }
  494. buf->data = new;
  495. buf->padding[new_subbuf] = 0;
  496. if (unlikely(length + buf->offset > buf->chan->subbuf_size))
  497. goto toobig;
  498. return length;
  499. toobig:
  500. buf->chan->last_toobig = length;
  501. return 0;
  502. }
  503. EXPORT_SYMBOL_GPL(relay_switch_subbuf);
  504. /**
  505. * relay_subbufs_consumed - update the buffer's sub-buffers-consumed count
  506. * @chan: the channel
  507. * @cpu: the cpu associated with the channel buffer to update
  508. * @subbufs_consumed: number of sub-buffers to add to current buf's count
  509. *
  510. * Adds to the channel buffer's consumed sub-buffer count.
  511. * subbufs_consumed should be the number of sub-buffers newly consumed,
  512. * not the total consumed.
  513. *
  514. * NOTE: Kernel clients don't need to call this function if the channel
  515. * mode is 'overwrite'.
  516. */
  517. void relay_subbufs_consumed(struct rchan *chan,
  518. unsigned int cpu,
  519. size_t subbufs_consumed)
  520. {
  521. struct rchan_buf *buf;
  522. if (!chan)
  523. return;
  524. if (cpu >= NR_CPUS || !chan->buf[cpu])
  525. return;
  526. buf = chan->buf[cpu];
  527. buf->subbufs_consumed += subbufs_consumed;
  528. if (buf->subbufs_consumed > buf->subbufs_produced)
  529. buf->subbufs_consumed = buf->subbufs_produced;
  530. }
  531. EXPORT_SYMBOL_GPL(relay_subbufs_consumed);
  532. /**
  533. * relay_close - close the channel
  534. * @chan: the channel
  535. *
  536. * Closes all channel buffers and frees the channel.
  537. */
  538. void relay_close(struct rchan *chan)
  539. {
  540. unsigned int i;
  541. struct rchan_buf *prev = NULL;
  542. if (!chan)
  543. return;
  544. for (i = 0; i < NR_CPUS; i++) {
  545. if (!chan->buf[i] || chan->buf[i] == prev)
  546. break;
  547. relay_close_buf(chan->buf[i]);
  548. prev = chan->buf[i];
  549. }
  550. if (chan->last_toobig)
  551. printk(KERN_WARNING "relay: one or more items not logged "
  552. "[item size (%Zd) > sub-buffer size (%Zd)]\n",
  553. chan->last_toobig, chan->subbuf_size);
  554. kref_put(&chan->kref, relay_destroy_channel);
  555. }
  556. EXPORT_SYMBOL_GPL(relay_close);
  557. /**
  558. * relay_flush - close the channel
  559. * @chan: the channel
  560. *
  561. * Flushes all channel buffers, i.e. forces buffer switch.
  562. */
  563. void relay_flush(struct rchan *chan)
  564. {
  565. unsigned int i;
  566. struct rchan_buf *prev = NULL;
  567. if (!chan)
  568. return;
  569. for (i = 0; i < NR_CPUS; i++) {
  570. if (!chan->buf[i] || chan->buf[i] == prev)
  571. break;
  572. relay_switch_subbuf(chan->buf[i], 0);
  573. prev = chan->buf[i];
  574. }
  575. }
  576. EXPORT_SYMBOL_GPL(relay_flush);
  577. /**
  578. * relay_file_open - open file op for relay files
  579. * @inode: the inode
  580. * @filp: the file
  581. *
  582. * Increments the channel buffer refcount.
  583. */
  584. static int relay_file_open(struct inode *inode, struct file *filp)
  585. {
  586. struct rchan_buf *buf = inode->i_private;
  587. kref_get(&buf->kref);
  588. filp->private_data = buf;
  589. return 0;
  590. }
  591. /**
  592. * relay_file_mmap - mmap file op for relay files
  593. * @filp: the file
  594. * @vma: the vma describing what to map
  595. *
  596. * Calls upon relay_mmap_buf to map the file into user space.
  597. */
  598. static int relay_file_mmap(struct file *filp, struct vm_area_struct *vma)
  599. {
  600. struct rchan_buf *buf = filp->private_data;
  601. return relay_mmap_buf(buf, vma);
  602. }
  603. /**
  604. * relay_file_poll - poll file op for relay files
  605. * @filp: the file
  606. * @wait: poll table
  607. *
  608. * Poll implemention.
  609. */
  610. static unsigned int relay_file_poll(struct file *filp, poll_table *wait)
  611. {
  612. unsigned int mask = 0;
  613. struct rchan_buf *buf = filp->private_data;
  614. if (buf->finalized)
  615. return POLLERR;
  616. if (filp->f_mode & FMODE_READ) {
  617. poll_wait(filp, &buf->read_wait, wait);
  618. if (!relay_buf_empty(buf))
  619. mask |= POLLIN | POLLRDNORM;
  620. }
  621. return mask;
  622. }
  623. /**
  624. * relay_file_release - release file op for relay files
  625. * @inode: the inode
  626. * @filp: the file
  627. *
  628. * Decrements the channel refcount, as the filesystem is
  629. * no longer using it.
  630. */
  631. static int relay_file_release(struct inode *inode, struct file *filp)
  632. {
  633. struct rchan_buf *buf = filp->private_data;
  634. kref_put(&buf->kref, relay_remove_buf);
  635. return 0;
  636. }
  637. /*
  638. * relay_file_read_consume - update the consumed count for the buffer
  639. */
  640. static void relay_file_read_consume(struct rchan_buf *buf,
  641. size_t read_pos,
  642. size_t bytes_consumed)
  643. {
  644. size_t subbuf_size = buf->chan->subbuf_size;
  645. size_t n_subbufs = buf->chan->n_subbufs;
  646. size_t read_subbuf;
  647. if (buf->bytes_consumed + bytes_consumed > subbuf_size) {
  648. relay_subbufs_consumed(buf->chan, buf->cpu, 1);
  649. buf->bytes_consumed = 0;
  650. }
  651. buf->bytes_consumed += bytes_consumed;
  652. read_subbuf = read_pos / buf->chan->subbuf_size;
  653. if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) {
  654. if ((read_subbuf == buf->subbufs_produced % n_subbufs) &&
  655. (buf->offset == subbuf_size))
  656. return;
  657. relay_subbufs_consumed(buf->chan, buf->cpu, 1);
  658. buf->bytes_consumed = 0;
  659. }
  660. }
  661. /*
  662. * relay_file_read_avail - boolean, are there unconsumed bytes available?
  663. */
  664. static int relay_file_read_avail(struct rchan_buf *buf, size_t read_pos)
  665. {
  666. size_t subbuf_size = buf->chan->subbuf_size;
  667. size_t n_subbufs = buf->chan->n_subbufs;
  668. size_t produced = buf->subbufs_produced;
  669. size_t consumed = buf->subbufs_consumed;
  670. relay_file_read_consume(buf, read_pos, 0);
  671. if (unlikely(buf->offset > subbuf_size)) {
  672. if (produced == consumed)
  673. return 0;
  674. return 1;
  675. }
  676. if (unlikely(produced - consumed >= n_subbufs)) {
  677. consumed = (produced / n_subbufs) * n_subbufs;
  678. buf->subbufs_consumed = consumed;
  679. }
  680. produced = (produced % n_subbufs) * subbuf_size + buf->offset;
  681. consumed = (consumed % n_subbufs) * subbuf_size + buf->bytes_consumed;
  682. if (consumed > produced)
  683. produced += n_subbufs * subbuf_size;
  684. if (consumed == produced)
  685. return 0;
  686. return 1;
  687. }
  688. /**
  689. * relay_file_read_subbuf_avail - return bytes available in sub-buffer
  690. * @read_pos: file read position
  691. * @buf: relay channel buffer
  692. */
  693. static size_t relay_file_read_subbuf_avail(size_t read_pos,
  694. struct rchan_buf *buf)
  695. {
  696. size_t padding, avail = 0;
  697. size_t read_subbuf, read_offset, write_subbuf, write_offset;
  698. size_t subbuf_size = buf->chan->subbuf_size;
  699. write_subbuf = (buf->data - buf->start) / subbuf_size;
  700. write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;
  701. read_subbuf = read_pos / subbuf_size;
  702. read_offset = read_pos % subbuf_size;
  703. padding = buf->padding[read_subbuf];
  704. if (read_subbuf == write_subbuf) {
  705. if (read_offset + padding < write_offset)
  706. avail = write_offset - (read_offset + padding);
  707. } else
  708. avail = (subbuf_size - padding) - read_offset;
  709. return avail;
  710. }
  711. /**
  712. * relay_file_read_start_pos - find the first available byte to read
  713. * @read_pos: file read position
  714. * @buf: relay channel buffer
  715. *
  716. * If the read_pos is in the middle of padding, return the
  717. * position of the first actually available byte, otherwise
  718. * return the original value.
  719. */
  720. static size_t relay_file_read_start_pos(size_t read_pos,
  721. struct rchan_buf *buf)
  722. {
  723. size_t read_subbuf, padding, padding_start, padding_end;
  724. size_t subbuf_size = buf->chan->subbuf_size;
  725. size_t n_subbufs = buf->chan->n_subbufs;
  726. read_subbuf = read_pos / subbuf_size;
  727. padding = buf->padding[read_subbuf];
  728. padding_start = (read_subbuf + 1) * subbuf_size - padding;
  729. padding_end = (read_subbuf + 1) * subbuf_size;
  730. if (read_pos >= padding_start && read_pos < padding_end) {
  731. read_subbuf = (read_subbuf + 1) % n_subbufs;
  732. read_pos = read_subbuf * subbuf_size;
  733. }
  734. return read_pos;
  735. }
  736. /**
  737. * relay_file_read_end_pos - return the new read position
  738. * @read_pos: file read position
  739. * @buf: relay channel buffer
  740. * @count: number of bytes to be read
  741. */
  742. static size_t relay_file_read_end_pos(struct rchan_buf *buf,
  743. size_t read_pos,
  744. size_t count)
  745. {
  746. size_t read_subbuf, padding, end_pos;
  747. size_t subbuf_size = buf->chan->subbuf_size;
  748. size_t n_subbufs = buf->chan->n_subbufs;
  749. read_subbuf = read_pos / subbuf_size;
  750. padding = buf->padding[read_subbuf];
  751. if (read_pos % subbuf_size + count + padding == subbuf_size)
  752. end_pos = (read_subbuf + 1) * subbuf_size;
  753. else
  754. end_pos = read_pos + count;
  755. if (end_pos >= subbuf_size * n_subbufs)
  756. end_pos = 0;
  757. return end_pos;
  758. }
  759. /*
  760. * subbuf_read_actor - read up to one subbuf's worth of data
  761. */
  762. static int subbuf_read_actor(size_t read_start,
  763. struct rchan_buf *buf,
  764. size_t avail,
  765. read_descriptor_t *desc,
  766. read_actor_t actor)
  767. {
  768. void *from;
  769. int ret = 0;
  770. from = buf->start + read_start;
  771. ret = avail;
  772. if (copy_to_user(desc->arg.data, from, avail)) {
  773. desc->error = -EFAULT;
  774. ret = 0;
  775. }
  776. desc->arg.data += ret;
  777. desc->written += ret;
  778. desc->count -= ret;
  779. return ret;
  780. }
  781. /*
  782. * subbuf_send_actor - send up to one subbuf's worth of data
  783. */
  784. static int subbuf_send_actor(size_t read_start,
  785. struct rchan_buf *buf,
  786. size_t avail,
  787. read_descriptor_t *desc,
  788. read_actor_t actor)
  789. {
  790. unsigned long pidx, poff;
  791. unsigned int subbuf_pages;
  792. int ret = 0;
  793. subbuf_pages = buf->chan->alloc_size >> PAGE_SHIFT;
  794. pidx = (read_start / PAGE_SIZE) % subbuf_pages;
  795. poff = read_start & ~PAGE_MASK;
  796. while (avail) {
  797. struct page *p = buf->page_array[pidx];
  798. unsigned int len;
  799. len = PAGE_SIZE - poff;
  800. if (len > avail)
  801. len = avail;
  802. len = actor(desc, p, poff, len);
  803. if (desc->error)
  804. break;
  805. avail -= len;
  806. ret += len;
  807. poff = 0;
  808. pidx = (pidx + 1) % subbuf_pages;
  809. }
  810. return ret;
  811. }
  812. typedef int (*subbuf_actor_t) (size_t read_start,
  813. struct rchan_buf *buf,
  814. size_t avail,
  815. read_descriptor_t *desc,
  816. read_actor_t actor);
  817. /*
  818. * relay_file_read_subbufs - read count bytes, bridging subbuf boundaries
  819. */
  820. static inline ssize_t relay_file_read_subbufs(struct file *filp,
  821. loff_t *ppos,
  822. size_t count,
  823. subbuf_actor_t subbuf_actor,
  824. read_actor_t actor,
  825. void *target)
  826. {
  827. struct rchan_buf *buf = filp->private_data;
  828. size_t read_start, avail;
  829. read_descriptor_t desc;
  830. int ret;
  831. if (!count)
  832. return 0;
  833. desc.written = 0;
  834. desc.count = count;
  835. desc.arg.data = target;
  836. desc.error = 0;
  837. mutex_lock(&filp->f_dentry->d_inode->i_mutex);
  838. do {
  839. if (!relay_file_read_avail(buf, *ppos))
  840. break;
  841. read_start = relay_file_read_start_pos(*ppos, buf);
  842. avail = relay_file_read_subbuf_avail(read_start, buf);
  843. if (!avail)
  844. break;
  845. avail = min(desc.count, avail);
  846. ret = subbuf_actor(read_start, buf, avail, &desc, actor);
  847. if (desc.error < 0)
  848. break;
  849. if (ret) {
  850. relay_file_read_consume(buf, read_start, ret);
  851. *ppos = relay_file_read_end_pos(buf, read_start, ret);
  852. }
  853. } while (desc.count && ret);
  854. mutex_unlock(&filp->f_dentry->d_inode->i_mutex);
  855. return desc.written;
  856. }
  857. static ssize_t relay_file_read(struct file *filp,
  858. char __user *buffer,
  859. size_t count,
  860. loff_t *ppos)
  861. {
  862. return relay_file_read_subbufs(filp, ppos, count, subbuf_read_actor,
  863. NULL, buffer);
  864. }
  865. static ssize_t relay_file_sendfile(struct file *filp,
  866. loff_t *ppos,
  867. size_t count,
  868. read_actor_t actor,
  869. void *target)
  870. {
  871. return relay_file_read_subbufs(filp, ppos, count, subbuf_send_actor,
  872. actor, target);
  873. }
  874. struct file_operations relay_file_operations = {
  875. .open = relay_file_open,
  876. .poll = relay_file_poll,
  877. .mmap = relay_file_mmap,
  878. .read = relay_file_read,
  879. .llseek = no_llseek,
  880. .release = relay_file_release,
  881. .sendfile = relay_file_sendfile,
  882. };
  883. EXPORT_SYMBOL_GPL(relay_file_operations);