blob: 8e5a1817496dd1e48949d99d2adb88c53e761773 [file] [log] [blame]
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +00001/*
2 * Contains the CIFS DFS referral mounting routines used for handling
3 * traversal via DFS junction point
4 *
5 * Copyright (c) 2007 Igor Mammedov
Steve French366781c2008-01-25 10:12:41 +00006 * Copyright (C) International Business Machines Corp., 2008
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +00007 * Author(s): Igor Mammedov (niallain@gmail.com)
Steve French366781c2008-01-25 10:12:41 +00008 * Steve French (sfrench@us.ibm.com)
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +00009 * 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.
13 */
14
15#include <linux/dcache.h>
16#include <linux/mount.h>
17#include <linux/namei.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +000019#include <linux/vfs.h>
20#include <linux/fs.h>
Jeff Layton166faf22013-05-24 07:40:04 -040021#include <linux/inet.h>
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +000022#include "cifsglob.h"
23#include "cifsproto.h"
24#include "cifsfs.h"
25#include "dns_resolve.h"
26#include "cifs_debug.h"
27
Steve French8d142132008-04-16 03:56:51 +000028static LIST_HEAD(cifs_dfs_automount_list);
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +000029
Igor Mammedov78d31a32008-04-24 12:56:07 +040030static void cifs_dfs_expire_automounts(struct work_struct *work);
31static DECLARE_DELAYED_WORK(cifs_dfs_automount_task,
32 cifs_dfs_expire_automounts);
33static int cifs_dfs_mountpoint_expiry_timeout = 500 * HZ;
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +000034
Igor Mammedov78d31a32008-04-24 12:56:07 +040035static void cifs_dfs_expire_automounts(struct work_struct *work)
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +000036{
Igor Mammedov78d31a32008-04-24 12:56:07 +040037 struct list_head *list = &cifs_dfs_automount_list;
38
39 mark_mounts_for_expiry(list);
40 if (!list_empty(list))
41 schedule_delayed_work(&cifs_dfs_automount_task,
42 cifs_dfs_mountpoint_expiry_timeout);
43}
44
45void cifs_dfs_release_automount_timer(void)
46{
47 BUG_ON(!list_empty(&cifs_dfs_automount_list));
Tejun Heo3e24e132010-10-19 17:55:54 +020048 cancel_delayed_work_sync(&cifs_dfs_automount_task);
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +000049}
50
51/**
52 * cifs_get_share_name - extracts share name from UNC
53 * @node_name: pointer to UNC string
54 *
55 * Extracts sharename form full UNC.
56 * i.e. strips from UNC trailing path that is not part of share
Lucas De Marchi25985ed2011-03-30 22:57:33 -030057 * name and fixup missing '\' in the beginning of DFS node refferal
Daniel Mack3ad2f3f2010-02-03 08:01:28 +080058 * if necessary.
Jeff Layton7b91e262009-07-23 15:22:30 -040059 * Returns pointer to share name on success or ERR_PTR on error.
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +000060 * Caller is responsible for freeing returned string.
61 */
62static char *cifs_get_share_name(const char *node_name)
63{
64 int len;
65 char *UNC;
66 char *pSep;
67
68 len = strlen(node_name);
69 UNC = kmalloc(len+2 /*for term null and additional \ if it's missed */,
70 GFP_KERNEL);
71 if (!UNC)
Jeff Layton7b91e262009-07-23 15:22:30 -040072 return ERR_PTR(-ENOMEM);
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +000073
74 /* get share name and server name */
75 if (node_name[1] != '\\') {
76 UNC[0] = '\\';
77 strncpy(UNC+1, node_name, len);
78 len++;
79 UNC[len] = 0;
80 } else {
81 strncpy(UNC, node_name, len);
82 UNC[len] = 0;
83 }
84
85 /* find server name end */
86 pSep = memchr(UNC+2, '\\', len-2);
87 if (!pSep) {
Joe Perchesf96637b2013-05-04 22:12:25 -050088 cifs_dbg(VFS, "%s: no server name end in node name: %s\n",
89 __func__, node_name);
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +000090 kfree(UNC);
Jeff Layton7b91e262009-07-23 15:22:30 -040091 return ERR_PTR(-EINVAL);
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +000092 }
93
94 /* find sharename end */
95 pSep++;
96 pSep = memchr(UNC+(pSep-UNC), '\\', len-(pSep-UNC));
Igor Mammedovbf62fd82008-04-28 23:05:58 +000097 if (pSep) {
98 /* trim path up to sharename end
99 * now we have share name in UNC */
100 *pSep = 0;
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000101 }
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000102
103 return UNC;
104}
105
106
107/**
Steve Frenchc6c00912009-03-18 05:50:07 +0000108 * cifs_compose_mount_options - creates mount options for refferral
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000109 * @sb_mountdata: parent/root DFS mount options (template)
Steve Frenchc6c00912009-03-18 05:50:07 +0000110 * @fullpath: full path in UNC format
Igor Mammedov2c556082008-10-23 13:58:42 +0400111 * @ref: server's referral
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000112 * @devname: pointer for saving device name
113 *
114 * creates mount options for submount based on template options sb_mountdata
115 * and replacing unc,ip,prefixpath options with ones we've got form ref_unc.
116 *
117 * Returns: pointer to new mount options or ERR_PTR.
118 * Caller is responcible for freeing retunrned value if it is not error.
119 */
Steve Frenchc6c00912009-03-18 05:50:07 +0000120char *cifs_compose_mount_options(const char *sb_mountdata,
121 const char *fullpath,
Igor Mammedov2c556082008-10-23 13:58:42 +0400122 const struct dfs_info3_param *ref,
Steve French366781c2008-01-25 10:12:41 +0000123 char **devname)
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000124{
125 int rc;
Steve Frenchc6fbba02008-12-18 01:41:20 +0000126 char *mountdata = NULL;
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000127 int md_len;
128 char *tkn_e;
129 char *srvIP = NULL;
130 char sep = ',';
131 int off, noff;
132
133 if (sb_mountdata == NULL)
134 return ERR_PTR(-EINVAL);
135
Igor Mammedov2c556082008-10-23 13:58:42 +0400136 *devname = cifs_get_share_name(ref->node_name);
Jeff Layton7b91e262009-07-23 15:22:30 -0400137 if (IS_ERR(*devname)) {
138 rc = PTR_ERR(*devname);
139 *devname = NULL;
140 goto compose_mount_options_err;
141 }
142
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000143 rc = dns_resolve_server_name_to_ip(*devname, &srvIP);
David Howells67b76262010-07-22 18:33:01 +0100144 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500145 cifs_dbg(FYI, "%s: Failed to resolve server part of %s to IP: %d\n",
146 __func__, *devname, rc);
Steve Frenchc6fbba02008-12-18 01:41:20 +0000147 goto compose_mount_options_err;
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000148 }
Jeff Laytonb8028982011-07-26 12:20:18 -0400149
Igor Mammedov2c556082008-10-23 13:58:42 +0400150 /* md_len = strlen(...) + 12 for 'sep+prefixpath='
151 * assuming that we have 'unc=' and 'ip=' in
152 * the original sb_mountdata
153 */
Jeff Layton166faf22013-05-24 07:40:04 -0400154 md_len = strlen(sb_mountdata) + rc + strlen(ref->node_name) + 12 +
155 INET6_ADDRSTRLEN;
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000156 mountdata = kzalloc(md_len+1, GFP_KERNEL);
157 if (mountdata == NULL) {
Steve Frenchc6fbba02008-12-18 01:41:20 +0000158 rc = -ENOMEM;
159 goto compose_mount_options_err;
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000160 }
161
162 /* copy all options except of unc,ip,prefixpath */
163 off = 0;
164 if (strncmp(sb_mountdata, "sep=", 4) == 0) {
165 sep = sb_mountdata[4];
166 strncpy(mountdata, sb_mountdata, 5);
167 off += 5;
168 }
Igor Mammedov2c556082008-10-23 13:58:42 +0400169
170 do {
171 tkn_e = strchr(sb_mountdata + off, sep);
172 if (tkn_e == NULL)
173 noff = strlen(sb_mountdata + off);
174 else
175 noff = tkn_e - (sb_mountdata + off) + 1;
176
177 if (strnicmp(sb_mountdata + off, "unc=", 4) == 0) {
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000178 off += noff;
179 continue;
180 }
Igor Mammedov2c556082008-10-23 13:58:42 +0400181 if (strnicmp(sb_mountdata + off, "ip=", 3) == 0) {
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000182 off += noff;
183 continue;
184 }
Igor Mammedov2c556082008-10-23 13:58:42 +0400185 if (strnicmp(sb_mountdata + off, "prefixpath=", 11) == 0) {
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000186 off += noff;
187 continue;
188 }
Igor Mammedov2c556082008-10-23 13:58:42 +0400189 strncat(mountdata, sb_mountdata + off, noff);
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000190 off += noff;
Igor Mammedov2c556082008-10-23 13:58:42 +0400191 } while (tkn_e);
192 strcat(mountdata, sb_mountdata + off);
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000193 mountdata[md_len] = '\0';
194
195 /* copy new IP and ref share name */
Igor Mammedov2c556082008-10-23 13:58:42 +0400196 if (mountdata[strlen(mountdata) - 1] != sep)
197 strncat(mountdata, &sep, 1);
198 strcat(mountdata, "ip=");
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000199 strcat(mountdata, srvIP);
Igor Mammedov2c556082008-10-23 13:58:42 +0400200 strncat(mountdata, &sep, 1);
201 strcat(mountdata, "unc=");
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000202 strcat(mountdata, *devname);
203
204 /* find & copy prefixpath */
Igor Mammedov2c556082008-10-23 13:58:42 +0400205 tkn_e = strchr(ref->node_name + 2, '\\');
Steve Frenchc6fbba02008-12-18 01:41:20 +0000206 if (tkn_e == NULL) {
207 /* invalid unc, missing share name*/
208 rc = -EINVAL;
209 goto compose_mount_options_err;
210 }
Igor Mammedov2c556082008-10-23 13:58:42 +0400211
Igor Mammedov2c556082008-10-23 13:58:42 +0400212 tkn_e = strchr(tkn_e + 1, '\\');
Steve Frenchc6fbba02008-12-18 01:41:20 +0000213 if (tkn_e || (strlen(fullpath) - ref->path_consumed)) {
Igor Mammedov2c556082008-10-23 13:58:42 +0400214 strncat(mountdata, &sep, 1);
215 strcat(mountdata, "prefixpath=");
216 if (tkn_e)
217 strcat(mountdata, tkn_e + 1);
Steve Frenchc6fbba02008-12-18 01:41:20 +0000218 strcat(mountdata, fullpath + ref->path_consumed);
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000219 }
220
Joe Perchesf96637b2013-05-04 22:12:25 -0500221 /*cifs_dbg(FYI, "%s: parent mountdata: %s\n", __func__, sb_mountdata);*/
222 /*cifs_dbg(FYI, "%s: submount mountdata: %s\n", __func__, mountdata );*/
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000223
224compose_mount_options_out:
225 kfree(srvIP);
226 return mountdata;
Steve Frenchc6fbba02008-12-18 01:41:20 +0000227
228compose_mount_options_err:
229 kfree(mountdata);
230 mountdata = ERR_PTR(rc);
Cong Ding10b8c7d2013-01-22 19:20:58 -0500231 kfree(*devname);
232 *devname = NULL;
Steve Frenchc6fbba02008-12-18 01:41:20 +0000233 goto compose_mount_options_out;
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000234}
235
Steve Frenchf67909c2010-07-26 18:20:16 +0000236/**
237 * cifs_dfs_do_refmount - mounts specified path using provided refferal
238 * @cifs_sb: parent/root superblock
239 * @fullpath: full path in UNC format
240 * @ref: server's referral
241 */
242static struct vfsmount *cifs_dfs_do_refmount(struct cifs_sb_info *cifs_sb,
243 const char *fullpath, const struct dfs_info3_param *ref)
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000244{
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000245 struct vfsmount *mnt;
246 char *mountdata;
Steve French366781c2008-01-25 10:12:41 +0000247 char *devname = NULL;
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000248
Steve Frenchf67909c2010-07-26 18:20:16 +0000249 /* strip first '\' from fullpath */
Steve Frenchc6c00912009-03-18 05:50:07 +0000250 mountdata = cifs_compose_mount_options(cifs_sb->mountdata,
251 fullpath + 1, ref, &devname);
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000252
253 if (IS_ERR(mountdata))
254 return (struct vfsmount *)mountdata;
255
256 mnt = vfs_kern_mount(&cifs_fs_type, 0, devname, mountdata);
257 kfree(mountdata);
258 kfree(devname);
259 return mnt;
260
261}
262
Steve French366781c2008-01-25 10:12:41 +0000263static void dump_referral(const struct dfs_info3_param *ref)
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000264{
Joe Perchesf96637b2013-05-04 22:12:25 -0500265 cifs_dbg(FYI, "DFS: ref path: %s\n", ref->path_name);
266 cifs_dbg(FYI, "DFS: node path: %s\n", ref->node_name);
267 cifs_dbg(FYI, "DFS: fl: %hd, srv_type: %hd\n",
268 ref->flags, ref->server_type);
269 cifs_dbg(FYI, "DFS: ref_flags: %hd, path_consumed: %hd\n",
270 ref->ref_flag, ref->path_consumed);
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000271}
272
David Howells01c64fe2011-01-14 18:45:47 +0000273/*
274 * Create a vfsmount that we can automount
275 */
276static struct vfsmount *cifs_dfs_do_automount(struct dentry *mntpt)
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000277{
278 struct dfs_info3_param *referrals = NULL;
279 unsigned int num_referrals = 0;
280 struct cifs_sb_info *cifs_sb;
Steve French96daf2b2011-05-27 04:34:02 +0000281 struct cifs_ses *ses;
David Howells01c64fe2011-01-14 18:45:47 +0000282 char *full_path;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400283 unsigned int xid;
284 int i;
David Howells01c64fe2011-01-14 18:45:47 +0000285 int rc;
286 struct vfsmount *mnt;
Jeff Layton7ffec372010-09-29 19:51:11 -0400287 struct tcon_link *tlink;
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000288
Joe Perchesf96637b2013-05-04 22:12:25 -0500289 cifs_dbg(FYI, "in %s\n", __func__);
David Howells01c64fe2011-01-14 18:45:47 +0000290 BUG_ON(IS_ROOT(mntpt));
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000291
Steve Frenchc6fbba02008-12-18 01:41:20 +0000292 /*
293 * The MSDFS spec states that paths in DFS referral requests and
294 * responses must be prefixed by a single '\' character instead of
295 * the double backslashes usually used in the UNC. This function
296 * gives us the latter, so we must adjust the result.
297 */
David Howells01c64fe2011-01-14 18:45:47 +0000298 mnt = ERR_PTR(-ENOMEM);
299 full_path = build_path_from_dentry(mntpt);
300 if (full_path == NULL)
Jeff Layton31c26592011-01-31 07:24:46 -0500301 goto cdda_exit;
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000302
David Howells01c64fe2011-01-14 18:45:47 +0000303 cifs_sb = CIFS_SB(mntpt->d_inode->i_sb);
Jeff Layton7ffec372010-09-29 19:51:11 -0400304 tlink = cifs_sb_tlink(cifs_sb);
305 if (IS_ERR(tlink)) {
David Howells01c64fe2011-01-14 18:45:47 +0000306 mnt = ERR_CAST(tlink);
307 goto free_full_path;
Jeff Layton7ffec372010-09-29 19:51:11 -0400308 }
309 ses = tlink_tcon(tlink)->ses;
310
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400311 xid = get_xid();
Jeff Layton7ffec372010-09-29 19:51:11 -0400312 rc = get_dfs_path(xid, ses, full_path + 1, cifs_sb->local_nls,
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000313 &num_referrals, &referrals,
314 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400315 free_xid(xid);
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000316
Jeff Layton7ffec372010-09-29 19:51:11 -0400317 cifs_put_tlink(tlink);
318
David Howells01c64fe2011-01-14 18:45:47 +0000319 mnt = ERR_PTR(-ENOENT);
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000320 for (i = 0; i < num_referrals; i++) {
Steve Frenchcf398e32009-05-01 03:50:42 +0000321 int len;
David Howells01c64fe2011-01-14 18:45:47 +0000322 dump_referral(referrals + i);
Igor Mammedov1af28ce2009-03-17 19:00:30 +0300323 /* connect to a node */
Igor Mammedov1af28ce2009-03-17 19:00:30 +0300324 len = strlen(referrals[i].node_name);
325 if (len < 2) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500326 cifs_dbg(VFS, "%s: Net Address path too short: %s\n",
327 __func__, referrals[i].node_name);
David Howells01c64fe2011-01-14 18:45:47 +0000328 mnt = ERR_PTR(-EINVAL);
329 break;
Igor Mammedov1af28ce2009-03-17 19:00:30 +0300330 }
Steve Frenchf67909c2010-07-26 18:20:16 +0000331 mnt = cifs_dfs_do_refmount(cifs_sb,
332 full_path, referrals + i);
Joe Perchesf96637b2013-05-04 22:12:25 -0500333 cifs_dbg(FYI, "%s: cifs_dfs_do_refmount:%s , mnt:%p\n",
334 __func__, referrals[i].node_name, mnt);
Igor Mammedov1af28ce2009-03-17 19:00:30 +0300335 if (!IS_ERR(mnt))
David Howells01c64fe2011-01-14 18:45:47 +0000336 goto success;
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000337 }
338
David Howells01c64fe2011-01-14 18:45:47 +0000339 /* no valid submounts were found; return error from get_dfs_path() by
340 * preference */
341 if (rc != 0)
342 mnt = ERR_PTR(rc);
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000343
David Howells01c64fe2011-01-14 18:45:47 +0000344success:
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000345 free_dfs_info_array(referrals, num_referrals);
David Howells01c64fe2011-01-14 18:45:47 +0000346free_full_path:
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000347 kfree(full_path);
Jeff Layton31c26592011-01-31 07:24:46 -0500348cdda_exit:
Joe Perchesf96637b2013-05-04 22:12:25 -0500349 cifs_dbg(FYI, "leaving %s\n" , __func__);
David Howells01c64fe2011-01-14 18:45:47 +0000350 return mnt;
351}
352
353/*
354 * Attempt to automount the referral
355 */
356struct vfsmount *cifs_dfs_d_automount(struct path *path)
357{
358 struct vfsmount *newmnt;
David Howells01c64fe2011-01-14 18:45:47 +0000359
Joe Perchesf96637b2013-05-04 22:12:25 -0500360 cifs_dbg(FYI, "in %s\n", __func__);
David Howells01c64fe2011-01-14 18:45:47 +0000361
362 newmnt = cifs_dfs_do_automount(path->dentry);
363 if (IS_ERR(newmnt)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500364 cifs_dbg(FYI, "leaving %s [automount failed]\n" , __func__);
David Howells01c64fe2011-01-14 18:45:47 +0000365 return newmnt;
366 }
367
David Howellsea5b7782011-01-14 19:10:03 +0000368 mntget(newmnt); /* prevent immediate expiration */
369 mnt_set_expiry(newmnt, &cifs_dfs_automount_list);
370 schedule_delayed_work(&cifs_dfs_automount_task,
371 cifs_dfs_mountpoint_expiry_timeout);
Joe Perchesf96637b2013-05-04 22:12:25 -0500372 cifs_dbg(FYI, "leaving %s [ok]\n" , __func__);
David Howellsea5b7782011-01-14 19:10:03 +0000373 return newmnt;
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000374}
375
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700376const struct inode_operations cifs_dfs_referral_inode_operations = {
Igor Mammedov6d5ae0d2008-01-25 03:28:31 +0000377};