nfs4proc.c 70 KB

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