nfsfh.c 18 KB

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