xdr.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  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. if (nwords > xdr->nwords) {
  651. nwords = xdr->nwords;
  652. len = nwords << 2;
  653. }
  654. /* Realign pages to current pointer position */
  655. iov = buf->head;
  656. if (iov->iov_len > cur)
  657. xdr_shrink_bufhead(buf, iov->iov_len - cur);
  658. /* Truncate page data and move it into the tail */
  659. if (buf->page_len > len)
  660. xdr_shrink_pagelen(buf, buf->page_len - len);
  661. xdr->nwords = XDR_QUADLEN(buf->len - cur);
  662. return len;
  663. }
  664. /**
  665. * xdr_read_pages - Ensure page-based XDR data to decode is aligned at current pointer position
  666. * @xdr: pointer to xdr_stream struct
  667. * @len: number of bytes of page data
  668. *
  669. * Moves data beyond the current pointer position from the XDR head[] buffer
  670. * into the page list. Any data that lies beyond current position + "len"
  671. * bytes is moved into the XDR tail[].
  672. *
  673. * Returns the number of XDR encoded bytes now contained in the pages
  674. */
  675. unsigned int xdr_read_pages(struct xdr_stream *xdr, unsigned int len)
  676. {
  677. struct xdr_buf *buf = xdr->buf;
  678. struct kvec *iov;
  679. unsigned int nwords;
  680. unsigned int end;
  681. unsigned int padding;
  682. len = xdr_align_pages(xdr, len);
  683. if (len == 0)
  684. return 0;
  685. nwords = XDR_QUADLEN(len);
  686. padding = (nwords << 2) - len;
  687. xdr->iov = iov = buf->tail;
  688. /* Compute remaining message length. */
  689. end = ((xdr->nwords - nwords) << 2) + padding;
  690. if (end > iov->iov_len)
  691. end = iov->iov_len;
  692. /*
  693. * Position current pointer at beginning of tail, and
  694. * set remaining message length.
  695. */
  696. xdr->p = (__be32 *)((char *)iov->iov_base + padding);
  697. xdr->end = (__be32 *)((char *)iov->iov_base + end);
  698. xdr->page_ptr = NULL;
  699. xdr->nwords = XDR_QUADLEN(end - padding);
  700. return len;
  701. }
  702. EXPORT_SYMBOL_GPL(xdr_read_pages);
  703. /**
  704. * xdr_enter_page - decode data from the XDR page
  705. * @xdr: pointer to xdr_stream struct
  706. * @len: number of bytes of page data
  707. *
  708. * Moves data beyond the current pointer position from the XDR head[] buffer
  709. * into the page list. Any data that lies beyond current position + "len"
  710. * bytes is moved into the XDR tail[]. The current pointer is then
  711. * repositioned at the beginning of the first XDR page.
  712. */
  713. void xdr_enter_page(struct xdr_stream *xdr, unsigned int len)
  714. {
  715. len = xdr_align_pages(xdr, len);
  716. /*
  717. * Position current pointer at beginning of tail, and
  718. * set remaining message length.
  719. */
  720. if (len != 0)
  721. xdr_set_page_base(xdr, 0, len);
  722. }
  723. EXPORT_SYMBOL_GPL(xdr_enter_page);
  724. static struct kvec empty_iov = {.iov_base = NULL, .iov_len = 0};
  725. void
  726. xdr_buf_from_iov(struct kvec *iov, struct xdr_buf *buf)
  727. {
  728. buf->head[0] = *iov;
  729. buf->tail[0] = empty_iov;
  730. buf->page_len = 0;
  731. buf->buflen = buf->len = iov->iov_len;
  732. }
  733. EXPORT_SYMBOL_GPL(xdr_buf_from_iov);
  734. /* Sets subbuf to the portion of buf of length len beginning base bytes
  735. * from the start of buf. Returns -1 if base of length are out of bounds. */
  736. int
  737. xdr_buf_subsegment(struct xdr_buf *buf, struct xdr_buf *subbuf,
  738. unsigned int base, unsigned int len)
  739. {
  740. subbuf->buflen = subbuf->len = len;
  741. if (base < buf->head[0].iov_len) {
  742. subbuf->head[0].iov_base = buf->head[0].iov_base + base;
  743. subbuf->head[0].iov_len = min_t(unsigned int, len,
  744. buf->head[0].iov_len - base);
  745. len -= subbuf->head[0].iov_len;
  746. base = 0;
  747. } else {
  748. subbuf->head[0].iov_base = NULL;
  749. subbuf->head[0].iov_len = 0;
  750. base -= buf->head[0].iov_len;
  751. }
  752. if (base < buf->page_len) {
  753. subbuf->page_len = min(buf->page_len - base, len);
  754. base += buf->page_base;
  755. subbuf->page_base = base & ~PAGE_CACHE_MASK;
  756. subbuf->pages = &buf->pages[base >> PAGE_CACHE_SHIFT];
  757. len -= subbuf->page_len;
  758. base = 0;
  759. } else {
  760. base -= buf->page_len;
  761. subbuf->page_len = 0;
  762. }
  763. if (base < buf->tail[0].iov_len) {
  764. subbuf->tail[0].iov_base = buf->tail[0].iov_base + base;
  765. subbuf->tail[0].iov_len = min_t(unsigned int, len,
  766. buf->tail[0].iov_len - base);
  767. len -= subbuf->tail[0].iov_len;
  768. base = 0;
  769. } else {
  770. subbuf->tail[0].iov_base = NULL;
  771. subbuf->tail[0].iov_len = 0;
  772. base -= buf->tail[0].iov_len;
  773. }
  774. if (base || len)
  775. return -1;
  776. return 0;
  777. }
  778. EXPORT_SYMBOL_GPL(xdr_buf_subsegment);
  779. static void __read_bytes_from_xdr_buf(struct xdr_buf *subbuf, void *obj, unsigned int len)
  780. {
  781. unsigned int this_len;
  782. this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
  783. memcpy(obj, subbuf->head[0].iov_base, this_len);
  784. len -= this_len;
  785. obj += this_len;
  786. this_len = min_t(unsigned int, len, subbuf->page_len);
  787. if (this_len)
  788. _copy_from_pages(obj, subbuf->pages, subbuf->page_base, this_len);
  789. len -= this_len;
  790. obj += this_len;
  791. this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
  792. memcpy(obj, subbuf->tail[0].iov_base, this_len);
  793. }
  794. /* obj is assumed to point to allocated memory of size at least len: */
  795. int read_bytes_from_xdr_buf(struct xdr_buf *buf, unsigned int base, void *obj, unsigned int len)
  796. {
  797. struct xdr_buf subbuf;
  798. int status;
  799. status = xdr_buf_subsegment(buf, &subbuf, base, len);
  800. if (status != 0)
  801. return status;
  802. __read_bytes_from_xdr_buf(&subbuf, obj, len);
  803. return 0;
  804. }
  805. EXPORT_SYMBOL_GPL(read_bytes_from_xdr_buf);
  806. static void __write_bytes_to_xdr_buf(struct xdr_buf *subbuf, void *obj, unsigned int len)
  807. {
  808. unsigned int this_len;
  809. this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
  810. memcpy(subbuf->head[0].iov_base, obj, this_len);
  811. len -= this_len;
  812. obj += this_len;
  813. this_len = min_t(unsigned int, len, subbuf->page_len);
  814. if (this_len)
  815. _copy_to_pages(subbuf->pages, subbuf->page_base, obj, this_len);
  816. len -= this_len;
  817. obj += this_len;
  818. this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
  819. memcpy(subbuf->tail[0].iov_base, obj, this_len);
  820. }
  821. /* obj is assumed to point to allocated memory of size at least len: */
  822. int write_bytes_to_xdr_buf(struct xdr_buf *buf, unsigned int base, void *obj, unsigned int len)
  823. {
  824. struct xdr_buf subbuf;
  825. int status;
  826. status = xdr_buf_subsegment(buf, &subbuf, base, len);
  827. if (status != 0)
  828. return status;
  829. __write_bytes_to_xdr_buf(&subbuf, obj, len);
  830. return 0;
  831. }
  832. EXPORT_SYMBOL_GPL(write_bytes_to_xdr_buf);
  833. int
  834. xdr_decode_word(struct xdr_buf *buf, unsigned int base, u32 *obj)
  835. {
  836. __be32 raw;
  837. int status;
  838. status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
  839. if (status)
  840. return status;
  841. *obj = be32_to_cpu(raw);
  842. return 0;
  843. }
  844. EXPORT_SYMBOL_GPL(xdr_decode_word);
  845. int
  846. xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj)
  847. {
  848. __be32 raw = cpu_to_be32(obj);
  849. return write_bytes_to_xdr_buf(buf, base, &raw, sizeof(obj));
  850. }
  851. EXPORT_SYMBOL_GPL(xdr_encode_word);
  852. /* If the netobj starting offset bytes from the start of xdr_buf is contained
  853. * entirely in the head or the tail, set object to point to it; otherwise
  854. * try to find space for it at the end of the tail, copy it there, and
  855. * set obj to point to it. */
  856. int xdr_buf_read_netobj(struct xdr_buf *buf, struct xdr_netobj *obj, unsigned int offset)
  857. {
  858. struct xdr_buf subbuf;
  859. if (xdr_decode_word(buf, offset, &obj->len))
  860. return -EFAULT;
  861. if (xdr_buf_subsegment(buf, &subbuf, offset + 4, obj->len))
  862. return -EFAULT;
  863. /* Is the obj contained entirely in the head? */
  864. obj->data = subbuf.head[0].iov_base;
  865. if (subbuf.head[0].iov_len == obj->len)
  866. return 0;
  867. /* ..or is the obj contained entirely in the tail? */
  868. obj->data = subbuf.tail[0].iov_base;
  869. if (subbuf.tail[0].iov_len == obj->len)
  870. return 0;
  871. /* use end of tail as storage for obj:
  872. * (We don't copy to the beginning because then we'd have
  873. * to worry about doing a potentially overlapping copy.
  874. * This assumes the object is at most half the length of the
  875. * tail.) */
  876. if (obj->len > buf->buflen - buf->len)
  877. return -ENOMEM;
  878. if (buf->tail[0].iov_len != 0)
  879. obj->data = buf->tail[0].iov_base + buf->tail[0].iov_len;
  880. else
  881. obj->data = buf->head[0].iov_base + buf->head[0].iov_len;
  882. __read_bytes_from_xdr_buf(&subbuf, obj->data, obj->len);
  883. return 0;
  884. }
  885. EXPORT_SYMBOL_GPL(xdr_buf_read_netobj);
  886. /* Returns 0 on success, or else a negative error code. */
  887. static int
  888. xdr_xcode_array2(struct xdr_buf *buf, unsigned int base,
  889. struct xdr_array2_desc *desc, int encode)
  890. {
  891. char *elem = NULL, *c;
  892. unsigned int copied = 0, todo, avail_here;
  893. struct page **ppages = NULL;
  894. int err;
  895. if (encode) {
  896. if (xdr_encode_word(buf, base, desc->array_len) != 0)
  897. return -EINVAL;
  898. } else {
  899. if (xdr_decode_word(buf, base, &desc->array_len) != 0 ||
  900. desc->array_len > desc->array_maxlen ||
  901. (unsigned long) base + 4 + desc->array_len *
  902. desc->elem_size > buf->len)
  903. return -EINVAL;
  904. }
  905. base += 4;
  906. if (!desc->xcode)
  907. return 0;
  908. todo = desc->array_len * desc->elem_size;
  909. /* process head */
  910. if (todo && base < buf->head->iov_len) {
  911. c = buf->head->iov_base + base;
  912. avail_here = min_t(unsigned int, todo,
  913. buf->head->iov_len - base);
  914. todo -= avail_here;
  915. while (avail_here >= desc->elem_size) {
  916. err = desc->xcode(desc, c);
  917. if (err)
  918. goto out;
  919. c += desc->elem_size;
  920. avail_here -= desc->elem_size;
  921. }
  922. if (avail_here) {
  923. if (!elem) {
  924. elem = kmalloc(desc->elem_size, GFP_KERNEL);
  925. err = -ENOMEM;
  926. if (!elem)
  927. goto out;
  928. }
  929. if (encode) {
  930. err = desc->xcode(desc, elem);
  931. if (err)
  932. goto out;
  933. memcpy(c, elem, avail_here);
  934. } else
  935. memcpy(elem, c, avail_here);
  936. copied = avail_here;
  937. }
  938. base = buf->head->iov_len; /* align to start of pages */
  939. }
  940. /* process pages array */
  941. base -= buf->head->iov_len;
  942. if (todo && base < buf->page_len) {
  943. unsigned int avail_page;
  944. avail_here = min(todo, buf->page_len - base);
  945. todo -= avail_here;
  946. base += buf->page_base;
  947. ppages = buf->pages + (base >> PAGE_CACHE_SHIFT);
  948. base &= ~PAGE_CACHE_MASK;
  949. avail_page = min_t(unsigned int, PAGE_CACHE_SIZE - base,
  950. avail_here);
  951. c = kmap(*ppages) + base;
  952. while (avail_here) {
  953. avail_here -= avail_page;
  954. if (copied || avail_page < desc->elem_size) {
  955. unsigned int l = min(avail_page,
  956. desc->elem_size - copied);
  957. if (!elem) {
  958. elem = kmalloc(desc->elem_size,
  959. GFP_KERNEL);
  960. err = -ENOMEM;
  961. if (!elem)
  962. goto out;
  963. }
  964. if (encode) {
  965. if (!copied) {
  966. err = desc->xcode(desc, elem);
  967. if (err)
  968. goto out;
  969. }
  970. memcpy(c, elem + copied, l);
  971. copied += l;
  972. if (copied == desc->elem_size)
  973. copied = 0;
  974. } else {
  975. memcpy(elem + copied, c, l);
  976. copied += l;
  977. if (copied == desc->elem_size) {
  978. err = desc->xcode(desc, elem);
  979. if (err)
  980. goto out;
  981. copied = 0;
  982. }
  983. }
  984. avail_page -= l;
  985. c += l;
  986. }
  987. while (avail_page >= desc->elem_size) {
  988. err = desc->xcode(desc, c);
  989. if (err)
  990. goto out;
  991. c += desc->elem_size;
  992. avail_page -= desc->elem_size;
  993. }
  994. if (avail_page) {
  995. unsigned int l = min(avail_page,
  996. desc->elem_size - copied);
  997. if (!elem) {
  998. elem = kmalloc(desc->elem_size,
  999. GFP_KERNEL);
  1000. err = -ENOMEM;
  1001. if (!elem)
  1002. goto out;
  1003. }
  1004. if (encode) {
  1005. if (!copied) {
  1006. err = desc->xcode(desc, elem);
  1007. if (err)
  1008. goto out;
  1009. }
  1010. memcpy(c, elem + copied, l);
  1011. copied += l;
  1012. if (copied == desc->elem_size)
  1013. copied = 0;
  1014. } else {
  1015. memcpy(elem + copied, c, l);
  1016. copied += l;
  1017. if (copied == desc->elem_size) {
  1018. err = desc->xcode(desc, elem);
  1019. if (err)
  1020. goto out;
  1021. copied = 0;
  1022. }
  1023. }
  1024. }
  1025. if (avail_here) {
  1026. kunmap(*ppages);
  1027. ppages++;
  1028. c = kmap(*ppages);
  1029. }
  1030. avail_page = min(avail_here,
  1031. (unsigned int) PAGE_CACHE_SIZE);
  1032. }
  1033. base = buf->page_len; /* align to start of tail */
  1034. }
  1035. /* process tail */
  1036. base -= buf->page_len;
  1037. if (todo) {
  1038. c = buf->tail->iov_base + base;
  1039. if (copied) {
  1040. unsigned int l = desc->elem_size - copied;
  1041. if (encode)
  1042. memcpy(c, elem + copied, l);
  1043. else {
  1044. memcpy(elem + copied, c, l);
  1045. err = desc->xcode(desc, elem);
  1046. if (err)
  1047. goto out;
  1048. }
  1049. todo -= l;
  1050. c += l;
  1051. }
  1052. while (todo) {
  1053. err = desc->xcode(desc, c);
  1054. if (err)
  1055. goto out;
  1056. c += desc->elem_size;
  1057. todo -= desc->elem_size;
  1058. }
  1059. }
  1060. err = 0;
  1061. out:
  1062. kfree(elem);
  1063. if (ppages)
  1064. kunmap(*ppages);
  1065. return err;
  1066. }
  1067. int
  1068. xdr_decode_array2(struct xdr_buf *buf, unsigned int base,
  1069. struct xdr_array2_desc *desc)
  1070. {
  1071. if (base >= buf->len)
  1072. return -EINVAL;
  1073. return xdr_xcode_array2(buf, base, desc, 0);
  1074. }
  1075. EXPORT_SYMBOL_GPL(xdr_decode_array2);
  1076. int
  1077. xdr_encode_array2(struct xdr_buf *buf, unsigned int base,
  1078. struct xdr_array2_desc *desc)
  1079. {
  1080. if ((unsigned long) base + 4 + desc->array_len * desc->elem_size >
  1081. buf->head->iov_len + buf->page_len + buf->tail->iov_len)
  1082. return -EINVAL;
  1083. return xdr_xcode_array2(buf, base, desc, 1);
  1084. }
  1085. EXPORT_SYMBOL_GPL(xdr_encode_array2);
  1086. int
  1087. xdr_process_buf(struct xdr_buf *buf, unsigned int offset, unsigned int len,
  1088. int (*actor)(struct scatterlist *, void *), void *data)
  1089. {
  1090. int i, ret = 0;
  1091. unsigned int page_len, thislen, page_offset;
  1092. struct scatterlist sg[1];
  1093. sg_init_table(sg, 1);
  1094. if (offset >= buf->head[0].iov_len) {
  1095. offset -= buf->head[0].iov_len;
  1096. } else {
  1097. thislen = buf->head[0].iov_len - offset;
  1098. if (thislen > len)
  1099. thislen = len;
  1100. sg_set_buf(sg, buf->head[0].iov_base + offset, thislen);
  1101. ret = actor(sg, data);
  1102. if (ret)
  1103. goto out;
  1104. offset = 0;
  1105. len -= thislen;
  1106. }
  1107. if (len == 0)
  1108. goto out;
  1109. if (offset >= buf->page_len) {
  1110. offset -= buf->page_len;
  1111. } else {
  1112. page_len = buf->page_len - offset;
  1113. if (page_len > len)
  1114. page_len = len;
  1115. len -= page_len;
  1116. page_offset = (offset + buf->page_base) & (PAGE_CACHE_SIZE - 1);
  1117. i = (offset + buf->page_base) >> PAGE_CACHE_SHIFT;
  1118. thislen = PAGE_CACHE_SIZE - page_offset;
  1119. do {
  1120. if (thislen > page_len)
  1121. thislen = page_len;
  1122. sg_set_page(sg, buf->pages[i], thislen, page_offset);
  1123. ret = actor(sg, data);
  1124. if (ret)
  1125. goto out;
  1126. page_len -= thislen;
  1127. i++;
  1128. page_offset = 0;
  1129. thislen = PAGE_CACHE_SIZE;
  1130. } while (page_len != 0);
  1131. offset = 0;
  1132. }
  1133. if (len == 0)
  1134. goto out;
  1135. if (offset < buf->tail[0].iov_len) {
  1136. thislen = buf->tail[0].iov_len - offset;
  1137. if (thislen > len)
  1138. thislen = len;
  1139. sg_set_buf(sg, buf->tail[0].iov_base + offset, thislen);
  1140. ret = actor(sg, data);
  1141. len -= thislen;
  1142. }
  1143. if (len != 0)
  1144. ret = -EINVAL;
  1145. out:
  1146. return ret;
  1147. }
  1148. EXPORT_SYMBOL_GPL(xdr_process_buf);