xdr.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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. char *p;
  353. unsigned int pglen = buf->page_len;
  354. tail = buf->tail;
  355. BUG_ON (len > pglen);
  356. /* Shift the tail first */
  357. if (tail->iov_len != 0) {
  358. p = (char *)tail->iov_base + len;
  359. if (tail->iov_len > len) {
  360. copy = tail->iov_len - len;
  361. memmove(p, tail->iov_base, copy);
  362. } else
  363. buf->buflen -= len;
  364. /* Copy from the inlined pages into the tail */
  365. copy = len;
  366. if (copy > tail->iov_len)
  367. copy = tail->iov_len;
  368. _copy_from_pages((char *)tail->iov_base,
  369. buf->pages, buf->page_base + pglen - len,
  370. copy);
  371. }
  372. buf->page_len -= len;
  373. buf->buflen -= len;
  374. /* Have we truncated the message? */
  375. if (buf->len > buf->buflen)
  376. buf->len = buf->buflen;
  377. }
  378. void
  379. xdr_shift_buf(struct xdr_buf *buf, size_t len)
  380. {
  381. xdr_shrink_bufhead(buf, len);
  382. }
  383. EXPORT_SYMBOL_GPL(xdr_shift_buf);
  384. /**
  385. * xdr_init_encode - Initialize a struct xdr_stream for sending data.
  386. * @xdr: pointer to xdr_stream struct
  387. * @buf: pointer to XDR buffer in which to encode data
  388. * @p: current pointer inside XDR buffer
  389. *
  390. * Note: at the moment the RPC client only passes the length of our
  391. * scratch buffer in the xdr_buf's header kvec. Previously this
  392. * meant we needed to call xdr_adjust_iovec() after encoding the
  393. * data. With the new scheme, the xdr_stream manages the details
  394. * of the buffer length, and takes care of adjusting the kvec
  395. * length for us.
  396. */
  397. void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p)
  398. {
  399. struct kvec *iov = buf->head;
  400. int scratch_len = buf->buflen - buf->page_len - buf->tail[0].iov_len;
  401. BUG_ON(scratch_len < 0);
  402. xdr->buf = buf;
  403. xdr->iov = iov;
  404. xdr->p = (__be32 *)((char *)iov->iov_base + iov->iov_len);
  405. xdr->end = (__be32 *)((char *)iov->iov_base + scratch_len);
  406. BUG_ON(iov->iov_len > scratch_len);
  407. if (p != xdr->p && p != NULL) {
  408. size_t len;
  409. BUG_ON(p < xdr->p || p > xdr->end);
  410. len = (char *)p - (char *)xdr->p;
  411. xdr->p = p;
  412. buf->len += len;
  413. iov->iov_len += len;
  414. }
  415. }
  416. EXPORT_SYMBOL_GPL(xdr_init_encode);
  417. /**
  418. * xdr_reserve_space - Reserve buffer space for sending
  419. * @xdr: pointer to xdr_stream
  420. * @nbytes: number of bytes to reserve
  421. *
  422. * Checks that we have enough buffer space to encode 'nbytes' more
  423. * bytes of data. If so, update the total xdr_buf length, and
  424. * adjust the length of the current kvec.
  425. */
  426. __be32 * xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes)
  427. {
  428. __be32 *p = xdr->p;
  429. __be32 *q;
  430. /* align nbytes on the next 32-bit boundary */
  431. nbytes += 3;
  432. nbytes &= ~3;
  433. q = p + (nbytes >> 2);
  434. if (unlikely(q > xdr->end || q < p))
  435. return NULL;
  436. xdr->p = q;
  437. xdr->iov->iov_len += nbytes;
  438. xdr->buf->len += nbytes;
  439. return p;
  440. }
  441. EXPORT_SYMBOL_GPL(xdr_reserve_space);
  442. /**
  443. * xdr_write_pages - Insert a list of pages into an XDR buffer for sending
  444. * @xdr: pointer to xdr_stream
  445. * @pages: list of pages
  446. * @base: offset of first byte
  447. * @len: length of data in bytes
  448. *
  449. */
  450. void xdr_write_pages(struct xdr_stream *xdr, struct page **pages, unsigned int base,
  451. unsigned int len)
  452. {
  453. struct xdr_buf *buf = xdr->buf;
  454. struct kvec *iov = buf->tail;
  455. buf->pages = pages;
  456. buf->page_base = base;
  457. buf->page_len = len;
  458. iov->iov_base = (char *)xdr->p;
  459. iov->iov_len = 0;
  460. xdr->iov = iov;
  461. if (len & 3) {
  462. unsigned int pad = 4 - (len & 3);
  463. BUG_ON(xdr->p >= xdr->end);
  464. iov->iov_base = (char *)xdr->p + (len & 3);
  465. iov->iov_len += pad;
  466. len += pad;
  467. *xdr->p++ = 0;
  468. }
  469. buf->buflen += len;
  470. buf->len += len;
  471. }
  472. EXPORT_SYMBOL_GPL(xdr_write_pages);
  473. /**
  474. * xdr_init_decode - Initialize an xdr_stream for decoding data.
  475. * @xdr: pointer to xdr_stream struct
  476. * @buf: pointer to XDR buffer from which to decode data
  477. * @p: current pointer inside XDR buffer
  478. */
  479. void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p)
  480. {
  481. struct kvec *iov = buf->head;
  482. unsigned int len = iov->iov_len;
  483. if (len > buf->len)
  484. len = buf->len;
  485. xdr->buf = buf;
  486. xdr->iov = iov;
  487. xdr->p = p;
  488. xdr->end = (__be32 *)((char *)iov->iov_base + len);
  489. }
  490. EXPORT_SYMBOL_GPL(xdr_init_decode);
  491. /**
  492. * xdr_inline_decode - Retrieve non-page XDR data to decode
  493. * @xdr: pointer to xdr_stream struct
  494. * @nbytes: number of bytes of data to decode
  495. *
  496. * Check if the input buffer is long enough to enable us to decode
  497. * 'nbytes' more bytes of data starting at the current position.
  498. * If so return the current pointer, then update the current
  499. * pointer position.
  500. */
  501. __be32 * xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
  502. {
  503. __be32 *p = xdr->p;
  504. __be32 *q = p + XDR_QUADLEN(nbytes);
  505. if (unlikely(q > xdr->end || q < p))
  506. return NULL;
  507. xdr->p = q;
  508. return p;
  509. }
  510. EXPORT_SYMBOL_GPL(xdr_inline_decode);
  511. /**
  512. * xdr_read_pages - Ensure page-based XDR data to decode is aligned at current pointer position
  513. * @xdr: pointer to xdr_stream struct
  514. * @len: number of bytes of page data
  515. *
  516. * Moves data beyond the current pointer position from the XDR head[] buffer
  517. * into the page list. Any data that lies beyond current position + "len"
  518. * bytes is moved into the XDR tail[].
  519. */
  520. void xdr_read_pages(struct xdr_stream *xdr, unsigned int len)
  521. {
  522. struct xdr_buf *buf = xdr->buf;
  523. struct kvec *iov;
  524. ssize_t shift;
  525. unsigned int end;
  526. int padding;
  527. /* Realign pages to current pointer position */
  528. iov = buf->head;
  529. shift = iov->iov_len + (char *)iov->iov_base - (char *)xdr->p;
  530. if (shift > 0)
  531. xdr_shrink_bufhead(buf, shift);
  532. /* Truncate page data and move it into the tail */
  533. if (buf->page_len > len)
  534. xdr_shrink_pagelen(buf, buf->page_len - len);
  535. padding = (XDR_QUADLEN(len) << 2) - len;
  536. xdr->iov = iov = buf->tail;
  537. /* Compute remaining message length. */
  538. end = iov->iov_len;
  539. shift = buf->buflen - buf->len;
  540. if (shift < end)
  541. end -= shift;
  542. else if (shift > 0)
  543. end = 0;
  544. /*
  545. * Position current pointer at beginning of tail, and
  546. * set remaining message length.
  547. */
  548. xdr->p = (__be32 *)((char *)iov->iov_base + padding);
  549. xdr->end = (__be32 *)((char *)iov->iov_base + end);
  550. }
  551. EXPORT_SYMBOL_GPL(xdr_read_pages);
  552. /**
  553. * xdr_enter_page - decode data from the XDR page
  554. * @xdr: pointer to xdr_stream struct
  555. * @len: number of bytes of page data
  556. *
  557. * Moves data beyond the current pointer position from the XDR head[] buffer
  558. * into the page list. Any data that lies beyond current position + "len"
  559. * bytes is moved into the XDR tail[]. The current pointer is then
  560. * repositioned at the beginning of the first XDR page.
  561. */
  562. void xdr_enter_page(struct xdr_stream *xdr, unsigned int len)
  563. {
  564. char * kaddr = page_address(xdr->buf->pages[0]);
  565. xdr_read_pages(xdr, len);
  566. /*
  567. * Position current pointer at beginning of tail, and
  568. * set remaining message length.
  569. */
  570. if (len > PAGE_CACHE_SIZE - xdr->buf->page_base)
  571. len = PAGE_CACHE_SIZE - xdr->buf->page_base;
  572. xdr->p = (__be32 *)(kaddr + xdr->buf->page_base);
  573. xdr->end = (__be32 *)((char *)xdr->p + len);
  574. }
  575. EXPORT_SYMBOL_GPL(xdr_enter_page);
  576. static struct kvec empty_iov = {.iov_base = NULL, .iov_len = 0};
  577. void
  578. xdr_buf_from_iov(struct kvec *iov, struct xdr_buf *buf)
  579. {
  580. buf->head[0] = *iov;
  581. buf->tail[0] = empty_iov;
  582. buf->page_len = 0;
  583. buf->buflen = buf->len = iov->iov_len;
  584. }
  585. EXPORT_SYMBOL_GPL(xdr_buf_from_iov);
  586. /* Sets subbuf to the portion of buf of length len beginning base bytes
  587. * from the start of buf. Returns -1 if base of length are out of bounds. */
  588. int
  589. xdr_buf_subsegment(struct xdr_buf *buf, struct xdr_buf *subbuf,
  590. unsigned int base, unsigned int len)
  591. {
  592. subbuf->buflen = subbuf->len = len;
  593. if (base < buf->head[0].iov_len) {
  594. subbuf->head[0].iov_base = buf->head[0].iov_base + base;
  595. subbuf->head[0].iov_len = min_t(unsigned int, len,
  596. buf->head[0].iov_len - base);
  597. len -= subbuf->head[0].iov_len;
  598. base = 0;
  599. } else {
  600. subbuf->head[0].iov_base = NULL;
  601. subbuf->head[0].iov_len = 0;
  602. base -= buf->head[0].iov_len;
  603. }
  604. if (base < buf->page_len) {
  605. subbuf->page_len = min(buf->page_len - base, len);
  606. base += buf->page_base;
  607. subbuf->page_base = base & ~PAGE_CACHE_MASK;
  608. subbuf->pages = &buf->pages[base >> PAGE_CACHE_SHIFT];
  609. len -= subbuf->page_len;
  610. base = 0;
  611. } else {
  612. base -= buf->page_len;
  613. subbuf->page_len = 0;
  614. }
  615. if (base < buf->tail[0].iov_len) {
  616. subbuf->tail[0].iov_base = buf->tail[0].iov_base + base;
  617. subbuf->tail[0].iov_len = min_t(unsigned int, len,
  618. buf->tail[0].iov_len - base);
  619. len -= subbuf->tail[0].iov_len;
  620. base = 0;
  621. } else {
  622. subbuf->tail[0].iov_base = NULL;
  623. subbuf->tail[0].iov_len = 0;
  624. base -= buf->tail[0].iov_len;
  625. }
  626. if (base || len)
  627. return -1;
  628. return 0;
  629. }
  630. EXPORT_SYMBOL_GPL(xdr_buf_subsegment);
  631. static void __read_bytes_from_xdr_buf(struct xdr_buf *subbuf, void *obj, unsigned int len)
  632. {
  633. unsigned int this_len;
  634. this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
  635. memcpy(obj, subbuf->head[0].iov_base, this_len);
  636. len -= this_len;
  637. obj += this_len;
  638. this_len = min_t(unsigned int, len, subbuf->page_len);
  639. if (this_len)
  640. _copy_from_pages(obj, subbuf->pages, subbuf->page_base, this_len);
  641. len -= this_len;
  642. obj += this_len;
  643. this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
  644. memcpy(obj, subbuf->tail[0].iov_base, this_len);
  645. }
  646. /* obj is assumed to point to allocated memory of size at least len: */
  647. int read_bytes_from_xdr_buf(struct xdr_buf *buf, unsigned int base, void *obj, unsigned int len)
  648. {
  649. struct xdr_buf subbuf;
  650. int status;
  651. status = xdr_buf_subsegment(buf, &subbuf, base, len);
  652. if (status != 0)
  653. return status;
  654. __read_bytes_from_xdr_buf(&subbuf, obj, len);
  655. return 0;
  656. }
  657. EXPORT_SYMBOL_GPL(read_bytes_from_xdr_buf);
  658. static void __write_bytes_to_xdr_buf(struct xdr_buf *subbuf, void *obj, unsigned int len)
  659. {
  660. unsigned int this_len;
  661. this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
  662. memcpy(subbuf->head[0].iov_base, obj, this_len);
  663. len -= this_len;
  664. obj += this_len;
  665. this_len = min_t(unsigned int, len, subbuf->page_len);
  666. if (this_len)
  667. _copy_to_pages(subbuf->pages, subbuf->page_base, obj, this_len);
  668. len -= this_len;
  669. obj += this_len;
  670. this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
  671. memcpy(subbuf->tail[0].iov_base, obj, this_len);
  672. }
  673. /* obj is assumed to point to allocated memory of size at least len: */
  674. int write_bytes_to_xdr_buf(struct xdr_buf *buf, unsigned int base, void *obj, unsigned int len)
  675. {
  676. struct xdr_buf subbuf;
  677. int status;
  678. status = xdr_buf_subsegment(buf, &subbuf, base, len);
  679. if (status != 0)
  680. return status;
  681. __write_bytes_to_xdr_buf(&subbuf, obj, len);
  682. return 0;
  683. }
  684. int
  685. xdr_decode_word(struct xdr_buf *buf, unsigned int base, u32 *obj)
  686. {
  687. __be32 raw;
  688. int status;
  689. status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
  690. if (status)
  691. return status;
  692. *obj = be32_to_cpu(raw);
  693. return 0;
  694. }
  695. EXPORT_SYMBOL_GPL(xdr_decode_word);
  696. int
  697. xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj)
  698. {
  699. __be32 raw = cpu_to_be32(obj);
  700. return write_bytes_to_xdr_buf(buf, base, &raw, sizeof(obj));
  701. }
  702. EXPORT_SYMBOL_GPL(xdr_encode_word);
  703. /* If the netobj starting offset bytes from the start of xdr_buf is contained
  704. * entirely in the head or the tail, set object to point to it; otherwise
  705. * try to find space for it at the end of the tail, copy it there, and
  706. * set obj to point to it. */
  707. int xdr_buf_read_netobj(struct xdr_buf *buf, struct xdr_netobj *obj, unsigned int offset)
  708. {
  709. struct xdr_buf subbuf;
  710. if (xdr_decode_word(buf, offset, &obj->len))
  711. return -EFAULT;
  712. if (xdr_buf_subsegment(buf, &subbuf, offset + 4, obj->len))
  713. return -EFAULT;
  714. /* Is the obj contained entirely in the head? */
  715. obj->data = subbuf.head[0].iov_base;
  716. if (subbuf.head[0].iov_len == obj->len)
  717. return 0;
  718. /* ..or is the obj contained entirely in the tail? */
  719. obj->data = subbuf.tail[0].iov_base;
  720. if (subbuf.tail[0].iov_len == obj->len)
  721. return 0;
  722. /* use end of tail as storage for obj:
  723. * (We don't copy to the beginning because then we'd have
  724. * to worry about doing a potentially overlapping copy.
  725. * This assumes the object is at most half the length of the
  726. * tail.) */
  727. if (obj->len > buf->buflen - buf->len)
  728. return -ENOMEM;
  729. if (buf->tail[0].iov_len != 0)
  730. obj->data = buf->tail[0].iov_base + buf->tail[0].iov_len;
  731. else
  732. obj->data = buf->head[0].iov_base + buf->head[0].iov_len;
  733. __read_bytes_from_xdr_buf(&subbuf, obj->data, obj->len);
  734. return 0;
  735. }
  736. EXPORT_SYMBOL_GPL(xdr_buf_read_netobj);
  737. /* Returns 0 on success, or else a negative error code. */
  738. static int
  739. xdr_xcode_array2(struct xdr_buf *buf, unsigned int base,
  740. struct xdr_array2_desc *desc, int encode)
  741. {
  742. char *elem = NULL, *c;
  743. unsigned int copied = 0, todo, avail_here;
  744. struct page **ppages = NULL;
  745. int err;
  746. if (encode) {
  747. if (xdr_encode_word(buf, base, desc->array_len) != 0)
  748. return -EINVAL;
  749. } else {
  750. if (xdr_decode_word(buf, base, &desc->array_len) != 0 ||
  751. desc->array_len > desc->array_maxlen ||
  752. (unsigned long) base + 4 + desc->array_len *
  753. desc->elem_size > buf->len)
  754. return -EINVAL;
  755. }
  756. base += 4;
  757. if (!desc->xcode)
  758. return 0;
  759. todo = desc->array_len * desc->elem_size;
  760. /* process head */
  761. if (todo && base < buf->head->iov_len) {
  762. c = buf->head->iov_base + base;
  763. avail_here = min_t(unsigned int, todo,
  764. buf->head->iov_len - base);
  765. todo -= avail_here;
  766. while (avail_here >= desc->elem_size) {
  767. err = desc->xcode(desc, c);
  768. if (err)
  769. goto out;
  770. c += desc->elem_size;
  771. avail_here -= desc->elem_size;
  772. }
  773. if (avail_here) {
  774. if (!elem) {
  775. elem = kmalloc(desc->elem_size, GFP_KERNEL);
  776. err = -ENOMEM;
  777. if (!elem)
  778. goto out;
  779. }
  780. if (encode) {
  781. err = desc->xcode(desc, elem);
  782. if (err)
  783. goto out;
  784. memcpy(c, elem, avail_here);
  785. } else
  786. memcpy(elem, c, avail_here);
  787. copied = avail_here;
  788. }
  789. base = buf->head->iov_len; /* align to start of pages */
  790. }
  791. /* process pages array */
  792. base -= buf->head->iov_len;
  793. if (todo && base < buf->page_len) {
  794. unsigned int avail_page;
  795. avail_here = min(todo, buf->page_len - base);
  796. todo -= avail_here;
  797. base += buf->page_base;
  798. ppages = buf->pages + (base >> PAGE_CACHE_SHIFT);
  799. base &= ~PAGE_CACHE_MASK;
  800. avail_page = min_t(unsigned int, PAGE_CACHE_SIZE - base,
  801. avail_here);
  802. c = kmap(*ppages) + base;
  803. while (avail_here) {
  804. avail_here -= avail_page;
  805. if (copied || avail_page < desc->elem_size) {
  806. unsigned int l = min(avail_page,
  807. desc->elem_size - copied);
  808. if (!elem) {
  809. elem = kmalloc(desc->elem_size,
  810. GFP_KERNEL);
  811. err = -ENOMEM;
  812. if (!elem)
  813. goto out;
  814. }
  815. if (encode) {
  816. if (!copied) {
  817. err = desc->xcode(desc, elem);
  818. if (err)
  819. goto out;
  820. }
  821. memcpy(c, elem + copied, l);
  822. copied += l;
  823. if (copied == desc->elem_size)
  824. copied = 0;
  825. } else {
  826. memcpy(elem + copied, c, l);
  827. copied += l;
  828. if (copied == desc->elem_size) {
  829. err = desc->xcode(desc, elem);
  830. if (err)
  831. goto out;
  832. copied = 0;
  833. }
  834. }
  835. avail_page -= l;
  836. c += l;
  837. }
  838. while (avail_page >= desc->elem_size) {
  839. err = desc->xcode(desc, c);
  840. if (err)
  841. goto out;
  842. c += desc->elem_size;
  843. avail_page -= desc->elem_size;
  844. }
  845. if (avail_page) {
  846. unsigned int l = min(avail_page,
  847. desc->elem_size - copied);
  848. if (!elem) {
  849. elem = kmalloc(desc->elem_size,
  850. GFP_KERNEL);
  851. err = -ENOMEM;
  852. if (!elem)
  853. goto out;
  854. }
  855. if (encode) {
  856. if (!copied) {
  857. err = desc->xcode(desc, elem);
  858. if (err)
  859. goto out;
  860. }
  861. memcpy(c, elem + copied, l);
  862. copied += l;
  863. if (copied == desc->elem_size)
  864. copied = 0;
  865. } else {
  866. memcpy(elem + copied, c, l);
  867. copied += l;
  868. if (copied == desc->elem_size) {
  869. err = desc->xcode(desc, elem);
  870. if (err)
  871. goto out;
  872. copied = 0;
  873. }
  874. }
  875. }
  876. if (avail_here) {
  877. kunmap(*ppages);
  878. ppages++;
  879. c = kmap(*ppages);
  880. }
  881. avail_page = min(avail_here,
  882. (unsigned int) PAGE_CACHE_SIZE);
  883. }
  884. base = buf->page_len; /* align to start of tail */
  885. }
  886. /* process tail */
  887. base -= buf->page_len;
  888. if (todo) {
  889. c = buf->tail->iov_base + base;
  890. if (copied) {
  891. unsigned int l = desc->elem_size - copied;
  892. if (encode)
  893. memcpy(c, elem + copied, l);
  894. else {
  895. memcpy(elem + copied, c, l);
  896. err = desc->xcode(desc, elem);
  897. if (err)
  898. goto out;
  899. }
  900. todo -= l;
  901. c += l;
  902. }
  903. while (todo) {
  904. err = desc->xcode(desc, c);
  905. if (err)
  906. goto out;
  907. c += desc->elem_size;
  908. todo -= desc->elem_size;
  909. }
  910. }
  911. err = 0;
  912. out:
  913. kfree(elem);
  914. if (ppages)
  915. kunmap(*ppages);
  916. return err;
  917. }
  918. int
  919. xdr_decode_array2(struct xdr_buf *buf, unsigned int base,
  920. struct xdr_array2_desc *desc)
  921. {
  922. if (base >= buf->len)
  923. return -EINVAL;
  924. return xdr_xcode_array2(buf, base, desc, 0);
  925. }
  926. EXPORT_SYMBOL_GPL(xdr_decode_array2);
  927. int
  928. xdr_encode_array2(struct xdr_buf *buf, unsigned int base,
  929. struct xdr_array2_desc *desc)
  930. {
  931. if ((unsigned long) base + 4 + desc->array_len * desc->elem_size >
  932. buf->head->iov_len + buf->page_len + buf->tail->iov_len)
  933. return -EINVAL;
  934. return xdr_xcode_array2(buf, base, desc, 1);
  935. }
  936. EXPORT_SYMBOL_GPL(xdr_encode_array2);
  937. int
  938. xdr_process_buf(struct xdr_buf *buf, unsigned int offset, unsigned int len,
  939. int (*actor)(struct scatterlist *, void *), void *data)
  940. {
  941. int i, ret = 0;
  942. unsigned page_len, thislen, page_offset;
  943. struct scatterlist sg[1];
  944. sg_init_table(sg, 1);
  945. if (offset >= buf->head[0].iov_len) {
  946. offset -= buf->head[0].iov_len;
  947. } else {
  948. thislen = buf->head[0].iov_len - offset;
  949. if (thislen > len)
  950. thislen = len;
  951. sg_set_buf(sg, buf->head[0].iov_base + offset, thislen);
  952. ret = actor(sg, data);
  953. if (ret)
  954. goto out;
  955. offset = 0;
  956. len -= thislen;
  957. }
  958. if (len == 0)
  959. goto out;
  960. if (offset >= buf->page_len) {
  961. offset -= buf->page_len;
  962. } else {
  963. page_len = buf->page_len - offset;
  964. if (page_len > len)
  965. page_len = len;
  966. len -= page_len;
  967. page_offset = (offset + buf->page_base) & (PAGE_CACHE_SIZE - 1);
  968. i = (offset + buf->page_base) >> PAGE_CACHE_SHIFT;
  969. thislen = PAGE_CACHE_SIZE - page_offset;
  970. do {
  971. if (thislen > page_len)
  972. thislen = page_len;
  973. sg_set_page(sg, buf->pages[i], thislen, page_offset);
  974. ret = actor(sg, data);
  975. if (ret)
  976. goto out;
  977. page_len -= thislen;
  978. i++;
  979. page_offset = 0;
  980. thislen = PAGE_CACHE_SIZE;
  981. } while (page_len != 0);
  982. offset = 0;
  983. }
  984. if (len == 0)
  985. goto out;
  986. if (offset < buf->tail[0].iov_len) {
  987. thislen = buf->tail[0].iov_len - offset;
  988. if (thislen > len)
  989. thislen = len;
  990. sg_set_buf(sg, buf->tail[0].iov_base + offset, thislen);
  991. ret = actor(sg, data);
  992. len -= thislen;
  993. }
  994. if (len != 0)
  995. ret = -EINVAL;
  996. out:
  997. return ret;
  998. }
  999. EXPORT_SYMBOL_GPL(xdr_process_buf);