blob: da6af81e59d9fa504345c306e6636acd1f91b093 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050034#include <net/9p/9p.h>
35
Eric Van Hensbergenee443992008-03-05 07:08:09 -060036/**
37 * struct p9_idpool - per-connection accounting for tag idpool
38 * @lock: protects the pool
39 * @pool: idr to allocate tag id from
40 *
41 */
42
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050043struct p9_idpool {
Anthony Liguoridea7bbb2008-02-06 19:25:04 -060044 spinlock_t lock;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050045 struct idr pool;
46};
47
Eric Van Hensbergenee443992008-03-05 07:08:09 -060048/**
49 * p9_idpool_create - create a new per-connection id pool
50 *
51 */
52
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050053struct p9_idpool *p9_idpool_create(void)
54{
55 struct p9_idpool *p;
56
57 p = kmalloc(sizeof(struct p9_idpool), GFP_KERNEL);
58 if (!p)
59 return ERR_PTR(-ENOMEM);
60
Anthony Liguoridea7bbb2008-02-06 19:25:04 -060061 spin_lock_init(&p->lock);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050062 idr_init(&p->pool);
63
64 return p;
65}
66EXPORT_SYMBOL(p9_idpool_create);
67
Eric Van Hensbergenee443992008-03-05 07:08:09 -060068/**
69 * p9_idpool_destroy - create a new per-connection id pool
Lucas De Marchi25985ed2011-03-30 22:57:33 -030070 * @p: idpool to destroy
Eric Van Hensbergenee443992008-03-05 07:08:09 -060071 */
72
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050073void p9_idpool_destroy(struct p9_idpool *p)
74{
75 idr_destroy(&p->pool);
76 kfree(p);
77}
78EXPORT_SYMBOL(p9_idpool_destroy);
79
80/**
81 * p9_idpool_get - allocate numeric id from pool
Eric Van Hensbergenee443992008-03-05 07:08:09 -060082 * @p: pool to allocate from
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050083 *
Eric Van Hensbergenee443992008-03-05 07:08:09 -060084 * Bugs: This seems to be an awful generic function, should it be in idr.c with
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050085 * the lock included in struct idr?
86 */
87
88int p9_idpool_get(struct p9_idpool *p)
89{
90 int i = 0;
91 int error;
Steven Rostedtd0c44712008-05-03 17:29:50 -050092 unsigned long flags;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050093
94retry:
Aneesh Kumar K.Veeff66e2011-03-08 16:39:47 +053095 if (idr_pre_get(&p->pool, GFP_NOFS) == 0)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050096 return 0;
97
Anthony Liguoridea7bbb2008-02-06 19:25:04 -060098 spin_lock_irqsave(&p->lock, flags);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050099
100 /* no need to store exactly p, we just need something non-null */
101 error = idr_get_new(&p->pool, p, &i);
Anthony Liguoridea7bbb2008-02-06 19:25:04 -0600102 spin_unlock_irqrestore(&p->lock, flags);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500103
104 if (error == -EAGAIN)
105 goto retry;
106 else if (error)
107 return -1;
108
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500109 P9_DPRINTK(P9_DEBUG_MUX, " id %d pool %p\n", i, p);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500110 return i;
111}
112EXPORT_SYMBOL(p9_idpool_get);
113
114/**
115 * p9_idpool_put - release numeric id from pool
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600116 * @id: numeric id which is being released
117 * @p: pool to release id into
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500118 *
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600119 * Bugs: This seems to be an awful generic function, should it be in idr.c with
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500120 * the lock included in struct idr?
121 */
122
123void p9_idpool_put(int id, struct p9_idpool *p)
124{
Steven Rostedtd0c44712008-05-03 17:29:50 -0500125 unsigned long flags;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500126
127 P9_DPRINTK(P9_DEBUG_MUX, " id %d pool %p\n", id, p);
128
Anthony Liguoridea7bbb2008-02-06 19:25:04 -0600129 spin_lock_irqsave(&p->lock, flags);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500130 idr_remove(&p->pool, id);
Anthony Liguoridea7bbb2008-02-06 19:25:04 -0600131 spin_unlock_irqrestore(&p->lock, flags);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500132}
133EXPORT_SYMBOL(p9_idpool_put);
134
135/**
136 * p9_idpool_check - check if the specified id is available
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600137 * @id: id to check
138 * @p: pool to check
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500139 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600140
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500141int p9_idpool_check(int id, struct p9_idpool *p)
142{
143 return idr_find(&p->pool, id) != NULL;
144}
145EXPORT_SYMBOL(p9_idpool_check);
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600146