blob: ef7215565d881de05e00c7214623f3a8fa759b8f [file] [log] [blame]
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001/*
2 * net/9p/util.c
3 *
4 * This file contains some helper functions
5 *
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>
9 *
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.
13 *
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.
18 *
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
24 *
25 */
26
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 <net/9p/9p.h>
34
35struct p9_idpool {
Anthony Liguoridea7bbb2008-02-06 19:25:04 -060036 spinlock_t lock;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050037 struct idr pool;
38};
39
40struct p9_idpool *p9_idpool_create(void)
41{
42 struct p9_idpool *p;
43
44 p = kmalloc(sizeof(struct p9_idpool), GFP_KERNEL);
45 if (!p)
46 return ERR_PTR(-ENOMEM);
47
Anthony Liguoridea7bbb2008-02-06 19:25:04 -060048 spin_lock_init(&p->lock);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050049 idr_init(&p->pool);
50
51 return p;
52}
53EXPORT_SYMBOL(p9_idpool_create);
54
55void p9_idpool_destroy(struct p9_idpool *p)
56{
57 idr_destroy(&p->pool);
58 kfree(p);
59}
60EXPORT_SYMBOL(p9_idpool_destroy);
61
62/**
63 * p9_idpool_get - allocate numeric id from pool
64 * @p - pool to allocate from
65 *
66 * XXX - This seems to be an awful generic function, should it be in idr.c with
67 * the lock included in struct idr?
68 */
69
70int p9_idpool_get(struct p9_idpool *p)
71{
72 int i = 0;
73 int error;
Anthony Liguoridea7bbb2008-02-06 19:25:04 -060074 unsigned int flags;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050075
76retry:
77 if (idr_pre_get(&p->pool, GFP_KERNEL) == 0)
78 return 0;
79
Anthony Liguoridea7bbb2008-02-06 19:25:04 -060080 spin_lock_irqsave(&p->lock, flags);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050081
82 /* no need to store exactly p, we just need something non-null */
83 error = idr_get_new(&p->pool, p, &i);
Anthony Liguoridea7bbb2008-02-06 19:25:04 -060084 spin_unlock_irqrestore(&p->lock, flags);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050085
86 if (error == -EAGAIN)
87 goto retry;
88 else if (error)
89 return -1;
90
91 return i;
92}
93EXPORT_SYMBOL(p9_idpool_get);
94
95/**
96 * p9_idpool_put - release numeric id from pool
97 * @p - pool to allocate from
98 *
99 * XXX - This seems to be an awful generic function, should it be in idr.c with
100 * the lock included in struct idr?
101 */
102
103void p9_idpool_put(int id, struct p9_idpool *p)
104{
Anthony Liguoridea7bbb2008-02-06 19:25:04 -0600105 unsigned int flags;
106 spin_lock_irqsave(&p->lock, flags);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500107 idr_remove(&p->pool, id);
Anthony Liguoridea7bbb2008-02-06 19:25:04 -0600108 spin_unlock_irqrestore(&p->lock, flags);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500109}
110EXPORT_SYMBOL(p9_idpool_put);
111
112/**
113 * p9_idpool_check - check if the specified id is available
114 * @id - id to check
115 * @p - pool
116 */
117int p9_idpool_check(int id, struct p9_idpool *p)
118{
119 return idr_find(&p->pool, id) != NULL;
120}
121EXPORT_SYMBOL(p9_idpool_check);