nfs4proc.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  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 int
  66. do_open_permission(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
  67. {
  68. int accmode, status;
  69. if (open->op_truncate &&
  70. !(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
  71. return nfserr_inval;
  72. accmode = MAY_NOP;
  73. if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
  74. accmode = MAY_READ;
  75. if (open->op_share_deny & NFS4_SHARE_ACCESS_WRITE)
  76. accmode |= (MAY_WRITE | MAY_TRUNC);
  77. accmode |= MAY_OWNER_OVERRIDE;
  78. status = fh_verify(rqstp, current_fh, S_IFREG, accmode);
  79. return status;
  80. }
  81. static int
  82. do_open_lookup(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
  83. {
  84. struct svc_fh resfh;
  85. int 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);
  113. }
  114. fh_put(&resfh);
  115. return status;
  116. }
  117. static int
  118. do_open_fhandle(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
  119. {
  120. int 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);
  136. return status;
  137. }
  138. static inline int
  139. nfsd4_open(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open, struct nfs4_stateowner **replay_owner)
  140. {
  141. int 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 int
  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 int
  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 int
  249. nfsd4_putrootfh(struct svc_rqst *rqstp, struct svc_fh *current_fh)
  250. {
  251. int status;
  252. fh_put(current_fh);
  253. status = exp_pseudoroot(rqstp->rq_client, current_fh,
  254. &rqstp->rq_chandle);
  255. if (!status)
  256. status = nfserrno(nfsd_setuser(rqstp, current_fh->fh_export));
  257. return status;
  258. }
  259. static inline int
  260. nfsd4_restorefh(struct svc_fh *current_fh, struct svc_fh *save_fh)
  261. {
  262. if (!save_fh->fh_dentry)
  263. return nfserr_restorefh;
  264. fh_dup2(current_fh, save_fh);
  265. return nfs_ok;
  266. }
  267. static inline int
  268. nfsd4_savefh(struct svc_fh *current_fh, struct svc_fh *save_fh)
  269. {
  270. if (!current_fh->fh_dentry)
  271. return nfserr_nofilehandle;
  272. fh_dup2(save_fh, current_fh);
  273. return nfs_ok;
  274. }
  275. /*
  276. * misc nfsv4 ops
  277. */
  278. static inline int
  279. nfsd4_access(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_access *access)
  280. {
  281. if (access->ac_req_access & ~NFS3_ACCESS_FULL)
  282. return nfserr_inval;
  283. access->ac_resp_access = access->ac_req_access;
  284. return nfsd_access(rqstp, current_fh, &access->ac_resp_access, &access->ac_supported);
  285. }
  286. static inline int
  287. nfsd4_commit(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_commit *commit)
  288. {
  289. int status;
  290. u32 *p = (u32 *)commit->co_verf.data;
  291. *p++ = nfssvc_boot.tv_sec;
  292. *p++ = nfssvc_boot.tv_usec;
  293. status = nfsd_commit(rqstp, current_fh, commit->co_offset, commit->co_count);
  294. if (status == nfserr_symlink)
  295. status = nfserr_inval;
  296. return status;
  297. }
  298. static int
  299. nfsd4_create(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_create *create)
  300. {
  301. struct svc_fh resfh;
  302. int status;
  303. dev_t rdev;
  304. fh_init(&resfh, NFS4_FHSIZE);
  305. status = fh_verify(rqstp, current_fh, S_IFDIR, MAY_CREATE);
  306. if (status == nfserr_symlink)
  307. status = nfserr_notdir;
  308. if (status)
  309. return status;
  310. switch (create->cr_type) {
  311. case NF4LNK:
  312. /* ugh! we have to null-terminate the linktext, or
  313. * vfs_symlink() will choke. it is always safe to
  314. * null-terminate by brute force, since at worst we
  315. * will overwrite the first byte of the create namelen
  316. * in the XDR buffer, which has already been extracted
  317. * during XDR decode.
  318. */
  319. create->cr_linkname[create->cr_linklen] = 0;
  320. status = nfsd_symlink(rqstp, current_fh, create->cr_name,
  321. create->cr_namelen, create->cr_linkname,
  322. create->cr_linklen, &resfh, &create->cr_iattr);
  323. break;
  324. case NF4BLK:
  325. rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
  326. if (MAJOR(rdev) != create->cr_specdata1 ||
  327. MINOR(rdev) != create->cr_specdata2)
  328. return nfserr_inval;
  329. status = nfsd_create(rqstp, current_fh, create->cr_name,
  330. create->cr_namelen, &create->cr_iattr,
  331. S_IFBLK, rdev, &resfh);
  332. break;
  333. case NF4CHR:
  334. rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
  335. if (MAJOR(rdev) != create->cr_specdata1 ||
  336. MINOR(rdev) != create->cr_specdata2)
  337. return nfserr_inval;
  338. status = nfsd_create(rqstp, current_fh, create->cr_name,
  339. create->cr_namelen, &create->cr_iattr,
  340. S_IFCHR, rdev, &resfh);
  341. break;
  342. case NF4SOCK:
  343. status = nfsd_create(rqstp, current_fh, create->cr_name,
  344. create->cr_namelen, &create->cr_iattr,
  345. S_IFSOCK, 0, &resfh);
  346. break;
  347. case NF4FIFO:
  348. status = nfsd_create(rqstp, current_fh, create->cr_name,
  349. create->cr_namelen, &create->cr_iattr,
  350. S_IFIFO, 0, &resfh);
  351. break;
  352. case NF4DIR:
  353. create->cr_iattr.ia_valid &= ~ATTR_SIZE;
  354. status = nfsd_create(rqstp, current_fh, create->cr_name,
  355. create->cr_namelen, &create->cr_iattr,
  356. S_IFDIR, 0, &resfh);
  357. break;
  358. default:
  359. status = nfserr_badtype;
  360. }
  361. if (!status) {
  362. fh_unlock(current_fh);
  363. set_change_info(&create->cr_cinfo, current_fh);
  364. fh_dup2(current_fh, &resfh);
  365. }
  366. fh_put(&resfh);
  367. return status;
  368. }
  369. static inline int
  370. nfsd4_getattr(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_getattr *getattr)
  371. {
  372. int status;
  373. status = fh_verify(rqstp, current_fh, 0, MAY_NOP);
  374. if (status)
  375. return status;
  376. if (getattr->ga_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
  377. return nfserr_inval;
  378. getattr->ga_bmval[0] &= NFSD_SUPPORTED_ATTRS_WORD0;
  379. getattr->ga_bmval[1] &= NFSD_SUPPORTED_ATTRS_WORD1;
  380. getattr->ga_fhp = current_fh;
  381. return nfs_ok;
  382. }
  383. static inline int
  384. nfsd4_link(struct svc_rqst *rqstp, struct svc_fh *current_fh,
  385. struct svc_fh *save_fh, struct nfsd4_link *link)
  386. {
  387. int status = nfserr_nofilehandle;
  388. if (!save_fh->fh_dentry)
  389. return status;
  390. status = nfsd_link(rqstp, current_fh, link->li_name, link->li_namelen, save_fh);
  391. if (!status)
  392. set_change_info(&link->li_cinfo, current_fh);
  393. return status;
  394. }
  395. static int
  396. nfsd4_lookupp(struct svc_rqst *rqstp, struct svc_fh *current_fh)
  397. {
  398. struct svc_fh tmp_fh;
  399. int ret;
  400. fh_init(&tmp_fh, NFS4_FHSIZE);
  401. if((ret = exp_pseudoroot(rqstp->rq_client, &tmp_fh,
  402. &rqstp->rq_chandle)) != 0)
  403. return ret;
  404. if (tmp_fh.fh_dentry == current_fh->fh_dentry) {
  405. fh_put(&tmp_fh);
  406. return nfserr_noent;
  407. }
  408. fh_put(&tmp_fh);
  409. return nfsd_lookup(rqstp, current_fh, "..", 2, current_fh);
  410. }
  411. static inline int
  412. nfsd4_lookup(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lookup *lookup)
  413. {
  414. return nfsd_lookup(rqstp, current_fh, lookup->lo_name, lookup->lo_len, current_fh);
  415. }
  416. static inline int
  417. nfsd4_read(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_read *read)
  418. {
  419. int status;
  420. /* no need to check permission - this will be done in nfsd_read() */
  421. read->rd_filp = NULL;
  422. if (read->rd_offset >= OFFSET_MAX)
  423. return nfserr_inval;
  424. nfs4_lock_state();
  425. /* check stateid */
  426. if ((status = nfs4_preprocess_stateid_op(current_fh, &read->rd_stateid,
  427. CHECK_FH | RD_STATE, &read->rd_filp))) {
  428. dprintk("NFSD: nfsd4_read: couldn't process stateid!\n");
  429. goto out;
  430. }
  431. if (read->rd_filp)
  432. get_file(read->rd_filp);
  433. status = nfs_ok;
  434. out:
  435. nfs4_unlock_state();
  436. read->rd_rqstp = rqstp;
  437. read->rd_fhp = current_fh;
  438. return status;
  439. }
  440. static inline int
  441. nfsd4_readdir(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_readdir *readdir)
  442. {
  443. u64 cookie = readdir->rd_cookie;
  444. static const nfs4_verifier zeroverf;
  445. /* no need to check permission - this will be done in nfsd_readdir() */
  446. if (readdir->rd_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
  447. return nfserr_inval;
  448. readdir->rd_bmval[0] &= NFSD_SUPPORTED_ATTRS_WORD0;
  449. readdir->rd_bmval[1] &= NFSD_SUPPORTED_ATTRS_WORD1;
  450. if ((cookie > ~(u32)0) || (cookie == 1) || (cookie == 2) ||
  451. (cookie == 0 && memcmp(readdir->rd_verf.data, zeroverf.data, NFS4_VERIFIER_SIZE)))
  452. return nfserr_bad_cookie;
  453. readdir->rd_rqstp = rqstp;
  454. readdir->rd_fhp = current_fh;
  455. return nfs_ok;
  456. }
  457. static inline int
  458. nfsd4_readlink(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_readlink *readlink)
  459. {
  460. readlink->rl_rqstp = rqstp;
  461. readlink->rl_fhp = current_fh;
  462. return nfs_ok;
  463. }
  464. static inline int
  465. nfsd4_remove(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_remove *remove)
  466. {
  467. int status;
  468. if (nfs4_in_grace())
  469. return nfserr_grace;
  470. status = nfsd_unlink(rqstp, current_fh, 0, remove->rm_name, remove->rm_namelen);
  471. if (status == nfserr_symlink)
  472. return nfserr_notdir;
  473. if (!status) {
  474. fh_unlock(current_fh);
  475. set_change_info(&remove->rm_cinfo, current_fh);
  476. }
  477. return status;
  478. }
  479. static inline int
  480. nfsd4_rename(struct svc_rqst *rqstp, struct svc_fh *current_fh,
  481. struct svc_fh *save_fh, struct nfsd4_rename *rename)
  482. {
  483. int status = nfserr_nofilehandle;
  484. if (!save_fh->fh_dentry)
  485. return status;
  486. if (nfs4_in_grace() && !(save_fh->fh_export->ex_flags
  487. & NFSEXP_NOSUBTREECHECK))
  488. return nfserr_grace;
  489. status = nfsd_rename(rqstp, save_fh, rename->rn_sname,
  490. rename->rn_snamelen, current_fh,
  491. rename->rn_tname, rename->rn_tnamelen);
  492. /* the underlying filesystem returns different error's than required
  493. * by NFSv4. both save_fh and current_fh have been verified.. */
  494. if (status == nfserr_isdir)
  495. status = nfserr_exist;
  496. else if ((status == nfserr_notdir) &&
  497. (S_ISDIR(save_fh->fh_dentry->d_inode->i_mode) &&
  498. S_ISDIR(current_fh->fh_dentry->d_inode->i_mode)))
  499. status = nfserr_exist;
  500. else if (status == nfserr_symlink)
  501. status = nfserr_notdir;
  502. if (!status) {
  503. set_change_info(&rename->rn_sinfo, current_fh);
  504. set_change_info(&rename->rn_tinfo, save_fh);
  505. }
  506. return status;
  507. }
  508. static inline int
  509. nfsd4_setattr(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_setattr *setattr)
  510. {
  511. int status = nfs_ok;
  512. if (setattr->sa_iattr.ia_valid & ATTR_SIZE) {
  513. nfs4_lock_state();
  514. status = nfs4_preprocess_stateid_op(current_fh,
  515. &setattr->sa_stateid, CHECK_FH | WR_STATE, NULL);
  516. nfs4_unlock_state();
  517. if (status) {
  518. dprintk("NFSD: nfsd4_setattr: couldn't process stateid!");
  519. return status;
  520. }
  521. }
  522. status = nfs_ok;
  523. if (setattr->sa_acl != NULL)
  524. status = nfsd4_set_nfs4_acl(rqstp, current_fh, setattr->sa_acl);
  525. if (status)
  526. return status;
  527. status = nfsd_setattr(rqstp, current_fh, &setattr->sa_iattr,
  528. 0, (time_t)0);
  529. return status;
  530. }
  531. static inline int
  532. nfsd4_write(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_write *write)
  533. {
  534. stateid_t *stateid = &write->wr_stateid;
  535. struct file *filp = NULL;
  536. u32 *p;
  537. int status = nfs_ok;
  538. /* no need to check permission - this will be done in nfsd_write() */
  539. if (write->wr_offset >= OFFSET_MAX)
  540. return nfserr_inval;
  541. nfs4_lock_state();
  542. status = nfs4_preprocess_stateid_op(current_fh, stateid,
  543. CHECK_FH | WR_STATE, &filp);
  544. if (filp)
  545. get_file(filp);
  546. nfs4_unlock_state();
  547. if (status) {
  548. dprintk("NFSD: nfsd4_write: couldn't process stateid!\n");
  549. return status;
  550. }
  551. write->wr_bytes_written = write->wr_buflen;
  552. write->wr_how_written = write->wr_stable_how;
  553. p = (u32 *)write->wr_verifier.data;
  554. *p++ = nfssvc_boot.tv_sec;
  555. *p++ = nfssvc_boot.tv_usec;
  556. status = nfsd_write(rqstp, current_fh, filp, write->wr_offset,
  557. write->wr_vec, write->wr_vlen, write->wr_buflen,
  558. &write->wr_how_written);
  559. if (filp)
  560. fput(filp);
  561. if (status == nfserr_symlink)
  562. status = nfserr_inval;
  563. return status;
  564. }
  565. /* This routine never returns NFS_OK! If there are no other errors, it
  566. * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the
  567. * attributes matched. VERIFY is implemented by mapping NFSERR_SAME
  568. * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK.
  569. */
  570. static int
  571. nfsd4_verify(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_verify *verify)
  572. {
  573. u32 *buf, *p;
  574. int count;
  575. int status;
  576. status = fh_verify(rqstp, current_fh, 0, MAY_NOP);
  577. if (status)
  578. return status;
  579. if ((verify->ve_bmval[0] & ~NFSD_SUPPORTED_ATTRS_WORD0)
  580. || (verify->ve_bmval[1] & ~NFSD_SUPPORTED_ATTRS_WORD1))
  581. return nfserr_attrnotsupp;
  582. if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR)
  583. || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1))
  584. return nfserr_inval;
  585. if (verify->ve_attrlen & 3)
  586. return nfserr_inval;
  587. /* count in words:
  588. * bitmap_len(1) + bitmap(2) + attr_len(1) = 4
  589. */
  590. count = 4 + (verify->ve_attrlen >> 2);
  591. buf = kmalloc(count << 2, GFP_KERNEL);
  592. if (!buf)
  593. return nfserr_resource;
  594. status = nfsd4_encode_fattr(current_fh, current_fh->fh_export,
  595. current_fh->fh_dentry, buf,
  596. &count, verify->ve_bmval,
  597. rqstp);
  598. /* this means that nfsd4_encode_fattr() ran out of space */
  599. if (status == nfserr_resource && count == 0)
  600. status = nfserr_not_same;
  601. if (status)
  602. goto out_kfree;
  603. p = buf + 3;
  604. status = nfserr_not_same;
  605. if (ntohl(*p++) != verify->ve_attrlen)
  606. goto out_kfree;
  607. if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen))
  608. status = nfserr_same;
  609. out_kfree:
  610. kfree(buf);
  611. return status;
  612. }
  613. /*
  614. * NULL call.
  615. */
  616. static int
  617. nfsd4_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
  618. {
  619. return nfs_ok;
  620. }
  621. /*
  622. * COMPOUND call.
  623. */
  624. static int
  625. nfsd4_proc_compound(struct svc_rqst *rqstp,
  626. struct nfsd4_compoundargs *args,
  627. struct nfsd4_compoundres *resp)
  628. {
  629. struct nfsd4_op *op;
  630. struct svc_fh *current_fh = NULL;
  631. struct svc_fh *save_fh = NULL;
  632. struct nfs4_stateowner *replay_owner = NULL;
  633. int slack_space; /* in words, not bytes! */
  634. int status;
  635. status = nfserr_resource;
  636. current_fh = kmalloc(sizeof(*current_fh), GFP_KERNEL);
  637. if (current_fh == NULL)
  638. goto out;
  639. fh_init(current_fh, NFS4_FHSIZE);
  640. save_fh = kmalloc(sizeof(*save_fh), GFP_KERNEL);
  641. if (save_fh == NULL)
  642. goto out;
  643. fh_init(save_fh, NFS4_FHSIZE);
  644. resp->xbuf = &rqstp->rq_res;
  645. resp->p = rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len;
  646. resp->tagp = resp->p;
  647. /* reserve space for: taglen, tag, and opcnt */
  648. resp->p += 2 + XDR_QUADLEN(args->taglen);
  649. resp->end = rqstp->rq_res.head[0].iov_base + PAGE_SIZE;
  650. resp->taglen = args->taglen;
  651. resp->tag = args->tag;
  652. resp->opcnt = 0;
  653. resp->rqstp = rqstp;
  654. /*
  655. * According to RFC3010, this takes precedence over all other errors.
  656. */
  657. status = nfserr_minor_vers_mismatch;
  658. if (args->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
  659. goto out;
  660. status = nfs_ok;
  661. while (!status && resp->opcnt < args->opcnt) {
  662. op = &args->ops[resp->opcnt++];
  663. dprintk("nfsv4 compound op #%d: %d\n", resp->opcnt, op->opnum);
  664. /*
  665. * The XDR decode routines may have pre-set op->status;
  666. * for example, if there is a miscellaneous XDR error
  667. * it will be set to nfserr_bad_xdr.
  668. */
  669. if (op->status)
  670. goto encode_op;
  671. /* We must be able to encode a successful response to
  672. * this operation, with enough room left over to encode a
  673. * failed response to the next operation. If we don't
  674. * have enough room, fail with ERR_RESOURCE.
  675. */
  676. /* FIXME - is slack_space *really* words, or bytes??? - neilb */
  677. slack_space = (char *)resp->end - (char *)resp->p;
  678. if (slack_space < COMPOUND_SLACK_SPACE + COMPOUND_ERR_SLACK_SPACE) {
  679. BUG_ON(slack_space < COMPOUND_ERR_SLACK_SPACE);
  680. op->status = nfserr_resource;
  681. goto encode_op;
  682. }
  683. /* All operations except RENEW, SETCLIENTID, RESTOREFH
  684. * SETCLIENTID_CONFIRM, PUTFH and PUTROOTFH
  685. * require a valid current filehandle
  686. */
  687. if ((!current_fh->fh_dentry) &&
  688. !((op->opnum == OP_PUTFH) || (op->opnum == OP_PUTROOTFH) ||
  689. (op->opnum == OP_SETCLIENTID) ||
  690. (op->opnum == OP_SETCLIENTID_CONFIRM) ||
  691. (op->opnum == OP_RENEW) || (op->opnum == OP_RESTOREFH) ||
  692. (op->opnum == OP_RELEASE_LOCKOWNER))) {
  693. op->status = nfserr_nofilehandle;
  694. goto encode_op;
  695. }
  696. switch (op->opnum) {
  697. case OP_ACCESS:
  698. op->status = nfsd4_access(rqstp, current_fh, &op->u.access);
  699. break;
  700. case OP_CLOSE:
  701. op->status = nfsd4_close(rqstp, current_fh, &op->u.close, &replay_owner);
  702. break;
  703. case OP_COMMIT:
  704. op->status = nfsd4_commit(rqstp, current_fh, &op->u.commit);
  705. break;
  706. case OP_CREATE:
  707. op->status = nfsd4_create(rqstp, current_fh, &op->u.create);
  708. break;
  709. case OP_DELEGRETURN:
  710. op->status = nfsd4_delegreturn(rqstp, current_fh, &op->u.delegreturn);
  711. break;
  712. case OP_GETATTR:
  713. op->status = nfsd4_getattr(rqstp, current_fh, &op->u.getattr);
  714. break;
  715. case OP_GETFH:
  716. op->status = nfsd4_getfh(current_fh, &op->u.getfh);
  717. break;
  718. case OP_LINK:
  719. op->status = nfsd4_link(rqstp, current_fh, save_fh, &op->u.link);
  720. break;
  721. case OP_LOCK:
  722. op->status = nfsd4_lock(rqstp, current_fh, &op->u.lock, &replay_owner);
  723. break;
  724. case OP_LOCKT:
  725. op->status = nfsd4_lockt(rqstp, current_fh, &op->u.lockt);
  726. break;
  727. case OP_LOCKU:
  728. op->status = nfsd4_locku(rqstp, current_fh, &op->u.locku, &replay_owner);
  729. break;
  730. case OP_LOOKUP:
  731. op->status = nfsd4_lookup(rqstp, current_fh, &op->u.lookup);
  732. break;
  733. case OP_LOOKUPP:
  734. op->status = nfsd4_lookupp(rqstp, current_fh);
  735. break;
  736. case OP_NVERIFY:
  737. op->status = nfsd4_verify(rqstp, current_fh, &op->u.nverify);
  738. if (op->status == nfserr_not_same)
  739. op->status = nfs_ok;
  740. break;
  741. case OP_OPEN:
  742. op->status = nfsd4_open(rqstp, current_fh, &op->u.open, &replay_owner);
  743. break;
  744. case OP_OPEN_CONFIRM:
  745. op->status = nfsd4_open_confirm(rqstp, current_fh, &op->u.open_confirm, &replay_owner);
  746. break;
  747. case OP_OPEN_DOWNGRADE:
  748. op->status = nfsd4_open_downgrade(rqstp, current_fh, &op->u.open_downgrade, &replay_owner);
  749. break;
  750. case OP_PUTFH:
  751. op->status = nfsd4_putfh(rqstp, current_fh, &op->u.putfh);
  752. break;
  753. case OP_PUTROOTFH:
  754. op->status = nfsd4_putrootfh(rqstp, current_fh);
  755. break;
  756. case OP_READ:
  757. op->status = nfsd4_read(rqstp, current_fh, &op->u.read);
  758. break;
  759. case OP_READDIR:
  760. op->status = nfsd4_readdir(rqstp, current_fh, &op->u.readdir);
  761. break;
  762. case OP_READLINK:
  763. op->status = nfsd4_readlink(rqstp, current_fh, &op->u.readlink);
  764. break;
  765. case OP_REMOVE:
  766. op->status = nfsd4_remove(rqstp, current_fh, &op->u.remove);
  767. break;
  768. case OP_RENAME:
  769. op->status = nfsd4_rename(rqstp, current_fh, save_fh, &op->u.rename);
  770. break;
  771. case OP_RENEW:
  772. op->status = nfsd4_renew(&op->u.renew);
  773. break;
  774. case OP_RESTOREFH:
  775. op->status = nfsd4_restorefh(current_fh, save_fh);
  776. break;
  777. case OP_SAVEFH:
  778. op->status = nfsd4_savefh(current_fh, save_fh);
  779. break;
  780. case OP_SETATTR:
  781. op->status = nfsd4_setattr(rqstp, current_fh, &op->u.setattr);
  782. break;
  783. case OP_SETCLIENTID:
  784. op->status = nfsd4_setclientid(rqstp, &op->u.setclientid);
  785. break;
  786. case OP_SETCLIENTID_CONFIRM:
  787. op->status = nfsd4_setclientid_confirm(rqstp, &op->u.setclientid_confirm);
  788. break;
  789. case OP_VERIFY:
  790. op->status = nfsd4_verify(rqstp, current_fh, &op->u.verify);
  791. if (op->status == nfserr_same)
  792. op->status = nfs_ok;
  793. break;
  794. case OP_WRITE:
  795. op->status = nfsd4_write(rqstp, current_fh, &op->u.write);
  796. break;
  797. case OP_RELEASE_LOCKOWNER:
  798. op->status = nfsd4_release_lockowner(rqstp, &op->u.release_lockowner);
  799. break;
  800. default:
  801. BUG_ON(op->status == nfs_ok);
  802. break;
  803. }
  804. encode_op:
  805. if (op->status == NFSERR_REPLAY_ME) {
  806. op->replay = &replay_owner->so_replay;
  807. nfsd4_encode_replay(resp, op);
  808. status = op->status = op->replay->rp_status;
  809. } else {
  810. nfsd4_encode_operation(resp, op);
  811. status = op->status;
  812. }
  813. if (replay_owner && (replay_owner != (void *)(-1))) {
  814. nfs4_put_stateowner(replay_owner);
  815. replay_owner = NULL;
  816. }
  817. /* XXX Ugh, we need to get rid of this kind of special case: */
  818. if (op->opnum == OP_READ && op->u.read.rd_filp)
  819. fput(op->u.read.rd_filp);
  820. }
  821. out:
  822. nfsd4_release_compoundargs(args);
  823. if (current_fh)
  824. fh_put(current_fh);
  825. kfree(current_fh);
  826. if (save_fh)
  827. fh_put(save_fh);
  828. kfree(save_fh);
  829. return status;
  830. }
  831. #define nfs4svc_decode_voidargs NULL
  832. #define nfs4svc_release_void NULL
  833. #define nfsd4_voidres nfsd4_voidargs
  834. #define nfs4svc_release_compound NULL
  835. struct nfsd4_voidargs { int dummy; };
  836. #define PROC(name, argt, rest, relt, cache, respsize) \
  837. { (svc_procfunc) nfsd4_proc_##name, \
  838. (kxdrproc_t) nfs4svc_decode_##argt##args, \
  839. (kxdrproc_t) nfs4svc_encode_##rest##res, \
  840. (kxdrproc_t) nfs4svc_release_##relt, \
  841. sizeof(struct nfsd4_##argt##args), \
  842. sizeof(struct nfsd4_##rest##res), \
  843. 0, \
  844. cache, \
  845. respsize, \
  846. }
  847. /*
  848. * TODO: At the present time, the NFSv4 server does not do XID caching
  849. * of requests. Implementing XID caching would not be a serious problem,
  850. * although it would require a mild change in interfaces since one
  851. * doesn't know whether an NFSv4 request is idempotent until after the
  852. * XDR decode. However, XID caching totally confuses pynfs (Peter
  853. * Astrand's regression testsuite for NFSv4 servers), which reuses
  854. * XID's liberally, so I've left it unimplemented until pynfs generates
  855. * better XID's.
  856. */
  857. static struct svc_procedure nfsd_procedures4[2] = {
  858. PROC(null, void, void, void, RC_NOCACHE, 1),
  859. PROC(compound, compound, compound, compound, RC_NOCACHE, NFSD_BUFSIZE)
  860. };
  861. struct svc_version nfsd_version4 = {
  862. .vs_vers = 4,
  863. .vs_nproc = 2,
  864. .vs_proc = nfsd_procedures4,
  865. .vs_dispatch = nfsd_dispatch,
  866. .vs_xdrsize = NFS4_SVC_XDRSIZE,
  867. };
  868. /*
  869. * Local variables:
  870. * c-basic-offset: 8
  871. * End:
  872. */