Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *   Contains the CIFS DFS referral mounting routines used for handling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *   traversal via DFS junction point
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *   Copyright (c) 2007 Igor Mammedov
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *   Copyright (C) International Business Machines  Corp., 2008
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *   Author(s): Igor Mammedov (niallain@gmail.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *		Steve French (sfrench@us.ibm.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/dcache.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/vfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/inet.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include "cifsglob.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include "cifsproto.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include "cifsfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include "dns_resolve.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include "cifs_debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include "cifs_unicode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include "dfs_cache.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) static LIST_HEAD(cifs_dfs_automount_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) static void cifs_dfs_expire_automounts(struct work_struct *work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static DECLARE_DELAYED_WORK(cifs_dfs_automount_task,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 			    cifs_dfs_expire_automounts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) static int cifs_dfs_mountpoint_expiry_timeout = 500 * HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) static void cifs_dfs_expire_automounts(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	struct list_head *list = &cifs_dfs_automount_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	mark_mounts_for_expiry(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	if (!list_empty(list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		schedule_delayed_work(&cifs_dfs_automount_task,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 				      cifs_dfs_mountpoint_expiry_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) void cifs_dfs_release_automount_timer(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	BUG_ON(!list_empty(&cifs_dfs_automount_list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	cancel_delayed_work_sync(&cifs_dfs_automount_task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  * cifs_build_devname - build a devicename from a UNC and optional prepath
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  * @nodename:	pointer to UNC string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  * @prepath:	pointer to prefixpath (or NULL if there isn't one)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  * Build a new cifs devicename after chasing a DFS referral. Allocate a buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  * big enough to hold the final thing. Copy the UNC from the nodename, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  * concatenate the prepath onto the end of it if there is one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  * Returns pointer to the built string, or a ERR_PTR. Caller is responsible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  * for freeing the returned string.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) static char *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) cifs_build_devname(char *nodename, const char *prepath)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	size_t pplen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	size_t unclen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	char *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	char *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	/* skip over any preceding delimiters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	nodename += strspn(nodename, "\\");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (!*nodename)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	/* get length of UNC and set pos to last char */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	unclen = strlen(nodename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	pos = nodename + unclen - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	/* trim off any trailing delimiters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	while (*pos == '\\') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		--pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		--unclen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	/* allocate a buffer:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	 * +2 for preceding "//"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	 * +1 for delimiter between UNC and prepath
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	 * +1 for trailing NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	pplen = prepath ? strlen(prepath) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	dev = kmalloc(2 + unclen + 1 + pplen + 1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	pos = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	/* add the initial "//" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	*pos = '/';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	++pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	*pos = '/';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	++pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	/* copy in the UNC portion from referral */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	memcpy(pos, nodename, unclen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	pos += unclen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	/* copy the prefixpath remainder (if there is one) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	if (pplen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		*pos = '/';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		++pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		memcpy(pos, prepath, pplen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		pos += pplen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	/* NULL terminator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	*pos = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	convert_delimiter(dev, '/');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)  * cifs_compose_mount_options	-	creates mount options for referral
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)  * @sb_mountdata:	parent/root DFS mount options (template)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)  * @fullpath:		full path in UNC format
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)  * @ref:		optional server's referral
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)  * @devname:		optional pointer for saving device name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)  * creates mount options for submount based on template options sb_mountdata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)  * and replacing unc,ip,prefixpath options with ones we've got form ref_unc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)  * Returns: pointer to new mount options or ERR_PTR.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)  * Caller is responsible for freeing returned value if it is not error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) char *cifs_compose_mount_options(const char *sb_mountdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 				   const char *fullpath,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 				   const struct dfs_info3_param *ref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 				   char **devname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	char *mountdata = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	const char *prepath = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	int md_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	char *tkn_e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	char *srvIP = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	char sep = ',';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	int off, noff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	if (sb_mountdata == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	if (ref) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		if (WARN_ON_ONCE(!ref->node_name || ref->path_consumed < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 			return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		if (strlen(fullpath) - ref->path_consumed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 			prepath = fullpath + ref->path_consumed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 			/* skip initial delimiter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 			if (*prepath == '/' || *prepath == '\\')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 				prepath++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		name = cifs_build_devname(ref->node_name, prepath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		if (IS_ERR(name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 			rc = PTR_ERR(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 			name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			goto compose_mount_options_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		name = cifs_build_devname((char *)fullpath, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		if (IS_ERR(name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 			rc = PTR_ERR(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 			name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 			goto compose_mount_options_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	rc = dns_resolve_server_name_to_ip(name, &srvIP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	if (rc < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		cifs_dbg(FYI, "%s: Failed to resolve server part of %s to IP: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 			 __func__, name, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		goto compose_mount_options_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	 * In most cases, we'll be building a shorter string than the original,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	 * but we do have to assume that the address in the ip= option may be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	 * much longer than the original. Add the max length of an address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	 * string to the length of the original string to allow for worst case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	md_len = strlen(sb_mountdata) + INET6_ADDRSTRLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	mountdata = kzalloc(md_len + sizeof("ip=") + 1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	if (mountdata == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		goto compose_mount_options_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	/* copy all options except of unc,ip,prefixpath */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	if (strncmp(sb_mountdata, "sep=", 4) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			sep = sb_mountdata[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 			strncpy(mountdata, sb_mountdata, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 			off += 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		tkn_e = strchr(sb_mountdata + off, sep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		if (tkn_e == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			noff = strlen(sb_mountdata + off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 			noff = tkn_e - (sb_mountdata + off) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		if (strncasecmp(sb_mountdata + off, "unc=", 4) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			off += noff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		if (strncasecmp(sb_mountdata + off, "ip=", 3) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			off += noff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		if (strncasecmp(sb_mountdata + off, "prefixpath=", 11) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 			off += noff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		strncat(mountdata, sb_mountdata + off, noff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		off += noff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	} while (tkn_e);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	strcat(mountdata, sb_mountdata + off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	mountdata[md_len] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	/* copy new IP and ref share name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	if (mountdata[strlen(mountdata) - 1] != sep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		strncat(mountdata, &sep, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	strcat(mountdata, "ip=");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	strcat(mountdata, srvIP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	if (devname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		*devname = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	/*cifs_dbg(FYI, "%s: parent mountdata: %s\n", __func__, sb_mountdata);*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	/*cifs_dbg(FYI, "%s: submount mountdata: %s\n", __func__, mountdata );*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) compose_mount_options_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	kfree(srvIP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	return mountdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) compose_mount_options_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	kfree(mountdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	mountdata = ERR_PTR(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	goto compose_mount_options_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)  * cifs_dfs_do_mount - mounts specified path using DFS full path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)  * Always pass down @fullpath to smb3_do_mount() so we can use the root server
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)  * to perform failover in case we failed to connect to the first target in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)  * referral.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)  * @cifs_sb:		parent/root superblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)  * @fullpath:		full path in UNC format
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) static struct vfsmount *cifs_dfs_do_mount(struct dentry *mntpt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 					  struct cifs_sb_info *cifs_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 					  const char *fullpath)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	struct vfsmount *mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	char *mountdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	char *devname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	devname = kstrndup(fullpath, strlen(fullpath), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	if (!devname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	convert_delimiter(devname, '/');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	/* strip first '\' from fullpath */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	mountdata = cifs_compose_mount_options(cifs_sb->mountdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 					       fullpath + 1, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	if (IS_ERR(mountdata)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		kfree(devname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		return (struct vfsmount *)mountdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	mnt = vfs_submount(mntpt, &cifs_fs_type, devname, mountdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	kfree(mountdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	kfree(devname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	return mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)  * Create a vfsmount that we can automount
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) static struct vfsmount *cifs_dfs_do_automount(struct dentry *mntpt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	struct cifs_sb_info *cifs_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	struct cifs_ses *ses;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	struct cifs_tcon *tcon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	char *full_path, *root_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	unsigned int xid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	struct vfsmount *mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	cifs_dbg(FYI, "in %s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	BUG_ON(IS_ROOT(mntpt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	 * The MSDFS spec states that paths in DFS referral requests and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	 * responses must be prefixed by a single '\' character instead of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	 * the double backslashes usually used in the UNC. This function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	 * gives us the latter, so we must adjust the result.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	mnt = ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	cifs_sb = CIFS_SB(mntpt->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		mnt = ERR_PTR(-EREMOTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		goto cdda_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	/* always use tree name prefix */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	full_path = build_path_from_dentry_optional_prefix(mntpt, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	if (full_path == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		goto cdda_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	convert_delimiter(full_path, '\\');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	if (!cifs_sb_master_tlink(cifs_sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		cifs_dbg(FYI, "%s: master tlink is NULL\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		goto free_full_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	tcon = cifs_sb_master_tcon(cifs_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	if (!tcon) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		cifs_dbg(FYI, "%s: master tcon is NULL\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		goto free_full_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	root_path = kstrdup(tcon->treeName, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	if (!root_path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		mnt = ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		goto free_full_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	cifs_dbg(FYI, "%s: root path: %s\n", __func__, root_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	ses = tcon->ses;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	xid = get_xid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	 * If DFS root has been expired, then unconditionally fetch it again to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	 * refresh DFS referral cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	rc = dfs_cache_find(xid, ses, cifs_sb->local_nls, cifs_remap(cifs_sb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 			    root_path + 1, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	if (!rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		rc = dfs_cache_find(xid, ses, cifs_sb->local_nls,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 				    cifs_remap(cifs_sb), full_path + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 				    NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		mnt = ERR_PTR(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		goto free_root_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	 * OK - we were able to get and cache a referral for @full_path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	 * Now, pass it down to cifs_mount() and it will retry every available
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	 * node server in case of failures - no need to do it here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	mnt = cifs_dfs_do_mount(mntpt, cifs_sb, full_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	cifs_dbg(FYI, "%s: cifs_dfs_do_mount:%s , mnt:%p\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		 full_path + 1, mnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) free_root_path:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	kfree(root_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) free_full_path:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	kfree(full_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) cdda_exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	cifs_dbg(FYI, "leaving %s\n" , __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	return mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)  * Attempt to automount the referral
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) struct vfsmount *cifs_dfs_d_automount(struct path *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	struct vfsmount *newmnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	cifs_dbg(FYI, "in %s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	newmnt = cifs_dfs_do_automount(path->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	if (IS_ERR(newmnt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		cifs_dbg(FYI, "leaving %s [automount failed]\n" , __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		return newmnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	mntget(newmnt); /* prevent immediate expiration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	mnt_set_expiry(newmnt, &cifs_dfs_automount_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	schedule_delayed_work(&cifs_dfs_automount_task,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 			      cifs_dfs_mountpoint_expiry_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	cifs_dbg(FYI, "leaving %s [ok]\n" , __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	return newmnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) const struct inode_operations cifs_dfs_referral_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) };