xref: /netbsd/sys/fs/adosfs/adutil.c (revision c0ce9b29)
1 /*	$NetBSD: adutil.c,v 1.17 2014/08/05 08:50:54 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.17 2014/08/05 08:50:54 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 static int CapitalChar(int, int);
51 
52 int
adosfs_getblktype(struct adosfsmount * amp,struct buf * bp)53 adosfs_getblktype(struct adosfsmount *amp, struct buf *bp)
54 {
55 	if (adoscksum(bp, amp->nwords)) {
56 #ifdef DIAGNOSTIC
57 		printf("adosfs: aget: cksum of blk %" PRId64 " failed\n",
58 		    bp->b_blkno / (amp->bsize / DEV_BSIZE));
59 #endif
60 		return (-1);
61 	}
62 
63 	/*
64 	 * check primary block type
65 	 */
66 	if (adoswordn(bp, 0) != BPT_SHORT) {
67 #ifdef DIAGNOSTIC
68 		printf("adosfs: aget: bad primary type blk %" PRId64 " (type = %d)\n",
69 		    bp->b_blkno / (amp->bsize / DEV_BSIZE), adoswordn(bp,0));
70 #endif
71 		return (-1);
72 	}
73 
74 	/*
75 	 * Check secondary block type.
76 	 */
77 	switch (adoswordn(bp, amp->nwords - 1)) {
78 	case BST_RDIR:		/* root block */
79 		return (AROOT);
80 	case BST_LDIR:		/* hard link to dir */
81 		return (ALDIR);
82 	case BST_UDIR:		/* user dir */
83 		return (ADIR);
84 	case BST_LFILE:		/* hard link to file */
85 		return (ALFILE);
86 	case BST_FILE:		/* file header */
87 		return (AFILE);
88 	case BST_SLINK:		/* soft link */
89 		return (ASLINK);
90 	}
91 
92 #ifdef DIAGNOSTIC
93 	printf("adosfs: aget: bad secondary type blk %" PRId64 " (type = %d)\n",
94 	    bp->b_blkno / (amp->bsize / DEV_BSIZE), adoswordn(bp, amp->nwords - 1));
95 #endif
96 
97 	return (-1);
98 }
99 
100 int
adunixprot(int adprot)101 adunixprot(int adprot)
102 {
103 	if (adprot & 0xc000ee00) {
104 		adprot = (adprot & 0xee0e) >> 1;
105 		return (((adprot & 0x7) << 6) |
106 			((adprot & 0x700) >> 5) |
107 			((adprot & 0x7000) >> 12));
108 	}
109 	else {
110 		adprot = (adprot >> 1) & 0x7;
111 		return((adprot << 6) | (adprot << 3) | adprot);
112 	}
113 }
114 
115 static int
CapitalChar(int ch,int inter)116 CapitalChar(int ch, int inter)
117 {
118 	if ((ch >= 'a' && ch <= 'z') ||
119 	    (inter && ch >= 0xe0 && ch <= 0xfe && ch != 0xf7))
120 		return(ch - ('a' - 'A'));
121 	return(ch);
122 }
123 
124 u_int32_t
adoscksum(struct buf * bp,int n)125 adoscksum(struct buf *bp, int n)
126 {
127 	u_int32_t sum, *lp;
128 
129 	lp = (u_int32_t *)bp->b_data;
130 	sum = 0;
131 
132 	while (n--)
133 		sum += ntohl(*lp++);
134 	return(sum);
135 }
136 
137 int
adoscaseequ(const u_char * name1,const u_char * name2,int len,int inter)138 adoscaseequ(const u_char *name1, const u_char *name2, int len, int inter)
139 {
140 	while (len-- > 0)
141 		if (CapitalChar(*name1++, inter) !=
142 		    CapitalChar(*name2++, inter))
143 			return 0;
144 
145 	return 1;
146 }
147 
148 int
adoshash(const u_char * nam,int namlen,int nelt,int inter)149 adoshash(const u_char *nam, int namlen, int nelt, int inter)
150 {
151 	int val;
152 
153 	val = namlen;
154 	while (namlen--)
155 		val = ((val * 13) + CapitalChar(*nam++, inter)) & 0x7ff;
156 	return(val % nelt);
157 }
158 
159 #ifdef notyet
160 /*
161  * datestamp is local time, tv is to be UTC
162  */
163 int
dstotv(struct datestamp * dsp,struct timeval * tvp)164 dstotv(struct datestamp *dsp, struct timeval *tvp)
165 {
166 }
167 
168 /*
169  * tv is UTC, datestamp is to be local time
170  */
171 int
tvtods(struct timeval * tvp,struct datestamp * dsp)172 tvtods(struct timeval *tvp, struct datestamp *dsp)
173 {
174 }
175 #endif
176 
177 #if BYTE_ORDER != BIG_ENDIAN
178 u_int32_t
adoswordn(struct buf * bp,int wn)179 adoswordn(struct buf *bp, int wn)
180 {
181 	/*
182 	 * ados stored in network (big endian) order
183 	 */
184 	return(ntohl(*((u_int32_t *)bp->b_data + wn)));
185 }
186 #endif
187