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>
30 #include <linux/sched.h>
31 #include <linux/parser.h>
32 #include <linux/idr.h>
33 #include <net/9p/9p.h>
36 struct semaphore lock
;
40 struct p9_idpool
*p9_idpool_create(void)
44 p
= kmalloc(sizeof(struct p9_idpool
), GFP_KERNEL
);
46 return ERR_PTR(-ENOMEM
);
53 EXPORT_SYMBOL(p9_idpool_create
);
55 void p9_idpool_destroy(struct p9_idpool
*p
)
57 idr_destroy(&p
->pool
);
60 EXPORT_SYMBOL(p9_idpool_destroy
);
63 * p9_idpool_get - allocate numeric id from pool
64 * @p - pool to allocate from
66 * XXX - This seems to be an awful generic function, should it be in idr.c with
67 * the lock included in struct idr?
70 int p9_idpool_get(struct p9_idpool
*p
)
76 if (idr_pre_get(&p
->pool
, GFP_KERNEL
) == 0)
79 if (down_interruptible(&p
->lock
) == -EINTR
) {
80 P9_EPRINTK(KERN_WARNING
, "Interrupted while locking\n");
84 /* no need to store exactly p, we just need something non-null */
85 error
= idr_get_new(&p
->pool
, p
, &i
);
95 EXPORT_SYMBOL(p9_idpool_get
);
98 * p9_idpool_put - release numeric id from pool
99 * @p - pool to allocate from
101 * XXX - This seems to be an awful generic function, should it be in idr.c with
102 * the lock included in struct idr?
105 void p9_idpool_put(int id
, struct p9_idpool
*p
)
107 if (down_interruptible(&p
->lock
) == -EINTR
) {
108 P9_EPRINTK(KERN_WARNING
, "Interrupted while locking\n");
111 idr_remove(&p
->pool
, id
);
114 EXPORT_SYMBOL(p9_idpool_put
);
117 * p9_idpool_check - check if the specified id is available
121 int p9_idpool_check(int id
, struct p9_idpool
*p
)
123 return idr_find(&p
->pool
, id
) != NULL
;
125 EXPORT_SYMBOL(p9_idpool_check
);