xdr.c 27 KB

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