xdr.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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. void
  103. xdr_encode_pages(struct xdr_buf *xdr, struct page **pages, unsigned int base,
  104. unsigned int len)
  105. {
  106. struct kvec *tail = xdr->tail;
  107. u32 *p;
  108. xdr->pages = pages;
  109. xdr->page_base = base;
  110. xdr->page_len = len;
  111. p = (u32 *)xdr->head[0].iov_base + XDR_QUADLEN(xdr->head[0].iov_len);
  112. tail->iov_base = p;
  113. tail->iov_len = 0;
  114. if (len & 3) {
  115. unsigned int pad = 4 - (len & 3);
  116. *p = 0;
  117. tail->iov_base = (char *)p + (len & 3);
  118. tail->iov_len = pad;
  119. len += pad;
  120. }
  121. xdr->buflen += len;
  122. xdr->len += len;
  123. }
  124. EXPORT_SYMBOL_GPL(xdr_encode_pages);
  125. void
  126. xdr_inline_pages(struct xdr_buf *xdr, unsigned int offset,
  127. struct page **pages, unsigned int base, unsigned int len)
  128. {
  129. struct kvec *head = xdr->head;
  130. struct kvec *tail = xdr->tail;
  131. char *buf = (char *)head->iov_base;
  132. unsigned int buflen = head->iov_len;
  133. head->iov_len = offset;
  134. xdr->pages = pages;
  135. xdr->page_base = base;
  136. xdr->page_len = len;
  137. tail->iov_base = buf + offset;
  138. tail->iov_len = buflen - offset;
  139. xdr->buflen += len;
  140. }
  141. EXPORT_SYMBOL_GPL(xdr_inline_pages);
  142. /*
  143. * Helper routines for doing 'memmove' like operations on a struct xdr_buf
  144. *
  145. * _shift_data_right_pages
  146. * @pages: vector of pages containing both the source and dest memory area.
  147. * @pgto_base: page vector address of destination
  148. * @pgfrom_base: page vector address of source
  149. * @len: number of bytes to copy
  150. *
  151. * Note: the addresses pgto_base and pgfrom_base are both calculated in
  152. * the same way:
  153. * if a memory area starts at byte 'base' in page 'pages[i]',
  154. * then its address is given as (i << PAGE_CACHE_SHIFT) + base
  155. * Also note: pgfrom_base must be < pgto_base, but the memory areas
  156. * they point to may overlap.
  157. */
  158. static void
  159. _shift_data_right_pages(struct page **pages, size_t pgto_base,
  160. size_t pgfrom_base, size_t len)
  161. {
  162. struct page **pgfrom, **pgto;
  163. char *vfrom, *vto;
  164. size_t copy;
  165. BUG_ON(pgto_base <= pgfrom_base);
  166. pgto_base += len;
  167. pgfrom_base += len;
  168. pgto = pages + (pgto_base >> PAGE_CACHE_SHIFT);
  169. pgfrom = pages + (pgfrom_base >> PAGE_CACHE_SHIFT);
  170. pgto_base &= ~PAGE_CACHE_MASK;
  171. pgfrom_base &= ~PAGE_CACHE_MASK;
  172. do {
  173. /* Are any pointers crossing a page boundary? */
  174. if (pgto_base == 0) {
  175. pgto_base = PAGE_CACHE_SIZE;
  176. pgto--;
  177. }
  178. if (pgfrom_base == 0) {
  179. pgfrom_base = PAGE_CACHE_SIZE;
  180. pgfrom--;
  181. }
  182. copy = len;
  183. if (copy > pgto_base)
  184. copy = pgto_base;
  185. if (copy > pgfrom_base)
  186. copy = pgfrom_base;
  187. pgto_base -= copy;
  188. pgfrom_base -= copy;
  189. vto = kmap_atomic(*pgto, KM_USER0);
  190. vfrom = kmap_atomic(*pgfrom, KM_USER1);
  191. memmove(vto + pgto_base, vfrom + pgfrom_base, copy);
  192. flush_dcache_page(*pgto);
  193. kunmap_atomic(vfrom, KM_USER1);
  194. kunmap_atomic(vto, KM_USER0);
  195. } while ((len -= copy) != 0);
  196. }
  197. /*
  198. * _copy_to_pages
  199. * @pages: array of pages
  200. * @pgbase: page vector address of destination
  201. * @p: pointer to source data
  202. * @len: length
  203. *
  204. * Copies data from an arbitrary memory location into an array of pages
  205. * The copy is assumed to be non-overlapping.
  206. */
  207. static void
  208. _copy_to_pages(struct page **pages, size_t pgbase, const char *p, size_t len)
  209. {
  210. struct page **pgto;
  211. char *vto;
  212. size_t copy;
  213. pgto = pages + (pgbase >> PAGE_CACHE_SHIFT);
  214. pgbase &= ~PAGE_CACHE_MASK;
  215. for (;;) {
  216. copy = PAGE_CACHE_SIZE - pgbase;
  217. if (copy > len)
  218. copy = len;
  219. vto = kmap_atomic(*pgto, KM_USER0);
  220. memcpy(vto + pgbase, p, copy);
  221. kunmap_atomic(vto, KM_USER0);
  222. len -= copy;
  223. if (len == 0)
  224. break;
  225. pgbase += copy;
  226. if (pgbase == PAGE_CACHE_SIZE) {
  227. flush_dcache_page(*pgto);
  228. pgbase = 0;
  229. pgto++;
  230. }
  231. p += copy;
  232. }
  233. flush_dcache_page(*pgto);
  234. }
  235. /*
  236. * _copy_from_pages
  237. * @p: pointer to destination
  238. * @pages: array of pages
  239. * @pgbase: offset of source data
  240. * @len: length
  241. *
  242. * Copies data into an arbitrary memory location from an array of pages
  243. * The copy is assumed to be non-overlapping.
  244. */
  245. static void
  246. _copy_from_pages(char *p, struct page **pages, size_t pgbase, size_t len)
  247. {
  248. struct page **pgfrom;
  249. char *vfrom;
  250. size_t copy;
  251. pgfrom = pages + (pgbase >> PAGE_CACHE_SHIFT);
  252. pgbase &= ~PAGE_CACHE_MASK;
  253. do {
  254. copy = PAGE_CACHE_SIZE - pgbase;
  255. if (copy > len)
  256. copy = len;
  257. vfrom = kmap_atomic(*pgfrom, KM_USER0);
  258. memcpy(p, vfrom + pgbase, copy);
  259. kunmap_atomic(vfrom, KM_USER0);
  260. pgbase += copy;
  261. if (pgbase == PAGE_CACHE_SIZE) {
  262. pgbase = 0;
  263. pgfrom++;
  264. }
  265. p += copy;
  266. } while ((len -= copy) != 0);
  267. }
  268. /*
  269. * xdr_shrink_bufhead
  270. * @buf: xdr_buf
  271. * @len: bytes to remove from buf->head[0]
  272. *
  273. * Shrinks XDR buffer's header kvec buf->head[0] by
  274. * 'len' bytes. The extra data is not lost, but is instead
  275. * moved into the inlined pages and/or the tail.
  276. */
  277. static void
  278. xdr_shrink_bufhead(struct xdr_buf *buf, size_t len)
  279. {
  280. struct kvec *head, *tail;
  281. size_t copy, offs;
  282. unsigned int pglen = buf->page_len;
  283. tail = buf->tail;
  284. head = buf->head;
  285. BUG_ON (len > head->iov_len);
  286. /* Shift the tail first */
  287. if (tail->iov_len != 0) {
  288. if (tail->iov_len > len) {
  289. copy = tail->iov_len - len;
  290. memmove((char *)tail->iov_base + len,
  291. tail->iov_base, copy);
  292. }
  293. /* Copy from the inlined pages into the tail */
  294. copy = len;
  295. if (copy > pglen)
  296. copy = pglen;
  297. offs = len - copy;
  298. if (offs >= tail->iov_len)
  299. copy = 0;
  300. else if (copy > tail->iov_len - offs)
  301. copy = tail->iov_len - offs;
  302. if (copy != 0)
  303. _copy_from_pages((char *)tail->iov_base + offs,
  304. buf->pages,
  305. buf->page_base + pglen + offs - len,
  306. copy);
  307. /* Do we also need to copy data from the head into the tail ? */
  308. if (len > pglen) {
  309. offs = copy = len - pglen;
  310. if (copy > tail->iov_len)
  311. copy = tail->iov_len;
  312. memcpy(tail->iov_base,
  313. (char *)head->iov_base +
  314. head->iov_len - offs,
  315. copy);
  316. }
  317. }
  318. /* Now handle pages */
  319. if (pglen != 0) {
  320. if (pglen > len)
  321. _shift_data_right_pages(buf->pages,
  322. buf->page_base + len,
  323. buf->page_base,
  324. pglen - len);
  325. copy = len;
  326. if (len > pglen)
  327. copy = pglen;
  328. _copy_to_pages(buf->pages, buf->page_base,
  329. (char *)head->iov_base + head->iov_len - len,
  330. copy);
  331. }
  332. head->iov_len -= len;
  333. buf->buflen -= len;
  334. /* Have we truncated the message? */
  335. if (buf->len > buf->buflen)
  336. buf->len = buf->buflen;
  337. }
  338. /*
  339. * xdr_shrink_pagelen
  340. * @buf: xdr_buf
  341. * @len: bytes to remove from buf->pages
  342. *
  343. * Shrinks XDR buffer's page array buf->pages by
  344. * 'len' bytes. The extra data is not lost, but is instead
  345. * moved into the tail.
  346. */
  347. static void
  348. xdr_shrink_pagelen(struct xdr_buf *buf, size_t len)
  349. {
  350. struct kvec *tail;
  351. size_t copy;
  352. unsigned int pglen = buf->page_len;
  353. unsigned int tailbuf_len;
  354. tail = buf->tail;
  355. BUG_ON (len > pglen);
  356. tailbuf_len = buf->buflen - buf->head->iov_len - buf->page_len;
  357. /* Shift the tail first */
  358. if (tailbuf_len != 0) {
  359. unsigned int free_space = tailbuf_len - tail->iov_len;
  360. if (len < free_space)
  361. free_space = len;
  362. tail->iov_len += free_space;
  363. copy = len;
  364. if (tail->iov_len > len) {
  365. char *p = (char *)tail->iov_base + len;
  366. memmove(p, tail->iov_base, tail->iov_len - len);
  367. } else
  368. copy = tail->iov_len;
  369. /* Copy from the inlined pages into the tail */
  370. _copy_from_pages((char *)tail->iov_base,
  371. buf->pages, buf->page_base + pglen - len,
  372. copy);
  373. }
  374. buf->page_len -= len;
  375. buf->buflen -= len;
  376. /* Have we truncated the message? */
  377. if (buf->len > buf->buflen)
  378. buf->len = buf->buflen;
  379. }
  380. void
  381. xdr_shift_buf(struct xdr_buf *buf, size_t len)
  382. {
  383. xdr_shrink_bufhead(buf, len);
  384. }
  385. EXPORT_SYMBOL_GPL(xdr_shift_buf);
  386. /**
  387. * xdr_init_encode - Initialize a struct xdr_stream for sending data.
  388. * @xdr: pointer to xdr_stream struct
  389. * @buf: pointer to XDR buffer in which to encode data
  390. * @p: current pointer inside XDR buffer
  391. *
  392. * Note: at the moment the RPC client only passes the length of our
  393. * scratch buffer in the xdr_buf's header kvec. Previously this
  394. * meant we needed to call xdr_adjust_iovec() after encoding the
  395. * data. With the new scheme, the xdr_stream manages the details
  396. * of the buffer length, and takes care of adjusting the kvec
  397. * length for us.
  398. */
  399. void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p)
  400. {
  401. struct kvec *iov = buf->head;
  402. int scratch_len = buf->buflen - buf->page_len - buf->tail[0].iov_len;
  403. BUG_ON(scratch_len < 0);
  404. xdr->buf = buf;
  405. xdr->iov = iov;
  406. xdr->p = (__be32 *)((char *)iov->iov_base + iov->iov_len);
  407. xdr->end = (__be32 *)((char *)iov->iov_base + scratch_len);
  408. BUG_ON(iov->iov_len > scratch_len);
  409. if (p != xdr->p && p != NULL) {
  410. size_t len;
  411. BUG_ON(p < xdr->p || p > xdr->end);
  412. len = (char *)p - (char *)xdr->p;
  413. xdr->p = p;
  414. buf->len += len;
  415. iov->iov_len += len;
  416. }
  417. }
  418. EXPORT_SYMBOL_GPL(xdr_init_encode);
  419. /**
  420. * xdr_reserve_space - Reserve buffer space for sending
  421. * @xdr: pointer to xdr_stream
  422. * @nbytes: number of bytes to reserve
  423. *
  424. * Checks that we have enough buffer space to encode 'nbytes' more
  425. * bytes of data. If so, update the total xdr_buf length, and
  426. * adjust the length of the current kvec.
  427. */
  428. __be32 * xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes)
  429. {
  430. __be32 *p = xdr->p;
  431. __be32 *q;
  432. /* align nbytes on the next 32-bit boundary */
  433. nbytes += 3;
  434. nbytes &= ~3;
  435. q = p + (nbytes >> 2);
  436. if (unlikely(q > xdr->end || q < p))
  437. return NULL;
  438. xdr->p = q;
  439. xdr->iov->iov_len += nbytes;
  440. xdr->buf->len += nbytes;
  441. return p;
  442. }
  443. EXPORT_SYMBOL_GPL(xdr_reserve_space);
  444. /**
  445. * xdr_write_pages - Insert a list of pages into an XDR buffer for sending
  446. * @xdr: pointer to xdr_stream
  447. * @pages: list of pages
  448. * @base: offset of first byte
  449. * @len: length of data in bytes
  450. *
  451. */
  452. void xdr_write_pages(struct xdr_stream *xdr, struct page **pages, unsigned int base,
  453. unsigned int len)
  454. {
  455. struct xdr_buf *buf = xdr->buf;
  456. struct kvec *iov = buf->tail;
  457. buf->pages = pages;
  458. buf->page_base = base;
  459. buf->page_len = len;
  460. iov->iov_base = (char *)xdr->p;
  461. iov->iov_len = 0;
  462. xdr->iov = iov;
  463. if (len & 3) {
  464. unsigned int pad = 4 - (len & 3);
  465. BUG_ON(xdr->p >= xdr->end);
  466. iov->iov_base = (char *)xdr->p + (len & 3);
  467. iov->iov_len += pad;
  468. len += pad;
  469. *xdr->p++ = 0;
  470. }
  471. buf->buflen += len;
  472. buf->len += len;
  473. }
  474. EXPORT_SYMBOL_GPL(xdr_write_pages);
  475. /**
  476. * xdr_init_decode - Initialize an xdr_stream for decoding data.
  477. * @xdr: pointer to xdr_stream struct
  478. * @buf: pointer to XDR buffer from which to decode data
  479. * @p: current pointer inside XDR buffer
  480. */
  481. void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p)
  482. {
  483. struct kvec *iov = buf->head;
  484. unsigned int len = iov->iov_len;
  485. if (len > buf->len)
  486. len = buf->len;
  487. xdr->buf = buf;
  488. xdr->iov = iov;
  489. xdr->p = p;
  490. xdr->end = (__be32 *)((char *)iov->iov_base + len);
  491. }
  492. EXPORT_SYMBOL_GPL(xdr_init_decode);
  493. /**
  494. * xdr_inline_decode - Retrieve non-page XDR data to decode
  495. * @xdr: pointer to xdr_stream struct
  496. * @nbytes: number of bytes of data to decode
  497. *
  498. * Check if the input buffer is long enough to enable us to decode
  499. * 'nbytes' more bytes of data starting at the current position.
  500. * If so return the current pointer, then update the current
  501. * pointer position.
  502. */
  503. __be32 * xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
  504. {
  505. __be32 *p = xdr->p;
  506. __be32 *q = p + XDR_QUADLEN(nbytes);
  507. if (unlikely(q > xdr->end || q < p))
  508. return NULL;
  509. xdr->p = q;
  510. return p;
  511. }
  512. EXPORT_SYMBOL_GPL(xdr_inline_decode);
  513. /**
  514. * xdr_read_pages - Ensure page-based XDR data to decode is aligned at current pointer position
  515. * @xdr: pointer to xdr_stream struct
  516. * @len: number of bytes of page data
  517. *
  518. * Moves data beyond the current pointer position from the XDR head[] buffer
  519. * into the page list. Any data that lies beyond current position + "len"
  520. * bytes is moved into the XDR tail[].
  521. */
  522. void xdr_read_pages(struct xdr_stream *xdr, unsigned int len)
  523. {
  524. struct xdr_buf *buf = xdr->buf;
  525. struct kvec *iov;
  526. ssize_t shift;
  527. unsigned int end;
  528. int padding;
  529. /* Realign pages to current pointer position */
  530. iov = buf->head;
  531. shift = iov->iov_len + (char *)iov->iov_base - (char *)xdr->p;
  532. if (shift > 0)
  533. xdr_shrink_bufhead(buf, shift);
  534. /* Truncate page data and move it into the tail */
  535. if (buf->page_len > len)
  536. xdr_shrink_pagelen(buf, buf->page_len - len);
  537. padding = (XDR_QUADLEN(len) << 2) - len;
  538. xdr->iov = iov = buf->tail;
  539. /* Compute remaining message length. */
  540. end = iov->iov_len;
  541. shift = buf->buflen - buf->len;
  542. if (shift < end)
  543. end -= shift;
  544. else if (shift > 0)
  545. end = 0;
  546. /*
  547. * Position current pointer at beginning of tail, and
  548. * set remaining message length.
  549. */
  550. xdr->p = (__be32 *)((char *)iov->iov_base + padding);
  551. xdr->end = (__be32 *)((char *)iov->iov_base + end);
  552. }
  553. EXPORT_SYMBOL_GPL(xdr_read_pages);
  554. /**
  555. * xdr_enter_page - decode data from the XDR page
  556. * @xdr: pointer to xdr_stream struct
  557. * @len: number of bytes of page data
  558. *
  559. * Moves data beyond the current pointer position from the XDR head[] buffer
  560. * into the page list. Any data that lies beyond current position + "len"
  561. * bytes is moved into the XDR tail[]. The current pointer is then
  562. * repositioned at the beginning of the first XDR page.
  563. */
  564. void xdr_enter_page(struct xdr_stream *xdr, unsigned int len)
  565. {
  566. char * kaddr = page_address(xdr->buf->pages[0]);
  567. xdr_read_pages(xdr, len);
  568. /*
  569. * Position current pointer at beginning of tail, and
  570. * set remaining message length.
  571. */
  572. if (len > PAGE_CACHE_SIZE - xdr->buf->page_base)
  573. len = PAGE_CACHE_SIZE - xdr->buf->page_base;
  574. xdr->p = (__be32 *)(kaddr + xdr->buf->page_base);
  575. xdr->end = (__be32 *)((char *)xdr->p + len);
  576. }
  577. EXPORT_SYMBOL_GPL(xdr_enter_page);
  578. static struct kvec empty_iov = {.iov_base = NULL, .iov_len = 0};
  579. void
  580. xdr_buf_from_iov(struct kvec *iov, struct xdr_buf *buf)
  581. {
  582. buf->head[0] = *iov;
  583. buf->tail[0] = empty_iov;
  584. buf->page_len = 0;
  585. buf->buflen = buf->len = iov->iov_len;
  586. }
  587. EXPORT_SYMBOL_GPL(xdr_buf_from_iov);
  588. /* Sets subbuf to the portion of buf of length len beginning base bytes
  589. * from the start of buf. Returns -1 if base of length are out of bounds. */
  590. int
  591. xdr_buf_subsegment(struct xdr_buf *buf, struct xdr_buf *subbuf,
  592. unsigned int base, unsigned int len)
  593. {
  594. subbuf->buflen = subbuf->len = len;
  595. if (base < buf->head[0].iov_len) {
  596. subbuf->head[0].iov_base = buf->head[0].iov_base + base;
  597. subbuf->head[0].iov_len = min_t(unsigned int, len,
  598. buf->head[0].iov_len - base);
  599. len -= subbuf->head[0].iov_len;
  600. base = 0;
  601. } else {
  602. subbuf->head[0].iov_base = NULL;
  603. subbuf->head[0].iov_len = 0;
  604. base -= buf->head[0].iov_len;
  605. }
  606. if (base < buf->page_len) {
  607. subbuf->page_len = min(buf->page_len - base, len);
  608. base += buf->page_base;
  609. subbuf->page_base = base & ~PAGE_CACHE_MASK;
  610. subbuf->pages = &buf->pages[base >> PAGE_CACHE_SHIFT];
  611. len -= subbuf->page_len;
  612. base = 0;
  613. } else {
  614. base -= buf->page_len;
  615. subbuf->page_len = 0;
  616. }
  617. if (base < buf->tail[0].iov_len) {
  618. subbuf->tail[0].iov_base = buf->tail[0].iov_base + base;
  619. subbuf->tail[0].iov_len = min_t(unsigned int, len,
  620. buf->tail[0].iov_len - base);
  621. len -= subbuf->tail[0].iov_len;
  622. base = 0;
  623. } else {
  624. subbuf->tail[0].iov_base = NULL;
  625. subbuf->tail[0].iov_len = 0;
  626. base -= buf->tail[0].iov_len;
  627. }
  628. if (base || len)
  629. return -1;
  630. return 0;
  631. }
  632. EXPORT_SYMBOL_GPL(xdr_buf_subsegment);
  633. static void __read_bytes_from_xdr_buf(struct xdr_buf *subbuf, void *obj, unsigned int len)
  634. {
  635. unsigned int this_len;
  636. this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
  637. memcpy(obj, subbuf->head[0].iov_base, this_len);
  638. len -= this_len;
  639. obj += this_len;
  640. this_len = min_t(unsigned int, len, subbuf->page_len);
  641. if (this_len)
  642. _copy_from_pages(obj, subbuf->pages, subbuf->page_base, this_len);
  643. len -= this_len;
  644. obj += this_len;
  645. this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
  646. memcpy(obj, subbuf->tail[0].iov_base, this_len);
  647. }
  648. /* obj is assumed to point to allocated memory of size at least len: */
  649. int read_bytes_from_xdr_buf(struct xdr_buf *buf, unsigned int base, void *obj, unsigned int len)
  650. {
  651. struct xdr_buf subbuf;
  652. int status;
  653. status = xdr_buf_subsegment(buf, &subbuf, base, len);
  654. if (status != 0)
  655. return status;
  656. __read_bytes_from_xdr_buf(&subbuf, obj, len);
  657. return 0;
  658. }
  659. EXPORT_SYMBOL_GPL(read_bytes_from_xdr_buf);
  660. static void __write_bytes_to_xdr_buf(struct xdr_buf *subbuf, void *obj, unsigned int len)
  661. {
  662. unsigned int this_len;
  663. this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
  664. memcpy(subbuf->head[0].iov_base, obj, this_len);
  665. len -= this_len;
  666. obj += this_len;
  667. this_len = min_t(unsigned int, len, subbuf->page_len);
  668. if (this_len)
  669. _copy_to_pages(subbuf->pages, subbuf->page_base, obj, this_len);
  670. len -= this_len;
  671. obj += this_len;
  672. this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
  673. memcpy(subbuf->tail[0].iov_base, obj, this_len);
  674. }
  675. /* obj is assumed to point to allocated memory of size at least len: */
  676. int write_bytes_to_xdr_buf(struct xdr_buf *buf, unsigned int base, void *obj, unsigned int len)
  677. {
  678. struct xdr_buf subbuf;
  679. int status;
  680. status = xdr_buf_subsegment(buf, &subbuf, base, len);
  681. if (status != 0)
  682. return status;
  683. __write_bytes_to_xdr_buf(&subbuf, obj, len);
  684. return 0;
  685. }
  686. EXPORT_SYMBOL_GPL(write_bytes_to_xdr_buf);
  687. int
  688. xdr_decode_word(struct xdr_buf *buf, unsigned int base, u32 *obj)
  689. {
  690. __be32 raw;
  691. int status;
  692. status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
  693. if (status)
  694. return status;
  695. *obj = be32_to_cpu(raw);
  696. return 0;
  697. }
  698. EXPORT_SYMBOL_GPL(xdr_decode_word);
  699. int
  700. xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj)
  701. {
  702. __be32 raw = cpu_to_be32(obj);
  703. return write_bytes_to_xdr_buf(buf, base, &raw, sizeof(obj));
  704. }
  705. EXPORT_SYMBOL_GPL(xdr_encode_word);
  706. /* If the netobj starting offset bytes from the start of xdr_buf is contained
  707. * entirely in the head or the tail, set object to point to it; otherwise
  708. * try to find space for it at the end of the tail, copy it there, and
  709. * set obj to point to it. */
  710. int xdr_buf_read_netobj(struct xdr_buf *buf, struct xdr_netobj *obj, unsigned int offset)
  711. {
  712. struct xdr_buf subbuf;
  713. if (xdr_decode_word(buf, offset, &obj->len))
  714. return -EFAULT;
  715. if (xdr_buf_subsegment(buf, &subbuf, offset + 4, obj->len))
  716. return -EFAULT;
  717. /* Is the obj contained entirely in the head? */
  718. obj->data = subbuf.head[0].iov_base;
  719. if (subbuf.head[0].iov_len == obj->len)
  720. return 0;
  721. /* ..or is the obj contained entirely in the tail? */
  722. obj->data = subbuf.tail[0].iov_base;
  723. if (subbuf.tail[0].iov_len == obj->len)
  724. return 0;
  725. /* use end of tail as storage for obj:
  726. * (We don't copy to the beginning because then we'd have
  727. * to worry about doing a potentially overlapping copy.
  728. * This assumes the object is at most half the length of the
  729. * tail.) */
  730. if (obj->len > buf->buflen - buf->len)
  731. return -ENOMEM;
  732. if (buf->tail[0].iov_len != 0)
  733. obj->data = buf->tail[0].iov_base + buf->tail[0].iov_len;
  734. else
  735. obj->data = buf->head[0].iov_base + buf->head[0].iov_len;
  736. __read_bytes_from_xdr_buf(&subbuf, obj->data, obj->len);
  737. return 0;
  738. }
  739. EXPORT_SYMBOL_GPL(xdr_buf_read_netobj);
  740. /* Returns 0 on success, or else a negative error code. */
  741. static int
  742. xdr_xcode_array2(struct xdr_buf *buf, unsigned int base,
  743. struct xdr_array2_desc *desc, int encode)
  744. {
  745. char *elem = NULL, *c;
  746. unsigned int copied = 0, todo, avail_here;
  747. struct page **ppages = NULL;
  748. int err;
  749. if (encode) {
  750. if (xdr_encode_word(buf, base, desc->array_len) != 0)
  751. return -EINVAL;
  752. } else {
  753. if (xdr_decode_word(buf, base, &desc->array_len) != 0 ||
  754. desc->array_len > desc->array_maxlen ||
  755. (unsigned long) base + 4 + desc->array_len *
  756. desc->elem_size > buf->len)
  757. return -EINVAL;
  758. }
  759. base += 4;
  760. if (!desc->xcode)
  761. return 0;
  762. todo = desc->array_len * desc->elem_size;
  763. /* process head */
  764. if (todo && base < buf->head->iov_len) {
  765. c = buf->head->iov_base + base;
  766. avail_here = min_t(unsigned int, todo,
  767. buf->head->iov_len - base);
  768. todo -= avail_here;
  769. while (avail_here >= desc->elem_size) {
  770. err = desc->xcode(desc, c);
  771. if (err)
  772. goto out;
  773. c += desc->elem_size;
  774. avail_here -= desc->elem_size;
  775. }
  776. if (avail_here) {
  777. if (!elem) {
  778. elem = kmalloc(desc->elem_size, GFP_KERNEL);
  779. err = -ENOMEM;
  780. if (!elem)
  781. goto out;
  782. }
  783. if (encode) {
  784. err = desc->xcode(desc, elem);
  785. if (err)
  786. goto out;
  787. memcpy(c, elem, avail_here);
  788. } else
  789. memcpy(elem, c, avail_here);
  790. copied = avail_here;
  791. }
  792. base = buf->head->iov_len; /* align to start of pages */
  793. }
  794. /* process pages array */
  795. base -= buf->head->iov_len;
  796. if (todo && base < buf->page_len) {
  797. unsigned int avail_page;
  798. avail_here = min(todo, buf->page_len - base);
  799. todo -= avail_here;
  800. base += buf->page_base;
  801. ppages = buf->pages + (base >> PAGE_CACHE_SHIFT);
  802. base &= ~PAGE_CACHE_MASK;
  803. avail_page = min_t(unsigned int, PAGE_CACHE_SIZE - base,
  804. avail_here);
  805. c = kmap(*ppages) + base;
  806. while (avail_here) {
  807. avail_here -= avail_page;
  808. if (copied || avail_page < desc->elem_size) {
  809. unsigned int l = min(avail_page,
  810. desc->elem_size - copied);
  811. if (!elem) {
  812. elem = kmalloc(desc->elem_size,
  813. GFP_KERNEL);
  814. err = -ENOMEM;
  815. if (!elem)
  816. goto out;
  817. }
  818. if (encode) {
  819. if (!copied) {
  820. err = desc->xcode(desc, elem);
  821. if (err)
  822. goto out;
  823. }
  824. memcpy(c, elem + copied, l);
  825. copied += l;
  826. if (copied == desc->elem_size)
  827. copied = 0;
  828. } else {
  829. memcpy(elem + copied, c, l);
  830. copied += l;
  831. if (copied == desc->elem_size) {
  832. err = desc->xcode(desc, elem);
  833. if (err)
  834. goto out;
  835. copied = 0;
  836. }
  837. }
  838. avail_page -= l;
  839. c += l;
  840. }
  841. while (avail_page >= desc->elem_size) {
  842. err = desc->xcode(desc, c);
  843. if (err)
  844. goto out;
  845. c += desc->elem_size;
  846. avail_page -= desc->elem_size;
  847. }
  848. if (avail_page) {
  849. unsigned int l = min(avail_page,
  850. desc->elem_size - copied);
  851. if (!elem) {
  852. elem = kmalloc(desc->elem_size,
  853. GFP_KERNEL);
  854. err = -ENOMEM;
  855. if (!elem)
  856. goto out;
  857. }
  858. if (encode) {
  859. if (!copied) {
  860. err = desc->xcode(desc, elem);
  861. if (err)
  862. goto out;
  863. }
  864. memcpy(c, elem + copied, l);
  865. copied += l;
  866. if (copied == desc->elem_size)
  867. copied = 0;
  868. } else {
  869. memcpy(elem + copied, c, l);
  870. copied += l;
  871. if (copied == desc->elem_size) {
  872. err = desc->xcode(desc, elem);
  873. if (err)
  874. goto out;
  875. copied = 0;
  876. }
  877. }
  878. }
  879. if (avail_here) {
  880. kunmap(*ppages);
  881. ppages++;
  882. c = kmap(*ppages);
  883. }
  884. avail_page = min(avail_here,
  885. (unsigned int) PAGE_CACHE_SIZE);
  886. }
  887. base = buf->page_len; /* align to start of tail */
  888. }
  889. /* process tail */
  890. base -= buf->page_len;
  891. if (todo) {
  892. c = buf->tail->iov_base + base;
  893. if (copied) {
  894. unsigned int l = desc->elem_size - copied;
  895. if (encode)
  896. memcpy(c, elem + copied, l);
  897. else {
  898. memcpy(elem + copied, c, l);
  899. err = desc->xcode(desc, elem);
  900. if (err)
  901. goto out;
  902. }
  903. todo -= l;
  904. c += l;
  905. }
  906. while (todo) {
  907. err = desc->xcode(desc, c);
  908. if (err)
  909. goto out;
  910. c += desc->elem_size;
  911. todo -= desc->elem_size;
  912. }
  913. }
  914. err = 0;
  915. out:
  916. kfree(elem);
  917. if (ppages)
  918. kunmap(*ppages);
  919. return err;
  920. }
  921. int
  922. xdr_decode_array2(struct xdr_buf *buf, unsigned int base,
  923. struct xdr_array2_desc *desc)
  924. {
  925. if (base >= buf->len)
  926. return -EINVAL;
  927. return xdr_xcode_array2(buf, base, desc, 0);
  928. }
  929. EXPORT_SYMBOL_GPL(xdr_decode_array2);
  930. int
  931. xdr_encode_array2(struct xdr_buf *buf, unsigned int base,
  932. struct xdr_array2_desc *desc)
  933. {
  934. if ((unsigned long) base + 4 + desc->array_len * desc->elem_size >
  935. buf->head->iov_len + buf->page_len + buf->tail->iov_len)
  936. return -EINVAL;
  937. return xdr_xcode_array2(buf, base, desc, 1);
  938. }
  939. EXPORT_SYMBOL_GPL(xdr_encode_array2);
  940. int
  941. xdr_process_buf(struct xdr_buf *buf, unsigned int offset, unsigned int len,
  942. int (*actor)(struct scatterlist *, void *), void *data)
  943. {
  944. int i, ret = 0;
  945. unsigned page_len, thislen, page_offset;
  946. struct scatterlist sg[1];
  947. sg_init_table(sg, 1);
  948. if (offset >= buf->head[0].iov_len) {
  949. offset -= buf->head[0].iov_len;
  950. } else {
  951. thislen = buf->head[0].iov_len - offset;
  952. if (thislen > len)
  953. thislen = len;
  954. sg_set_buf(sg, buf->head[0].iov_base + offset, thislen);
  955. ret = actor(sg, data);
  956. if (ret)
  957. goto out;
  958. offset = 0;
  959. len -= thislen;
  960. }
  961. if (len == 0)
  962. goto out;
  963. if (offset >= buf->page_len) {
  964. offset -= buf->page_len;
  965. } else {
  966. page_len = buf->page_len - offset;
  967. if (page_len > len)
  968. page_len = len;
  969. len -= page_len;
  970. page_offset = (offset + buf->page_base) & (PAGE_CACHE_SIZE - 1);
  971. i = (offset + buf->page_base) >> PAGE_CACHE_SHIFT;
  972. thislen = PAGE_CACHE_SIZE - page_offset;
  973. do {
  974. if (thislen > page_len)
  975. thislen = page_len;
  976. sg_set_page(sg, buf->pages[i], thislen, page_offset);
  977. ret = actor(sg, data);
  978. if (ret)
  979. goto out;
  980. page_len -= thislen;
  981. i++;
  982. page_offset = 0;
  983. thislen = PAGE_CACHE_SIZE;
  984. } while (page_len != 0);
  985. offset = 0;
  986. }
  987. if (len == 0)
  988. goto out;
  989. if (offset < buf->tail[0].iov_len) {
  990. thislen = buf->tail[0].iov_len - offset;
  991. if (thislen > len)
  992. thislen = len;
  993. sg_set_buf(sg, buf->tail[0].iov_base + offset, thislen);
  994. ret = actor(sg, data);
  995. len -= thislen;
  996. }
  997. if (len != 0)
  998. ret = -EINVAL;
  999. out:
  1000. return ret;
  1001. }
  1002. EXPORT_SYMBOL_GPL(xdr_process_buf);