>grand.central.org GCO Public CellServDB 25 Oct 2007
[arla.git] / arlad / fprio.c
blobeb94b9bc5e5b653999f98881da26acfc11151ce3
1 /*
2 * Copyright (c) 1998 - 2000 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
35 * Manage the priority of the files
38 #include "arla_local.h"
39 RCSID("$Id$");
41 /* Hashtable of entries by name */
42 static Hashtab *fpriohashtab;
45 * fprio - hash help functions
48 static int
49 fpriocmp (void *a, void *b)
51 struct fpriorityentry *n1 = (struct fpriorityentry *)a;
52 struct fpriorityentry *n2 = (struct fpriorityentry *)b;
54 return n1->fid.Cell != n2->fid.Cell ||
55 n1->fid.fid.Volume != n2->fid.fid.Volume ||
56 n1->fid.fid.Vnode != n2->fid.fid.Vnode ||
57 n1->fid.fid.Unique != n2->fid.fid.Unique;
60 static unsigned
61 fpriohash (void *a)
63 struct fpriorityentry *n = (struct fpriorityentry *)a;
65 return n->fid.Cell ^ n->fid.fid.Volume ^
66 n->fid.fid.Vnode ^ n->fid.fid.Unique;
70 * fprio_init
72 * Just create the hashtab. Leave the smartness to the user.
75 void
76 fprio_init (char *file)
78 fpriohashtab = hashtabnew (FPRIOCACHE_SIZE, fpriocmp, fpriohash);
79 if (fpriohashtab == NULL)
80 arla_errx (1, ADEBERROR, "fprio_init: hashtabnew failed");
82 if (file)
83 fprio_readin(file);
87 * Cleanout unwanted enteries
90 static Bool
91 cleanupfpriohash(void *ptr, void *arg)
93 struct fpriorityentry *n = (struct fpriorityentry *)ptr;
94 struct fpriorityentry *a = (struct fpriorityentry *)arg;
96 /* Clean out if
98 * NULL cleanout argument
99 * cleanout argument is in the same Cell and
100 * Volume == Vnode == 0 (ie, when whole cell), or
101 * Volume == the victim entry's Volume
102 * && Vnode == 0 (ie, whole volume), or
103 * the Vnode and Unique also match (ie the file/direntry)
105 * This means that memset(&myarg, 0, sizeof(struct fprioentry))
106 * is probably not what you want. (Cleaning out the localcell's
107 * all entries)
110 if (a == NULL ||
111 (a->fid.Cell == n->fid.Cell &&
112 ((a->fid.fid.Volume == 0 && a->fid.fid.Vnode ==0) ||
113 (a->fid.fid.Volume == n->fid.fid.Volume &&
114 (a->fid.fid.Vnode == 0 ||
115 (a->fid.fid.Vnode == n->fid.fid.Vnode &&
116 a->fid.fid.Unique == n->fid.fid.Unique)))))) {
118 AFSCallBack broken_callback = {0, 0, CBDROPPED};
120 fcache_stale_entry (n->fid, broken_callback);
121 free(n);
123 return TRUE;
126 return FALSE;
131 fprio_clear(void)
133 hashtabcleantab(fpriohashtab, cleanupfpriohash, NULL);
134 return 0;
138 * zapp the `fid'
141 void
142 fprio_remove(VenusFid fid)
144 struct fpriorityentry key;
146 key.fid = fid;
147 hashtabfree(fpriohashtab, &key);
148 return;
152 * set a `fid' with `prio' to the hashtab
155 void
156 fprio_set(VenusFid fid, Bool prio)
158 struct fpriorityentry *e;
159 struct fpriorityentry key;
161 key.fid = fid;
163 e = hashtabsearch(fpriohashtab, &key);
164 if (e) {
165 e->priority = prio;
166 return;
169 e = calloc(1, sizeof(*e));
170 if (e == NULL) {
171 arla_warn(ADEBFCACHE, 1, "fprio_set: Out of memory");
172 return;
174 e->fid = fid;
175 e->priority = prio;
177 hashtabadd(fpriohashtab, e);
181 * Read in new data from the file
184 #define MAXFPRIOLINE 1024
187 fprio_readin(char *file)
189 FILE *f;
190 char line[MAXFPRIOLINE];
191 unsigned prio;
192 char cell[MAXFPRIOLINE];
193 int32_t cellnum;
194 VenusFid fid;
195 int lineno = 0 ;
197 f = fopen(file, "r");
198 if (f == NULL)
199 return -1;
201 while(fgets(line, sizeof(line), f) != NULL) {
202 lineno++;
204 line[strcspn(line, "\n")] = '\0';
206 if (line[0] == '#')
207 continue;
209 if (sscanf(line, "%d:%s:%u:%u:%u", &prio,
210 cell,
211 &fid.fid.Volume,
212 &fid.fid.Vnode,
213 &fid.fid.Unique) != 5) {
214 arla_warn(ADEBFCACHE, 1,
215 "fprio_readin: %s:%d contain error(s)",
216 file, lineno);
217 continue;
220 cellnum = cell_name2num(cell);
221 if (cellnum == -1) {
222 arla_warn(ADEBFCACHE, 1,
223 "fprio_readin: the cell %s does not exist", cell);
224 continue;
227 fid.Cell = cellnum;
228 fprio_set(fid, prio ? TRUE : FALSE);
230 return 0;
234 * Find the priority of a fid
237 Bool
238 fprio_get(VenusFid fid)
240 struct fpriorityentry a;
241 struct fpriorityentry *b;
243 a.fid = fid;
245 b = hashtabsearch(fpriohashtab, &a);
246 if (b)
247 return b->priority;
248 return FALSE;
252 * Print the entry `ptr' to the FILE `arg'
255 static Bool
256 fprio_print_entry (void *ptr, void *arg)
258 struct fpriorityentry *n = (struct fpriorityentry *)ptr;
259 const char *cell = cell_num2name(n->fid.Cell);
260 const char *comment;
262 if (cell == NULL) /* If we cant find the cell comment it out */
263 comment = "#";
264 else
265 comment = "";
267 arla_log(ADEBVLOG, "%s%d:%s:%d:%d:%d",
268 comment, n->priority == TRUE ? 1 : 0, cell?cell:"unknowncell",
269 n->fid.fid.Volume, n->fid.fid.Vnode, n->fid.fid.Unique);
270 return FALSE;
274 * Print the status of the fprio module in some strange format...
277 void
278 fprio_status (void)
280 time_t the_time = time(NULL);
282 arla_log(ADEBVLOG, "#fprio entries\n#\n# Date: %s\n#"
283 "#Syntax: (# means comment)\n"
284 "#priority:cell:volume:vnode:unique\n",
285 ctime(&the_time));
286 hashtabforeach (fpriohashtab, fprio_print_entry, NULL);