fs_parser.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /* Filesystem parameter description and parser
  3. *
  4. * Copyright (C) 2018 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells ([email protected])
  6. */
  7. #ifndef _LINUX_FS_PARSER_H
  8. #define _LINUX_FS_PARSER_H
  9. #include <linux/fs_context.h>
  10. struct path;
  11. struct constant_table {
  12. const char *name;
  13. int value;
  14. };
  15. struct fs_parameter_spec;
  16. struct fs_parse_result;
  17. typedef int fs_param_type(struct p_log *,
  18. const struct fs_parameter_spec *,
  19. struct fs_parameter *,
  20. struct fs_parse_result *);
  21. /*
  22. * The type of parameter expected.
  23. */
  24. fs_param_type fs_param_is_bool, fs_param_is_u32, fs_param_is_s32, fs_param_is_u64,
  25. fs_param_is_enum, fs_param_is_string, fs_param_is_blob, fs_param_is_blockdev,
  26. fs_param_is_path, fs_param_is_fd;
  27. /*
  28. * Specification of the type of value a parameter wants.
  29. *
  30. * Note that the fsparam_flag(), fsparam_string(), fsparam_u32(), ... macros
  31. * should be used to generate elements of this type.
  32. */
  33. struct fs_parameter_spec {
  34. const char *name;
  35. fs_param_type *type; /* The desired parameter type */
  36. u8 opt; /* Option number (returned by fs_parse()) */
  37. unsigned short flags;
  38. #define fs_param_neg_with_no 0x0002 /* "noxxx" is negative param */
  39. #define fs_param_can_be_empty 0x0004 /* "xxx=" is allowed */
  40. #define fs_param_deprecated 0x0008 /* The param is deprecated */
  41. const void *data;
  42. };
  43. /*
  44. * Result of parse.
  45. */
  46. struct fs_parse_result {
  47. bool negated; /* T if param was "noxxx" */
  48. union {
  49. bool boolean; /* For spec_bool */
  50. int int_32; /* For spec_s32/spec_enum */
  51. unsigned int uint_32; /* For spec_u32{,_octal,_hex}/spec_enum */
  52. u64 uint_64; /* For spec_u64 */
  53. };
  54. };
  55. extern int __fs_parse(struct p_log *log,
  56. const struct fs_parameter_spec *desc,
  57. struct fs_parameter *value,
  58. struct fs_parse_result *result);
  59. static inline int fs_parse(struct fs_context *fc,
  60. const struct fs_parameter_spec *desc,
  61. struct fs_parameter *param,
  62. struct fs_parse_result *result)
  63. {
  64. return __fs_parse(&fc->log, desc, param, result);
  65. }
  66. extern int fs_lookup_param(struct fs_context *fc,
  67. struct fs_parameter *param,
  68. bool want_bdev,
  69. unsigned int flags,
  70. struct path *_path);
  71. extern int lookup_constant(const struct constant_table tbl[], const char *name, int not_found);
  72. #ifdef CONFIG_VALIDATE_FS_PARSER
  73. extern bool validate_constant_table(const struct constant_table *tbl, size_t tbl_size,
  74. int low, int high, int special);
  75. extern bool fs_validate_description(const char *name,
  76. const struct fs_parameter_spec *desc);
  77. #else
  78. static inline bool validate_constant_table(const struct constant_table *tbl, size_t tbl_size,
  79. int low, int high, int special)
  80. { return true; }
  81. static inline bool fs_validate_description(const char *name,
  82. const struct fs_parameter_spec *desc)
  83. { return true; }
  84. #endif
  85. /*
  86. * Parameter type, name, index and flags element constructors. Use as:
  87. *
  88. * fsparam_xxxx("foo", Opt_foo)
  89. *
  90. * If existing helpers are not enough, direct use of __fsparam() would
  91. * work, but any such case is probably a sign that new helper is needed.
  92. * Helpers will remain stable; low-level implementation may change.
  93. */
  94. #define __fsparam(TYPE, NAME, OPT, FLAGS, DATA) \
  95. { \
  96. .name = NAME, \
  97. .opt = OPT, \
  98. .type = TYPE, \
  99. .flags = FLAGS, \
  100. .data = DATA \
  101. }
  102. #define fsparam_flag(NAME, OPT) __fsparam(NULL, NAME, OPT, 0, NULL)
  103. #define fsparam_flag_no(NAME, OPT) \
  104. __fsparam(NULL, NAME, OPT, fs_param_neg_with_no, NULL)
  105. #define fsparam_bool(NAME, OPT) __fsparam(fs_param_is_bool, NAME, OPT, 0, NULL)
  106. #define fsparam_u32(NAME, OPT) __fsparam(fs_param_is_u32, NAME, OPT, 0, NULL)
  107. #define fsparam_u32oct(NAME, OPT) \
  108. __fsparam(fs_param_is_u32, NAME, OPT, 0, (void *)8)
  109. #define fsparam_u32hex(NAME, OPT) \
  110. __fsparam(fs_param_is_u32_hex, NAME, OPT, 0, (void *)16)
  111. #define fsparam_s32(NAME, OPT) __fsparam(fs_param_is_s32, NAME, OPT, 0, NULL)
  112. #define fsparam_u64(NAME, OPT) __fsparam(fs_param_is_u64, NAME, OPT, 0, NULL)
  113. #define fsparam_enum(NAME, OPT, array) __fsparam(fs_param_is_enum, NAME, OPT, 0, array)
  114. #define fsparam_string(NAME, OPT) \
  115. __fsparam(fs_param_is_string, NAME, OPT, 0, NULL)
  116. #define fsparam_blob(NAME, OPT) __fsparam(fs_param_is_blob, NAME, OPT, 0, NULL)
  117. #define fsparam_bdev(NAME, OPT) __fsparam(fs_param_is_blockdev, NAME, OPT, 0, NULL)
  118. #define fsparam_path(NAME, OPT) __fsparam(fs_param_is_path, NAME, OPT, 0, NULL)
  119. #define fsparam_fd(NAME, OPT) __fsparam(fs_param_is_fd, NAME, OPT, 0, NULL)
  120. #endif /* _LINUX_FS_PARSER_H */