relay.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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(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 inline 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 inline 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 inline 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 = kcalloc(1, 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 inline ssize_t relay_file_read_subbufs(struct file *filp,
  823. loff_t *ppos,
  824. subbuf_actor_t subbuf_actor,
  825. read_actor_t actor,
  826. read_descriptor_t *desc)
  827. {
  828. struct rchan_buf *buf = filp->private_data;
  829. size_t read_start, avail;
  830. int ret;
  831. if (!desc->count)
  832. return 0;
  833. mutex_lock(&filp->f_path.dentry->d_inode->i_mutex);
  834. do {
  835. if (!relay_file_read_avail(buf, *ppos))
  836. break;
  837. read_start = relay_file_read_start_pos(*ppos, buf);
  838. avail = relay_file_read_subbuf_avail(read_start, buf);
  839. if (!avail)
  840. break;
  841. avail = min(desc->count, avail);
  842. ret = subbuf_actor(read_start, buf, avail, desc, actor);
  843. if (desc->error < 0)
  844. break;
  845. if (ret) {
  846. relay_file_read_consume(buf, read_start, ret);
  847. *ppos = relay_file_read_end_pos(buf, read_start, ret);
  848. }
  849. } while (desc->count && ret);
  850. mutex_unlock(&filp->f_path.dentry->d_inode->i_mutex);
  851. return desc->written;
  852. }
  853. static ssize_t relay_file_read(struct file *filp,
  854. char __user *buffer,
  855. size_t count,
  856. loff_t *ppos)
  857. {
  858. read_descriptor_t desc;
  859. desc.written = 0;
  860. desc.count = count;
  861. desc.arg.buf = buffer;
  862. desc.error = 0;
  863. return relay_file_read_subbufs(filp, ppos, subbuf_read_actor,
  864. NULL, &desc);
  865. }
  866. static ssize_t relay_file_sendfile(struct file *filp,
  867. loff_t *ppos,
  868. size_t count,
  869. read_actor_t actor,
  870. void *target)
  871. {
  872. read_descriptor_t desc;
  873. desc.written = 0;
  874. desc.count = count;
  875. desc.arg.data = target;
  876. desc.error = 0;
  877. return relay_file_read_subbufs(filp, ppos, subbuf_send_actor,
  878. actor, &desc);
  879. }
  880. const struct file_operations relay_file_operations = {
  881. .open = relay_file_open,
  882. .poll = relay_file_poll,
  883. .mmap = relay_file_mmap,
  884. .read = relay_file_read,
  885. .llseek = no_llseek,
  886. .release = relay_file_release,
  887. .sendfile = relay_file_sendfile,
  888. };
  889. EXPORT_SYMBOL_GPL(relay_file_operations);