textsearch.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * lib/textsearch.c Generic text search interface
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Authors: Thomas Graf <tgraf@suug.ch>
  10. * Pablo Neira Ayuso <pablo@netfilter.org>
  11. *
  12. * ==========================================================================
  13. *
  14. * INTRODUCTION
  15. *
  16. * The textsearch infrastructure provides text searching facilities for
  17. * both linear and non-linear data. Individual search algorithms are
  18. * implemented in modules and chosen by the user.
  19. *
  20. * ARCHITECTURE
  21. *
  22. * User
  23. * +----------------+
  24. * | finish()|<--------------(6)-----------------+
  25. * |get_next_block()|<--------------(5)---------------+ |
  26. * | | Algorithm | |
  27. * | | +------------------------------+
  28. * | | | init() find() destroy() |
  29. * | | +------------------------------+
  30. * | | Core API ^ ^ ^
  31. * | | +---------------+ (2) (4) (8)
  32. * | (1)|----->| prepare() |---+ | |
  33. * | (3)|----->| find()/next() |-----------+ |
  34. * | (7)|----->| destroy() |----------------------+
  35. * +----------------+ +---------------+
  36. *
  37. * (1) User configures a search by calling _prepare() specifying the
  38. * search parameters such as the pattern and algorithm name.
  39. * (2) Core requests the algorithm to allocate and initialize a search
  40. * configuration according to the specified parameters.
  41. * (3) User starts the search(es) by calling _find() or _next() to
  42. * fetch subsequent occurrences. A state variable is provided
  43. * to the algorithm to store persistent variables.
  44. * (4) Core eventually resets the search offset and forwards the find()
  45. * request to the algorithm.
  46. * (5) Algorithm calls get_next_block() provided by the user continuously
  47. * to fetch the data to be searched in block by block.
  48. * (6) Algorithm invokes finish() after the last call to get_next_block
  49. * to clean up any leftovers from get_next_block. (Optional)
  50. * (7) User destroys the configuration by calling _destroy().
  51. * (8) Core notifies the algorithm to destroy algorithm specific
  52. * allocations. (Optional)
  53. *
  54. * USAGE
  55. *
  56. * Before a search can be performed, a configuration must be created
  57. * by calling textsearch_prepare() specifying the searching algorithm,
  58. * the pattern to look for and flags. As a flag, you can set TS_IGNORECASE
  59. * to perform case insensitive matching. But it might slow down
  60. * performance of algorithm, so you should use it at own your risk.
  61. * The returned configuration may then be used for an arbitrary
  62. * amount of times and even in parallel as long as a separate struct
  63. * ts_state variable is provided to every instance.
  64. *
  65. * The actual search is performed by either calling textsearch_find_-
  66. * continuous() for linear data or by providing an own get_next_block()
  67. * implementation and calling textsearch_find(). Both functions return
  68. * the position of the first occurrence of the pattern or UINT_MAX if
  69. * no match was found. Subsequent occurrences can be found by calling
  70. * textsearch_next() regardless of the linearity of the data.
  71. *
  72. * Once you're done using a configuration it must be given back via
  73. * textsearch_destroy.
  74. *
  75. * EXAMPLE
  76. *
  77. * int pos;
  78. * struct ts_config *conf;
  79. * struct ts_state state;
  80. * const char *pattern = "chicken";
  81. * const char *example = "We dance the funky chicken";
  82. *
  83. * conf = textsearch_prepare("kmp", pattern, strlen(pattern),
  84. * GFP_KERNEL, TS_AUTOLOAD);
  85. * if (IS_ERR(conf)) {
  86. * err = PTR_ERR(conf);
  87. * goto errout;
  88. * }
  89. *
  90. * pos = textsearch_find_continuous(conf, &state, example, strlen(example));
  91. * if (pos != UINT_MAX)
  92. * panic("Oh my god, dancing chickens at %d\n", pos);
  93. *
  94. * textsearch_destroy(conf);
  95. * ==========================================================================
  96. */
  97. #include <linux/module.h>
  98. #include <linux/types.h>
  99. #include <linux/string.h>
  100. #include <linux/init.h>
  101. #include <linux/rculist.h>
  102. #include <linux/rcupdate.h>
  103. #include <linux/err.h>
  104. #include <linux/textsearch.h>
  105. #include <linux/slab.h>
  106. static LIST_HEAD(ts_ops);
  107. static DEFINE_SPINLOCK(ts_mod_lock);
  108. static inline struct ts_ops *lookup_ts_algo(const char *name)
  109. {
  110. struct ts_ops *o;
  111. rcu_read_lock();
  112. list_for_each_entry_rcu(o, &ts_ops, list) {
  113. if (!strcmp(name, o->name)) {
  114. if (!try_module_get(o->owner))
  115. o = NULL;
  116. rcu_read_unlock();
  117. return o;
  118. }
  119. }
  120. rcu_read_unlock();
  121. return NULL;
  122. }
  123. /**
  124. * textsearch_register - register a textsearch module
  125. * @ops: operations lookup table
  126. *
  127. * This function must be called by textsearch modules to announce
  128. * their presence. The specified &@ops must have %name set to a
  129. * unique identifier and the callbacks find(), init(), get_pattern(),
  130. * and get_pattern_len() must be implemented.
  131. *
  132. * Returns 0 or -EEXISTS if another module has already registered
  133. * with same name.
  134. */
  135. int textsearch_register(struct ts_ops *ops)
  136. {
  137. int err = -EEXIST;
  138. struct ts_ops *o;
  139. if (ops->name == NULL || ops->find == NULL || ops->init == NULL ||
  140. ops->get_pattern == NULL || ops->get_pattern_len == NULL)
  141. return -EINVAL;
  142. spin_lock(&ts_mod_lock);
  143. list_for_each_entry(o, &ts_ops, list) {
  144. if (!strcmp(ops->name, o->name))
  145. goto errout;
  146. }
  147. list_add_tail_rcu(&ops->list, &ts_ops);
  148. err = 0;
  149. errout:
  150. spin_unlock(&ts_mod_lock);
  151. return err;
  152. }
  153. /**
  154. * textsearch_unregister - unregister a textsearch module
  155. * @ops: operations lookup table
  156. *
  157. * This function must be called by textsearch modules to announce
  158. * their disappearance for examples when the module gets unloaded.
  159. * The &ops parameter must be the same as the one during the
  160. * registration.
  161. *
  162. * Returns 0 on success or -ENOENT if no matching textsearch
  163. * registration was found.
  164. */
  165. int textsearch_unregister(struct ts_ops *ops)
  166. {
  167. int err = 0;
  168. struct ts_ops *o;
  169. spin_lock(&ts_mod_lock);
  170. list_for_each_entry(o, &ts_ops, list) {
  171. if (o == ops) {
  172. list_del_rcu(&o->list);
  173. goto out;
  174. }
  175. }
  176. err = -ENOENT;
  177. out:
  178. spin_unlock(&ts_mod_lock);
  179. return err;
  180. }
  181. struct ts_linear_state
  182. {
  183. unsigned int len;
  184. const void *data;
  185. };
  186. static unsigned int get_linear_data(unsigned int consumed, const u8 **dst,
  187. struct ts_config *conf,
  188. struct ts_state *state)
  189. {
  190. struct ts_linear_state *st = (struct ts_linear_state *) state->cb;
  191. if (likely(consumed < st->len)) {
  192. *dst = st->data + consumed;
  193. return st->len - consumed;
  194. }
  195. return 0;
  196. }
  197. /**
  198. * textsearch_find_continuous - search a pattern in continuous/linear data
  199. * @conf: search configuration
  200. * @state: search state
  201. * @data: data to search in
  202. * @len: length of data
  203. *
  204. * A simplified version of textsearch_find() for continuous/linear data.
  205. * Call textsearch_next() to retrieve subsequent matches.
  206. *
  207. * Returns the position of first occurrence of the pattern or
  208. * %UINT_MAX if no occurrence was found.
  209. */
  210. unsigned int textsearch_find_continuous(struct ts_config *conf,
  211. struct ts_state *state,
  212. const void *data, unsigned int len)
  213. {
  214. struct ts_linear_state *st = (struct ts_linear_state *) state->cb;
  215. conf->get_next_block = get_linear_data;
  216. st->data = data;
  217. st->len = len;
  218. return textsearch_find(conf, state);
  219. }
  220. /**
  221. * textsearch_prepare - Prepare a search
  222. * @algo: name of search algorithm
  223. * @pattern: pattern data
  224. * @len: length of pattern
  225. * @gfp_mask: allocation mask
  226. * @flags: search flags
  227. *
  228. * Looks up the search algorithm module and creates a new textsearch
  229. * configuration for the specified pattern. Upon completion all
  230. * necessary refcnts are held and the configuration must be put back
  231. * using textsearch_put() after usage.
  232. *
  233. * Note: The format of the pattern may not be compatible between
  234. * the various search algorithms.
  235. *
  236. * Returns a new textsearch configuration according to the specified
  237. * parameters or a ERR_PTR(). If a zero length pattern is passed, this
  238. * function returns EINVAL.
  239. */
  240. struct ts_config *textsearch_prepare(const char *algo, const void *pattern,
  241. unsigned int len, gfp_t gfp_mask, int flags)
  242. {
  243. int err = -ENOENT;
  244. struct ts_config *conf;
  245. struct ts_ops *ops;
  246. if (len == 0)
  247. return ERR_PTR(-EINVAL);
  248. ops = lookup_ts_algo(algo);
  249. #ifdef CONFIG_MODULES
  250. /*
  251. * Why not always autoload you may ask. Some users are
  252. * in a situation where requesting a module may deadlock,
  253. * especially when the module is located on a NFS mount.
  254. */
  255. if (ops == NULL && flags & TS_AUTOLOAD) {
  256. request_module("ts_%s", algo);
  257. ops = lookup_ts_algo(algo);
  258. }
  259. #endif
  260. if (ops == NULL)
  261. goto errout;
  262. conf = ops->init(pattern, len, gfp_mask, flags);
  263. if (IS_ERR(conf)) {
  264. err = PTR_ERR(conf);
  265. goto errout;
  266. }
  267. conf->ops = ops;
  268. return conf;
  269. errout:
  270. if (ops)
  271. module_put(ops->owner);
  272. return ERR_PTR(err);
  273. }
  274. /**
  275. * textsearch_destroy - destroy a search configuration
  276. * @conf: search configuration
  277. *
  278. * Releases all references of the configuration and frees
  279. * up the memory.
  280. */
  281. void textsearch_destroy(struct ts_config *conf)
  282. {
  283. if (conf->ops) {
  284. if (conf->ops->destroy)
  285. conf->ops->destroy(conf);
  286. module_put(conf->ops->owner);
  287. }
  288. kfree(conf);
  289. }
  290. EXPORT_SYMBOL(textsearch_register);
  291. EXPORT_SYMBOL(textsearch_unregister);
  292. EXPORT_SYMBOL(textsearch_prepare);
  293. EXPORT_SYMBOL(textsearch_find_continuous);
  294. EXPORT_SYMBOL(textsearch_destroy);