nfs3xdr.c 55 KB

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