nfsfh.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. __u32 *datap=NULL;
  107. __u32 tfh[3]; /* filehandle fragment for oldstyle filehandles */
  108. int fileid_type;
  109. int data_left = fh->fh_size/4;
  110. error = nfserr_stale;
  111. if (rqstp->rq_vers > 2)
  112. error = nfserr_badhandle;
  113. if (rqstp->rq_vers == 4 && fh->fh_size == 0)
  114. return nfserr_nofilehandle;
  115. if (fh->fh_version == 1) {
  116. int len;
  117. datap = fh->fh_auth;
  118. if (--data_left<0) goto out;
  119. switch (fh->fh_auth_type) {
  120. case 0: break;
  121. default: goto out;
  122. }
  123. len = key_len(fh->fh_fsid_type) / 4;
  124. if (len == 0) goto out;
  125. if (fh->fh_fsid_type == FSID_MAJOR_MINOR) {
  126. /* deprecated, convert to type 3 */
  127. len = key_len(FSID_ENCODE_DEV)/4;
  128. fh->fh_fsid_type = FSID_ENCODE_DEV;
  129. fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl(fh->fh_fsid[0]), ntohl(fh->fh_fsid[1])));
  130. fh->fh_fsid[1] = fh->fh_fsid[2];
  131. }
  132. if ((data_left -= len)<0) goto out;
  133. exp = rqst_exp_find(rqstp, fh->fh_fsid_type, datap);
  134. datap += len;
  135. } else {
  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. tfh[0] = fh->ofh_ino;
  174. tfh[1] = fh->ofh_generation;
  175. tfh[2] = fh->ofh_dirino;
  176. datap = tfh;
  177. data_left = 3;
  178. if (fh->ofh_dirino == 0)
  179. fileid_type = 1;
  180. else
  181. fileid_type = 2;
  182. } else
  183. fileid_type = fh->fh_fileid_type;
  184. if (fileid_type == 0)
  185. dentry = dget(exp->ex_dentry);
  186. else {
  187. dentry = exportfs_decode_fh(exp->ex_mnt, datap,
  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 inline int _fh_update(struct dentry *dentry, struct svc_export *exp,
  260. __u32 *datap, int *maxsize)
  261. {
  262. if (dentry == exp->ex_dentry) {
  263. *maxsize = 0;
  264. return 0;
  265. }
  266. return exportfs_encode_fh(dentry, datap, maxsize,
  267. !(exp->ex_flags & NFSEXP_NOSUBTREECHECK));
  268. }
  269. /*
  270. * for composing old style file handles
  271. */
  272. static inline void _fh_update_old(struct dentry *dentry,
  273. struct svc_export *exp,
  274. struct knfsd_fh *fh)
  275. {
  276. fh->ofh_ino = ino_t_to_u32(dentry->d_inode->i_ino);
  277. fh->ofh_generation = dentry->d_inode->i_generation;
  278. if (S_ISDIR(dentry->d_inode->i_mode) ||
  279. (exp->ex_flags & NFSEXP_NOSUBTREECHECK))
  280. fh->ofh_dirino = 0;
  281. }
  282. __be32
  283. fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry,
  284. struct svc_fh *ref_fh)
  285. {
  286. /* ref_fh is a reference file handle.
  287. * if it is non-null and for the same filesystem, then we should compose
  288. * a filehandle which is of the same version, where possible.
  289. * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca
  290. * Then create a 32byte filehandle using nfs_fhbase_old
  291. *
  292. */
  293. u8 version;
  294. u8 fsid_type = 0;
  295. struct inode * inode = dentry->d_inode;
  296. struct dentry *parent = dentry->d_parent;
  297. __u32 *datap;
  298. dev_t ex_dev = exp->ex_dentry->d_inode->i_sb->s_dev;
  299. int root_export = (exp->ex_dentry == exp->ex_dentry->d_sb->s_root);
  300. dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %s/%s, ino=%ld)\n",
  301. MAJOR(ex_dev), MINOR(ex_dev),
  302. (long) exp->ex_dentry->d_inode->i_ino,
  303. parent->d_name.name, dentry->d_name.name,
  304. (inode ? inode->i_ino : 0));
  305. /* Choose filehandle version and fsid type based on
  306. * the reference filehandle (if it is in the same export)
  307. * or the export options.
  308. */
  309. retry:
  310. version = 1;
  311. if (ref_fh && ref_fh->fh_export == exp) {
  312. version = ref_fh->fh_handle.fh_version;
  313. fsid_type = ref_fh->fh_handle.fh_fsid_type;
  314. if (ref_fh == fhp)
  315. fh_put(ref_fh);
  316. ref_fh = NULL;
  317. switch (version) {
  318. case 0xca:
  319. fsid_type = FSID_DEV;
  320. break;
  321. case 1:
  322. break;
  323. default:
  324. goto retry;
  325. }
  326. /* Need to check that this type works for this
  327. * export point. As the fsid -> filesystem mapping
  328. * was guided by user-space, there is no guarantee
  329. * that the filesystem actually supports that fsid
  330. * type. If it doesn't we loop around again without
  331. * ref_fh set.
  332. */
  333. switch(fsid_type) {
  334. case FSID_DEV:
  335. if (!old_valid_dev(ex_dev))
  336. goto retry;
  337. /* FALL THROUGH */
  338. case FSID_MAJOR_MINOR:
  339. case FSID_ENCODE_DEV:
  340. if (!(exp->ex_dentry->d_inode->i_sb->s_type->fs_flags
  341. & FS_REQUIRES_DEV))
  342. goto retry;
  343. break;
  344. case FSID_NUM:
  345. if (! (exp->ex_flags & NFSEXP_FSID))
  346. goto retry;
  347. break;
  348. case FSID_UUID8:
  349. case FSID_UUID16:
  350. if (!root_export)
  351. goto retry;
  352. /* fall through */
  353. case FSID_UUID4_INUM:
  354. case FSID_UUID16_INUM:
  355. if (exp->ex_uuid == NULL)
  356. goto retry;
  357. break;
  358. }
  359. } else if (exp->ex_uuid) {
  360. if (fhp->fh_maxsize >= 64) {
  361. if (root_export)
  362. fsid_type = FSID_UUID16;
  363. else
  364. fsid_type = FSID_UUID16_INUM;
  365. } else {
  366. if (root_export)
  367. fsid_type = FSID_UUID8;
  368. else
  369. fsid_type = FSID_UUID4_INUM;
  370. }
  371. } else if (exp->ex_flags & NFSEXP_FSID)
  372. fsid_type = FSID_NUM;
  373. else if (!old_valid_dev(ex_dev))
  374. /* for newer device numbers, we must use a newer fsid format */
  375. fsid_type = FSID_ENCODE_DEV;
  376. else
  377. fsid_type = FSID_DEV;
  378. if (ref_fh == fhp)
  379. fh_put(ref_fh);
  380. if (fhp->fh_locked || fhp->fh_dentry) {
  381. printk(KERN_ERR "fh_compose: fh %s/%s not initialized!\n",
  382. parent->d_name.name, dentry->d_name.name);
  383. }
  384. if (fhp->fh_maxsize < NFS_FHSIZE)
  385. printk(KERN_ERR "fh_compose: called with maxsize %d! %s/%s\n",
  386. fhp->fh_maxsize,
  387. parent->d_name.name, dentry->d_name.name);
  388. fhp->fh_dentry = dget(dentry); /* our internal copy */
  389. fhp->fh_export = exp;
  390. cache_get(&exp->h);
  391. if (version == 0xca) {
  392. /* old style filehandle please */
  393. memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE);
  394. fhp->fh_handle.fh_size = NFS_FHSIZE;
  395. fhp->fh_handle.ofh_dcookie = 0xfeebbaca;
  396. fhp->fh_handle.ofh_dev = old_encode_dev(ex_dev);
  397. fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev;
  398. fhp->fh_handle.ofh_xino =
  399. ino_t_to_u32(exp->ex_dentry->d_inode->i_ino);
  400. fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry));
  401. if (inode)
  402. _fh_update_old(dentry, exp, &fhp->fh_handle);
  403. } else {
  404. int len;
  405. fhp->fh_handle.fh_version = 1;
  406. fhp->fh_handle.fh_auth_type = 0;
  407. datap = fhp->fh_handle.fh_auth+0;
  408. fhp->fh_handle.fh_fsid_type = fsid_type;
  409. mk_fsid(fsid_type, datap, ex_dev,
  410. exp->ex_dentry->d_inode->i_ino,
  411. exp->ex_fsid, exp->ex_uuid);
  412. len = key_len(fsid_type);
  413. datap += len/4;
  414. fhp->fh_handle.fh_size = 4 + len;
  415. if (inode) {
  416. int size = (fhp->fh_maxsize-len-4)/4;
  417. fhp->fh_handle.fh_fileid_type =
  418. _fh_update(dentry, exp, datap, &size);
  419. fhp->fh_handle.fh_size += size*4;
  420. }
  421. if (fhp->fh_handle.fh_fileid_type == 255)
  422. return nfserr_opnotsupp;
  423. }
  424. nfsd_nr_verified++;
  425. return 0;
  426. }
  427. /*
  428. * Update file handle information after changing a dentry.
  429. * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
  430. */
  431. __be32
  432. fh_update(struct svc_fh *fhp)
  433. {
  434. struct dentry *dentry;
  435. __u32 *datap;
  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. int size;
  445. if (fhp->fh_handle.fh_fileid_type != 0)
  446. goto out;
  447. datap = fhp->fh_handle.fh_auth+
  448. fhp->fh_handle.fh_size/4 -1;
  449. size = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
  450. fhp->fh_handle.fh_fileid_type =
  451. _fh_update(dentry, fhp->fh_export, datap, &size);
  452. fhp->fh_handle.fh_size += size*4;
  453. if (fhp->fh_handle.fh_fileid_type == 255)
  454. return nfserr_opnotsupp;
  455. }
  456. out:
  457. return 0;
  458. out_bad:
  459. printk(KERN_ERR "fh_update: fh not verified!\n");
  460. goto out;
  461. out_negative:
  462. printk(KERN_ERR "fh_update: %s/%s still negative!\n",
  463. dentry->d_parent->d_name.name, dentry->d_name.name);
  464. goto out;
  465. }
  466. /*
  467. * Release a file handle.
  468. */
  469. void
  470. fh_put(struct svc_fh *fhp)
  471. {
  472. struct dentry * dentry = fhp->fh_dentry;
  473. struct svc_export * exp = fhp->fh_export;
  474. if (dentry) {
  475. fh_unlock(fhp);
  476. fhp->fh_dentry = NULL;
  477. dput(dentry);
  478. #ifdef CONFIG_NFSD_V3
  479. fhp->fh_pre_saved = 0;
  480. fhp->fh_post_saved = 0;
  481. #endif
  482. nfsd_nr_put++;
  483. }
  484. if (exp) {
  485. cache_put(&exp->h, &svc_export_cache);
  486. fhp->fh_export = NULL;
  487. }
  488. return;
  489. }
  490. /*
  491. * Shorthand for dprintk()'s
  492. */
  493. char * SVCFH_fmt(struct svc_fh *fhp)
  494. {
  495. struct knfsd_fh *fh = &fhp->fh_handle;
  496. static char buf[80];
  497. sprintf(buf, "%d: %08x %08x %08x %08x %08x %08x",
  498. fh->fh_size,
  499. fh->fh_base.fh_pad[0],
  500. fh->fh_base.fh_pad[1],
  501. fh->fh_base.fh_pad[2],
  502. fh->fh_base.fh_pad[3],
  503. fh->fh_base.fh_pad[4],
  504. fh->fh_base.fh_pad[5]);
  505. return buf;
  506. }
  507. enum fsid_source fsid_source(struct svc_fh *fhp)
  508. {
  509. if (fhp->fh_handle.fh_version != 1)
  510. return FSIDSOURCE_DEV;
  511. switch(fhp->fh_handle.fh_fsid_type) {
  512. case FSID_DEV:
  513. case FSID_ENCODE_DEV:
  514. case FSID_MAJOR_MINOR:
  515. if (fhp->fh_export->ex_dentry->d_inode->i_sb->s_type->fs_flags
  516. & FS_REQUIRES_DEV)
  517. return FSIDSOURCE_DEV;
  518. break;
  519. case FSID_NUM:
  520. if (fhp->fh_export->ex_flags & NFSEXP_FSID)
  521. return FSIDSOURCE_FSID;
  522. break;
  523. default:
  524. break;
  525. }
  526. /* either a UUID type filehandle, or the filehandle doesn't
  527. * match the export.
  528. */
  529. if (fhp->fh_export->ex_flags & NFSEXP_FSID)
  530. return FSIDSOURCE_FSID;
  531. if (fhp->fh_export->ex_uuid)
  532. return FSIDSOURCE_UUID;
  533. return FSIDSOURCE_DEV;
  534. }