22/*
33 * DFS referral cache routines
44 *
5- * Copyright (c) 2018 Paulo Alcantara <palcantara@suse.de>
5+ * Copyright (c) 2018-2019 Paulo Alcantara <palcantara@suse.de>
66 */
77
88#include <linux/rcupdate.h>
@@ -52,6 +52,7 @@ static struct kmem_cache *dfs_cache_slab __read_mostly;
5252struct dfs_cache_vol_info {
5353 char * vi_fullpath ;
5454 struct smb_vol vi_vol ;
55+ char * vi_mntdata ;
5556 struct list_head vi_list ;
5657};
5758
@@ -529,6 +530,7 @@ static inline void free_vol(struct dfs_cache_vol_info *vi)
529530{
530531 list_del (& vi -> vi_list );
531532 kfree (vi -> vi_fullpath );
533+ kfree (vi -> vi_mntdata );
532534 cifs_cleanup_volume_info_contents (& vi -> vi_vol );
533535 kfree (vi );
534536}
@@ -1139,17 +1141,18 @@ static int dup_vol(struct smb_vol *vol, struct smb_vol *new)
11391141 * dfs_cache_add_vol - add a cifs volume during mount() that will be handled by
11401142 * DFS cache refresh worker.
11411143 *
1144+ * @mntdata: mount data.
11421145 * @vol: cifs volume.
11431146 * @fullpath: origin full path.
11441147 *
11451148 * Return zero if volume was set up correctly, otherwise non-zero.
11461149 */
1147- int dfs_cache_add_vol (struct smb_vol * vol , const char * fullpath )
1150+ int dfs_cache_add_vol (char * mntdata , struct smb_vol * vol , const char * fullpath )
11481151{
11491152 int rc ;
11501153 struct dfs_cache_vol_info * vi ;
11511154
1152- if (!vol || !fullpath )
1155+ if (!vol || !fullpath || ! mntdata )
11531156 return - EINVAL ;
11541157
11551158 cifs_dbg (FYI , "%s: fullpath: %s\n" , __func__ , fullpath );
@@ -1168,6 +1171,8 @@ int dfs_cache_add_vol(struct smb_vol *vol, const char *fullpath)
11681171 if (rc )
11691172 goto err_free_fullpath ;
11701173
1174+ vi -> vi_mntdata = mntdata ;
1175+
11711176 mutex_lock (& dfs_cache .dc_lock );
11721177 list_add_tail (& vi -> vi_list , & dfs_cache .dc_vol_list );
11731178 mutex_unlock (& dfs_cache .dc_lock );
@@ -1275,8 +1280,102 @@ static void get_tcons(struct TCP_Server_Info *server, struct list_head *head)
12751280 spin_unlock (& cifs_tcp_ses_lock );
12761281}
12771282
1283+ static inline bool is_dfs_link (const char * path )
1284+ {
1285+ char * s ;
1286+
1287+ s = strchr (path + 1 , '\\' );
1288+ if (!s )
1289+ return false;
1290+ return !!strchr (s + 1 , '\\' );
1291+ }
1292+
1293+ static inline char * get_dfs_root (const char * path )
1294+ {
1295+ char * s , * npath ;
1296+
1297+ s = strchr (path + 1 , '\\' );
1298+ if (!s )
1299+ return ERR_PTR (- EINVAL );
1300+
1301+ s = strchr (s + 1 , '\\' );
1302+ if (!s )
1303+ return ERR_PTR (- EINVAL );
1304+
1305+ npath = kstrndup (path , s - path , GFP_KERNEL );
1306+ if (!npath )
1307+ return ERR_PTR (- ENOMEM );
1308+
1309+ return npath ;
1310+ }
1311+
1312+ /* Find root SMB session out of a DFS link path */
1313+ static struct cifs_ses * find_root_ses (struct dfs_cache_vol_info * vi ,
1314+ struct cifs_tcon * tcon , const char * path )
1315+ {
1316+ char * rpath ;
1317+ int rc ;
1318+ struct dfs_info3_param ref = {0 };
1319+ char * mdata = NULL , * devname = NULL ;
1320+ bool is_smb3 = tcon -> ses -> server -> vals -> header_preamble_size == 0 ;
1321+ struct TCP_Server_Info * server ;
1322+ struct cifs_ses * ses ;
1323+ struct smb_vol vol ;
1324+
1325+ rpath = get_dfs_root (path );
1326+ if (IS_ERR (rpath ))
1327+ return ERR_CAST (rpath );
1328+
1329+ memset (& vol , 0 , sizeof (vol ));
1330+
1331+ rc = dfs_cache_noreq_find (rpath , & ref , NULL );
1332+ if (rc ) {
1333+ ses = ERR_PTR (rc );
1334+ goto out ;
1335+ }
1336+
1337+ mdata = cifs_compose_mount_options (vi -> vi_mntdata , rpath , & ref ,
1338+ & devname );
1339+ free_dfs_info_param (& ref );
1340+
1341+ if (IS_ERR (mdata )) {
1342+ ses = ERR_CAST (mdata );
1343+ mdata = NULL ;
1344+ goto out ;
1345+ }
1346+
1347+ rc = cifs_setup_volume_info (& vol , mdata , devname , is_smb3 );
1348+ kfree (devname );
1349+
1350+ if (rc ) {
1351+ ses = ERR_PTR (rc );
1352+ goto out ;
1353+ }
1354+
1355+ server = cifs_find_tcp_session (& vol );
1356+ if (IS_ERR_OR_NULL (server )) {
1357+ ses = ERR_PTR (- EHOSTDOWN );
1358+ goto out ;
1359+ }
1360+ if (server -> tcpStatus != CifsGood ) {
1361+ cifs_put_tcp_session (server , 0 );
1362+ ses = ERR_PTR (- EHOSTDOWN );
1363+ goto out ;
1364+ }
1365+
1366+ ses = cifs_get_smb_ses (server , & vol );
1367+
1368+ out :
1369+ cifs_cleanup_volume_info_contents (& vol );
1370+ kfree (mdata );
1371+ kfree (rpath );
1372+
1373+ return ses ;
1374+ }
1375+
12781376/* Refresh DFS cache entry from a given tcon */
1279- static void do_refresh_tcon (struct dfs_cache * dc , struct cifs_tcon * tcon )
1377+ static void do_refresh_tcon (struct dfs_cache * dc , struct dfs_cache_vol_info * vi ,
1378+ struct cifs_tcon * tcon )
12801379{
12811380 int rc = 0 ;
12821381 unsigned int xid ;
@@ -1285,6 +1384,7 @@ static void do_refresh_tcon(struct dfs_cache *dc, struct cifs_tcon *tcon)
12851384 struct dfs_cache_entry * ce ;
12861385 struct dfs_info3_param * refs = NULL ;
12871386 int numrefs = 0 ;
1387+ struct cifs_ses * root_ses = NULL , * ses ;
12881388
12891389 xid = get_xid ();
12901390
@@ -1306,13 +1406,24 @@ static void do_refresh_tcon(struct dfs_cache *dc, struct cifs_tcon *tcon)
13061406 if (!cache_entry_expired (ce ))
13071407 goto out ;
13081408
1309- if (unlikely (!tcon -> ses -> server -> ops -> get_dfs_refer )) {
1409+ /* If it's a DFS Link, then use root SMB session for refreshing it */
1410+ if (is_dfs_link (npath )) {
1411+ ses = root_ses = find_root_ses (vi , tcon , npath );
1412+ if (IS_ERR (ses )) {
1413+ rc = PTR_ERR (ses );
1414+ root_ses = NULL ;
1415+ goto out ;
1416+ }
1417+ } else {
1418+ ses = tcon -> ses ;
1419+ }
1420+
1421+ if (unlikely (!ses -> server -> ops -> get_dfs_refer )) {
13101422 rc = - EOPNOTSUPP ;
13111423 } else {
1312- rc = tcon -> ses -> server -> ops -> get_dfs_refer (xid , tcon -> ses , path ,
1313- & refs , & numrefs ,
1314- dc -> dc_nlsc ,
1315- tcon -> remap );
1424+ rc = ses -> server -> ops -> get_dfs_refer (xid , ses , path , & refs ,
1425+ & numrefs , dc -> dc_nlsc ,
1426+ tcon -> remap );
13161427 if (!rc ) {
13171428 mutex_lock (& dfs_cache_list_lock );
13181429 ce = __update_cache_entry (npath , refs , numrefs );
@@ -1323,19 +1434,18 @@ static void do_refresh_tcon(struct dfs_cache *dc, struct cifs_tcon *tcon)
13231434 rc = PTR_ERR (ce );
13241435 }
13251436 }
1326- if (rc )
1327- cifs_dbg (FYI , "%s: failed to update expired entry\n" , __func__ );
1437+
13281438out :
1439+ if (root_ses )
1440+ cifs_put_smb_ses (root_ses );
1441+
13291442 free_xid (xid );
13301443 free_normalized_path (path , npath );
13311444}
13321445
13331446/*
13341447 * Worker that will refresh DFS cache based on lowest TTL value from a DFS
13351448 * referral.
1336- *
1337- * FIXME: ensure that all requests are sent to DFS root for refreshing the
1338- * cache.
13391449 */
13401450static void refresh_cache_worker (struct work_struct * work )
13411451{
@@ -1356,7 +1466,7 @@ static void refresh_cache_worker(struct work_struct *work)
13561466 goto next ;
13571467 get_tcons (server , & list );
13581468 list_for_each_entry_safe (tcon , ntcon , & list , ulist ) {
1359- do_refresh_tcon (dc , tcon );
1469+ do_refresh_tcon (dc , vi , tcon );
13601470 list_del_init (& tcon -> ulist );
13611471 cifs_put_tcon (tcon );
13621472 }
0 commit comments