nfs4proc.c 72 KB

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