nfsfh.c 14 KB

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