nfs4proc.c 57 KB

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