nfs3xdr.c 55 KB

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