xdr.c 41 KB

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