nfs4proc.c 30 KB

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