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