2525#include "dns_resolve.h"
2626#include "cifs_debug.h"
2727#include "cifs_unicode.h"
28+ #include "dfs_cache.h"
2829
2930static LIST_HEAD (cifs_dfs_automount_list );
3031
@@ -126,7 +127,7 @@ cifs_build_devname(char *nodename, const char *prepath)
126127 * @sb_mountdata: parent/root DFS mount options (template)
127128 * @fullpath: full path in UNC format
128129 * @ref: server's referral
129- * @devname: pointer for saving device name
130+ * @devname: optional pointer for saving device name
130131 *
131132 * creates mount options for submount based on template options sb_mountdata
132133 * and replacing unc,ip,prefixpath options with ones we've got form ref_unc.
@@ -140,6 +141,7 @@ char *cifs_compose_mount_options(const char *sb_mountdata,
140141 char * * devname )
141142{
142143 int rc ;
144+ char * name ;
143145 char * mountdata = NULL ;
144146 const char * prepath = NULL ;
145147 int md_len ;
@@ -158,17 +160,17 @@ char *cifs_compose_mount_options(const char *sb_mountdata,
158160 prepath ++ ;
159161 }
160162
161- * devname = cifs_build_devname (ref -> node_name , prepath );
162- if (IS_ERR (* devname )) {
163- rc = PTR_ERR (* devname );
164- * devname = NULL ;
163+ name = cifs_build_devname (ref -> node_name , prepath );
164+ if (IS_ERR (name )) {
165+ rc = PTR_ERR (name );
166+ name = NULL ;
165167 goto compose_mount_options_err ;
166168 }
167169
168- rc = dns_resolve_server_name_to_ip (* devname , & srvIP );
170+ rc = dns_resolve_server_name_to_ip (name , & srvIP );
169171 if (rc < 0 ) {
170172 cifs_dbg (FYI , "%s: Failed to resolve server part of %s to IP: %d\n" ,
171- __func__ , * devname , rc );
173+ __func__ , name , rc );
172174 goto compose_mount_options_err ;
173175 }
174176
@@ -224,6 +226,9 @@ char *cifs_compose_mount_options(const char *sb_mountdata,
224226 strcat (mountdata , "ip=" );
225227 strcat (mountdata , srvIP );
226228
229+ if (devname )
230+ * devname = name ;
231+
227232 /*cifs_dbg(FYI, "%s: parent mountdata: %s\n", __func__, sb_mountdata);*/
228233 /*cifs_dbg(FYI, "%s: submount mountdata: %s\n", __func__, mountdata );*/
229234
@@ -234,8 +239,7 @@ char *cifs_compose_mount_options(const char *sb_mountdata,
234239compose_mount_options_err :
235240 kfree (mountdata );
236241 mountdata = ERR_PTR (rc );
237- kfree (* devname );
238- * devname = NULL ;
242+ kfree (name );
239243 goto compose_mount_options_out ;
240244}
241245
@@ -251,20 +255,30 @@ static struct vfsmount *cifs_dfs_do_refmount(struct dentry *mntpt,
251255{
252256 struct vfsmount * mnt ;
253257 char * mountdata ;
254- char * devname = NULL ;
258+ char * devname ;
259+
260+ /*
261+ * Always pass down the DFS full path to smb3_do_mount() so we
262+ * can use it later for failover.
263+ */
264+ devname = kstrndup (fullpath , strlen (fullpath ), GFP_KERNEL );
265+ if (!devname )
266+ return ERR_PTR (- ENOMEM );
267+
268+ convert_delimiter (devname , '/' );
255269
256270 /* strip first '\' from fullpath */
257271 mountdata = cifs_compose_mount_options (cifs_sb -> mountdata ,
258- fullpath + 1 , ref , & devname );
259-
260- if ( IS_ERR ( mountdata ))
272+ fullpath + 1 , ref , NULL );
273+ if ( IS_ERR ( mountdata )) {
274+ kfree ( devname );
261275 return (struct vfsmount * )mountdata ;
276+ }
262277
263278 mnt = vfs_submount (mntpt , & cifs_fs_type , devname , mountdata );
264279 kfree (mountdata );
265280 kfree (devname );
266281 return mnt ;
267-
268282}
269283
270284static void dump_referral (const struct dfs_info3_param * ref )
@@ -282,16 +296,15 @@ static void dump_referral(const struct dfs_info3_param *ref)
282296 */
283297static struct vfsmount * cifs_dfs_do_automount (struct dentry * mntpt )
284298{
285- struct dfs_info3_param * referrals = NULL ;
286- unsigned int num_referrals = 0 ;
299+ struct dfs_info3_param referral = {0 };
287300 struct cifs_sb_info * cifs_sb ;
288301 struct cifs_ses * ses ;
289- char * full_path ;
302+ struct cifs_tcon * tcon ;
303+ char * full_path , * root_path ;
290304 unsigned int xid ;
291- int i ;
305+ int len ;
292306 int rc ;
293307 struct vfsmount * mnt ;
294- struct tcon_link * tlink ;
295308
296309 cifs_dbg (FYI , "in %s\n" , __func__ );
297310 BUG_ON (IS_ROOT (mntpt ));
@@ -315,48 +328,69 @@ static struct vfsmount *cifs_dfs_do_automount(struct dentry *mntpt)
315328 if (full_path == NULL )
316329 goto cdda_exit ;
317330
318- tlink = cifs_sb_tlink (cifs_sb );
319- if (IS_ERR (tlink )) {
320- mnt = ERR_CAST (tlink );
331+ cifs_dbg (FYI , "%s: full_path: %s\n" , __func__ , full_path );
332+
333+ if (!cifs_sb_master_tlink (cifs_sb )) {
334+ cifs_dbg (FYI , "%s: master tlink is NULL\n" , __func__ );
321335 goto free_full_path ;
322336 }
323- ses = tlink_tcon (tlink )-> ses ;
324337
338+ tcon = cifs_sb_master_tcon (cifs_sb );
339+ if (!tcon ) {
340+ cifs_dbg (FYI , "%s: master tcon is NULL\n" , __func__ );
341+ goto free_full_path ;
342+ }
343+
344+ root_path = kstrdup (tcon -> treeName , GFP_KERNEL );
345+ if (!root_path ) {
346+ mnt = ERR_PTR (- ENOMEM );
347+ goto free_full_path ;
348+ }
349+ cifs_dbg (FYI , "%s: root path: %s\n" , __func__ , root_path );
350+
351+ ses = tcon -> ses ;
325352 xid = get_xid ();
326- rc = get_dfs_path (xid , ses , full_path + 1 , cifs_sb -> local_nls ,
327- & num_referrals , & referrals ,
328- cifs_remap (cifs_sb ));
329- free_xid (xid );
330353
331- cifs_put_tlink (tlink );
332-
333- mnt = ERR_PTR (- ENOENT );
334- for (i = 0 ; i < num_referrals ; i ++ ) {
335- int len ;
336- dump_referral (referrals + i );
337- /* connect to a node */
338- len = strlen (referrals [i ].node_name );
339- if (len < 2 ) {
340- cifs_dbg (VFS , "%s: Net Address path too short: %s\n" ,
341- __func__ , referrals [i ].node_name );
342- mnt = ERR_PTR (- EINVAL );
343- break ;
344- }
345- mnt = cifs_dfs_do_refmount (mntpt , cifs_sb ,
346- full_path , referrals + i );
347- cifs_dbg (FYI , "%s: cifs_dfs_do_refmount:%s , mnt:%p\n" ,
348- __func__ , referrals [i ].node_name , mnt );
349- if (!IS_ERR (mnt ))
350- goto success ;
354+ /*
355+ * If DFS root has been expired, then unconditionally fetch it again to
356+ * refresh DFS referral cache.
357+ */
358+ rc = dfs_cache_find (xid , ses , cifs_sb -> local_nls , cifs_remap (cifs_sb ),
359+ root_path + 1 , NULL , NULL );
360+ if (!rc ) {
361+ rc = dfs_cache_find (xid , ses , cifs_sb -> local_nls ,
362+ cifs_remap (cifs_sb ), full_path + 1 ,
363+ & referral , NULL );
351364 }
352365
353- /* no valid submounts were found; return error from get_dfs_path() by
354- * preference */
355- if (rc != 0 )
366+ free_xid ( xid );
367+
368+ if (rc ) {
356369 mnt = ERR_PTR (rc );
370+ goto free_root_path ;
371+ }
372+
373+ dump_referral (& referral );
357374
358- success :
359- free_dfs_info_array (referrals , num_referrals );
375+ len = strlen (referral .node_name );
376+ if (len < 2 ) {
377+ cifs_dbg (VFS , "%s: Net Address path too short: %s\n" ,
378+ __func__ , referral .node_name );
379+ mnt = ERR_PTR (- EINVAL );
380+ goto free_dfs_ref ;
381+ }
382+ /*
383+ * cifs_mount() will retry every available node server in case
384+ * of failures.
385+ */
386+ mnt = cifs_dfs_do_refmount (mntpt , cifs_sb , full_path , & referral );
387+ cifs_dbg (FYI , "%s: cifs_dfs_do_refmount:%s , mnt:%p\n" , __func__ ,
388+ referral .node_name , mnt );
389+
390+ free_dfs_ref :
391+ free_dfs_info_param (& referral );
392+ free_root_path :
393+ kfree (root_path );
360394free_full_path :
361395 kfree (full_path );
362396cdda_exit :
0 commit comments