xref: /netbsd-src/sys/fs/adosfs/adutil.c (revision ea4bd7d40f626ecee69928e4b6639b37899e7050)
1*ea4bd7d4Shannken /*	$NetBSD: adutil.c,v 1.17 2014/08/05 08:50:54 hannken Exp $	*/
28974c3cfSjdolecek 
38974c3cfSjdolecek /*
48974c3cfSjdolecek  * Copyright (c) 1994 Christian E. Hopps
58974c3cfSjdolecek  * Copyright (c) 1996 Matthias Scheler
68974c3cfSjdolecek  * All rights reserved.
78974c3cfSjdolecek  *
88974c3cfSjdolecek  * Redistribution and use in source and binary forms, with or without
98974c3cfSjdolecek  * modification, are permitted provided that the following conditions
108974c3cfSjdolecek  * are met:
118974c3cfSjdolecek  * 1. Redistributions of source code must retain the above copyright
128974c3cfSjdolecek  *    notice, this list of conditions and the following disclaimer.
138974c3cfSjdolecek  * 2. Redistributions in binary form must reproduce the above copyright
148974c3cfSjdolecek  *    notice, this list of conditions and the following disclaimer in the
158974c3cfSjdolecek  *    documentation and/or other materials provided with the distribution.
168974c3cfSjdolecek  * 3. All advertising materials mentioning features or use of this software
178974c3cfSjdolecek  *    must display the following acknowledgement:
188974c3cfSjdolecek  *      This product includes software developed by Christian E. Hopps.
198974c3cfSjdolecek  * 4. The name of the author may not be used to endorse or promote products
208974c3cfSjdolecek  *    derived from this software without specific prior written permission
218974c3cfSjdolecek  *
228974c3cfSjdolecek  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
238974c3cfSjdolecek  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
248974c3cfSjdolecek  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
258974c3cfSjdolecek  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
268974c3cfSjdolecek  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
278974c3cfSjdolecek  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
288974c3cfSjdolecek  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
298974c3cfSjdolecek  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
308974c3cfSjdolecek  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
318974c3cfSjdolecek  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
328974c3cfSjdolecek  */
338974c3cfSjdolecek 
348974c3cfSjdolecek #include <sys/cdefs.h>
35*ea4bd7d4Shannken __KERNEL_RCSID(0, "$NetBSD: adutil.c,v 1.17 2014/08/05 08:50:54 hannken Exp $");
368974c3cfSjdolecek 
378974c3cfSjdolecek #include <sys/param.h>
388974c3cfSjdolecek #include <sys/vnode.h>
398974c3cfSjdolecek #include <sys/mount.h>
408974c3cfSjdolecek #include <sys/proc.h>
418974c3cfSjdolecek #include <sys/systm.h>
428974c3cfSjdolecek #include <sys/time.h>
438974c3cfSjdolecek #include <sys/queue.h>
448974c3cfSjdolecek #include <sys/buf.h>
458974c3cfSjdolecek #include <fs/adosfs/adosfs.h>
468974c3cfSjdolecek 
478974c3cfSjdolecek /*
488974c3cfSjdolecek  * look for anode in the mount's hash table, return locked.
498974c3cfSjdolecek  */
5002cdf4d2Sdsl static int CapitalChar(int, int);
518974c3cfSjdolecek 
528974c3cfSjdolecek int
adosfs_getblktype(struct adosfsmount * amp,struct buf * bp)53454af1c0Sdsl adosfs_getblktype(struct adosfsmount *amp, struct buf *bp)
548974c3cfSjdolecek {
558974c3cfSjdolecek 	if (adoscksum(bp, amp->nwords)) {
568974c3cfSjdolecek #ifdef DIAGNOSTIC
5714757120Slonewolf 		printf("adosfs: aget: cksum of blk %" PRId64 " failed\n",
588974c3cfSjdolecek 		    bp->b_blkno / (amp->bsize / DEV_BSIZE));
598974c3cfSjdolecek #endif
608974c3cfSjdolecek 		return (-1);
618974c3cfSjdolecek 	}
628974c3cfSjdolecek 
638974c3cfSjdolecek 	/*
648974c3cfSjdolecek 	 * check primary block type
658974c3cfSjdolecek 	 */
668974c3cfSjdolecek 	if (adoswordn(bp, 0) != BPT_SHORT) {
678974c3cfSjdolecek #ifdef DIAGNOSTIC
6814757120Slonewolf 		printf("adosfs: aget: bad primary type blk %" PRId64 " (type = %d)\n",
698974c3cfSjdolecek 		    bp->b_blkno / (amp->bsize / DEV_BSIZE), adoswordn(bp,0));
708974c3cfSjdolecek #endif
718974c3cfSjdolecek 		return (-1);
728974c3cfSjdolecek 	}
738974c3cfSjdolecek 
748974c3cfSjdolecek 	/*
758974c3cfSjdolecek 	 * Check secondary block type.
768974c3cfSjdolecek 	 */
778974c3cfSjdolecek 	switch (adoswordn(bp, amp->nwords - 1)) {
788974c3cfSjdolecek 	case BST_RDIR:		/* root block */
798974c3cfSjdolecek 		return (AROOT);
808974c3cfSjdolecek 	case BST_LDIR:		/* hard link to dir */
818974c3cfSjdolecek 		return (ALDIR);
828974c3cfSjdolecek 	case BST_UDIR:		/* user dir */
838974c3cfSjdolecek 		return (ADIR);
848974c3cfSjdolecek 	case BST_LFILE:		/* hard link to file */
858974c3cfSjdolecek 		return (ALFILE);
868974c3cfSjdolecek 	case BST_FILE:		/* file header */
878974c3cfSjdolecek 		return (AFILE);
888974c3cfSjdolecek 	case BST_SLINK:		/* soft link */
898974c3cfSjdolecek 		return (ASLINK);
908974c3cfSjdolecek 	}
918974c3cfSjdolecek 
928974c3cfSjdolecek #ifdef DIAGNOSTIC
9314757120Slonewolf 	printf("adosfs: aget: bad secondary type blk %" PRId64 " (type = %d)\n",
948974c3cfSjdolecek 	    bp->b_blkno / (amp->bsize / DEV_BSIZE), adoswordn(bp, amp->nwords - 1));
958974c3cfSjdolecek #endif
968974c3cfSjdolecek 
978974c3cfSjdolecek 	return (-1);
988974c3cfSjdolecek }
998974c3cfSjdolecek 
1008974c3cfSjdolecek int
adunixprot(int adprot)101454af1c0Sdsl adunixprot(int adprot)
1028974c3cfSjdolecek {
1038974c3cfSjdolecek 	if (adprot & 0xc000ee00) {
1048974c3cfSjdolecek 		adprot = (adprot & 0xee0e) >> 1;
1058974c3cfSjdolecek 		return (((adprot & 0x7) << 6) |
1068974c3cfSjdolecek 			((adprot & 0x700) >> 5) |
1078974c3cfSjdolecek 			((adprot & 0x7000) >> 12));
1088974c3cfSjdolecek 	}
1098974c3cfSjdolecek 	else {
1108974c3cfSjdolecek 		adprot = (adprot >> 1) & 0x7;
1118974c3cfSjdolecek 		return((adprot << 6) | (adprot << 3) | adprot);
1128974c3cfSjdolecek 	}
1138974c3cfSjdolecek }
1148974c3cfSjdolecek 
1158974c3cfSjdolecek static int
CapitalChar(int ch,int inter)11682357f6dSdsl CapitalChar(int ch, int inter)
1178974c3cfSjdolecek {
1188974c3cfSjdolecek 	if ((ch >= 'a' && ch <= 'z') ||
1198974c3cfSjdolecek 	    (inter && ch >= 0xe0 && ch <= 0xfe && ch != 0xf7))
1208974c3cfSjdolecek 		return(ch - ('a' - 'A'));
1218974c3cfSjdolecek 	return(ch);
1228974c3cfSjdolecek }
1238974c3cfSjdolecek 
1248974c3cfSjdolecek u_int32_t
adoscksum(struct buf * bp,int n)125454af1c0Sdsl adoscksum(struct buf *bp, int n)
1268974c3cfSjdolecek {
1278974c3cfSjdolecek 	u_int32_t sum, *lp;
1288974c3cfSjdolecek 
1298974c3cfSjdolecek 	lp = (u_int32_t *)bp->b_data;
1308974c3cfSjdolecek 	sum = 0;
1318974c3cfSjdolecek 
1328974c3cfSjdolecek 	while (n--)
1338974c3cfSjdolecek 		sum += ntohl(*lp++);
1348974c3cfSjdolecek 	return(sum);
1358974c3cfSjdolecek }
1368974c3cfSjdolecek 
1378974c3cfSjdolecek int
adoscaseequ(const u_char * name1,const u_char * name2,int len,int inter)13882357f6dSdsl adoscaseequ(const u_char *name1, const u_char *name2, int len, int inter)
1398974c3cfSjdolecek {
1408974c3cfSjdolecek 	while (len-- > 0)
1418974c3cfSjdolecek 		if (CapitalChar(*name1++, inter) !=
1428974c3cfSjdolecek 		    CapitalChar(*name2++, inter))
1438974c3cfSjdolecek 			return 0;
1448974c3cfSjdolecek 
1458974c3cfSjdolecek 	return 1;
1468974c3cfSjdolecek }
1478974c3cfSjdolecek 
1488974c3cfSjdolecek int
adoshash(const u_char * nam,int namlen,int nelt,int inter)14982357f6dSdsl adoshash(const u_char *nam, int namlen, int nelt, int inter)
1508974c3cfSjdolecek {
1518974c3cfSjdolecek 	int val;
1528974c3cfSjdolecek 
1538974c3cfSjdolecek 	val = namlen;
1548974c3cfSjdolecek 	while (namlen--)
1558974c3cfSjdolecek 		val = ((val * 13) + CapitalChar(*nam++, inter)) & 0x7ff;
1568974c3cfSjdolecek 	return(val % nelt);
1578974c3cfSjdolecek }
1588974c3cfSjdolecek 
1598974c3cfSjdolecek #ifdef notyet
1608974c3cfSjdolecek /*
1618974c3cfSjdolecek  * datestamp is local time, tv is to be UTC
1628974c3cfSjdolecek  */
1638974c3cfSjdolecek int
dstotv(struct datestamp * dsp,struct timeval * tvp)164454af1c0Sdsl dstotv(struct datestamp *dsp, struct timeval *tvp)
1658974c3cfSjdolecek {
1668974c3cfSjdolecek }
1678974c3cfSjdolecek 
1688974c3cfSjdolecek /*
1698974c3cfSjdolecek  * tv is UTC, datestamp is to be local time
1708974c3cfSjdolecek  */
1718974c3cfSjdolecek int
tvtods(struct timeval * tvp,struct datestamp * dsp)172454af1c0Sdsl tvtods(struct timeval *tvp, struct datestamp *dsp)
1738974c3cfSjdolecek {
1748974c3cfSjdolecek }
1758974c3cfSjdolecek #endif
1768974c3cfSjdolecek 
1778974c3cfSjdolecek #if BYTE_ORDER != BIG_ENDIAN
1788974c3cfSjdolecek u_int32_t
adoswordn(struct buf * bp,int wn)179454af1c0Sdsl adoswordn(struct buf *bp, int wn)
1808974c3cfSjdolecek {
1818974c3cfSjdolecek 	/*
1828974c3cfSjdolecek 	 * ados stored in network (big endian) order
1838974c3cfSjdolecek 	 */
1848974c3cfSjdolecek 	return(ntohl(*((u_int32_t *)bp->b_data + wn)));
1858974c3cfSjdolecek }
1868974c3cfSjdolecek #endif
187