xdr.c 59 KB

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