nfsfh.c 19 KB

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