xdr.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  1. /*
  2. * linux/net/sunrpc/xdr.c
  3. *
  4. * Generic XDR support.
  5. *
  6. * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. #include <linux/types.h>
  11. #include <linux/string.h>
  12. #include <linux/kernel.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/errno.h>
  15. #include <linux/sunrpc/xdr.h>
  16. #include <linux/sunrpc/msg_prot.h>
  17. /*
  18. * XDR functions for basic NFS types
  19. */
  20. __be32 *
  21. xdr_encode_netobj(__be32 *p, const struct xdr_netobj *obj)
  22. {
  23. unsigned int quadlen = XDR_QUADLEN(obj->len);
  24. p[quadlen] = 0; /* zero trailing bytes */
  25. *p++ = cpu_to_be32(obj->len);
  26. memcpy(p, obj->data, obj->len);
  27. return p + XDR_QUADLEN(obj->len);
  28. }
  29. EXPORT_SYMBOL_GPL(xdr_encode_netobj);
  30. __be32 *
  31. xdr_decode_netobj(__be32 *p, struct xdr_netobj *obj)
  32. {
  33. unsigned int len;
  34. if ((len = be32_to_cpu(*p++)) > XDR_MAX_NETOBJ)
  35. return NULL;
  36. obj->len = len;
  37. obj->data = (u8 *) p;
  38. return p + XDR_QUADLEN(len);
  39. }
  40. EXPORT_SYMBOL_GPL(xdr_decode_netobj);
  41. /**
  42. * xdr_encode_opaque_fixed - Encode fixed length opaque data
  43. * @p: pointer to current position in XDR buffer.
  44. * @ptr: pointer to data to encode (or NULL)
  45. * @nbytes: size of data.
  46. *
  47. * Copy the array of data of length nbytes at ptr to the XDR buffer
  48. * at position p, then align to the next 32-bit boundary by padding
  49. * with zero bytes (see RFC1832).
  50. * Note: if ptr is NULL, only the padding is performed.
  51. *
  52. * Returns the updated current XDR buffer position
  53. *
  54. */
  55. __be32 *xdr_encode_opaque_fixed(__be32 *p, const void *ptr, unsigned int nbytes)
  56. {
  57. if (likely(nbytes != 0)) {
  58. unsigned int quadlen = XDR_QUADLEN(nbytes);
  59. unsigned int padding = (quadlen << 2) - nbytes;
  60. if (ptr != NULL)
  61. memcpy(p, ptr, nbytes);
  62. if (padding != 0)
  63. memset((char *)p + nbytes, 0, padding);
  64. p += quadlen;
  65. }
  66. return p;
  67. }
  68. EXPORT_SYMBOL_GPL(xdr_encode_opaque_fixed);
  69. /**
  70. * xdr_encode_opaque - Encode variable length opaque data
  71. * @p: pointer to current position in XDR buffer.
  72. * @ptr: pointer to data to encode (or NULL)
  73. * @nbytes: size of data.
  74. *
  75. * Returns the updated current XDR buffer position
  76. */
  77. __be32 *xdr_encode_opaque(__be32 *p, const void *ptr, unsigned int nbytes)
  78. {
  79. *p++ = cpu_to_be32(nbytes);
  80. return xdr_encode_opaque_fixed(p, ptr, nbytes);
  81. }
  82. EXPORT_SYMBOL_GPL(xdr_encode_opaque);
  83. __be32 *
  84. xdr_encode_string(__be32 *p, const char *string)
  85. {
  86. return xdr_encode_array(p, string, strlen(string));
  87. }
  88. EXPORT_SYMBOL_GPL(xdr_encode_string);
  89. __be32 *
  90. xdr_decode_string_inplace(__be32 *p, char **sp,
  91. unsigned int *lenp, unsigned int maxlen)
  92. {
  93. u32 len;
  94. len = be32_to_cpu(*p++);
  95. if (len > maxlen)
  96. return NULL;
  97. *lenp = len;
  98. *sp = (char *) p;
  99. return p + XDR_QUADLEN(len);
  100. }
  101. EXPORT_SYMBOL_GPL(xdr_decode_string_inplace);
  102. /**
  103. * xdr_terminate_string - '\0'-terminate a string residing in an xdr_buf
  104. * @buf: XDR buffer where string resides
  105. * @len: length of string, in bytes
  106. *
  107. */
  108. void
  109. xdr_terminate_string(struct xdr_buf *buf, const u32 len)
  110. {
  111. char *kaddr;
  112. kaddr = kmap_atomic(buf->pages[0]);
  113. kaddr[buf->page_base + len] = '\0';
  114. kunmap_atomic(kaddr);
  115. }
  116. EXPORT_SYMBOL_GPL(xdr_terminate_string);
  117. void
  118. xdr_inline_pages(struct xdr_buf *xdr, unsigned int offset,
  119. struct page **pages, unsigned int base, unsigned int len)
  120. {
  121. struct kvec *head = xdr->head;
  122. struct kvec *tail = xdr->tail;
  123. char *buf = (char *)head->iov_base;
  124. unsigned int buflen = head->iov_len;
  125. head->iov_len = offset;
  126. xdr->pages = pages;
  127. xdr->page_base = base;
  128. xdr->page_len = len;
  129. tail->iov_base = buf + offset;
  130. tail->iov_len = buflen - offset;
  131. xdr->buflen += len;
  132. }
  133. EXPORT_SYMBOL_GPL(xdr_inline_pages);
  134. /*
  135. * Helper routines for doing 'memmove' like operations on a struct xdr_buf
  136. */
  137. /**
  138. * _shift_data_right_pages
  139. * @pages: vector of pages containing both the source and dest memory area.
  140. * @pgto_base: page vector address of destination
  141. * @pgfrom_base: page vector address of source
  142. * @len: number of bytes to copy
  143. *
  144. * Note: the addresses pgto_base and pgfrom_base are both calculated in
  145. * the same way:
  146. * if a memory area starts at byte 'base' in page 'pages[i]',
  147. * then its address is given as (i << PAGE_CACHE_SHIFT) + base
  148. * Also note: pgfrom_base must be < pgto_base, but the memory areas
  149. * they point to may overlap.
  150. */
  151. static void
  152. _shift_data_right_pages(struct page **pages, size_t pgto_base,
  153. size_t pgfrom_base, size_t len)
  154. {
  155. struct page **pgfrom, **pgto;
  156. char *vfrom, *vto;
  157. size_t copy;
  158. BUG_ON(pgto_base <= pgfrom_base);
  159. pgto_base += len;
  160. pgfrom_base += len;
  161. pgto = pages + (pgto_base >> PAGE_CACHE_SHIFT);
  162. pgfrom = pages + (pgfrom_base >> PAGE_CACHE_SHIFT);
  163. pgto_base &= ~PAGE_CACHE_MASK;
  164. pgfrom_base &= ~PAGE_CACHE_MASK;
  165. do {
  166. /* Are any pointers crossing a page boundary? */
  167. if (pgto_base == 0) {
  168. pgto_base = PAGE_CACHE_SIZE;
  169. pgto--;
  170. }
  171. if (pgfrom_base == 0) {
  172. pgfrom_base = PAGE_CACHE_SIZE;
  173. pgfrom--;
  174. }
  175. copy = len;
  176. if (copy > pgto_base)
  177. copy = pgto_base;
  178. if (copy > pgfrom_base)
  179. copy = pgfrom_base;
  180. pgto_base -= copy;
  181. pgfrom_base -= copy;
  182. vto = kmap_atomic(*pgto);
  183. vfrom = kmap_atomic(*pgfrom);
  184. memmove(vto + pgto_base, vfrom + pgfrom_base, copy);
  185. flush_dcache_page(*pgto);
  186. kunmap_atomic(vfrom);
  187. kunmap_atomic(vto);
  188. } while ((len -= copy) != 0);
  189. }
  190. /**
  191. * _copy_to_pages
  192. * @pages: array of pages
  193. * @pgbase: page vector address of destination
  194. * @p: pointer to source data
  195. * @len: length
  196. *
  197. * Copies data from an arbitrary memory location into an array of pages
  198. * The copy is assumed to be non-overlapping.
  199. */
  200. static void
  201. _copy_to_pages(struct page **pages, size_t pgbase, const char *p, size_t len)
  202. {
  203. struct page **pgto;
  204. char *vto;
  205. size_t copy;
  206. pgto = pages + (pgbase >> PAGE_CACHE_SHIFT);
  207. pgbase &= ~PAGE_CACHE_MASK;
  208. for (;;) {
  209. copy = PAGE_CACHE_SIZE - pgbase;
  210. if (copy > len)
  211. copy = len;
  212. vto = kmap_atomic(*pgto);
  213. memcpy(vto + pgbase, p, copy);
  214. kunmap_atomic(vto);
  215. len -= copy;
  216. if (len == 0)
  217. break;
  218. pgbase += copy;
  219. if (pgbase == PAGE_CACHE_SIZE) {
  220. flush_dcache_page(*pgto);
  221. pgbase = 0;
  222. pgto++;
  223. }
  224. p += copy;
  225. }
  226. flush_dcache_page(*pgto);
  227. }
  228. /**
  229. * _copy_from_pages
  230. * @p: pointer to destination
  231. * @pages: array of pages
  232. * @pgbase: offset of source data
  233. * @len: length
  234. *
  235. * Copies data into an arbitrary memory location from an array of pages
  236. * The copy is assumed to be non-overlapping.
  237. */
  238. void
  239. _copy_from_pages(char *p, struct page **pages, size_t pgbase, size_t len)
  240. {
  241. struct page **pgfrom;
  242. char *vfrom;
  243. size_t copy;
  244. pgfrom = pages + (pgbase >> PAGE_CACHE_SHIFT);
  245. pgbase &= ~PAGE_CACHE_MASK;
  246. do {
  247. copy = PAGE_CACHE_SIZE - pgbase;
  248. if (copy > len)
  249. copy = len;
  250. vfrom = kmap_atomic(*pgfrom);
  251. memcpy(p, vfrom + pgbase, copy);
  252. kunmap_atomic(vfrom);
  253. pgbase += copy;
  254. if (pgbase == PAGE_CACHE_SIZE) {
  255. pgbase = 0;
  256. pgfrom++;
  257. }
  258. p += copy;
  259. } while ((len -= copy) != 0);
  260. }
  261. EXPORT_SYMBOL_GPL(_copy_from_pages);
  262. /**
  263. * xdr_shrink_bufhead
  264. * @buf: xdr_buf
  265. * @len: bytes to remove from buf->head[0]
  266. *
  267. * Shrinks XDR buffer's header kvec buf->head[0] by
  268. * 'len' bytes. The extra data is not lost, but is instead
  269. * moved into the inlined pages and/or the tail.
  270. */
  271. static void
  272. xdr_shrink_bufhead(struct xdr_buf *buf, size_t len)
  273. {
  274. struct kvec *head, *tail;
  275. size_t copy, offs;
  276. unsigned int pglen = buf->page_len;
  277. tail = buf->tail;
  278. head = buf->head;
  279. BUG_ON (len > head->iov_len);
  280. /* Shift the tail first */
  281. if (tail->iov_len != 0) {
  282. if (tail->iov_len > len) {
  283. copy = tail->iov_len - len;
  284. memmove((char *)tail->iov_base + len,
  285. tail->iov_base, copy);
  286. }
  287. /* Copy from the inlined pages into the tail */
  288. copy = len;
  289. if (copy > pglen)
  290. copy = pglen;
  291. offs = len - copy;
  292. if (offs >= tail->iov_len)
  293. copy = 0;
  294. else if (copy > tail->iov_len - offs)
  295. copy = tail->iov_len - offs;
  296. if (copy != 0)
  297. _copy_from_pages((char *)tail->iov_base + offs,
  298. buf->pages,
  299. buf->page_base + pglen + offs - len,
  300. copy);
  301. /* Do we also need to copy data from the head into the tail ? */
  302. if (len > pglen) {
  303. offs = copy = len - pglen;
  304. if (copy > tail->iov_len)
  305. copy = tail->iov_len;
  306. memcpy(tail->iov_base,
  307. (char *)head->iov_base +
  308. head->iov_len - offs,
  309. copy);
  310. }
  311. }
  312. /* Now handle pages */
  313. if (pglen != 0) {
  314. if (pglen > len)
  315. _shift_data_right_pages(buf->pages,
  316. buf->page_base + len,
  317. buf->page_base,
  318. pglen - len);
  319. copy = len;
  320. if (len > pglen)
  321. copy = pglen;
  322. _copy_to_pages(buf->pages, buf->page_base,
  323. (char *)head->iov_base + head->iov_len - len,
  324. copy);
  325. }
  326. head->iov_len -= len;
  327. buf->buflen -= len;
  328. /* Have we truncated the message? */
  329. if (buf->len > buf->buflen)
  330. buf->len = buf->buflen;
  331. }
  332. /**
  333. * xdr_shrink_pagelen
  334. * @buf: xdr_buf
  335. * @len: bytes to remove from buf->pages
  336. *
  337. * Shrinks XDR buffer's page array buf->pages by
  338. * 'len' bytes. The extra data is not lost, but is instead
  339. * moved into the tail.
  340. */
  341. static void
  342. xdr_shrink_pagelen(struct xdr_buf *buf, size_t len)
  343. {
  344. struct kvec *tail;
  345. size_t copy;
  346. unsigned int pglen = buf->page_len;
  347. unsigned int tailbuf_len;
  348. tail = buf->tail;
  349. BUG_ON (len > pglen);
  350. tailbuf_len = buf->buflen - buf->head->iov_len - buf->page_len;
  351. /* Shift the tail first */
  352. if (tailbuf_len != 0) {
  353. unsigned int free_space = tailbuf_len - tail->iov_len;
  354. if (len < free_space)
  355. free_space = len;
  356. tail->iov_len += free_space;
  357. copy = len;
  358. if (tail->iov_len > len) {
  359. char *p = (char *)tail->iov_base + len;
  360. memmove(p, tail->iov_base, tail->iov_len - len);
  361. } else
  362. copy = tail->iov_len;
  363. /* Copy from the inlined pages into the tail */
  364. _copy_from_pages((char *)tail->iov_base,
  365. buf->pages, buf->page_base + pglen - len,
  366. copy);
  367. }
  368. buf->page_len -= len;
  369. buf->buflen -= len;
  370. /* Have we truncated the message? */
  371. if (buf->len > buf->buflen)
  372. buf->len = buf->buflen;
  373. }
  374. void
  375. xdr_shift_buf(struct xdr_buf *buf, size_t len)
  376. {
  377. xdr_shrink_bufhead(buf, len);
  378. }
  379. EXPORT_SYMBOL_GPL(xdr_shift_buf);
  380. /**
  381. * xdr_stream_pos - Return the current offset from the start of the xdr_stream
  382. * @xdr: pointer to struct xdr_stream
  383. */
  384. unsigned int xdr_stream_pos(const struct xdr_stream *xdr)
  385. {
  386. return (unsigned int)(XDR_QUADLEN(xdr->buf->len) - xdr->nwords) << 2;
  387. }
  388. EXPORT_SYMBOL_GPL(xdr_stream_pos);
  389. /**
  390. * xdr_init_encode - Initialize a struct xdr_stream for sending data.
  391. * @xdr: pointer to xdr_stream struct
  392. * @buf: pointer to XDR buffer in which to encode data
  393. * @p: current pointer inside XDR buffer
  394. *
  395. * Note: at the moment the RPC client only passes the length of our
  396. * scratch buffer in the xdr_buf's header kvec. Previously this
  397. * meant we needed to call xdr_adjust_iovec() after encoding the
  398. * data. With the new scheme, the xdr_stream manages the details
  399. * of the buffer length, and takes care of adjusting the kvec
  400. * length for us.
  401. */
  402. void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p)
  403. {
  404. struct kvec *iov = buf->head;
  405. int scratch_len = buf->buflen - buf->page_len - buf->tail[0].iov_len;
  406. BUG_ON(scratch_len < 0);
  407. xdr->buf = buf;
  408. xdr->iov = iov;
  409. xdr->p = (__be32 *)((char *)iov->iov_base + iov->iov_len);
  410. xdr->end = (__be32 *)((char *)iov->iov_base + scratch_len);
  411. BUG_ON(iov->iov_len > scratch_len);
  412. if (p != xdr->p && p != NULL) {
  413. size_t len;
  414. BUG_ON(p < xdr->p || p > xdr->end);
  415. len = (char *)p - (char *)xdr->p;
  416. xdr->p = p;
  417. buf->len += len;
  418. iov->iov_len += len;
  419. }
  420. }
  421. EXPORT_SYMBOL_GPL(xdr_init_encode);
  422. /**
  423. * xdr_reserve_space - Reserve buffer space for sending
  424. * @xdr: pointer to xdr_stream
  425. * @nbytes: number of bytes to reserve
  426. *
  427. * Checks that we have enough buffer space to encode 'nbytes' more
  428. * bytes of data. If so, update the total xdr_buf length, and
  429. * adjust the length of the current kvec.
  430. */
  431. __be32 * xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes)
  432. {
  433. __be32 *p = xdr->p;
  434. __be32 *q;
  435. /* align nbytes on the next 32-bit boundary */
  436. nbytes += 3;
  437. nbytes &= ~3;
  438. q = p + (nbytes >> 2);
  439. if (unlikely(q > xdr->end || q < p))
  440. return NULL;
  441. xdr->p = q;
  442. xdr->iov->iov_len += nbytes;
  443. xdr->buf->len += nbytes;
  444. return p;
  445. }
  446. EXPORT_SYMBOL_GPL(xdr_reserve_space);
  447. /**
  448. * xdr_write_pages - Insert a list of pages into an XDR buffer for sending
  449. * @xdr: pointer to xdr_stream
  450. * @pages: list of pages
  451. * @base: offset of first byte
  452. * @len: length of data in bytes
  453. *
  454. */
  455. void xdr_write_pages(struct xdr_stream *xdr, struct page **pages, unsigned int base,
  456. unsigned int len)
  457. {
  458. struct xdr_buf *buf = xdr->buf;
  459. struct kvec *iov = buf->tail;
  460. buf->pages = pages;
  461. buf->page_base = base;
  462. buf->page_len = len;
  463. iov->iov_base = (char *)xdr->p;
  464. iov->iov_len = 0;
  465. xdr->iov = iov;
  466. if (len & 3) {
  467. unsigned int pad = 4 - (len & 3);
  468. BUG_ON(xdr->p >= xdr->end);
  469. iov->iov_base = (char *)xdr->p + (len & 3);
  470. iov->iov_len += pad;
  471. len += pad;
  472. *xdr->p++ = 0;
  473. }
  474. buf->buflen += len;
  475. buf->len += len;
  476. }
  477. EXPORT_SYMBOL_GPL(xdr_write_pages);
  478. static void xdr_set_iov(struct xdr_stream *xdr, struct kvec *iov,
  479. unsigned int len)
  480. {
  481. if (len > iov->iov_len)
  482. len = iov->iov_len;
  483. xdr->p = (__be32*)iov->iov_base;
  484. xdr->end = (__be32*)(iov->iov_base + len);
  485. xdr->iov = iov;
  486. xdr->page_ptr = NULL;
  487. }
  488. static int xdr_set_page_base(struct xdr_stream *xdr,
  489. unsigned int base, unsigned int len)
  490. {
  491. unsigned int pgnr;
  492. unsigned int maxlen;
  493. unsigned int pgoff;
  494. unsigned int pgend;
  495. void *kaddr;
  496. maxlen = xdr->buf->page_len;
  497. if (base >= maxlen)
  498. return -EINVAL;
  499. maxlen -= base;
  500. if (len > maxlen)
  501. len = maxlen;
  502. base += xdr->buf->page_base;
  503. pgnr = base >> PAGE_SHIFT;
  504. xdr->page_ptr = &xdr->buf->pages[pgnr];
  505. kaddr = page_address(*xdr->page_ptr);
  506. pgoff = base & ~PAGE_MASK;
  507. xdr->p = (__be32*)(kaddr + pgoff);
  508. pgend = pgoff + len;
  509. if (pgend > PAGE_SIZE)
  510. pgend = PAGE_SIZE;
  511. xdr->end = (__be32*)(kaddr + pgend);
  512. xdr->iov = NULL;
  513. return 0;
  514. }
  515. static void xdr_set_next_page(struct xdr_stream *xdr)
  516. {
  517. unsigned int newbase;
  518. newbase = (1 + xdr->page_ptr - xdr->buf->pages) << PAGE_SHIFT;
  519. newbase -= xdr->buf->page_base;
  520. if (xdr_set_page_base(xdr, newbase, PAGE_SIZE) < 0)
  521. xdr_set_iov(xdr, xdr->buf->tail, xdr->buf->len);
  522. }
  523. static bool xdr_set_next_buffer(struct xdr_stream *xdr)
  524. {
  525. if (xdr->page_ptr != NULL)
  526. xdr_set_next_page(xdr);
  527. else if (xdr->iov == xdr->buf->head) {
  528. if (xdr_set_page_base(xdr, 0, PAGE_SIZE) < 0)
  529. xdr_set_iov(xdr, xdr->buf->tail, xdr->buf->len);
  530. }
  531. return xdr->p != xdr->end;
  532. }
  533. /**
  534. * xdr_init_decode - Initialize an xdr_stream for decoding data.
  535. * @xdr: pointer to xdr_stream struct
  536. * @buf: pointer to XDR buffer from which to decode data
  537. * @p: current pointer inside XDR buffer
  538. */
  539. void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p)
  540. {
  541. xdr->buf = buf;
  542. xdr->scratch.iov_base = NULL;
  543. xdr->scratch.iov_len = 0;
  544. xdr->nwords = XDR_QUADLEN(buf->len);
  545. if (buf->head[0].iov_len != 0)
  546. xdr_set_iov(xdr, buf->head, buf->len);
  547. else if (buf->page_len != 0)
  548. xdr_set_page_base(xdr, 0, buf->len);
  549. if (p != NULL && p > xdr->p && xdr->end >= p) {
  550. xdr->nwords -= p - xdr->p;
  551. xdr->p = p;
  552. }
  553. }
  554. EXPORT_SYMBOL_GPL(xdr_init_decode);
  555. /**
  556. * xdr_init_decode - Initialize an xdr_stream for decoding data.
  557. * @xdr: pointer to xdr_stream struct
  558. * @buf: pointer to XDR buffer from which to decode data
  559. * @pages: list of pages to decode into
  560. * @len: length in bytes of buffer in pages
  561. */
  562. void xdr_init_decode_pages(struct xdr_stream *xdr, struct xdr_buf *buf,
  563. struct page **pages, unsigned int len)
  564. {
  565. memset(buf, 0, sizeof(*buf));
  566. buf->pages = pages;
  567. buf->page_len = len;
  568. buf->buflen = len;
  569. buf->len = len;
  570. xdr_init_decode(xdr, buf, NULL);
  571. }
  572. EXPORT_SYMBOL_GPL(xdr_init_decode_pages);
  573. static __be32 * __xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
  574. {
  575. unsigned int nwords = XDR_QUADLEN(nbytes);
  576. __be32 *p = xdr->p;
  577. __be32 *q = p + nwords;
  578. if (unlikely(nwords > xdr->nwords || q > xdr->end || q < p))
  579. return NULL;
  580. xdr->p = q;
  581. xdr->nwords -= nwords;
  582. return p;
  583. }
  584. /**
  585. * xdr_set_scratch_buffer - Attach a scratch buffer for decoding data.
  586. * @xdr: pointer to xdr_stream struct
  587. * @buf: pointer to an empty buffer
  588. * @buflen: size of 'buf'
  589. *
  590. * The scratch buffer is used when decoding from an array of pages.
  591. * If an xdr_inline_decode() call spans across page boundaries, then
  592. * we copy the data into the scratch buffer in order to allow linear
  593. * access.
  594. */
  595. void xdr_set_scratch_buffer(struct xdr_stream *xdr, void *buf, size_t buflen)
  596. {
  597. xdr->scratch.iov_base = buf;
  598. xdr->scratch.iov_len = buflen;
  599. }
  600. EXPORT_SYMBOL_GPL(xdr_set_scratch_buffer);
  601. static __be32 *xdr_copy_to_scratch(struct xdr_stream *xdr, size_t nbytes)
  602. {
  603. __be32 *p;
  604. void *cpdest = xdr->scratch.iov_base;
  605. size_t cplen = (char *)xdr->end - (char *)xdr->p;
  606. if (nbytes > xdr->scratch.iov_len)
  607. return NULL;
  608. memcpy(cpdest, xdr->p, cplen);
  609. cpdest += cplen;
  610. nbytes -= cplen;
  611. if (!xdr_set_next_buffer(xdr))
  612. return NULL;
  613. p = __xdr_inline_decode(xdr, nbytes);
  614. if (p == NULL)
  615. return NULL;
  616. memcpy(cpdest, p, nbytes);
  617. return xdr->scratch.iov_base;
  618. }
  619. /**
  620. * xdr_inline_decode - Retrieve XDR data to decode
  621. * @xdr: pointer to xdr_stream struct
  622. * @nbytes: number of bytes of data to decode
  623. *
  624. * Check if the input buffer is long enough to enable us to decode
  625. * 'nbytes' more bytes of data starting at the current position.
  626. * If so return the current pointer, then update the current
  627. * pointer position.
  628. */
  629. __be32 * xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
  630. {
  631. __be32 *p;
  632. if (nbytes == 0)
  633. return xdr->p;
  634. if (xdr->p == xdr->end && !xdr_set_next_buffer(xdr))
  635. return NULL;
  636. p = __xdr_inline_decode(xdr, nbytes);
  637. if (p != NULL)
  638. return p;
  639. return xdr_copy_to_scratch(xdr, nbytes);
  640. }
  641. EXPORT_SYMBOL_GPL(xdr_inline_decode);
  642. static unsigned int xdr_align_pages(struct xdr_stream *xdr, unsigned int len)
  643. {
  644. struct xdr_buf *buf = xdr->buf;
  645. struct kvec *iov;
  646. unsigned int nwords = XDR_QUADLEN(len);
  647. unsigned int cur = xdr_stream_pos(xdr);
  648. if (xdr->nwords == 0)
  649. return 0;
  650. /* Realign pages to current pointer position */
  651. iov = buf->head;
  652. if (iov->iov_len > cur) {
  653. xdr_shrink_bufhead(buf, iov->iov_len - cur);
  654. xdr->nwords = XDR_QUADLEN(buf->len - cur);
  655. }
  656. if (nwords > xdr->nwords) {
  657. nwords = xdr->nwords;
  658. len = nwords << 2;
  659. }
  660. if (buf->page_len <= len)
  661. len = buf->page_len;
  662. else if (nwords < xdr->nwords) {
  663. /* Truncate page data and move it into the tail */
  664. xdr_shrink_pagelen(buf, buf->page_len - len);
  665. xdr->nwords = XDR_QUADLEN(buf->len - cur);
  666. }
  667. return len;
  668. }
  669. /**
  670. * xdr_read_pages - Ensure page-based XDR data to decode is aligned at current pointer position
  671. * @xdr: pointer to xdr_stream struct
  672. * @len: number of bytes of page data
  673. *
  674. * Moves data beyond the current pointer position from the XDR head[] buffer
  675. * into the page list. Any data that lies beyond current position + "len"
  676. * bytes is moved into the XDR tail[].
  677. *
  678. * Returns the number of XDR encoded bytes now contained in the pages
  679. */
  680. unsigned int xdr_read_pages(struct xdr_stream *xdr, unsigned int len)
  681. {
  682. struct xdr_buf *buf = xdr->buf;
  683. struct kvec *iov;
  684. unsigned int nwords;
  685. unsigned int end;
  686. unsigned int padding;
  687. len = xdr_align_pages(xdr, len);
  688. if (len == 0)
  689. return 0;
  690. nwords = XDR_QUADLEN(len);
  691. padding = (nwords << 2) - len;
  692. xdr->iov = iov = buf->tail;
  693. /* Compute remaining message length. */
  694. end = ((xdr->nwords - nwords) << 2) + padding;
  695. if (end > iov->iov_len)
  696. end = iov->iov_len;
  697. /*
  698. * Position current pointer at beginning of tail, and
  699. * set remaining message length.
  700. */
  701. xdr->p = (__be32 *)((char *)iov->iov_base + padding);
  702. xdr->end = (__be32 *)((char *)iov->iov_base + end);
  703. xdr->page_ptr = NULL;
  704. xdr->nwords = XDR_QUADLEN(end - padding);
  705. return len;
  706. }
  707. EXPORT_SYMBOL_GPL(xdr_read_pages);
  708. /**
  709. * xdr_enter_page - decode data from the XDR page
  710. * @xdr: pointer to xdr_stream struct
  711. * @len: number of bytes of page data
  712. *
  713. * Moves data beyond the current pointer position from the XDR head[] buffer
  714. * into the page list. Any data that lies beyond current position + "len"
  715. * bytes is moved into the XDR tail[]. The current pointer is then
  716. * repositioned at the beginning of the first XDR page.
  717. */
  718. void xdr_enter_page(struct xdr_stream *xdr, unsigned int len)
  719. {
  720. len = xdr_align_pages(xdr, len);
  721. /*
  722. * Position current pointer at beginning of tail, and
  723. * set remaining message length.
  724. */
  725. if (len != 0)
  726. xdr_set_page_base(xdr, 0, len);
  727. }
  728. EXPORT_SYMBOL_GPL(xdr_enter_page);
  729. static struct kvec empty_iov = {.iov_base = NULL, .iov_len = 0};
  730. void
  731. xdr_buf_from_iov(struct kvec *iov, struct xdr_buf *buf)
  732. {
  733. buf->head[0] = *iov;
  734. buf->tail[0] = empty_iov;
  735. buf->page_len = 0;
  736. buf->buflen = buf->len = iov->iov_len;
  737. }
  738. EXPORT_SYMBOL_GPL(xdr_buf_from_iov);
  739. /* Sets subbuf to the portion of buf of length len beginning base bytes
  740. * from the start of buf. Returns -1 if base of length are out of bounds. */
  741. int
  742. xdr_buf_subsegment(struct xdr_buf *buf, struct xdr_buf *subbuf,
  743. unsigned int base, unsigned int len)
  744. {
  745. subbuf->buflen = subbuf->len = len;
  746. if (base < buf->head[0].iov_len) {
  747. subbuf->head[0].iov_base = buf->head[0].iov_base + base;
  748. subbuf->head[0].iov_len = min_t(unsigned int, len,
  749. buf->head[0].iov_len - base);
  750. len -= subbuf->head[0].iov_len;
  751. base = 0;
  752. } else {
  753. subbuf->head[0].iov_base = NULL;
  754. subbuf->head[0].iov_len = 0;
  755. base -= buf->head[0].iov_len;
  756. }
  757. if (base < buf->page_len) {
  758. subbuf->page_len = min(buf->page_len - base, len);
  759. base += buf->page_base;
  760. subbuf->page_base = base & ~PAGE_CACHE_MASK;
  761. subbuf->pages = &buf->pages[base >> PAGE_CACHE_SHIFT];
  762. len -= subbuf->page_len;
  763. base = 0;
  764. } else {
  765. base -= buf->page_len;
  766. subbuf->page_len = 0;
  767. }
  768. if (base < buf->tail[0].iov_len) {
  769. subbuf->tail[0].iov_base = buf->tail[0].iov_base + base;
  770. subbuf->tail[0].iov_len = min_t(unsigned int, len,
  771. buf->tail[0].iov_len - base);
  772. len -= subbuf->tail[0].iov_len;
  773. base = 0;
  774. } else {
  775. subbuf->tail[0].iov_base = NULL;
  776. subbuf->tail[0].iov_len = 0;
  777. base -= buf->tail[0].iov_len;
  778. }
  779. if (base || len)
  780. return -1;
  781. return 0;
  782. }
  783. EXPORT_SYMBOL_GPL(xdr_buf_subsegment);
  784. static void __read_bytes_from_xdr_buf(struct xdr_buf *subbuf, void *obj, unsigned int len)
  785. {
  786. unsigned int this_len;
  787. this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
  788. memcpy(obj, subbuf->head[0].iov_base, this_len);
  789. len -= this_len;
  790. obj += this_len;
  791. this_len = min_t(unsigned int, len, subbuf->page_len);
  792. if (this_len)
  793. _copy_from_pages(obj, subbuf->pages, subbuf->page_base, this_len);
  794. len -= this_len;
  795. obj += this_len;
  796. this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
  797. memcpy(obj, subbuf->tail[0].iov_base, this_len);
  798. }
  799. /* obj is assumed to point to allocated memory of size at least len: */
  800. int read_bytes_from_xdr_buf(struct xdr_buf *buf, unsigned int base, void *obj, unsigned int len)
  801. {
  802. struct xdr_buf subbuf;
  803. int status;
  804. status = xdr_buf_subsegment(buf, &subbuf, base, len);
  805. if (status != 0)
  806. return status;
  807. __read_bytes_from_xdr_buf(&subbuf, obj, len);
  808. return 0;
  809. }
  810. EXPORT_SYMBOL_GPL(read_bytes_from_xdr_buf);
  811. static void __write_bytes_to_xdr_buf(struct xdr_buf *subbuf, void *obj, unsigned int len)
  812. {
  813. unsigned int this_len;
  814. this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
  815. memcpy(subbuf->head[0].iov_base, obj, this_len);
  816. len -= this_len;
  817. obj += this_len;
  818. this_len = min_t(unsigned int, len, subbuf->page_len);
  819. if (this_len)
  820. _copy_to_pages(subbuf->pages, subbuf->page_base, obj, this_len);
  821. len -= this_len;
  822. obj += this_len;
  823. this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
  824. memcpy(subbuf->tail[0].iov_base, obj, this_len);
  825. }
  826. /* obj is assumed to point to allocated memory of size at least len: */
  827. int write_bytes_to_xdr_buf(struct xdr_buf *buf, unsigned int base, void *obj, unsigned int len)
  828. {
  829. struct xdr_buf subbuf;
  830. int status;
  831. status = xdr_buf_subsegment(buf, &subbuf, base, len);
  832. if (status != 0)
  833. return status;
  834. __write_bytes_to_xdr_buf(&subbuf, obj, len);
  835. return 0;
  836. }
  837. EXPORT_SYMBOL_GPL(write_bytes_to_xdr_buf);
  838. int
  839. xdr_decode_word(struct xdr_buf *buf, unsigned int base, u32 *obj)
  840. {
  841. __be32 raw;
  842. int status;
  843. status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
  844. if (status)
  845. return status;
  846. *obj = be32_to_cpu(raw);
  847. return 0;
  848. }
  849. EXPORT_SYMBOL_GPL(xdr_decode_word);
  850. int
  851. xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj)
  852. {
  853. __be32 raw = cpu_to_be32(obj);
  854. return write_bytes_to_xdr_buf(buf, base, &raw, sizeof(obj));
  855. }
  856. EXPORT_SYMBOL_GPL(xdr_encode_word);
  857. /* If the netobj starting offset bytes from the start of xdr_buf is contained
  858. * entirely in the head or the tail, set object to point to it; otherwise
  859. * try to find space for it at the end of the tail, copy it there, and
  860. * set obj to point to it. */
  861. int xdr_buf_read_netobj(struct xdr_buf *buf, struct xdr_netobj *obj, unsigned int offset)
  862. {
  863. struct xdr_buf subbuf;
  864. if (xdr_decode_word(buf, offset, &obj->len))
  865. return -EFAULT;
  866. if (xdr_buf_subsegment(buf, &subbuf, offset + 4, obj->len))
  867. return -EFAULT;
  868. /* Is the obj contained entirely in the head? */
  869. obj->data = subbuf.head[0].iov_base;
  870. if (subbuf.head[0].iov_len == obj->len)
  871. return 0;
  872. /* ..or is the obj contained entirely in the tail? */
  873. obj->data = subbuf.tail[0].iov_base;
  874. if (subbuf.tail[0].iov_len == obj->len)
  875. return 0;
  876. /* use end of tail as storage for obj:
  877. * (We don't copy to the beginning because then we'd have
  878. * to worry about doing a potentially overlapping copy.
  879. * This assumes the object is at most half the length of the
  880. * tail.) */
  881. if (obj->len > buf->buflen - buf->len)
  882. return -ENOMEM;
  883. if (buf->tail[0].iov_len != 0)
  884. obj->data = buf->tail[0].iov_base + buf->tail[0].iov_len;
  885. else
  886. obj->data = buf->head[0].iov_base + buf->head[0].iov_len;
  887. __read_bytes_from_xdr_buf(&subbuf, obj->data, obj->len);
  888. return 0;
  889. }
  890. EXPORT_SYMBOL_GPL(xdr_buf_read_netobj);
  891. /* Returns 0 on success, or else a negative error code. */
  892. static int
  893. xdr_xcode_array2(struct xdr_buf *buf, unsigned int base,
  894. struct xdr_array2_desc *desc, int encode)
  895. {
  896. char *elem = NULL, *c;
  897. unsigned int copied = 0, todo, avail_here;
  898. struct page **ppages = NULL;
  899. int err;
  900. if (encode) {
  901. if (xdr_encode_word(buf, base, desc->array_len) != 0)
  902. return -EINVAL;
  903. } else {
  904. if (xdr_decode_word(buf, base, &desc->array_len) != 0 ||
  905. desc->array_len > desc->array_maxlen ||
  906. (unsigned long) base + 4 + desc->array_len *
  907. desc->elem_size > buf->len)
  908. return -EINVAL;
  909. }
  910. base += 4;
  911. if (!desc->xcode)
  912. return 0;
  913. todo = desc->array_len * desc->elem_size;
  914. /* process head */
  915. if (todo && base < buf->head->iov_len) {
  916. c = buf->head->iov_base + base;
  917. avail_here = min_t(unsigned int, todo,
  918. buf->head->iov_len - base);
  919. todo -= avail_here;
  920. while (avail_here >= desc->elem_size) {
  921. err = desc->xcode(desc, c);
  922. if (err)
  923. goto out;
  924. c += desc->elem_size;
  925. avail_here -= desc->elem_size;
  926. }
  927. if (avail_here) {
  928. if (!elem) {
  929. elem = kmalloc(desc->elem_size, GFP_KERNEL);
  930. err = -ENOMEM;
  931. if (!elem)
  932. goto out;
  933. }
  934. if (encode) {
  935. err = desc->xcode(desc, elem);
  936. if (err)
  937. goto out;
  938. memcpy(c, elem, avail_here);
  939. } else
  940. memcpy(elem, c, avail_here);
  941. copied = avail_here;
  942. }
  943. base = buf->head->iov_len; /* align to start of pages */
  944. }
  945. /* process pages array */
  946. base -= buf->head->iov_len;
  947. if (todo && base < buf->page_len) {
  948. unsigned int avail_page;
  949. avail_here = min(todo, buf->page_len - base);
  950. todo -= avail_here;
  951. base += buf->page_base;
  952. ppages = buf->pages + (base >> PAGE_CACHE_SHIFT);
  953. base &= ~PAGE_CACHE_MASK;
  954. avail_page = min_t(unsigned int, PAGE_CACHE_SIZE - base,
  955. avail_here);
  956. c = kmap(*ppages) + base;
  957. while (avail_here) {
  958. avail_here -= avail_page;
  959. if (copied || avail_page < desc->elem_size) {
  960. unsigned int l = min(avail_page,
  961. desc->elem_size - copied);
  962. if (!elem) {
  963. elem = kmalloc(desc->elem_size,
  964. GFP_KERNEL);
  965. err = -ENOMEM;
  966. if (!elem)
  967. goto out;
  968. }
  969. if (encode) {
  970. if (!copied) {
  971. err = desc->xcode(desc, elem);
  972. if (err)
  973. goto out;
  974. }
  975. memcpy(c, elem + copied, l);
  976. copied += l;
  977. if (copied == desc->elem_size)
  978. copied = 0;
  979. } else {
  980. memcpy(elem + copied, c, l);
  981. copied += l;
  982. if (copied == desc->elem_size) {
  983. err = desc->xcode(desc, elem);
  984. if (err)
  985. goto out;
  986. copied = 0;
  987. }
  988. }
  989. avail_page -= l;
  990. c += l;
  991. }
  992. while (avail_page >= desc->elem_size) {
  993. err = desc->xcode(desc, c);
  994. if (err)
  995. goto out;
  996. c += desc->elem_size;
  997. avail_page -= desc->elem_size;
  998. }
  999. if (avail_page) {
  1000. unsigned int l = min(avail_page,
  1001. desc->elem_size - copied);
  1002. if (!elem) {
  1003. elem = kmalloc(desc->elem_size,
  1004. GFP_KERNEL);
  1005. err = -ENOMEM;
  1006. if (!elem)
  1007. goto out;
  1008. }
  1009. if (encode) {
  1010. if (!copied) {
  1011. err = desc->xcode(desc, elem);
  1012. if (err)
  1013. goto out;
  1014. }
  1015. memcpy(c, elem + copied, l);
  1016. copied += l;
  1017. if (copied == desc->elem_size)
  1018. copied = 0;
  1019. } else {
  1020. memcpy(elem + copied, c, l);
  1021. copied += l;
  1022. if (copied == desc->elem_size) {
  1023. err = desc->xcode(desc, elem);
  1024. if (err)
  1025. goto out;
  1026. copied = 0;
  1027. }
  1028. }
  1029. }
  1030. if (avail_here) {
  1031. kunmap(*ppages);
  1032. ppages++;
  1033. c = kmap(*ppages);
  1034. }
  1035. avail_page = min(avail_here,
  1036. (unsigned int) PAGE_CACHE_SIZE);
  1037. }
  1038. base = buf->page_len; /* align to start of tail */
  1039. }
  1040. /* process tail */
  1041. base -= buf->page_len;
  1042. if (todo) {
  1043. c = buf->tail->iov_base + base;
  1044. if (copied) {
  1045. unsigned int l = desc->elem_size - copied;
  1046. if (encode)
  1047. memcpy(c, elem + copied, l);
  1048. else {
  1049. memcpy(elem + copied, c, l);
  1050. err = desc->xcode(desc, elem);
  1051. if (err)
  1052. goto out;
  1053. }
  1054. todo -= l;
  1055. c += l;
  1056. }
  1057. while (todo) {
  1058. err = desc->xcode(desc, c);
  1059. if (err)
  1060. goto out;
  1061. c += desc->elem_size;
  1062. todo -= desc->elem_size;
  1063. }
  1064. }
  1065. err = 0;
  1066. out:
  1067. kfree(elem);
  1068. if (ppages)
  1069. kunmap(*ppages);
  1070. return err;
  1071. }
  1072. int
  1073. xdr_decode_array2(struct xdr_buf *buf, unsigned int base,
  1074. struct xdr_array2_desc *desc)
  1075. {
  1076. if (base >= buf->len)
  1077. return -EINVAL;
  1078. return xdr_xcode_array2(buf, base, desc, 0);
  1079. }
  1080. EXPORT_SYMBOL_GPL(xdr_decode_array2);
  1081. int
  1082. xdr_encode_array2(struct xdr_buf *buf, unsigned int base,
  1083. struct xdr_array2_desc *desc)
  1084. {
  1085. if ((unsigned long) base + 4 + desc->array_len * desc->elem_size >
  1086. buf->head->iov_len + buf->page_len + buf->tail->iov_len)
  1087. return -EINVAL;
  1088. return xdr_xcode_array2(buf, base, desc, 1);
  1089. }
  1090. EXPORT_SYMBOL_GPL(xdr_encode_array2);
  1091. int
  1092. xdr_process_buf(struct xdr_buf *buf, unsigned int offset, unsigned int len,
  1093. int (*actor)(struct scatterlist *, void *), void *data)
  1094. {
  1095. int i, ret = 0;
  1096. unsigned int page_len, thislen, page_offset;
  1097. struct scatterlist sg[1];
  1098. sg_init_table(sg, 1);
  1099. if (offset >= buf->head[0].iov_len) {
  1100. offset -= buf->head[0].iov_len;
  1101. } else {
  1102. thislen = buf->head[0].iov_len - offset;
  1103. if (thislen > len)
  1104. thislen = len;
  1105. sg_set_buf(sg, buf->head[0].iov_base + offset, thislen);
  1106. ret = actor(sg, data);
  1107. if (ret)
  1108. goto out;
  1109. offset = 0;
  1110. len -= thislen;
  1111. }
  1112. if (len == 0)
  1113. goto out;
  1114. if (offset >= buf->page_len) {
  1115. offset -= buf->page_len;
  1116. } else {
  1117. page_len = buf->page_len - offset;
  1118. if (page_len > len)
  1119. page_len = len;
  1120. len -= page_len;
  1121. page_offset = (offset + buf->page_base) & (PAGE_CACHE_SIZE - 1);
  1122. i = (offset + buf->page_base) >> PAGE_CACHE_SHIFT;
  1123. thislen = PAGE_CACHE_SIZE - page_offset;
  1124. do {
  1125. if (thislen > page_len)
  1126. thislen = page_len;
  1127. sg_set_page(sg, buf->pages[i], thislen, page_offset);
  1128. ret = actor(sg, data);
  1129. if (ret)
  1130. goto out;
  1131. page_len -= thislen;
  1132. i++;
  1133. page_offset = 0;
  1134. thislen = PAGE_CACHE_SIZE;
  1135. } while (page_len != 0);
  1136. offset = 0;
  1137. }
  1138. if (len == 0)
  1139. goto out;
  1140. if (offset < buf->tail[0].iov_len) {
  1141. thislen = buf->tail[0].iov_len - offset;
  1142. if (thislen > len)
  1143. thislen = len;
  1144. sg_set_buf(sg, buf->tail[0].iov_base + offset, thislen);
  1145. ret = actor(sg, data);
  1146. len -= thislen;
  1147. }
  1148. if (len != 0)
  1149. ret = -EINVAL;
  1150. out:
  1151. return ret;
  1152. }
  1153. EXPORT_SYMBOL_GPL(xdr_process_buf);