[linux-cifs-client] [DFS support patchset: ] [4/5]: Provides DFS shrinkable submounts functionality.

Q (Igor Mammedov) qwerty0987654321 at mail.ru
Tue Dec 18 14:15:41 GMT 2007


Signed-off-by: Igor Mammedov <niallain at gmail.com>
---
 fs/cifs/cifs_dfs_ref.c |  303 ++++++++++++++++++++++++++++++++++++++++++++++++
 fs/cifs/cifs_dfs_ref.h |    2 +
 fs/cifs/cifsfs.c       |    6 +
 3 files changed, 311 insertions(+), 0 deletions(-)

diff --git a/fs/cifs/cifs_dfs_ref.c b/fs/cifs/cifs_dfs_ref.c
index 30b0ed1..441d520 100644
--- a/fs/cifs/cifs_dfs_ref.c
+++ b/fs/cifs/cifs_dfs_ref.c
@@ -21,12 +21,20 @@
  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  */
 
+#include <linux/dcache.h>
+#include <linux/mount.h>
+#include <linux/namei.h>
+#include <linux/vfs.h>
+#include <linux/fs.h>
 #include <keys/user-type.h>
 #include <linux/key-type.h>
 #include "cifsglob.h"
 #include "cifsproto.h"
+#include "cifsfs.h"
 #include "cifs_debug.h"
 
+LIST_HEAD(cifs_dfs_automount_list);
+
 static int cifs_resolver_instantiate(struct key *key, const void *data,
 		size_t datalen)
 {
@@ -118,4 +126,299 @@ cifs_resolve_server_name_to_ip(const char *unc, char **ip_addr) {
 	return rc;
 }
 
+/*
+ * DFS functions
+ */
+
+static char *cifs_get_share_name(const char *node_name)
+{
+	int len;
+	char *UNC;
+	char *pSep;
+
+	len = strlen(node_name);
+	UNC = kmalloc(len+2 /*for term null and additional \ if it's missed */,
+			 GFP_KERNEL);
+	if (!UNC)
+		return NULL;
+
+	/* get share name and server name */
+	if (node_name[1] != '\\') {
+		UNC[0] = '\\';
+		strncpy(UNC+1, node_name, len);
+		len++;
+		UNC[len] = 0;
+	} else {
+		strncpy(UNC, node_name, len);
+		UNC[len] = 0;
+	}
+
+	/* find server name end */
+	pSep = memchr(UNC+2, '\\', len-2);
+	if (!pSep) {
+		cERROR(1, ("%s: no server name end in node name: %s",
+			__FUNCTION__, node_name));
+		kfree(UNC);
+		return NULL;
+	}
+
+	/* find sharename end */
+	pSep++;
+	pSep = memchr(UNC+(pSep-UNC), '\\', len-(pSep-UNC));
+	if (!pSep) {
+		cERROR(1, ("%s:2 cant find share name in node name: %s",
+			__FUNCTION__, node_name));
+		kfree(UNC);
+		return NULL;
+	}
+	/* trim path up to sharename end
+	 *          * now we have share name in UNC */
+	*pSep = 0;
+	len = pSep-UNC;
+
+	return UNC;
+}
+
+
+struct vfsmount *cifs_dfs_do_refmount(const struct vfsmount *mnt_parent,
+		struct dentry *dentry, char *ref_unc)
+{
+	int rc;
+	struct cifs_sb_info *cifs_sb;
+	struct vfsmount *mnt = ERR_PTR(-ENOENT);
+	char *mountdata;
+	int md_len;
+	char *devname;
+	char *tkn_e;
+	char *srvIP = NULL;
+	char sep = ',';
+	int off, noff;
+
+	cifs_sb = CIFS_SB(dentry->d_inode->i_sb);
+
+	if (cifs_sb->mountdata == NULL)
+		return ERR_PTR(-EINVAL);
+
+	devname = cifs_get_share_name(ref_unc);
+	rc = cifs_resolve_server_name_to_ip(devname, &srvIP);
+	if (rc != 0) {
+		kfree(devname);
+		cERROR(1, ("%s: failed to resolve server part of %s to IP",
+			  __FUNCTION__, devname));
+		return ERR_PTR(rc);
+	}
+	md_len = strlen(cifs_sb->mountdata) + strlen(srvIP) +
+			strlen(ref_unc) + 3;
+	mountdata = kzalloc(md_len+1, GFP_KERNEL);
+
+	/* copy all options except of unc,ip,prefixpath */
+	off = 0;
+	if (strncmp(cifs_sb->mountdata, "sep=", 4) == 0) {
+			sep = cifs_sb->mountdata[4];
+			strncpy(mountdata, cifs_sb->mountdata, 5);
+			off += 5;
+	}
+	while ((tkn_e = strchr(cifs_sb->mountdata+off, sep))) {
+		noff = (tkn_e - (cifs_sb->mountdata+off)) + 1;
+		if (strnicmp(cifs_sb->mountdata+off, "unc=", 4) == 0) {
+			off += noff;
+			continue;
+		}
+		if (strnicmp(cifs_sb->mountdata+off, "ip=", 3) == 0) {
+			off += noff;
+			continue;
+		}
+		if (strnicmp(cifs_sb->mountdata+off, "prefixpath=", 3) == 0) {
+			off += noff;
+			continue;
+		}
+		strncat(mountdata, cifs_sb->mountdata+off, noff);
+		off += noff;
+	}
+	strcat(mountdata, cifs_sb->mountdata+off);
+	mountdata[md_len] = '\0';
+	strcat(mountdata, ",ip=");
+	strcat(mountdata, srvIP);
+	strcat(mountdata, ",unc=");
+	strcat(mountdata, devname);
+	/* find prefixpath */
+	tkn_e = strchr(ref_unc+2, '\\');
+	if (tkn_e) {
+		tkn_e = strchr(tkn_e+1, '\\');
+		if (tkn_e) {
+			strcat(mountdata, ",prefixpath=");
+			strcat(mountdata, tkn_e);
+		}
+	}
+
+	/*cFYI(1,("%s: old mountdata: %s", __FUNCTION__,cifs_sb->mountdata));*/
+	/*cFYI(1, ("%s: new mountdata: %s", __FUNCTION__, mountdata ));*/
+
+	mnt = vfs_kern_mount(&cifs_fs_type, 0, devname, mountdata);
+	kfree(srvIP);
+	kfree(devname);
+	kfree(mountdata);
+	return mnt;
+
+}
+
+static char *build_full_dfs_path_from_dentry(struct dentry *dentry)
+{
+	char *full_path = NULL;
+	char *search_path;
+	char *tmp_path;
+	size_t l_max_len;
+	struct cifs_sb_info *cifs_sb;
+
+	if (dentry->d_inode == NULL)
+		return NULL;
+
+	cifs_sb = CIFS_SB(dentry->d_inode->i_sb);
+
+	if (cifs_sb->tcon == NULL)
+		return NULL;
+
+	search_path = build_path_from_dentry(dentry);
+	if (search_path == NULL)
+		return NULL;
+
+	if (cifs_sb->tcon->Flags & 0x2) {
+		/* we should use full path name to correct working with DFS */
+		l_max_len = strnlen(cifs_sb->tcon->treeName, MAX_TREE_SIZE+1) +
+					strnlen(search_path, MAX_PATHCONF) + 1;
+		tmp_path = kmalloc(l_max_len, GFP_KERNEL);
+		if (tmp_path == NULL) {
+			kfree(search_path);
+			return NULL;
+		}
+		strncpy(tmp_path, cifs_sb->tcon->treeName, l_max_len);
+		strcat(tmp_path, search_path);
+		tmp_path[l_max_len-1] = 0;
+		full_path = tmp_path;
+		kfree(search_path);
+	} else {
+		full_path = search_path;
+	}
+	return full_path;
+}
+
+static void*
+cifs_dfs_follow_mountpoint(struct dentry *dentry, struct nameidata *nd)
+{
+	struct dfs_info3_param *referrals = NULL;
+	unsigned int num_referrals = 0;
+	struct cifs_sb_info *cifs_sb;
+	struct cifsSesInfo *ses;
+	char *full_path = NULL;
+	int xid, i;
+	int rc = 0;
+	struct vfsmount *mnt = ERR_PTR(-ENOENT);
+
+	cFYI(1, ("in %s", __FUNCTION__));
+	BUG_ON(IS_ROOT(dentry));
+
+	xid = GetXid();
+
+	dput(nd->dentry);
+	nd->dentry = dget(dentry);
+	if (d_mountpoint(nd->dentry))
+		goto out_follow;
+
+	if (dentry->d_inode == NULL) {
+		rc = -EINVAL;
+		goto out_err;
+	}
+
+	cifs_sb = CIFS_SB(dentry->d_inode->i_sb);
+	ses = cifs_sb->tcon->ses;
+
+	if (!ses) {
+		rc = -EINVAL;
+		goto out_err;
+	}
+
+	full_path = build_full_dfs_path_from_dentry(dentry);
+	if (full_path == NULL) {
+		rc = -ENOMEM;
+		goto out_err;
+	}
+
+	rc = get_dfs_path(xid, ses , full_path, cifs_sb->local_nls,
+		&num_referrals, &referrals,
+		cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
+
+	for (i = 0; i < num_referrals; i++) {
+		cFYI(1, ("%s: ref path: %s", __FUNCTION__,
+				referrals[i].path_name));
+		cFYI(1, ("%s: node path: %s", __FUNCTION__,
+				referrals[i].node_name));
+		cFYI(1, ("%s: fl: %hd, srv_type: %hd",
+					__FUNCTION__, referrals[i].flags,
+					referrals[i].server_type));
+		cFYI(1, ("%s: ref_flags: %hd, path_consumed: %hd",
+					__FUNCTION__,
+					referrals[i].ref_flag,
+					referrals[i].PathConsumed));
+
+		/* connect to storage node */
+		if (referrals[i].flags & DFSREF_STORAGE_SERVER) {
+			int len;
+			len = strlen(referrals[i].node_name);
+			if (len < 2) {
+				cERROR(1, ("%s: Net Address path too short: %s",
+					__FUNCTION__, referrals[i].node_name));
+				rc = -EINVAL;
+				goto out_err;
+			} else {
+				mnt = cifs_dfs_do_refmount(nd->mnt,
+						nd->dentry,
+						referrals[i].node_name);
+				cFYI(1, ("%s: cifs_dfs_do_refmount:%s , mnt:%p",
+					 __FUNCTION__,
+					referrals[i].node_name, mnt));
+				if (!rc) {
+					/* have server so stop here & return */
+					break;
+				}
+			}
+		}
+	}
+
+	rc = PTR_ERR(mnt);
+	if (IS_ERR(mnt))
+		goto out_err;
+
+	mntget(mnt);
+	rc = do_add_mount(mnt, nd, nd->mnt->mnt_flags,
+				&cifs_dfs_automount_list);
+	if (rc < 0) {
+		mntput(mnt);
+		if (rc == -EBUSY)
+			goto out_follow;
+		goto out_err;
+	}
+	mntput(nd->mnt);
+	dput(nd->dentry);
+	nd->mnt = mnt;
+	nd->dentry = dget(mnt->mnt_root);
+
+out:
+	FreeXid(xid);
+	free_dfs_info_array(referrals, num_referrals);
+	cFYI(1, ("leaving %s" , __FUNCTION__));
+	return ERR_PTR(rc);
+out_err:
+	kfree(full_path);
+	path_release(nd);
+	goto out;
+out_follow:
+	/* BB ??? */
+	while (d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry)) ;
+	rc = 0;
+	goto out;
+}
+
+struct inode_operations cifs_dfs_referral_inode_operations = {
+	.follow_link = cifs_dfs_follow_mountpoint,
+};
 
diff --git a/fs/cifs/cifs_dfs_ref.h b/fs/cifs/cifs_dfs_ref.h
index 312a0bb..6b8b31f 100644
--- a/fs/cifs/cifs_dfs_ref.h
+++ b/fs/cifs/cifs_dfs_ref.h
@@ -24,6 +24,8 @@
 
 #ifdef __KERNEL__
 extern struct key_type key_type_cifs_resolver;
+extern struct list_head cifs_dfs_automount_list;
+extern struct inode_operations cifs_dfs_referral_inode_operations;
 #endif /* KERNEL */
 
 #endif /* _CIFS_DFS_REF_H */
diff --git a/fs/cifs/cifsfs.c b/fs/cifs/cifsfs.c
index b459e47..7e8e356 100644
--- a/fs/cifs/cifsfs.c
+++ b/fs/cifs/cifsfs.c
@@ -471,6 +471,12 @@ static void cifs_umount_begin(struct vfsmount *vfsmnt, int flags)
 	struct cifs_sb_info *cifs_sb;
 	struct cifsTconInfo *tcon;
 
+#ifdef CONFIG_CIFS_DFS_UPCALL
+	mark_mounts_for_expiry(&cifs_dfs_automount_list);
+	mark_mounts_for_expiry(&cifs_dfs_automount_list);
+	shrink_submounts(vfsmnt, &cifs_dfs_automount_list);
+#endif
+
 	if (!(flags & MNT_FORCE))
 		return;
 	cifs_sb = CIFS_SB(vfsmnt->mnt_sb);
-- 
1.5.3.7






More information about the linux-cifs-client mailing list