nfs4proc.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. /*
  2. * fs/nfsd/nfs4proc.c
  3. *
  4. * Server-side procedures for NFSv4.
  5. *
  6. * Copyright (c) 2002 The Regents of the University of Michigan.
  7. * All rights reserved.
  8. *
  9. * Kendrick Smith <kmsmith@umich.edu>
  10. * Andy Adamson <andros@umich.edu>
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * 3. Neither the name of the University nor the names of its
  22. * contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  26. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  27. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  32. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include <linux/param.h>
  38. #include <linux/major.h>
  39. #include <linux/slab.h>
  40. #include <linux/file.h>
  41. #include <linux/sunrpc/svc.h>
  42. #include <linux/nfsd/nfsd.h>
  43. #include <linux/nfsd/cache.h>
  44. #include <linux/nfs4.h>
  45. #include <linux/nfsd/state.h>
  46. #include <linux/nfsd/xdr4.h>
  47. #include <linux/nfs4_acl.h>
  48. #include <linux/sunrpc/gss_api.h>
  49. #define NFSDDBG_FACILITY NFSDDBG_PROC
  50. static inline void
  51. fh_dup2(struct svc_fh *dst, struct svc_fh *src)
  52. {
  53. fh_put(dst);
  54. dget(src->fh_dentry);
  55. if (src->fh_export)
  56. cache_get(&src->fh_export->h);
  57. *dst = *src;
  58. }
  59. static __be32
  60. do_open_permission(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open, int accmode)
  61. {
  62. __be32 status;
  63. if (open->op_truncate &&
  64. !(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
  65. return nfserr_inval;
  66. if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
  67. accmode |= MAY_READ;
  68. if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
  69. accmode |= (MAY_WRITE | MAY_TRUNC);
  70. if (open->op_share_deny & NFS4_SHARE_DENY_WRITE)
  71. accmode |= MAY_WRITE;
  72. status = fh_verify(rqstp, current_fh, S_IFREG, accmode);
  73. return status;
  74. }
  75. static __be32
  76. do_open_lookup(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
  77. {
  78. struct svc_fh resfh;
  79. __be32 status;
  80. int created = 0;
  81. fh_init(&resfh, NFS4_FHSIZE);
  82. open->op_truncate = 0;
  83. if (open->op_create) {
  84. /*
  85. * Note: create modes (UNCHECKED,GUARDED...) are the same
  86. * in NFSv4 as in v3.
  87. */
  88. status = nfsd_create_v3(rqstp, current_fh, open->op_fname.data,
  89. open->op_fname.len, &open->op_iattr,
  90. &resfh, open->op_createmode,
  91. (u32 *)open->op_verf.data,
  92. &open->op_truncate, &created);
  93. /* If we ever decide to use different attrs to store the
  94. * verifier in nfsd_create_v3, then we'll need to change this
  95. */
  96. if (open->op_createmode == NFS4_CREATE_EXCLUSIVE && status == 0)
  97. open->op_bmval[1] |= (FATTR4_WORD1_TIME_ACCESS |
  98. FATTR4_WORD1_TIME_MODIFY);
  99. } else {
  100. status = nfsd_lookup(rqstp, current_fh,
  101. open->op_fname.data, open->op_fname.len, &resfh);
  102. fh_unlock(current_fh);
  103. }
  104. if (status)
  105. goto out;
  106. set_change_info(&open->op_cinfo, current_fh);
  107. /* set reply cache */
  108. fh_dup2(current_fh, &resfh);
  109. open->op_stateowner->so_replay.rp_openfh_len = resfh.fh_handle.fh_size;
  110. memcpy(open->op_stateowner->so_replay.rp_openfh,
  111. &resfh.fh_handle.fh_base, resfh.fh_handle.fh_size);
  112. if (!created)
  113. status = do_open_permission(rqstp, current_fh, open, MAY_NOP);
  114. out:
  115. fh_put(&resfh);
  116. return status;
  117. }
  118. static __be32
  119. do_open_fhandle(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
  120. {
  121. __be32 status;
  122. /* Only reclaims from previously confirmed clients are valid */
  123. if ((status = nfs4_check_open_reclaim(&open->op_clientid)))
  124. return status;
  125. /* We don't know the target directory, and therefore can not
  126. * set the change info
  127. */
  128. memset(&open->op_cinfo, 0, sizeof(struct nfsd4_change_info));
  129. /* set replay cache */
  130. open->op_stateowner->so_replay.rp_openfh_len = current_fh->fh_handle.fh_size;
  131. memcpy(open->op_stateowner->so_replay.rp_openfh,
  132. &current_fh->fh_handle.fh_base,
  133. current_fh->fh_handle.fh_size);
  134. open->op_truncate = (open->op_iattr.ia_valid & ATTR_SIZE) &&
  135. (open->op_iattr.ia_size == 0);
  136. status = do_open_permission(rqstp, current_fh, open, MAY_OWNER_OVERRIDE);
  137. return status;
  138. }
  139. static __be32
  140. nfsd4_open(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  141. struct nfsd4_open *open)
  142. {
  143. __be32 status;
  144. dprintk("NFSD: nfsd4_open filename %.*s op_stateowner %p\n",
  145. (int)open->op_fname.len, open->op_fname.data,
  146. open->op_stateowner);
  147. /* This check required by spec. */
  148. if (open->op_create && open->op_claim_type != NFS4_OPEN_CLAIM_NULL)
  149. return nfserr_inval;
  150. nfs4_lock_state();
  151. /* check seqid for replay. set nfs4_owner */
  152. status = nfsd4_process_open1(open);
  153. if (status == nfserr_replay_me) {
  154. struct nfs4_replay *rp = &open->op_stateowner->so_replay;
  155. fh_put(&cstate->current_fh);
  156. cstate->current_fh.fh_handle.fh_size = rp->rp_openfh_len;
  157. memcpy(&cstate->current_fh.fh_handle.fh_base, rp->rp_openfh,
  158. rp->rp_openfh_len);
  159. status = fh_verify(rqstp, &cstate->current_fh, 0, MAY_NOP);
  160. if (status)
  161. dprintk("nfsd4_open: replay failed"
  162. " restoring previous filehandle\n");
  163. else
  164. status = nfserr_replay_me;
  165. }
  166. if (status)
  167. goto out;
  168. /* Openowner is now set, so sequence id will get bumped. Now we need
  169. * these checks before we do any creates: */
  170. status = nfserr_grace;
  171. if (nfs4_in_grace() && open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
  172. goto out;
  173. status = nfserr_no_grace;
  174. if (!nfs4_in_grace() && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
  175. goto out;
  176. switch (open->op_claim_type) {
  177. case NFS4_OPEN_CLAIM_DELEGATE_CUR:
  178. status = nfserr_inval;
  179. if (open->op_create)
  180. goto out;
  181. /* fall through */
  182. case NFS4_OPEN_CLAIM_NULL:
  183. /*
  184. * (1) set CURRENT_FH to the file being opened,
  185. * creating it if necessary, (2) set open->op_cinfo,
  186. * (3) set open->op_truncate if the file is to be
  187. * truncated after opening, (4) do permission checking.
  188. */
  189. status = do_open_lookup(rqstp, &cstate->current_fh,
  190. open);
  191. if (status)
  192. goto out;
  193. break;
  194. case NFS4_OPEN_CLAIM_PREVIOUS:
  195. open->op_stateowner->so_confirmed = 1;
  196. /*
  197. * The CURRENT_FH is already set to the file being
  198. * opened. (1) set open->op_cinfo, (2) set
  199. * open->op_truncate if the file is to be truncated
  200. * after opening, (3) do permission checking.
  201. */
  202. status = do_open_fhandle(rqstp, &cstate->current_fh,
  203. open);
  204. if (status)
  205. goto out;
  206. break;
  207. case NFS4_OPEN_CLAIM_DELEGATE_PREV:
  208. open->op_stateowner->so_confirmed = 1;
  209. dprintk("NFSD: unsupported OPEN claim type %d\n",
  210. open->op_claim_type);
  211. status = nfserr_notsupp;
  212. goto out;
  213. default:
  214. dprintk("NFSD: Invalid OPEN claim type %d\n",
  215. open->op_claim_type);
  216. status = nfserr_inval;
  217. goto out;
  218. }
  219. /*
  220. * nfsd4_process_open2() does the actual opening of the file. If
  221. * successful, it (1) truncates the file if open->op_truncate was
  222. * set, (2) sets open->op_stateid, (3) sets open->op_delegation.
  223. */
  224. status = nfsd4_process_open2(rqstp, &cstate->current_fh, open);
  225. out:
  226. if (open->op_stateowner) {
  227. nfs4_get_stateowner(open->op_stateowner);
  228. cstate->replay_owner = open->op_stateowner;
  229. }
  230. nfs4_unlock_state();
  231. return status;
  232. }
  233. /*
  234. * filehandle-manipulating ops.
  235. */
  236. static __be32
  237. nfsd4_getfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  238. struct svc_fh **getfh)
  239. {
  240. if (!cstate->current_fh.fh_dentry)
  241. return nfserr_nofilehandle;
  242. *getfh = &cstate->current_fh;
  243. return nfs_ok;
  244. }
  245. static __be32
  246. nfsd4_putfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  247. struct nfsd4_putfh *putfh)
  248. {
  249. fh_put(&cstate->current_fh);
  250. cstate->current_fh.fh_handle.fh_size = putfh->pf_fhlen;
  251. memcpy(&cstate->current_fh.fh_handle.fh_base, putfh->pf_fhval,
  252. putfh->pf_fhlen);
  253. return fh_verify(rqstp, &cstate->current_fh, 0, MAY_NOP);
  254. }
  255. static __be32
  256. nfsd4_putrootfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  257. void *arg)
  258. {
  259. __be32 status;
  260. fh_put(&cstate->current_fh);
  261. status = exp_pseudoroot(rqstp, &cstate->current_fh);
  262. return status;
  263. }
  264. static __be32
  265. nfsd4_restorefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  266. void *arg)
  267. {
  268. if (!cstate->save_fh.fh_dentry)
  269. return nfserr_restorefh;
  270. fh_dup2(&cstate->current_fh, &cstate->save_fh);
  271. return nfs_ok;
  272. }
  273. static __be32
  274. nfsd4_savefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  275. void *arg)
  276. {
  277. if (!cstate->current_fh.fh_dentry)
  278. return nfserr_nofilehandle;
  279. fh_dup2(&cstate->save_fh, &cstate->current_fh);
  280. return nfs_ok;
  281. }
  282. /*
  283. * misc nfsv4 ops
  284. */
  285. static __be32
  286. nfsd4_access(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  287. struct nfsd4_access *access)
  288. {
  289. if (access->ac_req_access & ~NFS3_ACCESS_FULL)
  290. return nfserr_inval;
  291. access->ac_resp_access = access->ac_req_access;
  292. return nfsd_access(rqstp, &cstate->current_fh, &access->ac_resp_access,
  293. &access->ac_supported);
  294. }
  295. static __be32
  296. nfsd4_commit(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  297. struct nfsd4_commit *commit)
  298. {
  299. __be32 status;
  300. u32 *p = (u32 *)commit->co_verf.data;
  301. *p++ = nfssvc_boot.tv_sec;
  302. *p++ = nfssvc_boot.tv_usec;
  303. status = nfsd_commit(rqstp, &cstate->current_fh, commit->co_offset,
  304. commit->co_count);
  305. if (status == nfserr_symlink)
  306. status = nfserr_inval;
  307. return status;
  308. }
  309. static __be32
  310. nfsd4_create(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  311. struct nfsd4_create *create)
  312. {
  313. struct svc_fh resfh;
  314. __be32 status;
  315. dev_t rdev;
  316. fh_init(&resfh, NFS4_FHSIZE);
  317. status = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, MAY_CREATE);
  318. if (status == nfserr_symlink)
  319. status = nfserr_notdir;
  320. if (status)
  321. return status;
  322. switch (create->cr_type) {
  323. case NF4LNK:
  324. /* ugh! we have to null-terminate the linktext, or
  325. * vfs_symlink() will choke. it is always safe to
  326. * null-terminate by brute force, since at worst we
  327. * will overwrite the first byte of the create namelen
  328. * in the XDR buffer, which has already been extracted
  329. * during XDR decode.
  330. */
  331. create->cr_linkname[create->cr_linklen] = 0;
  332. status = nfsd_symlink(rqstp, &cstate->current_fh,
  333. create->cr_name, create->cr_namelen,
  334. create->cr_linkname, create->cr_linklen,
  335. &resfh, &create->cr_iattr);
  336. break;
  337. case NF4BLK:
  338. rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
  339. if (MAJOR(rdev) != create->cr_specdata1 ||
  340. MINOR(rdev) != create->cr_specdata2)
  341. return nfserr_inval;
  342. status = nfsd_create(rqstp, &cstate->current_fh,
  343. create->cr_name, create->cr_namelen,
  344. &create->cr_iattr, S_IFBLK, rdev, &resfh);
  345. break;
  346. case NF4CHR:
  347. rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
  348. if (MAJOR(rdev) != create->cr_specdata1 ||
  349. MINOR(rdev) != create->cr_specdata2)
  350. return nfserr_inval;
  351. status = nfsd_create(rqstp, &cstate->current_fh,
  352. create->cr_name, create->cr_namelen,
  353. &create->cr_iattr,S_IFCHR, rdev, &resfh);
  354. break;
  355. case NF4SOCK:
  356. status = nfsd_create(rqstp, &cstate->current_fh,
  357. create->cr_name, create->cr_namelen,
  358. &create->cr_iattr, S_IFSOCK, 0, &resfh);
  359. break;
  360. case NF4FIFO:
  361. status = nfsd_create(rqstp, &cstate->current_fh,
  362. create->cr_name, create->cr_namelen,
  363. &create->cr_iattr, S_IFIFO, 0, &resfh);
  364. break;
  365. case NF4DIR:
  366. create->cr_iattr.ia_valid &= ~ATTR_SIZE;
  367. status = nfsd_create(rqstp, &cstate->current_fh,
  368. create->cr_name, create->cr_namelen,
  369. &create->cr_iattr, S_IFDIR, 0, &resfh);
  370. break;
  371. default:
  372. status = nfserr_badtype;
  373. }
  374. if (!status) {
  375. fh_unlock(&cstate->current_fh);
  376. set_change_info(&create->cr_cinfo, &cstate->current_fh);
  377. fh_dup2(&cstate->current_fh, &resfh);
  378. }
  379. fh_put(&resfh);
  380. return status;
  381. }
  382. static __be32
  383. nfsd4_getattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  384. struct nfsd4_getattr *getattr)
  385. {
  386. __be32 status;
  387. status = fh_verify(rqstp, &cstate->current_fh, 0, MAY_NOP);
  388. if (status)
  389. return status;
  390. if (getattr->ga_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
  391. return nfserr_inval;
  392. getattr->ga_bmval[0] &= NFSD_SUPPORTED_ATTRS_WORD0;
  393. getattr->ga_bmval[1] &= NFSD_SUPPORTED_ATTRS_WORD1;
  394. getattr->ga_fhp = &cstate->current_fh;
  395. return nfs_ok;
  396. }
  397. static __be32
  398. nfsd4_link(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  399. struct nfsd4_link *link)
  400. {
  401. __be32 status = nfserr_nofilehandle;
  402. if (!cstate->save_fh.fh_dentry)
  403. return status;
  404. status = nfsd_link(rqstp, &cstate->current_fh,
  405. link->li_name, link->li_namelen, &cstate->save_fh);
  406. if (!status)
  407. set_change_info(&link->li_cinfo, &cstate->current_fh);
  408. return status;
  409. }
  410. static __be32
  411. nfsd4_lookupp(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  412. void *arg)
  413. {
  414. struct svc_fh tmp_fh;
  415. __be32 ret;
  416. fh_init(&tmp_fh, NFS4_FHSIZE);
  417. ret = exp_pseudoroot(rqstp, &tmp_fh);
  418. if (ret)
  419. return ret;
  420. if (tmp_fh.fh_dentry == cstate->current_fh.fh_dentry) {
  421. fh_put(&tmp_fh);
  422. return nfserr_noent;
  423. }
  424. fh_put(&tmp_fh);
  425. return nfsd_lookup(rqstp, &cstate->current_fh,
  426. "..", 2, &cstate->current_fh);
  427. }
  428. static __be32
  429. nfsd4_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  430. struct nfsd4_lookup *lookup)
  431. {
  432. return nfsd_lookup(rqstp, &cstate->current_fh,
  433. lookup->lo_name, lookup->lo_len,
  434. &cstate->current_fh);
  435. }
  436. static __be32
  437. nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  438. struct nfsd4_read *read)
  439. {
  440. __be32 status;
  441. /* no need to check permission - this will be done in nfsd_read() */
  442. read->rd_filp = NULL;
  443. if (read->rd_offset >= OFFSET_MAX)
  444. return nfserr_inval;
  445. nfs4_lock_state();
  446. /* check stateid */
  447. if ((status = nfs4_preprocess_stateid_op(&cstate->current_fh,
  448. &read->rd_stateid,
  449. CHECK_FH | RD_STATE, &read->rd_filp))) {
  450. dprintk("NFSD: nfsd4_read: couldn't process stateid!\n");
  451. goto out;
  452. }
  453. if (read->rd_filp)
  454. get_file(read->rd_filp);
  455. status = nfs_ok;
  456. out:
  457. nfs4_unlock_state();
  458. read->rd_rqstp = rqstp;
  459. read->rd_fhp = &cstate->current_fh;
  460. return status;
  461. }
  462. static __be32
  463. nfsd4_readdir(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  464. struct nfsd4_readdir *readdir)
  465. {
  466. u64 cookie = readdir->rd_cookie;
  467. static const nfs4_verifier zeroverf;
  468. /* no need to check permission - this will be done in nfsd_readdir() */
  469. if (readdir->rd_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
  470. return nfserr_inval;
  471. readdir->rd_bmval[0] &= NFSD_SUPPORTED_ATTRS_WORD0;
  472. readdir->rd_bmval[1] &= NFSD_SUPPORTED_ATTRS_WORD1;
  473. if ((cookie > ~(u32)0) || (cookie == 1) || (cookie == 2) ||
  474. (cookie == 0 && memcmp(readdir->rd_verf.data, zeroverf.data, NFS4_VERIFIER_SIZE)))
  475. return nfserr_bad_cookie;
  476. readdir->rd_rqstp = rqstp;
  477. readdir->rd_fhp = &cstate->current_fh;
  478. return nfs_ok;
  479. }
  480. static __be32
  481. nfsd4_readlink(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  482. struct nfsd4_readlink *readlink)
  483. {
  484. readlink->rl_rqstp = rqstp;
  485. readlink->rl_fhp = &cstate->current_fh;
  486. return nfs_ok;
  487. }
  488. static __be32
  489. nfsd4_remove(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  490. struct nfsd4_remove *remove)
  491. {
  492. __be32 status;
  493. if (nfs4_in_grace())
  494. return nfserr_grace;
  495. status = nfsd_unlink(rqstp, &cstate->current_fh, 0,
  496. remove->rm_name, remove->rm_namelen);
  497. if (status == nfserr_symlink)
  498. return nfserr_notdir;
  499. if (!status) {
  500. fh_unlock(&cstate->current_fh);
  501. set_change_info(&remove->rm_cinfo, &cstate->current_fh);
  502. }
  503. return status;
  504. }
  505. static __be32
  506. nfsd4_rename(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  507. struct nfsd4_rename *rename)
  508. {
  509. __be32 status = nfserr_nofilehandle;
  510. if (!cstate->save_fh.fh_dentry)
  511. return status;
  512. if (nfs4_in_grace() && !(cstate->save_fh.fh_export->ex_flags
  513. & NFSEXP_NOSUBTREECHECK))
  514. return nfserr_grace;
  515. status = nfsd_rename(rqstp, &cstate->save_fh, rename->rn_sname,
  516. rename->rn_snamelen, &cstate->current_fh,
  517. rename->rn_tname, rename->rn_tnamelen);
  518. /* the underlying filesystem returns different error's than required
  519. * by NFSv4. both save_fh and current_fh have been verified.. */
  520. if (status == nfserr_isdir)
  521. status = nfserr_exist;
  522. else if ((status == nfserr_notdir) &&
  523. (S_ISDIR(cstate->save_fh.fh_dentry->d_inode->i_mode) &&
  524. S_ISDIR(cstate->current_fh.fh_dentry->d_inode->i_mode)))
  525. status = nfserr_exist;
  526. else if (status == nfserr_symlink)
  527. status = nfserr_notdir;
  528. if (!status) {
  529. set_change_info(&rename->rn_sinfo, &cstate->current_fh);
  530. set_change_info(&rename->rn_tinfo, &cstate->save_fh);
  531. }
  532. return status;
  533. }
  534. static __be32
  535. nfsd4_secinfo(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  536. struct nfsd4_secinfo *secinfo)
  537. {
  538. struct svc_fh resfh;
  539. struct svc_export *exp;
  540. struct dentry *dentry;
  541. __be32 err;
  542. fh_init(&resfh, NFS4_FHSIZE);
  543. err = nfsd_lookup_dentry(rqstp, &cstate->current_fh,
  544. secinfo->si_name, secinfo->si_namelen,
  545. &exp, &dentry);
  546. if (err)
  547. return err;
  548. if (dentry->d_inode == NULL) {
  549. exp_put(exp);
  550. err = nfserr_noent;
  551. } else
  552. secinfo->si_exp = exp;
  553. dput(dentry);
  554. return err;
  555. }
  556. static __be32
  557. nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  558. struct nfsd4_setattr *setattr)
  559. {
  560. __be32 status = nfs_ok;
  561. if (setattr->sa_iattr.ia_valid & ATTR_SIZE) {
  562. nfs4_lock_state();
  563. status = nfs4_preprocess_stateid_op(&cstate->current_fh,
  564. &setattr->sa_stateid, CHECK_FH | WR_STATE, NULL);
  565. nfs4_unlock_state();
  566. if (status) {
  567. dprintk("NFSD: nfsd4_setattr: couldn't process stateid!\n");
  568. return status;
  569. }
  570. }
  571. status = mnt_want_write(cstate->current_fh.fh_export->ex_path.mnt);
  572. if (status)
  573. return status;
  574. status = nfs_ok;
  575. if (setattr->sa_acl != NULL)
  576. status = nfsd4_set_nfs4_acl(rqstp, &cstate->current_fh,
  577. setattr->sa_acl);
  578. if (status)
  579. goto out;
  580. status = nfsd_setattr(rqstp, &cstate->current_fh, &setattr->sa_iattr,
  581. 0, (time_t)0);
  582. out:
  583. mnt_drop_write(cstate->current_fh.fh_export->ex_path.mnt);
  584. return status;
  585. }
  586. static __be32
  587. nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  588. struct nfsd4_write *write)
  589. {
  590. stateid_t *stateid = &write->wr_stateid;
  591. struct file *filp = NULL;
  592. u32 *p;
  593. __be32 status = nfs_ok;
  594. /* no need to check permission - this will be done in nfsd_write() */
  595. if (write->wr_offset >= OFFSET_MAX)
  596. return nfserr_inval;
  597. nfs4_lock_state();
  598. status = nfs4_preprocess_stateid_op(&cstate->current_fh, stateid,
  599. CHECK_FH | WR_STATE, &filp);
  600. if (filp)
  601. get_file(filp);
  602. nfs4_unlock_state();
  603. if (status) {
  604. dprintk("NFSD: nfsd4_write: couldn't process stateid!\n");
  605. return status;
  606. }
  607. write->wr_bytes_written = write->wr_buflen;
  608. write->wr_how_written = write->wr_stable_how;
  609. p = (u32 *)write->wr_verifier.data;
  610. *p++ = nfssvc_boot.tv_sec;
  611. *p++ = nfssvc_boot.tv_usec;
  612. status = nfsd_write(rqstp, &cstate->current_fh, filp,
  613. write->wr_offset, rqstp->rq_vec, write->wr_vlen,
  614. write->wr_buflen, &write->wr_how_written);
  615. if (filp)
  616. fput(filp);
  617. if (status == nfserr_symlink)
  618. status = nfserr_inval;
  619. return status;
  620. }
  621. /* This routine never returns NFS_OK! If there are no other errors, it
  622. * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the
  623. * attributes matched. VERIFY is implemented by mapping NFSERR_SAME
  624. * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK.
  625. */
  626. static __be32
  627. _nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  628. struct nfsd4_verify *verify)
  629. {
  630. __be32 *buf, *p;
  631. int count;
  632. __be32 status;
  633. status = fh_verify(rqstp, &cstate->current_fh, 0, MAY_NOP);
  634. if (status)
  635. return status;
  636. if ((verify->ve_bmval[0] & ~NFSD_SUPPORTED_ATTRS_WORD0)
  637. || (verify->ve_bmval[1] & ~NFSD_SUPPORTED_ATTRS_WORD1))
  638. return nfserr_attrnotsupp;
  639. if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR)
  640. || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1))
  641. return nfserr_inval;
  642. if (verify->ve_attrlen & 3)
  643. return nfserr_inval;
  644. /* count in words:
  645. * bitmap_len(1) + bitmap(2) + attr_len(1) = 4
  646. */
  647. count = 4 + (verify->ve_attrlen >> 2);
  648. buf = kmalloc(count << 2, GFP_KERNEL);
  649. if (!buf)
  650. return nfserr_resource;
  651. status = nfsd4_encode_fattr(&cstate->current_fh,
  652. cstate->current_fh.fh_export,
  653. cstate->current_fh.fh_dentry, buf,
  654. &count, verify->ve_bmval,
  655. rqstp, 0);
  656. /* this means that nfsd4_encode_fattr() ran out of space */
  657. if (status == nfserr_resource && count == 0)
  658. status = nfserr_not_same;
  659. if (status)
  660. goto out_kfree;
  661. p = buf + 3;
  662. status = nfserr_not_same;
  663. if (ntohl(*p++) != verify->ve_attrlen)
  664. goto out_kfree;
  665. if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen))
  666. status = nfserr_same;
  667. out_kfree:
  668. kfree(buf);
  669. return status;
  670. }
  671. static __be32
  672. nfsd4_nverify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  673. struct nfsd4_verify *verify)
  674. {
  675. __be32 status;
  676. status = _nfsd4_verify(rqstp, cstate, verify);
  677. return status == nfserr_not_same ? nfs_ok : status;
  678. }
  679. static __be32
  680. nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  681. struct nfsd4_verify *verify)
  682. {
  683. __be32 status;
  684. status = _nfsd4_verify(rqstp, cstate, verify);
  685. return status == nfserr_same ? nfs_ok : status;
  686. }
  687. /*
  688. * NULL call.
  689. */
  690. static __be32
  691. nfsd4_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
  692. {
  693. return nfs_ok;
  694. }
  695. static inline void nfsd4_increment_op_stats(u32 opnum)
  696. {
  697. if (opnum >= FIRST_NFS4_OP && opnum <= LAST_NFS4_OP)
  698. nfsdstats.nfs4_opcount[opnum]++;
  699. }
  700. static void cstate_free(struct nfsd4_compound_state *cstate)
  701. {
  702. if (cstate == NULL)
  703. return;
  704. fh_put(&cstate->current_fh);
  705. fh_put(&cstate->save_fh);
  706. BUG_ON(cstate->replay_owner);
  707. kfree(cstate);
  708. }
  709. static struct nfsd4_compound_state *cstate_alloc(void)
  710. {
  711. struct nfsd4_compound_state *cstate;
  712. cstate = kmalloc(sizeof(struct nfsd4_compound_state), GFP_KERNEL);
  713. if (cstate == NULL)
  714. return NULL;
  715. fh_init(&cstate->current_fh, NFS4_FHSIZE);
  716. fh_init(&cstate->save_fh, NFS4_FHSIZE);
  717. cstate->replay_owner = NULL;
  718. return cstate;
  719. }
  720. typedef __be32(*nfsd4op_func)(struct svc_rqst *, struct nfsd4_compound_state *,
  721. void *);
  722. struct nfsd4_operation {
  723. nfsd4op_func op_func;
  724. u32 op_flags;
  725. /* Most ops require a valid current filehandle; a few don't: */
  726. #define ALLOWED_WITHOUT_FH 1
  727. /* GETATTR and ops not listed as returning NFS4ERR_MOVED: */
  728. #define ALLOWED_ON_ABSENT_FS 2
  729. };
  730. static struct nfsd4_operation nfsd4_ops[];
  731. /*
  732. * COMPOUND call.
  733. */
  734. static __be32
  735. nfsd4_proc_compound(struct svc_rqst *rqstp,
  736. struct nfsd4_compoundargs *args,
  737. struct nfsd4_compoundres *resp)
  738. {
  739. struct nfsd4_op *op;
  740. struct nfsd4_operation *opdesc;
  741. struct nfsd4_compound_state *cstate = NULL;
  742. int slack_bytes;
  743. __be32 status;
  744. status = nfserr_resource;
  745. cstate = cstate_alloc();
  746. if (cstate == NULL)
  747. goto out;
  748. resp->xbuf = &rqstp->rq_res;
  749. resp->p = rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len;
  750. resp->tagp = resp->p;
  751. /* reserve space for: taglen, tag, and opcnt */
  752. resp->p += 2 + XDR_QUADLEN(args->taglen);
  753. resp->end = rqstp->rq_res.head[0].iov_base + PAGE_SIZE;
  754. resp->taglen = args->taglen;
  755. resp->tag = args->tag;
  756. resp->opcnt = 0;
  757. resp->rqstp = rqstp;
  758. /*
  759. * According to RFC3010, this takes precedence over all other errors.
  760. */
  761. status = nfserr_minor_vers_mismatch;
  762. if (args->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
  763. goto out;
  764. status = nfs_ok;
  765. while (!status && resp->opcnt < args->opcnt) {
  766. op = &args->ops[resp->opcnt++];
  767. dprintk("nfsv4 compound op #%d: %d\n", resp->opcnt, op->opnum);
  768. /*
  769. * The XDR decode routines may have pre-set op->status;
  770. * for example, if there is a miscellaneous XDR error
  771. * it will be set to nfserr_bad_xdr.
  772. */
  773. if (op->status)
  774. goto encode_op;
  775. /* We must be able to encode a successful response to
  776. * this operation, with enough room left over to encode a
  777. * failed response to the next operation. If we don't
  778. * have enough room, fail with ERR_RESOURCE.
  779. */
  780. slack_bytes = (char *)resp->end - (char *)resp->p;
  781. if (slack_bytes < COMPOUND_SLACK_SPACE
  782. + COMPOUND_ERR_SLACK_SPACE) {
  783. BUG_ON(slack_bytes < COMPOUND_ERR_SLACK_SPACE);
  784. op->status = nfserr_resource;
  785. goto encode_op;
  786. }
  787. opdesc = &nfsd4_ops[op->opnum];
  788. if (!cstate->current_fh.fh_dentry) {
  789. if (!(opdesc->op_flags & ALLOWED_WITHOUT_FH)) {
  790. op->status = nfserr_nofilehandle;
  791. goto encode_op;
  792. }
  793. } else if (cstate->current_fh.fh_export->ex_fslocs.migrated &&
  794. !(opdesc->op_flags & ALLOWED_ON_ABSENT_FS)) {
  795. op->status = nfserr_moved;
  796. goto encode_op;
  797. }
  798. if (opdesc->op_func)
  799. op->status = opdesc->op_func(rqstp, cstate, &op->u);
  800. else
  801. BUG_ON(op->status == nfs_ok);
  802. encode_op:
  803. if (op->status == nfserr_replay_me) {
  804. op->replay = &cstate->replay_owner->so_replay;
  805. nfsd4_encode_replay(resp, op);
  806. status = op->status = op->replay->rp_status;
  807. } else {
  808. nfsd4_encode_operation(resp, op);
  809. status = op->status;
  810. }
  811. if (cstate->replay_owner) {
  812. nfs4_put_stateowner(cstate->replay_owner);
  813. cstate->replay_owner = NULL;
  814. }
  815. /* XXX Ugh, we need to get rid of this kind of special case: */
  816. if (op->opnum == OP_READ && op->u.read.rd_filp)
  817. fput(op->u.read.rd_filp);
  818. nfsd4_increment_op_stats(op->opnum);
  819. }
  820. out:
  821. nfsd4_release_compoundargs(args);
  822. cstate_free(cstate);
  823. return status;
  824. }
  825. static struct nfsd4_operation nfsd4_ops[OP_RELEASE_LOCKOWNER+1] = {
  826. [OP_ACCESS] = {
  827. .op_func = (nfsd4op_func)nfsd4_access,
  828. },
  829. [OP_CLOSE] = {
  830. .op_func = (nfsd4op_func)nfsd4_close,
  831. },
  832. [OP_COMMIT] = {
  833. .op_func = (nfsd4op_func)nfsd4_commit,
  834. },
  835. [OP_CREATE] = {
  836. .op_func = (nfsd4op_func)nfsd4_create,
  837. },
  838. [OP_DELEGRETURN] = {
  839. .op_func = (nfsd4op_func)nfsd4_delegreturn,
  840. },
  841. [OP_GETATTR] = {
  842. .op_func = (nfsd4op_func)nfsd4_getattr,
  843. .op_flags = ALLOWED_ON_ABSENT_FS,
  844. },
  845. [OP_GETFH] = {
  846. .op_func = (nfsd4op_func)nfsd4_getfh,
  847. },
  848. [OP_LINK] = {
  849. .op_func = (nfsd4op_func)nfsd4_link,
  850. },
  851. [OP_LOCK] = {
  852. .op_func = (nfsd4op_func)nfsd4_lock,
  853. },
  854. [OP_LOCKT] = {
  855. .op_func = (nfsd4op_func)nfsd4_lockt,
  856. },
  857. [OP_LOCKU] = {
  858. .op_func = (nfsd4op_func)nfsd4_locku,
  859. },
  860. [OP_LOOKUP] = {
  861. .op_func = (nfsd4op_func)nfsd4_lookup,
  862. },
  863. [OP_LOOKUPP] = {
  864. .op_func = (nfsd4op_func)nfsd4_lookupp,
  865. },
  866. [OP_NVERIFY] = {
  867. .op_func = (nfsd4op_func)nfsd4_nverify,
  868. },
  869. [OP_OPEN] = {
  870. .op_func = (nfsd4op_func)nfsd4_open,
  871. },
  872. [OP_OPEN_CONFIRM] = {
  873. .op_func = (nfsd4op_func)nfsd4_open_confirm,
  874. },
  875. [OP_OPEN_DOWNGRADE] = {
  876. .op_func = (nfsd4op_func)nfsd4_open_downgrade,
  877. },
  878. [OP_PUTFH] = {
  879. .op_func = (nfsd4op_func)nfsd4_putfh,
  880. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
  881. },
  882. [OP_PUTPUBFH] = {
  883. /* unsupported; just for future reference: */
  884. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
  885. },
  886. [OP_PUTROOTFH] = {
  887. .op_func = (nfsd4op_func)nfsd4_putrootfh,
  888. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
  889. },
  890. [OP_READ] = {
  891. .op_func = (nfsd4op_func)nfsd4_read,
  892. },
  893. [OP_READDIR] = {
  894. .op_func = (nfsd4op_func)nfsd4_readdir,
  895. },
  896. [OP_READLINK] = {
  897. .op_func = (nfsd4op_func)nfsd4_readlink,
  898. },
  899. [OP_REMOVE] = {
  900. .op_func = (nfsd4op_func)nfsd4_remove,
  901. },
  902. [OP_RENAME] = {
  903. .op_func = (nfsd4op_func)nfsd4_rename,
  904. },
  905. [OP_RENEW] = {
  906. .op_func = (nfsd4op_func)nfsd4_renew,
  907. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
  908. },
  909. [OP_RESTOREFH] = {
  910. .op_func = (nfsd4op_func)nfsd4_restorefh,
  911. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
  912. },
  913. [OP_SAVEFH] = {
  914. .op_func = (nfsd4op_func)nfsd4_savefh,
  915. },
  916. [OP_SECINFO] = {
  917. .op_func = (nfsd4op_func)nfsd4_secinfo,
  918. },
  919. [OP_SETATTR] = {
  920. .op_func = (nfsd4op_func)nfsd4_setattr,
  921. },
  922. [OP_SETCLIENTID] = {
  923. .op_func = (nfsd4op_func)nfsd4_setclientid,
  924. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
  925. },
  926. [OP_SETCLIENTID_CONFIRM] = {
  927. .op_func = (nfsd4op_func)nfsd4_setclientid_confirm,
  928. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
  929. },
  930. [OP_VERIFY] = {
  931. .op_func = (nfsd4op_func)nfsd4_verify,
  932. },
  933. [OP_WRITE] = {
  934. .op_func = (nfsd4op_func)nfsd4_write,
  935. },
  936. [OP_RELEASE_LOCKOWNER] = {
  937. .op_func = (nfsd4op_func)nfsd4_release_lockowner,
  938. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
  939. },
  940. };
  941. #define nfs4svc_decode_voidargs NULL
  942. #define nfs4svc_release_void NULL
  943. #define nfsd4_voidres nfsd4_voidargs
  944. #define nfs4svc_release_compound NULL
  945. struct nfsd4_voidargs { int dummy; };
  946. #define PROC(name, argt, rest, relt, cache, respsize) \
  947. { (svc_procfunc) nfsd4_proc_##name, \
  948. (kxdrproc_t) nfs4svc_decode_##argt##args, \
  949. (kxdrproc_t) nfs4svc_encode_##rest##res, \
  950. (kxdrproc_t) nfs4svc_release_##relt, \
  951. sizeof(struct nfsd4_##argt##args), \
  952. sizeof(struct nfsd4_##rest##res), \
  953. 0, \
  954. cache, \
  955. respsize, \
  956. }
  957. /*
  958. * TODO: At the present time, the NFSv4 server does not do XID caching
  959. * of requests. Implementing XID caching would not be a serious problem,
  960. * although it would require a mild change in interfaces since one
  961. * doesn't know whether an NFSv4 request is idempotent until after the
  962. * XDR decode. However, XID caching totally confuses pynfs (Peter
  963. * Astrand's regression testsuite for NFSv4 servers), which reuses
  964. * XID's liberally, so I've left it unimplemented until pynfs generates
  965. * better XID's.
  966. */
  967. static struct svc_procedure nfsd_procedures4[2] = {
  968. PROC(null, void, void, void, RC_NOCACHE, 1),
  969. PROC(compound, compound, compound, compound, RC_NOCACHE, NFSD_BUFSIZE/4)
  970. };
  971. struct svc_version nfsd_version4 = {
  972. .vs_vers = 4,
  973. .vs_nproc = 2,
  974. .vs_proc = nfsd_procedures4,
  975. .vs_dispatch = nfsd_dispatch,
  976. .vs_xdrsize = NFS4_SVC_XDRSIZE,
  977. };
  978. /*
  979. * Local variables:
  980. * c-basic-offset: 8
  981. * End:
  982. */