nfsfh.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  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. #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(&nop_mnt_idmap,
  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 dentry *dentry, umode_t requested)
  58. {
  59. umode_t mode = d_inode(dentry)->i_mode & S_IFMT;
  60. if (requested == 0) /* the caller doesn't care */
  61. return nfs_ok;
  62. if (mode == requested) {
  63. if (mode == S_IFDIR && !d_can_lookup(dentry)) {
  64. WARN_ON_ONCE(1);
  65. return nfserr_notdir;
  66. }
  67. return nfs_ok;
  68. }
  69. if (mode == S_IFLNK) {
  70. if (requested == S_IFDIR)
  71. return nfserr_symlink_not_dir;
  72. return nfserr_symlink;
  73. }
  74. if (requested == S_IFDIR)
  75. return nfserr_notdir;
  76. if (mode == S_IFDIR)
  77. return nfserr_isdir;
  78. return nfserr_wrong_type;
  79. }
  80. static bool nfsd_originating_port_ok(struct svc_rqst *rqstp,
  81. struct svc_cred *cred,
  82. struct svc_export *exp)
  83. {
  84. if (nfsexp_flags(cred, exp) & NFSEXP_INSECURE_PORT)
  85. return true;
  86. /* We don't require gss requests to use low ports: */
  87. if (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_cred *cred,
  93. struct svc_export *exp)
  94. {
  95. /* Check if the request originated from a secure port. */
  96. if (rqstp && !nfsd_originating_port_ok(rqstp, cred, exp)) {
  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(cred, exp));
  104. }
  105. static inline __be32 check_pseudo_root(struct dentry *dentry,
  106. struct svc_export *exp)
  107. {
  108. if (!(exp->ex_flags & NFSEXP_V4ROOT))
  109. return nfs_ok;
  110. /*
  111. * We're exposing only the directories and symlinks that have to be
  112. * traversed on the way to real exports:
  113. */
  114. if (unlikely(!d_is_dir(dentry) &&
  115. !d_is_symlink(dentry)))
  116. return nfserr_stale;
  117. /*
  118. * A pseudoroot export gives permission to access only one
  119. * single directory; the kernel has to make another upcall
  120. * before granting access to anything else under it:
  121. */
  122. if (unlikely(dentry != exp->ex_path.dentry))
  123. return nfserr_stale;
  124. return nfs_ok;
  125. }
  126. /*
  127. * Use the given filehandle to look up the corresponding export and
  128. * dentry. On success, the results are used to set fh_export and
  129. * fh_dentry.
  130. */
  131. static __be32 nfsd_set_fh_dentry(struct svc_rqst *rqstp, struct net *net,
  132. struct svc_cred *cred,
  133. struct auth_domain *client,
  134. struct auth_domain *gssclient,
  135. struct svc_fh *fhp)
  136. {
  137. struct knfsd_fh *fh = &fhp->fh_handle;
  138. struct fid *fid = NULL;
  139. struct svc_export *exp;
  140. struct dentry *dentry;
  141. int fileid_type;
  142. int data_left = fh->fh_size/4;
  143. int len;
  144. __be32 error;
  145. error = nfserr_badhandle;
  146. if (fh->fh_size == 0)
  147. return nfserr_nofilehandle;
  148. if (fh->fh_version != 1)
  149. return error;
  150. if (--data_left < 0)
  151. return error;
  152. if (fh->fh_auth_type != 0)
  153. return error;
  154. len = key_len(fh->fh_fsid_type) / 4;
  155. if (len == 0)
  156. return error;
  157. if (fh->fh_fsid_type == FSID_MAJOR_MINOR) {
  158. /* deprecated, convert to type 3 */
  159. len = key_len(FSID_ENCODE_DEV)/4;
  160. fh->fh_fsid_type = FSID_ENCODE_DEV;
  161. /*
  162. * struct knfsd_fh uses host-endian fields, which are
  163. * sometimes used to hold net-endian values. This
  164. * confuses sparse, so we must use __force here to
  165. * keep it from complaining.
  166. */
  167. fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl((__force __be32)fh->fh_fsid[0]),
  168. ntohl((__force __be32)fh->fh_fsid[1])));
  169. fh->fh_fsid[1] = fh->fh_fsid[2];
  170. }
  171. data_left -= len;
  172. if (data_left < 0)
  173. return error;
  174. exp = rqst_exp_find(rqstp ? &rqstp->rq_chandle : NULL,
  175. net, client, gssclient,
  176. fh->fh_fsid_type, fh->fh_fsid);
  177. fid = (struct fid *)(fh->fh_fsid + len);
  178. error = nfserr_stale;
  179. if (IS_ERR(exp)) {
  180. trace_nfsd_set_fh_dentry_badexport(rqstp, fhp, PTR_ERR(exp));
  181. if (PTR_ERR(exp) == -ENOENT)
  182. return error;
  183. return nfserrno(PTR_ERR(exp));
  184. }
  185. if (exp->ex_flags & NFSEXP_NOSUBTREECHECK) {
  186. /* Elevate privileges so that the lack of 'r' or 'x'
  187. * permission on some parent directory will
  188. * not stop exportfs_decode_fh from being able
  189. * to reconnect a directory into the dentry cache.
  190. * The same problem can affect "SUBTREECHECK" exports,
  191. * but as nfsd_acceptable depends on correct
  192. * access control settings being in effect, we cannot
  193. * fix that case easily.
  194. */
  195. struct cred *new = prepare_creds();
  196. if (!new) {
  197. error = nfserrno(-ENOMEM);
  198. goto out;
  199. }
  200. new->cap_effective =
  201. cap_raise_nfsd_set(new->cap_effective,
  202. new->cap_permitted);
  203. put_cred(override_creds(new));
  204. put_cred(new);
  205. } else {
  206. error = nfsd_setuser_and_check_port(rqstp, cred, exp);
  207. if (error)
  208. goto out;
  209. }
  210. /*
  211. * Look up the dentry using the NFS file handle.
  212. */
  213. error = nfserr_badhandle;
  214. fileid_type = fh->fh_fileid_type;
  215. if (fileid_type == FILEID_ROOT)
  216. dentry = dget(exp->ex_path.dentry);
  217. else {
  218. dentry = exportfs_decode_fh_raw(exp->ex_path.mnt, fid,
  219. data_left, fileid_type, 0,
  220. nfsd_acceptable, exp);
  221. if (IS_ERR_OR_NULL(dentry)) {
  222. trace_nfsd_set_fh_dentry_badhandle(rqstp, fhp,
  223. dentry ? PTR_ERR(dentry) : -ESTALE);
  224. switch (PTR_ERR(dentry)) {
  225. case -ENOMEM:
  226. case -ETIMEDOUT:
  227. break;
  228. default:
  229. dentry = ERR_PTR(-ESTALE);
  230. }
  231. }
  232. }
  233. if (dentry == NULL)
  234. goto out;
  235. if (IS_ERR(dentry)) {
  236. if (PTR_ERR(dentry) != -EINVAL)
  237. error = nfserrno(PTR_ERR(dentry));
  238. goto out;
  239. }
  240. if (d_is_dir(dentry) &&
  241. (dentry->d_flags & DCACHE_DISCONNECTED)) {
  242. printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %pd2\n",
  243. dentry);
  244. }
  245. fhp->fh_dentry = dentry;
  246. fhp->fh_export = exp;
  247. switch (fhp->fh_maxsize) {
  248. case NFS4_FHSIZE:
  249. if (dentry->d_sb->s_export_op->flags & EXPORT_OP_NOATOMIC_ATTR)
  250. fhp->fh_no_atomic_attr = true;
  251. fhp->fh_64bit_cookies = true;
  252. break;
  253. case NFS3_FHSIZE:
  254. if (dentry->d_sb->s_export_op->flags & EXPORT_OP_NOWCC)
  255. fhp->fh_no_wcc = true;
  256. fhp->fh_64bit_cookies = true;
  257. if (exp->ex_flags & NFSEXP_V4ROOT)
  258. goto out;
  259. break;
  260. case NFS_FHSIZE:
  261. fhp->fh_no_wcc = true;
  262. if (EX_WGATHER(exp))
  263. fhp->fh_use_wgather = true;
  264. if (exp->ex_flags & NFSEXP_V4ROOT)
  265. goto out;
  266. }
  267. return 0;
  268. out:
  269. exp_put(exp);
  270. return error;
  271. }
  272. /**
  273. * __fh_verify - filehandle lookup and access checking
  274. * @rqstp: RPC transaction context, or NULL
  275. * @net: net namespace in which to perform the export lookup
  276. * @cred: RPC user credential
  277. * @client: RPC auth domain
  278. * @gssclient: RPC GSS auth domain, or NULL
  279. * @fhp: filehandle to be verified
  280. * @type: expected type of object pointed to by filehandle
  281. * @access: type of access needed to object
  282. *
  283. * See fh_verify() for further descriptions of @fhp, @type, and @access.
  284. */
  285. static __be32
  286. __fh_verify(struct svc_rqst *rqstp,
  287. struct net *net, struct svc_cred *cred,
  288. struct auth_domain *client,
  289. struct auth_domain *gssclient,
  290. struct svc_fh *fhp, umode_t type, int access)
  291. {
  292. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  293. struct svc_export *exp = NULL;
  294. bool may_bypass_gss = false;
  295. struct dentry *dentry;
  296. __be32 error;
  297. if (!fhp->fh_dentry) {
  298. error = nfsd_set_fh_dentry(rqstp, net, cred, client,
  299. gssclient, fhp);
  300. if (error)
  301. goto out;
  302. }
  303. dentry = fhp->fh_dentry;
  304. exp = fhp->fh_export;
  305. trace_nfsd_fh_verify(rqstp, fhp, type, access);
  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(dentry, exp);
  323. if (error)
  324. goto out;
  325. error = nfsd_setuser_and_check_port(rqstp, cred, exp);
  326. if (error)
  327. goto out;
  328. error = nfsd_mode_check(dentry, type);
  329. if (error)
  330. goto out;
  331. /*
  332. * If rqstp is NULL, this is a LOCALIO request which will only
  333. * ever use a filehandle/credential pair for which access has
  334. * been affirmed (by ACCESS or OPEN NFS requests) over the
  335. * wire. Skip both the xprtsec policy and the security flavor
  336. * checks.
  337. */
  338. if (!rqstp)
  339. goto check_permissions;
  340. if ((access & NFSD_MAY_NLM) && (exp->ex_flags & NFSEXP_NOAUTHNLM))
  341. /* NLM is allowed to fully bypass authentication */
  342. goto out;
  343. /*
  344. * NLM is allowed to bypass the xprtsec policy check because lockd
  345. * doesn't support xprtsec.
  346. */
  347. if (!(access & NFSD_MAY_NLM)) {
  348. error = check_xprtsec_policy(exp, rqstp);
  349. if (error)
  350. goto out;
  351. }
  352. if (access & NFSD_MAY_BYPASS_GSS)
  353. may_bypass_gss = true;
  354. /*
  355. * Clients may expect to be able to use auth_sys during mount,
  356. * even if they use gss for everything else; see section 2.3.2
  357. * of rfc 2623.
  358. */
  359. if (access & NFSD_MAY_BYPASS_GSS_ON_ROOT
  360. && exp->ex_path.dentry == dentry)
  361. may_bypass_gss = true;
  362. error = check_security_flavor(exp, rqstp, may_bypass_gss);
  363. if (error)
  364. goto out;
  365. /* During LOCALIO call to fh_verify will be called with a NULL rqstp */
  366. if (rqstp)
  367. svc_xprt_set_valid(rqstp->rq_xprt);
  368. check_permissions:
  369. /* Finally, check access permissions. */
  370. error = nfsd_permission(cred, exp, dentry, access);
  371. out:
  372. trace_nfsd_fh_verify_err(rqstp, fhp, type, access, error);
  373. if (error == nfserr_stale)
  374. nfsd_stats_fh_stale_inc(nn, exp);
  375. return error;
  376. }
  377. /**
  378. * fh_verify_local - filehandle lookup and access checking
  379. * @net: net namespace in which to perform the export lookup
  380. * @cred: RPC user credential
  381. * @client: RPC auth domain
  382. * @fhp: filehandle to be verified
  383. * @type: expected type of object pointed to by filehandle
  384. * @access: type of access needed to object
  385. *
  386. * This API can be used by callers who do not have an RPC
  387. * transaction context (ie are not running in an nfsd thread).
  388. *
  389. * See fh_verify() for further descriptions of @fhp, @type, and @access.
  390. */
  391. __be32
  392. fh_verify_local(struct net *net, struct svc_cred *cred,
  393. struct auth_domain *client, struct svc_fh *fhp,
  394. umode_t type, int access)
  395. {
  396. return __fh_verify(NULL, net, cred, client, NULL,
  397. fhp, type, access);
  398. }
  399. /**
  400. * fh_verify - filehandle lookup and access checking
  401. * @rqstp: pointer to current rpc request
  402. * @fhp: filehandle to be verified
  403. * @type: expected type of object pointed to by filehandle
  404. * @access: type of access needed to object
  405. *
  406. * Look up a dentry from the on-the-wire filehandle, check the client's
  407. * access to the export, and set the current task's credentials.
  408. *
  409. * Regardless of success or failure of fh_verify(), fh_put() should be
  410. * called on @fhp when the caller is finished with the filehandle.
  411. *
  412. * fh_verify() may be called multiple times on a given filehandle, for
  413. * example, when processing an NFSv4 compound. The first call will look
  414. * up a dentry using the on-the-wire filehandle. Subsequent calls will
  415. * skip the lookup and just perform the other checks and possibly change
  416. * the current task's credentials.
  417. *
  418. * @type specifies the type of object expected using one of the S_IF*
  419. * constants defined in include/linux/stat.h. The caller may use zero
  420. * to indicate that it doesn't care, or a negative integer to indicate
  421. * that it expects something not of the given type.
  422. *
  423. * @access is formed from the NFSD_MAY_* constants defined in
  424. * fs/nfsd/vfs.h.
  425. */
  426. __be32
  427. fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type, int access)
  428. {
  429. return __fh_verify(rqstp, SVC_NET(rqstp), &rqstp->rq_cred,
  430. rqstp->rq_client, rqstp->rq_gssclient,
  431. fhp, type, access);
  432. }
  433. /*
  434. * Compose a file handle for an NFS reply.
  435. *
  436. * Note that when first composed, the dentry may not yet have
  437. * an inode. In this case a call to fh_update should be made
  438. * before the fh goes out on the wire ...
  439. */
  440. static void _fh_update(struct svc_fh *fhp, struct svc_export *exp,
  441. struct dentry *dentry)
  442. {
  443. if (dentry != exp->ex_path.dentry) {
  444. struct fid *fid = (struct fid *)
  445. (fhp->fh_handle.fh_fsid + fhp->fh_handle.fh_size/4 - 1);
  446. int maxsize = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
  447. int fh_flags = (exp->ex_flags & NFSEXP_NOSUBTREECHECK) ? 0 :
  448. EXPORT_FH_CONNECTABLE;
  449. int fileid_type =
  450. exportfs_encode_fh(dentry, fid, &maxsize, fh_flags);
  451. fhp->fh_handle.fh_fileid_type =
  452. fileid_type > 0 ? fileid_type : FILEID_INVALID;
  453. fhp->fh_handle.fh_size += maxsize * 4;
  454. } else {
  455. fhp->fh_handle.fh_fileid_type = FILEID_ROOT;
  456. }
  457. }
  458. static bool is_root_export(struct svc_export *exp)
  459. {
  460. return exp->ex_path.dentry == exp->ex_path.dentry->d_sb->s_root;
  461. }
  462. static struct super_block *exp_sb(struct svc_export *exp)
  463. {
  464. return exp->ex_path.dentry->d_sb;
  465. }
  466. static bool fsid_type_ok_for_exp(u8 fsid_type, struct svc_export *exp)
  467. {
  468. switch (fsid_type) {
  469. case FSID_DEV:
  470. if (!old_valid_dev(exp_sb(exp)->s_dev))
  471. return false;
  472. fallthrough;
  473. case FSID_MAJOR_MINOR:
  474. case FSID_ENCODE_DEV:
  475. return exp_sb(exp)->s_type->fs_flags & FS_REQUIRES_DEV;
  476. case FSID_NUM:
  477. return exp->ex_flags & NFSEXP_FSID;
  478. case FSID_UUID8:
  479. case FSID_UUID16:
  480. if (!is_root_export(exp))
  481. return false;
  482. fallthrough;
  483. case FSID_UUID4_INUM:
  484. case FSID_UUID16_INUM:
  485. return exp->ex_uuid != NULL;
  486. }
  487. return true;
  488. }
  489. static void set_version_and_fsid_type(struct svc_fh *fhp, struct svc_export *exp, struct svc_fh *ref_fh)
  490. {
  491. u8 version;
  492. u8 fsid_type;
  493. retry:
  494. version = 1;
  495. if (ref_fh && ref_fh->fh_export == exp) {
  496. version = ref_fh->fh_handle.fh_version;
  497. fsid_type = ref_fh->fh_handle.fh_fsid_type;
  498. ref_fh = NULL;
  499. switch (version) {
  500. case 0xca:
  501. fsid_type = FSID_DEV;
  502. break;
  503. case 1:
  504. break;
  505. default:
  506. goto retry;
  507. }
  508. /*
  509. * As the fsid -> filesystem mapping was guided by
  510. * user-space, there is no guarantee that the filesystem
  511. * actually supports that fsid type. If it doesn't we
  512. * loop around again without ref_fh set.
  513. */
  514. if (!fsid_type_ok_for_exp(fsid_type, exp))
  515. goto retry;
  516. } else if (exp->ex_flags & NFSEXP_FSID) {
  517. fsid_type = FSID_NUM;
  518. } else if (exp->ex_uuid) {
  519. if (fhp->fh_maxsize >= 64) {
  520. if (is_root_export(exp))
  521. fsid_type = FSID_UUID16;
  522. else
  523. fsid_type = FSID_UUID16_INUM;
  524. } else {
  525. if (is_root_export(exp))
  526. fsid_type = FSID_UUID8;
  527. else
  528. fsid_type = FSID_UUID4_INUM;
  529. }
  530. } else if (!old_valid_dev(exp_sb(exp)->s_dev))
  531. /* for newer device numbers, we must use a newer fsid format */
  532. fsid_type = FSID_ENCODE_DEV;
  533. else
  534. fsid_type = FSID_DEV;
  535. fhp->fh_handle.fh_version = version;
  536. if (version)
  537. fhp->fh_handle.fh_fsid_type = fsid_type;
  538. }
  539. __be32
  540. fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry,
  541. struct svc_fh *ref_fh)
  542. {
  543. /* ref_fh is a reference file handle.
  544. * if it is non-null and for the same filesystem, then we should compose
  545. * a filehandle which is of the same version, where possible.
  546. */
  547. struct inode * inode = d_inode(dentry);
  548. dev_t ex_dev = exp_sb(exp)->s_dev;
  549. dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %pd2, ino=%ld)\n",
  550. MAJOR(ex_dev), MINOR(ex_dev),
  551. (long) d_inode(exp->ex_path.dentry)->i_ino,
  552. dentry,
  553. (inode ? inode->i_ino : 0));
  554. /* Choose filehandle version and fsid type based on
  555. * the reference filehandle (if it is in the same export)
  556. * or the export options.
  557. */
  558. set_version_and_fsid_type(fhp, exp, ref_fh);
  559. /* If we have a ref_fh, then copy the fh_no_wcc setting from it. */
  560. fhp->fh_no_wcc = ref_fh ? ref_fh->fh_no_wcc : false;
  561. if (ref_fh == fhp)
  562. fh_put(ref_fh);
  563. if (fhp->fh_dentry) {
  564. printk(KERN_ERR "fh_compose: fh %pd2 not initialized!\n",
  565. dentry);
  566. }
  567. if (fhp->fh_maxsize < NFS_FHSIZE)
  568. printk(KERN_ERR "fh_compose: called with maxsize %d! %pd2\n",
  569. fhp->fh_maxsize,
  570. dentry);
  571. fhp->fh_dentry = dget(dentry); /* our internal copy */
  572. fhp->fh_export = exp_get(exp);
  573. fhp->fh_handle.fh_size =
  574. key_len(fhp->fh_handle.fh_fsid_type) + 4;
  575. fhp->fh_handle.fh_auth_type = 0;
  576. mk_fsid(fhp->fh_handle.fh_fsid_type,
  577. fhp->fh_handle.fh_fsid,
  578. ex_dev,
  579. d_inode(exp->ex_path.dentry)->i_ino,
  580. exp->ex_fsid, exp->ex_uuid);
  581. if (inode)
  582. _fh_update(fhp, exp, dentry);
  583. if (fhp->fh_handle.fh_fileid_type == FILEID_INVALID) {
  584. fh_put(fhp);
  585. return nfserr_stale;
  586. }
  587. return 0;
  588. }
  589. /*
  590. * Update file handle information after changing a dentry.
  591. * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
  592. */
  593. __be32
  594. fh_update(struct svc_fh *fhp)
  595. {
  596. struct dentry *dentry;
  597. if (!fhp->fh_dentry)
  598. goto out_bad;
  599. dentry = fhp->fh_dentry;
  600. if (d_really_is_negative(dentry))
  601. goto out_negative;
  602. if (fhp->fh_handle.fh_fileid_type != FILEID_ROOT)
  603. return 0;
  604. _fh_update(fhp, fhp->fh_export, dentry);
  605. if (fhp->fh_handle.fh_fileid_type == FILEID_INVALID)
  606. return nfserr_stale;
  607. return 0;
  608. out_bad:
  609. printk(KERN_ERR "fh_update: fh not verified!\n");
  610. return nfserr_serverfault;
  611. out_negative:
  612. printk(KERN_ERR "fh_update: %pd2 still negative!\n",
  613. dentry);
  614. return nfserr_serverfault;
  615. }
  616. /**
  617. * fh_fill_pre_attrs - Fill in pre-op attributes
  618. * @fhp: file handle to be updated
  619. *
  620. */
  621. __be32 __must_check fh_fill_pre_attrs(struct svc_fh *fhp)
  622. {
  623. bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
  624. struct kstat stat;
  625. __be32 err;
  626. if (fhp->fh_no_wcc || fhp->fh_pre_saved)
  627. return nfs_ok;
  628. err = fh_getattr(fhp, &stat);
  629. if (err)
  630. return err;
  631. if (v4)
  632. fhp->fh_pre_change = nfsd4_change_attribute(&stat);
  633. fhp->fh_pre_mtime = stat.mtime;
  634. fhp->fh_pre_ctime = stat.ctime;
  635. fhp->fh_pre_size = stat.size;
  636. fhp->fh_pre_saved = true;
  637. return nfs_ok;
  638. }
  639. /**
  640. * fh_fill_post_attrs - Fill in post-op attributes
  641. * @fhp: file handle to be updated
  642. *
  643. */
  644. __be32 fh_fill_post_attrs(struct svc_fh *fhp)
  645. {
  646. bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
  647. __be32 err;
  648. if (fhp->fh_no_wcc)
  649. return nfs_ok;
  650. if (fhp->fh_post_saved)
  651. printk("nfsd: inode locked twice during operation.\n");
  652. err = fh_getattr(fhp, &fhp->fh_post_attr);
  653. if (err)
  654. return err;
  655. fhp->fh_post_saved = true;
  656. if (v4)
  657. fhp->fh_post_change =
  658. nfsd4_change_attribute(&fhp->fh_post_attr);
  659. return nfs_ok;
  660. }
  661. /**
  662. * fh_fill_both_attrs - Fill pre-op and post-op attributes
  663. * @fhp: file handle to be updated
  664. *
  665. * This is used when the directory wasn't changed, but wcc attributes
  666. * are needed anyway.
  667. */
  668. __be32 __must_check fh_fill_both_attrs(struct svc_fh *fhp)
  669. {
  670. __be32 err;
  671. err = fh_fill_post_attrs(fhp);
  672. if (err)
  673. return err;
  674. fhp->fh_pre_change = fhp->fh_post_change;
  675. fhp->fh_pre_mtime = fhp->fh_post_attr.mtime;
  676. fhp->fh_pre_ctime = fhp->fh_post_attr.ctime;
  677. fhp->fh_pre_size = fhp->fh_post_attr.size;
  678. fhp->fh_pre_saved = true;
  679. return nfs_ok;
  680. }
  681. /*
  682. * Release a file handle.
  683. */
  684. void
  685. fh_put(struct svc_fh *fhp)
  686. {
  687. struct dentry * dentry = fhp->fh_dentry;
  688. struct svc_export * exp = fhp->fh_export;
  689. if (dentry) {
  690. fhp->fh_dentry = NULL;
  691. dput(dentry);
  692. fh_clear_pre_post_attrs(fhp);
  693. }
  694. fh_drop_write(fhp);
  695. if (exp) {
  696. exp_put(exp);
  697. fhp->fh_export = NULL;
  698. }
  699. fhp->fh_no_wcc = false;
  700. return;
  701. }
  702. /*
  703. * Shorthand for dprintk()'s
  704. */
  705. char * SVCFH_fmt(struct svc_fh *fhp)
  706. {
  707. struct knfsd_fh *fh = &fhp->fh_handle;
  708. static char buf[2+1+1+64*3+1];
  709. if (fh->fh_size < 0 || fh->fh_size> 64)
  710. return "bad-fh";
  711. sprintf(buf, "%d: %*ph", fh->fh_size, fh->fh_size, fh->fh_raw);
  712. return buf;
  713. }
  714. enum fsid_source fsid_source(const struct svc_fh *fhp)
  715. {
  716. if (fhp->fh_handle.fh_version != 1)
  717. return FSIDSOURCE_DEV;
  718. switch(fhp->fh_handle.fh_fsid_type) {
  719. case FSID_DEV:
  720. case FSID_ENCODE_DEV:
  721. case FSID_MAJOR_MINOR:
  722. if (exp_sb(fhp->fh_export)->s_type->fs_flags & FS_REQUIRES_DEV)
  723. return FSIDSOURCE_DEV;
  724. break;
  725. case FSID_NUM:
  726. if (fhp->fh_export->ex_flags & NFSEXP_FSID)
  727. return FSIDSOURCE_FSID;
  728. break;
  729. default:
  730. break;
  731. }
  732. /* either a UUID type filehandle, or the filehandle doesn't
  733. * match the export.
  734. */
  735. if (fhp->fh_export->ex_flags & NFSEXP_FSID)
  736. return FSIDSOURCE_FSID;
  737. if (fhp->fh_export->ex_uuid)
  738. return FSIDSOURCE_UUID;
  739. return FSIDSOURCE_DEV;
  740. }
  741. /**
  742. * nfsd4_change_attribute - Generate an NFSv4 change_attribute value
  743. * @stat: inode attributes
  744. *
  745. * Caller must fill in @stat before calling, typically by invoking
  746. * vfs_getattr() with STATX_MODE, STATX_CTIME, and STATX_CHANGE_COOKIE.
  747. * Returns an unsigned 64-bit changeid4 value (RFC 8881 Section 3.2).
  748. *
  749. * We could use i_version alone as the change attribute. However, i_version
  750. * can go backwards on a regular file after an unclean shutdown. On its own
  751. * that doesn't necessarily cause a problem, but if i_version goes backwards
  752. * and then is incremented again it could reuse a value that was previously
  753. * used before boot, and a client who queried the two values might incorrectly
  754. * assume nothing changed.
  755. *
  756. * By using both ctime and the i_version counter we guarantee that as long as
  757. * time doesn't go backwards we never reuse an old value. If the filesystem
  758. * advertises STATX_ATTR_CHANGE_MONOTONIC, then this mitigation is not
  759. * needed.
  760. *
  761. * We only need to do this for regular files as well. For directories, we
  762. * assume that the new change attr is always logged to stable storage in some
  763. * fashion before the results can be seen.
  764. */
  765. u64 nfsd4_change_attribute(const struct kstat *stat)
  766. {
  767. u64 chattr;
  768. if (stat->result_mask & STATX_CHANGE_COOKIE) {
  769. chattr = stat->change_cookie;
  770. if (S_ISREG(stat->mode) &&
  771. !(stat->attributes & STATX_ATTR_CHANGE_MONOTONIC)) {
  772. chattr += (u64)stat->ctime.tv_sec << 30;
  773. chattr += stat->ctime.tv_nsec;
  774. }
  775. } else {
  776. chattr = time_to_chattr(&stat->ctime);
  777. }
  778. return chattr;
  779. }