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