xdr.c 28 KB

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