xdr.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521
  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. static __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. xdr->page_ptr--;
  577. }
  578. /* (otherwise assume xdr->end is already set) */
  579. head->iov_len = len;
  580. buf->len = len;
  581. xdr->p = head->iov_base + head->iov_len;
  582. xdr->iov = buf->head;
  583. }
  584. EXPORT_SYMBOL(xdr_truncate_encode);
  585. /**
  586. * xdr_restrict_buflen - decrease available buffer space
  587. * @xdr: pointer to xdr_stream
  588. * @newbuflen: new maximum number of bytes available
  589. *
  590. * Adjust our idea of how much space is available in the buffer.
  591. * If we've already used too much space in the buffer, returns -1.
  592. * If the available space is already smaller than newbuflen, returns 0
  593. * and does nothing. Otherwise, adjusts xdr->buf->buflen to newbuflen
  594. * and ensures xdr->end is set at most offset newbuflen from the start
  595. * of the buffer.
  596. */
  597. int xdr_restrict_buflen(struct xdr_stream *xdr, int newbuflen)
  598. {
  599. struct xdr_buf *buf = xdr->buf;
  600. int left_in_this_buf = (void *)xdr->end - (void *)xdr->p;
  601. int end_offset = buf->len + left_in_this_buf;
  602. if (newbuflen < 0 || newbuflen < buf->len)
  603. return -1;
  604. if (newbuflen > buf->buflen)
  605. return 0;
  606. if (newbuflen < end_offset)
  607. xdr->end = (void *)xdr->end + newbuflen - end_offset;
  608. buf->buflen = newbuflen;
  609. return 0;
  610. }
  611. EXPORT_SYMBOL(xdr_restrict_buflen);
  612. /**
  613. * xdr_write_pages - Insert a list of pages into an XDR buffer for sending
  614. * @xdr: pointer to xdr_stream
  615. * @pages: list of pages
  616. * @base: offset of first byte
  617. * @len: length of data in bytes
  618. *
  619. */
  620. void xdr_write_pages(struct xdr_stream *xdr, struct page **pages, unsigned int base,
  621. unsigned int len)
  622. {
  623. struct xdr_buf *buf = xdr->buf;
  624. struct kvec *iov = buf->tail;
  625. buf->pages = pages;
  626. buf->page_base = base;
  627. buf->page_len = len;
  628. iov->iov_base = (char *)xdr->p;
  629. iov->iov_len = 0;
  630. xdr->iov = iov;
  631. if (len & 3) {
  632. unsigned int pad = 4 - (len & 3);
  633. BUG_ON(xdr->p >= xdr->end);
  634. iov->iov_base = (char *)xdr->p + (len & 3);
  635. iov->iov_len += pad;
  636. len += pad;
  637. *xdr->p++ = 0;
  638. }
  639. buf->buflen += len;
  640. buf->len += len;
  641. }
  642. EXPORT_SYMBOL_GPL(xdr_write_pages);
  643. static void xdr_set_iov(struct xdr_stream *xdr, struct kvec *iov,
  644. unsigned int len)
  645. {
  646. if (len > iov->iov_len)
  647. len = iov->iov_len;
  648. xdr->p = (__be32*)iov->iov_base;
  649. xdr->end = (__be32*)(iov->iov_base + len);
  650. xdr->iov = iov;
  651. xdr->page_ptr = NULL;
  652. }
  653. static int xdr_set_page_base(struct xdr_stream *xdr,
  654. unsigned int base, unsigned int len)
  655. {
  656. unsigned int pgnr;
  657. unsigned int maxlen;
  658. unsigned int pgoff;
  659. unsigned int pgend;
  660. void *kaddr;
  661. maxlen = xdr->buf->page_len;
  662. if (base >= maxlen)
  663. return -EINVAL;
  664. maxlen -= base;
  665. if (len > maxlen)
  666. len = maxlen;
  667. base += xdr->buf->page_base;
  668. pgnr = base >> PAGE_SHIFT;
  669. xdr->page_ptr = &xdr->buf->pages[pgnr];
  670. kaddr = page_address(*xdr->page_ptr);
  671. pgoff = base & ~PAGE_MASK;
  672. xdr->p = (__be32*)(kaddr + pgoff);
  673. pgend = pgoff + len;
  674. if (pgend > PAGE_SIZE)
  675. pgend = PAGE_SIZE;
  676. xdr->end = (__be32*)(kaddr + pgend);
  677. xdr->iov = NULL;
  678. return 0;
  679. }
  680. static void xdr_set_next_page(struct xdr_stream *xdr)
  681. {
  682. unsigned int newbase;
  683. newbase = (1 + xdr->page_ptr - xdr->buf->pages) << PAGE_SHIFT;
  684. newbase -= xdr->buf->page_base;
  685. if (xdr_set_page_base(xdr, newbase, PAGE_SIZE) < 0)
  686. xdr_set_iov(xdr, xdr->buf->tail, xdr->nwords << 2);
  687. }
  688. static bool xdr_set_next_buffer(struct xdr_stream *xdr)
  689. {
  690. if (xdr->page_ptr != NULL)
  691. xdr_set_next_page(xdr);
  692. else if (xdr->iov == xdr->buf->head) {
  693. if (xdr_set_page_base(xdr, 0, PAGE_SIZE) < 0)
  694. xdr_set_iov(xdr, xdr->buf->tail, xdr->nwords << 2);
  695. }
  696. return xdr->p != xdr->end;
  697. }
  698. /**
  699. * xdr_init_decode - Initialize an xdr_stream for decoding data.
  700. * @xdr: pointer to xdr_stream struct
  701. * @buf: pointer to XDR buffer from which to decode data
  702. * @p: current pointer inside XDR buffer
  703. */
  704. void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p)
  705. {
  706. xdr->buf = buf;
  707. xdr->scratch.iov_base = NULL;
  708. xdr->scratch.iov_len = 0;
  709. xdr->nwords = XDR_QUADLEN(buf->len);
  710. if (buf->head[0].iov_len != 0)
  711. xdr_set_iov(xdr, buf->head, buf->len);
  712. else if (buf->page_len != 0)
  713. xdr_set_page_base(xdr, 0, buf->len);
  714. else
  715. xdr_set_iov(xdr, buf->head, buf->len);
  716. if (p != NULL && p > xdr->p && xdr->end >= p) {
  717. xdr->nwords -= p - xdr->p;
  718. xdr->p = p;
  719. }
  720. }
  721. EXPORT_SYMBOL_GPL(xdr_init_decode);
  722. /**
  723. * xdr_init_decode - Initialize an xdr_stream for decoding data.
  724. * @xdr: pointer to xdr_stream struct
  725. * @buf: pointer to XDR buffer from which to decode data
  726. * @pages: list of pages to decode into
  727. * @len: length in bytes of buffer in pages
  728. */
  729. void xdr_init_decode_pages(struct xdr_stream *xdr, struct xdr_buf *buf,
  730. struct page **pages, unsigned int len)
  731. {
  732. memset(buf, 0, sizeof(*buf));
  733. buf->pages = pages;
  734. buf->page_len = len;
  735. buf->buflen = len;
  736. buf->len = len;
  737. xdr_init_decode(xdr, buf, NULL);
  738. }
  739. EXPORT_SYMBOL_GPL(xdr_init_decode_pages);
  740. static __be32 * __xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
  741. {
  742. unsigned int nwords = XDR_QUADLEN(nbytes);
  743. __be32 *p = xdr->p;
  744. __be32 *q = p + nwords;
  745. if (unlikely(nwords > xdr->nwords || q > xdr->end || q < p))
  746. return NULL;
  747. xdr->p = q;
  748. xdr->nwords -= nwords;
  749. return p;
  750. }
  751. /**
  752. * xdr_set_scratch_buffer - Attach a scratch buffer for decoding data.
  753. * @xdr: pointer to xdr_stream struct
  754. * @buf: pointer to an empty buffer
  755. * @buflen: size of 'buf'
  756. *
  757. * The scratch buffer is used when decoding from an array of pages.
  758. * If an xdr_inline_decode() call spans across page boundaries, then
  759. * we copy the data into the scratch buffer in order to allow linear
  760. * access.
  761. */
  762. void xdr_set_scratch_buffer(struct xdr_stream *xdr, void *buf, size_t buflen)
  763. {
  764. xdr->scratch.iov_base = buf;
  765. xdr->scratch.iov_len = buflen;
  766. }
  767. EXPORT_SYMBOL_GPL(xdr_set_scratch_buffer);
  768. static __be32 *xdr_copy_to_scratch(struct xdr_stream *xdr, size_t nbytes)
  769. {
  770. __be32 *p;
  771. char *cpdest = xdr->scratch.iov_base;
  772. size_t cplen = (char *)xdr->end - (char *)xdr->p;
  773. if (nbytes > xdr->scratch.iov_len)
  774. return NULL;
  775. p = __xdr_inline_decode(xdr, cplen);
  776. if (p == NULL)
  777. return NULL;
  778. memcpy(cpdest, p, cplen);
  779. cpdest += cplen;
  780. nbytes -= cplen;
  781. if (!xdr_set_next_buffer(xdr))
  782. return NULL;
  783. p = __xdr_inline_decode(xdr, nbytes);
  784. if (p == NULL)
  785. return NULL;
  786. memcpy(cpdest, p, nbytes);
  787. return xdr->scratch.iov_base;
  788. }
  789. /**
  790. * xdr_inline_decode - Retrieve XDR data to decode
  791. * @xdr: pointer to xdr_stream struct
  792. * @nbytes: number of bytes of data to decode
  793. *
  794. * Check if the input buffer is long enough to enable us to decode
  795. * 'nbytes' more bytes of data starting at the current position.
  796. * If so return the current pointer, then update the current
  797. * pointer position.
  798. */
  799. __be32 * xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
  800. {
  801. __be32 *p;
  802. if (nbytes == 0)
  803. return xdr->p;
  804. if (xdr->p == xdr->end && !xdr_set_next_buffer(xdr))
  805. return NULL;
  806. p = __xdr_inline_decode(xdr, nbytes);
  807. if (p != NULL)
  808. return p;
  809. return xdr_copy_to_scratch(xdr, nbytes);
  810. }
  811. EXPORT_SYMBOL_GPL(xdr_inline_decode);
  812. static unsigned int xdr_align_pages(struct xdr_stream *xdr, unsigned int len)
  813. {
  814. struct xdr_buf *buf = xdr->buf;
  815. struct kvec *iov;
  816. unsigned int nwords = XDR_QUADLEN(len);
  817. unsigned int cur = xdr_stream_pos(xdr);
  818. if (xdr->nwords == 0)
  819. return 0;
  820. /* Realign pages to current pointer position */
  821. iov = buf->head;
  822. if (iov->iov_len > cur) {
  823. xdr_shrink_bufhead(buf, iov->iov_len - cur);
  824. xdr->nwords = XDR_QUADLEN(buf->len - cur);
  825. }
  826. if (nwords > xdr->nwords) {
  827. nwords = xdr->nwords;
  828. len = nwords << 2;
  829. }
  830. if (buf->page_len <= len)
  831. len = buf->page_len;
  832. else if (nwords < xdr->nwords) {
  833. /* Truncate page data and move it into the tail */
  834. xdr_shrink_pagelen(buf, buf->page_len - len);
  835. xdr->nwords = XDR_QUADLEN(buf->len - cur);
  836. }
  837. return len;
  838. }
  839. /**
  840. * xdr_read_pages - Ensure page-based XDR data to decode is aligned at current pointer position
  841. * @xdr: pointer to xdr_stream struct
  842. * @len: number of bytes of page data
  843. *
  844. * Moves data beyond the current pointer position from the XDR head[] buffer
  845. * into the page list. Any data that lies beyond current position + "len"
  846. * bytes is moved into the XDR tail[].
  847. *
  848. * Returns the number of XDR encoded bytes now contained in the pages
  849. */
  850. unsigned int xdr_read_pages(struct xdr_stream *xdr, unsigned int len)
  851. {
  852. struct xdr_buf *buf = xdr->buf;
  853. struct kvec *iov;
  854. unsigned int nwords;
  855. unsigned int end;
  856. unsigned int padding;
  857. len = xdr_align_pages(xdr, len);
  858. if (len == 0)
  859. return 0;
  860. nwords = XDR_QUADLEN(len);
  861. padding = (nwords << 2) - len;
  862. xdr->iov = iov = buf->tail;
  863. /* Compute remaining message length. */
  864. end = ((xdr->nwords - nwords) << 2) + padding;
  865. if (end > iov->iov_len)
  866. end = iov->iov_len;
  867. /*
  868. * Position current pointer at beginning of tail, and
  869. * set remaining message length.
  870. */
  871. xdr->p = (__be32 *)((char *)iov->iov_base + padding);
  872. xdr->end = (__be32 *)((char *)iov->iov_base + end);
  873. xdr->page_ptr = NULL;
  874. xdr->nwords = XDR_QUADLEN(end - padding);
  875. return len;
  876. }
  877. EXPORT_SYMBOL_GPL(xdr_read_pages);
  878. /**
  879. * xdr_enter_page - decode data from the XDR page
  880. * @xdr: pointer to xdr_stream struct
  881. * @len: number of bytes of page data
  882. *
  883. * Moves data beyond the current pointer position from the XDR head[] buffer
  884. * into the page list. Any data that lies beyond current position + "len"
  885. * bytes is moved into the XDR tail[]. The current pointer is then
  886. * repositioned at the beginning of the first XDR page.
  887. */
  888. void xdr_enter_page(struct xdr_stream *xdr, unsigned int len)
  889. {
  890. len = xdr_align_pages(xdr, len);
  891. /*
  892. * Position current pointer at beginning of tail, and
  893. * set remaining message length.
  894. */
  895. if (len != 0)
  896. xdr_set_page_base(xdr, 0, len);
  897. }
  898. EXPORT_SYMBOL_GPL(xdr_enter_page);
  899. static struct kvec empty_iov = {.iov_base = NULL, .iov_len = 0};
  900. void
  901. xdr_buf_from_iov(struct kvec *iov, struct xdr_buf *buf)
  902. {
  903. buf->head[0] = *iov;
  904. buf->tail[0] = empty_iov;
  905. buf->page_len = 0;
  906. buf->buflen = buf->len = iov->iov_len;
  907. }
  908. EXPORT_SYMBOL_GPL(xdr_buf_from_iov);
  909. /**
  910. * xdr_buf_subsegment - set subbuf to a portion of buf
  911. * @buf: an xdr buffer
  912. * @subbuf: the result buffer
  913. * @base: beginning of range in bytes
  914. * @len: length of range in bytes
  915. *
  916. * sets @subbuf to an xdr buffer representing the portion of @buf of
  917. * length @len starting at offset @base.
  918. *
  919. * @buf and @subbuf may be pointers to the same struct xdr_buf.
  920. *
  921. * Returns -1 if base of length are out of bounds.
  922. */
  923. int
  924. xdr_buf_subsegment(struct xdr_buf *buf, struct xdr_buf *subbuf,
  925. unsigned int base, unsigned int len)
  926. {
  927. subbuf->buflen = subbuf->len = len;
  928. if (base < buf->head[0].iov_len) {
  929. subbuf->head[0].iov_base = buf->head[0].iov_base + base;
  930. subbuf->head[0].iov_len = min_t(unsigned int, len,
  931. buf->head[0].iov_len - base);
  932. len -= subbuf->head[0].iov_len;
  933. base = 0;
  934. } else {
  935. base -= buf->head[0].iov_len;
  936. subbuf->head[0].iov_len = 0;
  937. }
  938. if (base < buf->page_len) {
  939. subbuf->page_len = min(buf->page_len - base, len);
  940. base += buf->page_base;
  941. subbuf->page_base = base & ~PAGE_MASK;
  942. subbuf->pages = &buf->pages[base >> PAGE_SHIFT];
  943. len -= subbuf->page_len;
  944. base = 0;
  945. } else {
  946. base -= buf->page_len;
  947. subbuf->page_len = 0;
  948. }
  949. if (base < buf->tail[0].iov_len) {
  950. subbuf->tail[0].iov_base = buf->tail[0].iov_base + base;
  951. subbuf->tail[0].iov_len = min_t(unsigned int, len,
  952. buf->tail[0].iov_len - base);
  953. len -= subbuf->tail[0].iov_len;
  954. base = 0;
  955. } else {
  956. base -= buf->tail[0].iov_len;
  957. subbuf->tail[0].iov_len = 0;
  958. }
  959. if (base || len)
  960. return -1;
  961. return 0;
  962. }
  963. EXPORT_SYMBOL_GPL(xdr_buf_subsegment);
  964. /**
  965. * xdr_buf_trim - lop at most "len" bytes off the end of "buf"
  966. * @buf: buf to be trimmed
  967. * @len: number of bytes to reduce "buf" by
  968. *
  969. * Trim an xdr_buf by the given number of bytes by fixing up the lengths. Note
  970. * that it's possible that we'll trim less than that amount if the xdr_buf is
  971. * too small, or if (for instance) it's all in the head and the parser has
  972. * already read too far into it.
  973. */
  974. void xdr_buf_trim(struct xdr_buf *buf, unsigned int len)
  975. {
  976. size_t cur;
  977. unsigned int trim = len;
  978. if (buf->tail[0].iov_len) {
  979. cur = min_t(size_t, buf->tail[0].iov_len, trim);
  980. buf->tail[0].iov_len -= cur;
  981. trim -= cur;
  982. if (!trim)
  983. goto fix_len;
  984. }
  985. if (buf->page_len) {
  986. cur = min_t(unsigned int, buf->page_len, trim);
  987. buf->page_len -= cur;
  988. trim -= cur;
  989. if (!trim)
  990. goto fix_len;
  991. }
  992. if (buf->head[0].iov_len) {
  993. cur = min_t(size_t, buf->head[0].iov_len, trim);
  994. buf->head[0].iov_len -= cur;
  995. trim -= cur;
  996. }
  997. fix_len:
  998. buf->len -= (len - trim);
  999. }
  1000. EXPORT_SYMBOL_GPL(xdr_buf_trim);
  1001. static void __read_bytes_from_xdr_buf(struct xdr_buf *subbuf, void *obj, unsigned int len)
  1002. {
  1003. unsigned int this_len;
  1004. this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
  1005. memcpy(obj, subbuf->head[0].iov_base, this_len);
  1006. len -= this_len;
  1007. obj += this_len;
  1008. this_len = min_t(unsigned int, len, subbuf->page_len);
  1009. if (this_len)
  1010. _copy_from_pages(obj, subbuf->pages, subbuf->page_base, this_len);
  1011. len -= this_len;
  1012. obj += this_len;
  1013. this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
  1014. memcpy(obj, subbuf->tail[0].iov_base, this_len);
  1015. }
  1016. /* obj is assumed to point to allocated memory of size at least len: */
  1017. int read_bytes_from_xdr_buf(struct xdr_buf *buf, unsigned int base, void *obj, unsigned int len)
  1018. {
  1019. struct xdr_buf subbuf;
  1020. int status;
  1021. status = xdr_buf_subsegment(buf, &subbuf, base, len);
  1022. if (status != 0)
  1023. return status;
  1024. __read_bytes_from_xdr_buf(&subbuf, obj, len);
  1025. return 0;
  1026. }
  1027. EXPORT_SYMBOL_GPL(read_bytes_from_xdr_buf);
  1028. static void __write_bytes_to_xdr_buf(struct xdr_buf *subbuf, void *obj, unsigned int len)
  1029. {
  1030. unsigned int this_len;
  1031. this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
  1032. memcpy(subbuf->head[0].iov_base, obj, this_len);
  1033. len -= this_len;
  1034. obj += this_len;
  1035. this_len = min_t(unsigned int, len, subbuf->page_len);
  1036. if (this_len)
  1037. _copy_to_pages(subbuf->pages, subbuf->page_base, obj, this_len);
  1038. len -= this_len;
  1039. obj += this_len;
  1040. this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
  1041. memcpy(subbuf->tail[0].iov_base, obj, this_len);
  1042. }
  1043. /* obj is assumed to point to allocated memory of size at least len: */
  1044. int write_bytes_to_xdr_buf(struct xdr_buf *buf, unsigned int base, void *obj, unsigned int len)
  1045. {
  1046. struct xdr_buf subbuf;
  1047. int status;
  1048. status = xdr_buf_subsegment(buf, &subbuf, base, len);
  1049. if (status != 0)
  1050. return status;
  1051. __write_bytes_to_xdr_buf(&subbuf, obj, len);
  1052. return 0;
  1053. }
  1054. EXPORT_SYMBOL_GPL(write_bytes_to_xdr_buf);
  1055. int
  1056. xdr_decode_word(struct xdr_buf *buf, unsigned int base, u32 *obj)
  1057. {
  1058. __be32 raw;
  1059. int status;
  1060. status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
  1061. if (status)
  1062. return status;
  1063. *obj = be32_to_cpu(raw);
  1064. return 0;
  1065. }
  1066. EXPORT_SYMBOL_GPL(xdr_decode_word);
  1067. int
  1068. xdr_encode_word(struct xdr_buf *buf, unsigned int base, u32 obj)
  1069. {
  1070. __be32 raw = cpu_to_be32(obj);
  1071. return write_bytes_to_xdr_buf(buf, base, &raw, sizeof(obj));
  1072. }
  1073. EXPORT_SYMBOL_GPL(xdr_encode_word);
  1074. /* If the netobj starting offset bytes from the start of xdr_buf is contained
  1075. * entirely in the head or the tail, set object to point to it; otherwise
  1076. * try to find space for it at the end of the tail, copy it there, and
  1077. * set obj to point to it. */
  1078. int xdr_buf_read_netobj(struct xdr_buf *buf, struct xdr_netobj *obj, unsigned int offset)
  1079. {
  1080. struct xdr_buf subbuf;
  1081. if (xdr_decode_word(buf, offset, &obj->len))
  1082. return -EFAULT;
  1083. if (xdr_buf_subsegment(buf, &subbuf, offset + 4, obj->len))
  1084. return -EFAULT;
  1085. /* Is the obj contained entirely in the head? */
  1086. obj->data = subbuf.head[0].iov_base;
  1087. if (subbuf.head[0].iov_len == obj->len)
  1088. return 0;
  1089. /* ..or is the obj contained entirely in the tail? */
  1090. obj->data = subbuf.tail[0].iov_base;
  1091. if (subbuf.tail[0].iov_len == obj->len)
  1092. return 0;
  1093. /* use end of tail as storage for obj:
  1094. * (We don't copy to the beginning because then we'd have
  1095. * to worry about doing a potentially overlapping copy.
  1096. * This assumes the object is at most half the length of the
  1097. * tail.) */
  1098. if (obj->len > buf->buflen - buf->len)
  1099. return -ENOMEM;
  1100. if (buf->tail[0].iov_len != 0)
  1101. obj->data = buf->tail[0].iov_base + buf->tail[0].iov_len;
  1102. else
  1103. obj->data = buf->head[0].iov_base + buf->head[0].iov_len;
  1104. __read_bytes_from_xdr_buf(&subbuf, obj->data, obj->len);
  1105. return 0;
  1106. }
  1107. EXPORT_SYMBOL_GPL(xdr_buf_read_netobj);
  1108. /* Returns 0 on success, or else a negative error code. */
  1109. static int
  1110. xdr_xcode_array2(struct xdr_buf *buf, unsigned int base,
  1111. struct xdr_array2_desc *desc, int encode)
  1112. {
  1113. char *elem = NULL, *c;
  1114. unsigned int copied = 0, todo, avail_here;
  1115. struct page **ppages = NULL;
  1116. int err;
  1117. if (encode) {
  1118. if (xdr_encode_word(buf, base, desc->array_len) != 0)
  1119. return -EINVAL;
  1120. } else {
  1121. if (xdr_decode_word(buf, base, &desc->array_len) != 0 ||
  1122. desc->array_len > desc->array_maxlen ||
  1123. (unsigned long) base + 4 + desc->array_len *
  1124. desc->elem_size > buf->len)
  1125. return -EINVAL;
  1126. }
  1127. base += 4;
  1128. if (!desc->xcode)
  1129. return 0;
  1130. todo = desc->array_len * desc->elem_size;
  1131. /* process head */
  1132. if (todo && base < buf->head->iov_len) {
  1133. c = buf->head->iov_base + base;
  1134. avail_here = min_t(unsigned int, todo,
  1135. buf->head->iov_len - base);
  1136. todo -= avail_here;
  1137. while (avail_here >= desc->elem_size) {
  1138. err = desc->xcode(desc, c);
  1139. if (err)
  1140. goto out;
  1141. c += desc->elem_size;
  1142. avail_here -= desc->elem_size;
  1143. }
  1144. if (avail_here) {
  1145. if (!elem) {
  1146. elem = kmalloc(desc->elem_size, GFP_KERNEL);
  1147. err = -ENOMEM;
  1148. if (!elem)
  1149. goto out;
  1150. }
  1151. if (encode) {
  1152. err = desc->xcode(desc, elem);
  1153. if (err)
  1154. goto out;
  1155. memcpy(c, elem, avail_here);
  1156. } else
  1157. memcpy(elem, c, avail_here);
  1158. copied = avail_here;
  1159. }
  1160. base = buf->head->iov_len; /* align to start of pages */
  1161. }
  1162. /* process pages array */
  1163. base -= buf->head->iov_len;
  1164. if (todo && base < buf->page_len) {
  1165. unsigned int avail_page;
  1166. avail_here = min(todo, buf->page_len - base);
  1167. todo -= avail_here;
  1168. base += buf->page_base;
  1169. ppages = buf->pages + (base >> PAGE_SHIFT);
  1170. base &= ~PAGE_MASK;
  1171. avail_page = min_t(unsigned int, PAGE_SIZE - base,
  1172. avail_here);
  1173. c = kmap(*ppages) + base;
  1174. while (avail_here) {
  1175. avail_here -= avail_page;
  1176. if (copied || avail_page < desc->elem_size) {
  1177. unsigned int l = min(avail_page,
  1178. desc->elem_size - copied);
  1179. if (!elem) {
  1180. elem = kmalloc(desc->elem_size,
  1181. GFP_KERNEL);
  1182. err = -ENOMEM;
  1183. if (!elem)
  1184. goto out;
  1185. }
  1186. if (encode) {
  1187. if (!copied) {
  1188. err = desc->xcode(desc, elem);
  1189. if (err)
  1190. goto out;
  1191. }
  1192. memcpy(c, elem + copied, l);
  1193. copied += l;
  1194. if (copied == desc->elem_size)
  1195. copied = 0;
  1196. } else {
  1197. memcpy(elem + copied, c, l);
  1198. copied += l;
  1199. if (copied == desc->elem_size) {
  1200. err = desc->xcode(desc, elem);
  1201. if (err)
  1202. goto out;
  1203. copied = 0;
  1204. }
  1205. }
  1206. avail_page -= l;
  1207. c += l;
  1208. }
  1209. while (avail_page >= desc->elem_size) {
  1210. err = desc->xcode(desc, c);
  1211. if (err)
  1212. goto out;
  1213. c += desc->elem_size;
  1214. avail_page -= desc->elem_size;
  1215. }
  1216. if (avail_page) {
  1217. unsigned int l = min(avail_page,
  1218. desc->elem_size - copied);
  1219. if (!elem) {
  1220. elem = kmalloc(desc->elem_size,
  1221. GFP_KERNEL);
  1222. err = -ENOMEM;
  1223. if (!elem)
  1224. goto out;
  1225. }
  1226. if (encode) {
  1227. if (!copied) {
  1228. err = desc->xcode(desc, elem);
  1229. if (err)
  1230. goto out;
  1231. }
  1232. memcpy(c, elem + copied, l);
  1233. copied += l;
  1234. if (copied == desc->elem_size)
  1235. copied = 0;
  1236. } else {
  1237. memcpy(elem + copied, c, l);
  1238. copied += l;
  1239. if (copied == desc->elem_size) {
  1240. err = desc->xcode(desc, elem);
  1241. if (err)
  1242. goto out;
  1243. copied = 0;
  1244. }
  1245. }
  1246. }
  1247. if (avail_here) {
  1248. kunmap(*ppages);
  1249. ppages++;
  1250. c = kmap(*ppages);
  1251. }
  1252. avail_page = min(avail_here,
  1253. (unsigned int) PAGE_SIZE);
  1254. }
  1255. base = buf->page_len; /* align to start of tail */
  1256. }
  1257. /* process tail */
  1258. base -= buf->page_len;
  1259. if (todo) {
  1260. c = buf->tail->iov_base + base;
  1261. if (copied) {
  1262. unsigned int l = desc->elem_size - copied;
  1263. if (encode)
  1264. memcpy(c, elem + copied, l);
  1265. else {
  1266. memcpy(elem + copied, c, l);
  1267. err = desc->xcode(desc, elem);
  1268. if (err)
  1269. goto out;
  1270. }
  1271. todo -= l;
  1272. c += l;
  1273. }
  1274. while (todo) {
  1275. err = desc->xcode(desc, c);
  1276. if (err)
  1277. goto out;
  1278. c += desc->elem_size;
  1279. todo -= desc->elem_size;
  1280. }
  1281. }
  1282. err = 0;
  1283. out:
  1284. kfree(elem);
  1285. if (ppages)
  1286. kunmap(*ppages);
  1287. return err;
  1288. }
  1289. int
  1290. xdr_decode_array2(struct xdr_buf *buf, unsigned int base,
  1291. struct xdr_array2_desc *desc)
  1292. {
  1293. if (base >= buf->len)
  1294. return -EINVAL;
  1295. return xdr_xcode_array2(buf, base, desc, 0);
  1296. }
  1297. EXPORT_SYMBOL_GPL(xdr_decode_array2);
  1298. int
  1299. xdr_encode_array2(struct xdr_buf *buf, unsigned int base,
  1300. struct xdr_array2_desc *desc)
  1301. {
  1302. if ((unsigned long) base + 4 + desc->array_len * desc->elem_size >
  1303. buf->head->iov_len + buf->page_len + buf->tail->iov_len)
  1304. return -EINVAL;
  1305. return xdr_xcode_array2(buf, base, desc, 1);
  1306. }
  1307. EXPORT_SYMBOL_GPL(xdr_encode_array2);
  1308. int
  1309. xdr_process_buf(struct xdr_buf *buf, unsigned int offset, unsigned int len,
  1310. int (*actor)(struct scatterlist *, void *), void *data)
  1311. {
  1312. int i, ret = 0;
  1313. unsigned int page_len, thislen, page_offset;
  1314. struct scatterlist sg[1];
  1315. sg_init_table(sg, 1);
  1316. if (offset >= buf->head[0].iov_len) {
  1317. offset -= buf->head[0].iov_len;
  1318. } else {
  1319. thislen = buf->head[0].iov_len - offset;
  1320. if (thislen > len)
  1321. thislen = len;
  1322. sg_set_buf(sg, buf->head[0].iov_base + offset, thislen);
  1323. ret = actor(sg, data);
  1324. if (ret)
  1325. goto out;
  1326. offset = 0;
  1327. len -= thislen;
  1328. }
  1329. if (len == 0)
  1330. goto out;
  1331. if (offset >= buf->page_len) {
  1332. offset -= buf->page_len;
  1333. } else {
  1334. page_len = buf->page_len - offset;
  1335. if (page_len > len)
  1336. page_len = len;
  1337. len -= page_len;
  1338. page_offset = (offset + buf->page_base) & (PAGE_SIZE - 1);
  1339. i = (offset + buf->page_base) >> PAGE_SHIFT;
  1340. thislen = PAGE_SIZE - page_offset;
  1341. do {
  1342. if (thislen > page_len)
  1343. thislen = page_len;
  1344. sg_set_page(sg, buf->pages[i], thislen, page_offset);
  1345. ret = actor(sg, data);
  1346. if (ret)
  1347. goto out;
  1348. page_len -= thislen;
  1349. i++;
  1350. page_offset = 0;
  1351. thislen = PAGE_SIZE;
  1352. } while (page_len != 0);
  1353. offset = 0;
  1354. }
  1355. if (len == 0)
  1356. goto out;
  1357. if (offset < buf->tail[0].iov_len) {
  1358. thislen = buf->tail[0].iov_len - offset;
  1359. if (thislen > len)
  1360. thislen = len;
  1361. sg_set_buf(sg, buf->tail[0].iov_base + offset, thislen);
  1362. ret = actor(sg, data);
  1363. len -= thislen;
  1364. }
  1365. if (len != 0)
  1366. ret = -EINVAL;
  1367. out:
  1368. return ret;
  1369. }
  1370. EXPORT_SYMBOL_GPL(xdr_process_buf);