nfsfh.c 15 KB

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