nfs3xdr.c 54 KB

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