nfs4proc.c 28 KB

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