nfs4proc.c 66 KB

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