nfs4proc.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571
  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. if (!cstate->current_fh.fh_dentry)
  433. return nfserr_nofilehandle;
  434. u->getfh = &cstate->current_fh;
  435. return nfs_ok;
  436. }
  437. static __be32
  438. nfsd4_putfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  439. union nfsd4_op_u *u)
  440. {
  441. struct nfsd4_putfh *putfh = &u->putfh;
  442. fh_put(&cstate->current_fh);
  443. cstate->current_fh.fh_handle.fh_size = putfh->pf_fhlen;
  444. memcpy(&cstate->current_fh.fh_handle.fh_base, putfh->pf_fhval,
  445. putfh->pf_fhlen);
  446. return fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_BYPASS_GSS);
  447. }
  448. static __be32
  449. nfsd4_putrootfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  450. union nfsd4_op_u *u)
  451. {
  452. __be32 status;
  453. fh_put(&cstate->current_fh);
  454. status = exp_pseudoroot(rqstp, &cstate->current_fh);
  455. return status;
  456. }
  457. static __be32
  458. nfsd4_restorefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  459. union nfsd4_op_u *u)
  460. {
  461. if (!cstate->save_fh.fh_dentry)
  462. return nfserr_restorefh;
  463. fh_dup2(&cstate->current_fh, &cstate->save_fh);
  464. if (HAS_STATE_ID(cstate, SAVED_STATE_ID_FLAG)) {
  465. memcpy(&cstate->current_stateid, &cstate->save_stateid, sizeof(stateid_t));
  466. SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
  467. }
  468. return nfs_ok;
  469. }
  470. static __be32
  471. nfsd4_savefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  472. union nfsd4_op_u *u)
  473. {
  474. if (!cstate->current_fh.fh_dentry)
  475. return nfserr_nofilehandle;
  476. fh_dup2(&cstate->save_fh, &cstate->current_fh);
  477. if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG)) {
  478. memcpy(&cstate->save_stateid, &cstate->current_stateid, sizeof(stateid_t));
  479. SET_STATE_ID(cstate, SAVED_STATE_ID_FLAG);
  480. }
  481. return nfs_ok;
  482. }
  483. /*
  484. * misc nfsv4 ops
  485. */
  486. static __be32
  487. nfsd4_access(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  488. union nfsd4_op_u *u)
  489. {
  490. struct nfsd4_access *access = &u->access;
  491. if (access->ac_req_access & ~NFS3_ACCESS_FULL)
  492. return nfserr_inval;
  493. access->ac_resp_access = access->ac_req_access;
  494. return nfsd_access(rqstp, &cstate->current_fh, &access->ac_resp_access,
  495. &access->ac_supported);
  496. }
  497. static void gen_boot_verifier(nfs4_verifier *verifier, struct net *net)
  498. {
  499. __be32 verf[2];
  500. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  501. /*
  502. * This is opaque to client, so no need to byte-swap. Use
  503. * __force to keep sparse happy
  504. */
  505. verf[0] = (__force __be32)nn->nfssvc_boot.tv_sec;
  506. verf[1] = (__force __be32)nn->nfssvc_boot.tv_usec;
  507. memcpy(verifier->data, verf, sizeof(verifier->data));
  508. }
  509. static __be32
  510. nfsd4_commit(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  511. union nfsd4_op_u *u)
  512. {
  513. struct nfsd4_commit *commit = &u->commit;
  514. gen_boot_verifier(&commit->co_verf, SVC_NET(rqstp));
  515. return nfsd_commit(rqstp, &cstate->current_fh, commit->co_offset,
  516. commit->co_count);
  517. }
  518. static __be32
  519. nfsd4_create(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  520. union nfsd4_op_u *u)
  521. {
  522. struct nfsd4_create *create = &u->create;
  523. struct svc_fh resfh;
  524. __be32 status;
  525. dev_t rdev;
  526. fh_init(&resfh, NFS4_FHSIZE);
  527. status = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_NOP);
  528. if (status)
  529. return status;
  530. status = check_attr_support(rqstp, cstate, create->cr_bmval,
  531. nfsd_attrmask);
  532. if (status)
  533. return status;
  534. switch (create->cr_type) {
  535. case NF4LNK:
  536. status = nfsd_symlink(rqstp, &cstate->current_fh,
  537. create->cr_name, create->cr_namelen,
  538. create->cr_data, &resfh);
  539. break;
  540. case NF4BLK:
  541. rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
  542. if (MAJOR(rdev) != create->cr_specdata1 ||
  543. MINOR(rdev) != create->cr_specdata2)
  544. return nfserr_inval;
  545. status = nfsd_create(rqstp, &cstate->current_fh,
  546. create->cr_name, create->cr_namelen,
  547. &create->cr_iattr, S_IFBLK, rdev, &resfh);
  548. break;
  549. case NF4CHR:
  550. rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
  551. if (MAJOR(rdev) != create->cr_specdata1 ||
  552. MINOR(rdev) != create->cr_specdata2)
  553. return nfserr_inval;
  554. status = nfsd_create(rqstp, &cstate->current_fh,
  555. create->cr_name, create->cr_namelen,
  556. &create->cr_iattr,S_IFCHR, rdev, &resfh);
  557. break;
  558. case NF4SOCK:
  559. status = nfsd_create(rqstp, &cstate->current_fh,
  560. create->cr_name, create->cr_namelen,
  561. &create->cr_iattr, S_IFSOCK, 0, &resfh);
  562. break;
  563. case NF4FIFO:
  564. status = nfsd_create(rqstp, &cstate->current_fh,
  565. create->cr_name, create->cr_namelen,
  566. &create->cr_iattr, S_IFIFO, 0, &resfh);
  567. break;
  568. case NF4DIR:
  569. create->cr_iattr.ia_valid &= ~ATTR_SIZE;
  570. status = nfsd_create(rqstp, &cstate->current_fh,
  571. create->cr_name, create->cr_namelen,
  572. &create->cr_iattr, S_IFDIR, 0, &resfh);
  573. break;
  574. default:
  575. status = nfserr_badtype;
  576. }
  577. if (status)
  578. goto out;
  579. if (create->cr_label.len)
  580. nfsd4_security_inode_setsecctx(&resfh, &create->cr_label, create->cr_bmval);
  581. if (create->cr_acl != NULL)
  582. do_set_nfs4_acl(rqstp, &resfh, create->cr_acl,
  583. create->cr_bmval);
  584. fh_unlock(&cstate->current_fh);
  585. set_change_info(&create->cr_cinfo, &cstate->current_fh);
  586. fh_dup2(&cstate->current_fh, &resfh);
  587. out:
  588. fh_put(&resfh);
  589. return status;
  590. }
  591. static __be32
  592. nfsd4_getattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  593. union nfsd4_op_u *u)
  594. {
  595. struct nfsd4_getattr *getattr = &u->getattr;
  596. __be32 status;
  597. status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
  598. if (status)
  599. return status;
  600. if (getattr->ga_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
  601. return nfserr_inval;
  602. getattr->ga_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
  603. getattr->ga_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
  604. getattr->ga_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
  605. getattr->ga_fhp = &cstate->current_fh;
  606. return nfs_ok;
  607. }
  608. static __be32
  609. nfsd4_link(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  610. union nfsd4_op_u *u)
  611. {
  612. struct nfsd4_link *link = &u->link;
  613. __be32 status = nfserr_nofilehandle;
  614. if (!cstate->save_fh.fh_dentry)
  615. return status;
  616. status = nfsd_link(rqstp, &cstate->current_fh,
  617. link->li_name, link->li_namelen, &cstate->save_fh);
  618. if (!status)
  619. set_change_info(&link->li_cinfo, &cstate->current_fh);
  620. return status;
  621. }
  622. static __be32 nfsd4_do_lookupp(struct svc_rqst *rqstp, struct svc_fh *fh)
  623. {
  624. struct svc_fh tmp_fh;
  625. __be32 ret;
  626. fh_init(&tmp_fh, NFS4_FHSIZE);
  627. ret = exp_pseudoroot(rqstp, &tmp_fh);
  628. if (ret)
  629. return ret;
  630. if (tmp_fh.fh_dentry == fh->fh_dentry) {
  631. fh_put(&tmp_fh);
  632. return nfserr_noent;
  633. }
  634. fh_put(&tmp_fh);
  635. return nfsd_lookup(rqstp, fh, "..", 2, fh);
  636. }
  637. static __be32
  638. nfsd4_lookupp(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  639. union nfsd4_op_u *u)
  640. {
  641. return nfsd4_do_lookupp(rqstp, &cstate->current_fh);
  642. }
  643. static __be32
  644. nfsd4_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  645. union nfsd4_op_u *u)
  646. {
  647. return nfsd_lookup(rqstp, &cstate->current_fh,
  648. u->lookup.lo_name, u->lookup.lo_len,
  649. &cstate->current_fh);
  650. }
  651. static __be32
  652. nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  653. union nfsd4_op_u *u)
  654. {
  655. struct nfsd4_read *read = &u->read;
  656. __be32 status;
  657. read->rd_filp = NULL;
  658. if (read->rd_offset >= OFFSET_MAX)
  659. return nfserr_inval;
  660. /*
  661. * If we do a zero copy read, then a client will see read data
  662. * that reflects the state of the file *after* performing the
  663. * following compound.
  664. *
  665. * To ensure proper ordering, we therefore turn off zero copy if
  666. * the client wants us to do more in this compound:
  667. */
  668. if (!nfsd4_last_compound_op(rqstp))
  669. clear_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
  670. /* check stateid */
  671. status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
  672. &read->rd_stateid, RD_STATE,
  673. &read->rd_filp, &read->rd_tmp_file);
  674. if (status) {
  675. dprintk("NFSD: nfsd4_read: couldn't process stateid!\n");
  676. goto out;
  677. }
  678. status = nfs_ok;
  679. out:
  680. read->rd_rqstp = rqstp;
  681. read->rd_fhp = &cstate->current_fh;
  682. return status;
  683. }
  684. static __be32
  685. nfsd4_readdir(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  686. union nfsd4_op_u *u)
  687. {
  688. struct nfsd4_readdir *readdir = &u->readdir;
  689. u64 cookie = readdir->rd_cookie;
  690. static const nfs4_verifier zeroverf;
  691. /* no need to check permission - this will be done in nfsd_readdir() */
  692. if (readdir->rd_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
  693. return nfserr_inval;
  694. readdir->rd_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
  695. readdir->rd_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
  696. readdir->rd_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
  697. if ((cookie == 1) || (cookie == 2) ||
  698. (cookie == 0 && memcmp(readdir->rd_verf.data, zeroverf.data, NFS4_VERIFIER_SIZE)))
  699. return nfserr_bad_cookie;
  700. readdir->rd_rqstp = rqstp;
  701. readdir->rd_fhp = &cstate->current_fh;
  702. return nfs_ok;
  703. }
  704. static __be32
  705. nfsd4_readlink(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  706. union nfsd4_op_u *u)
  707. {
  708. u->readlink.rl_rqstp = rqstp;
  709. u->readlink.rl_fhp = &cstate->current_fh;
  710. return nfs_ok;
  711. }
  712. static __be32
  713. nfsd4_remove(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  714. union nfsd4_op_u *u)
  715. {
  716. struct nfsd4_remove *remove = &u->remove;
  717. __be32 status;
  718. if (opens_in_grace(SVC_NET(rqstp)))
  719. return nfserr_grace;
  720. status = nfsd_unlink(rqstp, &cstate->current_fh, 0,
  721. remove->rm_name, remove->rm_namelen);
  722. if (!status) {
  723. fh_unlock(&cstate->current_fh);
  724. set_change_info(&remove->rm_cinfo, &cstate->current_fh);
  725. }
  726. return status;
  727. }
  728. static __be32
  729. nfsd4_rename(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  730. union nfsd4_op_u *u)
  731. {
  732. struct nfsd4_rename *rename = &u->rename;
  733. __be32 status = nfserr_nofilehandle;
  734. if (!cstate->save_fh.fh_dentry)
  735. return status;
  736. if (opens_in_grace(SVC_NET(rqstp)) &&
  737. !(cstate->save_fh.fh_export->ex_flags & NFSEXP_NOSUBTREECHECK))
  738. return nfserr_grace;
  739. status = nfsd_rename(rqstp, &cstate->save_fh, rename->rn_sname,
  740. rename->rn_snamelen, &cstate->current_fh,
  741. rename->rn_tname, rename->rn_tnamelen);
  742. if (status)
  743. return status;
  744. set_change_info(&rename->rn_sinfo, &cstate->current_fh);
  745. set_change_info(&rename->rn_tinfo, &cstate->save_fh);
  746. return nfs_ok;
  747. }
  748. static __be32
  749. nfsd4_secinfo(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  750. union nfsd4_op_u *u)
  751. {
  752. struct nfsd4_secinfo *secinfo = &u->secinfo;
  753. struct svc_export *exp;
  754. struct dentry *dentry;
  755. __be32 err;
  756. err = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_EXEC);
  757. if (err)
  758. return err;
  759. err = nfsd_lookup_dentry(rqstp, &cstate->current_fh,
  760. secinfo->si_name, secinfo->si_namelen,
  761. &exp, &dentry);
  762. if (err)
  763. return err;
  764. fh_unlock(&cstate->current_fh);
  765. if (d_really_is_negative(dentry)) {
  766. exp_put(exp);
  767. err = nfserr_noent;
  768. } else
  769. secinfo->si_exp = exp;
  770. dput(dentry);
  771. if (cstate->minorversion)
  772. /* See rfc 5661 section 2.6.3.1.1.8 */
  773. fh_put(&cstate->current_fh);
  774. return err;
  775. }
  776. static __be32
  777. nfsd4_secinfo_no_name(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  778. union nfsd4_op_u *u)
  779. {
  780. __be32 err;
  781. switch (u->secinfo_no_name.sin_style) {
  782. case NFS4_SECINFO_STYLE4_CURRENT_FH:
  783. break;
  784. case NFS4_SECINFO_STYLE4_PARENT:
  785. err = nfsd4_do_lookupp(rqstp, &cstate->current_fh);
  786. if (err)
  787. return err;
  788. break;
  789. default:
  790. return nfserr_inval;
  791. }
  792. u->secinfo_no_name.sin_exp = exp_get(cstate->current_fh.fh_export);
  793. fh_put(&cstate->current_fh);
  794. return nfs_ok;
  795. }
  796. static __be32
  797. nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  798. union nfsd4_op_u *u)
  799. {
  800. struct nfsd4_setattr *setattr = &u->setattr;
  801. __be32 status = nfs_ok;
  802. int err;
  803. if (setattr->sa_iattr.ia_valid & ATTR_SIZE) {
  804. status = nfs4_preprocess_stateid_op(rqstp, cstate,
  805. &cstate->current_fh, &setattr->sa_stateid,
  806. WR_STATE, NULL, NULL);
  807. if (status) {
  808. dprintk("NFSD: nfsd4_setattr: couldn't process stateid!\n");
  809. return status;
  810. }
  811. }
  812. err = fh_want_write(&cstate->current_fh);
  813. if (err)
  814. return nfserrno(err);
  815. status = nfs_ok;
  816. status = check_attr_support(rqstp, cstate, setattr->sa_bmval,
  817. nfsd_attrmask);
  818. if (status)
  819. goto out;
  820. if (setattr->sa_acl != NULL)
  821. status = nfsd4_set_nfs4_acl(rqstp, &cstate->current_fh,
  822. setattr->sa_acl);
  823. if (status)
  824. goto out;
  825. if (setattr->sa_label.len)
  826. status = nfsd4_set_nfs4_label(rqstp, &cstate->current_fh,
  827. &setattr->sa_label);
  828. if (status)
  829. goto out;
  830. status = nfsd_setattr(rqstp, &cstate->current_fh, &setattr->sa_iattr,
  831. 0, (time_t)0);
  832. out:
  833. fh_drop_write(&cstate->current_fh);
  834. return status;
  835. }
  836. static int fill_in_write_vector(struct kvec *vec, struct nfsd4_write *write)
  837. {
  838. int i = 1;
  839. int buflen = write->wr_buflen;
  840. vec[0].iov_base = write->wr_head.iov_base;
  841. vec[0].iov_len = min_t(int, buflen, write->wr_head.iov_len);
  842. buflen -= vec[0].iov_len;
  843. while (buflen) {
  844. vec[i].iov_base = page_address(write->wr_pagelist[i - 1]);
  845. vec[i].iov_len = min_t(int, PAGE_SIZE, buflen);
  846. buflen -= vec[i].iov_len;
  847. i++;
  848. }
  849. return i;
  850. }
  851. static __be32
  852. nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  853. union nfsd4_op_u *u)
  854. {
  855. struct nfsd4_write *write = &u->write;
  856. stateid_t *stateid = &write->wr_stateid;
  857. struct file *filp = NULL;
  858. __be32 status = nfs_ok;
  859. unsigned long cnt;
  860. int nvecs;
  861. if (write->wr_offset >= OFFSET_MAX)
  862. return nfserr_inval;
  863. status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
  864. stateid, WR_STATE, &filp, NULL);
  865. if (status) {
  866. dprintk("NFSD: nfsd4_write: couldn't process stateid!\n");
  867. return status;
  868. }
  869. cnt = write->wr_buflen;
  870. write->wr_how_written = write->wr_stable_how;
  871. gen_boot_verifier(&write->wr_verifier, SVC_NET(rqstp));
  872. nvecs = fill_in_write_vector(rqstp->rq_vec, write);
  873. WARN_ON_ONCE(nvecs > ARRAY_SIZE(rqstp->rq_vec));
  874. status = nfsd_vfs_write(rqstp, &cstate->current_fh, filp,
  875. write->wr_offset, rqstp->rq_vec, nvecs, &cnt,
  876. write->wr_how_written);
  877. fput(filp);
  878. write->wr_bytes_written = cnt;
  879. return status;
  880. }
  881. static __be32
  882. nfsd4_verify_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  883. stateid_t *src_stateid, struct file **src,
  884. stateid_t *dst_stateid, struct file **dst)
  885. {
  886. __be32 status;
  887. status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->save_fh,
  888. src_stateid, RD_STATE, src, NULL);
  889. if (status) {
  890. dprintk("NFSD: %s: couldn't process src stateid!\n", __func__);
  891. goto out;
  892. }
  893. status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
  894. dst_stateid, WR_STATE, dst, NULL);
  895. if (status) {
  896. dprintk("NFSD: %s: couldn't process dst stateid!\n", __func__);
  897. goto out_put_src;
  898. }
  899. /* fix up for NFS-specific error code */
  900. if (!S_ISREG(file_inode(*src)->i_mode) ||
  901. !S_ISREG(file_inode(*dst)->i_mode)) {
  902. status = nfserr_wrong_type;
  903. goto out_put_dst;
  904. }
  905. out:
  906. return status;
  907. out_put_dst:
  908. fput(*dst);
  909. out_put_src:
  910. fput(*src);
  911. goto out;
  912. }
  913. static __be32
  914. nfsd4_clone(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  915. union nfsd4_op_u *u)
  916. {
  917. struct nfsd4_clone *clone = &u->clone;
  918. struct file *src, *dst;
  919. __be32 status;
  920. status = nfsd4_verify_copy(rqstp, cstate, &clone->cl_src_stateid, &src,
  921. &clone->cl_dst_stateid, &dst);
  922. if (status)
  923. goto out;
  924. status = nfsd4_clone_file_range(src, clone->cl_src_pos,
  925. dst, clone->cl_dst_pos, clone->cl_count);
  926. fput(dst);
  927. fput(src);
  928. out:
  929. return status;
  930. }
  931. static __be32
  932. nfsd4_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  933. union nfsd4_op_u *u)
  934. {
  935. struct nfsd4_copy *copy = &u->copy;
  936. struct file *src, *dst;
  937. __be32 status;
  938. ssize_t bytes;
  939. status = nfsd4_verify_copy(rqstp, cstate, &copy->cp_src_stateid, &src,
  940. &copy->cp_dst_stateid, &dst);
  941. if (status)
  942. goto out;
  943. bytes = nfsd_copy_file_range(src, copy->cp_src_pos,
  944. dst, copy->cp_dst_pos, copy->cp_count);
  945. if (bytes < 0)
  946. status = nfserrno(bytes);
  947. else {
  948. copy->cp_res.wr_bytes_written = bytes;
  949. copy->cp_res.wr_stable_how = NFS_UNSTABLE;
  950. copy->cp_consecutive = 1;
  951. copy->cp_synchronous = 1;
  952. gen_boot_verifier(&copy->cp_res.wr_verifier, SVC_NET(rqstp));
  953. status = nfs_ok;
  954. }
  955. fput(src);
  956. fput(dst);
  957. out:
  958. return status;
  959. }
  960. static __be32
  961. nfsd4_fallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  962. struct nfsd4_fallocate *fallocate, int flags)
  963. {
  964. __be32 status = nfserr_notsupp;
  965. struct file *file;
  966. status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
  967. &fallocate->falloc_stateid,
  968. WR_STATE, &file, NULL);
  969. if (status != nfs_ok) {
  970. dprintk("NFSD: nfsd4_fallocate: couldn't process stateid!\n");
  971. return status;
  972. }
  973. status = nfsd4_vfs_fallocate(rqstp, &cstate->current_fh, file,
  974. fallocate->falloc_offset,
  975. fallocate->falloc_length,
  976. flags);
  977. fput(file);
  978. return status;
  979. }
  980. static __be32
  981. nfsd4_allocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  982. union nfsd4_op_u *u)
  983. {
  984. return nfsd4_fallocate(rqstp, cstate, &u->allocate, 0);
  985. }
  986. static __be32
  987. nfsd4_deallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  988. union nfsd4_op_u *u)
  989. {
  990. return nfsd4_fallocate(rqstp, cstate, &u->deallocate,
  991. FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
  992. }
  993. static __be32
  994. nfsd4_seek(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  995. union nfsd4_op_u *u)
  996. {
  997. struct nfsd4_seek *seek = &u->seek;
  998. int whence;
  999. __be32 status;
  1000. struct file *file;
  1001. status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
  1002. &seek->seek_stateid,
  1003. RD_STATE, &file, NULL);
  1004. if (status) {
  1005. dprintk("NFSD: nfsd4_seek: couldn't process stateid!\n");
  1006. return status;
  1007. }
  1008. switch (seek->seek_whence) {
  1009. case NFS4_CONTENT_DATA:
  1010. whence = SEEK_DATA;
  1011. break;
  1012. case NFS4_CONTENT_HOLE:
  1013. whence = SEEK_HOLE;
  1014. break;
  1015. default:
  1016. status = nfserr_union_notsupp;
  1017. goto out;
  1018. }
  1019. /*
  1020. * Note: This call does change file->f_pos, but nothing in NFSD
  1021. * should ever file->f_pos.
  1022. */
  1023. seek->seek_pos = vfs_llseek(file, seek->seek_offset, whence);
  1024. if (seek->seek_pos < 0)
  1025. status = nfserrno(seek->seek_pos);
  1026. else if (seek->seek_pos >= i_size_read(file_inode(file)))
  1027. seek->seek_eof = true;
  1028. out:
  1029. fput(file);
  1030. return status;
  1031. }
  1032. /* This routine never returns NFS_OK! If there are no other errors, it
  1033. * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the
  1034. * attributes matched. VERIFY is implemented by mapping NFSERR_SAME
  1035. * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK.
  1036. */
  1037. static __be32
  1038. _nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1039. struct nfsd4_verify *verify)
  1040. {
  1041. __be32 *buf, *p;
  1042. int count;
  1043. __be32 status;
  1044. status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
  1045. if (status)
  1046. return status;
  1047. status = check_attr_support(rqstp, cstate, verify->ve_bmval, NULL);
  1048. if (status)
  1049. return status;
  1050. if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR)
  1051. || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1))
  1052. return nfserr_inval;
  1053. if (verify->ve_attrlen & 3)
  1054. return nfserr_inval;
  1055. /* count in words:
  1056. * bitmap_len(1) + bitmap(2) + attr_len(1) = 4
  1057. */
  1058. count = 4 + (verify->ve_attrlen >> 2);
  1059. buf = kmalloc(count << 2, GFP_KERNEL);
  1060. if (!buf)
  1061. return nfserr_jukebox;
  1062. p = buf;
  1063. status = nfsd4_encode_fattr_to_buf(&p, count, &cstate->current_fh,
  1064. cstate->current_fh.fh_export,
  1065. cstate->current_fh.fh_dentry,
  1066. verify->ve_bmval,
  1067. rqstp, 0);
  1068. /*
  1069. * If nfsd4_encode_fattr() ran out of space, assume that's because
  1070. * the attributes are longer (hence different) than those given:
  1071. */
  1072. if (status == nfserr_resource)
  1073. status = nfserr_not_same;
  1074. if (status)
  1075. goto out_kfree;
  1076. /* skip bitmap */
  1077. p = buf + 1 + ntohl(buf[0]);
  1078. status = nfserr_not_same;
  1079. if (ntohl(*p++) != verify->ve_attrlen)
  1080. goto out_kfree;
  1081. if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen))
  1082. status = nfserr_same;
  1083. out_kfree:
  1084. kfree(buf);
  1085. return status;
  1086. }
  1087. static __be32
  1088. nfsd4_nverify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1089. union nfsd4_op_u *u)
  1090. {
  1091. __be32 status;
  1092. status = _nfsd4_verify(rqstp, cstate, &u->verify);
  1093. return status == nfserr_not_same ? nfs_ok : status;
  1094. }
  1095. static __be32
  1096. nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1097. union nfsd4_op_u *u)
  1098. {
  1099. __be32 status;
  1100. status = _nfsd4_verify(rqstp, cstate, &u->nverify);
  1101. return status == nfserr_same ? nfs_ok : status;
  1102. }
  1103. #ifdef CONFIG_NFSD_PNFS
  1104. static const struct nfsd4_layout_ops *
  1105. nfsd4_layout_verify(struct svc_export *exp, unsigned int layout_type)
  1106. {
  1107. if (!exp->ex_layout_types) {
  1108. dprintk("%s: export does not support pNFS\n", __func__);
  1109. return NULL;
  1110. }
  1111. if (layout_type >= LAYOUT_TYPE_MAX ||
  1112. !(exp->ex_layout_types & (1 << layout_type))) {
  1113. dprintk("%s: layout type %d not supported\n",
  1114. __func__, layout_type);
  1115. return NULL;
  1116. }
  1117. return nfsd4_layout_ops[layout_type];
  1118. }
  1119. static __be32
  1120. nfsd4_getdeviceinfo(struct svc_rqst *rqstp,
  1121. struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
  1122. {
  1123. struct nfsd4_getdeviceinfo *gdp = &u->getdeviceinfo;
  1124. const struct nfsd4_layout_ops *ops;
  1125. struct nfsd4_deviceid_map *map;
  1126. struct svc_export *exp;
  1127. __be32 nfserr;
  1128. dprintk("%s: layout_type %u dev_id [0x%llx:0x%x] maxcnt %u\n",
  1129. __func__,
  1130. gdp->gd_layout_type,
  1131. gdp->gd_devid.fsid_idx, gdp->gd_devid.generation,
  1132. gdp->gd_maxcount);
  1133. map = nfsd4_find_devid_map(gdp->gd_devid.fsid_idx);
  1134. if (!map) {
  1135. dprintk("%s: couldn't find device ID to export mapping!\n",
  1136. __func__);
  1137. return nfserr_noent;
  1138. }
  1139. exp = rqst_exp_find(rqstp, map->fsid_type, map->fsid);
  1140. if (IS_ERR(exp)) {
  1141. dprintk("%s: could not find device id\n", __func__);
  1142. return nfserr_noent;
  1143. }
  1144. nfserr = nfserr_layoutunavailable;
  1145. ops = nfsd4_layout_verify(exp, gdp->gd_layout_type);
  1146. if (!ops)
  1147. goto out;
  1148. nfserr = nfs_ok;
  1149. if (gdp->gd_maxcount != 0) {
  1150. nfserr = ops->proc_getdeviceinfo(exp->ex_path.mnt->mnt_sb,
  1151. rqstp, cstate->session->se_client, gdp);
  1152. }
  1153. gdp->gd_notify_types &= ops->notify_types;
  1154. out:
  1155. exp_put(exp);
  1156. return nfserr;
  1157. }
  1158. static __be32
  1159. nfsd4_layoutget(struct svc_rqst *rqstp,
  1160. struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
  1161. {
  1162. struct nfsd4_layoutget *lgp = &u->layoutget;
  1163. struct svc_fh *current_fh = &cstate->current_fh;
  1164. const struct nfsd4_layout_ops *ops;
  1165. struct nfs4_layout_stateid *ls;
  1166. __be32 nfserr;
  1167. int accmode;
  1168. switch (lgp->lg_seg.iomode) {
  1169. case IOMODE_READ:
  1170. accmode = NFSD_MAY_READ;
  1171. break;
  1172. case IOMODE_RW:
  1173. accmode = NFSD_MAY_READ | NFSD_MAY_WRITE;
  1174. break;
  1175. default:
  1176. dprintk("%s: invalid iomode %d\n",
  1177. __func__, lgp->lg_seg.iomode);
  1178. nfserr = nfserr_badiomode;
  1179. goto out;
  1180. }
  1181. nfserr = fh_verify(rqstp, current_fh, 0, accmode);
  1182. if (nfserr)
  1183. goto out;
  1184. nfserr = nfserr_layoutunavailable;
  1185. ops = nfsd4_layout_verify(current_fh->fh_export, lgp->lg_layout_type);
  1186. if (!ops)
  1187. goto out;
  1188. /*
  1189. * Verify minlength and range as per RFC5661:
  1190. * o If loga_length is less than loga_minlength,
  1191. * the metadata server MUST return NFS4ERR_INVAL.
  1192. * o If the sum of loga_offset and loga_minlength exceeds
  1193. * NFS4_UINT64_MAX, and loga_minlength is not
  1194. * NFS4_UINT64_MAX, the error NFS4ERR_INVAL MUST result.
  1195. * o If the sum of loga_offset and loga_length exceeds
  1196. * NFS4_UINT64_MAX, and loga_length is not NFS4_UINT64_MAX,
  1197. * the error NFS4ERR_INVAL MUST result.
  1198. */
  1199. nfserr = nfserr_inval;
  1200. if (lgp->lg_seg.length < lgp->lg_minlength ||
  1201. (lgp->lg_minlength != NFS4_MAX_UINT64 &&
  1202. lgp->lg_minlength > NFS4_MAX_UINT64 - lgp->lg_seg.offset) ||
  1203. (lgp->lg_seg.length != NFS4_MAX_UINT64 &&
  1204. lgp->lg_seg.length > NFS4_MAX_UINT64 - lgp->lg_seg.offset))
  1205. goto out;
  1206. if (lgp->lg_seg.length == 0)
  1207. goto out;
  1208. nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lgp->lg_sid,
  1209. true, lgp->lg_layout_type, &ls);
  1210. if (nfserr) {
  1211. trace_layout_get_lookup_fail(&lgp->lg_sid);
  1212. goto out;
  1213. }
  1214. nfserr = nfserr_recallconflict;
  1215. if (atomic_read(&ls->ls_stid.sc_file->fi_lo_recalls))
  1216. goto out_put_stid;
  1217. nfserr = ops->proc_layoutget(d_inode(current_fh->fh_dentry),
  1218. current_fh, lgp);
  1219. if (nfserr)
  1220. goto out_put_stid;
  1221. nfserr = nfsd4_insert_layout(lgp, ls);
  1222. out_put_stid:
  1223. mutex_unlock(&ls->ls_mutex);
  1224. nfs4_put_stid(&ls->ls_stid);
  1225. out:
  1226. return nfserr;
  1227. }
  1228. static __be32
  1229. nfsd4_layoutcommit(struct svc_rqst *rqstp,
  1230. struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
  1231. {
  1232. struct nfsd4_layoutcommit *lcp = &u->layoutcommit;
  1233. const struct nfsd4_layout_seg *seg = &lcp->lc_seg;
  1234. struct svc_fh *current_fh = &cstate->current_fh;
  1235. const struct nfsd4_layout_ops *ops;
  1236. loff_t new_size = lcp->lc_last_wr + 1;
  1237. struct inode *inode;
  1238. struct nfs4_layout_stateid *ls;
  1239. __be32 nfserr;
  1240. nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_WRITE);
  1241. if (nfserr)
  1242. goto out;
  1243. nfserr = nfserr_layoutunavailable;
  1244. ops = nfsd4_layout_verify(current_fh->fh_export, lcp->lc_layout_type);
  1245. if (!ops)
  1246. goto out;
  1247. inode = d_inode(current_fh->fh_dentry);
  1248. nfserr = nfserr_inval;
  1249. if (new_size <= seg->offset) {
  1250. dprintk("pnfsd: last write before layout segment\n");
  1251. goto out;
  1252. }
  1253. if (new_size > seg->offset + seg->length) {
  1254. dprintk("pnfsd: last write beyond layout segment\n");
  1255. goto out;
  1256. }
  1257. if (!lcp->lc_newoffset && new_size > i_size_read(inode)) {
  1258. dprintk("pnfsd: layoutcommit beyond EOF\n");
  1259. goto out;
  1260. }
  1261. nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lcp->lc_sid,
  1262. false, lcp->lc_layout_type,
  1263. &ls);
  1264. if (nfserr) {
  1265. trace_layout_commit_lookup_fail(&lcp->lc_sid);
  1266. /* fixup error code as per RFC5661 */
  1267. if (nfserr == nfserr_bad_stateid)
  1268. nfserr = nfserr_badlayout;
  1269. goto out;
  1270. }
  1271. /* LAYOUTCOMMIT does not require any serialization */
  1272. mutex_unlock(&ls->ls_mutex);
  1273. if (new_size > i_size_read(inode)) {
  1274. lcp->lc_size_chg = 1;
  1275. lcp->lc_newsize = new_size;
  1276. } else {
  1277. lcp->lc_size_chg = 0;
  1278. }
  1279. nfserr = ops->proc_layoutcommit(inode, lcp);
  1280. nfs4_put_stid(&ls->ls_stid);
  1281. out:
  1282. return nfserr;
  1283. }
  1284. static __be32
  1285. nfsd4_layoutreturn(struct svc_rqst *rqstp,
  1286. struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
  1287. {
  1288. struct nfsd4_layoutreturn *lrp = &u->layoutreturn;
  1289. struct svc_fh *current_fh = &cstate->current_fh;
  1290. __be32 nfserr;
  1291. nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_NOP);
  1292. if (nfserr)
  1293. goto out;
  1294. nfserr = nfserr_layoutunavailable;
  1295. if (!nfsd4_layout_verify(current_fh->fh_export, lrp->lr_layout_type))
  1296. goto out;
  1297. switch (lrp->lr_seg.iomode) {
  1298. case IOMODE_READ:
  1299. case IOMODE_RW:
  1300. case IOMODE_ANY:
  1301. break;
  1302. default:
  1303. dprintk("%s: invalid iomode %d\n", __func__,
  1304. lrp->lr_seg.iomode);
  1305. nfserr = nfserr_inval;
  1306. goto out;
  1307. }
  1308. switch (lrp->lr_return_type) {
  1309. case RETURN_FILE:
  1310. nfserr = nfsd4_return_file_layouts(rqstp, cstate, lrp);
  1311. break;
  1312. case RETURN_FSID:
  1313. case RETURN_ALL:
  1314. nfserr = nfsd4_return_client_layouts(rqstp, cstate, lrp);
  1315. break;
  1316. default:
  1317. dprintk("%s: invalid return_type %d\n", __func__,
  1318. lrp->lr_return_type);
  1319. nfserr = nfserr_inval;
  1320. break;
  1321. }
  1322. out:
  1323. return nfserr;
  1324. }
  1325. #endif /* CONFIG_NFSD_PNFS */
  1326. /*
  1327. * NULL call.
  1328. */
  1329. static __be32
  1330. nfsd4_proc_null(struct svc_rqst *rqstp)
  1331. {
  1332. return nfs_ok;
  1333. }
  1334. static inline void nfsd4_increment_op_stats(u32 opnum)
  1335. {
  1336. if (opnum >= FIRST_NFS4_OP && opnum <= LAST_NFS4_OP)
  1337. nfsdstats.nfs4_opcount[opnum]++;
  1338. }
  1339. enum nfsd4_op_flags {
  1340. ALLOWED_WITHOUT_FH = 1 << 0, /* No current filehandle required */
  1341. ALLOWED_ON_ABSENT_FS = 1 << 1, /* ops processed on absent fs */
  1342. ALLOWED_AS_FIRST_OP = 1 << 2, /* ops reqired first in compound */
  1343. /* For rfc 5661 section 2.6.3.1.1: */
  1344. OP_HANDLES_WRONGSEC = 1 << 3,
  1345. OP_IS_PUTFH_LIKE = 1 << 4,
  1346. /*
  1347. * These are the ops whose result size we estimate before
  1348. * encoding, to avoid performing an op then not being able to
  1349. * respond or cache a response. This includes writes and setattrs
  1350. * as well as the operations usually called "nonidempotent":
  1351. */
  1352. OP_MODIFIES_SOMETHING = 1 << 5,
  1353. /*
  1354. * Cache compounds containing these ops in the xid-based drc:
  1355. * We use the DRC for compounds containing non-idempotent
  1356. * operations, *except* those that are 4.1-specific (since
  1357. * sessions provide their own EOS), and except for stateful
  1358. * operations other than setclientid and setclientid_confirm
  1359. * (since sequence numbers provide EOS for open, lock, etc in
  1360. * the v4.0 case).
  1361. */
  1362. OP_CACHEME = 1 << 6,
  1363. /*
  1364. * These are ops which clear current state id.
  1365. */
  1366. OP_CLEAR_STATEID = 1 << 7,
  1367. };
  1368. struct nfsd4_operation {
  1369. __be32 (*op_func)(struct svc_rqst *, struct nfsd4_compound_state *,
  1370. union nfsd4_op_u *);
  1371. u32 op_flags;
  1372. char *op_name;
  1373. /* Try to get response size before operation */
  1374. u32 (*op_rsize_bop)(struct svc_rqst *, struct nfsd4_op *);
  1375. void (*op_get_currentstateid)(struct nfsd4_compound_state *,
  1376. union nfsd4_op_u *);
  1377. void (*op_set_currentstateid)(struct nfsd4_compound_state *,
  1378. union nfsd4_op_u *);
  1379. };
  1380. static const struct nfsd4_operation nfsd4_ops[];
  1381. static const char *nfsd4_op_name(unsigned opnum);
  1382. /*
  1383. * Enforce NFSv4.1 COMPOUND ordering rules:
  1384. *
  1385. * Also note, enforced elsewhere:
  1386. * - SEQUENCE other than as first op results in
  1387. * NFS4ERR_SEQUENCE_POS. (Enforced in nfsd4_sequence().)
  1388. * - BIND_CONN_TO_SESSION must be the only op in its compound.
  1389. * (Enforced in nfsd4_bind_conn_to_session().)
  1390. * - DESTROY_SESSION must be the final operation in a compound, if
  1391. * sessionid's in SEQUENCE and DESTROY_SESSION are the same.
  1392. * (Enforced in nfsd4_destroy_session().)
  1393. */
  1394. static __be32 nfs41_check_op_ordering(struct nfsd4_compoundargs *args)
  1395. {
  1396. struct nfsd4_op *op = &args->ops[0];
  1397. /* These ordering requirements don't apply to NFSv4.0: */
  1398. if (args->minorversion == 0)
  1399. return nfs_ok;
  1400. /* This is weird, but OK, not our problem: */
  1401. if (args->opcnt == 0)
  1402. return nfs_ok;
  1403. if (op->status == nfserr_op_illegal)
  1404. return nfs_ok;
  1405. if (!(nfsd4_ops[op->opnum].op_flags & ALLOWED_AS_FIRST_OP))
  1406. return nfserr_op_not_in_session;
  1407. if (op->opnum == OP_SEQUENCE)
  1408. return nfs_ok;
  1409. if (args->opcnt != 1)
  1410. return nfserr_not_only_op;
  1411. return nfs_ok;
  1412. }
  1413. static inline const struct nfsd4_operation *OPDESC(struct nfsd4_op *op)
  1414. {
  1415. return &nfsd4_ops[op->opnum];
  1416. }
  1417. bool nfsd4_cache_this_op(struct nfsd4_op *op)
  1418. {
  1419. if (op->opnum == OP_ILLEGAL)
  1420. return false;
  1421. return OPDESC(op)->op_flags & OP_CACHEME;
  1422. }
  1423. static bool need_wrongsec_check(struct svc_rqst *rqstp)
  1424. {
  1425. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  1426. struct nfsd4_compoundargs *argp = rqstp->rq_argp;
  1427. struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
  1428. struct nfsd4_op *next = &argp->ops[resp->opcnt];
  1429. const struct nfsd4_operation *thisd = OPDESC(this);
  1430. const struct nfsd4_operation *nextd;
  1431. /*
  1432. * Most ops check wronsec on our own; only the putfh-like ops
  1433. * have special rules.
  1434. */
  1435. if (!(thisd->op_flags & OP_IS_PUTFH_LIKE))
  1436. return false;
  1437. /*
  1438. * rfc 5661 2.6.3.1.1.6: don't bother erroring out a
  1439. * put-filehandle operation if we're not going to use the
  1440. * result:
  1441. */
  1442. if (argp->opcnt == resp->opcnt)
  1443. return false;
  1444. if (next->opnum == OP_ILLEGAL)
  1445. return false;
  1446. nextd = OPDESC(next);
  1447. /*
  1448. * Rest of 2.6.3.1.1: certain operations will return WRONGSEC
  1449. * errors themselves as necessary; others should check for them
  1450. * now:
  1451. */
  1452. return !(nextd->op_flags & OP_HANDLES_WRONGSEC);
  1453. }
  1454. static void svcxdr_init_encode(struct svc_rqst *rqstp,
  1455. struct nfsd4_compoundres *resp)
  1456. {
  1457. struct xdr_stream *xdr = &resp->xdr;
  1458. struct xdr_buf *buf = &rqstp->rq_res;
  1459. struct kvec *head = buf->head;
  1460. xdr->buf = buf;
  1461. xdr->iov = head;
  1462. xdr->p = head->iov_base + head->iov_len;
  1463. xdr->end = head->iov_base + PAGE_SIZE - rqstp->rq_auth_slack;
  1464. /* Tail and page_len should be zero at this point: */
  1465. buf->len = buf->head[0].iov_len;
  1466. xdr->scratch.iov_len = 0;
  1467. xdr->page_ptr = buf->pages - 1;
  1468. buf->buflen = PAGE_SIZE * (1 + rqstp->rq_page_end - buf->pages)
  1469. - rqstp->rq_auth_slack;
  1470. }
  1471. /*
  1472. * COMPOUND call.
  1473. */
  1474. static __be32
  1475. nfsd4_proc_compound(struct svc_rqst *rqstp)
  1476. {
  1477. struct nfsd4_compoundargs *args = rqstp->rq_argp;
  1478. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  1479. struct nfsd4_op *op;
  1480. const struct nfsd4_operation *opdesc;
  1481. struct nfsd4_compound_state *cstate = &resp->cstate;
  1482. struct svc_fh *current_fh = &cstate->current_fh;
  1483. struct svc_fh *save_fh = &cstate->save_fh;
  1484. __be32 status;
  1485. svcxdr_init_encode(rqstp, resp);
  1486. resp->tagp = resp->xdr.p;
  1487. /* reserve space for: taglen, tag, and opcnt */
  1488. xdr_reserve_space(&resp->xdr, 8 + args->taglen);
  1489. resp->taglen = args->taglen;
  1490. resp->tag = args->tag;
  1491. resp->rqstp = rqstp;
  1492. cstate->minorversion = args->minorversion;
  1493. fh_init(current_fh, NFS4_FHSIZE);
  1494. fh_init(save_fh, NFS4_FHSIZE);
  1495. /*
  1496. * Don't use the deferral mechanism for NFSv4; compounds make it
  1497. * too hard to avoid non-idempotency problems.
  1498. */
  1499. clear_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
  1500. /*
  1501. * According to RFC3010, this takes precedence over all other errors.
  1502. */
  1503. status = nfserr_minor_vers_mismatch;
  1504. if (nfsd_minorversion(args->minorversion, NFSD_TEST) <= 0)
  1505. goto out;
  1506. status = nfs41_check_op_ordering(args);
  1507. if (status) {
  1508. op = &args->ops[0];
  1509. op->status = status;
  1510. goto encode_op;
  1511. }
  1512. while (!status && resp->opcnt < args->opcnt) {
  1513. op = &args->ops[resp->opcnt++];
  1514. dprintk("nfsv4 compound op #%d/%d: %d (%s)\n",
  1515. resp->opcnt, args->opcnt, op->opnum,
  1516. nfsd4_op_name(op->opnum));
  1517. /*
  1518. * The XDR decode routines may have pre-set op->status;
  1519. * for example, if there is a miscellaneous XDR error
  1520. * it will be set to nfserr_bad_xdr.
  1521. */
  1522. if (op->status) {
  1523. if (op->opnum == OP_OPEN)
  1524. op->status = nfsd4_open_omfg(rqstp, cstate, op);
  1525. goto encode_op;
  1526. }
  1527. opdesc = OPDESC(op);
  1528. if (!current_fh->fh_dentry) {
  1529. if (!(opdesc->op_flags & ALLOWED_WITHOUT_FH)) {
  1530. op->status = nfserr_nofilehandle;
  1531. goto encode_op;
  1532. }
  1533. } else if (current_fh->fh_export->ex_fslocs.migrated &&
  1534. !(opdesc->op_flags & ALLOWED_ON_ABSENT_FS)) {
  1535. op->status = nfserr_moved;
  1536. goto encode_op;
  1537. }
  1538. fh_clear_wcc(current_fh);
  1539. /* If op is non-idempotent */
  1540. if (opdesc->op_flags & OP_MODIFIES_SOMETHING) {
  1541. /*
  1542. * Don't execute this op if we couldn't encode a
  1543. * succesful reply:
  1544. */
  1545. u32 plen = opdesc->op_rsize_bop(rqstp, op);
  1546. /*
  1547. * Plus if there's another operation, make sure
  1548. * we'll have space to at least encode an error:
  1549. */
  1550. if (resp->opcnt < args->opcnt)
  1551. plen += COMPOUND_ERR_SLACK_SPACE;
  1552. op->status = nfsd4_check_resp_size(resp, plen);
  1553. }
  1554. if (op->status)
  1555. goto encode_op;
  1556. if (opdesc->op_get_currentstateid)
  1557. opdesc->op_get_currentstateid(cstate, &op->u);
  1558. op->status = opdesc->op_func(rqstp, cstate, &op->u);
  1559. /* Only from SEQUENCE */
  1560. if (cstate->status == nfserr_replay_cache) {
  1561. dprintk("%s NFS4.1 replay from cache\n", __func__);
  1562. status = op->status;
  1563. goto out;
  1564. }
  1565. if (!op->status) {
  1566. if (opdesc->op_set_currentstateid)
  1567. opdesc->op_set_currentstateid(cstate, &op->u);
  1568. if (opdesc->op_flags & OP_CLEAR_STATEID)
  1569. clear_current_stateid(cstate);
  1570. if (need_wrongsec_check(rqstp))
  1571. op->status = check_nfsd_access(current_fh->fh_export, rqstp);
  1572. }
  1573. encode_op:
  1574. if (op->status == nfserr_replay_me) {
  1575. op->replay = &cstate->replay_owner->so_replay;
  1576. nfsd4_encode_replay(&resp->xdr, op);
  1577. status = op->status = op->replay->rp_status;
  1578. } else {
  1579. nfsd4_encode_operation(resp, op);
  1580. status = op->status;
  1581. }
  1582. dprintk("nfsv4 compound op %p opcnt %d #%d: %d: status %d\n",
  1583. args->ops, args->opcnt, resp->opcnt, op->opnum,
  1584. be32_to_cpu(status));
  1585. nfsd4_cstate_clear_replay(cstate);
  1586. nfsd4_increment_op_stats(op->opnum);
  1587. }
  1588. cstate->status = status;
  1589. fh_put(current_fh);
  1590. fh_put(save_fh);
  1591. BUG_ON(cstate->replay_owner);
  1592. out:
  1593. /* Reset deferral mechanism for RPC deferrals */
  1594. set_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
  1595. dprintk("nfsv4 compound returned %d\n", ntohl(status));
  1596. return status;
  1597. }
  1598. #define op_encode_hdr_size (2)
  1599. #define op_encode_stateid_maxsz (XDR_QUADLEN(NFS4_STATEID_SIZE))
  1600. #define op_encode_verifier_maxsz (XDR_QUADLEN(NFS4_VERIFIER_SIZE))
  1601. #define op_encode_change_info_maxsz (5)
  1602. #define nfs4_fattr_bitmap_maxsz (4)
  1603. /* We'll fall back on returning no lockowner if run out of space: */
  1604. #define op_encode_lockowner_maxsz (0)
  1605. #define op_encode_lock_denied_maxsz (8 + op_encode_lockowner_maxsz)
  1606. #define nfs4_owner_maxsz (1 + XDR_QUADLEN(IDMAP_NAMESZ))
  1607. #define op_encode_ace_maxsz (3 + nfs4_owner_maxsz)
  1608. #define op_encode_delegation_maxsz (1 + op_encode_stateid_maxsz + 1 + \
  1609. op_encode_ace_maxsz)
  1610. #define op_encode_channel_attrs_maxsz (6 + 1 + 1)
  1611. static inline u32 nfsd4_only_status_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1612. {
  1613. return (op_encode_hdr_size) * sizeof(__be32);
  1614. }
  1615. static inline u32 nfsd4_status_stateid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1616. {
  1617. return (op_encode_hdr_size + op_encode_stateid_maxsz)* sizeof(__be32);
  1618. }
  1619. static inline u32 nfsd4_access_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1620. {
  1621. /* ac_supported, ac_resp_access */
  1622. return (op_encode_hdr_size + 2)* sizeof(__be32);
  1623. }
  1624. static inline u32 nfsd4_commit_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1625. {
  1626. return (op_encode_hdr_size + op_encode_verifier_maxsz) * sizeof(__be32);
  1627. }
  1628. static inline u32 nfsd4_create_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1629. {
  1630. return (op_encode_hdr_size + op_encode_change_info_maxsz
  1631. + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
  1632. }
  1633. /*
  1634. * Note since this is an idempotent operation we won't insist on failing
  1635. * the op prematurely if the estimate is too large. We may turn off splice
  1636. * reads unnecessarily.
  1637. */
  1638. static inline u32 nfsd4_getattr_rsize(struct svc_rqst *rqstp,
  1639. struct nfsd4_op *op)
  1640. {
  1641. u32 *bmap = op->u.getattr.ga_bmval;
  1642. u32 bmap0 = bmap[0], bmap1 = bmap[1], bmap2 = bmap[2];
  1643. u32 ret = 0;
  1644. if (bmap0 & FATTR4_WORD0_ACL)
  1645. return svc_max_payload(rqstp);
  1646. if (bmap0 & FATTR4_WORD0_FS_LOCATIONS)
  1647. return svc_max_payload(rqstp);
  1648. if (bmap1 & FATTR4_WORD1_OWNER) {
  1649. ret += IDMAP_NAMESZ + 4;
  1650. bmap1 &= ~FATTR4_WORD1_OWNER;
  1651. }
  1652. if (bmap1 & FATTR4_WORD1_OWNER_GROUP) {
  1653. ret += IDMAP_NAMESZ + 4;
  1654. bmap1 &= ~FATTR4_WORD1_OWNER_GROUP;
  1655. }
  1656. if (bmap0 & FATTR4_WORD0_FILEHANDLE) {
  1657. ret += NFS4_FHSIZE + 4;
  1658. bmap0 &= ~FATTR4_WORD0_FILEHANDLE;
  1659. }
  1660. if (bmap2 & FATTR4_WORD2_SECURITY_LABEL) {
  1661. ret += NFS4_MAXLABELLEN + 12;
  1662. bmap2 &= ~FATTR4_WORD2_SECURITY_LABEL;
  1663. }
  1664. /*
  1665. * Largest of remaining attributes are 16 bytes (e.g.,
  1666. * supported_attributes)
  1667. */
  1668. ret += 16 * (hweight32(bmap0) + hweight32(bmap1) + hweight32(bmap2));
  1669. /* bitmask, length */
  1670. ret += 20;
  1671. return ret;
  1672. }
  1673. static inline u32 nfsd4_getfh_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1674. {
  1675. return (op_encode_hdr_size + 1) * sizeof(__be32) + NFS4_FHSIZE;
  1676. }
  1677. static inline u32 nfsd4_link_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1678. {
  1679. return (op_encode_hdr_size + op_encode_change_info_maxsz)
  1680. * sizeof(__be32);
  1681. }
  1682. static inline u32 nfsd4_lock_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1683. {
  1684. return (op_encode_hdr_size + op_encode_lock_denied_maxsz)
  1685. * sizeof(__be32);
  1686. }
  1687. static inline u32 nfsd4_open_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1688. {
  1689. return (op_encode_hdr_size + op_encode_stateid_maxsz
  1690. + op_encode_change_info_maxsz + 1
  1691. + nfs4_fattr_bitmap_maxsz
  1692. + op_encode_delegation_maxsz) * sizeof(__be32);
  1693. }
  1694. static inline u32 nfsd4_read_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1695. {
  1696. u32 maxcount = 0, rlen = 0;
  1697. maxcount = svc_max_payload(rqstp);
  1698. rlen = min(op->u.read.rd_length, maxcount);
  1699. return (op_encode_hdr_size + 2 + XDR_QUADLEN(rlen)) * sizeof(__be32);
  1700. }
  1701. static inline u32 nfsd4_readdir_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1702. {
  1703. u32 maxcount = 0, rlen = 0;
  1704. maxcount = svc_max_payload(rqstp);
  1705. rlen = min(op->u.readdir.rd_maxcount, maxcount);
  1706. return (op_encode_hdr_size + op_encode_verifier_maxsz +
  1707. XDR_QUADLEN(rlen)) * sizeof(__be32);
  1708. }
  1709. static inline u32 nfsd4_readlink_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1710. {
  1711. return (op_encode_hdr_size + 1) * sizeof(__be32) + PAGE_SIZE;
  1712. }
  1713. static inline u32 nfsd4_remove_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1714. {
  1715. return (op_encode_hdr_size + op_encode_change_info_maxsz)
  1716. * sizeof(__be32);
  1717. }
  1718. static inline u32 nfsd4_rename_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1719. {
  1720. return (op_encode_hdr_size + op_encode_change_info_maxsz
  1721. + op_encode_change_info_maxsz) * sizeof(__be32);
  1722. }
  1723. static inline u32 nfsd4_sequence_rsize(struct svc_rqst *rqstp,
  1724. struct nfsd4_op *op)
  1725. {
  1726. return (op_encode_hdr_size
  1727. + XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + 5) * sizeof(__be32);
  1728. }
  1729. static inline u32 nfsd4_test_stateid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1730. {
  1731. return (op_encode_hdr_size + 1 + op->u.test_stateid.ts_num_ids)
  1732. * sizeof(__be32);
  1733. }
  1734. static inline u32 nfsd4_setattr_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1735. {
  1736. return (op_encode_hdr_size + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
  1737. }
  1738. static inline u32 nfsd4_secinfo_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1739. {
  1740. return (op_encode_hdr_size + RPC_AUTH_MAXFLAVOR *
  1741. (4 + XDR_QUADLEN(GSS_OID_MAX_LEN))) * sizeof(__be32);
  1742. }
  1743. static inline u32 nfsd4_setclientid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1744. {
  1745. return (op_encode_hdr_size + 2 + XDR_QUADLEN(NFS4_VERIFIER_SIZE)) *
  1746. sizeof(__be32);
  1747. }
  1748. static inline u32 nfsd4_write_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1749. {
  1750. return (op_encode_hdr_size + 2 + op_encode_verifier_maxsz) * sizeof(__be32);
  1751. }
  1752. static inline u32 nfsd4_exchange_id_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1753. {
  1754. return (op_encode_hdr_size + 2 + 1 + /* eir_clientid, eir_sequenceid */\
  1755. 1 + 1 + /* eir_flags, spr_how */\
  1756. 4 + /* spo_must_enforce & _allow with bitmap */\
  1757. 2 + /*eir_server_owner.so_minor_id */\
  1758. /* eir_server_owner.so_major_id<> */\
  1759. XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
  1760. /* eir_server_scope<> */\
  1761. XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
  1762. 1 + /* eir_server_impl_id array length */\
  1763. 0 /* ignored eir_server_impl_id contents */) * sizeof(__be32);
  1764. }
  1765. static inline u32 nfsd4_bind_conn_to_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1766. {
  1767. return (op_encode_hdr_size + \
  1768. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* bctsr_sessid */\
  1769. 2 /* bctsr_dir, use_conn_in_rdma_mode */) * sizeof(__be32);
  1770. }
  1771. static inline u32 nfsd4_create_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1772. {
  1773. return (op_encode_hdr_size + \
  1774. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* sessionid */\
  1775. 2 + /* csr_sequence, csr_flags */\
  1776. op_encode_channel_attrs_maxsz + \
  1777. op_encode_channel_attrs_maxsz) * sizeof(__be32);
  1778. }
  1779. static inline u32 nfsd4_copy_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1780. {
  1781. return (op_encode_hdr_size +
  1782. 1 /* wr_callback */ +
  1783. op_encode_stateid_maxsz /* wr_callback */ +
  1784. 2 /* wr_count */ +
  1785. 1 /* wr_committed */ +
  1786. op_encode_verifier_maxsz +
  1787. 1 /* cr_consecutive */ +
  1788. 1 /* cr_synchronous */) * sizeof(__be32);
  1789. }
  1790. #ifdef CONFIG_NFSD_PNFS
  1791. static inline u32 nfsd4_getdeviceinfo_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1792. {
  1793. u32 maxcount = 0, rlen = 0;
  1794. maxcount = svc_max_payload(rqstp);
  1795. rlen = min(op->u.getdeviceinfo.gd_maxcount, maxcount);
  1796. return (op_encode_hdr_size +
  1797. 1 /* gd_layout_type*/ +
  1798. XDR_QUADLEN(rlen) +
  1799. 2 /* gd_notify_types */) * sizeof(__be32);
  1800. }
  1801. /*
  1802. * At this stage we don't really know what layout driver will handle the request,
  1803. * so we need to define an arbitrary upper bound here.
  1804. */
  1805. #define MAX_LAYOUT_SIZE 128
  1806. static inline u32 nfsd4_layoutget_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1807. {
  1808. return (op_encode_hdr_size +
  1809. 1 /* logr_return_on_close */ +
  1810. op_encode_stateid_maxsz +
  1811. 1 /* nr of layouts */ +
  1812. MAX_LAYOUT_SIZE) * sizeof(__be32);
  1813. }
  1814. static inline u32 nfsd4_layoutcommit_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1815. {
  1816. return (op_encode_hdr_size +
  1817. 1 /* locr_newsize */ +
  1818. 2 /* ns_size */) * sizeof(__be32);
  1819. }
  1820. static inline u32 nfsd4_layoutreturn_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1821. {
  1822. return (op_encode_hdr_size +
  1823. 1 /* lrs_stateid */ +
  1824. op_encode_stateid_maxsz) * sizeof(__be32);
  1825. }
  1826. #endif /* CONFIG_NFSD_PNFS */
  1827. static inline u32 nfsd4_seek_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1828. {
  1829. return (op_encode_hdr_size + 3) * sizeof(__be32);
  1830. }
  1831. static const struct nfsd4_operation nfsd4_ops[] = {
  1832. [OP_ACCESS] = {
  1833. .op_func = nfsd4_access,
  1834. .op_name = "OP_ACCESS",
  1835. .op_rsize_bop = nfsd4_access_rsize,
  1836. },
  1837. [OP_CLOSE] = {
  1838. .op_func = nfsd4_close,
  1839. .op_flags = OP_MODIFIES_SOMETHING,
  1840. .op_name = "OP_CLOSE",
  1841. .op_rsize_bop = nfsd4_status_stateid_rsize,
  1842. .op_get_currentstateid = nfsd4_get_closestateid,
  1843. .op_set_currentstateid = nfsd4_set_closestateid,
  1844. },
  1845. [OP_COMMIT] = {
  1846. .op_func = nfsd4_commit,
  1847. .op_flags = OP_MODIFIES_SOMETHING,
  1848. .op_name = "OP_COMMIT",
  1849. .op_rsize_bop = nfsd4_commit_rsize,
  1850. },
  1851. [OP_CREATE] = {
  1852. .op_func = nfsd4_create,
  1853. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME | OP_CLEAR_STATEID,
  1854. .op_name = "OP_CREATE",
  1855. .op_rsize_bop = nfsd4_create_rsize,
  1856. },
  1857. [OP_DELEGRETURN] = {
  1858. .op_func = nfsd4_delegreturn,
  1859. .op_flags = OP_MODIFIES_SOMETHING,
  1860. .op_name = "OP_DELEGRETURN",
  1861. .op_rsize_bop = nfsd4_only_status_rsize,
  1862. .op_get_currentstateid = nfsd4_get_delegreturnstateid,
  1863. },
  1864. [OP_GETATTR] = {
  1865. .op_func = nfsd4_getattr,
  1866. .op_flags = ALLOWED_ON_ABSENT_FS,
  1867. .op_rsize_bop = nfsd4_getattr_rsize,
  1868. .op_name = "OP_GETATTR",
  1869. },
  1870. [OP_GETFH] = {
  1871. .op_func = nfsd4_getfh,
  1872. .op_name = "OP_GETFH",
  1873. .op_rsize_bop = nfsd4_getfh_rsize,
  1874. },
  1875. [OP_LINK] = {
  1876. .op_func = nfsd4_link,
  1877. .op_flags = ALLOWED_ON_ABSENT_FS | OP_MODIFIES_SOMETHING
  1878. | OP_CACHEME,
  1879. .op_name = "OP_LINK",
  1880. .op_rsize_bop = nfsd4_link_rsize,
  1881. },
  1882. [OP_LOCK] = {
  1883. .op_func = nfsd4_lock,
  1884. .op_flags = OP_MODIFIES_SOMETHING,
  1885. .op_name = "OP_LOCK",
  1886. .op_rsize_bop = nfsd4_lock_rsize,
  1887. .op_set_currentstateid = nfsd4_set_lockstateid,
  1888. },
  1889. [OP_LOCKT] = {
  1890. .op_func = nfsd4_lockt,
  1891. .op_name = "OP_LOCKT",
  1892. .op_rsize_bop = nfsd4_lock_rsize,
  1893. },
  1894. [OP_LOCKU] = {
  1895. .op_func = nfsd4_locku,
  1896. .op_flags = OP_MODIFIES_SOMETHING,
  1897. .op_name = "OP_LOCKU",
  1898. .op_rsize_bop = nfsd4_status_stateid_rsize,
  1899. .op_get_currentstateid = nfsd4_get_lockustateid,
  1900. },
  1901. [OP_LOOKUP] = {
  1902. .op_func = nfsd4_lookup,
  1903. .op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
  1904. .op_name = "OP_LOOKUP",
  1905. .op_rsize_bop = nfsd4_only_status_rsize,
  1906. },
  1907. [OP_LOOKUPP] = {
  1908. .op_func = nfsd4_lookupp,
  1909. .op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
  1910. .op_name = "OP_LOOKUPP",
  1911. .op_rsize_bop = nfsd4_only_status_rsize,
  1912. },
  1913. [OP_NVERIFY] = {
  1914. .op_func = nfsd4_nverify,
  1915. .op_name = "OP_NVERIFY",
  1916. .op_rsize_bop = nfsd4_only_status_rsize,
  1917. },
  1918. [OP_OPEN] = {
  1919. .op_func = nfsd4_open,
  1920. .op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
  1921. .op_name = "OP_OPEN",
  1922. .op_rsize_bop = nfsd4_open_rsize,
  1923. .op_set_currentstateid = nfsd4_set_openstateid,
  1924. },
  1925. [OP_OPEN_CONFIRM] = {
  1926. .op_func = nfsd4_open_confirm,
  1927. .op_flags = OP_MODIFIES_SOMETHING,
  1928. .op_name = "OP_OPEN_CONFIRM",
  1929. .op_rsize_bop = nfsd4_status_stateid_rsize,
  1930. },
  1931. [OP_OPEN_DOWNGRADE] = {
  1932. .op_func = nfsd4_open_downgrade,
  1933. .op_flags = OP_MODIFIES_SOMETHING,
  1934. .op_name = "OP_OPEN_DOWNGRADE",
  1935. .op_rsize_bop = nfsd4_status_stateid_rsize,
  1936. .op_get_currentstateid = nfsd4_get_opendowngradestateid,
  1937. .op_set_currentstateid = nfsd4_set_opendowngradestateid,
  1938. },
  1939. [OP_PUTFH] = {
  1940. .op_func = nfsd4_putfh,
  1941. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1942. | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
  1943. .op_name = "OP_PUTFH",
  1944. .op_rsize_bop = nfsd4_only_status_rsize,
  1945. },
  1946. [OP_PUTPUBFH] = {
  1947. .op_func = nfsd4_putrootfh,
  1948. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1949. | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
  1950. .op_name = "OP_PUTPUBFH",
  1951. .op_rsize_bop = nfsd4_only_status_rsize,
  1952. },
  1953. [OP_PUTROOTFH] = {
  1954. .op_func = nfsd4_putrootfh,
  1955. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1956. | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
  1957. .op_name = "OP_PUTROOTFH",
  1958. .op_rsize_bop = nfsd4_only_status_rsize,
  1959. },
  1960. [OP_READ] = {
  1961. .op_func = nfsd4_read,
  1962. .op_name = "OP_READ",
  1963. .op_rsize_bop = nfsd4_read_rsize,
  1964. .op_get_currentstateid = nfsd4_get_readstateid,
  1965. },
  1966. [OP_READDIR] = {
  1967. .op_func = nfsd4_readdir,
  1968. .op_name = "OP_READDIR",
  1969. .op_rsize_bop = nfsd4_readdir_rsize,
  1970. },
  1971. [OP_READLINK] = {
  1972. .op_func = nfsd4_readlink,
  1973. .op_name = "OP_READLINK",
  1974. .op_rsize_bop = nfsd4_readlink_rsize,
  1975. },
  1976. [OP_REMOVE] = {
  1977. .op_func = nfsd4_remove,
  1978. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  1979. .op_name = "OP_REMOVE",
  1980. .op_rsize_bop = nfsd4_remove_rsize,
  1981. },
  1982. [OP_RENAME] = {
  1983. .op_func = nfsd4_rename,
  1984. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  1985. .op_name = "OP_RENAME",
  1986. .op_rsize_bop = nfsd4_rename_rsize,
  1987. },
  1988. [OP_RENEW] = {
  1989. .op_func = nfsd4_renew,
  1990. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1991. | OP_MODIFIES_SOMETHING,
  1992. .op_name = "OP_RENEW",
  1993. .op_rsize_bop = nfsd4_only_status_rsize,
  1994. },
  1995. [OP_RESTOREFH] = {
  1996. .op_func = nfsd4_restorefh,
  1997. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1998. | OP_IS_PUTFH_LIKE | OP_MODIFIES_SOMETHING,
  1999. .op_name = "OP_RESTOREFH",
  2000. .op_rsize_bop = nfsd4_only_status_rsize,
  2001. },
  2002. [OP_SAVEFH] = {
  2003. .op_func = nfsd4_savefh,
  2004. .op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
  2005. .op_name = "OP_SAVEFH",
  2006. .op_rsize_bop = nfsd4_only_status_rsize,
  2007. },
  2008. [OP_SECINFO] = {
  2009. .op_func = nfsd4_secinfo,
  2010. .op_flags = OP_HANDLES_WRONGSEC,
  2011. .op_name = "OP_SECINFO",
  2012. .op_rsize_bop = nfsd4_secinfo_rsize,
  2013. },
  2014. [OP_SETATTR] = {
  2015. .op_func = nfsd4_setattr,
  2016. .op_name = "OP_SETATTR",
  2017. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  2018. .op_rsize_bop = nfsd4_setattr_rsize,
  2019. .op_get_currentstateid = nfsd4_get_setattrstateid,
  2020. },
  2021. [OP_SETCLIENTID] = {
  2022. .op_func = nfsd4_setclientid,
  2023. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  2024. | OP_MODIFIES_SOMETHING | OP_CACHEME,
  2025. .op_name = "OP_SETCLIENTID",
  2026. .op_rsize_bop = nfsd4_setclientid_rsize,
  2027. },
  2028. [OP_SETCLIENTID_CONFIRM] = {
  2029. .op_func = nfsd4_setclientid_confirm,
  2030. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  2031. | OP_MODIFIES_SOMETHING | OP_CACHEME,
  2032. .op_name = "OP_SETCLIENTID_CONFIRM",
  2033. .op_rsize_bop = nfsd4_only_status_rsize,
  2034. },
  2035. [OP_VERIFY] = {
  2036. .op_func = nfsd4_verify,
  2037. .op_name = "OP_VERIFY",
  2038. .op_rsize_bop = nfsd4_only_status_rsize,
  2039. },
  2040. [OP_WRITE] = {
  2041. .op_func = nfsd4_write,
  2042. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  2043. .op_name = "OP_WRITE",
  2044. .op_rsize_bop = nfsd4_write_rsize,
  2045. .op_get_currentstateid = nfsd4_get_writestateid,
  2046. },
  2047. [OP_RELEASE_LOCKOWNER] = {
  2048. .op_func = nfsd4_release_lockowner,
  2049. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  2050. | OP_MODIFIES_SOMETHING,
  2051. .op_name = "OP_RELEASE_LOCKOWNER",
  2052. .op_rsize_bop = nfsd4_only_status_rsize,
  2053. },
  2054. /* NFSv4.1 operations */
  2055. [OP_EXCHANGE_ID] = {
  2056. .op_func = nfsd4_exchange_id,
  2057. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
  2058. | OP_MODIFIES_SOMETHING,
  2059. .op_name = "OP_EXCHANGE_ID",
  2060. .op_rsize_bop = nfsd4_exchange_id_rsize,
  2061. },
  2062. [OP_BACKCHANNEL_CTL] = {
  2063. .op_func = nfsd4_backchannel_ctl,
  2064. .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
  2065. .op_name = "OP_BACKCHANNEL_CTL",
  2066. .op_rsize_bop = nfsd4_only_status_rsize,
  2067. },
  2068. [OP_BIND_CONN_TO_SESSION] = {
  2069. .op_func = nfsd4_bind_conn_to_session,
  2070. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
  2071. | OP_MODIFIES_SOMETHING,
  2072. .op_name = "OP_BIND_CONN_TO_SESSION",
  2073. .op_rsize_bop = nfsd4_bind_conn_to_session_rsize,
  2074. },
  2075. [OP_CREATE_SESSION] = {
  2076. .op_func = nfsd4_create_session,
  2077. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
  2078. | OP_MODIFIES_SOMETHING,
  2079. .op_name = "OP_CREATE_SESSION",
  2080. .op_rsize_bop = nfsd4_create_session_rsize,
  2081. },
  2082. [OP_DESTROY_SESSION] = {
  2083. .op_func = nfsd4_destroy_session,
  2084. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
  2085. | OP_MODIFIES_SOMETHING,
  2086. .op_name = "OP_DESTROY_SESSION",
  2087. .op_rsize_bop = nfsd4_only_status_rsize,
  2088. },
  2089. [OP_SEQUENCE] = {
  2090. .op_func = nfsd4_sequence,
  2091. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
  2092. .op_name = "OP_SEQUENCE",
  2093. .op_rsize_bop = nfsd4_sequence_rsize,
  2094. },
  2095. [OP_DESTROY_CLIENTID] = {
  2096. .op_func = nfsd4_destroy_clientid,
  2097. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
  2098. | OP_MODIFIES_SOMETHING,
  2099. .op_name = "OP_DESTROY_CLIENTID",
  2100. .op_rsize_bop = nfsd4_only_status_rsize,
  2101. },
  2102. [OP_RECLAIM_COMPLETE] = {
  2103. .op_func = nfsd4_reclaim_complete,
  2104. .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
  2105. .op_name = "OP_RECLAIM_COMPLETE",
  2106. .op_rsize_bop = nfsd4_only_status_rsize,
  2107. },
  2108. [OP_SECINFO_NO_NAME] = {
  2109. .op_func = nfsd4_secinfo_no_name,
  2110. .op_flags = OP_HANDLES_WRONGSEC,
  2111. .op_name = "OP_SECINFO_NO_NAME",
  2112. .op_rsize_bop = nfsd4_secinfo_rsize,
  2113. },
  2114. [OP_TEST_STATEID] = {
  2115. .op_func = nfsd4_test_stateid,
  2116. .op_flags = ALLOWED_WITHOUT_FH,
  2117. .op_name = "OP_TEST_STATEID",
  2118. .op_rsize_bop = nfsd4_test_stateid_rsize,
  2119. },
  2120. [OP_FREE_STATEID] = {
  2121. .op_func = nfsd4_free_stateid,
  2122. .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
  2123. .op_name = "OP_FREE_STATEID",
  2124. .op_get_currentstateid = nfsd4_get_freestateid,
  2125. .op_rsize_bop = nfsd4_only_status_rsize,
  2126. },
  2127. #ifdef CONFIG_NFSD_PNFS
  2128. [OP_GETDEVICEINFO] = {
  2129. .op_func = nfsd4_getdeviceinfo,
  2130. .op_flags = ALLOWED_WITHOUT_FH,
  2131. .op_name = "OP_GETDEVICEINFO",
  2132. .op_rsize_bop = nfsd4_getdeviceinfo_rsize,
  2133. },
  2134. [OP_LAYOUTGET] = {
  2135. .op_func = nfsd4_layoutget,
  2136. .op_flags = OP_MODIFIES_SOMETHING,
  2137. .op_name = "OP_LAYOUTGET",
  2138. .op_rsize_bop = nfsd4_layoutget_rsize,
  2139. },
  2140. [OP_LAYOUTCOMMIT] = {
  2141. .op_func = nfsd4_layoutcommit,
  2142. .op_flags = OP_MODIFIES_SOMETHING,
  2143. .op_name = "OP_LAYOUTCOMMIT",
  2144. .op_rsize_bop = nfsd4_layoutcommit_rsize,
  2145. },
  2146. [OP_LAYOUTRETURN] = {
  2147. .op_func = nfsd4_layoutreturn,
  2148. .op_flags = OP_MODIFIES_SOMETHING,
  2149. .op_name = "OP_LAYOUTRETURN",
  2150. .op_rsize_bop = nfsd4_layoutreturn_rsize,
  2151. },
  2152. #endif /* CONFIG_NFSD_PNFS */
  2153. /* NFSv4.2 operations */
  2154. [OP_ALLOCATE] = {
  2155. .op_func = nfsd4_allocate,
  2156. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  2157. .op_name = "OP_ALLOCATE",
  2158. .op_rsize_bop = nfsd4_only_status_rsize,
  2159. },
  2160. [OP_DEALLOCATE] = {
  2161. .op_func = nfsd4_deallocate,
  2162. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  2163. .op_name = "OP_DEALLOCATE",
  2164. .op_rsize_bop = nfsd4_only_status_rsize,
  2165. },
  2166. [OP_CLONE] = {
  2167. .op_func = nfsd4_clone,
  2168. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  2169. .op_name = "OP_CLONE",
  2170. .op_rsize_bop = nfsd4_only_status_rsize,
  2171. },
  2172. [OP_COPY] = {
  2173. .op_func = nfsd4_copy,
  2174. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  2175. .op_name = "OP_COPY",
  2176. .op_rsize_bop = nfsd4_copy_rsize,
  2177. },
  2178. [OP_SEEK] = {
  2179. .op_func = nfsd4_seek,
  2180. .op_name = "OP_SEEK",
  2181. .op_rsize_bop = nfsd4_seek_rsize,
  2182. },
  2183. };
  2184. /**
  2185. * nfsd4_spo_must_allow - Determine if the compound op contains an
  2186. * operation that is allowed to be sent with machine credentials
  2187. *
  2188. * @rqstp: a pointer to the struct svc_rqst
  2189. *
  2190. * Checks to see if the compound contains a spo_must_allow op
  2191. * and confirms that it was sent with the proper machine creds.
  2192. */
  2193. bool nfsd4_spo_must_allow(struct svc_rqst *rqstp)
  2194. {
  2195. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  2196. struct nfsd4_compoundargs *argp = rqstp->rq_argp;
  2197. struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
  2198. struct nfsd4_compound_state *cstate = &resp->cstate;
  2199. struct nfs4_op_map *allow = &cstate->clp->cl_spo_must_allow;
  2200. u32 opiter;
  2201. if (!cstate->minorversion)
  2202. return false;
  2203. if (cstate->spo_must_allowed == true)
  2204. return true;
  2205. opiter = resp->opcnt;
  2206. while (opiter < argp->opcnt) {
  2207. this = &argp->ops[opiter++];
  2208. if (test_bit(this->opnum, allow->u.longs) &&
  2209. cstate->clp->cl_mach_cred &&
  2210. nfsd4_mach_creds_match(cstate->clp, rqstp)) {
  2211. cstate->spo_must_allowed = true;
  2212. return true;
  2213. }
  2214. }
  2215. cstate->spo_must_allowed = false;
  2216. return false;
  2217. }
  2218. int nfsd4_max_reply(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2219. {
  2220. if (op->opnum == OP_ILLEGAL || op->status == nfserr_notsupp)
  2221. return op_encode_hdr_size * sizeof(__be32);
  2222. BUG_ON(OPDESC(op)->op_rsize_bop == NULL);
  2223. return OPDESC(op)->op_rsize_bop(rqstp, op);
  2224. }
  2225. void warn_on_nonidempotent_op(struct nfsd4_op *op)
  2226. {
  2227. if (OPDESC(op)->op_flags & OP_MODIFIES_SOMETHING) {
  2228. pr_err("unable to encode reply to nonidempotent op %d (%s)\n",
  2229. op->opnum, nfsd4_op_name(op->opnum));
  2230. WARN_ON_ONCE(1);
  2231. }
  2232. }
  2233. static const char *nfsd4_op_name(unsigned opnum)
  2234. {
  2235. if (opnum < ARRAY_SIZE(nfsd4_ops))
  2236. return nfsd4_ops[opnum].op_name;
  2237. return "unknown_operation";
  2238. }
  2239. #define nfsd4_voidres nfsd4_voidargs
  2240. struct nfsd4_voidargs { int dummy; };
  2241. static const struct svc_procedure nfsd_procedures4[2] = {
  2242. [NFSPROC4_NULL] = {
  2243. .pc_func = nfsd4_proc_null,
  2244. .pc_encode = nfs4svc_encode_voidres,
  2245. .pc_argsize = sizeof(struct nfsd4_voidargs),
  2246. .pc_ressize = sizeof(struct nfsd4_voidres),
  2247. .pc_cachetype = RC_NOCACHE,
  2248. .pc_xdrressize = 1,
  2249. },
  2250. [NFSPROC4_COMPOUND] = {
  2251. .pc_func = nfsd4_proc_compound,
  2252. .pc_decode = nfs4svc_decode_compoundargs,
  2253. .pc_encode = nfs4svc_encode_compoundres,
  2254. .pc_argsize = sizeof(struct nfsd4_compoundargs),
  2255. .pc_ressize = sizeof(struct nfsd4_compoundres),
  2256. .pc_release = nfsd4_release_compoundargs,
  2257. .pc_cachetype = RC_NOCACHE,
  2258. .pc_xdrressize = NFSD_BUFSIZE/4,
  2259. },
  2260. };
  2261. static unsigned int nfsd_count3[ARRAY_SIZE(nfsd_procedures4)];
  2262. const struct svc_version nfsd_version4 = {
  2263. .vs_vers = 4,
  2264. .vs_nproc = 2,
  2265. .vs_proc = nfsd_procedures4,
  2266. .vs_count = nfsd_count3,
  2267. .vs_dispatch = nfsd_dispatch,
  2268. .vs_xdrsize = NFS4_SVC_XDRSIZE,
  2269. .vs_rpcb_optnl = true,
  2270. .vs_need_cong_ctrl = true,
  2271. };
  2272. /*
  2273. * Local variables:
  2274. * c-basic-offset: 8
  2275. * End:
  2276. */