nfsfh.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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/sched.h>
  12. #include <linux/slab.h>
  13. #include <linux/smp_lock.h>
  14. #include <linux/fs.h>
  15. #include <linux/unistd.h>
  16. #include <linux/string.h>
  17. #include <linux/stat.h>
  18. #include <linux/dcache.h>
  19. #include <linux/mount.h>
  20. #include <asm/pgtable.h>
  21. #include <linux/sunrpc/svc.h>
  22. #include <linux/nfsd/nfsd.h>
  23. #define NFSDDBG_FACILITY NFSDDBG_FH
  24. #define NFSD_PARANOIA 1
  25. /* #define NFSD_DEBUG_VERBOSE 1 */
  26. static int nfsd_nr_verified;
  27. static int nfsd_nr_put;
  28. extern struct export_operations export_op_default;
  29. #define CALL(ops,fun) ((ops->fun)?(ops->fun):export_op_default.fun)
  30. /*
  31. * our acceptability function.
  32. * if NOSUBTREECHECK, accept anything
  33. * if not, require that we can walk up to exp->ex_dentry
  34. * doing some checks on the 'x' bits
  35. */
  36. static int nfsd_acceptable(void *expv, struct dentry *dentry)
  37. {
  38. struct svc_export *exp = expv;
  39. int rv;
  40. struct dentry *tdentry;
  41. struct dentry *parent;
  42. if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
  43. return 1;
  44. tdentry = dget(dentry);
  45. while (tdentry != exp->ex_dentry && ! IS_ROOT(tdentry)) {
  46. /* make sure parents give x permission to user */
  47. int err;
  48. parent = dget_parent(tdentry);
  49. err = permission(parent->d_inode, MAY_EXEC, NULL);
  50. if (err < 0) {
  51. dput(parent);
  52. break;
  53. }
  54. dput(tdentry);
  55. tdentry = parent;
  56. }
  57. if (tdentry != exp->ex_dentry)
  58. dprintk("nfsd_acceptable failed at %p %s\n", tdentry, tdentry->d_name.name);
  59. rv = (tdentry == exp->ex_dentry);
  60. dput(tdentry);
  61. return rv;
  62. }
  63. /* Type check. The correct error return for type mismatches does not seem to be
  64. * generally agreed upon. SunOS seems to use EISDIR if file isn't S_IFREG; a
  65. * comment in the NFSv3 spec says this is incorrect (implementation notes for
  66. * the write call).
  67. */
  68. static inline int
  69. nfsd_mode_check(struct svc_rqst *rqstp, umode_t mode, int type)
  70. {
  71. /* Type can be negative when creating hardlinks - not to a dir */
  72. if (type > 0 && (mode & S_IFMT) != type) {
  73. if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK)
  74. return nfserr_symlink;
  75. else if (type == S_IFDIR)
  76. return nfserr_notdir;
  77. else if ((mode & S_IFMT) == S_IFDIR)
  78. return nfserr_isdir;
  79. else
  80. return nfserr_inval;
  81. }
  82. if (type < 0 && (mode & S_IFMT) == -type) {
  83. if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK)
  84. return nfserr_symlink;
  85. else if (type == -S_IFDIR)
  86. return nfserr_isdir;
  87. else
  88. return nfserr_notdir;
  89. }
  90. return 0;
  91. }
  92. /*
  93. * Perform sanity checks on the dentry in a client's file handle.
  94. *
  95. * Note that the file handle dentry may need to be freed even after
  96. * an error return.
  97. *
  98. * This is only called at the start of an nfsproc call, so fhp points to
  99. * a svc_fh which is all 0 except for the over-the-wire file handle.
  100. */
  101. u32
  102. fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, int access)
  103. {
  104. struct knfsd_fh *fh = &fhp->fh_handle;
  105. struct svc_export *exp = NULL;
  106. struct dentry *dentry;
  107. u32 error = 0;
  108. dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp));
  109. /* keep this filehandle for possible reference when encoding attributes */
  110. rqstp->rq_reffh = fh;
  111. if (!fhp->fh_dentry) {
  112. __u32 *datap=NULL;
  113. __u32 tfh[3]; /* filehandle fragment for oldstyle filehandles */
  114. int fileid_type;
  115. int data_left = fh->fh_size/4;
  116. error = nfserr_stale;
  117. if (rqstp->rq_client == NULL)
  118. goto out;
  119. if (rqstp->rq_vers > 2)
  120. error = nfserr_badhandle;
  121. if (rqstp->rq_vers == 4 && fh->fh_size == 0)
  122. return nfserr_nofilehandle;
  123. if (fh->fh_version == 1) {
  124. int len;
  125. datap = fh->fh_auth;
  126. if (--data_left<0) goto out;
  127. switch (fh->fh_auth_type) {
  128. case 0: break;
  129. default: goto out;
  130. }
  131. len = key_len(fh->fh_fsid_type) / 4;
  132. if (len == 0) goto out;
  133. if (fh->fh_fsid_type == 2) {
  134. /* deprecated, convert to type 3 */
  135. len = 3;
  136. fh->fh_fsid_type = 3;
  137. fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl(fh->fh_fsid[0]), ntohl(fh->fh_fsid[1])));
  138. fh->fh_fsid[1] = fh->fh_fsid[2];
  139. }
  140. if ((data_left -= len)<0) goto out;
  141. exp = exp_find(rqstp->rq_client, fh->fh_fsid_type, datap, &rqstp->rq_chandle);
  142. datap += len;
  143. } else {
  144. dev_t xdev;
  145. ino_t xino;
  146. if (fh->fh_size != NFS_FHSIZE)
  147. goto out;
  148. /* assume old filehandle format */
  149. xdev = old_decode_dev(fh->ofh_xdev);
  150. xino = u32_to_ino_t(fh->ofh_xino);
  151. mk_fsid_v0(tfh, xdev, xino);
  152. exp = exp_find(rqstp->rq_client, 0, tfh, &rqstp->rq_chandle);
  153. }
  154. error = nfserr_dropit;
  155. if (IS_ERR(exp) && PTR_ERR(exp) == -EAGAIN)
  156. goto out;
  157. error = nfserr_stale;
  158. if (!exp || IS_ERR(exp))
  159. goto out;
  160. /* Check if the request originated from a secure port. */
  161. error = nfserr_perm;
  162. if (!rqstp->rq_secure && EX_SECURE(exp)) {
  163. printk(KERN_WARNING
  164. "nfsd: request from insecure port (%u.%u.%u.%u:%d)!\n",
  165. NIPQUAD(rqstp->rq_addr.sin_addr.s_addr),
  166. ntohs(rqstp->rq_addr.sin_port));
  167. goto out;
  168. }
  169. /* Set user creds for this exportpoint */
  170. error = nfserrno(nfsd_setuser(rqstp, exp));
  171. if (error)
  172. goto out;
  173. /*
  174. * Look up the dentry using the NFS file handle.
  175. */
  176. error = nfserr_stale;
  177. if (rqstp->rq_vers > 2)
  178. error = nfserr_badhandle;
  179. if (fh->fh_version != 1) {
  180. tfh[0] = fh->ofh_ino;
  181. tfh[1] = fh->ofh_generation;
  182. tfh[2] = fh->ofh_dirino;
  183. datap = tfh;
  184. data_left = 3;
  185. if (fh->ofh_dirino == 0)
  186. fileid_type = 1;
  187. else
  188. fileid_type = 2;
  189. } else
  190. fileid_type = fh->fh_fileid_type;
  191. if (fileid_type == 0)
  192. dentry = dget(exp->ex_dentry);
  193. else {
  194. struct export_operations *nop = exp->ex_mnt->mnt_sb->s_export_op;
  195. dentry = CALL(nop,decode_fh)(exp->ex_mnt->mnt_sb,
  196. datap, data_left,
  197. fileid_type,
  198. nfsd_acceptable, exp);
  199. }
  200. if (dentry == NULL)
  201. goto out;
  202. if (IS_ERR(dentry)) {
  203. if (PTR_ERR(dentry) != -EINVAL)
  204. error = nfserrno(PTR_ERR(dentry));
  205. goto out;
  206. }
  207. #ifdef NFSD_PARANOIA
  208. if (S_ISDIR(dentry->d_inode->i_mode) &&
  209. (dentry->d_flags & DCACHE_DISCONNECTED)) {
  210. printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %s/%s\n",
  211. dentry->d_parent->d_name.name, dentry->d_name.name);
  212. }
  213. #endif
  214. fhp->fh_dentry = dentry;
  215. fhp->fh_export = exp;
  216. nfsd_nr_verified++;
  217. } else {
  218. /* just rechecking permissions
  219. * (e.g. nfsproc_create calls fh_verify, then nfsd_create does as well)
  220. */
  221. dprintk("nfsd: fh_verify - just checking\n");
  222. dentry = fhp->fh_dentry;
  223. exp = fhp->fh_export;
  224. /* Set user creds for this exportpoint; necessary even
  225. * in the "just checking" case because this may be a
  226. * filehandle that was created by fh_compose, and that
  227. * is about to be used in another nfsv4 compound
  228. * operation */
  229. error = nfserrno(nfsd_setuser(rqstp, exp));
  230. if (error)
  231. goto out;
  232. }
  233. cache_get(&exp->h);
  234. error = nfsd_mode_check(rqstp, dentry->d_inode->i_mode, type);
  235. if (error)
  236. goto out;
  237. /* Finally, check access permissions. */
  238. error = nfsd_permission(exp, dentry, access);
  239. #ifdef NFSD_PARANOIA_EXTREME
  240. if (error) {
  241. printk("fh_verify: %s/%s permission failure, acc=%x, error=%d\n",
  242. dentry->d_parent->d_name.name, dentry->d_name.name, access, (error >> 24));
  243. }
  244. #endif
  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. struct export_operations *nop = exp->ex_mnt->mnt_sb->s_export_op;
  263. if (dentry == exp->ex_dentry) {
  264. *maxsize = 0;
  265. return 0;
  266. }
  267. return CALL(nop,encode_fh)(dentry, datap, maxsize,
  268. !(exp->ex_flags&NFSEXP_NOSUBTREECHECK));
  269. }
  270. /*
  271. * for composing old style file handles
  272. */
  273. static inline void _fh_update_old(struct dentry *dentry,
  274. struct svc_export *exp,
  275. struct knfsd_fh *fh)
  276. {
  277. fh->ofh_ino = ino_t_to_u32(dentry->d_inode->i_ino);
  278. fh->ofh_generation = dentry->d_inode->i_generation;
  279. if (S_ISDIR(dentry->d_inode->i_mode) ||
  280. (exp->ex_flags & NFSEXP_NOSUBTREECHECK))
  281. fh->ofh_dirino = 0;
  282. }
  283. int
  284. fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry, 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 ref_fh_version = 0;
  294. u8 ref_fh_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. dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %s/%s, ino=%ld)\n",
  300. MAJOR(ex_dev), MINOR(ex_dev),
  301. (long) exp->ex_dentry->d_inode->i_ino,
  302. parent->d_name.name, dentry->d_name.name,
  303. (inode ? inode->i_ino : 0));
  304. if (ref_fh && ref_fh->fh_export == exp) {
  305. ref_fh_version = ref_fh->fh_handle.fh_version;
  306. if (ref_fh_version == 0xca)
  307. ref_fh_fsid_type = 0;
  308. else
  309. ref_fh_fsid_type = ref_fh->fh_handle.fh_fsid_type;
  310. if (ref_fh_fsid_type > 3)
  311. ref_fh_fsid_type = 0;
  312. /* make sure ref_fh type works for given export */
  313. if (ref_fh_fsid_type == 1 &&
  314. !(exp->ex_flags & NFSEXP_FSID)) {
  315. /* if we don't have an fsid, we cannot provide one... */
  316. ref_fh_fsid_type = 0;
  317. }
  318. } else if (exp->ex_flags & NFSEXP_FSID)
  319. ref_fh_fsid_type = 1;
  320. if (!old_valid_dev(ex_dev) && ref_fh_fsid_type == 0) {
  321. /* for newer device numbers, we must use a newer fsid format */
  322. ref_fh_version = 1;
  323. ref_fh_fsid_type = 3;
  324. }
  325. if (old_valid_dev(ex_dev) &&
  326. (ref_fh_fsid_type == 2 || ref_fh_fsid_type == 3))
  327. /* must use type1 for smaller device numbers */
  328. ref_fh_fsid_type = 0;
  329. if (ref_fh == fhp)
  330. fh_put(ref_fh);
  331. if (fhp->fh_locked || fhp->fh_dentry) {
  332. printk(KERN_ERR "fh_compose: fh %s/%s not initialized!\n",
  333. parent->d_name.name, dentry->d_name.name);
  334. }
  335. if (fhp->fh_maxsize < NFS_FHSIZE)
  336. printk(KERN_ERR "fh_compose: called with maxsize %d! %s/%s\n",
  337. fhp->fh_maxsize, parent->d_name.name, dentry->d_name.name);
  338. fhp->fh_dentry = dget(dentry); /* our internal copy */
  339. fhp->fh_export = exp;
  340. cache_get(&exp->h);
  341. if (ref_fh_version == 0xca) {
  342. /* old style filehandle please */
  343. memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE);
  344. fhp->fh_handle.fh_size = NFS_FHSIZE;
  345. fhp->fh_handle.ofh_dcookie = 0xfeebbaca;
  346. fhp->fh_handle.ofh_dev = old_encode_dev(ex_dev);
  347. fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev;
  348. fhp->fh_handle.ofh_xino = ino_t_to_u32(exp->ex_dentry->d_inode->i_ino);
  349. fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry));
  350. if (inode)
  351. _fh_update_old(dentry, exp, &fhp->fh_handle);
  352. } else {
  353. int len;
  354. fhp->fh_handle.fh_version = 1;
  355. fhp->fh_handle.fh_auth_type = 0;
  356. datap = fhp->fh_handle.fh_auth+0;
  357. fhp->fh_handle.fh_fsid_type = ref_fh_fsid_type;
  358. switch (ref_fh_fsid_type) {
  359. case 0:
  360. /*
  361. * fsid_type 0:
  362. * 2byte major, 2byte minor, 4byte inode
  363. */
  364. mk_fsid_v0(datap, ex_dev,
  365. exp->ex_dentry->d_inode->i_ino);
  366. break;
  367. case 1:
  368. /* fsid_type 1 == 4 bytes filesystem id */
  369. mk_fsid_v1(datap, exp->ex_fsid);
  370. break;
  371. case 2:
  372. /*
  373. * fsid_type 2:
  374. * 4byte major, 4byte minor, 4byte inode
  375. */
  376. mk_fsid_v2(datap, ex_dev,
  377. exp->ex_dentry->d_inode->i_ino);
  378. break;
  379. case 3:
  380. /*
  381. * fsid_type 3:
  382. * 4byte devicenumber, 4byte inode
  383. */
  384. mk_fsid_v3(datap, ex_dev,
  385. exp->ex_dentry->d_inode->i_ino);
  386. break;
  387. }
  388. len = key_len(ref_fh_fsid_type);
  389. datap += len/4;
  390. fhp->fh_handle.fh_size = 4 + len;
  391. if (inode) {
  392. int size = (fhp->fh_maxsize-len-4)/4;
  393. fhp->fh_handle.fh_fileid_type =
  394. _fh_update(dentry, exp, datap, &size);
  395. fhp->fh_handle.fh_size += size*4;
  396. }
  397. if (fhp->fh_handle.fh_fileid_type == 255)
  398. return nfserr_opnotsupp;
  399. }
  400. nfsd_nr_verified++;
  401. return 0;
  402. }
  403. /*
  404. * Update file handle information after changing a dentry.
  405. * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
  406. */
  407. int
  408. fh_update(struct svc_fh *fhp)
  409. {
  410. struct dentry *dentry;
  411. __u32 *datap;
  412. if (!fhp->fh_dentry)
  413. goto out_bad;
  414. dentry = fhp->fh_dentry;
  415. if (!dentry->d_inode)
  416. goto out_negative;
  417. if (fhp->fh_handle.fh_version != 1) {
  418. _fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle);
  419. } else {
  420. int size;
  421. if (fhp->fh_handle.fh_fileid_type != 0)
  422. goto out;
  423. datap = fhp->fh_handle.fh_auth+
  424. fhp->fh_handle.fh_size/4 -1;
  425. size = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
  426. fhp->fh_handle.fh_fileid_type =
  427. _fh_update(dentry, fhp->fh_export, datap, &size);
  428. fhp->fh_handle.fh_size += size*4;
  429. if (fhp->fh_handle.fh_fileid_type == 255)
  430. return nfserr_opnotsupp;
  431. }
  432. out:
  433. return 0;
  434. out_bad:
  435. printk(KERN_ERR "fh_update: fh not verified!\n");
  436. goto out;
  437. out_negative:
  438. printk(KERN_ERR "fh_update: %s/%s still negative!\n",
  439. dentry->d_parent->d_name.name, dentry->d_name.name);
  440. goto out;
  441. }
  442. /*
  443. * Release a file handle.
  444. */
  445. void
  446. fh_put(struct svc_fh *fhp)
  447. {
  448. struct dentry * dentry = fhp->fh_dentry;
  449. struct svc_export * exp = fhp->fh_export;
  450. if (dentry) {
  451. fh_unlock(fhp);
  452. fhp->fh_dentry = NULL;
  453. dput(dentry);
  454. #ifdef CONFIG_NFSD_V3
  455. fhp->fh_pre_saved = 0;
  456. fhp->fh_post_saved = 0;
  457. #endif
  458. nfsd_nr_put++;
  459. }
  460. if (exp) {
  461. cache_put(&exp->h, &svc_export_cache);
  462. fhp->fh_export = NULL;
  463. }
  464. return;
  465. }
  466. /*
  467. * Shorthand for dprintk()'s
  468. */
  469. char * SVCFH_fmt(struct svc_fh *fhp)
  470. {
  471. struct knfsd_fh *fh = &fhp->fh_handle;
  472. static char buf[80];
  473. sprintf(buf, "%d: %08x %08x %08x %08x %08x %08x",
  474. fh->fh_size,
  475. fh->fh_base.fh_pad[0],
  476. fh->fh_base.fh_pad[1],
  477. fh->fh_base.fh_pad[2],
  478. fh->fh_base.fh_pad[3],
  479. fh->fh_base.fh_pad[4],
  480. fh->fh_base.fh_pad[5]);
  481. return buf;
  482. }