adutil.c revision 1.10 1 1.10 dsl /* $NetBSD: adutil.c,v 1.10 2009/03/14 15:36:21 dsl 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.10 dsl __KERNEL_RCSID(0, "$NetBSD: adutil.c,v 1.10 2009/03/14 15:36:21 dsl 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.7 ad #include <sys/simplelock.h>
47 1.1 jdolecek #include <fs/adosfs/adosfs.h>
48 1.1 jdolecek
49 1.1 jdolecek /*
50 1.1 jdolecek * look for anode in the mount's hash table, return locked.
51 1.1 jdolecek */
52 1.1 jdolecek #define AHASH(an) ((an) & (ANODEHASHSZ - 1))
53 1.9 dsl static int CapitalChar(int, int);
54 1.1 jdolecek
55 1.1 jdolecek extern struct simplelock adosfs_hashlock;
56 1.1 jdolecek
57 1.3 perry struct vnode *
58 1.10 dsl adosfs_ahashget(struct mount *mp, ino_t an)
59 1.1 jdolecek {
60 1.1 jdolecek struct anodechain *hp;
61 1.1 jdolecek struct anode *ap;
62 1.1 jdolecek struct vnode *vp;
63 1.1 jdolecek
64 1.1 jdolecek hp = &VFSTOADOSFS(mp)->anodetab[AHASH(an)];
65 1.1 jdolecek
66 1.1 jdolecek start_over:
67 1.1 jdolecek simple_lock(&adosfs_hashlock);
68 1.1 jdolecek for (ap = hp->lh_first; ap != NULL; ap = ap->link.le_next) {
69 1.1 jdolecek if (ap->block == an) {
70 1.1 jdolecek vp = ATOV(ap);
71 1.6 ad mutex_enter(&vp->v_interlock);
72 1.1 jdolecek simple_unlock(&adosfs_hashlock);
73 1.1 jdolecek if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
74 1.1 jdolecek goto start_over;
75 1.1 jdolecek return (ATOV(ap));
76 1.1 jdolecek }
77 1.1 jdolecek }
78 1.1 jdolecek simple_unlock(&adosfs_hashlock);
79 1.1 jdolecek return (NULL);
80 1.1 jdolecek }
81 1.1 jdolecek
82 1.1 jdolecek /*
83 1.1 jdolecek * insert in hash table and lock
84 1.1 jdolecek *
85 1.1 jdolecek * ap->vp must have been initialized before this call.
86 1.1 jdolecek */
87 1.1 jdolecek void
88 1.10 dsl adosfs_ainshash(struct adosfsmount *amp, struct anode *ap)
89 1.1 jdolecek {
90 1.8 ad vlockmgr(&ap->vp->v_lock, LK_EXCLUSIVE);
91 1.1 jdolecek
92 1.1 jdolecek simple_lock(&adosfs_hashlock);
93 1.1 jdolecek LIST_INSERT_HEAD(&->anodetab[AHASH(ap->block)], ap, link);
94 1.1 jdolecek simple_unlock(&adosfs_hashlock);
95 1.1 jdolecek }
96 1.1 jdolecek
97 1.1 jdolecek void
98 1.10 dsl adosfs_aremhash(struct anode *ap)
99 1.1 jdolecek {
100 1.1 jdolecek simple_lock(&adosfs_hashlock);
101 1.1 jdolecek LIST_REMOVE(ap, link);
102 1.1 jdolecek simple_unlock(&adosfs_hashlock);
103 1.1 jdolecek }
104 1.1 jdolecek
105 1.1 jdolecek int
106 1.10 dsl adosfs_getblktype(struct adosfsmount *amp, struct buf *bp)
107 1.1 jdolecek {
108 1.1 jdolecek if (adoscksum(bp, amp->nwords)) {
109 1.1 jdolecek #ifdef DIAGNOSTIC
110 1.2 lonewolf printf("adosfs: aget: cksum of blk %" PRId64 " failed\n",
111 1.1 jdolecek bp->b_blkno / (amp->bsize / DEV_BSIZE));
112 1.1 jdolecek #endif
113 1.1 jdolecek return (-1);
114 1.1 jdolecek }
115 1.1 jdolecek
116 1.1 jdolecek /*
117 1.1 jdolecek * check primary block type
118 1.1 jdolecek */
119 1.1 jdolecek if (adoswordn(bp, 0) != BPT_SHORT) {
120 1.1 jdolecek #ifdef DIAGNOSTIC
121 1.2 lonewolf printf("adosfs: aget: bad primary type blk %" PRId64 " (type = %d)\n",
122 1.1 jdolecek bp->b_blkno / (amp->bsize / DEV_BSIZE), adoswordn(bp,0));
123 1.1 jdolecek #endif
124 1.1 jdolecek return (-1);
125 1.1 jdolecek }
126 1.1 jdolecek
127 1.1 jdolecek /*
128 1.1 jdolecek * Check secondary block type.
129 1.1 jdolecek */
130 1.1 jdolecek switch (adoswordn(bp, amp->nwords - 1)) {
131 1.1 jdolecek case BST_RDIR: /* root block */
132 1.1 jdolecek return (AROOT);
133 1.1 jdolecek case BST_LDIR: /* hard link to dir */
134 1.1 jdolecek return (ALDIR);
135 1.1 jdolecek case BST_UDIR: /* user dir */
136 1.1 jdolecek return (ADIR);
137 1.1 jdolecek case BST_LFILE: /* hard link to file */
138 1.1 jdolecek return (ALFILE);
139 1.1 jdolecek case BST_FILE: /* file header */
140 1.1 jdolecek return (AFILE);
141 1.1 jdolecek case BST_SLINK: /* soft link */
142 1.1 jdolecek return (ASLINK);
143 1.1 jdolecek }
144 1.1 jdolecek
145 1.1 jdolecek #ifdef DIAGNOSTIC
146 1.2 lonewolf printf("adosfs: aget: bad secondary type blk %" PRId64 " (type = %d)\n",
147 1.1 jdolecek bp->b_blkno / (amp->bsize / DEV_BSIZE), adoswordn(bp, amp->nwords - 1));
148 1.1 jdolecek #endif
149 1.1 jdolecek
150 1.1 jdolecek return (-1);
151 1.1 jdolecek }
152 1.1 jdolecek
153 1.1 jdolecek int
154 1.10 dsl adunixprot(int adprot)
155 1.1 jdolecek {
156 1.1 jdolecek if (adprot & 0xc000ee00) {
157 1.1 jdolecek adprot = (adprot & 0xee0e) >> 1;
158 1.1 jdolecek return (((adprot & 0x7) << 6) |
159 1.1 jdolecek ((adprot & 0x700) >> 5) |
160 1.1 jdolecek ((adprot & 0x7000) >> 12));
161 1.1 jdolecek }
162 1.1 jdolecek else {
163 1.1 jdolecek adprot = (adprot >> 1) & 0x7;
164 1.1 jdolecek return((adprot << 6) | (adprot << 3) | adprot);
165 1.1 jdolecek }
166 1.1 jdolecek }
167 1.1 jdolecek
168 1.1 jdolecek static int
169 1.1 jdolecek CapitalChar(ch, inter)
170 1.1 jdolecek int ch, inter;
171 1.1 jdolecek {
172 1.3 perry if ((ch >= 'a' && ch <= 'z') ||
173 1.1 jdolecek (inter && ch >= 0xe0 && ch <= 0xfe && ch != 0xf7))
174 1.1 jdolecek return(ch - ('a' - 'A'));
175 1.1 jdolecek return(ch);
176 1.1 jdolecek }
177 1.1 jdolecek
178 1.1 jdolecek u_int32_t
179 1.10 dsl adoscksum(struct buf *bp, int n)
180 1.1 jdolecek {
181 1.1 jdolecek u_int32_t sum, *lp;
182 1.3 perry
183 1.1 jdolecek lp = (u_int32_t *)bp->b_data;
184 1.1 jdolecek sum = 0;
185 1.1 jdolecek
186 1.1 jdolecek while (n--)
187 1.1 jdolecek sum += ntohl(*lp++);
188 1.1 jdolecek return(sum);
189 1.1 jdolecek }
190 1.1 jdolecek
191 1.1 jdolecek int
192 1.1 jdolecek adoscaseequ(name1, name2, len, inter)
193 1.1 jdolecek const u_char *name1, *name2;
194 1.1 jdolecek int len, inter;
195 1.1 jdolecek {
196 1.3 perry while (len-- > 0)
197 1.3 perry if (CapitalChar(*name1++, inter) !=
198 1.1 jdolecek CapitalChar(*name2++, inter))
199 1.1 jdolecek return 0;
200 1.3 perry
201 1.1 jdolecek return 1;
202 1.1 jdolecek }
203 1.1 jdolecek
204 1.1 jdolecek int
205 1.1 jdolecek adoshash(nam, namlen, nelt, inter)
206 1.1 jdolecek const u_char *nam;
207 1.1 jdolecek int namlen, nelt, inter;
208 1.1 jdolecek {
209 1.1 jdolecek int val;
210 1.1 jdolecek
211 1.1 jdolecek val = namlen;
212 1.1 jdolecek while (namlen--)
213 1.1 jdolecek val = ((val * 13) + CapitalChar(*nam++, inter)) & 0x7ff;
214 1.1 jdolecek return(val % nelt);
215 1.1 jdolecek }
216 1.1 jdolecek
217 1.1 jdolecek #ifdef notyet
218 1.1 jdolecek /*
219 1.1 jdolecek * datestamp is local time, tv is to be UTC
220 1.1 jdolecek */
221 1.1 jdolecek int
222 1.10 dsl dstotv(struct datestamp *dsp, struct timeval *tvp)
223 1.1 jdolecek {
224 1.1 jdolecek }
225 1.1 jdolecek
226 1.1 jdolecek /*
227 1.1 jdolecek * tv is UTC, datestamp is to be local time
228 1.1 jdolecek */
229 1.1 jdolecek int
230 1.10 dsl tvtods(struct timeval *tvp, struct datestamp *dsp)
231 1.1 jdolecek {
232 1.1 jdolecek }
233 1.1 jdolecek #endif
234 1.1 jdolecek
235 1.1 jdolecek #if BYTE_ORDER != BIG_ENDIAN
236 1.1 jdolecek u_int32_t
237 1.10 dsl adoswordn(struct buf *bp, int wn)
238 1.1 jdolecek {
239 1.1 jdolecek /*
240 1.1 jdolecek * ados stored in network (big endian) order
241 1.1 jdolecek */
242 1.1 jdolecek return(ntohl(*((u_int32_t *)bp->b_data + wn)));
243 1.1 jdolecek }
244 1.1 jdolecek #endif
245