nfs4proc.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  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 "cache.h"
  38. #include "xdr4.h"
  39. #include "vfs.h"
  40. #define NFSDDBG_FACILITY NFSDDBG_PROC
  41. static u32 nfsd_attrmask[] = {
  42. NFSD_WRITEABLE_ATTRS_WORD0,
  43. NFSD_WRITEABLE_ATTRS_WORD1,
  44. NFSD_WRITEABLE_ATTRS_WORD2
  45. };
  46. static u32 nfsd41_ex_attrmask[] = {
  47. NFSD_SUPPATTR_EXCLCREAT_WORD0,
  48. NFSD_SUPPATTR_EXCLCREAT_WORD1,
  49. NFSD_SUPPATTR_EXCLCREAT_WORD2
  50. };
  51. static __be32
  52. check_attr_support(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  53. u32 *bmval, u32 *writable)
  54. {
  55. struct dentry *dentry = cstate->current_fh.fh_dentry;
  56. /*
  57. * Check about attributes are supported by the NFSv4 server or not.
  58. * According to spec, unsupported attributes return ERR_ATTRNOTSUPP.
  59. */
  60. if ((bmval[0] & ~nfsd_suppattrs0(cstate->minorversion)) ||
  61. (bmval[1] & ~nfsd_suppattrs1(cstate->minorversion)) ||
  62. (bmval[2] & ~nfsd_suppattrs2(cstate->minorversion)))
  63. return nfserr_attrnotsupp;
  64. /*
  65. * Check FATTR4_WORD0_ACL can be supported
  66. * in current environment or not.
  67. */
  68. if (bmval[0] & FATTR4_WORD0_ACL) {
  69. if (!IS_POSIXACL(dentry->d_inode))
  70. return nfserr_attrnotsupp;
  71. }
  72. /*
  73. * According to spec, read-only attributes return ERR_INVAL.
  74. */
  75. if (writable) {
  76. if ((bmval[0] & ~writable[0]) || (bmval[1] & ~writable[1]) ||
  77. (bmval[2] & ~writable[2]))
  78. return nfserr_inval;
  79. }
  80. return nfs_ok;
  81. }
  82. static __be32
  83. nfsd4_check_open_attributes(struct svc_rqst *rqstp,
  84. struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
  85. {
  86. __be32 status = nfs_ok;
  87. if (open->op_create == NFS4_OPEN_CREATE) {
  88. if (open->op_createmode == NFS4_CREATE_UNCHECKED
  89. || open->op_createmode == NFS4_CREATE_GUARDED)
  90. status = check_attr_support(rqstp, cstate,
  91. open->op_bmval, nfsd_attrmask);
  92. else if (open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1)
  93. status = check_attr_support(rqstp, cstate,
  94. open->op_bmval, nfsd41_ex_attrmask);
  95. }
  96. return status;
  97. }
  98. static int
  99. is_create_with_attrs(struct nfsd4_open *open)
  100. {
  101. return open->op_create == NFS4_OPEN_CREATE
  102. && (open->op_createmode == NFS4_CREATE_UNCHECKED
  103. || open->op_createmode == NFS4_CREATE_GUARDED
  104. || open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1);
  105. }
  106. /*
  107. * if error occurs when setting the acl, just clear the acl bit
  108. * in the returned attr bitmap.
  109. */
  110. static void
  111. do_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
  112. struct nfs4_acl *acl, u32 *bmval)
  113. {
  114. __be32 status;
  115. status = nfsd4_set_nfs4_acl(rqstp, fhp, acl);
  116. if (status)
  117. /*
  118. * We should probably fail the whole open at this point,
  119. * but we've already created the file, so it's too late;
  120. * So this seems the least of evils:
  121. */
  122. bmval[0] &= ~FATTR4_WORD0_ACL;
  123. }
  124. static inline void
  125. fh_dup2(struct svc_fh *dst, struct svc_fh *src)
  126. {
  127. fh_put(dst);
  128. dget(src->fh_dentry);
  129. if (src->fh_export)
  130. cache_get(&src->fh_export->h);
  131. *dst = *src;
  132. }
  133. static __be32
  134. do_open_permission(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open, int accmode)
  135. {
  136. __be32 status;
  137. if (open->op_truncate &&
  138. !(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
  139. return nfserr_inval;
  140. if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
  141. accmode |= NFSD_MAY_READ;
  142. if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
  143. accmode |= (NFSD_MAY_WRITE | NFSD_MAY_TRUNC);
  144. if (open->op_share_deny & NFS4_SHARE_DENY_READ)
  145. accmode |= NFSD_MAY_WRITE;
  146. status = fh_verify(rqstp, current_fh, S_IFREG, accmode);
  147. return status;
  148. }
  149. static __be32
  150. do_open_lookup(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
  151. {
  152. struct svc_fh resfh;
  153. __be32 status;
  154. int created = 0;
  155. fh_init(&resfh, NFS4_FHSIZE);
  156. open->op_truncate = 0;
  157. if (open->op_create) {
  158. /* FIXME: check session persistence and pnfs flags.
  159. * The nfsv4.1 spec requires the following semantics:
  160. *
  161. * Persistent | pNFS | Server REQUIRED | Client Allowed
  162. * Reply Cache | server | |
  163. * -------------+--------+-----------------+--------------------
  164. * no | no | EXCLUSIVE4_1 | EXCLUSIVE4_1
  165. * | | | (SHOULD)
  166. * | | and EXCLUSIVE4 | or EXCLUSIVE4
  167. * | | | (SHOULD NOT)
  168. * no | yes | EXCLUSIVE4_1 | EXCLUSIVE4_1
  169. * yes | no | GUARDED4 | GUARDED4
  170. * yes | yes | GUARDED4 | GUARDED4
  171. */
  172. /*
  173. * Note: create modes (UNCHECKED,GUARDED...) are the same
  174. * in NFSv4 as in v3 except EXCLUSIVE4_1.
  175. */
  176. status = do_nfsd_create(rqstp, current_fh, open->op_fname.data,
  177. open->op_fname.len, &open->op_iattr,
  178. &resfh, open->op_createmode,
  179. (u32 *)open->op_verf.data,
  180. &open->op_truncate, &created);
  181. /*
  182. * Following rfc 3530 14.2.16, use the returned bitmask
  183. * to indicate which attributes we used to store the
  184. * verifier:
  185. */
  186. if (open->op_createmode == NFS4_CREATE_EXCLUSIVE && status == 0)
  187. open->op_bmval[1] = (FATTR4_WORD1_TIME_ACCESS |
  188. FATTR4_WORD1_TIME_MODIFY);
  189. } else {
  190. status = nfsd_lookup(rqstp, current_fh,
  191. open->op_fname.data, open->op_fname.len, &resfh);
  192. fh_unlock(current_fh);
  193. }
  194. if (status)
  195. goto out;
  196. if (is_create_with_attrs(open) && open->op_acl != NULL)
  197. do_set_nfs4_acl(rqstp, &resfh, open->op_acl, open->op_bmval);
  198. set_change_info(&open->op_cinfo, current_fh);
  199. fh_dup2(current_fh, &resfh);
  200. /* set reply cache */
  201. fh_copy_shallow(&open->op_stateowner->so_replay.rp_openfh,
  202. &resfh.fh_handle);
  203. if (!created)
  204. status = do_open_permission(rqstp, current_fh, open,
  205. NFSD_MAY_NOP);
  206. out:
  207. fh_put(&resfh);
  208. return status;
  209. }
  210. static __be32
  211. do_open_fhandle(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
  212. {
  213. __be32 status;
  214. /* Only reclaims from previously confirmed clients are valid */
  215. if ((status = nfs4_check_open_reclaim(&open->op_clientid)))
  216. return status;
  217. /* We don't know the target directory, and therefore can not
  218. * set the change info
  219. */
  220. memset(&open->op_cinfo, 0, sizeof(struct nfsd4_change_info));
  221. /* set replay cache */
  222. fh_copy_shallow(&open->op_stateowner->so_replay.rp_openfh,
  223. &current_fh->fh_handle);
  224. open->op_truncate = (open->op_iattr.ia_valid & ATTR_SIZE) &&
  225. (open->op_iattr.ia_size == 0);
  226. status = do_open_permission(rqstp, current_fh, open,
  227. NFSD_MAY_OWNER_OVERRIDE);
  228. return status;
  229. }
  230. static void
  231. copy_clientid(clientid_t *clid, struct nfsd4_session *session)
  232. {
  233. struct nfsd4_sessionid *sid =
  234. (struct nfsd4_sessionid *)session->se_sessionid.data;
  235. clid->cl_boot = sid->clientid.cl_boot;
  236. clid->cl_id = sid->clientid.cl_id;
  237. }
  238. static __be32
  239. nfsd4_open(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  240. struct nfsd4_open *open)
  241. {
  242. __be32 status;
  243. struct nfsd4_compoundres *resp;
  244. dprintk("NFSD: nfsd4_open filename %.*s op_stateowner %p\n",
  245. (int)open->op_fname.len, open->op_fname.data,
  246. open->op_stateowner);
  247. /* This check required by spec. */
  248. if (open->op_create && open->op_claim_type != NFS4_OPEN_CLAIM_NULL)
  249. return nfserr_inval;
  250. /*
  251. * RFC5661 18.51.3
  252. * Before RECLAIM_COMPLETE done, server should deny new lock
  253. */
  254. if (nfsd4_has_session(cstate) &&
  255. !cstate->session->se_client->cl_firststate &&
  256. open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
  257. return nfserr_grace;
  258. if (nfsd4_has_session(cstate))
  259. copy_clientid(&open->op_clientid, cstate->session);
  260. nfs4_lock_state();
  261. /* check seqid for replay. set nfs4_owner */
  262. resp = rqstp->rq_resp;
  263. status = nfsd4_process_open1(&resp->cstate, open);
  264. if (status == nfserr_replay_me) {
  265. struct nfs4_replay *rp = &open->op_stateowner->so_replay;
  266. fh_put(&cstate->current_fh);
  267. fh_copy_shallow(&cstate->current_fh.fh_handle,
  268. &rp->rp_openfh);
  269. status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
  270. if (status)
  271. dprintk("nfsd4_open: replay failed"
  272. " restoring previous filehandle\n");
  273. else
  274. status = nfserr_replay_me;
  275. }
  276. if (status)
  277. goto out;
  278. status = nfsd4_check_open_attributes(rqstp, cstate, open);
  279. if (status)
  280. goto out;
  281. /* Openowner is now set, so sequence id will get bumped. Now we need
  282. * these checks before we do any creates: */
  283. status = nfserr_grace;
  284. if (locks_in_grace() && open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
  285. goto out;
  286. status = nfserr_no_grace;
  287. if (!locks_in_grace() && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
  288. goto out;
  289. switch (open->op_claim_type) {
  290. case NFS4_OPEN_CLAIM_DELEGATE_CUR:
  291. case NFS4_OPEN_CLAIM_NULL:
  292. /*
  293. * (1) set CURRENT_FH to the file being opened,
  294. * creating it if necessary, (2) set open->op_cinfo,
  295. * (3) set open->op_truncate if the file is to be
  296. * truncated after opening, (4) do permission checking.
  297. */
  298. status = do_open_lookup(rqstp, &cstate->current_fh,
  299. open);
  300. if (status)
  301. goto out;
  302. break;
  303. case NFS4_OPEN_CLAIM_PREVIOUS:
  304. open->op_stateowner->so_confirmed = 1;
  305. /*
  306. * The CURRENT_FH is already set to the file being
  307. * opened. (1) set open->op_cinfo, (2) set
  308. * open->op_truncate if the file is to be truncated
  309. * after opening, (3) do permission checking.
  310. */
  311. status = do_open_fhandle(rqstp, &cstate->current_fh,
  312. open);
  313. if (status)
  314. goto out;
  315. break;
  316. case NFS4_OPEN_CLAIM_DELEGATE_PREV:
  317. open->op_stateowner->so_confirmed = 1;
  318. dprintk("NFSD: unsupported OPEN claim type %d\n",
  319. open->op_claim_type);
  320. status = nfserr_notsupp;
  321. goto out;
  322. default:
  323. dprintk("NFSD: Invalid OPEN claim type %d\n",
  324. open->op_claim_type);
  325. status = nfserr_inval;
  326. goto out;
  327. }
  328. /*
  329. * nfsd4_process_open2() does the actual opening of the file. If
  330. * successful, it (1) truncates the file if open->op_truncate was
  331. * set, (2) sets open->op_stateid, (3) sets open->op_delegation.
  332. */
  333. status = nfsd4_process_open2(rqstp, &cstate->current_fh, open);
  334. out:
  335. if (open->op_stateowner) {
  336. nfs4_get_stateowner(open->op_stateowner);
  337. cstate->replay_owner = open->op_stateowner;
  338. }
  339. nfs4_unlock_state();
  340. return status;
  341. }
  342. /*
  343. * filehandle-manipulating ops.
  344. */
  345. static __be32
  346. nfsd4_getfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  347. struct svc_fh **getfh)
  348. {
  349. if (!cstate->current_fh.fh_dentry)
  350. return nfserr_nofilehandle;
  351. *getfh = &cstate->current_fh;
  352. return nfs_ok;
  353. }
  354. static __be32
  355. nfsd4_putfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  356. struct nfsd4_putfh *putfh)
  357. {
  358. fh_put(&cstate->current_fh);
  359. cstate->current_fh.fh_handle.fh_size = putfh->pf_fhlen;
  360. memcpy(&cstate->current_fh.fh_handle.fh_base, putfh->pf_fhval,
  361. putfh->pf_fhlen);
  362. return fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_BYPASS_GSS);
  363. }
  364. static __be32
  365. nfsd4_putrootfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  366. void *arg)
  367. {
  368. __be32 status;
  369. fh_put(&cstate->current_fh);
  370. status = exp_pseudoroot(rqstp, &cstate->current_fh);
  371. return status;
  372. }
  373. static __be32
  374. nfsd4_restorefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  375. void *arg)
  376. {
  377. if (!cstate->save_fh.fh_dentry)
  378. return nfserr_restorefh;
  379. fh_dup2(&cstate->current_fh, &cstate->save_fh);
  380. return nfs_ok;
  381. }
  382. static __be32
  383. nfsd4_savefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  384. void *arg)
  385. {
  386. if (!cstate->current_fh.fh_dentry)
  387. return nfserr_nofilehandle;
  388. fh_dup2(&cstate->save_fh, &cstate->current_fh);
  389. return nfs_ok;
  390. }
  391. /*
  392. * misc nfsv4 ops
  393. */
  394. static __be32
  395. nfsd4_access(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  396. struct nfsd4_access *access)
  397. {
  398. if (access->ac_req_access & ~NFS3_ACCESS_FULL)
  399. return nfserr_inval;
  400. access->ac_resp_access = access->ac_req_access;
  401. return nfsd_access(rqstp, &cstate->current_fh, &access->ac_resp_access,
  402. &access->ac_supported);
  403. }
  404. static __be32
  405. nfsd4_commit(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  406. struct nfsd4_commit *commit)
  407. {
  408. __be32 status;
  409. u32 *p = (u32 *)commit->co_verf.data;
  410. *p++ = nfssvc_boot.tv_sec;
  411. *p++ = nfssvc_boot.tv_usec;
  412. status = nfsd_commit(rqstp, &cstate->current_fh, commit->co_offset,
  413. commit->co_count);
  414. if (status == nfserr_symlink)
  415. status = nfserr_inval;
  416. return status;
  417. }
  418. static __be32
  419. nfsd4_create(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  420. struct nfsd4_create *create)
  421. {
  422. struct svc_fh resfh;
  423. __be32 status;
  424. dev_t rdev;
  425. fh_init(&resfh, NFS4_FHSIZE);
  426. status = fh_verify(rqstp, &cstate->current_fh, S_IFDIR,
  427. NFSD_MAY_CREATE);
  428. if (status == nfserr_symlink)
  429. status = nfserr_notdir;
  430. if (status)
  431. return status;
  432. status = check_attr_support(rqstp, cstate, create->cr_bmval,
  433. nfsd_attrmask);
  434. if (status)
  435. return status;
  436. switch (create->cr_type) {
  437. case NF4LNK:
  438. /* ugh! we have to null-terminate the linktext, or
  439. * vfs_symlink() will choke. it is always safe to
  440. * null-terminate by brute force, since at worst we
  441. * will overwrite the first byte of the create namelen
  442. * in the XDR buffer, which has already been extracted
  443. * during XDR decode.
  444. */
  445. create->cr_linkname[create->cr_linklen] = 0;
  446. status = nfsd_symlink(rqstp, &cstate->current_fh,
  447. create->cr_name, create->cr_namelen,
  448. create->cr_linkname, create->cr_linklen,
  449. &resfh, &create->cr_iattr);
  450. break;
  451. case NF4BLK:
  452. rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
  453. if (MAJOR(rdev) != create->cr_specdata1 ||
  454. MINOR(rdev) != create->cr_specdata2)
  455. return nfserr_inval;
  456. status = nfsd_create(rqstp, &cstate->current_fh,
  457. create->cr_name, create->cr_namelen,
  458. &create->cr_iattr, S_IFBLK, rdev, &resfh);
  459. break;
  460. case NF4CHR:
  461. rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
  462. if (MAJOR(rdev) != create->cr_specdata1 ||
  463. MINOR(rdev) != create->cr_specdata2)
  464. return nfserr_inval;
  465. status = nfsd_create(rqstp, &cstate->current_fh,
  466. create->cr_name, create->cr_namelen,
  467. &create->cr_iattr,S_IFCHR, rdev, &resfh);
  468. break;
  469. case NF4SOCK:
  470. status = nfsd_create(rqstp, &cstate->current_fh,
  471. create->cr_name, create->cr_namelen,
  472. &create->cr_iattr, S_IFSOCK, 0, &resfh);
  473. break;
  474. case NF4FIFO:
  475. status = nfsd_create(rqstp, &cstate->current_fh,
  476. create->cr_name, create->cr_namelen,
  477. &create->cr_iattr, S_IFIFO, 0, &resfh);
  478. break;
  479. case NF4DIR:
  480. create->cr_iattr.ia_valid &= ~ATTR_SIZE;
  481. status = nfsd_create(rqstp, &cstate->current_fh,
  482. create->cr_name, create->cr_namelen,
  483. &create->cr_iattr, S_IFDIR, 0, &resfh);
  484. break;
  485. default:
  486. status = nfserr_badtype;
  487. }
  488. if (status)
  489. goto out;
  490. if (create->cr_acl != NULL)
  491. do_set_nfs4_acl(rqstp, &resfh, create->cr_acl,
  492. create->cr_bmval);
  493. fh_unlock(&cstate->current_fh);
  494. set_change_info(&create->cr_cinfo, &cstate->current_fh);
  495. fh_dup2(&cstate->current_fh, &resfh);
  496. out:
  497. fh_put(&resfh);
  498. return status;
  499. }
  500. static __be32
  501. nfsd4_getattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  502. struct nfsd4_getattr *getattr)
  503. {
  504. __be32 status;
  505. status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
  506. if (status)
  507. return status;
  508. if (getattr->ga_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
  509. return nfserr_inval;
  510. getattr->ga_bmval[0] &= nfsd_suppattrs0(cstate->minorversion);
  511. getattr->ga_bmval[1] &= nfsd_suppattrs1(cstate->minorversion);
  512. getattr->ga_bmval[2] &= nfsd_suppattrs2(cstate->minorversion);
  513. getattr->ga_fhp = &cstate->current_fh;
  514. return nfs_ok;
  515. }
  516. static __be32
  517. nfsd4_link(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  518. struct nfsd4_link *link)
  519. {
  520. __be32 status = nfserr_nofilehandle;
  521. if (!cstate->save_fh.fh_dentry)
  522. return status;
  523. status = nfsd_link(rqstp, &cstate->current_fh,
  524. link->li_name, link->li_namelen, &cstate->save_fh);
  525. if (!status)
  526. set_change_info(&link->li_cinfo, &cstate->current_fh);
  527. return status;
  528. }
  529. static __be32 nfsd4_do_lookupp(struct svc_rqst *rqstp, struct svc_fh *fh)
  530. {
  531. struct svc_fh tmp_fh;
  532. __be32 ret;
  533. fh_init(&tmp_fh, NFS4_FHSIZE);
  534. ret = exp_pseudoroot(rqstp, &tmp_fh);
  535. if (ret)
  536. return ret;
  537. if (tmp_fh.fh_dentry == fh->fh_dentry) {
  538. fh_put(&tmp_fh);
  539. return nfserr_noent;
  540. }
  541. fh_put(&tmp_fh);
  542. return nfsd_lookup(rqstp, fh, "..", 2, fh);
  543. }
  544. static __be32
  545. nfsd4_lookupp(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  546. void *arg)
  547. {
  548. return nfsd4_do_lookupp(rqstp, &cstate->current_fh);
  549. }
  550. static __be32
  551. nfsd4_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  552. struct nfsd4_lookup *lookup)
  553. {
  554. return nfsd_lookup(rqstp, &cstate->current_fh,
  555. lookup->lo_name, lookup->lo_len,
  556. &cstate->current_fh);
  557. }
  558. static __be32
  559. nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  560. struct nfsd4_read *read)
  561. {
  562. __be32 status;
  563. /* no need to check permission - this will be done in nfsd_read() */
  564. read->rd_filp = NULL;
  565. if (read->rd_offset >= OFFSET_MAX)
  566. return nfserr_inval;
  567. nfs4_lock_state();
  568. /* check stateid */
  569. if ((status = nfs4_preprocess_stateid_op(cstate, &read->rd_stateid,
  570. RD_STATE, &read->rd_filp))) {
  571. dprintk("NFSD: nfsd4_read: couldn't process stateid!\n");
  572. goto out;
  573. }
  574. if (read->rd_filp)
  575. get_file(read->rd_filp);
  576. status = nfs_ok;
  577. out:
  578. nfs4_unlock_state();
  579. read->rd_rqstp = rqstp;
  580. read->rd_fhp = &cstate->current_fh;
  581. return status;
  582. }
  583. static __be32
  584. nfsd4_readdir(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  585. struct nfsd4_readdir *readdir)
  586. {
  587. u64 cookie = readdir->rd_cookie;
  588. static const nfs4_verifier zeroverf;
  589. /* no need to check permission - this will be done in nfsd_readdir() */
  590. if (readdir->rd_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
  591. return nfserr_inval;
  592. readdir->rd_bmval[0] &= nfsd_suppattrs0(cstate->minorversion);
  593. readdir->rd_bmval[1] &= nfsd_suppattrs1(cstate->minorversion);
  594. readdir->rd_bmval[2] &= nfsd_suppattrs2(cstate->minorversion);
  595. if ((cookie > ~(u32)0) || (cookie == 1) || (cookie == 2) ||
  596. (cookie == 0 && memcmp(readdir->rd_verf.data, zeroverf.data, NFS4_VERIFIER_SIZE)))
  597. return nfserr_bad_cookie;
  598. readdir->rd_rqstp = rqstp;
  599. readdir->rd_fhp = &cstate->current_fh;
  600. return nfs_ok;
  601. }
  602. static __be32
  603. nfsd4_readlink(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  604. struct nfsd4_readlink *readlink)
  605. {
  606. readlink->rl_rqstp = rqstp;
  607. readlink->rl_fhp = &cstate->current_fh;
  608. return nfs_ok;
  609. }
  610. static __be32
  611. nfsd4_remove(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  612. struct nfsd4_remove *remove)
  613. {
  614. __be32 status;
  615. if (locks_in_grace())
  616. return nfserr_grace;
  617. status = nfsd_unlink(rqstp, &cstate->current_fh, 0,
  618. remove->rm_name, remove->rm_namelen);
  619. if (status == nfserr_symlink)
  620. return nfserr_notdir;
  621. if (!status) {
  622. fh_unlock(&cstate->current_fh);
  623. set_change_info(&remove->rm_cinfo, &cstate->current_fh);
  624. }
  625. return status;
  626. }
  627. static __be32
  628. nfsd4_rename(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  629. struct nfsd4_rename *rename)
  630. {
  631. __be32 status = nfserr_nofilehandle;
  632. if (!cstate->save_fh.fh_dentry)
  633. return status;
  634. if (locks_in_grace() && !(cstate->save_fh.fh_export->ex_flags
  635. & NFSEXP_NOSUBTREECHECK))
  636. return nfserr_grace;
  637. status = nfsd_rename(rqstp, &cstate->save_fh, rename->rn_sname,
  638. rename->rn_snamelen, &cstate->current_fh,
  639. rename->rn_tname, rename->rn_tnamelen);
  640. /* the underlying filesystem returns different error's than required
  641. * by NFSv4. both save_fh and current_fh have been verified.. */
  642. if (status == nfserr_isdir)
  643. status = nfserr_exist;
  644. else if ((status == nfserr_notdir) &&
  645. (S_ISDIR(cstate->save_fh.fh_dentry->d_inode->i_mode) &&
  646. S_ISDIR(cstate->current_fh.fh_dentry->d_inode->i_mode)))
  647. status = nfserr_exist;
  648. else if (status == nfserr_symlink)
  649. status = nfserr_notdir;
  650. if (!status) {
  651. set_change_info(&rename->rn_sinfo, &cstate->current_fh);
  652. set_change_info(&rename->rn_tinfo, &cstate->save_fh);
  653. }
  654. return status;
  655. }
  656. static __be32
  657. nfsd4_secinfo(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  658. struct nfsd4_secinfo *secinfo)
  659. {
  660. struct svc_fh resfh;
  661. struct svc_export *exp;
  662. struct dentry *dentry;
  663. __be32 err;
  664. fh_init(&resfh, NFS4_FHSIZE);
  665. err = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_EXEC);
  666. if (err)
  667. return err;
  668. err = nfsd_lookup_dentry(rqstp, &cstate->current_fh,
  669. secinfo->si_name, secinfo->si_namelen,
  670. &exp, &dentry);
  671. if (err)
  672. return err;
  673. if (dentry->d_inode == NULL) {
  674. exp_put(exp);
  675. err = nfserr_noent;
  676. } else
  677. secinfo->si_exp = exp;
  678. dput(dentry);
  679. if (cstate->minorversion)
  680. /* See rfc 5661 section 2.6.3.1.1.8 */
  681. fh_put(&cstate->current_fh);
  682. return err;
  683. }
  684. static __be32
  685. nfsd4_secinfo_no_name(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  686. struct nfsd4_secinfo_no_name *sin)
  687. {
  688. __be32 err;
  689. switch (sin->sin_style) {
  690. case NFS4_SECINFO_STYLE4_CURRENT_FH:
  691. break;
  692. case NFS4_SECINFO_STYLE4_PARENT:
  693. err = nfsd4_do_lookupp(rqstp, &cstate->current_fh);
  694. if (err)
  695. return err;
  696. break;
  697. default:
  698. return nfserr_inval;
  699. }
  700. exp_get(cstate->current_fh.fh_export);
  701. sin->sin_exp = cstate->current_fh.fh_export;
  702. fh_put(&cstate->current_fh);
  703. return nfs_ok;
  704. }
  705. static __be32
  706. nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  707. struct nfsd4_setattr *setattr)
  708. {
  709. __be32 status = nfs_ok;
  710. if (setattr->sa_iattr.ia_valid & ATTR_SIZE) {
  711. nfs4_lock_state();
  712. status = nfs4_preprocess_stateid_op(cstate,
  713. &setattr->sa_stateid, WR_STATE, NULL);
  714. nfs4_unlock_state();
  715. if (status) {
  716. dprintk("NFSD: nfsd4_setattr: couldn't process stateid!\n");
  717. return status;
  718. }
  719. }
  720. status = mnt_want_write(cstate->current_fh.fh_export->ex_path.mnt);
  721. if (status)
  722. return status;
  723. status = nfs_ok;
  724. status = check_attr_support(rqstp, cstate, setattr->sa_bmval,
  725. nfsd_attrmask);
  726. if (status)
  727. goto out;
  728. if (setattr->sa_acl != NULL)
  729. status = nfsd4_set_nfs4_acl(rqstp, &cstate->current_fh,
  730. setattr->sa_acl);
  731. if (status)
  732. goto out;
  733. status = nfsd_setattr(rqstp, &cstate->current_fh, &setattr->sa_iattr,
  734. 0, (time_t)0);
  735. out:
  736. mnt_drop_write(cstate->current_fh.fh_export->ex_path.mnt);
  737. return status;
  738. }
  739. static __be32
  740. nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  741. struct nfsd4_write *write)
  742. {
  743. stateid_t *stateid = &write->wr_stateid;
  744. struct file *filp = NULL;
  745. u32 *p;
  746. __be32 status = nfs_ok;
  747. unsigned long cnt;
  748. /* no need to check permission - this will be done in nfsd_write() */
  749. if (write->wr_offset >= OFFSET_MAX)
  750. return nfserr_inval;
  751. nfs4_lock_state();
  752. status = nfs4_preprocess_stateid_op(cstate, stateid, WR_STATE, &filp);
  753. if (filp)
  754. get_file(filp);
  755. nfs4_unlock_state();
  756. if (status) {
  757. dprintk("NFSD: nfsd4_write: couldn't process stateid!\n");
  758. return status;
  759. }
  760. cnt = write->wr_buflen;
  761. write->wr_how_written = write->wr_stable_how;
  762. p = (u32 *)write->wr_verifier.data;
  763. *p++ = nfssvc_boot.tv_sec;
  764. *p++ = nfssvc_boot.tv_usec;
  765. status = nfsd_write(rqstp, &cstate->current_fh, filp,
  766. write->wr_offset, rqstp->rq_vec, write->wr_vlen,
  767. &cnt, &write->wr_how_written);
  768. if (filp)
  769. fput(filp);
  770. write->wr_bytes_written = cnt;
  771. if (status == nfserr_symlink)
  772. status = nfserr_inval;
  773. return status;
  774. }
  775. /* This routine never returns NFS_OK! If there are no other errors, it
  776. * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the
  777. * attributes matched. VERIFY is implemented by mapping NFSERR_SAME
  778. * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK.
  779. */
  780. static __be32
  781. _nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  782. struct nfsd4_verify *verify)
  783. {
  784. __be32 *buf, *p;
  785. int count;
  786. __be32 status;
  787. status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
  788. if (status)
  789. return status;
  790. status = check_attr_support(rqstp, cstate, verify->ve_bmval, NULL);
  791. if (status)
  792. return status;
  793. if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR)
  794. || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1))
  795. return nfserr_inval;
  796. if (verify->ve_attrlen & 3)
  797. return nfserr_inval;
  798. /* count in words:
  799. * bitmap_len(1) + bitmap(2) + attr_len(1) = 4
  800. */
  801. count = 4 + (verify->ve_attrlen >> 2);
  802. buf = kmalloc(count << 2, GFP_KERNEL);
  803. if (!buf)
  804. return nfserr_resource;
  805. status = nfsd4_encode_fattr(&cstate->current_fh,
  806. cstate->current_fh.fh_export,
  807. cstate->current_fh.fh_dentry, buf,
  808. &count, verify->ve_bmval,
  809. rqstp, 0);
  810. /* this means that nfsd4_encode_fattr() ran out of space */
  811. if (status == nfserr_resource && count == 0)
  812. status = nfserr_not_same;
  813. if (status)
  814. goto out_kfree;
  815. /* skip bitmap */
  816. p = buf + 1 + ntohl(buf[0]);
  817. status = nfserr_not_same;
  818. if (ntohl(*p++) != verify->ve_attrlen)
  819. goto out_kfree;
  820. if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen))
  821. status = nfserr_same;
  822. out_kfree:
  823. kfree(buf);
  824. return status;
  825. }
  826. static __be32
  827. nfsd4_nverify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  828. struct nfsd4_verify *verify)
  829. {
  830. __be32 status;
  831. status = _nfsd4_verify(rqstp, cstate, verify);
  832. return status == nfserr_not_same ? nfs_ok : status;
  833. }
  834. static __be32
  835. nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  836. struct nfsd4_verify *verify)
  837. {
  838. __be32 status;
  839. status = _nfsd4_verify(rqstp, cstate, verify);
  840. return status == nfserr_same ? nfs_ok : status;
  841. }
  842. /*
  843. * NULL call.
  844. */
  845. static __be32
  846. nfsd4_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
  847. {
  848. return nfs_ok;
  849. }
  850. static inline void nfsd4_increment_op_stats(u32 opnum)
  851. {
  852. if (opnum >= FIRST_NFS4_OP && opnum <= LAST_NFS4_OP)
  853. nfsdstats.nfs4_opcount[opnum]++;
  854. }
  855. typedef __be32(*nfsd4op_func)(struct svc_rqst *, struct nfsd4_compound_state *,
  856. void *);
  857. enum nfsd4_op_flags {
  858. ALLOWED_WITHOUT_FH = 1 << 0, /* No current filehandle required */
  859. ALLOWED_ON_ABSENT_FS = 1 << 1, /* ops processed on absent fs */
  860. ALLOWED_AS_FIRST_OP = 1 << 2, /* ops reqired first in compound */
  861. /* For rfc 5661 section 2.6.3.1.1: */
  862. OP_HANDLES_WRONGSEC = 1 << 3,
  863. OP_IS_PUTFH_LIKE = 1 << 4,
  864. };
  865. struct nfsd4_operation {
  866. nfsd4op_func op_func;
  867. u32 op_flags;
  868. char *op_name;
  869. /*
  870. * We use the DRC for compounds containing non-idempotent
  871. * operations, *except* those that are 4.1-specific (since
  872. * sessions provide their own EOS), and except for stateful
  873. * operations other than setclientid and setclientid_confirm
  874. * (since sequence numbers provide EOS for open, lock, etc in
  875. * the v4.0 case).
  876. */
  877. bool op_cacheresult;
  878. };
  879. static struct nfsd4_operation nfsd4_ops[];
  880. static const char *nfsd4_op_name(unsigned opnum);
  881. /*
  882. * Enforce NFSv4.1 COMPOUND ordering rules:
  883. *
  884. * Also note, enforced elsewhere:
  885. * - SEQUENCE other than as first op results in
  886. * NFS4ERR_SEQUENCE_POS. (Enforced in nfsd4_sequence().)
  887. * - BIND_CONN_TO_SESSION must be the only op in its compound.
  888. * (Enforced in nfsd4_bind_conn_to_session().)
  889. * - DESTROY_SESSION must be the final operation in a compound, if
  890. * sessionid's in SEQUENCE and DESTROY_SESSION are the same.
  891. * (Enforced in nfsd4_destroy_session().)
  892. */
  893. static __be32 nfs41_check_op_ordering(struct nfsd4_compoundargs *args)
  894. {
  895. struct nfsd4_op *op = &args->ops[0];
  896. /* These ordering requirements don't apply to NFSv4.0: */
  897. if (args->minorversion == 0)
  898. return nfs_ok;
  899. /* This is weird, but OK, not our problem: */
  900. if (args->opcnt == 0)
  901. return nfs_ok;
  902. if (op->status == nfserr_op_illegal)
  903. return nfs_ok;
  904. if (!(nfsd4_ops[op->opnum].op_flags & ALLOWED_AS_FIRST_OP))
  905. return nfserr_op_not_in_session;
  906. if (op->opnum == OP_SEQUENCE)
  907. return nfs_ok;
  908. if (args->opcnt != 1)
  909. return nfserr_not_only_op;
  910. return nfs_ok;
  911. }
  912. static inline struct nfsd4_operation *OPDESC(struct nfsd4_op *op)
  913. {
  914. return &nfsd4_ops[op->opnum];
  915. }
  916. bool nfsd4_cache_this_op(struct nfsd4_op *op)
  917. {
  918. return OPDESC(op)->op_cacheresult;
  919. }
  920. static bool need_wrongsec_check(struct svc_rqst *rqstp)
  921. {
  922. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  923. struct nfsd4_compoundargs *argp = rqstp->rq_argp;
  924. struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
  925. struct nfsd4_op *next = &argp->ops[resp->opcnt];
  926. struct nfsd4_operation *thisd;
  927. struct nfsd4_operation *nextd;
  928. thisd = OPDESC(this);
  929. /*
  930. * Most ops check wronsec on our own; only the putfh-like ops
  931. * have special rules.
  932. */
  933. if (!(thisd->op_flags & OP_IS_PUTFH_LIKE))
  934. return false;
  935. /*
  936. * rfc 5661 2.6.3.1.1.6: don't bother erroring out a
  937. * put-filehandle operation if we're not going to use the
  938. * result:
  939. */
  940. if (argp->opcnt == resp->opcnt)
  941. return false;
  942. nextd = OPDESC(next);
  943. /*
  944. * Rest of 2.6.3.1.1: certain operations will return WRONGSEC
  945. * errors themselves as necessary; others should check for them
  946. * now:
  947. */
  948. return !(nextd->op_flags & OP_HANDLES_WRONGSEC);
  949. }
  950. /*
  951. * COMPOUND call.
  952. */
  953. static __be32
  954. nfsd4_proc_compound(struct svc_rqst *rqstp,
  955. struct nfsd4_compoundargs *args,
  956. struct nfsd4_compoundres *resp)
  957. {
  958. struct nfsd4_op *op;
  959. struct nfsd4_operation *opdesc;
  960. struct nfsd4_compound_state *cstate = &resp->cstate;
  961. int slack_bytes;
  962. __be32 status;
  963. resp->xbuf = &rqstp->rq_res;
  964. resp->p = rqstp->rq_res.head[0].iov_base +
  965. rqstp->rq_res.head[0].iov_len;
  966. resp->tagp = resp->p;
  967. /* reserve space for: taglen, tag, and opcnt */
  968. resp->p += 2 + XDR_QUADLEN(args->taglen);
  969. resp->end = rqstp->rq_res.head[0].iov_base + PAGE_SIZE;
  970. resp->taglen = args->taglen;
  971. resp->tag = args->tag;
  972. resp->opcnt = 0;
  973. resp->rqstp = rqstp;
  974. resp->cstate.minorversion = args->minorversion;
  975. resp->cstate.replay_owner = NULL;
  976. resp->cstate.session = NULL;
  977. fh_init(&resp->cstate.current_fh, NFS4_FHSIZE);
  978. fh_init(&resp->cstate.save_fh, NFS4_FHSIZE);
  979. /*
  980. * Don't use the deferral mechanism for NFSv4; compounds make it
  981. * too hard to avoid non-idempotency problems.
  982. */
  983. rqstp->rq_usedeferral = 0;
  984. /*
  985. * According to RFC3010, this takes precedence over all other errors.
  986. */
  987. status = nfserr_minor_vers_mismatch;
  988. if (args->minorversion > nfsd_supported_minorversion)
  989. goto out;
  990. status = nfs41_check_op_ordering(args);
  991. if (status) {
  992. op = &args->ops[0];
  993. op->status = status;
  994. goto encode_op;
  995. }
  996. while (!status && resp->opcnt < args->opcnt) {
  997. op = &args->ops[resp->opcnt++];
  998. dprintk("nfsv4 compound op #%d/%d: %d (%s)\n",
  999. resp->opcnt, args->opcnt, op->opnum,
  1000. nfsd4_op_name(op->opnum));
  1001. /*
  1002. * The XDR decode routines may have pre-set op->status;
  1003. * for example, if there is a miscellaneous XDR error
  1004. * it will be set to nfserr_bad_xdr.
  1005. */
  1006. if (op->status)
  1007. goto encode_op;
  1008. /* We must be able to encode a successful response to
  1009. * this operation, with enough room left over to encode a
  1010. * failed response to the next operation. If we don't
  1011. * have enough room, fail with ERR_RESOURCE.
  1012. */
  1013. slack_bytes = (char *)resp->end - (char *)resp->p;
  1014. if (slack_bytes < COMPOUND_SLACK_SPACE
  1015. + COMPOUND_ERR_SLACK_SPACE) {
  1016. BUG_ON(slack_bytes < COMPOUND_ERR_SLACK_SPACE);
  1017. op->status = nfserr_resource;
  1018. goto encode_op;
  1019. }
  1020. opdesc = OPDESC(op);
  1021. if (!cstate->current_fh.fh_dentry) {
  1022. if (!(opdesc->op_flags & ALLOWED_WITHOUT_FH)) {
  1023. op->status = nfserr_nofilehandle;
  1024. goto encode_op;
  1025. }
  1026. } else if (cstate->current_fh.fh_export->ex_fslocs.migrated &&
  1027. !(opdesc->op_flags & ALLOWED_ON_ABSENT_FS)) {
  1028. op->status = nfserr_moved;
  1029. goto encode_op;
  1030. }
  1031. if (opdesc->op_func)
  1032. op->status = opdesc->op_func(rqstp, cstate, &op->u);
  1033. else
  1034. BUG_ON(op->status == nfs_ok);
  1035. if (!op->status && need_wrongsec_check(rqstp))
  1036. op->status = check_nfsd_access(cstate->current_fh.fh_export, rqstp);
  1037. encode_op:
  1038. /* Only from SEQUENCE */
  1039. if (resp->cstate.status == nfserr_replay_cache) {
  1040. dprintk("%s NFS4.1 replay from cache\n", __func__);
  1041. status = op->status;
  1042. goto out;
  1043. }
  1044. if (op->status == nfserr_replay_me) {
  1045. op->replay = &cstate->replay_owner->so_replay;
  1046. nfsd4_encode_replay(resp, op);
  1047. status = op->status = op->replay->rp_status;
  1048. } else {
  1049. nfsd4_encode_operation(resp, op);
  1050. status = op->status;
  1051. }
  1052. dprintk("nfsv4 compound op %p opcnt %d #%d: %d: status %d\n",
  1053. args->ops, args->opcnt, resp->opcnt, op->opnum,
  1054. be32_to_cpu(status));
  1055. if (cstate->replay_owner) {
  1056. nfs4_put_stateowner(cstate->replay_owner);
  1057. cstate->replay_owner = NULL;
  1058. }
  1059. /* XXX Ugh, we need to get rid of this kind of special case: */
  1060. if (op->opnum == OP_READ && op->u.read.rd_filp)
  1061. fput(op->u.read.rd_filp);
  1062. nfsd4_increment_op_stats(op->opnum);
  1063. }
  1064. resp->cstate.status = status;
  1065. fh_put(&resp->cstate.current_fh);
  1066. fh_put(&resp->cstate.save_fh);
  1067. BUG_ON(resp->cstate.replay_owner);
  1068. out:
  1069. /* Reset deferral mechanism for RPC deferrals */
  1070. rqstp->rq_usedeferral = 1;
  1071. dprintk("nfsv4 compound returned %d\n", ntohl(status));
  1072. return status;
  1073. }
  1074. static struct nfsd4_operation nfsd4_ops[] = {
  1075. [OP_ACCESS] = {
  1076. .op_func = (nfsd4op_func)nfsd4_access,
  1077. .op_name = "OP_ACCESS",
  1078. },
  1079. [OP_CLOSE] = {
  1080. .op_func = (nfsd4op_func)nfsd4_close,
  1081. .op_name = "OP_CLOSE",
  1082. },
  1083. [OP_COMMIT] = {
  1084. .op_func = (nfsd4op_func)nfsd4_commit,
  1085. .op_name = "OP_COMMIT",
  1086. },
  1087. [OP_CREATE] = {
  1088. .op_func = (nfsd4op_func)nfsd4_create,
  1089. .op_name = "OP_CREATE",
  1090. .op_cacheresult = true,
  1091. },
  1092. [OP_DELEGRETURN] = {
  1093. .op_func = (nfsd4op_func)nfsd4_delegreturn,
  1094. .op_name = "OP_DELEGRETURN",
  1095. },
  1096. [OP_GETATTR] = {
  1097. .op_func = (nfsd4op_func)nfsd4_getattr,
  1098. .op_flags = ALLOWED_ON_ABSENT_FS,
  1099. .op_name = "OP_GETATTR",
  1100. },
  1101. [OP_GETFH] = {
  1102. .op_func = (nfsd4op_func)nfsd4_getfh,
  1103. .op_name = "OP_GETFH",
  1104. },
  1105. [OP_LINK] = {
  1106. .op_func = (nfsd4op_func)nfsd4_link,
  1107. .op_name = "OP_LINK",
  1108. .op_cacheresult = true,
  1109. },
  1110. [OP_LOCK] = {
  1111. .op_func = (nfsd4op_func)nfsd4_lock,
  1112. .op_name = "OP_LOCK",
  1113. },
  1114. [OP_LOCKT] = {
  1115. .op_func = (nfsd4op_func)nfsd4_lockt,
  1116. .op_name = "OP_LOCKT",
  1117. },
  1118. [OP_LOCKU] = {
  1119. .op_func = (nfsd4op_func)nfsd4_locku,
  1120. .op_name = "OP_LOCKU",
  1121. },
  1122. [OP_LOOKUP] = {
  1123. .op_func = (nfsd4op_func)nfsd4_lookup,
  1124. .op_flags = OP_HANDLES_WRONGSEC,
  1125. .op_name = "OP_LOOKUP",
  1126. },
  1127. [OP_LOOKUPP] = {
  1128. .op_func = (nfsd4op_func)nfsd4_lookupp,
  1129. .op_flags = OP_HANDLES_WRONGSEC,
  1130. .op_name = "OP_LOOKUPP",
  1131. },
  1132. [OP_NVERIFY] = {
  1133. .op_func = (nfsd4op_func)nfsd4_nverify,
  1134. .op_name = "OP_NVERIFY",
  1135. },
  1136. [OP_OPEN] = {
  1137. .op_func = (nfsd4op_func)nfsd4_open,
  1138. .op_flags = OP_HANDLES_WRONGSEC,
  1139. .op_name = "OP_OPEN",
  1140. },
  1141. [OP_OPEN_CONFIRM] = {
  1142. .op_func = (nfsd4op_func)nfsd4_open_confirm,
  1143. .op_name = "OP_OPEN_CONFIRM",
  1144. },
  1145. [OP_OPEN_DOWNGRADE] = {
  1146. .op_func = (nfsd4op_func)nfsd4_open_downgrade,
  1147. .op_name = "OP_OPEN_DOWNGRADE",
  1148. },
  1149. [OP_PUTFH] = {
  1150. .op_func = (nfsd4op_func)nfsd4_putfh,
  1151. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1152. | OP_IS_PUTFH_LIKE,
  1153. .op_name = "OP_PUTFH",
  1154. },
  1155. [OP_PUTPUBFH] = {
  1156. .op_func = (nfsd4op_func)nfsd4_putrootfh,
  1157. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1158. | OP_IS_PUTFH_LIKE,
  1159. .op_name = "OP_PUTPUBFH",
  1160. },
  1161. [OP_PUTROOTFH] = {
  1162. .op_func = (nfsd4op_func)nfsd4_putrootfh,
  1163. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1164. | OP_IS_PUTFH_LIKE,
  1165. .op_name = "OP_PUTROOTFH",
  1166. },
  1167. [OP_READ] = {
  1168. .op_func = (nfsd4op_func)nfsd4_read,
  1169. .op_name = "OP_READ",
  1170. },
  1171. [OP_READDIR] = {
  1172. .op_func = (nfsd4op_func)nfsd4_readdir,
  1173. .op_name = "OP_READDIR",
  1174. },
  1175. [OP_READLINK] = {
  1176. .op_func = (nfsd4op_func)nfsd4_readlink,
  1177. .op_name = "OP_READLINK",
  1178. },
  1179. [OP_REMOVE] = {
  1180. .op_func = (nfsd4op_func)nfsd4_remove,
  1181. .op_name = "OP_REMOVE",
  1182. .op_cacheresult = true,
  1183. },
  1184. [OP_RENAME] = {
  1185. .op_name = "OP_RENAME",
  1186. .op_func = (nfsd4op_func)nfsd4_rename,
  1187. .op_cacheresult = true,
  1188. },
  1189. [OP_RENEW] = {
  1190. .op_func = (nfsd4op_func)nfsd4_renew,
  1191. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
  1192. .op_name = "OP_RENEW",
  1193. },
  1194. [OP_RESTOREFH] = {
  1195. .op_func = (nfsd4op_func)nfsd4_restorefh,
  1196. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1197. | OP_IS_PUTFH_LIKE,
  1198. .op_name = "OP_RESTOREFH",
  1199. },
  1200. [OP_SAVEFH] = {
  1201. .op_func = (nfsd4op_func)nfsd4_savefh,
  1202. .op_flags = OP_HANDLES_WRONGSEC,
  1203. .op_name = "OP_SAVEFH",
  1204. },
  1205. [OP_SECINFO] = {
  1206. .op_func = (nfsd4op_func)nfsd4_secinfo,
  1207. .op_flags = OP_HANDLES_WRONGSEC,
  1208. .op_name = "OP_SECINFO",
  1209. },
  1210. [OP_SETATTR] = {
  1211. .op_func = (nfsd4op_func)nfsd4_setattr,
  1212. .op_name = "OP_SETATTR",
  1213. .op_cacheresult = true,
  1214. },
  1215. [OP_SETCLIENTID] = {
  1216. .op_func = (nfsd4op_func)nfsd4_setclientid,
  1217. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
  1218. .op_name = "OP_SETCLIENTID",
  1219. .op_cacheresult = true,
  1220. },
  1221. [OP_SETCLIENTID_CONFIRM] = {
  1222. .op_func = (nfsd4op_func)nfsd4_setclientid_confirm,
  1223. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
  1224. .op_name = "OP_SETCLIENTID_CONFIRM",
  1225. .op_cacheresult = true,
  1226. },
  1227. [OP_VERIFY] = {
  1228. .op_func = (nfsd4op_func)nfsd4_verify,
  1229. .op_name = "OP_VERIFY",
  1230. },
  1231. [OP_WRITE] = {
  1232. .op_func = (nfsd4op_func)nfsd4_write,
  1233. .op_name = "OP_WRITE",
  1234. .op_cacheresult = true,
  1235. },
  1236. [OP_RELEASE_LOCKOWNER] = {
  1237. .op_func = (nfsd4op_func)nfsd4_release_lockowner,
  1238. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
  1239. .op_name = "OP_RELEASE_LOCKOWNER",
  1240. },
  1241. /* NFSv4.1 operations */
  1242. [OP_EXCHANGE_ID] = {
  1243. .op_func = (nfsd4op_func)nfsd4_exchange_id,
  1244. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
  1245. .op_name = "OP_EXCHANGE_ID",
  1246. },
  1247. [OP_BIND_CONN_TO_SESSION] = {
  1248. .op_func = (nfsd4op_func)nfsd4_bind_conn_to_session,
  1249. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
  1250. .op_name = "OP_BIND_CONN_TO_SESSION",
  1251. },
  1252. [OP_CREATE_SESSION] = {
  1253. .op_func = (nfsd4op_func)nfsd4_create_session,
  1254. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
  1255. .op_name = "OP_CREATE_SESSION",
  1256. },
  1257. [OP_DESTROY_SESSION] = {
  1258. .op_func = (nfsd4op_func)nfsd4_destroy_session,
  1259. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
  1260. .op_name = "OP_DESTROY_SESSION",
  1261. },
  1262. [OP_SEQUENCE] = {
  1263. .op_func = (nfsd4op_func)nfsd4_sequence,
  1264. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
  1265. .op_name = "OP_SEQUENCE",
  1266. },
  1267. [OP_DESTROY_CLIENTID] = {
  1268. .op_func = NULL,
  1269. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
  1270. .op_name = "OP_DESTROY_CLIENTID",
  1271. },
  1272. [OP_RECLAIM_COMPLETE] = {
  1273. .op_func = (nfsd4op_func)nfsd4_reclaim_complete,
  1274. .op_flags = ALLOWED_WITHOUT_FH,
  1275. .op_name = "OP_RECLAIM_COMPLETE",
  1276. },
  1277. [OP_SECINFO_NO_NAME] = {
  1278. .op_func = (nfsd4op_func)nfsd4_secinfo_no_name,
  1279. .op_flags = OP_HANDLES_WRONGSEC,
  1280. .op_name = "OP_SECINFO_NO_NAME",
  1281. },
  1282. [OP_TEST_STATEID] = {
  1283. .op_func = (nfsd4op_func)nfsd4_test_stateid,
  1284. .op_flags = ALLOWED_WITHOUT_FH,
  1285. .op_name = "OP_TEST_STATEID",
  1286. },
  1287. [OP_FREE_STATEID] = {
  1288. .op_func = (nfsd4op_func)nfsd4_free_stateid,
  1289. .op_flags = ALLOWED_WITHOUT_FH,
  1290. .op_name = "OP_FREE_STATEID",
  1291. },
  1292. };
  1293. static const char *nfsd4_op_name(unsigned opnum)
  1294. {
  1295. if (opnum < ARRAY_SIZE(nfsd4_ops))
  1296. return nfsd4_ops[opnum].op_name;
  1297. return "unknown_operation";
  1298. }
  1299. #define nfsd4_voidres nfsd4_voidargs
  1300. struct nfsd4_voidargs { int dummy; };
  1301. static struct svc_procedure nfsd_procedures4[2] = {
  1302. [NFSPROC4_NULL] = {
  1303. .pc_func = (svc_procfunc) nfsd4_proc_null,
  1304. .pc_encode = (kxdrproc_t) nfs4svc_encode_voidres,
  1305. .pc_argsize = sizeof(struct nfsd4_voidargs),
  1306. .pc_ressize = sizeof(struct nfsd4_voidres),
  1307. .pc_cachetype = RC_NOCACHE,
  1308. .pc_xdrressize = 1,
  1309. },
  1310. [NFSPROC4_COMPOUND] = {
  1311. .pc_func = (svc_procfunc) nfsd4_proc_compound,
  1312. .pc_decode = (kxdrproc_t) nfs4svc_decode_compoundargs,
  1313. .pc_encode = (kxdrproc_t) nfs4svc_encode_compoundres,
  1314. .pc_argsize = sizeof(struct nfsd4_compoundargs),
  1315. .pc_ressize = sizeof(struct nfsd4_compoundres),
  1316. .pc_release = nfsd4_release_compoundargs,
  1317. .pc_cachetype = RC_NOCACHE,
  1318. .pc_xdrressize = NFSD_BUFSIZE/4,
  1319. },
  1320. };
  1321. struct svc_version nfsd_version4 = {
  1322. .vs_vers = 4,
  1323. .vs_nproc = 2,
  1324. .vs_proc = nfsd_procedures4,
  1325. .vs_dispatch = nfsd_dispatch,
  1326. .vs_xdrsize = NFS4_SVC_XDRSIZE,
  1327. };
  1328. /*
  1329. * Local variables:
  1330. * c-basic-offset: 8
  1331. * End:
  1332. */