pandora: defconfig: update
[pandora-kernel.git] / fs / cifs / cifs_dfs_ref.c
1 /*
2  *   Contains the CIFS DFS referral mounting routines used for handling
3  *   traversal via DFS junction point
4  *
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.
13  */
14
15 #include <linux/dcache.h>
16 #include <linux/mount.h>
17 #include <linux/namei.h>
18 #include <linux/slab.h>
19 #include <linux/vfs.h>
20 #include <linux/fs.h>
21 #include <linux/inet.h>
22 #include "cifsglob.h"
23 #include "cifsproto.h"
24 #include "cifsfs.h"
25 #include "dns_resolve.h"
26 #include "cifs_debug.h"
27
28 static LIST_HEAD(cifs_dfs_automount_list);
29
30 static void cifs_dfs_expire_automounts(struct work_struct *work);
31 static DECLARE_DELAYED_WORK(cifs_dfs_automount_task,
32                             cifs_dfs_expire_automounts);
33 static int cifs_dfs_mountpoint_expiry_timeout = 500 * HZ;
34
35 static void cifs_dfs_expire_automounts(struct work_struct *work)
36 {
37         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
45 void cifs_dfs_release_automount_timer(void)
46 {
47         BUG_ON(!list_empty(&cifs_dfs_automount_list));
48         cancel_delayed_work_sync(&cifs_dfs_automount_task);
49 }
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
57  * name and fixup missing '\' in the beginning of DFS node refferal
58  * if necessary.
59  * Returns pointer to share name on success or ERR_PTR on error.
60  * Caller is responsible for freeing returned string.
61  */
62 static 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)
72                 return ERR_PTR(-ENOMEM);
73
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) {
88                 cERROR(1, "%s: no server name end in node name: %s",
89                         __func__, node_name);
90                 kfree(UNC);
91                 return ERR_PTR(-EINVAL);
92         }
93
94         /* find sharename end */
95         pSep++;
96         pSep = memchr(UNC+(pSep-UNC), '\\', len-(pSep-UNC));
97         if (pSep) {
98                 /* trim path up to sharename end
99                  * now we have share name in UNC */
100                 *pSep = 0;
101         }
102
103         return UNC;
104 }
105
106
107 /**
108  * cifs_compose_mount_options   -       creates mount options for refferral
109  * @sb_mountdata:       parent/root DFS mount options (template)
110  * @fullpath:           full path in UNC format
111  * @ref:                server's referral
112  * @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  */
120 char *cifs_compose_mount_options(const char *sb_mountdata,
121                                    const char *fullpath,
122                                    const struct dfs_info3_param *ref,
123                                    char **devname)
124 {
125         int rc;
126         char *mountdata = NULL;
127         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
136         *devname = cifs_get_share_name(ref->node_name);
137         if (IS_ERR(*devname)) {
138                 rc = PTR_ERR(*devname);
139                 *devname = NULL;
140                 goto compose_mount_options_err;
141         }
142
143         rc = dns_resolve_server_name_to_ip(*devname, &srvIP);
144         if (rc < 0) {
145                 cFYI(1, "%s: Failed to resolve server part of %s to IP: %d",
146                         __func__, *devname, rc);
147                 goto compose_mount_options_err;
148         }
149
150         /* md_len = strlen(...) + 12 for 'sep+prefixpath='
151          * assuming that we have 'unc=' and 'ip=' in
152          * the original sb_mountdata
153          */
154         md_len = strlen(sb_mountdata) + rc + strlen(ref->node_name) + 12 +
155                         INET6_ADDRSTRLEN;
156         mountdata = kzalloc(md_len+1, GFP_KERNEL);
157         if (mountdata == NULL) {
158                 rc = -ENOMEM;
159                 goto compose_mount_options_err;
160         }
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         }
169
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) {
178                         off += noff;
179                         continue;
180                 }
181                 if (strnicmp(sb_mountdata + off, "ip=", 3) == 0) {
182                         off += noff;
183                         continue;
184                 }
185                 if (strnicmp(sb_mountdata + off, "prefixpath=", 11) == 0) {
186                         off += noff;
187                         continue;
188                 }
189                 strncat(mountdata, sb_mountdata + off, noff);
190                 off += noff;
191         } while (tkn_e);
192         strcat(mountdata, sb_mountdata + off);
193         mountdata[md_len] = '\0';
194
195         /* copy new IP and ref share name */
196         if (mountdata[strlen(mountdata) - 1] != sep)
197                 strncat(mountdata, &sep, 1);
198         strcat(mountdata, "ip=");
199         strcat(mountdata, srvIP);
200         strncat(mountdata, &sep, 1);
201         strcat(mountdata, "unc=");
202         strcat(mountdata, *devname);
203
204         /* find & copy prefixpath */
205         tkn_e = strchr(ref->node_name + 2, '\\');
206         if (tkn_e == NULL) {
207                 /* invalid unc, missing share name*/
208                 rc = -EINVAL;
209                 goto compose_mount_options_err;
210         }
211
212         tkn_e = strchr(tkn_e + 1, '\\');
213         if (tkn_e || (strlen(fullpath) - ref->path_consumed)) {
214                 strncat(mountdata, &sep, 1);
215                 strcat(mountdata, "prefixpath=");
216                 if (tkn_e)
217                         strcat(mountdata, tkn_e + 1);
218                 strcat(mountdata, fullpath + ref->path_consumed);
219         }
220
221         /*cFYI(1, "%s: parent mountdata: %s", __func__,sb_mountdata);*/
222         /*cFYI(1, "%s: submount mountdata: %s", __func__, mountdata );*/
223
224 compose_mount_options_out:
225         kfree(srvIP);
226         return mountdata;
227
228 compose_mount_options_err:
229         kfree(mountdata);
230         mountdata = ERR_PTR(rc);
231         kfree(*devname);
232         *devname = NULL;
233         goto compose_mount_options_out;
234 }
235
236 /**
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  */
242 static struct vfsmount *cifs_dfs_do_refmount(struct cifs_sb_info *cifs_sb,
243                 const char *fullpath, const struct dfs_info3_param *ref)
244 {
245         struct vfsmount *mnt;
246         char *mountdata;
247         char *devname = NULL;
248
249         /* strip first '\' from fullpath */
250         mountdata = cifs_compose_mount_options(cifs_sb->mountdata,
251                         fullpath + 1, ref, &devname);
252
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
263 static void dump_referral(const struct dfs_info3_param *ref)
264 {
265         cFYI(1, "DFS: ref path: %s", ref->path_name);
266         cFYI(1, "DFS: node path: %s", ref->node_name);
267         cFYI(1, "DFS: fl: %hd, srv_type: %hd", ref->flags, ref->server_type);
268         cFYI(1, "DFS: ref_flags: %hd, path_consumed: %hd", ref->ref_flag,
269                                 ref->path_consumed);
270 }
271
272 /*
273  * Create a vfsmount that we can automount
274  */
275 static struct vfsmount *cifs_dfs_do_automount(struct dentry *mntpt)
276 {
277         struct dfs_info3_param *referrals = NULL;
278         unsigned int num_referrals = 0;
279         struct cifs_sb_info *cifs_sb;
280         struct cifs_ses *ses;
281         char *full_path;
282         int xid, i;
283         int rc;
284         struct vfsmount *mnt;
285         struct tcon_link *tlink;
286
287         cFYI(1, "in %s", __func__);
288         BUG_ON(IS_ROOT(mntpt));
289
290         /*
291          * The MSDFS spec states that paths in DFS referral requests and
292          * responses must be prefixed by a single '\' character instead of
293          * the double backslashes usually used in the UNC. This function
294          * gives us the latter, so we must adjust the result.
295          */
296         mnt = ERR_PTR(-ENOMEM);
297         full_path = build_path_from_dentry(mntpt);
298         if (full_path == NULL)
299                 goto cdda_exit;
300
301         cifs_sb = CIFS_SB(mntpt->d_inode->i_sb);
302         tlink = cifs_sb_tlink(cifs_sb);
303         if (IS_ERR(tlink)) {
304                 mnt = ERR_CAST(tlink);
305                 goto free_full_path;
306         }
307         ses = tlink_tcon(tlink)->ses;
308
309         xid = GetXid();
310         rc = get_dfs_path(xid, ses, full_path + 1, cifs_sb->local_nls,
311                 &num_referrals, &referrals,
312                 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
313         FreeXid(xid);
314
315         cifs_put_tlink(tlink);
316
317         mnt = ERR_PTR(-ENOENT);
318         for (i = 0; i < num_referrals; i++) {
319                 int len;
320                 dump_referral(referrals + i);
321                 /* connect to a node */
322                 len = strlen(referrals[i].node_name);
323                 if (len < 2) {
324                         cERROR(1, "%s: Net Address path too short: %s",
325                                         __func__, referrals[i].node_name);
326                         mnt = ERR_PTR(-EINVAL);
327                         break;
328                 }
329                 mnt = cifs_dfs_do_refmount(cifs_sb,
330                                 full_path, referrals + i);
331                 cFYI(1, "%s: cifs_dfs_do_refmount:%s , mnt:%p", __func__,
332                                         referrals[i].node_name, mnt);
333                 if (!IS_ERR(mnt))
334                         goto success;
335         }
336
337         /* no valid submounts were found; return error from get_dfs_path() by
338          * preference */
339         if (rc != 0)
340                 mnt = ERR_PTR(rc);
341
342 success:
343         free_dfs_info_array(referrals, num_referrals);
344 free_full_path:
345         kfree(full_path);
346 cdda_exit:
347         cFYI(1, "leaving %s" , __func__);
348         return mnt;
349 }
350
351 /*
352  * Attempt to automount the referral
353  */
354 struct vfsmount *cifs_dfs_d_automount(struct path *path)
355 {
356         struct vfsmount *newmnt;
357
358         cFYI(1, "in %s", __func__);
359
360         newmnt = cifs_dfs_do_automount(path->dentry);
361         if (IS_ERR(newmnt)) {
362                 cFYI(1, "leaving %s [automount failed]" , __func__);
363                 return newmnt;
364         }
365
366         mntget(newmnt); /* prevent immediate expiration */
367         mnt_set_expiry(newmnt, &cifs_dfs_automount_list);
368         schedule_delayed_work(&cifs_dfs_automount_task,
369                               cifs_dfs_mountpoint_expiry_timeout);
370         cFYI(1, "leaving %s [ok]" , __func__);
371         return newmnt;
372 }
373
374 const struct inode_operations cifs_dfs_referral_inode_operations = {
375 };