xdr.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514
  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]);
  113. kaddr[buf->page_base + len] = '\0';
  114. kunmap_atomic(kaddr);
  115. }
  116. EXPORT_SYMBOL_GPL(xdr_terminate_string);
  117. void
  118. xdr_inline_pages(struct xdr_buf *xdr, unsigned int offset,
  119. struct page **pages, unsigned int base, unsigned int len)
  120. {
  121. struct kvec *head = xdr->head;
  122. struct kvec *tail = xdr->tail;
  123. char *buf = (char *)head->iov_base;
  124. unsigned int buflen = head->iov_len;
  125. head->iov_len = offset;
  126. xdr->pages = pages;
  127. xdr->page_base = base;
  128. xdr->page_len = len;
  129. tail->iov_base = buf + offset;
  130. tail->iov_len = buflen - offset;
  131. xdr->buflen += len;
  132. }
  133. EXPORT_SYMBOL_GPL(xdr_inline_pages);
  134. /*
  135. * Helper routines for doing 'memmove' like operations on a struct xdr_buf
  136. */
  137. /**
  138. * _shift_data_right_pages
  139. * @pages: vector of pages containing both the source and dest memory area.
  140. * @pgto_base: page vector address of destination
  141. * @pgfrom_base: page vector address of source
  142. * @len: number of bytes to copy
  143. *
  144. * Note: the addresses pgto_base and pgfrom_base are both calculated in
  145. * the same way:
  146. * if a memory area starts at byte 'base' in page 'pages[i]',
  147. * then its address is given as (i << PAGE_CACHE_SHIFT) + base
  148. * Also note: pgfrom_base must be < pgto_base, but the memory areas
  149. * they point to may overlap.
  150. */
  151. static void
  152. _shift_data_right_pages(struct page **pages, size_t pgto_base,
  153. size_t pgfrom_base, size_t len)
  154. {
  155. struct page **pgfrom, **pgto;
  156. char *vfrom, *vto;
  157. size_t copy;
  158. BUG_ON(pgto_base <= pgfrom_base);
  159. pgto_base += len;
  160. pgfrom_base += len;
  161. pgto = pages + (pgto_base >> PAGE_CACHE_SHIFT);
  162. pgfrom = pages + (pgfrom_base >> PAGE_CACHE_SHIFT);
  163. pgto_base &= ~PAGE_CACHE_MASK;
  164. pgfrom_base &= ~PAGE_CACHE_MASK;
  165. do {
  166. /* Are any pointers crossing a page boundary? */
  167. if (pgto_base == 0) {
  168. pgto_base = PAGE_CACHE_SIZE;
  169. pgto--;
  170. }
  171. if (pgfrom_base == 0) {
  172. pgfrom_base = PAGE_CACHE_SIZE;
  173. pgfrom--;
  174. }
  175. copy = len;
  176. if (copy > pgto_base)
  177. copy = pgto_base;
  178. if (copy > pgfrom_base)
  179. copy = pgfrom_base;
  180. pgto_base -= copy;
  181. pgfrom_base -= copy;
  182. vto = kmap_atomic(*pgto);
  183. if (*pgto != *pgfrom) {
  184. vfrom = kmap_atomic(*pgfrom);
  185. memcpy(vto + pgto_base, vfrom + pgfrom_base, copy);
  186. kunmap_atomic(vfrom);
  187. } else
  188. memmove(vto + pgto_base, vto + pgfrom_base, copy);
  189. flush_dcache_page(*pgto);
  190. kunmap_atomic(vto);
  191. } while ((len -= copy) != 0);
  192. }
  193. /**
  194. * _copy_to_pages
  195. * @pages: array of pages
  196. * @pgbase: page vector address of destination
  197. * @p: pointer to source data
  198. * @len: length
  199. *
  200. * Copies data from an arbitrary memory location into an array of pages
  201. * The copy is assumed to be non-overlapping.
  202. */
  203. static void
  204. _copy_to_pages(struct page **pages, size_t pgbase, const char *p, size_t len)
  205. {
  206. struct page **pgto;
  207. char *vto;
  208. size_t copy;
  209. pgto = pages + (pgbase >> PAGE_CACHE_SHIFT);
  210. pgbase &= ~PAGE_CACHE_MASK;
  211. for (;;) {
  212. copy = PAGE_CACHE_SIZE - pgbase;
  213. if (copy > len)
  214. copy = len;
  215. vto = kmap_atomic(*pgto);
  216. memcpy(vto + pgbase, p, copy);
  217. kunmap_atomic(vto);
  218. len -= copy;
  219. if (len == 0)
  220. break;
  221. pgbase += copy;
  222. if (pgbase == PAGE_CACHE_SIZE) {
  223. flush_dcache_page(*pgto);
  224. pgbase = 0;
  225. pgto++;
  226. }
  227. p += copy;
  228. }
  229. flush_dcache_page(*pgto);
  230. }
  231. /**
  232. * _copy_from_pages
  233. * @p: pointer to destination
  234. * @pages: array of pages
  235. * @pgbase: offset of source data
  236. * @len: length
  237. *
  238. * Copies data into an arbitrary memory location from an array of pages
  239. * The copy is assumed to be non-overlapping.
  240. */
  241. void
  242. _copy_from_pages(char *p, struct page **pages, size_t pgbase, size_t len)
  243. {
  244. struct page **pgfrom;
  245. char *vfrom;
  246. size_t copy;
  247. pgfrom = pages + (pgbase >> PAGE_CACHE_SHIFT);
  248. pgbase &= ~PAGE_CACHE_MASK;
  249. do {
  250. copy = PAGE_CACHE_SIZE - pgbase;
  251. if (copy > len)
  252. copy = len;
  253. vfrom = kmap_atomic(*pgfrom);
  254. memcpy(p, vfrom + pgbase, copy);
  255. kunmap_atomic(vfrom);
  256. pgbase += copy;
  257. if (pgbase == PAGE_CACHE_SIZE) {
  258. pgbase = 0;
  259. pgfrom++;
  260. }
  261. p += copy;
  262. } while ((len -= copy) != 0);
  263. }
  264. EXPORT_SYMBOL_GPL(_copy_from_pages);
  265. /**
  266. * xdr_shrink_bufhead
  267. * @buf: xdr_buf
  268. * @len: bytes to remove from buf->head[0]
  269. *
  270. * Shrinks XDR buffer's header kvec buf->head[0] by
  271. * 'len' bytes. The extra data is not lost, but is instead
  272. * moved into the inlined pages and/or the tail.
  273. */
  274. static void
  275. xdr_shrink_bufhead(struct xdr_buf *buf, size_t len)
  276. {
  277. struct kvec *head, *tail;
  278. size_t copy, offs;
  279. unsigned int pglen = buf->page_len;
  280. tail = buf->tail;
  281. head = buf->head;
  282. WARN_ON_ONCE(len > head->iov_len);
  283. if (len > head->iov_len)
  284. len = head->iov_len;
  285. /* Shift the tail first */
  286. if (tail->iov_len != 0) {
  287. if (tail->iov_len > len) {
  288. copy = tail->iov_len - len;
  289. memmove((char *)tail->iov_base + len,
  290. tail->iov_base, copy);
  291. }
  292. /* Copy from the inlined pages into the tail */
  293. copy = len;
  294. if (copy > pglen)
  295. copy = pglen;
  296. offs = len - copy;
  297. if (offs >= tail->iov_len)
  298. copy = 0;
  299. else if (copy > tail->iov_len - offs)
  300. copy = tail->iov_len - offs;
  301. if (copy != 0)
  302. _copy_from_pages((char *)tail->iov_base + offs,
  303. buf->pages,
  304. buf->page_base + pglen + offs - len,
  305. copy);
  306. /* Do we also need to copy data from the head into the tail ? */
  307. if (len > pglen) {
  308. offs = copy = len - pglen;
  309. if (copy > tail->iov_len)
  310. copy = tail->iov_len;
  311. memcpy(tail->iov_base,
  312. (char *)head->iov_base +
  313. head->iov_len - offs,
  314. copy);
  315. }
  316. }
  317. /* Now handle pages */
  318. if (pglen != 0) {
  319. if (pglen > len)
  320. _shift_data_right_pages(buf->pages,
  321. buf->page_base + len,
  322. buf->page_base,
  323. pglen - len);
  324. copy = len;
  325. if (len > pglen)
  326. copy = pglen;
  327. _copy_to_pages(buf->pages, buf->page_base,
  328. (char *)head->iov_base + head->iov_len - len,
  329. copy);
  330. }
  331. head->iov_len -= len;
  332. buf->buflen -= len;
  333. /* Have we truncated the message? */
  334. if (buf->len > buf->buflen)
  335. buf->len = buf->buflen;
  336. }
  337. /**
  338. * xdr_shrink_pagelen
  339. * @buf: xdr_buf
  340. * @len: bytes to remove from buf->pages
  341. *
  342. * Shrinks XDR buffer's page array buf->pages by
  343. * 'len' bytes. The extra data is not lost, but is instead
  344. * moved into the tail.
  345. */
  346. static void
  347. xdr_shrink_pagelen(struct xdr_buf *buf, size_t len)
  348. {
  349. struct kvec *tail;
  350. size_t copy;
  351. unsigned int pglen = buf->page_len;
  352. unsigned int tailbuf_len;
  353. tail = buf->tail;
  354. BUG_ON (len > pglen);
  355. tailbuf_len = buf->buflen - buf->head->iov_len - buf->page_len;
  356. /* Shift the tail first */
  357. if (tailbuf_len != 0) {
  358. unsigned int free_space = tailbuf_len - tail->iov_len;
  359. if (len < free_space)
  360. free_space = len;
  361. tail->iov_len += free_space;
  362. copy = len;
  363. if (tail->iov_len > len) {
  364. char *p = (char *)tail->iov_base + len;
  365. memmove(p, tail->iov_base, tail->iov_len - len);
  366. } else
  367. copy = tail->iov_len;
  368. /* Copy from the inlined pages into the tail */
  369. _copy_from_pages((char *)tail->iov_base,
  370. buf->pages, buf->page_base + pglen - len,
  371. copy);
  372. }
  373. buf->page_len -= len;
  374. buf->buflen -= len;
  375. /* Have we truncated the message? */
  376. if (buf->len > buf->buflen)
  377. buf->len = buf->buflen;
  378. }
  379. void
  380. xdr_shift_buf(struct xdr_buf *buf, size_t len)
  381. {
  382. xdr_shrink_bufhead(buf, len);
  383. }
  384. EXPORT_SYMBOL_GPL(xdr_shift_buf);
  385. /**
  386. * xdr_stream_pos - Return the current offset from the start of the xdr_stream
  387. * @xdr: pointer to struct xdr_stream
  388. */
  389. unsigned int xdr_stream_pos(const struct xdr_stream *xdr)
  390. {
  391. return (unsigned int)(XDR_QUADLEN(xdr->buf->len) - xdr->nwords) << 2;
  392. }
  393. EXPORT_SYMBOL_GPL(xdr_stream_pos);
  394. /**
  395. * xdr_init_encode - Initialize a struct xdr_stream for sending data.
  396. * @xdr: pointer to xdr_stream struct
  397. * @buf: pointer to XDR buffer in which to encode data
  398. * @p: current pointer inside XDR buffer
  399. *
  400. * Note: at the moment the RPC client only passes the length of our
  401. * scratch buffer in the xdr_buf's header kvec. Previously this
  402. * meant we needed to call xdr_adjust_iovec() after encoding the
  403. * data. With the new scheme, the xdr_stream manages the details
  404. * of the buffer length, and takes care of adjusting the kvec
  405. * length for us.
  406. */
  407. void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p)
  408. {
  409. struct kvec *iov = buf->head;
  410. int scratch_len = buf->buflen - buf->page_len - buf->tail[0].iov_len;
  411. xdr_set_scratch_buffer(xdr, NULL, 0);
  412. BUG_ON(scratch_len < 0);
  413. xdr->buf = buf;
  414. xdr->iov = iov;
  415. xdr->p = (__be32 *)((char *)iov->iov_base + iov->iov_len);
  416. xdr->end = (__be32 *)((char *)iov->iov_base + scratch_len);
  417. BUG_ON(iov->iov_len > scratch_len);
  418. if (p != xdr->p && p != NULL) {
  419. size_t len;
  420. BUG_ON(p < xdr->p || p > xdr->end);
  421. len = (char *)p - (char *)xdr->p;
  422. xdr->p = p;
  423. buf->len += len;
  424. iov->iov_len += len;
  425. }
  426. }
  427. EXPORT_SYMBOL_GPL(xdr_init_encode);
  428. /**
  429. * xdr_commit_encode - Ensure all data is written to buffer
  430. * @xdr: pointer to xdr_stream
  431. *
  432. * We handle encoding across page boundaries by giving the caller a
  433. * temporary location to write to, then later copying the data into
  434. * place; xdr_commit_encode does that copying.
  435. *
  436. * Normally the caller doesn't need to call this directly, as the
  437. * following xdr_reserve_space will do it. But an explicit call may be
  438. * required at the end of encoding, or any other time when the xdr_buf
  439. * data might be read.
  440. */
  441. void xdr_commit_encode(struct xdr_stream *xdr)
  442. {
  443. int shift = xdr->scratch.iov_len;
  444. void *page;
  445. if (shift == 0)
  446. return;
  447. page = page_address(*xdr->page_ptr);
  448. memcpy(xdr->scratch.iov_base, page, shift);
  449. memmove(page, page + shift, (void *)xdr->p - page);
  450. xdr->scratch.iov_len = 0;
  451. }
  452. EXPORT_SYMBOL_GPL(xdr_commit_encode);
  453. static __be32 *xdr_get_next_encode_buffer(struct xdr_stream *xdr,
  454. size_t nbytes)
  455. {
  456. __be32 *p;
  457. int space_left;
  458. int frag1bytes, frag2bytes;
  459. if (nbytes > PAGE_SIZE)
  460. return NULL; /* Bigger buffers require special handling */
  461. if (xdr->buf->len + nbytes > xdr->buf->buflen)
  462. return NULL; /* Sorry, we're totally out of space */
  463. frag1bytes = (xdr->end - xdr->p) << 2;
  464. frag2bytes = nbytes - frag1bytes;
  465. if (xdr->iov)
  466. xdr->iov->iov_len += frag1bytes;
  467. else
  468. xdr->buf->page_len += frag1bytes;
  469. xdr->page_ptr++;
  470. xdr->iov = NULL;
  471. /*
  472. * If the last encode didn't end exactly on a page boundary, the
  473. * next one will straddle boundaries. Encode into the next
  474. * page, then copy it back later in xdr_commit_encode. We use
  475. * the "scratch" iov to track any temporarily unused fragment of
  476. * space at the end of the previous buffer:
  477. */
  478. xdr->scratch.iov_base = xdr->p;
  479. xdr->scratch.iov_len = frag1bytes;
  480. p = page_address(*xdr->page_ptr);
  481. /*
  482. * Note this is where the next encode will start after we've
  483. * shifted this one back:
  484. */
  485. xdr->p = (void *)p + frag2bytes;
  486. space_left = xdr->buf->buflen - xdr->buf->len;
  487. xdr->end = (void *)p + min_t(int, space_left, PAGE_SIZE);
  488. xdr->buf->page_len += frag2bytes;
  489. xdr->buf->len += nbytes;
  490. return p;
  491. }
  492. /**
  493. * xdr_reserve_space - Reserve buffer space for sending
  494. * @xdr: pointer to xdr_stream
  495. * @nbytes: number of bytes to reserve
  496. *
  497. * Checks that we have enough buffer space to encode 'nbytes' more
  498. * bytes of data. If so, update the total xdr_buf length, and
  499. * adjust the length of the current kvec.
  500. */
  501. __be32 * xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes)
  502. {
  503. __be32 *p = xdr->p;
  504. __be32 *q;
  505. xdr_commit_encode(xdr);
  506. /* align nbytes on the next 32-bit boundary */
  507. nbytes += 3;
  508. nbytes &= ~3;
  509. q = p + (nbytes >> 2);
  510. if (unlikely(q > xdr->end || q < p))
  511. return xdr_get_next_encode_buffer(xdr, nbytes);
  512. xdr->p = q;
  513. if (xdr->iov)
  514. xdr->iov->iov_len += nbytes;
  515. else
  516. xdr->buf->page_len += nbytes;
  517. xdr->buf->len += nbytes;
  518. return p;
  519. }
  520. EXPORT_SYMBOL_GPL(xdr_reserve_space);
  521. /**
  522. * xdr_truncate_encode - truncate an encode buffer
  523. * @xdr: pointer to xdr_stream
  524. * @len: new length of buffer
  525. *
  526. * Truncates the xdr stream, so that xdr->buf->len == len,
  527. * and xdr->p points at offset len from the start of the buffer, and
  528. * head, tail, and page lengths are adjusted to correspond.
  529. *
  530. * If this means moving xdr->p to a different buffer, we assume that
  531. * that the end pointer should be set to the end of the current page,
  532. * except in the case of the head buffer when we assume the head
  533. * buffer's current length represents the end of the available buffer.
  534. *
  535. * This is *not* safe to use on a buffer that already has inlined page
  536. * cache pages (as in a zero-copy server read reply), except for the
  537. * simple case of truncating from one position in the tail to another.
  538. *
  539. */
  540. void xdr_truncate_encode(struct xdr_stream *xdr, size_t len)
  541. {
  542. struct xdr_buf *buf = xdr->buf;
  543. struct kvec *head = buf->head;
  544. struct kvec *tail = buf->tail;
  545. int fraglen;
  546. int new;
  547. if (len > buf->len) {
  548. WARN_ON_ONCE(1);
  549. return;
  550. }
  551. xdr_commit_encode(xdr);
  552. fraglen = min_t(int, buf->len - len, tail->iov_len);
  553. tail->iov_len -= fraglen;
  554. buf->len -= fraglen;
  555. if (tail->iov_len) {
  556. xdr->p = tail->iov_base + tail->iov_len;
  557. WARN_ON_ONCE(!xdr->end);
  558. WARN_ON_ONCE(!xdr->iov);
  559. return;
  560. }
  561. WARN_ON_ONCE(fraglen);
  562. fraglen = min_t(int, buf->len - len, buf->page_len);
  563. buf->page_len -= fraglen;
  564. buf->len -= fraglen;
  565. new = buf->page_base + buf->page_len;
  566. xdr->page_ptr = buf->pages + (new >> PAGE_SHIFT);
  567. if (buf->page_len) {
  568. xdr->p = page_address(*xdr->page_ptr);
  569. xdr->end = (void *)xdr->p + PAGE_SIZE;
  570. xdr->p = (void *)xdr->p + (new % PAGE_SIZE);
  571. WARN_ON_ONCE(xdr->iov);
  572. return;
  573. }
  574. if (fraglen)
  575. xdr->end = head->iov_base + head->iov_len;
  576. /* (otherwise assume xdr->end is already set) */
  577. xdr->page_ptr--;
  578. head->iov_len = len;
  579. buf->len = len;
  580. xdr->p = head->iov_base + head->iov_len;
  581. xdr->iov = buf->head;
  582. }
  583. EXPORT_SYMBOL(xdr_truncate_encode);
  584. /**
  585. * xdr_restrict_buflen - decrease available buffer space
  586. * @xdr: pointer to xdr_stream
  587. * @newbuflen: new maximum number of bytes available
  588. *
  589. * Adjust our idea of how much space is available in the buffer.
  590. * If we've already used too much space in the buffer, returns -1.
  591. * If the available space is already smaller than newbuflen, returns 0
  592. * and does nothing. Otherwise, adjusts xdr->buf->buflen to newbuflen
  593. * and ensures xdr->end is set at most offset newbuflen from the start
  594. * of the buffer.
  595. */
  596. int xdr_restrict_buflen(struct xdr_stream *xdr, int newbuflen)
  597. {
  598. struct xdr_buf *buf = xdr->buf;
  599. int left_in_this_buf = (void *)xdr->end - (void *)xdr->p;
  600. int end_offset = buf->len + left_in_this_buf;
  601. if (newbuflen < 0 || newbuflen < buf->len)
  602. return -1;
  603. if (newbuflen > buf->buflen)
  604. return 0;
  605. if (newbuflen < end_offset)
  606. xdr->end = (void *)xdr->end + newbuflen - end_offset;
  607. buf->buflen = newbuflen;
  608. return 0;
  609. }
  610. EXPORT_SYMBOL(xdr_restrict_buflen);
  611. /**
  612. * xdr_write_pages - Insert a list of pages into an XDR buffer for sending
  613. * @xdr: pointer to xdr_stream
  614. * @pages: list of pages
  615. * @base: offset of first byte
  616. * @len: length of data in bytes
  617. *
  618. */
  619. void xdr_write_pages(struct xdr_stream *xdr, struct page **pages, unsigned int base,
  620. unsigned int len)
  621. {
  622. struct xdr_buf *buf = xdr->buf;
  623. struct kvec *iov = buf->tail;
  624. buf->pages = pages;
  625. buf->page_base = base;
  626. buf->page_len = len;
  627. iov->iov_base = (char *)xdr->p;
  628. iov->iov_len = 0;
  629. xdr->iov = iov;
  630. if (len & 3) {
  631. unsigned int pad = 4 - (len & 3);
  632. BUG_ON(xdr->p >= xdr->end);
  633. iov->iov_base = (char *)xdr->p + (len & 3);
  634. iov->iov_len += pad;
  635. len += pad;
  636. *xdr->p++ = 0;
  637. }
  638. buf->buflen += len;
  639. buf->len += len;
  640. }
  641. EXPORT_SYMBOL_GPL(xdr_write_pages);
  642. static void xdr_set_iov(struct xdr_stream *xdr, struct kvec *iov,
  643. unsigned int len)
  644. {
  645. if (len > iov->iov_len)
  646. len = iov->iov_len;
  647. xdr->p = (__be32*)iov->iov_base;
  648. xdr->end = (__be32*)(iov->iov_base + len);
  649. xdr->iov = iov;
  650. xdr->page_ptr = NULL;
  651. }
  652. static int xdr_set_page_base(struct xdr_stream *xdr,
  653. unsigned int base, unsigned int len)
  654. {
  655. unsigned int pgnr;
  656. unsigned int maxlen;
  657. unsigned int pgoff;
  658. unsigned int pgend;
  659. void *kaddr;
  660. maxlen = xdr->buf->page_len;
  661. if (base >= maxlen)
  662. return -EINVAL;
  663. maxlen -= base;
  664. if (len > maxlen)
  665. len = maxlen;
  666. base += xdr->buf->page_base;
  667. pgnr = base >> PAGE_SHIFT;
  668. xdr->page_ptr = &xdr->buf->pages[pgnr];
  669. kaddr = page_address(*xdr->page_ptr);
  670. pgoff = base & ~PAGE_MASK;
  671. xdr->p = (__be32*)(kaddr + pgoff);
  672. pgend = pgoff + len;
  673. if (pgend > PAGE_SIZE)
  674. pgend = PAGE_SIZE;
  675. xdr->end = (__be32*)(kaddr + pgend);
  676. xdr->iov = NULL;
  677. return 0;
  678. }
  679. static void xdr_set_next_page(struct xdr_stream *xdr)
  680. {
  681. unsigned int newbase;
  682. newbase = (1 + xdr->page_ptr - xdr->buf->pages) << PAGE_SHIFT;
  683. newbase -= xdr->buf->page_base;
  684. if (xdr_set_page_base(xdr, newbase, PAGE_SIZE) < 0)
  685. xdr_set_iov(xdr, xdr->buf->tail, xdr->buf->len);
  686. }
  687. static bool xdr_set_next_buffer(struct xdr_stream *xdr)
  688. {
  689. if (xdr->page_ptr != NULL)
  690. xdr_set_next_page(xdr);
  691. else if (xdr->iov == xdr->buf->head) {
  692. if (xdr_set_page_base(xdr, 0, PAGE_SIZE) < 0)
  693. xdr_set_iov(xdr, xdr->buf->tail, xdr->buf->len);
  694. }
  695. return xdr->p != xdr->end;
  696. }
  697. /**
  698. * xdr_init_decode - Initialize an xdr_stream for decoding data.
  699. * @xdr: pointer to xdr_stream struct
  700. * @buf: pointer to XDR buffer from which to decode data
  701. * @p: current pointer inside XDR buffer
  702. */
  703. void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p)
  704. {
  705. xdr->buf = buf;
  706. xdr->scratch.iov_base = NULL;
  707. xdr->scratch.iov_len = 0;
  708. xdr->nwords = XDR_QUADLEN(buf->len);
  709. if (buf->head[0].iov_len != 0)
  710. xdr_set_iov(xdr, buf->head, buf->len);
  711. else if (buf->page_len != 0)
  712. xdr_set_page_base(xdr, 0, buf->len);
  713. if (p != NULL && p > xdr->p && xdr->end >= p) {
  714. xdr->nwords -= p - xdr->p;
  715. xdr->p = p;
  716. }
  717. }
  718. EXPORT_SYMBOL_GPL(xdr_init_decode);
  719. /**
  720. * xdr_init_decode - Initialize an xdr_stream for decoding data.
  721. * @xdr: pointer to xdr_stream struct
  722. * @buf: pointer to XDR buffer from which to decode data
  723. * @pages: list of pages to decode into
  724. * @len: length in bytes of buffer in pages
  725. */
  726. void xdr_init_decode_pages(struct xdr_stream *xdr, struct xdr_buf *buf,
  727. struct page **pages, unsigned int len)
  728. {
  729. memset(buf, 0, sizeof(*buf));
  730. buf->pages = pages;
  731. buf->page_len = len;
  732. buf->buflen = len;
  733. buf->len = len;
  734. xdr_init_decode(xdr, buf, NULL);
  735. }
  736. EXPORT_SYMBOL_GPL(xdr_init_decode_pages);
  737. static __be32 * __xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
  738. {
  739. unsigned int nwords = XDR_QUADLEN(nbytes);
  740. __be32 *p = xdr->p;
  741. __be32 *q = p + nwords;
  742. if (unlikely(nwords > xdr->nwords || q > xdr->end || q < p))
  743. return NULL;
  744. xdr->p = q;
  745. xdr->nwords -= nwords;
  746. return p;
  747. }
  748. /**
  749. * xdr_set_scratch_buffer - Attach a scratch buffer for decoding data.
  750. * @xdr: pointer to xdr_stream struct
  751. * @buf: pointer to an empty buffer
  752. * @buflen: size of 'buf'
  753. *
  754. * The scratch buffer is used when decoding from an array of pages.
  755. * If an xdr_inline_decode() call spans across page boundaries, then
  756. * we copy the data into the scratch buffer in order to allow linear
  757. * access.
  758. */
  759. void xdr_set_scratch_buffer(struct xdr_stream *xdr, void *buf, size_t buflen)
  760. {
  761. xdr->scratch.iov_base = buf;
  762. xdr->scratch.iov_len = buflen;
  763. }
  764. EXPORT_SYMBOL_GPL(xdr_set_scratch_buffer);
  765. static __be32 *xdr_copy_to_scratch(struct xdr_stream *xdr, size_t nbytes)
  766. {
  767. __be32 *p;
  768. void *cpdest = xdr->scratch.iov_base;
  769. size_t cplen = (char *)xdr->end - (char *)xdr->p;
  770. if (nbytes > xdr->scratch.iov_len)
  771. return NULL;
  772. memcpy(cpdest, xdr->p, cplen);
  773. cpdest += cplen;
  774. nbytes -= cplen;
  775. if (!xdr_set_next_buffer(xdr))
  776. return NULL;
  777. p = __xdr_inline_decode(xdr, nbytes);
  778. if (p == NULL)
  779. return NULL;
  780. memcpy(cpdest, p, nbytes);
  781. return xdr->scratch.iov_base;
  782. }
  783. /**
  784. * xdr_inline_decode - Retrieve XDR data to decode
  785. * @xdr: pointer to xdr_stream struct
  786. * @nbytes: number of bytes of data to decode
  787. *
  788. * Check if the input buffer is long enough to enable us to decode
  789. * 'nbytes' more bytes of data starting at the current position.
  790. * If so return the current pointer, then update the current
  791. * pointer position.
  792. */
  793. __be32 * xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
  794. {
  795. __be32 *p;
  796. if (nbytes == 0)
  797. return xdr->p;
  798. if (xdr->p == xdr->end && !xdr_set_next_buffer(xdr))
  799. return NULL;
  800. p = __xdr_inline_decode(xdr, nbytes);
  801. if (p != NULL)
  802. return p;
  803. return xdr_copy_to_scratch(xdr, nbytes);
  804. }
  805. EXPORT_SYMBOL_GPL(xdr_inline_decode);
  806. static unsigned int xdr_align_pages(struct xdr_stream *xdr, unsigned int len)
  807. {
  808. struct xdr_buf *buf = xdr->buf;
  809. struct kvec *iov;
  810. unsigned int nwords = XDR_QUADLEN(len);
  811. unsigned int cur = xdr_stream_pos(xdr);
  812. if (xdr->nwords == 0)
  813. return 0;
  814. /* Realign pages to current pointer position */
  815. iov = buf->head;
  816. if (iov->iov_len > cur) {
  817. xdr_shrink_bufhead(buf, iov->iov_len - cur);
  818. xdr->nwords = XDR_QUADLEN(buf->len - cur);
  819. }
  820. if (nwords > xdr->nwords) {
  821. nwords = xdr->nwords;
  822. len = nwords << 2;
  823. }
  824. if (buf->page_len <= len)
  825. len = buf->page_len;
  826. else if (nwords < xdr->nwords) {
  827. /* Truncate page data and move it into the tail */
  828. xdr_shrink_pagelen(buf, buf->page_len - len);
  829. xdr->nwords = XDR_QUADLEN(buf->len - cur);
  830. }
  831. return len;
  832. }
  833. /**
  834. * xdr_read_pages - Ensure page-based XDR data to decode is aligned at current pointer position
  835. * @xdr: pointer to xdr_stream struct
  836. * @len: number of bytes of page data
  837. *
  838. * Moves data beyond the current pointer position from the XDR head[] buffer
  839. * into the page list. Any data that lies beyond current position + "len"
  840. * bytes is moved into the XDR tail[].
  841. *
  842. * Returns the number of XDR encoded bytes now contained in the pages
  843. */
  844. unsigned int xdr_read_pages(struct xdr_stream *xdr, unsigned int len)
  845. {
  846. struct xdr_buf *buf = xdr->buf;
  847. struct kvec *iov;
  848. unsigned int nwords;
  849. unsigned int end;
  850. unsigned int padding;
  851. len = xdr_align_pages(xdr, len);
  852. if (len == 0)
  853. return 0;
  854. nwords = XDR_QUADLEN(len);
  855. padding = (nwords << 2) - len;
  856. xdr->iov = iov = buf->tail;
  857. /* Compute remaining message length. */
  858. end = ((xdr->nwords - nwords) << 2) + padding;
  859. if (end > iov->iov_len)
  860. end = iov->iov_len;
  861. /*
  862. * Position current pointer at beginning of tail, and
  863. * set remaining message length.
  864. */
  865. xdr->p = (__be32 *)((char *)iov->iov_base + padding);
  866. xdr->end = (__be32 *)((char *)iov->iov_base + end);
  867. xdr->page_ptr = NULL;
  868. xdr->nwords = XDR_QUADLEN(end - padding);
  869. return len;
  870. }
  871. EXPORT_SYMBOL_GPL(xdr_read_pages);
  872. /**
  873. * xdr_enter_page - decode data from the XDR page
  874. * @xdr: pointer to xdr_stream struct
  875. * @len: number of bytes of page data
  876. *
  877. * Moves data beyond the current pointer position from the XDR head[] buffer
  878. * into the page list. Any data that lies beyond current position + "len"
  879. * bytes is moved into the XDR tail[]. The current pointer is then
  880. * repositioned at the beginning of the first XDR page.
  881. */
  882. void xdr_enter_page(struct xdr_stream *xdr, unsigned int len)
  883. {
  884. len = xdr_align_pages(xdr, len);
  885. /*
  886. * Position current pointer at beginning of tail, and
  887. * set remaining message length.
  888. */
  889. if (len != 0)
  890. xdr_set_page_base(xdr, 0, len);
  891. }
  892. EXPORT_SYMBOL_GPL(xdr_enter_page);
  893. static struct kvec empty_iov = {.iov_base = NULL, .iov_len = 0};
  894. void
  895. xdr_buf_from_iov(struct kvec *iov, struct xdr_buf *buf)
  896. {
  897. buf->head[0] = *iov;
  898. buf->tail[0] = empty_iov;
  899. buf->page_len = 0;
  900. buf->buflen = buf->len = iov->iov_len;
  901. }
  902. EXPORT_SYMBOL_GPL(xdr_buf_from_iov);
  903. /**
  904. * xdr_buf_subsegment - set subbuf to a portion of buf
  905. * @buf: an xdr buffer
  906. * @subbuf: the result buffer
  907. * @base: beginning of range in bytes
  908. * @len: length of range in bytes
  909. *
  910. * sets @subbuf to an xdr buffer representing the portion of @buf of
  911. * length @len starting at offset @base.
  912. *
  913. * @buf and @subbuf may be pointers to the same struct xdr_buf.
  914. *
  915. * Returns -1 if base of length are out of bounds.
  916. */
  917. int
  918. xdr_buf_subsegment(struct xdr_buf *buf, struct xdr_buf *subbuf,
  919. unsigned int base, unsigned int len)
  920. {
  921. subbuf->buflen = subbuf->len = len;
  922. if (base < buf->head[0].iov_len) {
  923. subbuf->head[0].iov_base = buf->head[0].iov_base + base;
  924. subbuf->head[0].iov_len = min_t(unsigned int, len,
  925. buf->head[0].iov_len - base);
  926. len -= subbuf->head[0].iov_len;
  927. base = 0;
  928. } else {
  929. base -= buf->head[0].iov_len;
  930. subbuf->head[0].iov_len = 0;
  931. }
  932. if (base < buf->page_len) {
  933. subbuf->page_len = min(buf->page_len - base, len);
  934. base += buf->page_base;
  935. subbuf->page_base = base & ~PAGE_CACHE_MASK;
  936. subbuf->pages = &buf->pages[base >> PAGE_CACHE_SHIFT];
  937. len -= subbuf->page_len;
  938. base = 0;
  939. } else {
  940. base -= buf->page_len;
  941. subbuf->page_len = 0;
  942. }
  943. if (base < buf->tail[0].iov_len) {
  944. subbuf->tail[0].iov_base = buf->tail[0].iov_base + base;
  945. subbuf->tail[0].iov_len = min_t(unsigned int, len,
  946. buf->tail[0].iov_len - base);
  947. len -= subbuf->tail[0].iov_len;
  948. base = 0;
  949. } else {
  950. base -= buf->tail[0].iov_len;
  951. subbuf->tail[0].iov_len = 0;
  952. }
  953. if (base || len)
  954. return -1;
  955. return 0;
  956. }
  957. EXPORT_SYMBOL_GPL(xdr_buf_subsegment);
  958. /**
  959. * xdr_buf_trim - lop at most "len" bytes off the end of "buf"
  960. * @buf: buf to be trimmed
  961. * @len: number of bytes to reduce "buf" by
  962. *
  963. * Trim an xdr_buf by the given number of bytes by fixing up the lengths. Note
  964. * that it's possible that we'll trim less than that amount if the xdr_buf is
  965. * too small, or if (for instance) it's all in the head and the parser has
  966. * already read too far into it.
  967. */
  968. void xdr_buf_trim(struct xdr_buf *buf, unsigned int len)
  969. {
  970. size_t cur;
  971. unsigned int trim = len;
  972. if (buf->tail[0].iov_len) {
  973. cur = min_t(size_t, buf->tail[0].iov_len, trim);
  974. buf->tail[0].iov_len -= cur;
  975. trim -= cur;
  976. if (!trim)
  977. goto fix_len;
  978. }
  979. if (buf->page_len) {
  980. cur = min_t(unsigned int, buf->page_len, trim);
  981. buf->page_len -= cur;
  982. trim -= cur;
  983. if (!trim)
  984. goto fix_len;
  985. }
  986. if (buf->head[0].iov_len) {
  987. cur = min_t(size_t, buf->head[0].iov_len, trim);
  988. buf->head[0].iov_len -= cur;
  989. trim -= cur;
  990. }
  991. fix_len:
  992. buf->len -= (len - trim);
  993. }
  994. EXPORT_SYMBOL_GPL(xdr_buf_trim);
  995. static void __read_bytes_from_xdr_buf(struct xdr_buf *subbuf, void *obj, unsigned int len)
  996. {
  997. unsigned int this_len;
  998. this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
  999. memcpy(obj, subbuf->head[0].iov_base, this_len);
  1000. len -= this_len;
  1001. obj += this_len;
  1002. this_len = min_t(unsigned int, len, subbuf->page_len);
  1003. if (this_len)
  1004. _copy_from_pages(obj, subbuf->pages, subbuf->page_base, this_len);
  1005. len -= this_len;
  1006. obj += this_len;
  1007. this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
  1008. memcpy(obj, subbuf->tail[0].iov_base, this_len);
  1009. }
  1010. /* obj is assumed to point to allocated memory of size at least len: */
  1011. int read_bytes_from_xdr_buf(struct xdr_buf *buf, unsigned int base, void *obj, unsigned int len)
  1012. {
  1013. struct xdr_buf subbuf;
  1014. int status;
  1015. status = xdr_buf_subsegment(buf, &subbuf, base, len);
  1016. if (status != 0)
  1017. return status;
  1018. __read_bytes_from_xdr_buf(&subbuf, obj, len);
  1019. return 0;
  1020. }
  1021. EXPORT_SYMBOL_GPL(read_bytes_from_xdr_buf);
  1022. static void __write_bytes_to_xdr_buf(struct xdr_buf *subbuf, void *obj, unsigned int len)
  1023. {
  1024. unsigned int this_len;
  1025. this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
  1026. memcpy(subbuf->head[0].iov_base, obj, this_len);
  1027. len -= this_len;
  1028. obj += this_len;
  1029. this_len = min_t(unsigned int, len, subbuf->page_len);
  1030. if (this_len)
  1031. _copy_to_pages(subbuf->pages, subbuf->page_base, obj, this_len);
  1032. len -= this_len;
  1033. obj += this_len;
  1034. this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
  1035. memcpy(subbuf->tail[0].iov_base, obj, this_len);
  1036. }
  1037. /* obj is assumed to point to allocated memory of size at least len: */
  1038. int write_bytes_to_xdr_buf(struct xdr_buf *buf, unsigned int base, void *obj, unsigned int len)
  1039. {
  1040. struct xdr_buf subbuf;
  1041. int status;
  1042. status = xdr_buf_subsegment(buf, &subbuf, base, len);
  1043. if (status != 0)
  1044. return status;
  1045. __write_bytes_to_xdr_buf(&subbuf, obj, len);
  1046. return 0;
  1047. }
  1048. EXPORT_SYMBOL_GPL(write_bytes_to_xdr_buf);
  1049. int
  1050. xdr_decode_word(struct xdr_buf *buf, unsigned int base, u32 *obj)
  1051. {
  1052. __be32 raw;
  1053. int status;
  1054. status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
  1055. if (status)
  1056. return status;
  1057. *obj = be32_to_cpu(raw);
  1058. return 0;
  1059. }
  1060. EXPORT_SYMBOL_GPL(xdr_decode_word);
  1061. int
  1062. xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj)
  1063. {
  1064. __be32 raw = cpu_to_be32(obj);
  1065. return write_bytes_to_xdr_buf(buf, base, &raw, sizeof(obj));
  1066. }
  1067. EXPORT_SYMBOL_GPL(xdr_encode_word);
  1068. /* If the netobj starting offset bytes from the start of xdr_buf is contained
  1069. * entirely in the head or the tail, set object to point to it; otherwise
  1070. * try to find space for it at the end of the tail, copy it there, and
  1071. * set obj to point to it. */
  1072. int xdr_buf_read_netobj(struct xdr_buf *buf, struct xdr_netobj *obj, unsigned int offset)
  1073. {
  1074. struct xdr_buf subbuf;
  1075. if (xdr_decode_word(buf, offset, &obj->len))
  1076. return -EFAULT;
  1077. if (xdr_buf_subsegment(buf, &subbuf, offset + 4, obj->len))
  1078. return -EFAULT;
  1079. /* Is the obj contained entirely in the head? */
  1080. obj->data = subbuf.head[0].iov_base;
  1081. if (subbuf.head[0].iov_len == obj->len)
  1082. return 0;
  1083. /* ..or is the obj contained entirely in the tail? */
  1084. obj->data = subbuf.tail[0].iov_base;
  1085. if (subbuf.tail[0].iov_len == obj->len)
  1086. return 0;
  1087. /* use end of tail as storage for obj:
  1088. * (We don't copy to the beginning because then we'd have
  1089. * to worry about doing a potentially overlapping copy.
  1090. * This assumes the object is at most half the length of the
  1091. * tail.) */
  1092. if (obj->len > buf->buflen - buf->len)
  1093. return -ENOMEM;
  1094. if (buf->tail[0].iov_len != 0)
  1095. obj->data = buf->tail[0].iov_base + buf->tail[0].iov_len;
  1096. else
  1097. obj->data = buf->head[0].iov_base + buf->head[0].iov_len;
  1098. __read_bytes_from_xdr_buf(&subbuf, obj->data, obj->len);
  1099. return 0;
  1100. }
  1101. EXPORT_SYMBOL_GPL(xdr_buf_read_netobj);
  1102. /* Returns 0 on success, or else a negative error code. */
  1103. static int
  1104. xdr_xcode_array2(struct xdr_buf *buf, unsigned int base,
  1105. struct xdr_array2_desc *desc, int encode)
  1106. {
  1107. char *elem = NULL, *c;
  1108. unsigned int copied = 0, todo, avail_here;
  1109. struct page **ppages = NULL;
  1110. int err;
  1111. if (encode) {
  1112. if (xdr_encode_word(buf, base, desc->array_len) != 0)
  1113. return -EINVAL;
  1114. } else {
  1115. if (xdr_decode_word(buf, base, &desc->array_len) != 0 ||
  1116. desc->array_len > desc->array_maxlen ||
  1117. (unsigned long) base + 4 + desc->array_len *
  1118. desc->elem_size > buf->len)
  1119. return -EINVAL;
  1120. }
  1121. base += 4;
  1122. if (!desc->xcode)
  1123. return 0;
  1124. todo = desc->array_len * desc->elem_size;
  1125. /* process head */
  1126. if (todo && base < buf->head->iov_len) {
  1127. c = buf->head->iov_base + base;
  1128. avail_here = min_t(unsigned int, todo,
  1129. buf->head->iov_len - base);
  1130. todo -= avail_here;
  1131. while (avail_here >= desc->elem_size) {
  1132. err = desc->xcode(desc, c);
  1133. if (err)
  1134. goto out;
  1135. c += desc->elem_size;
  1136. avail_here -= desc->elem_size;
  1137. }
  1138. if (avail_here) {
  1139. if (!elem) {
  1140. elem = kmalloc(desc->elem_size, GFP_KERNEL);
  1141. err = -ENOMEM;
  1142. if (!elem)
  1143. goto out;
  1144. }
  1145. if (encode) {
  1146. err = desc->xcode(desc, elem);
  1147. if (err)
  1148. goto out;
  1149. memcpy(c, elem, avail_here);
  1150. } else
  1151. memcpy(elem, c, avail_here);
  1152. copied = avail_here;
  1153. }
  1154. base = buf->head->iov_len; /* align to start of pages */
  1155. }
  1156. /* process pages array */
  1157. base -= buf->head->iov_len;
  1158. if (todo && base < buf->page_len) {
  1159. unsigned int avail_page;
  1160. avail_here = min(todo, buf->page_len - base);
  1161. todo -= avail_here;
  1162. base += buf->page_base;
  1163. ppages = buf->pages + (base >> PAGE_CACHE_SHIFT);
  1164. base &= ~PAGE_CACHE_MASK;
  1165. avail_page = min_t(unsigned int, PAGE_CACHE_SIZE - base,
  1166. avail_here);
  1167. c = kmap(*ppages) + base;
  1168. while (avail_here) {
  1169. avail_here -= avail_page;
  1170. if (copied || avail_page < desc->elem_size) {
  1171. unsigned int l = min(avail_page,
  1172. desc->elem_size - copied);
  1173. if (!elem) {
  1174. elem = kmalloc(desc->elem_size,
  1175. GFP_KERNEL);
  1176. err = -ENOMEM;
  1177. if (!elem)
  1178. goto out;
  1179. }
  1180. if (encode) {
  1181. if (!copied) {
  1182. err = desc->xcode(desc, elem);
  1183. if (err)
  1184. goto out;
  1185. }
  1186. memcpy(c, elem + copied, l);
  1187. copied += l;
  1188. if (copied == desc->elem_size)
  1189. copied = 0;
  1190. } else {
  1191. memcpy(elem + copied, c, l);
  1192. copied += l;
  1193. if (copied == desc->elem_size) {
  1194. err = desc->xcode(desc, elem);
  1195. if (err)
  1196. goto out;
  1197. copied = 0;
  1198. }
  1199. }
  1200. avail_page -= l;
  1201. c += l;
  1202. }
  1203. while (avail_page >= desc->elem_size) {
  1204. err = desc->xcode(desc, c);
  1205. if (err)
  1206. goto out;
  1207. c += desc->elem_size;
  1208. avail_page -= desc->elem_size;
  1209. }
  1210. if (avail_page) {
  1211. unsigned int l = min(avail_page,
  1212. desc->elem_size - copied);
  1213. if (!elem) {
  1214. elem = kmalloc(desc->elem_size,
  1215. GFP_KERNEL);
  1216. err = -ENOMEM;
  1217. if (!elem)
  1218. goto out;
  1219. }
  1220. if (encode) {
  1221. if (!copied) {
  1222. err = desc->xcode(desc, elem);
  1223. if (err)
  1224. goto out;
  1225. }
  1226. memcpy(c, elem + copied, l);
  1227. copied += l;
  1228. if (copied == desc->elem_size)
  1229. copied = 0;
  1230. } else {
  1231. memcpy(elem + copied, c, l);
  1232. copied += l;
  1233. if (copied == desc->elem_size) {
  1234. err = desc->xcode(desc, elem);
  1235. if (err)
  1236. goto out;
  1237. copied = 0;
  1238. }
  1239. }
  1240. }
  1241. if (avail_here) {
  1242. kunmap(*ppages);
  1243. ppages++;
  1244. c = kmap(*ppages);
  1245. }
  1246. avail_page = min(avail_here,
  1247. (unsigned int) PAGE_CACHE_SIZE);
  1248. }
  1249. base = buf->page_len; /* align to start of tail */
  1250. }
  1251. /* process tail */
  1252. base -= buf->page_len;
  1253. if (todo) {
  1254. c = buf->tail->iov_base + base;
  1255. if (copied) {
  1256. unsigned int l = desc->elem_size - copied;
  1257. if (encode)
  1258. memcpy(c, elem + copied, l);
  1259. else {
  1260. memcpy(elem + copied, c, l);
  1261. err = desc->xcode(desc, elem);
  1262. if (err)
  1263. goto out;
  1264. }
  1265. todo -= l;
  1266. c += l;
  1267. }
  1268. while (todo) {
  1269. err = desc->xcode(desc, c);
  1270. if (err)
  1271. goto out;
  1272. c += desc->elem_size;
  1273. todo -= desc->elem_size;
  1274. }
  1275. }
  1276. err = 0;
  1277. out:
  1278. kfree(elem);
  1279. if (ppages)
  1280. kunmap(*ppages);
  1281. return err;
  1282. }
  1283. int
  1284. xdr_decode_array2(struct xdr_buf *buf, unsigned int base,
  1285. struct xdr_array2_desc *desc)
  1286. {
  1287. if (base >= buf->len)
  1288. return -EINVAL;
  1289. return xdr_xcode_array2(buf, base, desc, 0);
  1290. }
  1291. EXPORT_SYMBOL_GPL(xdr_decode_array2);
  1292. int
  1293. xdr_encode_array2(struct xdr_buf *buf, unsigned int base,
  1294. struct xdr_array2_desc *desc)
  1295. {
  1296. if ((unsigned long) base + 4 + desc->array_len * desc->elem_size >
  1297. buf->head->iov_len + buf->page_len + buf->tail->iov_len)
  1298. return -EINVAL;
  1299. return xdr_xcode_array2(buf, base, desc, 1);
  1300. }
  1301. EXPORT_SYMBOL_GPL(xdr_encode_array2);
  1302. int
  1303. xdr_process_buf(struct xdr_buf *buf, unsigned int offset, unsigned int len,
  1304. int (*actor)(struct scatterlist *, void *), void *data)
  1305. {
  1306. int i, ret = 0;
  1307. unsigned int page_len, thislen, page_offset;
  1308. struct scatterlist sg[1];
  1309. sg_init_table(sg, 1);
  1310. if (offset >= buf->head[0].iov_len) {
  1311. offset -= buf->head[0].iov_len;
  1312. } else {
  1313. thislen = buf->head[0].iov_len - offset;
  1314. if (thislen > len)
  1315. thislen = len;
  1316. sg_set_buf(sg, buf->head[0].iov_base + offset, thislen);
  1317. ret = actor(sg, data);
  1318. if (ret)
  1319. goto out;
  1320. offset = 0;
  1321. len -= thislen;
  1322. }
  1323. if (len == 0)
  1324. goto out;
  1325. if (offset >= buf->page_len) {
  1326. offset -= buf->page_len;
  1327. } else {
  1328. page_len = buf->page_len - offset;
  1329. if (page_len > len)
  1330. page_len = len;
  1331. len -= page_len;
  1332. page_offset = (offset + buf->page_base) & (PAGE_CACHE_SIZE - 1);
  1333. i = (offset + buf->page_base) >> PAGE_CACHE_SHIFT;
  1334. thislen = PAGE_CACHE_SIZE - page_offset;
  1335. do {
  1336. if (thislen > page_len)
  1337. thislen = page_len;
  1338. sg_set_page(sg, buf->pages[i], thislen, page_offset);
  1339. ret = actor(sg, data);
  1340. if (ret)
  1341. goto out;
  1342. page_len -= thislen;
  1343. i++;
  1344. page_offset = 0;
  1345. thislen = PAGE_CACHE_SIZE;
  1346. } while (page_len != 0);
  1347. offset = 0;
  1348. }
  1349. if (len == 0)
  1350. goto out;
  1351. if (offset < buf->tail[0].iov_len) {
  1352. thislen = buf->tail[0].iov_len - offset;
  1353. if (thislen > len)
  1354. thislen = len;
  1355. sg_set_buf(sg, buf->tail[0].iov_base + offset, thislen);
  1356. ret = actor(sg, data);
  1357. len -= thislen;
  1358. }
  1359. if (len != 0)
  1360. ret = -EINVAL;
  1361. out:
  1362. return ret;
  1363. }
  1364. EXPORT_SYMBOL_GPL(xdr_process_buf);