Skip to content

Commit e8aee4f

Browse files
ematsumiyagregkh
authored andcommitted
smb: client: negotiate compression algorithms
[ Upstream commit 8fe7062 ] Change "compress=" mount option to a boolean flag, that, if set, will enable negotiating compression algorithms with the server. Do not de/compress anything for now. Signed-off-by: Enzo Matsumiya <[email protected]> Signed-off-by: Steve French <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 25f3604 commit e8aee4f

File tree

6 files changed

+49
-15
lines changed

6 files changed

+49
-15
lines changed

fs/smb/client/cifs_debug.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,24 @@ static int cifs_debug_files_proc_show(struct seq_file *m, void *v)
280280
return 0;
281281
}
282282

283+
static __always_inline const char *compression_alg_str(__le16 alg)
284+
{
285+
switch (alg) {
286+
case SMB3_COMPRESS_NONE:
287+
return "NONE";
288+
case SMB3_COMPRESS_LZNT1:
289+
return "LZNT1";
290+
case SMB3_COMPRESS_LZ77:
291+
return "LZ77";
292+
case SMB3_COMPRESS_LZ77_HUFF:
293+
return "LZ77-Huffman";
294+
case SMB3_COMPRESS_PATTERN:
295+
return "Pattern_V1";
296+
default:
297+
return "invalid";
298+
}
299+
}
300+
283301
static int cifs_debug_data_proc_show(struct seq_file *m, void *v)
284302
{
285303
struct mid_q_entry *mid_entry;
@@ -425,12 +443,6 @@ static int cifs_debug_data_proc_show(struct seq_file *m, void *v)
425443
server->echo_credits,
426444
server->oplock_credits,
427445
server->dialect);
428-
if (server->compress_algorithm == SMB3_COMPRESS_LZNT1)
429-
seq_printf(m, " COMPRESS_LZNT1");
430-
else if (server->compress_algorithm == SMB3_COMPRESS_LZ77)
431-
seq_printf(m, " COMPRESS_LZ77");
432-
else if (server->compress_algorithm == SMB3_COMPRESS_LZ77_HUFF)
433-
seq_printf(m, " COMPRESS_LZ77_HUFF");
434446
if (server->sign)
435447
seq_printf(m, " signed");
436448
if (server->posix_ext_supported)
@@ -462,6 +474,14 @@ static int cifs_debug_data_proc_show(struct seq_file *m, void *v)
462474
server->leaf_fullpath);
463475
}
464476

477+
seq_puts(m, "\nCompression: ");
478+
if (!server->compression.requested)
479+
seq_puts(m, "disabled on mount");
480+
else if (server->compression.enabled)
481+
seq_printf(m, "enabled (%s)", compression_alg_str(server->compression.alg));
482+
else
483+
seq_puts(m, "disabled (not supported by this server)");
484+
465485
seq_printf(m, "\n\n\tSessions: ");
466486
i = 0;
467487
list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {

fs/smb/client/cifsglob.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,11 @@ struct TCP_Server_Info {
772772
unsigned int max_write;
773773
unsigned int min_offload;
774774
unsigned int retrans;
775-
__le16 compress_algorithm;
775+
struct {
776+
bool requested; /* "compress" mount option set*/
777+
bool enabled; /* actually negotiated with server */
778+
__le16 alg; /* preferred alg negotiated with server */
779+
} compression;
776780
__u16 signing_algorithm;
777781
__le16 cipher_type;
778782
/* save initital negprot hash */

fs/smb/client/connect.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1748,7 +1748,7 @@ cifs_get_tcp_session(struct smb3_fs_context *ctx,
17481748
tcp_ses->channel_sequence_num = 0; /* only tracked for primary channel */
17491749
tcp_ses->reconnect_instance = 1;
17501750
tcp_ses->lstrp = jiffies;
1751-
tcp_ses->compress_algorithm = cpu_to_le16(ctx->compression);
1751+
tcp_ses->compression.requested = ctx->compress;
17521752
spin_lock_init(&tcp_ses->req_lock);
17531753
spin_lock_init(&tcp_ses->srv_lock);
17541754
spin_lock_init(&tcp_ses->mid_lock);

fs/smb/client/fs_context.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,7 @@ static int smb3_fs_context_parse_param(struct fs_context *fc,
978978

979979
switch (opt) {
980980
case Opt_compress:
981-
ctx->compression = UNKNOWN_TYPE;
981+
ctx->compress = true;
982982
cifs_dbg(VFS,
983983
"SMB3 compression support is experimental\n");
984984
break;

fs/smb/client/fs_context.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ struct smb3_fs_context {
277277
unsigned int max_credits; /* smb3 max_credits 10 < credits < 60000 */
278278
unsigned int max_channels;
279279
unsigned int max_cached_dirs;
280-
__u16 compression; /* compression algorithm 0xFFFF default 0=disabled */
280+
bool compress; /* enable SMB2 messages (READ/WRITE) de/compression */
281281
bool rootfs:1; /* if it's a SMB root file system */
282282
bool witness:1; /* use witness protocol */
283283
char *leaf_fullpath;

fs/smb/client/smb2pdu.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ assemble_neg_contexts(struct smb2_negotiate_req *req,
743743
pneg_ctxt += sizeof(struct smb2_posix_neg_context);
744744
neg_context_count++;
745745

746-
if (server->compress_algorithm) {
746+
if (server->compression.requested) {
747747
build_compression_ctxt((struct smb2_compression_capabilities_context *)
748748
pneg_ctxt);
749749
ctxt_len = ALIGN(sizeof(struct smb2_compression_capabilities_context), 8);
@@ -791,6 +791,9 @@ static void decode_compress_ctx(struct TCP_Server_Info *server,
791791
struct smb2_compression_capabilities_context *ctxt)
792792
{
793793
unsigned int len = le16_to_cpu(ctxt->DataLength);
794+
__le16 alg;
795+
796+
server->compression.enabled = false;
794797

795798
/*
796799
* Caller checked that DataLength remains within SMB boundary. We still
@@ -801,15 +804,22 @@ static void decode_compress_ctx(struct TCP_Server_Info *server,
801804
pr_warn_once("server sent bad compression cntxt\n");
802805
return;
803806
}
807+
804808
if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
805-
pr_warn_once("Invalid SMB3 compress algorithm count\n");
809+
pr_warn_once("invalid SMB3 compress algorithm count\n");
806810
return;
807811
}
808-
if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
809-
pr_warn_once("unknown compression algorithm\n");
812+
813+
alg = ctxt->CompressionAlgorithms[0];
814+
815+
/* 'NONE' (0) compressor type is never negotiated */
816+
if (alg == 0 || le16_to_cpu(alg) > 3) {
817+
pr_warn_once("invalid compression algorithm '%u'\n", alg);
810818
return;
811819
}
812-
server->compress_algorithm = ctxt->CompressionAlgorithms[0];
820+
821+
server->compression.alg = alg;
822+
server->compression.enabled = true;
813823
}
814824

815825
static int decode_encrypt_ctx(struct TCP_Server_Info *server,

0 commit comments

Comments
 (0)