nfs4proc.c 27 KB

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