nfsfh.c 15 KB

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