memcg/sl[au]b: shrink dead caches
[linux-2.6/libata-dev.git] / net / 9p / util.c
blob6ceeeb384de73cbd4583b9b22a41c27b8b0885bb
1 /*
2 * net/9p/util.c
4 * This file contains some helper functions
6 * Copyright (C) 2007 by Latchesar Ionkov <lucho@ionkov.net>
7 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
8 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to:
21 * Free Software Foundation
22 * 51 Franklin Street, Fifth Floor
23 * Boston, MA 02111-1301 USA
27 #include <linux/module.h>
28 #include <linux/errno.h>
29 #include <linux/fs.h>
30 #include <linux/sched.h>
31 #include <linux/parser.h>
32 #include <linux/idr.h>
33 #include <linux/slab.h>
34 #include <net/9p/9p.h>
36 /**
37 * struct p9_idpool - per-connection accounting for tag idpool
38 * @lock: protects the pool
39 * @pool: idr to allocate tag id from
43 struct p9_idpool {
44 spinlock_t lock;
45 struct idr pool;
48 /**
49 * p9_idpool_create - create a new per-connection id pool
53 struct p9_idpool *p9_idpool_create(void)
55 struct p9_idpool *p;
57 p = kmalloc(sizeof(struct p9_idpool), GFP_KERNEL);
58 if (!p)
59 return ERR_PTR(-ENOMEM);
61 spin_lock_init(&p->lock);
62 idr_init(&p->pool);
64 return p;
66 EXPORT_SYMBOL(p9_idpool_create);
68 /**
69 * p9_idpool_destroy - create a new per-connection id pool
70 * @p: idpool to destroy
73 void p9_idpool_destroy(struct p9_idpool *p)
75 idr_destroy(&p->pool);
76 kfree(p);
78 EXPORT_SYMBOL(p9_idpool_destroy);
80 /**
81 * p9_idpool_get - allocate numeric id from pool
82 * @p: pool to allocate from
84 * Bugs: This seems to be an awful generic function, should it be in idr.c with
85 * the lock included in struct idr?
88 int p9_idpool_get(struct p9_idpool *p)
90 int i = 0;
91 int error;
92 unsigned long flags;
94 retry:
95 if (idr_pre_get(&p->pool, GFP_NOFS) == 0)
96 return -1;
98 spin_lock_irqsave(&p->lock, flags);
100 /* no need to store exactly p, we just need something non-null */
101 error = idr_get_new(&p->pool, p, &i);
102 spin_unlock_irqrestore(&p->lock, flags);
104 if (error == -EAGAIN)
105 goto retry;
106 else if (error)
107 return -1;
109 p9_debug(P9_DEBUG_MUX, " id %d pool %p\n", i, p);
110 return i;
112 EXPORT_SYMBOL(p9_idpool_get);
115 * p9_idpool_put - release numeric id from pool
116 * @id: numeric id which is being released
117 * @p: pool to release id into
119 * Bugs: This seems to be an awful generic function, should it be in idr.c with
120 * the lock included in struct idr?
123 void p9_idpool_put(int id, struct p9_idpool *p)
125 unsigned long flags;
127 p9_debug(P9_DEBUG_MUX, " id %d pool %p\n", id, p);
129 spin_lock_irqsave(&p->lock, flags);
130 idr_remove(&p->pool, id);
131 spin_unlock_irqrestore(&p->lock, flags);
133 EXPORT_SYMBOL(p9_idpool_put);
136 * p9_idpool_check - check if the specified id is available
137 * @id: id to check
138 * @p: pool to check
141 int p9_idpool_check(int id, struct p9_idpool *p)
143 return idr_find(&p->pool, id) != NULL;
145 EXPORT_SYMBOL(p9_idpool_check);