1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Contains mounting routines used for handling traversal via SMB junctions.
5 * Copyright (c) 2007 Igor Mammedov
6 * Copyright (C) International Business Machines Corp., 2008
7 * Author(s): Igor Mammedov (niallain@gmail.com)
8 * Steve French (sfrench@us.ibm.com)
9 * Copyright (c) 2023 Paulo Alcantara <palcantara@suse.de>
12 #include <linux/dcache.h>
13 #include <linux/mount.h>
14 #include <linux/namei.h>
15 #include <linux/slab.h>
16 #include <linux/vfs.h>
18 #include <linux/inet.h>
20 #include "cifsproto.h"
22 #include "cifs_debug.h"
23 #include "fs_context.h"
25 static LIST_HEAD(cifs_automount_list);
27 static void cifs_expire_automounts(struct work_struct *work);
28 static DECLARE_DELAYED_WORK(cifs_automount_task,
29 cifs_expire_automounts);
30 static int cifs_mountpoint_expiry_timeout = 500 * HZ;
32 static void cifs_expire_automounts(struct work_struct *work)
34 struct list_head *list = &cifs_automount_list;
36 mark_mounts_for_expiry(list);
37 if (!list_empty(list))
38 schedule_delayed_work(&cifs_automount_task,
39 cifs_mountpoint_expiry_timeout);
42 void cifs_release_automount_timer(void)
44 if (WARN_ON(!list_empty(&cifs_automount_list)))
46 cancel_delayed_work_sync(&cifs_automount_task);
50 * cifs_build_devname - build a devicename from a UNC and optional prepath
51 * @nodename: pointer to UNC string
52 * @prepath: pointer to prefixpath (or NULL if there isn't one)
54 * Build a new cifs devicename after chasing a DFS referral. Allocate a buffer
55 * big enough to hold the final thing. Copy the UNC from the nodename, and
56 * concatenate the prepath onto the end of it if there is one.
58 * Returns pointer to the built string, or a ERR_PTR. Caller is responsible
59 * for freeing the returned string.
62 cifs_build_devname(char *nodename, const char *prepath)
69 /* skip over any preceding delimiters */
70 nodename += strspn(nodename, "\\");
72 return ERR_PTR(-EINVAL);
74 /* get length of UNC and set pos to last char */
75 unclen = strlen(nodename);
76 pos = nodename + unclen - 1;
78 /* trim off any trailing delimiters */
79 while (*pos == '\\') {
85 * +2 for preceding "//"
86 * +1 for delimiter between UNC and prepath
87 * +1 for trailing NULL
89 pplen = prepath ? strlen(prepath) : 0;
90 dev = kmalloc(2 + unclen + 1 + pplen + 1, GFP_KERNEL);
92 return ERR_PTR(-ENOMEM);
95 /* add the initial "//" */
101 /* copy in the UNC portion from referral */
102 memcpy(pos, nodename, unclen);
105 /* copy the prefixpath remainder (if there is one) */
109 memcpy(pos, prepath, pplen);
113 /* NULL terminator */
116 convert_delimiter(dev, '/');
120 static bool is_dfs_mount(struct dentry *dentry)
122 struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
123 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
126 spin_lock(&tcon->tc_lock);
127 ret = !!tcon->origin_fullpath;
128 spin_unlock(&tcon->tc_lock);
132 /* Return full path out of a dentry set for automount */
133 static char *automount_fullpath(struct dentry *dentry, void *page)
135 struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
136 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
140 spin_lock(&tcon->tc_lock);
141 if (!tcon->origin_fullpath) {
142 spin_unlock(&tcon->tc_lock);
143 return build_path_from_dentry_optional_prefix(dentry,
147 spin_unlock(&tcon->tc_lock);
149 s = dentry_path_raw(dentry, page, PATH_MAX);
152 /* for root, we want "" */
156 spin_lock(&tcon->tc_lock);
157 len = strlen(tcon->origin_fullpath);
158 if (s < (char *)page + len) {
159 spin_unlock(&tcon->tc_lock);
160 return ERR_PTR(-ENAMETOOLONG);
164 memcpy(s, tcon->origin_fullpath, len);
165 spin_unlock(&tcon->tc_lock);
166 convert_delimiter(s, '/');
172 * Create a vfsmount that we can automount
174 static struct vfsmount *cifs_do_automount(struct path *path)
177 struct dentry *mntpt = path->dentry;
178 struct fs_context *fc;
180 struct smb3_fs_context *ctx, *cur_ctx;
181 struct smb3_fs_context tmp;
183 struct vfsmount *mnt;
186 return ERR_PTR(-ESTALE);
188 cur_ctx = CIFS_SB(mntpt->d_sb)->ctx;
190 fc = fs_context_for_submount(path->mnt->mnt_sb->s_type, mntpt);
194 ctx = smb3_fc2context(fc);
196 page = alloc_dentry_path();
197 full_path = automount_fullpath(mntpt, page);
198 if (IS_ERR(full_path)) {
199 mnt = ERR_CAST(full_path);
205 tmp.leaf_fullpath = NULL;
206 tmp.UNC = tmp.prepath = NULL;
207 tmp.dfs_root_ses = NULL;
209 rc = smb3_fs_context_dup(ctx, &tmp);
215 rc = smb3_parse_devname(full_path, ctx);
221 ctx->source = smb3_fs_context_fullpath(ctx, '/');
222 if (IS_ERR(ctx->source)) {
223 mnt = ERR_CAST(ctx->source);
227 ctx->dfs_automount = is_dfs_mount(mntpt);
228 cifs_dbg(FYI, "%s: ctx: source=%s UNC=%s prepath=%s dfs_automount=%d\n",
229 __func__, ctx->source, ctx->UNC, ctx->prepath, ctx->dfs_automount);
234 free_dentry_path(page);
239 * Attempt to automount the referral
241 struct vfsmount *cifs_d_automount(struct path *path)
243 struct vfsmount *newmnt;
245 cifs_dbg(FYI, "%s: %pd\n", __func__, path->dentry);
247 newmnt = cifs_do_automount(path);
248 if (IS_ERR(newmnt)) {
249 cifs_dbg(FYI, "leaving %s [automount failed]\n" , __func__);
253 mntget(newmnt); /* prevent immediate expiration */
254 mnt_set_expiry(newmnt, &cifs_automount_list);
255 schedule_delayed_work(&cifs_automount_task,
256 cifs_mountpoint_expiry_timeout);
257 cifs_dbg(FYI, "leaving %s [ok]\n" , __func__);
261 const struct inode_operations cifs_namespace_inode_operations = {