nfsfh.c 15 KB

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