nfs4proc.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969
  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/slab.h>
  37. #include "idmap.h"
  38. #include "cache.h"
  39. #include "xdr4.h"
  40. #include "vfs.h"
  41. #include "current_stateid.h"
  42. #include "netns.h"
  43. #include "acl.h"
  44. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  45. #include <linux/security.h>
  46. static inline void
  47. nfsd4_security_inode_setsecctx(struct svc_fh *resfh, struct xdr_netobj *label, u32 *bmval)
  48. {
  49. struct inode *inode = resfh->fh_dentry->d_inode;
  50. int status;
  51. mutex_lock(&inode->i_mutex);
  52. status = security_inode_setsecctx(resfh->fh_dentry,
  53. label->data, label->len);
  54. mutex_unlock(&inode->i_mutex);
  55. if (status)
  56. /*
  57. * XXX: We should really fail the whole open, but we may
  58. * already have created a new file, so it may be too
  59. * late. For now this seems the least of evils:
  60. */
  61. bmval[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
  62. return;
  63. }
  64. #else
  65. static inline void
  66. nfsd4_security_inode_setsecctx(struct svc_fh *resfh, struct xdr_netobj *label, u32 *bmval)
  67. { }
  68. #endif
  69. #define NFSDDBG_FACILITY NFSDDBG_PROC
  70. static u32 nfsd_attrmask[] = {
  71. NFSD_WRITEABLE_ATTRS_WORD0,
  72. NFSD_WRITEABLE_ATTRS_WORD1,
  73. NFSD_WRITEABLE_ATTRS_WORD2
  74. };
  75. static u32 nfsd41_ex_attrmask[] = {
  76. NFSD_SUPPATTR_EXCLCREAT_WORD0,
  77. NFSD_SUPPATTR_EXCLCREAT_WORD1,
  78. NFSD_SUPPATTR_EXCLCREAT_WORD2
  79. };
  80. static __be32
  81. check_attr_support(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  82. u32 *bmval, u32 *writable)
  83. {
  84. struct dentry *dentry = cstate->current_fh.fh_dentry;
  85. /*
  86. * Check about attributes are supported by the NFSv4 server or not.
  87. * According to spec, unsupported attributes return ERR_ATTRNOTSUPP.
  88. */
  89. if ((bmval[0] & ~nfsd_suppattrs0(cstate->minorversion)) ||
  90. (bmval[1] & ~nfsd_suppattrs1(cstate->minorversion)) ||
  91. (bmval[2] & ~nfsd_suppattrs2(cstate->minorversion)))
  92. return nfserr_attrnotsupp;
  93. /*
  94. * Check FATTR4_WORD0_ACL can be supported
  95. * in current environment or not.
  96. */
  97. if (bmval[0] & FATTR4_WORD0_ACL) {
  98. if (!IS_POSIXACL(dentry->d_inode))
  99. return nfserr_attrnotsupp;
  100. }
  101. /*
  102. * According to spec, read-only attributes return ERR_INVAL.
  103. */
  104. if (writable) {
  105. if ((bmval[0] & ~writable[0]) || (bmval[1] & ~writable[1]) ||
  106. (bmval[2] & ~writable[2]))
  107. return nfserr_inval;
  108. }
  109. return nfs_ok;
  110. }
  111. static __be32
  112. nfsd4_check_open_attributes(struct svc_rqst *rqstp,
  113. struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
  114. {
  115. __be32 status = nfs_ok;
  116. if (open->op_create == NFS4_OPEN_CREATE) {
  117. if (open->op_createmode == NFS4_CREATE_UNCHECKED
  118. || open->op_createmode == NFS4_CREATE_GUARDED)
  119. status = check_attr_support(rqstp, cstate,
  120. open->op_bmval, nfsd_attrmask);
  121. else if (open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1)
  122. status = check_attr_support(rqstp, cstate,
  123. open->op_bmval, nfsd41_ex_attrmask);
  124. }
  125. return status;
  126. }
  127. static int
  128. is_create_with_attrs(struct nfsd4_open *open)
  129. {
  130. return open->op_create == NFS4_OPEN_CREATE
  131. && (open->op_createmode == NFS4_CREATE_UNCHECKED
  132. || open->op_createmode == NFS4_CREATE_GUARDED
  133. || open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1);
  134. }
  135. /*
  136. * if error occurs when setting the acl, just clear the acl bit
  137. * in the returned attr bitmap.
  138. */
  139. static void
  140. do_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
  141. struct nfs4_acl *acl, u32 *bmval)
  142. {
  143. __be32 status;
  144. status = nfsd4_set_nfs4_acl(rqstp, fhp, acl);
  145. if (status)
  146. /*
  147. * We should probably fail the whole open at this point,
  148. * but we've already created the file, so it's too late;
  149. * So this seems the least of evils:
  150. */
  151. bmval[0] &= ~FATTR4_WORD0_ACL;
  152. }
  153. static inline void
  154. fh_dup2(struct svc_fh *dst, struct svc_fh *src)
  155. {
  156. fh_put(dst);
  157. dget(src->fh_dentry);
  158. if (src->fh_export)
  159. cache_get(&src->fh_export->h);
  160. *dst = *src;
  161. }
  162. static __be32
  163. do_open_permission(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open, int accmode)
  164. {
  165. __be32 status;
  166. if (open->op_truncate &&
  167. !(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
  168. return nfserr_inval;
  169. accmode |= NFSD_MAY_READ_IF_EXEC;
  170. if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
  171. accmode |= NFSD_MAY_READ;
  172. if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
  173. accmode |= (NFSD_MAY_WRITE | NFSD_MAY_TRUNC);
  174. if (open->op_share_deny & NFS4_SHARE_DENY_READ)
  175. accmode |= NFSD_MAY_WRITE;
  176. status = fh_verify(rqstp, current_fh, S_IFREG, accmode);
  177. return status;
  178. }
  179. static __be32 nfsd_check_obj_isreg(struct svc_fh *fh)
  180. {
  181. umode_t mode = fh->fh_dentry->d_inode->i_mode;
  182. if (S_ISREG(mode))
  183. return nfs_ok;
  184. if (S_ISDIR(mode))
  185. return nfserr_isdir;
  186. /*
  187. * Using err_symlink as our catch-all case may look odd; but
  188. * there's no other obvious error for this case in 4.0, and we
  189. * happen to know that it will cause the linux v4 client to do
  190. * the right thing on attempts to open something other than a
  191. * regular file.
  192. */
  193. return nfserr_symlink;
  194. }
  195. static void nfsd4_set_open_owner_reply_cache(struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh *resfh)
  196. {
  197. if (nfsd4_has_session(cstate))
  198. return;
  199. fh_copy_shallow(&open->op_openowner->oo_owner.so_replay.rp_openfh,
  200. &resfh->fh_handle);
  201. }
  202. static __be32
  203. do_open_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh **resfh)
  204. {
  205. struct svc_fh *current_fh = &cstate->current_fh;
  206. int accmode;
  207. __be32 status;
  208. *resfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
  209. if (!*resfh)
  210. return nfserr_jukebox;
  211. fh_init(*resfh, NFS4_FHSIZE);
  212. open->op_truncate = 0;
  213. if (open->op_create) {
  214. /* FIXME: check session persistence and pnfs flags.
  215. * The nfsv4.1 spec requires the following semantics:
  216. *
  217. * Persistent | pNFS | Server REQUIRED | Client Allowed
  218. * Reply Cache | server | |
  219. * -------------+--------+-----------------+--------------------
  220. * no | no | EXCLUSIVE4_1 | EXCLUSIVE4_1
  221. * | | | (SHOULD)
  222. * | | and EXCLUSIVE4 | or EXCLUSIVE4
  223. * | | | (SHOULD NOT)
  224. * no | yes | EXCLUSIVE4_1 | EXCLUSIVE4_1
  225. * yes | no | GUARDED4 | GUARDED4
  226. * yes | yes | GUARDED4 | GUARDED4
  227. */
  228. /*
  229. * Note: create modes (UNCHECKED,GUARDED...) are the same
  230. * in NFSv4 as in v3 except EXCLUSIVE4_1.
  231. */
  232. status = do_nfsd_create(rqstp, current_fh, open->op_fname.data,
  233. open->op_fname.len, &open->op_iattr,
  234. *resfh, open->op_createmode,
  235. (u32 *)open->op_verf.data,
  236. &open->op_truncate, &open->op_created);
  237. if (!status && open->op_label.len)
  238. nfsd4_security_inode_setsecctx(*resfh, &open->op_label, open->op_bmval);
  239. /*
  240. * Following rfc 3530 14.2.16, use the returned bitmask
  241. * to indicate which attributes we used to store the
  242. * verifier:
  243. */
  244. if (open->op_createmode == NFS4_CREATE_EXCLUSIVE && status == 0)
  245. open->op_bmval[1] = (FATTR4_WORD1_TIME_ACCESS |
  246. FATTR4_WORD1_TIME_MODIFY);
  247. } else
  248. /*
  249. * Note this may exit with the parent still locked.
  250. * We will hold the lock until nfsd4_open's final
  251. * lookup, to prevent renames or unlinks until we've had
  252. * a chance to an acquire a delegation if appropriate.
  253. */
  254. status = nfsd_lookup(rqstp, current_fh,
  255. open->op_fname.data, open->op_fname.len, *resfh);
  256. if (status)
  257. goto out;
  258. status = nfsd_check_obj_isreg(*resfh);
  259. if (status)
  260. goto out;
  261. if (is_create_with_attrs(open) && open->op_acl != NULL)
  262. do_set_nfs4_acl(rqstp, *resfh, open->op_acl, open->op_bmval);
  263. nfsd4_set_open_owner_reply_cache(cstate, open, *resfh);
  264. accmode = NFSD_MAY_NOP;
  265. if (open->op_created ||
  266. open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
  267. accmode |= NFSD_MAY_OWNER_OVERRIDE;
  268. status = do_open_permission(rqstp, *resfh, open, accmode);
  269. set_change_info(&open->op_cinfo, current_fh);
  270. out:
  271. return status;
  272. }
  273. static __be32
  274. do_open_fhandle(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
  275. {
  276. struct svc_fh *current_fh = &cstate->current_fh;
  277. __be32 status;
  278. int accmode = 0;
  279. /* We don't know the target directory, and therefore can not
  280. * set the change info
  281. */
  282. memset(&open->op_cinfo, 0, sizeof(struct nfsd4_change_info));
  283. nfsd4_set_open_owner_reply_cache(cstate, open, current_fh);
  284. open->op_truncate = (open->op_iattr.ia_valid & ATTR_SIZE) &&
  285. (open->op_iattr.ia_size == 0);
  286. /*
  287. * In the delegation case, the client is telling us about an
  288. * open that it *already* performed locally, some time ago. We
  289. * should let it succeed now if possible.
  290. *
  291. * In the case of a CLAIM_FH open, on the other hand, the client
  292. * may be counting on us to enforce permissions (the Linux 4.1
  293. * client uses this for normal opens, for example).
  294. */
  295. if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH)
  296. accmode = NFSD_MAY_OWNER_OVERRIDE;
  297. status = do_open_permission(rqstp, current_fh, open, accmode);
  298. return status;
  299. }
  300. static void
  301. copy_clientid(clientid_t *clid, struct nfsd4_session *session)
  302. {
  303. struct nfsd4_sessionid *sid =
  304. (struct nfsd4_sessionid *)session->se_sessionid.data;
  305. clid->cl_boot = sid->clientid.cl_boot;
  306. clid->cl_id = sid->clientid.cl_id;
  307. }
  308. static __be32
  309. nfsd4_open(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  310. struct nfsd4_open *open)
  311. {
  312. __be32 status;
  313. struct svc_fh *resfh = NULL;
  314. struct nfsd4_compoundres *resp;
  315. struct net *net = SVC_NET(rqstp);
  316. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  317. dprintk("NFSD: nfsd4_open filename %.*s op_openowner %p\n",
  318. (int)open->op_fname.len, open->op_fname.data,
  319. open->op_openowner);
  320. /* This check required by spec. */
  321. if (open->op_create && open->op_claim_type != NFS4_OPEN_CLAIM_NULL)
  322. return nfserr_inval;
  323. open->op_created = 0;
  324. /*
  325. * RFC5661 18.51.3
  326. * Before RECLAIM_COMPLETE done, server should deny new lock
  327. */
  328. if (nfsd4_has_session(cstate) &&
  329. !test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
  330. &cstate->session->se_client->cl_flags) &&
  331. open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
  332. return nfserr_grace;
  333. if (nfsd4_has_session(cstate))
  334. copy_clientid(&open->op_clientid, cstate->session);
  335. nfs4_lock_state();
  336. /* check seqid for replay. set nfs4_owner */
  337. resp = rqstp->rq_resp;
  338. status = nfsd4_process_open1(&resp->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 (locks_in_grace(net) && open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
  364. goto out;
  365. status = nfserr_no_grace;
  366. if (!locks_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->minorversion,
  378. nn);
  379. if (status)
  380. goto out;
  381. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  382. case NFS4_OPEN_CLAIM_FH:
  383. case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
  384. status = do_open_fhandle(rqstp, cstate, open);
  385. if (status)
  386. goto out;
  387. resfh = &cstate->current_fh;
  388. break;
  389. case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
  390. case NFS4_OPEN_CLAIM_DELEGATE_PREV:
  391. dprintk("NFSD: unsupported OPEN claim type %d\n",
  392. open->op_claim_type);
  393. status = nfserr_notsupp;
  394. goto out;
  395. default:
  396. dprintk("NFSD: Invalid OPEN claim type %d\n",
  397. open->op_claim_type);
  398. status = nfserr_inval;
  399. goto out;
  400. }
  401. /*
  402. * nfsd4_process_open2() does the actual opening of the file. If
  403. * successful, it (1) truncates the file if open->op_truncate was
  404. * set, (2) sets open->op_stateid, (3) sets open->op_delegation.
  405. */
  406. status = nfsd4_process_open2(rqstp, resfh, open);
  407. WARN_ON(status && open->op_created);
  408. out:
  409. if (resfh && resfh != &cstate->current_fh) {
  410. fh_dup2(&cstate->current_fh, resfh);
  411. fh_put(resfh);
  412. kfree(resfh);
  413. }
  414. nfsd4_cleanup_open_state(open, status);
  415. if (open->op_openowner && !nfsd4_has_session(cstate))
  416. cstate->replay_owner = &open->op_openowner->oo_owner;
  417. nfsd4_bump_seqid(cstate, status);
  418. if (!cstate->replay_owner)
  419. nfs4_unlock_state();
  420. return status;
  421. }
  422. /*
  423. * OPEN is the only seqid-mutating operation whose decoding can fail
  424. * with a seqid-mutating error (specifically, decoding of user names in
  425. * the attributes). Therefore we have to do some processing to look up
  426. * the stateowner so that we can bump the seqid.
  427. */
  428. static __be32 nfsd4_open_omfg(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_op *op)
  429. {
  430. struct nfsd4_open *open = (struct nfsd4_open *)&op->u;
  431. if (!seqid_mutating_err(ntohl(op->status)))
  432. return op->status;
  433. if (nfsd4_has_session(cstate))
  434. return op->status;
  435. open->op_xdr_error = op->status;
  436. return nfsd4_open(rqstp, cstate, open);
  437. }
  438. /*
  439. * filehandle-manipulating ops.
  440. */
  441. static __be32
  442. nfsd4_getfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  443. struct svc_fh **getfh)
  444. {
  445. if (!cstate->current_fh.fh_dentry)
  446. return nfserr_nofilehandle;
  447. *getfh = &cstate->current_fh;
  448. return nfs_ok;
  449. }
  450. static __be32
  451. nfsd4_putfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  452. struct nfsd4_putfh *putfh)
  453. {
  454. fh_put(&cstate->current_fh);
  455. cstate->current_fh.fh_handle.fh_size = putfh->pf_fhlen;
  456. memcpy(&cstate->current_fh.fh_handle.fh_base, putfh->pf_fhval,
  457. putfh->pf_fhlen);
  458. return fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_BYPASS_GSS);
  459. }
  460. static __be32
  461. nfsd4_putrootfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  462. void *arg)
  463. {
  464. __be32 status;
  465. fh_put(&cstate->current_fh);
  466. status = exp_pseudoroot(rqstp, &cstate->current_fh);
  467. return status;
  468. }
  469. static __be32
  470. nfsd4_restorefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  471. void *arg)
  472. {
  473. if (!cstate->save_fh.fh_dentry)
  474. return nfserr_restorefh;
  475. fh_dup2(&cstate->current_fh, &cstate->save_fh);
  476. if (HAS_STATE_ID(cstate, SAVED_STATE_ID_FLAG)) {
  477. memcpy(&cstate->current_stateid, &cstate->save_stateid, sizeof(stateid_t));
  478. SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
  479. }
  480. return nfs_ok;
  481. }
  482. static __be32
  483. nfsd4_savefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  484. void *arg)
  485. {
  486. if (!cstate->current_fh.fh_dentry)
  487. return nfserr_nofilehandle;
  488. fh_dup2(&cstate->save_fh, &cstate->current_fh);
  489. if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG)) {
  490. memcpy(&cstate->save_stateid, &cstate->current_stateid, sizeof(stateid_t));
  491. SET_STATE_ID(cstate, SAVED_STATE_ID_FLAG);
  492. }
  493. return nfs_ok;
  494. }
  495. /*
  496. * misc nfsv4 ops
  497. */
  498. static __be32
  499. nfsd4_access(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  500. struct nfsd4_access *access)
  501. {
  502. if (access->ac_req_access & ~NFS3_ACCESS_FULL)
  503. return nfserr_inval;
  504. access->ac_resp_access = access->ac_req_access;
  505. return nfsd_access(rqstp, &cstate->current_fh, &access->ac_resp_access,
  506. &access->ac_supported);
  507. }
  508. static void gen_boot_verifier(nfs4_verifier *verifier, struct net *net)
  509. {
  510. __be32 verf[2];
  511. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  512. verf[0] = (__be32)nn->nfssvc_boot.tv_sec;
  513. verf[1] = (__be32)nn->nfssvc_boot.tv_usec;
  514. memcpy(verifier->data, verf, sizeof(verifier->data));
  515. }
  516. static __be32
  517. nfsd4_commit(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  518. struct nfsd4_commit *commit)
  519. {
  520. gen_boot_verifier(&commit->co_verf, SVC_NET(rqstp));
  521. return nfsd_commit(rqstp, &cstate->current_fh, commit->co_offset,
  522. commit->co_count);
  523. }
  524. static __be32
  525. nfsd4_create(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  526. struct nfsd4_create *create)
  527. {
  528. struct svc_fh resfh;
  529. __be32 status;
  530. dev_t rdev;
  531. fh_init(&resfh, NFS4_FHSIZE);
  532. status = fh_verify(rqstp, &cstate->current_fh, S_IFDIR,
  533. NFSD_MAY_CREATE);
  534. if (status)
  535. return status;
  536. status = check_attr_support(rqstp, cstate, create->cr_bmval,
  537. nfsd_attrmask);
  538. if (status)
  539. return status;
  540. switch (create->cr_type) {
  541. case NF4LNK:
  542. /* ugh! we have to null-terminate the linktext, or
  543. * vfs_symlink() will choke. it is always safe to
  544. * null-terminate by brute force, since at worst we
  545. * will overwrite the first byte of the create namelen
  546. * in the XDR buffer, which has already been extracted
  547. * during XDR decode.
  548. */
  549. create->cr_linkname[create->cr_linklen] = 0;
  550. status = nfsd_symlink(rqstp, &cstate->current_fh,
  551. create->cr_name, create->cr_namelen,
  552. create->cr_linkname, create->cr_linklen,
  553. &resfh, &create->cr_iattr);
  554. break;
  555. case NF4BLK:
  556. rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
  557. if (MAJOR(rdev) != create->cr_specdata1 ||
  558. MINOR(rdev) != create->cr_specdata2)
  559. return nfserr_inval;
  560. status = nfsd_create(rqstp, &cstate->current_fh,
  561. create->cr_name, create->cr_namelen,
  562. &create->cr_iattr, S_IFBLK, rdev, &resfh);
  563. break;
  564. case NF4CHR:
  565. rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
  566. if (MAJOR(rdev) != create->cr_specdata1 ||
  567. MINOR(rdev) != create->cr_specdata2)
  568. return nfserr_inval;
  569. status = nfsd_create(rqstp, &cstate->current_fh,
  570. create->cr_name, create->cr_namelen,
  571. &create->cr_iattr,S_IFCHR, rdev, &resfh);
  572. break;
  573. case NF4SOCK:
  574. status = nfsd_create(rqstp, &cstate->current_fh,
  575. create->cr_name, create->cr_namelen,
  576. &create->cr_iattr, S_IFSOCK, 0, &resfh);
  577. break;
  578. case NF4FIFO:
  579. status = nfsd_create(rqstp, &cstate->current_fh,
  580. create->cr_name, create->cr_namelen,
  581. &create->cr_iattr, S_IFIFO, 0, &resfh);
  582. break;
  583. case NF4DIR:
  584. create->cr_iattr.ia_valid &= ~ATTR_SIZE;
  585. status = nfsd_create(rqstp, &cstate->current_fh,
  586. create->cr_name, create->cr_namelen,
  587. &create->cr_iattr, S_IFDIR, 0, &resfh);
  588. break;
  589. default:
  590. status = nfserr_badtype;
  591. }
  592. if (status)
  593. goto out;
  594. if (create->cr_label.len)
  595. nfsd4_security_inode_setsecctx(&resfh, &create->cr_label, create->cr_bmval);
  596. if (create->cr_acl != NULL)
  597. do_set_nfs4_acl(rqstp, &resfh, create->cr_acl,
  598. create->cr_bmval);
  599. fh_unlock(&cstate->current_fh);
  600. set_change_info(&create->cr_cinfo, &cstate->current_fh);
  601. fh_dup2(&cstate->current_fh, &resfh);
  602. out:
  603. fh_put(&resfh);
  604. return status;
  605. }
  606. static __be32
  607. nfsd4_getattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  608. struct nfsd4_getattr *getattr)
  609. {
  610. __be32 status;
  611. status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
  612. if (status)
  613. return status;
  614. if (getattr->ga_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
  615. return nfserr_inval;
  616. getattr->ga_bmval[0] &= nfsd_suppattrs0(cstate->minorversion);
  617. getattr->ga_bmval[1] &= nfsd_suppattrs1(cstate->minorversion);
  618. getattr->ga_bmval[2] &= nfsd_suppattrs2(cstate->minorversion);
  619. getattr->ga_fhp = &cstate->current_fh;
  620. return nfs_ok;
  621. }
  622. static __be32
  623. nfsd4_link(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  624. struct nfsd4_link *link)
  625. {
  626. __be32 status = nfserr_nofilehandle;
  627. if (!cstate->save_fh.fh_dentry)
  628. return status;
  629. status = nfsd_link(rqstp, &cstate->current_fh,
  630. link->li_name, link->li_namelen, &cstate->save_fh);
  631. if (!status)
  632. set_change_info(&link->li_cinfo, &cstate->current_fh);
  633. return status;
  634. }
  635. static __be32 nfsd4_do_lookupp(struct svc_rqst *rqstp, struct svc_fh *fh)
  636. {
  637. struct svc_fh tmp_fh;
  638. __be32 ret;
  639. fh_init(&tmp_fh, NFS4_FHSIZE);
  640. ret = exp_pseudoroot(rqstp, &tmp_fh);
  641. if (ret)
  642. return ret;
  643. if (tmp_fh.fh_dentry == fh->fh_dentry) {
  644. fh_put(&tmp_fh);
  645. return nfserr_noent;
  646. }
  647. fh_put(&tmp_fh);
  648. return nfsd_lookup(rqstp, fh, "..", 2, fh);
  649. }
  650. static __be32
  651. nfsd4_lookupp(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  652. void *arg)
  653. {
  654. return nfsd4_do_lookupp(rqstp, &cstate->current_fh);
  655. }
  656. static __be32
  657. nfsd4_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  658. struct nfsd4_lookup *lookup)
  659. {
  660. return nfsd_lookup(rqstp, &cstate->current_fh,
  661. lookup->lo_name, lookup->lo_len,
  662. &cstate->current_fh);
  663. }
  664. static __be32
  665. nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  666. struct nfsd4_read *read)
  667. {
  668. __be32 status;
  669. /* no need to check permission - this will be done in nfsd_read() */
  670. read->rd_filp = NULL;
  671. if (read->rd_offset >= OFFSET_MAX)
  672. return nfserr_inval;
  673. /*
  674. * If we do a zero copy read, then a client will see read data
  675. * that reflects the state of the file *after* performing the
  676. * following compound.
  677. *
  678. * To ensure proper ordering, we therefore turn off zero copy if
  679. * the client wants us to do more in this compound:
  680. */
  681. if (!nfsd4_last_compound_op(rqstp))
  682. rqstp->rq_splice_ok = false;
  683. /* check stateid */
  684. if ((status = nfs4_preprocess_stateid_op(SVC_NET(rqstp),
  685. cstate, &read->rd_stateid,
  686. RD_STATE, &read->rd_filp))) {
  687. dprintk("NFSD: nfsd4_read: couldn't process stateid!\n");
  688. goto out;
  689. }
  690. status = nfs_ok;
  691. out:
  692. read->rd_rqstp = rqstp;
  693. read->rd_fhp = &cstate->current_fh;
  694. return status;
  695. }
  696. static __be32
  697. nfsd4_readdir(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  698. struct nfsd4_readdir *readdir)
  699. {
  700. u64 cookie = readdir->rd_cookie;
  701. static const nfs4_verifier zeroverf;
  702. /* no need to check permission - this will be done in nfsd_readdir() */
  703. if (readdir->rd_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
  704. return nfserr_inval;
  705. readdir->rd_bmval[0] &= nfsd_suppattrs0(cstate->minorversion);
  706. readdir->rd_bmval[1] &= nfsd_suppattrs1(cstate->minorversion);
  707. readdir->rd_bmval[2] &= nfsd_suppattrs2(cstate->minorversion);
  708. if ((cookie == 1) || (cookie == 2) ||
  709. (cookie == 0 && memcmp(readdir->rd_verf.data, zeroverf.data, NFS4_VERIFIER_SIZE)))
  710. return nfserr_bad_cookie;
  711. readdir->rd_rqstp = rqstp;
  712. readdir->rd_fhp = &cstate->current_fh;
  713. return nfs_ok;
  714. }
  715. static __be32
  716. nfsd4_readlink(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  717. struct nfsd4_readlink *readlink)
  718. {
  719. readlink->rl_rqstp = rqstp;
  720. readlink->rl_fhp = &cstate->current_fh;
  721. return nfs_ok;
  722. }
  723. static __be32
  724. nfsd4_remove(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  725. struct nfsd4_remove *remove)
  726. {
  727. __be32 status;
  728. if (locks_in_grace(SVC_NET(rqstp)))
  729. return nfserr_grace;
  730. status = nfsd_unlink(rqstp, &cstate->current_fh, 0,
  731. remove->rm_name, remove->rm_namelen);
  732. if (!status) {
  733. fh_unlock(&cstate->current_fh);
  734. set_change_info(&remove->rm_cinfo, &cstate->current_fh);
  735. }
  736. return status;
  737. }
  738. static __be32
  739. nfsd4_rename(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  740. struct nfsd4_rename *rename)
  741. {
  742. __be32 status = nfserr_nofilehandle;
  743. if (!cstate->save_fh.fh_dentry)
  744. return status;
  745. if (locks_in_grace(SVC_NET(rqstp)) &&
  746. !(cstate->save_fh.fh_export->ex_flags & NFSEXP_NOSUBTREECHECK))
  747. return nfserr_grace;
  748. status = nfsd_rename(rqstp, &cstate->save_fh, rename->rn_sname,
  749. rename->rn_snamelen, &cstate->current_fh,
  750. rename->rn_tname, rename->rn_tnamelen);
  751. if (status)
  752. return status;
  753. set_change_info(&rename->rn_sinfo, &cstate->current_fh);
  754. set_change_info(&rename->rn_tinfo, &cstate->save_fh);
  755. return nfs_ok;
  756. }
  757. static __be32
  758. nfsd4_secinfo(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  759. struct nfsd4_secinfo *secinfo)
  760. {
  761. struct svc_fh resfh;
  762. struct svc_export *exp;
  763. struct dentry *dentry;
  764. __be32 err;
  765. fh_init(&resfh, NFS4_FHSIZE);
  766. err = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_EXEC);
  767. if (err)
  768. return err;
  769. err = nfsd_lookup_dentry(rqstp, &cstate->current_fh,
  770. secinfo->si_name, secinfo->si_namelen,
  771. &exp, &dentry);
  772. if (err)
  773. return err;
  774. if (dentry->d_inode == NULL) {
  775. exp_put(exp);
  776. err = nfserr_noent;
  777. } else
  778. secinfo->si_exp = exp;
  779. dput(dentry);
  780. if (cstate->minorversion)
  781. /* See rfc 5661 section 2.6.3.1.1.8 */
  782. fh_put(&cstate->current_fh);
  783. return err;
  784. }
  785. static __be32
  786. nfsd4_secinfo_no_name(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  787. struct nfsd4_secinfo_no_name *sin)
  788. {
  789. __be32 err;
  790. switch (sin->sin_style) {
  791. case NFS4_SECINFO_STYLE4_CURRENT_FH:
  792. break;
  793. case NFS4_SECINFO_STYLE4_PARENT:
  794. err = nfsd4_do_lookupp(rqstp, &cstate->current_fh);
  795. if (err)
  796. return err;
  797. break;
  798. default:
  799. return nfserr_inval;
  800. }
  801. exp_get(cstate->current_fh.fh_export);
  802. sin->sin_exp = cstate->current_fh.fh_export;
  803. fh_put(&cstate->current_fh);
  804. return nfs_ok;
  805. }
  806. static __be32
  807. nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  808. struct nfsd4_setattr *setattr)
  809. {
  810. __be32 status = nfs_ok;
  811. int err;
  812. if (setattr->sa_iattr.ia_valid & ATTR_SIZE) {
  813. status = nfs4_preprocess_stateid_op(SVC_NET(rqstp), cstate,
  814. &setattr->sa_stateid, WR_STATE, NULL);
  815. if (status) {
  816. dprintk("NFSD: nfsd4_setattr: couldn't process stateid!\n");
  817. return status;
  818. }
  819. }
  820. err = fh_want_write(&cstate->current_fh);
  821. if (err)
  822. return nfserrno(err);
  823. status = nfs_ok;
  824. status = check_attr_support(rqstp, cstate, setattr->sa_bmval,
  825. nfsd_attrmask);
  826. if (status)
  827. goto out;
  828. if (setattr->sa_acl != NULL)
  829. status = nfsd4_set_nfs4_acl(rqstp, &cstate->current_fh,
  830. setattr->sa_acl);
  831. if (status)
  832. goto out;
  833. if (setattr->sa_label.len)
  834. status = nfsd4_set_nfs4_label(rqstp, &cstate->current_fh,
  835. &setattr->sa_label);
  836. if (status)
  837. goto out;
  838. status = nfsd_setattr(rqstp, &cstate->current_fh, &setattr->sa_iattr,
  839. 0, (time_t)0);
  840. out:
  841. fh_drop_write(&cstate->current_fh);
  842. return status;
  843. }
  844. static int fill_in_write_vector(struct kvec *vec, struct nfsd4_write *write)
  845. {
  846. int i = 1;
  847. int buflen = write->wr_buflen;
  848. vec[0].iov_base = write->wr_head.iov_base;
  849. vec[0].iov_len = min_t(int, buflen, write->wr_head.iov_len);
  850. buflen -= vec[0].iov_len;
  851. while (buflen) {
  852. vec[i].iov_base = page_address(write->wr_pagelist[i - 1]);
  853. vec[i].iov_len = min_t(int, PAGE_SIZE, buflen);
  854. buflen -= vec[i].iov_len;
  855. i++;
  856. }
  857. return i;
  858. }
  859. static __be32
  860. nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  861. struct nfsd4_write *write)
  862. {
  863. stateid_t *stateid = &write->wr_stateid;
  864. struct file *filp = NULL;
  865. __be32 status = nfs_ok;
  866. unsigned long cnt;
  867. int nvecs;
  868. /* no need to check permission - this will be done in nfsd_write() */
  869. if (write->wr_offset >= OFFSET_MAX)
  870. return nfserr_inval;
  871. status = nfs4_preprocess_stateid_op(SVC_NET(rqstp),
  872. cstate, stateid, WR_STATE, &filp);
  873. if (status) {
  874. dprintk("NFSD: nfsd4_write: couldn't process stateid!\n");
  875. return status;
  876. }
  877. cnt = write->wr_buflen;
  878. write->wr_how_written = write->wr_stable_how;
  879. gen_boot_verifier(&write->wr_verifier, SVC_NET(rqstp));
  880. nvecs = fill_in_write_vector(rqstp->rq_vec, write);
  881. WARN_ON_ONCE(nvecs > ARRAY_SIZE(rqstp->rq_vec));
  882. status = nfsd_write(rqstp, &cstate->current_fh, filp,
  883. write->wr_offset, rqstp->rq_vec, nvecs,
  884. &cnt, &write->wr_how_written);
  885. if (filp)
  886. fput(filp);
  887. write->wr_bytes_written = cnt;
  888. return status;
  889. }
  890. /* This routine never returns NFS_OK! If there are no other errors, it
  891. * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the
  892. * attributes matched. VERIFY is implemented by mapping NFSERR_SAME
  893. * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK.
  894. */
  895. static __be32
  896. _nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  897. struct nfsd4_verify *verify)
  898. {
  899. __be32 *buf, *p;
  900. int count;
  901. __be32 status;
  902. status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
  903. if (status)
  904. return status;
  905. status = check_attr_support(rqstp, cstate, verify->ve_bmval, NULL);
  906. if (status)
  907. return status;
  908. if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR)
  909. || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1))
  910. return nfserr_inval;
  911. if (verify->ve_attrlen & 3)
  912. return nfserr_inval;
  913. /* count in words:
  914. * bitmap_len(1) + bitmap(2) + attr_len(1) = 4
  915. */
  916. count = 4 + (verify->ve_attrlen >> 2);
  917. buf = kmalloc(count << 2, GFP_KERNEL);
  918. if (!buf)
  919. return nfserr_jukebox;
  920. p = buf;
  921. status = nfsd4_encode_fattr_to_buf(&p, count, &cstate->current_fh,
  922. cstate->current_fh.fh_export,
  923. cstate->current_fh.fh_dentry,
  924. verify->ve_bmval,
  925. rqstp, 0);
  926. /*
  927. * If nfsd4_encode_fattr() ran out of space, assume that's because
  928. * the attributes are longer (hence different) than those given:
  929. */
  930. if (status == nfserr_resource)
  931. status = nfserr_not_same;
  932. if (status)
  933. goto out_kfree;
  934. /* skip bitmap */
  935. p = buf + 1 + ntohl(buf[0]);
  936. status = nfserr_not_same;
  937. if (ntohl(*p++) != verify->ve_attrlen)
  938. goto out_kfree;
  939. if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen))
  940. status = nfserr_same;
  941. out_kfree:
  942. kfree(buf);
  943. return status;
  944. }
  945. static __be32
  946. nfsd4_nverify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  947. struct nfsd4_verify *verify)
  948. {
  949. __be32 status;
  950. status = _nfsd4_verify(rqstp, cstate, verify);
  951. return status == nfserr_not_same ? nfs_ok : status;
  952. }
  953. static __be32
  954. nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  955. struct nfsd4_verify *verify)
  956. {
  957. __be32 status;
  958. status = _nfsd4_verify(rqstp, cstate, verify);
  959. return status == nfserr_same ? nfs_ok : status;
  960. }
  961. /*
  962. * NULL call.
  963. */
  964. static __be32
  965. nfsd4_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
  966. {
  967. return nfs_ok;
  968. }
  969. static inline void nfsd4_increment_op_stats(u32 opnum)
  970. {
  971. if (opnum >= FIRST_NFS4_OP && opnum <= LAST_NFS4_OP)
  972. nfsdstats.nfs4_opcount[opnum]++;
  973. }
  974. typedef __be32(*nfsd4op_func)(struct svc_rqst *, struct nfsd4_compound_state *,
  975. void *);
  976. typedef u32(*nfsd4op_rsize)(struct svc_rqst *, struct nfsd4_op *op);
  977. typedef void(*stateid_setter)(struct nfsd4_compound_state *, void *);
  978. typedef void(*stateid_getter)(struct nfsd4_compound_state *, void *);
  979. enum nfsd4_op_flags {
  980. ALLOWED_WITHOUT_FH = 1 << 0, /* No current filehandle required */
  981. ALLOWED_ON_ABSENT_FS = 1 << 1, /* ops processed on absent fs */
  982. ALLOWED_AS_FIRST_OP = 1 << 2, /* ops reqired first in compound */
  983. /* For rfc 5661 section 2.6.3.1.1: */
  984. OP_HANDLES_WRONGSEC = 1 << 3,
  985. OP_IS_PUTFH_LIKE = 1 << 4,
  986. /*
  987. * These are the ops whose result size we estimate before
  988. * encoding, to avoid performing an op then not being able to
  989. * respond or cache a response. This includes writes and setattrs
  990. * as well as the operations usually called "nonidempotent":
  991. */
  992. OP_MODIFIES_SOMETHING = 1 << 5,
  993. /*
  994. * Cache compounds containing these ops in the xid-based drc:
  995. * We use the DRC for compounds containing non-idempotent
  996. * operations, *except* those that are 4.1-specific (since
  997. * sessions provide their own EOS), and except for stateful
  998. * operations other than setclientid and setclientid_confirm
  999. * (since sequence numbers provide EOS for open, lock, etc in
  1000. * the v4.0 case).
  1001. */
  1002. OP_CACHEME = 1 << 6,
  1003. /*
  1004. * These are ops which clear current state id.
  1005. */
  1006. OP_CLEAR_STATEID = 1 << 7,
  1007. };
  1008. struct nfsd4_operation {
  1009. nfsd4op_func op_func;
  1010. u32 op_flags;
  1011. char *op_name;
  1012. /* Try to get response size before operation */
  1013. nfsd4op_rsize op_rsize_bop;
  1014. stateid_getter op_get_currentstateid;
  1015. stateid_setter op_set_currentstateid;
  1016. };
  1017. static struct nfsd4_operation nfsd4_ops[];
  1018. static const char *nfsd4_op_name(unsigned opnum);
  1019. /*
  1020. * Enforce NFSv4.1 COMPOUND ordering rules:
  1021. *
  1022. * Also note, enforced elsewhere:
  1023. * - SEQUENCE other than as first op results in
  1024. * NFS4ERR_SEQUENCE_POS. (Enforced in nfsd4_sequence().)
  1025. * - BIND_CONN_TO_SESSION must be the only op in its compound.
  1026. * (Enforced in nfsd4_bind_conn_to_session().)
  1027. * - DESTROY_SESSION must be the final operation in a compound, if
  1028. * sessionid's in SEQUENCE and DESTROY_SESSION are the same.
  1029. * (Enforced in nfsd4_destroy_session().)
  1030. */
  1031. static __be32 nfs41_check_op_ordering(struct nfsd4_compoundargs *args)
  1032. {
  1033. struct nfsd4_op *op = &args->ops[0];
  1034. /* These ordering requirements don't apply to NFSv4.0: */
  1035. if (args->minorversion == 0)
  1036. return nfs_ok;
  1037. /* This is weird, but OK, not our problem: */
  1038. if (args->opcnt == 0)
  1039. return nfs_ok;
  1040. if (op->status == nfserr_op_illegal)
  1041. return nfs_ok;
  1042. if (!(nfsd4_ops[op->opnum].op_flags & ALLOWED_AS_FIRST_OP))
  1043. return nfserr_op_not_in_session;
  1044. if (op->opnum == OP_SEQUENCE)
  1045. return nfs_ok;
  1046. if (args->opcnt != 1)
  1047. return nfserr_not_only_op;
  1048. return nfs_ok;
  1049. }
  1050. static inline struct nfsd4_operation *OPDESC(struct nfsd4_op *op)
  1051. {
  1052. return &nfsd4_ops[op->opnum];
  1053. }
  1054. bool nfsd4_cache_this_op(struct nfsd4_op *op)
  1055. {
  1056. if (op->opnum == OP_ILLEGAL)
  1057. return false;
  1058. return OPDESC(op)->op_flags & OP_CACHEME;
  1059. }
  1060. static bool need_wrongsec_check(struct svc_rqst *rqstp)
  1061. {
  1062. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  1063. struct nfsd4_compoundargs *argp = rqstp->rq_argp;
  1064. struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
  1065. struct nfsd4_op *next = &argp->ops[resp->opcnt];
  1066. struct nfsd4_operation *thisd;
  1067. struct nfsd4_operation *nextd;
  1068. thisd = OPDESC(this);
  1069. /*
  1070. * Most ops check wronsec on our own; only the putfh-like ops
  1071. * have special rules.
  1072. */
  1073. if (!(thisd->op_flags & OP_IS_PUTFH_LIKE))
  1074. return false;
  1075. /*
  1076. * rfc 5661 2.6.3.1.1.6: don't bother erroring out a
  1077. * put-filehandle operation if we're not going to use the
  1078. * result:
  1079. */
  1080. if (argp->opcnt == resp->opcnt)
  1081. return false;
  1082. nextd = OPDESC(next);
  1083. /*
  1084. * Rest of 2.6.3.1.1: certain operations will return WRONGSEC
  1085. * errors themselves as necessary; others should check for them
  1086. * now:
  1087. */
  1088. return !(nextd->op_flags & OP_HANDLES_WRONGSEC);
  1089. }
  1090. static void svcxdr_init_encode(struct svc_rqst *rqstp,
  1091. struct nfsd4_compoundres *resp)
  1092. {
  1093. struct xdr_stream *xdr = &resp->xdr;
  1094. struct xdr_buf *buf = &rqstp->rq_res;
  1095. struct kvec *head = buf->head;
  1096. xdr->buf = buf;
  1097. xdr->iov = head;
  1098. xdr->p = head->iov_base + head->iov_len;
  1099. xdr->end = head->iov_base + PAGE_SIZE - rqstp->rq_auth_slack;
  1100. /* Tail and page_len should be zero at this point: */
  1101. buf->len = buf->head[0].iov_len;
  1102. xdr->scratch.iov_len = 0;
  1103. xdr->page_ptr = buf->pages - 1;
  1104. buf->buflen = PAGE_SIZE * (1 + rqstp->rq_page_end - buf->pages)
  1105. - rqstp->rq_auth_slack;
  1106. }
  1107. /*
  1108. * COMPOUND call.
  1109. */
  1110. static __be32
  1111. nfsd4_proc_compound(struct svc_rqst *rqstp,
  1112. struct nfsd4_compoundargs *args,
  1113. struct nfsd4_compoundres *resp)
  1114. {
  1115. struct nfsd4_op *op;
  1116. struct nfsd4_operation *opdesc;
  1117. struct nfsd4_compound_state *cstate = &resp->cstate;
  1118. struct svc_fh *current_fh = &cstate->current_fh;
  1119. struct svc_fh *save_fh = &cstate->save_fh;
  1120. __be32 status;
  1121. svcxdr_init_encode(rqstp, resp);
  1122. resp->tagp = resp->xdr.p;
  1123. /* reserve space for: taglen, tag, and opcnt */
  1124. xdr_reserve_space(&resp->xdr, 8 + args->taglen);
  1125. resp->taglen = args->taglen;
  1126. resp->tag = args->tag;
  1127. resp->rqstp = rqstp;
  1128. cstate->minorversion = args->minorversion;
  1129. fh_init(current_fh, NFS4_FHSIZE);
  1130. fh_init(save_fh, NFS4_FHSIZE);
  1131. /*
  1132. * Don't use the deferral mechanism for NFSv4; compounds make it
  1133. * too hard to avoid non-idempotency problems.
  1134. */
  1135. rqstp->rq_usedeferral = 0;
  1136. /*
  1137. * According to RFC3010, this takes precedence over all other errors.
  1138. */
  1139. status = nfserr_minor_vers_mismatch;
  1140. if (nfsd_minorversion(args->minorversion, NFSD_TEST) <= 0)
  1141. goto out;
  1142. status = nfs41_check_op_ordering(args);
  1143. if (status) {
  1144. op = &args->ops[0];
  1145. op->status = status;
  1146. goto encode_op;
  1147. }
  1148. while (!status && resp->opcnt < args->opcnt) {
  1149. op = &args->ops[resp->opcnt++];
  1150. dprintk("nfsv4 compound op #%d/%d: %d (%s)\n",
  1151. resp->opcnt, args->opcnt, op->opnum,
  1152. nfsd4_op_name(op->opnum));
  1153. /*
  1154. * The XDR decode routines may have pre-set op->status;
  1155. * for example, if there is a miscellaneous XDR error
  1156. * it will be set to nfserr_bad_xdr.
  1157. */
  1158. if (op->status) {
  1159. if (op->opnum == OP_OPEN)
  1160. op->status = nfsd4_open_omfg(rqstp, cstate, op);
  1161. goto encode_op;
  1162. }
  1163. opdesc = OPDESC(op);
  1164. if (!current_fh->fh_dentry) {
  1165. if (!(opdesc->op_flags & ALLOWED_WITHOUT_FH)) {
  1166. op->status = nfserr_nofilehandle;
  1167. goto encode_op;
  1168. }
  1169. } else if (current_fh->fh_export->ex_fslocs.migrated &&
  1170. !(opdesc->op_flags & ALLOWED_ON_ABSENT_FS)) {
  1171. op->status = nfserr_moved;
  1172. goto encode_op;
  1173. }
  1174. fh_clear_wcc(current_fh);
  1175. /* If op is non-idempotent */
  1176. if (opdesc->op_flags & OP_MODIFIES_SOMETHING) {
  1177. /*
  1178. * Don't execute this op if we couldn't encode a
  1179. * succesful reply:
  1180. */
  1181. u32 plen = opdesc->op_rsize_bop(rqstp, op);
  1182. /*
  1183. * Plus if there's another operation, make sure
  1184. * we'll have space to at least encode an error:
  1185. */
  1186. if (resp->opcnt < args->opcnt)
  1187. plen += COMPOUND_ERR_SLACK_SPACE;
  1188. op->status = nfsd4_check_resp_size(resp, plen);
  1189. }
  1190. if (op->status)
  1191. goto encode_op;
  1192. if (opdesc->op_get_currentstateid)
  1193. opdesc->op_get_currentstateid(cstate, &op->u);
  1194. op->status = opdesc->op_func(rqstp, cstate, &op->u);
  1195. if (!op->status) {
  1196. if (opdesc->op_set_currentstateid)
  1197. opdesc->op_set_currentstateid(cstate, &op->u);
  1198. if (opdesc->op_flags & OP_CLEAR_STATEID)
  1199. clear_current_stateid(cstate);
  1200. if (need_wrongsec_check(rqstp))
  1201. op->status = check_nfsd_access(current_fh->fh_export, rqstp);
  1202. }
  1203. encode_op:
  1204. /* Only from SEQUENCE */
  1205. if (cstate->status == nfserr_replay_cache) {
  1206. dprintk("%s NFS4.1 replay from cache\n", __func__);
  1207. status = op->status;
  1208. goto out;
  1209. }
  1210. if (op->status == nfserr_replay_me) {
  1211. op->replay = &cstate->replay_owner->so_replay;
  1212. nfsd4_encode_replay(&resp->xdr, op);
  1213. status = op->status = op->replay->rp_status;
  1214. } else {
  1215. nfsd4_encode_operation(resp, op);
  1216. status = op->status;
  1217. }
  1218. dprintk("nfsv4 compound op %p opcnt %d #%d: %d: status %d\n",
  1219. args->ops, args->opcnt, resp->opcnt, op->opnum,
  1220. be32_to_cpu(status));
  1221. if (cstate->replay_owner) {
  1222. nfs4_unlock_state();
  1223. cstate->replay_owner = NULL;
  1224. }
  1225. /* XXX Ugh, we need to get rid of this kind of special case: */
  1226. if (op->opnum == OP_READ && op->u.read.rd_filp)
  1227. fput(op->u.read.rd_filp);
  1228. nfsd4_increment_op_stats(op->opnum);
  1229. }
  1230. cstate->status = status;
  1231. fh_put(current_fh);
  1232. fh_put(save_fh);
  1233. BUG_ON(cstate->replay_owner);
  1234. out:
  1235. /* Reset deferral mechanism for RPC deferrals */
  1236. rqstp->rq_usedeferral = 1;
  1237. dprintk("nfsv4 compound returned %d\n", ntohl(status));
  1238. return status;
  1239. }
  1240. #define op_encode_hdr_size (2)
  1241. #define op_encode_stateid_maxsz (XDR_QUADLEN(NFS4_STATEID_SIZE))
  1242. #define op_encode_verifier_maxsz (XDR_QUADLEN(NFS4_VERIFIER_SIZE))
  1243. #define op_encode_change_info_maxsz (5)
  1244. #define nfs4_fattr_bitmap_maxsz (4)
  1245. /* We'll fall back on returning no lockowner if run out of space: */
  1246. #define op_encode_lockowner_maxsz (0)
  1247. #define op_encode_lock_denied_maxsz (8 + op_encode_lockowner_maxsz)
  1248. #define nfs4_owner_maxsz (1 + XDR_QUADLEN(IDMAP_NAMESZ))
  1249. #define op_encode_ace_maxsz (3 + nfs4_owner_maxsz)
  1250. #define op_encode_delegation_maxsz (1 + op_encode_stateid_maxsz + 1 + \
  1251. op_encode_ace_maxsz)
  1252. #define op_encode_channel_attrs_maxsz (6 + 1 + 1)
  1253. static inline u32 nfsd4_only_status_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1254. {
  1255. return (op_encode_hdr_size) * sizeof(__be32);
  1256. }
  1257. static inline u32 nfsd4_status_stateid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1258. {
  1259. return (op_encode_hdr_size + op_encode_stateid_maxsz)* sizeof(__be32);
  1260. }
  1261. static inline u32 nfsd4_commit_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1262. {
  1263. return (op_encode_hdr_size + op_encode_verifier_maxsz) * sizeof(__be32);
  1264. }
  1265. static inline u32 nfsd4_create_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1266. {
  1267. return (op_encode_hdr_size + op_encode_change_info_maxsz
  1268. + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
  1269. }
  1270. /*
  1271. * Note since this is an idempotent operation we won't insist on failing
  1272. * the op prematurely if the estimate is too large. We may turn off splice
  1273. * reads unnecessarily.
  1274. */
  1275. static inline u32 nfsd4_getattr_rsize(struct svc_rqst *rqstp,
  1276. struct nfsd4_op *op)
  1277. {
  1278. u32 *bmap = op->u.getattr.ga_bmval;
  1279. u32 bmap0 = bmap[0], bmap1 = bmap[1], bmap2 = bmap[2];
  1280. u32 ret = 0;
  1281. if (bmap0 & FATTR4_WORD0_ACL)
  1282. return svc_max_payload(rqstp);
  1283. if (bmap0 & FATTR4_WORD0_FS_LOCATIONS)
  1284. return svc_max_payload(rqstp);
  1285. if (bmap1 & FATTR4_WORD1_OWNER) {
  1286. ret += IDMAP_NAMESZ + 4;
  1287. bmap1 &= ~FATTR4_WORD1_OWNER;
  1288. }
  1289. if (bmap1 & FATTR4_WORD1_OWNER_GROUP) {
  1290. ret += IDMAP_NAMESZ + 4;
  1291. bmap1 &= ~FATTR4_WORD1_OWNER_GROUP;
  1292. }
  1293. if (bmap0 & FATTR4_WORD0_FILEHANDLE) {
  1294. ret += NFS4_FHSIZE + 4;
  1295. bmap0 &= ~FATTR4_WORD0_FILEHANDLE;
  1296. }
  1297. if (bmap2 & FATTR4_WORD2_SECURITY_LABEL) {
  1298. ret += NFSD4_MAX_SEC_LABEL_LEN + 12;
  1299. bmap2 &= ~FATTR4_WORD2_SECURITY_LABEL;
  1300. }
  1301. /*
  1302. * Largest of remaining attributes are 16 bytes (e.g.,
  1303. * supported_attributes)
  1304. */
  1305. ret += 16 * (hweight32(bmap0) + hweight32(bmap1) + hweight32(bmap2));
  1306. /* bitmask, length */
  1307. ret += 20;
  1308. return ret;
  1309. }
  1310. static inline u32 nfsd4_link_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1311. {
  1312. return (op_encode_hdr_size + op_encode_change_info_maxsz)
  1313. * sizeof(__be32);
  1314. }
  1315. static inline u32 nfsd4_lock_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1316. {
  1317. return (op_encode_hdr_size + op_encode_lock_denied_maxsz)
  1318. * sizeof(__be32);
  1319. }
  1320. static inline u32 nfsd4_open_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1321. {
  1322. return (op_encode_hdr_size + op_encode_stateid_maxsz
  1323. + op_encode_change_info_maxsz + 1
  1324. + nfs4_fattr_bitmap_maxsz
  1325. + op_encode_delegation_maxsz) * sizeof(__be32);
  1326. }
  1327. static inline u32 nfsd4_read_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1328. {
  1329. u32 maxcount = 0, rlen = 0;
  1330. maxcount = svc_max_payload(rqstp);
  1331. rlen = op->u.read.rd_length;
  1332. if (rlen > maxcount)
  1333. rlen = maxcount;
  1334. return (op_encode_hdr_size + 2 + XDR_QUADLEN(rlen)) * sizeof(__be32);
  1335. }
  1336. static inline u32 nfsd4_readdir_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1337. {
  1338. u32 maxcount = svc_max_payload(rqstp);
  1339. u32 rlen = op->u.readdir.rd_maxcount;
  1340. if (rlen > maxcount)
  1341. rlen = maxcount;
  1342. return (op_encode_hdr_size + op_encode_verifier_maxsz +
  1343. XDR_QUADLEN(rlen)) * sizeof(__be32);
  1344. }
  1345. static inline u32 nfsd4_remove_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1346. {
  1347. return (op_encode_hdr_size + op_encode_change_info_maxsz)
  1348. * sizeof(__be32);
  1349. }
  1350. static inline u32 nfsd4_rename_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1351. {
  1352. return (op_encode_hdr_size + op_encode_change_info_maxsz
  1353. + op_encode_change_info_maxsz) * sizeof(__be32);
  1354. }
  1355. static inline u32 nfsd4_sequence_rsize(struct svc_rqst *rqstp,
  1356. struct nfsd4_op *op)
  1357. {
  1358. return NFS4_MAX_SESSIONID_LEN + 20;
  1359. }
  1360. static inline u32 nfsd4_setattr_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1361. {
  1362. return (op_encode_hdr_size + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
  1363. }
  1364. static inline u32 nfsd4_setclientid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1365. {
  1366. return (op_encode_hdr_size + 2 + XDR_QUADLEN(NFS4_VERIFIER_SIZE)) *
  1367. sizeof(__be32);
  1368. }
  1369. static inline u32 nfsd4_write_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1370. {
  1371. return (op_encode_hdr_size + 2 + op_encode_verifier_maxsz) * sizeof(__be32);
  1372. }
  1373. static inline u32 nfsd4_exchange_id_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1374. {
  1375. return (op_encode_hdr_size + 2 + 1 + /* eir_clientid, eir_sequenceid */\
  1376. 1 + 1 + /* eir_flags, spr_how */\
  1377. 4 + /* spo_must_enforce & _allow with bitmap */\
  1378. 2 + /*eir_server_owner.so_minor_id */\
  1379. /* eir_server_owner.so_major_id<> */\
  1380. XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
  1381. /* eir_server_scope<> */\
  1382. XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
  1383. 1 + /* eir_server_impl_id array length */\
  1384. 0 /* ignored eir_server_impl_id contents */) * sizeof(__be32);
  1385. }
  1386. static inline u32 nfsd4_bind_conn_to_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1387. {
  1388. return (op_encode_hdr_size + \
  1389. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* bctsr_sessid */\
  1390. 2 /* bctsr_dir, use_conn_in_rdma_mode */) * sizeof(__be32);
  1391. }
  1392. static inline u32 nfsd4_create_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1393. {
  1394. return (op_encode_hdr_size + \
  1395. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* sessionid */\
  1396. 2 + /* csr_sequence, csr_flags */\
  1397. op_encode_channel_attrs_maxsz + \
  1398. op_encode_channel_attrs_maxsz) * sizeof(__be32);
  1399. }
  1400. static struct nfsd4_operation nfsd4_ops[] = {
  1401. [OP_ACCESS] = {
  1402. .op_func = (nfsd4op_func)nfsd4_access,
  1403. .op_name = "OP_ACCESS",
  1404. },
  1405. [OP_CLOSE] = {
  1406. .op_func = (nfsd4op_func)nfsd4_close,
  1407. .op_flags = OP_MODIFIES_SOMETHING,
  1408. .op_name = "OP_CLOSE",
  1409. .op_rsize_bop = (nfsd4op_rsize)nfsd4_status_stateid_rsize,
  1410. .op_get_currentstateid = (stateid_getter)nfsd4_get_closestateid,
  1411. .op_set_currentstateid = (stateid_setter)nfsd4_set_closestateid,
  1412. },
  1413. [OP_COMMIT] = {
  1414. .op_func = (nfsd4op_func)nfsd4_commit,
  1415. .op_flags = OP_MODIFIES_SOMETHING,
  1416. .op_name = "OP_COMMIT",
  1417. .op_rsize_bop = (nfsd4op_rsize)nfsd4_commit_rsize,
  1418. },
  1419. [OP_CREATE] = {
  1420. .op_func = (nfsd4op_func)nfsd4_create,
  1421. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME | OP_CLEAR_STATEID,
  1422. .op_name = "OP_CREATE",
  1423. .op_rsize_bop = (nfsd4op_rsize)nfsd4_create_rsize,
  1424. },
  1425. [OP_DELEGRETURN] = {
  1426. .op_func = (nfsd4op_func)nfsd4_delegreturn,
  1427. .op_flags = OP_MODIFIES_SOMETHING,
  1428. .op_name = "OP_DELEGRETURN",
  1429. .op_rsize_bop = nfsd4_only_status_rsize,
  1430. .op_get_currentstateid = (stateid_getter)nfsd4_get_delegreturnstateid,
  1431. },
  1432. [OP_GETATTR] = {
  1433. .op_func = (nfsd4op_func)nfsd4_getattr,
  1434. .op_flags = ALLOWED_ON_ABSENT_FS,
  1435. .op_rsize_bop = nfsd4_getattr_rsize,
  1436. .op_name = "OP_GETATTR",
  1437. },
  1438. [OP_GETFH] = {
  1439. .op_func = (nfsd4op_func)nfsd4_getfh,
  1440. .op_name = "OP_GETFH",
  1441. },
  1442. [OP_LINK] = {
  1443. .op_func = (nfsd4op_func)nfsd4_link,
  1444. .op_flags = ALLOWED_ON_ABSENT_FS | OP_MODIFIES_SOMETHING
  1445. | OP_CACHEME,
  1446. .op_name = "OP_LINK",
  1447. .op_rsize_bop = (nfsd4op_rsize)nfsd4_link_rsize,
  1448. },
  1449. [OP_LOCK] = {
  1450. .op_func = (nfsd4op_func)nfsd4_lock,
  1451. .op_flags = OP_MODIFIES_SOMETHING,
  1452. .op_name = "OP_LOCK",
  1453. .op_rsize_bop = (nfsd4op_rsize)nfsd4_lock_rsize,
  1454. .op_set_currentstateid = (stateid_setter)nfsd4_set_lockstateid,
  1455. },
  1456. [OP_LOCKT] = {
  1457. .op_func = (nfsd4op_func)nfsd4_lockt,
  1458. .op_name = "OP_LOCKT",
  1459. },
  1460. [OP_LOCKU] = {
  1461. .op_func = (nfsd4op_func)nfsd4_locku,
  1462. .op_flags = OP_MODIFIES_SOMETHING,
  1463. .op_name = "OP_LOCKU",
  1464. .op_rsize_bop = (nfsd4op_rsize)nfsd4_status_stateid_rsize,
  1465. .op_get_currentstateid = (stateid_getter)nfsd4_get_lockustateid,
  1466. },
  1467. [OP_LOOKUP] = {
  1468. .op_func = (nfsd4op_func)nfsd4_lookup,
  1469. .op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
  1470. .op_name = "OP_LOOKUP",
  1471. },
  1472. [OP_LOOKUPP] = {
  1473. .op_func = (nfsd4op_func)nfsd4_lookupp,
  1474. .op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
  1475. .op_name = "OP_LOOKUPP",
  1476. },
  1477. [OP_NVERIFY] = {
  1478. .op_func = (nfsd4op_func)nfsd4_nverify,
  1479. .op_name = "OP_NVERIFY",
  1480. },
  1481. [OP_OPEN] = {
  1482. .op_func = (nfsd4op_func)nfsd4_open,
  1483. .op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
  1484. .op_name = "OP_OPEN",
  1485. .op_rsize_bop = (nfsd4op_rsize)nfsd4_open_rsize,
  1486. .op_set_currentstateid = (stateid_setter)nfsd4_set_openstateid,
  1487. },
  1488. [OP_OPEN_CONFIRM] = {
  1489. .op_func = (nfsd4op_func)nfsd4_open_confirm,
  1490. .op_flags = OP_MODIFIES_SOMETHING,
  1491. .op_name = "OP_OPEN_CONFIRM",
  1492. .op_rsize_bop = (nfsd4op_rsize)nfsd4_status_stateid_rsize,
  1493. },
  1494. [OP_OPEN_DOWNGRADE] = {
  1495. .op_func = (nfsd4op_func)nfsd4_open_downgrade,
  1496. .op_flags = OP_MODIFIES_SOMETHING,
  1497. .op_name = "OP_OPEN_DOWNGRADE",
  1498. .op_rsize_bop = (nfsd4op_rsize)nfsd4_status_stateid_rsize,
  1499. .op_get_currentstateid = (stateid_getter)nfsd4_get_opendowngradestateid,
  1500. .op_set_currentstateid = (stateid_setter)nfsd4_set_opendowngradestateid,
  1501. },
  1502. [OP_PUTFH] = {
  1503. .op_func = (nfsd4op_func)nfsd4_putfh,
  1504. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1505. | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
  1506. .op_name = "OP_PUTFH",
  1507. .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
  1508. },
  1509. [OP_PUTPUBFH] = {
  1510. .op_func = (nfsd4op_func)nfsd4_putrootfh,
  1511. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1512. | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
  1513. .op_name = "OP_PUTPUBFH",
  1514. .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
  1515. },
  1516. [OP_PUTROOTFH] = {
  1517. .op_func = (nfsd4op_func)nfsd4_putrootfh,
  1518. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1519. | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
  1520. .op_name = "OP_PUTROOTFH",
  1521. .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
  1522. },
  1523. [OP_READ] = {
  1524. .op_func = (nfsd4op_func)nfsd4_read,
  1525. .op_name = "OP_READ",
  1526. .op_rsize_bop = (nfsd4op_rsize)nfsd4_read_rsize,
  1527. .op_get_currentstateid = (stateid_getter)nfsd4_get_readstateid,
  1528. },
  1529. [OP_READDIR] = {
  1530. .op_func = (nfsd4op_func)nfsd4_readdir,
  1531. .op_name = "OP_READDIR",
  1532. .op_rsize_bop = (nfsd4op_rsize)nfsd4_readdir_rsize,
  1533. },
  1534. [OP_READLINK] = {
  1535. .op_func = (nfsd4op_func)nfsd4_readlink,
  1536. .op_name = "OP_READLINK",
  1537. },
  1538. [OP_REMOVE] = {
  1539. .op_func = (nfsd4op_func)nfsd4_remove,
  1540. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  1541. .op_name = "OP_REMOVE",
  1542. .op_rsize_bop = (nfsd4op_rsize)nfsd4_remove_rsize,
  1543. },
  1544. [OP_RENAME] = {
  1545. .op_func = (nfsd4op_func)nfsd4_rename,
  1546. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  1547. .op_name = "OP_RENAME",
  1548. .op_rsize_bop = (nfsd4op_rsize)nfsd4_rename_rsize,
  1549. },
  1550. [OP_RENEW] = {
  1551. .op_func = (nfsd4op_func)nfsd4_renew,
  1552. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1553. | OP_MODIFIES_SOMETHING,
  1554. .op_name = "OP_RENEW",
  1555. .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
  1556. },
  1557. [OP_RESTOREFH] = {
  1558. .op_func = (nfsd4op_func)nfsd4_restorefh,
  1559. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1560. | OP_IS_PUTFH_LIKE | OP_MODIFIES_SOMETHING,
  1561. .op_name = "OP_RESTOREFH",
  1562. .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
  1563. },
  1564. [OP_SAVEFH] = {
  1565. .op_func = (nfsd4op_func)nfsd4_savefh,
  1566. .op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
  1567. .op_name = "OP_SAVEFH",
  1568. .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
  1569. },
  1570. [OP_SECINFO] = {
  1571. .op_func = (nfsd4op_func)nfsd4_secinfo,
  1572. .op_flags = OP_HANDLES_WRONGSEC,
  1573. .op_name = "OP_SECINFO",
  1574. },
  1575. [OP_SETATTR] = {
  1576. .op_func = (nfsd4op_func)nfsd4_setattr,
  1577. .op_name = "OP_SETATTR",
  1578. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  1579. .op_rsize_bop = (nfsd4op_rsize)nfsd4_setattr_rsize,
  1580. .op_get_currentstateid = (stateid_getter)nfsd4_get_setattrstateid,
  1581. },
  1582. [OP_SETCLIENTID] = {
  1583. .op_func = (nfsd4op_func)nfsd4_setclientid,
  1584. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1585. | OP_MODIFIES_SOMETHING | OP_CACHEME,
  1586. .op_name = "OP_SETCLIENTID",
  1587. .op_rsize_bop = (nfsd4op_rsize)nfsd4_setclientid_rsize,
  1588. },
  1589. [OP_SETCLIENTID_CONFIRM] = {
  1590. .op_func = (nfsd4op_func)nfsd4_setclientid_confirm,
  1591. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1592. | OP_MODIFIES_SOMETHING | OP_CACHEME,
  1593. .op_name = "OP_SETCLIENTID_CONFIRM",
  1594. .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
  1595. },
  1596. [OP_VERIFY] = {
  1597. .op_func = (nfsd4op_func)nfsd4_verify,
  1598. .op_name = "OP_VERIFY",
  1599. },
  1600. [OP_WRITE] = {
  1601. .op_func = (nfsd4op_func)nfsd4_write,
  1602. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  1603. .op_name = "OP_WRITE",
  1604. .op_rsize_bop = (nfsd4op_rsize)nfsd4_write_rsize,
  1605. .op_get_currentstateid = (stateid_getter)nfsd4_get_writestateid,
  1606. },
  1607. [OP_RELEASE_LOCKOWNER] = {
  1608. .op_func = (nfsd4op_func)nfsd4_release_lockowner,
  1609. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1610. | OP_MODIFIES_SOMETHING,
  1611. .op_name = "OP_RELEASE_LOCKOWNER",
  1612. .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
  1613. },
  1614. /* NFSv4.1 operations */
  1615. [OP_EXCHANGE_ID] = {
  1616. .op_func = (nfsd4op_func)nfsd4_exchange_id,
  1617. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
  1618. | OP_MODIFIES_SOMETHING,
  1619. .op_name = "OP_EXCHANGE_ID",
  1620. .op_rsize_bop = (nfsd4op_rsize)nfsd4_exchange_id_rsize,
  1621. },
  1622. [OP_BACKCHANNEL_CTL] = {
  1623. .op_func = (nfsd4op_func)nfsd4_backchannel_ctl,
  1624. .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
  1625. .op_name = "OP_BACKCHANNEL_CTL",
  1626. .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
  1627. },
  1628. [OP_BIND_CONN_TO_SESSION] = {
  1629. .op_func = (nfsd4op_func)nfsd4_bind_conn_to_session,
  1630. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
  1631. | OP_MODIFIES_SOMETHING,
  1632. .op_name = "OP_BIND_CONN_TO_SESSION",
  1633. .op_rsize_bop = (nfsd4op_rsize)nfsd4_bind_conn_to_session_rsize,
  1634. },
  1635. [OP_CREATE_SESSION] = {
  1636. .op_func = (nfsd4op_func)nfsd4_create_session,
  1637. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
  1638. | OP_MODIFIES_SOMETHING,
  1639. .op_name = "OP_CREATE_SESSION",
  1640. .op_rsize_bop = (nfsd4op_rsize)nfsd4_create_session_rsize,
  1641. },
  1642. [OP_DESTROY_SESSION] = {
  1643. .op_func = (nfsd4op_func)nfsd4_destroy_session,
  1644. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
  1645. | OP_MODIFIES_SOMETHING,
  1646. .op_name = "OP_DESTROY_SESSION",
  1647. .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
  1648. },
  1649. [OP_SEQUENCE] = {
  1650. .op_func = (nfsd4op_func)nfsd4_sequence,
  1651. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
  1652. .op_name = "OP_SEQUENCE",
  1653. },
  1654. [OP_DESTROY_CLIENTID] = {
  1655. .op_func = (nfsd4op_func)nfsd4_destroy_clientid,
  1656. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
  1657. | OP_MODIFIES_SOMETHING,
  1658. .op_name = "OP_DESTROY_CLIENTID",
  1659. .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
  1660. },
  1661. [OP_RECLAIM_COMPLETE] = {
  1662. .op_func = (nfsd4op_func)nfsd4_reclaim_complete,
  1663. .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
  1664. .op_name = "OP_RECLAIM_COMPLETE",
  1665. .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
  1666. },
  1667. [OP_SECINFO_NO_NAME] = {
  1668. .op_func = (nfsd4op_func)nfsd4_secinfo_no_name,
  1669. .op_flags = OP_HANDLES_WRONGSEC,
  1670. .op_name = "OP_SECINFO_NO_NAME",
  1671. },
  1672. [OP_TEST_STATEID] = {
  1673. .op_func = (nfsd4op_func)nfsd4_test_stateid,
  1674. .op_flags = ALLOWED_WITHOUT_FH,
  1675. .op_name = "OP_TEST_STATEID",
  1676. },
  1677. [OP_FREE_STATEID] = {
  1678. .op_func = (nfsd4op_func)nfsd4_free_stateid,
  1679. .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
  1680. .op_name = "OP_FREE_STATEID",
  1681. .op_get_currentstateid = (stateid_getter)nfsd4_get_freestateid,
  1682. .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
  1683. },
  1684. };
  1685. int nfsd4_max_reply(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1686. {
  1687. struct nfsd4_operation *opdesc;
  1688. nfsd4op_rsize estimator;
  1689. if (op->opnum == OP_ILLEGAL)
  1690. return op_encode_hdr_size * sizeof(__be32);
  1691. opdesc = OPDESC(op);
  1692. estimator = opdesc->op_rsize_bop;
  1693. return estimator ? estimator(rqstp, op) : PAGE_SIZE;
  1694. }
  1695. void warn_on_nonidempotent_op(struct nfsd4_op *op)
  1696. {
  1697. if (OPDESC(op)->op_flags & OP_MODIFIES_SOMETHING) {
  1698. pr_err("unable to encode reply to nonidempotent op %d (%s)\n",
  1699. op->opnum, nfsd4_op_name(op->opnum));
  1700. WARN_ON_ONCE(1);
  1701. }
  1702. }
  1703. static const char *nfsd4_op_name(unsigned opnum)
  1704. {
  1705. if (opnum < ARRAY_SIZE(nfsd4_ops))
  1706. return nfsd4_ops[opnum].op_name;
  1707. return "unknown_operation";
  1708. }
  1709. #define nfsd4_voidres nfsd4_voidargs
  1710. struct nfsd4_voidargs { int dummy; };
  1711. static struct svc_procedure nfsd_procedures4[2] = {
  1712. [NFSPROC4_NULL] = {
  1713. .pc_func = (svc_procfunc) nfsd4_proc_null,
  1714. .pc_encode = (kxdrproc_t) nfs4svc_encode_voidres,
  1715. .pc_argsize = sizeof(struct nfsd4_voidargs),
  1716. .pc_ressize = sizeof(struct nfsd4_voidres),
  1717. .pc_cachetype = RC_NOCACHE,
  1718. .pc_xdrressize = 1,
  1719. },
  1720. [NFSPROC4_COMPOUND] = {
  1721. .pc_func = (svc_procfunc) nfsd4_proc_compound,
  1722. .pc_decode = (kxdrproc_t) nfs4svc_decode_compoundargs,
  1723. .pc_encode = (kxdrproc_t) nfs4svc_encode_compoundres,
  1724. .pc_argsize = sizeof(struct nfsd4_compoundargs),
  1725. .pc_ressize = sizeof(struct nfsd4_compoundres),
  1726. .pc_release = nfsd4_release_compoundargs,
  1727. .pc_cachetype = RC_NOCACHE,
  1728. .pc_xdrressize = NFSD_BUFSIZE/4,
  1729. },
  1730. };
  1731. struct svc_version nfsd_version4 = {
  1732. .vs_vers = 4,
  1733. .vs_nproc = 2,
  1734. .vs_proc = nfsd_procedures4,
  1735. .vs_dispatch = nfsd_dispatch,
  1736. .vs_xdrsize = NFS4_SVC_XDRSIZE,
  1737. .vs_rpcb_optnl = 1,
  1738. };
  1739. /*
  1740. * Local variables:
  1741. * c-basic-offset: 8
  1742. * End:
  1743. */