Lines Matching +full:max +full:- +full:len
1 // SPDX-License-Identifier: GPL-2.0-only
11 * MS-SMB2 "3.1.4.4 Compressing the Message"
12 * MS-SMB2 "3.1.5.3 Decompressing the Chained Message"
13 * MS-XCA - for details of the supported algorithms
39 * Parsing the sample goes from "low-hanging fruits" (fastest algorithms, likely compressible)
48 * has_low_entropy() - Compute Shannon entropy of the sampled data.
66 size_t i, p, p2, len, sum = 0; in has_low_entropy() local
69 len = ilog2(pow4(slen)); in has_low_entropy()
74 sum += p * (len - p2); in has_low_entropy()
86 * calc_byte_distribution() - Compute byte distribution on the sampled data.
91 * BYTE_DIST_BAD: A "hard no" for compression -- a computed uniform distribution of
132 /* Too many non-ASCII (0-63) bytes. */ in is_mostly_ascii()
139 static bool has_repeated_data(const u8 *sample, size_t len) in has_repeated_data() argument
141 size_t s = len / 2; in has_repeated_data()
151 if (a->count > b->count) in cmp_bkt()
152 return -1; in cmp_bkt()
162 static int collect_sample(const struct iov_iter *iter, ssize_t max, u8 *sample) in collect_sample() argument
166 loff_t start = iter->xarray_start + iter->iov_offset; in collect_sample()
168 size_t len, off, foff; in collect_sample() local
172 last = (start + max - 1) / PAGE_SIZE; in collect_sample()
174 nr = xa_extract(iter->xarray, (void **)folios, index, last, ARRAY_SIZE(folios), in collect_sample()
177 return -EIO; in collect_sample()
182 foff = start - folio_pos(folio); in collect_sample()
188 len = min_t(size_t, max, PAGE_SIZE - off); in collect_sample()
189 len2 = min_t(size_t, len, SZ_2K); in collect_sample()
197 if (len2 < SZ_2K || s >= max - SZ_2K) in collect_sample()
200 max -= len; in collect_sample()
201 if (max <= 0) in collect_sample()
204 start += len; in collect_sample()
215 * is_compressible() - Determines if a chunk of data is compressible.
225 const size_t read_size = SZ_2K, bkt_size = 256, max = SZ_4M; in is_compressible() local
227 size_t len; in is_compressible() local
232 /* Preventive double check -- already checked in should_compress(). */ in is_compressible()
233 len = iov_iter_count(data); in is_compressible()
234 if (unlikely(len < read_size)) in is_compressible()
237 if (len - read_size > max) in is_compressible()
238 len = max; in is_compressible()
240 sample = kvzalloc(len, GFP_KERNEL); in is_compressible()
248 i = collect_sample(data, len, sample); in is_compressible()
255 len = i; in is_compressible()
258 if (has_repeated_data(sample, len)) in is_compressible()
269 for (i = 0; i < len; i++) in is_compressible()
278 i = calc_byte_distribution(bkt, len); in is_compressible()
285 ret = has_low_entropy(bkt, len); in is_compressible()
295 const struct smb2_hdr *shdr = rq->rq_iov->iov_base; in should_compress()
297 if (unlikely(!tcon || !tcon->ses || !tcon->ses->server)) in should_compress()
300 if (!tcon->ses->server->compression.enabled) in should_compress()
303 if (!(tcon->share_flags & SMB2_SHAREFLAG_COMPRESS_DATA)) in should_compress()
306 if (shdr->Command == SMB2_WRITE) { in should_compress()
307 const struct smb2_write_req *wreq = rq->rq_iov->iov_base; in should_compress()
309 if (le32_to_cpu(wreq->Length) < SMB_COMPRESS_MIN_LEN) in should_compress()
312 return is_compressible(&rq->rq_iter); in should_compress()
315 return (shdr->Command == SMB2_READ); in should_compress()
325 if (!server || !rq || !rq->rq_iov || !rq->rq_iov->iov_base) in smb_compress()
326 return -EINVAL; in smb_compress()
328 if (rq->rq_iov->iov_len != sizeof(struct smb2_write_req)) in smb_compress()
329 return -EINVAL; in smb_compress()
331 slen = iov_iter_count(&rq->rq_iter); in smb_compress()
334 ret = -ENOMEM; in smb_compress()
339 iter = rq->rq_iter; in smb_compress()
342 ret = -EIO; in smb_compress()
353 ret = -ENOMEM; in smb_compress()
367 hdr.Offset = cpu_to_le32(rq->rq_iov[0].iov_len); in smb_compress()
371 iov[1] = rq->rq_iov[0]; in smb_compress()
378 } else if (ret == -EMSGSIZE || dlen >= slen) { in smb_compress()