relay.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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 = kzalloc(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. * @work: work struct that contains the 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(struct work_struct *work)
  273. {
  274. struct rchan_buf *buf =
  275. container_of(work, struct rchan_buf, wake_readers.work);
  276. wake_up_interruptible(&buf->read_wait);
  277. }
  278. /**
  279. * __relay_reset - reset a channel buffer
  280. * @buf: the channel buffer
  281. * @init: 1 if this is a first-time initialization
  282. *
  283. * See relay_reset for description of effect.
  284. */
  285. static void __relay_reset(struct rchan_buf *buf, unsigned int init)
  286. {
  287. size_t i;
  288. if (init) {
  289. init_waitqueue_head(&buf->read_wait);
  290. kref_init(&buf->kref);
  291. INIT_DELAYED_WORK(&buf->wake_readers, NULL);
  292. } else {
  293. cancel_delayed_work(&buf->wake_readers);
  294. flush_scheduled_work();
  295. }
  296. buf->subbufs_produced = 0;
  297. buf->subbufs_consumed = 0;
  298. buf->bytes_consumed = 0;
  299. buf->finalized = 0;
  300. buf->data = buf->start;
  301. buf->offset = 0;
  302. for (i = 0; i < buf->chan->n_subbufs; i++)
  303. buf->padding[i] = 0;
  304. buf->chan->cb->subbuf_start(buf, buf->data, NULL, 0);
  305. }
  306. /**
  307. * relay_reset - reset the channel
  308. * @chan: the channel
  309. *
  310. * This has the effect of erasing all data from all channel buffers
  311. * and restarting the channel in its initial state. The buffers
  312. * are not freed, so any mappings are still in effect.
  313. *
  314. * NOTE: Care should be taken that the channel isn't actually
  315. * being used by anything when this call is made.
  316. */
  317. void relay_reset(struct rchan *chan)
  318. {
  319. unsigned int i;
  320. struct rchan_buf *prev = NULL;
  321. if (!chan)
  322. return;
  323. for (i = 0; i < NR_CPUS; i++) {
  324. if (!chan->buf[i] || chan->buf[i] == prev)
  325. break;
  326. __relay_reset(chan->buf[i], 0);
  327. prev = chan->buf[i];
  328. }
  329. }
  330. EXPORT_SYMBOL_GPL(relay_reset);
  331. /*
  332. * relay_open_buf - create a new relay channel buffer
  333. *
  334. * Internal - used by relay_open().
  335. */
  336. static struct rchan_buf *relay_open_buf(struct rchan *chan,
  337. const char *filename,
  338. struct dentry *parent,
  339. int *is_global)
  340. {
  341. struct rchan_buf *buf;
  342. struct dentry *dentry;
  343. if (*is_global)
  344. return chan->buf[0];
  345. buf = relay_create_buf(chan);
  346. if (!buf)
  347. return NULL;
  348. /* Create file in fs */
  349. dentry = chan->cb->create_buf_file(filename, parent, S_IRUSR,
  350. buf, is_global);
  351. if (!dentry) {
  352. relay_destroy_buf(buf);
  353. return NULL;
  354. }
  355. buf->dentry = dentry;
  356. __relay_reset(buf, 1);
  357. return buf;
  358. }
  359. /**
  360. * relay_close_buf - close a channel buffer
  361. * @buf: channel buffer
  362. *
  363. * Marks the buffer finalized and restores the default callbacks.
  364. * The channel buffer and channel buffer data structure are then freed
  365. * automatically when the last reference is given up.
  366. */
  367. static void relay_close_buf(struct rchan_buf *buf)
  368. {
  369. buf->finalized = 1;
  370. cancel_delayed_work(&buf->wake_readers);
  371. flush_scheduled_work();
  372. kref_put(&buf->kref, relay_remove_buf);
  373. }
  374. static void setup_callbacks(struct rchan *chan,
  375. struct rchan_callbacks *cb)
  376. {
  377. if (!cb) {
  378. chan->cb = &default_channel_callbacks;
  379. return;
  380. }
  381. if (!cb->subbuf_start)
  382. cb->subbuf_start = subbuf_start_default_callback;
  383. if (!cb->buf_mapped)
  384. cb->buf_mapped = buf_mapped_default_callback;
  385. if (!cb->buf_unmapped)
  386. cb->buf_unmapped = buf_unmapped_default_callback;
  387. if (!cb->create_buf_file)
  388. cb->create_buf_file = create_buf_file_default_callback;
  389. if (!cb->remove_buf_file)
  390. cb->remove_buf_file = remove_buf_file_default_callback;
  391. chan->cb = cb;
  392. }
  393. /**
  394. * relay_open - create a new relay channel
  395. * @base_filename: base name of files to create
  396. * @parent: dentry of parent directory, %NULL for root directory
  397. * @subbuf_size: size of sub-buffers
  398. * @n_subbufs: number of sub-buffers
  399. * @cb: client callback functions
  400. *
  401. * Returns channel pointer if successful, %NULL otherwise.
  402. *
  403. * Creates a channel buffer for each cpu using the sizes and
  404. * attributes specified. The created channel buffer files
  405. * will be named base_filename0...base_filenameN-1. File
  406. * permissions will be S_IRUSR.
  407. */
  408. struct rchan *relay_open(const char *base_filename,
  409. struct dentry *parent,
  410. size_t subbuf_size,
  411. size_t n_subbufs,
  412. struct rchan_callbacks *cb)
  413. {
  414. unsigned int i;
  415. struct rchan *chan;
  416. char *tmpname;
  417. int is_global = 0;
  418. if (!base_filename)
  419. return NULL;
  420. if (!(subbuf_size && n_subbufs))
  421. return NULL;
  422. chan = kzalloc(sizeof(struct rchan), GFP_KERNEL);
  423. if (!chan)
  424. return NULL;
  425. chan->version = RELAYFS_CHANNEL_VERSION;
  426. chan->n_subbufs = n_subbufs;
  427. chan->subbuf_size = subbuf_size;
  428. chan->alloc_size = FIX_SIZE(subbuf_size * n_subbufs);
  429. setup_callbacks(chan, cb);
  430. kref_init(&chan->kref);
  431. tmpname = kmalloc(NAME_MAX + 1, GFP_KERNEL);
  432. if (!tmpname)
  433. goto free_chan;
  434. for_each_online_cpu(i) {
  435. sprintf(tmpname, "%s%d", base_filename, i);
  436. chan->buf[i] = relay_open_buf(chan, tmpname, parent,
  437. &is_global);
  438. if (!chan->buf[i])
  439. goto free_bufs;
  440. chan->buf[i]->cpu = i;
  441. }
  442. kfree(tmpname);
  443. return chan;
  444. free_bufs:
  445. for (i = 0; i < NR_CPUS; i++) {
  446. if (!chan->buf[i])
  447. break;
  448. relay_close_buf(chan->buf[i]);
  449. if (is_global)
  450. break;
  451. }
  452. kfree(tmpname);
  453. free_chan:
  454. kref_put(&chan->kref, relay_destroy_channel);
  455. return NULL;
  456. }
  457. EXPORT_SYMBOL_GPL(relay_open);
  458. /**
  459. * relay_switch_subbuf - switch to a new sub-buffer
  460. * @buf: channel buffer
  461. * @length: size of current event
  462. *
  463. * Returns either the length passed in or 0 if full.
  464. *
  465. * Performs sub-buffer-switch tasks such as invoking callbacks,
  466. * updating padding counts, waking up readers, etc.
  467. */
  468. size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
  469. {
  470. void *old, *new;
  471. size_t old_subbuf, new_subbuf;
  472. if (unlikely(length > buf->chan->subbuf_size))
  473. goto toobig;
  474. if (buf->offset != buf->chan->subbuf_size + 1) {
  475. buf->prev_padding = buf->chan->subbuf_size - buf->offset;
  476. old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
  477. buf->padding[old_subbuf] = buf->prev_padding;
  478. buf->subbufs_produced++;
  479. buf->dentry->d_inode->i_size += buf->chan->subbuf_size -
  480. buf->padding[old_subbuf];
  481. smp_mb();
  482. if (waitqueue_active(&buf->read_wait)) {
  483. PREPARE_DELAYED_WORK(&buf->wake_readers,
  484. wakeup_readers);
  485. schedule_delayed_work(&buf->wake_readers, 1);
  486. }
  487. }
  488. old = buf->data;
  489. new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
  490. new = buf->start + new_subbuf * buf->chan->subbuf_size;
  491. buf->offset = 0;
  492. if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) {
  493. buf->offset = buf->chan->subbuf_size + 1;
  494. return 0;
  495. }
  496. buf->data = new;
  497. buf->padding[new_subbuf] = 0;
  498. if (unlikely(length + buf->offset > buf->chan->subbuf_size))
  499. goto toobig;
  500. return length;
  501. toobig:
  502. buf->chan->last_toobig = length;
  503. return 0;
  504. }
  505. EXPORT_SYMBOL_GPL(relay_switch_subbuf);
  506. /**
  507. * relay_subbufs_consumed - update the buffer's sub-buffers-consumed count
  508. * @chan: the channel
  509. * @cpu: the cpu associated with the channel buffer to update
  510. * @subbufs_consumed: number of sub-buffers to add to current buf's count
  511. *
  512. * Adds to the channel buffer's consumed sub-buffer count.
  513. * subbufs_consumed should be the number of sub-buffers newly consumed,
  514. * not the total consumed.
  515. *
  516. * NOTE: Kernel clients don't need to call this function if the channel
  517. * mode is 'overwrite'.
  518. */
  519. void relay_subbufs_consumed(struct rchan *chan,
  520. unsigned int cpu,
  521. size_t subbufs_consumed)
  522. {
  523. struct rchan_buf *buf;
  524. if (!chan)
  525. return;
  526. if (cpu >= NR_CPUS || !chan->buf[cpu])
  527. return;
  528. buf = chan->buf[cpu];
  529. buf->subbufs_consumed += subbufs_consumed;
  530. if (buf->subbufs_consumed > buf->subbufs_produced)
  531. buf->subbufs_consumed = buf->subbufs_produced;
  532. }
  533. EXPORT_SYMBOL_GPL(relay_subbufs_consumed);
  534. /**
  535. * relay_close - close the channel
  536. * @chan: the channel
  537. *
  538. * Closes all channel buffers and frees the channel.
  539. */
  540. void relay_close(struct rchan *chan)
  541. {
  542. unsigned int i;
  543. struct rchan_buf *prev = NULL;
  544. if (!chan)
  545. return;
  546. for (i = 0; i < NR_CPUS; i++) {
  547. if (!chan->buf[i] || chan->buf[i] == prev)
  548. break;
  549. relay_close_buf(chan->buf[i]);
  550. prev = chan->buf[i];
  551. }
  552. if (chan->last_toobig)
  553. printk(KERN_WARNING "relay: one or more items not logged "
  554. "[item size (%Zd) > sub-buffer size (%Zd)]\n",
  555. chan->last_toobig, chan->subbuf_size);
  556. kref_put(&chan->kref, relay_destroy_channel);
  557. }
  558. EXPORT_SYMBOL_GPL(relay_close);
  559. /**
  560. * relay_flush - close the channel
  561. * @chan: the channel
  562. *
  563. * Flushes all channel buffers, i.e. forces buffer switch.
  564. */
  565. void relay_flush(struct rchan *chan)
  566. {
  567. unsigned int i;
  568. struct rchan_buf *prev = NULL;
  569. if (!chan)
  570. return;
  571. for (i = 0; i < NR_CPUS; i++) {
  572. if (!chan->buf[i] || chan->buf[i] == prev)
  573. break;
  574. relay_switch_subbuf(chan->buf[i], 0);
  575. prev = chan->buf[i];
  576. }
  577. }
  578. EXPORT_SYMBOL_GPL(relay_flush);
  579. /**
  580. * relay_file_open - open file op for relay files
  581. * @inode: the inode
  582. * @filp: the file
  583. *
  584. * Increments the channel buffer refcount.
  585. */
  586. static int relay_file_open(struct inode *inode, struct file *filp)
  587. {
  588. struct rchan_buf *buf = inode->i_private;
  589. kref_get(&buf->kref);
  590. filp->private_data = buf;
  591. return 0;
  592. }
  593. /**
  594. * relay_file_mmap - mmap file op for relay files
  595. * @filp: the file
  596. * @vma: the vma describing what to map
  597. *
  598. * Calls upon relay_mmap_buf to map the file into user space.
  599. */
  600. static int relay_file_mmap(struct file *filp, struct vm_area_struct *vma)
  601. {
  602. struct rchan_buf *buf = filp->private_data;
  603. return relay_mmap_buf(buf, vma);
  604. }
  605. /**
  606. * relay_file_poll - poll file op for relay files
  607. * @filp: the file
  608. * @wait: poll table
  609. *
  610. * Poll implemention.
  611. */
  612. static unsigned int relay_file_poll(struct file *filp, poll_table *wait)
  613. {
  614. unsigned int mask = 0;
  615. struct rchan_buf *buf = filp->private_data;
  616. if (buf->finalized)
  617. return POLLERR;
  618. if (filp->f_mode & FMODE_READ) {
  619. poll_wait(filp, &buf->read_wait, wait);
  620. if (!relay_buf_empty(buf))
  621. mask |= POLLIN | POLLRDNORM;
  622. }
  623. return mask;
  624. }
  625. /**
  626. * relay_file_release - release file op for relay files
  627. * @inode: the inode
  628. * @filp: the file
  629. *
  630. * Decrements the channel refcount, as the filesystem is
  631. * no longer using it.
  632. */
  633. static int relay_file_release(struct inode *inode, struct file *filp)
  634. {
  635. struct rchan_buf *buf = filp->private_data;
  636. kref_put(&buf->kref, relay_remove_buf);
  637. return 0;
  638. }
  639. /*
  640. * relay_file_read_consume - update the consumed count for the buffer
  641. */
  642. static void relay_file_read_consume(struct rchan_buf *buf,
  643. size_t read_pos,
  644. size_t bytes_consumed)
  645. {
  646. size_t subbuf_size = buf->chan->subbuf_size;
  647. size_t n_subbufs = buf->chan->n_subbufs;
  648. size_t read_subbuf;
  649. if (buf->bytes_consumed + bytes_consumed > subbuf_size) {
  650. relay_subbufs_consumed(buf->chan, buf->cpu, 1);
  651. buf->bytes_consumed = 0;
  652. }
  653. buf->bytes_consumed += bytes_consumed;
  654. read_subbuf = read_pos / buf->chan->subbuf_size;
  655. if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) {
  656. if ((read_subbuf == buf->subbufs_produced % n_subbufs) &&
  657. (buf->offset == subbuf_size))
  658. return;
  659. relay_subbufs_consumed(buf->chan, buf->cpu, 1);
  660. buf->bytes_consumed = 0;
  661. }
  662. }
  663. /*
  664. * relay_file_read_avail - boolean, are there unconsumed bytes available?
  665. */
  666. static int relay_file_read_avail(struct rchan_buf *buf, size_t read_pos)
  667. {
  668. size_t subbuf_size = buf->chan->subbuf_size;
  669. size_t n_subbufs = buf->chan->n_subbufs;
  670. size_t produced = buf->subbufs_produced;
  671. size_t consumed = buf->subbufs_consumed;
  672. relay_file_read_consume(buf, read_pos, 0);
  673. if (unlikely(buf->offset > subbuf_size)) {
  674. if (produced == consumed)
  675. return 0;
  676. return 1;
  677. }
  678. if (unlikely(produced - consumed >= n_subbufs)) {
  679. consumed = (produced / n_subbufs) * n_subbufs;
  680. buf->subbufs_consumed = consumed;
  681. }
  682. produced = (produced % n_subbufs) * subbuf_size + buf->offset;
  683. consumed = (consumed % n_subbufs) * subbuf_size + buf->bytes_consumed;
  684. if (consumed > produced)
  685. produced += n_subbufs * subbuf_size;
  686. if (consumed == produced)
  687. return 0;
  688. return 1;
  689. }
  690. /**
  691. * relay_file_read_subbuf_avail - return bytes available in sub-buffer
  692. * @read_pos: file read position
  693. * @buf: relay channel buffer
  694. */
  695. static size_t relay_file_read_subbuf_avail(size_t read_pos,
  696. struct rchan_buf *buf)
  697. {
  698. size_t padding, avail = 0;
  699. size_t read_subbuf, read_offset, write_subbuf, write_offset;
  700. size_t subbuf_size = buf->chan->subbuf_size;
  701. write_subbuf = (buf->data - buf->start) / subbuf_size;
  702. write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;
  703. read_subbuf = read_pos / subbuf_size;
  704. read_offset = read_pos % subbuf_size;
  705. padding = buf->padding[read_subbuf];
  706. if (read_subbuf == write_subbuf) {
  707. if (read_offset + padding < write_offset)
  708. avail = write_offset - (read_offset + padding);
  709. } else
  710. avail = (subbuf_size - padding) - read_offset;
  711. return avail;
  712. }
  713. /**
  714. * relay_file_read_start_pos - find the first available byte to read
  715. * @read_pos: file read position
  716. * @buf: relay channel buffer
  717. *
  718. * If the read_pos is in the middle of padding, return the
  719. * position of the first actually available byte, otherwise
  720. * return the original value.
  721. */
  722. static size_t relay_file_read_start_pos(size_t read_pos,
  723. struct rchan_buf *buf)
  724. {
  725. size_t read_subbuf, padding, padding_start, padding_end;
  726. size_t subbuf_size = buf->chan->subbuf_size;
  727. size_t n_subbufs = buf->chan->n_subbufs;
  728. read_subbuf = read_pos / subbuf_size;
  729. padding = buf->padding[read_subbuf];
  730. padding_start = (read_subbuf + 1) * subbuf_size - padding;
  731. padding_end = (read_subbuf + 1) * subbuf_size;
  732. if (read_pos >= padding_start && read_pos < padding_end) {
  733. read_subbuf = (read_subbuf + 1) % n_subbufs;
  734. read_pos = read_subbuf * subbuf_size;
  735. }
  736. return read_pos;
  737. }
  738. /**
  739. * relay_file_read_end_pos - return the new read position
  740. * @read_pos: file read position
  741. * @buf: relay channel buffer
  742. * @count: number of bytes to be read
  743. */
  744. static size_t relay_file_read_end_pos(struct rchan_buf *buf,
  745. size_t read_pos,
  746. size_t count)
  747. {
  748. size_t read_subbuf, padding, end_pos;
  749. size_t subbuf_size = buf->chan->subbuf_size;
  750. size_t n_subbufs = buf->chan->n_subbufs;
  751. read_subbuf = read_pos / subbuf_size;
  752. padding = buf->padding[read_subbuf];
  753. if (read_pos % subbuf_size + count + padding == subbuf_size)
  754. end_pos = (read_subbuf + 1) * subbuf_size;
  755. else
  756. end_pos = read_pos + count;
  757. if (end_pos >= subbuf_size * n_subbufs)
  758. end_pos = 0;
  759. return end_pos;
  760. }
  761. /*
  762. * subbuf_read_actor - read up to one subbuf's worth of data
  763. */
  764. static int subbuf_read_actor(size_t read_start,
  765. struct rchan_buf *buf,
  766. size_t avail,
  767. read_descriptor_t *desc,
  768. read_actor_t actor)
  769. {
  770. void *from;
  771. int ret = 0;
  772. from = buf->start + read_start;
  773. ret = avail;
  774. if (copy_to_user(desc->arg.buf, from, avail)) {
  775. desc->error = -EFAULT;
  776. ret = 0;
  777. }
  778. desc->arg.data += ret;
  779. desc->written += ret;
  780. desc->count -= ret;
  781. return ret;
  782. }
  783. /*
  784. * subbuf_send_actor - send up to one subbuf's worth of data
  785. */
  786. static int subbuf_send_actor(size_t read_start,
  787. struct rchan_buf *buf,
  788. size_t avail,
  789. read_descriptor_t *desc,
  790. read_actor_t actor)
  791. {
  792. unsigned long pidx, poff;
  793. unsigned int subbuf_pages;
  794. int ret = 0;
  795. subbuf_pages = buf->chan->alloc_size >> PAGE_SHIFT;
  796. pidx = (read_start / PAGE_SIZE) % subbuf_pages;
  797. poff = read_start & ~PAGE_MASK;
  798. while (avail) {
  799. struct page *p = buf->page_array[pidx];
  800. unsigned int len;
  801. len = PAGE_SIZE - poff;
  802. if (len > avail)
  803. len = avail;
  804. len = actor(desc, p, poff, len);
  805. if (desc->error)
  806. break;
  807. avail -= len;
  808. ret += len;
  809. poff = 0;
  810. pidx = (pidx + 1) % subbuf_pages;
  811. }
  812. return ret;
  813. }
  814. typedef int (*subbuf_actor_t) (size_t read_start,
  815. struct rchan_buf *buf,
  816. size_t avail,
  817. read_descriptor_t *desc,
  818. read_actor_t actor);
  819. /*
  820. * relay_file_read_subbufs - read count bytes, bridging subbuf boundaries
  821. */
  822. static ssize_t relay_file_read_subbufs(struct file *filp, loff_t *ppos,
  823. subbuf_actor_t subbuf_actor,
  824. read_actor_t actor,
  825. read_descriptor_t *desc)
  826. {
  827. struct rchan_buf *buf = filp->private_data;
  828. size_t read_start, avail;
  829. int ret;
  830. if (!desc->count)
  831. return 0;
  832. mutex_lock(&filp->f_path.dentry->d_inode->i_mutex);
  833. do {
  834. if (!relay_file_read_avail(buf, *ppos))
  835. break;
  836. read_start = relay_file_read_start_pos(*ppos, buf);
  837. avail = relay_file_read_subbuf_avail(read_start, buf);
  838. if (!avail)
  839. break;
  840. avail = min(desc->count, avail);
  841. ret = subbuf_actor(read_start, buf, avail, desc, actor);
  842. if (desc->error < 0)
  843. break;
  844. if (ret) {
  845. relay_file_read_consume(buf, read_start, ret);
  846. *ppos = relay_file_read_end_pos(buf, read_start, ret);
  847. }
  848. } while (desc->count && ret);
  849. mutex_unlock(&filp->f_path.dentry->d_inode->i_mutex);
  850. return desc->written;
  851. }
  852. static ssize_t relay_file_read(struct file *filp,
  853. char __user *buffer,
  854. size_t count,
  855. loff_t *ppos)
  856. {
  857. read_descriptor_t desc;
  858. desc.written = 0;
  859. desc.count = count;
  860. desc.arg.buf = buffer;
  861. desc.error = 0;
  862. return relay_file_read_subbufs(filp, ppos, subbuf_read_actor,
  863. NULL, &desc);
  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. read_descriptor_t desc;
  872. desc.written = 0;
  873. desc.count = count;
  874. desc.arg.data = target;
  875. desc.error = 0;
  876. return relay_file_read_subbufs(filp, ppos, subbuf_send_actor,
  877. actor, &desc);
  878. }
  879. const struct file_operations relay_file_operations = {
  880. .open = relay_file_open,
  881. .poll = relay_file_poll,
  882. .mmap = relay_file_mmap,
  883. .read = relay_file_read,
  884. .llseek = no_llseek,
  885. .release = relay_file_release,
  886. .sendfile = relay_file_sendfile,
  887. };
  888. EXPORT_SYMBOL_GPL(relay_file_operations);