xdr.c 27 KB

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