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