Add the -p option to specify a port for the spclient examples
[spfs.git] / libspfs / fidpool.c
blobdc32d6bf9a543290f3665a6da1d4ac922db006d9
1 /*
2 * Copyright (C) 2006 by Latchesar Ionkov <lucho@ionkov.net>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * LATCHESAR IONKOV AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <errno.h>
27 #include "spfs.h"
28 #include "spfsimpl.h"
30 Spfid**
31 sp_fidpool_create(void)
33 Spfid **ret;
35 return calloc(FID_HTABLE_SIZE, sizeof(*ret));
38 void
39 sp_fidpool_destroy(Spfid **pool)
41 int i;
42 Spfid *f, *ff;
43 Spsrv *srv;
45 for(i = 0; i < FID_HTABLE_SIZE; i++) {
46 f = pool[i];
47 while (f != NULL) {
48 ff = f->next;
49 srv = f->conn->srv;
50 if (f->type&Qtauth && srv->auth && srv->auth->clunk)
51 (*srv->auth->clunk)(f);
52 else if (!(f->type&Qtauth) && srv->fiddestroy)
53 (*srv->fiddestroy)(f);
54 free(f);
55 f = ff;
59 free(pool);
62 Spfid*
63 sp_fid_find(Spconn *conn, u32 fid)
65 int hash;
66 Spfid **htable, *f, **prevp;
68 hash = fid % FID_HTABLE_SIZE;
69 htable = conn->fidpool;
70 if (!htable)
71 return NULL;
73 prevp = &htable[hash];
74 f = *prevp;
75 while (f != NULL) {
76 if (f->fid == fid) {
77 *prevp = f->next;
78 f->next = htable[hash];
79 htable[hash] = f;
80 break;
83 prevp = &f->next;
84 f = *prevp;
86 return f;
89 Spfid*
90 sp_fid_create(Spconn *conn, u32 fid, void *aux)
92 int hash;
93 Spfid **htable, *f;
95 hash = fid % FID_HTABLE_SIZE;
96 htable = conn->fidpool;
97 if (!htable)
98 return NULL;
100 f = sp_fid_find(conn, fid);
101 if (f)
102 return NULL;
104 f = sp_malloc(sizeof(*f));
105 if (!f)
106 return NULL;
108 f->fid = fid;
109 f->conn = conn;
110 f->refcount = 0;
111 f->omode = ~0;
112 f->type = 0;
113 f->diroffset = 0;
114 f->user = NULL;
115 f->aux = aux;
117 f->next = htable[hash];
118 htable[hash] = f;
120 return f;
124 sp_fid_destroy(Spfid *fid)
126 int hash;
127 Spconn *conn;
128 Spsrv *srv;
129 Spfid **htable, *f, **prevp;
131 conn = fid->conn;
132 hash = fid->fid % FID_HTABLE_SIZE;
133 htable = conn->fidpool;
134 if (!htable)
135 return 0;
137 prevp = &htable[hash];
138 f = *prevp;
139 while (f != NULL) {
140 if (f->fid == fid->fid) {
141 *prevp = f->next;
142 srv = f->conn->srv;
143 if (f->type & Qtauth && srv->auth && srv->auth->clunk)
144 (*srv->auth->clunk)(f);
145 else if (!(f->type&Qtauth) && srv->fiddestroy)
146 (*srv->fiddestroy)(f);
148 if (f->user)
149 sp_user_decref(f->user);
151 free(f);
152 break;
155 prevp = &f->next;
156 f = *prevp;
158 return f != NULL;
161 void
162 sp_fid_incref(Spfid *fid)
164 if (!fid)
165 return;
167 fid->refcount++;
170 void
171 sp_fid_decref(Spfid *fid)
173 if (!fid)
174 return;
176 fid->refcount--;
178 if (!fid->refcount)
179 sp_fid_destroy(fid);