localio.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * NFS server support for local clients to bypass network stack
  4. *
  5. * Copyright (C) 2014 Weston Andros Adamson <dros@primarydata.com>
  6. * Copyright (C) 2019 Trond Myklebust <trond.myklebust@hammerspace.com>
  7. * Copyright (C) 2024 Mike Snitzer <snitzer@hammerspace.com>
  8. * Copyright (C) 2024 NeilBrown <neilb@suse.de>
  9. */
  10. #include <linux/exportfs.h>
  11. #include <linux/sunrpc/svcauth.h>
  12. #include <linux/sunrpc/clnt.h>
  13. #include <linux/nfs.h>
  14. #include <linux/nfs_common.h>
  15. #include <linux/nfslocalio.h>
  16. #include <linux/nfs_fs.h>
  17. #include <linux/nfs_xdr.h>
  18. #include <linux/string.h>
  19. #include "nfsd.h"
  20. #include "vfs.h"
  21. #include "netns.h"
  22. #include "filecache.h"
  23. #include "cache.h"
  24. /**
  25. * nfsd_open_local_fh - lookup a local filehandle @nfs_fh and map to nfsd_file
  26. *
  27. * @net: 'struct net' to get the proper nfsd_net required for LOCALIO access
  28. * @dom: 'struct auth_domain' required for LOCALIO access
  29. * @rpc_clnt: rpc_clnt that the client established
  30. * @cred: cred that the client established
  31. * @nfs_fh: filehandle to lookup
  32. * @pnf: place to find the nfsd_file, or store it if it was non-NULL
  33. * @fmode: fmode_t to use for open
  34. *
  35. * This function maps a local fh to a path on a local filesystem.
  36. * This is useful when the nfs client has the local server mounted - it can
  37. * avoid all the NFS overhead with reads, writes and commits.
  38. *
  39. * On successful return, returned nfsd_file will have its nf_net member
  40. * set. Caller (NFS client) is responsible for calling nfsd_net_put and
  41. * nfsd_file_put (via nfs_to_nfsd_file_put_local).
  42. */
  43. static struct nfsd_file *
  44. nfsd_open_local_fh(struct net *net, struct auth_domain *dom,
  45. struct rpc_clnt *rpc_clnt, const struct cred *cred,
  46. const struct nfs_fh *nfs_fh, struct nfsd_file __rcu **pnf,
  47. const fmode_t fmode)
  48. {
  49. int mayflags = NFSD_MAY_LOCALIO;
  50. struct svc_cred rq_cred;
  51. struct svc_fh fh;
  52. struct nfsd_file *localio;
  53. __be32 beres;
  54. if (nfs_fh->size > NFS4_FHSIZE)
  55. return ERR_PTR(-EINVAL);
  56. if (!nfsd_net_try_get(net))
  57. return ERR_PTR(-ENXIO);
  58. rcu_read_lock();
  59. localio = nfsd_file_get(rcu_dereference(*pnf));
  60. rcu_read_unlock();
  61. if (localio)
  62. return localio;
  63. /* nfs_fh -> svc_fh */
  64. fh_init(&fh, NFS4_FHSIZE);
  65. fh.fh_handle.fh_size = nfs_fh->size;
  66. memcpy(fh.fh_handle.fh_raw, nfs_fh->data, nfs_fh->size);
  67. if (fmode & FMODE_READ)
  68. mayflags |= NFSD_MAY_READ;
  69. if (fmode & FMODE_WRITE)
  70. mayflags |= NFSD_MAY_WRITE;
  71. svcauth_map_clnt_to_svc_cred_local(rpc_clnt, cred, &rq_cred);
  72. beres = nfsd_file_acquire_local(net, &rq_cred, dom,
  73. &fh, mayflags, &localio);
  74. if (beres)
  75. localio = ERR_PTR(nfs_stat_to_errno(be32_to_cpu(beres)));
  76. fh_put(&fh);
  77. if (rq_cred.cr_group_info)
  78. put_group_info(rq_cred.cr_group_info);
  79. if (!IS_ERR(localio)) {
  80. struct nfsd_file *new;
  81. if (!nfsd_net_try_get(net)) {
  82. nfsd_file_put(localio);
  83. nfsd_net_put(net);
  84. return ERR_PTR(-ENXIO);
  85. }
  86. nfsd_file_get(localio);
  87. again:
  88. new = unrcu_pointer(cmpxchg(pnf, NULL, RCU_INITIALIZER(localio)));
  89. if (new) {
  90. /* Some other thread installed an nfsd_file */
  91. if (nfsd_file_get(new) == NULL)
  92. goto again;
  93. /*
  94. * Drop the ref we were going to install (both file and
  95. * net) and the one we were going to return (only file).
  96. */
  97. nfsd_file_put(localio);
  98. nfsd_net_put(net);
  99. nfsd_file_put(localio);
  100. localio = new;
  101. }
  102. } else
  103. nfsd_net_put(net);
  104. return localio;
  105. }
  106. static void nfsd_file_dio_alignment(struct nfsd_file *nf,
  107. u32 *nf_dio_mem_align,
  108. u32 *nf_dio_offset_align,
  109. u32 *nf_dio_read_offset_align)
  110. {
  111. *nf_dio_mem_align = nf->nf_dio_mem_align;
  112. *nf_dio_offset_align = nf->nf_dio_offset_align;
  113. *nf_dio_read_offset_align = nf->nf_dio_read_offset_align;
  114. }
  115. static const struct nfsd_localio_operations nfsd_localio_ops = {
  116. .nfsd_net_try_get = nfsd_net_try_get,
  117. .nfsd_net_put = nfsd_net_put,
  118. .nfsd_open_local_fh = nfsd_open_local_fh,
  119. .nfsd_file_put_local = nfsd_file_put_local,
  120. .nfsd_file_file = nfsd_file_file,
  121. .nfsd_file_dio_alignment = nfsd_file_dio_alignment,
  122. };
  123. void nfsd_localio_ops_init(void)
  124. {
  125. nfs_to = &nfsd_localio_ops;
  126. }
  127. /*
  128. * UUID_IS_LOCAL XDR functions
  129. */
  130. static __be32 localio_proc_null(struct svc_rqst *rqstp)
  131. {
  132. return rpc_success;
  133. }
  134. struct localio_uuidarg {
  135. uuid_t uuid;
  136. };
  137. static __be32 localio_proc_uuid_is_local(struct svc_rqst *rqstp)
  138. {
  139. struct localio_uuidarg *argp = rqstp->rq_argp;
  140. struct net *net = SVC_NET(rqstp);
  141. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  142. nfs_uuid_is_local(&argp->uuid, &nn->local_clients,
  143. &nn->local_clients_lock,
  144. net, rqstp->rq_client, THIS_MODULE);
  145. return rpc_success;
  146. }
  147. static bool localio_decode_uuidarg(struct svc_rqst *rqstp,
  148. struct xdr_stream *xdr)
  149. {
  150. struct localio_uuidarg *argp = rqstp->rq_argp;
  151. u8 uuid[UUID_SIZE];
  152. if (decode_opaque_fixed(xdr, uuid, UUID_SIZE))
  153. return false;
  154. import_uuid(&argp->uuid, uuid);
  155. return true;
  156. }
  157. static const struct svc_procedure localio_procedures1[] = {
  158. [LOCALIOPROC_NULL] = {
  159. .pc_func = localio_proc_null,
  160. .pc_decode = nfssvc_decode_voidarg,
  161. .pc_encode = nfssvc_encode_voidres,
  162. .pc_argsize = sizeof(struct nfsd_voidargs),
  163. .pc_ressize = sizeof(struct nfsd_voidres),
  164. .pc_cachetype = RC_NOCACHE,
  165. .pc_xdrressize = 0,
  166. .pc_name = "NULL",
  167. },
  168. [LOCALIOPROC_UUID_IS_LOCAL] = {
  169. .pc_func = localio_proc_uuid_is_local,
  170. .pc_decode = localio_decode_uuidarg,
  171. .pc_encode = nfssvc_encode_voidres,
  172. .pc_argsize = sizeof(struct localio_uuidarg),
  173. .pc_argzero = sizeof(struct localio_uuidarg),
  174. .pc_ressize = sizeof(struct nfsd_voidres),
  175. .pc_cachetype = RC_NOCACHE,
  176. .pc_name = "UUID_IS_LOCAL",
  177. },
  178. };
  179. #define LOCALIO_NR_PROCEDURES ARRAY_SIZE(localio_procedures1)
  180. static DEFINE_PER_CPU_ALIGNED(unsigned long,
  181. localio_count[LOCALIO_NR_PROCEDURES]);
  182. const struct svc_version localio_version1 = {
  183. .vs_vers = 1,
  184. .vs_nproc = LOCALIO_NR_PROCEDURES,
  185. .vs_proc = localio_procedures1,
  186. .vs_dispatch = nfsd_dispatch,
  187. .vs_count = localio_count,
  188. .vs_xdrsize = XDR_QUADLEN(UUID_SIZE),
  189. .vs_hidden = true,
  190. };