nfs4proc.c 70 KB

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