nfs4proc.c 29 KB

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