xdr.c 24 KB

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