drm: Split drm_map and drm_local_map
[deliverable/linux.git] / lib / textsearch.c
CommitLineData
2de4ff7b
TG
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>
e03ba84a 10 * Pablo Neira Ayuso <pablo@netfilter.org>
2de4ff7b
TG
11 *
12 * ==========================================================================
13 *
14 * INTRODUCTION
15 *
16 * The textsearch infrastructure provides text searching facitilies 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
d08df601 43 * to the algorithm to store persistent variables.
2de4ff7b
TG
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 continously
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
b9c79678
JP
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 arbitary
62 * amount of times and even in parallel as long as a separate struct
63 * ts_state variable is provided to every instance.
2de4ff7b
TG
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 patern or UINT_MAX if
69 * no match was found. Subsequent occurences 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);
2de4ff7b
TG
95 * ==========================================================================
96 */
97
2de4ff7b
TG
98#include <linux/module.h>
99#include <linux/types.h>
100#include <linux/string.h>
101#include <linux/init.h>
82524746 102#include <linux/rculist.h>
2de4ff7b
TG
103#include <linux/rcupdate.h>
104#include <linux/err.h>
105#include <linux/textsearch.h>
106
107static LIST_HEAD(ts_ops);
108static DEFINE_SPINLOCK(ts_mod_lock);
109
110static inline struct ts_ops *lookup_ts_algo(const char *name)
111{
112 struct ts_ops *o;
113
114 rcu_read_lock();
115 list_for_each_entry_rcu(o, &ts_ops, list) {
116 if (!strcmp(name, o->name)) {
117 if (!try_module_get(o->owner))
118 o = NULL;
119 rcu_read_unlock();
120 return o;
121 }
122 }
123 rcu_read_unlock();
124
125 return NULL;
126}
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 */
140int textsearch_register(struct ts_ops *ops)
141{
142 int err = -EEXIST;
143 struct ts_ops *o;
144
145 if (ops->name == NULL || ops->find == NULL || ops->init == NULL ||
146 ops->get_pattern == NULL || ops->get_pattern_len == NULL)
147 return -EINVAL;
148
149 spin_lock(&ts_mod_lock);
150 list_for_each_entry(o, &ts_ops, list) {
151 if (!strcmp(ops->name, o->name))
152 goto errout;
153 }
154
155 list_add_tail_rcu(&ops->list, &ts_ops);
156 err = 0;
157errout:
158 spin_unlock(&ts_mod_lock);
159 return err;
160}
161
162/**
163 * textsearch_unregister - unregister a textsearch module
164 * @ops: operations lookup table
165 *
166 * This function must be called by textsearch modules to announce
167 * their disappearance for examples when the module gets unloaded.
168 * The &ops parameter must be the same as the one during the
169 * registration.
170 *
171 * Returns 0 on success or -ENOENT if no matching textsearch
172 * registration was found.
173 */
174int textsearch_unregister(struct ts_ops *ops)
175{
176 int err = 0;
177 struct ts_ops *o;
178
179 spin_lock(&ts_mod_lock);
180 list_for_each_entry(o, &ts_ops, list) {
181 if (o == ops) {
182 list_del_rcu(&o->list);
183 goto out;
184 }
185 }
186
187 err = -ENOENT;
188out:
189 spin_unlock(&ts_mod_lock);
190 return err;
191}
192
193struct ts_linear_state
194{
195 unsigned int len;
196 const void *data;
197};
198
199static unsigned int get_linear_data(unsigned int consumed, const u8 **dst,
200 struct ts_config *conf,
201 struct ts_state *state)
202{
203 struct ts_linear_state *st = (struct ts_linear_state *) state->cb;
204
205 if (likely(consumed < st->len)) {
206 *dst = st->data + consumed;
207 return st->len - consumed;
208 }
209
210 return 0;
211}
212
213/**
214 * textsearch_find_continuous - search a pattern in continuous/linear data
215 * @conf: search configuration
216 * @state: search state
217 * @data: data to search in
218 * @len: length of data
219 *
220 * A simplified version of textsearch_find() for continuous/linear data.
221 * Call textsearch_next() to retrieve subsequent matches.
222 *
223 * Returns the position of first occurrence of the pattern or
72fd4a35 224 * %UINT_MAX if no occurrence was found.
2de4ff7b
TG
225 */
226unsigned int textsearch_find_continuous(struct ts_config *conf,
227 struct ts_state *state,
228 const void *data, unsigned int len)
229{
230 struct ts_linear_state *st = (struct ts_linear_state *) state->cb;
231
232 conf->get_next_block = get_linear_data;
233 st->data = data;
234 st->len = len;
235
236 return textsearch_find(conf, state);
237}
238
239/**
240 * textsearch_prepare - Prepare a search
241 * @algo: name of search algorithm
242 * @pattern: pattern data
243 * @len: length of pattern
244 * @gfp_mask: allocation mask
245 * @flags: search flags
246 *
247 * Looks up the search algorithm module and creates a new textsearch
248 * configuration for the specified pattern. Upon completion all
249 * necessary refcnts are held and the configuration must be put back
250 * using textsearch_put() after usage.
251 *
252 * Note: The format of the pattern may not be compatible between
253 * the various search algorithms.
254 *
255 * Returns a new textsearch configuration according to the specified
e03ba84a
PNA
256 * parameters or a ERR_PTR(). If a zero length pattern is passed, this
257 * function returns EINVAL.
2de4ff7b
TG
258 */
259struct ts_config *textsearch_prepare(const char *algo, const void *pattern,
fd4f2df2 260 unsigned int len, gfp_t gfp_mask, int flags)
2de4ff7b
TG
261{
262 int err = -ENOENT;
263 struct ts_config *conf;
264 struct ts_ops *ops;
265
e03ba84a
PNA
266 if (len == 0)
267 return ERR_PTR(-EINVAL);
268
2de4ff7b 269 ops = lookup_ts_algo(algo);
a00caa1f 270#ifdef CONFIG_MODULES
2de4ff7b
TG
271 /*
272 * Why not always autoload you may ask. Some users are
273 * in a situation where requesting a module may deadlock,
274 * especially when the module is located on a NFS mount.
275 */
276 if (ops == NULL && flags & TS_AUTOLOAD) {
277 request_module("ts_%s", algo);
278 ops = lookup_ts_algo(algo);
279 }
280#endif
281
282 if (ops == NULL)
283 goto errout;
284
b9c79678 285 conf = ops->init(pattern, len, gfp_mask, flags);
2de4ff7b
TG
286 if (IS_ERR(conf)) {
287 err = PTR_ERR(conf);
288 goto errout;
289 }
290
291 conf->ops = ops;
292 return conf;
293
294errout:
295 if (ops)
296 module_put(ops->owner);
297
298 return ERR_PTR(err);
299}
300
301/**
302 * textsearch_destroy - destroy a search configuration
303 * @conf: search configuration
304 *
305 * Releases all references of the configuration and frees
306 * up the memory.
307 */
308void textsearch_destroy(struct ts_config *conf)
309{
310 if (conf->ops) {
311 if (conf->ops->destroy)
312 conf->ops->destroy(conf);
313 module_put(conf->ops->owner);
314 }
315
316 kfree(conf);
317}
318
319EXPORT_SYMBOL(textsearch_register);
320EXPORT_SYMBOL(textsearch_unregister);
321EXPORT_SYMBOL(textsearch_prepare);
322EXPORT_SYMBOL(textsearch_find_continuous);
323EXPORT_SYMBOL(textsearch_destroy);
This page took 0.387818 seconds and 5 git commands to generate.