nfs4proc.c 66 KB

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