textsearch.c 9.5 KB

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