xdr.c 27 KB

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