nfs4proc.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562
  1. /*
  2. * Server-side procedures for NFSv4.
  3. *
  4. * Copyright (c) 2002 The Regents of the University of Michigan.
  5. * All rights reserved.
  6. *
  7. * Kendrick Smith <kmsmith@umich.edu>
  8. * Andy Adamson <andros@umich.edu>
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. Neither the name of the University nor the names of its
  20. * contributors may be used to endorse or promote products derived
  21. * from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  24. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  30. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  31. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <linux/file.h>
  36. #include <linux/falloc.h>
  37. #include <linux/slab.h>
  38. #include "idmap.h"
  39. #include "cache.h"
  40. #include "xdr4.h"
  41. #include "vfs.h"
  42. #include "current_stateid.h"
  43. #include "netns.h"
  44. #include "acl.h"
  45. #include "pnfs.h"
  46. #include "trace.h"
  47. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  48. #include <linux/security.h>
  49. static inline void
  50. nfsd4_security_inode_setsecctx(struct svc_fh *resfh, struct xdr_netobj *label, u32 *bmval)
  51. {
  52. struct inode *inode = d_inode(resfh->fh_dentry);
  53. int status;
  54. inode_lock(inode);
  55. status = security_inode_setsecctx(resfh->fh_dentry,
  56. label->data, label->len);
  57. inode_unlock(inode);
  58. if (status)
  59. /*
  60. * XXX: We should really fail the whole open, but we may
  61. * already have created a new file, so it may be too
  62. * late. For now this seems the least of evils:
  63. */
  64. bmval[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
  65. return;
  66. }
  67. #else
  68. static inline void
  69. nfsd4_security_inode_setsecctx(struct svc_fh *resfh, struct xdr_netobj *label, u32 *bmval)
  70. { }
  71. #endif
  72. #define NFSDDBG_FACILITY NFSDDBG_PROC
  73. static u32 nfsd_attrmask[] = {
  74. NFSD_WRITEABLE_ATTRS_WORD0,
  75. NFSD_WRITEABLE_ATTRS_WORD1,
  76. NFSD_WRITEABLE_ATTRS_WORD2
  77. };
  78. static u32 nfsd41_ex_attrmask[] = {
  79. NFSD_SUPPATTR_EXCLCREAT_WORD0,
  80. NFSD_SUPPATTR_EXCLCREAT_WORD1,
  81. NFSD_SUPPATTR_EXCLCREAT_WORD2
  82. };
  83. static __be32
  84. check_attr_support(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  85. u32 *bmval, u32 *writable)
  86. {
  87. struct dentry *dentry = cstate->current_fh.fh_dentry;
  88. struct svc_export *exp = cstate->current_fh.fh_export;
  89. if (!nfsd_attrs_supported(cstate->minorversion, bmval))
  90. return nfserr_attrnotsupp;
  91. if ((bmval[0] & FATTR4_WORD0_ACL) && !IS_POSIXACL(d_inode(dentry)))
  92. return nfserr_attrnotsupp;
  93. if ((bmval[2] & FATTR4_WORD2_SECURITY_LABEL) &&
  94. !(exp->ex_flags & NFSEXP_SECURITY_LABEL))
  95. return nfserr_attrnotsupp;
  96. if (writable && !bmval_is_subset(bmval, writable))
  97. return nfserr_inval;
  98. if (writable && (bmval[2] & FATTR4_WORD2_MODE_UMASK) &&
  99. (bmval[1] & FATTR4_WORD1_MODE))
  100. return nfserr_inval;
  101. return nfs_ok;
  102. }
  103. static __be32
  104. nfsd4_check_open_attributes(struct svc_rqst *rqstp,
  105. struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
  106. {
  107. __be32 status = nfs_ok;
  108. if (open->op_create == NFS4_OPEN_CREATE) {
  109. if (open->op_createmode == NFS4_CREATE_UNCHECKED
  110. || open->op_createmode == NFS4_CREATE_GUARDED)
  111. status = check_attr_support(rqstp, cstate,
  112. open->op_bmval, nfsd_attrmask);
  113. else if (open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1)
  114. status = check_attr_support(rqstp, cstate,
  115. open->op_bmval, nfsd41_ex_attrmask);
  116. }
  117. return status;
  118. }
  119. static int
  120. is_create_with_attrs(struct nfsd4_open *open)
  121. {
  122. return open->op_create == NFS4_OPEN_CREATE
  123. && (open->op_createmode == NFS4_CREATE_UNCHECKED
  124. || open->op_createmode == NFS4_CREATE_GUARDED
  125. || open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1);
  126. }
  127. /*
  128. * if error occurs when setting the acl, just clear the acl bit
  129. * in the returned attr bitmap.
  130. */
  131. static void
  132. do_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
  133. struct nfs4_acl *acl, u32 *bmval)
  134. {
  135. __be32 status;
  136. status = nfsd4_set_nfs4_acl(rqstp, fhp, acl);
  137. if (status)
  138. /*
  139. * We should probably fail the whole open at this point,
  140. * but we've already created the file, so it's too late;
  141. * So this seems the least of evils:
  142. */
  143. bmval[0] &= ~FATTR4_WORD0_ACL;
  144. }
  145. static inline void
  146. fh_dup2(struct svc_fh *dst, struct svc_fh *src)
  147. {
  148. fh_put(dst);
  149. dget(src->fh_dentry);
  150. if (src->fh_export)
  151. exp_get(src->fh_export);
  152. *dst = *src;
  153. }
  154. static __be32
  155. do_open_permission(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open, int accmode)
  156. {
  157. __be32 status;
  158. if (open->op_truncate &&
  159. !(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
  160. return nfserr_inval;
  161. accmode |= NFSD_MAY_READ_IF_EXEC;
  162. if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
  163. accmode |= NFSD_MAY_READ;
  164. if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
  165. accmode |= (NFSD_MAY_WRITE | NFSD_MAY_TRUNC);
  166. if (open->op_share_deny & NFS4_SHARE_DENY_READ)
  167. accmode |= NFSD_MAY_WRITE;
  168. status = fh_verify(rqstp, current_fh, S_IFREG, accmode);
  169. return status;
  170. }
  171. static __be32 nfsd_check_obj_isreg(struct svc_fh *fh)
  172. {
  173. umode_t mode = d_inode(fh->fh_dentry)->i_mode;
  174. if (S_ISREG(mode))
  175. return nfs_ok;
  176. if (S_ISDIR(mode))
  177. return nfserr_isdir;
  178. /*
  179. * Using err_symlink as our catch-all case may look odd; but
  180. * there's no other obvious error for this case in 4.0, and we
  181. * happen to know that it will cause the linux v4 client to do
  182. * the right thing on attempts to open something other than a
  183. * regular file.
  184. */
  185. return nfserr_symlink;
  186. }
  187. static void nfsd4_set_open_owner_reply_cache(struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh *resfh)
  188. {
  189. if (nfsd4_has_session(cstate))
  190. return;
  191. fh_copy_shallow(&open->op_openowner->oo_owner.so_replay.rp_openfh,
  192. &resfh->fh_handle);
  193. }
  194. static __be32
  195. do_open_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh **resfh)
  196. {
  197. struct svc_fh *current_fh = &cstate->current_fh;
  198. int accmode;
  199. __be32 status;
  200. *resfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
  201. if (!*resfh)
  202. return nfserr_jukebox;
  203. fh_init(*resfh, NFS4_FHSIZE);
  204. open->op_truncate = 0;
  205. if (open->op_create) {
  206. /* FIXME: check session persistence and pnfs flags.
  207. * The nfsv4.1 spec requires the following semantics:
  208. *
  209. * Persistent | pNFS | Server REQUIRED | Client Allowed
  210. * Reply Cache | server | |
  211. * -------------+--------+-----------------+--------------------
  212. * no | no | EXCLUSIVE4_1 | EXCLUSIVE4_1
  213. * | | | (SHOULD)
  214. * | | and EXCLUSIVE4 | or EXCLUSIVE4
  215. * | | | (SHOULD NOT)
  216. * no | yes | EXCLUSIVE4_1 | EXCLUSIVE4_1
  217. * yes | no | GUARDED4 | GUARDED4
  218. * yes | yes | GUARDED4 | GUARDED4
  219. */
  220. /*
  221. * Note: create modes (UNCHECKED,GUARDED...) are the same
  222. * in NFSv4 as in v3 except EXCLUSIVE4_1.
  223. */
  224. status = do_nfsd_create(rqstp, current_fh, open->op_fname.data,
  225. open->op_fname.len, &open->op_iattr,
  226. *resfh, open->op_createmode,
  227. (u32 *)open->op_verf.data,
  228. &open->op_truncate, &open->op_created);
  229. if (!status && open->op_label.len)
  230. nfsd4_security_inode_setsecctx(*resfh, &open->op_label, open->op_bmval);
  231. /*
  232. * Following rfc 3530 14.2.16, and rfc 5661 18.16.4
  233. * use the returned bitmask to indicate which attributes
  234. * we used to store the verifier:
  235. */
  236. if (nfsd_create_is_exclusive(open->op_createmode) && status == 0)
  237. open->op_bmval[1] |= (FATTR4_WORD1_TIME_ACCESS |
  238. FATTR4_WORD1_TIME_MODIFY);
  239. } else
  240. /*
  241. * Note this may exit with the parent still locked.
  242. * We will hold the lock until nfsd4_open's final
  243. * lookup, to prevent renames or unlinks until we've had
  244. * a chance to an acquire a delegation if appropriate.
  245. */
  246. status = nfsd_lookup(rqstp, current_fh,
  247. open->op_fname.data, open->op_fname.len, *resfh);
  248. if (status)
  249. goto out;
  250. status = nfsd_check_obj_isreg(*resfh);
  251. if (status)
  252. goto out;
  253. if (is_create_with_attrs(open) && open->op_acl != NULL)
  254. do_set_nfs4_acl(rqstp, *resfh, open->op_acl, open->op_bmval);
  255. nfsd4_set_open_owner_reply_cache(cstate, open, *resfh);
  256. accmode = NFSD_MAY_NOP;
  257. if (open->op_created ||
  258. open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
  259. accmode |= NFSD_MAY_OWNER_OVERRIDE;
  260. status = do_open_permission(rqstp, *resfh, open, accmode);
  261. set_change_info(&open->op_cinfo, current_fh);
  262. out:
  263. return status;
  264. }
  265. static __be32
  266. do_open_fhandle(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
  267. {
  268. struct svc_fh *current_fh = &cstate->current_fh;
  269. __be32 status;
  270. int accmode = 0;
  271. /* We don't know the target directory, and therefore can not
  272. * set the change info
  273. */
  274. memset(&open->op_cinfo, 0, sizeof(struct nfsd4_change_info));
  275. nfsd4_set_open_owner_reply_cache(cstate, open, current_fh);
  276. open->op_truncate = (open->op_iattr.ia_valid & ATTR_SIZE) &&
  277. (open->op_iattr.ia_size == 0);
  278. /*
  279. * In the delegation case, the client is telling us about an
  280. * open that it *already* performed locally, some time ago. We
  281. * should let it succeed now if possible.
  282. *
  283. * In the case of a CLAIM_FH open, on the other hand, the client
  284. * may be counting on us to enforce permissions (the Linux 4.1
  285. * client uses this for normal opens, for example).
  286. */
  287. if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH)
  288. accmode = NFSD_MAY_OWNER_OVERRIDE;
  289. status = do_open_permission(rqstp, current_fh, open, accmode);
  290. return status;
  291. }
  292. static void
  293. copy_clientid(clientid_t *clid, struct nfsd4_session *session)
  294. {
  295. struct nfsd4_sessionid *sid =
  296. (struct nfsd4_sessionid *)session->se_sessionid.data;
  297. clid->cl_boot = sid->clientid.cl_boot;
  298. clid->cl_id = sid->clientid.cl_id;
  299. }
  300. static __be32
  301. nfsd4_open(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  302. union nfsd4_op_u *u)
  303. {
  304. struct nfsd4_open *open = &u->open;
  305. __be32 status;
  306. struct svc_fh *resfh = NULL;
  307. struct net *net = SVC_NET(rqstp);
  308. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  309. dprintk("NFSD: nfsd4_open filename %.*s op_openowner %p\n",
  310. (int)open->op_fname.len, open->op_fname.data,
  311. open->op_openowner);
  312. /* This check required by spec. */
  313. if (open->op_create && open->op_claim_type != NFS4_OPEN_CLAIM_NULL)
  314. return nfserr_inval;
  315. open->op_created = 0;
  316. /*
  317. * RFC5661 18.51.3
  318. * Before RECLAIM_COMPLETE done, server should deny new lock
  319. */
  320. if (nfsd4_has_session(cstate) &&
  321. !test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
  322. &cstate->session->se_client->cl_flags) &&
  323. open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
  324. return nfserr_grace;
  325. if (nfsd4_has_session(cstate))
  326. copy_clientid(&open->op_clientid, cstate->session);
  327. /* check seqid for replay. set nfs4_owner */
  328. status = nfsd4_process_open1(cstate, open, nn);
  329. if (status == nfserr_replay_me) {
  330. struct nfs4_replay *rp = &open->op_openowner->oo_owner.so_replay;
  331. fh_put(&cstate->current_fh);
  332. fh_copy_shallow(&cstate->current_fh.fh_handle,
  333. &rp->rp_openfh);
  334. status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
  335. if (status)
  336. dprintk("nfsd4_open: replay failed"
  337. " restoring previous filehandle\n");
  338. else
  339. status = nfserr_replay_me;
  340. }
  341. if (status)
  342. goto out;
  343. if (open->op_xdr_error) {
  344. status = open->op_xdr_error;
  345. goto out;
  346. }
  347. status = nfsd4_check_open_attributes(rqstp, cstate, open);
  348. if (status)
  349. goto out;
  350. /* Openowner is now set, so sequence id will get bumped. Now we need
  351. * these checks before we do any creates: */
  352. status = nfserr_grace;
  353. if (opens_in_grace(net) && open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
  354. goto out;
  355. status = nfserr_no_grace;
  356. if (!opens_in_grace(net) && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
  357. goto out;
  358. switch (open->op_claim_type) {
  359. case NFS4_OPEN_CLAIM_DELEGATE_CUR:
  360. case NFS4_OPEN_CLAIM_NULL:
  361. status = do_open_lookup(rqstp, cstate, open, &resfh);
  362. if (status)
  363. goto out;
  364. break;
  365. case NFS4_OPEN_CLAIM_PREVIOUS:
  366. status = nfs4_check_open_reclaim(&open->op_clientid,
  367. cstate, nn);
  368. if (status)
  369. goto out;
  370. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  371. case NFS4_OPEN_CLAIM_FH:
  372. case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
  373. status = do_open_fhandle(rqstp, cstate, open);
  374. if (status)
  375. goto out;
  376. resfh = &cstate->current_fh;
  377. break;
  378. case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
  379. case NFS4_OPEN_CLAIM_DELEGATE_PREV:
  380. dprintk("NFSD: unsupported OPEN claim type %d\n",
  381. open->op_claim_type);
  382. status = nfserr_notsupp;
  383. goto out;
  384. default:
  385. dprintk("NFSD: Invalid OPEN claim type %d\n",
  386. open->op_claim_type);
  387. status = nfserr_inval;
  388. goto out;
  389. }
  390. /*
  391. * nfsd4_process_open2() does the actual opening of the file. If
  392. * successful, it (1) truncates the file if open->op_truncate was
  393. * set, (2) sets open->op_stateid, (3) sets open->op_delegation.
  394. */
  395. status = nfsd4_process_open2(rqstp, resfh, open);
  396. WARN(status && open->op_created,
  397. "nfsd4_process_open2 failed to open newly-created file! status=%u\n",
  398. be32_to_cpu(status));
  399. out:
  400. if (resfh && resfh != &cstate->current_fh) {
  401. fh_dup2(&cstate->current_fh, resfh);
  402. fh_put(resfh);
  403. kfree(resfh);
  404. }
  405. nfsd4_cleanup_open_state(cstate, open);
  406. nfsd4_bump_seqid(cstate, status);
  407. return status;
  408. }
  409. /*
  410. * OPEN is the only seqid-mutating operation whose decoding can fail
  411. * with a seqid-mutating error (specifically, decoding of user names in
  412. * the attributes). Therefore we have to do some processing to look up
  413. * the stateowner so that we can bump the seqid.
  414. */
  415. static __be32 nfsd4_open_omfg(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_op *op)
  416. {
  417. struct nfsd4_open *open = &op->u.open;
  418. if (!seqid_mutating_err(ntohl(op->status)))
  419. return op->status;
  420. if (nfsd4_has_session(cstate))
  421. return op->status;
  422. open->op_xdr_error = op->status;
  423. return nfsd4_open(rqstp, cstate, &op->u);
  424. }
  425. /*
  426. * filehandle-manipulating ops.
  427. */
  428. static __be32
  429. nfsd4_getfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  430. union nfsd4_op_u *u)
  431. {
  432. u->getfh = &cstate->current_fh;
  433. return nfs_ok;
  434. }
  435. static __be32
  436. nfsd4_putfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  437. union nfsd4_op_u *u)
  438. {
  439. struct nfsd4_putfh *putfh = &u->putfh;
  440. fh_put(&cstate->current_fh);
  441. cstate->current_fh.fh_handle.fh_size = putfh->pf_fhlen;
  442. memcpy(&cstate->current_fh.fh_handle.fh_base, putfh->pf_fhval,
  443. putfh->pf_fhlen);
  444. return fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_BYPASS_GSS);
  445. }
  446. static __be32
  447. nfsd4_putrootfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  448. union nfsd4_op_u *u)
  449. {
  450. __be32 status;
  451. fh_put(&cstate->current_fh);
  452. status = exp_pseudoroot(rqstp, &cstate->current_fh);
  453. return status;
  454. }
  455. static __be32
  456. nfsd4_restorefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  457. union nfsd4_op_u *u)
  458. {
  459. if (!cstate->save_fh.fh_dentry)
  460. return nfserr_restorefh;
  461. fh_dup2(&cstate->current_fh, &cstate->save_fh);
  462. if (HAS_STATE_ID(cstate, SAVED_STATE_ID_FLAG)) {
  463. memcpy(&cstate->current_stateid, &cstate->save_stateid, sizeof(stateid_t));
  464. SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
  465. }
  466. return nfs_ok;
  467. }
  468. static __be32
  469. nfsd4_savefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  470. union nfsd4_op_u *u)
  471. {
  472. fh_dup2(&cstate->save_fh, &cstate->current_fh);
  473. if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG)) {
  474. memcpy(&cstate->save_stateid, &cstate->current_stateid, sizeof(stateid_t));
  475. SET_STATE_ID(cstate, SAVED_STATE_ID_FLAG);
  476. }
  477. return nfs_ok;
  478. }
  479. /*
  480. * misc nfsv4 ops
  481. */
  482. static __be32
  483. nfsd4_access(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  484. union nfsd4_op_u *u)
  485. {
  486. struct nfsd4_access *access = &u->access;
  487. if (access->ac_req_access & ~NFS3_ACCESS_FULL)
  488. return nfserr_inval;
  489. access->ac_resp_access = access->ac_req_access;
  490. return nfsd_access(rqstp, &cstate->current_fh, &access->ac_resp_access,
  491. &access->ac_supported);
  492. }
  493. static void gen_boot_verifier(nfs4_verifier *verifier, struct net *net)
  494. {
  495. __be32 verf[2];
  496. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  497. /*
  498. * This is opaque to client, so no need to byte-swap. Use
  499. * __force to keep sparse happy. y2038 time_t overflow is
  500. * irrelevant in this usage.
  501. */
  502. verf[0] = (__force __be32)nn->nfssvc_boot.tv_sec;
  503. verf[1] = (__force __be32)nn->nfssvc_boot.tv_nsec;
  504. memcpy(verifier->data, verf, sizeof(verifier->data));
  505. }
  506. static __be32
  507. nfsd4_commit(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  508. union nfsd4_op_u *u)
  509. {
  510. struct nfsd4_commit *commit = &u->commit;
  511. gen_boot_verifier(&commit->co_verf, SVC_NET(rqstp));
  512. return nfsd_commit(rqstp, &cstate->current_fh, commit->co_offset,
  513. commit->co_count);
  514. }
  515. static __be32
  516. nfsd4_create(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  517. union nfsd4_op_u *u)
  518. {
  519. struct nfsd4_create *create = &u->create;
  520. struct svc_fh resfh;
  521. __be32 status;
  522. dev_t rdev;
  523. fh_init(&resfh, NFS4_FHSIZE);
  524. status = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_NOP);
  525. if (status)
  526. return status;
  527. status = check_attr_support(rqstp, cstate, create->cr_bmval,
  528. nfsd_attrmask);
  529. if (status)
  530. return status;
  531. switch (create->cr_type) {
  532. case NF4LNK:
  533. status = nfsd_symlink(rqstp, &cstate->current_fh,
  534. create->cr_name, create->cr_namelen,
  535. create->cr_data, &resfh);
  536. break;
  537. case NF4BLK:
  538. rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
  539. if (MAJOR(rdev) != create->cr_specdata1 ||
  540. MINOR(rdev) != create->cr_specdata2)
  541. return nfserr_inval;
  542. status = nfsd_create(rqstp, &cstate->current_fh,
  543. create->cr_name, create->cr_namelen,
  544. &create->cr_iattr, S_IFBLK, rdev, &resfh);
  545. break;
  546. case NF4CHR:
  547. rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
  548. if (MAJOR(rdev) != create->cr_specdata1 ||
  549. MINOR(rdev) != create->cr_specdata2)
  550. return nfserr_inval;
  551. status = nfsd_create(rqstp, &cstate->current_fh,
  552. create->cr_name, create->cr_namelen,
  553. &create->cr_iattr,S_IFCHR, rdev, &resfh);
  554. break;
  555. case NF4SOCK:
  556. status = nfsd_create(rqstp, &cstate->current_fh,
  557. create->cr_name, create->cr_namelen,
  558. &create->cr_iattr, S_IFSOCK, 0, &resfh);
  559. break;
  560. case NF4FIFO:
  561. status = nfsd_create(rqstp, &cstate->current_fh,
  562. create->cr_name, create->cr_namelen,
  563. &create->cr_iattr, S_IFIFO, 0, &resfh);
  564. break;
  565. case NF4DIR:
  566. create->cr_iattr.ia_valid &= ~ATTR_SIZE;
  567. status = nfsd_create(rqstp, &cstate->current_fh,
  568. create->cr_name, create->cr_namelen,
  569. &create->cr_iattr, S_IFDIR, 0, &resfh);
  570. break;
  571. default:
  572. status = nfserr_badtype;
  573. }
  574. if (status)
  575. goto out;
  576. if (create->cr_label.len)
  577. nfsd4_security_inode_setsecctx(&resfh, &create->cr_label, create->cr_bmval);
  578. if (create->cr_acl != NULL)
  579. do_set_nfs4_acl(rqstp, &resfh, create->cr_acl,
  580. create->cr_bmval);
  581. fh_unlock(&cstate->current_fh);
  582. set_change_info(&create->cr_cinfo, &cstate->current_fh);
  583. fh_dup2(&cstate->current_fh, &resfh);
  584. out:
  585. fh_put(&resfh);
  586. return status;
  587. }
  588. static __be32
  589. nfsd4_getattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  590. union nfsd4_op_u *u)
  591. {
  592. struct nfsd4_getattr *getattr = &u->getattr;
  593. __be32 status;
  594. status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
  595. if (status)
  596. return status;
  597. if (getattr->ga_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
  598. return nfserr_inval;
  599. getattr->ga_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
  600. getattr->ga_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
  601. getattr->ga_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
  602. getattr->ga_fhp = &cstate->current_fh;
  603. return nfs_ok;
  604. }
  605. static __be32
  606. nfsd4_link(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  607. union nfsd4_op_u *u)
  608. {
  609. struct nfsd4_link *link = &u->link;
  610. __be32 status;
  611. status = nfsd_link(rqstp, &cstate->current_fh,
  612. link->li_name, link->li_namelen, &cstate->save_fh);
  613. if (!status)
  614. set_change_info(&link->li_cinfo, &cstate->current_fh);
  615. return status;
  616. }
  617. static __be32 nfsd4_do_lookupp(struct svc_rqst *rqstp, struct svc_fh *fh)
  618. {
  619. struct svc_fh tmp_fh;
  620. __be32 ret;
  621. fh_init(&tmp_fh, NFS4_FHSIZE);
  622. ret = exp_pseudoroot(rqstp, &tmp_fh);
  623. if (ret)
  624. return ret;
  625. if (tmp_fh.fh_dentry == fh->fh_dentry) {
  626. fh_put(&tmp_fh);
  627. return nfserr_noent;
  628. }
  629. fh_put(&tmp_fh);
  630. return nfsd_lookup(rqstp, fh, "..", 2, fh);
  631. }
  632. static __be32
  633. nfsd4_lookupp(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  634. union nfsd4_op_u *u)
  635. {
  636. return nfsd4_do_lookupp(rqstp, &cstate->current_fh);
  637. }
  638. static __be32
  639. nfsd4_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  640. union nfsd4_op_u *u)
  641. {
  642. return nfsd_lookup(rqstp, &cstate->current_fh,
  643. u->lookup.lo_name, u->lookup.lo_len,
  644. &cstate->current_fh);
  645. }
  646. static __be32
  647. nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  648. union nfsd4_op_u *u)
  649. {
  650. struct nfsd4_read *read = &u->read;
  651. __be32 status;
  652. read->rd_filp = NULL;
  653. if (read->rd_offset >= OFFSET_MAX)
  654. return nfserr_inval;
  655. /*
  656. * If we do a zero copy read, then a client will see read data
  657. * that reflects the state of the file *after* performing the
  658. * following compound.
  659. *
  660. * To ensure proper ordering, we therefore turn off zero copy if
  661. * the client wants us to do more in this compound:
  662. */
  663. if (!nfsd4_last_compound_op(rqstp))
  664. clear_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
  665. /* check stateid */
  666. status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
  667. &read->rd_stateid, RD_STATE,
  668. &read->rd_filp, &read->rd_tmp_file);
  669. if (status) {
  670. dprintk("NFSD: nfsd4_read: couldn't process stateid!\n");
  671. goto out;
  672. }
  673. status = nfs_ok;
  674. out:
  675. read->rd_rqstp = rqstp;
  676. read->rd_fhp = &cstate->current_fh;
  677. return status;
  678. }
  679. static void
  680. nfsd4_read_release(union nfsd4_op_u *u)
  681. {
  682. if (u->read.rd_filp)
  683. fput(u->read.rd_filp);
  684. }
  685. static __be32
  686. nfsd4_readdir(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  687. union nfsd4_op_u *u)
  688. {
  689. struct nfsd4_readdir *readdir = &u->readdir;
  690. u64 cookie = readdir->rd_cookie;
  691. static const nfs4_verifier zeroverf;
  692. /* no need to check permission - this will be done in nfsd_readdir() */
  693. if (readdir->rd_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
  694. return nfserr_inval;
  695. readdir->rd_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
  696. readdir->rd_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
  697. readdir->rd_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
  698. if ((cookie == 1) || (cookie == 2) ||
  699. (cookie == 0 && memcmp(readdir->rd_verf.data, zeroverf.data, NFS4_VERIFIER_SIZE)))
  700. return nfserr_bad_cookie;
  701. readdir->rd_rqstp = rqstp;
  702. readdir->rd_fhp = &cstate->current_fh;
  703. return nfs_ok;
  704. }
  705. static __be32
  706. nfsd4_readlink(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  707. union nfsd4_op_u *u)
  708. {
  709. u->readlink.rl_rqstp = rqstp;
  710. u->readlink.rl_fhp = &cstate->current_fh;
  711. return nfs_ok;
  712. }
  713. static __be32
  714. nfsd4_remove(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  715. union nfsd4_op_u *u)
  716. {
  717. struct nfsd4_remove *remove = &u->remove;
  718. __be32 status;
  719. if (opens_in_grace(SVC_NET(rqstp)))
  720. return nfserr_grace;
  721. status = nfsd_unlink(rqstp, &cstate->current_fh, 0,
  722. remove->rm_name, remove->rm_namelen);
  723. if (!status) {
  724. fh_unlock(&cstate->current_fh);
  725. set_change_info(&remove->rm_cinfo, &cstate->current_fh);
  726. }
  727. return status;
  728. }
  729. static __be32
  730. nfsd4_rename(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  731. union nfsd4_op_u *u)
  732. {
  733. struct nfsd4_rename *rename = &u->rename;
  734. __be32 status;
  735. if (opens_in_grace(SVC_NET(rqstp)) &&
  736. !(cstate->save_fh.fh_export->ex_flags & NFSEXP_NOSUBTREECHECK))
  737. return nfserr_grace;
  738. status = nfsd_rename(rqstp, &cstate->save_fh, rename->rn_sname,
  739. rename->rn_snamelen, &cstate->current_fh,
  740. rename->rn_tname, rename->rn_tnamelen);
  741. if (status)
  742. return status;
  743. set_change_info(&rename->rn_sinfo, &cstate->current_fh);
  744. set_change_info(&rename->rn_tinfo, &cstate->save_fh);
  745. return nfs_ok;
  746. }
  747. static __be32
  748. nfsd4_secinfo(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  749. union nfsd4_op_u *u)
  750. {
  751. struct nfsd4_secinfo *secinfo = &u->secinfo;
  752. struct svc_export *exp;
  753. struct dentry *dentry;
  754. __be32 err;
  755. err = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_EXEC);
  756. if (err)
  757. return err;
  758. err = nfsd_lookup_dentry(rqstp, &cstate->current_fh,
  759. secinfo->si_name, secinfo->si_namelen,
  760. &exp, &dentry);
  761. if (err)
  762. return err;
  763. fh_unlock(&cstate->current_fh);
  764. if (d_really_is_negative(dentry)) {
  765. exp_put(exp);
  766. err = nfserr_noent;
  767. } else
  768. secinfo->si_exp = exp;
  769. dput(dentry);
  770. if (cstate->minorversion)
  771. /* See rfc 5661 section 2.6.3.1.1.8 */
  772. fh_put(&cstate->current_fh);
  773. return err;
  774. }
  775. static __be32
  776. nfsd4_secinfo_no_name(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  777. union nfsd4_op_u *u)
  778. {
  779. __be32 err;
  780. switch (u->secinfo_no_name.sin_style) {
  781. case NFS4_SECINFO_STYLE4_CURRENT_FH:
  782. break;
  783. case NFS4_SECINFO_STYLE4_PARENT:
  784. err = nfsd4_do_lookupp(rqstp, &cstate->current_fh);
  785. if (err)
  786. return err;
  787. break;
  788. default:
  789. return nfserr_inval;
  790. }
  791. u->secinfo_no_name.sin_exp = exp_get(cstate->current_fh.fh_export);
  792. fh_put(&cstate->current_fh);
  793. return nfs_ok;
  794. }
  795. static void
  796. nfsd4_secinfo_release(union nfsd4_op_u *u)
  797. {
  798. if (u->secinfo.si_exp)
  799. exp_put(u->secinfo.si_exp);
  800. }
  801. static void
  802. nfsd4_secinfo_no_name_release(union nfsd4_op_u *u)
  803. {
  804. if (u->secinfo_no_name.sin_exp)
  805. exp_put(u->secinfo_no_name.sin_exp);
  806. }
  807. static __be32
  808. nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  809. union nfsd4_op_u *u)
  810. {
  811. struct nfsd4_setattr *setattr = &u->setattr;
  812. __be32 status = nfs_ok;
  813. int err;
  814. if (setattr->sa_iattr.ia_valid & ATTR_SIZE) {
  815. status = nfs4_preprocess_stateid_op(rqstp, cstate,
  816. &cstate->current_fh, &setattr->sa_stateid,
  817. WR_STATE, NULL, NULL);
  818. if (status) {
  819. dprintk("NFSD: nfsd4_setattr: couldn't process stateid!\n");
  820. return status;
  821. }
  822. }
  823. err = fh_want_write(&cstate->current_fh);
  824. if (err)
  825. return nfserrno(err);
  826. status = nfs_ok;
  827. status = check_attr_support(rqstp, cstate, setattr->sa_bmval,
  828. nfsd_attrmask);
  829. if (status)
  830. goto out;
  831. if (setattr->sa_acl != NULL)
  832. status = nfsd4_set_nfs4_acl(rqstp, &cstate->current_fh,
  833. setattr->sa_acl);
  834. if (status)
  835. goto out;
  836. if (setattr->sa_label.len)
  837. status = nfsd4_set_nfs4_label(rqstp, &cstate->current_fh,
  838. &setattr->sa_label);
  839. if (status)
  840. goto out;
  841. status = nfsd_setattr(rqstp, &cstate->current_fh, &setattr->sa_iattr,
  842. 0, (time_t)0);
  843. out:
  844. fh_drop_write(&cstate->current_fh);
  845. return status;
  846. }
  847. static int fill_in_write_vector(struct kvec *vec, struct nfsd4_write *write)
  848. {
  849. int i = 1;
  850. int buflen = write->wr_buflen;
  851. vec[0].iov_base = write->wr_head.iov_base;
  852. vec[0].iov_len = min_t(int, buflen, write->wr_head.iov_len);
  853. buflen -= vec[0].iov_len;
  854. while (buflen) {
  855. vec[i].iov_base = page_address(write->wr_pagelist[i - 1]);
  856. vec[i].iov_len = min_t(int, PAGE_SIZE, buflen);
  857. buflen -= vec[i].iov_len;
  858. i++;
  859. }
  860. return i;
  861. }
  862. static __be32
  863. nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  864. union nfsd4_op_u *u)
  865. {
  866. struct nfsd4_write *write = &u->write;
  867. stateid_t *stateid = &write->wr_stateid;
  868. struct file *filp = NULL;
  869. __be32 status = nfs_ok;
  870. unsigned long cnt;
  871. int nvecs;
  872. if (write->wr_offset >= OFFSET_MAX)
  873. return nfserr_inval;
  874. status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
  875. stateid, WR_STATE, &filp, NULL);
  876. if (status) {
  877. dprintk("NFSD: nfsd4_write: couldn't process stateid!\n");
  878. return status;
  879. }
  880. cnt = write->wr_buflen;
  881. write->wr_how_written = write->wr_stable_how;
  882. gen_boot_verifier(&write->wr_verifier, SVC_NET(rqstp));
  883. nvecs = fill_in_write_vector(rqstp->rq_vec, write);
  884. WARN_ON_ONCE(nvecs > ARRAY_SIZE(rqstp->rq_vec));
  885. status = nfsd_vfs_write(rqstp, &cstate->current_fh, filp,
  886. write->wr_offset, rqstp->rq_vec, nvecs, &cnt,
  887. write->wr_how_written);
  888. fput(filp);
  889. write->wr_bytes_written = cnt;
  890. return status;
  891. }
  892. static __be32
  893. nfsd4_verify_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  894. stateid_t *src_stateid, struct file **src,
  895. stateid_t *dst_stateid, struct file **dst)
  896. {
  897. __be32 status;
  898. status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->save_fh,
  899. src_stateid, RD_STATE, src, NULL);
  900. if (status) {
  901. dprintk("NFSD: %s: couldn't process src stateid!\n", __func__);
  902. goto out;
  903. }
  904. status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
  905. dst_stateid, WR_STATE, dst, NULL);
  906. if (status) {
  907. dprintk("NFSD: %s: couldn't process dst stateid!\n", __func__);
  908. goto out_put_src;
  909. }
  910. /* fix up for NFS-specific error code */
  911. if (!S_ISREG(file_inode(*src)->i_mode) ||
  912. !S_ISREG(file_inode(*dst)->i_mode)) {
  913. status = nfserr_wrong_type;
  914. goto out_put_dst;
  915. }
  916. out:
  917. return status;
  918. out_put_dst:
  919. fput(*dst);
  920. out_put_src:
  921. fput(*src);
  922. goto out;
  923. }
  924. static __be32
  925. nfsd4_clone(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  926. union nfsd4_op_u *u)
  927. {
  928. struct nfsd4_clone *clone = &u->clone;
  929. struct file *src, *dst;
  930. __be32 status;
  931. status = nfsd4_verify_copy(rqstp, cstate, &clone->cl_src_stateid, &src,
  932. &clone->cl_dst_stateid, &dst);
  933. if (status)
  934. goto out;
  935. status = nfsd4_clone_file_range(src, clone->cl_src_pos,
  936. dst, clone->cl_dst_pos, clone->cl_count);
  937. fput(dst);
  938. fput(src);
  939. out:
  940. return status;
  941. }
  942. static __be32
  943. nfsd4_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  944. union nfsd4_op_u *u)
  945. {
  946. struct nfsd4_copy *copy = &u->copy;
  947. struct file *src, *dst;
  948. __be32 status;
  949. ssize_t bytes;
  950. status = nfsd4_verify_copy(rqstp, cstate, &copy->cp_src_stateid, &src,
  951. &copy->cp_dst_stateid, &dst);
  952. if (status)
  953. goto out;
  954. bytes = nfsd_copy_file_range(src, copy->cp_src_pos,
  955. dst, copy->cp_dst_pos, copy->cp_count);
  956. if (bytes < 0)
  957. status = nfserrno(bytes);
  958. else {
  959. copy->cp_res.wr_bytes_written = bytes;
  960. copy->cp_res.wr_stable_how = NFS_UNSTABLE;
  961. copy->cp_consecutive = 1;
  962. copy->cp_synchronous = 1;
  963. gen_boot_verifier(&copy->cp_res.wr_verifier, SVC_NET(rqstp));
  964. status = nfs_ok;
  965. }
  966. fput(src);
  967. fput(dst);
  968. out:
  969. return status;
  970. }
  971. static __be32
  972. nfsd4_fallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  973. struct nfsd4_fallocate *fallocate, int flags)
  974. {
  975. __be32 status = nfserr_notsupp;
  976. struct file *file;
  977. status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
  978. &fallocate->falloc_stateid,
  979. WR_STATE, &file, NULL);
  980. if (status != nfs_ok) {
  981. dprintk("NFSD: nfsd4_fallocate: couldn't process stateid!\n");
  982. return status;
  983. }
  984. status = nfsd4_vfs_fallocate(rqstp, &cstate->current_fh, file,
  985. fallocate->falloc_offset,
  986. fallocate->falloc_length,
  987. flags);
  988. fput(file);
  989. return status;
  990. }
  991. static __be32
  992. nfsd4_allocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  993. union nfsd4_op_u *u)
  994. {
  995. return nfsd4_fallocate(rqstp, cstate, &u->allocate, 0);
  996. }
  997. static __be32
  998. nfsd4_deallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  999. union nfsd4_op_u *u)
  1000. {
  1001. return nfsd4_fallocate(rqstp, cstate, &u->deallocate,
  1002. FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
  1003. }
  1004. static __be32
  1005. nfsd4_seek(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1006. union nfsd4_op_u *u)
  1007. {
  1008. struct nfsd4_seek *seek = &u->seek;
  1009. int whence;
  1010. __be32 status;
  1011. struct file *file;
  1012. status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
  1013. &seek->seek_stateid,
  1014. RD_STATE, &file, NULL);
  1015. if (status) {
  1016. dprintk("NFSD: nfsd4_seek: couldn't process stateid!\n");
  1017. return status;
  1018. }
  1019. switch (seek->seek_whence) {
  1020. case NFS4_CONTENT_DATA:
  1021. whence = SEEK_DATA;
  1022. break;
  1023. case NFS4_CONTENT_HOLE:
  1024. whence = SEEK_HOLE;
  1025. break;
  1026. default:
  1027. status = nfserr_union_notsupp;
  1028. goto out;
  1029. }
  1030. /*
  1031. * Note: This call does change file->f_pos, but nothing in NFSD
  1032. * should ever file->f_pos.
  1033. */
  1034. seek->seek_pos = vfs_llseek(file, seek->seek_offset, whence);
  1035. if (seek->seek_pos < 0)
  1036. status = nfserrno(seek->seek_pos);
  1037. else if (seek->seek_pos >= i_size_read(file_inode(file)))
  1038. seek->seek_eof = true;
  1039. out:
  1040. fput(file);
  1041. return status;
  1042. }
  1043. /* This routine never returns NFS_OK! If there are no other errors, it
  1044. * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the
  1045. * attributes matched. VERIFY is implemented by mapping NFSERR_SAME
  1046. * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK.
  1047. */
  1048. static __be32
  1049. _nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1050. struct nfsd4_verify *verify)
  1051. {
  1052. __be32 *buf, *p;
  1053. int count;
  1054. __be32 status;
  1055. status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
  1056. if (status)
  1057. return status;
  1058. status = check_attr_support(rqstp, cstate, verify->ve_bmval, NULL);
  1059. if (status)
  1060. return status;
  1061. if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR)
  1062. || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1))
  1063. return nfserr_inval;
  1064. if (verify->ve_attrlen & 3)
  1065. return nfserr_inval;
  1066. /* count in words:
  1067. * bitmap_len(1) + bitmap(2) + attr_len(1) = 4
  1068. */
  1069. count = 4 + (verify->ve_attrlen >> 2);
  1070. buf = kmalloc(count << 2, GFP_KERNEL);
  1071. if (!buf)
  1072. return nfserr_jukebox;
  1073. p = buf;
  1074. status = nfsd4_encode_fattr_to_buf(&p, count, &cstate->current_fh,
  1075. cstate->current_fh.fh_export,
  1076. cstate->current_fh.fh_dentry,
  1077. verify->ve_bmval,
  1078. rqstp, 0);
  1079. /*
  1080. * If nfsd4_encode_fattr() ran out of space, assume that's because
  1081. * the attributes are longer (hence different) than those given:
  1082. */
  1083. if (status == nfserr_resource)
  1084. status = nfserr_not_same;
  1085. if (status)
  1086. goto out_kfree;
  1087. /* skip bitmap */
  1088. p = buf + 1 + ntohl(buf[0]);
  1089. status = nfserr_not_same;
  1090. if (ntohl(*p++) != verify->ve_attrlen)
  1091. goto out_kfree;
  1092. if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen))
  1093. status = nfserr_same;
  1094. out_kfree:
  1095. kfree(buf);
  1096. return status;
  1097. }
  1098. static __be32
  1099. nfsd4_nverify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1100. union nfsd4_op_u *u)
  1101. {
  1102. __be32 status;
  1103. status = _nfsd4_verify(rqstp, cstate, &u->verify);
  1104. return status == nfserr_not_same ? nfs_ok : status;
  1105. }
  1106. static __be32
  1107. nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1108. union nfsd4_op_u *u)
  1109. {
  1110. __be32 status;
  1111. status = _nfsd4_verify(rqstp, cstate, &u->nverify);
  1112. return status == nfserr_same ? nfs_ok : status;
  1113. }
  1114. #ifdef CONFIG_NFSD_PNFS
  1115. static const struct nfsd4_layout_ops *
  1116. nfsd4_layout_verify(struct svc_export *exp, unsigned int layout_type)
  1117. {
  1118. if (!exp->ex_layout_types) {
  1119. dprintk("%s: export does not support pNFS\n", __func__);
  1120. return NULL;
  1121. }
  1122. if (layout_type >= LAYOUT_TYPE_MAX ||
  1123. !(exp->ex_layout_types & (1 << layout_type))) {
  1124. dprintk("%s: layout type %d not supported\n",
  1125. __func__, layout_type);
  1126. return NULL;
  1127. }
  1128. return nfsd4_layout_ops[layout_type];
  1129. }
  1130. static __be32
  1131. nfsd4_getdeviceinfo(struct svc_rqst *rqstp,
  1132. struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
  1133. {
  1134. struct nfsd4_getdeviceinfo *gdp = &u->getdeviceinfo;
  1135. const struct nfsd4_layout_ops *ops;
  1136. struct nfsd4_deviceid_map *map;
  1137. struct svc_export *exp;
  1138. __be32 nfserr;
  1139. dprintk("%s: layout_type %u dev_id [0x%llx:0x%x] maxcnt %u\n",
  1140. __func__,
  1141. gdp->gd_layout_type,
  1142. gdp->gd_devid.fsid_idx, gdp->gd_devid.generation,
  1143. gdp->gd_maxcount);
  1144. map = nfsd4_find_devid_map(gdp->gd_devid.fsid_idx);
  1145. if (!map) {
  1146. dprintk("%s: couldn't find device ID to export mapping!\n",
  1147. __func__);
  1148. return nfserr_noent;
  1149. }
  1150. exp = rqst_exp_find(rqstp, map->fsid_type, map->fsid);
  1151. if (IS_ERR(exp)) {
  1152. dprintk("%s: could not find device id\n", __func__);
  1153. return nfserr_noent;
  1154. }
  1155. nfserr = nfserr_layoutunavailable;
  1156. ops = nfsd4_layout_verify(exp, gdp->gd_layout_type);
  1157. if (!ops)
  1158. goto out;
  1159. nfserr = nfs_ok;
  1160. if (gdp->gd_maxcount != 0) {
  1161. nfserr = ops->proc_getdeviceinfo(exp->ex_path.mnt->mnt_sb,
  1162. rqstp, cstate->session->se_client, gdp);
  1163. }
  1164. gdp->gd_notify_types &= ops->notify_types;
  1165. out:
  1166. exp_put(exp);
  1167. return nfserr;
  1168. }
  1169. static void
  1170. nfsd4_getdeviceinfo_release(union nfsd4_op_u *u)
  1171. {
  1172. kfree(u->getdeviceinfo.gd_device);
  1173. }
  1174. static __be32
  1175. nfsd4_layoutget(struct svc_rqst *rqstp,
  1176. struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
  1177. {
  1178. struct nfsd4_layoutget *lgp = &u->layoutget;
  1179. struct svc_fh *current_fh = &cstate->current_fh;
  1180. const struct nfsd4_layout_ops *ops;
  1181. struct nfs4_layout_stateid *ls;
  1182. __be32 nfserr;
  1183. int accmode = NFSD_MAY_READ_IF_EXEC;
  1184. switch (lgp->lg_seg.iomode) {
  1185. case IOMODE_READ:
  1186. accmode |= NFSD_MAY_READ;
  1187. break;
  1188. case IOMODE_RW:
  1189. accmode |= NFSD_MAY_READ | NFSD_MAY_WRITE;
  1190. break;
  1191. default:
  1192. dprintk("%s: invalid iomode %d\n",
  1193. __func__, lgp->lg_seg.iomode);
  1194. nfserr = nfserr_badiomode;
  1195. goto out;
  1196. }
  1197. nfserr = fh_verify(rqstp, current_fh, 0, accmode);
  1198. if (nfserr)
  1199. goto out;
  1200. nfserr = nfserr_layoutunavailable;
  1201. ops = nfsd4_layout_verify(current_fh->fh_export, lgp->lg_layout_type);
  1202. if (!ops)
  1203. goto out;
  1204. /*
  1205. * Verify minlength and range as per RFC5661:
  1206. * o If loga_length is less than loga_minlength,
  1207. * the metadata server MUST return NFS4ERR_INVAL.
  1208. * o If the sum of loga_offset and loga_minlength exceeds
  1209. * NFS4_UINT64_MAX, and loga_minlength is not
  1210. * NFS4_UINT64_MAX, the error NFS4ERR_INVAL MUST result.
  1211. * o If the sum of loga_offset and loga_length exceeds
  1212. * NFS4_UINT64_MAX, and loga_length is not NFS4_UINT64_MAX,
  1213. * the error NFS4ERR_INVAL MUST result.
  1214. */
  1215. nfserr = nfserr_inval;
  1216. if (lgp->lg_seg.length < lgp->lg_minlength ||
  1217. (lgp->lg_minlength != NFS4_MAX_UINT64 &&
  1218. lgp->lg_minlength > NFS4_MAX_UINT64 - lgp->lg_seg.offset) ||
  1219. (lgp->lg_seg.length != NFS4_MAX_UINT64 &&
  1220. lgp->lg_seg.length > NFS4_MAX_UINT64 - lgp->lg_seg.offset))
  1221. goto out;
  1222. if (lgp->lg_seg.length == 0)
  1223. goto out;
  1224. nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lgp->lg_sid,
  1225. true, lgp->lg_layout_type, &ls);
  1226. if (nfserr) {
  1227. trace_layout_get_lookup_fail(&lgp->lg_sid);
  1228. goto out;
  1229. }
  1230. nfserr = nfserr_recallconflict;
  1231. if (atomic_read(&ls->ls_stid.sc_file->fi_lo_recalls))
  1232. goto out_put_stid;
  1233. nfserr = ops->proc_layoutget(d_inode(current_fh->fh_dentry),
  1234. current_fh, lgp);
  1235. if (nfserr)
  1236. goto out_put_stid;
  1237. nfserr = nfsd4_insert_layout(lgp, ls);
  1238. out_put_stid:
  1239. mutex_unlock(&ls->ls_mutex);
  1240. nfs4_put_stid(&ls->ls_stid);
  1241. out:
  1242. return nfserr;
  1243. }
  1244. static void
  1245. nfsd4_layoutget_release(union nfsd4_op_u *u)
  1246. {
  1247. kfree(u->layoutget.lg_content);
  1248. }
  1249. static __be32
  1250. nfsd4_layoutcommit(struct svc_rqst *rqstp,
  1251. struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
  1252. {
  1253. struct nfsd4_layoutcommit *lcp = &u->layoutcommit;
  1254. const struct nfsd4_layout_seg *seg = &lcp->lc_seg;
  1255. struct svc_fh *current_fh = &cstate->current_fh;
  1256. const struct nfsd4_layout_ops *ops;
  1257. loff_t new_size = lcp->lc_last_wr + 1;
  1258. struct inode *inode;
  1259. struct nfs4_layout_stateid *ls;
  1260. __be32 nfserr;
  1261. nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_WRITE);
  1262. if (nfserr)
  1263. goto out;
  1264. nfserr = nfserr_layoutunavailable;
  1265. ops = nfsd4_layout_verify(current_fh->fh_export, lcp->lc_layout_type);
  1266. if (!ops)
  1267. goto out;
  1268. inode = d_inode(current_fh->fh_dentry);
  1269. nfserr = nfserr_inval;
  1270. if (new_size <= seg->offset) {
  1271. dprintk("pnfsd: last write before layout segment\n");
  1272. goto out;
  1273. }
  1274. if (new_size > seg->offset + seg->length) {
  1275. dprintk("pnfsd: last write beyond layout segment\n");
  1276. goto out;
  1277. }
  1278. if (!lcp->lc_newoffset && new_size > i_size_read(inode)) {
  1279. dprintk("pnfsd: layoutcommit beyond EOF\n");
  1280. goto out;
  1281. }
  1282. nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lcp->lc_sid,
  1283. false, lcp->lc_layout_type,
  1284. &ls);
  1285. if (nfserr) {
  1286. trace_layout_commit_lookup_fail(&lcp->lc_sid);
  1287. /* fixup error code as per RFC5661 */
  1288. if (nfserr == nfserr_bad_stateid)
  1289. nfserr = nfserr_badlayout;
  1290. goto out;
  1291. }
  1292. /* LAYOUTCOMMIT does not require any serialization */
  1293. mutex_unlock(&ls->ls_mutex);
  1294. if (new_size > i_size_read(inode)) {
  1295. lcp->lc_size_chg = 1;
  1296. lcp->lc_newsize = new_size;
  1297. } else {
  1298. lcp->lc_size_chg = 0;
  1299. }
  1300. nfserr = ops->proc_layoutcommit(inode, lcp);
  1301. nfs4_put_stid(&ls->ls_stid);
  1302. out:
  1303. return nfserr;
  1304. }
  1305. static __be32
  1306. nfsd4_layoutreturn(struct svc_rqst *rqstp,
  1307. struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
  1308. {
  1309. struct nfsd4_layoutreturn *lrp = &u->layoutreturn;
  1310. struct svc_fh *current_fh = &cstate->current_fh;
  1311. __be32 nfserr;
  1312. nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_NOP);
  1313. if (nfserr)
  1314. goto out;
  1315. nfserr = nfserr_layoutunavailable;
  1316. if (!nfsd4_layout_verify(current_fh->fh_export, lrp->lr_layout_type))
  1317. goto out;
  1318. switch (lrp->lr_seg.iomode) {
  1319. case IOMODE_READ:
  1320. case IOMODE_RW:
  1321. case IOMODE_ANY:
  1322. break;
  1323. default:
  1324. dprintk("%s: invalid iomode %d\n", __func__,
  1325. lrp->lr_seg.iomode);
  1326. nfserr = nfserr_inval;
  1327. goto out;
  1328. }
  1329. switch (lrp->lr_return_type) {
  1330. case RETURN_FILE:
  1331. nfserr = nfsd4_return_file_layouts(rqstp, cstate, lrp);
  1332. break;
  1333. case RETURN_FSID:
  1334. case RETURN_ALL:
  1335. nfserr = nfsd4_return_client_layouts(rqstp, cstate, lrp);
  1336. break;
  1337. default:
  1338. dprintk("%s: invalid return_type %d\n", __func__,
  1339. lrp->lr_return_type);
  1340. nfserr = nfserr_inval;
  1341. break;
  1342. }
  1343. out:
  1344. return nfserr;
  1345. }
  1346. #endif /* CONFIG_NFSD_PNFS */
  1347. /*
  1348. * NULL call.
  1349. */
  1350. static __be32
  1351. nfsd4_proc_null(struct svc_rqst *rqstp)
  1352. {
  1353. return nfs_ok;
  1354. }
  1355. static inline void nfsd4_increment_op_stats(u32 opnum)
  1356. {
  1357. if (opnum >= FIRST_NFS4_OP && opnum <= LAST_NFS4_OP)
  1358. nfsdstats.nfs4_opcount[opnum]++;
  1359. }
  1360. static const struct nfsd4_operation nfsd4_ops[];
  1361. static const char *nfsd4_op_name(unsigned opnum);
  1362. /*
  1363. * Enforce NFSv4.1 COMPOUND ordering rules:
  1364. *
  1365. * Also note, enforced elsewhere:
  1366. * - SEQUENCE other than as first op results in
  1367. * NFS4ERR_SEQUENCE_POS. (Enforced in nfsd4_sequence().)
  1368. * - BIND_CONN_TO_SESSION must be the only op in its compound.
  1369. * (Enforced in nfsd4_bind_conn_to_session().)
  1370. * - DESTROY_SESSION must be the final operation in a compound, if
  1371. * sessionid's in SEQUENCE and DESTROY_SESSION are the same.
  1372. * (Enforced in nfsd4_destroy_session().)
  1373. */
  1374. static __be32 nfs41_check_op_ordering(struct nfsd4_compoundargs *args)
  1375. {
  1376. struct nfsd4_op *op = &args->ops[0];
  1377. /* These ordering requirements don't apply to NFSv4.0: */
  1378. if (args->minorversion == 0)
  1379. return nfs_ok;
  1380. /* This is weird, but OK, not our problem: */
  1381. if (args->opcnt == 0)
  1382. return nfs_ok;
  1383. if (op->status == nfserr_op_illegal)
  1384. return nfs_ok;
  1385. if (!(nfsd4_ops[op->opnum].op_flags & ALLOWED_AS_FIRST_OP))
  1386. return nfserr_op_not_in_session;
  1387. if (op->opnum == OP_SEQUENCE)
  1388. return nfs_ok;
  1389. if (args->opcnt != 1)
  1390. return nfserr_not_only_op;
  1391. return nfs_ok;
  1392. }
  1393. const struct nfsd4_operation *OPDESC(struct nfsd4_op *op)
  1394. {
  1395. return &nfsd4_ops[op->opnum];
  1396. }
  1397. bool nfsd4_cache_this_op(struct nfsd4_op *op)
  1398. {
  1399. if (op->opnum == OP_ILLEGAL)
  1400. return false;
  1401. return OPDESC(op)->op_flags & OP_CACHEME;
  1402. }
  1403. static bool need_wrongsec_check(struct svc_rqst *rqstp)
  1404. {
  1405. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  1406. struct nfsd4_compoundargs *argp = rqstp->rq_argp;
  1407. struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
  1408. struct nfsd4_op *next = &argp->ops[resp->opcnt];
  1409. const struct nfsd4_operation *thisd = OPDESC(this);
  1410. const struct nfsd4_operation *nextd;
  1411. /*
  1412. * Most ops check wronsec on our own; only the putfh-like ops
  1413. * have special rules.
  1414. */
  1415. if (!(thisd->op_flags & OP_IS_PUTFH_LIKE))
  1416. return false;
  1417. /*
  1418. * rfc 5661 2.6.3.1.1.6: don't bother erroring out a
  1419. * put-filehandle operation if we're not going to use the
  1420. * result:
  1421. */
  1422. if (argp->opcnt == resp->opcnt)
  1423. return false;
  1424. if (next->opnum == OP_ILLEGAL)
  1425. return false;
  1426. nextd = OPDESC(next);
  1427. /*
  1428. * Rest of 2.6.3.1.1: certain operations will return WRONGSEC
  1429. * errors themselves as necessary; others should check for them
  1430. * now:
  1431. */
  1432. return !(nextd->op_flags & OP_HANDLES_WRONGSEC);
  1433. }
  1434. static void svcxdr_init_encode(struct svc_rqst *rqstp,
  1435. struct nfsd4_compoundres *resp)
  1436. {
  1437. struct xdr_stream *xdr = &resp->xdr;
  1438. struct xdr_buf *buf = &rqstp->rq_res;
  1439. struct kvec *head = buf->head;
  1440. xdr->buf = buf;
  1441. xdr->iov = head;
  1442. xdr->p = head->iov_base + head->iov_len;
  1443. xdr->end = head->iov_base + PAGE_SIZE - rqstp->rq_auth_slack;
  1444. /* Tail and page_len should be zero at this point: */
  1445. buf->len = buf->head[0].iov_len;
  1446. xdr->scratch.iov_len = 0;
  1447. xdr->page_ptr = buf->pages - 1;
  1448. buf->buflen = PAGE_SIZE * (1 + rqstp->rq_page_end - buf->pages)
  1449. - rqstp->rq_auth_slack;
  1450. }
  1451. /*
  1452. * COMPOUND call.
  1453. */
  1454. static __be32
  1455. nfsd4_proc_compound(struct svc_rqst *rqstp)
  1456. {
  1457. struct nfsd4_compoundargs *args = rqstp->rq_argp;
  1458. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  1459. struct nfsd4_op *op;
  1460. struct nfsd4_compound_state *cstate = &resp->cstate;
  1461. struct svc_fh *current_fh = &cstate->current_fh;
  1462. struct svc_fh *save_fh = &cstate->save_fh;
  1463. __be32 status;
  1464. svcxdr_init_encode(rqstp, resp);
  1465. resp->tagp = resp->xdr.p;
  1466. /* reserve space for: taglen, tag, and opcnt */
  1467. xdr_reserve_space(&resp->xdr, 8 + args->taglen);
  1468. resp->taglen = args->taglen;
  1469. resp->tag = args->tag;
  1470. resp->rqstp = rqstp;
  1471. cstate->minorversion = args->minorversion;
  1472. fh_init(current_fh, NFS4_FHSIZE);
  1473. fh_init(save_fh, NFS4_FHSIZE);
  1474. /*
  1475. * Don't use the deferral mechanism for NFSv4; compounds make it
  1476. * too hard to avoid non-idempotency problems.
  1477. */
  1478. clear_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
  1479. /*
  1480. * According to RFC3010, this takes precedence over all other errors.
  1481. */
  1482. status = nfserr_minor_vers_mismatch;
  1483. if (nfsd_minorversion(args->minorversion, NFSD_TEST) <= 0)
  1484. goto out;
  1485. status = nfserr_resource;
  1486. if (args->opcnt > NFSD_MAX_OPS_PER_COMPOUND)
  1487. goto out;
  1488. status = nfs41_check_op_ordering(args);
  1489. if (status) {
  1490. op = &args->ops[0];
  1491. op->status = status;
  1492. goto encode_op;
  1493. }
  1494. while (!status && resp->opcnt < args->opcnt) {
  1495. op = &args->ops[resp->opcnt++];
  1496. dprintk("nfsv4 compound op #%d/%d: %d (%s)\n",
  1497. resp->opcnt, args->opcnt, op->opnum,
  1498. nfsd4_op_name(op->opnum));
  1499. /*
  1500. * The XDR decode routines may have pre-set op->status;
  1501. * for example, if there is a miscellaneous XDR error
  1502. * it will be set to nfserr_bad_xdr.
  1503. */
  1504. if (op->status) {
  1505. if (op->opnum == OP_OPEN)
  1506. op->status = nfsd4_open_omfg(rqstp, cstate, op);
  1507. goto encode_op;
  1508. }
  1509. if (!current_fh->fh_dentry) {
  1510. if (!(op->opdesc->op_flags & ALLOWED_WITHOUT_FH)) {
  1511. op->status = nfserr_nofilehandle;
  1512. goto encode_op;
  1513. }
  1514. } else if (current_fh->fh_export->ex_fslocs.migrated &&
  1515. !(op->opdesc->op_flags & ALLOWED_ON_ABSENT_FS)) {
  1516. op->status = nfserr_moved;
  1517. goto encode_op;
  1518. }
  1519. fh_clear_wcc(current_fh);
  1520. /* If op is non-idempotent */
  1521. if (op->opdesc->op_flags & OP_MODIFIES_SOMETHING) {
  1522. /*
  1523. * Don't execute this op if we couldn't encode a
  1524. * succesful reply:
  1525. */
  1526. u32 plen = op->opdesc->op_rsize_bop(rqstp, op);
  1527. /*
  1528. * Plus if there's another operation, make sure
  1529. * we'll have space to at least encode an error:
  1530. */
  1531. if (resp->opcnt < args->opcnt)
  1532. plen += COMPOUND_ERR_SLACK_SPACE;
  1533. op->status = nfsd4_check_resp_size(resp, plen);
  1534. }
  1535. if (op->status)
  1536. goto encode_op;
  1537. if (op->opdesc->op_get_currentstateid)
  1538. op->opdesc->op_get_currentstateid(cstate, &op->u);
  1539. op->status = op->opdesc->op_func(rqstp, cstate, &op->u);
  1540. /* Only from SEQUENCE */
  1541. if (cstate->status == nfserr_replay_cache) {
  1542. dprintk("%s NFS4.1 replay from cache\n", __func__);
  1543. status = op->status;
  1544. goto out;
  1545. }
  1546. if (!op->status) {
  1547. if (op->opdesc->op_set_currentstateid)
  1548. op->opdesc->op_set_currentstateid(cstate, &op->u);
  1549. if (op->opdesc->op_flags & OP_CLEAR_STATEID)
  1550. clear_current_stateid(cstate);
  1551. if (need_wrongsec_check(rqstp))
  1552. op->status = check_nfsd_access(current_fh->fh_export, rqstp);
  1553. }
  1554. encode_op:
  1555. if (op->status == nfserr_replay_me) {
  1556. op->replay = &cstate->replay_owner->so_replay;
  1557. nfsd4_encode_replay(&resp->xdr, op);
  1558. status = op->status = op->replay->rp_status;
  1559. } else {
  1560. nfsd4_encode_operation(resp, op);
  1561. status = op->status;
  1562. }
  1563. dprintk("nfsv4 compound op %p opcnt %d #%d: %d: status %d\n",
  1564. args->ops, args->opcnt, resp->opcnt, op->opnum,
  1565. be32_to_cpu(status));
  1566. nfsd4_cstate_clear_replay(cstate);
  1567. nfsd4_increment_op_stats(op->opnum);
  1568. }
  1569. cstate->status = status;
  1570. fh_put(current_fh);
  1571. fh_put(save_fh);
  1572. BUG_ON(cstate->replay_owner);
  1573. out:
  1574. /* Reset deferral mechanism for RPC deferrals */
  1575. set_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
  1576. dprintk("nfsv4 compound returned %d\n", ntohl(status));
  1577. return status;
  1578. }
  1579. #define op_encode_hdr_size (2)
  1580. #define op_encode_stateid_maxsz (XDR_QUADLEN(NFS4_STATEID_SIZE))
  1581. #define op_encode_verifier_maxsz (XDR_QUADLEN(NFS4_VERIFIER_SIZE))
  1582. #define op_encode_change_info_maxsz (5)
  1583. #define nfs4_fattr_bitmap_maxsz (4)
  1584. /* We'll fall back on returning no lockowner if run out of space: */
  1585. #define op_encode_lockowner_maxsz (0)
  1586. #define op_encode_lock_denied_maxsz (8 + op_encode_lockowner_maxsz)
  1587. #define nfs4_owner_maxsz (1 + XDR_QUADLEN(IDMAP_NAMESZ))
  1588. #define op_encode_ace_maxsz (3 + nfs4_owner_maxsz)
  1589. #define op_encode_delegation_maxsz (1 + op_encode_stateid_maxsz + 1 + \
  1590. op_encode_ace_maxsz)
  1591. #define op_encode_channel_attrs_maxsz (6 + 1 + 1)
  1592. static inline u32 nfsd4_only_status_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1593. {
  1594. return (op_encode_hdr_size) * sizeof(__be32);
  1595. }
  1596. static inline u32 nfsd4_status_stateid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1597. {
  1598. return (op_encode_hdr_size + op_encode_stateid_maxsz)* sizeof(__be32);
  1599. }
  1600. static inline u32 nfsd4_access_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1601. {
  1602. /* ac_supported, ac_resp_access */
  1603. return (op_encode_hdr_size + 2)* sizeof(__be32);
  1604. }
  1605. static inline u32 nfsd4_commit_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1606. {
  1607. return (op_encode_hdr_size + op_encode_verifier_maxsz) * sizeof(__be32);
  1608. }
  1609. static inline u32 nfsd4_create_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1610. {
  1611. return (op_encode_hdr_size + op_encode_change_info_maxsz
  1612. + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
  1613. }
  1614. /*
  1615. * Note since this is an idempotent operation we won't insist on failing
  1616. * the op prematurely if the estimate is too large. We may turn off splice
  1617. * reads unnecessarily.
  1618. */
  1619. static inline u32 nfsd4_getattr_rsize(struct svc_rqst *rqstp,
  1620. struct nfsd4_op *op)
  1621. {
  1622. u32 *bmap = op->u.getattr.ga_bmval;
  1623. u32 bmap0 = bmap[0], bmap1 = bmap[1], bmap2 = bmap[2];
  1624. u32 ret = 0;
  1625. if (bmap0 & FATTR4_WORD0_ACL)
  1626. return svc_max_payload(rqstp);
  1627. if (bmap0 & FATTR4_WORD0_FS_LOCATIONS)
  1628. return svc_max_payload(rqstp);
  1629. if (bmap1 & FATTR4_WORD1_OWNER) {
  1630. ret += IDMAP_NAMESZ + 4;
  1631. bmap1 &= ~FATTR4_WORD1_OWNER;
  1632. }
  1633. if (bmap1 & FATTR4_WORD1_OWNER_GROUP) {
  1634. ret += IDMAP_NAMESZ + 4;
  1635. bmap1 &= ~FATTR4_WORD1_OWNER_GROUP;
  1636. }
  1637. if (bmap0 & FATTR4_WORD0_FILEHANDLE) {
  1638. ret += NFS4_FHSIZE + 4;
  1639. bmap0 &= ~FATTR4_WORD0_FILEHANDLE;
  1640. }
  1641. if (bmap2 & FATTR4_WORD2_SECURITY_LABEL) {
  1642. ret += NFS4_MAXLABELLEN + 12;
  1643. bmap2 &= ~FATTR4_WORD2_SECURITY_LABEL;
  1644. }
  1645. /*
  1646. * Largest of remaining attributes are 16 bytes (e.g.,
  1647. * supported_attributes)
  1648. */
  1649. ret += 16 * (hweight32(bmap0) + hweight32(bmap1) + hweight32(bmap2));
  1650. /* bitmask, length */
  1651. ret += 20;
  1652. return ret;
  1653. }
  1654. static inline u32 nfsd4_getfh_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1655. {
  1656. return (op_encode_hdr_size + 1) * sizeof(__be32) + NFS4_FHSIZE;
  1657. }
  1658. static inline u32 nfsd4_link_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1659. {
  1660. return (op_encode_hdr_size + op_encode_change_info_maxsz)
  1661. * sizeof(__be32);
  1662. }
  1663. static inline u32 nfsd4_lock_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1664. {
  1665. return (op_encode_hdr_size + op_encode_lock_denied_maxsz)
  1666. * sizeof(__be32);
  1667. }
  1668. static inline u32 nfsd4_open_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1669. {
  1670. return (op_encode_hdr_size + op_encode_stateid_maxsz
  1671. + op_encode_change_info_maxsz + 1
  1672. + nfs4_fattr_bitmap_maxsz
  1673. + op_encode_delegation_maxsz) * sizeof(__be32);
  1674. }
  1675. static inline u32 nfsd4_read_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1676. {
  1677. u32 maxcount = 0, rlen = 0;
  1678. maxcount = svc_max_payload(rqstp);
  1679. rlen = min(op->u.read.rd_length, maxcount);
  1680. return (op_encode_hdr_size + 2 + XDR_QUADLEN(rlen)) * sizeof(__be32);
  1681. }
  1682. static inline u32 nfsd4_readdir_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1683. {
  1684. u32 maxcount = 0, rlen = 0;
  1685. maxcount = svc_max_payload(rqstp);
  1686. rlen = min(op->u.readdir.rd_maxcount, maxcount);
  1687. return (op_encode_hdr_size + op_encode_verifier_maxsz +
  1688. XDR_QUADLEN(rlen)) * sizeof(__be32);
  1689. }
  1690. static inline u32 nfsd4_readlink_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1691. {
  1692. return (op_encode_hdr_size + 1) * sizeof(__be32) + PAGE_SIZE;
  1693. }
  1694. static inline u32 nfsd4_remove_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1695. {
  1696. return (op_encode_hdr_size + op_encode_change_info_maxsz)
  1697. * sizeof(__be32);
  1698. }
  1699. static inline u32 nfsd4_rename_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1700. {
  1701. return (op_encode_hdr_size + op_encode_change_info_maxsz
  1702. + op_encode_change_info_maxsz) * sizeof(__be32);
  1703. }
  1704. static inline u32 nfsd4_sequence_rsize(struct svc_rqst *rqstp,
  1705. struct nfsd4_op *op)
  1706. {
  1707. return (op_encode_hdr_size
  1708. + XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + 5) * sizeof(__be32);
  1709. }
  1710. static inline u32 nfsd4_test_stateid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1711. {
  1712. return (op_encode_hdr_size + 1 + op->u.test_stateid.ts_num_ids)
  1713. * sizeof(__be32);
  1714. }
  1715. static inline u32 nfsd4_setattr_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1716. {
  1717. return (op_encode_hdr_size + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
  1718. }
  1719. static inline u32 nfsd4_secinfo_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1720. {
  1721. return (op_encode_hdr_size + RPC_AUTH_MAXFLAVOR *
  1722. (4 + XDR_QUADLEN(GSS_OID_MAX_LEN))) * sizeof(__be32);
  1723. }
  1724. static inline u32 nfsd4_setclientid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1725. {
  1726. return (op_encode_hdr_size + 2 + XDR_QUADLEN(NFS4_VERIFIER_SIZE)) *
  1727. sizeof(__be32);
  1728. }
  1729. static inline u32 nfsd4_write_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1730. {
  1731. return (op_encode_hdr_size + 2 + op_encode_verifier_maxsz) * sizeof(__be32);
  1732. }
  1733. static inline u32 nfsd4_exchange_id_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1734. {
  1735. return (op_encode_hdr_size + 2 + 1 + /* eir_clientid, eir_sequenceid */\
  1736. 1 + 1 + /* eir_flags, spr_how */\
  1737. 4 + /* spo_must_enforce & _allow with bitmap */\
  1738. 2 + /*eir_server_owner.so_minor_id */\
  1739. /* eir_server_owner.so_major_id<> */\
  1740. XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
  1741. /* eir_server_scope<> */\
  1742. XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
  1743. 1 + /* eir_server_impl_id array length */\
  1744. 0 /* ignored eir_server_impl_id contents */) * sizeof(__be32);
  1745. }
  1746. static inline u32 nfsd4_bind_conn_to_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1747. {
  1748. return (op_encode_hdr_size + \
  1749. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* bctsr_sessid */\
  1750. 2 /* bctsr_dir, use_conn_in_rdma_mode */) * sizeof(__be32);
  1751. }
  1752. static inline u32 nfsd4_create_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1753. {
  1754. return (op_encode_hdr_size + \
  1755. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* sessionid */\
  1756. 2 + /* csr_sequence, csr_flags */\
  1757. op_encode_channel_attrs_maxsz + \
  1758. op_encode_channel_attrs_maxsz) * sizeof(__be32);
  1759. }
  1760. static inline u32 nfsd4_copy_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1761. {
  1762. return (op_encode_hdr_size +
  1763. 1 /* wr_callback */ +
  1764. op_encode_stateid_maxsz /* wr_callback */ +
  1765. 2 /* wr_count */ +
  1766. 1 /* wr_committed */ +
  1767. op_encode_verifier_maxsz +
  1768. 1 /* cr_consecutive */ +
  1769. 1 /* cr_synchronous */) * sizeof(__be32);
  1770. }
  1771. #ifdef CONFIG_NFSD_PNFS
  1772. static inline u32 nfsd4_getdeviceinfo_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1773. {
  1774. u32 maxcount = 0, rlen = 0;
  1775. maxcount = svc_max_payload(rqstp);
  1776. rlen = min(op->u.getdeviceinfo.gd_maxcount, maxcount);
  1777. return (op_encode_hdr_size +
  1778. 1 /* gd_layout_type*/ +
  1779. XDR_QUADLEN(rlen) +
  1780. 2 /* gd_notify_types */) * sizeof(__be32);
  1781. }
  1782. /*
  1783. * At this stage we don't really know what layout driver will handle the request,
  1784. * so we need to define an arbitrary upper bound here.
  1785. */
  1786. #define MAX_LAYOUT_SIZE 128
  1787. static inline u32 nfsd4_layoutget_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1788. {
  1789. return (op_encode_hdr_size +
  1790. 1 /* logr_return_on_close */ +
  1791. op_encode_stateid_maxsz +
  1792. 1 /* nr of layouts */ +
  1793. MAX_LAYOUT_SIZE) * sizeof(__be32);
  1794. }
  1795. static inline u32 nfsd4_layoutcommit_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1796. {
  1797. return (op_encode_hdr_size +
  1798. 1 /* locr_newsize */ +
  1799. 2 /* ns_size */) * sizeof(__be32);
  1800. }
  1801. static inline u32 nfsd4_layoutreturn_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1802. {
  1803. return (op_encode_hdr_size +
  1804. 1 /* lrs_stateid */ +
  1805. op_encode_stateid_maxsz) * sizeof(__be32);
  1806. }
  1807. #endif /* CONFIG_NFSD_PNFS */
  1808. static inline u32 nfsd4_seek_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1809. {
  1810. return (op_encode_hdr_size + 3) * sizeof(__be32);
  1811. }
  1812. static const struct nfsd4_operation nfsd4_ops[] = {
  1813. [OP_ACCESS] = {
  1814. .op_func = nfsd4_access,
  1815. .op_name = "OP_ACCESS",
  1816. .op_rsize_bop = nfsd4_access_rsize,
  1817. },
  1818. [OP_CLOSE] = {
  1819. .op_func = nfsd4_close,
  1820. .op_flags = OP_MODIFIES_SOMETHING,
  1821. .op_name = "OP_CLOSE",
  1822. .op_rsize_bop = nfsd4_status_stateid_rsize,
  1823. .op_get_currentstateid = nfsd4_get_closestateid,
  1824. .op_set_currentstateid = nfsd4_set_closestateid,
  1825. },
  1826. [OP_COMMIT] = {
  1827. .op_func = nfsd4_commit,
  1828. .op_flags = OP_MODIFIES_SOMETHING,
  1829. .op_name = "OP_COMMIT",
  1830. .op_rsize_bop = nfsd4_commit_rsize,
  1831. },
  1832. [OP_CREATE] = {
  1833. .op_func = nfsd4_create,
  1834. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME | OP_CLEAR_STATEID,
  1835. .op_name = "OP_CREATE",
  1836. .op_rsize_bop = nfsd4_create_rsize,
  1837. },
  1838. [OP_DELEGRETURN] = {
  1839. .op_func = nfsd4_delegreturn,
  1840. .op_flags = OP_MODIFIES_SOMETHING,
  1841. .op_name = "OP_DELEGRETURN",
  1842. .op_rsize_bop = nfsd4_only_status_rsize,
  1843. .op_get_currentstateid = nfsd4_get_delegreturnstateid,
  1844. },
  1845. [OP_GETATTR] = {
  1846. .op_func = nfsd4_getattr,
  1847. .op_flags = ALLOWED_ON_ABSENT_FS,
  1848. .op_rsize_bop = nfsd4_getattr_rsize,
  1849. .op_name = "OP_GETATTR",
  1850. },
  1851. [OP_GETFH] = {
  1852. .op_func = nfsd4_getfh,
  1853. .op_name = "OP_GETFH",
  1854. .op_rsize_bop = nfsd4_getfh_rsize,
  1855. },
  1856. [OP_LINK] = {
  1857. .op_func = nfsd4_link,
  1858. .op_flags = ALLOWED_ON_ABSENT_FS | OP_MODIFIES_SOMETHING
  1859. | OP_CACHEME,
  1860. .op_name = "OP_LINK",
  1861. .op_rsize_bop = nfsd4_link_rsize,
  1862. },
  1863. [OP_LOCK] = {
  1864. .op_func = nfsd4_lock,
  1865. .op_flags = OP_MODIFIES_SOMETHING |
  1866. OP_NONTRIVIAL_ERROR_ENCODE,
  1867. .op_name = "OP_LOCK",
  1868. .op_rsize_bop = nfsd4_lock_rsize,
  1869. .op_set_currentstateid = nfsd4_set_lockstateid,
  1870. },
  1871. [OP_LOCKT] = {
  1872. .op_func = nfsd4_lockt,
  1873. .op_flags = OP_NONTRIVIAL_ERROR_ENCODE,
  1874. .op_name = "OP_LOCKT",
  1875. .op_rsize_bop = nfsd4_lock_rsize,
  1876. },
  1877. [OP_LOCKU] = {
  1878. .op_func = nfsd4_locku,
  1879. .op_flags = OP_MODIFIES_SOMETHING,
  1880. .op_name = "OP_LOCKU",
  1881. .op_rsize_bop = nfsd4_status_stateid_rsize,
  1882. .op_get_currentstateid = nfsd4_get_lockustateid,
  1883. },
  1884. [OP_LOOKUP] = {
  1885. .op_func = nfsd4_lookup,
  1886. .op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
  1887. .op_name = "OP_LOOKUP",
  1888. .op_rsize_bop = nfsd4_only_status_rsize,
  1889. },
  1890. [OP_LOOKUPP] = {
  1891. .op_func = nfsd4_lookupp,
  1892. .op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
  1893. .op_name = "OP_LOOKUPP",
  1894. .op_rsize_bop = nfsd4_only_status_rsize,
  1895. },
  1896. [OP_NVERIFY] = {
  1897. .op_func = nfsd4_nverify,
  1898. .op_name = "OP_NVERIFY",
  1899. .op_rsize_bop = nfsd4_only_status_rsize,
  1900. },
  1901. [OP_OPEN] = {
  1902. .op_func = nfsd4_open,
  1903. .op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
  1904. .op_name = "OP_OPEN",
  1905. .op_rsize_bop = nfsd4_open_rsize,
  1906. .op_set_currentstateid = nfsd4_set_openstateid,
  1907. },
  1908. [OP_OPEN_CONFIRM] = {
  1909. .op_func = nfsd4_open_confirm,
  1910. .op_flags = OP_MODIFIES_SOMETHING,
  1911. .op_name = "OP_OPEN_CONFIRM",
  1912. .op_rsize_bop = nfsd4_status_stateid_rsize,
  1913. },
  1914. [OP_OPEN_DOWNGRADE] = {
  1915. .op_func = nfsd4_open_downgrade,
  1916. .op_flags = OP_MODIFIES_SOMETHING,
  1917. .op_name = "OP_OPEN_DOWNGRADE",
  1918. .op_rsize_bop = nfsd4_status_stateid_rsize,
  1919. .op_get_currentstateid = nfsd4_get_opendowngradestateid,
  1920. .op_set_currentstateid = nfsd4_set_opendowngradestateid,
  1921. },
  1922. [OP_PUTFH] = {
  1923. .op_func = nfsd4_putfh,
  1924. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1925. | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
  1926. .op_name = "OP_PUTFH",
  1927. .op_rsize_bop = nfsd4_only_status_rsize,
  1928. },
  1929. [OP_PUTPUBFH] = {
  1930. .op_func = nfsd4_putrootfh,
  1931. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1932. | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
  1933. .op_name = "OP_PUTPUBFH",
  1934. .op_rsize_bop = nfsd4_only_status_rsize,
  1935. },
  1936. [OP_PUTROOTFH] = {
  1937. .op_func = nfsd4_putrootfh,
  1938. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1939. | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
  1940. .op_name = "OP_PUTROOTFH",
  1941. .op_rsize_bop = nfsd4_only_status_rsize,
  1942. },
  1943. [OP_READ] = {
  1944. .op_func = nfsd4_read,
  1945. .op_release = nfsd4_read_release,
  1946. .op_name = "OP_READ",
  1947. .op_rsize_bop = nfsd4_read_rsize,
  1948. .op_get_currentstateid = nfsd4_get_readstateid,
  1949. },
  1950. [OP_READDIR] = {
  1951. .op_func = nfsd4_readdir,
  1952. .op_name = "OP_READDIR",
  1953. .op_rsize_bop = nfsd4_readdir_rsize,
  1954. },
  1955. [OP_READLINK] = {
  1956. .op_func = nfsd4_readlink,
  1957. .op_name = "OP_READLINK",
  1958. .op_rsize_bop = nfsd4_readlink_rsize,
  1959. },
  1960. [OP_REMOVE] = {
  1961. .op_func = nfsd4_remove,
  1962. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  1963. .op_name = "OP_REMOVE",
  1964. .op_rsize_bop = nfsd4_remove_rsize,
  1965. },
  1966. [OP_RENAME] = {
  1967. .op_func = nfsd4_rename,
  1968. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  1969. .op_name = "OP_RENAME",
  1970. .op_rsize_bop = nfsd4_rename_rsize,
  1971. },
  1972. [OP_RENEW] = {
  1973. .op_func = nfsd4_renew,
  1974. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1975. | OP_MODIFIES_SOMETHING,
  1976. .op_name = "OP_RENEW",
  1977. .op_rsize_bop = nfsd4_only_status_rsize,
  1978. },
  1979. [OP_RESTOREFH] = {
  1980. .op_func = nfsd4_restorefh,
  1981. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1982. | OP_IS_PUTFH_LIKE | OP_MODIFIES_SOMETHING,
  1983. .op_name = "OP_RESTOREFH",
  1984. .op_rsize_bop = nfsd4_only_status_rsize,
  1985. },
  1986. [OP_SAVEFH] = {
  1987. .op_func = nfsd4_savefh,
  1988. .op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
  1989. .op_name = "OP_SAVEFH",
  1990. .op_rsize_bop = nfsd4_only_status_rsize,
  1991. },
  1992. [OP_SECINFO] = {
  1993. .op_func = nfsd4_secinfo,
  1994. .op_release = nfsd4_secinfo_release,
  1995. .op_flags = OP_HANDLES_WRONGSEC,
  1996. .op_name = "OP_SECINFO",
  1997. .op_rsize_bop = nfsd4_secinfo_rsize,
  1998. },
  1999. [OP_SETATTR] = {
  2000. .op_func = nfsd4_setattr,
  2001. .op_name = "OP_SETATTR",
  2002. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME
  2003. | OP_NONTRIVIAL_ERROR_ENCODE,
  2004. .op_rsize_bop = nfsd4_setattr_rsize,
  2005. .op_get_currentstateid = nfsd4_get_setattrstateid,
  2006. },
  2007. [OP_SETCLIENTID] = {
  2008. .op_func = nfsd4_setclientid,
  2009. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  2010. | OP_MODIFIES_SOMETHING | OP_CACHEME
  2011. | OP_NONTRIVIAL_ERROR_ENCODE,
  2012. .op_name = "OP_SETCLIENTID",
  2013. .op_rsize_bop = nfsd4_setclientid_rsize,
  2014. },
  2015. [OP_SETCLIENTID_CONFIRM] = {
  2016. .op_func = nfsd4_setclientid_confirm,
  2017. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  2018. | OP_MODIFIES_SOMETHING | OP_CACHEME,
  2019. .op_name = "OP_SETCLIENTID_CONFIRM",
  2020. .op_rsize_bop = nfsd4_only_status_rsize,
  2021. },
  2022. [OP_VERIFY] = {
  2023. .op_func = nfsd4_verify,
  2024. .op_name = "OP_VERIFY",
  2025. .op_rsize_bop = nfsd4_only_status_rsize,
  2026. },
  2027. [OP_WRITE] = {
  2028. .op_func = nfsd4_write,
  2029. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  2030. .op_name = "OP_WRITE",
  2031. .op_rsize_bop = nfsd4_write_rsize,
  2032. .op_get_currentstateid = nfsd4_get_writestateid,
  2033. },
  2034. [OP_RELEASE_LOCKOWNER] = {
  2035. .op_func = nfsd4_release_lockowner,
  2036. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  2037. | OP_MODIFIES_SOMETHING,
  2038. .op_name = "OP_RELEASE_LOCKOWNER",
  2039. .op_rsize_bop = nfsd4_only_status_rsize,
  2040. },
  2041. /* NFSv4.1 operations */
  2042. [OP_EXCHANGE_ID] = {
  2043. .op_func = nfsd4_exchange_id,
  2044. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
  2045. | OP_MODIFIES_SOMETHING,
  2046. .op_name = "OP_EXCHANGE_ID",
  2047. .op_rsize_bop = nfsd4_exchange_id_rsize,
  2048. },
  2049. [OP_BACKCHANNEL_CTL] = {
  2050. .op_func = nfsd4_backchannel_ctl,
  2051. .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
  2052. .op_name = "OP_BACKCHANNEL_CTL",
  2053. .op_rsize_bop = nfsd4_only_status_rsize,
  2054. },
  2055. [OP_BIND_CONN_TO_SESSION] = {
  2056. .op_func = nfsd4_bind_conn_to_session,
  2057. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
  2058. | OP_MODIFIES_SOMETHING,
  2059. .op_name = "OP_BIND_CONN_TO_SESSION",
  2060. .op_rsize_bop = nfsd4_bind_conn_to_session_rsize,
  2061. },
  2062. [OP_CREATE_SESSION] = {
  2063. .op_func = nfsd4_create_session,
  2064. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
  2065. | OP_MODIFIES_SOMETHING,
  2066. .op_name = "OP_CREATE_SESSION",
  2067. .op_rsize_bop = nfsd4_create_session_rsize,
  2068. },
  2069. [OP_DESTROY_SESSION] = {
  2070. .op_func = nfsd4_destroy_session,
  2071. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
  2072. | OP_MODIFIES_SOMETHING,
  2073. .op_name = "OP_DESTROY_SESSION",
  2074. .op_rsize_bop = nfsd4_only_status_rsize,
  2075. },
  2076. [OP_SEQUENCE] = {
  2077. .op_func = nfsd4_sequence,
  2078. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
  2079. .op_name = "OP_SEQUENCE",
  2080. .op_rsize_bop = nfsd4_sequence_rsize,
  2081. },
  2082. [OP_DESTROY_CLIENTID] = {
  2083. .op_func = nfsd4_destroy_clientid,
  2084. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
  2085. | OP_MODIFIES_SOMETHING,
  2086. .op_name = "OP_DESTROY_CLIENTID",
  2087. .op_rsize_bop = nfsd4_only_status_rsize,
  2088. },
  2089. [OP_RECLAIM_COMPLETE] = {
  2090. .op_func = nfsd4_reclaim_complete,
  2091. .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
  2092. .op_name = "OP_RECLAIM_COMPLETE",
  2093. .op_rsize_bop = nfsd4_only_status_rsize,
  2094. },
  2095. [OP_SECINFO_NO_NAME] = {
  2096. .op_func = nfsd4_secinfo_no_name,
  2097. .op_release = nfsd4_secinfo_no_name_release,
  2098. .op_flags = OP_HANDLES_WRONGSEC,
  2099. .op_name = "OP_SECINFO_NO_NAME",
  2100. .op_rsize_bop = nfsd4_secinfo_rsize,
  2101. },
  2102. [OP_TEST_STATEID] = {
  2103. .op_func = nfsd4_test_stateid,
  2104. .op_flags = ALLOWED_WITHOUT_FH,
  2105. .op_name = "OP_TEST_STATEID",
  2106. .op_rsize_bop = nfsd4_test_stateid_rsize,
  2107. },
  2108. [OP_FREE_STATEID] = {
  2109. .op_func = nfsd4_free_stateid,
  2110. .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
  2111. .op_name = "OP_FREE_STATEID",
  2112. .op_get_currentstateid = nfsd4_get_freestateid,
  2113. .op_rsize_bop = nfsd4_only_status_rsize,
  2114. },
  2115. #ifdef CONFIG_NFSD_PNFS
  2116. [OP_GETDEVICEINFO] = {
  2117. .op_func = nfsd4_getdeviceinfo,
  2118. .op_release = nfsd4_getdeviceinfo_release,
  2119. .op_flags = ALLOWED_WITHOUT_FH,
  2120. .op_name = "OP_GETDEVICEINFO",
  2121. .op_rsize_bop = nfsd4_getdeviceinfo_rsize,
  2122. },
  2123. [OP_LAYOUTGET] = {
  2124. .op_func = nfsd4_layoutget,
  2125. .op_release = nfsd4_layoutget_release,
  2126. .op_flags = OP_MODIFIES_SOMETHING,
  2127. .op_name = "OP_LAYOUTGET",
  2128. .op_rsize_bop = nfsd4_layoutget_rsize,
  2129. },
  2130. [OP_LAYOUTCOMMIT] = {
  2131. .op_func = nfsd4_layoutcommit,
  2132. .op_flags = OP_MODIFIES_SOMETHING,
  2133. .op_name = "OP_LAYOUTCOMMIT",
  2134. .op_rsize_bop = nfsd4_layoutcommit_rsize,
  2135. },
  2136. [OP_LAYOUTRETURN] = {
  2137. .op_func = nfsd4_layoutreturn,
  2138. .op_flags = OP_MODIFIES_SOMETHING,
  2139. .op_name = "OP_LAYOUTRETURN",
  2140. .op_rsize_bop = nfsd4_layoutreturn_rsize,
  2141. },
  2142. #endif /* CONFIG_NFSD_PNFS */
  2143. /* NFSv4.2 operations */
  2144. [OP_ALLOCATE] = {
  2145. .op_func = nfsd4_allocate,
  2146. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  2147. .op_name = "OP_ALLOCATE",
  2148. .op_rsize_bop = nfsd4_only_status_rsize,
  2149. },
  2150. [OP_DEALLOCATE] = {
  2151. .op_func = nfsd4_deallocate,
  2152. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  2153. .op_name = "OP_DEALLOCATE",
  2154. .op_rsize_bop = nfsd4_only_status_rsize,
  2155. },
  2156. [OP_CLONE] = {
  2157. .op_func = nfsd4_clone,
  2158. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  2159. .op_name = "OP_CLONE",
  2160. .op_rsize_bop = nfsd4_only_status_rsize,
  2161. },
  2162. [OP_COPY] = {
  2163. .op_func = nfsd4_copy,
  2164. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  2165. .op_name = "OP_COPY",
  2166. .op_rsize_bop = nfsd4_copy_rsize,
  2167. },
  2168. [OP_SEEK] = {
  2169. .op_func = nfsd4_seek,
  2170. .op_name = "OP_SEEK",
  2171. .op_rsize_bop = nfsd4_seek_rsize,
  2172. },
  2173. };
  2174. /**
  2175. * nfsd4_spo_must_allow - Determine if the compound op contains an
  2176. * operation that is allowed to be sent with machine credentials
  2177. *
  2178. * @rqstp: a pointer to the struct svc_rqst
  2179. *
  2180. * Checks to see if the compound contains a spo_must_allow op
  2181. * and confirms that it was sent with the proper machine creds.
  2182. */
  2183. bool nfsd4_spo_must_allow(struct svc_rqst *rqstp)
  2184. {
  2185. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  2186. struct nfsd4_compoundargs *argp = rqstp->rq_argp;
  2187. struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
  2188. struct nfsd4_compound_state *cstate = &resp->cstate;
  2189. struct nfs4_op_map *allow = &cstate->clp->cl_spo_must_allow;
  2190. u32 opiter;
  2191. if (!cstate->minorversion)
  2192. return false;
  2193. if (cstate->spo_must_allowed == true)
  2194. return true;
  2195. opiter = resp->opcnt;
  2196. while (opiter < argp->opcnt) {
  2197. this = &argp->ops[opiter++];
  2198. if (test_bit(this->opnum, allow->u.longs) &&
  2199. cstate->clp->cl_mach_cred &&
  2200. nfsd4_mach_creds_match(cstate->clp, rqstp)) {
  2201. cstate->spo_must_allowed = true;
  2202. return true;
  2203. }
  2204. }
  2205. cstate->spo_must_allowed = false;
  2206. return false;
  2207. }
  2208. int nfsd4_max_reply(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2209. {
  2210. if (op->opnum == OP_ILLEGAL || op->status == nfserr_notsupp)
  2211. return op_encode_hdr_size * sizeof(__be32);
  2212. BUG_ON(OPDESC(op)->op_rsize_bop == NULL);
  2213. return OPDESC(op)->op_rsize_bop(rqstp, op);
  2214. }
  2215. void warn_on_nonidempotent_op(struct nfsd4_op *op)
  2216. {
  2217. if (OPDESC(op)->op_flags & OP_MODIFIES_SOMETHING) {
  2218. pr_err("unable to encode reply to nonidempotent op %d (%s)\n",
  2219. op->opnum, nfsd4_op_name(op->opnum));
  2220. WARN_ON_ONCE(1);
  2221. }
  2222. }
  2223. static const char *nfsd4_op_name(unsigned opnum)
  2224. {
  2225. if (opnum < ARRAY_SIZE(nfsd4_ops))
  2226. return nfsd4_ops[opnum].op_name;
  2227. return "unknown_operation";
  2228. }
  2229. #define nfsd4_voidres nfsd4_voidargs
  2230. struct nfsd4_voidargs { int dummy; };
  2231. static const struct svc_procedure nfsd_procedures4[2] = {
  2232. [NFSPROC4_NULL] = {
  2233. .pc_func = nfsd4_proc_null,
  2234. .pc_encode = nfs4svc_encode_voidres,
  2235. .pc_argsize = sizeof(struct nfsd4_voidargs),
  2236. .pc_ressize = sizeof(struct nfsd4_voidres),
  2237. .pc_cachetype = RC_NOCACHE,
  2238. .pc_xdrressize = 1,
  2239. },
  2240. [NFSPROC4_COMPOUND] = {
  2241. .pc_func = nfsd4_proc_compound,
  2242. .pc_decode = nfs4svc_decode_compoundargs,
  2243. .pc_encode = nfs4svc_encode_compoundres,
  2244. .pc_argsize = sizeof(struct nfsd4_compoundargs),
  2245. .pc_ressize = sizeof(struct nfsd4_compoundres),
  2246. .pc_release = nfsd4_release_compoundargs,
  2247. .pc_cachetype = RC_NOCACHE,
  2248. .pc_xdrressize = NFSD_BUFSIZE/4,
  2249. },
  2250. };
  2251. static unsigned int nfsd_count3[ARRAY_SIZE(nfsd_procedures4)];
  2252. const struct svc_version nfsd_version4 = {
  2253. .vs_vers = 4,
  2254. .vs_nproc = 2,
  2255. .vs_proc = nfsd_procedures4,
  2256. .vs_count = nfsd_count3,
  2257. .vs_dispatch = nfsd_dispatch,
  2258. .vs_xdrsize = NFS4_SVC_XDRSIZE,
  2259. .vs_rpcb_optnl = true,
  2260. .vs_need_cong_ctrl = true,
  2261. };
  2262. /*
  2263. * Local variables:
  2264. * c-basic-offset: 8
  2265. * End:
  2266. */