nfs3xdr.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566
  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. xdr->buf->flags |= XDRBUF_WRITE;
  998. }
  999. /*
  1000. * 3.3.11 MKNOD3args
  1001. *
  1002. * struct devicedata3 {
  1003. * sattr3 dev_attributes;
  1004. * specdata3 spec;
  1005. * };
  1006. *
  1007. * union mknoddata3 switch (ftype3 type) {
  1008. * case NF3CHR:
  1009. * case NF3BLK:
  1010. * devicedata3 device;
  1011. * case NF3SOCK:
  1012. * case NF3FIFO:
  1013. * sattr3 pipe_attributes;
  1014. * default:
  1015. * void;
  1016. * };
  1017. *
  1018. * struct MKNOD3args {
  1019. * diropargs3 where;
  1020. * mknoddata3 what;
  1021. * };
  1022. */
  1023. static void encode_devicedata3(struct xdr_stream *xdr,
  1024. const struct nfs3_mknodargs *args)
  1025. {
  1026. encode_sattr3(xdr, args->sattr);
  1027. encode_specdata3(xdr, args->rdev);
  1028. }
  1029. static void encode_mknoddata3(struct xdr_stream *xdr,
  1030. const struct nfs3_mknodargs *args)
  1031. {
  1032. encode_ftype3(xdr, args->type);
  1033. switch (args->type) {
  1034. case NF3CHR:
  1035. case NF3BLK:
  1036. encode_devicedata3(xdr, args);
  1037. break;
  1038. case NF3SOCK:
  1039. case NF3FIFO:
  1040. encode_sattr3(xdr, args->sattr);
  1041. break;
  1042. case NF3REG:
  1043. case NF3DIR:
  1044. break;
  1045. default:
  1046. BUG();
  1047. }
  1048. }
  1049. static void nfs3_xdr_enc_mknod3args(struct rpc_rqst *req,
  1050. struct xdr_stream *xdr,
  1051. const struct nfs3_mknodargs *args)
  1052. {
  1053. encode_diropargs3(xdr, args->fh, args->name, args->len);
  1054. encode_mknoddata3(xdr, args);
  1055. }
  1056. /*
  1057. * 3.3.12 REMOVE3args
  1058. *
  1059. * struct REMOVE3args {
  1060. * diropargs3 object;
  1061. * };
  1062. */
  1063. static void nfs3_xdr_enc_remove3args(struct rpc_rqst *req,
  1064. struct xdr_stream *xdr,
  1065. const struct nfs_removeargs *args)
  1066. {
  1067. encode_diropargs3(xdr, args->fh, args->name.name, args->name.len);
  1068. }
  1069. /*
  1070. * 3.3.14 RENAME3args
  1071. *
  1072. * struct RENAME3args {
  1073. * diropargs3 from;
  1074. * diropargs3 to;
  1075. * };
  1076. */
  1077. static void nfs3_xdr_enc_rename3args(struct rpc_rqst *req,
  1078. struct xdr_stream *xdr,
  1079. const struct nfs_renameargs *args)
  1080. {
  1081. const struct qstr *old = args->old_name;
  1082. const struct qstr *new = args->new_name;
  1083. encode_diropargs3(xdr, args->old_dir, old->name, old->len);
  1084. encode_diropargs3(xdr, args->new_dir, new->name, new->len);
  1085. }
  1086. /*
  1087. * 3.3.15 LINK3args
  1088. *
  1089. * struct LINK3args {
  1090. * nfs_fh3 file;
  1091. * diropargs3 link;
  1092. * };
  1093. */
  1094. static void nfs3_xdr_enc_link3args(struct rpc_rqst *req,
  1095. struct xdr_stream *xdr,
  1096. const struct nfs3_linkargs *args)
  1097. {
  1098. encode_nfs_fh3(xdr, args->fromfh);
  1099. encode_diropargs3(xdr, args->tofh, args->toname, args->tolen);
  1100. }
  1101. /*
  1102. * 3.3.16 READDIR3args
  1103. *
  1104. * struct READDIR3args {
  1105. * nfs_fh3 dir;
  1106. * cookie3 cookie;
  1107. * cookieverf3 cookieverf;
  1108. * count3 count;
  1109. * };
  1110. */
  1111. static void encode_readdir3args(struct xdr_stream *xdr,
  1112. const struct nfs3_readdirargs *args)
  1113. {
  1114. __be32 *p;
  1115. encode_nfs_fh3(xdr, args->fh);
  1116. p = xdr_reserve_space(xdr, 8 + NFS3_COOKIEVERFSIZE + 4);
  1117. p = xdr_encode_cookie3(p, args->cookie);
  1118. p = xdr_encode_cookieverf3(p, args->verf);
  1119. *p = cpu_to_be32(args->count);
  1120. }
  1121. static void nfs3_xdr_enc_readdir3args(struct rpc_rqst *req,
  1122. struct xdr_stream *xdr,
  1123. const struct nfs3_readdirargs *args)
  1124. {
  1125. encode_readdir3args(xdr, args);
  1126. prepare_reply_buffer(req, args->pages, 0,
  1127. args->count, NFS3_readdirres_sz);
  1128. }
  1129. /*
  1130. * 3.3.17 READDIRPLUS3args
  1131. *
  1132. * struct READDIRPLUS3args {
  1133. * nfs_fh3 dir;
  1134. * cookie3 cookie;
  1135. * cookieverf3 cookieverf;
  1136. * count3 dircount;
  1137. * count3 maxcount;
  1138. * };
  1139. */
  1140. static void encode_readdirplus3args(struct xdr_stream *xdr,
  1141. const struct nfs3_readdirargs *args)
  1142. {
  1143. __be32 *p;
  1144. encode_nfs_fh3(xdr, args->fh);
  1145. p = xdr_reserve_space(xdr, 8 + NFS3_COOKIEVERFSIZE + 4 + 4);
  1146. p = xdr_encode_cookie3(p, args->cookie);
  1147. p = xdr_encode_cookieverf3(p, args->verf);
  1148. /*
  1149. * readdirplus: need dircount + buffer size.
  1150. * We just make sure we make dircount big enough
  1151. */
  1152. *p++ = cpu_to_be32(args->count >> 3);
  1153. *p = cpu_to_be32(args->count);
  1154. }
  1155. static void nfs3_xdr_enc_readdirplus3args(struct rpc_rqst *req,
  1156. struct xdr_stream *xdr,
  1157. const struct nfs3_readdirargs *args)
  1158. {
  1159. encode_readdirplus3args(xdr, args);
  1160. prepare_reply_buffer(req, args->pages, 0,
  1161. args->count, NFS3_readdirres_sz);
  1162. }
  1163. /*
  1164. * 3.3.21 COMMIT3args
  1165. *
  1166. * struct COMMIT3args {
  1167. * nfs_fh3 file;
  1168. * offset3 offset;
  1169. * count3 count;
  1170. * };
  1171. */
  1172. static void encode_commit3args(struct xdr_stream *xdr,
  1173. const struct nfs_commitargs *args)
  1174. {
  1175. __be32 *p;
  1176. encode_nfs_fh3(xdr, args->fh);
  1177. p = xdr_reserve_space(xdr, 8 + 4);
  1178. p = xdr_encode_hyper(p, args->offset);
  1179. *p = cpu_to_be32(args->count);
  1180. }
  1181. static void nfs3_xdr_enc_commit3args(struct rpc_rqst *req,
  1182. struct xdr_stream *xdr,
  1183. const struct nfs_commitargs *args)
  1184. {
  1185. encode_commit3args(xdr, args);
  1186. }
  1187. #ifdef CONFIG_NFS_V3_ACL
  1188. static void nfs3_xdr_enc_getacl3args(struct rpc_rqst *req,
  1189. struct xdr_stream *xdr,
  1190. const struct nfs3_getaclargs *args)
  1191. {
  1192. encode_nfs_fh3(xdr, args->fh);
  1193. encode_uint32(xdr, args->mask);
  1194. if (args->mask & (NFS_ACL | NFS_DFACL))
  1195. prepare_reply_buffer(req, args->pages, 0,
  1196. NFSACL_MAXPAGES << PAGE_SHIFT,
  1197. ACL3_getaclres_sz);
  1198. }
  1199. static void nfs3_xdr_enc_setacl3args(struct rpc_rqst *req,
  1200. struct xdr_stream *xdr,
  1201. const struct nfs3_setaclargs *args)
  1202. {
  1203. unsigned int base;
  1204. int error;
  1205. encode_nfs_fh3(xdr, NFS_FH(args->inode));
  1206. encode_uint32(xdr, args->mask);
  1207. base = req->rq_slen;
  1208. if (args->npages != 0)
  1209. xdr_write_pages(xdr, args->pages, 0, args->len);
  1210. else
  1211. xdr_reserve_space(xdr, args->len);
  1212. error = nfsacl_encode(xdr->buf, base, args->inode,
  1213. (args->mask & NFS_ACL) ?
  1214. args->acl_access : NULL, 1, 0);
  1215. /* FIXME: this is just broken */
  1216. BUG_ON(error < 0);
  1217. error = nfsacl_encode(xdr->buf, base + error, args->inode,
  1218. (args->mask & NFS_DFACL) ?
  1219. args->acl_default : NULL, 1,
  1220. NFS_ACL_DEFAULT);
  1221. BUG_ON(error < 0);
  1222. }
  1223. #endif /* CONFIG_NFS_V3_ACL */
  1224. /*
  1225. * NFSv3 XDR decode functions
  1226. *
  1227. * NFSv3 result types are defined in section 3.3 of RFC 1813:
  1228. * "NFS Version 3 Protocol Specification".
  1229. */
  1230. /*
  1231. * 3.3.1 GETATTR3res
  1232. *
  1233. * struct GETATTR3resok {
  1234. * fattr3 obj_attributes;
  1235. * };
  1236. *
  1237. * union GETATTR3res switch (nfsstat3 status) {
  1238. * case NFS3_OK:
  1239. * GETATTR3resok resok;
  1240. * default:
  1241. * void;
  1242. * };
  1243. */
  1244. static int nfs3_xdr_dec_getattr3res(struct rpc_rqst *req,
  1245. struct xdr_stream *xdr,
  1246. struct nfs_fattr *result)
  1247. {
  1248. enum nfs_stat status;
  1249. int error;
  1250. error = decode_nfsstat3(xdr, &status);
  1251. if (unlikely(error))
  1252. goto out;
  1253. if (status != NFS3_OK)
  1254. goto out_default;
  1255. error = decode_fattr3(xdr, result);
  1256. out:
  1257. return error;
  1258. out_default:
  1259. return nfs3_stat_to_errno(status);
  1260. }
  1261. /*
  1262. * 3.3.2 SETATTR3res
  1263. *
  1264. * struct SETATTR3resok {
  1265. * wcc_data obj_wcc;
  1266. * };
  1267. *
  1268. * struct SETATTR3resfail {
  1269. * wcc_data obj_wcc;
  1270. * };
  1271. *
  1272. * union SETATTR3res switch (nfsstat3 status) {
  1273. * case NFS3_OK:
  1274. * SETATTR3resok resok;
  1275. * default:
  1276. * SETATTR3resfail resfail;
  1277. * };
  1278. */
  1279. static int nfs3_xdr_dec_setattr3res(struct rpc_rqst *req,
  1280. struct xdr_stream *xdr,
  1281. struct nfs_fattr *result)
  1282. {
  1283. enum nfs_stat status;
  1284. int error;
  1285. error = decode_nfsstat3(xdr, &status);
  1286. if (unlikely(error))
  1287. goto out;
  1288. error = decode_wcc_data(xdr, result);
  1289. if (unlikely(error))
  1290. goto out;
  1291. if (status != NFS3_OK)
  1292. goto out_status;
  1293. out:
  1294. return error;
  1295. out_status:
  1296. return nfs3_stat_to_errno(status);
  1297. }
  1298. /*
  1299. * 3.3.3 LOOKUP3res
  1300. *
  1301. * struct LOOKUP3resok {
  1302. * nfs_fh3 object;
  1303. * post_op_attr obj_attributes;
  1304. * post_op_attr dir_attributes;
  1305. * };
  1306. *
  1307. * struct LOOKUP3resfail {
  1308. * post_op_attr dir_attributes;
  1309. * };
  1310. *
  1311. * union LOOKUP3res switch (nfsstat3 status) {
  1312. * case NFS3_OK:
  1313. * LOOKUP3resok resok;
  1314. * default:
  1315. * LOOKUP3resfail resfail;
  1316. * };
  1317. */
  1318. static int nfs3_xdr_dec_lookup3res(struct rpc_rqst *req,
  1319. struct xdr_stream *xdr,
  1320. struct nfs3_diropres *result)
  1321. {
  1322. enum nfs_stat status;
  1323. int error;
  1324. error = decode_nfsstat3(xdr, &status);
  1325. if (unlikely(error))
  1326. goto out;
  1327. if (status != NFS3_OK)
  1328. goto out_default;
  1329. error = decode_nfs_fh3(xdr, result->fh);
  1330. if (unlikely(error))
  1331. goto out;
  1332. error = decode_post_op_attr(xdr, result->fattr);
  1333. if (unlikely(error))
  1334. goto out;
  1335. error = decode_post_op_attr(xdr, result->dir_attr);
  1336. out:
  1337. return error;
  1338. out_default:
  1339. error = decode_post_op_attr(xdr, result->dir_attr);
  1340. if (unlikely(error))
  1341. goto out;
  1342. return nfs3_stat_to_errno(status);
  1343. }
  1344. /*
  1345. * 3.3.4 ACCESS3res
  1346. *
  1347. * struct ACCESS3resok {
  1348. * post_op_attr obj_attributes;
  1349. * uint32 access;
  1350. * };
  1351. *
  1352. * struct ACCESS3resfail {
  1353. * post_op_attr obj_attributes;
  1354. * };
  1355. *
  1356. * union ACCESS3res switch (nfsstat3 status) {
  1357. * case NFS3_OK:
  1358. * ACCESS3resok resok;
  1359. * default:
  1360. * ACCESS3resfail resfail;
  1361. * };
  1362. */
  1363. static int nfs3_xdr_dec_access3res(struct rpc_rqst *req,
  1364. struct xdr_stream *xdr,
  1365. struct nfs3_accessres *result)
  1366. {
  1367. enum nfs_stat status;
  1368. int error;
  1369. error = decode_nfsstat3(xdr, &status);
  1370. if (unlikely(error))
  1371. goto out;
  1372. error = decode_post_op_attr(xdr, result->fattr);
  1373. if (unlikely(error))
  1374. goto out;
  1375. if (status != NFS3_OK)
  1376. goto out_default;
  1377. error = decode_uint32(xdr, &result->access);
  1378. out:
  1379. return error;
  1380. out_default:
  1381. return nfs3_stat_to_errno(status);
  1382. }
  1383. /*
  1384. * 3.3.5 READLINK3res
  1385. *
  1386. * struct READLINK3resok {
  1387. * post_op_attr symlink_attributes;
  1388. * nfspath3 data;
  1389. * };
  1390. *
  1391. * struct READLINK3resfail {
  1392. * post_op_attr symlink_attributes;
  1393. * };
  1394. *
  1395. * union READLINK3res switch (nfsstat3 status) {
  1396. * case NFS3_OK:
  1397. * READLINK3resok resok;
  1398. * default:
  1399. * READLINK3resfail resfail;
  1400. * };
  1401. */
  1402. static int nfs3_xdr_dec_readlink3res(struct rpc_rqst *req,
  1403. struct xdr_stream *xdr,
  1404. struct nfs_fattr *result)
  1405. {
  1406. enum nfs_stat status;
  1407. int error;
  1408. error = decode_nfsstat3(xdr, &status);
  1409. if (unlikely(error))
  1410. goto out;
  1411. error = decode_post_op_attr(xdr, result);
  1412. if (unlikely(error))
  1413. goto out;
  1414. if (status != NFS3_OK)
  1415. goto out_default;
  1416. error = decode_nfspath3(xdr);
  1417. out:
  1418. return error;
  1419. out_default:
  1420. return nfs3_stat_to_errno(status);
  1421. }
  1422. /*
  1423. * 3.3.6 READ3res
  1424. *
  1425. * struct READ3resok {
  1426. * post_op_attr file_attributes;
  1427. * count3 count;
  1428. * bool eof;
  1429. * opaque data<>;
  1430. * };
  1431. *
  1432. * struct READ3resfail {
  1433. * post_op_attr file_attributes;
  1434. * };
  1435. *
  1436. * union READ3res switch (nfsstat3 status) {
  1437. * case NFS3_OK:
  1438. * READ3resok resok;
  1439. * default:
  1440. * READ3resfail resfail;
  1441. * };
  1442. */
  1443. static int decode_read3resok(struct xdr_stream *xdr,
  1444. struct nfs_pgio_res *result)
  1445. {
  1446. u32 eof, count, ocount, recvd;
  1447. __be32 *p;
  1448. p = xdr_inline_decode(xdr, 4 + 4 + 4);
  1449. if (unlikely(p == NULL))
  1450. goto out_overflow;
  1451. count = be32_to_cpup(p++);
  1452. eof = be32_to_cpup(p++);
  1453. ocount = be32_to_cpup(p++);
  1454. if (unlikely(ocount != count))
  1455. goto out_mismatch;
  1456. recvd = xdr_read_pages(xdr, count);
  1457. if (unlikely(count > recvd))
  1458. goto out_cheating;
  1459. out:
  1460. result->eof = eof;
  1461. result->count = count;
  1462. return count;
  1463. out_mismatch:
  1464. dprintk("NFS: READ count doesn't match length of opaque: "
  1465. "count %u != ocount %u\n", count, ocount);
  1466. return -EIO;
  1467. out_cheating:
  1468. dprintk("NFS: server cheating in read result: "
  1469. "count %u > recvd %u\n", count, recvd);
  1470. count = recvd;
  1471. eof = 0;
  1472. goto out;
  1473. out_overflow:
  1474. print_overflow_msg(__func__, xdr);
  1475. return -EIO;
  1476. }
  1477. static int nfs3_xdr_dec_read3res(struct rpc_rqst *req, struct xdr_stream *xdr,
  1478. struct nfs_pgio_res *result)
  1479. {
  1480. enum nfs_stat status;
  1481. int error;
  1482. error = decode_nfsstat3(xdr, &status);
  1483. if (unlikely(error))
  1484. goto out;
  1485. error = decode_post_op_attr(xdr, result->fattr);
  1486. if (unlikely(error))
  1487. goto out;
  1488. result->op_status = status;
  1489. if (status != NFS3_OK)
  1490. goto out_status;
  1491. error = decode_read3resok(xdr, result);
  1492. out:
  1493. return error;
  1494. out_status:
  1495. return nfs3_stat_to_errno(status);
  1496. }
  1497. /*
  1498. * 3.3.7 WRITE3res
  1499. *
  1500. * enum stable_how {
  1501. * UNSTABLE = 0,
  1502. * DATA_SYNC = 1,
  1503. * FILE_SYNC = 2
  1504. * };
  1505. *
  1506. * struct WRITE3resok {
  1507. * wcc_data file_wcc;
  1508. * count3 count;
  1509. * stable_how committed;
  1510. * writeverf3 verf;
  1511. * };
  1512. *
  1513. * struct WRITE3resfail {
  1514. * wcc_data file_wcc;
  1515. * };
  1516. *
  1517. * union WRITE3res switch (nfsstat3 status) {
  1518. * case NFS3_OK:
  1519. * WRITE3resok resok;
  1520. * default:
  1521. * WRITE3resfail resfail;
  1522. * };
  1523. */
  1524. static int decode_write3resok(struct xdr_stream *xdr,
  1525. struct nfs_pgio_res *result)
  1526. {
  1527. __be32 *p;
  1528. p = xdr_inline_decode(xdr, 4 + 4);
  1529. if (unlikely(p == NULL))
  1530. goto out_overflow;
  1531. result->count = be32_to_cpup(p++);
  1532. result->verf->committed = be32_to_cpup(p++);
  1533. if (unlikely(result->verf->committed > NFS_FILE_SYNC))
  1534. goto out_badvalue;
  1535. if (decode_writeverf3(xdr, &result->verf->verifier))
  1536. goto out_eio;
  1537. return result->count;
  1538. out_badvalue:
  1539. dprintk("NFS: bad stable_how value: %u\n", result->verf->committed);
  1540. return -EIO;
  1541. out_overflow:
  1542. print_overflow_msg(__func__, xdr);
  1543. out_eio:
  1544. return -EIO;
  1545. }
  1546. static int nfs3_xdr_dec_write3res(struct rpc_rqst *req, struct xdr_stream *xdr,
  1547. struct nfs_pgio_res *result)
  1548. {
  1549. enum nfs_stat status;
  1550. int error;
  1551. error = decode_nfsstat3(xdr, &status);
  1552. if (unlikely(error))
  1553. goto out;
  1554. error = decode_wcc_data(xdr, result->fattr);
  1555. if (unlikely(error))
  1556. goto out;
  1557. result->op_status = status;
  1558. if (status != NFS3_OK)
  1559. goto out_status;
  1560. error = decode_write3resok(xdr, result);
  1561. out:
  1562. return error;
  1563. out_status:
  1564. return nfs3_stat_to_errno(status);
  1565. }
  1566. /*
  1567. * 3.3.8 CREATE3res
  1568. *
  1569. * struct CREATE3resok {
  1570. * post_op_fh3 obj;
  1571. * post_op_attr obj_attributes;
  1572. * wcc_data dir_wcc;
  1573. * };
  1574. *
  1575. * struct CREATE3resfail {
  1576. * wcc_data dir_wcc;
  1577. * };
  1578. *
  1579. * union CREATE3res switch (nfsstat3 status) {
  1580. * case NFS3_OK:
  1581. * CREATE3resok resok;
  1582. * default:
  1583. * CREATE3resfail resfail;
  1584. * };
  1585. */
  1586. static int decode_create3resok(struct xdr_stream *xdr,
  1587. struct nfs3_diropres *result)
  1588. {
  1589. int error;
  1590. error = decode_post_op_fh3(xdr, result->fh);
  1591. if (unlikely(error))
  1592. goto out;
  1593. error = decode_post_op_attr(xdr, result->fattr);
  1594. if (unlikely(error))
  1595. goto out;
  1596. /* The server isn't required to return a file handle.
  1597. * If it didn't, force the client to perform a LOOKUP
  1598. * to determine the correct file handle and attribute
  1599. * values for the new object. */
  1600. if (result->fh->size == 0)
  1601. result->fattr->valid = 0;
  1602. error = decode_wcc_data(xdr, result->dir_attr);
  1603. out:
  1604. return error;
  1605. }
  1606. static int nfs3_xdr_dec_create3res(struct rpc_rqst *req,
  1607. struct xdr_stream *xdr,
  1608. struct nfs3_diropres *result)
  1609. {
  1610. enum nfs_stat status;
  1611. int error;
  1612. error = decode_nfsstat3(xdr, &status);
  1613. if (unlikely(error))
  1614. goto out;
  1615. if (status != NFS3_OK)
  1616. goto out_default;
  1617. error = decode_create3resok(xdr, result);
  1618. out:
  1619. return error;
  1620. out_default:
  1621. error = decode_wcc_data(xdr, result->dir_attr);
  1622. if (unlikely(error))
  1623. goto out;
  1624. return nfs3_stat_to_errno(status);
  1625. }
  1626. /*
  1627. * 3.3.12 REMOVE3res
  1628. *
  1629. * struct REMOVE3resok {
  1630. * wcc_data dir_wcc;
  1631. * };
  1632. *
  1633. * struct REMOVE3resfail {
  1634. * wcc_data dir_wcc;
  1635. * };
  1636. *
  1637. * union REMOVE3res switch (nfsstat3 status) {
  1638. * case NFS3_OK:
  1639. * REMOVE3resok resok;
  1640. * default:
  1641. * REMOVE3resfail resfail;
  1642. * };
  1643. */
  1644. static int nfs3_xdr_dec_remove3res(struct rpc_rqst *req,
  1645. struct xdr_stream *xdr,
  1646. struct nfs_removeres *result)
  1647. {
  1648. enum nfs_stat status;
  1649. int error;
  1650. error = decode_nfsstat3(xdr, &status);
  1651. if (unlikely(error))
  1652. goto out;
  1653. error = decode_wcc_data(xdr, result->dir_attr);
  1654. if (unlikely(error))
  1655. goto out;
  1656. if (status != NFS3_OK)
  1657. goto out_status;
  1658. out:
  1659. return error;
  1660. out_status:
  1661. return nfs3_stat_to_errno(status);
  1662. }
  1663. /*
  1664. * 3.3.14 RENAME3res
  1665. *
  1666. * struct RENAME3resok {
  1667. * wcc_data fromdir_wcc;
  1668. * wcc_data todir_wcc;
  1669. * };
  1670. *
  1671. * struct RENAME3resfail {
  1672. * wcc_data fromdir_wcc;
  1673. * wcc_data todir_wcc;
  1674. * };
  1675. *
  1676. * union RENAME3res switch (nfsstat3 status) {
  1677. * case NFS3_OK:
  1678. * RENAME3resok resok;
  1679. * default:
  1680. * RENAME3resfail resfail;
  1681. * };
  1682. */
  1683. static int nfs3_xdr_dec_rename3res(struct rpc_rqst *req,
  1684. struct xdr_stream *xdr,
  1685. struct nfs_renameres *result)
  1686. {
  1687. enum nfs_stat status;
  1688. int error;
  1689. error = decode_nfsstat3(xdr, &status);
  1690. if (unlikely(error))
  1691. goto out;
  1692. error = decode_wcc_data(xdr, result->old_fattr);
  1693. if (unlikely(error))
  1694. goto out;
  1695. error = decode_wcc_data(xdr, result->new_fattr);
  1696. if (unlikely(error))
  1697. goto out;
  1698. if (status != NFS3_OK)
  1699. goto out_status;
  1700. out:
  1701. return error;
  1702. out_status:
  1703. return nfs3_stat_to_errno(status);
  1704. }
  1705. /*
  1706. * 3.3.15 LINK3res
  1707. *
  1708. * struct LINK3resok {
  1709. * post_op_attr file_attributes;
  1710. * wcc_data linkdir_wcc;
  1711. * };
  1712. *
  1713. * struct LINK3resfail {
  1714. * post_op_attr file_attributes;
  1715. * wcc_data linkdir_wcc;
  1716. * };
  1717. *
  1718. * union LINK3res switch (nfsstat3 status) {
  1719. * case NFS3_OK:
  1720. * LINK3resok resok;
  1721. * default:
  1722. * LINK3resfail resfail;
  1723. * };
  1724. */
  1725. static int nfs3_xdr_dec_link3res(struct rpc_rqst *req, struct xdr_stream *xdr,
  1726. struct nfs3_linkres *result)
  1727. {
  1728. enum nfs_stat status;
  1729. int error;
  1730. error = decode_nfsstat3(xdr, &status);
  1731. if (unlikely(error))
  1732. goto out;
  1733. error = decode_post_op_attr(xdr, result->fattr);
  1734. if (unlikely(error))
  1735. goto out;
  1736. error = decode_wcc_data(xdr, result->dir_attr);
  1737. if (unlikely(error))
  1738. goto out;
  1739. if (status != NFS3_OK)
  1740. goto out_status;
  1741. out:
  1742. return error;
  1743. out_status:
  1744. return nfs3_stat_to_errno(status);
  1745. }
  1746. /**
  1747. * nfs3_decode_dirent - Decode a single NFSv3 directory entry stored in
  1748. * the local page cache
  1749. * @xdr: XDR stream where entry resides
  1750. * @entry: buffer to fill in with entry data
  1751. * @plus: boolean indicating whether this should be a readdirplus entry
  1752. *
  1753. * Returns zero if successful, otherwise a negative errno value is
  1754. * returned.
  1755. *
  1756. * This function is not invoked during READDIR reply decoding, but
  1757. * rather whenever an application invokes the getdents(2) system call
  1758. * on a directory already in our cache.
  1759. *
  1760. * 3.3.16 entry3
  1761. *
  1762. * struct entry3 {
  1763. * fileid3 fileid;
  1764. * filename3 name;
  1765. * cookie3 cookie;
  1766. * fhandle3 filehandle;
  1767. * post_op_attr3 attributes;
  1768. * entry3 *nextentry;
  1769. * };
  1770. *
  1771. * 3.3.17 entryplus3
  1772. * struct entryplus3 {
  1773. * fileid3 fileid;
  1774. * filename3 name;
  1775. * cookie3 cookie;
  1776. * post_op_attr name_attributes;
  1777. * post_op_fh3 name_handle;
  1778. * entryplus3 *nextentry;
  1779. * };
  1780. */
  1781. int nfs3_decode_dirent(struct xdr_stream *xdr, struct nfs_entry *entry,
  1782. int plus)
  1783. {
  1784. struct nfs_entry old = *entry;
  1785. __be32 *p;
  1786. int error;
  1787. p = xdr_inline_decode(xdr, 4);
  1788. if (unlikely(p == NULL))
  1789. goto out_overflow;
  1790. if (*p == xdr_zero) {
  1791. p = xdr_inline_decode(xdr, 4);
  1792. if (unlikely(p == NULL))
  1793. goto out_overflow;
  1794. if (*p == xdr_zero)
  1795. return -EAGAIN;
  1796. entry->eof = 1;
  1797. return -EBADCOOKIE;
  1798. }
  1799. error = decode_fileid3(xdr, &entry->ino);
  1800. if (unlikely(error))
  1801. return error;
  1802. error = decode_inline_filename3(xdr, &entry->name, &entry->len);
  1803. if (unlikely(error))
  1804. return error;
  1805. entry->prev_cookie = entry->cookie;
  1806. error = decode_cookie3(xdr, &entry->cookie);
  1807. if (unlikely(error))
  1808. return error;
  1809. entry->d_type = DT_UNKNOWN;
  1810. if (plus) {
  1811. entry->fattr->valid = 0;
  1812. error = decode_post_op_attr(xdr, entry->fattr);
  1813. if (unlikely(error))
  1814. return error;
  1815. if (entry->fattr->valid & NFS_ATTR_FATTR_V3)
  1816. entry->d_type = nfs_umode_to_dtype(entry->fattr->mode);
  1817. if (entry->fattr->fileid != entry->ino) {
  1818. entry->fattr->mounted_on_fileid = entry->ino;
  1819. entry->fattr->valid |= NFS_ATTR_FATTR_MOUNTED_ON_FILEID;
  1820. }
  1821. /* In fact, a post_op_fh3: */
  1822. p = xdr_inline_decode(xdr, 4);
  1823. if (unlikely(p == NULL))
  1824. goto out_overflow;
  1825. if (*p != xdr_zero) {
  1826. error = decode_nfs_fh3(xdr, entry->fh);
  1827. if (unlikely(error)) {
  1828. if (error == -E2BIG)
  1829. goto out_truncated;
  1830. return error;
  1831. }
  1832. } else
  1833. zero_nfs_fh3(entry->fh);
  1834. }
  1835. return 0;
  1836. out_overflow:
  1837. print_overflow_msg(__func__, xdr);
  1838. return -EAGAIN;
  1839. out_truncated:
  1840. dprintk("NFS: directory entry contains invalid file handle\n");
  1841. *entry = old;
  1842. return -EAGAIN;
  1843. }
  1844. /*
  1845. * 3.3.16 READDIR3res
  1846. *
  1847. * struct dirlist3 {
  1848. * entry3 *entries;
  1849. * bool eof;
  1850. * };
  1851. *
  1852. * struct READDIR3resok {
  1853. * post_op_attr dir_attributes;
  1854. * cookieverf3 cookieverf;
  1855. * dirlist3 reply;
  1856. * };
  1857. *
  1858. * struct READDIR3resfail {
  1859. * post_op_attr dir_attributes;
  1860. * };
  1861. *
  1862. * union READDIR3res switch (nfsstat3 status) {
  1863. * case NFS3_OK:
  1864. * READDIR3resok resok;
  1865. * default:
  1866. * READDIR3resfail resfail;
  1867. * };
  1868. *
  1869. * Read the directory contents into the page cache, but otherwise
  1870. * don't touch them. The actual decoding is done by nfs3_decode_entry()
  1871. * during subsequent nfs_readdir() calls.
  1872. */
  1873. static int decode_dirlist3(struct xdr_stream *xdr)
  1874. {
  1875. return xdr_read_pages(xdr, xdr->buf->page_len);
  1876. }
  1877. static int decode_readdir3resok(struct xdr_stream *xdr,
  1878. struct nfs3_readdirres *result)
  1879. {
  1880. int error;
  1881. error = decode_post_op_attr(xdr, result->dir_attr);
  1882. if (unlikely(error))
  1883. goto out;
  1884. /* XXX: do we need to check if result->verf != NULL ? */
  1885. error = decode_cookieverf3(xdr, result->verf);
  1886. if (unlikely(error))
  1887. goto out;
  1888. error = decode_dirlist3(xdr);
  1889. out:
  1890. return error;
  1891. }
  1892. static int nfs3_xdr_dec_readdir3res(struct rpc_rqst *req,
  1893. struct xdr_stream *xdr,
  1894. struct nfs3_readdirres *result)
  1895. {
  1896. enum nfs_stat status;
  1897. int error;
  1898. error = decode_nfsstat3(xdr, &status);
  1899. if (unlikely(error))
  1900. goto out;
  1901. if (status != NFS3_OK)
  1902. goto out_default;
  1903. error = decode_readdir3resok(xdr, result);
  1904. out:
  1905. return error;
  1906. out_default:
  1907. error = decode_post_op_attr(xdr, result->dir_attr);
  1908. if (unlikely(error))
  1909. goto out;
  1910. return nfs3_stat_to_errno(status);
  1911. }
  1912. /*
  1913. * 3.3.18 FSSTAT3res
  1914. *
  1915. * struct FSSTAT3resok {
  1916. * post_op_attr obj_attributes;
  1917. * size3 tbytes;
  1918. * size3 fbytes;
  1919. * size3 abytes;
  1920. * size3 tfiles;
  1921. * size3 ffiles;
  1922. * size3 afiles;
  1923. * uint32 invarsec;
  1924. * };
  1925. *
  1926. * struct FSSTAT3resfail {
  1927. * post_op_attr obj_attributes;
  1928. * };
  1929. *
  1930. * union FSSTAT3res switch (nfsstat3 status) {
  1931. * case NFS3_OK:
  1932. * FSSTAT3resok resok;
  1933. * default:
  1934. * FSSTAT3resfail resfail;
  1935. * };
  1936. */
  1937. static int decode_fsstat3resok(struct xdr_stream *xdr,
  1938. struct nfs_fsstat *result)
  1939. {
  1940. __be32 *p;
  1941. p = xdr_inline_decode(xdr, 8 * 6 + 4);
  1942. if (unlikely(p == NULL))
  1943. goto out_overflow;
  1944. p = xdr_decode_size3(p, &result->tbytes);
  1945. p = xdr_decode_size3(p, &result->fbytes);
  1946. p = xdr_decode_size3(p, &result->abytes);
  1947. p = xdr_decode_size3(p, &result->tfiles);
  1948. p = xdr_decode_size3(p, &result->ffiles);
  1949. xdr_decode_size3(p, &result->afiles);
  1950. /* ignore invarsec */
  1951. return 0;
  1952. out_overflow:
  1953. print_overflow_msg(__func__, xdr);
  1954. return -EIO;
  1955. }
  1956. static int nfs3_xdr_dec_fsstat3res(struct rpc_rqst *req,
  1957. struct xdr_stream *xdr,
  1958. struct nfs_fsstat *result)
  1959. {
  1960. enum nfs_stat status;
  1961. int error;
  1962. error = decode_nfsstat3(xdr, &status);
  1963. if (unlikely(error))
  1964. goto out;
  1965. error = decode_post_op_attr(xdr, result->fattr);
  1966. if (unlikely(error))
  1967. goto out;
  1968. if (status != NFS3_OK)
  1969. goto out_status;
  1970. error = decode_fsstat3resok(xdr, result);
  1971. out:
  1972. return error;
  1973. out_status:
  1974. return nfs3_stat_to_errno(status);
  1975. }
  1976. /*
  1977. * 3.3.19 FSINFO3res
  1978. *
  1979. * struct FSINFO3resok {
  1980. * post_op_attr obj_attributes;
  1981. * uint32 rtmax;
  1982. * uint32 rtpref;
  1983. * uint32 rtmult;
  1984. * uint32 wtmax;
  1985. * uint32 wtpref;
  1986. * uint32 wtmult;
  1987. * uint32 dtpref;
  1988. * size3 maxfilesize;
  1989. * nfstime3 time_delta;
  1990. * uint32 properties;
  1991. * };
  1992. *
  1993. * struct FSINFO3resfail {
  1994. * post_op_attr obj_attributes;
  1995. * };
  1996. *
  1997. * union FSINFO3res switch (nfsstat3 status) {
  1998. * case NFS3_OK:
  1999. * FSINFO3resok resok;
  2000. * default:
  2001. * FSINFO3resfail resfail;
  2002. * };
  2003. */
  2004. static int decode_fsinfo3resok(struct xdr_stream *xdr,
  2005. struct nfs_fsinfo *result)
  2006. {
  2007. __be32 *p;
  2008. p = xdr_inline_decode(xdr, 4 * 7 + 8 + 8 + 4);
  2009. if (unlikely(p == NULL))
  2010. goto out_overflow;
  2011. result->rtmax = be32_to_cpup(p++);
  2012. result->rtpref = be32_to_cpup(p++);
  2013. result->rtmult = be32_to_cpup(p++);
  2014. result->wtmax = be32_to_cpup(p++);
  2015. result->wtpref = be32_to_cpup(p++);
  2016. result->wtmult = be32_to_cpup(p++);
  2017. result->dtpref = be32_to_cpup(p++);
  2018. p = xdr_decode_size3(p, &result->maxfilesize);
  2019. xdr_decode_nfstime3(p, &result->time_delta);
  2020. /* ignore properties */
  2021. result->lease_time = 0;
  2022. return 0;
  2023. out_overflow:
  2024. print_overflow_msg(__func__, xdr);
  2025. return -EIO;
  2026. }
  2027. static int nfs3_xdr_dec_fsinfo3res(struct rpc_rqst *req,
  2028. struct xdr_stream *xdr,
  2029. struct nfs_fsinfo *result)
  2030. {
  2031. enum nfs_stat status;
  2032. int error;
  2033. error = decode_nfsstat3(xdr, &status);
  2034. if (unlikely(error))
  2035. goto out;
  2036. error = decode_post_op_attr(xdr, result->fattr);
  2037. if (unlikely(error))
  2038. goto out;
  2039. if (status != NFS3_OK)
  2040. goto out_status;
  2041. error = decode_fsinfo3resok(xdr, result);
  2042. out:
  2043. return error;
  2044. out_status:
  2045. return nfs3_stat_to_errno(status);
  2046. }
  2047. /*
  2048. * 3.3.20 PATHCONF3res
  2049. *
  2050. * struct PATHCONF3resok {
  2051. * post_op_attr obj_attributes;
  2052. * uint32 linkmax;
  2053. * uint32 name_max;
  2054. * bool no_trunc;
  2055. * bool chown_restricted;
  2056. * bool case_insensitive;
  2057. * bool case_preserving;
  2058. * };
  2059. *
  2060. * struct PATHCONF3resfail {
  2061. * post_op_attr obj_attributes;
  2062. * };
  2063. *
  2064. * union PATHCONF3res switch (nfsstat3 status) {
  2065. * case NFS3_OK:
  2066. * PATHCONF3resok resok;
  2067. * default:
  2068. * PATHCONF3resfail resfail;
  2069. * };
  2070. */
  2071. static int decode_pathconf3resok(struct xdr_stream *xdr,
  2072. struct nfs_pathconf *result)
  2073. {
  2074. __be32 *p;
  2075. p = xdr_inline_decode(xdr, 4 * 6);
  2076. if (unlikely(p == NULL))
  2077. goto out_overflow;
  2078. result->max_link = be32_to_cpup(p++);
  2079. result->max_namelen = be32_to_cpup(p);
  2080. /* ignore remaining fields */
  2081. return 0;
  2082. out_overflow:
  2083. print_overflow_msg(__func__, xdr);
  2084. return -EIO;
  2085. }
  2086. static int nfs3_xdr_dec_pathconf3res(struct rpc_rqst *req,
  2087. struct xdr_stream *xdr,
  2088. struct nfs_pathconf *result)
  2089. {
  2090. enum nfs_stat status;
  2091. int error;
  2092. error = decode_nfsstat3(xdr, &status);
  2093. if (unlikely(error))
  2094. goto out;
  2095. error = decode_post_op_attr(xdr, result->fattr);
  2096. if (unlikely(error))
  2097. goto out;
  2098. if (status != NFS3_OK)
  2099. goto out_status;
  2100. error = decode_pathconf3resok(xdr, result);
  2101. out:
  2102. return error;
  2103. out_status:
  2104. return nfs3_stat_to_errno(status);
  2105. }
  2106. /*
  2107. * 3.3.21 COMMIT3res
  2108. *
  2109. * struct COMMIT3resok {
  2110. * wcc_data file_wcc;
  2111. * writeverf3 verf;
  2112. * };
  2113. *
  2114. * struct COMMIT3resfail {
  2115. * wcc_data file_wcc;
  2116. * };
  2117. *
  2118. * union COMMIT3res switch (nfsstat3 status) {
  2119. * case NFS3_OK:
  2120. * COMMIT3resok resok;
  2121. * default:
  2122. * COMMIT3resfail resfail;
  2123. * };
  2124. */
  2125. static int nfs3_xdr_dec_commit3res(struct rpc_rqst *req,
  2126. struct xdr_stream *xdr,
  2127. struct nfs_commitres *result)
  2128. {
  2129. enum nfs_stat status;
  2130. int error;
  2131. error = decode_nfsstat3(xdr, &status);
  2132. if (unlikely(error))
  2133. goto out;
  2134. error = decode_wcc_data(xdr, result->fattr);
  2135. if (unlikely(error))
  2136. goto out;
  2137. result->op_status = status;
  2138. if (status != NFS3_OK)
  2139. goto out_status;
  2140. error = decode_writeverf3(xdr, &result->verf->verifier);
  2141. out:
  2142. return error;
  2143. out_status:
  2144. return nfs3_stat_to_errno(status);
  2145. }
  2146. #ifdef CONFIG_NFS_V3_ACL
  2147. static inline int decode_getacl3resok(struct xdr_stream *xdr,
  2148. struct nfs3_getaclres *result)
  2149. {
  2150. struct posix_acl **acl;
  2151. unsigned int *aclcnt;
  2152. size_t hdrlen;
  2153. int error;
  2154. error = decode_post_op_attr(xdr, result->fattr);
  2155. if (unlikely(error))
  2156. goto out;
  2157. error = decode_uint32(xdr, &result->mask);
  2158. if (unlikely(error))
  2159. goto out;
  2160. error = -EINVAL;
  2161. if (result->mask & ~(NFS_ACL|NFS_ACLCNT|NFS_DFACL|NFS_DFACLCNT))
  2162. goto out;
  2163. hdrlen = xdr_stream_pos(xdr);
  2164. acl = NULL;
  2165. if (result->mask & NFS_ACL)
  2166. acl = &result->acl_access;
  2167. aclcnt = NULL;
  2168. if (result->mask & NFS_ACLCNT)
  2169. aclcnt = &result->acl_access_count;
  2170. error = nfsacl_decode(xdr->buf, hdrlen, aclcnt, acl);
  2171. if (unlikely(error <= 0))
  2172. goto out;
  2173. acl = NULL;
  2174. if (result->mask & NFS_DFACL)
  2175. acl = &result->acl_default;
  2176. aclcnt = NULL;
  2177. if (result->mask & NFS_DFACLCNT)
  2178. aclcnt = &result->acl_default_count;
  2179. error = nfsacl_decode(xdr->buf, hdrlen + error, aclcnt, acl);
  2180. if (unlikely(error <= 0))
  2181. return error;
  2182. error = 0;
  2183. out:
  2184. return error;
  2185. }
  2186. static int nfs3_xdr_dec_getacl3res(struct rpc_rqst *req,
  2187. struct xdr_stream *xdr,
  2188. struct nfs3_getaclres *result)
  2189. {
  2190. enum nfs_stat status;
  2191. int error;
  2192. error = decode_nfsstat3(xdr, &status);
  2193. if (unlikely(error))
  2194. goto out;
  2195. if (status != NFS3_OK)
  2196. goto out_default;
  2197. error = decode_getacl3resok(xdr, result);
  2198. out:
  2199. return error;
  2200. out_default:
  2201. return nfs3_stat_to_errno(status);
  2202. }
  2203. static int nfs3_xdr_dec_setacl3res(struct rpc_rqst *req,
  2204. struct xdr_stream *xdr,
  2205. struct nfs_fattr *result)
  2206. {
  2207. enum nfs_stat status;
  2208. int error;
  2209. error = decode_nfsstat3(xdr, &status);
  2210. if (unlikely(error))
  2211. goto out;
  2212. if (status != NFS3_OK)
  2213. goto out_default;
  2214. error = decode_post_op_attr(xdr, result);
  2215. out:
  2216. return error;
  2217. out_default:
  2218. return nfs3_stat_to_errno(status);
  2219. }
  2220. #endif /* CONFIG_NFS_V3_ACL */
  2221. /*
  2222. * We need to translate between nfs status return values and
  2223. * the local errno values which may not be the same.
  2224. */
  2225. static const struct {
  2226. int stat;
  2227. int errno;
  2228. } nfs_errtbl[] = {
  2229. { NFS_OK, 0 },
  2230. { NFSERR_PERM, -EPERM },
  2231. { NFSERR_NOENT, -ENOENT },
  2232. { NFSERR_IO, -errno_NFSERR_IO},
  2233. { NFSERR_NXIO, -ENXIO },
  2234. /* { NFSERR_EAGAIN, -EAGAIN }, */
  2235. { NFSERR_ACCES, -EACCES },
  2236. { NFSERR_EXIST, -EEXIST },
  2237. { NFSERR_XDEV, -EXDEV },
  2238. { NFSERR_NODEV, -ENODEV },
  2239. { NFSERR_NOTDIR, -ENOTDIR },
  2240. { NFSERR_ISDIR, -EISDIR },
  2241. { NFSERR_INVAL, -EINVAL },
  2242. { NFSERR_FBIG, -EFBIG },
  2243. { NFSERR_NOSPC, -ENOSPC },
  2244. { NFSERR_ROFS, -EROFS },
  2245. { NFSERR_MLINK, -EMLINK },
  2246. { NFSERR_NAMETOOLONG, -ENAMETOOLONG },
  2247. { NFSERR_NOTEMPTY, -ENOTEMPTY },
  2248. { NFSERR_DQUOT, -EDQUOT },
  2249. { NFSERR_STALE, -ESTALE },
  2250. { NFSERR_REMOTE, -EREMOTE },
  2251. #ifdef EWFLUSH
  2252. { NFSERR_WFLUSH, -EWFLUSH },
  2253. #endif
  2254. { NFSERR_BADHANDLE, -EBADHANDLE },
  2255. { NFSERR_NOT_SYNC, -ENOTSYNC },
  2256. { NFSERR_BAD_COOKIE, -EBADCOOKIE },
  2257. { NFSERR_NOTSUPP, -ENOTSUPP },
  2258. { NFSERR_TOOSMALL, -ETOOSMALL },
  2259. { NFSERR_SERVERFAULT, -EREMOTEIO },
  2260. { NFSERR_BADTYPE, -EBADTYPE },
  2261. { NFSERR_JUKEBOX, -EJUKEBOX },
  2262. { -1, -EIO }
  2263. };
  2264. /**
  2265. * nfs3_stat_to_errno - convert an NFS status code to a local errno
  2266. * @status: NFS status code to convert
  2267. *
  2268. * Returns a local errno value, or -EIO if the NFS status code is
  2269. * not recognized. This function is used jointly by NFSv2 and NFSv3.
  2270. */
  2271. static int nfs3_stat_to_errno(enum nfs_stat status)
  2272. {
  2273. int i;
  2274. for (i = 0; nfs_errtbl[i].stat != -1; i++) {
  2275. if (nfs_errtbl[i].stat == (int)status)
  2276. return nfs_errtbl[i].errno;
  2277. }
  2278. dprintk("NFS: Unrecognized nfs status value: %u\n", status);
  2279. return nfs_errtbl[i].errno;
  2280. }
  2281. #define PROC(proc, argtype, restype, timer) \
  2282. [NFS3PROC_##proc] = { \
  2283. .p_proc = NFS3PROC_##proc, \
  2284. .p_encode = (kxdreproc_t)nfs3_xdr_enc_##argtype##3args, \
  2285. .p_decode = (kxdrdproc_t)nfs3_xdr_dec_##restype##3res, \
  2286. .p_arglen = NFS3_##argtype##args_sz, \
  2287. .p_replen = NFS3_##restype##res_sz, \
  2288. .p_timer = timer, \
  2289. .p_statidx = NFS3PROC_##proc, \
  2290. .p_name = #proc, \
  2291. }
  2292. struct rpc_procinfo nfs3_procedures[] = {
  2293. PROC(GETATTR, getattr, getattr, 1),
  2294. PROC(SETATTR, setattr, setattr, 0),
  2295. PROC(LOOKUP, lookup, lookup, 2),
  2296. PROC(ACCESS, access, access, 1),
  2297. PROC(READLINK, readlink, readlink, 3),
  2298. PROC(READ, read, read, 3),
  2299. PROC(WRITE, write, write, 4),
  2300. PROC(CREATE, create, create, 0),
  2301. PROC(MKDIR, mkdir, create, 0),
  2302. PROC(SYMLINK, symlink, create, 0),
  2303. PROC(MKNOD, mknod, create, 0),
  2304. PROC(REMOVE, remove, remove, 0),
  2305. PROC(RMDIR, lookup, setattr, 0),
  2306. PROC(RENAME, rename, rename, 0),
  2307. PROC(LINK, link, link, 0),
  2308. PROC(READDIR, readdir, readdir, 3),
  2309. PROC(READDIRPLUS, readdirplus, readdir, 3),
  2310. PROC(FSSTAT, getattr, fsstat, 0),
  2311. PROC(FSINFO, getattr, fsinfo, 0),
  2312. PROC(PATHCONF, getattr, pathconf, 0),
  2313. PROC(COMMIT, commit, commit, 5),
  2314. };
  2315. const struct rpc_version nfs_version3 = {
  2316. .number = 3,
  2317. .nrprocs = ARRAY_SIZE(nfs3_procedures),
  2318. .procs = nfs3_procedures
  2319. };
  2320. #ifdef CONFIG_NFS_V3_ACL
  2321. static struct rpc_procinfo nfs3_acl_procedures[] = {
  2322. [ACLPROC3_GETACL] = {
  2323. .p_proc = ACLPROC3_GETACL,
  2324. .p_encode = (kxdreproc_t)nfs3_xdr_enc_getacl3args,
  2325. .p_decode = (kxdrdproc_t)nfs3_xdr_dec_getacl3res,
  2326. .p_arglen = ACL3_getaclargs_sz,
  2327. .p_replen = ACL3_getaclres_sz,
  2328. .p_timer = 1,
  2329. .p_name = "GETACL",
  2330. },
  2331. [ACLPROC3_SETACL] = {
  2332. .p_proc = ACLPROC3_SETACL,
  2333. .p_encode = (kxdreproc_t)nfs3_xdr_enc_setacl3args,
  2334. .p_decode = (kxdrdproc_t)nfs3_xdr_dec_setacl3res,
  2335. .p_arglen = ACL3_setaclargs_sz,
  2336. .p_replen = ACL3_setaclres_sz,
  2337. .p_timer = 0,
  2338. .p_name = "SETACL",
  2339. },
  2340. };
  2341. const struct rpc_version nfsacl_version3 = {
  2342. .number = 3,
  2343. .nrprocs = sizeof(nfs3_acl_procedures)/
  2344. sizeof(nfs3_acl_procedures[0]),
  2345. .procs = nfs3_acl_procedures,
  2346. };
  2347. #endif /* CONFIG_NFS_V3_ACL */