nfs4proc.c 69 KB

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