nfsfh.c 14 KB

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