nfsfh.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /*
  2. * linux/fs/nfsd/nfsfh.c
  3. *
  4. * NFS server file handle treatment.
  5. *
  6. * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
  7. * Portions Copyright (C) 1999 G. Allen Morris III <gam3@acm.org>
  8. * Extensive rewrite by Neil Brown <neilb@cse.unsw.edu.au> Southern-Spring 1999
  9. * ... and again Southern-Winter 2001 to support export_operations
  10. */
  11. #include <linux/slab.h>
  12. #include <linux/fs.h>
  13. #include <linux/unistd.h>
  14. #include <linux/string.h>
  15. #include <linux/stat.h>
  16. #include <linux/dcache.h>
  17. #include <linux/exportfs.h>
  18. #include <linux/mount.h>
  19. #include <linux/sunrpc/clnt.h>
  20. #include <linux/sunrpc/svc.h>
  21. #include <linux/sunrpc/svcauth_gss.h>
  22. #include <linux/nfsd/nfsd.h>
  23. #define NFSDDBG_FACILITY NFSDDBG_FH
  24. static int nfsd_nr_verified;
  25. static int nfsd_nr_put;
  26. /*
  27. * our acceptability function.
  28. * if NOSUBTREECHECK, accept anything
  29. * if not, require that we can walk up to exp->ex_dentry
  30. * doing some checks on the 'x' bits
  31. */
  32. static int nfsd_acceptable(void *expv, struct dentry *dentry)
  33. {
  34. struct svc_export *exp = expv;
  35. int rv;
  36. struct dentry *tdentry;
  37. struct dentry *parent;
  38. if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
  39. return 1;
  40. tdentry = dget(dentry);
  41. while (tdentry != exp->ex_dentry && ! IS_ROOT(tdentry)) {
  42. /* make sure parents give x permission to user */
  43. int err;
  44. parent = dget_parent(tdentry);
  45. err = permission(parent->d_inode, MAY_EXEC, NULL);
  46. if (err < 0) {
  47. dput(parent);
  48. break;
  49. }
  50. dput(tdentry);
  51. tdentry = parent;
  52. }
  53. if (tdentry != exp->ex_dentry)
  54. dprintk("nfsd_acceptable failed at %p %s\n", tdentry, tdentry->d_name.name);
  55. rv = (tdentry == exp->ex_dentry);
  56. dput(tdentry);
  57. return rv;
  58. }
  59. /* Type check. The correct error return for type mismatches does not seem to be
  60. * generally agreed upon. SunOS seems to use EISDIR if file isn't S_IFREG; a
  61. * comment in the NFSv3 spec says this is incorrect (implementation notes for
  62. * the write call).
  63. */
  64. static inline __be32
  65. nfsd_mode_check(struct svc_rqst *rqstp, umode_t mode, int type)
  66. {
  67. /* Type can be negative when creating hardlinks - not to a dir */
  68. if (type > 0 && (mode & S_IFMT) != type) {
  69. if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK)
  70. return nfserr_symlink;
  71. else if (type == S_IFDIR)
  72. return nfserr_notdir;
  73. else if ((mode & S_IFMT) == S_IFDIR)
  74. return nfserr_isdir;
  75. else
  76. return nfserr_inval;
  77. }
  78. if (type < 0 && (mode & S_IFMT) == -type) {
  79. if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK)
  80. return nfserr_symlink;
  81. else if (type == -S_IFDIR)
  82. return nfserr_isdir;
  83. else
  84. return nfserr_notdir;
  85. }
  86. return 0;
  87. }
  88. /*
  89. * Perform sanity checks on the dentry in a client's file handle.
  90. *
  91. * Note that the file handle dentry may need to be freed even after
  92. * an error return.
  93. *
  94. * This is only called at the start of an nfsproc call, so fhp points to
  95. * a svc_fh which is all 0 except for the over-the-wire file handle.
  96. */
  97. __be32
  98. fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, int access)
  99. {
  100. struct knfsd_fh *fh = &fhp->fh_handle;
  101. struct svc_export *exp = NULL;
  102. struct dentry *dentry;
  103. __be32 error = 0;
  104. dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp));
  105. if (!fhp->fh_dentry) {
  106. struct fid *fid = NULL, sfid;
  107. int fileid_type;
  108. int data_left = fh->fh_size/4;
  109. error = nfserr_stale;
  110. if (rqstp->rq_vers > 2)
  111. error = nfserr_badhandle;
  112. if (rqstp->rq_vers == 4 && fh->fh_size == 0)
  113. return nfserr_nofilehandle;
  114. if (fh->fh_version == 1) {
  115. int len;
  116. if (--data_left<0) goto out;
  117. switch (fh->fh_auth_type) {
  118. case 0: break;
  119. default: goto out;
  120. }
  121. len = key_len(fh->fh_fsid_type) / 4;
  122. if (len == 0) goto out;
  123. if (fh->fh_fsid_type == FSID_MAJOR_MINOR) {
  124. /* deprecated, convert to type 3 */
  125. len = key_len(FSID_ENCODE_DEV)/4;
  126. fh->fh_fsid_type = FSID_ENCODE_DEV;
  127. fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl(fh->fh_fsid[0]), ntohl(fh->fh_fsid[1])));
  128. fh->fh_fsid[1] = fh->fh_fsid[2];
  129. }
  130. if ((data_left -= len)<0) goto out;
  131. exp = rqst_exp_find(rqstp, fh->fh_fsid_type,
  132. fh->fh_auth);
  133. fid = (struct fid *)(fh->fh_auth + len);
  134. } else {
  135. __u32 tfh[2];
  136. dev_t xdev;
  137. ino_t xino;
  138. if (fh->fh_size != NFS_FHSIZE)
  139. goto out;
  140. /* assume old filehandle format */
  141. xdev = old_decode_dev(fh->ofh_xdev);
  142. xino = u32_to_ino_t(fh->ofh_xino);
  143. mk_fsid(FSID_DEV, tfh, xdev, xino, 0, NULL);
  144. exp = rqst_exp_find(rqstp, FSID_DEV, tfh);
  145. }
  146. error = nfserr_stale;
  147. if (PTR_ERR(exp) == -ENOENT)
  148. goto out;
  149. if (IS_ERR(exp)) {
  150. error = nfserrno(PTR_ERR(exp));
  151. goto out;
  152. }
  153. /* Check if the request originated from a secure port. */
  154. error = nfserr_perm;
  155. if (!rqstp->rq_secure && EX_SECURE(exp)) {
  156. char buf[RPC_MAX_ADDRBUFLEN];
  157. printk(KERN_WARNING
  158. "nfsd: request from insecure port %s!\n",
  159. svc_print_addr(rqstp, buf, sizeof(buf)));
  160. goto out;
  161. }
  162. /* Set user creds for this exportpoint */
  163. error = nfserrno(nfsd_setuser(rqstp, exp));
  164. if (error)
  165. goto out;
  166. /*
  167. * Look up the dentry using the NFS file handle.
  168. */
  169. error = nfserr_stale;
  170. if (rqstp->rq_vers > 2)
  171. error = nfserr_badhandle;
  172. if (fh->fh_version != 1) {
  173. sfid.i32.ino = fh->ofh_ino;
  174. sfid.i32.gen = fh->ofh_generation;
  175. sfid.i32.parent_ino = fh->ofh_dirino;
  176. fid = &sfid;
  177. data_left = 3;
  178. if (fh->ofh_dirino == 0)
  179. fileid_type = FILEID_INO32_GEN;
  180. else
  181. fileid_type = FILEID_INO32_GEN_PARENT;
  182. } else
  183. fileid_type = fh->fh_fileid_type;
  184. if (fileid_type == FILEID_ROOT)
  185. dentry = dget(exp->ex_dentry);
  186. else {
  187. dentry = exportfs_decode_fh(exp->ex_mnt, fid,
  188. data_left, fileid_type,
  189. nfsd_acceptable, exp);
  190. }
  191. if (dentry == NULL)
  192. goto out;
  193. if (IS_ERR(dentry)) {
  194. if (PTR_ERR(dentry) != -EINVAL)
  195. error = nfserrno(PTR_ERR(dentry));
  196. goto out;
  197. }
  198. if (S_ISDIR(dentry->d_inode->i_mode) &&
  199. (dentry->d_flags & DCACHE_DISCONNECTED)) {
  200. printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %s/%s\n",
  201. dentry->d_parent->d_name.name, dentry->d_name.name);
  202. }
  203. fhp->fh_dentry = dentry;
  204. fhp->fh_export = exp;
  205. nfsd_nr_verified++;
  206. } else {
  207. /* just rechecking permissions
  208. * (e.g. nfsproc_create calls fh_verify, then nfsd_create does as well)
  209. */
  210. dprintk("nfsd: fh_verify - just checking\n");
  211. dentry = fhp->fh_dentry;
  212. exp = fhp->fh_export;
  213. /* Set user creds for this exportpoint; necessary even
  214. * in the "just checking" case because this may be a
  215. * filehandle that was created by fh_compose, and that
  216. * is about to be used in another nfsv4 compound
  217. * operation */
  218. error = nfserrno(nfsd_setuser(rqstp, exp));
  219. if (error)
  220. goto out;
  221. }
  222. cache_get(&exp->h);
  223. error = nfsd_mode_check(rqstp, dentry->d_inode->i_mode, type);
  224. if (error)
  225. goto out;
  226. if (!(access & MAY_LOCK)) {
  227. /*
  228. * pseudoflavor restrictions are not enforced on NLM,
  229. * which clients virtually always use auth_sys for,
  230. * even while using RPCSEC_GSS for NFS.
  231. */
  232. error = check_nfsd_access(exp, rqstp);
  233. if (error)
  234. goto out;
  235. }
  236. /* Finally, check access permissions. */
  237. error = nfsd_permission(rqstp, exp, dentry, access);
  238. if (error) {
  239. dprintk("fh_verify: %s/%s permission failure, "
  240. "acc=%x, error=%d\n",
  241. dentry->d_parent->d_name.name,
  242. dentry->d_name.name,
  243. access, ntohl(error));
  244. }
  245. out:
  246. if (exp && !IS_ERR(exp))
  247. exp_put(exp);
  248. if (error == nfserr_stale)
  249. nfsdstats.fh_stale++;
  250. return error;
  251. }
  252. /*
  253. * Compose a file handle for an NFS reply.
  254. *
  255. * Note that when first composed, the dentry may not yet have
  256. * an inode. In this case a call to fh_update should be made
  257. * before the fh goes out on the wire ...
  258. */
  259. static void _fh_update(struct svc_fh *fhp, struct svc_export *exp,
  260. struct dentry *dentry)
  261. {
  262. if (dentry != exp->ex_dentry) {
  263. struct fid *fid = (struct fid *)
  264. (fhp->fh_handle.fh_auth + fhp->fh_handle.fh_size/4 - 1);
  265. int maxsize = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
  266. int subtreecheck = !(exp->ex_flags & NFSEXP_NOSUBTREECHECK);
  267. fhp->fh_handle.fh_fileid_type =
  268. exportfs_encode_fh(dentry, fid, &maxsize, subtreecheck);
  269. fhp->fh_handle.fh_size += maxsize * 4;
  270. } else {
  271. fhp->fh_handle.fh_fileid_type = FILEID_ROOT;
  272. }
  273. }
  274. /*
  275. * for composing old style file handles
  276. */
  277. static inline void _fh_update_old(struct dentry *dentry,
  278. struct svc_export *exp,
  279. struct knfsd_fh *fh)
  280. {
  281. fh->ofh_ino = ino_t_to_u32(dentry->d_inode->i_ino);
  282. fh->ofh_generation = dentry->d_inode->i_generation;
  283. if (S_ISDIR(dentry->d_inode->i_mode) ||
  284. (exp->ex_flags & NFSEXP_NOSUBTREECHECK))
  285. fh->ofh_dirino = 0;
  286. }
  287. __be32
  288. fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry,
  289. struct svc_fh *ref_fh)
  290. {
  291. /* ref_fh is a reference file handle.
  292. * if it is non-null and for the same filesystem, then we should compose
  293. * a filehandle which is of the same version, where possible.
  294. * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca
  295. * Then create a 32byte filehandle using nfs_fhbase_old
  296. *
  297. */
  298. u8 version;
  299. u8 fsid_type = 0;
  300. struct inode * inode = dentry->d_inode;
  301. struct dentry *parent = dentry->d_parent;
  302. __u32 *datap;
  303. dev_t ex_dev = exp->ex_dentry->d_inode->i_sb->s_dev;
  304. int root_export = (exp->ex_dentry == exp->ex_dentry->d_sb->s_root);
  305. dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %s/%s, ino=%ld)\n",
  306. MAJOR(ex_dev), MINOR(ex_dev),
  307. (long) exp->ex_dentry->d_inode->i_ino,
  308. parent->d_name.name, dentry->d_name.name,
  309. (inode ? inode->i_ino : 0));
  310. /* Choose filehandle version and fsid type based on
  311. * the reference filehandle (if it is in the same export)
  312. * or the export options.
  313. */
  314. retry:
  315. version = 1;
  316. if (ref_fh && ref_fh->fh_export == exp) {
  317. version = ref_fh->fh_handle.fh_version;
  318. fsid_type = ref_fh->fh_handle.fh_fsid_type;
  319. if (ref_fh == fhp)
  320. fh_put(ref_fh);
  321. ref_fh = NULL;
  322. switch (version) {
  323. case 0xca:
  324. fsid_type = FSID_DEV;
  325. break;
  326. case 1:
  327. break;
  328. default:
  329. goto retry;
  330. }
  331. /* Need to check that this type works for this
  332. * export point. As the fsid -> filesystem mapping
  333. * was guided by user-space, there is no guarantee
  334. * that the filesystem actually supports that fsid
  335. * type. If it doesn't we loop around again without
  336. * ref_fh set.
  337. */
  338. switch(fsid_type) {
  339. case FSID_DEV:
  340. if (!old_valid_dev(ex_dev))
  341. goto retry;
  342. /* FALL THROUGH */
  343. case FSID_MAJOR_MINOR:
  344. case FSID_ENCODE_DEV:
  345. if (!(exp->ex_dentry->d_inode->i_sb->s_type->fs_flags
  346. & FS_REQUIRES_DEV))
  347. goto retry;
  348. break;
  349. case FSID_NUM:
  350. if (! (exp->ex_flags & NFSEXP_FSID))
  351. goto retry;
  352. break;
  353. case FSID_UUID8:
  354. case FSID_UUID16:
  355. if (!root_export)
  356. goto retry;
  357. /* fall through */
  358. case FSID_UUID4_INUM:
  359. case FSID_UUID16_INUM:
  360. if (exp->ex_uuid == NULL)
  361. goto retry;
  362. break;
  363. }
  364. } else if (exp->ex_uuid) {
  365. if (fhp->fh_maxsize >= 64) {
  366. if (root_export)
  367. fsid_type = FSID_UUID16;
  368. else
  369. fsid_type = FSID_UUID16_INUM;
  370. } else {
  371. if (root_export)
  372. fsid_type = FSID_UUID8;
  373. else
  374. fsid_type = FSID_UUID4_INUM;
  375. }
  376. } else if (exp->ex_flags & NFSEXP_FSID)
  377. fsid_type = FSID_NUM;
  378. else if (!old_valid_dev(ex_dev))
  379. /* for newer device numbers, we must use a newer fsid format */
  380. fsid_type = FSID_ENCODE_DEV;
  381. else
  382. fsid_type = FSID_DEV;
  383. if (ref_fh == fhp)
  384. fh_put(ref_fh);
  385. if (fhp->fh_locked || fhp->fh_dentry) {
  386. printk(KERN_ERR "fh_compose: fh %s/%s not initialized!\n",
  387. parent->d_name.name, dentry->d_name.name);
  388. }
  389. if (fhp->fh_maxsize < NFS_FHSIZE)
  390. printk(KERN_ERR "fh_compose: called with maxsize %d! %s/%s\n",
  391. fhp->fh_maxsize,
  392. parent->d_name.name, dentry->d_name.name);
  393. fhp->fh_dentry = dget(dentry); /* our internal copy */
  394. fhp->fh_export = exp;
  395. cache_get(&exp->h);
  396. if (version == 0xca) {
  397. /* old style filehandle please */
  398. memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE);
  399. fhp->fh_handle.fh_size = NFS_FHSIZE;
  400. fhp->fh_handle.ofh_dcookie = 0xfeebbaca;
  401. fhp->fh_handle.ofh_dev = old_encode_dev(ex_dev);
  402. fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev;
  403. fhp->fh_handle.ofh_xino =
  404. ino_t_to_u32(exp->ex_dentry->d_inode->i_ino);
  405. fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry));
  406. if (inode)
  407. _fh_update_old(dentry, exp, &fhp->fh_handle);
  408. } else {
  409. int len;
  410. fhp->fh_handle.fh_version = 1;
  411. fhp->fh_handle.fh_auth_type = 0;
  412. datap = fhp->fh_handle.fh_auth+0;
  413. fhp->fh_handle.fh_fsid_type = fsid_type;
  414. mk_fsid(fsid_type, datap, ex_dev,
  415. exp->ex_dentry->d_inode->i_ino,
  416. exp->ex_fsid, exp->ex_uuid);
  417. len = key_len(fsid_type);
  418. datap += len/4;
  419. fhp->fh_handle.fh_size = 4 + len;
  420. if (inode)
  421. _fh_update(fhp, exp, dentry);
  422. if (fhp->fh_handle.fh_fileid_type == 255)
  423. return nfserr_opnotsupp;
  424. }
  425. nfsd_nr_verified++;
  426. return 0;
  427. }
  428. /*
  429. * Update file handle information after changing a dentry.
  430. * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
  431. */
  432. __be32
  433. fh_update(struct svc_fh *fhp)
  434. {
  435. struct dentry *dentry;
  436. if (!fhp->fh_dentry)
  437. goto out_bad;
  438. dentry = fhp->fh_dentry;
  439. if (!dentry->d_inode)
  440. goto out_negative;
  441. if (fhp->fh_handle.fh_version != 1) {
  442. _fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle);
  443. } else {
  444. if (fhp->fh_handle.fh_fileid_type != FILEID_ROOT)
  445. goto out;
  446. _fh_update(fhp, fhp->fh_export, dentry);
  447. if (fhp->fh_handle.fh_fileid_type == 255)
  448. return nfserr_opnotsupp;
  449. }
  450. out:
  451. return 0;
  452. out_bad:
  453. printk(KERN_ERR "fh_update: fh not verified!\n");
  454. goto out;
  455. out_negative:
  456. printk(KERN_ERR "fh_update: %s/%s still negative!\n",
  457. dentry->d_parent->d_name.name, dentry->d_name.name);
  458. goto out;
  459. }
  460. /*
  461. * Release a file handle.
  462. */
  463. void
  464. fh_put(struct svc_fh *fhp)
  465. {
  466. struct dentry * dentry = fhp->fh_dentry;
  467. struct svc_export * exp = fhp->fh_export;
  468. if (dentry) {
  469. fh_unlock(fhp);
  470. fhp->fh_dentry = NULL;
  471. dput(dentry);
  472. #ifdef CONFIG_NFSD_V3
  473. fhp->fh_pre_saved = 0;
  474. fhp->fh_post_saved = 0;
  475. #endif
  476. nfsd_nr_put++;
  477. }
  478. if (exp) {
  479. cache_put(&exp->h, &svc_export_cache);
  480. fhp->fh_export = NULL;
  481. }
  482. return;
  483. }
  484. /*
  485. * Shorthand for dprintk()'s
  486. */
  487. char * SVCFH_fmt(struct svc_fh *fhp)
  488. {
  489. struct knfsd_fh *fh = &fhp->fh_handle;
  490. static char buf[80];
  491. sprintf(buf, "%d: %08x %08x %08x %08x %08x %08x",
  492. fh->fh_size,
  493. fh->fh_base.fh_pad[0],
  494. fh->fh_base.fh_pad[1],
  495. fh->fh_base.fh_pad[2],
  496. fh->fh_base.fh_pad[3],
  497. fh->fh_base.fh_pad[4],
  498. fh->fh_base.fh_pad[5]);
  499. return buf;
  500. }
  501. enum fsid_source fsid_source(struct svc_fh *fhp)
  502. {
  503. if (fhp->fh_handle.fh_version != 1)
  504. return FSIDSOURCE_DEV;
  505. switch(fhp->fh_handle.fh_fsid_type) {
  506. case FSID_DEV:
  507. case FSID_ENCODE_DEV:
  508. case FSID_MAJOR_MINOR:
  509. if (fhp->fh_export->ex_dentry->d_inode->i_sb->s_type->fs_flags
  510. & FS_REQUIRES_DEV)
  511. return FSIDSOURCE_DEV;
  512. break;
  513. case FSID_NUM:
  514. if (fhp->fh_export->ex_flags & NFSEXP_FSID)
  515. return FSIDSOURCE_FSID;
  516. break;
  517. default:
  518. break;
  519. }
  520. /* either a UUID type filehandle, or the filehandle doesn't
  521. * match the export.
  522. */
  523. if (fhp->fh_export->ex_flags & NFSEXP_FSID)
  524. return FSIDSOURCE_FSID;
  525. if (fhp->fh_export->ex_uuid)
  526. return FSIDSOURCE_UUID;
  527. return FSIDSOURCE_DEV;
  528. }