xdr.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636
  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_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_SHIFT);
  162. pgfrom = pages + (pgfrom_base >> PAGE_SHIFT);
  163. pgto_base &= ~PAGE_MASK;
  164. pgfrom_base &= ~PAGE_MASK;
  165. do {
  166. /* Are any pointers crossing a page boundary? */
  167. if (pgto_base == 0) {
  168. pgto_base = PAGE_SIZE;
  169. pgto--;
  170. }
  171. if (pgfrom_base == 0) {
  172. pgfrom_base = PAGE_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_SHIFT);
  210. pgbase &= ~PAGE_MASK;
  211. for (;;) {
  212. copy = PAGE_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_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_SHIFT);
  248. pgbase &= ~PAGE_MASK;
  249. do {
  250. copy = PAGE_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_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->nwords << 2);
  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->nwords << 2);
  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. else
  714. xdr_set_iov(xdr, buf->head, buf->len);
  715. if (p != NULL && p > xdr->p && xdr->end >= p) {
  716. xdr->nwords -= p - xdr->p;
  717. xdr->p = p;
  718. }
  719. }
  720. EXPORT_SYMBOL_GPL(xdr_init_decode);
  721. /**
  722. * xdr_init_decode_pages - Initialize an xdr_stream for decoding into pages
  723. * @xdr: pointer to xdr_stream struct
  724. * @buf: pointer to XDR buffer from which to decode data
  725. * @pages: list of pages to decode into
  726. * @len: length in bytes of buffer in pages
  727. */
  728. void xdr_init_decode_pages(struct xdr_stream *xdr, struct xdr_buf *buf,
  729. struct page **pages, unsigned int len)
  730. {
  731. memset(buf, 0, sizeof(*buf));
  732. buf->pages = pages;
  733. buf->page_len = len;
  734. buf->buflen = len;
  735. buf->len = len;
  736. xdr_init_decode(xdr, buf, NULL);
  737. }
  738. EXPORT_SYMBOL_GPL(xdr_init_decode_pages);
  739. static __be32 * __xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
  740. {
  741. unsigned int nwords = XDR_QUADLEN(nbytes);
  742. __be32 *p = xdr->p;
  743. __be32 *q = p + nwords;
  744. if (unlikely(nwords > xdr->nwords || q > xdr->end || q < p))
  745. return NULL;
  746. xdr->p = q;
  747. xdr->nwords -= nwords;
  748. return p;
  749. }
  750. /**
  751. * xdr_set_scratch_buffer - Attach a scratch buffer for decoding data.
  752. * @xdr: pointer to xdr_stream struct
  753. * @buf: pointer to an empty buffer
  754. * @buflen: size of 'buf'
  755. *
  756. * The scratch buffer is used when decoding from an array of pages.
  757. * If an xdr_inline_decode() call spans across page boundaries, then
  758. * we copy the data into the scratch buffer in order to allow linear
  759. * access.
  760. */
  761. void xdr_set_scratch_buffer(struct xdr_stream *xdr, void *buf, size_t buflen)
  762. {
  763. xdr->scratch.iov_base = buf;
  764. xdr->scratch.iov_len = buflen;
  765. }
  766. EXPORT_SYMBOL_GPL(xdr_set_scratch_buffer);
  767. static __be32 *xdr_copy_to_scratch(struct xdr_stream *xdr, size_t nbytes)
  768. {
  769. __be32 *p;
  770. char *cpdest = xdr->scratch.iov_base;
  771. size_t cplen = (char *)xdr->end - (char *)xdr->p;
  772. if (nbytes > xdr->scratch.iov_len)
  773. return NULL;
  774. p = __xdr_inline_decode(xdr, cplen);
  775. if (p == NULL)
  776. return NULL;
  777. memcpy(cpdest, p, cplen);
  778. cpdest += cplen;
  779. nbytes -= cplen;
  780. if (!xdr_set_next_buffer(xdr))
  781. return NULL;
  782. p = __xdr_inline_decode(xdr, nbytes);
  783. if (p == NULL)
  784. return NULL;
  785. memcpy(cpdest, p, nbytes);
  786. return xdr->scratch.iov_base;
  787. }
  788. /**
  789. * xdr_inline_decode - Retrieve XDR data to decode
  790. * @xdr: pointer to xdr_stream struct
  791. * @nbytes: number of bytes of data to decode
  792. *
  793. * Check if the input buffer is long enough to enable us to decode
  794. * 'nbytes' more bytes of data starting at the current position.
  795. * If so return the current pointer, then update the current
  796. * pointer position.
  797. */
  798. __be32 * xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
  799. {
  800. __be32 *p;
  801. if (nbytes == 0)
  802. return xdr->p;
  803. if (xdr->p == xdr->end && !xdr_set_next_buffer(xdr))
  804. return NULL;
  805. p = __xdr_inline_decode(xdr, nbytes);
  806. if (p != NULL)
  807. return p;
  808. return xdr_copy_to_scratch(xdr, nbytes);
  809. }
  810. EXPORT_SYMBOL_GPL(xdr_inline_decode);
  811. static unsigned int xdr_align_pages(struct xdr_stream *xdr, unsigned int len)
  812. {
  813. struct xdr_buf *buf = xdr->buf;
  814. struct kvec *iov;
  815. unsigned int nwords = XDR_QUADLEN(len);
  816. unsigned int cur = xdr_stream_pos(xdr);
  817. if (xdr->nwords == 0)
  818. return 0;
  819. /* Realign pages to current pointer position */
  820. iov = buf->head;
  821. if (iov->iov_len > cur) {
  822. xdr_shrink_bufhead(buf, iov->iov_len - cur);
  823. xdr->nwords = XDR_QUADLEN(buf->len - cur);
  824. }
  825. if (nwords > xdr->nwords) {
  826. nwords = xdr->nwords;
  827. len = nwords << 2;
  828. }
  829. if (buf->page_len <= len)
  830. len = buf->page_len;
  831. else if (nwords < xdr->nwords) {
  832. /* Truncate page data and move it into the tail */
  833. xdr_shrink_pagelen(buf, buf->page_len - len);
  834. xdr->nwords = XDR_QUADLEN(buf->len - cur);
  835. }
  836. return len;
  837. }
  838. /**
  839. * xdr_read_pages - Ensure page-based XDR data to decode is aligned at current pointer position
  840. * @xdr: pointer to xdr_stream struct
  841. * @len: number of bytes of page data
  842. *
  843. * Moves data beyond the current pointer position from the XDR head[] buffer
  844. * into the page list. Any data that lies beyond current position + "len"
  845. * bytes is moved into the XDR tail[].
  846. *
  847. * Returns the number of XDR encoded bytes now contained in the pages
  848. */
  849. unsigned int xdr_read_pages(struct xdr_stream *xdr, unsigned int len)
  850. {
  851. struct xdr_buf *buf = xdr->buf;
  852. struct kvec *iov;
  853. unsigned int nwords;
  854. unsigned int end;
  855. unsigned int padding;
  856. len = xdr_align_pages(xdr, len);
  857. if (len == 0)
  858. return 0;
  859. nwords = XDR_QUADLEN(len);
  860. padding = (nwords << 2) - len;
  861. xdr->iov = iov = buf->tail;
  862. /* Compute remaining message length. */
  863. end = ((xdr->nwords - nwords) << 2) + padding;
  864. if (end > iov->iov_len)
  865. end = iov->iov_len;
  866. /*
  867. * Position current pointer at beginning of tail, and
  868. * set remaining message length.
  869. */
  870. xdr->p = (__be32 *)((char *)iov->iov_base + padding);
  871. xdr->end = (__be32 *)((char *)iov->iov_base + end);
  872. xdr->page_ptr = NULL;
  873. xdr->nwords = XDR_QUADLEN(end - padding);
  874. return len;
  875. }
  876. EXPORT_SYMBOL_GPL(xdr_read_pages);
  877. /**
  878. * xdr_enter_page - decode data from the XDR page
  879. * @xdr: pointer to xdr_stream struct
  880. * @len: number of bytes of page data
  881. *
  882. * Moves data beyond the current pointer position from the XDR head[] buffer
  883. * into the page list. Any data that lies beyond current position + "len"
  884. * bytes is moved into the XDR tail[]. The current pointer is then
  885. * repositioned at the beginning of the first XDR page.
  886. */
  887. void xdr_enter_page(struct xdr_stream *xdr, unsigned int len)
  888. {
  889. len = xdr_align_pages(xdr, len);
  890. /*
  891. * Position current pointer at beginning of tail, and
  892. * set remaining message length.
  893. */
  894. if (len != 0)
  895. xdr_set_page_base(xdr, 0, len);
  896. }
  897. EXPORT_SYMBOL_GPL(xdr_enter_page);
  898. static struct kvec empty_iov = {.iov_base = NULL, .iov_len = 0};
  899. void
  900. xdr_buf_from_iov(struct kvec *iov, struct xdr_buf *buf)
  901. {
  902. buf->head[0] = *iov;
  903. buf->tail[0] = empty_iov;
  904. buf->page_len = 0;
  905. buf->buflen = buf->len = iov->iov_len;
  906. }
  907. EXPORT_SYMBOL_GPL(xdr_buf_from_iov);
  908. /**
  909. * xdr_buf_subsegment - set subbuf to a portion of buf
  910. * @buf: an xdr buffer
  911. * @subbuf: the result buffer
  912. * @base: beginning of range in bytes
  913. * @len: length of range in bytes
  914. *
  915. * sets @subbuf to an xdr buffer representing the portion of @buf of
  916. * length @len starting at offset @base.
  917. *
  918. * @buf and @subbuf may be pointers to the same struct xdr_buf.
  919. *
  920. * Returns -1 if base of length are out of bounds.
  921. */
  922. int
  923. xdr_buf_subsegment(struct xdr_buf *buf, struct xdr_buf *subbuf,
  924. unsigned int base, unsigned int len)
  925. {
  926. subbuf->buflen = subbuf->len = len;
  927. if (base < buf->head[0].iov_len) {
  928. subbuf->head[0].iov_base = buf->head[0].iov_base + base;
  929. subbuf->head[0].iov_len = min_t(unsigned int, len,
  930. buf->head[0].iov_len - base);
  931. len -= subbuf->head[0].iov_len;
  932. base = 0;
  933. } else {
  934. base -= buf->head[0].iov_len;
  935. subbuf->head[0].iov_len = 0;
  936. }
  937. if (base < buf->page_len) {
  938. subbuf->page_len = min(buf->page_len - base, len);
  939. base += buf->page_base;
  940. subbuf->page_base = base & ~PAGE_MASK;
  941. subbuf->pages = &buf->pages[base >> PAGE_SHIFT];
  942. len -= subbuf->page_len;
  943. base = 0;
  944. } else {
  945. base -= buf->page_len;
  946. subbuf->page_len = 0;
  947. }
  948. if (base < buf->tail[0].iov_len) {
  949. subbuf->tail[0].iov_base = buf->tail[0].iov_base + base;
  950. subbuf->tail[0].iov_len = min_t(unsigned int, len,
  951. buf->tail[0].iov_len - base);
  952. len -= subbuf->tail[0].iov_len;
  953. base = 0;
  954. } else {
  955. base -= buf->tail[0].iov_len;
  956. subbuf->tail[0].iov_len = 0;
  957. }
  958. if (base || len)
  959. return -1;
  960. return 0;
  961. }
  962. EXPORT_SYMBOL_GPL(xdr_buf_subsegment);
  963. /**
  964. * xdr_buf_trim - lop at most "len" bytes off the end of "buf"
  965. * @buf: buf to be trimmed
  966. * @len: number of bytes to reduce "buf" by
  967. *
  968. * Trim an xdr_buf by the given number of bytes by fixing up the lengths. Note
  969. * that it's possible that we'll trim less than that amount if the xdr_buf is
  970. * too small, or if (for instance) it's all in the head and the parser has
  971. * already read too far into it.
  972. */
  973. void xdr_buf_trim(struct xdr_buf *buf, unsigned int len)
  974. {
  975. size_t cur;
  976. unsigned int trim = len;
  977. if (buf->tail[0].iov_len) {
  978. cur = min_t(size_t, buf->tail[0].iov_len, trim);
  979. buf->tail[0].iov_len -= cur;
  980. trim -= cur;
  981. if (!trim)
  982. goto fix_len;
  983. }
  984. if (buf->page_len) {
  985. cur = min_t(unsigned int, buf->page_len, trim);
  986. buf->page_len -= cur;
  987. trim -= cur;
  988. if (!trim)
  989. goto fix_len;
  990. }
  991. if (buf->head[0].iov_len) {
  992. cur = min_t(size_t, buf->head[0].iov_len, trim);
  993. buf->head[0].iov_len -= cur;
  994. trim -= cur;
  995. }
  996. fix_len:
  997. buf->len -= (len - trim);
  998. }
  999. EXPORT_SYMBOL_GPL(xdr_buf_trim);
  1000. static void __read_bytes_from_xdr_buf(struct xdr_buf *subbuf, void *obj, unsigned int len)
  1001. {
  1002. unsigned int this_len;
  1003. this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
  1004. memcpy(obj, subbuf->head[0].iov_base, this_len);
  1005. len -= this_len;
  1006. obj += this_len;
  1007. this_len = min_t(unsigned int, len, subbuf->page_len);
  1008. if (this_len)
  1009. _copy_from_pages(obj, subbuf->pages, subbuf->page_base, this_len);
  1010. len -= this_len;
  1011. obj += this_len;
  1012. this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
  1013. memcpy(obj, subbuf->tail[0].iov_base, this_len);
  1014. }
  1015. /* obj is assumed to point to allocated memory of size at least len: */
  1016. int read_bytes_from_xdr_buf(struct xdr_buf *buf, unsigned int base, void *obj, unsigned int len)
  1017. {
  1018. struct xdr_buf subbuf;
  1019. int status;
  1020. status = xdr_buf_subsegment(buf, &subbuf, base, len);
  1021. if (status != 0)
  1022. return status;
  1023. __read_bytes_from_xdr_buf(&subbuf, obj, len);
  1024. return 0;
  1025. }
  1026. EXPORT_SYMBOL_GPL(read_bytes_from_xdr_buf);
  1027. static void __write_bytes_to_xdr_buf(struct xdr_buf *subbuf, void *obj, unsigned int len)
  1028. {
  1029. unsigned int this_len;
  1030. this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
  1031. memcpy(subbuf->head[0].iov_base, obj, this_len);
  1032. len -= this_len;
  1033. obj += this_len;
  1034. this_len = min_t(unsigned int, len, subbuf->page_len);
  1035. if (this_len)
  1036. _copy_to_pages(subbuf->pages, subbuf->page_base, obj, this_len);
  1037. len -= this_len;
  1038. obj += this_len;
  1039. this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
  1040. memcpy(subbuf->tail[0].iov_base, obj, this_len);
  1041. }
  1042. /* obj is assumed to point to allocated memory of size at least len: */
  1043. int write_bytes_to_xdr_buf(struct xdr_buf *buf, unsigned int base, void *obj, unsigned int len)
  1044. {
  1045. struct xdr_buf subbuf;
  1046. int status;
  1047. status = xdr_buf_subsegment(buf, &subbuf, base, len);
  1048. if (status != 0)
  1049. return status;
  1050. __write_bytes_to_xdr_buf(&subbuf, obj, len);
  1051. return 0;
  1052. }
  1053. EXPORT_SYMBOL_GPL(write_bytes_to_xdr_buf);
  1054. int
  1055. xdr_decode_word(struct xdr_buf *buf, unsigned int base, u32 *obj)
  1056. {
  1057. __be32 raw;
  1058. int status;
  1059. status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
  1060. if (status)
  1061. return status;
  1062. *obj = be32_to_cpu(raw);
  1063. return 0;
  1064. }
  1065. EXPORT_SYMBOL_GPL(xdr_decode_word);
  1066. int
  1067. xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj)
  1068. {
  1069. __be32 raw = cpu_to_be32(obj);
  1070. return write_bytes_to_xdr_buf(buf, base, &raw, sizeof(obj));
  1071. }
  1072. EXPORT_SYMBOL_GPL(xdr_encode_word);
  1073. /* If the netobj starting offset bytes from the start of xdr_buf is contained
  1074. * entirely in the head or the tail, set object to point to it; otherwise
  1075. * try to find space for it at the end of the tail, copy it there, and
  1076. * set obj to point to it. */
  1077. int xdr_buf_read_netobj(struct xdr_buf *buf, struct xdr_netobj *obj, unsigned int offset)
  1078. {
  1079. struct xdr_buf subbuf;
  1080. if (xdr_decode_word(buf, offset, &obj->len))
  1081. return -EFAULT;
  1082. if (xdr_buf_subsegment(buf, &subbuf, offset + 4, obj->len))
  1083. return -EFAULT;
  1084. /* Is the obj contained entirely in the head? */
  1085. obj->data = subbuf.head[0].iov_base;
  1086. if (subbuf.head[0].iov_len == obj->len)
  1087. return 0;
  1088. /* ..or is the obj contained entirely in the tail? */
  1089. obj->data = subbuf.tail[0].iov_base;
  1090. if (subbuf.tail[0].iov_len == obj->len)
  1091. return 0;
  1092. /* use end of tail as storage for obj:
  1093. * (We don't copy to the beginning because then we'd have
  1094. * to worry about doing a potentially overlapping copy.
  1095. * This assumes the object is at most half the length of the
  1096. * tail.) */
  1097. if (obj->len > buf->buflen - buf->len)
  1098. return -ENOMEM;
  1099. if (buf->tail[0].iov_len != 0)
  1100. obj->data = buf->tail[0].iov_base + buf->tail[0].iov_len;
  1101. else
  1102. obj->data = buf->head[0].iov_base + buf->head[0].iov_len;
  1103. __read_bytes_from_xdr_buf(&subbuf, obj->data, obj->len);
  1104. return 0;
  1105. }
  1106. EXPORT_SYMBOL_GPL(xdr_buf_read_netobj);
  1107. /* Returns 0 on success, or else a negative error code. */
  1108. static int
  1109. xdr_xcode_array2(struct xdr_buf *buf, unsigned int base,
  1110. struct xdr_array2_desc *desc, int encode)
  1111. {
  1112. char *elem = NULL, *c;
  1113. unsigned int copied = 0, todo, avail_here;
  1114. struct page **ppages = NULL;
  1115. int err;
  1116. if (encode) {
  1117. if (xdr_encode_word(buf, base, desc->array_len) != 0)
  1118. return -EINVAL;
  1119. } else {
  1120. if (xdr_decode_word(buf, base, &desc->array_len) != 0 ||
  1121. desc->array_len > desc->array_maxlen ||
  1122. (unsigned long) base + 4 + desc->array_len *
  1123. desc->elem_size > buf->len)
  1124. return -EINVAL;
  1125. }
  1126. base += 4;
  1127. if (!desc->xcode)
  1128. return 0;
  1129. todo = desc->array_len * desc->elem_size;
  1130. /* process head */
  1131. if (todo && base < buf->head->iov_len) {
  1132. c = buf->head->iov_base + base;
  1133. avail_here = min_t(unsigned int, todo,
  1134. buf->head->iov_len - base);
  1135. todo -= avail_here;
  1136. while (avail_here >= desc->elem_size) {
  1137. err = desc->xcode(desc, c);
  1138. if (err)
  1139. goto out;
  1140. c += desc->elem_size;
  1141. avail_here -= desc->elem_size;
  1142. }
  1143. if (avail_here) {
  1144. if (!elem) {
  1145. elem = kmalloc(desc->elem_size, GFP_KERNEL);
  1146. err = -ENOMEM;
  1147. if (!elem)
  1148. goto out;
  1149. }
  1150. if (encode) {
  1151. err = desc->xcode(desc, elem);
  1152. if (err)
  1153. goto out;
  1154. memcpy(c, elem, avail_here);
  1155. } else
  1156. memcpy(elem, c, avail_here);
  1157. copied = avail_here;
  1158. }
  1159. base = buf->head->iov_len; /* align to start of pages */
  1160. }
  1161. /* process pages array */
  1162. base -= buf->head->iov_len;
  1163. if (todo && base < buf->page_len) {
  1164. unsigned int avail_page;
  1165. avail_here = min(todo, buf->page_len - base);
  1166. todo -= avail_here;
  1167. base += buf->page_base;
  1168. ppages = buf->pages + (base >> PAGE_SHIFT);
  1169. base &= ~PAGE_MASK;
  1170. avail_page = min_t(unsigned int, PAGE_SIZE - base,
  1171. avail_here);
  1172. c = kmap(*ppages) + base;
  1173. while (avail_here) {
  1174. avail_here -= avail_page;
  1175. if (copied || avail_page < desc->elem_size) {
  1176. unsigned int l = min(avail_page,
  1177. desc->elem_size - copied);
  1178. if (!elem) {
  1179. elem = kmalloc(desc->elem_size,
  1180. GFP_KERNEL);
  1181. err = -ENOMEM;
  1182. if (!elem)
  1183. goto out;
  1184. }
  1185. if (encode) {
  1186. if (!copied) {
  1187. err = desc->xcode(desc, elem);
  1188. if (err)
  1189. goto out;
  1190. }
  1191. memcpy(c, elem + copied, l);
  1192. copied += l;
  1193. if (copied == desc->elem_size)
  1194. copied = 0;
  1195. } else {
  1196. memcpy(elem + copied, c, l);
  1197. copied += l;
  1198. if (copied == desc->elem_size) {
  1199. err = desc->xcode(desc, elem);
  1200. if (err)
  1201. goto out;
  1202. copied = 0;
  1203. }
  1204. }
  1205. avail_page -= l;
  1206. c += l;
  1207. }
  1208. while (avail_page >= desc->elem_size) {
  1209. err = desc->xcode(desc, c);
  1210. if (err)
  1211. goto out;
  1212. c += desc->elem_size;
  1213. avail_page -= desc->elem_size;
  1214. }
  1215. if (avail_page) {
  1216. unsigned int l = min(avail_page,
  1217. desc->elem_size - copied);
  1218. if (!elem) {
  1219. elem = kmalloc(desc->elem_size,
  1220. GFP_KERNEL);
  1221. err = -ENOMEM;
  1222. if (!elem)
  1223. goto out;
  1224. }
  1225. if (encode) {
  1226. if (!copied) {
  1227. err = desc->xcode(desc, elem);
  1228. if (err)
  1229. goto out;
  1230. }
  1231. memcpy(c, elem + copied, l);
  1232. copied += l;
  1233. if (copied == desc->elem_size)
  1234. copied = 0;
  1235. } else {
  1236. memcpy(elem + copied, c, l);
  1237. copied += l;
  1238. if (copied == desc->elem_size) {
  1239. err = desc->xcode(desc, elem);
  1240. if (err)
  1241. goto out;
  1242. copied = 0;
  1243. }
  1244. }
  1245. }
  1246. if (avail_here) {
  1247. kunmap(*ppages);
  1248. ppages++;
  1249. c = kmap(*ppages);
  1250. }
  1251. avail_page = min(avail_here,
  1252. (unsigned int) PAGE_SIZE);
  1253. }
  1254. base = buf->page_len; /* align to start of tail */
  1255. }
  1256. /* process tail */
  1257. base -= buf->page_len;
  1258. if (todo) {
  1259. c = buf->tail->iov_base + base;
  1260. if (copied) {
  1261. unsigned int l = desc->elem_size - copied;
  1262. if (encode)
  1263. memcpy(c, elem + copied, l);
  1264. else {
  1265. memcpy(elem + copied, c, l);
  1266. err = desc->xcode(desc, elem);
  1267. if (err)
  1268. goto out;
  1269. }
  1270. todo -= l;
  1271. c += l;
  1272. }
  1273. while (todo) {
  1274. err = desc->xcode(desc, c);
  1275. if (err)
  1276. goto out;
  1277. c += desc->elem_size;
  1278. todo -= desc->elem_size;
  1279. }
  1280. }
  1281. err = 0;
  1282. out:
  1283. kfree(elem);
  1284. if (ppages)
  1285. kunmap(*ppages);
  1286. return err;
  1287. }
  1288. int
  1289. xdr_decode_array2(struct xdr_buf *buf, unsigned int base,
  1290. struct xdr_array2_desc *desc)
  1291. {
  1292. if (base >= buf->len)
  1293. return -EINVAL;
  1294. return xdr_xcode_array2(buf, base, desc, 0);
  1295. }
  1296. EXPORT_SYMBOL_GPL(xdr_decode_array2);
  1297. int
  1298. xdr_encode_array2(struct xdr_buf *buf, unsigned int base,
  1299. struct xdr_array2_desc *desc)
  1300. {
  1301. if ((unsigned long) base + 4 + desc->array_len * desc->elem_size >
  1302. buf->head->iov_len + buf->page_len + buf->tail->iov_len)
  1303. return -EINVAL;
  1304. return xdr_xcode_array2(buf, base, desc, 1);
  1305. }
  1306. EXPORT_SYMBOL_GPL(xdr_encode_array2);
  1307. int
  1308. xdr_process_buf(struct xdr_buf *buf, unsigned int offset, unsigned int len,
  1309. int (*actor)(struct scatterlist *, void *), void *data)
  1310. {
  1311. int i, ret = 0;
  1312. unsigned int page_len, thislen, page_offset;
  1313. struct scatterlist sg[1];
  1314. sg_init_table(sg, 1);
  1315. if (offset >= buf->head[0].iov_len) {
  1316. offset -= buf->head[0].iov_len;
  1317. } else {
  1318. thislen = buf->head[0].iov_len - offset;
  1319. if (thislen > len)
  1320. thislen = len;
  1321. sg_set_buf(sg, buf->head[0].iov_base + offset, thislen);
  1322. ret = actor(sg, data);
  1323. if (ret)
  1324. goto out;
  1325. offset = 0;
  1326. len -= thislen;
  1327. }
  1328. if (len == 0)
  1329. goto out;
  1330. if (offset >= buf->page_len) {
  1331. offset -= buf->page_len;
  1332. } else {
  1333. page_len = buf->page_len - offset;
  1334. if (page_len > len)
  1335. page_len = len;
  1336. len -= page_len;
  1337. page_offset = (offset + buf->page_base) & (PAGE_SIZE - 1);
  1338. i = (offset + buf->page_base) >> PAGE_SHIFT;
  1339. thislen = PAGE_SIZE - page_offset;
  1340. do {
  1341. if (thislen > page_len)
  1342. thislen = page_len;
  1343. sg_set_page(sg, buf->pages[i], thislen, page_offset);
  1344. ret = actor(sg, data);
  1345. if (ret)
  1346. goto out;
  1347. page_len -= thislen;
  1348. i++;
  1349. page_offset = 0;
  1350. thislen = PAGE_SIZE;
  1351. } while (page_len != 0);
  1352. offset = 0;
  1353. }
  1354. if (len == 0)
  1355. goto out;
  1356. if (offset < buf->tail[0].iov_len) {
  1357. thislen = buf->tail[0].iov_len - offset;
  1358. if (thislen > len)
  1359. thislen = len;
  1360. sg_set_buf(sg, buf->tail[0].iov_base + offset, thislen);
  1361. ret = actor(sg, data);
  1362. len -= thislen;
  1363. }
  1364. if (len != 0)
  1365. ret = -EINVAL;
  1366. out:
  1367. return ret;
  1368. }
  1369. EXPORT_SYMBOL_GPL(xdr_process_buf);
  1370. /**
  1371. * xdr_stream_decode_opaque - Decode variable length opaque
  1372. * @xdr: pointer to xdr_stream
  1373. * @ptr: location to store opaque data
  1374. * @size: size of storage buffer @ptr
  1375. *
  1376. * Return values:
  1377. * On success, returns size of object stored in *@ptr
  1378. * %-EBADMSG on XDR buffer overflow
  1379. * %-EMSGSIZE on overflow of storage buffer @ptr
  1380. */
  1381. ssize_t xdr_stream_decode_opaque(struct xdr_stream *xdr, void *ptr, size_t size)
  1382. {
  1383. ssize_t ret;
  1384. void *p;
  1385. ret = xdr_stream_decode_opaque_inline(xdr, &p, size);
  1386. if (ret <= 0)
  1387. return ret;
  1388. memcpy(ptr, p, ret);
  1389. return ret;
  1390. }
  1391. EXPORT_SYMBOL_GPL(xdr_stream_decode_opaque);
  1392. /**
  1393. * xdr_stream_decode_opaque_dup - Decode and duplicate variable length opaque
  1394. * @xdr: pointer to xdr_stream
  1395. * @ptr: location to store pointer to opaque data
  1396. * @maxlen: maximum acceptable object size
  1397. * @gfp_flags: GFP mask to use
  1398. *
  1399. * Return values:
  1400. * On success, returns size of object stored in *@ptr
  1401. * %-EBADMSG on XDR buffer overflow
  1402. * %-EMSGSIZE if the size of the object would exceed @maxlen
  1403. * %-ENOMEM on memory allocation failure
  1404. */
  1405. ssize_t xdr_stream_decode_opaque_dup(struct xdr_stream *xdr, void **ptr,
  1406. size_t maxlen, gfp_t gfp_flags)
  1407. {
  1408. ssize_t ret;
  1409. void *p;
  1410. ret = xdr_stream_decode_opaque_inline(xdr, &p, maxlen);
  1411. if (ret > 0) {
  1412. *ptr = kmemdup(p, ret, gfp_flags);
  1413. if (*ptr != NULL)
  1414. return ret;
  1415. ret = -ENOMEM;
  1416. }
  1417. *ptr = NULL;
  1418. return ret;
  1419. }
  1420. EXPORT_SYMBOL_GPL(xdr_stream_decode_opaque_dup);
  1421. /**
  1422. * xdr_stream_decode_string - Decode variable length string
  1423. * @xdr: pointer to xdr_stream
  1424. * @str: location to store string
  1425. * @size: size of storage buffer @str
  1426. *
  1427. * Return values:
  1428. * On success, returns length of NUL-terminated string stored in *@str
  1429. * %-EBADMSG on XDR buffer overflow
  1430. * %-EMSGSIZE on overflow of storage buffer @str
  1431. */
  1432. ssize_t xdr_stream_decode_string(struct xdr_stream *xdr, char *str, size_t size)
  1433. {
  1434. ssize_t ret;
  1435. void *p;
  1436. ret = xdr_stream_decode_opaque_inline(xdr, &p, size);
  1437. if (ret > 0) {
  1438. memcpy(str, p, ret);
  1439. str[ret] = '\0';
  1440. return strlen(str);
  1441. }
  1442. *str = '\0';
  1443. return ret;
  1444. }
  1445. EXPORT_SYMBOL_GPL(xdr_stream_decode_string);
  1446. /**
  1447. * xdr_stream_decode_string_dup - Decode and duplicate variable length string
  1448. * @xdr: pointer to xdr_stream
  1449. * @str: location to store pointer to string
  1450. * @maxlen: maximum acceptable string length
  1451. * @gfp_flags: GFP mask to use
  1452. *
  1453. * Return values:
  1454. * On success, returns length of NUL-terminated string stored in *@ptr
  1455. * %-EBADMSG on XDR buffer overflow
  1456. * %-EMSGSIZE if the size of the string would exceed @maxlen
  1457. * %-ENOMEM on memory allocation failure
  1458. */
  1459. ssize_t xdr_stream_decode_string_dup(struct xdr_stream *xdr, char **str,
  1460. size_t maxlen, gfp_t gfp_flags)
  1461. {
  1462. void *p;
  1463. ssize_t ret;
  1464. ret = xdr_stream_decode_opaque_inline(xdr, &p, maxlen);
  1465. if (ret > 0) {
  1466. char *s = kmalloc(ret + 1, gfp_flags);
  1467. if (s != NULL) {
  1468. memcpy(s, p, ret);
  1469. s[ret] = '\0';
  1470. *str = s;
  1471. return strlen(s);
  1472. }
  1473. ret = -ENOMEM;
  1474. }
  1475. *str = NULL;
  1476. return ret;
  1477. }
  1478. EXPORT_SYMBOL_GPL(xdr_stream_decode_string_dup);