nfs4proc.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  1. /*
  2. * fs/nfsd/nfs4proc.c
  3. *
  4. * Server-side procedures for NFSv4.
  5. *
  6. * Copyright (c) 2002 The Regents of the University of Michigan.
  7. * All rights reserved.
  8. *
  9. * Kendrick Smith <kmsmith@umich.edu>
  10. * Andy Adamson <andros@umich.edu>
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * 3. Neither the name of the University nor the names of its
  22. * contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  26. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  27. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  32. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include <linux/param.h>
  38. #include <linux/major.h>
  39. #include <linux/slab.h>
  40. #include <linux/file.h>
  41. #include <linux/sunrpc/svc.h>
  42. #include <linux/nfsd/nfsd.h>
  43. #include <linux/nfsd/cache.h>
  44. #include <linux/nfs4.h>
  45. #include <linux/nfsd/state.h>
  46. #include <linux/nfsd/xdr4.h>
  47. #include <linux/nfs4_acl.h>
  48. #include <linux/sunrpc/gss_api.h>
  49. #define NFSDDBG_FACILITY NFSDDBG_PROC
  50. static inline void
  51. fh_dup2(struct svc_fh *dst, struct svc_fh *src)
  52. {
  53. fh_put(dst);
  54. dget(src->fh_dentry);
  55. if (src->fh_export)
  56. cache_get(&src->fh_export->h);
  57. *dst = *src;
  58. }
  59. static __be32
  60. do_open_permission(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open, int accmode)
  61. {
  62. __be32 status;
  63. if (open->op_truncate &&
  64. !(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
  65. return nfserr_inval;
  66. if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
  67. accmode |= MAY_READ;
  68. if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
  69. accmode |= (MAY_WRITE | MAY_TRUNC);
  70. if (open->op_share_deny & NFS4_SHARE_DENY_WRITE)
  71. accmode |= MAY_WRITE;
  72. status = fh_verify(rqstp, current_fh, S_IFREG, accmode);
  73. return status;
  74. }
  75. static __be32
  76. do_open_lookup(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
  77. {
  78. struct svc_fh resfh;
  79. __be32 status;
  80. int created = 0;
  81. fh_init(&resfh, NFS4_FHSIZE);
  82. open->op_truncate = 0;
  83. if (open->op_create) {
  84. /*
  85. * Note: create modes (UNCHECKED,GUARDED...) are the same
  86. * in NFSv4 as in v3.
  87. */
  88. status = nfsd_create_v3(rqstp, current_fh, open->op_fname.data,
  89. open->op_fname.len, &open->op_iattr,
  90. &resfh, open->op_createmode,
  91. (u32 *)open->op_verf.data, &open->op_truncate, &created);
  92. } else {
  93. status = nfsd_lookup(rqstp, current_fh,
  94. open->op_fname.data, open->op_fname.len, &resfh);
  95. fh_unlock(current_fh);
  96. }
  97. if (status)
  98. goto out;
  99. set_change_info(&open->op_cinfo, current_fh);
  100. /* set reply cache */
  101. fh_dup2(current_fh, &resfh);
  102. open->op_stateowner->so_replay.rp_openfh_len = resfh.fh_handle.fh_size;
  103. memcpy(open->op_stateowner->so_replay.rp_openfh,
  104. &resfh.fh_handle.fh_base, resfh.fh_handle.fh_size);
  105. if (!created)
  106. status = do_open_permission(rqstp, current_fh, open, MAY_NOP);
  107. out:
  108. fh_put(&resfh);
  109. return status;
  110. }
  111. static __be32
  112. do_open_fhandle(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
  113. {
  114. __be32 status;
  115. /* Only reclaims from previously confirmed clients are valid */
  116. if ((status = nfs4_check_open_reclaim(&open->op_clientid)))
  117. return status;
  118. /* We don't know the target directory, and therefore can not
  119. * set the change info
  120. */
  121. memset(&open->op_cinfo, 0, sizeof(struct nfsd4_change_info));
  122. /* set replay cache */
  123. open->op_stateowner->so_replay.rp_openfh_len = current_fh->fh_handle.fh_size;
  124. memcpy(open->op_stateowner->so_replay.rp_openfh,
  125. &current_fh->fh_handle.fh_base,
  126. current_fh->fh_handle.fh_size);
  127. open->op_truncate = (open->op_iattr.ia_valid & ATTR_SIZE) &&
  128. (open->op_iattr.ia_size == 0);
  129. status = do_open_permission(rqstp, current_fh, open, MAY_OWNER_OVERRIDE);
  130. return status;
  131. }
  132. static __be32
  133. nfsd4_open(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  134. struct nfsd4_open *open)
  135. {
  136. __be32 status;
  137. dprintk("NFSD: nfsd4_open filename %.*s op_stateowner %p\n",
  138. (int)open->op_fname.len, open->op_fname.data,
  139. open->op_stateowner);
  140. /* This check required by spec. */
  141. if (open->op_create && open->op_claim_type != NFS4_OPEN_CLAIM_NULL)
  142. return nfserr_inval;
  143. nfs4_lock_state();
  144. /* check seqid for replay. set nfs4_owner */
  145. status = nfsd4_process_open1(open);
  146. if (status == nfserr_replay_me) {
  147. struct nfs4_replay *rp = &open->op_stateowner->so_replay;
  148. fh_put(&cstate->current_fh);
  149. cstate->current_fh.fh_handle.fh_size = rp->rp_openfh_len;
  150. memcpy(&cstate->current_fh.fh_handle.fh_base, rp->rp_openfh,
  151. rp->rp_openfh_len);
  152. status = fh_verify(rqstp, &cstate->current_fh, 0, MAY_NOP);
  153. if (status)
  154. dprintk("nfsd4_open: replay failed"
  155. " restoring previous filehandle\n");
  156. else
  157. status = nfserr_replay_me;
  158. }
  159. if (status)
  160. goto out;
  161. /* Openowner is now set, so sequence id will get bumped. Now we need
  162. * these checks before we do any creates: */
  163. status = nfserr_grace;
  164. if (nfs4_in_grace() && open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
  165. goto out;
  166. status = nfserr_no_grace;
  167. if (!nfs4_in_grace() && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
  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, &cstate->current_fh,
  183. open);
  184. if (status)
  185. goto out;
  186. break;
  187. case NFS4_OPEN_CLAIM_PREVIOUS:
  188. open->op_stateowner->so_confirmed = 1;
  189. /*
  190. * The CURRENT_FH is already set to the file being
  191. * opened. (1) set open->op_cinfo, (2) set
  192. * open->op_truncate if the file is to be truncated
  193. * after opening, (3) do permission checking.
  194. */
  195. status = do_open_fhandle(rqstp, &cstate->current_fh,
  196. open);
  197. if (status)
  198. goto out;
  199. break;
  200. case NFS4_OPEN_CLAIM_DELEGATE_PREV:
  201. open->op_stateowner->so_confirmed = 1;
  202. printk("NFSD: unsupported OPEN claim type %d\n",
  203. open->op_claim_type);
  204. status = nfserr_notsupp;
  205. goto out;
  206. default:
  207. printk("NFSD: Invalid OPEN claim type %d\n",
  208. open->op_claim_type);
  209. status = nfserr_inval;
  210. goto out;
  211. }
  212. /*
  213. * nfsd4_process_open2() does the actual opening of the file. If
  214. * successful, it (1) truncates the file if open->op_truncate was
  215. * set, (2) sets open->op_stateid, (3) sets open->op_delegation.
  216. */
  217. status = nfsd4_process_open2(rqstp, &cstate->current_fh, open);
  218. out:
  219. if (open->op_stateowner) {
  220. nfs4_get_stateowner(open->op_stateowner);
  221. cstate->replay_owner = open->op_stateowner;
  222. }
  223. nfs4_unlock_state();
  224. return status;
  225. }
  226. /*
  227. * filehandle-manipulating ops.
  228. */
  229. static __be32
  230. nfsd4_getfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  231. struct svc_fh **getfh)
  232. {
  233. if (!cstate->current_fh.fh_dentry)
  234. return nfserr_nofilehandle;
  235. *getfh = &cstate->current_fh;
  236. return nfs_ok;
  237. }
  238. static __be32
  239. nfsd4_putfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  240. struct nfsd4_putfh *putfh)
  241. {
  242. fh_put(&cstate->current_fh);
  243. cstate->current_fh.fh_handle.fh_size = putfh->pf_fhlen;
  244. memcpy(&cstate->current_fh.fh_handle.fh_base, putfh->pf_fhval,
  245. putfh->pf_fhlen);
  246. return fh_verify(rqstp, &cstate->current_fh, 0, MAY_NOP);
  247. }
  248. static __be32
  249. nfsd4_putrootfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  250. void *arg)
  251. {
  252. __be32 status;
  253. fh_put(&cstate->current_fh);
  254. status = exp_pseudoroot(rqstp, &cstate->current_fh);
  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. ret = exp_pseudoroot(rqstp, &tmp_fh);
  411. if (ret)
  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_secinfo(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  529. struct nfsd4_secinfo *secinfo)
  530. {
  531. struct svc_fh resfh;
  532. struct svc_export *exp;
  533. struct dentry *dentry;
  534. __be32 err;
  535. fh_init(&resfh, NFS4_FHSIZE);
  536. err = nfsd_lookup_dentry(rqstp, &cstate->current_fh,
  537. secinfo->si_name, secinfo->si_namelen,
  538. &exp, &dentry);
  539. if (err)
  540. return err;
  541. if (dentry->d_inode == NULL) {
  542. exp_put(exp);
  543. err = nfserr_noent;
  544. } else
  545. secinfo->si_exp = exp;
  546. dput(dentry);
  547. return err;
  548. }
  549. static __be32
  550. nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  551. struct nfsd4_setattr *setattr)
  552. {
  553. __be32 status = nfs_ok;
  554. if (setattr->sa_iattr.ia_valid & ATTR_SIZE) {
  555. nfs4_lock_state();
  556. status = nfs4_preprocess_stateid_op(&cstate->current_fh,
  557. &setattr->sa_stateid, CHECK_FH | WR_STATE, NULL);
  558. nfs4_unlock_state();
  559. if (status) {
  560. dprintk("NFSD: nfsd4_setattr: couldn't process stateid!\n");
  561. return status;
  562. }
  563. }
  564. status = nfs_ok;
  565. if (setattr->sa_acl != NULL)
  566. status = nfsd4_set_nfs4_acl(rqstp, &cstate->current_fh,
  567. setattr->sa_acl);
  568. if (status)
  569. return status;
  570. status = nfsd_setattr(rqstp, &cstate->current_fh, &setattr->sa_iattr,
  571. 0, (time_t)0);
  572. return status;
  573. }
  574. static __be32
  575. nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  576. struct nfsd4_write *write)
  577. {
  578. stateid_t *stateid = &write->wr_stateid;
  579. struct file *filp = NULL;
  580. u32 *p;
  581. __be32 status = nfs_ok;
  582. /* no need to check permission - this will be done in nfsd_write() */
  583. if (write->wr_offset >= OFFSET_MAX)
  584. return nfserr_inval;
  585. nfs4_lock_state();
  586. status = nfs4_preprocess_stateid_op(&cstate->current_fh, stateid,
  587. CHECK_FH | WR_STATE, &filp);
  588. if (filp)
  589. get_file(filp);
  590. nfs4_unlock_state();
  591. if (status) {
  592. dprintk("NFSD: nfsd4_write: couldn't process stateid!\n");
  593. return status;
  594. }
  595. write->wr_bytes_written = write->wr_buflen;
  596. write->wr_how_written = write->wr_stable_how;
  597. p = (u32 *)write->wr_verifier.data;
  598. *p++ = nfssvc_boot.tv_sec;
  599. *p++ = nfssvc_boot.tv_usec;
  600. status = nfsd_write(rqstp, &cstate->current_fh, filp,
  601. write->wr_offset, rqstp->rq_vec, write->wr_vlen,
  602. write->wr_buflen, &write->wr_how_written);
  603. if (filp)
  604. fput(filp);
  605. if (status == nfserr_symlink)
  606. status = nfserr_inval;
  607. return status;
  608. }
  609. /* This routine never returns NFS_OK! If there are no other errors, it
  610. * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the
  611. * attributes matched. VERIFY is implemented by mapping NFSERR_SAME
  612. * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK.
  613. */
  614. static __be32
  615. _nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  616. struct nfsd4_verify *verify)
  617. {
  618. __be32 *buf, *p;
  619. int count;
  620. __be32 status;
  621. status = fh_verify(rqstp, &cstate->current_fh, 0, MAY_NOP);
  622. if (status)
  623. return status;
  624. if ((verify->ve_bmval[0] & ~NFSD_SUPPORTED_ATTRS_WORD0)
  625. || (verify->ve_bmval[1] & ~NFSD_SUPPORTED_ATTRS_WORD1))
  626. return nfserr_attrnotsupp;
  627. if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR)
  628. || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1))
  629. return nfserr_inval;
  630. if (verify->ve_attrlen & 3)
  631. return nfserr_inval;
  632. /* count in words:
  633. * bitmap_len(1) + bitmap(2) + attr_len(1) = 4
  634. */
  635. count = 4 + (verify->ve_attrlen >> 2);
  636. buf = kmalloc(count << 2, GFP_KERNEL);
  637. if (!buf)
  638. return nfserr_resource;
  639. status = nfsd4_encode_fattr(&cstate->current_fh,
  640. cstate->current_fh.fh_export,
  641. cstate->current_fh.fh_dentry, buf,
  642. &count, verify->ve_bmval,
  643. rqstp);
  644. /* this means that nfsd4_encode_fattr() ran out of space */
  645. if (status == nfserr_resource && count == 0)
  646. status = nfserr_not_same;
  647. if (status)
  648. goto out_kfree;
  649. p = buf + 3;
  650. status = nfserr_not_same;
  651. if (ntohl(*p++) != verify->ve_attrlen)
  652. goto out_kfree;
  653. if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen))
  654. status = nfserr_same;
  655. out_kfree:
  656. kfree(buf);
  657. return status;
  658. }
  659. static __be32
  660. nfsd4_nverify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  661. struct nfsd4_verify *verify)
  662. {
  663. __be32 status;
  664. status = _nfsd4_verify(rqstp, cstate, verify);
  665. return status == nfserr_not_same ? nfs_ok : status;
  666. }
  667. static __be32
  668. nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  669. struct nfsd4_verify *verify)
  670. {
  671. __be32 status;
  672. status = _nfsd4_verify(rqstp, cstate, verify);
  673. return status == nfserr_same ? nfs_ok : status;
  674. }
  675. /*
  676. * NULL call.
  677. */
  678. static __be32
  679. nfsd4_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
  680. {
  681. return nfs_ok;
  682. }
  683. static inline void nfsd4_increment_op_stats(u32 opnum)
  684. {
  685. if (opnum >= FIRST_NFS4_OP && opnum <= LAST_NFS4_OP)
  686. nfsdstats.nfs4_opcount[opnum]++;
  687. }
  688. static void cstate_free(struct nfsd4_compound_state *cstate)
  689. {
  690. if (cstate == NULL)
  691. return;
  692. fh_put(&cstate->current_fh);
  693. fh_put(&cstate->save_fh);
  694. BUG_ON(cstate->replay_owner);
  695. kfree(cstate);
  696. }
  697. static struct nfsd4_compound_state *cstate_alloc(void)
  698. {
  699. struct nfsd4_compound_state *cstate;
  700. cstate = kmalloc(sizeof(struct nfsd4_compound_state), GFP_KERNEL);
  701. if (cstate == NULL)
  702. return NULL;
  703. fh_init(&cstate->current_fh, NFS4_FHSIZE);
  704. fh_init(&cstate->save_fh, NFS4_FHSIZE);
  705. cstate->replay_owner = NULL;
  706. return cstate;
  707. }
  708. typedef __be32(*nfsd4op_func)(struct svc_rqst *, struct nfsd4_compound_state *,
  709. void *);
  710. struct nfsd4_operation {
  711. nfsd4op_func op_func;
  712. u32 op_flags;
  713. /* Most ops require a valid current filehandle; a few don't: */
  714. #define ALLOWED_WITHOUT_FH 1
  715. /* GETATTR and ops not listed as returning NFS4ERR_MOVED: */
  716. #define ALLOWED_ON_ABSENT_FS 2
  717. };
  718. static struct nfsd4_operation nfsd4_ops[];
  719. /*
  720. * COMPOUND call.
  721. */
  722. static __be32
  723. nfsd4_proc_compound(struct svc_rqst *rqstp,
  724. struct nfsd4_compoundargs *args,
  725. struct nfsd4_compoundres *resp)
  726. {
  727. struct nfsd4_op *op;
  728. struct nfsd4_operation *opdesc;
  729. struct nfsd4_compound_state *cstate = NULL;
  730. int slack_bytes;
  731. __be32 status;
  732. status = nfserr_resource;
  733. cstate = cstate_alloc();
  734. if (cstate == NULL)
  735. goto out;
  736. resp->xbuf = &rqstp->rq_res;
  737. resp->p = rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len;
  738. resp->tagp = resp->p;
  739. /* reserve space for: taglen, tag, and opcnt */
  740. resp->p += 2 + XDR_QUADLEN(args->taglen);
  741. resp->end = rqstp->rq_res.head[0].iov_base + PAGE_SIZE;
  742. resp->taglen = args->taglen;
  743. resp->tag = args->tag;
  744. resp->opcnt = 0;
  745. resp->rqstp = rqstp;
  746. /*
  747. * According to RFC3010, this takes precedence over all other errors.
  748. */
  749. status = nfserr_minor_vers_mismatch;
  750. if (args->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
  751. goto out;
  752. status = nfs_ok;
  753. while (!status && resp->opcnt < args->opcnt) {
  754. op = &args->ops[resp->opcnt++];
  755. dprintk("nfsv4 compound op #%d: %d\n", resp->opcnt, op->opnum);
  756. /*
  757. * The XDR decode routines may have pre-set op->status;
  758. * for example, if there is a miscellaneous XDR error
  759. * it will be set to nfserr_bad_xdr.
  760. */
  761. if (op->status)
  762. goto encode_op;
  763. /* We must be able to encode a successful response to
  764. * this operation, with enough room left over to encode a
  765. * failed response to the next operation. If we don't
  766. * have enough room, fail with ERR_RESOURCE.
  767. */
  768. slack_bytes = (char *)resp->end - (char *)resp->p;
  769. if (slack_bytes < COMPOUND_SLACK_SPACE
  770. + COMPOUND_ERR_SLACK_SPACE) {
  771. BUG_ON(slack_bytes < COMPOUND_ERR_SLACK_SPACE);
  772. op->status = nfserr_resource;
  773. goto encode_op;
  774. }
  775. opdesc = &nfsd4_ops[op->opnum];
  776. if (!cstate->current_fh.fh_dentry) {
  777. if (!(opdesc->op_flags & ALLOWED_WITHOUT_FH)) {
  778. op->status = nfserr_nofilehandle;
  779. goto encode_op;
  780. }
  781. } else if (cstate->current_fh.fh_export->ex_fslocs.migrated &&
  782. !(opdesc->op_flags & ALLOWED_ON_ABSENT_FS)) {
  783. op->status = nfserr_moved;
  784. goto encode_op;
  785. }
  786. if (opdesc->op_func)
  787. op->status = opdesc->op_func(rqstp, cstate, &op->u);
  788. else
  789. BUG_ON(op->status == nfs_ok);
  790. encode_op:
  791. if (op->status == nfserr_replay_me) {
  792. op->replay = &cstate->replay_owner->so_replay;
  793. nfsd4_encode_replay(resp, op);
  794. status = op->status = op->replay->rp_status;
  795. } else {
  796. nfsd4_encode_operation(resp, op);
  797. status = op->status;
  798. }
  799. if (cstate->replay_owner) {
  800. nfs4_put_stateowner(cstate->replay_owner);
  801. cstate->replay_owner = NULL;
  802. }
  803. /* XXX Ugh, we need to get rid of this kind of special case: */
  804. if (op->opnum == OP_READ && op->u.read.rd_filp)
  805. fput(op->u.read.rd_filp);
  806. nfsd4_increment_op_stats(op->opnum);
  807. }
  808. out:
  809. nfsd4_release_compoundargs(args);
  810. cstate_free(cstate);
  811. return status;
  812. }
  813. static struct nfsd4_operation nfsd4_ops[OP_RELEASE_LOCKOWNER+1] = {
  814. [OP_ACCESS] = {
  815. .op_func = (nfsd4op_func)nfsd4_access,
  816. },
  817. [OP_CLOSE] = {
  818. .op_func = (nfsd4op_func)nfsd4_close,
  819. },
  820. [OP_COMMIT] = {
  821. .op_func = (nfsd4op_func)nfsd4_commit,
  822. },
  823. [OP_CREATE] = {
  824. .op_func = (nfsd4op_func)nfsd4_create,
  825. },
  826. [OP_DELEGRETURN] = {
  827. .op_func = (nfsd4op_func)nfsd4_delegreturn,
  828. },
  829. [OP_GETATTR] = {
  830. .op_func = (nfsd4op_func)nfsd4_getattr,
  831. .op_flags = ALLOWED_ON_ABSENT_FS,
  832. },
  833. [OP_GETFH] = {
  834. .op_func = (nfsd4op_func)nfsd4_getfh,
  835. },
  836. [OP_LINK] = {
  837. .op_func = (nfsd4op_func)nfsd4_link,
  838. },
  839. [OP_LOCK] = {
  840. .op_func = (nfsd4op_func)nfsd4_lock,
  841. },
  842. [OP_LOCKT] = {
  843. .op_func = (nfsd4op_func)nfsd4_lockt,
  844. },
  845. [OP_LOCKU] = {
  846. .op_func = (nfsd4op_func)nfsd4_locku,
  847. },
  848. [OP_LOOKUP] = {
  849. .op_func = (nfsd4op_func)nfsd4_lookup,
  850. },
  851. [OP_LOOKUPP] = {
  852. .op_func = (nfsd4op_func)nfsd4_lookupp,
  853. },
  854. [OP_NVERIFY] = {
  855. .op_func = (nfsd4op_func)nfsd4_nverify,
  856. },
  857. [OP_OPEN] = {
  858. .op_func = (nfsd4op_func)nfsd4_open,
  859. },
  860. [OP_OPEN_CONFIRM] = {
  861. .op_func = (nfsd4op_func)nfsd4_open_confirm,
  862. },
  863. [OP_OPEN_DOWNGRADE] = {
  864. .op_func = (nfsd4op_func)nfsd4_open_downgrade,
  865. },
  866. [OP_PUTFH] = {
  867. .op_func = (nfsd4op_func)nfsd4_putfh,
  868. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
  869. },
  870. [OP_PUTPUBFH] = {
  871. /* unsupported; just for future reference: */
  872. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
  873. },
  874. [OP_PUTROOTFH] = {
  875. .op_func = (nfsd4op_func)nfsd4_putrootfh,
  876. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
  877. },
  878. [OP_READ] = {
  879. .op_func = (nfsd4op_func)nfsd4_read,
  880. },
  881. [OP_READDIR] = {
  882. .op_func = (nfsd4op_func)nfsd4_readdir,
  883. },
  884. [OP_READLINK] = {
  885. .op_func = (nfsd4op_func)nfsd4_readlink,
  886. },
  887. [OP_REMOVE] = {
  888. .op_func = (nfsd4op_func)nfsd4_remove,
  889. },
  890. [OP_RENAME] = {
  891. .op_func = (nfsd4op_func)nfsd4_rename,
  892. },
  893. [OP_RENEW] = {
  894. .op_func = (nfsd4op_func)nfsd4_renew,
  895. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
  896. },
  897. [OP_RESTOREFH] = {
  898. .op_func = (nfsd4op_func)nfsd4_restorefh,
  899. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
  900. },
  901. [OP_SAVEFH] = {
  902. .op_func = (nfsd4op_func)nfsd4_savefh,
  903. },
  904. [OP_SECINFO] = {
  905. .op_func = (nfsd4op_func)nfsd4_secinfo,
  906. },
  907. [OP_SETATTR] = {
  908. .op_func = (nfsd4op_func)nfsd4_setattr,
  909. },
  910. [OP_SETCLIENTID] = {
  911. .op_func = (nfsd4op_func)nfsd4_setclientid,
  912. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
  913. },
  914. [OP_SETCLIENTID_CONFIRM] = {
  915. .op_func = (nfsd4op_func)nfsd4_setclientid_confirm,
  916. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
  917. },
  918. [OP_VERIFY] = {
  919. .op_func = (nfsd4op_func)nfsd4_verify,
  920. },
  921. [OP_WRITE] = {
  922. .op_func = (nfsd4op_func)nfsd4_write,
  923. },
  924. [OP_RELEASE_LOCKOWNER] = {
  925. .op_func = (nfsd4op_func)nfsd4_release_lockowner,
  926. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
  927. },
  928. };
  929. #define nfs4svc_decode_voidargs NULL
  930. #define nfs4svc_release_void NULL
  931. #define nfsd4_voidres nfsd4_voidargs
  932. #define nfs4svc_release_compound NULL
  933. struct nfsd4_voidargs { int dummy; };
  934. #define PROC(name, argt, rest, relt, cache, respsize) \
  935. { (svc_procfunc) nfsd4_proc_##name, \
  936. (kxdrproc_t) nfs4svc_decode_##argt##args, \
  937. (kxdrproc_t) nfs4svc_encode_##rest##res, \
  938. (kxdrproc_t) nfs4svc_release_##relt, \
  939. sizeof(struct nfsd4_##argt##args), \
  940. sizeof(struct nfsd4_##rest##res), \
  941. 0, \
  942. cache, \
  943. respsize, \
  944. }
  945. /*
  946. * TODO: At the present time, the NFSv4 server does not do XID caching
  947. * of requests. Implementing XID caching would not be a serious problem,
  948. * although it would require a mild change in interfaces since one
  949. * doesn't know whether an NFSv4 request is idempotent until after the
  950. * XDR decode. However, XID caching totally confuses pynfs (Peter
  951. * Astrand's regression testsuite for NFSv4 servers), which reuses
  952. * XID's liberally, so I've left it unimplemented until pynfs generates
  953. * better XID's.
  954. */
  955. static struct svc_procedure nfsd_procedures4[2] = {
  956. PROC(null, void, void, void, RC_NOCACHE, 1),
  957. PROC(compound, compound, compound, compound, RC_NOCACHE, NFSD_BUFSIZE/4)
  958. };
  959. struct svc_version nfsd_version4 = {
  960. .vs_vers = 4,
  961. .vs_nproc = 2,
  962. .vs_proc = nfsd_procedures4,
  963. .vs_dispatch = nfsd_dispatch,
  964. .vs_xdrsize = NFS4_SVC_XDRSIZE,
  965. };
  966. /*
  967. * Local variables:
  968. * c-basic-offset: 8
  969. * End:
  970. */