xdr.c 33 KB

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