nfs3xdr.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500
  1. /*
  2. * linux/fs/nfs/nfs3xdr.c
  3. *
  4. * XDR functions to encode/decode NFSv3 RPC arguments and results.
  5. *
  6. * Copyright (C) 1996, 1997 Olaf Kirch
  7. */
  8. #include <linux/param.h>
  9. #include <linux/time.h>
  10. #include <linux/mm.h>
  11. #include <linux/errno.h>
  12. #include <linux/string.h>
  13. #include <linux/in.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/proc_fs.h>
  16. #include <linux/kdev_t.h>
  17. #include <linux/sunrpc/clnt.h>
  18. #include <linux/nfs.h>
  19. #include <linux/nfs3.h>
  20. #include <linux/nfs_fs.h>
  21. #include <linux/nfsacl.h>
  22. #include "internal.h"
  23. #define NFSDBG_FACILITY NFSDBG_XDR
  24. /* Mapping from NFS error code to "errno" error code. */
  25. #define errno_NFSERR_IO EIO
  26. /*
  27. * Declare the space requirements for NFS arguments and replies as
  28. * number of 32bit-words
  29. */
  30. #define NFS3_fhandle_sz (1+16)
  31. #define NFS3_fh_sz (NFS3_fhandle_sz) /* shorthand */
  32. #define NFS3_sattr_sz (15)
  33. #define NFS3_filename_sz (1+(NFS3_MAXNAMLEN>>2))
  34. #define NFS3_path_sz (1+(NFS3_MAXPATHLEN>>2))
  35. #define NFS3_fattr_sz (21)
  36. #define NFS3_cookieverf_sz (NFS3_COOKIEVERFSIZE>>2)
  37. #define NFS3_wcc_attr_sz (6)
  38. #define NFS3_pre_op_attr_sz (1+NFS3_wcc_attr_sz)
  39. #define NFS3_post_op_attr_sz (1+NFS3_fattr_sz)
  40. #define NFS3_wcc_data_sz (NFS3_pre_op_attr_sz+NFS3_post_op_attr_sz)
  41. #define NFS3_diropargs_sz (NFS3_fh_sz+NFS3_filename_sz)
  42. #define NFS3_getattrargs_sz (NFS3_fh_sz)
  43. #define NFS3_setattrargs_sz (NFS3_fh_sz+NFS3_sattr_sz+3)
  44. #define NFS3_lookupargs_sz (NFS3_fh_sz+NFS3_filename_sz)
  45. #define NFS3_accessargs_sz (NFS3_fh_sz+1)
  46. #define NFS3_readlinkargs_sz (NFS3_fh_sz)
  47. #define NFS3_readargs_sz (NFS3_fh_sz+3)
  48. #define NFS3_writeargs_sz (NFS3_fh_sz+5)
  49. #define NFS3_createargs_sz (NFS3_diropargs_sz+NFS3_sattr_sz)
  50. #define NFS3_mkdirargs_sz (NFS3_diropargs_sz+NFS3_sattr_sz)
  51. #define NFS3_symlinkargs_sz (NFS3_diropargs_sz+1+NFS3_sattr_sz)
  52. #define NFS3_mknodargs_sz (NFS3_diropargs_sz+2+NFS3_sattr_sz)
  53. #define NFS3_removeargs_sz (NFS3_fh_sz+NFS3_filename_sz)
  54. #define NFS3_renameargs_sz (NFS3_diropargs_sz+NFS3_diropargs_sz)
  55. #define NFS3_linkargs_sz (NFS3_fh_sz+NFS3_diropargs_sz)
  56. #define NFS3_readdirargs_sz (NFS3_fh_sz+NFS3_cookieverf_sz+3)
  57. #define NFS3_readdirplusargs_sz (NFS3_fh_sz+NFS3_cookieverf_sz+4)
  58. #define NFS3_commitargs_sz (NFS3_fh_sz+3)
  59. #define NFS3_getattrres_sz (1+NFS3_fattr_sz)
  60. #define NFS3_setattrres_sz (1+NFS3_wcc_data_sz)
  61. #define NFS3_removeres_sz (NFS3_setattrres_sz)
  62. #define NFS3_lookupres_sz (1+NFS3_fh_sz+(2 * NFS3_post_op_attr_sz))
  63. #define NFS3_accessres_sz (1+NFS3_post_op_attr_sz+1)
  64. #define NFS3_readlinkres_sz (1+NFS3_post_op_attr_sz+1)
  65. #define NFS3_readres_sz (1+NFS3_post_op_attr_sz+3)
  66. #define NFS3_writeres_sz (1+NFS3_wcc_data_sz+4)
  67. #define NFS3_createres_sz (1+NFS3_fh_sz+NFS3_post_op_attr_sz+NFS3_wcc_data_sz)
  68. #define NFS3_renameres_sz (1+(2 * NFS3_wcc_data_sz))
  69. #define NFS3_linkres_sz (1+NFS3_post_op_attr_sz+NFS3_wcc_data_sz)
  70. #define NFS3_readdirres_sz (1+NFS3_post_op_attr_sz+2)
  71. #define NFS3_fsstatres_sz (1+NFS3_post_op_attr_sz+13)
  72. #define NFS3_fsinfores_sz (1+NFS3_post_op_attr_sz+12)
  73. #define NFS3_pathconfres_sz (1+NFS3_post_op_attr_sz+6)
  74. #define NFS3_commitres_sz (1+NFS3_wcc_data_sz+2)
  75. #define ACL3_getaclargs_sz (NFS3_fh_sz+1)
  76. #define ACL3_setaclargs_sz (NFS3_fh_sz+1+ \
  77. XDR_QUADLEN(NFS_ACL_INLINE_BUFSIZE))
  78. #define ACL3_getaclres_sz (1+NFS3_post_op_attr_sz+1+ \
  79. XDR_QUADLEN(NFS_ACL_INLINE_BUFSIZE))
  80. #define ACL3_setaclres_sz (1+NFS3_post_op_attr_sz)
  81. /*
  82. * Map file type to S_IFMT bits
  83. */
  84. static const umode_t nfs_type2fmt[] = {
  85. [NF3BAD] = 0,
  86. [NF3REG] = S_IFREG,
  87. [NF3DIR] = S_IFDIR,
  88. [NF3BLK] = S_IFBLK,
  89. [NF3CHR] = S_IFCHR,
  90. [NF3LNK] = S_IFLNK,
  91. [NF3SOCK] = S_IFSOCK,
  92. [NF3FIFO] = S_IFIFO,
  93. };
  94. /*
  95. * While encoding arguments, set up the reply buffer in advance to
  96. * receive reply data directly into the page cache.
  97. */
  98. static void prepare_reply_buffer(struct rpc_rqst *req, struct page **pages,
  99. unsigned int base, unsigned int len,
  100. unsigned int bufsize)
  101. {
  102. struct rpc_auth *auth = req->rq_cred->cr_auth;
  103. unsigned int replen;
  104. replen = RPC_REPHDRSIZE + auth->au_rslack + bufsize;
  105. xdr_inline_pages(&req->rq_rcv_buf, replen << 2, pages, base, len);
  106. }
  107. /*
  108. * Handle decode buffer overflows out-of-line.
  109. */
  110. static void print_overflow_msg(const char *func, const struct xdr_stream *xdr)
  111. {
  112. dprintk("NFS: %s prematurely hit the end of our receive buffer. "
  113. "Remaining buffer length is %tu words.\n",
  114. func, xdr->end - xdr->p);
  115. }
  116. /*
  117. * Encode/decode NFSv3 basic data types
  118. *
  119. * Basic NFSv3 data types are defined in section 2.5 of RFC 1813:
  120. * "NFS Version 3 Protocol Specification".
  121. *
  122. * Not all basic data types have their own encoding and decoding
  123. * functions. For run-time efficiency, some data types are encoded
  124. * or decoded inline.
  125. */
  126. static void encode_uint32(struct xdr_stream *xdr, u32 value)
  127. {
  128. __be32 *p = xdr_reserve_space(xdr, 4);
  129. *p = cpu_to_be32(value);
  130. }
  131. static int decode_uint32(struct xdr_stream *xdr, u32 *value)
  132. {
  133. __be32 *p;
  134. p = xdr_inline_decode(xdr, 4);
  135. if (unlikely(p == NULL))
  136. goto out_overflow;
  137. *value = be32_to_cpup(p);
  138. return 0;
  139. out_overflow:
  140. print_overflow_msg(__func__, xdr);
  141. return -EIO;
  142. }
  143. static int decode_uint64(struct xdr_stream *xdr, u64 *value)
  144. {
  145. __be32 *p;
  146. p = xdr_inline_decode(xdr, 8);
  147. if (unlikely(p == NULL))
  148. goto out_overflow;
  149. xdr_decode_hyper(p, value);
  150. return 0;
  151. out_overflow:
  152. print_overflow_msg(__func__, xdr);
  153. return -EIO;
  154. }
  155. /*
  156. * fileid3
  157. *
  158. * typedef uint64 fileid3;
  159. */
  160. static __be32 *xdr_decode_fileid3(__be32 *p, u64 *fileid)
  161. {
  162. return xdr_decode_hyper(p, fileid);
  163. }
  164. static int decode_fileid3(struct xdr_stream *xdr, u64 *fileid)
  165. {
  166. return decode_uint64(xdr, fileid);
  167. }
  168. /*
  169. * filename3
  170. *
  171. * typedef string filename3<>;
  172. */
  173. static void encode_filename3(struct xdr_stream *xdr,
  174. const char *name, u32 length)
  175. {
  176. __be32 *p;
  177. BUG_ON(length > NFS3_MAXNAMLEN);
  178. p = xdr_reserve_space(xdr, 4 + length);
  179. xdr_encode_opaque(p, name, length);
  180. }
  181. static int decode_inline_filename3(struct xdr_stream *xdr,
  182. const char **name, u32 *length)
  183. {
  184. __be32 *p;
  185. u32 count;
  186. p = xdr_inline_decode(xdr, 4);
  187. if (unlikely(p == NULL))
  188. goto out_overflow;
  189. count = be32_to_cpup(p);
  190. if (count > NFS3_MAXNAMLEN)
  191. goto out_nametoolong;
  192. p = xdr_inline_decode(xdr, count);
  193. if (unlikely(p == NULL))
  194. goto out_overflow;
  195. *name = (const char *)p;
  196. *length = count;
  197. return 0;
  198. out_nametoolong:
  199. dprintk("NFS: returned filename too long: %u\n", count);
  200. return -ENAMETOOLONG;
  201. out_overflow:
  202. print_overflow_msg(__func__, xdr);
  203. return -EIO;
  204. }
  205. /*
  206. * nfspath3
  207. *
  208. * typedef string nfspath3<>;
  209. */
  210. static void encode_nfspath3(struct xdr_stream *xdr, struct page **pages,
  211. const u32 length)
  212. {
  213. BUG_ON(length > NFS3_MAXPATHLEN);
  214. encode_uint32(xdr, length);
  215. xdr_write_pages(xdr, pages, 0, length);
  216. }
  217. static int decode_nfspath3(struct xdr_stream *xdr)
  218. {
  219. u32 recvd, count;
  220. size_t hdrlen;
  221. __be32 *p;
  222. p = xdr_inline_decode(xdr, 4);
  223. if (unlikely(p == NULL))
  224. goto out_overflow;
  225. count = be32_to_cpup(p);
  226. if (unlikely(count >= xdr->buf->page_len || count > NFS3_MAXPATHLEN))
  227. goto out_nametoolong;
  228. hdrlen = (u8 *)xdr->p - (u8 *)xdr->iov->iov_base;
  229. recvd = xdr->buf->len - hdrlen;
  230. if (unlikely(count > recvd))
  231. goto out_cheating;
  232. xdr_read_pages(xdr, count);
  233. xdr_terminate_string(xdr->buf, count);
  234. return 0;
  235. out_nametoolong:
  236. dprintk("NFS: returned pathname too long: %u\n", count);
  237. return -ENAMETOOLONG;
  238. out_cheating:
  239. dprintk("NFS: server cheating in pathname result: "
  240. "count %u > recvd %u\n", count, recvd);
  241. return -EIO;
  242. out_overflow:
  243. print_overflow_msg(__func__, xdr);
  244. return -EIO;
  245. }
  246. /*
  247. * cookie3
  248. *
  249. * typedef uint64 cookie3
  250. */
  251. static __be32 *xdr_encode_cookie3(__be32 *p, u64 cookie)
  252. {
  253. return xdr_encode_hyper(p, cookie);
  254. }
  255. static int decode_cookie3(struct xdr_stream *xdr, u64 *cookie)
  256. {
  257. return decode_uint64(xdr, cookie);
  258. }
  259. /*
  260. * cookieverf3
  261. *
  262. * typedef opaque cookieverf3[NFS3_COOKIEVERFSIZE];
  263. */
  264. static __be32 *xdr_encode_cookieverf3(__be32 *p, const __be32 *verifier)
  265. {
  266. memcpy(p, verifier, NFS3_COOKIEVERFSIZE);
  267. return p + XDR_QUADLEN(NFS3_COOKIEVERFSIZE);
  268. }
  269. static int decode_cookieverf3(struct xdr_stream *xdr, __be32 *verifier)
  270. {
  271. __be32 *p;
  272. p = xdr_inline_decode(xdr, NFS3_COOKIEVERFSIZE);
  273. if (unlikely(p == NULL))
  274. goto out_overflow;
  275. memcpy(verifier, p, NFS3_COOKIEVERFSIZE);
  276. return 0;
  277. out_overflow:
  278. print_overflow_msg(__func__, xdr);
  279. return -EIO;
  280. }
  281. /*
  282. * createverf3
  283. *
  284. * typedef opaque createverf3[NFS3_CREATEVERFSIZE];
  285. */
  286. static void encode_createverf3(struct xdr_stream *xdr, const __be32 *verifier)
  287. {
  288. __be32 *p;
  289. p = xdr_reserve_space(xdr, NFS3_CREATEVERFSIZE);
  290. memcpy(p, verifier, NFS3_CREATEVERFSIZE);
  291. }
  292. static int decode_writeverf3(struct xdr_stream *xdr, __be32 *verifier)
  293. {
  294. __be32 *p;
  295. p = xdr_inline_decode(xdr, NFS3_WRITEVERFSIZE);
  296. if (unlikely(p == NULL))
  297. goto out_overflow;
  298. memcpy(verifier, p, NFS3_WRITEVERFSIZE);
  299. return 0;
  300. out_overflow:
  301. print_overflow_msg(__func__, xdr);
  302. return -EIO;
  303. }
  304. /*
  305. * size3
  306. *
  307. * typedef uint64 size3;
  308. */
  309. static __be32 *xdr_decode_size3(__be32 *p, u64 *size)
  310. {
  311. return xdr_decode_hyper(p, size);
  312. }
  313. /*
  314. * nfsstat3
  315. *
  316. * enum nfsstat3 {
  317. * NFS3_OK = 0,
  318. * ...
  319. * }
  320. */
  321. #define NFS3_OK NFS_OK
  322. static int decode_nfsstat3(struct xdr_stream *xdr, enum nfs_stat *status)
  323. {
  324. __be32 *p;
  325. p = xdr_inline_decode(xdr, 4);
  326. if (unlikely(p == NULL))
  327. goto out_overflow;
  328. *status = be32_to_cpup(p);
  329. return 0;
  330. out_overflow:
  331. print_overflow_msg(__func__, xdr);
  332. return -EIO;
  333. }
  334. /*
  335. * ftype3
  336. *
  337. * enum ftype3 {
  338. * NF3REG = 1,
  339. * NF3DIR = 2,
  340. * NF3BLK = 3,
  341. * NF3CHR = 4,
  342. * NF3LNK = 5,
  343. * NF3SOCK = 6,
  344. * NF3FIFO = 7
  345. * };
  346. */
  347. static void encode_ftype3(struct xdr_stream *xdr, const u32 type)
  348. {
  349. BUG_ON(type > NF3FIFO);
  350. encode_uint32(xdr, type);
  351. }
  352. static __be32 *xdr_decode_ftype3(__be32 *p, umode_t *mode)
  353. {
  354. u32 type;
  355. type = be32_to_cpup(p++);
  356. if (type > NF3FIFO)
  357. type = NF3NON;
  358. *mode = nfs_type2fmt[type];
  359. return p;
  360. }
  361. /*
  362. * specdata3
  363. *
  364. * struct specdata3 {
  365. * uint32 specdata1;
  366. * uint32 specdata2;
  367. * };
  368. */
  369. static void encode_specdata3(struct xdr_stream *xdr, const dev_t rdev)
  370. {
  371. __be32 *p;
  372. p = xdr_reserve_space(xdr, 8);
  373. *p++ = cpu_to_be32(MAJOR(rdev));
  374. *p = cpu_to_be32(MINOR(rdev));
  375. }
  376. static __be32 *xdr_decode_specdata3(__be32 *p, dev_t *rdev)
  377. {
  378. unsigned int major, minor;
  379. major = be32_to_cpup(p++);
  380. minor = be32_to_cpup(p++);
  381. *rdev = MKDEV(major, minor);
  382. if (MAJOR(*rdev) != major || MINOR(*rdev) != minor)
  383. *rdev = 0;
  384. return p;
  385. }
  386. /*
  387. * nfs_fh3
  388. *
  389. * struct nfs_fh3 {
  390. * opaque data<NFS3_FHSIZE>;
  391. * };
  392. */
  393. static void encode_nfs_fh3(struct xdr_stream *xdr, const struct nfs_fh *fh)
  394. {
  395. __be32 *p;
  396. BUG_ON(fh->size > NFS3_FHSIZE);
  397. p = xdr_reserve_space(xdr, 4 + fh->size);
  398. xdr_encode_opaque(p, fh->data, fh->size);
  399. }
  400. static int decode_nfs_fh3(struct xdr_stream *xdr, struct nfs_fh *fh)
  401. {
  402. u32 length;
  403. __be32 *p;
  404. p = xdr_inline_decode(xdr, 4);
  405. if (unlikely(p == NULL))
  406. goto out_overflow;
  407. length = be32_to_cpup(p++);
  408. if (unlikely(length > NFS3_FHSIZE))
  409. goto out_toobig;
  410. p = xdr_inline_decode(xdr, length);
  411. if (unlikely(p == NULL))
  412. goto out_overflow;
  413. fh->size = length;
  414. memcpy(fh->data, p, length);
  415. return 0;
  416. out_toobig:
  417. dprintk("NFS: file handle size (%u) too big\n", length);
  418. return -E2BIG;
  419. out_overflow:
  420. print_overflow_msg(__func__, xdr);
  421. return -EIO;
  422. }
  423. static void zero_nfs_fh3(struct nfs_fh *fh)
  424. {
  425. memset(fh, 0, sizeof(*fh));
  426. }
  427. /*
  428. * nfstime3
  429. *
  430. * struct nfstime3 {
  431. * uint32 seconds;
  432. * uint32 nseconds;
  433. * };
  434. */
  435. static __be32 *xdr_encode_nfstime3(__be32 *p, const struct timespec *timep)
  436. {
  437. *p++ = cpu_to_be32(timep->tv_sec);
  438. *p++ = cpu_to_be32(timep->tv_nsec);
  439. return p;
  440. }
  441. static __be32 *xdr_decode_nfstime3(__be32 *p, struct timespec *timep)
  442. {
  443. timep->tv_sec = be32_to_cpup(p++);
  444. timep->tv_nsec = be32_to_cpup(p++);
  445. return p;
  446. }
  447. /*
  448. * sattr3
  449. *
  450. * enum time_how {
  451. * DONT_CHANGE = 0,
  452. * SET_TO_SERVER_TIME = 1,
  453. * SET_TO_CLIENT_TIME = 2
  454. * };
  455. *
  456. * union set_mode3 switch (bool set_it) {
  457. * case TRUE:
  458. * mode3 mode;
  459. * default:
  460. * void;
  461. * };
  462. *
  463. * union set_uid3 switch (bool set_it) {
  464. * case TRUE:
  465. * uid3 uid;
  466. * default:
  467. * void;
  468. * };
  469. *
  470. * union set_gid3 switch (bool set_it) {
  471. * case TRUE:
  472. * gid3 gid;
  473. * default:
  474. * void;
  475. * };
  476. *
  477. * union set_size3 switch (bool set_it) {
  478. * case TRUE:
  479. * size3 size;
  480. * default:
  481. * void;
  482. * };
  483. *
  484. * union set_atime switch (time_how set_it) {
  485. * case SET_TO_CLIENT_TIME:
  486. * nfstime3 atime;
  487. * default:
  488. * void;
  489. * };
  490. *
  491. * union set_mtime switch (time_how set_it) {
  492. * case SET_TO_CLIENT_TIME:
  493. * nfstime3 mtime;
  494. * default:
  495. * void;
  496. * };
  497. *
  498. * struct sattr3 {
  499. * set_mode3 mode;
  500. * set_uid3 uid;
  501. * set_gid3 gid;
  502. * set_size3 size;
  503. * set_atime atime;
  504. * set_mtime mtime;
  505. * };
  506. */
  507. static void encode_sattr3(struct xdr_stream *xdr, const struct iattr *attr)
  508. {
  509. u32 nbytes;
  510. __be32 *p;
  511. /*
  512. * In order to make only a single xdr_reserve_space() call,
  513. * pre-compute the total number of bytes to be reserved.
  514. * Six boolean values, one for each set_foo field, are always
  515. * present in the encoded result, so start there.
  516. */
  517. nbytes = 6 * 4;
  518. if (attr->ia_valid & ATTR_MODE)
  519. nbytes += 4;
  520. if (attr->ia_valid & ATTR_UID)
  521. nbytes += 4;
  522. if (attr->ia_valid & ATTR_GID)
  523. nbytes += 4;
  524. if (attr->ia_valid & ATTR_SIZE)
  525. nbytes += 8;
  526. if (attr->ia_valid & ATTR_ATIME_SET)
  527. nbytes += 8;
  528. if (attr->ia_valid & ATTR_MTIME_SET)
  529. nbytes += 8;
  530. p = xdr_reserve_space(xdr, nbytes);
  531. if (attr->ia_valid & ATTR_MODE) {
  532. *p++ = xdr_one;
  533. *p++ = cpu_to_be32(attr->ia_mode & S_IALLUGO);
  534. } else
  535. *p++ = xdr_zero;
  536. if (attr->ia_valid & ATTR_UID) {
  537. *p++ = xdr_one;
  538. *p++ = cpu_to_be32(attr->ia_uid);
  539. } else
  540. *p++ = xdr_zero;
  541. if (attr->ia_valid & ATTR_GID) {
  542. *p++ = xdr_one;
  543. *p++ = cpu_to_be32(attr->ia_gid);
  544. } else
  545. *p++ = xdr_zero;
  546. if (attr->ia_valid & ATTR_SIZE) {
  547. *p++ = xdr_one;
  548. p = xdr_encode_hyper(p, (u64)attr->ia_size);
  549. } else
  550. *p++ = xdr_zero;
  551. if (attr->ia_valid & ATTR_ATIME_SET) {
  552. *p++ = xdr_two;
  553. p = xdr_encode_nfstime3(p, &attr->ia_atime);
  554. } else if (attr->ia_valid & ATTR_ATIME) {
  555. *p++ = xdr_one;
  556. } else
  557. *p++ = xdr_zero;
  558. if (attr->ia_valid & ATTR_MTIME_SET) {
  559. *p++ = xdr_two;
  560. xdr_encode_nfstime3(p, &attr->ia_mtime);
  561. } else if (attr->ia_valid & ATTR_MTIME) {
  562. *p = xdr_one;
  563. } else
  564. *p = xdr_zero;
  565. }
  566. /*
  567. * fattr3
  568. *
  569. * struct fattr3 {
  570. * ftype3 type;
  571. * mode3 mode;
  572. * uint32 nlink;
  573. * uid3 uid;
  574. * gid3 gid;
  575. * size3 size;
  576. * size3 used;
  577. * specdata3 rdev;
  578. * uint64 fsid;
  579. * fileid3 fileid;
  580. * nfstime3 atime;
  581. * nfstime3 mtime;
  582. * nfstime3 ctime;
  583. * };
  584. */
  585. static int decode_fattr3(struct xdr_stream *xdr, struct nfs_fattr *fattr)
  586. {
  587. umode_t fmode;
  588. __be32 *p;
  589. p = xdr_inline_decode(xdr, NFS3_fattr_sz << 2);
  590. if (unlikely(p == NULL))
  591. goto out_overflow;
  592. p = xdr_decode_ftype3(p, &fmode);
  593. fattr->mode = (be32_to_cpup(p++) & ~S_IFMT) | fmode;
  594. fattr->nlink = be32_to_cpup(p++);
  595. fattr->uid = be32_to_cpup(p++);
  596. fattr->gid = be32_to_cpup(p++);
  597. p = xdr_decode_size3(p, &fattr->size);
  598. p = xdr_decode_size3(p, &fattr->du.nfs3.used);
  599. p = xdr_decode_specdata3(p, &fattr->rdev);
  600. p = xdr_decode_hyper(p, &fattr->fsid.major);
  601. fattr->fsid.minor = 0;
  602. p = xdr_decode_fileid3(p, &fattr->fileid);
  603. p = xdr_decode_nfstime3(p, &fattr->atime);
  604. p = xdr_decode_nfstime3(p, &fattr->mtime);
  605. xdr_decode_nfstime3(p, &fattr->ctime);
  606. fattr->valid |= NFS_ATTR_FATTR_V3;
  607. return 0;
  608. out_overflow:
  609. print_overflow_msg(__func__, xdr);
  610. return -EIO;
  611. }
  612. /*
  613. * post_op_attr
  614. *
  615. * union post_op_attr switch (bool attributes_follow) {
  616. * case TRUE:
  617. * fattr3 attributes;
  618. * case FALSE:
  619. * void;
  620. * };
  621. */
  622. static int decode_post_op_attr(struct xdr_stream *xdr, struct nfs_fattr *fattr)
  623. {
  624. __be32 *p;
  625. p = xdr_inline_decode(xdr, 4);
  626. if (unlikely(p == NULL))
  627. goto out_overflow;
  628. if (*p != xdr_zero)
  629. return decode_fattr3(xdr, fattr);
  630. return 0;
  631. out_overflow:
  632. print_overflow_msg(__func__, xdr);
  633. return -EIO;
  634. }
  635. /*
  636. * wcc_attr
  637. * struct wcc_attr {
  638. * size3 size;
  639. * nfstime3 mtime;
  640. * nfstime3 ctime;
  641. * };
  642. */
  643. static int decode_wcc_attr(struct xdr_stream *xdr, struct nfs_fattr *fattr)
  644. {
  645. __be32 *p;
  646. p = xdr_inline_decode(xdr, NFS3_wcc_attr_sz << 2);
  647. if (unlikely(p == NULL))
  648. goto out_overflow;
  649. fattr->valid |= NFS_ATTR_FATTR_PRESIZE
  650. | NFS_ATTR_FATTR_PREMTIME
  651. | NFS_ATTR_FATTR_PRECTIME;
  652. p = xdr_decode_size3(p, &fattr->pre_size);
  653. p = xdr_decode_nfstime3(p, &fattr->pre_mtime);
  654. xdr_decode_nfstime3(p, &fattr->pre_ctime);
  655. return 0;
  656. out_overflow:
  657. print_overflow_msg(__func__, xdr);
  658. return -EIO;
  659. }
  660. /*
  661. * pre_op_attr
  662. * union pre_op_attr switch (bool attributes_follow) {
  663. * case TRUE:
  664. * wcc_attr attributes;
  665. * case FALSE:
  666. * void;
  667. * };
  668. *
  669. * wcc_data
  670. *
  671. * struct wcc_data {
  672. * pre_op_attr before;
  673. * post_op_attr after;
  674. * };
  675. */
  676. static int decode_pre_op_attr(struct xdr_stream *xdr, struct nfs_fattr *fattr)
  677. {
  678. __be32 *p;
  679. p = xdr_inline_decode(xdr, 4);
  680. if (unlikely(p == NULL))
  681. goto out_overflow;
  682. if (*p != xdr_zero)
  683. return decode_wcc_attr(xdr, fattr);
  684. return 0;
  685. out_overflow:
  686. print_overflow_msg(__func__, xdr);
  687. return -EIO;
  688. }
  689. static int decode_wcc_data(struct xdr_stream *xdr, struct nfs_fattr *fattr)
  690. {
  691. int error;
  692. error = decode_pre_op_attr(xdr, fattr);
  693. if (unlikely(error))
  694. goto out;
  695. error = decode_post_op_attr(xdr, fattr);
  696. out:
  697. return error;
  698. }
  699. /*
  700. * post_op_fh3
  701. *
  702. * union post_op_fh3 switch (bool handle_follows) {
  703. * case TRUE:
  704. * nfs_fh3 handle;
  705. * case FALSE:
  706. * void;
  707. * };
  708. */
  709. static int decode_post_op_fh3(struct xdr_stream *xdr, struct nfs_fh *fh)
  710. {
  711. __be32 *p = xdr_inline_decode(xdr, 4);
  712. if (unlikely(p == NULL))
  713. goto out_overflow;
  714. if (*p != xdr_zero)
  715. return decode_nfs_fh3(xdr, fh);
  716. zero_nfs_fh3(fh);
  717. return 0;
  718. out_overflow:
  719. print_overflow_msg(__func__, xdr);
  720. return -EIO;
  721. }
  722. /*
  723. * diropargs3
  724. *
  725. * struct diropargs3 {
  726. * nfs_fh3 dir;
  727. * filename3 name;
  728. * };
  729. */
  730. static void encode_diropargs3(struct xdr_stream *xdr, const struct nfs_fh *fh,
  731. const char *name, u32 length)
  732. {
  733. encode_nfs_fh3(xdr, fh);
  734. encode_filename3(xdr, name, length);
  735. }
  736. /*
  737. * NFSv3 XDR encode functions
  738. *
  739. * NFSv3 argument types are defined in section 3.3 of RFC 1813:
  740. * "NFS Version 3 Protocol Specification".
  741. */
  742. /*
  743. * 3.3.1 GETATTR3args
  744. *
  745. * struct GETATTR3args {
  746. * nfs_fh3 object;
  747. * };
  748. */
  749. static void nfs3_xdr_enc_getattr3args(struct rpc_rqst *req,
  750. struct xdr_stream *xdr,
  751. const struct nfs_fh *fh)
  752. {
  753. encode_nfs_fh3(xdr, fh);
  754. }
  755. /*
  756. * 3.3.2 SETATTR3args
  757. *
  758. * union sattrguard3 switch (bool check) {
  759. * case TRUE:
  760. * nfstime3 obj_ctime;
  761. * case FALSE:
  762. * void;
  763. * };
  764. *
  765. * struct SETATTR3args {
  766. * nfs_fh3 object;
  767. * sattr3 new_attributes;
  768. * sattrguard3 guard;
  769. * };
  770. */
  771. static void encode_sattrguard3(struct xdr_stream *xdr,
  772. const struct nfs3_sattrargs *args)
  773. {
  774. __be32 *p;
  775. if (args->guard) {
  776. p = xdr_reserve_space(xdr, 4 + 8);
  777. *p++ = xdr_one;
  778. xdr_encode_nfstime3(p, &args->guardtime);
  779. } else {
  780. p = xdr_reserve_space(xdr, 4);
  781. *p = xdr_zero;
  782. }
  783. }
  784. static void nfs3_xdr_enc_setattr3args(struct rpc_rqst *req,
  785. struct xdr_stream *xdr,
  786. const struct nfs3_sattrargs *args)
  787. {
  788. encode_nfs_fh3(xdr, args->fh);
  789. encode_sattr3(xdr, args->sattr);
  790. encode_sattrguard3(xdr, args);
  791. }
  792. /*
  793. * 3.3.3 LOOKUP3args
  794. *
  795. * struct LOOKUP3args {
  796. * diropargs3 what;
  797. * };
  798. */
  799. static void nfs3_xdr_enc_lookup3args(struct rpc_rqst *req,
  800. struct xdr_stream *xdr,
  801. const struct nfs3_diropargs *args)
  802. {
  803. encode_diropargs3(xdr, args->fh, args->name, args->len);
  804. }
  805. /*
  806. * 3.3.4 ACCESS3args
  807. *
  808. * struct ACCESS3args {
  809. * nfs_fh3 object;
  810. * uint32 access;
  811. * };
  812. */
  813. static void encode_access3args(struct xdr_stream *xdr,
  814. const struct nfs3_accessargs *args)
  815. {
  816. encode_nfs_fh3(xdr, args->fh);
  817. encode_uint32(xdr, args->access);
  818. }
  819. static void nfs3_xdr_enc_access3args(struct rpc_rqst *req,
  820. struct xdr_stream *xdr,
  821. const struct nfs3_accessargs *args)
  822. {
  823. encode_access3args(xdr, args);
  824. }
  825. /*
  826. * 3.3.5 READLINK3args
  827. *
  828. * struct READLINK3args {
  829. * nfs_fh3 symlink;
  830. * };
  831. */
  832. static void nfs3_xdr_enc_readlink3args(struct rpc_rqst *req,
  833. struct xdr_stream *xdr,
  834. const struct nfs3_readlinkargs *args)
  835. {
  836. encode_nfs_fh3(xdr, args->fh);
  837. prepare_reply_buffer(req, args->pages, args->pgbase,
  838. args->pglen, NFS3_readlinkres_sz);
  839. }
  840. /*
  841. * 3.3.6 READ3args
  842. *
  843. * struct READ3args {
  844. * nfs_fh3 file;
  845. * offset3 offset;
  846. * count3 count;
  847. * };
  848. */
  849. static void encode_read3args(struct xdr_stream *xdr,
  850. const struct nfs_readargs *args)
  851. {
  852. __be32 *p;
  853. encode_nfs_fh3(xdr, args->fh);
  854. p = xdr_reserve_space(xdr, 8 + 4);
  855. p = xdr_encode_hyper(p, args->offset);
  856. *p = cpu_to_be32(args->count);
  857. }
  858. static void nfs3_xdr_enc_read3args(struct rpc_rqst *req,
  859. struct xdr_stream *xdr,
  860. const struct nfs_readargs *args)
  861. {
  862. encode_read3args(xdr, args);
  863. prepare_reply_buffer(req, args->pages, args->pgbase,
  864. args->count, NFS3_readres_sz);
  865. req->rq_rcv_buf.flags |= XDRBUF_READ;
  866. }
  867. /*
  868. * 3.3.7 WRITE3args
  869. *
  870. * enum stable_how {
  871. * UNSTABLE = 0,
  872. * DATA_SYNC = 1,
  873. * FILE_SYNC = 2
  874. * };
  875. *
  876. * struct WRITE3args {
  877. * nfs_fh3 file;
  878. * offset3 offset;
  879. * count3 count;
  880. * stable_how stable;
  881. * opaque data<>;
  882. * };
  883. */
  884. static void encode_write3args(struct xdr_stream *xdr,
  885. const struct nfs_writeargs *args)
  886. {
  887. __be32 *p;
  888. encode_nfs_fh3(xdr, args->fh);
  889. p = xdr_reserve_space(xdr, 8 + 4 + 4 + 4);
  890. p = xdr_encode_hyper(p, args->offset);
  891. *p++ = cpu_to_be32(args->count);
  892. *p++ = cpu_to_be32(args->stable);
  893. *p = cpu_to_be32(args->count);
  894. xdr_write_pages(xdr, args->pages, args->pgbase, args->count);
  895. }
  896. static void nfs3_xdr_enc_write3args(struct rpc_rqst *req,
  897. struct xdr_stream *xdr,
  898. const struct nfs_writeargs *args)
  899. {
  900. encode_write3args(xdr, args);
  901. xdr->buf->flags |= XDRBUF_WRITE;
  902. }
  903. /*
  904. * 3.3.8 CREATE3args
  905. *
  906. * enum createmode3 {
  907. * UNCHECKED = 0,
  908. * GUARDED = 1,
  909. * EXCLUSIVE = 2
  910. * };
  911. *
  912. * union createhow3 switch (createmode3 mode) {
  913. * case UNCHECKED:
  914. * case GUARDED:
  915. * sattr3 obj_attributes;
  916. * case EXCLUSIVE:
  917. * createverf3 verf;
  918. * };
  919. *
  920. * struct CREATE3args {
  921. * diropargs3 where;
  922. * createhow3 how;
  923. * };
  924. */
  925. static void encode_createhow3(struct xdr_stream *xdr,
  926. const struct nfs3_createargs *args)
  927. {
  928. encode_uint32(xdr, args->createmode);
  929. switch (args->createmode) {
  930. case NFS3_CREATE_UNCHECKED:
  931. case NFS3_CREATE_GUARDED:
  932. encode_sattr3(xdr, args->sattr);
  933. break;
  934. case NFS3_CREATE_EXCLUSIVE:
  935. encode_createverf3(xdr, args->verifier);
  936. break;
  937. default:
  938. BUG();
  939. }
  940. }
  941. static void nfs3_xdr_enc_create3args(struct rpc_rqst *req,
  942. struct xdr_stream *xdr,
  943. const struct nfs3_createargs *args)
  944. {
  945. encode_diropargs3(xdr, args->fh, args->name, args->len);
  946. encode_createhow3(xdr, args);
  947. }
  948. /*
  949. * 3.3.9 MKDIR3args
  950. *
  951. * struct MKDIR3args {
  952. * diropargs3 where;
  953. * sattr3 attributes;
  954. * };
  955. */
  956. static void nfs3_xdr_enc_mkdir3args(struct rpc_rqst *req,
  957. struct xdr_stream *xdr,
  958. const struct nfs3_mkdirargs *args)
  959. {
  960. encode_diropargs3(xdr, args->fh, args->name, args->len);
  961. encode_sattr3(xdr, args->sattr);
  962. }
  963. /*
  964. * 3.3.10 SYMLINK3args
  965. *
  966. * struct symlinkdata3 {
  967. * sattr3 symlink_attributes;
  968. * nfspath3 symlink_data;
  969. * };
  970. *
  971. * struct SYMLINK3args {
  972. * diropargs3 where;
  973. * symlinkdata3 symlink;
  974. * };
  975. */
  976. static void encode_symlinkdata3(struct xdr_stream *xdr,
  977. const struct nfs3_symlinkargs *args)
  978. {
  979. encode_sattr3(xdr, args->sattr);
  980. encode_nfspath3(xdr, args->pages, args->pathlen);
  981. }
  982. static void nfs3_xdr_enc_symlink3args(struct rpc_rqst *req,
  983. struct xdr_stream *xdr,
  984. const struct nfs3_symlinkargs *args)
  985. {
  986. encode_diropargs3(xdr, args->fromfh, args->fromname, args->fromlen);
  987. encode_symlinkdata3(xdr, args);
  988. }
  989. /*
  990. * 3.3.11 MKNOD3args
  991. *
  992. * struct devicedata3 {
  993. * sattr3 dev_attributes;
  994. * specdata3 spec;
  995. * };
  996. *
  997. * union mknoddata3 switch (ftype3 type) {
  998. * case NF3CHR:
  999. * case NF3BLK:
  1000. * devicedata3 device;
  1001. * case NF3SOCK:
  1002. * case NF3FIFO:
  1003. * sattr3 pipe_attributes;
  1004. * default:
  1005. * void;
  1006. * };
  1007. *
  1008. * struct MKNOD3args {
  1009. * diropargs3 where;
  1010. * mknoddata3 what;
  1011. * };
  1012. */
  1013. static void encode_devicedata3(struct xdr_stream *xdr,
  1014. const struct nfs3_mknodargs *args)
  1015. {
  1016. encode_sattr3(xdr, args->sattr);
  1017. encode_specdata3(xdr, args->rdev);
  1018. }
  1019. static void encode_mknoddata3(struct xdr_stream *xdr,
  1020. const struct nfs3_mknodargs *args)
  1021. {
  1022. encode_ftype3(xdr, args->type);
  1023. switch (args->type) {
  1024. case NF3CHR:
  1025. case NF3BLK:
  1026. encode_devicedata3(xdr, args);
  1027. break;
  1028. case NF3SOCK:
  1029. case NF3FIFO:
  1030. encode_sattr3(xdr, args->sattr);
  1031. break;
  1032. case NF3REG:
  1033. case NF3DIR:
  1034. break;
  1035. default:
  1036. BUG();
  1037. }
  1038. }
  1039. static void nfs3_xdr_enc_mknod3args(struct rpc_rqst *req,
  1040. struct xdr_stream *xdr,
  1041. const struct nfs3_mknodargs *args)
  1042. {
  1043. encode_diropargs3(xdr, args->fh, args->name, args->len);
  1044. encode_mknoddata3(xdr, args);
  1045. }
  1046. /*
  1047. * 3.3.12 REMOVE3args
  1048. *
  1049. * struct REMOVE3args {
  1050. * diropargs3 object;
  1051. * };
  1052. */
  1053. static void nfs3_xdr_enc_remove3args(struct rpc_rqst *req,
  1054. struct xdr_stream *xdr,
  1055. const struct nfs_removeargs *args)
  1056. {
  1057. encode_diropargs3(xdr, args->fh, args->name.name, args->name.len);
  1058. }
  1059. /*
  1060. * 3.3.14 RENAME3args
  1061. *
  1062. * struct RENAME3args {
  1063. * diropargs3 from;
  1064. * diropargs3 to;
  1065. * };
  1066. */
  1067. static void nfs3_xdr_enc_rename3args(struct rpc_rqst *req,
  1068. struct xdr_stream *xdr,
  1069. const struct nfs_renameargs *args)
  1070. {
  1071. const struct qstr *old = args->old_name;
  1072. const struct qstr *new = args->new_name;
  1073. encode_diropargs3(xdr, args->old_dir, old->name, old->len);
  1074. encode_diropargs3(xdr, args->new_dir, new->name, new->len);
  1075. }
  1076. /*
  1077. * 3.3.15 LINK3args
  1078. *
  1079. * struct LINK3args {
  1080. * nfs_fh3 file;
  1081. * diropargs3 link;
  1082. * };
  1083. */
  1084. static void nfs3_xdr_enc_link3args(struct rpc_rqst *req,
  1085. struct xdr_stream *xdr,
  1086. const struct nfs3_linkargs *args)
  1087. {
  1088. encode_nfs_fh3(xdr, args->fromfh);
  1089. encode_diropargs3(xdr, args->tofh, args->toname, args->tolen);
  1090. }
  1091. /*
  1092. * 3.3.16 READDIR3args
  1093. *
  1094. * struct READDIR3args {
  1095. * nfs_fh3 dir;
  1096. * cookie3 cookie;
  1097. * cookieverf3 cookieverf;
  1098. * count3 count;
  1099. * };
  1100. */
  1101. static void encode_readdir3args(struct xdr_stream *xdr,
  1102. const struct nfs3_readdirargs *args)
  1103. {
  1104. __be32 *p;
  1105. encode_nfs_fh3(xdr, args->fh);
  1106. p = xdr_reserve_space(xdr, 8 + NFS3_COOKIEVERFSIZE + 4);
  1107. p = xdr_encode_cookie3(p, args->cookie);
  1108. p = xdr_encode_cookieverf3(p, args->verf);
  1109. *p = cpu_to_be32(args->count);
  1110. }
  1111. static void nfs3_xdr_enc_readdir3args(struct rpc_rqst *req,
  1112. struct xdr_stream *xdr,
  1113. const struct nfs3_readdirargs *args)
  1114. {
  1115. encode_readdir3args(xdr, args);
  1116. prepare_reply_buffer(req, args->pages, 0,
  1117. args->count, NFS3_readdirres_sz);
  1118. }
  1119. /*
  1120. * 3.3.17 READDIRPLUS3args
  1121. *
  1122. * struct READDIRPLUS3args {
  1123. * nfs_fh3 dir;
  1124. * cookie3 cookie;
  1125. * cookieverf3 cookieverf;
  1126. * count3 dircount;
  1127. * count3 maxcount;
  1128. * };
  1129. */
  1130. static void encode_readdirplus3args(struct xdr_stream *xdr,
  1131. const struct nfs3_readdirargs *args)
  1132. {
  1133. __be32 *p;
  1134. encode_nfs_fh3(xdr, args->fh);
  1135. p = xdr_reserve_space(xdr, 8 + NFS3_COOKIEVERFSIZE + 4 + 4);
  1136. p = xdr_encode_cookie3(p, args->cookie);
  1137. p = xdr_encode_cookieverf3(p, args->verf);
  1138. /*
  1139. * readdirplus: need dircount + buffer size.
  1140. * We just make sure we make dircount big enough
  1141. */
  1142. *p++ = cpu_to_be32(args->count >> 3);
  1143. *p = cpu_to_be32(args->count);
  1144. }
  1145. static void nfs3_xdr_enc_readdirplus3args(struct rpc_rqst *req,
  1146. struct xdr_stream *xdr,
  1147. const struct nfs3_readdirargs *args)
  1148. {
  1149. encode_readdirplus3args(xdr, args);
  1150. prepare_reply_buffer(req, args->pages, 0,
  1151. args->count, NFS3_readdirres_sz);
  1152. }
  1153. /*
  1154. * 3.3.21 COMMIT3args
  1155. *
  1156. * struct COMMIT3args {
  1157. * nfs_fh3 file;
  1158. * offset3 offset;
  1159. * count3 count;
  1160. * };
  1161. */
  1162. static void encode_commit3args(struct xdr_stream *xdr,
  1163. const struct nfs_writeargs *args)
  1164. {
  1165. __be32 *p;
  1166. encode_nfs_fh3(xdr, args->fh);
  1167. p = xdr_reserve_space(xdr, 8 + 4);
  1168. p = xdr_encode_hyper(p, args->offset);
  1169. *p = cpu_to_be32(args->count);
  1170. }
  1171. static void nfs3_xdr_enc_commit3args(struct rpc_rqst *req,
  1172. struct xdr_stream *xdr,
  1173. const struct nfs_writeargs *args)
  1174. {
  1175. encode_commit3args(xdr, args);
  1176. }
  1177. #ifdef CONFIG_NFS_V3_ACL
  1178. static void nfs3_xdr_enc_getacl3args(struct rpc_rqst *req,
  1179. struct xdr_stream *xdr,
  1180. const struct nfs3_getaclargs *args)
  1181. {
  1182. encode_nfs_fh3(xdr, args->fh);
  1183. encode_uint32(xdr, args->mask);
  1184. if (args->mask & (NFS_ACL | NFS_DFACL))
  1185. prepare_reply_buffer(req, args->pages, 0,
  1186. NFSACL_MAXPAGES << PAGE_SHIFT,
  1187. ACL3_getaclres_sz);
  1188. }
  1189. static void nfs3_xdr_enc_setacl3args(struct rpc_rqst *req,
  1190. struct xdr_stream *xdr,
  1191. const struct nfs3_setaclargs *args)
  1192. {
  1193. unsigned int base;
  1194. int error;
  1195. encode_nfs_fh3(xdr, NFS_FH(args->inode));
  1196. encode_uint32(xdr, args->mask);
  1197. if (args->npages != 0)
  1198. xdr_write_pages(xdr, args->pages, 0, args->len);
  1199. base = req->rq_slen;
  1200. error = nfsacl_encode(xdr->buf, base, args->inode,
  1201. (args->mask & NFS_ACL) ?
  1202. args->acl_access : NULL, 1, 0);
  1203. BUG_ON(error < 0);
  1204. error = nfsacl_encode(xdr->buf, base + error, args->inode,
  1205. (args->mask & NFS_DFACL) ?
  1206. args->acl_default : NULL, 1,
  1207. NFS_ACL_DEFAULT);
  1208. BUG_ON(error < 0);
  1209. }
  1210. #endif /* CONFIG_NFS_V3_ACL */
  1211. /*
  1212. * NFSv3 XDR decode functions
  1213. *
  1214. * NFSv3 result types are defined in section 3.3 of RFC 1813:
  1215. * "NFS Version 3 Protocol Specification".
  1216. */
  1217. /*
  1218. * 3.3.1 GETATTR3res
  1219. *
  1220. * struct GETATTR3resok {
  1221. * fattr3 obj_attributes;
  1222. * };
  1223. *
  1224. * union GETATTR3res switch (nfsstat3 status) {
  1225. * case NFS3_OK:
  1226. * GETATTR3resok resok;
  1227. * default:
  1228. * void;
  1229. * };
  1230. */
  1231. static int nfs3_xdr_dec_getattr3res(struct rpc_rqst *req,
  1232. struct xdr_stream *xdr,
  1233. struct nfs_fattr *result)
  1234. {
  1235. enum nfs_stat status;
  1236. int error;
  1237. error = decode_nfsstat3(xdr, &status);
  1238. if (unlikely(error))
  1239. goto out;
  1240. if (status != NFS3_OK)
  1241. goto out_default;
  1242. error = decode_fattr3(xdr, result);
  1243. out:
  1244. return error;
  1245. out_default:
  1246. return nfs_stat_to_errno(status);
  1247. }
  1248. /*
  1249. * 3.3.2 SETATTR3res
  1250. *
  1251. * struct SETATTR3resok {
  1252. * wcc_data obj_wcc;
  1253. * };
  1254. *
  1255. * struct SETATTR3resfail {
  1256. * wcc_data obj_wcc;
  1257. * };
  1258. *
  1259. * union SETATTR3res switch (nfsstat3 status) {
  1260. * case NFS3_OK:
  1261. * SETATTR3resok resok;
  1262. * default:
  1263. * SETATTR3resfail resfail;
  1264. * };
  1265. */
  1266. static int nfs3_xdr_dec_setattr3res(struct rpc_rqst *req,
  1267. struct xdr_stream *xdr,
  1268. struct nfs_fattr *result)
  1269. {
  1270. enum nfs_stat status;
  1271. int error;
  1272. error = decode_nfsstat3(xdr, &status);
  1273. if (unlikely(error))
  1274. goto out;
  1275. error = decode_wcc_data(xdr, result);
  1276. if (unlikely(error))
  1277. goto out;
  1278. if (status != NFS3_OK)
  1279. goto out_status;
  1280. out:
  1281. return error;
  1282. out_status:
  1283. return nfs_stat_to_errno(status);
  1284. }
  1285. /*
  1286. * 3.3.3 LOOKUP3res
  1287. *
  1288. * struct LOOKUP3resok {
  1289. * nfs_fh3 object;
  1290. * post_op_attr obj_attributes;
  1291. * post_op_attr dir_attributes;
  1292. * };
  1293. *
  1294. * struct LOOKUP3resfail {
  1295. * post_op_attr dir_attributes;
  1296. * };
  1297. *
  1298. * union LOOKUP3res switch (nfsstat3 status) {
  1299. * case NFS3_OK:
  1300. * LOOKUP3resok resok;
  1301. * default:
  1302. * LOOKUP3resfail resfail;
  1303. * };
  1304. */
  1305. static int nfs3_xdr_dec_lookup3res(struct rpc_rqst *req,
  1306. struct xdr_stream *xdr,
  1307. struct nfs3_diropres *result)
  1308. {
  1309. enum nfs_stat status;
  1310. int error;
  1311. error = decode_nfsstat3(xdr, &status);
  1312. if (unlikely(error))
  1313. goto out;
  1314. if (status != NFS3_OK)
  1315. goto out_default;
  1316. error = decode_nfs_fh3(xdr, result->fh);
  1317. if (unlikely(error))
  1318. goto out;
  1319. error = decode_post_op_attr(xdr, result->fattr);
  1320. if (unlikely(error))
  1321. goto out;
  1322. error = decode_post_op_attr(xdr, result->dir_attr);
  1323. out:
  1324. return error;
  1325. out_default:
  1326. error = decode_post_op_attr(xdr, result->dir_attr);
  1327. if (unlikely(error))
  1328. goto out;
  1329. return nfs_stat_to_errno(status);
  1330. }
  1331. /*
  1332. * 3.3.4 ACCESS3res
  1333. *
  1334. * struct ACCESS3resok {
  1335. * post_op_attr obj_attributes;
  1336. * uint32 access;
  1337. * };
  1338. *
  1339. * struct ACCESS3resfail {
  1340. * post_op_attr obj_attributes;
  1341. * };
  1342. *
  1343. * union ACCESS3res switch (nfsstat3 status) {
  1344. * case NFS3_OK:
  1345. * ACCESS3resok resok;
  1346. * default:
  1347. * ACCESS3resfail resfail;
  1348. * };
  1349. */
  1350. static int nfs3_xdr_dec_access3res(struct rpc_rqst *req,
  1351. struct xdr_stream *xdr,
  1352. struct nfs3_accessres *result)
  1353. {
  1354. enum nfs_stat status;
  1355. int error;
  1356. error = decode_nfsstat3(xdr, &status);
  1357. if (unlikely(error))
  1358. goto out;
  1359. error = decode_post_op_attr(xdr, result->fattr);
  1360. if (unlikely(error))
  1361. goto out;
  1362. if (status != NFS3_OK)
  1363. goto out_default;
  1364. error = decode_uint32(xdr, &result->access);
  1365. out:
  1366. return error;
  1367. out_default:
  1368. return nfs_stat_to_errno(status);
  1369. }
  1370. /*
  1371. * 3.3.5 READLINK3res
  1372. *
  1373. * struct READLINK3resok {
  1374. * post_op_attr symlink_attributes;
  1375. * nfspath3 data;
  1376. * };
  1377. *
  1378. * struct READLINK3resfail {
  1379. * post_op_attr symlink_attributes;
  1380. * };
  1381. *
  1382. * union READLINK3res switch (nfsstat3 status) {
  1383. * case NFS3_OK:
  1384. * READLINK3resok resok;
  1385. * default:
  1386. * READLINK3resfail resfail;
  1387. * };
  1388. */
  1389. static int nfs3_xdr_dec_readlink3res(struct rpc_rqst *req,
  1390. struct xdr_stream *xdr,
  1391. struct nfs_fattr *result)
  1392. {
  1393. enum nfs_stat status;
  1394. int error;
  1395. error = decode_nfsstat3(xdr, &status);
  1396. if (unlikely(error))
  1397. goto out;
  1398. error = decode_post_op_attr(xdr, result);
  1399. if (unlikely(error))
  1400. goto out;
  1401. if (status != NFS3_OK)
  1402. goto out_default;
  1403. error = decode_nfspath3(xdr);
  1404. out:
  1405. return error;
  1406. out_default:
  1407. return nfs_stat_to_errno(status);
  1408. }
  1409. /*
  1410. * 3.3.6 READ3res
  1411. *
  1412. * struct READ3resok {
  1413. * post_op_attr file_attributes;
  1414. * count3 count;
  1415. * bool eof;
  1416. * opaque data<>;
  1417. * };
  1418. *
  1419. * struct READ3resfail {
  1420. * post_op_attr file_attributes;
  1421. * };
  1422. *
  1423. * union READ3res switch (nfsstat3 status) {
  1424. * case NFS3_OK:
  1425. * READ3resok resok;
  1426. * default:
  1427. * READ3resfail resfail;
  1428. * };
  1429. */
  1430. static int decode_read3resok(struct xdr_stream *xdr,
  1431. struct nfs_readres *result)
  1432. {
  1433. u32 eof, count, ocount, recvd;
  1434. size_t hdrlen;
  1435. __be32 *p;
  1436. p = xdr_inline_decode(xdr, 4 + 4 + 4);
  1437. if (unlikely(p == NULL))
  1438. goto out_overflow;
  1439. count = be32_to_cpup(p++);
  1440. eof = be32_to_cpup(p++);
  1441. ocount = be32_to_cpup(p++);
  1442. if (unlikely(ocount != count))
  1443. goto out_mismatch;
  1444. hdrlen = (u8 *)xdr->p - (u8 *)xdr->iov->iov_base;
  1445. recvd = xdr->buf->len - hdrlen;
  1446. if (unlikely(count > recvd))
  1447. goto out_cheating;
  1448. out:
  1449. xdr_read_pages(xdr, count);
  1450. result->eof = eof;
  1451. result->count = count;
  1452. return count;
  1453. out_mismatch:
  1454. dprintk("NFS: READ count doesn't match length of opaque: "
  1455. "count %u != ocount %u\n", count, ocount);
  1456. return -EIO;
  1457. out_cheating:
  1458. dprintk("NFS: server cheating in read result: "
  1459. "count %u > recvd %u\n", count, recvd);
  1460. count = recvd;
  1461. eof = 0;
  1462. goto out;
  1463. out_overflow:
  1464. print_overflow_msg(__func__, xdr);
  1465. return -EIO;
  1466. }
  1467. static int nfs3_xdr_dec_read3res(struct rpc_rqst *req, struct xdr_stream *xdr,
  1468. struct nfs_readres *result)
  1469. {
  1470. enum nfs_stat status;
  1471. int error;
  1472. error = decode_nfsstat3(xdr, &status);
  1473. if (unlikely(error))
  1474. goto out;
  1475. error = decode_post_op_attr(xdr, result->fattr);
  1476. if (unlikely(error))
  1477. goto out;
  1478. if (status != NFS3_OK)
  1479. goto out_status;
  1480. error = decode_read3resok(xdr, result);
  1481. out:
  1482. return error;
  1483. out_status:
  1484. return nfs_stat_to_errno(status);
  1485. }
  1486. /*
  1487. * 3.3.7 WRITE3res
  1488. *
  1489. * enum stable_how {
  1490. * UNSTABLE = 0,
  1491. * DATA_SYNC = 1,
  1492. * FILE_SYNC = 2
  1493. * };
  1494. *
  1495. * struct WRITE3resok {
  1496. * wcc_data file_wcc;
  1497. * count3 count;
  1498. * stable_how committed;
  1499. * writeverf3 verf;
  1500. * };
  1501. *
  1502. * struct WRITE3resfail {
  1503. * wcc_data file_wcc;
  1504. * };
  1505. *
  1506. * union WRITE3res switch (nfsstat3 status) {
  1507. * case NFS3_OK:
  1508. * WRITE3resok resok;
  1509. * default:
  1510. * WRITE3resfail resfail;
  1511. * };
  1512. */
  1513. static int decode_write3resok(struct xdr_stream *xdr,
  1514. struct nfs_writeres *result)
  1515. {
  1516. __be32 *p;
  1517. p = xdr_inline_decode(xdr, 4 + 4 + NFS3_WRITEVERFSIZE);
  1518. if (unlikely(p == NULL))
  1519. goto out_overflow;
  1520. result->count = be32_to_cpup(p++);
  1521. result->verf->committed = be32_to_cpup(p++);
  1522. if (unlikely(result->verf->committed > NFS_FILE_SYNC))
  1523. goto out_badvalue;
  1524. memcpy(result->verf->verifier, p, NFS3_WRITEVERFSIZE);
  1525. return result->count;
  1526. out_badvalue:
  1527. dprintk("NFS: bad stable_how value: %u\n", result->verf->committed);
  1528. return -EIO;
  1529. out_overflow:
  1530. print_overflow_msg(__func__, xdr);
  1531. return -EIO;
  1532. }
  1533. static int nfs3_xdr_dec_write3res(struct rpc_rqst *req, struct xdr_stream *xdr,
  1534. struct nfs_writeres *result)
  1535. {
  1536. enum nfs_stat status;
  1537. int error;
  1538. error = decode_nfsstat3(xdr, &status);
  1539. if (unlikely(error))
  1540. goto out;
  1541. error = decode_wcc_data(xdr, result->fattr);
  1542. if (unlikely(error))
  1543. goto out;
  1544. if (status != NFS3_OK)
  1545. goto out_status;
  1546. error = decode_write3resok(xdr, result);
  1547. out:
  1548. return error;
  1549. out_status:
  1550. return nfs_stat_to_errno(status);
  1551. }
  1552. /*
  1553. * 3.3.8 CREATE3res
  1554. *
  1555. * struct CREATE3resok {
  1556. * post_op_fh3 obj;
  1557. * post_op_attr obj_attributes;
  1558. * wcc_data dir_wcc;
  1559. * };
  1560. *
  1561. * struct CREATE3resfail {
  1562. * wcc_data dir_wcc;
  1563. * };
  1564. *
  1565. * union CREATE3res switch (nfsstat3 status) {
  1566. * case NFS3_OK:
  1567. * CREATE3resok resok;
  1568. * default:
  1569. * CREATE3resfail resfail;
  1570. * };
  1571. */
  1572. static int decode_create3resok(struct xdr_stream *xdr,
  1573. struct nfs3_diropres *result)
  1574. {
  1575. int error;
  1576. error = decode_post_op_fh3(xdr, result->fh);
  1577. if (unlikely(error))
  1578. goto out;
  1579. error = decode_post_op_attr(xdr, result->fattr);
  1580. if (unlikely(error))
  1581. goto out;
  1582. /* The server isn't required to return a file handle.
  1583. * If it didn't, force the client to perform a LOOKUP
  1584. * to determine the correct file handle and attribute
  1585. * values for the new object. */
  1586. if (result->fh->size == 0)
  1587. result->fattr->valid = 0;
  1588. error = decode_wcc_data(xdr, result->dir_attr);
  1589. out:
  1590. return error;
  1591. }
  1592. static int nfs3_xdr_dec_create3res(struct rpc_rqst *req,
  1593. struct xdr_stream *xdr,
  1594. struct nfs3_diropres *result)
  1595. {
  1596. enum nfs_stat status;
  1597. int error;
  1598. error = decode_nfsstat3(xdr, &status);
  1599. if (unlikely(error))
  1600. goto out;
  1601. if (status != NFS3_OK)
  1602. goto out_default;
  1603. error = decode_create3resok(xdr, result);
  1604. out:
  1605. return error;
  1606. out_default:
  1607. error = decode_wcc_data(xdr, result->dir_attr);
  1608. if (unlikely(error))
  1609. goto out;
  1610. return nfs_stat_to_errno(status);
  1611. }
  1612. /*
  1613. * 3.3.12 REMOVE3res
  1614. *
  1615. * struct REMOVE3resok {
  1616. * wcc_data dir_wcc;
  1617. * };
  1618. *
  1619. * struct REMOVE3resfail {
  1620. * wcc_data dir_wcc;
  1621. * };
  1622. *
  1623. * union REMOVE3res switch (nfsstat3 status) {
  1624. * case NFS3_OK:
  1625. * REMOVE3resok resok;
  1626. * default:
  1627. * REMOVE3resfail resfail;
  1628. * };
  1629. */
  1630. static int nfs3_xdr_dec_remove3res(struct rpc_rqst *req,
  1631. struct xdr_stream *xdr,
  1632. struct nfs_removeres *result)
  1633. {
  1634. enum nfs_stat status;
  1635. int error;
  1636. error = decode_nfsstat3(xdr, &status);
  1637. if (unlikely(error))
  1638. goto out;
  1639. error = decode_wcc_data(xdr, result->dir_attr);
  1640. if (unlikely(error))
  1641. goto out;
  1642. if (status != NFS3_OK)
  1643. goto out_status;
  1644. out:
  1645. return error;
  1646. out_status:
  1647. return nfs_stat_to_errno(status);
  1648. }
  1649. /*
  1650. * 3.3.14 RENAME3res
  1651. *
  1652. * struct RENAME3resok {
  1653. * wcc_data fromdir_wcc;
  1654. * wcc_data todir_wcc;
  1655. * };
  1656. *
  1657. * struct RENAME3resfail {
  1658. * wcc_data fromdir_wcc;
  1659. * wcc_data todir_wcc;
  1660. * };
  1661. *
  1662. * union RENAME3res switch (nfsstat3 status) {
  1663. * case NFS3_OK:
  1664. * RENAME3resok resok;
  1665. * default:
  1666. * RENAME3resfail resfail;
  1667. * };
  1668. */
  1669. static int nfs3_xdr_dec_rename3res(struct rpc_rqst *req,
  1670. struct xdr_stream *xdr,
  1671. struct nfs_renameres *result)
  1672. {
  1673. enum nfs_stat status;
  1674. int error;
  1675. error = decode_nfsstat3(xdr, &status);
  1676. if (unlikely(error))
  1677. goto out;
  1678. error = decode_wcc_data(xdr, result->old_fattr);
  1679. if (unlikely(error))
  1680. goto out;
  1681. error = decode_wcc_data(xdr, result->new_fattr);
  1682. if (unlikely(error))
  1683. goto out;
  1684. if (status != NFS3_OK)
  1685. goto out_status;
  1686. out:
  1687. return error;
  1688. out_status:
  1689. return nfs_stat_to_errno(status);
  1690. }
  1691. /*
  1692. * 3.3.15 LINK3res
  1693. *
  1694. * struct LINK3resok {
  1695. * post_op_attr file_attributes;
  1696. * wcc_data linkdir_wcc;
  1697. * };
  1698. *
  1699. * struct LINK3resfail {
  1700. * post_op_attr file_attributes;
  1701. * wcc_data linkdir_wcc;
  1702. * };
  1703. *
  1704. * union LINK3res switch (nfsstat3 status) {
  1705. * case NFS3_OK:
  1706. * LINK3resok resok;
  1707. * default:
  1708. * LINK3resfail resfail;
  1709. * };
  1710. */
  1711. static int nfs3_xdr_dec_link3res(struct rpc_rqst *req, struct xdr_stream *xdr,
  1712. struct nfs3_linkres *result)
  1713. {
  1714. enum nfs_stat status;
  1715. int error;
  1716. error = decode_nfsstat3(xdr, &status);
  1717. if (unlikely(error))
  1718. goto out;
  1719. error = decode_post_op_attr(xdr, result->fattr);
  1720. if (unlikely(error))
  1721. goto out;
  1722. error = decode_wcc_data(xdr, result->dir_attr);
  1723. if (unlikely(error))
  1724. goto out;
  1725. if (status != NFS3_OK)
  1726. goto out_status;
  1727. out:
  1728. return error;
  1729. out_status:
  1730. return nfs_stat_to_errno(status);
  1731. }
  1732. /**
  1733. * nfs3_decode_dirent - Decode a single NFSv3 directory entry stored in
  1734. * the local page cache
  1735. * @xdr: XDR stream where entry resides
  1736. * @entry: buffer to fill in with entry data
  1737. * @plus: boolean indicating whether this should be a readdirplus entry
  1738. *
  1739. * Returns zero if successful, otherwise a negative errno value is
  1740. * returned.
  1741. *
  1742. * This function is not invoked during READDIR reply decoding, but
  1743. * rather whenever an application invokes the getdents(2) system call
  1744. * on a directory already in our cache.
  1745. *
  1746. * 3.3.16 entry3
  1747. *
  1748. * struct entry3 {
  1749. * fileid3 fileid;
  1750. * filename3 name;
  1751. * cookie3 cookie;
  1752. * fhandle3 filehandle;
  1753. * post_op_attr3 attributes;
  1754. * entry3 *nextentry;
  1755. * };
  1756. *
  1757. * 3.3.17 entryplus3
  1758. * struct entryplus3 {
  1759. * fileid3 fileid;
  1760. * filename3 name;
  1761. * cookie3 cookie;
  1762. * post_op_attr name_attributes;
  1763. * post_op_fh3 name_handle;
  1764. * entryplus3 *nextentry;
  1765. * };
  1766. */
  1767. int nfs3_decode_dirent(struct xdr_stream *xdr, struct nfs_entry *entry,
  1768. int plus)
  1769. {
  1770. struct nfs_entry old = *entry;
  1771. __be32 *p;
  1772. int error;
  1773. p = xdr_inline_decode(xdr, 4);
  1774. if (unlikely(p == NULL))
  1775. goto out_overflow;
  1776. if (*p == xdr_zero) {
  1777. p = xdr_inline_decode(xdr, 4);
  1778. if (unlikely(p == NULL))
  1779. goto out_overflow;
  1780. if (*p == xdr_zero)
  1781. return -EAGAIN;
  1782. entry->eof = 1;
  1783. return -EBADCOOKIE;
  1784. }
  1785. error = decode_fileid3(xdr, &entry->ino);
  1786. if (unlikely(error))
  1787. return error;
  1788. error = decode_inline_filename3(xdr, &entry->name, &entry->len);
  1789. if (unlikely(error))
  1790. return error;
  1791. entry->prev_cookie = entry->cookie;
  1792. error = decode_cookie3(xdr, &entry->cookie);
  1793. if (unlikely(error))
  1794. return error;
  1795. entry->d_type = DT_UNKNOWN;
  1796. if (plus) {
  1797. entry->fattr->valid = 0;
  1798. error = decode_post_op_attr(xdr, entry->fattr);
  1799. if (unlikely(error))
  1800. return error;
  1801. if (entry->fattr->valid & NFS_ATTR_FATTR_V3)
  1802. entry->d_type = nfs_umode_to_dtype(entry->fattr->mode);
  1803. /* In fact, a post_op_fh3: */
  1804. p = xdr_inline_decode(xdr, 4);
  1805. if (unlikely(p == NULL))
  1806. goto out_overflow;
  1807. if (*p != xdr_zero) {
  1808. error = decode_nfs_fh3(xdr, entry->fh);
  1809. if (unlikely(error)) {
  1810. if (error == -E2BIG)
  1811. goto out_truncated;
  1812. return error;
  1813. }
  1814. } else
  1815. zero_nfs_fh3(entry->fh);
  1816. }
  1817. /* Peek at the next entry to see if we're at EOD */
  1818. p = xdr_inline_peek(xdr, 4 + 4);
  1819. entry->eof = 0;
  1820. if (p != NULL)
  1821. entry->eof = (p[0] == xdr_zero) && (p[1] != xdr_zero);
  1822. return 0;
  1823. out_overflow:
  1824. print_overflow_msg(__func__, xdr);
  1825. return -EAGAIN;
  1826. out_truncated:
  1827. dprintk("NFS: directory entry contains invalid file handle\n");
  1828. *entry = old;
  1829. return -EAGAIN;
  1830. }
  1831. /*
  1832. * 3.3.16 READDIR3res
  1833. *
  1834. * struct dirlist3 {
  1835. * entry3 *entries;
  1836. * bool eof;
  1837. * };
  1838. *
  1839. * struct READDIR3resok {
  1840. * post_op_attr dir_attributes;
  1841. * cookieverf3 cookieverf;
  1842. * dirlist3 reply;
  1843. * };
  1844. *
  1845. * struct READDIR3resfail {
  1846. * post_op_attr dir_attributes;
  1847. * };
  1848. *
  1849. * union READDIR3res switch (nfsstat3 status) {
  1850. * case NFS3_OK:
  1851. * READDIR3resok resok;
  1852. * default:
  1853. * READDIR3resfail resfail;
  1854. * };
  1855. *
  1856. * Read the directory contents into the page cache, but otherwise
  1857. * don't touch them. The actual decoding is done by nfs3_decode_entry()
  1858. * during subsequent nfs_readdir() calls.
  1859. */
  1860. static int decode_dirlist3(struct xdr_stream *xdr)
  1861. {
  1862. u32 recvd, pglen;
  1863. size_t hdrlen;
  1864. pglen = xdr->buf->page_len;
  1865. hdrlen = (u8 *)xdr->p - (u8 *)xdr->iov->iov_base;
  1866. recvd = xdr->buf->len - hdrlen;
  1867. if (unlikely(pglen > recvd))
  1868. goto out_cheating;
  1869. out:
  1870. xdr_read_pages(xdr, pglen);
  1871. return pglen;
  1872. out_cheating:
  1873. dprintk("NFS: server cheating in readdir result: "
  1874. "pglen %u > recvd %u\n", pglen, recvd);
  1875. pglen = recvd;
  1876. goto out;
  1877. }
  1878. static int decode_readdir3resok(struct xdr_stream *xdr,
  1879. struct nfs3_readdirres *result)
  1880. {
  1881. int error;
  1882. error = decode_post_op_attr(xdr, result->dir_attr);
  1883. if (unlikely(error))
  1884. goto out;
  1885. /* XXX: do we need to check if result->verf != NULL ? */
  1886. error = decode_cookieverf3(xdr, result->verf);
  1887. if (unlikely(error))
  1888. goto out;
  1889. error = decode_dirlist3(xdr);
  1890. out:
  1891. return error;
  1892. }
  1893. static int nfs3_xdr_dec_readdir3res(struct rpc_rqst *req,
  1894. struct xdr_stream *xdr,
  1895. struct nfs3_readdirres *result)
  1896. {
  1897. enum nfs_stat status;
  1898. int error;
  1899. error = decode_nfsstat3(xdr, &status);
  1900. if (unlikely(error))
  1901. goto out;
  1902. if (status != NFS3_OK)
  1903. goto out_default;
  1904. error = decode_readdir3resok(xdr, result);
  1905. out:
  1906. return error;
  1907. out_default:
  1908. error = decode_post_op_attr(xdr, result->dir_attr);
  1909. if (unlikely(error))
  1910. goto out;
  1911. return nfs_stat_to_errno(status);
  1912. }
  1913. /*
  1914. * 3.3.18 FSSTAT3res
  1915. *
  1916. * struct FSSTAT3resok {
  1917. * post_op_attr obj_attributes;
  1918. * size3 tbytes;
  1919. * size3 fbytes;
  1920. * size3 abytes;
  1921. * size3 tfiles;
  1922. * size3 ffiles;
  1923. * size3 afiles;
  1924. * uint32 invarsec;
  1925. * };
  1926. *
  1927. * struct FSSTAT3resfail {
  1928. * post_op_attr obj_attributes;
  1929. * };
  1930. *
  1931. * union FSSTAT3res switch (nfsstat3 status) {
  1932. * case NFS3_OK:
  1933. * FSSTAT3resok resok;
  1934. * default:
  1935. * FSSTAT3resfail resfail;
  1936. * };
  1937. */
  1938. static int decode_fsstat3resok(struct xdr_stream *xdr,
  1939. struct nfs_fsstat *result)
  1940. {
  1941. __be32 *p;
  1942. p = xdr_inline_decode(xdr, 8 * 6 + 4);
  1943. if (unlikely(p == NULL))
  1944. goto out_overflow;
  1945. p = xdr_decode_size3(p, &result->tbytes);
  1946. p = xdr_decode_size3(p, &result->fbytes);
  1947. p = xdr_decode_size3(p, &result->abytes);
  1948. p = xdr_decode_size3(p, &result->tfiles);
  1949. p = xdr_decode_size3(p, &result->ffiles);
  1950. xdr_decode_size3(p, &result->afiles);
  1951. /* ignore invarsec */
  1952. return 0;
  1953. out_overflow:
  1954. print_overflow_msg(__func__, xdr);
  1955. return -EIO;
  1956. }
  1957. static int nfs3_xdr_dec_fsstat3res(struct rpc_rqst *req,
  1958. struct xdr_stream *xdr,
  1959. struct nfs_fsstat *result)
  1960. {
  1961. enum nfs_stat status;
  1962. int error;
  1963. error = decode_nfsstat3(xdr, &status);
  1964. if (unlikely(error))
  1965. goto out;
  1966. error = decode_post_op_attr(xdr, result->fattr);
  1967. if (unlikely(error))
  1968. goto out;
  1969. if (status != NFS3_OK)
  1970. goto out_status;
  1971. error = decode_fsstat3resok(xdr, result);
  1972. out:
  1973. return error;
  1974. out_status:
  1975. return nfs_stat_to_errno(status);
  1976. }
  1977. /*
  1978. * 3.3.19 FSINFO3res
  1979. *
  1980. * struct FSINFO3resok {
  1981. * post_op_attr obj_attributes;
  1982. * uint32 rtmax;
  1983. * uint32 rtpref;
  1984. * uint32 rtmult;
  1985. * uint32 wtmax;
  1986. * uint32 wtpref;
  1987. * uint32 wtmult;
  1988. * uint32 dtpref;
  1989. * size3 maxfilesize;
  1990. * nfstime3 time_delta;
  1991. * uint32 properties;
  1992. * };
  1993. *
  1994. * struct FSINFO3resfail {
  1995. * post_op_attr obj_attributes;
  1996. * };
  1997. *
  1998. * union FSINFO3res switch (nfsstat3 status) {
  1999. * case NFS3_OK:
  2000. * FSINFO3resok resok;
  2001. * default:
  2002. * FSINFO3resfail resfail;
  2003. * };
  2004. */
  2005. static int decode_fsinfo3resok(struct xdr_stream *xdr,
  2006. struct nfs_fsinfo *result)
  2007. {
  2008. __be32 *p;
  2009. p = xdr_inline_decode(xdr, 4 * 7 + 8 + 8 + 4);
  2010. if (unlikely(p == NULL))
  2011. goto out_overflow;
  2012. result->rtmax = be32_to_cpup(p++);
  2013. result->rtpref = be32_to_cpup(p++);
  2014. result->rtmult = be32_to_cpup(p++);
  2015. result->wtmax = be32_to_cpup(p++);
  2016. result->wtpref = be32_to_cpup(p++);
  2017. result->wtmult = be32_to_cpup(p++);
  2018. result->dtpref = be32_to_cpup(p++);
  2019. p = xdr_decode_size3(p, &result->maxfilesize);
  2020. xdr_decode_nfstime3(p, &result->time_delta);
  2021. /* ignore properties */
  2022. result->lease_time = 0;
  2023. return 0;
  2024. out_overflow:
  2025. print_overflow_msg(__func__, xdr);
  2026. return -EIO;
  2027. }
  2028. static int nfs3_xdr_dec_fsinfo3res(struct rpc_rqst *req,
  2029. struct xdr_stream *xdr,
  2030. struct nfs_fsinfo *result)
  2031. {
  2032. enum nfs_stat status;
  2033. int error;
  2034. error = decode_nfsstat3(xdr, &status);
  2035. if (unlikely(error))
  2036. goto out;
  2037. error = decode_post_op_attr(xdr, result->fattr);
  2038. if (unlikely(error))
  2039. goto out;
  2040. if (status != NFS3_OK)
  2041. goto out_status;
  2042. error = decode_fsinfo3resok(xdr, result);
  2043. out:
  2044. return error;
  2045. out_status:
  2046. return nfs_stat_to_errno(status);
  2047. }
  2048. /*
  2049. * 3.3.20 PATHCONF3res
  2050. *
  2051. * struct PATHCONF3resok {
  2052. * post_op_attr obj_attributes;
  2053. * uint32 linkmax;
  2054. * uint32 name_max;
  2055. * bool no_trunc;
  2056. * bool chown_restricted;
  2057. * bool case_insensitive;
  2058. * bool case_preserving;
  2059. * };
  2060. *
  2061. * struct PATHCONF3resfail {
  2062. * post_op_attr obj_attributes;
  2063. * };
  2064. *
  2065. * union PATHCONF3res switch (nfsstat3 status) {
  2066. * case NFS3_OK:
  2067. * PATHCONF3resok resok;
  2068. * default:
  2069. * PATHCONF3resfail resfail;
  2070. * };
  2071. */
  2072. static int decode_pathconf3resok(struct xdr_stream *xdr,
  2073. struct nfs_pathconf *result)
  2074. {
  2075. __be32 *p;
  2076. p = xdr_inline_decode(xdr, 4 * 6);
  2077. if (unlikely(p == NULL))
  2078. goto out_overflow;
  2079. result->max_link = be32_to_cpup(p++);
  2080. result->max_namelen = be32_to_cpup(p);
  2081. /* ignore remaining fields */
  2082. return 0;
  2083. out_overflow:
  2084. print_overflow_msg(__func__, xdr);
  2085. return -EIO;
  2086. }
  2087. static int nfs3_xdr_dec_pathconf3res(struct rpc_rqst *req,
  2088. struct xdr_stream *xdr,
  2089. struct nfs_pathconf *result)
  2090. {
  2091. enum nfs_stat status;
  2092. int error;
  2093. error = decode_nfsstat3(xdr, &status);
  2094. if (unlikely(error))
  2095. goto out;
  2096. error = decode_post_op_attr(xdr, result->fattr);
  2097. if (unlikely(error))
  2098. goto out;
  2099. if (status != NFS3_OK)
  2100. goto out_status;
  2101. error = decode_pathconf3resok(xdr, result);
  2102. out:
  2103. return error;
  2104. out_status:
  2105. return nfs_stat_to_errno(status);
  2106. }
  2107. /*
  2108. * 3.3.21 COMMIT3res
  2109. *
  2110. * struct COMMIT3resok {
  2111. * wcc_data file_wcc;
  2112. * writeverf3 verf;
  2113. * };
  2114. *
  2115. * struct COMMIT3resfail {
  2116. * wcc_data file_wcc;
  2117. * };
  2118. *
  2119. * union COMMIT3res switch (nfsstat3 status) {
  2120. * case NFS3_OK:
  2121. * COMMIT3resok resok;
  2122. * default:
  2123. * COMMIT3resfail resfail;
  2124. * };
  2125. */
  2126. static int nfs3_xdr_dec_commit3res(struct rpc_rqst *req,
  2127. struct xdr_stream *xdr,
  2128. struct nfs_writeres *result)
  2129. {
  2130. enum nfs_stat status;
  2131. int error;
  2132. error = decode_nfsstat3(xdr, &status);
  2133. if (unlikely(error))
  2134. goto out;
  2135. error = decode_wcc_data(xdr, result->fattr);
  2136. if (unlikely(error))
  2137. goto out;
  2138. if (status != NFS3_OK)
  2139. goto out_status;
  2140. error = decode_writeverf3(xdr, result->verf->verifier);
  2141. out:
  2142. return error;
  2143. out_status:
  2144. return nfs_stat_to_errno(status);
  2145. }
  2146. #ifdef CONFIG_NFS_V3_ACL
  2147. static inline int decode_getacl3resok(struct xdr_stream *xdr,
  2148. struct nfs3_getaclres *result)
  2149. {
  2150. struct posix_acl **acl;
  2151. unsigned int *aclcnt;
  2152. size_t hdrlen;
  2153. int error;
  2154. error = decode_post_op_attr(xdr, result->fattr);
  2155. if (unlikely(error))
  2156. goto out;
  2157. error = decode_uint32(xdr, &result->mask);
  2158. if (unlikely(error))
  2159. goto out;
  2160. error = -EINVAL;
  2161. if (result->mask & ~(NFS_ACL|NFS_ACLCNT|NFS_DFACL|NFS_DFACLCNT))
  2162. goto out;
  2163. hdrlen = (u8 *)xdr->p - (u8 *)xdr->iov->iov_base;
  2164. acl = NULL;
  2165. if (result->mask & NFS_ACL)
  2166. acl = &result->acl_access;
  2167. aclcnt = NULL;
  2168. if (result->mask & NFS_ACLCNT)
  2169. aclcnt = &result->acl_access_count;
  2170. error = nfsacl_decode(xdr->buf, hdrlen, aclcnt, acl);
  2171. if (unlikely(error <= 0))
  2172. goto out;
  2173. acl = NULL;
  2174. if (result->mask & NFS_DFACL)
  2175. acl = &result->acl_default;
  2176. aclcnt = NULL;
  2177. if (result->mask & NFS_DFACLCNT)
  2178. aclcnt = &result->acl_default_count;
  2179. error = nfsacl_decode(xdr->buf, hdrlen + error, aclcnt, acl);
  2180. if (unlikely(error <= 0))
  2181. return error;
  2182. error = 0;
  2183. out:
  2184. return error;
  2185. }
  2186. static int nfs3_xdr_dec_getacl3res(struct rpc_rqst *req,
  2187. struct xdr_stream *xdr,
  2188. struct nfs3_getaclres *result)
  2189. {
  2190. enum nfs_stat status;
  2191. int error;
  2192. error = decode_nfsstat3(xdr, &status);
  2193. if (unlikely(error))
  2194. goto out;
  2195. if (status != NFS3_OK)
  2196. goto out_default;
  2197. error = decode_getacl3resok(xdr, result);
  2198. out:
  2199. return error;
  2200. out_default:
  2201. return nfs_stat_to_errno(status);
  2202. }
  2203. static int nfs3_xdr_dec_setacl3res(struct rpc_rqst *req,
  2204. struct xdr_stream *xdr,
  2205. struct nfs_fattr *result)
  2206. {
  2207. enum nfs_stat status;
  2208. int error;
  2209. error = decode_nfsstat3(xdr, &status);
  2210. if (unlikely(error))
  2211. goto out;
  2212. if (status != NFS3_OK)
  2213. goto out_default;
  2214. error = decode_post_op_attr(xdr, result);
  2215. out:
  2216. return error;
  2217. out_default:
  2218. return nfs_stat_to_errno(status);
  2219. }
  2220. #endif /* CONFIG_NFS_V3_ACL */
  2221. #define PROC(proc, argtype, restype, timer) \
  2222. [NFS3PROC_##proc] = { \
  2223. .p_proc = NFS3PROC_##proc, \
  2224. .p_encode = (kxdreproc_t)nfs3_xdr_enc_##argtype##3args, \
  2225. .p_decode = (kxdrdproc_t)nfs3_xdr_dec_##restype##3res, \
  2226. .p_arglen = NFS3_##argtype##args_sz, \
  2227. .p_replen = NFS3_##restype##res_sz, \
  2228. .p_timer = timer, \
  2229. .p_statidx = NFS3PROC_##proc, \
  2230. .p_name = #proc, \
  2231. }
  2232. struct rpc_procinfo nfs3_procedures[] = {
  2233. PROC(GETATTR, getattr, getattr, 1),
  2234. PROC(SETATTR, setattr, setattr, 0),
  2235. PROC(LOOKUP, lookup, lookup, 2),
  2236. PROC(ACCESS, access, access, 1),
  2237. PROC(READLINK, readlink, readlink, 3),
  2238. PROC(READ, read, read, 3),
  2239. PROC(WRITE, write, write, 4),
  2240. PROC(CREATE, create, create, 0),
  2241. PROC(MKDIR, mkdir, create, 0),
  2242. PROC(SYMLINK, symlink, create, 0),
  2243. PROC(MKNOD, mknod, create, 0),
  2244. PROC(REMOVE, remove, remove, 0),
  2245. PROC(RMDIR, lookup, setattr, 0),
  2246. PROC(RENAME, rename, rename, 0),
  2247. PROC(LINK, link, link, 0),
  2248. PROC(READDIR, readdir, readdir, 3),
  2249. PROC(READDIRPLUS, readdirplus, readdir, 3),
  2250. PROC(FSSTAT, getattr, fsstat, 0),
  2251. PROC(FSINFO, getattr, fsinfo, 0),
  2252. PROC(PATHCONF, getattr, pathconf, 0),
  2253. PROC(COMMIT, commit, commit, 5),
  2254. };
  2255. struct rpc_version nfs_version3 = {
  2256. .number = 3,
  2257. .nrprocs = ARRAY_SIZE(nfs3_procedures),
  2258. .procs = nfs3_procedures
  2259. };
  2260. #ifdef CONFIG_NFS_V3_ACL
  2261. static struct rpc_procinfo nfs3_acl_procedures[] = {
  2262. [ACLPROC3_GETACL] = {
  2263. .p_proc = ACLPROC3_GETACL,
  2264. .p_encode = (kxdreproc_t)nfs3_xdr_enc_getacl3args,
  2265. .p_decode = (kxdrdproc_t)nfs3_xdr_dec_getacl3res,
  2266. .p_arglen = ACL3_getaclargs_sz,
  2267. .p_replen = ACL3_getaclres_sz,
  2268. .p_timer = 1,
  2269. .p_name = "GETACL",
  2270. },
  2271. [ACLPROC3_SETACL] = {
  2272. .p_proc = ACLPROC3_SETACL,
  2273. .p_encode = (kxdreproc_t)nfs3_xdr_enc_setacl3args,
  2274. .p_decode = (kxdrdproc_t)nfs3_xdr_dec_setacl3res,
  2275. .p_arglen = ACL3_setaclargs_sz,
  2276. .p_replen = ACL3_setaclres_sz,
  2277. .p_timer = 0,
  2278. .p_name = "SETACL",
  2279. },
  2280. };
  2281. struct rpc_version nfsacl_version3 = {
  2282. .number = 3,
  2283. .nrprocs = sizeof(nfs3_acl_procedures)/
  2284. sizeof(nfs3_acl_procedures[0]),
  2285. .procs = nfs3_acl_procedures,
  2286. };
  2287. #endif /* CONFIG_NFS_V3_ACL */