nfsfh.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. /*
  2. * NFS server file handle treatment.
  3. *
  4. * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
  5. * Portions Copyright (C) 1999 G. Allen Morris III <gam3@acm.org>
  6. * Extensive rewrite by Neil Brown <neilb@cse.unsw.edu.au> Southern-Spring 1999
  7. * ... and again Southern-Winter 2001 to support export_operations
  8. */
  9. #include <linux/exportfs.h>
  10. #include <linux/sunrpc/svcauth_gss.h>
  11. #include "nfsd.h"
  12. #include "vfs.h"
  13. #include "auth.h"
  14. #define NFSDDBG_FACILITY NFSDDBG_FH
  15. /*
  16. * our acceptability function.
  17. * if NOSUBTREECHECK, accept anything
  18. * if not, require that we can walk up to exp->ex_dentry
  19. * doing some checks on the 'x' bits
  20. */
  21. static int nfsd_acceptable(void *expv, struct dentry *dentry)
  22. {
  23. struct svc_export *exp = expv;
  24. int rv;
  25. struct dentry *tdentry;
  26. struct dentry *parent;
  27. if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
  28. return 1;
  29. tdentry = dget(dentry);
  30. while (tdentry != exp->ex_path.dentry && !IS_ROOT(tdentry)) {
  31. /* make sure parents give x permission to user */
  32. int err;
  33. parent = dget_parent(tdentry);
  34. err = inode_permission(parent->d_inode, MAY_EXEC);
  35. if (err < 0) {
  36. dput(parent);
  37. break;
  38. }
  39. dput(tdentry);
  40. tdentry = parent;
  41. }
  42. if (tdentry != exp->ex_path.dentry)
  43. dprintk("nfsd_acceptable failed at %p %s\n", tdentry, tdentry->d_name.name);
  44. rv = (tdentry == exp->ex_path.dentry);
  45. dput(tdentry);
  46. return rv;
  47. }
  48. /* Type check. The correct error return for type mismatches does not seem to be
  49. * generally agreed upon. SunOS seems to use EISDIR if file isn't S_IFREG; a
  50. * comment in the NFSv3 spec says this is incorrect (implementation notes for
  51. * the write call).
  52. */
  53. static inline __be32
  54. nfsd_mode_check(struct svc_rqst *rqstp, umode_t mode, int type)
  55. {
  56. /* Type can be negative when creating hardlinks - not to a dir */
  57. if (type > 0 && (mode & S_IFMT) != type) {
  58. if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK)
  59. return nfserr_symlink;
  60. else if (type == S_IFDIR)
  61. return nfserr_notdir;
  62. else if ((mode & S_IFMT) == S_IFDIR)
  63. return nfserr_isdir;
  64. else
  65. return nfserr_inval;
  66. }
  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_isdir;
  72. else
  73. return nfserr_notdir;
  74. }
  75. return 0;
  76. }
  77. static __be32 nfsd_setuser_and_check_port(struct svc_rqst *rqstp,
  78. struct svc_export *exp)
  79. {
  80. int flags = nfsexp_flags(rqstp, exp);
  81. /* Check if the request originated from a secure port. */
  82. if (!rqstp->rq_secure && !(flags & NFSEXP_INSECURE_PORT)) {
  83. RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
  84. dprintk(KERN_WARNING
  85. "nfsd: request from insecure port %s!\n",
  86. svc_print_addr(rqstp, buf, sizeof(buf)));
  87. return nfserr_perm;
  88. }
  89. /* Set user creds for this exportpoint */
  90. return nfserrno(nfsd_setuser(rqstp, exp));
  91. }
  92. static inline __be32 check_pseudo_root(struct svc_rqst *rqstp,
  93. struct dentry *dentry, struct svc_export *exp)
  94. {
  95. if (!(exp->ex_flags & NFSEXP_V4ROOT))
  96. return nfs_ok;
  97. /*
  98. * v2/v3 clients have no need for the V4ROOT export--they use
  99. * the mount protocl instead; also, further V4ROOT checks may be
  100. * in v4-specific code, in which case v2/v3 clients could bypass
  101. * them.
  102. */
  103. if (!nfsd_v4client(rqstp))
  104. return nfserr_stale;
  105. /*
  106. * We're exposing only the directories and symlinks that have to be
  107. * traversed on the way to real exports:
  108. */
  109. if (unlikely(!S_ISDIR(dentry->d_inode->i_mode) &&
  110. !S_ISLNK(dentry->d_inode->i_mode)))
  111. return nfserr_stale;
  112. /*
  113. * A pseudoroot export gives permission to access only one
  114. * single directory; the kernel has to make another upcall
  115. * before granting access to anything else under it:
  116. */
  117. if (unlikely(dentry != exp->ex_path.dentry))
  118. return nfserr_stale;
  119. return nfs_ok;
  120. }
  121. /*
  122. * Use the given filehandle to look up the corresponding export and
  123. * dentry. On success, the results are used to set fh_export and
  124. * fh_dentry.
  125. */
  126. static __be32 nfsd_set_fh_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp)
  127. {
  128. struct knfsd_fh *fh = &fhp->fh_handle;
  129. struct fid *fid = NULL, sfid;
  130. struct svc_export *exp;
  131. struct dentry *dentry;
  132. int fileid_type;
  133. int data_left = fh->fh_size/4;
  134. __be32 error;
  135. error = nfserr_stale;
  136. if (rqstp->rq_vers > 2)
  137. error = nfserr_badhandle;
  138. if (rqstp->rq_vers == 4 && fh->fh_size == 0)
  139. return nfserr_nofilehandle;
  140. if (fh->fh_version == 1) {
  141. int len;
  142. if (--data_left < 0)
  143. return error;
  144. if (fh->fh_auth_type != 0)
  145. return error;
  146. len = key_len(fh->fh_fsid_type) / 4;
  147. if (len == 0)
  148. return error;
  149. if (fh->fh_fsid_type == FSID_MAJOR_MINOR) {
  150. /* deprecated, convert to type 3 */
  151. len = key_len(FSID_ENCODE_DEV)/4;
  152. fh->fh_fsid_type = FSID_ENCODE_DEV;
  153. fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl(fh->fh_fsid[0]), ntohl(fh->fh_fsid[1])));
  154. fh->fh_fsid[1] = fh->fh_fsid[2];
  155. }
  156. data_left -= len;
  157. if (data_left < 0)
  158. return error;
  159. exp = rqst_exp_find(rqstp, fh->fh_fsid_type, fh->fh_auth);
  160. fid = (struct fid *)(fh->fh_auth + len);
  161. } else {
  162. __u32 tfh[2];
  163. dev_t xdev;
  164. ino_t xino;
  165. if (fh->fh_size != NFS_FHSIZE)
  166. return error;
  167. /* assume old filehandle format */
  168. xdev = old_decode_dev(fh->ofh_xdev);
  169. xino = u32_to_ino_t(fh->ofh_xino);
  170. mk_fsid(FSID_DEV, tfh, xdev, xino, 0, NULL);
  171. exp = rqst_exp_find(rqstp, FSID_DEV, tfh);
  172. }
  173. error = nfserr_stale;
  174. if (PTR_ERR(exp) == -ENOENT)
  175. return error;
  176. if (IS_ERR(exp))
  177. return nfserrno(PTR_ERR(exp));
  178. if (exp->ex_flags & NFSEXP_NOSUBTREECHECK) {
  179. /* Elevate privileges so that the lack of 'r' or 'x'
  180. * permission on some parent directory will
  181. * not stop exportfs_decode_fh from being able
  182. * to reconnect a directory into the dentry cache.
  183. * The same problem can affect "SUBTREECHECK" exports,
  184. * but as nfsd_acceptable depends on correct
  185. * access control settings being in effect, we cannot
  186. * fix that case easily.
  187. */
  188. struct cred *new = prepare_creds();
  189. if (!new)
  190. return nfserrno(-ENOMEM);
  191. new->cap_effective =
  192. cap_raise_nfsd_set(new->cap_effective,
  193. new->cap_permitted);
  194. put_cred(override_creds(new));
  195. put_cred(new);
  196. } else {
  197. error = nfsd_setuser_and_check_port(rqstp, exp);
  198. if (error)
  199. goto out;
  200. }
  201. /*
  202. * Look up the dentry using the NFS file handle.
  203. */
  204. error = nfserr_stale;
  205. if (rqstp->rq_vers > 2)
  206. error = nfserr_badhandle;
  207. if (fh->fh_version != 1) {
  208. sfid.i32.ino = fh->ofh_ino;
  209. sfid.i32.gen = fh->ofh_generation;
  210. sfid.i32.parent_ino = fh->ofh_dirino;
  211. fid = &sfid;
  212. data_left = 3;
  213. if (fh->ofh_dirino == 0)
  214. fileid_type = FILEID_INO32_GEN;
  215. else
  216. fileid_type = FILEID_INO32_GEN_PARENT;
  217. } else
  218. fileid_type = fh->fh_fileid_type;
  219. if (fileid_type == FILEID_ROOT)
  220. dentry = dget(exp->ex_path.dentry);
  221. else {
  222. dentry = exportfs_decode_fh(exp->ex_path.mnt, fid,
  223. data_left, fileid_type,
  224. nfsd_acceptable, exp);
  225. }
  226. if (dentry == NULL)
  227. goto out;
  228. if (IS_ERR(dentry)) {
  229. if (PTR_ERR(dentry) != -EINVAL)
  230. error = nfserrno(PTR_ERR(dentry));
  231. goto out;
  232. }
  233. if (S_ISDIR(dentry->d_inode->i_mode) &&
  234. (dentry->d_flags & DCACHE_DISCONNECTED)) {
  235. printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %s/%s\n",
  236. dentry->d_parent->d_name.name, dentry->d_name.name);
  237. }
  238. fhp->fh_dentry = dentry;
  239. fhp->fh_export = exp;
  240. return 0;
  241. out:
  242. exp_put(exp);
  243. return error;
  244. }
  245. /**
  246. * fh_verify - filehandle lookup and access checking
  247. * @rqstp: pointer to current rpc request
  248. * @fhp: filehandle to be verified
  249. * @type: expected type of object pointed to by filehandle
  250. * @access: type of access needed to object
  251. *
  252. * Look up a dentry from the on-the-wire filehandle, check the client's
  253. * access to the export, and set the current task's credentials.
  254. *
  255. * Regardless of success or failure of fh_verify(), fh_put() should be
  256. * called on @fhp when the caller is finished with the filehandle.
  257. *
  258. * fh_verify() may be called multiple times on a given filehandle, for
  259. * example, when processing an NFSv4 compound. The first call will look
  260. * up a dentry using the on-the-wire filehandle. Subsequent calls will
  261. * skip the lookup and just perform the other checks and possibly change
  262. * the current task's credentials.
  263. *
  264. * @type specifies the type of object expected using one of the S_IF*
  265. * constants defined in include/linux/stat.h. The caller may use zero
  266. * to indicate that it doesn't care, or a negative integer to indicate
  267. * that it expects something not of the given type.
  268. *
  269. * @access is formed from the NFSD_MAY_* constants defined in
  270. * include/linux/nfsd/nfsd.h.
  271. */
  272. __be32
  273. fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, int access)
  274. {
  275. struct svc_export *exp;
  276. struct dentry *dentry;
  277. __be32 error;
  278. dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp));
  279. if (!fhp->fh_dentry) {
  280. error = nfsd_set_fh_dentry(rqstp, fhp);
  281. if (error)
  282. goto out;
  283. }
  284. dentry = fhp->fh_dentry;
  285. exp = fhp->fh_export;
  286. /*
  287. * We still have to do all these permission checks, even when
  288. * fh_dentry is already set:
  289. * - fh_verify may be called multiple times with different
  290. * "access" arguments (e.g. nfsd_proc_create calls
  291. * fh_verify(...,NFSD_MAY_EXEC) first, then later (in
  292. * nfsd_create) calls fh_verify(...,NFSD_MAY_CREATE).
  293. * - in the NFSv4 case, the filehandle may have been filled
  294. * in by fh_compose, and given a dentry, but further
  295. * compound operations performed with that filehandle
  296. * still need permissions checks. In the worst case, a
  297. * mountpoint crossing may have changed the export
  298. * options, and we may now need to use a different uid
  299. * (for example, if different id-squashing options are in
  300. * effect on the new filesystem).
  301. */
  302. error = check_pseudo_root(rqstp, dentry, exp);
  303. if (error)
  304. goto out;
  305. error = nfsd_setuser_and_check_port(rqstp, exp);
  306. if (error)
  307. goto out;
  308. error = nfsd_mode_check(rqstp, dentry->d_inode->i_mode, type);
  309. if (error)
  310. goto out;
  311. /*
  312. * pseudoflavor restrictions are not enforced on NLM,
  313. * which clients virtually always use auth_sys for,
  314. * even while using RPCSEC_GSS for NFS.
  315. */
  316. if (access & NFSD_MAY_LOCK || access & NFSD_MAY_BYPASS_GSS)
  317. goto skip_pseudoflavor_check;
  318. /*
  319. * Clients may expect to be able to use auth_sys during mount,
  320. * even if they use gss for everything else; see section 2.3.2
  321. * of rfc 2623.
  322. */
  323. if (access & NFSD_MAY_BYPASS_GSS_ON_ROOT
  324. && exp->ex_path.dentry == dentry)
  325. goto skip_pseudoflavor_check;
  326. error = check_nfsd_access(exp, rqstp);
  327. if (error)
  328. goto out;
  329. skip_pseudoflavor_check:
  330. /* Finally, check access permissions. */
  331. error = nfsd_permission(rqstp, exp, dentry, access);
  332. if (error) {
  333. dprintk("fh_verify: %s/%s permission failure, "
  334. "acc=%x, error=%d\n",
  335. dentry->d_parent->d_name.name,
  336. dentry->d_name.name,
  337. access, ntohl(error));
  338. }
  339. out:
  340. if (error == nfserr_stale)
  341. nfsdstats.fh_stale++;
  342. return error;
  343. }
  344. /*
  345. * Compose a file handle for an NFS reply.
  346. *
  347. * Note that when first composed, the dentry may not yet have
  348. * an inode. In this case a call to fh_update should be made
  349. * before the fh goes out on the wire ...
  350. */
  351. static void _fh_update(struct svc_fh *fhp, struct svc_export *exp,
  352. struct dentry *dentry)
  353. {
  354. if (dentry != exp->ex_path.dentry) {
  355. struct fid *fid = (struct fid *)
  356. (fhp->fh_handle.fh_auth + fhp->fh_handle.fh_size/4 - 1);
  357. int maxsize = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
  358. int subtreecheck = !(exp->ex_flags & NFSEXP_NOSUBTREECHECK);
  359. fhp->fh_handle.fh_fileid_type =
  360. exportfs_encode_fh(dentry, fid, &maxsize, subtreecheck);
  361. fhp->fh_handle.fh_size += maxsize * 4;
  362. } else {
  363. fhp->fh_handle.fh_fileid_type = FILEID_ROOT;
  364. }
  365. }
  366. /*
  367. * for composing old style file handles
  368. */
  369. static inline void _fh_update_old(struct dentry *dentry,
  370. struct svc_export *exp,
  371. struct knfsd_fh *fh)
  372. {
  373. fh->ofh_ino = ino_t_to_u32(dentry->d_inode->i_ino);
  374. fh->ofh_generation = dentry->d_inode->i_generation;
  375. if (S_ISDIR(dentry->d_inode->i_mode) ||
  376. (exp->ex_flags & NFSEXP_NOSUBTREECHECK))
  377. fh->ofh_dirino = 0;
  378. }
  379. static bool is_root_export(struct svc_export *exp)
  380. {
  381. return exp->ex_path.dentry == exp->ex_path.dentry->d_sb->s_root;
  382. }
  383. static struct super_block *exp_sb(struct svc_export *exp)
  384. {
  385. return exp->ex_path.dentry->d_inode->i_sb;
  386. }
  387. static bool fsid_type_ok_for_exp(u8 fsid_type, struct svc_export *exp)
  388. {
  389. switch (fsid_type) {
  390. case FSID_DEV:
  391. if (!old_valid_dev(exp_sb(exp)->s_dev))
  392. return 0;
  393. /* FALL THROUGH */
  394. case FSID_MAJOR_MINOR:
  395. case FSID_ENCODE_DEV:
  396. return exp_sb(exp)->s_type->fs_flags & FS_REQUIRES_DEV;
  397. case FSID_NUM:
  398. return exp->ex_flags & NFSEXP_FSID;
  399. case FSID_UUID8:
  400. case FSID_UUID16:
  401. if (!is_root_export(exp))
  402. return 0;
  403. /* fall through */
  404. case FSID_UUID4_INUM:
  405. case FSID_UUID16_INUM:
  406. return exp->ex_uuid != NULL;
  407. }
  408. return 1;
  409. }
  410. static void set_version_and_fsid_type(struct svc_fh *fhp, struct svc_export *exp, struct svc_fh *ref_fh)
  411. {
  412. u8 version;
  413. u8 fsid_type;
  414. retry:
  415. version = 1;
  416. if (ref_fh && ref_fh->fh_export == exp) {
  417. version = ref_fh->fh_handle.fh_version;
  418. fsid_type = ref_fh->fh_handle.fh_fsid_type;
  419. ref_fh = NULL;
  420. switch (version) {
  421. case 0xca:
  422. fsid_type = FSID_DEV;
  423. break;
  424. case 1:
  425. break;
  426. default:
  427. goto retry;
  428. }
  429. /*
  430. * As the fsid -> filesystem mapping was guided by
  431. * user-space, there is no guarantee that the filesystem
  432. * actually supports that fsid type. If it doesn't we
  433. * loop around again without ref_fh set.
  434. */
  435. if (!fsid_type_ok_for_exp(fsid_type, exp))
  436. goto retry;
  437. } else if (exp->ex_flags & NFSEXP_FSID) {
  438. fsid_type = FSID_NUM;
  439. } else if (exp->ex_uuid) {
  440. if (fhp->fh_maxsize >= 64) {
  441. if (is_root_export(exp))
  442. fsid_type = FSID_UUID16;
  443. else
  444. fsid_type = FSID_UUID16_INUM;
  445. } else {
  446. if (is_root_export(exp))
  447. fsid_type = FSID_UUID8;
  448. else
  449. fsid_type = FSID_UUID4_INUM;
  450. }
  451. } else if (!old_valid_dev(exp_sb(exp)->s_dev))
  452. /* for newer device numbers, we must use a newer fsid format */
  453. fsid_type = FSID_ENCODE_DEV;
  454. else
  455. fsid_type = FSID_DEV;
  456. fhp->fh_handle.fh_version = version;
  457. if (version)
  458. fhp->fh_handle.fh_fsid_type = fsid_type;
  459. }
  460. __be32
  461. fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry,
  462. struct svc_fh *ref_fh)
  463. {
  464. /* ref_fh is a reference file handle.
  465. * if it is non-null and for the same filesystem, then we should compose
  466. * a filehandle which is of the same version, where possible.
  467. * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca
  468. * Then create a 32byte filehandle using nfs_fhbase_old
  469. *
  470. */
  471. struct inode * inode = dentry->d_inode;
  472. struct dentry *parent = dentry->d_parent;
  473. __u32 *datap;
  474. dev_t ex_dev = exp_sb(exp)->s_dev;
  475. dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %s/%s, ino=%ld)\n",
  476. MAJOR(ex_dev), MINOR(ex_dev),
  477. (long) exp->ex_path.dentry->d_inode->i_ino,
  478. parent->d_name.name, dentry->d_name.name,
  479. (inode ? inode->i_ino : 0));
  480. /* Choose filehandle version and fsid type based on
  481. * the reference filehandle (if it is in the same export)
  482. * or the export options.
  483. */
  484. set_version_and_fsid_type(fhp, exp, ref_fh);
  485. if (ref_fh == fhp)
  486. fh_put(ref_fh);
  487. if (fhp->fh_locked || fhp->fh_dentry) {
  488. printk(KERN_ERR "fh_compose: fh %s/%s not initialized!\n",
  489. parent->d_name.name, dentry->d_name.name);
  490. }
  491. if (fhp->fh_maxsize < NFS_FHSIZE)
  492. printk(KERN_ERR "fh_compose: called with maxsize %d! %s/%s\n",
  493. fhp->fh_maxsize,
  494. parent->d_name.name, dentry->d_name.name);
  495. fhp->fh_dentry = dget(dentry); /* our internal copy */
  496. fhp->fh_export = exp;
  497. cache_get(&exp->h);
  498. if (fhp->fh_handle.fh_version == 0xca) {
  499. /* old style filehandle please */
  500. memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE);
  501. fhp->fh_handle.fh_size = NFS_FHSIZE;
  502. fhp->fh_handle.ofh_dcookie = 0xfeebbaca;
  503. fhp->fh_handle.ofh_dev = old_encode_dev(ex_dev);
  504. fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev;
  505. fhp->fh_handle.ofh_xino =
  506. ino_t_to_u32(exp->ex_path.dentry->d_inode->i_ino);
  507. fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry));
  508. if (inode)
  509. _fh_update_old(dentry, exp, &fhp->fh_handle);
  510. } else {
  511. int len;
  512. fhp->fh_handle.fh_auth_type = 0;
  513. datap = fhp->fh_handle.fh_auth+0;
  514. mk_fsid(fhp->fh_handle.fh_fsid_type, datap, ex_dev,
  515. exp->ex_path.dentry->d_inode->i_ino,
  516. exp->ex_fsid, exp->ex_uuid);
  517. len = key_len(fhp->fh_handle.fh_fsid_type);
  518. datap += len/4;
  519. fhp->fh_handle.fh_size = 4 + len;
  520. if (inode)
  521. _fh_update(fhp, exp, dentry);
  522. if (fhp->fh_handle.fh_fileid_type == 255) {
  523. fh_put(fhp);
  524. return nfserr_opnotsupp;
  525. }
  526. }
  527. return 0;
  528. }
  529. /*
  530. * Update file handle information after changing a dentry.
  531. * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
  532. */
  533. __be32
  534. fh_update(struct svc_fh *fhp)
  535. {
  536. struct dentry *dentry;
  537. if (!fhp->fh_dentry)
  538. goto out_bad;
  539. dentry = fhp->fh_dentry;
  540. if (!dentry->d_inode)
  541. goto out_negative;
  542. if (fhp->fh_handle.fh_version != 1) {
  543. _fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle);
  544. } else {
  545. if (fhp->fh_handle.fh_fileid_type != FILEID_ROOT)
  546. goto out;
  547. _fh_update(fhp, fhp->fh_export, dentry);
  548. if (fhp->fh_handle.fh_fileid_type == 255)
  549. return nfserr_opnotsupp;
  550. }
  551. out:
  552. return 0;
  553. out_bad:
  554. printk(KERN_ERR "fh_update: fh not verified!\n");
  555. goto out;
  556. out_negative:
  557. printk(KERN_ERR "fh_update: %s/%s still negative!\n",
  558. dentry->d_parent->d_name.name, dentry->d_name.name);
  559. goto out;
  560. }
  561. /*
  562. * Release a file handle.
  563. */
  564. void
  565. fh_put(struct svc_fh *fhp)
  566. {
  567. struct dentry * dentry = fhp->fh_dentry;
  568. struct svc_export * exp = fhp->fh_export;
  569. if (dentry) {
  570. fh_unlock(fhp);
  571. fhp->fh_dentry = NULL;
  572. dput(dentry);
  573. #ifdef CONFIG_NFSD_V3
  574. fhp->fh_pre_saved = 0;
  575. fhp->fh_post_saved = 0;
  576. #endif
  577. }
  578. if (exp) {
  579. cache_put(&exp->h, &svc_export_cache);
  580. fhp->fh_export = NULL;
  581. }
  582. return;
  583. }
  584. /*
  585. * Shorthand for dprintk()'s
  586. */
  587. char * SVCFH_fmt(struct svc_fh *fhp)
  588. {
  589. struct knfsd_fh *fh = &fhp->fh_handle;
  590. static char buf[80];
  591. sprintf(buf, "%d: %08x %08x %08x %08x %08x %08x",
  592. fh->fh_size,
  593. fh->fh_base.fh_pad[0],
  594. fh->fh_base.fh_pad[1],
  595. fh->fh_base.fh_pad[2],
  596. fh->fh_base.fh_pad[3],
  597. fh->fh_base.fh_pad[4],
  598. fh->fh_base.fh_pad[5]);
  599. return buf;
  600. }
  601. enum fsid_source fsid_source(struct svc_fh *fhp)
  602. {
  603. if (fhp->fh_handle.fh_version != 1)
  604. return FSIDSOURCE_DEV;
  605. switch(fhp->fh_handle.fh_fsid_type) {
  606. case FSID_DEV:
  607. case FSID_ENCODE_DEV:
  608. case FSID_MAJOR_MINOR:
  609. if (exp_sb(fhp->fh_export)->s_type->fs_flags & FS_REQUIRES_DEV)
  610. return FSIDSOURCE_DEV;
  611. break;
  612. case FSID_NUM:
  613. if (fhp->fh_export->ex_flags & NFSEXP_FSID)
  614. return FSIDSOURCE_FSID;
  615. break;
  616. default:
  617. break;
  618. }
  619. /* either a UUID type filehandle, or the filehandle doesn't
  620. * match the export.
  621. */
  622. if (fhp->fh_export->ex_flags & NFSEXP_FSID)
  623. return FSIDSOURCE_FSID;
  624. if (fhp->fh_export->ex_uuid)
  625. return FSIDSOURCE_UUID;
  626. return FSIDSOURCE_DEV;
  627. }