namespace.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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_SERVER(d_inode(path->dentry));
  143. struct nfs_client *client = server->nfs_client;
  144. int timeout = READ_ONCE(nfs_mountpoint_expiry_timeout);
  145. int ret;
  146. if (IS_ROOT(path->dentry))
  147. return ERR_PTR(-ESTALE);
  148. /* Open a new filesystem context, transferring parameters from the
  149. * parent superblock, including the network namespace.
  150. */
  151. fc = fs_context_for_submount(path->mnt->mnt_sb->s_type, path->dentry);
  152. if (IS_ERR(fc))
  153. return ERR_CAST(fc);
  154. ctx = nfs_fc2context(fc);
  155. ctx->clone_data.dentry = path->dentry;
  156. ctx->clone_data.sb = path->dentry->d_sb;
  157. ctx->clone_data.fattr = nfs_alloc_fattr();
  158. if (!ctx->clone_data.fattr)
  159. goto out_fc;
  160. if (fc->net_ns != client->cl_net) {
  161. put_net(fc->net_ns);
  162. fc->net_ns = get_net(client->cl_net);
  163. }
  164. /* for submounts we want the same server; referrals will reassign */
  165. memcpy(&ctx->nfs_server.address, &client->cl_addr, client->cl_addrlen);
  166. ctx->nfs_server.addrlen = client->cl_addrlen;
  167. ctx->nfs_server.port = server->port;
  168. ctx->version = client->rpc_ops->version;
  169. ctx->minorversion = client->cl_minorversion;
  170. ctx->nfs_mod = client->cl_nfs_mod;
  171. __module_get(ctx->nfs_mod->owner);
  172. ret = client->rpc_ops->submount(fc, server);
  173. if (ret < 0) {
  174. mnt = ERR_PTR(ret);
  175. goto out_fc;
  176. }
  177. up_write(&fc->root->d_sb->s_umount);
  178. mnt = vfs_create_mount(fc);
  179. if (IS_ERR(mnt))
  180. goto out_fc;
  181. mntget(mnt); /* prevent immediate expiration */
  182. if (timeout <= 0)
  183. goto out_fc;
  184. mnt_set_expiry(mnt, &nfs_automount_list);
  185. schedule_delayed_work(&nfs_automount_task, timeout);
  186. out_fc:
  187. put_fs_context(fc);
  188. return mnt;
  189. }
  190. static int
  191. nfs_namespace_getattr(const struct path *path, struct kstat *stat,
  192. u32 request_mask, unsigned int query_flags)
  193. {
  194. if (NFS_FH(d_inode(path->dentry))->size != 0)
  195. return nfs_getattr(path, stat, request_mask, query_flags);
  196. generic_fillattr(d_inode(path->dentry), stat);
  197. return 0;
  198. }
  199. static int
  200. nfs_namespace_setattr(struct dentry *dentry, struct iattr *attr)
  201. {
  202. if (NFS_FH(d_inode(dentry))->size != 0)
  203. return nfs_setattr(dentry, attr);
  204. return -EACCES;
  205. }
  206. const struct inode_operations nfs_mountpoint_inode_operations = {
  207. .getattr = nfs_getattr,
  208. .setattr = nfs_setattr,
  209. };
  210. const struct inode_operations nfs_referral_inode_operations = {
  211. .getattr = nfs_namespace_getattr,
  212. .setattr = nfs_namespace_setattr,
  213. };
  214. static void nfs_expire_automounts(struct work_struct *work)
  215. {
  216. struct list_head *list = &nfs_automount_list;
  217. int timeout = READ_ONCE(nfs_mountpoint_expiry_timeout);
  218. mark_mounts_for_expiry(list);
  219. if (!list_empty(list) && timeout > 0)
  220. schedule_delayed_work(&nfs_automount_task, timeout);
  221. }
  222. void nfs_release_automount_timer(void)
  223. {
  224. if (list_empty(&nfs_automount_list))
  225. cancel_delayed_work(&nfs_automount_task);
  226. }
  227. /**
  228. * nfs_do_submount - set up mountpoint when crossing a filesystem boundary
  229. * @fc: pointer to struct nfs_fs_context
  230. *
  231. */
  232. int nfs_do_submount(struct fs_context *fc)
  233. {
  234. struct nfs_fs_context *ctx = nfs_fc2context(fc);
  235. struct dentry *dentry = ctx->clone_data.dentry;
  236. struct nfs_server *server;
  237. char *buffer, *p;
  238. int ret;
  239. /* create a new volume representation */
  240. server = ctx->nfs_mod->rpc_ops->clone_server(NFS_SB(ctx->clone_data.sb),
  241. ctx->mntfh,
  242. ctx->clone_data.fattr,
  243. ctx->selected_flavor);
  244. if (IS_ERR(server))
  245. return PTR_ERR(server);
  246. ctx->server = server;
  247. buffer = kmalloc(4096, GFP_USER);
  248. if (!buffer)
  249. return -ENOMEM;
  250. ctx->internal = true;
  251. ctx->clone_data.inherited_bsize = ctx->clone_data.sb->s_blocksize_bits;
  252. p = nfs_devname(dentry, buffer, 4096);
  253. if (IS_ERR(p)) {
  254. nfs_errorf(fc, "NFS: Couldn't determine submount pathname");
  255. ret = PTR_ERR(p);
  256. } else {
  257. ret = vfs_parse_fs_string(fc, "source", p, buffer + 4096 - p);
  258. if (!ret)
  259. ret = vfs_get_tree(fc);
  260. }
  261. kfree(buffer);
  262. return ret;
  263. }
  264. EXPORT_SYMBOL_GPL(nfs_do_submount);
  265. int nfs_submount(struct fs_context *fc, struct nfs_server *server)
  266. {
  267. struct nfs_fs_context *ctx = nfs_fc2context(fc);
  268. struct dentry *dentry = ctx->clone_data.dentry;
  269. struct dentry *parent = dget_parent(dentry);
  270. int err;
  271. /* Look it up again to get its attributes */
  272. err = server->nfs_client->rpc_ops->lookup(d_inode(parent), dentry,
  273. ctx->mntfh, ctx->clone_data.fattr,
  274. NULL);
  275. dput(parent);
  276. if (err != 0)
  277. return err;
  278. ctx->selected_flavor = server->client->cl_auth->au_flavor;
  279. return nfs_do_submount(fc);
  280. }
  281. EXPORT_SYMBOL_GPL(nfs_submount);
  282. static int param_set_nfs_timeout(const char *val, const struct kernel_param *kp)
  283. {
  284. long num;
  285. int ret;
  286. if (!val)
  287. return -EINVAL;
  288. ret = kstrtol(val, 0, &num);
  289. if (ret)
  290. return -EINVAL;
  291. if (num > 0) {
  292. if (num >= INT_MAX / HZ)
  293. num = INT_MAX;
  294. else
  295. num *= HZ;
  296. *((int *)kp->arg) = num;
  297. if (!list_empty(&nfs_automount_list))
  298. mod_delayed_work(system_wq, &nfs_automount_task, num);
  299. } else {
  300. *((int *)kp->arg) = -1*HZ;
  301. cancel_delayed_work(&nfs_automount_task);
  302. }
  303. return 0;
  304. }
  305. static int param_get_nfs_timeout(char *buffer, const struct kernel_param *kp)
  306. {
  307. long num = *((int *)kp->arg);
  308. if (num > 0) {
  309. if (num >= INT_MAX - (HZ - 1))
  310. num = INT_MAX / HZ;
  311. else
  312. num = (num + (HZ - 1)) / HZ;
  313. } else
  314. num = -1;
  315. return scnprintf(buffer, PAGE_SIZE, "%li\n", num);
  316. }
  317. static const struct kernel_param_ops param_ops_nfs_timeout = {
  318. .set = param_set_nfs_timeout,
  319. .get = param_get_nfs_timeout,
  320. };
  321. #define param_check_nfs_timeout(name, p) __param_check(name, p, int);
  322. module_param(nfs_mountpoint_expiry_timeout, nfs_timeout, 0644);
  323. MODULE_PARM_DESC(nfs_mountpoint_expiry_timeout,
  324. "Set the NFS automounted mountpoint timeout value (seconds)."
  325. "Values <= 0 turn expiration off.");