compress.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2024, SUSE LLC
  4. *
  5. * Authors: Enzo Matsumiya <ematsumiya@suse.de>
  6. *
  7. * This file implements I/O compression support for SMB2 messages (SMB 3.1.1 only).
  8. * See compress/ for implementation details of each algorithm.
  9. *
  10. * References:
  11. * MS-SMB2 "3.1.4.4 Compressing the Message" - for compression details
  12. * MS-SMB2 "3.1.5.3 Decompressing the Chained Message" - for decompression details
  13. * MS-XCA - for details of the supported algorithms
  14. */
  15. #ifndef _SMB_COMPRESS_H
  16. #define _SMB_COMPRESS_H
  17. #include <linux/uio.h>
  18. #include <linux/kernel.h>
  19. #include "../common/smb2pdu.h"
  20. #include "cifsglob.h"
  21. /* sizeof(smb2_compression_hdr) - sizeof(OriginalPayloadSize) */
  22. #define SMB_COMPRESS_HDR_LEN 16
  23. /* sizeof(smb2_compression_payload_hdr) - sizeof(OriginalPayloadSize) */
  24. #define SMB_COMPRESS_PAYLOAD_HDR_LEN 8
  25. #define SMB_COMPRESS_MIN_LEN PAGE_SIZE
  26. #ifdef CONFIG_CIFS_COMPRESSION
  27. typedef int (*compress_send_fn)(struct TCP_Server_Info *, int, struct smb_rqst *);
  28. int smb_compress(struct TCP_Server_Info *server, struct smb_rqst *rq, compress_send_fn send_fn);
  29. /**
  30. * should_compress() - Determines if a request (write) or the response to a
  31. * request (read) should be compressed.
  32. * @tcon: tcon of the request is being sent to
  33. * @rqst: request to evaluate
  34. *
  35. * Return: true iff:
  36. * - compression was successfully negotiated with server
  37. * - server has enabled compression for the share
  38. * - it's a read or write request
  39. * - (write only) request length is >= SMB_COMPRESS_MIN_LEN
  40. * - (write only) is_compressible() returns 1
  41. *
  42. * Return false otherwise.
  43. */
  44. bool should_compress(const struct cifs_tcon *tcon, const struct smb_rqst *rq);
  45. /**
  46. * smb_compress_alg_valid() - Validate a compression algorithm.
  47. * @alg: Compression algorithm to check.
  48. * @valid_none: Conditional check whether NONE algorithm should be
  49. * considered valid or not.
  50. *
  51. * If @alg is SMB3_COMPRESS_NONE, this function returns @valid_none.
  52. *
  53. * Note that 'NONE' (0) compressor type is considered invalid in protocol
  54. * negotiation, as it's never requested to/returned from the server.
  55. *
  56. * Return: true if @alg is valid/supported, false otherwise.
  57. */
  58. static __always_inline int smb_compress_alg_valid(__le16 alg, bool valid_none)
  59. {
  60. if (alg == SMB3_COMPRESS_NONE)
  61. return valid_none;
  62. if (alg == SMB3_COMPRESS_LZ77 || alg == SMB3_COMPRESS_PATTERN)
  63. return true;
  64. return false;
  65. }
  66. #else /* !CONFIG_CIFS_COMPRESSION */
  67. static inline int smb_compress(void *unused1, void *unused2, void *unused3)
  68. {
  69. return -EOPNOTSUPP;
  70. }
  71. static inline bool should_compress(void *unused1, void *unused2)
  72. {
  73. return false;
  74. }
  75. static inline int smb_compress_alg_valid(__le16 unused1, bool unused2)
  76. {
  77. return -EOPNOTSUPP;
  78. }
  79. #endif /* !CONFIG_CIFS_COMPRESSION */
  80. #endif /* _SMB_COMPRESS_H */