relay.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388
  1. /*
  2. * Public API and common code for kernel->userspace relay file support.
  3. *
  4. * See Documentation/filesystems/relay.txt for an overview.
  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. * November 2006 - CPU hotplug support by Mathieu Desnoyers
  11. * (mathieu.desnoyers@polymtl.ca)
  12. *
  13. * This file is released under the GPL.
  14. */
  15. #include <linux/errno.h>
  16. #include <linux/stddef.h>
  17. #include <linux/slab.h>
  18. #include <linux/export.h>
  19. #include <linux/string.h>
  20. #include <linux/relay.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/mm.h>
  23. #include <linux/cpu.h>
  24. #include <linux/splice.h>
  25. /* list of open channels, for cpu hotplug */
  26. static DEFINE_MUTEX(relay_channels_mutex);
  27. static LIST_HEAD(relay_channels);
  28. /*
  29. * close() vm_op implementation for relay file mapping.
  30. */
  31. static void relay_file_mmap_close(struct vm_area_struct *vma)
  32. {
  33. struct rchan_buf *buf = vma->vm_private_data;
  34. buf->chan->cb->buf_unmapped(buf, vma->vm_file);
  35. }
  36. /*
  37. * fault() vm_op implementation for relay file mapping.
  38. */
  39. static int relay_buf_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  40. {
  41. struct page *page;
  42. struct rchan_buf *buf = vma->vm_private_data;
  43. pgoff_t pgoff = vmf->pgoff;
  44. if (!buf)
  45. return VM_FAULT_OOM;
  46. page = vmalloc_to_page(buf->start + (pgoff << PAGE_SHIFT));
  47. if (!page)
  48. return VM_FAULT_SIGBUS;
  49. get_page(page);
  50. vmf->page = page;
  51. return 0;
  52. }
  53. /*
  54. * vm_ops for relay file mappings.
  55. */
  56. static const struct vm_operations_struct relay_file_mmap_ops = {
  57. .fault = relay_buf_fault,
  58. .close = relay_file_mmap_close,
  59. };
  60. /*
  61. * allocate an array of pointers of struct page
  62. */
  63. static struct page **relay_alloc_page_array(unsigned int n_pages)
  64. {
  65. const size_t pa_size = n_pages * sizeof(struct page *);
  66. if (pa_size > PAGE_SIZE)
  67. return vzalloc(pa_size);
  68. return kzalloc(pa_size, GFP_KERNEL);
  69. }
  70. /*
  71. * free an array of pointers of struct page
  72. */
  73. static void relay_free_page_array(struct page **array)
  74. {
  75. kvfree(array);
  76. }
  77. /**
  78. * relay_mmap_buf: - mmap channel buffer to process address space
  79. * @buf: relay channel buffer
  80. * @vma: vm_area_struct describing memory to be mapped
  81. *
  82. * Returns 0 if ok, negative on error
  83. *
  84. * Caller should already have grabbed mmap_sem.
  85. */
  86. static int relay_mmap_buf(struct rchan_buf *buf, struct vm_area_struct *vma)
  87. {
  88. unsigned long length = vma->vm_end - vma->vm_start;
  89. struct file *filp = vma->vm_file;
  90. if (!buf)
  91. return -EBADF;
  92. if (length != (unsigned long)buf->chan->alloc_size)
  93. return -EINVAL;
  94. vma->vm_ops = &relay_file_mmap_ops;
  95. vma->vm_flags |= VM_DONTEXPAND;
  96. vma->vm_private_data = buf;
  97. buf->chan->cb->buf_mapped(buf, filp);
  98. return 0;
  99. }
  100. /**
  101. * relay_alloc_buf - allocate a channel buffer
  102. * @buf: the buffer struct
  103. * @size: total size of the buffer
  104. *
  105. * Returns a pointer to the resulting buffer, %NULL if unsuccessful. The
  106. * passed in size will get page aligned, if it isn't already.
  107. */
  108. static void *relay_alloc_buf(struct rchan_buf *buf, size_t *size)
  109. {
  110. void *mem;
  111. unsigned int i, j, n_pages;
  112. *size = PAGE_ALIGN(*size);
  113. n_pages = *size >> PAGE_SHIFT;
  114. buf->page_array = relay_alloc_page_array(n_pages);
  115. if (!buf->page_array)
  116. return NULL;
  117. for (i = 0; i < n_pages; i++) {
  118. buf->page_array[i] = alloc_page(GFP_KERNEL);
  119. if (unlikely(!buf->page_array[i]))
  120. goto depopulate;
  121. set_page_private(buf->page_array[i], (unsigned long)buf);
  122. }
  123. mem = vmap(buf->page_array, n_pages, VM_MAP, PAGE_KERNEL);
  124. if (!mem)
  125. goto depopulate;
  126. memset(mem, 0, *size);
  127. buf->page_count = n_pages;
  128. return mem;
  129. depopulate:
  130. for (j = 0; j < i; j++)
  131. __free_page(buf->page_array[j]);
  132. relay_free_page_array(buf->page_array);
  133. return NULL;
  134. }
  135. /**
  136. * relay_create_buf - allocate and initialize a channel buffer
  137. * @chan: the relay channel
  138. *
  139. * Returns channel buffer if successful, %NULL otherwise.
  140. */
  141. static struct rchan_buf *relay_create_buf(struct rchan *chan)
  142. {
  143. struct rchan_buf *buf;
  144. if (chan->n_subbufs > UINT_MAX / sizeof(size_t *))
  145. return NULL;
  146. buf = kzalloc(sizeof(struct rchan_buf), GFP_KERNEL);
  147. if (!buf)
  148. return NULL;
  149. buf->padding = kmalloc(chan->n_subbufs * sizeof(size_t *), GFP_KERNEL);
  150. if (!buf->padding)
  151. goto free_buf;
  152. buf->start = relay_alloc_buf(buf, &chan->alloc_size);
  153. if (!buf->start)
  154. goto free_buf;
  155. buf->chan = chan;
  156. kref_get(&buf->chan->kref);
  157. return buf;
  158. free_buf:
  159. kfree(buf->padding);
  160. kfree(buf);
  161. return NULL;
  162. }
  163. /**
  164. * relay_destroy_channel - free the channel struct
  165. * @kref: target kernel reference that contains the relay channel
  166. *
  167. * Should only be called from kref_put().
  168. */
  169. static void relay_destroy_channel(struct kref *kref)
  170. {
  171. struct rchan *chan = container_of(kref, struct rchan, kref);
  172. kfree(chan);
  173. }
  174. /**
  175. * relay_destroy_buf - destroy an rchan_buf struct and associated buffer
  176. * @buf: the buffer struct
  177. */
  178. static void relay_destroy_buf(struct rchan_buf *buf)
  179. {
  180. struct rchan *chan = buf->chan;
  181. unsigned int i;
  182. if (likely(buf->start)) {
  183. vunmap(buf->start);
  184. for (i = 0; i < buf->page_count; i++)
  185. __free_page(buf->page_array[i]);
  186. relay_free_page_array(buf->page_array);
  187. }
  188. chan->buf[buf->cpu] = NULL;
  189. kfree(buf->padding);
  190. kfree(buf);
  191. kref_put(&chan->kref, relay_destroy_channel);
  192. }
  193. /**
  194. * relay_remove_buf - remove a channel buffer
  195. * @kref: target kernel reference that contains the relay buffer
  196. *
  197. * Removes the file from the filesystem, which also frees the
  198. * rchan_buf_struct and the channel buffer. Should only be called from
  199. * kref_put().
  200. */
  201. static void relay_remove_buf(struct kref *kref)
  202. {
  203. struct rchan_buf *buf = container_of(kref, struct rchan_buf, kref);
  204. relay_destroy_buf(buf);
  205. }
  206. /**
  207. * relay_buf_empty - boolean, is the channel buffer empty?
  208. * @buf: channel buffer
  209. *
  210. * Returns 1 if the buffer is empty, 0 otherwise.
  211. */
  212. static int relay_buf_empty(struct rchan_buf *buf)
  213. {
  214. return (buf->subbufs_produced - buf->subbufs_consumed) ? 0 : 1;
  215. }
  216. /**
  217. * relay_buf_full - boolean, is the channel buffer full?
  218. * @buf: channel buffer
  219. *
  220. * Returns 1 if the buffer is full, 0 otherwise.
  221. */
  222. int relay_buf_full(struct rchan_buf *buf)
  223. {
  224. size_t ready = buf->subbufs_produced - buf->subbufs_consumed;
  225. return (ready >= buf->chan->n_subbufs) ? 1 : 0;
  226. }
  227. EXPORT_SYMBOL_GPL(relay_buf_full);
  228. /*
  229. * High-level relay kernel API and associated functions.
  230. */
  231. /*
  232. * rchan_callback implementations defining default channel behavior. Used
  233. * in place of corresponding NULL values in client callback struct.
  234. */
  235. /*
  236. * subbuf_start() default callback. Does nothing.
  237. */
  238. static int subbuf_start_default_callback (struct rchan_buf *buf,
  239. void *subbuf,
  240. void *prev_subbuf,
  241. size_t prev_padding)
  242. {
  243. if (relay_buf_full(buf))
  244. return 0;
  245. return 1;
  246. }
  247. /*
  248. * buf_mapped() default callback. Does nothing.
  249. */
  250. static void buf_mapped_default_callback(struct rchan_buf *buf,
  251. struct file *filp)
  252. {
  253. }
  254. /*
  255. * buf_unmapped() default callback. Does nothing.
  256. */
  257. static void buf_unmapped_default_callback(struct rchan_buf *buf,
  258. struct file *filp)
  259. {
  260. }
  261. /*
  262. * create_buf_file_create() default callback. Does nothing.
  263. */
  264. static struct dentry *create_buf_file_default_callback(const char *filename,
  265. struct dentry *parent,
  266. umode_t mode,
  267. struct rchan_buf *buf,
  268. int *is_global)
  269. {
  270. return NULL;
  271. }
  272. /*
  273. * remove_buf_file() default callback. Does nothing.
  274. */
  275. static int remove_buf_file_default_callback(struct dentry *dentry)
  276. {
  277. return -EINVAL;
  278. }
  279. /* relay channel default callbacks */
  280. static struct rchan_callbacks default_channel_callbacks = {
  281. .subbuf_start = subbuf_start_default_callback,
  282. .buf_mapped = buf_mapped_default_callback,
  283. .buf_unmapped = buf_unmapped_default_callback,
  284. .create_buf_file = create_buf_file_default_callback,
  285. .remove_buf_file = remove_buf_file_default_callback,
  286. };
  287. /**
  288. * wakeup_readers - wake up readers waiting on a channel
  289. * @data: contains the channel buffer
  290. *
  291. * This is the timer function used to defer reader waking.
  292. */
  293. static void wakeup_readers(unsigned long data)
  294. {
  295. struct rchan_buf *buf = (struct rchan_buf *)data;
  296. wake_up_interruptible(&buf->read_wait);
  297. }
  298. /**
  299. * __relay_reset - reset a channel buffer
  300. * @buf: the channel buffer
  301. * @init: 1 if this is a first-time initialization
  302. *
  303. * See relay_reset() for description of effect.
  304. */
  305. static void __relay_reset(struct rchan_buf *buf, unsigned int init)
  306. {
  307. size_t i;
  308. if (init) {
  309. init_waitqueue_head(&buf->read_wait);
  310. kref_init(&buf->kref);
  311. setup_timer(&buf->timer, wakeup_readers, (unsigned long)buf);
  312. } else
  313. del_timer_sync(&buf->timer);
  314. buf->subbufs_produced = 0;
  315. buf->subbufs_consumed = 0;
  316. buf->bytes_consumed = 0;
  317. buf->finalized = 0;
  318. buf->data = buf->start;
  319. buf->offset = 0;
  320. for (i = 0; i < buf->chan->n_subbufs; i++)
  321. buf->padding[i] = 0;
  322. buf->chan->cb->subbuf_start(buf, buf->data, NULL, 0);
  323. }
  324. /**
  325. * relay_reset - reset the channel
  326. * @chan: the channel
  327. *
  328. * This has the effect of erasing all data from all channel buffers
  329. * and restarting the channel in its initial state. The buffers
  330. * are not freed, so any mappings are still in effect.
  331. *
  332. * NOTE. Care should be taken that the channel isn't actually
  333. * being used by anything when this call is made.
  334. */
  335. void relay_reset(struct rchan *chan)
  336. {
  337. unsigned int i;
  338. if (!chan)
  339. return;
  340. if (chan->is_global && chan->buf[0]) {
  341. __relay_reset(chan->buf[0], 0);
  342. return;
  343. }
  344. mutex_lock(&relay_channels_mutex);
  345. for_each_possible_cpu(i)
  346. if (chan->buf[i])
  347. __relay_reset(chan->buf[i], 0);
  348. mutex_unlock(&relay_channels_mutex);
  349. }
  350. EXPORT_SYMBOL_GPL(relay_reset);
  351. static inline void relay_set_buf_dentry(struct rchan_buf *buf,
  352. struct dentry *dentry)
  353. {
  354. buf->dentry = dentry;
  355. d_inode(buf->dentry)->i_size = buf->early_bytes;
  356. }
  357. static struct dentry *relay_create_buf_file(struct rchan *chan,
  358. struct rchan_buf *buf,
  359. unsigned int cpu)
  360. {
  361. struct dentry *dentry;
  362. char *tmpname;
  363. tmpname = kzalloc(NAME_MAX + 1, GFP_KERNEL);
  364. if (!tmpname)
  365. return NULL;
  366. snprintf(tmpname, NAME_MAX, "%s%d", chan->base_filename, cpu);
  367. /* Create file in fs */
  368. dentry = chan->cb->create_buf_file(tmpname, chan->parent,
  369. S_IRUSR, buf,
  370. &chan->is_global);
  371. kfree(tmpname);
  372. return dentry;
  373. }
  374. /*
  375. * relay_open_buf - create a new relay channel buffer
  376. *
  377. * used by relay_open() and CPU hotplug.
  378. */
  379. static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu)
  380. {
  381. struct rchan_buf *buf = NULL;
  382. struct dentry *dentry;
  383. if (chan->is_global)
  384. return chan->buf[0];
  385. buf = relay_create_buf(chan);
  386. if (!buf)
  387. return NULL;
  388. if (chan->has_base_filename) {
  389. dentry = relay_create_buf_file(chan, buf, cpu);
  390. if (!dentry)
  391. goto free_buf;
  392. relay_set_buf_dentry(buf, dentry);
  393. } else {
  394. /* Only retrieve global info, nothing more, nothing less */
  395. dentry = chan->cb->create_buf_file(NULL, NULL,
  396. S_IRUSR, buf,
  397. &chan->is_global);
  398. if (WARN_ON(dentry))
  399. goto free_buf;
  400. }
  401. buf->cpu = cpu;
  402. __relay_reset(buf, 1);
  403. if(chan->is_global) {
  404. chan->buf[0] = buf;
  405. buf->cpu = 0;
  406. }
  407. return buf;
  408. free_buf:
  409. relay_destroy_buf(buf);
  410. return NULL;
  411. }
  412. /**
  413. * relay_close_buf - close a channel buffer
  414. * @buf: channel buffer
  415. *
  416. * Marks the buffer finalized and restores the default callbacks.
  417. * The channel buffer and channel buffer data structure are then freed
  418. * automatically when the last reference is given up.
  419. */
  420. static void relay_close_buf(struct rchan_buf *buf)
  421. {
  422. buf->finalized = 1;
  423. del_timer_sync(&buf->timer);
  424. buf->chan->cb->remove_buf_file(buf->dentry);
  425. kref_put(&buf->kref, relay_remove_buf);
  426. }
  427. static void setup_callbacks(struct rchan *chan,
  428. struct rchan_callbacks *cb)
  429. {
  430. if (!cb) {
  431. chan->cb = &default_channel_callbacks;
  432. return;
  433. }
  434. if (!cb->subbuf_start)
  435. cb->subbuf_start = subbuf_start_default_callback;
  436. if (!cb->buf_mapped)
  437. cb->buf_mapped = buf_mapped_default_callback;
  438. if (!cb->buf_unmapped)
  439. cb->buf_unmapped = buf_unmapped_default_callback;
  440. if (!cb->create_buf_file)
  441. cb->create_buf_file = create_buf_file_default_callback;
  442. if (!cb->remove_buf_file)
  443. cb->remove_buf_file = remove_buf_file_default_callback;
  444. chan->cb = cb;
  445. }
  446. /**
  447. * relay_hotcpu_callback - CPU hotplug callback
  448. * @nb: notifier block
  449. * @action: hotplug action to take
  450. * @hcpu: CPU number
  451. *
  452. * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
  453. */
  454. static int relay_hotcpu_callback(struct notifier_block *nb,
  455. unsigned long action,
  456. void *hcpu)
  457. {
  458. unsigned int hotcpu = (unsigned long)hcpu;
  459. struct rchan *chan;
  460. switch(action) {
  461. case CPU_UP_PREPARE:
  462. case CPU_UP_PREPARE_FROZEN:
  463. mutex_lock(&relay_channels_mutex);
  464. list_for_each_entry(chan, &relay_channels, list) {
  465. if (chan->buf[hotcpu])
  466. continue;
  467. chan->buf[hotcpu] = relay_open_buf(chan, hotcpu);
  468. if(!chan->buf[hotcpu]) {
  469. printk(KERN_ERR
  470. "relay_hotcpu_callback: cpu %d buffer "
  471. "creation failed\n", hotcpu);
  472. mutex_unlock(&relay_channels_mutex);
  473. return notifier_from_errno(-ENOMEM);
  474. }
  475. }
  476. mutex_unlock(&relay_channels_mutex);
  477. break;
  478. case CPU_DEAD:
  479. case CPU_DEAD_FROZEN:
  480. /* No need to flush the cpu : will be flushed upon
  481. * final relay_flush() call. */
  482. break;
  483. }
  484. return NOTIFY_OK;
  485. }
  486. /**
  487. * relay_open - create a new relay channel
  488. * @base_filename: base name of files to create, %NULL for buffering only
  489. * @parent: dentry of parent directory, %NULL for root directory or buffer
  490. * @subbuf_size: size of sub-buffers
  491. * @n_subbufs: number of sub-buffers
  492. * @cb: client callback functions
  493. * @private_data: user-defined data
  494. *
  495. * Returns channel pointer if successful, %NULL otherwise.
  496. *
  497. * Creates a channel buffer for each cpu using the sizes and
  498. * attributes specified. The created channel buffer files
  499. * will be named base_filename0...base_filenameN-1. File
  500. * permissions will be %S_IRUSR.
  501. *
  502. * If opening a buffer (@parent = NULL) that you later wish to register
  503. * in a filesystem, call relay_late_setup_files() once the @parent dentry
  504. * is available.
  505. */
  506. struct rchan *relay_open(const char *base_filename,
  507. struct dentry *parent,
  508. size_t subbuf_size,
  509. size_t n_subbufs,
  510. struct rchan_callbacks *cb,
  511. void *private_data)
  512. {
  513. unsigned int i;
  514. struct rchan *chan;
  515. if (!(subbuf_size && n_subbufs))
  516. return NULL;
  517. if (subbuf_size > UINT_MAX / n_subbufs)
  518. return NULL;
  519. chan = kzalloc(sizeof(struct rchan), GFP_KERNEL);
  520. if (!chan)
  521. return NULL;
  522. chan->version = RELAYFS_CHANNEL_VERSION;
  523. chan->n_subbufs = n_subbufs;
  524. chan->subbuf_size = subbuf_size;
  525. chan->alloc_size = PAGE_ALIGN(subbuf_size * n_subbufs);
  526. chan->parent = parent;
  527. chan->private_data = private_data;
  528. if (base_filename) {
  529. chan->has_base_filename = 1;
  530. strlcpy(chan->base_filename, base_filename, NAME_MAX);
  531. }
  532. setup_callbacks(chan, cb);
  533. kref_init(&chan->kref);
  534. mutex_lock(&relay_channels_mutex);
  535. for_each_online_cpu(i) {
  536. chan->buf[i] = relay_open_buf(chan, i);
  537. if (!chan->buf[i])
  538. goto free_bufs;
  539. }
  540. list_add(&chan->list, &relay_channels);
  541. mutex_unlock(&relay_channels_mutex);
  542. return chan;
  543. free_bufs:
  544. for_each_possible_cpu(i) {
  545. if (chan->buf[i])
  546. relay_close_buf(chan->buf[i]);
  547. }
  548. kref_put(&chan->kref, relay_destroy_channel);
  549. mutex_unlock(&relay_channels_mutex);
  550. kfree(chan);
  551. return NULL;
  552. }
  553. EXPORT_SYMBOL_GPL(relay_open);
  554. struct rchan_percpu_buf_dispatcher {
  555. struct rchan_buf *buf;
  556. struct dentry *dentry;
  557. };
  558. /* Called in atomic context. */
  559. static void __relay_set_buf_dentry(void *info)
  560. {
  561. struct rchan_percpu_buf_dispatcher *p = info;
  562. relay_set_buf_dentry(p->buf, p->dentry);
  563. }
  564. /**
  565. * relay_late_setup_files - triggers file creation
  566. * @chan: channel to operate on
  567. * @base_filename: base name of files to create
  568. * @parent: dentry of parent directory, %NULL for root directory
  569. *
  570. * Returns 0 if successful, non-zero otherwise.
  571. *
  572. * Use to setup files for a previously buffer-only channel created
  573. * by relay_open() with a NULL parent dentry.
  574. *
  575. * For example, this is useful for perfomring early tracing in kernel,
  576. * before VFS is up and then exposing the early results once the dentry
  577. * is available.
  578. */
  579. int relay_late_setup_files(struct rchan *chan,
  580. const char *base_filename,
  581. struct dentry *parent)
  582. {
  583. int err = 0;
  584. unsigned int i, curr_cpu;
  585. unsigned long flags;
  586. struct dentry *dentry;
  587. struct rchan_percpu_buf_dispatcher disp;
  588. if (!chan || !base_filename)
  589. return -EINVAL;
  590. strlcpy(chan->base_filename, base_filename, NAME_MAX);
  591. mutex_lock(&relay_channels_mutex);
  592. /* Is chan already set up? */
  593. if (unlikely(chan->has_base_filename)) {
  594. mutex_unlock(&relay_channels_mutex);
  595. return -EEXIST;
  596. }
  597. chan->has_base_filename = 1;
  598. chan->parent = parent;
  599. if (chan->is_global) {
  600. err = -EINVAL;
  601. if (!WARN_ON_ONCE(!chan->buf[0])) {
  602. dentry = relay_create_buf_file(chan, chan->buf[0], 0);
  603. if (dentry && !WARN_ON_ONCE(!chan->is_global)) {
  604. relay_set_buf_dentry(chan->buf[0], dentry);
  605. err = 0;
  606. }
  607. }
  608. mutex_unlock(&relay_channels_mutex);
  609. return err;
  610. }
  611. curr_cpu = get_cpu();
  612. /*
  613. * The CPU hotplug notifier ran before us and created buffers with
  614. * no files associated. So it's safe to call relay_setup_buf_file()
  615. * on all currently online CPUs.
  616. */
  617. for_each_online_cpu(i) {
  618. if (unlikely(!chan->buf[i])) {
  619. WARN_ONCE(1, KERN_ERR "CPU has no buffer!\n");
  620. err = -EINVAL;
  621. break;
  622. }
  623. dentry = relay_create_buf_file(chan, chan->buf[i], i);
  624. if (unlikely(!dentry)) {
  625. err = -EINVAL;
  626. break;
  627. }
  628. if (curr_cpu == i) {
  629. local_irq_save(flags);
  630. relay_set_buf_dentry(chan->buf[i], dentry);
  631. local_irq_restore(flags);
  632. } else {
  633. disp.buf = chan->buf[i];
  634. disp.dentry = dentry;
  635. smp_mb();
  636. /* relay_channels_mutex must be held, so wait. */
  637. err = smp_call_function_single(i,
  638. __relay_set_buf_dentry,
  639. &disp, 1);
  640. }
  641. if (unlikely(err))
  642. break;
  643. }
  644. put_cpu();
  645. mutex_unlock(&relay_channels_mutex);
  646. return err;
  647. }
  648. EXPORT_SYMBOL_GPL(relay_late_setup_files);
  649. /**
  650. * relay_switch_subbuf - switch to a new sub-buffer
  651. * @buf: channel buffer
  652. * @length: size of current event
  653. *
  654. * Returns either the length passed in or 0 if full.
  655. *
  656. * Performs sub-buffer-switch tasks such as invoking callbacks,
  657. * updating padding counts, waking up readers, etc.
  658. */
  659. size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
  660. {
  661. void *old, *new;
  662. size_t old_subbuf, new_subbuf;
  663. if (unlikely(length > buf->chan->subbuf_size))
  664. goto toobig;
  665. if (buf->offset != buf->chan->subbuf_size + 1) {
  666. buf->prev_padding = buf->chan->subbuf_size - buf->offset;
  667. old_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
  668. buf->padding[old_subbuf] = buf->prev_padding;
  669. buf->subbufs_produced++;
  670. if (buf->dentry)
  671. d_inode(buf->dentry)->i_size +=
  672. buf->chan->subbuf_size -
  673. buf->padding[old_subbuf];
  674. else
  675. buf->early_bytes += buf->chan->subbuf_size -
  676. buf->padding[old_subbuf];
  677. smp_mb();
  678. if (waitqueue_active(&buf->read_wait))
  679. /*
  680. * Calling wake_up_interruptible() from here
  681. * will deadlock if we happen to be logging
  682. * from the scheduler (trying to re-grab
  683. * rq->lock), so defer it.
  684. */
  685. mod_timer(&buf->timer, jiffies + 1);
  686. }
  687. old = buf->data;
  688. new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
  689. new = buf->start + new_subbuf * buf->chan->subbuf_size;
  690. buf->offset = 0;
  691. if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) {
  692. buf->offset = buf->chan->subbuf_size + 1;
  693. return 0;
  694. }
  695. buf->data = new;
  696. buf->padding[new_subbuf] = 0;
  697. if (unlikely(length + buf->offset > buf->chan->subbuf_size))
  698. goto toobig;
  699. return length;
  700. toobig:
  701. buf->chan->last_toobig = length;
  702. return 0;
  703. }
  704. EXPORT_SYMBOL_GPL(relay_switch_subbuf);
  705. /**
  706. * relay_subbufs_consumed - update the buffer's sub-buffers-consumed count
  707. * @chan: the channel
  708. * @cpu: the cpu associated with the channel buffer to update
  709. * @subbufs_consumed: number of sub-buffers to add to current buf's count
  710. *
  711. * Adds to the channel buffer's consumed sub-buffer count.
  712. * subbufs_consumed should be the number of sub-buffers newly consumed,
  713. * not the total consumed.
  714. *
  715. * NOTE. Kernel clients don't need to call this function if the channel
  716. * mode is 'overwrite'.
  717. */
  718. void relay_subbufs_consumed(struct rchan *chan,
  719. unsigned int cpu,
  720. size_t subbufs_consumed)
  721. {
  722. struct rchan_buf *buf;
  723. if (!chan)
  724. return;
  725. if (cpu >= NR_CPUS || !chan->buf[cpu] ||
  726. subbufs_consumed > chan->n_subbufs)
  727. return;
  728. buf = chan->buf[cpu];
  729. if (subbufs_consumed > buf->subbufs_produced - buf->subbufs_consumed)
  730. buf->subbufs_consumed = buf->subbufs_produced;
  731. else
  732. buf->subbufs_consumed += subbufs_consumed;
  733. }
  734. EXPORT_SYMBOL_GPL(relay_subbufs_consumed);
  735. /**
  736. * relay_close - close the channel
  737. * @chan: the channel
  738. *
  739. * Closes all channel buffers and frees the channel.
  740. */
  741. void relay_close(struct rchan *chan)
  742. {
  743. unsigned int i;
  744. if (!chan)
  745. return;
  746. mutex_lock(&relay_channels_mutex);
  747. if (chan->is_global && chan->buf[0])
  748. relay_close_buf(chan->buf[0]);
  749. else
  750. for_each_possible_cpu(i)
  751. if (chan->buf[i])
  752. relay_close_buf(chan->buf[i]);
  753. if (chan->last_toobig)
  754. printk(KERN_WARNING "relay: one or more items not logged "
  755. "[item size (%Zd) > sub-buffer size (%Zd)]\n",
  756. chan->last_toobig, chan->subbuf_size);
  757. list_del(&chan->list);
  758. kref_put(&chan->kref, relay_destroy_channel);
  759. mutex_unlock(&relay_channels_mutex);
  760. }
  761. EXPORT_SYMBOL_GPL(relay_close);
  762. /**
  763. * relay_flush - close the channel
  764. * @chan: the channel
  765. *
  766. * Flushes all channel buffers, i.e. forces buffer switch.
  767. */
  768. void relay_flush(struct rchan *chan)
  769. {
  770. unsigned int i;
  771. if (!chan)
  772. return;
  773. if (chan->is_global && chan->buf[0]) {
  774. relay_switch_subbuf(chan->buf[0], 0);
  775. return;
  776. }
  777. mutex_lock(&relay_channels_mutex);
  778. for_each_possible_cpu(i)
  779. if (chan->buf[i])
  780. relay_switch_subbuf(chan->buf[i], 0);
  781. mutex_unlock(&relay_channels_mutex);
  782. }
  783. EXPORT_SYMBOL_GPL(relay_flush);
  784. /**
  785. * relay_file_open - open file op for relay files
  786. * @inode: the inode
  787. * @filp: the file
  788. *
  789. * Increments the channel buffer refcount.
  790. */
  791. static int relay_file_open(struct inode *inode, struct file *filp)
  792. {
  793. struct rchan_buf *buf = inode->i_private;
  794. kref_get(&buf->kref);
  795. filp->private_data = buf;
  796. return nonseekable_open(inode, filp);
  797. }
  798. /**
  799. * relay_file_mmap - mmap file op for relay files
  800. * @filp: the file
  801. * @vma: the vma describing what to map
  802. *
  803. * Calls upon relay_mmap_buf() to map the file into user space.
  804. */
  805. static int relay_file_mmap(struct file *filp, struct vm_area_struct *vma)
  806. {
  807. struct rchan_buf *buf = filp->private_data;
  808. return relay_mmap_buf(buf, vma);
  809. }
  810. /**
  811. * relay_file_poll - poll file op for relay files
  812. * @filp: the file
  813. * @wait: poll table
  814. *
  815. * Poll implemention.
  816. */
  817. static unsigned int relay_file_poll(struct file *filp, poll_table *wait)
  818. {
  819. unsigned int mask = 0;
  820. struct rchan_buf *buf = filp->private_data;
  821. if (buf->finalized)
  822. return POLLERR;
  823. if (filp->f_mode & FMODE_READ) {
  824. poll_wait(filp, &buf->read_wait, wait);
  825. if (!relay_buf_empty(buf))
  826. mask |= POLLIN | POLLRDNORM;
  827. }
  828. return mask;
  829. }
  830. /**
  831. * relay_file_release - release file op for relay files
  832. * @inode: the inode
  833. * @filp: the file
  834. *
  835. * Decrements the channel refcount, as the filesystem is
  836. * no longer using it.
  837. */
  838. static int relay_file_release(struct inode *inode, struct file *filp)
  839. {
  840. struct rchan_buf *buf = filp->private_data;
  841. kref_put(&buf->kref, relay_remove_buf);
  842. return 0;
  843. }
  844. /*
  845. * relay_file_read_consume - update the consumed count for the buffer
  846. */
  847. static void relay_file_read_consume(struct rchan_buf *buf,
  848. size_t read_pos,
  849. size_t bytes_consumed)
  850. {
  851. size_t subbuf_size = buf->chan->subbuf_size;
  852. size_t n_subbufs = buf->chan->n_subbufs;
  853. size_t read_subbuf;
  854. if (buf->subbufs_produced == buf->subbufs_consumed &&
  855. buf->offset == buf->bytes_consumed)
  856. return;
  857. if (buf->bytes_consumed + bytes_consumed > subbuf_size) {
  858. relay_subbufs_consumed(buf->chan, buf->cpu, 1);
  859. buf->bytes_consumed = 0;
  860. }
  861. buf->bytes_consumed += bytes_consumed;
  862. if (!read_pos)
  863. read_subbuf = buf->subbufs_consumed % n_subbufs;
  864. else
  865. read_subbuf = read_pos / buf->chan->subbuf_size;
  866. if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) {
  867. if ((read_subbuf == buf->subbufs_produced % n_subbufs) &&
  868. (buf->offset == subbuf_size))
  869. return;
  870. relay_subbufs_consumed(buf->chan, buf->cpu, 1);
  871. buf->bytes_consumed = 0;
  872. }
  873. }
  874. /*
  875. * relay_file_read_avail - boolean, are there unconsumed bytes available?
  876. */
  877. static int relay_file_read_avail(struct rchan_buf *buf, size_t read_pos)
  878. {
  879. size_t subbuf_size = buf->chan->subbuf_size;
  880. size_t n_subbufs = buf->chan->n_subbufs;
  881. size_t produced = buf->subbufs_produced;
  882. size_t consumed = buf->subbufs_consumed;
  883. relay_file_read_consume(buf, read_pos, 0);
  884. consumed = buf->subbufs_consumed;
  885. if (unlikely(buf->offset > subbuf_size)) {
  886. if (produced == consumed)
  887. return 0;
  888. return 1;
  889. }
  890. if (unlikely(produced - consumed >= n_subbufs)) {
  891. consumed = produced - n_subbufs + 1;
  892. buf->subbufs_consumed = consumed;
  893. buf->bytes_consumed = 0;
  894. }
  895. produced = (produced % n_subbufs) * subbuf_size + buf->offset;
  896. consumed = (consumed % n_subbufs) * subbuf_size + buf->bytes_consumed;
  897. if (consumed > produced)
  898. produced += n_subbufs * subbuf_size;
  899. if (consumed == produced) {
  900. if (buf->offset == subbuf_size &&
  901. buf->subbufs_produced > buf->subbufs_consumed)
  902. return 1;
  903. return 0;
  904. }
  905. return 1;
  906. }
  907. /**
  908. * relay_file_read_subbuf_avail - return bytes available in sub-buffer
  909. * @read_pos: file read position
  910. * @buf: relay channel buffer
  911. */
  912. static size_t relay_file_read_subbuf_avail(size_t read_pos,
  913. struct rchan_buf *buf)
  914. {
  915. size_t padding, avail = 0;
  916. size_t read_subbuf, read_offset, write_subbuf, write_offset;
  917. size_t subbuf_size = buf->chan->subbuf_size;
  918. write_subbuf = (buf->data - buf->start) / subbuf_size;
  919. write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;
  920. read_subbuf = read_pos / subbuf_size;
  921. read_offset = read_pos % subbuf_size;
  922. padding = buf->padding[read_subbuf];
  923. if (read_subbuf == write_subbuf) {
  924. if (read_offset + padding < write_offset)
  925. avail = write_offset - (read_offset + padding);
  926. } else
  927. avail = (subbuf_size - padding) - read_offset;
  928. return avail;
  929. }
  930. /**
  931. * relay_file_read_start_pos - find the first available byte to read
  932. * @read_pos: file read position
  933. * @buf: relay channel buffer
  934. *
  935. * If the @read_pos is in the middle of padding, return the
  936. * position of the first actually available byte, otherwise
  937. * return the original value.
  938. */
  939. static size_t relay_file_read_start_pos(size_t read_pos,
  940. struct rchan_buf *buf)
  941. {
  942. size_t read_subbuf, padding, padding_start, padding_end;
  943. size_t subbuf_size = buf->chan->subbuf_size;
  944. size_t n_subbufs = buf->chan->n_subbufs;
  945. size_t consumed = buf->subbufs_consumed % n_subbufs;
  946. if (!read_pos)
  947. read_pos = consumed * subbuf_size + buf->bytes_consumed;
  948. read_subbuf = read_pos / subbuf_size;
  949. padding = buf->padding[read_subbuf];
  950. padding_start = (read_subbuf + 1) * subbuf_size - padding;
  951. padding_end = (read_subbuf + 1) * subbuf_size;
  952. if (read_pos >= padding_start && read_pos < padding_end) {
  953. read_subbuf = (read_subbuf + 1) % n_subbufs;
  954. read_pos = read_subbuf * subbuf_size;
  955. }
  956. return read_pos;
  957. }
  958. /**
  959. * relay_file_read_end_pos - return the new read position
  960. * @read_pos: file read position
  961. * @buf: relay channel buffer
  962. * @count: number of bytes to be read
  963. */
  964. static size_t relay_file_read_end_pos(struct rchan_buf *buf,
  965. size_t read_pos,
  966. size_t count)
  967. {
  968. size_t read_subbuf, padding, end_pos;
  969. size_t subbuf_size = buf->chan->subbuf_size;
  970. size_t n_subbufs = buf->chan->n_subbufs;
  971. read_subbuf = read_pos / subbuf_size;
  972. padding = buf->padding[read_subbuf];
  973. if (read_pos % subbuf_size + count + padding == subbuf_size)
  974. end_pos = (read_subbuf + 1) * subbuf_size;
  975. else
  976. end_pos = read_pos + count;
  977. if (end_pos >= subbuf_size * n_subbufs)
  978. end_pos = 0;
  979. return end_pos;
  980. }
  981. /*
  982. * subbuf_read_actor - read up to one subbuf's worth of data
  983. */
  984. static int subbuf_read_actor(size_t read_start,
  985. struct rchan_buf *buf,
  986. size_t avail,
  987. read_descriptor_t *desc)
  988. {
  989. void *from;
  990. int ret = 0;
  991. from = buf->start + read_start;
  992. ret = avail;
  993. if (copy_to_user(desc->arg.buf, from, avail)) {
  994. desc->error = -EFAULT;
  995. ret = 0;
  996. }
  997. desc->arg.data += ret;
  998. desc->written += ret;
  999. desc->count -= ret;
  1000. return ret;
  1001. }
  1002. typedef int (*subbuf_actor_t) (size_t read_start,
  1003. struct rchan_buf *buf,
  1004. size_t avail,
  1005. read_descriptor_t *desc);
  1006. /*
  1007. * relay_file_read_subbufs - read count bytes, bridging subbuf boundaries
  1008. */
  1009. static ssize_t relay_file_read_subbufs(struct file *filp, loff_t *ppos,
  1010. subbuf_actor_t subbuf_actor,
  1011. read_descriptor_t *desc)
  1012. {
  1013. struct rchan_buf *buf = filp->private_data;
  1014. size_t read_start, avail;
  1015. int ret;
  1016. if (!desc->count)
  1017. return 0;
  1018. inode_lock(file_inode(filp));
  1019. do {
  1020. if (!relay_file_read_avail(buf, *ppos))
  1021. break;
  1022. read_start = relay_file_read_start_pos(*ppos, buf);
  1023. avail = relay_file_read_subbuf_avail(read_start, buf);
  1024. if (!avail)
  1025. break;
  1026. avail = min(desc->count, avail);
  1027. ret = subbuf_actor(read_start, buf, avail, desc);
  1028. if (desc->error < 0)
  1029. break;
  1030. if (ret) {
  1031. relay_file_read_consume(buf, read_start, ret);
  1032. *ppos = relay_file_read_end_pos(buf, read_start, ret);
  1033. }
  1034. } while (desc->count && ret);
  1035. inode_unlock(file_inode(filp));
  1036. return desc->written;
  1037. }
  1038. static ssize_t relay_file_read(struct file *filp,
  1039. char __user *buffer,
  1040. size_t count,
  1041. loff_t *ppos)
  1042. {
  1043. read_descriptor_t desc;
  1044. desc.written = 0;
  1045. desc.count = count;
  1046. desc.arg.buf = buffer;
  1047. desc.error = 0;
  1048. return relay_file_read_subbufs(filp, ppos, subbuf_read_actor, &desc);
  1049. }
  1050. static void relay_consume_bytes(struct rchan_buf *rbuf, int bytes_consumed)
  1051. {
  1052. rbuf->bytes_consumed += bytes_consumed;
  1053. if (rbuf->bytes_consumed >= rbuf->chan->subbuf_size) {
  1054. relay_subbufs_consumed(rbuf->chan, rbuf->cpu, 1);
  1055. rbuf->bytes_consumed %= rbuf->chan->subbuf_size;
  1056. }
  1057. }
  1058. static void relay_pipe_buf_release(struct pipe_inode_info *pipe,
  1059. struct pipe_buffer *buf)
  1060. {
  1061. struct rchan_buf *rbuf;
  1062. rbuf = (struct rchan_buf *)page_private(buf->page);
  1063. relay_consume_bytes(rbuf, buf->private);
  1064. }
  1065. static const struct pipe_buf_operations relay_pipe_buf_ops = {
  1066. .can_merge = 0,
  1067. .confirm = generic_pipe_buf_confirm,
  1068. .release = relay_pipe_buf_release,
  1069. .steal = generic_pipe_buf_steal,
  1070. .get = generic_pipe_buf_get,
  1071. };
  1072. static void relay_page_release(struct splice_pipe_desc *spd, unsigned int i)
  1073. {
  1074. }
  1075. /*
  1076. * subbuf_splice_actor - splice up to one subbuf's worth of data
  1077. */
  1078. static ssize_t subbuf_splice_actor(struct file *in,
  1079. loff_t *ppos,
  1080. struct pipe_inode_info *pipe,
  1081. size_t len,
  1082. unsigned int flags,
  1083. int *nonpad_ret)
  1084. {
  1085. unsigned int pidx, poff, total_len, subbuf_pages, nr_pages;
  1086. struct rchan_buf *rbuf = in->private_data;
  1087. unsigned int subbuf_size = rbuf->chan->subbuf_size;
  1088. uint64_t pos = (uint64_t) *ppos;
  1089. uint32_t alloc_size = (uint32_t) rbuf->chan->alloc_size;
  1090. size_t read_start = (size_t) do_div(pos, alloc_size);
  1091. size_t read_subbuf = read_start / subbuf_size;
  1092. size_t padding = rbuf->padding[read_subbuf];
  1093. size_t nonpad_end = read_subbuf * subbuf_size + subbuf_size - padding;
  1094. struct page *pages[PIPE_DEF_BUFFERS];
  1095. struct partial_page partial[PIPE_DEF_BUFFERS];
  1096. struct splice_pipe_desc spd = {
  1097. .pages = pages,
  1098. .nr_pages = 0,
  1099. .nr_pages_max = PIPE_DEF_BUFFERS,
  1100. .partial = partial,
  1101. .flags = flags,
  1102. .ops = &relay_pipe_buf_ops,
  1103. .spd_release = relay_page_release,
  1104. };
  1105. ssize_t ret;
  1106. if (rbuf->subbufs_produced == rbuf->subbufs_consumed)
  1107. return 0;
  1108. if (splice_grow_spd(pipe, &spd))
  1109. return -ENOMEM;
  1110. /*
  1111. * Adjust read len, if longer than what is available
  1112. */
  1113. if (len > (subbuf_size - read_start % subbuf_size))
  1114. len = subbuf_size - read_start % subbuf_size;
  1115. subbuf_pages = rbuf->chan->alloc_size >> PAGE_SHIFT;
  1116. pidx = (read_start / PAGE_SIZE) % subbuf_pages;
  1117. poff = read_start & ~PAGE_MASK;
  1118. nr_pages = min_t(unsigned int, subbuf_pages, spd.nr_pages_max);
  1119. for (total_len = 0; spd.nr_pages < nr_pages; spd.nr_pages++) {
  1120. unsigned int this_len, this_end, private;
  1121. unsigned int cur_pos = read_start + total_len;
  1122. if (!len)
  1123. break;
  1124. this_len = min_t(unsigned long, len, PAGE_SIZE - poff);
  1125. private = this_len;
  1126. spd.pages[spd.nr_pages] = rbuf->page_array[pidx];
  1127. spd.partial[spd.nr_pages].offset = poff;
  1128. this_end = cur_pos + this_len;
  1129. if (this_end >= nonpad_end) {
  1130. this_len = nonpad_end - cur_pos;
  1131. private = this_len + padding;
  1132. }
  1133. spd.partial[spd.nr_pages].len = this_len;
  1134. spd.partial[spd.nr_pages].private = private;
  1135. len -= this_len;
  1136. total_len += this_len;
  1137. poff = 0;
  1138. pidx = (pidx + 1) % subbuf_pages;
  1139. if (this_end >= nonpad_end) {
  1140. spd.nr_pages++;
  1141. break;
  1142. }
  1143. }
  1144. ret = 0;
  1145. if (!spd.nr_pages)
  1146. goto out;
  1147. ret = *nonpad_ret = splice_to_pipe(pipe, &spd);
  1148. if (ret < 0 || ret < total_len)
  1149. goto out;
  1150. if (read_start + ret == nonpad_end)
  1151. ret += padding;
  1152. out:
  1153. splice_shrink_spd(&spd);
  1154. return ret;
  1155. }
  1156. static ssize_t relay_file_splice_read(struct file *in,
  1157. loff_t *ppos,
  1158. struct pipe_inode_info *pipe,
  1159. size_t len,
  1160. unsigned int flags)
  1161. {
  1162. ssize_t spliced;
  1163. int ret;
  1164. int nonpad_ret = 0;
  1165. ret = 0;
  1166. spliced = 0;
  1167. while (len && !spliced) {
  1168. ret = subbuf_splice_actor(in, ppos, pipe, len, flags, &nonpad_ret);
  1169. if (ret < 0)
  1170. break;
  1171. else if (!ret) {
  1172. if (flags & SPLICE_F_NONBLOCK)
  1173. ret = -EAGAIN;
  1174. break;
  1175. }
  1176. *ppos += ret;
  1177. if (ret > len)
  1178. len = 0;
  1179. else
  1180. len -= ret;
  1181. spliced += nonpad_ret;
  1182. nonpad_ret = 0;
  1183. }
  1184. if (spliced)
  1185. return spliced;
  1186. return ret;
  1187. }
  1188. const struct file_operations relay_file_operations = {
  1189. .open = relay_file_open,
  1190. .poll = relay_file_poll,
  1191. .mmap = relay_file_mmap,
  1192. .read = relay_file_read,
  1193. .llseek = no_llseek,
  1194. .release = relay_file_release,
  1195. .splice_read = relay_file_splice_read,
  1196. };
  1197. EXPORT_SYMBOL_GPL(relay_file_operations);
  1198. static __init int relay_init(void)
  1199. {
  1200. hotcpu_notifier(relay_hotcpu_callback, 0);
  1201. return 0;
  1202. }
  1203. early_initcall(relay_init);