xdr.c 22 KB

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