2 * Copyright (c) 1998 - 2000 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
35 * Manage the priority of the files
38 #include "arla_local.h"
41 /* Hashtable of entries by name */
42 static Hashtab
*fpriohashtab
;
45 * fprio - hash help functions
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
;
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
;
72 * Just create the hashtab. Leave the smartness to the user.
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");
87 * Cleanout unwanted enteries
91 cleanupfpriohash(void *ptr
, void *arg
)
93 struct fpriorityentry
*n
= (struct fpriorityentry
*)ptr
;
94 struct fpriorityentry
*a
= (struct fpriorityentry
*)arg
;
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
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
);
133 hashtabcleantab(fpriohashtab
, cleanupfpriohash
, NULL
);
142 fprio_remove(VenusFid fid
)
144 struct fpriorityentry key
;
147 hashtabfree(fpriohashtab
, &key
);
152 * set a `fid' with `prio' to the hashtab
156 fprio_set(VenusFid fid
, Bool prio
)
158 struct fpriorityentry
*e
;
159 struct fpriorityentry key
;
163 e
= hashtabsearch(fpriohashtab
, &key
);
169 e
= calloc(1, sizeof(*e
));
171 arla_warn(ADEBFCACHE
, 1, "fprio_set: Out of memory");
177 hashtabadd(fpriohashtab
, e
);
181 * Read in new data from the file
184 #define MAXFPRIOLINE 1024
187 fprio_readin(char *file
)
190 char line
[MAXFPRIOLINE
];
192 char cell
[MAXFPRIOLINE
];
197 f
= fopen(file
, "r");
201 while(fgets(line
, sizeof(line
), f
) != NULL
) {
204 line
[strcspn(line
, "\n")] = '\0';
209 if (sscanf(line
, "%d:%s:%u:%u:%u", &prio
,
213 &fid
.fid
.Unique
) != 5) {
214 arla_warn(ADEBFCACHE
, 1,
215 "fprio_readin: %s:%d contain error(s)",
220 cellnum
= cell_name2num(cell
);
222 arla_warn(ADEBFCACHE
, 1,
223 "fprio_readin: the cell %s does not exist", cell
);
228 fprio_set(fid
, prio
? TRUE
: FALSE
);
234 * Find the priority of a fid
238 fprio_get(VenusFid fid
)
240 struct fpriorityentry a
;
241 struct fpriorityentry
*b
;
245 b
= hashtabsearch(fpriohashtab
, &a
);
252 * Print the entry `ptr' to the FILE `arg'
256 fprio_print_entry (void *ptr
, void *arg
)
258 struct fpriorityentry
*n
= (struct fpriorityentry
*)ptr
;
259 const char *cell
= cell_num2name(n
->fid
.Cell
);
262 if (cell
== NULL
) /* If we cant find the cell comment it out */
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
);
274 * Print the status of the fprio module in some strange format...
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",
286 hashtabforeach (fpriohashtab
, fprio_print_entry
, NULL
);