~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/cifs/cifs_dfs_ref.c

Version: ~ [ linux-5.15-rc5 ] ~ [ linux-5.14.11 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.72 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.152 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.210 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.250 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.286 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.288 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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/vfs.h>
 19 #include <linux/fs.h>
 20 #include "cifsglob.h"
 21 #include "cifsproto.h"
 22 #include "cifsfs.h"
 23 #include "dns_resolve.h"
 24 #include "cifs_debug.h"
 25 
 26 static LIST_HEAD(cifs_dfs_automount_list);
 27 
 28 static void cifs_dfs_expire_automounts(struct work_struct *work);
 29 static DECLARE_DELAYED_WORK(cifs_dfs_automount_task,
 30                             cifs_dfs_expire_automounts);
 31 static int cifs_dfs_mountpoint_expiry_timeout = 500 * HZ;
 32 
 33 static void cifs_dfs_expire_automounts(struct work_struct *work)
 34 {
 35         struct list_head *list = &cifs_dfs_automount_list;
 36 
 37         mark_mounts_for_expiry(list);
 38         if (!list_empty(list))
 39                 schedule_delayed_work(&cifs_dfs_automount_task,
 40                                       cifs_dfs_mountpoint_expiry_timeout);
 41 }
 42 
 43 void cifs_dfs_release_automount_timer(void)
 44 {
 45         BUG_ON(!list_empty(&cifs_dfs_automount_list));
 46         cancel_delayed_work(&cifs_dfs_automount_task);
 47         flush_scheduled_work();
 48 }
 49 
 50 /**
 51  * cifs_get_share_name  -       extracts share name from UNC
 52  * @node_name:  pointer to UNC string
 53  *
 54  * Extracts sharename form full UNC.
 55  * i.e. strips from UNC trailing path that is not part of share
 56  * name and fixup missing '\' in the begining of DFS node refferal
 57  * if neccessary.
 58  * Returns pointer to share name on success or ERR_PTR on error.
 59  * Caller is responsible for freeing returned string.
 60  */
 61 static char *cifs_get_share_name(const char *node_name)
 62 {
 63         int len;
 64         char *UNC;
 65         char *pSep;
 66 
 67         len = strlen(node_name);
 68         UNC = kmalloc(len+2 /*for term null and additional \ if it's missed */,
 69                          GFP_KERNEL);
 70         if (!UNC)
 71                 return ERR_PTR(-ENOMEM);
 72 
 73         /* get share name and server name */
 74         if (node_name[1] != '\\') {
 75                 UNC[0] = '\\';
 76                 strncpy(UNC+1, node_name, len);
 77                 len++;
 78                 UNC[len] = 0;
 79         } else {
 80                 strncpy(UNC, node_name, len);
 81                 UNC[len] = 0;
 82         }
 83 
 84         /* find server name end */
 85         pSep = memchr(UNC+2, '\\', len-2);
 86         if (!pSep) {
 87                 cERROR(1, ("%s: no server name end in node name: %s",
 88                         __func__, node_name));
 89                 kfree(UNC);
 90                 return ERR_PTR(-EINVAL);
 91         }
 92 
 93         /* find sharename end */
 94         pSep++;
 95         pSep = memchr(UNC+(pSep-UNC), '\\', len-(pSep-UNC));
 96         if (pSep) {
 97                 /* trim path up to sharename end
 98                  * now we have share name in UNC */
 99                 *pSep = 0;
100         }
101 
102         return UNC;
103 }
104 
105 
106 /**
107  * cifs_compose_mount_options   -       creates mount options for refferral
108  * @sb_mountdata:       parent/root DFS mount options (template)
109  * @fullpath:           full path in UNC format
110  * @ref:                server's referral
111  * @devname:            pointer for saving device name
112  *
113  * creates mount options for submount based on template options sb_mountdata
114  * and replacing unc,ip,prefixpath options with ones we've got form ref_unc.
115  *
116  * Returns: pointer to new mount options or ERR_PTR.
117  * Caller is responcible for freeing retunrned value if it is not error.
118  */
119 char *cifs_compose_mount_options(const char *sb_mountdata,
120                                    const char *fullpath,
121                                    const struct dfs_info3_param *ref,
122                                    char **devname)
123 {
124         int rc;
125         char *mountdata = NULL;
126         int md_len;
127         char *tkn_e;
128         char *srvIP = NULL;
129         char sep = ',';
130         int off, noff;
131 
132         if (sb_mountdata == NULL)
133                 return ERR_PTR(-EINVAL);
134 
135         *devname = cifs_get_share_name(ref->node_name);
136         if (IS_ERR(*devname)) {
137                 rc = PTR_ERR(*devname);
138                 *devname = NULL;
139                 goto compose_mount_options_err;
140         }
141 
142         rc = dns_resolve_server_name_to_ip(*devname, &srvIP);
143         if (rc != 0) {
144                 cERROR(1, ("%s: Failed to resolve server part of %s to IP: %d",
145                           __func__, *devname, rc));
146                 goto compose_mount_options_err;
147         }
148         /* md_len = strlen(...) + 12 for 'sep+prefixpath='
149          * assuming that we have 'unc=' and 'ip=' in
150          * the original sb_mountdata
151          */
152         md_len = strlen(sb_mountdata) + strlen(srvIP) +
153                 strlen(ref->node_name) + 12;
154         mountdata = kzalloc(md_len+1, GFP_KERNEL);
155         if (mountdata == NULL) {
156                 rc = -ENOMEM;
157                 goto compose_mount_options_err;
158         }
159 
160         /* copy all options except of unc,ip,prefixpath */
161         off = 0;
162         if (strncmp(sb_mountdata, "sep=", 4) == 0) {
163                         sep = sb_mountdata[4];
164                         strncpy(mountdata, sb_mountdata, 5);
165                         off += 5;
166         }
167 
168         do {
169                 tkn_e = strchr(sb_mountdata + off, sep);
170                 if (tkn_e == NULL)
171                         noff = strlen(sb_mountdata + off);
172                 else
173                         noff = tkn_e - (sb_mountdata + off) + 1;
174 
175                 if (strnicmp(sb_mountdata + off, "unc=", 4) == 0) {
176                         off += noff;
177                         continue;
178                 }
179                 if (strnicmp(sb_mountdata + off, "ip=", 3) == 0) {
180                         off += noff;
181                         continue;
182                 }
183                 if (strnicmp(sb_mountdata + off, "prefixpath=", 11) == 0) {
184                         off += noff;
185                         continue;
186                 }
187                 strncat(mountdata, sb_mountdata + off, noff);
188                 off += noff;
189         } while (tkn_e);
190         strcat(mountdata, sb_mountdata + off);
191         mountdata[md_len] = '\0';
192 
193         /* copy new IP and ref share name */
194         if (mountdata[strlen(mountdata) - 1] != sep)
195                 strncat(mountdata, &sep, 1);
196         strcat(mountdata, "ip=");
197         strcat(mountdata, srvIP);
198         strncat(mountdata, &sep, 1);
199         strcat(mountdata, "unc=");
200         strcat(mountdata, *devname);
201 
202         /* find & copy prefixpath */
203         tkn_e = strchr(ref->node_name + 2, '\\');
204         if (tkn_e == NULL) {
205                 /* invalid unc, missing share name*/
206                 rc = -EINVAL;
207                 goto compose_mount_options_err;
208         }
209 
210         tkn_e = strchr(tkn_e + 1, '\\');
211         if (tkn_e || (strlen(fullpath) - ref->path_consumed)) {
212                 strncat(mountdata, &sep, 1);
213                 strcat(mountdata, "prefixpath=");
214                 if (tkn_e)
215                         strcat(mountdata, tkn_e + 1);
216                 strcat(mountdata, fullpath + ref->path_consumed);
217         }
218 
219         /*cFYI(1,("%s: parent mountdata: %s", __func__,sb_mountdata));*/
220         /*cFYI(1, ("%s: submount mountdata: %s", __func__, mountdata ));*/
221 
222 compose_mount_options_out:
223         kfree(srvIP);
224         return mountdata;
225 
226 compose_mount_options_err:
227         kfree(mountdata);
228         mountdata = ERR_PTR(rc);
229         kfree(*devname);
230         *devname = NULL;
231         goto compose_mount_options_out;
232 }
233 
234 
235 static struct vfsmount *cifs_dfs_do_refmount(const struct vfsmount *mnt_parent,
236                 struct dentry *dentry, const struct dfs_info3_param *ref)
237 {
238         struct cifs_sb_info *cifs_sb;
239         struct vfsmount *mnt;
240         char *mountdata;
241         char *devname = NULL;
242         char *fullpath;
243 
244         cifs_sb = CIFS_SB(dentry->d_inode->i_sb);
245         /*
246          * this function gives us a path with a double backslash prefix. We
247          * require a single backslash for DFS.
248          */
249         fullpath = build_path_from_dentry(dentry);
250         if (!fullpath)
251                 return ERR_PTR(-ENOMEM);
252 
253         mountdata = cifs_compose_mount_options(cifs_sb->mountdata,
254                         fullpath + 1, ref, &devname);
255         kfree(fullpath);
256 
257         if (IS_ERR(mountdata))
258                 return (struct vfsmount *)mountdata;
259 
260         mnt = vfs_kern_mount(&cifs_fs_type, 0, devname, mountdata);
261         kfree(mountdata);
262         kfree(devname);
263         return mnt;
264 
265 }
266 
267 static int add_mount_helper(struct vfsmount *newmnt, struct nameidata *nd,
268                                 struct list_head *mntlist)
269 {
270         /* stolen from afs code */
271         int err;
272 
273         mntget(newmnt);
274         err = do_add_mount(newmnt, &nd->path, nd->path.mnt->mnt_flags, mntlist);
275         switch (err) {
276         case 0:
277                 path_put(&nd->path);
278                 nd->path.mnt = newmnt;
279                 nd->path.dentry = dget(newmnt->mnt_root);
280                 schedule_delayed_work(&cifs_dfs_automount_task,
281                                       cifs_dfs_mountpoint_expiry_timeout);
282                 break;
283         case -EBUSY:
284                 /* someone else made a mount here whilst we were busy */
285                 while (d_mountpoint(nd->path.dentry) &&
286                        follow_down(&nd->path))
287                         ;
288                 err = 0;
289         default:
290                 mntput(newmnt);
291                 break;
292         }
293         return err;
294 }
295 
296 static void dump_referral(const struct dfs_info3_param *ref)
297 {
298         cFYI(1, ("DFS: ref path: %s", ref->path_name));
299         cFYI(1, ("DFS: node path: %s", ref->node_name));
300         cFYI(1, ("DFS: fl: %hd, srv_type: %hd", ref->flags, ref->server_type));
301         cFYI(1, ("DFS: ref_flags: %hd, path_consumed: %hd", ref->ref_flag,
302                                 ref->path_consumed));
303 }
304 
305 
306 static void*
307 cifs_dfs_follow_mountpoint(struct dentry *dentry, struct nameidata *nd)
308 {
309         struct dfs_info3_param *referrals = NULL;
310         unsigned int num_referrals = 0;
311         struct cifs_sb_info *cifs_sb;
312         struct cifsSesInfo *ses;
313         char *full_path = NULL;
314         int xid, i;
315         int rc = 0;
316         struct vfsmount *mnt = ERR_PTR(-ENOENT);
317 
318         cFYI(1, ("in %s", __func__));
319         BUG_ON(IS_ROOT(dentry));
320 
321         xid = GetXid();
322 
323         dput(nd->path.dentry);
324         nd->path.dentry = dget(dentry);
325 
326         cifs_sb = CIFS_SB(dentry->d_inode->i_sb);
327         ses = cifs_sb->tcon->ses;
328 
329         if (!ses) {
330                 rc = -EINVAL;
331                 goto out_err;
332         }
333 
334         /*
335          * The MSDFS spec states that paths in DFS referral requests and
336          * responses must be prefixed by a single '\' character instead of
337          * the double backslashes usually used in the UNC. This function
338          * gives us the latter, so we must adjust the result.
339          */
340         full_path = build_path_from_dentry(dentry);
341         if (full_path == NULL) {
342                 rc = -ENOMEM;
343                 goto out_err;
344         }
345 
346         rc = get_dfs_path(xid, ses , full_path + 1, cifs_sb->local_nls,
347                 &num_referrals, &referrals,
348                 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
349 
350         for (i = 0; i < num_referrals; i++) {
351                 int len;
352                 dump_referral(referrals+i);
353                 /* connect to a node */
354                 len = strlen(referrals[i].node_name);
355                 if (len < 2) {
356                         cERROR(1, ("%s: Net Address path too short: %s",
357                                         __func__, referrals[i].node_name));
358                         rc = -EINVAL;
359                         goto out_err;
360                 }
361                 mnt = cifs_dfs_do_refmount(nd->path.mnt,
362                                 nd->path.dentry, referrals + i);
363                 cFYI(1, ("%s: cifs_dfs_do_refmount:%s , mnt:%p", __func__,
364                                         referrals[i].node_name, mnt));
365 
366                 /* complete mount procedure if we accured submount */
367                 if (!IS_ERR(mnt))
368                         break;
369         }
370 
371         /* we need it cause for() above could exit without valid submount */
372         rc = PTR_ERR(mnt);
373         if (IS_ERR(mnt))
374                 goto out_err;
375 
376         nd->path.mnt->mnt_flags |= MNT_SHRINKABLE;
377         rc = add_mount_helper(mnt, nd, &cifs_dfs_automount_list);
378 
379 out:
380         FreeXid(xid);
381         free_dfs_info_array(referrals, num_referrals);
382         kfree(full_path);
383         cFYI(1, ("leaving %s" , __func__));
384         return ERR_PTR(rc);
385 out_err:
386         path_put(&nd->path);
387         goto out;
388 }
389 
390 const struct inode_operations cifs_dfs_referral_inode_operations = {
391         .follow_link = cifs_dfs_follow_mountpoint,
392 };
393 
394 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp