2 * Contains the CIFS DFS referral mounting routines used for handling
3 * traversal via DFS junction point
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 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
15 #include <linux/dcache.h>
16 #include <linux/mount.h>
17 #include <linux/namei.h>
18 #include <linux/vfs.h>
21 #include "cifsproto.h"
23 #include "dns_resolve.h"
24 #include "cifs_debug.h"
26 LIST_HEAD(cifs_dfs_automount_list
);
32 void dfs_shrink_umount_helper(struct vfsmount
*vfsmnt
)
34 mark_mounts_for_expiry(&cifs_dfs_automount_list
);
35 mark_mounts_for_expiry(&cifs_dfs_automount_list
);
36 shrink_submounts(vfsmnt
, &cifs_dfs_automount_list
);
40 * cifs_get_share_name - extracts share name from UNC
41 * @node_name: pointer to UNC string
43 * Extracts sharename form full UNC.
44 * i.e. strips from UNC trailing path that is not part of share
45 * name and fixup missing '\' in the begining of DFS node refferal
47 * Returns pointer to share name on success or NULL on error.
48 * Caller is responsible for freeing returned string.
50 static char *cifs_get_share_name(const char *node_name
)
56 len
= strlen(node_name
);
57 UNC
= kmalloc(len
+2 /*for term null and additional \ if it's missed */,
62 /* get share name and server name */
63 if (node_name
[1] != '\\') {
65 strncpy(UNC
+1, node_name
, len
);
69 strncpy(UNC
, node_name
, len
);
73 /* find server name end */
74 pSep
= memchr(UNC
+2, '\\', len
-2);
76 cERROR(1, ("%s: no server name end in node name: %s",
77 __FUNCTION__
, node_name
));
82 /* find sharename end */
84 pSep
= memchr(UNC
+(pSep
-UNC
), '\\', len
-(pSep
-UNC
));
86 cERROR(1, ("%s:2 cant find share name in node name: %s",
87 __FUNCTION__
, node_name
));
91 /* trim path up to sharename end
92 * * now we have share name in UNC */
100 * compose_mount_options - creates mount options for refferral
101 * @sb_mountdata: parent/root DFS mount options (template)
102 * @ref_unc: refferral server UNC
103 * @devname: pointer for saving device name
105 * creates mount options for submount based on template options sb_mountdata
106 * and replacing unc,ip,prefixpath options with ones we've got form ref_unc.
108 * Returns: pointer to new mount options or ERR_PTR.
109 * Caller is responcible for freeing retunrned value if it is not error.
111 static char *compose_mount_options(const char *sb_mountdata
,
123 if (sb_mountdata
== NULL
)
124 return ERR_PTR(-EINVAL
);
126 *devname
= cifs_get_share_name(ref_unc
);
127 rc
= dns_resolve_server_name_to_ip(*devname
, &srvIP
);
129 cERROR(1, ("%s: Failed to resolve server part of %s to IP",
130 __FUNCTION__
, *devname
));
131 mountdata
= ERR_PTR(rc
);
132 goto compose_mount_options_out
;
134 md_len
= strlen(sb_mountdata
) + strlen(srvIP
) + strlen(ref_unc
) + 3;
135 mountdata
= kzalloc(md_len
+1, GFP_KERNEL
);
136 if (mountdata
== NULL
) {
137 mountdata
= ERR_PTR(-ENOMEM
);
138 goto compose_mount_options_out
;
141 /* copy all options except of unc,ip,prefixpath */
143 if (strncmp(sb_mountdata
, "sep=", 4) == 0) {
144 sep
= sb_mountdata
[4];
145 strncpy(mountdata
, sb_mountdata
, 5);
148 while ((tkn_e
= strchr(sb_mountdata
+off
, sep
))) {
149 noff
= (tkn_e
- (sb_mountdata
+off
)) + 1;
150 if (strnicmp(sb_mountdata
+off
, "unc=", 4) == 0) {
154 if (strnicmp(sb_mountdata
+off
, "ip=", 3) == 0) {
158 if (strnicmp(sb_mountdata
+off
, "prefixpath=", 3) == 0) {
162 strncat(mountdata
, sb_mountdata
+off
, noff
);
165 strcat(mountdata
, sb_mountdata
+off
);
166 mountdata
[md_len
] = '\0';
168 /* copy new IP and ref share name */
169 strcat(mountdata
, ",ip=");
170 strcat(mountdata
, srvIP
);
171 strcat(mountdata
, ",unc=");
172 strcat(mountdata
, *devname
);
174 /* find & copy prefixpath */
175 tkn_e
= strchr(ref_unc
+2, '\\');
177 tkn_e
= strchr(tkn_e
+1, '\\');
179 strcat(mountdata
, ",prefixpath=");
180 strcat(mountdata
, tkn_e
);
184 /*cFYI(1,("%s: parent mountdata: %s", __FUNCTION__,sb_mountdata));*/
185 /*cFYI(1, ("%s: submount mountdata: %s", __FUNCTION__, mountdata ));*/
187 compose_mount_options_out
:
193 static struct vfsmount
*cifs_dfs_do_refmount(const struct vfsmount
*mnt_parent
,
194 struct dentry
*dentry
, char *ref_unc
)
196 struct cifs_sb_info
*cifs_sb
;
197 struct vfsmount
*mnt
;
199 char *devname
= NULL
;
201 cifs_sb
= CIFS_SB(dentry
->d_inode
->i_sb
);
202 mountdata
= compose_mount_options(cifs_sb
->mountdata
,
205 if (IS_ERR(mountdata
))
206 return (struct vfsmount
*)mountdata
;
208 mnt
= vfs_kern_mount(&cifs_fs_type
, 0, devname
, mountdata
);
215 static char *build_full_dfs_path_from_dentry(struct dentry
*dentry
)
217 char *full_path
= NULL
;
221 struct cifs_sb_info
*cifs_sb
;
223 if (dentry
->d_inode
== NULL
)
226 cifs_sb
= CIFS_SB(dentry
->d_inode
->i_sb
);
228 if (cifs_sb
->tcon
== NULL
)
231 search_path
= build_path_from_dentry(dentry
);
232 if (search_path
== NULL
)
235 if (cifs_sb
->tcon
->Flags
& SMB_SHARE_IS_IN_DFS
) {
236 /* we should use full path name to correct working with DFS */
237 l_max_len
= strnlen(cifs_sb
->tcon
->treeName
, MAX_TREE_SIZE
+1) +
238 strnlen(search_path
, MAX_PATHCONF
) + 1;
239 tmp_path
= kmalloc(l_max_len
, GFP_KERNEL
);
240 if (tmp_path
== NULL
) {
244 strncpy(tmp_path
, cifs_sb
->tcon
->treeName
, l_max_len
);
245 strcat(tmp_path
, search_path
);
246 tmp_path
[l_max_len
-1] = 0;
247 full_path
= tmp_path
;
250 full_path
= search_path
;
255 static int add_mount_helper(struct vfsmount
*newmnt
, struct nameidata
*nd
,
256 struct list_head
*mntlist
)
258 /* stolen from afs code */
262 err
= do_add_mount(newmnt
, nd
, nd
->path
.mnt
->mnt_flags
, mntlist
);
265 dput(nd
->path
.dentry
);
266 mntput(nd
->path
.mnt
);
267 nd
->path
.mnt
= newmnt
;
268 nd
->path
.dentry
= dget(newmnt
->mnt_root
);
271 /* someone else made a mount here whilst we were busy */
272 while (d_mountpoint(nd
->path
.dentry
) &&
273 follow_down(&nd
->path
.mnt
, &nd
->path
.dentry
))
283 static void dump_referral(const struct dfs_info3_param
*ref
)
285 cFYI(1, ("DFS: ref path: %s", ref
->path_name
));
286 cFYI(1, ("DFS: node path: %s", ref
->node_name
));
287 cFYI(1, ("DFS: fl: %hd, srv_type: %hd", ref
->flags
, ref
->server_type
));
288 cFYI(1, ("DFS: ref_flags: %hd, path_consumed: %hd", ref
->ref_flag
,
294 cifs_dfs_follow_mountpoint(struct dentry
*dentry
, struct nameidata
*nd
)
296 struct dfs_info3_param
*referrals
= NULL
;
297 unsigned int num_referrals
= 0;
298 struct cifs_sb_info
*cifs_sb
;
299 struct cifsSesInfo
*ses
;
300 char *full_path
= NULL
;
303 struct vfsmount
*mnt
= ERR_PTR(-ENOENT
);
305 cFYI(1, ("in %s", __FUNCTION__
));
306 BUG_ON(IS_ROOT(dentry
));
310 dput(nd
->path
.dentry
);
311 nd
->path
.dentry
= dget(dentry
);
313 cifs_sb
= CIFS_SB(dentry
->d_inode
->i_sb
);
314 ses
= cifs_sb
->tcon
->ses
;
321 full_path
= build_full_dfs_path_from_dentry(dentry
);
322 if (full_path
== NULL
) {
327 rc
= get_dfs_path(xid
, ses
, full_path
, cifs_sb
->local_nls
,
328 &num_referrals
, &referrals
,
329 cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_MAP_SPECIAL_CHR
);
331 for (i
= 0; i
< num_referrals
; i
++) {
332 dump_referral(referrals
+i
);
333 /* connect to a storage node */
334 if (referrals
[i
].flags
& DFSREF_STORAGE_SERVER
) {
336 len
= strlen(referrals
[i
].node_name
);
338 cERROR(1, ("%s: Net Address path too short: %s",
339 __FUNCTION__
, referrals
[i
].node_name
));
343 mnt
= cifs_dfs_do_refmount(nd
->path
.mnt
,
345 referrals
[i
].node_name
);
346 cFYI(1, ("%s: cifs_dfs_do_refmount:%s , mnt:%p",
348 referrals
[i
].node_name
, mnt
));
350 /* complete mount procedure if we accured submount */
356 /* we need it cause for() above could exit without valid submount */
361 nd
->path
.mnt
->mnt_flags
|= MNT_SHRINKABLE
;
362 rc
= add_mount_helper(mnt
, nd
, &cifs_dfs_automount_list
);
366 free_dfs_info_array(referrals
, num_referrals
);
368 cFYI(1, ("leaving %s" , __FUNCTION__
));
375 struct inode_operations cifs_dfs_referral_inode_operations
= {
376 .follow_link
= cifs_dfs_follow_mountpoint
,