linux-stable/fs/cachefiles
David Howells 5d439467b8 cachefiles: Implement key to filename encoding
Implement a function to encode a binary cookie key as something that can be
used as a filename.  Four options are considered:

 (1) All printable chars with no '/' characters.  Prepend a 'D' to indicate
     the encoding but otherwise use as-is.

 (2) Appears to be an array of __be32.  Encode as 'S' plus a list of
     hex-encoded 32-bit ints separated by commas.  If a number is 0, it is
     rendered as "" instead of "0".

 (3) Appears to be an array of __le32.  Encoded as (2) but with a 'T'
     encoding prefix.

 (4) Encoded as base64 with an 'E' prefix plus a second char indicating how
     much padding is involved.  A non-standard base64 encoding is used
     because '/' cannot be used in the encoded form.

If (1) is not possible, whichever of (2), (3) or (4) produces the shortest
string is selected (hex-encoding a number may be less dense than base64
encoding it).

Note that the prefix characters have to be selected from the set [DEIJST@]
lest cachefilesd remove the files because it recognise the name.

Changes
=======
ver #2:
 - Fix a short allocation that didn't allow for a string terminator[1]

Signed-off-by: David Howells <dhowells@redhat.com>
Reviewed-by: Jeff Layton <jlayton@kernel.org>
cc: linux-cachefs@redhat.com
Link: https://lore.kernel.org/r/bcefb8f2-576a-b3fc-cc29-89808ebfd7c1@linux.alibaba.com/ [1]
Link: https://lore.kernel.org/r/163819640393.215744.15212364106412961104.stgit@warthog.procyon.org.uk/ # v1
Link: https://lore.kernel.org/r/163906940529.143852.17352132319136117053.stgit@warthog.procyon.org.uk/ # v2
Link: https://lore.kernel.org/r/163967149827.1823006.6088580775428487961.stgit@warthog.procyon.org.uk/ # v3
Link: https://lore.kernel.org/r/164021549223.640689.14762875188193982341.stgit@warthog.procyon.org.uk/ # v4
2022-01-07 13:42:16 +00:00
..
cache.c cachefiles: Implement volume support 2022-01-07 13:41:53 +00:00
daemon.c cachefiles: Implement volume support 2022-01-07 13:41:53 +00:00
error_inject.c cachefiles: Add some error injection support 2022-01-07 13:40:56 +00:00
interface.c cachefiles: Implement object lifecycle funcs 2022-01-07 13:42:08 +00:00
internal.h cachefiles: Implement key to filename encoding 2022-01-07 13:42:16 +00:00
Kconfig cachefiles: Add some error injection support 2022-01-07 13:40:56 +00:00
key.c cachefiles: Implement key to filename encoding 2022-01-07 13:42:16 +00:00
main.c cachefiles: Implement object lifecycle funcs 2022-01-07 13:42:08 +00:00
Makefile cachefiles: Implement key to filename encoding 2022-01-07 13:42:16 +00:00
namei.c cachefiles: Implement a function to get/create a directory in the cache 2022-01-07 13:41:40 +00:00
security.c cachefiles: Add security derivation 2022-01-07 13:41:14 +00:00
volume.c cachefiles: Implement volume support 2022-01-07 13:41:53 +00:00