adutil.c revision 1.2 1 1.2 lonewolf /* $NetBSD: adutil.c,v 1.2 2003/01/27 04:08:45 lonewolf Exp $ */
2 1.1 jdolecek
3 1.1 jdolecek /*
4 1.1 jdolecek * Copyright (c) 1994 Christian E. Hopps
5 1.1 jdolecek * Copyright (c) 1996 Matthias Scheler
6 1.1 jdolecek * All rights reserved.
7 1.1 jdolecek *
8 1.1 jdolecek * Redistribution and use in source and binary forms, with or without
9 1.1 jdolecek * modification, are permitted provided that the following conditions
10 1.1 jdolecek * are met:
11 1.1 jdolecek * 1. Redistributions of source code must retain the above copyright
12 1.1 jdolecek * notice, this list of conditions and the following disclaimer.
13 1.1 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 jdolecek * notice, this list of conditions and the following disclaimer in the
15 1.1 jdolecek * documentation and/or other materials provided with the distribution.
16 1.1 jdolecek * 3. All advertising materials mentioning features or use of this software
17 1.1 jdolecek * must display the following acknowledgement:
18 1.1 jdolecek * This product includes software developed by Christian E. Hopps.
19 1.1 jdolecek * 4. The name of the author may not be used to endorse or promote products
20 1.1 jdolecek * derived from this software without specific prior written permission
21 1.1 jdolecek *
22 1.1 jdolecek * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 jdolecek * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 jdolecek * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 jdolecek * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 jdolecek * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.1 jdolecek * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.1 jdolecek * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.1 jdolecek * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.1 jdolecek * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.1 jdolecek * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 jdolecek */
33 1.1 jdolecek
34 1.1 jdolecek #include <sys/cdefs.h>
35 1.2 lonewolf __KERNEL_RCSID(0, "$NetBSD: adutil.c,v 1.2 2003/01/27 04:08:45 lonewolf Exp $");
36 1.1 jdolecek
37 1.1 jdolecek #include <sys/param.h>
38 1.1 jdolecek #include <sys/vnode.h>
39 1.1 jdolecek #include <sys/mount.h>
40 1.1 jdolecek #include <sys/proc.h>
41 1.1 jdolecek #include <sys/systm.h>
42 1.1 jdolecek #include <sys/malloc.h>
43 1.1 jdolecek #include <sys/time.h>
44 1.1 jdolecek #include <sys/queue.h>
45 1.1 jdolecek #include <sys/buf.h>
46 1.1 jdolecek #include <fs/adosfs/adosfs.h>
47 1.1 jdolecek
48 1.1 jdolecek /*
49 1.1 jdolecek * look for anode in the mount's hash table, return locked.
50 1.1 jdolecek */
51 1.1 jdolecek #define AHASH(an) ((an) & (ANODEHASHSZ - 1))
52 1.1 jdolecek static int CapitalChar __P((int, int));
53 1.1 jdolecek
54 1.1 jdolecek extern struct simplelock adosfs_hashlock;
55 1.1 jdolecek
56 1.1 jdolecek struct vnode *
57 1.1 jdolecek adosfs_ahashget(mp, an)
58 1.1 jdolecek struct mount *mp;
59 1.1 jdolecek ino_t an;
60 1.1 jdolecek {
61 1.1 jdolecek struct anodechain *hp;
62 1.1 jdolecek struct anode *ap;
63 1.1 jdolecek struct vnode *vp;
64 1.1 jdolecek
65 1.1 jdolecek hp = &VFSTOADOSFS(mp)->anodetab[AHASH(an)];
66 1.1 jdolecek
67 1.1 jdolecek start_over:
68 1.1 jdolecek simple_lock(&adosfs_hashlock);
69 1.1 jdolecek for (ap = hp->lh_first; ap != NULL; ap = ap->link.le_next) {
70 1.1 jdolecek if (ap->block == an) {
71 1.1 jdolecek vp = ATOV(ap);
72 1.1 jdolecek simple_lock(&vp->v_interlock);
73 1.1 jdolecek simple_unlock(&adosfs_hashlock);
74 1.1 jdolecek if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
75 1.1 jdolecek goto start_over;
76 1.1 jdolecek return (ATOV(ap));
77 1.1 jdolecek }
78 1.1 jdolecek }
79 1.1 jdolecek simple_unlock(&adosfs_hashlock);
80 1.1 jdolecek return (NULL);
81 1.1 jdolecek }
82 1.1 jdolecek
83 1.1 jdolecek /*
84 1.1 jdolecek * insert in hash table and lock
85 1.1 jdolecek *
86 1.1 jdolecek * ap->vp must have been initialized before this call.
87 1.1 jdolecek */
88 1.1 jdolecek void
89 1.1 jdolecek adosfs_ainshash(amp, ap)
90 1.1 jdolecek struct adosfsmount *amp;
91 1.1 jdolecek struct anode *ap;
92 1.1 jdolecek {
93 1.1 jdolecek lockmgr(&ap->vp->v_lock, LK_EXCLUSIVE, (struct simplelock *)0);
94 1.1 jdolecek
95 1.1 jdolecek simple_lock(&adosfs_hashlock);
96 1.1 jdolecek LIST_INSERT_HEAD(&->anodetab[AHASH(ap->block)], ap, link);
97 1.1 jdolecek simple_unlock(&adosfs_hashlock);
98 1.1 jdolecek }
99 1.1 jdolecek
100 1.1 jdolecek void
101 1.1 jdolecek adosfs_aremhash(ap)
102 1.1 jdolecek struct anode *ap;
103 1.1 jdolecek {
104 1.1 jdolecek simple_lock(&adosfs_hashlock);
105 1.1 jdolecek LIST_REMOVE(ap, link);
106 1.1 jdolecek simple_unlock(&adosfs_hashlock);
107 1.1 jdolecek }
108 1.1 jdolecek
109 1.1 jdolecek int
110 1.1 jdolecek adosfs_getblktype(amp, bp)
111 1.1 jdolecek struct adosfsmount *amp;
112 1.1 jdolecek struct buf *bp;
113 1.1 jdolecek {
114 1.1 jdolecek if (adoscksum(bp, amp->nwords)) {
115 1.1 jdolecek #ifdef DIAGNOSTIC
116 1.2 lonewolf printf("adosfs: aget: cksum of blk %" PRId64 " failed\n",
117 1.1 jdolecek bp->b_blkno / (amp->bsize / DEV_BSIZE));
118 1.1 jdolecek #endif
119 1.1 jdolecek return (-1);
120 1.1 jdolecek }
121 1.1 jdolecek
122 1.1 jdolecek /*
123 1.1 jdolecek * check primary block type
124 1.1 jdolecek */
125 1.1 jdolecek if (adoswordn(bp, 0) != BPT_SHORT) {
126 1.1 jdolecek #ifdef DIAGNOSTIC
127 1.2 lonewolf printf("adosfs: aget: bad primary type blk %" PRId64 " (type = %d)\n",
128 1.1 jdolecek bp->b_blkno / (amp->bsize / DEV_BSIZE), adoswordn(bp,0));
129 1.1 jdolecek #endif
130 1.1 jdolecek return (-1);
131 1.1 jdolecek }
132 1.1 jdolecek
133 1.1 jdolecek /*
134 1.1 jdolecek * Check secondary block type.
135 1.1 jdolecek */
136 1.1 jdolecek switch (adoswordn(bp, amp->nwords - 1)) {
137 1.1 jdolecek case BST_RDIR: /* root block */
138 1.1 jdolecek return (AROOT);
139 1.1 jdolecek case BST_LDIR: /* hard link to dir */
140 1.1 jdolecek return (ALDIR);
141 1.1 jdolecek case BST_UDIR: /* user dir */
142 1.1 jdolecek return (ADIR);
143 1.1 jdolecek case BST_LFILE: /* hard link to file */
144 1.1 jdolecek return (ALFILE);
145 1.1 jdolecek case BST_FILE: /* file header */
146 1.1 jdolecek return (AFILE);
147 1.1 jdolecek case BST_SLINK: /* soft link */
148 1.1 jdolecek return (ASLINK);
149 1.1 jdolecek }
150 1.1 jdolecek
151 1.1 jdolecek #ifdef DIAGNOSTIC
152 1.2 lonewolf printf("adosfs: aget: bad secondary type blk %" PRId64 " (type = %d)\n",
153 1.1 jdolecek bp->b_blkno / (amp->bsize / DEV_BSIZE), adoswordn(bp, amp->nwords - 1));
154 1.1 jdolecek #endif
155 1.1 jdolecek
156 1.1 jdolecek return (-1);
157 1.1 jdolecek }
158 1.1 jdolecek
159 1.1 jdolecek int
160 1.1 jdolecek adunixprot(adprot)
161 1.1 jdolecek int adprot;
162 1.1 jdolecek {
163 1.1 jdolecek if (adprot & 0xc000ee00) {
164 1.1 jdolecek adprot = (adprot & 0xee0e) >> 1;
165 1.1 jdolecek return (((adprot & 0x7) << 6) |
166 1.1 jdolecek ((adprot & 0x700) >> 5) |
167 1.1 jdolecek ((adprot & 0x7000) >> 12));
168 1.1 jdolecek }
169 1.1 jdolecek else {
170 1.1 jdolecek adprot = (adprot >> 1) & 0x7;
171 1.1 jdolecek return((adprot << 6) | (adprot << 3) | adprot);
172 1.1 jdolecek }
173 1.1 jdolecek }
174 1.1 jdolecek
175 1.1 jdolecek static int
176 1.1 jdolecek CapitalChar(ch, inter)
177 1.1 jdolecek int ch, inter;
178 1.1 jdolecek {
179 1.1 jdolecek if ((ch >= 'a' && ch <= 'z') ||
180 1.1 jdolecek (inter && ch >= 0xe0 && ch <= 0xfe && ch != 0xf7))
181 1.1 jdolecek return(ch - ('a' - 'A'));
182 1.1 jdolecek return(ch);
183 1.1 jdolecek }
184 1.1 jdolecek
185 1.1 jdolecek u_int32_t
186 1.1 jdolecek adoscksum(bp, n)
187 1.1 jdolecek struct buf *bp;
188 1.1 jdolecek int n;
189 1.1 jdolecek {
190 1.1 jdolecek u_int32_t sum, *lp;
191 1.1 jdolecek
192 1.1 jdolecek lp = (u_int32_t *)bp->b_data;
193 1.1 jdolecek sum = 0;
194 1.1 jdolecek
195 1.1 jdolecek while (n--)
196 1.1 jdolecek sum += ntohl(*lp++);
197 1.1 jdolecek return(sum);
198 1.1 jdolecek }
199 1.1 jdolecek
200 1.1 jdolecek int
201 1.1 jdolecek adoscaseequ(name1, name2, len, inter)
202 1.1 jdolecek const u_char *name1, *name2;
203 1.1 jdolecek int len, inter;
204 1.1 jdolecek {
205 1.1 jdolecek while (len-- > 0)
206 1.1 jdolecek if (CapitalChar(*name1++, inter) !=
207 1.1 jdolecek CapitalChar(*name2++, inter))
208 1.1 jdolecek return 0;
209 1.1 jdolecek
210 1.1 jdolecek return 1;
211 1.1 jdolecek }
212 1.1 jdolecek
213 1.1 jdolecek int
214 1.1 jdolecek adoshash(nam, namlen, nelt, inter)
215 1.1 jdolecek const u_char *nam;
216 1.1 jdolecek int namlen, nelt, inter;
217 1.1 jdolecek {
218 1.1 jdolecek int val;
219 1.1 jdolecek
220 1.1 jdolecek val = namlen;
221 1.1 jdolecek while (namlen--)
222 1.1 jdolecek val = ((val * 13) + CapitalChar(*nam++, inter)) & 0x7ff;
223 1.1 jdolecek return(val % nelt);
224 1.1 jdolecek }
225 1.1 jdolecek
226 1.1 jdolecek #ifdef notyet
227 1.1 jdolecek /*
228 1.1 jdolecek * datestamp is local time, tv is to be UTC
229 1.1 jdolecek */
230 1.1 jdolecek int
231 1.1 jdolecek dstotv(dsp, tvp)
232 1.1 jdolecek struct datestamp *dsp;
233 1.1 jdolecek struct timeval *tvp;
234 1.1 jdolecek {
235 1.1 jdolecek }
236 1.1 jdolecek
237 1.1 jdolecek /*
238 1.1 jdolecek * tv is UTC, datestamp is to be local time
239 1.1 jdolecek */
240 1.1 jdolecek int
241 1.1 jdolecek tvtods(tvp, dsp)
242 1.1 jdolecek struct timeval *tvp;
243 1.1 jdolecek struct datestamp *dsp;
244 1.1 jdolecek {
245 1.1 jdolecek }
246 1.1 jdolecek #endif
247 1.1 jdolecek
248 1.1 jdolecek #if BYTE_ORDER != BIG_ENDIAN
249 1.1 jdolecek u_int32_t
250 1.1 jdolecek adoswordn(bp, wn)
251 1.1 jdolecek struct buf *bp;
252 1.1 jdolecek int wn;
253 1.1 jdolecek {
254 1.1 jdolecek /*
255 1.1 jdolecek * ados stored in network (big endian) order
256 1.1 jdolecek */
257 1.1 jdolecek return(ntohl(*((u_int32_t *)bp->b_data + wn)));
258 1.1 jdolecek }
259 1.1 jdolecek #endif
260