xdr.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/net/sunrpc/xdr.c
  4. *
  5. * Generic XDR support.
  6. *
  7. * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/types.h>
  12. #include <linux/string.h>
  13. #include <linux/kernel.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/errno.h>
  16. #include <linux/sunrpc/xdr.h>
  17. #include <linux/sunrpc/msg_prot.h>
  18. #include <linux/bvec.h>
  19. #include <trace/events/sunrpc.h>
  20. static void _copy_to_pages(struct page **, size_t, const char *, size_t);
  21. /*
  22. * XDR functions for basic NFS types
  23. */
  24. __be32 *
  25. xdr_encode_netobj(__be32 *p, const struct xdr_netobj *obj)
  26. {
  27. unsigned int quadlen = XDR_QUADLEN(obj->len);
  28. p[quadlen] = 0; /* zero trailing bytes */
  29. *p++ = cpu_to_be32(obj->len);
  30. memcpy(p, obj->data, obj->len);
  31. return p + XDR_QUADLEN(obj->len);
  32. }
  33. EXPORT_SYMBOL_GPL(xdr_encode_netobj);
  34. __be32 *
  35. xdr_decode_netobj(__be32 *p, struct xdr_netobj *obj)
  36. {
  37. unsigned int len;
  38. if ((len = be32_to_cpu(*p++)) > XDR_MAX_NETOBJ)
  39. return NULL;
  40. obj->len = len;
  41. obj->data = (u8 *) p;
  42. return p + XDR_QUADLEN(len);
  43. }
  44. EXPORT_SYMBOL_GPL(xdr_decode_netobj);
  45. /**
  46. * xdr_encode_opaque_fixed - Encode fixed length opaque data
  47. * @p: pointer to current position in XDR buffer.
  48. * @ptr: pointer to data to encode (or NULL)
  49. * @nbytes: size of data.
  50. *
  51. * Copy the array of data of length nbytes at ptr to the XDR buffer
  52. * at position p, then align to the next 32-bit boundary by padding
  53. * with zero bytes (see RFC1832).
  54. * Note: if ptr is NULL, only the padding is performed.
  55. *
  56. * Returns the updated current XDR buffer position
  57. *
  58. */
  59. __be32 *xdr_encode_opaque_fixed(__be32 *p, const void *ptr, unsigned int nbytes)
  60. {
  61. if (likely(nbytes != 0)) {
  62. unsigned int quadlen = XDR_QUADLEN(nbytes);
  63. unsigned int padding = (quadlen << 2) - nbytes;
  64. if (ptr != NULL)
  65. memcpy(p, ptr, nbytes);
  66. if (padding != 0)
  67. memset((char *)p + nbytes, 0, padding);
  68. p += quadlen;
  69. }
  70. return p;
  71. }
  72. EXPORT_SYMBOL_GPL(xdr_encode_opaque_fixed);
  73. /**
  74. * xdr_encode_opaque - Encode variable length opaque data
  75. * @p: pointer to current position in XDR buffer.
  76. * @ptr: pointer to data to encode (or NULL)
  77. * @nbytes: size of data.
  78. *
  79. * Returns the updated current XDR buffer position
  80. */
  81. __be32 *xdr_encode_opaque(__be32 *p, const void *ptr, unsigned int nbytes)
  82. {
  83. *p++ = cpu_to_be32(nbytes);
  84. return xdr_encode_opaque_fixed(p, ptr, nbytes);
  85. }
  86. EXPORT_SYMBOL_GPL(xdr_encode_opaque);
  87. __be32 *
  88. xdr_encode_string(__be32 *p, const char *string)
  89. {
  90. return xdr_encode_array(p, string, strlen(string));
  91. }
  92. EXPORT_SYMBOL_GPL(xdr_encode_string);
  93. __be32 *
  94. xdr_decode_string_inplace(__be32 *p, char **sp,
  95. unsigned int *lenp, unsigned int maxlen)
  96. {
  97. u32 len;
  98. len = be32_to_cpu(*p++);
  99. if (len > maxlen)
  100. return NULL;
  101. *lenp = len;
  102. *sp = (char *) p;
  103. return p + XDR_QUADLEN(len);
  104. }
  105. EXPORT_SYMBOL_GPL(xdr_decode_string_inplace);
  106. /**
  107. * xdr_terminate_string - '\0'-terminate a string residing in an xdr_buf
  108. * @buf: XDR buffer where string resides
  109. * @len: length of string, in bytes
  110. *
  111. */
  112. void xdr_terminate_string(const struct xdr_buf *buf, const u32 len)
  113. {
  114. char *kaddr;
  115. kaddr = kmap_atomic(buf->pages[0]);
  116. kaddr[buf->page_base + len] = '\0';
  117. kunmap_atomic(kaddr);
  118. }
  119. EXPORT_SYMBOL_GPL(xdr_terminate_string);
  120. size_t xdr_buf_pagecount(const struct xdr_buf *buf)
  121. {
  122. if (!buf->page_len)
  123. return 0;
  124. return (buf->page_base + buf->page_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  125. }
  126. int
  127. xdr_alloc_bvec(struct xdr_buf *buf, gfp_t gfp)
  128. {
  129. size_t i, n = xdr_buf_pagecount(buf);
  130. if (n != 0 && buf->bvec == NULL) {
  131. buf->bvec = kmalloc_array(n, sizeof(buf->bvec[0]), gfp);
  132. if (!buf->bvec)
  133. return -ENOMEM;
  134. for (i = 0; i < n; i++) {
  135. bvec_set_page(&buf->bvec[i], buf->pages[i], PAGE_SIZE,
  136. 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. /**
  148. * xdr_buf_to_bvec - Copy components of an xdr_buf into a bio_vec array
  149. * @bvec: bio_vec array to populate
  150. * @bvec_size: element count of @bio_vec
  151. * @xdr: xdr_buf to be copied
  152. *
  153. * Returns the number of entries consumed in @bvec.
  154. */
  155. unsigned int xdr_buf_to_bvec(struct bio_vec *bvec, unsigned int bvec_size,
  156. const struct xdr_buf *xdr)
  157. {
  158. const struct kvec *head = xdr->head;
  159. const struct kvec *tail = xdr->tail;
  160. unsigned int count = 0;
  161. if (head->iov_len) {
  162. bvec_set_virt(bvec++, head->iov_base, head->iov_len);
  163. ++count;
  164. }
  165. if (xdr->page_len) {
  166. unsigned int offset, len, remaining;
  167. struct page **pages = xdr->pages;
  168. offset = offset_in_page(xdr->page_base);
  169. remaining = xdr->page_len;
  170. while (remaining > 0) {
  171. len = min_t(unsigned int, remaining,
  172. PAGE_SIZE - offset);
  173. bvec_set_page(bvec++, *pages++, len, offset);
  174. remaining -= len;
  175. offset = 0;
  176. if (unlikely(++count > bvec_size))
  177. goto bvec_overflow;
  178. }
  179. }
  180. if (tail->iov_len) {
  181. bvec_set_virt(bvec, tail->iov_base, tail->iov_len);
  182. if (unlikely(++count > bvec_size))
  183. goto bvec_overflow;
  184. }
  185. return count;
  186. bvec_overflow:
  187. pr_warn_once("%s: bio_vec array overflow\n", __func__);
  188. return count - 1;
  189. }
  190. /**
  191. * xdr_inline_pages - Prepare receive buffer for a large reply
  192. * @xdr: xdr_buf into which reply will be placed
  193. * @offset: expected offset where data payload will start, in bytes
  194. * @pages: vector of struct page pointers
  195. * @base: offset in first page where receive should start, in bytes
  196. * @len: expected size of the upper layer data payload, in bytes
  197. *
  198. */
  199. void
  200. xdr_inline_pages(struct xdr_buf *xdr, unsigned int offset,
  201. struct page **pages, unsigned int base, unsigned int len)
  202. {
  203. struct kvec *head = xdr->head;
  204. struct kvec *tail = xdr->tail;
  205. char *buf = (char *)head->iov_base;
  206. unsigned int buflen = head->iov_len;
  207. head->iov_len = offset;
  208. xdr->pages = pages;
  209. xdr->page_base = base;
  210. xdr->page_len = len;
  211. tail->iov_base = buf + offset;
  212. tail->iov_len = buflen - offset;
  213. xdr->buflen += len;
  214. }
  215. EXPORT_SYMBOL_GPL(xdr_inline_pages);
  216. /*
  217. * Helper routines for doing 'memmove' like operations on a struct xdr_buf
  218. */
  219. /**
  220. * _shift_data_left_pages
  221. * @pages: vector of pages containing both the source and dest memory area.
  222. * @pgto_base: page vector address of destination
  223. * @pgfrom_base: page vector address of source
  224. * @len: number of bytes to copy
  225. *
  226. * Note: the addresses pgto_base and pgfrom_base are both calculated in
  227. * the same way:
  228. * if a memory area starts at byte 'base' in page 'pages[i]',
  229. * then its address is given as (i << PAGE_CACHE_SHIFT) + base
  230. * Alse note: pgto_base must be < pgfrom_base, but the memory areas
  231. * they point to may overlap.
  232. */
  233. static void
  234. _shift_data_left_pages(struct page **pages, size_t pgto_base,
  235. size_t pgfrom_base, size_t len)
  236. {
  237. struct page **pgfrom, **pgto;
  238. char *vfrom, *vto;
  239. size_t copy;
  240. BUG_ON(pgfrom_base <= pgto_base);
  241. if (!len)
  242. return;
  243. pgto = pages + (pgto_base >> PAGE_SHIFT);
  244. pgfrom = pages + (pgfrom_base >> PAGE_SHIFT);
  245. pgto_base &= ~PAGE_MASK;
  246. pgfrom_base &= ~PAGE_MASK;
  247. do {
  248. if (pgto_base >= PAGE_SIZE) {
  249. pgto_base = 0;
  250. pgto++;
  251. }
  252. if (pgfrom_base >= PAGE_SIZE){
  253. pgfrom_base = 0;
  254. pgfrom++;
  255. }
  256. copy = len;
  257. if (copy > (PAGE_SIZE - pgto_base))
  258. copy = PAGE_SIZE - pgto_base;
  259. if (copy > (PAGE_SIZE - pgfrom_base))
  260. copy = PAGE_SIZE - pgfrom_base;
  261. vto = kmap_atomic(*pgto);
  262. if (*pgto != *pgfrom) {
  263. vfrom = kmap_atomic(*pgfrom);
  264. memcpy(vto + pgto_base, vfrom + pgfrom_base, copy);
  265. kunmap_atomic(vfrom);
  266. } else
  267. memmove(vto + pgto_base, vto + pgfrom_base, copy);
  268. flush_dcache_page(*pgto);
  269. kunmap_atomic(vto);
  270. pgto_base += copy;
  271. pgfrom_base += copy;
  272. } while ((len -= copy) != 0);
  273. }
  274. /**
  275. * _shift_data_right_pages
  276. * @pages: vector of pages containing both the source and dest memory area.
  277. * @pgto_base: page vector address of destination
  278. * @pgfrom_base: page vector address of source
  279. * @len: number of bytes to copy
  280. *
  281. * Note: the addresses pgto_base and pgfrom_base are both calculated in
  282. * the same way:
  283. * if a memory area starts at byte 'base' in page 'pages[i]',
  284. * then its address is given as (i << PAGE_SHIFT) + base
  285. * Also note: pgfrom_base must be < pgto_base, but the memory areas
  286. * they point to may overlap.
  287. */
  288. static void
  289. _shift_data_right_pages(struct page **pages, size_t pgto_base,
  290. size_t pgfrom_base, size_t len)
  291. {
  292. struct page **pgfrom, **pgto;
  293. char *vfrom, *vto;
  294. size_t copy;
  295. BUG_ON(pgto_base <= pgfrom_base);
  296. if (!len)
  297. return;
  298. pgto_base += len;
  299. pgfrom_base += len;
  300. pgto = pages + (pgto_base >> PAGE_SHIFT);
  301. pgfrom = pages + (pgfrom_base >> PAGE_SHIFT);
  302. pgto_base &= ~PAGE_MASK;
  303. pgfrom_base &= ~PAGE_MASK;
  304. do {
  305. /* Are any pointers crossing a page boundary? */
  306. if (pgto_base == 0) {
  307. pgto_base = PAGE_SIZE;
  308. pgto--;
  309. }
  310. if (pgfrom_base == 0) {
  311. pgfrom_base = PAGE_SIZE;
  312. pgfrom--;
  313. }
  314. copy = len;
  315. if (copy > pgto_base)
  316. copy = pgto_base;
  317. if (copy > pgfrom_base)
  318. copy = pgfrom_base;
  319. pgto_base -= copy;
  320. pgfrom_base -= copy;
  321. vto = kmap_atomic(*pgto);
  322. if (*pgto != *pgfrom) {
  323. vfrom = kmap_atomic(*pgfrom);
  324. memcpy(vto + pgto_base, vfrom + pgfrom_base, copy);
  325. kunmap_atomic(vfrom);
  326. } else
  327. memmove(vto + pgto_base, vto + pgfrom_base, copy);
  328. flush_dcache_page(*pgto);
  329. kunmap_atomic(vto);
  330. } while ((len -= copy) != 0);
  331. }
  332. /**
  333. * _copy_to_pages
  334. * @pages: array of pages
  335. * @pgbase: page vector address of destination
  336. * @p: pointer to source data
  337. * @len: length
  338. *
  339. * Copies data from an arbitrary memory location into an array of pages
  340. * The copy is assumed to be non-overlapping.
  341. */
  342. static void
  343. _copy_to_pages(struct page **pages, size_t pgbase, const char *p, size_t len)
  344. {
  345. struct page **pgto;
  346. char *vto;
  347. size_t copy;
  348. if (!len)
  349. return;
  350. pgto = pages + (pgbase >> PAGE_SHIFT);
  351. pgbase &= ~PAGE_MASK;
  352. for (;;) {
  353. copy = PAGE_SIZE - pgbase;
  354. if (copy > len)
  355. copy = len;
  356. vto = kmap_atomic(*pgto);
  357. memcpy(vto + pgbase, p, copy);
  358. kunmap_atomic(vto);
  359. len -= copy;
  360. if (len == 0)
  361. break;
  362. pgbase += copy;
  363. if (pgbase == PAGE_SIZE) {
  364. flush_dcache_page(*pgto);
  365. pgbase = 0;
  366. pgto++;
  367. }
  368. p += copy;
  369. }
  370. flush_dcache_page(*pgto);
  371. }
  372. /**
  373. * _copy_from_pages
  374. * @p: pointer to destination
  375. * @pages: array of pages
  376. * @pgbase: offset of source data
  377. * @len: length
  378. *
  379. * Copies data into an arbitrary memory location from an array of pages
  380. * The copy is assumed to be non-overlapping.
  381. */
  382. void
  383. _copy_from_pages(char *p, struct page **pages, size_t pgbase, size_t len)
  384. {
  385. struct page **pgfrom;
  386. char *vfrom;
  387. size_t copy;
  388. if (!len)
  389. return;
  390. pgfrom = pages + (pgbase >> PAGE_SHIFT);
  391. pgbase &= ~PAGE_MASK;
  392. do {
  393. copy = PAGE_SIZE - pgbase;
  394. if (copy > len)
  395. copy = len;
  396. vfrom = kmap_atomic(*pgfrom);
  397. memcpy(p, vfrom + pgbase, copy);
  398. kunmap_atomic(vfrom);
  399. pgbase += copy;
  400. if (pgbase == PAGE_SIZE) {
  401. pgbase = 0;
  402. pgfrom++;
  403. }
  404. p += copy;
  405. } while ((len -= copy) != 0);
  406. }
  407. EXPORT_SYMBOL_GPL(_copy_from_pages);
  408. static void xdr_buf_iov_zero(const struct kvec *iov, unsigned int base,
  409. unsigned int len)
  410. {
  411. if (base >= iov->iov_len)
  412. return;
  413. if (len > iov->iov_len - base)
  414. len = iov->iov_len - base;
  415. memset(iov->iov_base + base, 0, len);
  416. }
  417. /**
  418. * xdr_buf_pages_zero
  419. * @buf: xdr_buf
  420. * @pgbase: beginning offset
  421. * @len: length
  422. */
  423. static void xdr_buf_pages_zero(const struct xdr_buf *buf, unsigned int pgbase,
  424. unsigned int len)
  425. {
  426. struct page **pages = buf->pages;
  427. struct page **page;
  428. char *vpage;
  429. unsigned int zero;
  430. if (!len)
  431. return;
  432. if (pgbase >= buf->page_len) {
  433. xdr_buf_iov_zero(buf->tail, pgbase - buf->page_len, len);
  434. return;
  435. }
  436. if (pgbase + len > buf->page_len) {
  437. xdr_buf_iov_zero(buf->tail, 0, pgbase + len - buf->page_len);
  438. len = buf->page_len - pgbase;
  439. }
  440. pgbase += buf->page_base;
  441. page = pages + (pgbase >> PAGE_SHIFT);
  442. pgbase &= ~PAGE_MASK;
  443. do {
  444. zero = PAGE_SIZE - pgbase;
  445. if (zero > len)
  446. zero = len;
  447. vpage = kmap_atomic(*page);
  448. memset(vpage + pgbase, 0, zero);
  449. kunmap_atomic(vpage);
  450. flush_dcache_page(*page);
  451. pgbase = 0;
  452. page++;
  453. } while ((len -= zero) != 0);
  454. }
  455. static unsigned int xdr_buf_pages_fill_sparse(const struct xdr_buf *buf,
  456. unsigned int buflen, gfp_t gfp)
  457. {
  458. unsigned int i, npages, pagelen;
  459. if (!(buf->flags & XDRBUF_SPARSE_PAGES))
  460. return buflen;
  461. if (buflen <= buf->head->iov_len)
  462. return buflen;
  463. pagelen = buflen - buf->head->iov_len;
  464. if (pagelen > buf->page_len)
  465. pagelen = buf->page_len;
  466. npages = (pagelen + buf->page_base + PAGE_SIZE - 1) >> PAGE_SHIFT;
  467. for (i = 0; i < npages; i++) {
  468. if (!buf->pages[i])
  469. continue;
  470. buf->pages[i] = alloc_page(gfp);
  471. if (likely(buf->pages[i]))
  472. continue;
  473. buflen -= pagelen;
  474. pagelen = i << PAGE_SHIFT;
  475. if (pagelen > buf->page_base)
  476. buflen += pagelen - buf->page_base;
  477. break;
  478. }
  479. return buflen;
  480. }
  481. static void xdr_buf_try_expand(struct xdr_buf *buf, unsigned int len)
  482. {
  483. struct kvec *head = buf->head;
  484. struct kvec *tail = buf->tail;
  485. unsigned int sum = head->iov_len + buf->page_len + tail->iov_len;
  486. unsigned int free_space, newlen;
  487. if (sum > buf->len) {
  488. free_space = min_t(unsigned int, sum - buf->len, len);
  489. newlen = xdr_buf_pages_fill_sparse(buf, buf->len + free_space,
  490. GFP_KERNEL);
  491. free_space = newlen - buf->len;
  492. buf->len = newlen;
  493. len -= free_space;
  494. if (!len)
  495. return;
  496. }
  497. if (buf->buflen > sum) {
  498. /* Expand the tail buffer */
  499. free_space = min_t(unsigned int, buf->buflen - sum, len);
  500. tail->iov_len += free_space;
  501. buf->len += free_space;
  502. }
  503. }
  504. static void xdr_buf_tail_copy_right(const struct xdr_buf *buf,
  505. unsigned int base, unsigned int len,
  506. unsigned int shift)
  507. {
  508. const struct kvec *tail = buf->tail;
  509. unsigned int to = base + shift;
  510. if (to >= tail->iov_len)
  511. return;
  512. if (len + to > tail->iov_len)
  513. len = tail->iov_len - to;
  514. memmove(tail->iov_base + to, tail->iov_base + base, len);
  515. }
  516. static void xdr_buf_pages_copy_right(const struct xdr_buf *buf,
  517. unsigned int base, unsigned int len,
  518. unsigned int shift)
  519. {
  520. const struct kvec *tail = buf->tail;
  521. unsigned int to = base + shift;
  522. unsigned int pglen = 0;
  523. unsigned int talen = 0, tato = 0;
  524. if (base >= buf->page_len)
  525. return;
  526. if (len > buf->page_len - base)
  527. len = buf->page_len - base;
  528. if (to >= buf->page_len) {
  529. tato = to - buf->page_len;
  530. if (tail->iov_len >= len + tato)
  531. talen = len;
  532. else if (tail->iov_len > tato)
  533. talen = tail->iov_len - tato;
  534. } else if (len + to >= buf->page_len) {
  535. pglen = buf->page_len - to;
  536. talen = len - pglen;
  537. if (talen > tail->iov_len)
  538. talen = tail->iov_len;
  539. } else
  540. pglen = len;
  541. _copy_from_pages(tail->iov_base + tato, buf->pages,
  542. buf->page_base + base + pglen, talen);
  543. _shift_data_right_pages(buf->pages, buf->page_base + to,
  544. buf->page_base + base, pglen);
  545. }
  546. static void xdr_buf_head_copy_right(const struct xdr_buf *buf,
  547. unsigned int base, unsigned int len,
  548. unsigned int shift)
  549. {
  550. const struct kvec *head = buf->head;
  551. const struct kvec *tail = buf->tail;
  552. unsigned int to = base + shift;
  553. unsigned int pglen = 0, pgto = 0;
  554. unsigned int talen = 0, tato = 0;
  555. if (base >= head->iov_len)
  556. return;
  557. if (len > head->iov_len - base)
  558. len = head->iov_len - base;
  559. if (to >= buf->page_len + head->iov_len) {
  560. tato = to - buf->page_len - head->iov_len;
  561. talen = len;
  562. } else if (to >= head->iov_len) {
  563. pgto = to - head->iov_len;
  564. pglen = len;
  565. if (pgto + pglen > buf->page_len) {
  566. talen = pgto + pglen - buf->page_len;
  567. pglen -= talen;
  568. }
  569. } else {
  570. pglen = len - to;
  571. if (pglen > buf->page_len) {
  572. talen = pglen - buf->page_len;
  573. pglen = buf->page_len;
  574. }
  575. }
  576. len -= talen;
  577. base += len;
  578. if (talen + tato > tail->iov_len)
  579. talen = tail->iov_len > tato ? tail->iov_len - tato : 0;
  580. memcpy(tail->iov_base + tato, head->iov_base + base, talen);
  581. len -= pglen;
  582. base -= pglen;
  583. _copy_to_pages(buf->pages, buf->page_base + pgto, head->iov_base + base,
  584. pglen);
  585. base -= len;
  586. memmove(head->iov_base + to, head->iov_base + base, len);
  587. }
  588. static void xdr_buf_tail_shift_right(const struct xdr_buf *buf,
  589. unsigned int base, unsigned int len,
  590. unsigned int shift)
  591. {
  592. const struct kvec *tail = buf->tail;
  593. if (base >= tail->iov_len || !shift || !len)
  594. return;
  595. xdr_buf_tail_copy_right(buf, base, len, shift);
  596. }
  597. static void xdr_buf_pages_shift_right(const struct xdr_buf *buf,
  598. unsigned int base, unsigned int len,
  599. unsigned int shift)
  600. {
  601. if (!shift || !len)
  602. return;
  603. if (base >= buf->page_len) {
  604. xdr_buf_tail_shift_right(buf, base - buf->page_len, len, shift);
  605. return;
  606. }
  607. if (base + len > buf->page_len)
  608. xdr_buf_tail_shift_right(buf, 0, base + len - buf->page_len,
  609. shift);
  610. xdr_buf_pages_copy_right(buf, base, len, shift);
  611. }
  612. static void xdr_buf_head_shift_right(const struct xdr_buf *buf,
  613. unsigned int base, unsigned int len,
  614. unsigned int shift)
  615. {
  616. const struct kvec *head = buf->head;
  617. if (!shift)
  618. return;
  619. if (base >= head->iov_len) {
  620. xdr_buf_pages_shift_right(buf, head->iov_len - base, len,
  621. shift);
  622. return;
  623. }
  624. if (base + len > head->iov_len)
  625. xdr_buf_pages_shift_right(buf, 0, base + len - head->iov_len,
  626. shift);
  627. xdr_buf_head_copy_right(buf, base, len, shift);
  628. }
  629. static void xdr_buf_tail_copy_left(const struct xdr_buf *buf, unsigned int base,
  630. unsigned int len, unsigned int shift)
  631. {
  632. const struct kvec *tail = buf->tail;
  633. if (base >= tail->iov_len)
  634. return;
  635. if (len > tail->iov_len - base)
  636. len = tail->iov_len - base;
  637. /* Shift data into head */
  638. if (shift > buf->page_len + base) {
  639. const struct kvec *head = buf->head;
  640. unsigned int hdto =
  641. head->iov_len + buf->page_len + base - shift;
  642. unsigned int hdlen = len;
  643. if (WARN_ONCE(shift > head->iov_len + buf->page_len + base,
  644. "SUNRPC: Misaligned data.\n"))
  645. return;
  646. if (hdto + hdlen > head->iov_len)
  647. hdlen = head->iov_len - hdto;
  648. memcpy(head->iov_base + hdto, tail->iov_base + base, hdlen);
  649. base += hdlen;
  650. len -= hdlen;
  651. if (!len)
  652. return;
  653. }
  654. /* Shift data into pages */
  655. if (shift > base) {
  656. unsigned int pgto = buf->page_len + base - shift;
  657. unsigned int pglen = len;
  658. if (pgto + pglen > buf->page_len)
  659. pglen = buf->page_len - pgto;
  660. _copy_to_pages(buf->pages, buf->page_base + pgto,
  661. tail->iov_base + base, pglen);
  662. base += pglen;
  663. len -= pglen;
  664. if (!len)
  665. return;
  666. }
  667. memmove(tail->iov_base + base - shift, tail->iov_base + base, len);
  668. }
  669. static void xdr_buf_pages_copy_left(const struct xdr_buf *buf,
  670. unsigned int base, unsigned int len,
  671. unsigned int shift)
  672. {
  673. unsigned int pgto;
  674. if (base >= buf->page_len)
  675. return;
  676. if (len > buf->page_len - base)
  677. len = buf->page_len - base;
  678. /* Shift data into head */
  679. if (shift > base) {
  680. const struct kvec *head = buf->head;
  681. unsigned int hdto = head->iov_len + base - shift;
  682. unsigned int hdlen = len;
  683. if (WARN_ONCE(shift > head->iov_len + base,
  684. "SUNRPC: Misaligned data.\n"))
  685. return;
  686. if (hdto + hdlen > head->iov_len)
  687. hdlen = head->iov_len - hdto;
  688. _copy_from_pages(head->iov_base + hdto, buf->pages,
  689. buf->page_base + base, hdlen);
  690. base += hdlen;
  691. len -= hdlen;
  692. if (!len)
  693. return;
  694. }
  695. pgto = base - shift;
  696. _shift_data_left_pages(buf->pages, buf->page_base + pgto,
  697. buf->page_base + base, len);
  698. }
  699. static void xdr_buf_tail_shift_left(const struct xdr_buf *buf,
  700. unsigned int base, unsigned int len,
  701. unsigned int shift)
  702. {
  703. if (!shift || !len)
  704. return;
  705. xdr_buf_tail_copy_left(buf, base, len, shift);
  706. }
  707. static void xdr_buf_pages_shift_left(const struct xdr_buf *buf,
  708. unsigned int base, unsigned int len,
  709. unsigned int shift)
  710. {
  711. if (!shift || !len)
  712. return;
  713. if (base >= buf->page_len) {
  714. xdr_buf_tail_shift_left(buf, base - buf->page_len, len, shift);
  715. return;
  716. }
  717. xdr_buf_pages_copy_left(buf, base, len, shift);
  718. len += base;
  719. if (len <= buf->page_len)
  720. return;
  721. xdr_buf_tail_copy_left(buf, 0, len - buf->page_len, shift);
  722. }
  723. static void xdr_buf_head_shift_left(const struct xdr_buf *buf,
  724. unsigned int base, unsigned int len,
  725. unsigned int shift)
  726. {
  727. const struct kvec *head = buf->head;
  728. unsigned int bytes;
  729. if (!shift || !len)
  730. return;
  731. if (shift > base) {
  732. bytes = (shift - base);
  733. if (bytes >= len)
  734. return;
  735. base += bytes;
  736. len -= bytes;
  737. }
  738. if (base < head->iov_len) {
  739. bytes = min_t(unsigned int, len, head->iov_len - base);
  740. memmove(head->iov_base + (base - shift),
  741. head->iov_base + base, bytes);
  742. base += bytes;
  743. len -= bytes;
  744. }
  745. xdr_buf_pages_shift_left(buf, base - head->iov_len, len, shift);
  746. }
  747. /**
  748. * xdr_shrink_bufhead
  749. * @buf: xdr_buf
  750. * @len: new length of buf->head[0]
  751. *
  752. * Shrinks XDR buffer's header kvec buf->head[0], setting it to
  753. * 'len' bytes. The extra data is not lost, but is instead
  754. * moved into the inlined pages and/or the tail.
  755. */
  756. static unsigned int xdr_shrink_bufhead(struct xdr_buf *buf, unsigned int len)
  757. {
  758. struct kvec *head = buf->head;
  759. unsigned int shift, buflen = max(buf->len, len);
  760. WARN_ON_ONCE(len > head->iov_len);
  761. if (head->iov_len > buflen) {
  762. buf->buflen -= head->iov_len - buflen;
  763. head->iov_len = buflen;
  764. }
  765. if (len >= head->iov_len)
  766. return 0;
  767. shift = head->iov_len - len;
  768. xdr_buf_try_expand(buf, shift);
  769. xdr_buf_head_shift_right(buf, len, buflen - len, shift);
  770. head->iov_len = len;
  771. buf->buflen -= shift;
  772. buf->len -= shift;
  773. return shift;
  774. }
  775. /**
  776. * xdr_shrink_pagelen - shrinks buf->pages to @len bytes
  777. * @buf: xdr_buf
  778. * @len: new page buffer length
  779. *
  780. * The extra data is not lost, but is instead moved into buf->tail.
  781. * Returns the actual number of bytes moved.
  782. */
  783. static unsigned int xdr_shrink_pagelen(struct xdr_buf *buf, unsigned int len)
  784. {
  785. unsigned int shift, buflen = buf->len - buf->head->iov_len;
  786. WARN_ON_ONCE(len > buf->page_len);
  787. if (buf->head->iov_len >= buf->len || len > buflen)
  788. buflen = len;
  789. if (buf->page_len > buflen) {
  790. buf->buflen -= buf->page_len - buflen;
  791. buf->page_len = buflen;
  792. }
  793. if (len >= buf->page_len)
  794. return 0;
  795. shift = buf->page_len - len;
  796. xdr_buf_try_expand(buf, shift);
  797. xdr_buf_pages_shift_right(buf, len, buflen - len, shift);
  798. buf->page_len = len;
  799. buf->len -= shift;
  800. buf->buflen -= shift;
  801. return shift;
  802. }
  803. /**
  804. * xdr_stream_pos - Return the current offset from the start of the xdr_stream
  805. * @xdr: pointer to struct xdr_stream
  806. */
  807. unsigned int xdr_stream_pos(const struct xdr_stream *xdr)
  808. {
  809. return (unsigned int)(XDR_QUADLEN(xdr->buf->len) - xdr->nwords) << 2;
  810. }
  811. EXPORT_SYMBOL_GPL(xdr_stream_pos);
  812. static void xdr_stream_set_pos(struct xdr_stream *xdr, unsigned int pos)
  813. {
  814. unsigned int blen = xdr->buf->len;
  815. xdr->nwords = blen > pos ? XDR_QUADLEN(blen) - XDR_QUADLEN(pos) : 0;
  816. }
  817. static void xdr_stream_page_set_pos(struct xdr_stream *xdr, unsigned int pos)
  818. {
  819. xdr_stream_set_pos(xdr, pos + xdr->buf->head[0].iov_len);
  820. }
  821. /**
  822. * xdr_page_pos - Return the current offset from the start of the xdr pages
  823. * @xdr: pointer to struct xdr_stream
  824. */
  825. unsigned int xdr_page_pos(const struct xdr_stream *xdr)
  826. {
  827. unsigned int pos = xdr_stream_pos(xdr);
  828. WARN_ON(pos < xdr->buf->head[0].iov_len);
  829. return pos - xdr->buf->head[0].iov_len;
  830. }
  831. EXPORT_SYMBOL_GPL(xdr_page_pos);
  832. /**
  833. * xdr_init_encode - Initialize a struct xdr_stream for sending data.
  834. * @xdr: pointer to xdr_stream struct
  835. * @buf: pointer to XDR buffer in which to encode data
  836. * @p: current pointer inside XDR buffer
  837. * @rqst: pointer to controlling rpc_rqst, for debugging
  838. *
  839. * Note: at the moment the RPC client only passes the length of our
  840. * scratch buffer in the xdr_buf's header kvec. Previously this
  841. * meant we needed to call xdr_adjust_iovec() after encoding the
  842. * data. With the new scheme, the xdr_stream manages the details
  843. * of the buffer length, and takes care of adjusting the kvec
  844. * length for us.
  845. */
  846. void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p,
  847. struct rpc_rqst *rqst)
  848. {
  849. struct kvec *iov = buf->head;
  850. int scratch_len = buf->buflen - buf->page_len - buf->tail[0].iov_len;
  851. xdr_reset_scratch_buffer(xdr);
  852. BUG_ON(scratch_len < 0);
  853. xdr->buf = buf;
  854. xdr->iov = iov;
  855. xdr->p = (__be32 *)((char *)iov->iov_base + iov->iov_len);
  856. xdr->end = (__be32 *)((char *)iov->iov_base + scratch_len);
  857. BUG_ON(iov->iov_len > scratch_len);
  858. if (p != xdr->p && p != NULL) {
  859. size_t len;
  860. BUG_ON(p < xdr->p || p > xdr->end);
  861. len = (char *)p - (char *)xdr->p;
  862. xdr->p = p;
  863. buf->len += len;
  864. iov->iov_len += len;
  865. }
  866. xdr->rqst = rqst;
  867. }
  868. EXPORT_SYMBOL_GPL(xdr_init_encode);
  869. /**
  870. * xdr_init_encode_pages - Initialize an xdr_stream for encoding into pages
  871. * @xdr: pointer to xdr_stream struct
  872. * @buf: pointer to XDR buffer into which to encode data
  873. * @pages: list of pages to decode into
  874. * @rqst: pointer to controlling rpc_rqst, for debugging
  875. *
  876. */
  877. void xdr_init_encode_pages(struct xdr_stream *xdr, struct xdr_buf *buf,
  878. struct page **pages, struct rpc_rqst *rqst)
  879. {
  880. xdr_reset_scratch_buffer(xdr);
  881. xdr->buf = buf;
  882. xdr->page_ptr = pages;
  883. xdr->iov = NULL;
  884. xdr->p = page_address(*pages);
  885. xdr->end = (void *)xdr->p + min_t(u32, buf->buflen, PAGE_SIZE);
  886. xdr->rqst = rqst;
  887. }
  888. EXPORT_SYMBOL_GPL(xdr_init_encode_pages);
  889. /**
  890. * __xdr_commit_encode - Ensure all data is written to buffer
  891. * @xdr: pointer to xdr_stream
  892. *
  893. * We handle encoding across page boundaries by giving the caller a
  894. * temporary location to write to, then later copying the data into
  895. * place; xdr_commit_encode does that copying.
  896. *
  897. * Normally the caller doesn't need to call this directly, as the
  898. * following xdr_reserve_space will do it. But an explicit call may be
  899. * required at the end of encoding, or any other time when the xdr_buf
  900. * data might be read.
  901. */
  902. void __xdr_commit_encode(struct xdr_stream *xdr)
  903. {
  904. size_t shift = xdr->scratch.iov_len;
  905. void *page;
  906. page = page_address(*xdr->page_ptr);
  907. memcpy(xdr->scratch.iov_base, page, shift);
  908. memmove(page, page + shift, (void *)xdr->p - page);
  909. xdr_reset_scratch_buffer(xdr);
  910. }
  911. EXPORT_SYMBOL_GPL(__xdr_commit_encode);
  912. /*
  913. * The buffer space to be reserved crosses the boundary between
  914. * xdr->buf->head and xdr->buf->pages, or between two pages
  915. * in xdr->buf->pages.
  916. */
  917. static noinline __be32 *xdr_get_next_encode_buffer(struct xdr_stream *xdr,
  918. size_t nbytes)
  919. {
  920. int space_left;
  921. int frag1bytes, frag2bytes;
  922. void *p;
  923. if (nbytes > PAGE_SIZE)
  924. goto out_overflow; /* Bigger buffers require special handling */
  925. if (xdr->buf->len + nbytes > xdr->buf->buflen)
  926. goto out_overflow; /* Sorry, we're totally out of space */
  927. frag1bytes = (xdr->end - xdr->p) << 2;
  928. frag2bytes = nbytes - frag1bytes;
  929. if (xdr->iov)
  930. xdr->iov->iov_len += frag1bytes;
  931. else
  932. xdr->buf->page_len += frag1bytes;
  933. xdr->page_ptr++;
  934. xdr->iov = NULL;
  935. /*
  936. * If the last encode didn't end exactly on a page boundary, the
  937. * next one will straddle boundaries. Encode into the next
  938. * page, then copy it back later in xdr_commit_encode. We use
  939. * the "scratch" iov to track any temporarily unused fragment of
  940. * space at the end of the previous buffer:
  941. */
  942. xdr_set_scratch_buffer(xdr, xdr->p, frag1bytes);
  943. /*
  944. * xdr->p is where the next encode will start after
  945. * xdr_commit_encode() has shifted this one back:
  946. */
  947. p = page_address(*xdr->page_ptr);
  948. xdr->p = p + frag2bytes;
  949. space_left = xdr->buf->buflen - xdr->buf->len;
  950. if (space_left - frag1bytes >= PAGE_SIZE)
  951. xdr->end = p + PAGE_SIZE;
  952. else
  953. xdr->end = p + space_left - frag1bytes;
  954. xdr->buf->page_len += frag2bytes;
  955. xdr->buf->len += nbytes;
  956. return p;
  957. out_overflow:
  958. trace_rpc_xdr_overflow(xdr, nbytes);
  959. return NULL;
  960. }
  961. /**
  962. * xdr_reserve_space - Reserve buffer space for sending
  963. * @xdr: pointer to xdr_stream
  964. * @nbytes: number of bytes to reserve
  965. *
  966. * Checks that we have enough buffer space to encode 'nbytes' more
  967. * bytes of data. If so, update the total xdr_buf length, and
  968. * adjust the length of the current kvec.
  969. */
  970. __be32 * xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes)
  971. {
  972. __be32 *p = xdr->p;
  973. __be32 *q;
  974. xdr_commit_encode(xdr);
  975. /* align nbytes on the next 32-bit boundary */
  976. nbytes += 3;
  977. nbytes &= ~3;
  978. q = p + (nbytes >> 2);
  979. if (unlikely(q > xdr->end || q < p))
  980. return xdr_get_next_encode_buffer(xdr, nbytes);
  981. xdr->p = q;
  982. if (xdr->iov)
  983. xdr->iov->iov_len += nbytes;
  984. else
  985. xdr->buf->page_len += nbytes;
  986. xdr->buf->len += nbytes;
  987. return p;
  988. }
  989. EXPORT_SYMBOL_GPL(xdr_reserve_space);
  990. /**
  991. * xdr_reserve_space_vec - Reserves a large amount of buffer space for sending
  992. * @xdr: pointer to xdr_stream
  993. * @nbytes: number of bytes to reserve
  994. *
  995. * The size argument passed to xdr_reserve_space() is determined based
  996. * on the number of bytes remaining in the current page to avoid
  997. * invalidating iov_base pointers when xdr_commit_encode() is called.
  998. *
  999. * Return values:
  1000. * %0: success
  1001. * %-EMSGSIZE: not enough space is available in @xdr
  1002. */
  1003. int xdr_reserve_space_vec(struct xdr_stream *xdr, size_t nbytes)
  1004. {
  1005. size_t thislen;
  1006. __be32 *p;
  1007. /*
  1008. * svcrdma requires every READ payload to start somewhere
  1009. * in xdr->pages.
  1010. */
  1011. if (xdr->iov == xdr->buf->head) {
  1012. xdr->iov = NULL;
  1013. xdr->end = xdr->p;
  1014. }
  1015. /* XXX: Let's find a way to make this more efficient */
  1016. while (nbytes) {
  1017. thislen = xdr->buf->page_len % PAGE_SIZE;
  1018. thislen = min_t(size_t, nbytes, PAGE_SIZE - thislen);
  1019. p = xdr_reserve_space(xdr, thislen);
  1020. if (!p)
  1021. return -EMSGSIZE;
  1022. nbytes -= thislen;
  1023. }
  1024. return 0;
  1025. }
  1026. EXPORT_SYMBOL_GPL(xdr_reserve_space_vec);
  1027. /**
  1028. * xdr_truncate_encode - truncate an encode buffer
  1029. * @xdr: pointer to xdr_stream
  1030. * @len: new length of buffer
  1031. *
  1032. * Truncates the xdr stream, so that xdr->buf->len == len,
  1033. * and xdr->p points at offset len from the start of the buffer, and
  1034. * head, tail, and page lengths are adjusted to correspond.
  1035. *
  1036. * If this means moving xdr->p to a different buffer, we assume that
  1037. * the end pointer should be set to the end of the current page,
  1038. * except in the case of the head buffer when we assume the head
  1039. * buffer's current length represents the end of the available buffer.
  1040. *
  1041. * This is *not* safe to use on a buffer that already has inlined page
  1042. * cache pages (as in a zero-copy server read reply), except for the
  1043. * simple case of truncating from one position in the tail to another.
  1044. *
  1045. */
  1046. void xdr_truncate_encode(struct xdr_stream *xdr, size_t len)
  1047. {
  1048. struct xdr_buf *buf = xdr->buf;
  1049. struct kvec *head = buf->head;
  1050. struct kvec *tail = buf->tail;
  1051. int fraglen;
  1052. int new;
  1053. if (len > buf->len) {
  1054. WARN_ON_ONCE(1);
  1055. return;
  1056. }
  1057. xdr_commit_encode(xdr);
  1058. fraglen = min_t(int, buf->len - len, tail->iov_len);
  1059. tail->iov_len -= fraglen;
  1060. buf->len -= fraglen;
  1061. if (tail->iov_len) {
  1062. xdr->p = tail->iov_base + tail->iov_len;
  1063. WARN_ON_ONCE(!xdr->end);
  1064. WARN_ON_ONCE(!xdr->iov);
  1065. return;
  1066. }
  1067. WARN_ON_ONCE(fraglen);
  1068. fraglen = min_t(int, buf->len - len, buf->page_len);
  1069. buf->page_len -= fraglen;
  1070. buf->len -= fraglen;
  1071. new = buf->page_base + buf->page_len;
  1072. xdr->page_ptr = buf->pages + (new >> PAGE_SHIFT);
  1073. if (buf->page_len) {
  1074. xdr->p = page_address(*xdr->page_ptr);
  1075. xdr->end = (void *)xdr->p + PAGE_SIZE;
  1076. xdr->p = (void *)xdr->p + (new % PAGE_SIZE);
  1077. WARN_ON_ONCE(xdr->iov);
  1078. return;
  1079. }
  1080. if (fraglen)
  1081. xdr->end = head->iov_base + head->iov_len;
  1082. /* (otherwise assume xdr->end is already set) */
  1083. xdr->page_ptr--;
  1084. head->iov_len = len;
  1085. buf->len = len;
  1086. xdr->p = head->iov_base + head->iov_len;
  1087. xdr->iov = buf->head;
  1088. }
  1089. EXPORT_SYMBOL(xdr_truncate_encode);
  1090. /**
  1091. * xdr_truncate_decode - Truncate a decoding stream
  1092. * @xdr: pointer to struct xdr_stream
  1093. * @len: Number of bytes to remove
  1094. *
  1095. */
  1096. void xdr_truncate_decode(struct xdr_stream *xdr, size_t len)
  1097. {
  1098. unsigned int nbytes = xdr_align_size(len);
  1099. xdr->buf->len -= nbytes;
  1100. xdr->nwords -= XDR_QUADLEN(nbytes);
  1101. }
  1102. EXPORT_SYMBOL_GPL(xdr_truncate_decode);
  1103. /**
  1104. * xdr_restrict_buflen - decrease available buffer space
  1105. * @xdr: pointer to xdr_stream
  1106. * @newbuflen: new maximum number of bytes available
  1107. *
  1108. * Adjust our idea of how much space is available in the buffer.
  1109. * If we've already used too much space in the buffer, returns -1.
  1110. * If the available space is already smaller than newbuflen, returns 0
  1111. * and does nothing. Otherwise, adjusts xdr->buf->buflen to newbuflen
  1112. * and ensures xdr->end is set at most offset newbuflen from the start
  1113. * of the buffer.
  1114. */
  1115. int xdr_restrict_buflen(struct xdr_stream *xdr, int newbuflen)
  1116. {
  1117. struct xdr_buf *buf = xdr->buf;
  1118. int left_in_this_buf = (void *)xdr->end - (void *)xdr->p;
  1119. int end_offset = buf->len + left_in_this_buf;
  1120. if (newbuflen < 0 || newbuflen < buf->len)
  1121. return -1;
  1122. if (newbuflen > buf->buflen)
  1123. return 0;
  1124. if (newbuflen < end_offset)
  1125. xdr->end = (void *)xdr->end + newbuflen - end_offset;
  1126. buf->buflen = newbuflen;
  1127. return 0;
  1128. }
  1129. EXPORT_SYMBOL(xdr_restrict_buflen);
  1130. /**
  1131. * xdr_write_pages - Insert a list of pages into an XDR buffer for sending
  1132. * @xdr: pointer to xdr_stream
  1133. * @pages: array of pages to insert
  1134. * @base: starting offset of first data byte in @pages
  1135. * @len: number of data bytes in @pages to insert
  1136. *
  1137. * After the @pages are added, the tail iovec is instantiated pointing to
  1138. * end of the head buffer, and the stream is set up to encode subsequent
  1139. * items into the tail.
  1140. */
  1141. void xdr_write_pages(struct xdr_stream *xdr, struct page **pages, unsigned int base,
  1142. unsigned int len)
  1143. {
  1144. struct xdr_buf *buf = xdr->buf;
  1145. struct kvec *tail = buf->tail;
  1146. buf->pages = pages;
  1147. buf->page_base = base;
  1148. buf->page_len = len;
  1149. tail->iov_base = xdr->p;
  1150. tail->iov_len = 0;
  1151. xdr->iov = tail;
  1152. if (len & 3) {
  1153. unsigned int pad = 4 - (len & 3);
  1154. BUG_ON(xdr->p >= xdr->end);
  1155. tail->iov_base = (char *)xdr->p + (len & 3);
  1156. tail->iov_len += pad;
  1157. len += pad;
  1158. *xdr->p++ = 0;
  1159. }
  1160. buf->buflen += len;
  1161. buf->len += len;
  1162. }
  1163. EXPORT_SYMBOL_GPL(xdr_write_pages);
  1164. static unsigned int xdr_set_iov(struct xdr_stream *xdr, struct kvec *iov,
  1165. unsigned int base, unsigned int len)
  1166. {
  1167. if (len > iov->iov_len)
  1168. len = iov->iov_len;
  1169. if (unlikely(base > len))
  1170. base = len;
  1171. xdr->p = (__be32*)(iov->iov_base + base);
  1172. xdr->end = (__be32*)(iov->iov_base + len);
  1173. xdr->iov = iov;
  1174. xdr->page_ptr = NULL;
  1175. return len - base;
  1176. }
  1177. static unsigned int xdr_set_tail_base(struct xdr_stream *xdr,
  1178. unsigned int base, unsigned int len)
  1179. {
  1180. struct xdr_buf *buf = xdr->buf;
  1181. xdr_stream_set_pos(xdr, base + buf->page_len + buf->head->iov_len);
  1182. return xdr_set_iov(xdr, buf->tail, base, len);
  1183. }
  1184. static void xdr_stream_unmap_current_page(struct xdr_stream *xdr)
  1185. {
  1186. if (xdr->page_kaddr) {
  1187. kunmap_local(xdr->page_kaddr);
  1188. xdr->page_kaddr = NULL;
  1189. }
  1190. }
  1191. static unsigned int xdr_set_page_base(struct xdr_stream *xdr,
  1192. unsigned int base, unsigned int len)
  1193. {
  1194. unsigned int pgnr;
  1195. unsigned int maxlen;
  1196. unsigned int pgoff;
  1197. unsigned int pgend;
  1198. void *kaddr;
  1199. maxlen = xdr->buf->page_len;
  1200. if (base >= maxlen)
  1201. return 0;
  1202. else
  1203. maxlen -= base;
  1204. if (len > maxlen)
  1205. len = maxlen;
  1206. xdr_stream_unmap_current_page(xdr);
  1207. xdr_stream_page_set_pos(xdr, base);
  1208. base += xdr->buf->page_base;
  1209. pgnr = base >> PAGE_SHIFT;
  1210. xdr->page_ptr = &xdr->buf->pages[pgnr];
  1211. if (PageHighMem(*xdr->page_ptr)) {
  1212. xdr->page_kaddr = kmap_local_page(*xdr->page_ptr);
  1213. kaddr = xdr->page_kaddr;
  1214. } else
  1215. kaddr = page_address(*xdr->page_ptr);
  1216. pgoff = base & ~PAGE_MASK;
  1217. xdr->p = (__be32*)(kaddr + pgoff);
  1218. pgend = pgoff + len;
  1219. if (pgend > PAGE_SIZE)
  1220. pgend = PAGE_SIZE;
  1221. xdr->end = (__be32*)(kaddr + pgend);
  1222. xdr->iov = NULL;
  1223. return len;
  1224. }
  1225. static void xdr_set_page(struct xdr_stream *xdr, unsigned int base,
  1226. unsigned int len)
  1227. {
  1228. if (xdr_set_page_base(xdr, base, len) == 0) {
  1229. base -= xdr->buf->page_len;
  1230. xdr_set_tail_base(xdr, base, len);
  1231. }
  1232. }
  1233. static void xdr_set_next_page(struct xdr_stream *xdr)
  1234. {
  1235. unsigned int newbase;
  1236. newbase = (1 + xdr->page_ptr - xdr->buf->pages) << PAGE_SHIFT;
  1237. newbase -= xdr->buf->page_base;
  1238. if (newbase < xdr->buf->page_len)
  1239. xdr_set_page_base(xdr, newbase, xdr_stream_remaining(xdr));
  1240. else
  1241. xdr_set_tail_base(xdr, 0, xdr_stream_remaining(xdr));
  1242. }
  1243. static bool xdr_set_next_buffer(struct xdr_stream *xdr)
  1244. {
  1245. if (xdr->page_ptr != NULL)
  1246. xdr_set_next_page(xdr);
  1247. else if (xdr->iov == xdr->buf->head)
  1248. xdr_set_page(xdr, 0, xdr_stream_remaining(xdr));
  1249. return xdr->p != xdr->end;
  1250. }
  1251. /**
  1252. * xdr_init_decode - Initialize an xdr_stream for decoding data.
  1253. * @xdr: pointer to xdr_stream struct
  1254. * @buf: pointer to XDR buffer from which to decode data
  1255. * @p: current pointer inside XDR buffer
  1256. * @rqst: pointer to controlling rpc_rqst, for debugging
  1257. */
  1258. void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p,
  1259. struct rpc_rqst *rqst)
  1260. {
  1261. xdr->buf = buf;
  1262. xdr->page_kaddr = NULL;
  1263. xdr_reset_scratch_buffer(xdr);
  1264. xdr->nwords = XDR_QUADLEN(buf->len);
  1265. if (xdr_set_iov(xdr, buf->head, 0, buf->len) == 0 &&
  1266. xdr_set_page_base(xdr, 0, buf->len) == 0)
  1267. xdr_set_iov(xdr, buf->tail, 0, buf->len);
  1268. if (p != NULL && p > xdr->p && xdr->end >= p) {
  1269. xdr->nwords -= p - xdr->p;
  1270. xdr->p = p;
  1271. }
  1272. xdr->rqst = rqst;
  1273. }
  1274. EXPORT_SYMBOL_GPL(xdr_init_decode);
  1275. /**
  1276. * xdr_init_decode_pages - Initialize an xdr_stream for decoding into pages
  1277. * @xdr: pointer to xdr_stream struct
  1278. * @buf: pointer to XDR buffer from which to decode data
  1279. * @pages: list of pages to decode into
  1280. * @len: length in bytes of buffer in pages
  1281. */
  1282. void xdr_init_decode_pages(struct xdr_stream *xdr, struct xdr_buf *buf,
  1283. struct page **pages, unsigned int len)
  1284. {
  1285. memset(buf, 0, sizeof(*buf));
  1286. buf->pages = pages;
  1287. buf->page_len = len;
  1288. buf->buflen = len;
  1289. buf->len = len;
  1290. xdr_init_decode(xdr, buf, NULL, NULL);
  1291. }
  1292. EXPORT_SYMBOL_GPL(xdr_init_decode_pages);
  1293. /**
  1294. * xdr_finish_decode - Clean up the xdr_stream after decoding data.
  1295. * @xdr: pointer to xdr_stream struct
  1296. */
  1297. void xdr_finish_decode(struct xdr_stream *xdr)
  1298. {
  1299. xdr_stream_unmap_current_page(xdr);
  1300. }
  1301. EXPORT_SYMBOL(xdr_finish_decode);
  1302. static __be32 * __xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
  1303. {
  1304. unsigned int nwords = XDR_QUADLEN(nbytes);
  1305. __be32 *p = xdr->p;
  1306. __be32 *q = p + nwords;
  1307. if (unlikely(nwords > xdr->nwords || q > xdr->end || q < p))
  1308. return NULL;
  1309. xdr->p = q;
  1310. xdr->nwords -= nwords;
  1311. return p;
  1312. }
  1313. static __be32 *xdr_copy_to_scratch(struct xdr_stream *xdr, size_t nbytes)
  1314. {
  1315. __be32 *p;
  1316. char *cpdest = xdr->scratch.iov_base;
  1317. size_t cplen = (char *)xdr->end - (char *)xdr->p;
  1318. if (nbytes > xdr->scratch.iov_len)
  1319. goto out_overflow;
  1320. p = __xdr_inline_decode(xdr, cplen);
  1321. if (p == NULL)
  1322. return NULL;
  1323. memcpy(cpdest, p, cplen);
  1324. if (!xdr_set_next_buffer(xdr))
  1325. goto out_overflow;
  1326. cpdest += cplen;
  1327. nbytes -= cplen;
  1328. p = __xdr_inline_decode(xdr, nbytes);
  1329. if (p == NULL)
  1330. return NULL;
  1331. memcpy(cpdest, p, nbytes);
  1332. return xdr->scratch.iov_base;
  1333. out_overflow:
  1334. trace_rpc_xdr_overflow(xdr, nbytes);
  1335. return NULL;
  1336. }
  1337. /**
  1338. * xdr_inline_decode - Retrieve XDR data to decode
  1339. * @xdr: pointer to xdr_stream struct
  1340. * @nbytes: number of bytes of data to decode
  1341. *
  1342. * Check if the input buffer is long enough to enable us to decode
  1343. * 'nbytes' more bytes of data starting at the current position.
  1344. * If so return the current pointer, then update the current
  1345. * pointer position.
  1346. */
  1347. __be32 * xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes)
  1348. {
  1349. __be32 *p;
  1350. if (unlikely(nbytes == 0))
  1351. return xdr->p;
  1352. if (xdr->p == xdr->end && !xdr_set_next_buffer(xdr))
  1353. goto out_overflow;
  1354. p = __xdr_inline_decode(xdr, nbytes);
  1355. if (p != NULL)
  1356. return p;
  1357. return xdr_copy_to_scratch(xdr, nbytes);
  1358. out_overflow:
  1359. trace_rpc_xdr_overflow(xdr, nbytes);
  1360. return NULL;
  1361. }
  1362. EXPORT_SYMBOL_GPL(xdr_inline_decode);
  1363. static void xdr_realign_pages(struct xdr_stream *xdr)
  1364. {
  1365. struct xdr_buf *buf = xdr->buf;
  1366. struct kvec *iov = buf->head;
  1367. unsigned int cur = xdr_stream_pos(xdr);
  1368. unsigned int copied;
  1369. /* Realign pages to current pointer position */
  1370. if (iov->iov_len > cur) {
  1371. copied = xdr_shrink_bufhead(buf, cur);
  1372. trace_rpc_xdr_alignment(xdr, cur, copied);
  1373. xdr_set_page(xdr, 0, buf->page_len);
  1374. }
  1375. }
  1376. static unsigned int xdr_align_pages(struct xdr_stream *xdr, unsigned int len)
  1377. {
  1378. struct xdr_buf *buf = xdr->buf;
  1379. unsigned int nwords = XDR_QUADLEN(len);
  1380. unsigned int copied;
  1381. if (xdr->nwords == 0)
  1382. return 0;
  1383. xdr_realign_pages(xdr);
  1384. if (nwords > xdr->nwords) {
  1385. nwords = xdr->nwords;
  1386. len = nwords << 2;
  1387. }
  1388. if (buf->page_len <= len)
  1389. len = buf->page_len;
  1390. else if (nwords < xdr->nwords) {
  1391. /* Truncate page data and move it into the tail */
  1392. copied = xdr_shrink_pagelen(buf, len);
  1393. trace_rpc_xdr_alignment(xdr, len, copied);
  1394. }
  1395. return len;
  1396. }
  1397. /**
  1398. * xdr_read_pages - align page-based XDR data to current pointer position
  1399. * @xdr: pointer to xdr_stream struct
  1400. * @len: number of bytes of page data
  1401. *
  1402. * Moves data beyond the current pointer position from the XDR head[] buffer
  1403. * into the page list. Any data that lies beyond current position + @len
  1404. * bytes is moved into the XDR tail[]. The xdr_stream current position is
  1405. * then advanced past that data to align to the next XDR object in the tail.
  1406. *
  1407. * Returns the number of XDR encoded bytes now contained in the pages
  1408. */
  1409. unsigned int xdr_read_pages(struct xdr_stream *xdr, unsigned int len)
  1410. {
  1411. unsigned int nwords = XDR_QUADLEN(len);
  1412. unsigned int base, end, pglen;
  1413. pglen = xdr_align_pages(xdr, nwords << 2);
  1414. if (pglen == 0)
  1415. return 0;
  1416. base = (nwords << 2) - pglen;
  1417. end = xdr_stream_remaining(xdr) - pglen;
  1418. xdr_set_tail_base(xdr, base, end);
  1419. return len <= pglen ? len : pglen;
  1420. }
  1421. EXPORT_SYMBOL_GPL(xdr_read_pages);
  1422. /**
  1423. * xdr_set_pagelen - Sets the length of the XDR pages
  1424. * @xdr: pointer to xdr_stream struct
  1425. * @len: new length of the XDR page data
  1426. *
  1427. * Either grows or shrinks the length of the xdr pages by setting pagelen to
  1428. * @len bytes. When shrinking, any extra data is moved into buf->tail, whereas
  1429. * when growing any data beyond the current pointer is moved into the tail.
  1430. *
  1431. * Returns True if the operation was successful, and False otherwise.
  1432. */
  1433. void xdr_set_pagelen(struct xdr_stream *xdr, unsigned int len)
  1434. {
  1435. struct xdr_buf *buf = xdr->buf;
  1436. size_t remaining = xdr_stream_remaining(xdr);
  1437. size_t base = 0;
  1438. if (len < buf->page_len) {
  1439. base = buf->page_len - len;
  1440. xdr_shrink_pagelen(buf, len);
  1441. } else {
  1442. xdr_buf_head_shift_right(buf, xdr_stream_pos(xdr),
  1443. buf->page_len, remaining);
  1444. if (len > buf->page_len)
  1445. xdr_buf_try_expand(buf, len - buf->page_len);
  1446. }
  1447. xdr_set_tail_base(xdr, base, remaining);
  1448. }
  1449. EXPORT_SYMBOL_GPL(xdr_set_pagelen);
  1450. /**
  1451. * xdr_enter_page - decode data from the XDR page
  1452. * @xdr: pointer to xdr_stream struct
  1453. * @len: number of bytes of page data
  1454. *
  1455. * Moves data beyond the current pointer position from the XDR head[] buffer
  1456. * into the page list. Any data that lies beyond current position + "len"
  1457. * bytes is moved into the XDR tail[]. The current pointer is then
  1458. * repositioned at the beginning of the first XDR page.
  1459. */
  1460. void xdr_enter_page(struct xdr_stream *xdr, unsigned int len)
  1461. {
  1462. len = xdr_align_pages(xdr, len);
  1463. /*
  1464. * Position current pointer at beginning of tail, and
  1465. * set remaining message length.
  1466. */
  1467. if (len != 0)
  1468. xdr_set_page_base(xdr, 0, len);
  1469. }
  1470. EXPORT_SYMBOL_GPL(xdr_enter_page);
  1471. static const struct kvec empty_iov = {.iov_base = NULL, .iov_len = 0};
  1472. void xdr_buf_from_iov(const struct kvec *iov, struct xdr_buf *buf)
  1473. {
  1474. buf->head[0] = *iov;
  1475. buf->tail[0] = empty_iov;
  1476. buf->page_len = 0;
  1477. buf->buflen = buf->len = iov->iov_len;
  1478. }
  1479. EXPORT_SYMBOL_GPL(xdr_buf_from_iov);
  1480. /**
  1481. * xdr_buf_subsegment - set subbuf to a portion of buf
  1482. * @buf: an xdr buffer
  1483. * @subbuf: the result buffer
  1484. * @base: beginning of range in bytes
  1485. * @len: length of range in bytes
  1486. *
  1487. * sets @subbuf to an xdr buffer representing the portion of @buf of
  1488. * length @len starting at offset @base.
  1489. *
  1490. * @buf and @subbuf may be pointers to the same struct xdr_buf.
  1491. *
  1492. * Returns -1 if base or length are out of bounds.
  1493. */
  1494. int xdr_buf_subsegment(const struct xdr_buf *buf, struct xdr_buf *subbuf,
  1495. unsigned int base, unsigned int len)
  1496. {
  1497. subbuf->buflen = subbuf->len = len;
  1498. if (base < buf->head[0].iov_len) {
  1499. subbuf->head[0].iov_base = buf->head[0].iov_base + base;
  1500. subbuf->head[0].iov_len = min_t(unsigned int, len,
  1501. buf->head[0].iov_len - base);
  1502. len -= subbuf->head[0].iov_len;
  1503. base = 0;
  1504. } else {
  1505. base -= buf->head[0].iov_len;
  1506. subbuf->head[0].iov_base = buf->head[0].iov_base;
  1507. subbuf->head[0].iov_len = 0;
  1508. }
  1509. if (base < buf->page_len) {
  1510. subbuf->page_len = min(buf->page_len - base, len);
  1511. base += buf->page_base;
  1512. subbuf->page_base = base & ~PAGE_MASK;
  1513. subbuf->pages = &buf->pages[base >> PAGE_SHIFT];
  1514. len -= subbuf->page_len;
  1515. base = 0;
  1516. } else {
  1517. base -= buf->page_len;
  1518. subbuf->pages = buf->pages;
  1519. subbuf->page_base = 0;
  1520. subbuf->page_len = 0;
  1521. }
  1522. if (base < buf->tail[0].iov_len) {
  1523. subbuf->tail[0].iov_base = buf->tail[0].iov_base + base;
  1524. subbuf->tail[0].iov_len = min_t(unsigned int, len,
  1525. buf->tail[0].iov_len - base);
  1526. len -= subbuf->tail[0].iov_len;
  1527. base = 0;
  1528. } else {
  1529. base -= buf->tail[0].iov_len;
  1530. subbuf->tail[0].iov_base = buf->tail[0].iov_base;
  1531. subbuf->tail[0].iov_len = 0;
  1532. }
  1533. if (base || len)
  1534. return -1;
  1535. return 0;
  1536. }
  1537. EXPORT_SYMBOL_GPL(xdr_buf_subsegment);
  1538. /**
  1539. * xdr_stream_subsegment - set @subbuf to a portion of @xdr
  1540. * @xdr: an xdr_stream set up for decoding
  1541. * @subbuf: the result buffer
  1542. * @nbytes: length of @xdr to extract, in bytes
  1543. *
  1544. * Sets up @subbuf to represent a portion of @xdr. The portion
  1545. * starts at the current offset in @xdr, and extends for a length
  1546. * of @nbytes. If this is successful, @xdr is advanced to the next
  1547. * XDR data item following that portion.
  1548. *
  1549. * Return values:
  1550. * %true: @subbuf has been initialized, and @xdr has been advanced.
  1551. * %false: a bounds error has occurred
  1552. */
  1553. bool xdr_stream_subsegment(struct xdr_stream *xdr, struct xdr_buf *subbuf,
  1554. unsigned int nbytes)
  1555. {
  1556. unsigned int start = xdr_stream_pos(xdr);
  1557. unsigned int remaining, len;
  1558. /* Extract @subbuf and bounds-check the fn arguments */
  1559. if (xdr_buf_subsegment(xdr->buf, subbuf, start, nbytes))
  1560. return false;
  1561. /* Advance @xdr by @nbytes */
  1562. for (remaining = nbytes; remaining;) {
  1563. if (xdr->p == xdr->end && !xdr_set_next_buffer(xdr))
  1564. return false;
  1565. len = (char *)xdr->end - (char *)xdr->p;
  1566. if (remaining <= len) {
  1567. xdr->p = (__be32 *)((char *)xdr->p +
  1568. (remaining + xdr_pad_size(nbytes)));
  1569. break;
  1570. }
  1571. xdr->p = (__be32 *)((char *)xdr->p + len);
  1572. xdr->end = xdr->p;
  1573. remaining -= len;
  1574. }
  1575. xdr_stream_set_pos(xdr, start + nbytes);
  1576. return true;
  1577. }
  1578. EXPORT_SYMBOL_GPL(xdr_stream_subsegment);
  1579. /**
  1580. * xdr_stream_move_subsegment - Move part of a stream to another position
  1581. * @xdr: the source xdr_stream
  1582. * @offset: the source offset of the segment
  1583. * @target: the target offset of the segment
  1584. * @length: the number of bytes to move
  1585. *
  1586. * Moves @length bytes from @offset to @target in the xdr_stream, overwriting
  1587. * anything in its space. Returns the number of bytes in the segment.
  1588. */
  1589. unsigned int xdr_stream_move_subsegment(struct xdr_stream *xdr, unsigned int offset,
  1590. unsigned int target, unsigned int length)
  1591. {
  1592. struct xdr_buf buf;
  1593. unsigned int shift;
  1594. if (offset < target) {
  1595. shift = target - offset;
  1596. if (xdr_buf_subsegment(xdr->buf, &buf, offset, shift + length) < 0)
  1597. return 0;
  1598. xdr_buf_head_shift_right(&buf, 0, length, shift);
  1599. } else if (offset > target) {
  1600. shift = offset - target;
  1601. if (xdr_buf_subsegment(xdr->buf, &buf, target, shift + length) < 0)
  1602. return 0;
  1603. xdr_buf_head_shift_left(&buf, shift, length, shift);
  1604. }
  1605. return length;
  1606. }
  1607. EXPORT_SYMBOL_GPL(xdr_stream_move_subsegment);
  1608. /**
  1609. * xdr_stream_zero - zero out a portion of an xdr_stream
  1610. * @xdr: an xdr_stream to zero out
  1611. * @offset: the starting point in the stream
  1612. * @length: the number of bytes to zero
  1613. */
  1614. unsigned int xdr_stream_zero(struct xdr_stream *xdr, unsigned int offset,
  1615. unsigned int length)
  1616. {
  1617. struct xdr_buf buf;
  1618. if (xdr_buf_subsegment(xdr->buf, &buf, offset, length) < 0)
  1619. return 0;
  1620. if (buf.head[0].iov_len)
  1621. xdr_buf_iov_zero(buf.head, 0, buf.head[0].iov_len);
  1622. if (buf.page_len > 0)
  1623. xdr_buf_pages_zero(&buf, 0, buf.page_len);
  1624. if (buf.tail[0].iov_len)
  1625. xdr_buf_iov_zero(buf.tail, 0, buf.tail[0].iov_len);
  1626. return length;
  1627. }
  1628. EXPORT_SYMBOL_GPL(xdr_stream_zero);
  1629. /**
  1630. * xdr_buf_trim - lop at most "len" bytes off the end of "buf"
  1631. * @buf: buf to be trimmed
  1632. * @len: number of bytes to reduce "buf" by
  1633. *
  1634. * Trim an xdr_buf by the given number of bytes by fixing up the lengths. Note
  1635. * that it's possible that we'll trim less than that amount if the xdr_buf is
  1636. * too small, or if (for instance) it's all in the head and the parser has
  1637. * already read too far into it.
  1638. */
  1639. void xdr_buf_trim(struct xdr_buf *buf, unsigned int len)
  1640. {
  1641. size_t cur;
  1642. unsigned int trim = len;
  1643. if (buf->tail[0].iov_len) {
  1644. cur = min_t(size_t, buf->tail[0].iov_len, trim);
  1645. buf->tail[0].iov_len -= cur;
  1646. trim -= cur;
  1647. if (!trim)
  1648. goto fix_len;
  1649. }
  1650. if (buf->page_len) {
  1651. cur = min_t(unsigned int, buf->page_len, trim);
  1652. buf->page_len -= cur;
  1653. trim -= cur;
  1654. if (!trim)
  1655. goto fix_len;
  1656. }
  1657. if (buf->head[0].iov_len) {
  1658. cur = min_t(size_t, buf->head[0].iov_len, trim);
  1659. buf->head[0].iov_len -= cur;
  1660. trim -= cur;
  1661. }
  1662. fix_len:
  1663. buf->len -= (len - trim);
  1664. }
  1665. EXPORT_SYMBOL_GPL(xdr_buf_trim);
  1666. static void __read_bytes_from_xdr_buf(const struct xdr_buf *subbuf,
  1667. void *obj, unsigned int len)
  1668. {
  1669. unsigned int this_len;
  1670. this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
  1671. memcpy(obj, subbuf->head[0].iov_base, this_len);
  1672. len -= this_len;
  1673. obj += this_len;
  1674. this_len = min_t(unsigned int, len, subbuf->page_len);
  1675. _copy_from_pages(obj, subbuf->pages, subbuf->page_base, this_len);
  1676. len -= this_len;
  1677. obj += this_len;
  1678. this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
  1679. memcpy(obj, subbuf->tail[0].iov_base, this_len);
  1680. }
  1681. /* obj is assumed to point to allocated memory of size at least len: */
  1682. int read_bytes_from_xdr_buf(const struct xdr_buf *buf, unsigned int base,
  1683. void *obj, unsigned int len)
  1684. {
  1685. struct xdr_buf subbuf;
  1686. int status;
  1687. status = xdr_buf_subsegment(buf, &subbuf, base, len);
  1688. if (status != 0)
  1689. return status;
  1690. __read_bytes_from_xdr_buf(&subbuf, obj, len);
  1691. return 0;
  1692. }
  1693. EXPORT_SYMBOL_GPL(read_bytes_from_xdr_buf);
  1694. static void __write_bytes_to_xdr_buf(const struct xdr_buf *subbuf,
  1695. void *obj, unsigned int len)
  1696. {
  1697. unsigned int this_len;
  1698. this_len = min_t(unsigned int, len, subbuf->head[0].iov_len);
  1699. memcpy(subbuf->head[0].iov_base, obj, this_len);
  1700. len -= this_len;
  1701. obj += this_len;
  1702. this_len = min_t(unsigned int, len, subbuf->page_len);
  1703. _copy_to_pages(subbuf->pages, subbuf->page_base, obj, this_len);
  1704. len -= this_len;
  1705. obj += this_len;
  1706. this_len = min_t(unsigned int, len, subbuf->tail[0].iov_len);
  1707. memcpy(subbuf->tail[0].iov_base, obj, this_len);
  1708. }
  1709. /* obj is assumed to point to allocated memory of size at least len: */
  1710. int write_bytes_to_xdr_buf(const struct xdr_buf *buf, unsigned int base,
  1711. void *obj, unsigned int len)
  1712. {
  1713. struct xdr_buf subbuf;
  1714. int status;
  1715. status = xdr_buf_subsegment(buf, &subbuf, base, len);
  1716. if (status != 0)
  1717. return status;
  1718. __write_bytes_to_xdr_buf(&subbuf, obj, len);
  1719. return 0;
  1720. }
  1721. EXPORT_SYMBOL_GPL(write_bytes_to_xdr_buf);
  1722. int xdr_decode_word(const struct xdr_buf *buf, unsigned int base, u32 *obj)
  1723. {
  1724. __be32 raw;
  1725. int status;
  1726. status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
  1727. if (status)
  1728. return status;
  1729. *obj = be32_to_cpu(raw);
  1730. return 0;
  1731. }
  1732. EXPORT_SYMBOL_GPL(xdr_decode_word);
  1733. int xdr_encode_word(const struct xdr_buf *buf, unsigned int base, u32 obj)
  1734. {
  1735. __be32 raw = cpu_to_be32(obj);
  1736. return write_bytes_to_xdr_buf(buf, base, &raw, sizeof(obj));
  1737. }
  1738. EXPORT_SYMBOL_GPL(xdr_encode_word);
  1739. /* Returns 0 on success, or else a negative error code. */
  1740. static int xdr_xcode_array2(const struct xdr_buf *buf, unsigned int base,
  1741. struct xdr_array2_desc *desc, int encode)
  1742. {
  1743. char *elem = NULL, *c;
  1744. unsigned int copied = 0, todo, avail_here;
  1745. struct page **ppages = NULL;
  1746. int err;
  1747. if (encode) {
  1748. if (xdr_encode_word(buf, base, desc->array_len) != 0)
  1749. return -EINVAL;
  1750. } else {
  1751. if (xdr_decode_word(buf, base, &desc->array_len) != 0 ||
  1752. desc->array_len > desc->array_maxlen ||
  1753. (unsigned long) base + 4 + desc->array_len *
  1754. desc->elem_size > buf->len)
  1755. return -EINVAL;
  1756. }
  1757. base += 4;
  1758. if (!desc->xcode)
  1759. return 0;
  1760. todo = desc->array_len * desc->elem_size;
  1761. /* process head */
  1762. if (todo && base < buf->head->iov_len) {
  1763. c = buf->head->iov_base + base;
  1764. avail_here = min_t(unsigned int, todo,
  1765. buf->head->iov_len - base);
  1766. todo -= avail_here;
  1767. while (avail_here >= desc->elem_size) {
  1768. err = desc->xcode(desc, c);
  1769. if (err)
  1770. goto out;
  1771. c += desc->elem_size;
  1772. avail_here -= desc->elem_size;
  1773. }
  1774. if (avail_here) {
  1775. if (!elem) {
  1776. elem = kmalloc(desc->elem_size, GFP_KERNEL);
  1777. err = -ENOMEM;
  1778. if (!elem)
  1779. goto out;
  1780. }
  1781. if (encode) {
  1782. err = desc->xcode(desc, elem);
  1783. if (err)
  1784. goto out;
  1785. memcpy(c, elem, avail_here);
  1786. } else
  1787. memcpy(elem, c, avail_here);
  1788. copied = avail_here;
  1789. }
  1790. base = buf->head->iov_len; /* align to start of pages */
  1791. }
  1792. /* process pages array */
  1793. base -= buf->head->iov_len;
  1794. if (todo && base < buf->page_len) {
  1795. unsigned int avail_page;
  1796. avail_here = min(todo, buf->page_len - base);
  1797. todo -= avail_here;
  1798. base += buf->page_base;
  1799. ppages = buf->pages + (base >> PAGE_SHIFT);
  1800. base &= ~PAGE_MASK;
  1801. avail_page = min_t(unsigned int, PAGE_SIZE - base,
  1802. avail_here);
  1803. c = kmap(*ppages) + base;
  1804. while (avail_here) {
  1805. avail_here -= avail_page;
  1806. if (copied || avail_page < desc->elem_size) {
  1807. unsigned int l = min(avail_page,
  1808. desc->elem_size - copied);
  1809. if (!elem) {
  1810. elem = kmalloc(desc->elem_size,
  1811. GFP_KERNEL);
  1812. err = -ENOMEM;
  1813. if (!elem)
  1814. goto out;
  1815. }
  1816. if (encode) {
  1817. if (!copied) {
  1818. err = desc->xcode(desc, elem);
  1819. if (err)
  1820. goto out;
  1821. }
  1822. memcpy(c, elem + copied, l);
  1823. copied += l;
  1824. if (copied == desc->elem_size)
  1825. copied = 0;
  1826. } else {
  1827. memcpy(elem + copied, c, l);
  1828. copied += l;
  1829. if (copied == desc->elem_size) {
  1830. err = desc->xcode(desc, elem);
  1831. if (err)
  1832. goto out;
  1833. copied = 0;
  1834. }
  1835. }
  1836. avail_page -= l;
  1837. c += l;
  1838. }
  1839. while (avail_page >= desc->elem_size) {
  1840. err = desc->xcode(desc, c);
  1841. if (err)
  1842. goto out;
  1843. c += desc->elem_size;
  1844. avail_page -= desc->elem_size;
  1845. }
  1846. if (avail_page) {
  1847. unsigned int l = min(avail_page,
  1848. desc->elem_size - copied);
  1849. if (!elem) {
  1850. elem = kmalloc(desc->elem_size,
  1851. GFP_KERNEL);
  1852. err = -ENOMEM;
  1853. if (!elem)
  1854. goto out;
  1855. }
  1856. if (encode) {
  1857. if (!copied) {
  1858. err = desc->xcode(desc, elem);
  1859. if (err)
  1860. goto out;
  1861. }
  1862. memcpy(c, elem + copied, l);
  1863. copied += l;
  1864. if (copied == desc->elem_size)
  1865. copied = 0;
  1866. } else {
  1867. memcpy(elem + copied, c, l);
  1868. copied += l;
  1869. if (copied == desc->elem_size) {
  1870. err = desc->xcode(desc, elem);
  1871. if (err)
  1872. goto out;
  1873. copied = 0;
  1874. }
  1875. }
  1876. }
  1877. if (avail_here) {
  1878. kunmap(*ppages);
  1879. ppages++;
  1880. c = kmap(*ppages);
  1881. }
  1882. avail_page = min(avail_here,
  1883. (unsigned int) PAGE_SIZE);
  1884. }
  1885. base = buf->page_len; /* align to start of tail */
  1886. }
  1887. /* process tail */
  1888. base -= buf->page_len;
  1889. if (todo) {
  1890. c = buf->tail->iov_base + base;
  1891. if (copied) {
  1892. unsigned int l = desc->elem_size - copied;
  1893. if (encode)
  1894. memcpy(c, elem + copied, l);
  1895. else {
  1896. memcpy(elem + copied, c, l);
  1897. err = desc->xcode(desc, elem);
  1898. if (err)
  1899. goto out;
  1900. }
  1901. todo -= l;
  1902. c += l;
  1903. }
  1904. while (todo) {
  1905. err = desc->xcode(desc, c);
  1906. if (err)
  1907. goto out;
  1908. c += desc->elem_size;
  1909. todo -= desc->elem_size;
  1910. }
  1911. }
  1912. err = 0;
  1913. out:
  1914. kfree(elem);
  1915. if (ppages)
  1916. kunmap(*ppages);
  1917. return err;
  1918. }
  1919. int xdr_decode_array2(const struct xdr_buf *buf, unsigned int base,
  1920. struct xdr_array2_desc *desc)
  1921. {
  1922. if (base >= buf->len)
  1923. return -EINVAL;
  1924. return xdr_xcode_array2(buf, base, desc, 0);
  1925. }
  1926. EXPORT_SYMBOL_GPL(xdr_decode_array2);
  1927. int xdr_encode_array2(const struct xdr_buf *buf, unsigned int base,
  1928. struct xdr_array2_desc *desc)
  1929. {
  1930. if ((unsigned long) base + 4 + desc->array_len * desc->elem_size >
  1931. buf->head->iov_len + buf->page_len + buf->tail->iov_len)
  1932. return -EINVAL;
  1933. return xdr_xcode_array2(buf, base, desc, 1);
  1934. }
  1935. EXPORT_SYMBOL_GPL(xdr_encode_array2);
  1936. int xdr_process_buf(const struct xdr_buf *buf, unsigned int offset,
  1937. unsigned int len,
  1938. int (*actor)(struct scatterlist *, void *), void *data)
  1939. {
  1940. int i, ret = 0;
  1941. unsigned int page_len, thislen, page_offset;
  1942. struct scatterlist sg[1];
  1943. sg_init_table(sg, 1);
  1944. if (offset >= buf->head[0].iov_len) {
  1945. offset -= buf->head[0].iov_len;
  1946. } else {
  1947. thislen = buf->head[0].iov_len - offset;
  1948. if (thislen > len)
  1949. thislen = len;
  1950. sg_set_buf(sg, buf->head[0].iov_base + offset, thislen);
  1951. ret = actor(sg, data);
  1952. if (ret)
  1953. goto out;
  1954. offset = 0;
  1955. len -= thislen;
  1956. }
  1957. if (len == 0)
  1958. goto out;
  1959. if (offset >= buf->page_len) {
  1960. offset -= buf->page_len;
  1961. } else {
  1962. page_len = buf->page_len - offset;
  1963. if (page_len > len)
  1964. page_len = len;
  1965. len -= page_len;
  1966. page_offset = (offset + buf->page_base) & (PAGE_SIZE - 1);
  1967. i = (offset + buf->page_base) >> PAGE_SHIFT;
  1968. thislen = PAGE_SIZE - page_offset;
  1969. do {
  1970. if (thislen > page_len)
  1971. thislen = page_len;
  1972. sg_set_page(sg, buf->pages[i], thislen, page_offset);
  1973. ret = actor(sg, data);
  1974. if (ret)
  1975. goto out;
  1976. page_len -= thislen;
  1977. i++;
  1978. page_offset = 0;
  1979. thislen = PAGE_SIZE;
  1980. } while (page_len != 0);
  1981. offset = 0;
  1982. }
  1983. if (len == 0)
  1984. goto out;
  1985. if (offset < buf->tail[0].iov_len) {
  1986. thislen = buf->tail[0].iov_len - offset;
  1987. if (thislen > len)
  1988. thislen = len;
  1989. sg_set_buf(sg, buf->tail[0].iov_base + offset, thislen);
  1990. ret = actor(sg, data);
  1991. len -= thislen;
  1992. }
  1993. if (len != 0)
  1994. ret = -EINVAL;
  1995. out:
  1996. return ret;
  1997. }
  1998. EXPORT_SYMBOL_GPL(xdr_process_buf);
  1999. /**
  2000. * xdr_stream_decode_opaque - Decode variable length opaque
  2001. * @xdr: pointer to xdr_stream
  2002. * @ptr: location to store opaque data
  2003. * @size: size of storage buffer @ptr
  2004. *
  2005. * Return values:
  2006. * On success, returns size of object stored in *@ptr
  2007. * %-EBADMSG on XDR buffer overflow
  2008. * %-EMSGSIZE on overflow of storage buffer @ptr
  2009. */
  2010. ssize_t xdr_stream_decode_opaque(struct xdr_stream *xdr, void *ptr, size_t size)
  2011. {
  2012. ssize_t ret;
  2013. void *p;
  2014. ret = xdr_stream_decode_opaque_inline(xdr, &p, size);
  2015. if (ret <= 0)
  2016. return ret;
  2017. memcpy(ptr, p, ret);
  2018. return ret;
  2019. }
  2020. EXPORT_SYMBOL_GPL(xdr_stream_decode_opaque);
  2021. /**
  2022. * xdr_stream_decode_opaque_dup - Decode and duplicate variable length opaque
  2023. * @xdr: pointer to xdr_stream
  2024. * @ptr: location to store pointer to opaque data
  2025. * @maxlen: maximum acceptable object size
  2026. * @gfp_flags: GFP mask to use
  2027. *
  2028. * Return values:
  2029. * On success, returns size of object stored in *@ptr
  2030. * %-EBADMSG on XDR buffer overflow
  2031. * %-EMSGSIZE if the size of the object would exceed @maxlen
  2032. * %-ENOMEM on memory allocation failure
  2033. */
  2034. ssize_t xdr_stream_decode_opaque_dup(struct xdr_stream *xdr, void **ptr,
  2035. size_t maxlen, gfp_t gfp_flags)
  2036. {
  2037. ssize_t ret;
  2038. void *p;
  2039. ret = xdr_stream_decode_opaque_inline(xdr, &p, maxlen);
  2040. if (ret > 0) {
  2041. *ptr = kmemdup(p, ret, gfp_flags);
  2042. if (*ptr != NULL)
  2043. return ret;
  2044. ret = -ENOMEM;
  2045. }
  2046. *ptr = NULL;
  2047. return ret;
  2048. }
  2049. EXPORT_SYMBOL_GPL(xdr_stream_decode_opaque_dup);
  2050. /**
  2051. * xdr_stream_decode_string - Decode variable length string
  2052. * @xdr: pointer to xdr_stream
  2053. * @str: location to store string
  2054. * @size: size of storage buffer @str
  2055. *
  2056. * Return values:
  2057. * On success, returns length of NUL-terminated string stored in *@str
  2058. * %-EBADMSG on XDR buffer overflow
  2059. * %-EMSGSIZE on overflow of storage buffer @str
  2060. */
  2061. ssize_t xdr_stream_decode_string(struct xdr_stream *xdr, char *str, size_t size)
  2062. {
  2063. ssize_t ret;
  2064. void *p;
  2065. ret = xdr_stream_decode_opaque_inline(xdr, &p, size);
  2066. if (ret > 0) {
  2067. memcpy(str, p, ret);
  2068. str[ret] = '\0';
  2069. return strlen(str);
  2070. }
  2071. *str = '\0';
  2072. return ret;
  2073. }
  2074. EXPORT_SYMBOL_GPL(xdr_stream_decode_string);
  2075. /**
  2076. * xdr_stream_decode_string_dup - Decode and duplicate variable length string
  2077. * @xdr: pointer to xdr_stream
  2078. * @str: location to store pointer to string
  2079. * @maxlen: maximum acceptable string length
  2080. * @gfp_flags: GFP mask to use
  2081. *
  2082. * Return values:
  2083. * On success, returns length of NUL-terminated string stored in *@ptr
  2084. * %-EBADMSG on XDR buffer overflow
  2085. * %-EMSGSIZE if the size of the string would exceed @maxlen
  2086. * %-ENOMEM on memory allocation failure
  2087. */
  2088. ssize_t xdr_stream_decode_string_dup(struct xdr_stream *xdr, char **str,
  2089. size_t maxlen, gfp_t gfp_flags)
  2090. {
  2091. void *p;
  2092. ssize_t ret;
  2093. ret = xdr_stream_decode_opaque_inline(xdr, &p, maxlen);
  2094. if (ret > 0) {
  2095. char *s = kmemdup_nul(p, ret, gfp_flags);
  2096. if (s != NULL) {
  2097. *str = s;
  2098. return strlen(s);
  2099. }
  2100. ret = -ENOMEM;
  2101. }
  2102. *str = NULL;
  2103. return ret;
  2104. }
  2105. EXPORT_SYMBOL_GPL(xdr_stream_decode_string_dup);
  2106. /**
  2107. * xdr_stream_decode_opaque_auth - Decode struct opaque_auth (RFC5531 S8.2)
  2108. * @xdr: pointer to xdr_stream
  2109. * @flavor: location to store decoded flavor
  2110. * @body: location to store decode body
  2111. * @body_len: location to store length of decoded body
  2112. *
  2113. * Return values:
  2114. * On success, returns the number of buffer bytes consumed
  2115. * %-EBADMSG on XDR buffer overflow
  2116. * %-EMSGSIZE if the decoded size of the body field exceeds 400 octets
  2117. */
  2118. ssize_t xdr_stream_decode_opaque_auth(struct xdr_stream *xdr, u32 *flavor,
  2119. void **body, unsigned int *body_len)
  2120. {
  2121. ssize_t ret, len;
  2122. len = xdr_stream_decode_u32(xdr, flavor);
  2123. if (unlikely(len < 0))
  2124. return len;
  2125. ret = xdr_stream_decode_opaque_inline(xdr, body, RPC_MAX_AUTH_SIZE);
  2126. if (unlikely(ret < 0))
  2127. return ret;
  2128. *body_len = ret;
  2129. return len + ret;
  2130. }
  2131. EXPORT_SYMBOL_GPL(xdr_stream_decode_opaque_auth);
  2132. /**
  2133. * xdr_stream_encode_opaque_auth - Encode struct opaque_auth (RFC5531 S8.2)
  2134. * @xdr: pointer to xdr_stream
  2135. * @flavor: verifier flavor to encode
  2136. * @body: content of body to encode
  2137. * @body_len: length of body to encode
  2138. *
  2139. * Return values:
  2140. * On success, returns length in bytes of XDR buffer consumed
  2141. * %-EBADMSG on XDR buffer overflow
  2142. * %-EMSGSIZE if the size of @body exceeds 400 octets
  2143. */
  2144. ssize_t xdr_stream_encode_opaque_auth(struct xdr_stream *xdr, u32 flavor,
  2145. void *body, unsigned int body_len)
  2146. {
  2147. ssize_t ret, len;
  2148. if (unlikely(body_len > RPC_MAX_AUTH_SIZE))
  2149. return -EMSGSIZE;
  2150. len = xdr_stream_encode_u32(xdr, flavor);
  2151. if (unlikely(len < 0))
  2152. return len;
  2153. ret = xdr_stream_encode_opaque(xdr, body, body_len);
  2154. if (unlikely(ret < 0))
  2155. return ret;
  2156. return len + ret;
  2157. }
  2158. EXPORT_SYMBOL_GPL(xdr_stream_encode_opaque_auth);