namespace.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/fs/nfs/namespace.c
  4. *
  5. * Copyright (C) 2005 Trond Myklebust <Trond.Myklebust@netapp.com>
  6. * - Modified by David Howells <dhowells@redhat.com>
  7. *
  8. * NFS namespace
  9. */
  10. #include <linux/module.h>
  11. #include <linux/dcache.h>
  12. #include <linux/gfp.h>
  13. #include <linux/mount.h>
  14. #include <linux/namei.h>
  15. #include <linux/nfs_fs.h>
  16. #include <linux/string.h>
  17. #include <linux/sunrpc/clnt.h>
  18. #include <linux/vfs.h>
  19. #include <linux/sunrpc/gss_api.h>
  20. #include "internal.h"
  21. #include "nfs.h"
  22. #define NFSDBG_FACILITY NFSDBG_VFS
  23. static void nfs_expire_automounts(struct work_struct *work);
  24. static LIST_HEAD(nfs_automount_list);
  25. static DECLARE_DELAYED_WORK(nfs_automount_task, nfs_expire_automounts);
  26. int nfs_mountpoint_expiry_timeout = 500 * HZ;
  27. /*
  28. * nfs_path - reconstruct the path given an arbitrary dentry
  29. * @base - used to return pointer to the end of devname part of path
  30. * @dentry_in - pointer to dentry
  31. * @buffer - result buffer
  32. * @buflen_in - length of buffer
  33. * @flags - options (see below)
  34. *
  35. * Helper function for constructing the server pathname
  36. * by arbitrary hashed dentry.
  37. *
  38. * This is mainly for use in figuring out the path on the
  39. * server side when automounting on top of an existing partition
  40. * and in generating /proc/mounts and friends.
  41. *
  42. * Supported flags:
  43. * NFS_PATH_CANONICAL: ensure there is exactly one slash after
  44. * the original device (export) name
  45. * (if unset, the original name is returned verbatim)
  46. */
  47. char *nfs_path(char **p, struct dentry *dentry_in, char *buffer,
  48. ssize_t buflen_in, unsigned flags)
  49. {
  50. char *end;
  51. int namelen;
  52. unsigned seq;
  53. const char *base;
  54. struct dentry *dentry;
  55. ssize_t buflen;
  56. rename_retry:
  57. buflen = buflen_in;
  58. dentry = dentry_in;
  59. end = buffer+buflen;
  60. *--end = '\0';
  61. buflen--;
  62. seq = read_seqbegin(&rename_lock);
  63. rcu_read_lock();
  64. while (1) {
  65. spin_lock(&dentry->d_lock);
  66. if (IS_ROOT(dentry))
  67. break;
  68. namelen = dentry->d_name.len;
  69. buflen -= namelen + 1;
  70. if (buflen < 0)
  71. goto Elong_unlock;
  72. end -= namelen;
  73. memcpy(end, dentry->d_name.name, namelen);
  74. *--end = '/';
  75. spin_unlock(&dentry->d_lock);
  76. dentry = dentry->d_parent;
  77. }
  78. if (read_seqretry(&rename_lock, seq)) {
  79. spin_unlock(&dentry->d_lock);
  80. rcu_read_unlock();
  81. goto rename_retry;
  82. }
  83. if ((flags & NFS_PATH_CANONICAL) && *end != '/') {
  84. if (--buflen < 0) {
  85. spin_unlock(&dentry->d_lock);
  86. rcu_read_unlock();
  87. goto Elong;
  88. }
  89. *--end = '/';
  90. }
  91. *p = end;
  92. base = dentry->d_fsdata;
  93. if (!base) {
  94. spin_unlock(&dentry->d_lock);
  95. rcu_read_unlock();
  96. WARN_ON(1);
  97. return end;
  98. }
  99. namelen = strlen(base);
  100. if (*end == '/') {
  101. /* Strip off excess slashes in base string */
  102. while (namelen > 0 && base[namelen - 1] == '/')
  103. namelen--;
  104. }
  105. buflen -= namelen;
  106. if (buflen < 0) {
  107. spin_unlock(&dentry->d_lock);
  108. rcu_read_unlock();
  109. goto Elong;
  110. }
  111. end -= namelen;
  112. memcpy(end, base, namelen);
  113. spin_unlock(&dentry->d_lock);
  114. rcu_read_unlock();
  115. return end;
  116. Elong_unlock:
  117. spin_unlock(&dentry->d_lock);
  118. rcu_read_unlock();
  119. if (read_seqretry(&rename_lock, seq))
  120. goto rename_retry;
  121. Elong:
  122. return ERR_PTR(-ENAMETOOLONG);
  123. }
  124. EXPORT_SYMBOL_GPL(nfs_path);
  125. /*
  126. * nfs_d_automount - Handle crossing a mountpoint on the server
  127. * @path - The mountpoint
  128. *
  129. * When we encounter a mountpoint on the server, we want to set up
  130. * a mountpoint on the client too, to prevent inode numbers from
  131. * colliding, and to allow "df" to work properly.
  132. * On NFSv4, we also want to allow for the fact that different
  133. * filesystems may be migrated to different servers in a failover
  134. * situation, and that different filesystems may want to use
  135. * different security flavours.
  136. */
  137. struct vfsmount *nfs_d_automount(struct path *path)
  138. {
  139. struct nfs_fs_context *ctx;
  140. struct fs_context *fc;
  141. struct vfsmount *mnt = ERR_PTR(-ENOMEM);
  142. struct nfs_server *server = NFS_SB(path->dentry->d_sb);
  143. struct nfs_client *client = server->nfs_client;
  144. unsigned long s_flags = path->dentry->d_sb->s_flags;
  145. int timeout = READ_ONCE(nfs_mountpoint_expiry_timeout);
  146. int ret;
  147. if (IS_ROOT(path->dentry))
  148. return ERR_PTR(-ESTALE);
  149. /* Open a new filesystem context, transferring parameters from the
  150. * parent superblock, including the network namespace.
  151. */
  152. fc = fs_context_for_submount(path->mnt->mnt_sb->s_type, path->dentry);
  153. if (IS_ERR(fc))
  154. return ERR_CAST(fc);
  155. ctx = nfs_fc2context(fc);
  156. ctx->clone_data.dentry = path->dentry;
  157. ctx->clone_data.sb = path->dentry->d_sb;
  158. ctx->clone_data.fattr = nfs_alloc_fattr();
  159. if (!ctx->clone_data.fattr)
  160. goto out_fc;
  161. if (fc->cred != server->cred) {
  162. put_cred(fc->cred);
  163. fc->cred = get_cred(server->cred);
  164. }
  165. if (fc->net_ns != client->cl_net) {
  166. put_net(fc->net_ns);
  167. fc->net_ns = get_net(client->cl_net);
  168. }
  169. /* Inherit the flags covered by NFS_SB_MASK */
  170. fc->sb_flags_mask |= NFS_SB_MASK;
  171. fc->sb_flags &= ~NFS_SB_MASK;
  172. fc->sb_flags |= s_flags & NFS_SB_MASK;
  173. /* for submounts we want the same server; referrals will reassign */
  174. memcpy(&ctx->nfs_server._address, &client->cl_addr, client->cl_addrlen);
  175. ctx->nfs_server.addrlen = client->cl_addrlen;
  176. ctx->nfs_server.port = server->port;
  177. ctx->version = client->rpc_ops->version;
  178. ctx->minorversion = client->cl_minorversion;
  179. ctx->nfs_mod = client->cl_nfs_mod;
  180. get_nfs_version(ctx->nfs_mod);
  181. /* Inherit block sizes if they were specified as mount parameters */
  182. if (server->automount_inherit & NFS_AUTOMOUNT_INHERIT_BSIZE)
  183. ctx->bsize = server->bsize;
  184. ret = client->rpc_ops->submount(fc, server);
  185. if (ret < 0) {
  186. mnt = ERR_PTR(ret);
  187. goto out_fc;
  188. }
  189. up_write(&fc->root->d_sb->s_umount);
  190. mnt = vfs_create_mount(fc);
  191. if (IS_ERR(mnt))
  192. goto out_fc;
  193. if (timeout <= 0)
  194. goto out_fc;
  195. mnt_set_expiry(mnt, &nfs_automount_list);
  196. schedule_delayed_work(&nfs_automount_task, timeout);
  197. out_fc:
  198. put_fs_context(fc);
  199. return mnt;
  200. }
  201. static int
  202. nfs_namespace_getattr(struct mnt_idmap *idmap,
  203. const struct path *path, struct kstat *stat,
  204. u32 request_mask, unsigned int query_flags)
  205. {
  206. if (NFS_FH(d_inode(path->dentry))->size != 0)
  207. return nfs_getattr(idmap, path, stat, request_mask,
  208. query_flags);
  209. generic_fillattr(&nop_mnt_idmap, request_mask, d_inode(path->dentry),
  210. stat);
  211. return 0;
  212. }
  213. static int
  214. nfs_namespace_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
  215. struct iattr *attr)
  216. {
  217. if (NFS_FH(d_inode(dentry))->size != 0)
  218. return nfs_setattr(idmap, dentry, attr);
  219. return -EACCES;
  220. }
  221. const struct inode_operations nfs_mountpoint_inode_operations = {
  222. .getattr = nfs_getattr,
  223. .setattr = nfs_setattr,
  224. };
  225. const struct inode_operations nfs_referral_inode_operations = {
  226. .getattr = nfs_namespace_getattr,
  227. .setattr = nfs_namespace_setattr,
  228. };
  229. static void nfs_expire_automounts(struct work_struct *work)
  230. {
  231. struct list_head *list = &nfs_automount_list;
  232. int timeout = READ_ONCE(nfs_mountpoint_expiry_timeout);
  233. mark_mounts_for_expiry(list);
  234. if (!list_empty(list) && timeout > 0)
  235. schedule_delayed_work(&nfs_automount_task, timeout);
  236. }
  237. void nfs_release_automount_timer(void)
  238. {
  239. if (list_empty(&nfs_automount_list))
  240. cancel_delayed_work(&nfs_automount_task);
  241. }
  242. /**
  243. * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
  244. * @fc: pointer to struct nfs_fs_context
  245. *
  246. */
  247. int nfs_do_submount(struct fs_context *fc)
  248. {
  249. struct nfs_fs_context *ctx = nfs_fc2context(fc);
  250. struct dentry *dentry = ctx->clone_data.dentry;
  251. struct nfs_server *server;
  252. char *buffer, *p;
  253. int ret;
  254. /* create a new volume representation */
  255. server = ctx->nfs_mod->rpc_ops->clone_server(NFS_SB(ctx->clone_data.sb),
  256. ctx->mntfh,
  257. ctx->clone_data.fattr,
  258. ctx->selected_flavor);
  259. if (IS_ERR(server))
  260. return PTR_ERR(server);
  261. ctx->server = server;
  262. buffer = kmalloc(4096, GFP_USER);
  263. if (!buffer)
  264. return -ENOMEM;
  265. ctx->internal = true;
  266. p = nfs_devname(dentry, buffer, 4096);
  267. if (IS_ERR(p)) {
  268. nfs_errorf(fc, "NFS: Couldn't determine submount pathname");
  269. ret = PTR_ERR(p);
  270. } else {
  271. ret = vfs_parse_fs_qstr(fc, "source",
  272. &QSTR_LEN(p, buffer + 4096 - p));
  273. if (!ret)
  274. ret = vfs_get_tree(fc);
  275. }
  276. kfree(buffer);
  277. return ret;
  278. }
  279. EXPORT_SYMBOL_GPL(nfs_do_submount);
  280. int nfs_submount(struct fs_context *fc, struct nfs_server *server)
  281. {
  282. struct nfs_fs_context *ctx = nfs_fc2context(fc);
  283. struct dentry *dentry = ctx->clone_data.dentry;
  284. struct dentry *parent = dget_parent(dentry);
  285. int err;
  286. /* Look it up again to get its attributes */
  287. err = server->nfs_client->rpc_ops->lookup(d_inode(parent), dentry, &dentry->d_name,
  288. ctx->mntfh, ctx->clone_data.fattr);
  289. dput(parent);
  290. if (err != 0)
  291. return err;
  292. ctx->selected_flavor = server->client->cl_auth->au_flavor;
  293. return nfs_do_submount(fc);
  294. }
  295. EXPORT_SYMBOL_GPL(nfs_submount);
  296. static int param_set_nfs_timeout(const char *val, const struct kernel_param *kp)
  297. {
  298. long num;
  299. int ret;
  300. if (!val)
  301. return -EINVAL;
  302. ret = kstrtol(val, 0, &num);
  303. if (ret)
  304. return -EINVAL;
  305. if (num > 0) {
  306. if (num >= INT_MAX / HZ)
  307. num = INT_MAX;
  308. else
  309. num *= HZ;
  310. *((int *)kp->arg) = num;
  311. if (!list_empty(&nfs_automount_list))
  312. mod_delayed_work(system_percpu_wq, &nfs_automount_task, num);
  313. } else {
  314. *((int *)kp->arg) = -1*HZ;
  315. cancel_delayed_work(&nfs_automount_task);
  316. }
  317. return 0;
  318. }
  319. static int param_get_nfs_timeout(char *buffer, const struct kernel_param *kp)
  320. {
  321. long num = *((int *)kp->arg);
  322. if (num > 0) {
  323. if (num >= INT_MAX - (HZ - 1))
  324. num = INT_MAX / HZ;
  325. else
  326. num = (num + (HZ - 1)) / HZ;
  327. } else
  328. num = -1;
  329. return sysfs_emit(buffer, "%li\n", num);
  330. }
  331. static const struct kernel_param_ops param_ops_nfs_timeout = {
  332. .set = param_set_nfs_timeout,
  333. .get = param_get_nfs_timeout,
  334. };
  335. #define param_check_nfs_timeout(name, p) __param_check(name, p, int)
  336. module_param(nfs_mountpoint_expiry_timeout, nfs_timeout, 0644);
  337. MODULE_PARM_DESC(nfs_mountpoint_expiry_timeout,
  338. "Set the NFS automounted mountpoint timeout value (seconds)."
  339. "Values <= 0 turn expiration off.");