From: Yu Kuai yukuai3@huawei.com
hulk inclusion category: feature bugzilla: https://gitee.com/openeuler/kernel/issues/IB5UKT
--------------------------------
If input keyname contain character that is unprintable, the treatment from mainline is totally different. Hence bance port the treatment from mainline for the case new_location, so that cachefile location is the same as mainline.
Signed-off-by: Yu Kuai yukuai3@huawei.com Signed-off-by: Baokun Li libaokun1@huawei.com --- fs/cachefiles/key.c | 110 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+)
diff --git a/fs/cachefiles/key.c b/fs/cachefiles/key.c index 2895ab1a38da..772ad69c49ba 100644 --- a/fs/cachefiles/key.c +++ b/fs/cachefiles/key.c @@ -22,6 +22,11 @@ static const char cachefiles_filecharmap[256] = { [48 ... 127] = 1, /* '0' -> '~' */ };
+static inline unsigned int how_many_hex_digits(unsigned int x) +{ + return x ? round_up(ilog2(x) + 1, 4) / 4 : 0; +} + static void cachefiles_cook_acc(char *key, unsigned int acc, int *len) { key[*len + 1] = cachefiles_charmap[acc & 63]; @@ -50,6 +55,85 @@ static int cachefiles_cook_csum(struct fscache_cookie *cookie, const u8 *raw, return 5; }
+static char *cachefiles_cook_data_key(const u8 *key, int keylen) +{ + const u8 *kend; + unsigned int acc, i, n, nle, nbe; + unsigned int b64len, len, pad; + char *name, sep; + + /* See if it makes sense to encode it as "hex,hex,hex" for each 32-bit + * chunk. We rely on the key having been padded out to a whole number + * of 32-bit words. + */ + n = round_up(keylen, 4); + nbe = nle = 0; + for (i = 0; i < n; i += 4) { + u32 be = be32_to_cpu(*(__be32 *)(key + i)); + u32 le = le32_to_cpu(*(__le32 *)(key + i)); + + nbe += 1 + how_many_hex_digits(be); + nle += 1 + how_many_hex_digits(le); + } + + b64len = DIV_ROUND_UP(keylen, 3); + pad = b64len * 3 - keylen; + b64len = 2 + b64len * 4; /* Length if we base64-encode it */ + _debug("len=%u nbe=%u nle=%u b64=%u", keylen, nbe, nle, b64len); + if (nbe < b64len || nle < b64len) { + unsigned int nlen = min(nbe, nle) + 1; + + name = kmalloc(nlen, GFP_KERNEL); + if (!name) + return NULL; + sep = (nbe <= nle) ? 'S' : 'T'; /* Encoding indicator */ + len = 0; + for (i = 0; i < n; i += 4) { + u32 x; + + if (nbe <= nle) + x = be32_to_cpu(*(__be32 *)(key + i)); + else + x = le32_to_cpu(*(__le32 *)(key + i)); + name[len++] = sep; + if (x != 0) + len += snprintf(name + len, nlen - len, "%x", x); + sep = ','; + } + name[len] = 0; + return name; + } + + /* We need to base64-encode it */ + name = kmalloc(b64len + 1, GFP_KERNEL); + if (!name) + return NULL; + + name[0] = 'E'; + name[1] = '0' + pad; + len = 2; + kend = key + keylen; + do { + acc = *key++; + if (key < kend) { + acc |= *key++ << 8; + if (key < kend) + acc |= *key++ << 16; + } + + name[len++] = cachefiles_charmap[acc & 63]; + acc >>= 6; + name[len++] = cachefiles_charmap[acc & 63]; + acc >>= 6; + name[len++] = cachefiles_charmap[acc & 63]; + acc >>= 6; + name[len++] = cachefiles_charmap[acc & 63]; + } while (key < kend); + + name[len] = 0; + return name; +} + /* * turn the raw key into something cooked * - the raw key should include the length in the two bytes at the front @@ -86,6 +170,9 @@ char *cachefiles_cook_key(struct cachefiles_object *object, * is ((514 + 251) / 252) = 3 */ max += 1; /* NUL on end */ + } else if (data_new_location(cookie)) { + max = 5; /* @checksum/M */ + max += 1; /* NUL on end */ } else { /* calculate the maximum length of the cooked key */ keylen = (keylen + 2) / 3; @@ -131,6 +218,29 @@ char *cachefiles_cook_key(struct cachefiles_object *object, case FSCACHE_COOKIE_TYPE_DATAFILE: type = 'D'; break; default: type = 'S'; break; } + } else if (data_new_location(cookie)) { + int nlen; + char *name = cachefiles_cook_data_key(raw + 2, keylen - 2); + char *new_key; + + if (!name) { + kfree(key); + return NULL; + } + + nlen = max + strlen(name) - 1; + new_key = krealloc(key, nlen, GFP_KERNEL); + if (!new_key) { + kfree(key); + kfree(name); + return NULL; + } + + key = new_key; + type = name[0]; + for (loop = 1; loop < strlen(name); loop++) + key[len++] = name[loop]; + kfree(name); } else { seg = 252; for (loop = keylen; loop > 0; loop--) {