nfs4proc.c 29 KB

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