nfs4proc.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473
  1. /*
  2. * Server-side procedures for NFSv4.
  3. *
  4. * Copyright (c) 2002 The Regents of the University of Michigan.
  5. * All rights reserved.
  6. *
  7. * Kendrick Smith <kmsmith@umich.edu>
  8. * Andy Adamson <andros@umich.edu>
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. Neither the name of the University nor the names of its
  20. * contributors may be used to endorse or promote products derived
  21. * from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  24. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  30. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  31. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <linux/file.h>
  36. #include <linux/slab.h>
  37. #include "cache.h"
  38. #include "xdr4.h"
  39. #include "vfs.h"
  40. #define NFSDDBG_FACILITY NFSDDBG_PROC
  41. static u32 nfsd_attrmask[] = {
  42. NFSD_WRITEABLE_ATTRS_WORD0,
  43. NFSD_WRITEABLE_ATTRS_WORD1,
  44. NFSD_WRITEABLE_ATTRS_WORD2
  45. };
  46. static u32 nfsd41_ex_attrmask[] = {
  47. NFSD_SUPPATTR_EXCLCREAT_WORD0,
  48. NFSD_SUPPATTR_EXCLCREAT_WORD1,
  49. NFSD_SUPPATTR_EXCLCREAT_WORD2
  50. };
  51. static __be32
  52. check_attr_support(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  53. u32 *bmval, u32 *writable)
  54. {
  55. struct dentry *dentry = cstate->current_fh.fh_dentry;
  56. /*
  57. * Check about attributes are supported by the NFSv4 server or not.
  58. * According to spec, unsupported attributes return ERR_ATTRNOTSUPP.
  59. */
  60. if ((bmval[0] & ~nfsd_suppattrs0(cstate->minorversion)) ||
  61. (bmval[1] & ~nfsd_suppattrs1(cstate->minorversion)) ||
  62. (bmval[2] & ~nfsd_suppattrs2(cstate->minorversion)))
  63. return nfserr_attrnotsupp;
  64. /*
  65. * Check FATTR4_WORD0_ACL can be supported
  66. * in current environment or not.
  67. */
  68. if (bmval[0] & FATTR4_WORD0_ACL) {
  69. if (!IS_POSIXACL(dentry->d_inode))
  70. return nfserr_attrnotsupp;
  71. }
  72. /*
  73. * According to spec, read-only attributes return ERR_INVAL.
  74. */
  75. if (writable) {
  76. if ((bmval[0] & ~writable[0]) || (bmval[1] & ~writable[1]) ||
  77. (bmval[2] & ~writable[2]))
  78. return nfserr_inval;
  79. }
  80. return nfs_ok;
  81. }
  82. static __be32
  83. nfsd4_check_open_attributes(struct svc_rqst *rqstp,
  84. struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
  85. {
  86. __be32 status = nfs_ok;
  87. if (open->op_create == NFS4_OPEN_CREATE) {
  88. if (open->op_createmode == NFS4_CREATE_UNCHECKED
  89. || open->op_createmode == NFS4_CREATE_GUARDED)
  90. status = check_attr_support(rqstp, cstate,
  91. open->op_bmval, nfsd_attrmask);
  92. else if (open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1)
  93. status = check_attr_support(rqstp, cstate,
  94. open->op_bmval, nfsd41_ex_attrmask);
  95. }
  96. return status;
  97. }
  98. static int
  99. is_create_with_attrs(struct nfsd4_open *open)
  100. {
  101. return open->op_create == NFS4_OPEN_CREATE
  102. && (open->op_createmode == NFS4_CREATE_UNCHECKED
  103. || open->op_createmode == NFS4_CREATE_GUARDED
  104. || open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1);
  105. }
  106. /*
  107. * if error occurs when setting the acl, just clear the acl bit
  108. * in the returned attr bitmap.
  109. */
  110. static void
  111. do_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
  112. struct nfs4_acl *acl, u32 *bmval)
  113. {
  114. __be32 status;
  115. status = nfsd4_set_nfs4_acl(rqstp, fhp, acl);
  116. if (status)
  117. /*
  118. * We should probably fail the whole open at this point,
  119. * but we've already created the file, so it's too late;
  120. * So this seems the least of evils:
  121. */
  122. bmval[0] &= ~FATTR4_WORD0_ACL;
  123. }
  124. static inline void
  125. fh_dup2(struct svc_fh *dst, struct svc_fh *src)
  126. {
  127. fh_put(dst);
  128. dget(src->fh_dentry);
  129. if (src->fh_export)
  130. cache_get(&src->fh_export->h);
  131. *dst = *src;
  132. }
  133. static __be32
  134. do_open_permission(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open, int accmode)
  135. {
  136. __be32 status;
  137. if (open->op_truncate &&
  138. !(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
  139. return nfserr_inval;
  140. accmode |= NFSD_MAY_READ_IF_EXEC;
  141. if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
  142. accmode |= NFSD_MAY_READ;
  143. if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
  144. accmode |= (NFSD_MAY_WRITE | NFSD_MAY_TRUNC);
  145. if (open->op_share_deny & NFS4_SHARE_DENY_READ)
  146. accmode |= NFSD_MAY_WRITE;
  147. status = fh_verify(rqstp, current_fh, S_IFREG, accmode);
  148. return status;
  149. }
  150. static __be32
  151. do_open_lookup(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
  152. {
  153. struct svc_fh resfh;
  154. __be32 status;
  155. int created = 0;
  156. fh_init(&resfh, NFS4_FHSIZE);
  157. open->op_truncate = 0;
  158. if (open->op_create) {
  159. /* FIXME: check session persistence and pnfs flags.
  160. * The nfsv4.1 spec requires the following semantics:
  161. *
  162. * Persistent | pNFS | Server REQUIRED | Client Allowed
  163. * Reply Cache | server | |
  164. * -------------+--------+-----------------+--------------------
  165. * no | no | EXCLUSIVE4_1 | EXCLUSIVE4_1
  166. * | | | (SHOULD)
  167. * | | and EXCLUSIVE4 | or EXCLUSIVE4
  168. * | | | (SHOULD NOT)
  169. * no | yes | EXCLUSIVE4_1 | EXCLUSIVE4_1
  170. * yes | no | GUARDED4 | GUARDED4
  171. * yes | yes | GUARDED4 | GUARDED4
  172. */
  173. /*
  174. * Note: create modes (UNCHECKED,GUARDED...) are the same
  175. * in NFSv4 as in v3 except EXCLUSIVE4_1.
  176. */
  177. status = do_nfsd_create(rqstp, current_fh, open->op_fname.data,
  178. open->op_fname.len, &open->op_iattr,
  179. &resfh, open->op_createmode,
  180. (u32 *)open->op_verf.data,
  181. &open->op_truncate, &created);
  182. /*
  183. * Following rfc 3530 14.2.16, use the returned bitmask
  184. * to indicate which attributes we used to store the
  185. * verifier:
  186. */
  187. if (open->op_createmode == NFS4_CREATE_EXCLUSIVE && status == 0)
  188. open->op_bmval[1] = (FATTR4_WORD1_TIME_ACCESS |
  189. FATTR4_WORD1_TIME_MODIFY);
  190. } else {
  191. status = nfsd_lookup(rqstp, current_fh,
  192. open->op_fname.data, open->op_fname.len, &resfh);
  193. fh_unlock(current_fh);
  194. }
  195. if (status)
  196. goto out;
  197. if (is_create_with_attrs(open) && open->op_acl != NULL)
  198. do_set_nfs4_acl(rqstp, &resfh, open->op_acl, open->op_bmval);
  199. set_change_info(&open->op_cinfo, current_fh);
  200. fh_dup2(current_fh, &resfh);
  201. /* set reply cache */
  202. fh_copy_shallow(&open->op_stateowner->so_replay.rp_openfh,
  203. &resfh.fh_handle);
  204. if (!created)
  205. status = do_open_permission(rqstp, current_fh, open,
  206. NFSD_MAY_NOP);
  207. out:
  208. fh_put(&resfh);
  209. return status;
  210. }
  211. static __be32
  212. do_open_fhandle(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
  213. {
  214. __be32 status;
  215. /* Only reclaims from previously confirmed clients are valid */
  216. if ((status = nfs4_check_open_reclaim(&open->op_clientid)))
  217. return status;
  218. /* We don't know the target directory, and therefore can not
  219. * set the change info
  220. */
  221. memset(&open->op_cinfo, 0, sizeof(struct nfsd4_change_info));
  222. /* set replay cache */
  223. fh_copy_shallow(&open->op_stateowner->so_replay.rp_openfh,
  224. &current_fh->fh_handle);
  225. open->op_truncate = (open->op_iattr.ia_valid & ATTR_SIZE) &&
  226. (open->op_iattr.ia_size == 0);
  227. status = do_open_permission(rqstp, current_fh, open,
  228. NFSD_MAY_OWNER_OVERRIDE);
  229. return status;
  230. }
  231. static void
  232. copy_clientid(clientid_t *clid, struct nfsd4_session *session)
  233. {
  234. struct nfsd4_sessionid *sid =
  235. (struct nfsd4_sessionid *)session->se_sessionid.data;
  236. clid->cl_boot = sid->clientid.cl_boot;
  237. clid->cl_id = sid->clientid.cl_id;
  238. }
  239. static __be32
  240. nfsd4_open(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  241. struct nfsd4_open *open)
  242. {
  243. __be32 status;
  244. struct nfsd4_compoundres *resp;
  245. dprintk("NFSD: nfsd4_open filename %.*s op_stateowner %p\n",
  246. (int)open->op_fname.len, open->op_fname.data,
  247. open->op_stateowner);
  248. /* This check required by spec. */
  249. if (open->op_create && open->op_claim_type != NFS4_OPEN_CLAIM_NULL)
  250. return nfserr_inval;
  251. if (nfsd4_has_session(cstate))
  252. copy_clientid(&open->op_clientid, cstate->session);
  253. nfs4_lock_state();
  254. /* check seqid for replay. set nfs4_owner */
  255. resp = rqstp->rq_resp;
  256. status = nfsd4_process_open1(&resp->cstate, open);
  257. if (status == nfserr_replay_me) {
  258. struct nfs4_replay *rp = &open->op_stateowner->so_replay;
  259. fh_put(&cstate->current_fh);
  260. fh_copy_shallow(&cstate->current_fh.fh_handle,
  261. &rp->rp_openfh);
  262. status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
  263. if (status)
  264. dprintk("nfsd4_open: replay failed"
  265. " restoring previous filehandle\n");
  266. else
  267. status = nfserr_replay_me;
  268. }
  269. if (status)
  270. goto out;
  271. status = nfsd4_check_open_attributes(rqstp, cstate, open);
  272. if (status)
  273. goto out;
  274. /* Openowner is now set, so sequence id will get bumped. Now we need
  275. * these checks before we do any creates: */
  276. status = nfserr_grace;
  277. if (locks_in_grace() && open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
  278. goto out;
  279. status = nfserr_no_grace;
  280. if (!locks_in_grace() && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
  281. goto out;
  282. switch (open->op_claim_type) {
  283. case NFS4_OPEN_CLAIM_DELEGATE_CUR:
  284. case NFS4_OPEN_CLAIM_NULL:
  285. /*
  286. * (1) set CURRENT_FH to the file being opened,
  287. * creating it if necessary, (2) set open->op_cinfo,
  288. * (3) set open->op_truncate if the file is to be
  289. * truncated after opening, (4) do permission checking.
  290. */
  291. status = do_open_lookup(rqstp, &cstate->current_fh,
  292. open);
  293. if (status)
  294. goto out;
  295. break;
  296. case NFS4_OPEN_CLAIM_PREVIOUS:
  297. open->op_stateowner->so_confirmed = 1;
  298. /*
  299. * The CURRENT_FH is already set to the file being
  300. * opened. (1) set open->op_cinfo, (2) set
  301. * open->op_truncate if the file is to be truncated
  302. * after opening, (3) do permission checking.
  303. */
  304. status = do_open_fhandle(rqstp, &cstate->current_fh,
  305. open);
  306. if (status)
  307. goto out;
  308. break;
  309. case NFS4_OPEN_CLAIM_DELEGATE_PREV:
  310. open->op_stateowner->so_confirmed = 1;
  311. dprintk("NFSD: unsupported OPEN claim type %d\n",
  312. open->op_claim_type);
  313. status = nfserr_notsupp;
  314. goto out;
  315. default:
  316. dprintk("NFSD: Invalid OPEN claim type %d\n",
  317. open->op_claim_type);
  318. status = nfserr_inval;
  319. goto out;
  320. }
  321. /*
  322. * nfsd4_process_open2() does the actual opening of the file. If
  323. * successful, it (1) truncates the file if open->op_truncate was
  324. * set, (2) sets open->op_stateid, (3) sets open->op_delegation.
  325. */
  326. status = nfsd4_process_open2(rqstp, &cstate->current_fh, open);
  327. out:
  328. if (open->op_stateowner) {
  329. nfs4_get_stateowner(open->op_stateowner);
  330. cstate->replay_owner = open->op_stateowner;
  331. }
  332. nfs4_unlock_state();
  333. return status;
  334. }
  335. /*
  336. * filehandle-manipulating ops.
  337. */
  338. static __be32
  339. nfsd4_getfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  340. struct svc_fh **getfh)
  341. {
  342. if (!cstate->current_fh.fh_dentry)
  343. return nfserr_nofilehandle;
  344. *getfh = &cstate->current_fh;
  345. return nfs_ok;
  346. }
  347. static __be32
  348. nfsd4_putfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  349. struct nfsd4_putfh *putfh)
  350. {
  351. fh_put(&cstate->current_fh);
  352. cstate->current_fh.fh_handle.fh_size = putfh->pf_fhlen;
  353. memcpy(&cstate->current_fh.fh_handle.fh_base, putfh->pf_fhval,
  354. putfh->pf_fhlen);
  355. return fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_BYPASS_GSS);
  356. }
  357. static __be32
  358. nfsd4_putrootfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  359. void *arg)
  360. {
  361. __be32 status;
  362. fh_put(&cstate->current_fh);
  363. status = exp_pseudoroot(rqstp, &cstate->current_fh);
  364. return status;
  365. }
  366. static __be32
  367. nfsd4_restorefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  368. void *arg)
  369. {
  370. if (!cstate->save_fh.fh_dentry)
  371. return nfserr_restorefh;
  372. fh_dup2(&cstate->current_fh, &cstate->save_fh);
  373. return nfs_ok;
  374. }
  375. static __be32
  376. nfsd4_savefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  377. void *arg)
  378. {
  379. if (!cstate->current_fh.fh_dentry)
  380. return nfserr_nofilehandle;
  381. fh_dup2(&cstate->save_fh, &cstate->current_fh);
  382. return nfs_ok;
  383. }
  384. /*
  385. * misc nfsv4 ops
  386. */
  387. static __be32
  388. nfsd4_access(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  389. struct nfsd4_access *access)
  390. {
  391. if (access->ac_req_access & ~NFS3_ACCESS_FULL)
  392. return nfserr_inval;
  393. access->ac_resp_access = access->ac_req_access;
  394. return nfsd_access(rqstp, &cstate->current_fh, &access->ac_resp_access,
  395. &access->ac_supported);
  396. }
  397. static __be32
  398. nfsd4_commit(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  399. struct nfsd4_commit *commit)
  400. {
  401. __be32 status;
  402. u32 *p = (u32 *)commit->co_verf.data;
  403. *p++ = nfssvc_boot.tv_sec;
  404. *p++ = nfssvc_boot.tv_usec;
  405. status = nfsd_commit(rqstp, &cstate->current_fh, commit->co_offset,
  406. commit->co_count);
  407. if (status == nfserr_symlink)
  408. status = nfserr_inval;
  409. return status;
  410. }
  411. static __be32
  412. nfsd4_create(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  413. struct nfsd4_create *create)
  414. {
  415. struct svc_fh resfh;
  416. __be32 status;
  417. dev_t rdev;
  418. fh_init(&resfh, NFS4_FHSIZE);
  419. status = fh_verify(rqstp, &cstate->current_fh, S_IFDIR,
  420. NFSD_MAY_CREATE);
  421. if (status == nfserr_symlink)
  422. status = nfserr_notdir;
  423. if (status)
  424. return status;
  425. status = check_attr_support(rqstp, cstate, create->cr_bmval,
  426. nfsd_attrmask);
  427. if (status)
  428. return status;
  429. switch (create->cr_type) {
  430. case NF4LNK:
  431. /* ugh! we have to null-terminate the linktext, or
  432. * vfs_symlink() will choke. it is always safe to
  433. * null-terminate by brute force, since at worst we
  434. * will overwrite the first byte of the create namelen
  435. * in the XDR buffer, which has already been extracted
  436. * during XDR decode.
  437. */
  438. create->cr_linkname[create->cr_linklen] = 0;
  439. status = nfsd_symlink(rqstp, &cstate->current_fh,
  440. create->cr_name, create->cr_namelen,
  441. create->cr_linkname, create->cr_linklen,
  442. &resfh, &create->cr_iattr);
  443. break;
  444. case NF4BLK:
  445. rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
  446. if (MAJOR(rdev) != create->cr_specdata1 ||
  447. MINOR(rdev) != create->cr_specdata2)
  448. return nfserr_inval;
  449. status = nfsd_create(rqstp, &cstate->current_fh,
  450. create->cr_name, create->cr_namelen,
  451. &create->cr_iattr, S_IFBLK, rdev, &resfh);
  452. break;
  453. case NF4CHR:
  454. rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
  455. if (MAJOR(rdev) != create->cr_specdata1 ||
  456. MINOR(rdev) != create->cr_specdata2)
  457. return nfserr_inval;
  458. status = nfsd_create(rqstp, &cstate->current_fh,
  459. create->cr_name, create->cr_namelen,
  460. &create->cr_iattr,S_IFCHR, rdev, &resfh);
  461. break;
  462. case NF4SOCK:
  463. status = nfsd_create(rqstp, &cstate->current_fh,
  464. create->cr_name, create->cr_namelen,
  465. &create->cr_iattr, S_IFSOCK, 0, &resfh);
  466. break;
  467. case NF4FIFO:
  468. status = nfsd_create(rqstp, &cstate->current_fh,
  469. create->cr_name, create->cr_namelen,
  470. &create->cr_iattr, S_IFIFO, 0, &resfh);
  471. break;
  472. case NF4DIR:
  473. create->cr_iattr.ia_valid &= ~ATTR_SIZE;
  474. status = nfsd_create(rqstp, &cstate->current_fh,
  475. create->cr_name, create->cr_namelen,
  476. &create->cr_iattr, S_IFDIR, 0, &resfh);
  477. break;
  478. default:
  479. status = nfserr_badtype;
  480. }
  481. if (status)
  482. goto out;
  483. if (create->cr_acl != NULL)
  484. do_set_nfs4_acl(rqstp, &resfh, create->cr_acl,
  485. create->cr_bmval);
  486. fh_unlock(&cstate->current_fh);
  487. set_change_info(&create->cr_cinfo, &cstate->current_fh);
  488. fh_dup2(&cstate->current_fh, &resfh);
  489. out:
  490. fh_put(&resfh);
  491. return status;
  492. }
  493. static __be32
  494. nfsd4_getattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  495. struct nfsd4_getattr *getattr)
  496. {
  497. __be32 status;
  498. status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
  499. if (status)
  500. return status;
  501. if (getattr->ga_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
  502. return nfserr_inval;
  503. getattr->ga_bmval[0] &= nfsd_suppattrs0(cstate->minorversion);
  504. getattr->ga_bmval[1] &= nfsd_suppattrs1(cstate->minorversion);
  505. getattr->ga_bmval[2] &= nfsd_suppattrs2(cstate->minorversion);
  506. getattr->ga_fhp = &cstate->current_fh;
  507. return nfs_ok;
  508. }
  509. static __be32
  510. nfsd4_link(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  511. struct nfsd4_link *link)
  512. {
  513. __be32 status = nfserr_nofilehandle;
  514. if (!cstate->save_fh.fh_dentry)
  515. return status;
  516. status = nfsd_link(rqstp, &cstate->current_fh,
  517. link->li_name, link->li_namelen, &cstate->save_fh);
  518. if (!status)
  519. set_change_info(&link->li_cinfo, &cstate->current_fh);
  520. return status;
  521. }
  522. static __be32 nfsd4_do_lookupp(struct svc_rqst *rqstp, struct svc_fh *fh)
  523. {
  524. struct svc_fh tmp_fh;
  525. __be32 ret;
  526. fh_init(&tmp_fh, NFS4_FHSIZE);
  527. ret = exp_pseudoroot(rqstp, &tmp_fh);
  528. if (ret)
  529. return ret;
  530. if (tmp_fh.fh_dentry == fh->fh_dentry) {
  531. fh_put(&tmp_fh);
  532. return nfserr_noent;
  533. }
  534. fh_put(&tmp_fh);
  535. return nfsd_lookup(rqstp, fh, "..", 2, fh);
  536. }
  537. static __be32
  538. nfsd4_lookupp(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  539. void *arg)
  540. {
  541. return nfsd4_do_lookupp(rqstp, &cstate->current_fh);
  542. }
  543. static __be32
  544. nfsd4_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  545. struct nfsd4_lookup *lookup)
  546. {
  547. return nfsd_lookup(rqstp, &cstate->current_fh,
  548. lookup->lo_name, lookup->lo_len,
  549. &cstate->current_fh);
  550. }
  551. static __be32
  552. nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  553. struct nfsd4_read *read)
  554. {
  555. __be32 status;
  556. /* no need to check permission - this will be done in nfsd_read() */
  557. read->rd_filp = NULL;
  558. if (read->rd_offset >= OFFSET_MAX)
  559. return nfserr_inval;
  560. nfs4_lock_state();
  561. /* check stateid */
  562. if ((status = nfs4_preprocess_stateid_op(cstate, &read->rd_stateid,
  563. RD_STATE, &read->rd_filp))) {
  564. dprintk("NFSD: nfsd4_read: couldn't process stateid!\n");
  565. goto out;
  566. }
  567. if (read->rd_filp)
  568. get_file(read->rd_filp);
  569. status = nfs_ok;
  570. out:
  571. nfs4_unlock_state();
  572. read->rd_rqstp = rqstp;
  573. read->rd_fhp = &cstate->current_fh;
  574. return status;
  575. }
  576. static __be32
  577. nfsd4_readdir(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  578. struct nfsd4_readdir *readdir)
  579. {
  580. u64 cookie = readdir->rd_cookie;
  581. static const nfs4_verifier zeroverf;
  582. /* no need to check permission - this will be done in nfsd_readdir() */
  583. if (readdir->rd_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
  584. return nfserr_inval;
  585. readdir->rd_bmval[0] &= nfsd_suppattrs0(cstate->minorversion);
  586. readdir->rd_bmval[1] &= nfsd_suppattrs1(cstate->minorversion);
  587. readdir->rd_bmval[2] &= nfsd_suppattrs2(cstate->minorversion);
  588. if ((cookie == 1) || (cookie == 2) ||
  589. (cookie == 0 && memcmp(readdir->rd_verf.data, zeroverf.data, NFS4_VERIFIER_SIZE)))
  590. return nfserr_bad_cookie;
  591. readdir->rd_rqstp = rqstp;
  592. readdir->rd_fhp = &cstate->current_fh;
  593. return nfs_ok;
  594. }
  595. static __be32
  596. nfsd4_readlink(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  597. struct nfsd4_readlink *readlink)
  598. {
  599. readlink->rl_rqstp = rqstp;
  600. readlink->rl_fhp = &cstate->current_fh;
  601. return nfs_ok;
  602. }
  603. static __be32
  604. nfsd4_remove(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  605. struct nfsd4_remove *remove)
  606. {
  607. __be32 status;
  608. if (locks_in_grace())
  609. return nfserr_grace;
  610. status = nfsd_unlink(rqstp, &cstate->current_fh, 0,
  611. remove->rm_name, remove->rm_namelen);
  612. if (status == nfserr_symlink)
  613. return nfserr_notdir;
  614. if (!status) {
  615. fh_unlock(&cstate->current_fh);
  616. set_change_info(&remove->rm_cinfo, &cstate->current_fh);
  617. }
  618. return status;
  619. }
  620. static __be32
  621. nfsd4_rename(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  622. struct nfsd4_rename *rename)
  623. {
  624. __be32 status = nfserr_nofilehandle;
  625. if (!cstate->save_fh.fh_dentry)
  626. return status;
  627. if (locks_in_grace() && !(cstate->save_fh.fh_export->ex_flags
  628. & NFSEXP_NOSUBTREECHECK))
  629. return nfserr_grace;
  630. status = nfsd_rename(rqstp, &cstate->save_fh, rename->rn_sname,
  631. rename->rn_snamelen, &cstate->current_fh,
  632. rename->rn_tname, rename->rn_tnamelen);
  633. /* the underlying filesystem returns different error's than required
  634. * by NFSv4. both save_fh and current_fh have been verified.. */
  635. if (status == nfserr_isdir)
  636. status = nfserr_exist;
  637. else if ((status == nfserr_notdir) &&
  638. (S_ISDIR(cstate->save_fh.fh_dentry->d_inode->i_mode) &&
  639. S_ISDIR(cstate->current_fh.fh_dentry->d_inode->i_mode)))
  640. status = nfserr_exist;
  641. else if (status == nfserr_symlink)
  642. status = nfserr_notdir;
  643. if (!status) {
  644. set_change_info(&rename->rn_sinfo, &cstate->current_fh);
  645. set_change_info(&rename->rn_tinfo, &cstate->save_fh);
  646. }
  647. return status;
  648. }
  649. static __be32
  650. nfsd4_secinfo(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  651. struct nfsd4_secinfo *secinfo)
  652. {
  653. struct svc_fh resfh;
  654. struct svc_export *exp;
  655. struct dentry *dentry;
  656. __be32 err;
  657. fh_init(&resfh, NFS4_FHSIZE);
  658. err = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_EXEC);
  659. if (err)
  660. return err;
  661. err = nfsd_lookup_dentry(rqstp, &cstate->current_fh,
  662. secinfo->si_name, secinfo->si_namelen,
  663. &exp, &dentry);
  664. if (err)
  665. return err;
  666. if (dentry->d_inode == NULL) {
  667. exp_put(exp);
  668. err = nfserr_noent;
  669. } else
  670. secinfo->si_exp = exp;
  671. dput(dentry);
  672. if (cstate->minorversion)
  673. /* See rfc 5661 section 2.6.3.1.1.8 */
  674. fh_put(&cstate->current_fh);
  675. return err;
  676. }
  677. static __be32
  678. nfsd4_secinfo_no_name(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  679. struct nfsd4_secinfo_no_name *sin)
  680. {
  681. __be32 err;
  682. switch (sin->sin_style) {
  683. case NFS4_SECINFO_STYLE4_CURRENT_FH:
  684. break;
  685. case NFS4_SECINFO_STYLE4_PARENT:
  686. err = nfsd4_do_lookupp(rqstp, &cstate->current_fh);
  687. if (err)
  688. return err;
  689. break;
  690. default:
  691. return nfserr_inval;
  692. }
  693. exp_get(cstate->current_fh.fh_export);
  694. sin->sin_exp = cstate->current_fh.fh_export;
  695. fh_put(&cstate->current_fh);
  696. return nfs_ok;
  697. }
  698. static __be32
  699. nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  700. struct nfsd4_setattr *setattr)
  701. {
  702. __be32 status = nfs_ok;
  703. int err;
  704. if (setattr->sa_iattr.ia_valid & ATTR_SIZE) {
  705. nfs4_lock_state();
  706. status = nfs4_preprocess_stateid_op(cstate,
  707. &setattr->sa_stateid, WR_STATE, NULL);
  708. nfs4_unlock_state();
  709. if (status) {
  710. dprintk("NFSD: nfsd4_setattr: couldn't process stateid!\n");
  711. return status;
  712. }
  713. }
  714. err = mnt_want_write(cstate->current_fh.fh_export->ex_path.mnt);
  715. if (err)
  716. return nfserrno(err);
  717. status = nfs_ok;
  718. status = check_attr_support(rqstp, cstate, setattr->sa_bmval,
  719. nfsd_attrmask);
  720. if (status)
  721. goto out;
  722. if (setattr->sa_acl != NULL)
  723. status = nfsd4_set_nfs4_acl(rqstp, &cstate->current_fh,
  724. setattr->sa_acl);
  725. if (status)
  726. goto out;
  727. status = nfsd_setattr(rqstp, &cstate->current_fh, &setattr->sa_iattr,
  728. 0, (time_t)0);
  729. out:
  730. mnt_drop_write(cstate->current_fh.fh_export->ex_path.mnt);
  731. return status;
  732. }
  733. static __be32
  734. nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  735. struct nfsd4_write *write)
  736. {
  737. stateid_t *stateid = &write->wr_stateid;
  738. struct file *filp = NULL;
  739. u32 *p;
  740. __be32 status = nfs_ok;
  741. unsigned long cnt;
  742. /* no need to check permission - this will be done in nfsd_write() */
  743. if (write->wr_offset >= OFFSET_MAX)
  744. return nfserr_inval;
  745. nfs4_lock_state();
  746. status = nfs4_preprocess_stateid_op(cstate, stateid, WR_STATE, &filp);
  747. if (filp)
  748. get_file(filp);
  749. nfs4_unlock_state();
  750. if (status) {
  751. dprintk("NFSD: nfsd4_write: couldn't process stateid!\n");
  752. return status;
  753. }
  754. cnt = write->wr_buflen;
  755. write->wr_how_written = write->wr_stable_how;
  756. p = (u32 *)write->wr_verifier.data;
  757. *p++ = nfssvc_boot.tv_sec;
  758. *p++ = nfssvc_boot.tv_usec;
  759. status = nfsd_write(rqstp, &cstate->current_fh, filp,
  760. write->wr_offset, rqstp->rq_vec, write->wr_vlen,
  761. &cnt, &write->wr_how_written);
  762. if (filp)
  763. fput(filp);
  764. write->wr_bytes_written = cnt;
  765. if (status == nfserr_symlink)
  766. status = nfserr_inval;
  767. return status;
  768. }
  769. /* This routine never returns NFS_OK! If there are no other errors, it
  770. * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the
  771. * attributes matched. VERIFY is implemented by mapping NFSERR_SAME
  772. * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK.
  773. */
  774. static __be32
  775. _nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  776. struct nfsd4_verify *verify)
  777. {
  778. __be32 *buf, *p;
  779. int count;
  780. __be32 status;
  781. status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
  782. if (status)
  783. return status;
  784. status = check_attr_support(rqstp, cstate, verify->ve_bmval, NULL);
  785. if (status)
  786. return status;
  787. if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR)
  788. || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1))
  789. return nfserr_inval;
  790. if (verify->ve_attrlen & 3)
  791. return nfserr_inval;
  792. /* count in words:
  793. * bitmap_len(1) + bitmap(2) + attr_len(1) = 4
  794. */
  795. count = 4 + (verify->ve_attrlen >> 2);
  796. buf = kmalloc(count << 2, GFP_KERNEL);
  797. if (!buf)
  798. return nfserr_jukebox;
  799. status = nfsd4_encode_fattr(&cstate->current_fh,
  800. cstate->current_fh.fh_export,
  801. cstate->current_fh.fh_dentry, buf,
  802. &count, verify->ve_bmval,
  803. rqstp, 0);
  804. /* this means that nfsd4_encode_fattr() ran out of space */
  805. if (status == nfserr_resource && count == 0)
  806. status = nfserr_not_same;
  807. if (status)
  808. goto out_kfree;
  809. /* skip bitmap */
  810. p = buf + 1 + ntohl(buf[0]);
  811. status = nfserr_not_same;
  812. if (ntohl(*p++) != verify->ve_attrlen)
  813. goto out_kfree;
  814. if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen))
  815. status = nfserr_same;
  816. out_kfree:
  817. kfree(buf);
  818. return status;
  819. }
  820. static __be32
  821. nfsd4_nverify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  822. struct nfsd4_verify *verify)
  823. {
  824. __be32 status;
  825. status = _nfsd4_verify(rqstp, cstate, verify);
  826. return status == nfserr_not_same ? nfs_ok : status;
  827. }
  828. static __be32
  829. nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  830. struct nfsd4_verify *verify)
  831. {
  832. __be32 status;
  833. status = _nfsd4_verify(rqstp, cstate, verify);
  834. return status == nfserr_same ? nfs_ok : status;
  835. }
  836. /*
  837. * NULL call.
  838. */
  839. static __be32
  840. nfsd4_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
  841. {
  842. return nfs_ok;
  843. }
  844. static inline void nfsd4_increment_op_stats(u32 opnum)
  845. {
  846. if (opnum >= FIRST_NFS4_OP && opnum <= LAST_NFS4_OP)
  847. nfsdstats.nfs4_opcount[opnum]++;
  848. }
  849. typedef __be32(*nfsd4op_func)(struct svc_rqst *, struct nfsd4_compound_state *,
  850. void *);
  851. enum nfsd4_op_flags {
  852. ALLOWED_WITHOUT_FH = 1 << 0, /* No current filehandle required */
  853. ALLOWED_ON_ABSENT_FS = 1 << 1, /* ops processed on absent fs */
  854. ALLOWED_AS_FIRST_OP = 1 << 2, /* ops reqired first in compound */
  855. /* For rfc 5661 section 2.6.3.1.1: */
  856. OP_HANDLES_WRONGSEC = 1 << 3,
  857. OP_IS_PUTFH_LIKE = 1 << 4,
  858. };
  859. struct nfsd4_operation {
  860. nfsd4op_func op_func;
  861. u32 op_flags;
  862. char *op_name;
  863. };
  864. static struct nfsd4_operation nfsd4_ops[];
  865. static const char *nfsd4_op_name(unsigned opnum);
  866. /*
  867. * Enforce NFSv4.1 COMPOUND ordering rules:
  868. *
  869. * Also note, enforced elsewhere:
  870. * - SEQUENCE other than as first op results in
  871. * NFS4ERR_SEQUENCE_POS. (Enforced in nfsd4_sequence().)
  872. * - BIND_CONN_TO_SESSION must be the only op in its compound.
  873. * (Enforced in nfsd4_bind_conn_to_session().)
  874. * - DESTROY_SESSION must be the final operation in a compound, if
  875. * sessionid's in SEQUENCE and DESTROY_SESSION are the same.
  876. * (Enforced in nfsd4_destroy_session().)
  877. */
  878. static __be32 nfs41_check_op_ordering(struct nfsd4_compoundargs *args)
  879. {
  880. struct nfsd4_op *op = &args->ops[0];
  881. /* These ordering requirements don't apply to NFSv4.0: */
  882. if (args->minorversion == 0)
  883. return nfs_ok;
  884. /* This is weird, but OK, not our problem: */
  885. if (args->opcnt == 0)
  886. return nfs_ok;
  887. if (op->status == nfserr_op_illegal)
  888. return nfs_ok;
  889. if (!(nfsd4_ops[op->opnum].op_flags & ALLOWED_AS_FIRST_OP))
  890. return nfserr_op_not_in_session;
  891. if (op->opnum == OP_SEQUENCE)
  892. return nfs_ok;
  893. if (args->opcnt != 1)
  894. return nfserr_not_only_op;
  895. return nfs_ok;
  896. }
  897. static inline struct nfsd4_operation *OPDESC(struct nfsd4_op *op)
  898. {
  899. return &nfsd4_ops[op->opnum];
  900. }
  901. static bool need_wrongsec_check(struct svc_rqst *rqstp)
  902. {
  903. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  904. struct nfsd4_compoundargs *argp = rqstp->rq_argp;
  905. struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
  906. struct nfsd4_op *next = &argp->ops[resp->opcnt];
  907. struct nfsd4_operation *thisd;
  908. struct nfsd4_operation *nextd;
  909. thisd = OPDESC(this);
  910. /*
  911. * Most ops check wronsec on our own; only the putfh-like ops
  912. * have special rules.
  913. */
  914. if (!(thisd->op_flags & OP_IS_PUTFH_LIKE))
  915. return false;
  916. /*
  917. * rfc 5661 2.6.3.1.1.6: don't bother erroring out a
  918. * put-filehandle operation if we're not going to use the
  919. * result:
  920. */
  921. if (argp->opcnt == resp->opcnt)
  922. return false;
  923. nextd = OPDESC(next);
  924. /*
  925. * Rest of 2.6.3.1.1: certain operations will return WRONGSEC
  926. * errors themselves as necessary; others should check for them
  927. * now:
  928. */
  929. return !(nextd->op_flags & OP_HANDLES_WRONGSEC);
  930. }
  931. /*
  932. * COMPOUND call.
  933. */
  934. static __be32
  935. nfsd4_proc_compound(struct svc_rqst *rqstp,
  936. struct nfsd4_compoundargs *args,
  937. struct nfsd4_compoundres *resp)
  938. {
  939. struct nfsd4_op *op;
  940. struct nfsd4_operation *opdesc;
  941. struct nfsd4_compound_state *cstate = &resp->cstate;
  942. int slack_bytes;
  943. __be32 status;
  944. resp->xbuf = &rqstp->rq_res;
  945. resp->p = rqstp->rq_res.head[0].iov_base +
  946. rqstp->rq_res.head[0].iov_len;
  947. resp->tagp = resp->p;
  948. /* reserve space for: taglen, tag, and opcnt */
  949. resp->p += 2 + XDR_QUADLEN(args->taglen);
  950. resp->end = rqstp->rq_res.head[0].iov_base + PAGE_SIZE;
  951. resp->taglen = args->taglen;
  952. resp->tag = args->tag;
  953. resp->opcnt = 0;
  954. resp->rqstp = rqstp;
  955. resp->cstate.minorversion = args->minorversion;
  956. resp->cstate.replay_owner = NULL;
  957. resp->cstate.session = NULL;
  958. fh_init(&resp->cstate.current_fh, NFS4_FHSIZE);
  959. fh_init(&resp->cstate.save_fh, NFS4_FHSIZE);
  960. /*
  961. * Don't use the deferral mechanism for NFSv4; compounds make it
  962. * too hard to avoid non-idempotency problems.
  963. */
  964. rqstp->rq_usedeferral = 0;
  965. /*
  966. * According to RFC3010, this takes precedence over all other errors.
  967. */
  968. status = nfserr_minor_vers_mismatch;
  969. if (args->minorversion > nfsd_supported_minorversion)
  970. goto out;
  971. status = nfs41_check_op_ordering(args);
  972. if (status) {
  973. op = &args->ops[0];
  974. op->status = status;
  975. goto encode_op;
  976. }
  977. while (!status && resp->opcnt < args->opcnt) {
  978. op = &args->ops[resp->opcnt++];
  979. dprintk("nfsv4 compound op #%d/%d: %d (%s)\n",
  980. resp->opcnt, args->opcnt, op->opnum,
  981. nfsd4_op_name(op->opnum));
  982. /*
  983. * The XDR decode routines may have pre-set op->status;
  984. * for example, if there is a miscellaneous XDR error
  985. * it will be set to nfserr_bad_xdr.
  986. */
  987. if (op->status)
  988. goto encode_op;
  989. /* We must be able to encode a successful response to
  990. * this operation, with enough room left over to encode a
  991. * failed response to the next operation. If we don't
  992. * have enough room, fail with ERR_RESOURCE.
  993. */
  994. slack_bytes = (char *)resp->end - (char *)resp->p;
  995. if (slack_bytes < COMPOUND_SLACK_SPACE
  996. + COMPOUND_ERR_SLACK_SPACE) {
  997. BUG_ON(slack_bytes < COMPOUND_ERR_SLACK_SPACE);
  998. op->status = nfserr_resource;
  999. goto encode_op;
  1000. }
  1001. opdesc = OPDESC(op);
  1002. if (!cstate->current_fh.fh_dentry) {
  1003. if (!(opdesc->op_flags & ALLOWED_WITHOUT_FH)) {
  1004. op->status = nfserr_nofilehandle;
  1005. goto encode_op;
  1006. }
  1007. } else if (cstate->current_fh.fh_export->ex_fslocs.migrated &&
  1008. !(opdesc->op_flags & ALLOWED_ON_ABSENT_FS)) {
  1009. op->status = nfserr_moved;
  1010. goto encode_op;
  1011. }
  1012. if (opdesc->op_func)
  1013. op->status = opdesc->op_func(rqstp, cstate, &op->u);
  1014. else
  1015. BUG_ON(op->status == nfs_ok);
  1016. if (!op->status && need_wrongsec_check(rqstp))
  1017. op->status = check_nfsd_access(cstate->current_fh.fh_export, rqstp);
  1018. encode_op:
  1019. /* Only from SEQUENCE */
  1020. if (resp->cstate.status == nfserr_replay_cache) {
  1021. dprintk("%s NFS4.1 replay from cache\n", __func__);
  1022. status = op->status;
  1023. goto out;
  1024. }
  1025. if (op->status == nfserr_replay_me) {
  1026. op->replay = &cstate->replay_owner->so_replay;
  1027. nfsd4_encode_replay(resp, op);
  1028. status = op->status = op->replay->rp_status;
  1029. } else {
  1030. nfsd4_encode_operation(resp, op);
  1031. status = op->status;
  1032. }
  1033. dprintk("nfsv4 compound op %p opcnt %d #%d: %d: status %d\n",
  1034. args->ops, args->opcnt, resp->opcnt, op->opnum,
  1035. be32_to_cpu(status));
  1036. if (cstate->replay_owner) {
  1037. nfs4_put_stateowner(cstate->replay_owner);
  1038. cstate->replay_owner = NULL;
  1039. }
  1040. /* XXX Ugh, we need to get rid of this kind of special case: */
  1041. if (op->opnum == OP_READ && op->u.read.rd_filp)
  1042. fput(op->u.read.rd_filp);
  1043. nfsd4_increment_op_stats(op->opnum);
  1044. }
  1045. resp->cstate.status = status;
  1046. fh_put(&resp->cstate.current_fh);
  1047. fh_put(&resp->cstate.save_fh);
  1048. BUG_ON(resp->cstate.replay_owner);
  1049. out:
  1050. nfsd4_release_compoundargs(args);
  1051. /* Reset deferral mechanism for RPC deferrals */
  1052. rqstp->rq_usedeferral = 1;
  1053. dprintk("nfsv4 compound returned %d\n", ntohl(status));
  1054. return status;
  1055. }
  1056. static struct nfsd4_operation nfsd4_ops[] = {
  1057. [OP_ACCESS] = {
  1058. .op_func = (nfsd4op_func)nfsd4_access,
  1059. .op_name = "OP_ACCESS",
  1060. },
  1061. [OP_CLOSE] = {
  1062. .op_func = (nfsd4op_func)nfsd4_close,
  1063. .op_name = "OP_CLOSE",
  1064. },
  1065. [OP_COMMIT] = {
  1066. .op_func = (nfsd4op_func)nfsd4_commit,
  1067. .op_name = "OP_COMMIT",
  1068. },
  1069. [OP_CREATE] = {
  1070. .op_func = (nfsd4op_func)nfsd4_create,
  1071. .op_name = "OP_CREATE",
  1072. },
  1073. [OP_DELEGRETURN] = {
  1074. .op_func = (nfsd4op_func)nfsd4_delegreturn,
  1075. .op_name = "OP_DELEGRETURN",
  1076. },
  1077. [OP_GETATTR] = {
  1078. .op_func = (nfsd4op_func)nfsd4_getattr,
  1079. .op_flags = ALLOWED_ON_ABSENT_FS,
  1080. .op_name = "OP_GETATTR",
  1081. },
  1082. [OP_GETFH] = {
  1083. .op_func = (nfsd4op_func)nfsd4_getfh,
  1084. .op_name = "OP_GETFH",
  1085. },
  1086. [OP_LINK] = {
  1087. .op_func = (nfsd4op_func)nfsd4_link,
  1088. .op_name = "OP_LINK",
  1089. },
  1090. [OP_LOCK] = {
  1091. .op_func = (nfsd4op_func)nfsd4_lock,
  1092. .op_name = "OP_LOCK",
  1093. },
  1094. [OP_LOCKT] = {
  1095. .op_func = (nfsd4op_func)nfsd4_lockt,
  1096. .op_name = "OP_LOCKT",
  1097. },
  1098. [OP_LOCKU] = {
  1099. .op_func = (nfsd4op_func)nfsd4_locku,
  1100. .op_name = "OP_LOCKU",
  1101. },
  1102. [OP_LOOKUP] = {
  1103. .op_func = (nfsd4op_func)nfsd4_lookup,
  1104. .op_flags = OP_HANDLES_WRONGSEC,
  1105. .op_name = "OP_LOOKUP",
  1106. },
  1107. [OP_LOOKUPP] = {
  1108. .op_func = (nfsd4op_func)nfsd4_lookupp,
  1109. .op_flags = OP_HANDLES_WRONGSEC,
  1110. .op_name = "OP_LOOKUPP",
  1111. },
  1112. [OP_NVERIFY] = {
  1113. .op_func = (nfsd4op_func)nfsd4_nverify,
  1114. .op_name = "OP_NVERIFY",
  1115. },
  1116. [OP_OPEN] = {
  1117. .op_func = (nfsd4op_func)nfsd4_open,
  1118. .op_flags = OP_HANDLES_WRONGSEC,
  1119. .op_name = "OP_OPEN",
  1120. },
  1121. [OP_OPEN_CONFIRM] = {
  1122. .op_func = (nfsd4op_func)nfsd4_open_confirm,
  1123. .op_name = "OP_OPEN_CONFIRM",
  1124. },
  1125. [OP_OPEN_DOWNGRADE] = {
  1126. .op_func = (nfsd4op_func)nfsd4_open_downgrade,
  1127. .op_name = "OP_OPEN_DOWNGRADE",
  1128. },
  1129. [OP_PUTFH] = {
  1130. .op_func = (nfsd4op_func)nfsd4_putfh,
  1131. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1132. | OP_IS_PUTFH_LIKE,
  1133. .op_name = "OP_PUTFH",
  1134. },
  1135. [OP_PUTPUBFH] = {
  1136. .op_func = (nfsd4op_func)nfsd4_putrootfh,
  1137. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1138. | OP_IS_PUTFH_LIKE,
  1139. .op_name = "OP_PUTPUBFH",
  1140. },
  1141. [OP_PUTROOTFH] = {
  1142. .op_func = (nfsd4op_func)nfsd4_putrootfh,
  1143. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1144. | OP_IS_PUTFH_LIKE,
  1145. .op_name = "OP_PUTROOTFH",
  1146. },
  1147. [OP_READ] = {
  1148. .op_func = (nfsd4op_func)nfsd4_read,
  1149. .op_name = "OP_READ",
  1150. },
  1151. [OP_READDIR] = {
  1152. .op_func = (nfsd4op_func)nfsd4_readdir,
  1153. .op_name = "OP_READDIR",
  1154. },
  1155. [OP_READLINK] = {
  1156. .op_func = (nfsd4op_func)nfsd4_readlink,
  1157. .op_name = "OP_READLINK",
  1158. },
  1159. [OP_REMOVE] = {
  1160. .op_func = (nfsd4op_func)nfsd4_remove,
  1161. .op_name = "OP_REMOVE",
  1162. },
  1163. [OP_RENAME] = {
  1164. .op_name = "OP_RENAME",
  1165. .op_func = (nfsd4op_func)nfsd4_rename,
  1166. },
  1167. [OP_RENEW] = {
  1168. .op_func = (nfsd4op_func)nfsd4_renew,
  1169. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
  1170. .op_name = "OP_RENEW",
  1171. },
  1172. [OP_RESTOREFH] = {
  1173. .op_func = (nfsd4op_func)nfsd4_restorefh,
  1174. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1175. | OP_IS_PUTFH_LIKE,
  1176. .op_name = "OP_RESTOREFH",
  1177. },
  1178. [OP_SAVEFH] = {
  1179. .op_func = (nfsd4op_func)nfsd4_savefh,
  1180. .op_flags = OP_HANDLES_WRONGSEC,
  1181. .op_name = "OP_SAVEFH",
  1182. },
  1183. [OP_SECINFO] = {
  1184. .op_func = (nfsd4op_func)nfsd4_secinfo,
  1185. .op_flags = OP_HANDLES_WRONGSEC,
  1186. .op_name = "OP_SECINFO",
  1187. },
  1188. [OP_SETATTR] = {
  1189. .op_func = (nfsd4op_func)nfsd4_setattr,
  1190. .op_name = "OP_SETATTR",
  1191. },
  1192. [OP_SETCLIENTID] = {
  1193. .op_func = (nfsd4op_func)nfsd4_setclientid,
  1194. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
  1195. .op_name = "OP_SETCLIENTID",
  1196. },
  1197. [OP_SETCLIENTID_CONFIRM] = {
  1198. .op_func = (nfsd4op_func)nfsd4_setclientid_confirm,
  1199. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
  1200. .op_name = "OP_SETCLIENTID_CONFIRM",
  1201. },
  1202. [OP_VERIFY] = {
  1203. .op_func = (nfsd4op_func)nfsd4_verify,
  1204. .op_name = "OP_VERIFY",
  1205. },
  1206. [OP_WRITE] = {
  1207. .op_func = (nfsd4op_func)nfsd4_write,
  1208. .op_name = "OP_WRITE",
  1209. },
  1210. [OP_RELEASE_LOCKOWNER] = {
  1211. .op_func = (nfsd4op_func)nfsd4_release_lockowner,
  1212. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS,
  1213. .op_name = "OP_RELEASE_LOCKOWNER",
  1214. },
  1215. /* NFSv4.1 operations */
  1216. [OP_EXCHANGE_ID] = {
  1217. .op_func = (nfsd4op_func)nfsd4_exchange_id,
  1218. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
  1219. .op_name = "OP_EXCHANGE_ID",
  1220. },
  1221. [OP_BIND_CONN_TO_SESSION] = {
  1222. .op_func = (nfsd4op_func)nfsd4_bind_conn_to_session,
  1223. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
  1224. .op_name = "OP_BIND_CONN_TO_SESSION",
  1225. },
  1226. [OP_CREATE_SESSION] = {
  1227. .op_func = (nfsd4op_func)nfsd4_create_session,
  1228. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
  1229. .op_name = "OP_CREATE_SESSION",
  1230. },
  1231. [OP_DESTROY_SESSION] = {
  1232. .op_func = (nfsd4op_func)nfsd4_destroy_session,
  1233. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
  1234. .op_name = "OP_DESTROY_SESSION",
  1235. },
  1236. [OP_SEQUENCE] = {
  1237. .op_func = (nfsd4op_func)nfsd4_sequence,
  1238. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
  1239. .op_name = "OP_SEQUENCE",
  1240. },
  1241. [OP_RECLAIM_COMPLETE] = {
  1242. .op_func = (nfsd4op_func)nfsd4_reclaim_complete,
  1243. .op_flags = ALLOWED_WITHOUT_FH,
  1244. .op_name = "OP_RECLAIM_COMPLETE",
  1245. },
  1246. [OP_SECINFO_NO_NAME] = {
  1247. .op_func = (nfsd4op_func)nfsd4_secinfo_no_name,
  1248. .op_flags = OP_HANDLES_WRONGSEC,
  1249. .op_name = "OP_SECINFO_NO_NAME",
  1250. },
  1251. };
  1252. static const char *nfsd4_op_name(unsigned opnum)
  1253. {
  1254. if (opnum < ARRAY_SIZE(nfsd4_ops))
  1255. return nfsd4_ops[opnum].op_name;
  1256. return "unknown_operation";
  1257. }
  1258. #define nfsd4_voidres nfsd4_voidargs
  1259. struct nfsd4_voidargs { int dummy; };
  1260. /*
  1261. * TODO: At the present time, the NFSv4 server does not do XID caching
  1262. * of requests. Implementing XID caching would not be a serious problem,
  1263. * although it would require a mild change in interfaces since one
  1264. * doesn't know whether an NFSv4 request is idempotent until after the
  1265. * XDR decode. However, XID caching totally confuses pynfs (Peter
  1266. * Astrand's regression testsuite for NFSv4 servers), which reuses
  1267. * XID's liberally, so I've left it unimplemented until pynfs generates
  1268. * better XID's.
  1269. */
  1270. static struct svc_procedure nfsd_procedures4[2] = {
  1271. [NFSPROC4_NULL] = {
  1272. .pc_func = (svc_procfunc) nfsd4_proc_null,
  1273. .pc_encode = (kxdrproc_t) nfs4svc_encode_voidres,
  1274. .pc_argsize = sizeof(struct nfsd4_voidargs),
  1275. .pc_ressize = sizeof(struct nfsd4_voidres),
  1276. .pc_cachetype = RC_NOCACHE,
  1277. .pc_xdrressize = 1,
  1278. },
  1279. [NFSPROC4_COMPOUND] = {
  1280. .pc_func = (svc_procfunc) nfsd4_proc_compound,
  1281. .pc_decode = (kxdrproc_t) nfs4svc_decode_compoundargs,
  1282. .pc_encode = (kxdrproc_t) nfs4svc_encode_compoundres,
  1283. .pc_argsize = sizeof(struct nfsd4_compoundargs),
  1284. .pc_ressize = sizeof(struct nfsd4_compoundres),
  1285. .pc_cachetype = RC_NOCACHE,
  1286. .pc_xdrressize = NFSD_BUFSIZE/4,
  1287. },
  1288. };
  1289. struct svc_version nfsd_version4 = {
  1290. .vs_vers = 4,
  1291. .vs_nproc = 2,
  1292. .vs_proc = nfsd_procedures4,
  1293. .vs_dispatch = nfsd_dispatch,
  1294. .vs_xdrsize = NFS4_SVC_XDRSIZE,
  1295. };
  1296. /*
  1297. * Local variables:
  1298. * c-basic-offset: 8
  1299. * End:
  1300. */