nfs4proc.c 31 KB

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