Skip to content

Commit 5072010

Browse files
Paulo Alcantara (SUSE)smfrench
authored andcommitted
cifs: Fix DFS cache refresher for DFS links
As per MS-DFSC, when a DFS cache entry is expired and it is a DFS link, then a new DFS referral must be sent to root server in order to refresh the expired entry. This patch ensures that all new DFS referrals for refreshing the cache are sent to DFS root. Signed-off-by: Paulo Alcantara (SUSE) <paulo@paulo.ac> Signed-off-by: Steve French <stfrench@microsoft.com>
1 parent f530710 commit 5072010

4 files changed

Lines changed: 151 additions & 25 deletions

File tree

‎fs/cifs/cifsproto.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,12 +526,21 @@ extern int E_md4hash(const unsigned char *passwd, unsigned char *p16,
526526
const struct nls_table *codepage);
527527
extern int SMBencrypt(unsigned char *passwd, const unsigned char *c8,
528528
unsigned char *p24);
529+
530+
extern int
531+
cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
532+
const char *devname, bool is_smb3);
529533
extern void
530534
cifs_cleanup_volume_info_contents(struct smb_vol *volume_info);
531535

532536
extern struct TCP_Server_Info *
533537
cifs_find_tcp_session(struct smb_vol *vol);
534538

539+
extern void cifs_put_smb_ses(struct cifs_ses *ses);
540+
541+
extern struct cifs_ses *
542+
cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info);
543+
535544
void cifs_readdata_release(struct kref *refcount);
536545
int cifs_async_readv(struct cifs_readdata *rdata);
537546
int cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid);

‎fs/cifs/connect.c‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,6 @@ static int ip_connect(struct TCP_Server_Info *server);
323323
static int generic_ip_connect(struct TCP_Server_Info *server);
324324
static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
325325
static void cifs_prune_tlinks(struct work_struct *work);
326-
static int cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
327-
const char *devname, bool is_smb3);
328326
static char *extract_hostname(const char *unc);
329327

330328
/*
@@ -2904,8 +2902,7 @@ cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb_vol *vol)
29042902
return NULL;
29052903
}
29062904

2907-
static void
2908-
cifs_put_smb_ses(struct cifs_ses *ses)
2905+
void cifs_put_smb_ses(struct cifs_ses *ses)
29092906
{
29102907
unsigned int rc, xid;
29112908
struct TCP_Server_Info *server = ses->server;
@@ -3082,7 +3079,7 @@ cifs_set_cifscreds(struct smb_vol *vol __attribute__((unused)),
30823079
* already got a server reference (server refcount +1). See
30833080
* cifs_get_tcon() for refcount explanations.
30843081
*/
3085-
static struct cifs_ses *
3082+
struct cifs_ses *
30863083
cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
30873084
{
30883085
int rc = -ENOMEM;
@@ -4389,7 +4386,7 @@ static int mount_do_dfs_failover(const char *path,
43894386
}
43904387
#endif
43914388

4392-
static int
4389+
int
43934390
cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
43944391
const char *devname, bool is_smb3)
43954392
{
@@ -4543,7 +4540,7 @@ int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *vol)
45434540
struct cifs_tcon *tcon = NULL;
45444541
struct TCP_Server_Info *server;
45454542
char *root_path = NULL, *full_path = NULL;
4546-
char *old_mountdata;
4543+
char *old_mountdata, *origin_mountdata = NULL;
45474544
int count;
45484545

45494546
rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses, &tcon);
@@ -4602,6 +4599,14 @@ int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *vol)
46024599
goto error;
46034600
}
46044601

4602+
/* Save DFS root volume information for DFS refresh worker */
4603+
origin_mountdata = kstrndup(cifs_sb->mountdata,
4604+
strlen(cifs_sb->mountdata), GFP_KERNEL);
4605+
if (!origin_mountdata) {
4606+
rc = -ENOMEM;
4607+
goto error;
4608+
}
4609+
46054610
if (cifs_sb->mountdata != old_mountdata) {
46064611
/* If we were redirected, reconnect to new target server */
46074612
mount_put_conns(cifs_sb, xid, server, ses, tcon);
@@ -4710,7 +4715,7 @@ int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *vol)
47104715
}
47114716
spin_unlock(&cifs_tcp_ses_lock);
47124717

4713-
rc = dfs_cache_add_vol(vol, cifs_sb->origin_fullpath);
4718+
rc = dfs_cache_add_vol(origin_mountdata, vol, cifs_sb->origin_fullpath);
47144719
if (rc) {
47154720
kfree(cifs_sb->origin_fullpath);
47164721
goto error;
@@ -4728,6 +4733,7 @@ int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *vol)
47284733
error:
47294734
kfree(full_path);
47304735
kfree(root_path);
4736+
kfree(origin_mountdata);
47314737
mount_put_conns(cifs_sb, xid, server, ses, tcon);
47324738
return rc;
47334739
}

‎fs/cifs/dfs_cache.c‎

Lines changed: 125 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
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;
5252
struct 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+
13281438
out:
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
*/
13401450
static 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
}

‎fs/cifs/dfs_cache.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
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
#ifndef _CIFS_DFS_CACHE_H
@@ -43,7 +43,8 @@ dfs_cache_noreq_update_tgthint(const char *path,
4343
extern int dfs_cache_get_tgt_referral(const char *path,
4444
const struct dfs_cache_tgt_iterator *it,
4545
struct dfs_info3_param *ref);
46-
extern int dfs_cache_add_vol(struct smb_vol *vol, const char *fullpath);
46+
extern int dfs_cache_add_vol(char *mntdata, struct smb_vol *vol,
47+
const char *fullpath);
4748
extern int dfs_cache_update_vol(const char *fullpath,
4849
struct TCP_Server_Info *server);
4950
extern void dfs_cache_del_vol(const char *fullpath);

0 commit comments

Comments
 (0)