nfs4proc.c 28 KB

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