xdr.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  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_GPL(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. static void xdr_set_iov(struct xdr_stream *xdr, struct kvec *iov,
  491. __be32 *p, unsigned int len)
  492. {
  493. if (len > iov->iov_len)
  494. len = iov->iov_len;
  495. if (p == NULL)
  496. p = (__be32*)iov->iov_base;
  497. xdr->p = p;
  498. xdr->end = (__be32*)(iov->iov_base + len);
  499. xdr->iov = iov;
  500. xdr->page_ptr = NULL;
  501. }
  502. static int xdr_set_page_base(struct xdr_stream *xdr,
  503. unsigned int base, unsigned int len)
  504. {
  505. unsigned int pgnr;
  506. unsigned int maxlen;
  507. unsigned int pgoff;
  508. unsigned int pgend;
  509. void *kaddr;
  510. maxlen = xdr->buf->page_len;
  511. if (base >= maxlen)
  512. return -EINVAL;
  513. maxlen -= base;
  514. if (len > maxlen)
  515. len = maxlen;
  516. base += xdr->buf->page_base;
  517. pgnr = base >> PAGE_SHIFT;
  518. xdr->page_ptr = &xdr->buf->pages[pgnr];
  519. kaddr = page_address(*xdr->page_ptr);
  520. pgoff = base & ~PAGE_MASK;
  521. xdr->p = (__be32*)(kaddr + pgoff);
  522. pgend = pgoff + len;
  523. if (pgend > PAGE_SIZE)
  524. pgend = PAGE_SIZE;
  525. xdr->end = (__be32*)(kaddr + pgend);
  526. xdr->iov = NULL;
  527. return 0;
  528. }
  529. static void xdr_set_next_page(struct xdr_stream *xdr)
  530. {
  531. unsigned int newbase;
  532. newbase = (1 + xdr->page_ptr - xdr->buf->pages) << PAGE_SHIFT;
  533. newbase -= xdr->buf->page_base;
  534. if (xdr_set_page_base(xdr, newbase, PAGE_SIZE) < 0)
  535. xdr_set_iov(xdr, xdr->buf->tail, NULL, xdr->buf->len);
  536. }
  537. static bool xdr_set_next_buffer(struct xdr_stream *xdr)
  538. {
  539. if (xdr->page_ptr != NULL)
  540. xdr_set_next_page(xdr);
  541. else if (xdr->iov == xdr->buf->head) {
  542. if (xdr_set_page_base(xdr, 0, PAGE_SIZE) < 0)
  543. xdr_set_iov(xdr, xdr->buf->tail, NULL, xdr->buf->len);
  544. }
  545. return xdr->p != xdr->end;
  546. }
  547. /**
  548. * xdr_init_decode - Initialize an xdr_stream for decoding data.
  549. * @xdr: pointer to xdr_stream struct
  550. * @buf: pointer to XDR buffer from which to decode data
  551. * @p: current pointer inside XDR buffer
  552. */
  553. void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p)
  554. {
  555. xdr->buf = buf;
  556. xdr->scratch.iov_base = NULL;
  557. xdr->scratch.iov_len = 0;
  558. if (buf->head[0].iov_len != 0)
  559. xdr_set_iov(xdr, buf->head, p, buf->len);
  560. else if (buf->page_len != 0)
  561. xdr_set_page_base(xdr, 0, buf->len);
  562. }
  563. EXPORT_SYMBOL_GPL(xdr_init_decode);
  564. /**
  565. * xdr_init_decode - Initialize an xdr_stream for decoding data.
  566. * @xdr: pointer to xdr_stream struct
  567. * @buf: pointer to XDR buffer from which to decode data
  568. * @pages: list of pages to decode into
  569. * @len: length in bytes of buffer in pages
  570. */
  571. void xdr_init_decode_pages(struct xdr_stream *xdr, struct xdr_buf *buf,
  572. struct page **pages, unsigned int len)
  573. {
  574. memset(buf, 0, sizeof(*buf));
  575. buf->pages = pages;
  576. buf->page_len = len;
  577. buf->buflen = len;
  578. buf->len = len;
  579. xdr_init_decode(xdr, buf, NULL);
  580. }
  581. EXPORT_SYMBOL_GPL(xdr_init_decode_pages);
  582. static __be32 * __xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
  583. {
  584. __be32 *p = xdr->p;
  585. __be32 *q = p + XDR_QUADLEN(nbytes);
  586. if (unlikely(q > xdr->end || q < p))
  587. return NULL;
  588. xdr->p = q;
  589. return p;
  590. }
  591. /**
  592. * xdr_set_scratch_buffer - Attach a scratch buffer for decoding data.
  593. * @xdr: pointer to xdr_stream struct
  594. * @buf: pointer to an empty buffer
  595. * @buflen: size of 'buf'
  596. *
  597. * The scratch buffer is used when decoding from an array of pages.
  598. * If an xdr_inline_decode() call spans across page boundaries, then
  599. * we copy the data into the scratch buffer in order to allow linear
  600. * access.
  601. */
  602. void xdr_set_scratch_buffer(struct xdr_stream *xdr, void *buf, size_t buflen)
  603. {
  604. xdr->scratch.iov_base = buf;
  605. xdr->scratch.iov_len = buflen;
  606. }
  607. EXPORT_SYMBOL_GPL(xdr_set_scratch_buffer);
  608. static __be32 *xdr_copy_to_scratch(struct xdr_stream *xdr, size_t nbytes)
  609. {
  610. __be32 *p;
  611. void *cpdest = xdr->scratch.iov_base;
  612. size_t cplen = (char *)xdr->end - (char *)xdr->p;
  613. if (nbytes > xdr->scratch.iov_len)
  614. return NULL;
  615. memcpy(cpdest, xdr->p, cplen);
  616. cpdest += cplen;
  617. nbytes -= cplen;
  618. if (!xdr_set_next_buffer(xdr))
  619. return NULL;
  620. p = __xdr_inline_decode(xdr, nbytes);
  621. if (p == NULL)
  622. return NULL;
  623. memcpy(cpdest, p, nbytes);
  624. return xdr->scratch.iov_base;
  625. }
  626. /**
  627. * xdr_inline_decode - Retrieve XDR data to decode
  628. * @xdr: pointer to xdr_stream struct
  629. * @nbytes: number of bytes of data to decode
  630. *
  631. * Check if the input buffer is long enough to enable us to decode
  632. * 'nbytes' more bytes of data starting at the current position.
  633. * If so return the current pointer, then update the current
  634. * pointer position.
  635. */
  636. __be32 * xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
  637. {
  638. __be32 *p;
  639. if (nbytes == 0)
  640. return xdr->p;
  641. if (xdr->p == xdr->end && !xdr_set_next_buffer(xdr))
  642. return NULL;
  643. p = __xdr_inline_decode(xdr, nbytes);
  644. if (p != NULL)
  645. return p;
  646. return xdr_copy_to_scratch(xdr, nbytes);
  647. }
  648. EXPORT_SYMBOL_GPL(xdr_inline_decode);
  649. /**
  650. * xdr_read_pages - Ensure page-based XDR data to decode is aligned at current pointer position
  651. * @xdr: pointer to xdr_stream struct
  652. * @len: number of bytes of page data
  653. *
  654. * Moves data beyond the current pointer position from the XDR head[] buffer
  655. * into the page list. Any data that lies beyond current position + "len"
  656. * bytes is moved into the XDR tail[].
  657. */
  658. void xdr_read_pages(struct xdr_stream *xdr, unsigned int len)
  659. {
  660. struct xdr_buf *buf = xdr->buf;
  661. struct kvec *iov;
  662. ssize_t shift;
  663. unsigned int end;
  664. int padding;
  665. /* Realign pages to current pointer position */
  666. iov = buf->head;
  667. shift = iov->iov_len + (char *)iov->iov_base - (char *)xdr->p;
  668. if (shift > 0)
  669. xdr_shrink_bufhead(buf, shift);
  670. /* Truncate page data and move it into the tail */
  671. if (buf->page_len > len)
  672. xdr_shrink_pagelen(buf, buf->page_len - len);
  673. padding = (XDR_QUADLEN(len) << 2) - len;
  674. xdr->iov = iov = buf->tail;
  675. /* Compute remaining message length. */
  676. end = iov->iov_len;
  677. shift = buf->buflen - buf->len;
  678. if (shift < end)
  679. end -= shift;
  680. else if (shift > 0)
  681. end = 0;
  682. /*
  683. * Position current pointer at beginning of tail, and
  684. * set remaining message length.
  685. */
  686. xdr->p = (__be32 *)((char *)iov->iov_base + padding);
  687. xdr->end = (__be32 *)((char *)iov->iov_base + end);
  688. }
  689. EXPORT_SYMBOL_GPL(xdr_read_pages);
  690. /**
  691. * xdr_enter_page - decode data from the XDR page
  692. * @xdr: pointer to xdr_stream struct
  693. * @len: number of bytes of page data
  694. *
  695. * Moves data beyond the current pointer position from the XDR head[] buffer
  696. * into the page list. Any data that lies beyond current position + "len"
  697. * bytes is moved into the XDR tail[]. The current pointer is then
  698. * repositioned at the beginning of the first XDR page.
  699. */
  700. void xdr_enter_page(struct xdr_stream *xdr, unsigned int len)
  701. {
  702. xdr_read_pages(xdr, len);
  703. /*
  704. * Position current pointer at beginning of tail, and
  705. * set remaining message length.
  706. */
  707. xdr_set_page_base(xdr, 0, len);
  708. }
  709. EXPORT_SYMBOL_GPL(xdr_enter_page);
  710. static struct kvec empty_iov = {.iov_base = NULL, .iov_len = 0};
  711. void
  712. xdr_buf_from_iov(struct kvec *iov, struct xdr_buf *buf)
  713. {
  714. buf->head[0] = *iov;
  715. buf->tail[0] = empty_iov;
  716. buf->page_len = 0;
  717. buf->buflen = buf->len = iov->iov_len;
  718. }
  719. EXPORT_SYMBOL_GPL(xdr_buf_from_iov);
  720. /* Sets subbuf to the portion of buf of length len beginning base bytes
  721. * from the start of buf. Returns -1 if base of length are out of bounds. */
  722. int
  723. xdr_buf_subsegment(struct xdr_buf *buf, struct xdr_buf *subbuf,
  724. unsigned int base, unsigned int len)
  725. {
  726. subbuf->buflen = subbuf->len = len;
  727. if (base < buf->head[0].iov_len) {
  728. subbuf->head[0].iov_base = buf->head[0].iov_base + base;
  729. subbuf->head[0].iov_len = min_t(unsigned int, len,
  730. buf->head[0].iov_len - base);
  731. len -= subbuf->head[0].iov_len;
  732. base = 0;
  733. } else {
  734. subbuf->head[0].iov_base = NULL;
  735. subbuf->head[0].iov_len = 0;
  736. base -= buf->head[0].iov_len;
  737. }
  738. if (base < buf->page_len) {
  739. subbuf->page_len = min(buf->page_len - base, len);
  740. base += buf->page_base;
  741. subbuf->page_base = base & ~PAGE_CACHE_MASK;
  742. subbuf->pages = &buf->pages[base >> PAGE_CACHE_SHIFT];
  743. len -= subbuf->page_len;
  744. base = 0;
  745. } else {
  746. base -= buf->page_len;
  747. subbuf->page_len = 0;
  748. }
  749. if (base < buf->tail[0].iov_len) {
  750. subbuf->tail[0].iov_base = buf->tail[0].iov_base + base;
  751. subbuf->tail[0].iov_len = min_t(unsigned int, len,
  752. buf->tail[0].iov_len - base);
  753. len -= subbuf->tail[0].iov_len;
  754. base = 0;
  755. } else {
  756. subbuf->tail[0].iov_base = NULL;
  757. subbuf->tail[0].iov_len = 0;
  758. base -= buf->tail[0].iov_len;
  759. }
  760. if (base || len)
  761. return -1;
  762. return 0;
  763. }
  764. EXPORT_SYMBOL_GPL(xdr_buf_subsegment);
  765. static void __read_bytes_from_xdr_buf(struct xdr_buf *subbuf, void *obj, unsigned int len)
  766. {
  767. unsigned int this_len;
  768. this_len = min_t(unsigned int, len, 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_t(unsigned int, len, 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_t(unsigned int, len, subbuf->tail[0].iov_len);
  778. memcpy(obj, subbuf->tail[0].iov_base, this_len);
  779. }
  780. /* obj is assumed to point to allocated memory of size at least len: */
  781. int read_bytes_from_xdr_buf(struct xdr_buf *buf, unsigned int base, void *obj, unsigned int len)
  782. {
  783. struct xdr_buf subbuf;
  784. int status;
  785. status = xdr_buf_subsegment(buf, &subbuf, base, len);
  786. if (status != 0)
  787. return status;
  788. __read_bytes_from_xdr_buf(&subbuf, obj, len);
  789. return 0;
  790. }
  791. EXPORT_SYMBOL_GPL(read_bytes_from_xdr_buf);
  792. static void __write_bytes_to_xdr_buf(struct xdr_buf *subbuf, void *obj, unsigned int len)
  793. {
  794. unsigned int this_len;
  795. this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
  796. memcpy(subbuf->head[0].iov_base, obj, this_len);
  797. len -= this_len;
  798. obj += this_len;
  799. this_len = min_t(unsigned int, len, subbuf->page_len);
  800. if (this_len)
  801. _copy_to_pages(subbuf->pages, subbuf->page_base, obj, this_len);
  802. len -= this_len;
  803. obj += this_len;
  804. this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
  805. memcpy(subbuf->tail[0].iov_base, obj, this_len);
  806. }
  807. /* obj is assumed to point to allocated memory of size at least len: */
  808. int write_bytes_to_xdr_buf(struct xdr_buf *buf, unsigned int base, void *obj, unsigned int len)
  809. {
  810. struct xdr_buf subbuf;
  811. int status;
  812. status = xdr_buf_subsegment(buf, &subbuf, base, len);
  813. if (status != 0)
  814. return status;
  815. __write_bytes_to_xdr_buf(&subbuf, obj, len);
  816. return 0;
  817. }
  818. EXPORT_SYMBOL_GPL(write_bytes_to_xdr_buf);
  819. int
  820. xdr_decode_word(struct xdr_buf *buf, unsigned int base, u32 *obj)
  821. {
  822. __be32 raw;
  823. int status;
  824. status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
  825. if (status)
  826. return status;
  827. *obj = be32_to_cpu(raw);
  828. return 0;
  829. }
  830. EXPORT_SYMBOL_GPL(xdr_decode_word);
  831. int
  832. xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj)
  833. {
  834. __be32 raw = cpu_to_be32(obj);
  835. return write_bytes_to_xdr_buf(buf, base, &raw, sizeof(obj));
  836. }
  837. EXPORT_SYMBOL_GPL(xdr_encode_word);
  838. /* If the netobj starting offset bytes from the start of xdr_buf is contained
  839. * entirely in the head or the tail, set object to point to it; otherwise
  840. * try to find space for it at the end of the tail, copy it there, and
  841. * set obj to point to it. */
  842. int xdr_buf_read_netobj(struct xdr_buf *buf, struct xdr_netobj *obj, unsigned int offset)
  843. {
  844. struct xdr_buf subbuf;
  845. if (xdr_decode_word(buf, offset, &obj->len))
  846. return -EFAULT;
  847. if (xdr_buf_subsegment(buf, &subbuf, offset + 4, obj->len))
  848. return -EFAULT;
  849. /* Is the obj contained entirely in the head? */
  850. obj->data = subbuf.head[0].iov_base;
  851. if (subbuf.head[0].iov_len == obj->len)
  852. return 0;
  853. /* ..or is the obj contained entirely in the tail? */
  854. obj->data = subbuf.tail[0].iov_base;
  855. if (subbuf.tail[0].iov_len == obj->len)
  856. return 0;
  857. /* use end of tail as storage for obj:
  858. * (We don't copy to the beginning because then we'd have
  859. * to worry about doing a potentially overlapping copy.
  860. * This assumes the object is at most half the length of the
  861. * tail.) */
  862. if (obj->len > buf->buflen - buf->len)
  863. return -ENOMEM;
  864. if (buf->tail[0].iov_len != 0)
  865. obj->data = buf->tail[0].iov_base + buf->tail[0].iov_len;
  866. else
  867. obj->data = buf->head[0].iov_base + buf->head[0].iov_len;
  868. __read_bytes_from_xdr_buf(&subbuf, obj->data, obj->len);
  869. return 0;
  870. }
  871. EXPORT_SYMBOL_GPL(xdr_buf_read_netobj);
  872. /* Returns 0 on success, or else a negative error code. */
  873. static int
  874. xdr_xcode_array2(struct xdr_buf *buf, unsigned int base,
  875. struct xdr_array2_desc *desc, int encode)
  876. {
  877. char *elem = NULL, *c;
  878. unsigned int copied = 0, todo, avail_here;
  879. struct page **ppages = NULL;
  880. int err;
  881. if (encode) {
  882. if (xdr_encode_word(buf, base, desc->array_len) != 0)
  883. return -EINVAL;
  884. } else {
  885. if (xdr_decode_word(buf, base, &desc->array_len) != 0 ||
  886. desc->array_len > desc->array_maxlen ||
  887. (unsigned long) base + 4 + desc->array_len *
  888. desc->elem_size > buf->len)
  889. return -EINVAL;
  890. }
  891. base += 4;
  892. if (!desc->xcode)
  893. return 0;
  894. todo = desc->array_len * desc->elem_size;
  895. /* process head */
  896. if (todo && base < buf->head->iov_len) {
  897. c = buf->head->iov_base + base;
  898. avail_here = min_t(unsigned int, todo,
  899. buf->head->iov_len - base);
  900. todo -= avail_here;
  901. while (avail_here >= desc->elem_size) {
  902. err = desc->xcode(desc, c);
  903. if (err)
  904. goto out;
  905. c += desc->elem_size;
  906. avail_here -= desc->elem_size;
  907. }
  908. if (avail_here) {
  909. if (!elem) {
  910. elem = kmalloc(desc->elem_size, GFP_KERNEL);
  911. err = -ENOMEM;
  912. if (!elem)
  913. goto out;
  914. }
  915. if (encode) {
  916. err = desc->xcode(desc, elem);
  917. if (err)
  918. goto out;
  919. memcpy(c, elem, avail_here);
  920. } else
  921. memcpy(elem, c, avail_here);
  922. copied = avail_here;
  923. }
  924. base = buf->head->iov_len; /* align to start of pages */
  925. }
  926. /* process pages array */
  927. base -= buf->head->iov_len;
  928. if (todo && base < buf->page_len) {
  929. unsigned int avail_page;
  930. avail_here = min(todo, buf->page_len - base);
  931. todo -= avail_here;
  932. base += buf->page_base;
  933. ppages = buf->pages + (base >> PAGE_CACHE_SHIFT);
  934. base &= ~PAGE_CACHE_MASK;
  935. avail_page = min_t(unsigned int, PAGE_CACHE_SIZE - base,
  936. avail_here);
  937. c = kmap(*ppages) + base;
  938. while (avail_here) {
  939. avail_here -= avail_page;
  940. if (copied || avail_page < desc->elem_size) {
  941. unsigned int l = min(avail_page,
  942. desc->elem_size - copied);
  943. if (!elem) {
  944. elem = kmalloc(desc->elem_size,
  945. GFP_KERNEL);
  946. err = -ENOMEM;
  947. if (!elem)
  948. goto out;
  949. }
  950. if (encode) {
  951. if (!copied) {
  952. err = desc->xcode(desc, elem);
  953. if (err)
  954. goto out;
  955. }
  956. memcpy(c, elem + copied, l);
  957. copied += l;
  958. if (copied == desc->elem_size)
  959. copied = 0;
  960. } else {
  961. memcpy(elem + copied, c, l);
  962. copied += l;
  963. if (copied == desc->elem_size) {
  964. err = desc->xcode(desc, elem);
  965. if (err)
  966. goto out;
  967. copied = 0;
  968. }
  969. }
  970. avail_page -= l;
  971. c += l;
  972. }
  973. while (avail_page >= desc->elem_size) {
  974. err = desc->xcode(desc, c);
  975. if (err)
  976. goto out;
  977. c += desc->elem_size;
  978. avail_page -= desc->elem_size;
  979. }
  980. if (avail_page) {
  981. unsigned int l = min(avail_page,
  982. desc->elem_size - copied);
  983. if (!elem) {
  984. elem = kmalloc(desc->elem_size,
  985. GFP_KERNEL);
  986. err = -ENOMEM;
  987. if (!elem)
  988. goto out;
  989. }
  990. if (encode) {
  991. if (!copied) {
  992. err = desc->xcode(desc, elem);
  993. if (err)
  994. goto out;
  995. }
  996. memcpy(c, elem + copied, l);
  997. copied += l;
  998. if (copied == desc->elem_size)
  999. copied = 0;
  1000. } else {
  1001. memcpy(elem + copied, c, l);
  1002. copied += l;
  1003. if (copied == desc->elem_size) {
  1004. err = desc->xcode(desc, elem);
  1005. if (err)
  1006. goto out;
  1007. copied = 0;
  1008. }
  1009. }
  1010. }
  1011. if (avail_here) {
  1012. kunmap(*ppages);
  1013. ppages++;
  1014. c = kmap(*ppages);
  1015. }
  1016. avail_page = min(avail_here,
  1017. (unsigned int) PAGE_CACHE_SIZE);
  1018. }
  1019. base = buf->page_len; /* align to start of tail */
  1020. }
  1021. /* process tail */
  1022. base -= buf->page_len;
  1023. if (todo) {
  1024. c = buf->tail->iov_base + base;
  1025. if (copied) {
  1026. unsigned int l = desc->elem_size - copied;
  1027. if (encode)
  1028. memcpy(c, elem + copied, l);
  1029. else {
  1030. memcpy(elem + copied, c, l);
  1031. err = desc->xcode(desc, elem);
  1032. if (err)
  1033. goto out;
  1034. }
  1035. todo -= l;
  1036. c += l;
  1037. }
  1038. while (todo) {
  1039. err = desc->xcode(desc, c);
  1040. if (err)
  1041. goto out;
  1042. c += desc->elem_size;
  1043. todo -= desc->elem_size;
  1044. }
  1045. }
  1046. err = 0;
  1047. out:
  1048. kfree(elem);
  1049. if (ppages)
  1050. kunmap(*ppages);
  1051. return err;
  1052. }
  1053. int
  1054. xdr_decode_array2(struct xdr_buf *buf, unsigned int base,
  1055. struct xdr_array2_desc *desc)
  1056. {
  1057. if (base >= buf->len)
  1058. return -EINVAL;
  1059. return xdr_xcode_array2(buf, base, desc, 0);
  1060. }
  1061. EXPORT_SYMBOL_GPL(xdr_decode_array2);
  1062. int
  1063. xdr_encode_array2(struct xdr_buf *buf, unsigned int base,
  1064. struct xdr_array2_desc *desc)
  1065. {
  1066. if ((unsigned long) base + 4 + desc->array_len * desc->elem_size >
  1067. buf->head->iov_len + buf->page_len + buf->tail->iov_len)
  1068. return -EINVAL;
  1069. return xdr_xcode_array2(buf, base, desc, 1);
  1070. }
  1071. EXPORT_SYMBOL_GPL(xdr_encode_array2);
  1072. int
  1073. xdr_process_buf(struct xdr_buf *buf, unsigned int offset, unsigned int len,
  1074. int (*actor)(struct scatterlist *, void *), void *data)
  1075. {
  1076. int i, ret = 0;
  1077. unsigned page_len, thislen, page_offset;
  1078. struct scatterlist sg[1];
  1079. sg_init_table(sg, 1);
  1080. if (offset >= buf->head[0].iov_len) {
  1081. offset -= buf->head[0].iov_len;
  1082. } else {
  1083. thislen = buf->head[0].iov_len - offset;
  1084. if (thislen > len)
  1085. thislen = len;
  1086. sg_set_buf(sg, buf->head[0].iov_base + offset, thislen);
  1087. ret = actor(sg, data);
  1088. if (ret)
  1089. goto out;
  1090. offset = 0;
  1091. len -= thislen;
  1092. }
  1093. if (len == 0)
  1094. goto out;
  1095. if (offset >= buf->page_len) {
  1096. offset -= buf->page_len;
  1097. } else {
  1098. page_len = buf->page_len - offset;
  1099. if (page_len > len)
  1100. page_len = len;
  1101. len -= page_len;
  1102. page_offset = (offset + buf->page_base) & (PAGE_CACHE_SIZE - 1);
  1103. i = (offset + buf->page_base) >> PAGE_CACHE_SHIFT;
  1104. thislen = PAGE_CACHE_SIZE - page_offset;
  1105. do {
  1106. if (thislen > page_len)
  1107. thislen = page_len;
  1108. sg_set_page(sg, buf->pages[i], thislen, page_offset);
  1109. ret = actor(sg, data);
  1110. if (ret)
  1111. goto out;
  1112. page_len -= thislen;
  1113. i++;
  1114. page_offset = 0;
  1115. thislen = PAGE_CACHE_SIZE;
  1116. } while (page_len != 0);
  1117. offset = 0;
  1118. }
  1119. if (len == 0)
  1120. goto out;
  1121. if (offset < buf->tail[0].iov_len) {
  1122. thislen = buf->tail[0].iov_len - offset;
  1123. if (thislen > len)
  1124. thislen = len;
  1125. sg_set_buf(sg, buf->tail[0].iov_base + offset, thislen);
  1126. ret = actor(sg, data);
  1127. len -= thislen;
  1128. }
  1129. if (len != 0)
  1130. ret = -EINVAL;
  1131. out:
  1132. return ret;
  1133. }
  1134. EXPORT_SYMBOL_GPL(xdr_process_buf);