Back to home page

LXR

 
 

    


0001 /*
0002  * lib/textsearch.c Generic text search interface
0003  *
0004  *      This program is free software; you can redistribute it and/or
0005  *      modify it under the terms of the GNU General Public License
0006  *      as published by the Free Software Foundation; either version
0007  *      2 of the License, or (at your option) any later version.
0008  *
0009  * Authors: Thomas Graf <tgraf@suug.ch>
0010  *      Pablo Neira Ayuso <pablo@netfilter.org>
0011  *
0012  * ==========================================================================
0013  *
0014  * INTRODUCTION
0015  *
0016  *   The textsearch infrastructure provides text searching facilities for
0017  *   both linear and non-linear data. Individual search algorithms are
0018  *   implemented in modules and chosen by the user.
0019  *
0020  * ARCHITECTURE
0021  *
0022  *      User
0023  *     +----------------+
0024  *     |        finish()|<--------------(6)-----------------+
0025  *     |get_next_block()|<--------------(5)---------------+ |
0026  *     |                |                     Algorithm   | |
0027  *     |                |                    +------------------------------+
0028  *     |                |                    |  init()   find()   destroy() |
0029  *     |                |                    +------------------------------+
0030  *     |                |       Core API           ^       ^          ^
0031  *     |                |      +---------------+  (2)     (4)        (8)
0032  *     |             (1)|----->| prepare()     |---+       |          |
0033  *     |             (3)|----->| find()/next() |-----------+          |
0034  *     |             (7)|----->| destroy()     |----------------------+
0035  *     +----------------+      +---------------+
0036  *  
0037  *   (1) User configures a search by calling _prepare() specifying the
0038  *       search parameters such as the pattern and algorithm name.
0039  *   (2) Core requests the algorithm to allocate and initialize a search
0040  *       configuration according to the specified parameters.
0041  *   (3) User starts the search(es) by calling _find() or _next() to
0042  *       fetch subsequent occurrences. A state variable is provided
0043  *       to the algorithm to store persistent variables.
0044  *   (4) Core eventually resets the search offset and forwards the find()
0045  *       request to the algorithm.
0046  *   (5) Algorithm calls get_next_block() provided by the user continuously
0047  *       to fetch the data to be searched in block by block.
0048  *   (6) Algorithm invokes finish() after the last call to get_next_block
0049  *       to clean up any leftovers from get_next_block. (Optional)
0050  *   (7) User destroys the configuration by calling _destroy().
0051  *   (8) Core notifies the algorithm to destroy algorithm specific
0052  *       allocations. (Optional)
0053  *
0054  * USAGE
0055  *
0056  *   Before a search can be performed, a configuration must be created
0057  *   by calling textsearch_prepare() specifying the searching algorithm,
0058  *   the pattern to look for and flags. As a flag, you can set TS_IGNORECASE
0059  *   to perform case insensitive matching. But it might slow down
0060  *   performance of algorithm, so you should use it at own your risk.
0061  *   The returned configuration may then be used for an arbitrary
0062  *   amount of times and even in parallel as long as a separate struct
0063  *   ts_state variable is provided to every instance.
0064  *
0065  *   The actual search is performed by either calling textsearch_find_-
0066  *   continuous() for linear data or by providing an own get_next_block()
0067  *   implementation and calling textsearch_find(). Both functions return
0068  *   the position of the first occurrence of the pattern or UINT_MAX if
0069  *   no match was found. Subsequent occurrences can be found by calling
0070  *   textsearch_next() regardless of the linearity of the data.
0071  *
0072  *   Once you're done using a configuration it must be given back via
0073  *   textsearch_destroy.
0074  *
0075  * EXAMPLE
0076  *
0077  *   int pos;
0078  *   struct ts_config *conf;
0079  *   struct ts_state state;
0080  *   const char *pattern = "chicken";
0081  *   const char *example = "We dance the funky chicken";
0082  *
0083  *   conf = textsearch_prepare("kmp", pattern, strlen(pattern),
0084  *                             GFP_KERNEL, TS_AUTOLOAD);
0085  *   if (IS_ERR(conf)) {
0086  *       err = PTR_ERR(conf);
0087  *       goto errout;
0088  *   }
0089  *
0090  *   pos = textsearch_find_continuous(conf, &state, example, strlen(example));
0091  *   if (pos != UINT_MAX)
0092  *       panic("Oh my god, dancing chickens at %d\n", pos);
0093  *
0094  *   textsearch_destroy(conf);
0095  * ==========================================================================
0096  */
0097 
0098 #include <linux/module.h>
0099 #include <linux/types.h>
0100 #include <linux/string.h>
0101 #include <linux/init.h>
0102 #include <linux/rculist.h>
0103 #include <linux/rcupdate.h>
0104 #include <linux/err.h>
0105 #include <linux/textsearch.h>
0106 #include <linux/slab.h>
0107 
0108 static LIST_HEAD(ts_ops);
0109 static DEFINE_SPINLOCK(ts_mod_lock);
0110 
0111 static inline struct ts_ops *lookup_ts_algo(const char *name)
0112 {
0113     struct ts_ops *o;
0114 
0115     rcu_read_lock();
0116     list_for_each_entry_rcu(o, &ts_ops, list) {
0117         if (!strcmp(name, o->name)) {
0118             if (!try_module_get(o->owner))
0119                 o = NULL;
0120             rcu_read_unlock();
0121             return o;
0122         }
0123     }
0124     rcu_read_unlock();
0125 
0126     return NULL;
0127 }
0128 
0129 /**
0130  * textsearch_register - register a textsearch module
0131  * @ops: operations lookup table
0132  *
0133  * This function must be called by textsearch modules to announce
0134  * their presence. The specified &@ops must have %name set to a
0135  * unique identifier and the callbacks find(), init(), get_pattern(),
0136  * and get_pattern_len() must be implemented.
0137  *
0138  * Returns 0 or -EEXISTS if another module has already registered
0139  * with same name.
0140  */
0141 int textsearch_register(struct ts_ops *ops)
0142 {
0143     int err = -EEXIST;
0144     struct ts_ops *o;
0145 
0146     if (ops->name == NULL || ops->find == NULL || ops->init == NULL ||
0147         ops->get_pattern == NULL || ops->get_pattern_len == NULL)
0148         return -EINVAL;
0149 
0150     spin_lock(&ts_mod_lock);
0151     list_for_each_entry(o, &ts_ops, list) {
0152         if (!strcmp(ops->name, o->name))
0153             goto errout;
0154     }
0155 
0156     list_add_tail_rcu(&ops->list, &ts_ops);
0157     err = 0;
0158 errout:
0159     spin_unlock(&ts_mod_lock);
0160     return err;
0161 }
0162 EXPORT_SYMBOL(textsearch_register);
0163 
0164 /**
0165  * textsearch_unregister - unregister a textsearch module
0166  * @ops: operations lookup table
0167  *
0168  * This function must be called by textsearch modules to announce
0169  * their disappearance for examples when the module gets unloaded.
0170  * The &ops parameter must be the same as the one during the
0171  * registration.
0172  *
0173  * Returns 0 on success or -ENOENT if no matching textsearch
0174  * registration was found.
0175  */
0176 int textsearch_unregister(struct ts_ops *ops)
0177 {
0178     int err = 0;
0179     struct ts_ops *o;
0180 
0181     spin_lock(&ts_mod_lock);
0182     list_for_each_entry(o, &ts_ops, list) {
0183         if (o == ops) {
0184             list_del_rcu(&o->list);
0185             goto out;
0186         }
0187     }
0188 
0189     err = -ENOENT;
0190 out:
0191     spin_unlock(&ts_mod_lock);
0192     return err;
0193 }
0194 EXPORT_SYMBOL(textsearch_unregister);
0195 
0196 struct ts_linear_state
0197 {
0198     unsigned int    len;
0199     const void  *data;
0200 };
0201 
0202 static unsigned int get_linear_data(unsigned int consumed, const u8 **dst,
0203                     struct ts_config *conf,
0204                     struct ts_state *state)
0205 {
0206     struct ts_linear_state *st = (struct ts_linear_state *) state->cb;
0207 
0208     if (likely(consumed < st->len)) {
0209         *dst = st->data + consumed;
0210         return st->len - consumed;
0211     }
0212 
0213     return 0;
0214 }
0215 
0216 /**
0217  * textsearch_find_continuous - search a pattern in continuous/linear data
0218  * @conf: search configuration
0219  * @state: search state
0220  * @data: data to search in
0221  * @len: length of data
0222  *
0223  * A simplified version of textsearch_find() for continuous/linear data.
0224  * Call textsearch_next() to retrieve subsequent matches.
0225  *
0226  * Returns the position of first occurrence of the pattern or
0227  * %UINT_MAX if no occurrence was found.
0228  */ 
0229 unsigned int textsearch_find_continuous(struct ts_config *conf,
0230                     struct ts_state *state,
0231                     const void *data, unsigned int len)
0232 {
0233     struct ts_linear_state *st = (struct ts_linear_state *) state->cb;
0234 
0235     conf->get_next_block = get_linear_data;
0236     st->data = data;
0237     st->len = len;
0238 
0239     return textsearch_find(conf, state);
0240 }
0241 EXPORT_SYMBOL(textsearch_find_continuous);
0242 
0243 /**
0244  * textsearch_prepare - Prepare a search
0245  * @algo: name of search algorithm
0246  * @pattern: pattern data
0247  * @len: length of pattern
0248  * @gfp_mask: allocation mask
0249  * @flags: search flags
0250  *
0251  * Looks up the search algorithm module and creates a new textsearch
0252  * configuration for the specified pattern.
0253  *
0254  * Note: The format of the pattern may not be compatible between
0255  *       the various search algorithms.
0256  *
0257  * Returns a new textsearch configuration according to the specified
0258  * parameters or a ERR_PTR(). If a zero length pattern is passed, this
0259  * function returns EINVAL.
0260  */
0261 struct ts_config *textsearch_prepare(const char *algo, const void *pattern,
0262                      unsigned int len, gfp_t gfp_mask, int flags)
0263 {
0264     int err = -ENOENT;
0265     struct ts_config *conf;
0266     struct ts_ops *ops;
0267     
0268     if (len == 0)
0269         return ERR_PTR(-EINVAL);
0270 
0271     ops = lookup_ts_algo(algo);
0272 #ifdef CONFIG_MODULES
0273     /*
0274      * Why not always autoload you may ask. Some users are
0275      * in a situation where requesting a module may deadlock,
0276      * especially when the module is located on a NFS mount.
0277      */
0278     if (ops == NULL && flags & TS_AUTOLOAD) {
0279         request_module("ts_%s", algo);
0280         ops = lookup_ts_algo(algo);
0281     }
0282 #endif
0283 
0284     if (ops == NULL)
0285         goto errout;
0286 
0287     conf = ops->init(pattern, len, gfp_mask, flags);
0288     if (IS_ERR(conf)) {
0289         err = PTR_ERR(conf);
0290         goto errout;
0291     }
0292 
0293     conf->ops = ops;
0294     return conf;
0295 
0296 errout:
0297     if (ops)
0298         module_put(ops->owner);
0299         
0300     return ERR_PTR(err);
0301 }
0302 EXPORT_SYMBOL(textsearch_prepare);
0303 
0304 /**
0305  * textsearch_destroy - destroy a search configuration
0306  * @conf: search configuration
0307  *
0308  * Releases all references of the configuration and frees
0309  * up the memory.
0310  */
0311 void textsearch_destroy(struct ts_config *conf)
0312 {
0313     if (conf->ops) {
0314         if (conf->ops->destroy)
0315             conf->ops->destroy(conf);
0316         module_put(conf->ops->owner);
0317     }
0318 
0319     kfree(conf);
0320 }
0321 EXPORT_SYMBOL(textsearch_destroy);