nfs4proc.c 29 KB

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