nfs4proc.c 41 KB

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