diff options
author | semenu <semenu@FreeBSD.org> | 1999-12-09 19:10:13 +0000 |
---|---|---|
committer | semenu <semenu@FreeBSD.org> | 1999-12-09 19:10:13 +0000 |
commit | d230d55c6a76b5a3528898567115e4434c5d5885 (patch) | |
tree | 5d7427b5398caa08bf403312440238a6226bb8d4 /sys/fs/hpfs | |
parent | dd77f5d4fb9ad73450c3c19665c2c0841b74ba62 (diff) | |
download | FreeBSD-src-d230d55c6a76b5a3528898567115e4434c5d5885.zip FreeBSD-src-d230d55c6a76b5a3528898567115e4434c5d5885.tar.gz |
First version of HPFS stuff.
Diffstat (limited to 'sys/fs/hpfs')
-rw-r--r-- | sys/fs/hpfs/hpfs.h | 422 | ||||
-rw-r--r-- | sys/fs/hpfs/hpfs_alsubr.c | 906 | ||||
-rw-r--r-- | sys/fs/hpfs/hpfs_hash.c | 173 | ||||
-rw-r--r-- | sys/fs/hpfs/hpfs_ioctl.h | 43 | ||||
-rw-r--r-- | sys/fs/hpfs/hpfs_lookup.c | 212 | ||||
-rw-r--r-- | sys/fs/hpfs/hpfs_subr.c | 861 | ||||
-rw-r--r-- | sys/fs/hpfs/hpfs_subr.h | 87 | ||||
-rw-r--r-- | sys/fs/hpfs/hpfs_vfsops.c | 770 | ||||
-rw-r--r-- | sys/fs/hpfs/hpfs_vnops.c | 1480 | ||||
-rw-r--r-- | sys/fs/hpfs/hpfsmount.h | 39 |
10 files changed, 4993 insertions, 0 deletions
diff --git a/sys/fs/hpfs/hpfs.h b/sys/fs/hpfs/hpfs.h new file mode 100644 index 0000000..7c880b2 --- /dev/null +++ b/sys/fs/hpfs/hpfs.h @@ -0,0 +1,422 @@ +/*- + * Copyright (c) 1998, 1999 Semen Ustimenko (semenu@FreeBSD.org) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +/*#define HPFS_DEBUG 10*/ +typedef u_int32_t lsn_t; /* Logical Sector Number */ +typedef struct { + lsn_t lsn1; + lsn_t lsn2; +} rsp_t; /* Redundant Sector Pointer */ +typedef struct { + u_int32_t cnt; + lsn_t lsn; +} sptr_t; /* Storage Pointer */ + +#define SUBLOCK 0x10 +#define SUSIZE DEV_BSIZE +#define SPBLOCK 0x11 +#define SPSIZE DEV_BSIZE +#define BMSIZE (4 * DEV_BSIZE) +#define HPFS_MAXFILENAME 255 + +#define SU_MAGIC ((u_int64_t)0xFA53E9C5F995E849) +struct sublock { + u_int64_t su_magic; + u_int8_t su_hpfsver; + u_int8_t su_fnctver; + u_int16_t unused; + lsn_t su_rootfno; /* Root Fnode */ + u_int32_t su_btotal; /* Total blocks */ + u_int32_t su_badbtotal; /* Bad Sectors total */ + rsp_t su_bitmap; + rsp_t su_badbl; + u_long su_chkdskdate; + u_long su_dskoptdate; + u_int32_t su_dbbsz; /* Sectors in DirBlock Band */ + lsn_t su_dbbstart; + lsn_t su_dbbend; + lsn_t su_dbbbitmap; + char su_volname[0x20]; + lsn_t su_uidt; /* Ptr to User ID Table (8 sect) */ +}; + +#define SP_MAGIC ((u_int64_t)0xFA5229C5F9911849) +#define SP_DIRTY 0x0001 +#define SP_SPDBINUSE 0x0002 +#define SP_HFINUSE 0x0004 +#define SP_BADSECT 0x0008 +#define SP_BADBMBL 0x0010 +#define SP_FASTFRMT 0x0020 +#define SP_OLDHPFS 0x0080 +#define SP_IDASD 0x0100 +#define SP_RDASD 0x0200 +#define SP_DASD 0x0400 +#define SP_MMACTIVE 0x0800 +#define SP_DCEACLS 0x1000 +#define SP_DSADDIRTY 0x2000 +struct spblock { + u_int64_t sp_magic; + u_int16_t sp_flag; + u_int8_t sp_mmcontf; + u_int8_t unused; + lsn_t sp_hf; /* HotFix list */ + u_int32_t sp_hfinuse; /* HotFixes in use */ + u_int32_t sp_hfavail; /* HotFixes available */ + u_int32_t sp_spdbavail; /* Spare DirBlocks available */ + u_int32_t sp_spdbmax; /* Spare DirBlocks maximum */ + lsn_t sp_cpi; + u_int32_t sp_cpinum; + u_int32_t sp_suchecksum; + u_int32_t sp_spchecksum; + u_int8_t reserved[0x3C]; + lsn_t sp_spdb[0x65]; +}; + +#define DE_SPECIAL 0x0001 +#define DE_ACL 0x0002 +#define DE_DOWN 0x0004 +#define DE_END 0x0008 +#define DE_EALIST 0x0010 +#define DE_EPERM 0x0020 +#define DE_EXPLACL 0x0040 +#define DE_NEEDEA 0x0080 +#define DE_RONLY 0x0100 +#define DE_HIDDEN 0x0200 +#define DE_SYSTEM 0x0400 +#define DE_VOLLABEL 0x0800 +#define DE_DIR 0x1000 +#define DE_ARCHIV 0x2000 +#define DE_DOWNLSN(dep) (*(lsn_t *)((caddr_t)(dep) + (dep)->de_reclen - sizeof(lsn_t))) +#define DE_NEXTDE(dep) ((struct hpfsdirent *)((caddr_t)(dep) + (dep)->de_reclen)) +typedef struct hpfsdirent { + u_int16_t de_reclen; + u_int16_t de_flag; + lsn_t de_fnode; + u_long de_mtime; + u_int32_t de_size; + u_long de_atime; + u_long de_ctime; + u_int32_t de_ealen; + u_int8_t de_flexflag; + u_int8_t de_cpid; + u_int8_t de_namelen; + char de_name[1]; +/* ... de_flex; */ +/* lsn_t de_down; */ +} hpfsdirent_t; + +#define D_BSIZE (DEV_BSIZE*4) +#define D_MAGIC 0x77E40AAE +#define D_DIRENT(dbp) ((hpfsdirent_t *)((caddr_t)dbp + sizeof(dirblk_t))) +#define D_DE(dbp, deoff) ((hpfsdirent_t *)((caddr_t)dbp + sizeof(dirblk_t) + (deoff))) +typedef struct dirblk { + u_int32_t d_magic; + u_int32_t d_freeoff; /* Offset of first free byte */ + u_int32_t d_chcnt; /* Change count */ + lsn_t d_parent; + lsn_t d_self; +} dirblk_t; + +/* + * Allocation Block (ALBLK) + */ +#define AB_HBOFFEO 0x01 +#define AB_FNPARENT 0x20 +#define AB_SUGGBSCH 0x40 +#define AB_NODES 0x80 +#define AB_ALLEAF(abp) ((alleaf_t *)((caddr_t)(abp) + sizeof(alblk_t))) +#define AB_ALNODE(abp) ((alnode_t *)((caddr_t)(abp) + sizeof(alblk_t))) +#define AB_FREEALP(abp) ((alleaf_t *)((caddr_t)(abp) + (abp)->ab_freeoff)) +#define AB_FREEANP(abp) ((alnode_t *)((caddr_t)(abp) + (abp)->ab_freeoff)) +#define AB_LASTALP(abp) (AB_ALLEAF(abp) + (abp)->ab_busycnt - 1) +#define AB_LASTANP(abp) (AB_ALNODE(abp) + (abp)->ab_busycnt - 1) +#define AB_ADDNREC(abp, sz, n) { \ + (abp)->ab_busycnt += (n); \ + (abp)->ab_freecnt -= (n); \ + (abp)->ab_freeoff += (n) * (sz); \ +} +#define AB_RMNREC(abp, sz, n) { \ + (abp)->ab_busycnt -= (n); \ + (abp)->ab_freecnt += (n); \ + (abp)->ab_freeoff -= (n) * (sz);\ +} +#define AB_ADDAL(abp) AB_ADDNREC(abp,sizeof(alleaf_t), 1) +#define AB_ADDAN(abp) AB_ADDNREC(abp,sizeof(alnode_t), 1) +#define AB_RMAL(abp) AB_RMNREC(abp,sizeof(alleaf_t), 1) +#define AB_RMAN(abp) AB_RMNREC(abp,sizeof(alnode_t), 1) +typedef struct alblk { + u_int8_t ab_flag; + u_int8_t ab_res[3]; + u_int8_t ab_freecnt; + u_int8_t ab_busycnt; + u_int16_t ab_freeoff; +} alblk_t; + +/* + * FNode + */ +#define FNODESIZE DEV_BSIZE +#define FN_MAGIC 0xF7E40AAE +struct fnode { + u_int32_t fn_magic; + u_int64_t fn_readhist; + u_int8_t fn_namelen; + char fn_name[0xF]; /* First 15 symbols or less */ + lsn_t fn_parent; + sptr_t fn_extacl; + u_int16_t fn_acllen; + u_int8_t fn_extaclflag; + u_int8_t fn_histbitcount; + sptr_t fn_extea; + u_int16_t fn_ealen; /* Len of EAs in Fnode */ + u_int8_t fn_exteaflag; /* EAs in exteas */ + u_int8_t fn_flag; + alblk_t fn_ab; + u_int8_t fn_abd[0x60]; + u_int32_t fn_size; + u_int32_t fn_reqea; + u_int8_t fn_uid[0x10]; + u_int16_t fn_intoff; + u_int8_t fn_1dasdthr; + u_int8_t fn_dasdthr; + u_int32_t fn_dasdlim; + u_int32_t fn_dasdusage; + u_int8_t fn_int[0x13c]; +}; + +#define EA_NAME(eap) ((char *)(((caddr_t)(eap)) + sizeof(struct ea))) +struct ea { + u_int8_t ea_type; /* 0 - plain val */ + /* 1 - sptr to val */ + /* 3 - lsn point to AlSec, cont. val */ + u_int8_t ea_namelen; + u_int16_t ea_vallen; + /*u_int8_t ea_name[]; */ + /*u_int8_t ea_val[]; */ +}; + +/* + * Allocation Block Data (ALNODE) + * + * NOTE: AlNodes are used when there are too many fragments + * to represent the data in the AlBlk + */ +#define AN_SET(anp,nextoff,lsn) { \ + (anp)->an_nextoff = (nextoff); \ + (anp)->an_lsn = (lsn); \ +} +typedef struct alnode { + u_int32_t an_nextoff; /* next node offset in blocks */ + lsn_t an_lsn; /* position of AlSec structure */ +} alnode_t; + +/* + * Allocaion Block Data (ALLEAF) + * + * NOTE: Leaves are used to point at contiguous block of data + * (a fragment or an "extent"); + */ +#define AL_SET(alp,off,len,lsn) { \ + (alp)->al_off = (off); \ + (alp)->al_len = (len); \ + (alp)->al_lsn = (lsn); \ +} +typedef struct alleaf { + u_int32_t al_off; /* offset in blocks */ + u_int32_t al_len; /* len in blocks */ + lsn_t al_lsn; /* phys position */ +} alleaf_t; + +/* + * Allocation Sector + * + * NOTE: AlSecs are not initialized before use, so they ussually + * look full of junk. Use the AlBlk tto validate the data. + */ +#define AS_MAGIC 0x37E40AAE +typedef struct alsec { + u_int32_t as_magic; + lsn_t as_self; + lsn_t as_parent; + alblk_t as_ab; + u_int8_t as_abd[0x1E0]; +} alsec_t; + +/* + * Code Page structures + */ +struct cpdblk { + u_int16_t b_country; /* Country code */ + u_int16_t b_cpid; /* CP ID */ + u_int16_t b_dbcscnt; /* Count of DBCS ranges in CP */ + char b_upcase[0x80]; /* Case conversion table */ + u_int16_t b_dbcsrange; /* Start/End DBCS range pairs */ + +}; + +#define CPD_MAGIC ((u_int32_t)0x894521F7) +struct cpdsec { + u_int32_t d_magic; + u_int16_t d_cpcnt; /* CP Data count */ + u_int16_t d_cpfirst; /* Index of first CP Data */ + u_int32_t d_checksum[3]; /* CP Data checksumms */ + u_int16_t d_offset[3]; /* Offsets of CP Data blocks */ + struct cpdblk d_cpdblk[3]; /* Array of CP Data Blocks */ +}; + +struct cpiblk { + u_int16_t b_country; /* Country code */ + u_int16_t b_cpid; /* CP ID */ + u_int32_t b_checksum; + lsn_t b_cpdsec; /* Pointer to CP Data Sector */ + u_int16_t b_vcpid; /* Volume spec. CP ID */ + u_int16_t b_dbcscnt; /* Count of DBCS ranges in CP */ +}; + +#define CPI_MAGIC ((u_int32_t)0x494521F7) +struct cpisec { + u_int32_t s_magic; + u_int32_t s_cpicnt; /* Count of CPI's in this sector */ + u_int32_t s_cpifirst; /* Index of first CPI in this sector */ + lsn_t s_next; /* Pointer to next CPI Sector */ + struct cpiblk s_cpi[0x1F]; /* Array of CPI blocks */ +}; + +struct hpfsmount { + struct sublock hpm_su; + struct spblock hpm_sp; + struct netexport hpm_export; + struct mount * hpm_mp; + struct vnode * hpm_devvp; + dev_t hpm_dev; + uid_t hpm_uid; + gid_t hpm_gid; + mode_t hpm_mode; + + lsn_t * hpm_bmind; + struct cpdblk * hpm_cpdblk; /* Array of CP Data Blocks */ + u_char hpm_u2d[0x80]; /* Unix to DOS Table*/ + u_char hpm_d2u[0x80]; /* DOS to Unix Table*/ + + u_long hpm_bavail; /* Blocks available */ + u_long hpm_dbnum; /* Data Band number */ + u_int8_t * hpm_bitmap; +}; + +#define H_HASHED 0x0001 /* Present in hash */ +#define H_PARVALID 0x0002 /* parent info is valid */ +#define H_CHANGE 0x0004 /* node date was changed */ +#define H_PARCHANGE 0x0008 /* parent node date was changed */ +#define H_INVAL 0x0010 /* Invalid node */ +struct hpfsnode { + struct lock h_lock; /* Must be first, for std vops */ +#ifndef NULL_SIMPLELOCKS + struct simplelock h_interlock; +#endif + + LIST_ENTRY(hpfsnode) h_hash; + + struct hpfsmount *h_hpmp; + struct fnode h_fn; + struct vnode * h_vp; + struct vnode * h_devvp; + dev_t h_dev; + lsn_t h_no; + uid_t h_uid; + gid_t h_gid; + mode_t h_mode; + u_int32_t h_flag; + + /* parent dir information */ + u_long h_mtime; + u_long h_atime; + u_long h_ctime; + char h_name[HPFS_MAXFILENAME+1]; /* Used to speedup dirent */ + int h_namelen; /* lookup */ +}; + +/* This overlays the fid structure (see <sys/mount.h>) */ +struct hpfid { + u_int16_t hpfid_len; /* Length of structure. */ + u_int16_t hpfid_pad; /* Force 32-bit alignment. */ + lsn_t hpfid_ino; /* File number (ino). */ + int32_t hpfid_gen; /* Generation number. */ +}; + +#if defined(HPFS_DEBUG) +#define dprintf(a) printf a +#if HPFS_DEBUG > 1 +#define ddprintf(a) printf a +#else +#define ddprintf(a) +#endif +#else +#define dprintf(a) +#define ddprintf(a) +#endif + +#if __FreeBSD_version >= 300000 +MALLOC_DECLARE(M_HPFSMNT); +MALLOC_DECLARE(M_HPFSNO); +#endif +#define VFSTOHPFS(mp) ((struct hpfsmount *)((mp)->mnt_data)) +#define VTOHP(v) ((struct hpfsnode *)((v)->v_data)) +#define HPTOV(h) ((struct vnode *)((h)->h_vp)) +#define FID(f) (*((lsn_t *)(f)->fid_data)) + +#if defined(__NetBSD__) +#define MALLOC_DEFINE(a, b, c) +#define M_HPFSMNT M_TEMP +#define M_HPFSNO M_TEMP +typedef int (vop_t) __P((void *)); +#define HASHINIT(a, b, c, d) hashinit((a), (b), (c), (d)) +#define bqrelse(bp) brelse(bp) +#define VOP__LOCK(a, b, c) VOP_LOCK((a), (b) ? LK_EXCLUSIVE : LK_SHARED) +#define VOP__UNLOCK(a, b, c) VOP_UNLOCK((a), 0) +#define VGET(a, b, c) vget((a), LK_EXCLUSIVE) +#define VN_LOCK(a, b, c) vn_lock((a), LK_EXCLUSIVE) +#define LOCKMGR(a, b, c, d) lockmgr((a), (b), (c)) +#else /* defined(__FreeBSD__) */ +#define HASHINIT(a, b, c, d) hashinit((a), (b), (d)) +#define VOP__LOCK(a, b, c) VOP_LOCK((a), (b), (c)) +#define VOP__UNLOCK(a, b, c) VOP_UNLOCK((a), (b), (c)) +#define VGET(a, b, c) vget((a), (b), (c)) +#define VN_LOCK(a, b, c) vn_lock((a), (b), (c)) +#define LOCKMGR(a, b, c, d) lockmgr((a), (b), (c), (d)) +#endif + +extern vop_t ** hpfs_vnodeop_p; + +/* Hash routines, too small to be separate header */ +void hpfs_hphashinit __P((void)); +struct hpfsnode *hpfs_hphashlookup __P((dev_t, lsn_t)); +struct hpfsnode *hpfs_hphashget __P((dev_t, lsn_t)); +struct vnode *hpfs_hphashvget __P((dev_t, lsn_t, struct proc *)); +void hpfs_hphashins __P((register struct hpfsnode *)); +void hpfs_hphashrem __P((register struct hpfsnode *)); +extern struct lock hpfs_hphash_lock; diff --git a/sys/fs/hpfs/hpfs_alsubr.c b/sys/fs/hpfs/hpfs_alsubr.c new file mode 100644 index 0000000..52ca645 --- /dev/null +++ b/sys/fs/hpfs/hpfs_alsubr.c @@ -0,0 +1,906 @@ +/*- + * Copyright (c) 1998, 1999 Semen Ustimenko (semenu@FreeBSD.org) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/kernel.h> +#include <sys/proc.h> +#include <sys/time.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/vnode.h> +#include <sys/mount.h> +#include <sys/namei.h> +#include <sys/malloc.h> +#include <sys/buf.h> + +#include <fs/hpfs/hpfs.h> +#include <fs/hpfs/hpfs_subr.h> + +#define AE_DONE 0 /* Nothing to change */ +#define AE_SPLIT 2 /* Split was done, ranp is valid */ + +int hpfs_addextentr (struct hpfsmount *, lsn_t, alleaf_t *, + alnode_t *, u_long *); +int hpfs_allocalsec (struct hpfsmount *, lsn_t, struct buf **); +int hpfs_alblk2alsec (struct hpfsmount *, alblk_t *, alsec_t **, + struct buf **); +int hpfs_splitalsec (struct hpfsmount *, alsec_t *, alsec_t **, + struct buf **); +int hpfs_concatalsec (struct hpfsmount *, alsec_t *, alsec_t *, + alnode_t *); + +/* + * Map file offset to disk offset. hpfsnode have to be locked. + */ +int +hpfs_hpbmap(hp, bn, bnp, runp) + struct hpfsnode *hp; + daddr_t bn; + daddr_t *bnp; + int *runp; +{ + struct buf *bp; + alblk_t * abp; + alleaf_t *alp; + alnode_t *anp; + int error, i; + + dprintf(("hpfs_hpbmap(0x%x, 0x%x): ",hp->h_no, bn)); + + bp = NULL; + abp = &hp->h_fn.fn_ab; + alp = (alleaf_t *)&hp->h_fn.fn_abd; + anp = (alnode_t *)&hp->h_fn.fn_abd; + +dive: + if (abp->ab_flag & AB_NODES) { + for (i=0; i<abp->ab_busycnt; i++, anp++) { + dprintf(("[0x%x,0x%x] ",anp->an_nextoff,anp->an_lsn)); + if (bn < anp->an_nextoff) { + alsec_t *asp; + + dprintf(("< found | ")); + + if (bp) + brelse(bp); + error = bread(hp->h_devvp, anp->an_lsn, + DEV_BSIZE, NOCRED, &bp); + if (error) { + printf("hpfs_hpbmap: bread error\n"); + brelse(bp); + return (error); + } + + asp = (alsec_t *) bp->b_data; + if (asp->as_magic != AS_MAGIC) { + brelse(bp); + printf("hpfs_hpbmap: " + "MAGIC DOESN'T MATCH"); + return (EINVAL); + } + + abp = &asp->as_ab; + alp = (alleaf_t *)&asp->as_abd; + anp = (alnode_t *)&asp->as_abd; + + goto dive; + } + } + } else { + for (i=0; i<abp->ab_busycnt; i++, alp++) { + dprintf(("[0x%x,0x%x,0x%x] ", + alp->al_off,alp->al_len,alp->al_lsn)); + + if ((bn >= alp->al_off) && + (!alp->al_len || (bn < alp->al_off + alp->al_len))) { + dprintf(("found, ")); + + *bnp = bn - alp->al_off + alp->al_lsn; + + dprintf((" 0x%x ", *bnp)); + + if (runp != NULL) { + if (alp->al_len) + *runp = alp->al_off - 1 + + alp->al_len - bn; + else + *runp = 3; /* XXX */ + + dprintf((" 0x%x cont", *runp)); + } + + if (bp) + brelse(bp); + + dprintf(("\n")); + return (0); + } + } + } + + dprintf(("END, notfound\n")); + if (bp) + brelse(bp); + + dprintf(("hpfs_hpbmap: offset too big\n")); + + return (EFBIG); +} + +/* + * Find place and preinitialize AlSec structure + * AlBlk is initialized to contain AlLeafs. + */ +int +hpfs_allocalsec ( + struct hpfsmount *hpmp, + lsn_t parlsn, + struct buf **bpp) +{ + alsec_t * asp; + struct buf * bp; + lsn_t lsn; + int error; + + *bpp = NULL; + + error = hpfs_bmfblookup(hpmp, &lsn); + if (error) { + printf("hpfs_allocalsec: CAN'T ALLOC SPACE FOR AlSec\n"); + return (error); + } + + error = hpfs_bmmarkbusy(hpmp, lsn, 1); + if (error) + return (error); + + bp = getblk(hpmp->hpm_devvp, lsn, DEV_BSIZE, 0, 0); + clrbuf(bp); + + /* Fill AlSec info */ + asp = (alsec_t *) bp->b_data; + asp->as_magic = AS_MAGIC; + asp->as_self = lsn; + asp->as_parent = parlsn; + + /* Fill AlBlk */ + asp->as_ab.ab_flag = 0; + asp->as_ab.ab_busycnt = 0; + asp->as_ab.ab_freecnt = 0x28; + asp->as_ab.ab_freeoff = sizeof(alblk_t); + + *bpp = bp; + + return (0); +} + +/* + * Split AlSec structure into new allocated: + * allocate new AlSec; then move second half of asp's entries in + * into it; set proper flags. + * + * IF AlSec CONTAINS AlNodes, THEN YOU ALMOST EVERYTIME HAVE TO + * FIX LAST AlNode in OLD AlSec (NEXTOFF TO BE 0xFFFFFFFF). + * TOGETHER WITH FIXING ALL CHILDREN'S AlSecs (THEY HAVE GOT NEW PARENT). + */ +int +hpfs_splitalsec ( + struct hpfsmount *hpmp, + alsec_t *asp, + alsec_t **naspp, + struct buf **nbpp) +{ + alsec_t *nasp; + struct buf *nbp; + alblk_t *abp; + alblk_t *nabp; + int error, n1, n2, sz; + + error = hpfs_allocalsec(hpmp, asp->as_parent, &nbp); + if (error) + return (error); + + nasp = (alsec_t *)nbp->b_data; + nabp = &nasp->as_ab; + abp = &asp->as_ab; + + n1 = (abp->ab_busycnt + 1) / 2; + n2 = (abp->ab_busycnt - n1); + sz = (abp->ab_flag & AB_NODES) ? sizeof(alnode_t) : sizeof(alleaf_t); + + bcopy((caddr_t)abp + sizeof(alblk_t) + n1 * sz, + (caddr_t)nabp + sizeof(alblk_t), n2 * sz); + + nabp->ab_flag = abp->ab_flag; + nabp->ab_busycnt = n2; + nabp->ab_freecnt = (0x1e0 / sz - n2); + nabp->ab_freeoff += n2 * sz; + + abp->ab_busycnt -= n1; + abp->ab_freecnt += n1; + abp->ab_freeoff -= n1 * sz; + + *naspp = nasp; + *nbpp = nbp; + + return (0); +} + +/* + * Try to concatenate two AlSec's + * + * Moves all entries from AlSec corresponding (as1p, aanp[1]) into + * corresponding aanp[0] one. If not enought space, then return ENOSPC. + * + * WARNING! YOU HAVE TO FIX aanp VALUES YOURSELF LATER: + * aanp[0].an_nextoff = aanp[1].an_nextoff; + */ +int +hpfs_concatalsec ( + struct hpfsmount *hpmp, + alsec_t *as0p, + alsec_t *as1p, + alnode_t *aanp) +{ + alblk_t *ab0p; + alblk_t *ab1p; + int sz; + + dprintf(("hpfs_concatalsec: AlSecs at 0x%x and 0x%x \n", + as0p->as_self,as1p->as_self)); + + ab0p = &as0p->as_ab; + ab1p = &as1p->as_ab; + sz = (ab0p->ab_flag & AB_NODES) ? sizeof(alnode_t) : sizeof(alleaf_t); + + if (ab0p->ab_freecnt > ab1p->ab_busycnt) { + /* + * Concatenate AlSecs + */ + if (ab0p->ab_flag & AB_NODES) + AB_LASTANP(ab0p)->an_nextoff = aanp[0].an_nextoff; + + bcopy (AB_ALNODE(ab1p), AB_FREEANP(ab0p), + ab1p->ab_busycnt * sz); + + AB_ADDNREC(ab0p, sz, ab1p->ab_busycnt); + + return (0); + } else { + /* Not enought space to concatenate */ + return (ENOSPC); + } +} + +/* + * Transform AlBlk structure into new allocated + * AlSec. + * + * DOESN'T SET AlSec'S PARENT LSN. + */ +int +hpfs_alblk2alsec ( + struct hpfsmount *hpmp, + alblk_t *abp, + alsec_t **naspp, + struct buf **nbpp) +{ + alsec_t *nasp; + alblk_t *nabp; + struct buf *nbp; + int error, sz; + + error = hpfs_allocalsec(hpmp, 0, &nbp); + if (error) + return (error); + + nasp = (alsec_t *)nbp->b_data; + nabp = &nasp->as_ab; + + sz = (abp->ab_flag & AB_NODES) ? sizeof(alnode_t) : sizeof(alleaf_t); + + bcopy (abp, nabp, sizeof(alblk_t) + sz * abp->ab_busycnt); + + nabp->ab_freecnt = 0x1e0 / sz - nabp->ab_busycnt; + + *naspp = nasp; + *nbpp = nbp; + + return (0); +} + +/* + * Allocate len blocks and concatenate them to file. + * If we hadn't found contignous run of len blocks, concatenate + * as much as we can, and return. + * + */ +int +hpfs_addextent ( + struct hpfsmount *hpmp, + struct hpfsnode *hp, + u_long len) +{ + alblk_t *rabp; + alnode_t ranp[2]; + alleaf_t al; + int error; + u_long pf; + + /* + * We don't know for now start lsn of block + */ + al.al_lsn = ~0; + al.al_len = len; + al.al_off = (hp->h_fn.fn_size + DEV_BSIZE - 1) >> DEV_BSHIFT; + + rabp = &hp->h_fn.fn_ab; + + /* Init AlBlk if this is first extent */ + if (al.al_off == 0) { + lsn_t nlsn; + u_long nlen; + + dprintf(("hpfs_addextent: init AlBlk in root\n")); + + rabp->ab_busycnt = 0; + rabp->ab_freecnt = 0x8; + rabp->ab_freeoff = sizeof(alblk_t); + rabp->ab_flag = 0; + + error = hpfs_bmlookup (hpmp, 0, hp->h_no + 1, al.al_len, &nlsn, &nlen); + if (error) + return (error); + + error = hpfs_bmmarkbusy(hpmp, nlsn, nlen); + if (error) + return (error); + + dprintf(("hpfs_addextent: new: 0x%x 0x%lx, ", nlsn, nlen)); + + AL_SET(AB_FREEALP(rabp), al.al_off, nlen, nlsn); + AB_ADDAL(rabp); + + al.al_off += nlen; + al.al_len -= nlen; + } + +retry: + dprintf(("hpfs_addextent: AlBlk: [0x%x, 0x%x, 0x%x] need: 0x%x\n", + rabp->ab_freecnt, rabp->ab_busycnt, rabp->ab_flag, al.al_len)); + + while ((al.al_len) && (rabp->ab_freecnt > 0)) { + if (rabp->ab_flag & AB_NODES) { + alnode_t *anp; + /* + * This is level containing AlNodes, so try to + * insert recursively into last entry. + */ + anp = AB_LASTANP(rabp); + dprintf(("hpfs_addextent: AlNode: [0x%x,0x%x] \n", + anp->an_nextoff,anp->an_lsn)); + + /* + * Try to insert... + */ + error = hpfs_addextentr (hpmp, anp->an_lsn, &al, ranp, &pf); + if (error) { + printf("hpfs_addextent: FAILED %d\n",error); + return (error); + } + + switch (pf) { + case AE_SPLIT: + dprintf(("hpfs_addextent: successful (split)\n")); + /* + * Then hpfs_addextentr has split tree below, now + * we need to fix this level. Particulary: + * fix last AlNode and add another one. + */ + + bcopy(ranp, AB_LASTANP(rabp), sizeof(alnode_t) * 2); + AB_ADDAN(rabp); + break; + + default: + case AE_DONE: + dprintf(("hpfs_addextent: successful\n")); + break; + } + } else { + alleaf_t *alp; + + alp = AB_LASTALP(rabp); + dprintf(("hpfs_addextent: AlLeaf: [0x%x,0x%x,0x%x] \n", + alp->al_off,alp->al_len,alp->al_lsn)); + + /* Check if we trying to add in right place */ + if (alp->al_off + alp->al_len == al.al_off) { + lsn_t nlsn; + u_long nlen; + + /* + * Search bitmap for block begining from + * alp->al_lsn + alp->al_len and long of ralp->al_len + */ + error = hpfs_bmlookup (hpmp, 0, + alp->al_lsn + alp->al_len, al.al_len, &nlsn, &nlen); + if (error) + return (error); + + error = hpfs_bmmarkbusy(hpmp, nlsn, nlen); + if (error) + return (error); + + dprintf(("hpfs_addextent: new: 0x%x 0x%lx, ", nlsn, nlen)); + + if (alp->al_lsn + alp->al_len == nlsn) { + dprintf(("extended existed leaf\n")); + + alp->al_len += nlen; + } else { + dprintf(("created new leaf\n")); + AL_SET(AB_FREEALP(rabp), al.al_off, nlen, nlsn); + AB_ADDAL(rabp); + } + al.al_off += nlen; + al.al_len -= nlen; + } else { + printf("hpfs_addextent: INTERNAL INCONSISTENCE\n"); + return (EINVAL); + } + } + } + + /* + * Move AlBlk contain to new AlSec (it will fit more + * entries) if overflowed (no more free entries). + */ + if (rabp->ab_freecnt <= 0) { + struct buf *nbp; + alsec_t * nrasp; + + dprintf(("hpfs_addextent: overflow, convt\n")); + + /* + * Convert AlBlk to new AlSec, it will set + * AB_FNPARENT also. + */ + rabp->ab_flag |= AB_FNPARENT; + error = hpfs_alblk2alsec (hpmp, rabp, &nrasp, &nbp); + if (error) { + printf("hpfs_addextent: CAN'T CONVT\n"); + return (error); + } + nrasp->as_parent = hp->h_no; + + /* + * Scan all childrens (if exist), set new parent and + * clean their AB_FNPARENT flag. + */ + if (rabp->ab_flag & AB_NODES) { + int i; + alsec_t * asp; + alnode_t * anp; + struct buf * bp; + + anp = AB_ALNODE(rabp); + for (i=0; i<rabp->ab_busycnt; i++) { + error = hpfs_breadalsec(hpmp, anp->an_lsn, &bp); + if (error) + return (error); + + asp = (alsec_t *)bp->b_data; + asp->as_ab.ab_flag &= ~AB_FNPARENT; + asp->as_parent = nrasp->as_self; + + bdwrite(bp); + anp ++; + } + } + + /* Convert AlBlk to contain AlNodes */ + rabp->ab_flag = AB_NODES; + rabp->ab_busycnt = 0; + rabp->ab_freecnt = 0xC; + rabp->ab_freeoff = sizeof(alblk_t); + + /* Add AlNode for new allocated AlSec */ + AN_SET(AB_FREEANP(rabp), ~0, nrasp->as_self); + AB_ADDAN(rabp); + + bdwrite(nbp); + } + + if (al.al_len) { + dprintf(("hpfs_addextent: root retry\n")); + goto retry; + } + + return (0); +} + +/* + * Descent down to the end of tree, then search for + * ralp->len contignous run begining from last run's end and + * concatenate new block! If we can't find one, then... + */ +int +hpfs_addextentr ( + struct hpfsmount *hpmp, /* Mix info */ + lsn_t rlsn, /* LSN containing AlSec */ + alleaf_t *ralp, /* AlLeaf to insert */ + alnode_t *ranp, /* New AlNodes' values */ + u_long *resp) /* Mix returning info */ +{ + struct buf *rbp; + alsec_t *rasp; + alblk_t *rabp; + alleaf_t *alp; + alnode_t *anp; + int error; + u_long pf; + u_long wb; + + *resp = 0; + + dprintf(("hpfs_addextentr: AlSec at 0x%x\n", rlsn)); + + error = hpfs_breadalsec(hpmp, rlsn, &rbp); + if (error) + return (error); + + rasp = (alsec_t *)rbp->b_data; + rabp = &rasp->as_ab; + wb = 0; + + dprintf(("hpfs_addextentr: AlBlk: [0x%x, 0x%x, 0x%x]\n", + rabp->ab_freecnt, rabp->ab_busycnt, rabp->ab_flag)); + + while ((ralp->al_len) && (rabp->ab_freecnt > 0)) { + if (rabp->ab_flag & AB_NODES) { + /* + * This is level containing AlNodes, so try to + * insert recursively into last entry. + */ + anp = AB_LASTANP(rabp); + dprintf(("hpfs_addextentr: AlNode: [0x%x,0x%x] \n", + anp->an_nextoff,anp->an_lsn)); + + /* + * Try to insert... + */ + error = hpfs_addextentr (hpmp, anp->an_lsn, ralp, ranp, &pf); + if (error) { + printf("hpfs_addextentr: FAILED %d\n",error); + goto fail; + } + + switch (pf) { + case AE_SPLIT: + dprintf(("hpfs_addextentr: successful (split)\n")); + /* + * Then hpfs_addextentr has split tree below, now + * we need to fix this level. Particulary: + * fix last AlNode and add another one. + */ + bcopy(ranp, AB_LASTANP(rabp), sizeof(alnode_t) * 2); + AB_ADDAN(rabp); + wb = 1; + break; + + default: + case AE_DONE: + dprintf(("hpfs_addextentr: successful\n")); + break; + } + } else { + alp = AB_LASTALP(rabp); + dprintf(("hpfs_addextentr: AlLeaf: [0x%x,0x%x,0x%x] \n", + alp->al_off,alp->al_len,alp->al_lsn)); + + /* Check if we trying to add in right place */ + if (alp->al_off + alp->al_len == ralp->al_off) { + lsn_t nlsn; + u_long nlen; + /* + * Search bitmap for block begining from + * alp->al_lsn + alp->al_len and long of ralp->al_len + */ + error = hpfs_bmlookup (hpmp, 0, + alp->al_lsn + alp->al_len, ralp->al_len, &nlsn, &nlen); + if (error) + goto fail; + + error = hpfs_bmmarkbusy(hpmp, nlsn, nlen); + if (error) + goto fail; + + dprintf(("hpfs_addextentr: new: 0x%x 0x%lx, ", nlsn, nlen)); + + /* + * If ending of existed entry fits the + * begining of the extent being added, + * then we add concatenate two extents. + */ + if (alp->al_lsn + alp->al_len == nlsn) { + dprintf(("concat\n")); + alp->al_len += nlen; + } else { + dprintf(("created new leaf\n")); + AL_SET(AB_FREEALP(rabp), ralp->al_off, nlen, nlsn); + AB_ADDAL(rabp); + } + + ralp->al_len -= nlen; + ralp->al_off += nlen; + } else { + printf("hpfs_addextentr: INTERNAL INCONSISTENCE\n"); + error = (EINVAL); + goto fail; + } + } + } + + /* + * Split AlBlk if overflowed. + */ + if (rabp->ab_freecnt <= 0) { + struct buf *nbp; + alsec_t * nrasp; + + dprintf(("hpfs_addextentr: overflow, split\n")); + + error = hpfs_splitalsec (hpmp, rasp, &nrasp, &nbp); + if (error) { + printf("hpfs_addextent: CAN'T SPLIT\n"); + goto fail; + } + + if (rabp->ab_flag & AB_NODES) { + int i; + alsec_t * asp; + alnode_t * anp; + struct buf * bp; + + ranp[0].an_nextoff = + AB_LASTANP(&rasp->as_ab)->an_nextoff; + + /* We need to set left subtree's last entry + * offset to 0xFFFFFFFF for OS/2 to be able + * to read our files. It treats absence of + * 0xFFFFFFFF as error. + */ + AB_LASTANP(&rasp->as_ab)->an_nextoff = ~0; + + /* We need to fix new allocated AlSec's + * children, becouse their parent has changed. + */ + anp = AB_ALNODE(&nrasp->as_ab); + for (i=0; i<nrasp->as_ab.ab_busycnt; i++) { + error = hpfs_breadalsec(hpmp, anp->an_lsn, &bp); + if (error) { + brelse(nbp); + goto fail; + } + + asp = (alsec_t *)bp->b_data; + asp->as_parent = nrasp->as_self; + + bdwrite(bp); + anp ++; + } + } else { + ranp[0].an_nextoff = + AB_ALLEAF(&nrasp->as_ab)->al_off; + } + + ranp[0].an_lsn = rasp->as_self; + ranp[1].an_nextoff = ~0; + ranp[1].an_lsn = nrasp->as_self; + + bdwrite(nbp); + + *resp = AE_SPLIT; + wb = 1; + } + + if (wb) + bdwrite (rbp); + else + brelse(rbp); + + return (0); + +fail: + brelse(rbp); + + return (error); +} + +/* + * Recursive routine walking down the b-tree and deallocating all + * extents above bn. Returns *resp != 0 if alblk was totally + * deallocated and may be freed. Tries to keep b-tree. + * + * (XXXX) NOTE! THIS ROUTINE WILL NEVER DECREMENT DEPTH OF + * THE TREE. + */ +int +hpfs_truncatealblk ( + struct hpfsmount *hpmp, + alblk_t *abp, + lsn_t bn, + int *resp) +{ + int error; + alleaf_t *alp; + alnode_t *anp; + alsec_t *asp; + struct buf *bp; + + dprintf(("hpfs_truncatealblk: AlBlk: [0x%x,0x%x, 0x%x]\n", + abp->ab_freecnt, abp->ab_busycnt, abp->ab_flag)); + + if (abp->ab_flag & AB_NODES) { + /* + * Scan array of AlNodes backward, + * diving in recursion if needed + */ + anp = AB_LASTANP(abp); + + while (abp->ab_busycnt && (bn <= anp->an_nextoff)) { + dprintf(("hpfs_truncatealblk: AlNode: [0x%x,0x%x] \n", + anp->an_nextoff,anp->an_lsn)); + + error = hpfs_breadalsec(hpmp, anp->an_lsn, &bp); + if (error) + return (error); + + asp = (alsec_t *)bp->b_data; + + error = hpfs_truncatealblk (hpmp, + &asp->as_ab, bn, resp); + if (error) { + brelse(bp); + return (error); + } + + if (*resp) { + brelse (bp); + + error = hpfs_bmmarkfree(hpmp, + anp->an_lsn, 1); + if (error) + return (error); + + AB_RMAN(abp); + anp --; + } else { + /* + * We have deallocated some entries, some space + * migth been freed, then try to concat two + * last AlSec. + */ + anp->an_nextoff = ~0; + if (abp->ab_busycnt >= 2) { + alsec_t *as0p; + struct buf *b0p; + + error = hpfs_breadalsec(hpmp, + (anp-1)->an_lsn, &b0p); + if (error) + return (error); + + as0p = (alsec_t *)b0p->b_data; + error = hpfs_concatalsec(hpmp, + as0p, asp, anp - 1); + if (error == ENOSPC) { + /* Not enought space */ + brelse (b0p); + bdwrite (bp); + } else if (error == 0) { + /* All OK */ + (anp-1)->an_nextoff = anp->an_nextoff; + + bdwrite (b0p); + brelse (bp); + + error = hpfs_bmmarkfree(hpmp, + anp->an_lsn, 1); + if (error) + return (error); + + AB_RMAN(abp); + } else { + /* True error */ + brelse (b0p); + brelse (bp); + return (error); + } + } else { + /* Nowhere to concatenate */ + bdwrite (bp); + } + + /* There can not be any more entries + * over greater bn, becouse last AlSec + * wasn't freed totally. So go out. + */ + break; + } + } + + if (abp->ab_busycnt == 0) + *resp = 1; + else + *resp = 0; + } else { + /* + * Scan array of AlLeafs backward, + * free all above bn. + */ + alp = AB_LASTALP(abp); + + while (abp->ab_busycnt && (bn < alp->al_off + alp->al_len)){ + dprintf(("hpfs_truncatealblk: AlLeaf: [0x%x,0x%x,0x%x] \n", + alp->al_off,alp->al_len,alp->al_lsn)); + + if (bn <= alp->al_off) { + error = hpfs_bmmarkfree(hpmp, alp->al_lsn, + alp->al_len); + if (error) + return (error); + + AB_RMAL(abp); + alp --; + } else if ((bn > alp->al_off) && + (bn < alp->al_off + alp->al_len)){ + error = hpfs_bmmarkfree(hpmp, + alp->al_lsn + bn - alp->al_off, + alp->al_len - bn + alp->al_off); + if (error) + return (error); + + alp->al_len = bn - alp->al_off; + + break; + } else + break; + } + } + + /* Signal parent deallocation, if need */ + if (abp->ab_busycnt == 0) + *resp = 1; + else + *resp = 0; + + return (0); +} diff --git a/sys/fs/hpfs/hpfs_hash.c b/sys/fs/hpfs/hpfs_hash.c new file mode 100644 index 0000000..a948ade --- /dev/null +++ b/sys/fs/hpfs/hpfs_hash.c @@ -0,0 +1,173 @@ +/* + * Copyright (c) 1982, 1986, 1989, 1991, 1993, 1995 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)ufs_ihash.c 8.7 (Berkeley) 5/17/95 + * $FreeBSD$ + */ + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/kernel.h> +#include <sys/lock.h> +#include <sys/vnode.h> +#include <sys/mount.h> +#include <sys/malloc.h> +#include <sys/proc.h> + +#include <fs/hpfs/hpfs.h> + +MALLOC_DEFINE(M_HPFSHASH, "HPFS hash", "HPFS node hash tables"); + +/* + * Structures associated with hpfsnode cacheing. + */ +static LIST_HEAD(hphashhead, hpfsnode) *hpfs_hphashtbl; +static u_long hpfs_hphash; /* size of hash table - 1 */ +#define HPNOHASH(dev, lsn) (&hpfs_hphashtbl[(minor(dev) + (lsn)) & hpfs_hphash]) +#ifndef NULL_SIMPLELOCKS +static struct simplelock hpfs_hphash_slock; +#endif +struct lock hpfs_hphash_lock; + +/* + * Initialize inode hash table. + */ +void +hpfs_hphashinit() +{ + + lockinit (&hpfs_hphash_lock, PINOD, "hpfs_hphashlock", 0, 0); + hpfs_hphashtbl = HASHINIT(desiredvnodes, M_HPFSHASH, M_WAITOK, + &hpfs_hphash); + simple_lock_init(&hpfs_hphash_slock); +} + +/* + * Use the device/inum pair to find the incore inode, and return a pointer + * to it. If it is in core, return it, even if it is locked. + */ +struct hpfsnode * +hpfs_hphashlookup(dev, ino) + dev_t dev; + lsn_t ino; +{ + struct hpfsnode *hp; + + simple_lock(&hpfs_hphash_slock); + for (hp = HPNOHASH(dev, ino)->lh_first; hp; hp = hp->h_hash.le_next) + if (ino == hp->h_no && dev == hp->h_dev) + break; + simple_unlock(&hpfs_hphash_slock); + + return (hp); +} + +#if 0 +struct hpfsnode * +hpfs_hphashget(dev, ino) + dev_t dev; + lsn_t ino; +{ + struct hpfsnode *hp; + +loop: + simple_lock(&hpfs_hphash_slock); + for (hp = HPNOHASH(dev, ino)->lh_first; hp; hp = hp->h_hash.le_next) { + if (ino == hp->h_no && dev == hp->h_dev) { + LOCKMGR(&hp->h_intlock, LK_EXCLUSIVE | LK_INTERLOCK, &hpfs_hphash_slock, NULL); + return (hp); + } + } + simple_unlock(&hpfs_hphash_slock); + return (hp); +} +#endif + +struct vnode * +hpfs_hphashvget(dev, ino, p) + dev_t dev; + lsn_t ino; + struct proc *p; +{ + struct hpfsnode *hp; + struct vnode *vp; + +loop: + simple_lock(&hpfs_hphash_slock); + for (hp = HPNOHASH(dev, ino)->lh_first; hp; hp = hp->h_hash.le_next) { + if (ino == hp->h_no && dev == hp->h_dev) { + vp = HPTOV(hp); + simple_lock (&vp->v_interlock); + simple_unlock (&hpfs_hphash_slock); + if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, p)) + goto loop; + return (vp); + } + } + simple_unlock(&hpfs_hphash_slock); + return (NULLVP); +} + +/* + * Insert the hpfsnode into the hash table. + */ +void +hpfs_hphashins(hp) + struct hpfsnode *hp; +{ + struct hphashhead *hpp; + + simple_lock(&hpfs_hphash_slock); + hpp = HPNOHASH(hp->h_dev, hp->h_no); + hp->h_flag |= H_HASHED; + LIST_INSERT_HEAD(hpp, hp, h_hash); + simple_unlock(&hpfs_hphash_slock); +} + +/* + * Remove the inode from the hash table. + */ +void +hpfs_hphashrem(hp) + struct hpfsnode *hp; +{ + simple_lock(&hpfs_hphash_slock); + if (hp->h_flag & H_HASHED) { + hp->h_flag &= ~H_HASHED; + LIST_REMOVE(hp, h_hash); +#ifdef DIAGNOSTIC + hp->h_hash.le_next = NULL; + hp->h_hash.le_prev = NULL; +#endif + } + simple_unlock(&hpfs_hphash_slock); +} diff --git a/sys/fs/hpfs/hpfs_ioctl.h b/sys/fs/hpfs/hpfs_ioctl.h new file mode 100644 index 0000000..7f60729 --- /dev/null +++ b/sys/fs/hpfs/hpfs_ioctl.h @@ -0,0 +1,43 @@ +/*- + * Copyright (c) 1998, 1999 Semen Ustimenko (semenu@FreeBSD.org) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#ifndef _HPFS_IOCTL_H_ +#define _HPFS_IOCTL_H_ +#include <sys/ioccom.h> + +struct hpfs_rdea { + u_long ea_no; + u_long ea_sz; + void * ea_data; +}; + +#define HPFSIOCGEANUM _IOR('H', 0, u_long) /* Get EA number */ +#define HPFSIOCGEASZ _IOWR('H', 1, u_long) /* Get EA size */ +#define HPFSIOCRDEA _IOWR('H', 2, struct hpfs_rdea) /* Read EA */ + +#endif diff --git a/sys/fs/hpfs/hpfs_lookup.c b/sys/fs/hpfs/hpfs_lookup.c new file mode 100644 index 0000000..53fde4d --- /dev/null +++ b/sys/fs/hpfs/hpfs_lookup.c @@ -0,0 +1,212 @@ +/*- + * Copyright (c) 1998, 1999 Semen Ustimenko (semenu@FreeBSD.org) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/kernel.h> +#include <sys/proc.h> +#include <sys/time.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/vnode.h> +#include <sys/mount.h> +#include <sys/namei.h> +#include <sys/malloc.h> +#include <sys/buf.h> + +#include <fs/hpfs/hpfs.h> +#include <fs/hpfs/hpfsmount.h> +#include <fs/hpfs/hpfs_subr.h> + +int hpfs_removedirent (struct hpfsmount *, lsn_t, char *, int, int *); + +/* + * This routine traverse the b+ tree representing directory + * looking for file named 'name'. Returns buf struct and hpfsdirent + * pointer. Calling routine is supposed to brelse buffer. + * name is supposed in Unix encodeing. + */ +int +hpfs_genlookupbyname ( + struct hpfsnode *dhp, + char *name, + int namelen, + struct buf **bpp, + struct hpfsdirent **depp) +{ + struct hpfsmount *hpmp = dhp->h_hpmp; + struct buf *bp; + struct dirblk *dp; + struct hpfsdirent *dep; + lsn_t lsn; + int error, res; + + dprintf(("hpfs_genlookupbyname(0x%x, %s (%d)): \n", + dhp->h_no, name, namelen)); + + lsn = ((alleaf_t *)dhp->h_fn.fn_abd)->al_lsn; +dive: + error = hpfs_breaddirblk (hpmp, lsn, &bp); + if (error) + return (error); + + dp = (struct dirblk *) bp->b_data; + dep = D_DIRENT(dp); + + while(!(dep->de_flag & DE_END)) { + dprintf(("no: 0x%x, size: %d, name: %2d:%.*s, flag: 0x%x\n", + dep->de_fnode, dep->de_size, dep->de_namelen, + dep->de_namelen, dep->de_name, dep->de_flag)); + + res = hpfs_cmpfname(hpmp, name, namelen, + dep->de_name, dep->de_namelen, dep->de_cpid); + if (res == 0) { + *bpp = bp; + *depp = dep; + return (0); + } else if (res < 0) + break; + + dep = (hpfsdirent_t *)(((caddr_t)dep) + dep->de_reclen); + } + + if (dep->de_flag & DE_DOWN) { + lsn = DE_DOWNLSN(dep); + brelse(bp); + goto dive; + } + + brelse(bp); + + return (ENOENT); +} + +int +hpfs_makefnode ( + struct vnode * dvp, + struct vnode ** vpp, + struct componentname *cnp, + struct vattr *vap) +{ +#ifdef HPFS_DEBUG + register struct hpfsnode *dhp = VTOHP(dvp); + dprintf(("hpfs_makefnode(0x%x, %s, %ld): \n", + dhp->h_no, cnp->cn_nameptr, cnp->cn_namelen)); +#endif + + return (EOPNOTSUPP); +} + +int +hpfs_removedirent ( + struct hpfsmount *hpmp, + lsn_t lsn, + char *name, + int namelen, + int *retp) +{ +#if 0 + struct buf *bp; + dirblk_t *dbp; + struct hpfsdirent *dep; + int deoff; + int error, ret; + + dprintf(("hpfs_removedirent(0x%x, %.*s, %d): \n", + lsn, namelen, name, namelen)); + + error = hpfs_breaddirblk (hpmp, lsn, &bp); + if (error) + return (error); + + dbp = (dirblk_t *) bp->b_data; + deoff = sizeof(dirblk_t); + dep = DB_DIRENT(dbp); + + while(!(dep->de_flag & DE_END)) { + dprintf(("no: 0x%x, size: %d, name: %2d:%.*s, flag: 0x%x\n", + dep->de_fnode, dep->de_size, dep->de_namelen, + dep->de_namelen, dep->de_name, dep->de_flag)); + + res = hpfs_cmpfname(hpmp, name, namelen, + dep->de_name, dep->de_namelen, dep->de_cpid); + if (res == 0) { + if (dep->de_flag & DE_DOWN) { + /*XXXXXX*/ + } else { + /* XXX we can copy less */ + bcopy (DE_NEXTDE(dep), dep, DB_BSIZE - deoff - dep->de_reclen); + dbp->d_freeoff -= dep->de_reclen; + *retp = 0; + } + bdwrite (bp); + return (0); + } else if (res < 0) + break; + + deoff += dep->de_reclen; + dep = DB_NEXTDE(dep); + } + + if (dep->de_flag & DE_DOWN) { + error = hpfs_removede (hpmp, DE_DOWNLSN(dep), name, namelen, &ret); + if (error) { + brelse (bp); + return (error); + } + if (ret == 0) { + if (deoff > sizeof (dirblk_t)) { + } else if (deoff + dep->de_reclen < dbp->db_freeoff) { + } + } + } else { + error = ENOENT; + } + + brelse (bp); + return (error); +#endif + return (EOPNOTSUPP); +} + +int +hpfs_removefnode ( + struct vnode * dvp, + struct vnode * vp, + struct componentname *cnp) +{ +#ifdef HPFS_DEBUG + register struct hpfsnode *dhp = VTOHP(dvp); + register struct hpfsnode *hp = VTOHP(vp); + dprintf(("hpfs_removefnode(0x%x, 0x%x, %s, %ld): \n", + dhp->h_no, hp->h_no, cnp->cn_nameptr, cnp->cn_namelen)); +#endif + + + return (EOPNOTSUPP); +} diff --git a/sys/fs/hpfs/hpfs_subr.c b/sys/fs/hpfs/hpfs_subr.c new file mode 100644 index 0000000..4d60f92 --- /dev/null +++ b/sys/fs/hpfs/hpfs_subr.c @@ -0,0 +1,861 @@ +/*- + * Copyright (c) 1998, 1999 Semen Ustimenko (semenu@FreeBSD.org) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/kernel.h> +#include <sys/proc.h> +#include <sys/time.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/vnode.h> +#include <sys/mount.h> +#include <sys/namei.h> +#include <sys/malloc.h> +#include <sys/buf.h> + +#include <fs/hpfs/hpfs.h> +#include <fs/hpfs/hpfsmount.h> +#include <fs/hpfs/hpfs_subr.h> + +u_long +hpfs_checksum( + u_int8_t *object, + int size) +{ + register int i; + u_long csum=0L; + for (i=0; i < size; i++) { + csum += (u_long) *object++; + csum = (csum << 7) + (csum >> (25)); + } + return (csum); +} + +void +hpfs_bmdeinit( + struct hpfsmount *hpmp) +{ + struct buf *bp; + int i; + + dprintf(("hpmp_bmdeinit: ")); + + if (!(hpmp->hpm_mp->mnt_flag & MNT_RDONLY)) { + /* + * Write down BitMap. + */ + for (i=0; i<hpmp->hpm_dbnum; i++) { + dprintf(("[%d: 0x%x] ", i, hpmp->hpm_bmind[i])); + + bp = getblk(hpmp->hpm_devvp, hpmp->hpm_bmind[i], + BMSIZE, 0, 0); + clrbuf(bp); + + bcopy(hpmp->hpm_bitmap + BMSIZE * i, bp->b_data, + BMSIZE); + + bwrite(bp); + } + } + + FREE(hpmp->hpm_bitmap,M_HPFSMNT); + FREE(hpmp->hpm_bmind,M_HPFSMNT); + + dprintf(("\n")); +} + +/* + * Initialize BitMap management, includes calculation of + * available blocks number. + */ +int +hpfs_bminit( + struct hpfsmount *hpmp) +{ + struct buf *bp; + int error, i, k; + u_long dbavail; + + dprintf(("hpfs_bminit: ")); + + hpmp->hpm_dbnum = (hpmp->hpm_su.su_btotal + 0x3FFF) / 0x4000; + + dprintf(("0x%lx data bands, ", hpmp->hpm_dbnum)); + + MALLOC(hpmp->hpm_bmind, lsn_t *, hpmp->hpm_dbnum * sizeof(lsn_t), + M_HPFSMNT, M_WAITOK); + + MALLOC(hpmp->hpm_bitmap, u_int8_t *, hpmp->hpm_dbnum * BMSIZE, + M_HPFSMNT, M_WAITOK); + + error = bread(hpmp->hpm_devvp, hpmp->hpm_su.su_bitmap.lsn1, + ((hpmp->hpm_dbnum + 0x7F) & ~(0x7F)) << 2, NOCRED, &bp); + if (error) { + brelse(bp); + FREE(hpmp->hpm_bitmap, M_HPFSMNT); + FREE(hpmp->hpm_bmind, M_HPFSMNT); + dprintf((" error %d\n", error)); + return (error); + } + bcopy(bp->b_data, hpmp->hpm_bmind, hpmp->hpm_dbnum * sizeof(lsn_t)); + + brelse(bp); + + /* + * Read in all BitMap + */ + for (i=0; i<hpmp->hpm_dbnum; i++) { + dprintf(("[%d: 0x%x] ", i, hpmp->hpm_bmind[i])); + + error = bread(hpmp->hpm_devvp, hpmp->hpm_bmind[i], + BMSIZE, NOCRED, &bp); + if (error) { + brelse(bp); + FREE(hpmp->hpm_bitmap, M_HPFSMNT); + FREE(hpmp->hpm_bmind, M_HPFSMNT); + dprintf((" error %d\n", error)); + return (error); + } + bcopy(bp->b_data, hpmp->hpm_bitmap + BMSIZE * i, BMSIZE); + + brelse(bp); + } + + /* + * Look througth BitMap and count free bits + */ + dbavail = 0; + for (i=0; i < hpmp->hpm_su.su_btotal >> 5; i++) { + register u_int32_t mask; + for (k=0, mask=1; k < 32; k++, mask<<=1) + if(((u_int32_t *)hpmp->hpm_bitmap)[i] & mask) + dbavail ++; + + } + hpmp->hpm_bavail = dbavail; + + return (0); +} + +int +hpfs_cmpfname ( + struct hpfsmount *hpmp, + char * uname, + int ulen, + char * dname, + int dlen, + u_int16_t cp) +{ + register int i, res; + + for (i = 0; i < ulen && i < dlen; i++) { + res = hpfs_toupper(hpmp, hpfs_u2d(hpmp, uname[i]), cp) - + hpfs_toupper(hpmp, dname[i], cp); + if (res) + return res; + } + return (ulen - dlen); +} + +int +hpfs_cpstrnnicmp ( + struct hpfsmount *hpmp, + char * str1, + int str1len, + u_int16_t str1cp, + char * str2, + int str2len, + u_int16_t str2cp) +{ + int i, res; + + for (i = 0; i < str1len && i < str2len; i++) { + res = (int)hpfs_toupper(hpmp, ((u_char *)str1)[i], str1cp) - + (int)hpfs_toupper(hpmp, ((u_char *)str2)[i], str2cp); + if (res) + return res; + } + return (str1len - str2len); +} + + +int +hpfs_cpload ( + struct hpfsmount *hpmp, + struct cpiblk *cpibp, + struct cpdblk *cpdbp) +{ + struct buf *bp; + struct cpdsec * cpdsp; + int error, i; + + error = bread(hpmp->hpm_devvp, cpibp->b_cpdsec, DEV_BSIZE, NOCRED, &bp); + if (error) { + brelse(bp); + return (error); + } + + cpdsp = (struct cpdsec *)bp->b_data; + + for (i=cpdsp->d_cpfirst; i<cpdsp->d_cpcnt; i++) { + if (cpdsp->d_cpdblk[i].b_cpid == cpibp->b_cpid) { + bcopy(cpdsp->d_cpdblk + i, cpdbp, + sizeof(struct cpdblk)); + + brelse(bp); + + return (0); + } + } + + brelse(bp); + + return (ENOENT); +} + + +/* + * Initialize Code Page information management. + * Load all copdepages in memory. + */ +int +hpfs_cpinit ( + struct hpfsmount *hpmp, + struct hpfs_args *argsp) +{ + struct buf *bp; + int error, i; + lsn_t lsn; + int cpicnt; + struct cpisec * cpisp; + struct cpiblk * cpibp; + struct cpdblk * cpdbp; + + dprintf(("hpfs_cpinit: \n")); + + if (argsp->flags & HPFSMNT_TABLES) { + bcopy(argsp->d2u, hpmp->hpm_d2u, sizeof(u_char) * 0x80); + bcopy(argsp->u2d, hpmp->hpm_u2d, sizeof(u_char) * 0x80); + } else { + for (i=0x0; i<0x80;i++) { + hpmp->hpm_d2u[i] = i + 0x80; + hpmp->hpm_u2d[i] = i + 0x80; + } + } + + cpicnt = hpmp->hpm_sp.sp_cpinum; + + MALLOC(hpmp->hpm_cpdblk, struct cpdblk *, + cpicnt * sizeof(struct cpdblk), M_HPFSMNT, M_WAITOK); + + cpdbp = hpmp->hpm_cpdblk; + lsn = hpmp->hpm_sp.sp_cpi; + + while (cpicnt > 0) { + error = bread(hpmp->hpm_devvp, lsn, DEV_BSIZE, NOCRED, &bp); + if (error) { + brelse(bp); + return (error); + } + + cpisp = (struct cpisec *)bp->b_data; + + cpibp = cpisp->s_cpi; + for (i=0; i<cpisp->s_cpicnt; i++, cpicnt --, cpdbp++, cpibp++) { + dprintf(("hpfs_cpinit: Country: %d, CP: %d (%d)\n", + cpibp->b_country, cpibp->b_cpid, + cpibp->b_vcpid)); + + error = hpfs_cpload(hpmp, cpibp, cpdbp); + if (error) { + brelse(bp); + return (error); + } + } + lsn = cpisp->s_next; + brelse(bp); + } + + return (0); +} + +int +hpfs_cpdeinit ( + struct hpfsmount *hpmp) +{ + dprintf(("hpmp_cpdeinit: ")); + FREE(hpmp->hpm_cpdblk,M_HPFSMNT); + return (0); +} + +/* + * Lookup for a run of blocks. + */ +int +hpfs_bmlookup ( + struct hpfsmount *hpmp, + u_long flags, /* 1 means we want right len blocks in run, not less */ + lsn_t lsn, /* We want near this one */ + u_long len, /* We want such long */ + lsn_t *lsnp, /* We got here */ + u_long *lenp) /* We got this long */ +{ + u_int32_t * bitmap; + register u_int32_t mask; + int i,k; + int cband, vcband; + u_int bandsz; + int count; + + dprintf(("hpfs_bmlookup: lsn: 0x%x, len 0x%lx | Step1\n", lsn, len)); + + if (lsn > hpmp->hpm_su.su_btotal) { + printf("hpfs_bmlookup: OUT OF VOLUME\n"); + return ENOSPC; + } + if (len > hpmp->hpm_bavail) { + printf("hpfs_bmlookup: OUT OF SPACE\n"); + return ENOSPC; + } + i = lsn >> 5; + k = lsn & 0x1F; + mask = 1 << k; + bitmap = (u_int32_t *)hpmp->hpm_bitmap + i; + + if (*bitmap & mask) { + *lsnp = lsn; + *lenp = 0; + for (; k < 32; k++, mask<<=1) { + if (*bitmap & mask) + (*lenp) ++; + else { + if (flags & 1) + goto step2; + else + return (0); + } + + if (*lenp == len) + return (0); + } + + bitmap++; + i++; + for (; i < hpmp->hpm_su.su_btotal >> 5; i++, bitmap++) { + for (k=0, mask=1; k < 32; k++, mask<<=1) { + if (*bitmap & mask) + (*lenp) ++; + else { + if (flags & 1) + goto step2; + else + return (0); + } + + if (*lenp == len) + return (0); + } + } + return (0); + } + +step2: + /* + * Lookup all bands begining from cband, lookup for first block + */ + cband = (lsn >> 14); + dprintf(("hpfs_bmlookup: Step2: band 0x%x (0x%lx)\n", + cband, hpmp->hpm_dbnum)); + for (vcband = 0; vcband < hpmp->hpm_dbnum; vcband ++, cband++) { + cband = cband % hpmp->hpm_dbnum; + bandsz = min (hpmp->hpm_su.su_btotal - (cband << 14), 0x4000); + dprintf(("hpfs_bmlookup: band: %d, sz: 0x%x\n", cband, bandsz)); + + bitmap = (u_int32_t *)hpmp->hpm_bitmap + (cband << 9); + *lsnp = cband << 14; + *lenp = 0; + count = 0; + for (i=0; i < bandsz >> 5; i++, bitmap++) { + for (k=0, mask=1; k < 32; k++, mask<<=1) { + if (*bitmap & mask) { + if (count) { + (*lenp) ++; + } else { + count = 1; + *lsnp = (cband << 14) + (i << 5) + k; + *lenp = 1; + } + } else { + if ((*lenp) && !(flags & 1)) { + return (0); + } else { + count = 0; + } + } + + if (*lenp == len) + return (0); + } + } + if (cband == hpmp->hpm_dbnum - 1) { + if ((*lenp) && !(flags & 1)) { + return (0); + } else { + count = 0; + } + } + } + + return (ENOSPC); +} + +/* + * Lookup a single free block. XXX Need locking on BitMap operations + * VERY STUPID ROUTINE!!! + */ +int +hpfs_bmfblookup ( + struct hpfsmount *hpmp, + lsn_t *lp) +{ + u_int32_t * bitmap; + int i,k; + + dprintf(("hpfs_bmfblookup: ")); + + bitmap = (u_int32_t *)hpmp->hpm_bitmap; + for (i=0; i < hpmp->hpm_su.su_btotal >> 5; i++, bitmap++) { + k = ffs(*bitmap); + if (k) { + *lp = (i << 5) + k - 1; + dprintf((" found: 0x%x\n",*lp)); + return (0); + } + } + + return (ENOSPC); +} + +/* + * Mark contignous block of blocks. + */ +int +hpfs_bmmark ( + struct hpfsmount *hpmp, + lsn_t bn, + u_long bl, + int state) +{ + u_int32_t * bitmap; + int i, didprint = 0; + + dprintf(("hpfs_bmmark(0x%x, 0x%lx, %d): \n",bn,bl, state)); + + if ((bn > hpmp->hpm_su.su_btotal) || (bn+bl > hpmp->hpm_su.su_btotal)) { + printf("hpfs_bmmark: MARKING OUT OF VOLUME\n"); + return 0; + } + bitmap = (u_int32_t *)hpmp->hpm_bitmap; + bitmap += bn >> 5; + + while (bl > 0) { + for (i = bn & 0x1F; (i < 0x20) && (bl > 0) ; i++, bl--) { + if (state) { + if ( *bitmap & (1 << i)) { + if (!didprint) { + printf("hpfs_bmmark: ALREADY FREE\n"); + didprint = 1; + } + } else + hpmp->hpm_bavail++; + + *bitmap |= (1 << i); + } else { + if ((~(*bitmap)) & (1 << i)) { + if (!didprint) { + printf("hpfs_bmmark: ALREADY BUSY\n"); + didprint = 1; + } + } else + hpmp->hpm_bavail--; + + *bitmap &= ~(1 << i); + } + } + bn = 0; + bitmap++; + } + + return (0); +} + + +int +hpfs_validateparent ( + struct hpfsnode *hp) +{ + struct hpfsnode *dhp; + struct vnode *dvp; + struct hpfsmount *hpmp = hp->h_hpmp; + struct buf *bp; + struct dirblk *dp; + struct hpfsdirent *dep; + lsn_t lsn, olsn; + int level, error; + + dprintf(("hpfs_validatetimes(0x%x): [parent: 0x%x] ", + hp->h_no, hp->h_fn.fn_parent)); + + if (hp->h_no == hp->h_fn.fn_parent) { + dhp = hp; + } else { + error = VFS_VGET(hpmp->hpm_mp, hp->h_fn.fn_parent, &dvp); + if (error) + return (error); + dhp = VTOHP(dvp); + } + + lsn = ((alleaf_t *)dhp->h_fn.fn_abd)->al_lsn; + + olsn = 0; + level = 1; + bp = NULL; + +dive: + dprintf(("[dive 0x%x] ", lsn)); + if (bp != NULL) + brelse(bp); + error = bread(dhp->h_devvp, lsn, D_BSIZE, NOCRED, &bp); + if (error) + goto failed; + + dp = (struct dirblk *) bp->b_data; + if (dp->d_magic != D_MAGIC) { + printf("hpfs_validatetimes: magic doesn't match\n"); + error = EINVAL; + goto failed; + } + + dep = D_DIRENT(dp); + + if (olsn) { + dprintf(("[restore 0x%x] ", olsn)); + + while(!(dep->de_flag & DE_END) ) { + if((dep->de_flag & DE_DOWN) && + (olsn == DE_DOWNLSN(dep))) + break; + dep = (hpfsdirent_t *)((caddr_t)dep + dep->de_reclen); + } + + if((dep->de_flag & DE_DOWN) && (olsn == DE_DOWNLSN(dep))) { + if (dep->de_flag & DE_END) + goto blockdone; + + if (hp->h_no == dep->de_fnode) { + dprintf(("[found] ")); + goto readdone; + } + + dep = (hpfsdirent_t *)((caddr_t)dep + dep->de_reclen); + } else { + printf("hpfs_validatetimes: ERROR! oLSN not found\n"); + error = EINVAL; + goto failed; + } + } + + olsn = 0; + + while(!(dep->de_flag & DE_END)) { + if(dep->de_flag & DE_DOWN) { + lsn = DE_DOWNLSN(dep); + level++; + goto dive; + } + + if (hp->h_no == dep->de_fnode) { + dprintf(("[found] ")); + goto readdone; + } + + dep = (hpfsdirent_t *)((caddr_t)dep + dep->de_reclen); + } + + if(dep->de_flag & DE_DOWN) { + dprintf(("[enddive] ")); + lsn = DE_DOWNLSN(dep); + level++; + goto dive; + } + +blockdone: + dprintf(("[EOB] ")); + olsn = lsn; + lsn = dp->d_parent; + level--; + dprintf(("[level %d] ", level)); + if (level > 0) + goto dive; /* undive really */ + + goto failed; + +readdone: + bcopy(dep->de_name,hp->h_name,dep->de_namelen); + hp->h_name[dep->de_namelen] = '\0'; + hp->h_namelen = dep->de_namelen; + hp->h_ctime = dep->de_ctime; + hp->h_atime = dep->de_atime; + hp->h_mtime = dep->de_mtime; + hp->h_flag |= H_PARVALID; + + dprintf(("[readdone]")); + +failed: + dprintf(("\n")); + if (bp != NULL) + brelse(bp); + if (hp != dhp) + vput(dvp); + + return (error); +} + +struct timespec +hpfstimetounix ( + u_long hptime) +{ + struct timespec t; + + t.tv_nsec = 0; + t.tv_sec = hptime; + + return t; +} + +/* + * Write down changes done to parent dir, these are only times for now. + * hpfsnode have to be locked. + */ +int +hpfs_updateparent ( + struct hpfsnode *hp) +{ + struct hpfsnode *dhp; + struct vnode *dvp; + struct hpfsdirent *dep; + struct buf * bp; + int error; + + dprintf(("hpfs_updateparent(0x%x): \n", hp->h_no)); + + if (!(hp->h_flag & H_PARCHANGE)) + return (0); + + if (!(hp->h_flag & H_PARVALID)) { + error = hpfs_validateparent (hp); + if (error) + return (error); + } + + if (hp->h_no == hp->h_fn.fn_parent) { + dhp = hp; + } else { + error = VFS_VGET(hp->h_hpmp->hpm_mp, hp->h_fn.fn_parent, + &dvp); + if (error) + return (error); + dhp = VTOHP(dvp); + } + + error = hpfs_genlookupbyname (dhp, hp->h_name, hp->h_namelen, + &bp, &dep); + if (error) { + goto failed; + } + + dep->de_atime = hp->h_atime; + dep->de_mtime = hp->h_mtime; + dep->de_size = hp->h_fn.fn_size; + + bdwrite (bp); + + hp->h_flag &= ~H_PARCHANGE; + + error = 0; +failed: + if (hp != dhp) + vput(dvp); + + return (0); +} + +/* + * Write down on disk changes done to fnode. hpfsnode have to be locked. + */ +int +hpfs_update ( + struct hpfsnode *hp) +{ + struct buf * bp; + + dprintf(("hpfs_update(0x%x): \n", hp->h_no)); + + if (!(hp->h_flag & H_CHANGE)) + return (0); + + bp = getblk(hp->h_devvp, hp->h_no, FNODESIZE, 0, 0); + clrbuf(bp); + + bcopy (&hp->h_fn, bp->b_data, sizeof(struct fnode)); + bdwrite (bp); + + hp->h_flag &= ~H_CHANGE; + + if (hp->h_flag & H_PARCHANGE) + return (hpfs_updateparent(hp)); + + return (0); +} + +/* + * Truncate file to specifed size. hpfsnode have to be locked. + */ +int +hpfs_truncate ( + struct hpfsnode *hp, + u_long size) +{ + struct hpfsmount *hpmp = hp->h_hpmp; + lsn_t newblen, oldblen; + int error, pf; + + dprintf(("hpfs_truncate(0x%x, 0x%x -> 0x%lx): ", + hp->h_no, hp->h_fn.fn_size, size)); + + newblen = (size + DEV_BSIZE - 1) >> DEV_BSHIFT; + oldblen = (hp->h_fn.fn_size + DEV_BSIZE - 1) >> DEV_BSHIFT; + + dprintf(("blen: 0x%x -> 0x%x\n", oldblen, newblen)); + + error = hpfs_truncatealblk (hpmp, &hp->h_fn.fn_ab, newblen, &pf); + if (error) + return (error); + if (pf) { + hp->h_fn.fn_ab.ab_flag = 0; + hp->h_fn.fn_ab.ab_freecnt = 0x8; + hp->h_fn.fn_ab.ab_busycnt = 0x0; + hp->h_fn.fn_ab.ab_freeoff = sizeof(alblk_t); + } + + hp->h_fn.fn_size = size; + + hp->h_flag |= (H_CHANGE | H_PARCHANGE); + + dprintf(("hpfs_truncate: successful\n")); + + return (0); +} + +/* + * Enlarge file to specifed size. hpfsnode have to be locked. + */ +int +hpfs_extend ( + struct hpfsnode *hp, + u_long size) +{ + struct hpfsmount *hpmp = hp->h_hpmp; + lsn_t newblen, oldblen; + int error; + + dprintf(("hpfs_extend(0x%x, 0x%x -> 0x%lx): ", + hp->h_no, hp->h_fn.fn_size, size)); + + if (hpmp->hpm_bavail < 0x10) + return (ENOSPC); + + newblen = (size + DEV_BSIZE - 1) >> DEV_BSHIFT; + oldblen = (hp->h_fn.fn_size + DEV_BSIZE - 1) >> DEV_BSHIFT; + + dprintf(("blen: 0x%x -> 0x%x\n", oldblen, newblen)); + + error = hpfs_addextent(hpmp, hp, newblen - oldblen); + if (error) { + printf("hpfs_extend: FAILED TO ADD EXTENT %d\n", error); + return (error); + } + + hp->h_fn.fn_size = size; + + hp->h_flag |= (H_CHANGE | H_PARCHANGE); + + dprintf(("hpfs_extend: successful\n")); + + return (0); +} + +/* + * Read AlSec structure, and check if magic is valid. + * You don't need to brelse buf on error. + */ +int +hpfs_breadstruct ( + struct hpfsmount *hpmp, + lsn_t lsn, + u_int len, + u_int32_t magic, + struct buf **bpp) +{ + struct buf *bp; + u_int32_t *mp; + int error; + + dprintf(("hpfs_breadstruct: reading at 0x%x\n", lsn)); + + *bpp = NULL; + + error = bread(hpmp->hpm_devvp, lsn, len, NOCRED, &bp); + if (error) { + brelse(bp); + return (error); + } + mp = (u_int32_t *) bp->b_data; + if (*mp != magic) { + brelse(bp); + printf("hpfs_breadstruct: MAGIC DOESN'T MATCH (0x%08x != 0x%08x)\n", + *mp, magic); + return (EINVAL); + } + + *bpp = bp; + + return (0); +} + diff --git a/sys/fs/hpfs/hpfs_subr.h b/sys/fs/hpfs/hpfs_subr.h new file mode 100644 index 0000000..46d2f41 --- /dev/null +++ b/sys/fs/hpfs/hpfs_subr.h @@ -0,0 +1,87 @@ +/*- + * Copyright (c) 1998, 1999 Semen Ustimenko (semenu@FreeBSD.org) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#define hpfs_bmmarkfree(hpmp, bn,bl) hpfs_bmmark(hpmp, bn, bl, 1) +#define hpfs_bmmarkbusy(hpmp, bn,bl) hpfs_bmmark(hpmp, bn, bl, 0) + +u_long hpfs_checksum (u_int8_t *, int); + +int hpfs_bminit (struct hpfsmount *); +void hpfs_bmdeinit (struct hpfsmount *); +int hpfs_bmfblookup (struct hpfsmount *, lsn_t *); +int hpfs_bmmark (struct hpfsmount *, lsn_t, u_long, int); +int hpfs_bmlookup (struct hpfsmount *, u_long, lsn_t, u_long, + lsn_t *, u_long *); + +struct hpfs_args; +int hpfs_cpinit (struct hpfsmount *, struct hpfs_args *); +int hpfs_cpdeinit (struct hpfsmount *); +int hpfs_cpload (struct hpfsmount *, struct cpiblk *, + struct cpdblk *); +int hpfs_cpstrnnicmp (struct hpfsmount *, char *, int, u_int16_t, + char *, int, u_int16_t); +int hpfs_cmpfname (struct hpfsmount *, char *, int, + char *, int, u_int16_t); + +/* XXX Need unsigned conversion? */ +#define hpfs_u2d(hpmp, c) ((((u_char)(c))&0x80)?(hpmp->hpm_u2d[((u_char)(c))&0x7F]):((u_char)(c))) +#define hpfs_d2u(hpmp, c) ((((u_char)(c))&0x80)?(hpmp->hpm_d2u[((u_char)(c))&0x7F]):((u_char)(c))) +#define hpfs_toupper(hpmp, c, cp) ((((u_char)(c))&0x80) ? ((u_char)((hpmp)->hpm_cpdblk[(cp)].b_upcase[((u_char)(c))&0x7F])) : ((((u_char)(c)) >= 'a' && ((u_char)(c)) <='z')?(((u_char)(c))-'a'+'A'):((u_char)(c)))) + + +int hpfs_truncate (struct hpfsnode *, u_long); +int hpfs_extend (struct hpfsnode *, u_long); + +int hpfs_updateparent (struct hpfsnode *); +int hpfs_update (struct hpfsnode *); + +int hpfs_validateparent (struct hpfsnode *); +struct timespec hpfstimetounix (u_long); +int hpfs_genlookupbyname (struct hpfsnode *, char *, int, + struct buf **, struct hpfsdirent **); + +int hpfs_makefnode (struct vnode *, struct vnode **, + struct componentname *, struct vattr *); +int hpfs_removefnode (struct vnode *, struct vnode *, + struct componentname *); + +int hpfs_breadstruct (struct hpfsmount *, lsn_t, u_int, u_int32_t, + struct buf **); +#define hpfs_breadalsec(hpmp, lsn, bpp) \ + hpfs_breadstruct(hpmp, lsn, DEV_BSIZE, AS_MAGIC, bpp) +#define hpfs_breaddirblk(hpmp, lsn, bpp) \ + hpfs_breadstruct(hpmp, lsn, D_BSIZE, D_MAGIC, bpp) + +#if 0 +#define hpfs_hplock(hp, p) LOCKMGR(&(hp)->h_intlock, LK_EXCLUSIVE, (p)) +#define hpfs_hpunlock(hp, p) LOCKMGR(&(hp)->h_intlock, LK_RELEASE, (p)) +#endif + +int hpfs_hpbmap (struct hpfsnode *, daddr_t, daddr_t *, int *); +int hpfs_truncatealblk (struct hpfsmount *, alblk_t *, lsn_t,int *); +int hpfs_addextent (struct hpfsmount *, struct hpfsnode *, u_long); diff --git a/sys/fs/hpfs/hpfs_vfsops.c b/sys/fs/hpfs/hpfs_vfsops.c new file mode 100644 index 0000000..7e4750f --- /dev/null +++ b/sys/fs/hpfs/hpfs_vfsops.c @@ -0,0 +1,770 @@ +/*- + * Copyright (c) 1998, 1999 Semen Ustimenko (semenu@FreeBSD.org) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/namei.h> +#include <sys/conf.h> +#include <sys/proc.h> +#include <sys/kernel.h> +#include <sys/vnode.h> +#include <sys/mount.h> +#include <sys/buf.h> +#include <sys/fcntl.h> +#include <sys/malloc.h> + +#include <vm/vm.h> +#include <vm/vm_param.h> +#if defined(__NetBSD__) +#include <vm/vm_prot.h> +#endif +#include <vm/vm_page.h> +#include <vm/vm_object.h> +#include <vm/vm_extern.h> + +#if defined(__NetBSD__) +#include <miscfs/specfs/specdev.h> +#endif + +#include <fs/hpfs/hpfs.h> +#include <fs/hpfs/hpfsmount.h> +#include <fs/hpfs/hpfs_subr.h> + +#if defined(__FreeBSD__) +MALLOC_DEFINE(M_HPFSMNT, "HPFS mount", "HPFS mount structure"); +MALLOC_DEFINE(M_HPFSNO, "HPFS node", "HPFS node structure"); +#endif + +static int hpfs_root __P((struct mount *, struct vnode **)); +static int hpfs_statfs __P((struct mount *, struct statfs *, + struct proc *)); +static int hpfs_unmount __P((struct mount *, int, struct proc *)); +static int hpfs_vget __P((struct mount *mp, ino_t ino, + struct vnode **vpp)); +static int hpfs_mountfs __P((register struct vnode *, struct mount *, + struct hpfs_args *, struct proc *)); +static int hpfs_vptofh __P((struct vnode *, struct fid *)); +static int hpfs_fhtovp __P((struct mount *, struct fid *, + struct vnode **)); + +#if !defined(__FreeBSD__) +static int hpfs_quotactl __P((struct mount *, int, uid_t, caddr_t, + struct proc *)); +static int hpfs_start __P((struct mount *, int, struct proc *)); +static int hpfs_sync __P((struct mount *, int, struct ucred *, + struct proc *)); +#endif + +#if defined(__FreeBSD__) +struct sockaddr; +static int hpfs_mount __P((struct mount *, char *, caddr_t, + struct nameidata *, struct proc *)); +static int hpfs_init __P((struct vfsconf *)); +static int hpfs_checkexp __P((struct mount *, struct sockaddr *, + int *, struct ucred **)); +#else /* defined(__NetBSD__) */ +static int hpfs_mount __P((struct mount *, const char *, void *, + struct nameidata *, struct proc *)); +static void hpfs_init __P((void)); +static int hpfs_mountroot __P((void)); +static int hpfs_sysctl __P((int *, u_int, void *, size_t *, void *, + size_t, struct proc *)); +static int hpfs_checkexp __P((struct mount *, struct mbuf *, + int *, struct ucred **)); +#endif + +/*ARGSUSED*/ +static int +hpfs_checkexp(mp, nam, exflagsp, credanonp) +#if defined(__FreeBSD__) + register struct mount *mp; + struct sockaddr *nam; + int *exflagsp; + struct ucred **credanonp; +#else /* defined(__NetBSD__) */ + register struct mount *mp; + struct mbuf *nam; + int *exflagsp; + struct ucred **credanonp; +#endif +{ + register struct netcred *np; + register struct hpfsmount *hpm = VFSTOHPFS(mp); + + /* + * Get the export permission structure for this <mp, client> tuple. + */ + np = vfs_export_lookup(mp, &hpm->hpm_export, nam); + if (np == NULL) + return (EACCES); + + *exflagsp = np->netc_exflags; + *credanonp = &np->netc_anon; + return (0); +} + +#if !defined(__FreeBSD__) +/*ARGSUSED*/ +static int +hpfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p) + int *name; + u_int namelen; + void *oldp; + size_t *oldlenp; + void *newp; + size_t newlen; + struct proc *p; +{ + return (EINVAL); +} + +static int +hpfs_mountroot() +{ + return (EINVAL); +} +#endif + +#if defined(__FreeBSD__) +static int +hpfs_init ( + struct vfsconf *vcp ) +#else /* defined(__NetBSD__) */ +static void +hpfs_init () +#endif +{ + dprintf(("hpfs_init():\n")); + + hpfs_hphashinit(); +#if defined(__FreeBSD__) + return 0; +#endif +} + +static int +hpfs_mount ( + struct mount *mp, +#if defined(__FreeBSD__) + char *path, + caddr_t data, +#else /* defined(__NetBSD__) */ + const char *path, + void *data, +#endif + struct nameidata *ndp, + struct proc *p ) +{ + u_int size; + int err = 0; + struct vnode *devvp; + struct hpfs_args args; + struct hpfsmount *hpmp = 0; + + dprintf(("hpfs_mount():\n")); + /* + *** + * Mounting non-root file system or updating a file system + *** + */ + + /* copy in user arguments*/ + err = copyin(data, (caddr_t)&args, sizeof (struct hpfs_args)); + if (err) + goto error_1; /* can't get arguments*/ + + /* + * If updating, check whether changing from read-only to + * read/write; if there is no device name, that's all we do. + */ + if (mp->mnt_flag & MNT_UPDATE) { + dprintf(("hpfs_mount: MNT_UPDATE: ")); + + hpmp = VFSTOHPFS(mp); + + if (args.fspec == 0) { + dprintf(("export 0x%x\n",args.export.ex_flags)); + err = vfs_export(mp, &hpmp->hpm_export, &args.export); + if (err) { + printf("hpfs_mount: vfs_export failed %d\n", + err); + } + goto success; + } else { + dprintf(("name [FAILED]\n")); + err = EINVAL; + goto success; + } + dprintf(("\n")); + } + + /* + * Not an update, or updating the name: look up the name + * and verify that it refers to a sensible block device. + */ + NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p); + err = namei(ndp); + if (err) { + /* can't get devvp!*/ + goto error_1; + } + + devvp = ndp->ni_vp; + +#if defined(__FreeBSD__) + if (!vn_isdisk(devvp)) { + err = ENOTBLK; + goto error_2; + } +#else /* defined(__NetBSD__) */ + if (devvp->v_type != VBLK) { + err = ENOTBLK; + goto error_2; + } + if (major(devvp->v_rdev) >= nblkdev) { + err = ENXIO; + goto error_2; + } +#endif + + /* + ******************** + * NEW MOUNT + ******************** + */ + + /* + * Since this is a new mount, we want the names for + * the device and the mount point copied in. If an + * error occurs, the mountpoint is discarded by the + * upper level code. + */ + /* Save "last mounted on" info for mount point (NULL pad)*/ + copyinstr( path, /* mount point*/ + mp->mnt_stat.f_mntonname, /* save area*/ + MNAMELEN - 1, /* max size*/ + &size); /* real size*/ + bzero( mp->mnt_stat.f_mntonname + size, MNAMELEN - size); + + /* Save "mounted from" info for mount point (NULL pad)*/ + copyinstr( args.fspec, /* device name*/ + mp->mnt_stat.f_mntfromname, /* save area*/ + MNAMELEN - 1, /* max size*/ + &size); /* real size*/ + bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); + + err = hpfs_mountfs(devvp, mp, &args, p); + if (err) + goto error_2; + + /* + * Initialize FS stat information in mount struct; uses both + * mp->mnt_stat.f_mntonname and mp->mnt_stat.f_mntfromname + * + * This code is common to root and non-root mounts + */ + (void)VFS_STATFS(mp, &mp->mnt_stat, p); + + goto success; + + +error_2: /* error with devvp held*/ + + /* release devvp before failing*/ + vrele(devvp); + +error_1: /* no state to back out*/ + +success: + return( err); +} + +/* + * Common code for mount and mountroot + */ +int +hpfs_mountfs(devvp, mp, argsp, p) + register struct vnode *devvp; + struct mount *mp; + struct hpfs_args *argsp; + struct proc *p; +{ + int error, ncount, ronly; + struct sublock *sup; + struct spblock *spp; + struct hpfsmount *hpmp; + struct buf *bp = NULL; + struct vnode *vp; + dev_t dev = devvp->v_rdev; + + dprintf(("hpfs_mountfs():\n")); + /* + * Disallow multiple mounts of the same device. + * Disallow mounting of a device that is currently in use + * (except for root, which might share swap device for miniroot). + * Flush out any old buffers remaining from a previous use. + */ + error = vfs_mountedon(devvp); + if (error) + return (error); + ncount = vcount(devvp); +#if defined(__FreeBSD__) + if (devvp->v_object) + ncount -= 1; +#endif + if (ncount > 1 && devvp != rootvp) + return (EBUSY); + +#if defined(__FreeBSD__) + VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY, p); + error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0); + VOP__UNLOCK(devvp, 0, p); +#else + error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0); +#endif + if (error) + return (error); + + ronly = (mp->mnt_flag & MNT_RDONLY) != 0; + VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY, p); + error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p); + VOP__UNLOCK(devvp, 0, p); + if (error) + return (error); + + /* + * Do actual mount + */ + hpmp = malloc(sizeof(struct hpfsmount), M_HPFSMNT, M_WAITOK); + bzero(hpmp, sizeof(struct hpfsmount)); + + /* Read in SuperBlock */ + error = bread(devvp, SUBLOCK, SUSIZE, NOCRED, &bp); + if (error) + goto failed; + bcopy(bp->b_data, &hpmp->hpm_su, sizeof(struct sublock)); + brelse(bp); bp = NULL; + + /* Read in SpareBlock */ + error = bread(devvp, SPBLOCK, SPSIZE, NOCRED, &bp); + if (error) + goto failed; + bcopy(bp->b_data, &hpmp->hpm_sp, sizeof(struct spblock)); + brelse(bp); bp = NULL; + + sup = &hpmp->hpm_su; + spp = &hpmp->hpm_sp; + + /* Check magic */ + if (sup->su_magic != SU_MAGIC) { + printf("hpfs_mountfs: SuperBlock MAGIC DOESN'T MATCH\n"); + error = EINVAL; + goto failed; + } + if (spp->sp_magic != SP_MAGIC) { + printf("hpfs_mountfs: SpareBlock MAGIC DOESN'T MATCH\n"); + error = EINVAL; + goto failed; + } + + mp->mnt_data = (qaddr_t)hpmp; + hpmp->hpm_devvp = devvp; + hpmp->hpm_dev = devvp->v_rdev; + hpmp->hpm_mp = mp; + hpmp->hpm_uid = argsp->uid; + hpmp->hpm_gid = argsp->gid; + hpmp->hpm_mode = argsp->mode; + + error = hpfs_bminit(hpmp); + if (error) + goto failed; + + error = hpfs_cpinit(hpmp, argsp); + if (error) { + hpfs_bmdeinit(hpmp); + goto failed; + } + + error = hpfs_root(mp, &vp); + if (error) { + hpfs_cpdeinit(hpmp); + hpfs_bmdeinit(hpmp); + goto failed; + } + + vput(vp); + +#if defined(__FreeBSD__) + mp->mnt_stat.f_fsid.val[0] = (long)dev2udev(dev); + mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum; +#else + mp->mnt_stat.f_fsid.val[0] = (long)dev; + mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_HPFS); +#endif + mp->mnt_maxsymlinklen = 0; + mp->mnt_flag |= MNT_LOCAL; + devvp->v_specmountpoint = mp; + return (0); + +failed: + if (bp) + brelse (bp); + mp->mnt_data = (qaddr_t)NULL; +#if defined(__FreeBSD__) + devvp->v_specmountpoint = NULL; +#else + devvp->v_specflags &= ~SI_MOUNTEDON; +#endif + (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p); + return (error); +} + +#if !defined(__FreeBSD__) +static int +hpfs_start ( + struct mount *mp, + int flags, + struct proc *p ) +{ + return (0); +} +#endif + +static int +hpfs_unmount( + struct mount *mp, + int mntflags, + struct proc *p) +{ + int error, flags, ronly; + register struct hpfsmount *hpmp = VFSTOHPFS(mp); + + dprintf(("hpfs_unmount():\n")); + + ronly = (mp->mnt_flag & MNT_RDONLY) != 0; + + flags = 0; + if(mntflags & MNT_FORCE) + flags |= FORCECLOSE; + + dprintf(("hpfs_unmount: vflushing...\n")); + + error = vflush(mp,NULLVP,flags); + if (error) { + printf("hpfs_unmount: vflush failed: %d\n",error); + return (error); + } + +#if defined(__FreeBSD__) + hpmp->hpm_devvp->v_specmountpoint = NULL; +#else + hpmp->hpm_devvp->v_specflags &= ~SI_MOUNTEDON; +#endif + + vinvalbuf(hpmp->hpm_devvp, V_SAVE, NOCRED, p, 0, 0); + error = VOP_CLOSE(hpmp->hpm_devvp, ronly ? FREAD : FREAD|FWRITE, + NOCRED, p); + + vrele(hpmp->hpm_devvp); + + dprintf(("hpfs_umount: freeing memory...\n")); + hpfs_cpdeinit(hpmp); + hpfs_bmdeinit(hpmp); + mp->mnt_data = (qaddr_t)0; + mp->mnt_flag &= ~MNT_LOCAL; + FREE(hpmp, M_HPFSMNT); + + return (0); +} + +static int +hpfs_root( + struct mount *mp, + struct vnode **vpp ) +{ + int error = 0; + struct hpfsmount *hpmp = VFSTOHPFS(mp); + + dprintf(("hpfs_root():\n")); + error = VFS_VGET(mp, (ino_t)hpmp->hpm_su.su_rootfno, vpp); + if(error) { + printf("hpfs_root: VFS_VGET failed: %d\n",error); + return (error); + } + + return (error); +} + +static int +hpfs_statfs( + struct mount *mp, + struct statfs *sbp, + struct proc *p) +{ + struct hpfsmount *hpmp = VFSTOHPFS(mp); + + dprintf(("hpfs_statfs(): HPFS%d.%d\n", + hpmp->hpm_su.su_hpfsver, hpmp->hpm_su.su_fnctver)); + +#if defined(__FreeBSD__) + sbp->f_type = mp->mnt_vfc->vfc_typenum; +#else /* defined(__NetBSD__) */ + sbp->f_type = 0; +#endif + sbp->f_bsize = DEV_BSIZE; + sbp->f_iosize = DEV_BSIZE; + sbp->f_blocks = hpmp->hpm_su.su_btotal; + sbp->f_bfree = sbp->f_bavail = hpmp->hpm_bavail; + sbp->f_ffree = 0; + sbp->f_files = 0; + if (sbp != &mp->mnt_stat) { + bcopy((caddr_t)mp->mnt_stat.f_mntonname, + (caddr_t)&sbp->f_mntonname[0], MNAMELEN); + bcopy((caddr_t)mp->mnt_stat.f_mntfromname, + (caddr_t)&sbp->f_mntfromname[0], MNAMELEN); + } + sbp->f_flags = mp->mnt_flag; + + return (0); +} + +#if !defined(__FreeBSD__) +static int +hpfs_sync ( + struct mount *mp, + int waitfor, + struct ucred *cred, + struct proc *p) +{ + return (0); +} + +static int +hpfs_quotactl ( + struct mount *mp, + int cmds, + uid_t uid, + caddr_t arg, + struct proc *p) +{ + printf("hpfs_quotactl():\n"); + return (EOPNOTSUPP); +} +#endif + +/*ARGSUSED*/ +static int +hpfs_fhtovp( + struct mount *mp, + struct fid *fhp, + struct vnode **vpp) +{ + struct vnode *nvp; + struct hpfid *hpfhp = (struct hpfid *)fhp; + int error; + + if ((error = VFS_VGET(mp, hpfhp->hpfid_ino, &nvp)) != 0) { + *vpp = NULLVP; + return (error); + } + /* XXX as unlink/rmdir/mkdir/creat are not currently possible + * with HPFS, we don't need to check anything else for now */ + *vpp = nvp; + + return (0); +} + +static int +hpfs_vptofh( + struct vnode *vp, + struct fid *fhp) +{ + register struct hpfsnode *hpp; + register struct hpfid *hpfhp; + + hpp = VTOHP(vp); + hpfhp = (struct hpfid *)fhp; + hpfhp->hpfid_len = sizeof(struct hpfid); + hpfhp->hpfid_ino = hpp->h_no; + /* hpfhp->hpfid_gen = hpp->h_gen; */ + return (0); +} + +static int +hpfs_vget( + struct mount *mp, + ino_t ino, + struct vnode **vpp) +{ + struct hpfsmount *hpmp = VFSTOHPFS(mp); + struct vnode *vp; + struct hpfsnode *hp; + struct buf *bp; + struct proc *p = curproc; /* XXX */ + int error; + + dprintf(("hpfs_vget(0x%x): ",ino)); + + *vpp = NULL; + hp = NULL; + vp = NULL; + + if ((*vpp = hpfs_hphashvget(hpmp->hpm_dev, ino, p)) != NULL) { + dprintf(("hashed\n")); + return (0); + } + + /* + * We have to lock node creation for a while, + * but then we have to call getnewvnode(), + * this may cause hpfs_reclaim() to be called, + * this may need to VOP_VGET() parent dir for + * update reasons, and if parent is not in + * hash, we have to lock node creation... + * To solve this, we MALLOC, getnewvnode and init while + * not locked (probability of node appearence + * at that time is little, and anyway - we'll + * check for it). + */ + MALLOC(hp, struct hpfsnode *, sizeof(struct hpfsnode), + M_HPFSNO, M_WAITOK); + + error = getnewvnode(VT_HPFS, hpmp->hpm_mp, hpfs_vnodeop_p, &vp); + if (error) { + printf("hpfs_vget: can't get new vnode\n"); + FREE(hp, M_HPFSNO); + return (error); + } + + dprintf(("prenew ")); + + vp->v_data = hp; + + if (ino == (ino_t)hpmp->hpm_su.su_rootfno) + vp->v_flag |= VROOT; + + simple_lock_init(&hp->h_interlock); + lockinit(&hp->h_lock, PINOD, "hpnode", 0, 0); + + hp->h_flag = H_INVAL; + hp->h_vp = vp; + hp->h_hpmp = hpmp; + hp->h_no = ino; + hp->h_dev = hpmp->hpm_dev; + hp->h_uid = hpmp->hpm_uid; + hp->h_gid = hpmp->hpm_uid; + hp->h_mode = hpmp->hpm_mode; + hp->h_devvp = hpmp->hpm_devvp; + VREF(hp->h_devvp); + + error = VN_LOCK(vp, LK_EXCLUSIVE, p); + if (error) { + vput(vp); + return (error); + } + + do { + if ((*vpp = hpfs_hphashvget(hpmp->hpm_dev, ino, p)) != NULL) { + dprintf(("hashed2\n")); + vput(vp); + return (0); + } + } while(LOCKMGR(&hpfs_hphash_lock,LK_EXCLUSIVE|LK_SLEEPFAIL,NULL,NULL)); + + hpfs_hphashins(hp); + + LOCKMGR(&hpfs_hphash_lock, LK_RELEASE, NULL, NULL); + + error = bread(hpmp->hpm_devvp, ino, FNODESIZE, NOCRED, &bp); + if (error) { + printf("hpfs_vget: can't read ino %d\n",ino); + vput(vp); + return (error); + } + bcopy(bp->b_data, &hp->h_fn, sizeof(struct fnode)); + brelse(bp); + + if (hp->h_fn.fn_magic != FN_MAGIC) { + printf("hpfs_vget: MAGIC DOESN'T MATCH\n"); + vput(vp); + return (EINVAL); + } + + vp->v_type = hp->h_fn.fn_flag ? VDIR:VREG; + hp->h_flag &= ~H_INVAL; + + *vpp = vp; + + return (0); +} + +#if defined(__FreeBSD__) +static struct vfsops hpfs_vfsops = { + hpfs_mount, + vfs_stdstart, + hpfs_unmount, + hpfs_root, + vfs_stdquotactl, + hpfs_statfs, + vfs_stdsync, + hpfs_vget, + hpfs_fhtovp, + hpfs_checkexp, + hpfs_vptofh, + hpfs_init, + NULL +}; +VFS_SET(hpfs_vfsops, hpfs, 0); +#else /* defined(__NetBSD__) */ +extern struct vnodeopv_desc hpfs_vnodeop_opv_desc; + +struct vnodeopv_desc *hpfs_vnodeopv_descs[] = { + &hpfs_vnodeop_opv_desc, + NULL, +}; + +struct vfsops hpfs_vfsops = { + MOUNT_HPFS, + hpfs_mount, + hpfs_start, + hpfs_unmount, + hpfs_root, + hpfs_quotactl, + hpfs_statfs, + hpfs_sync, + hpfs_vget, + hpfs_fhtovp, + hpfs_vptofh, + hpfs_init, + hpfs_sysctl, + hpfs_mountroot, + hpfs_checkexp, + hpfs_vnodeopv_descs, +}; +#endif diff --git a/sys/fs/hpfs/hpfs_vnops.c b/sys/fs/hpfs/hpfs_vnops.c new file mode 100644 index 0000000..acc2949 --- /dev/null +++ b/sys/fs/hpfs/hpfs_vnops.c @@ -0,0 +1,1480 @@ +/*- + * Copyright (c) 1998, 1999 Semen Ustimenko (semenu@FreeBSD.org) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/kernel.h> +#include <sys/proc.h> +#include <sys/time.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/vnode.h> +#include <sys/mount.h> +#include <sys/namei.h> +#include <sys/malloc.h> +#include <sys/buf.h> +#include <sys/dirent.h> + +#include <vm/vm.h> +#include <vm/vm_param.h> +#if !defined(__FreeBSD__) +#include <vm/vm_prot.h> +#endif +#include <vm/vm_page.h> +#include <vm/vm_object.h> +#include <vm/vm_pager.h> +#include <vm/vm_zone.h> +#if defined(__FreeBSD__) +#include <vm/vnode_pager.h> +#endif +#include <vm/vm_extern.h> + +#if !defined(__FreeBSD__) +#include <miscfs/specfs/specdev.h> +#include <miscfs/genfs/genfs.h> +#endif + +#include <sys/unistd.h> /* for pathconf(2) constants */ + +#include <fs/hpfs/hpfs.h> +#include <fs/hpfs/hpfsmount.h> +#include <fs/hpfs/hpfs_subr.h> +#include <fs/hpfs/hpfs_ioctl.h> + +static int hpfs_de_uiomove __P((struct hpfsmount *, struct hpfsdirent *, + struct uio *)); +static int hpfs_ioctl __P((struct vop_ioctl_args *ap)); +static int hpfs_bypass __P((struct vop_generic_args *ap)); +static int hpfs_read __P((struct vop_read_args *)); +static int hpfs_write __P((struct vop_write_args *ap)); +static int hpfs_getattr __P((struct vop_getattr_args *ap)); +static int hpfs_setattr __P((struct vop_setattr_args *ap)); +static int hpfs_inactive __P((struct vop_inactive_args *ap)); +static int hpfs_print __P((struct vop_print_args *ap)); +static int hpfs_reclaim __P((struct vop_reclaim_args *ap)); +static int hpfs_strategy __P((struct vop_strategy_args *ap)); +static int hpfs_access __P((struct vop_access_args *ap)); +static int hpfs_open __P((struct vop_open_args *ap)); +static int hpfs_close __P((struct vop_close_args *ap)); +static int hpfs_readdir __P((struct vop_readdir_args *ap)); +static int hpfs_lookup __P((struct vop_lookup_args *ap)); +static int hpfs_create __P((struct vop_create_args *)); +static int hpfs_remove __P((struct vop_remove_args *)); +static int hpfs_abortop __P((struct vop_abortop_args *)); +static int hpfs_bmap __P((struct vop_bmap_args *ap)); +#if defined(__FreeBSD__) +static int hpfs_getpages __P((struct vop_getpages_args *ap)); +static int hpfs_putpages __P((struct vop_putpages_args *)); +static int hpfs_fsync __P((struct vop_fsync_args *ap)); +#endif +static int hpfs_pathconf __P((struct vop_pathconf_args *ap)); + +#if defined(__FreeBSD__) +int +hpfs_getpages(ap) + struct vop_getpages_args *ap; +{ + return vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count, + ap->a_reqpage); +} + +int +hpfs_putpages(ap) + struct vop_putpages_args *ap; +{ + return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count, + ap->a_sync, ap->a_rtvals); +} + +static int +hpfs_fsync(ap) + struct vop_fsync_args /* { + struct vnode *a_vp; + struct ucred *a_cred; + int a_waitfor; + struct proc *a_p; + } */ *ap; +{ + struct vnode *vp = ap->a_vp; + int s; + struct buf *bp, *nbp; + + /* + * Flush all dirty buffers associated with a vnode. + */ +loop: + s = splbio(); + for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) { + nbp = TAILQ_NEXT(bp, b_vnbufs); + if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) + continue; + if ((bp->b_flags & B_DELWRI) == 0) + panic("hpfs_fsync: not dirty"); + bremfree(bp); + splx(s); + (void) bwrite(bp); + goto loop; + } + while (vp->v_numoutput) { + vp->v_flag |= VBWAIT; + (void) tsleep((caddr_t)&vp->v_numoutput, PRIBIO + 1, "hpfsn", 0); + } +#ifdef DIAGNOSTIC + if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) { + vprint("hpfs_fsync: dirty", vp); + goto loop; + } +#endif + splx(s); + + /* + * Write out the on-disc version of the vnode. + */ + return hpfs_update(VTOHP(vp)); +} + +#endif + +static int +hpfs_ioctl ( + struct vop_ioctl_args /* { + struct vnode *a_vp; + u_long a_command; + caddr_t a_data; + int a_fflag; + struct ucred *a_cred; + struct proc *a_p; + } */ *ap) +{ + register struct vnode *vp = ap->a_vp; + register struct hpfsnode *hp = VTOHP(vp); + int error; + + printf("hpfs_ioctl(0x%x, 0x%lx, 0x%p, 0x%x): ", + hp->h_no, ap->a_command, ap->a_data, ap->a_fflag); + + switch (ap->a_command) { + case HPFSIOCGEANUM: { + u_long eanum; + u_long passed; + struct ea *eap; + + eanum = 0; + + if (hp->h_fn.fn_ealen > 0) { + eap = (struct ea *)&(hp->h_fn.fn_int); + passed = 0; + + while (passed < hp->h_fn.fn_ealen) { + + printf("EAname: %s\n", EA_NAME(eap)); + + eanum++; + passed += sizeof(struct ea) + + eap->ea_namelen + 1 + eap->ea_vallen; + eap = (struct ea *)((caddr_t)hp->h_fn.fn_int + + passed); + } + error = 0; + } else { + error = ENOENT; + } + + printf("%lu eas\n", eanum); + + *(u_long *)ap->a_data = eanum; + + break; + } + case HPFSIOCGEASZ: { + u_long eanum; + u_long passed; + struct ea *eap; + + printf("EA%ld\n", *(u_long *)ap->a_data); + + eanum = 0; + if (hp->h_fn.fn_ealen > 0) { + eap = (struct ea *)&(hp->h_fn.fn_int); + passed = 0; + + error = ENOENT; + while (passed < hp->h_fn.fn_ealen) { + printf("EAname: %s\n", EA_NAME(eap)); + + if (eanum == *(u_long *)ap->a_data) { + *(u_long *)ap->a_data = + eap->ea_namelen + 1 + + eap->ea_vallen; + + error = 0; + break; + } + + eanum++; + passed += sizeof(struct ea) + + eap->ea_namelen + 1 + eap->ea_vallen; + eap = (struct ea *)((caddr_t)hp->h_fn.fn_int + + passed); + } + } else { + error = ENOENT; + } + + break; + } + case HPFSIOCRDEA: { + u_long eanum; + u_long passed; + struct hpfs_rdea *rdeap; + struct ea *eap; + + rdeap = (struct hpfs_rdea *)ap->a_data; + printf("EA%ld\n", rdeap->ea_no); + + eanum = 0; + if (hp->h_fn.fn_ealen > 0) { + eap = (struct ea *)&(hp->h_fn.fn_int); + passed = 0; + + error = ENOENT; + while (passed < hp->h_fn.fn_ealen) { + printf("EAname: %s\n", EA_NAME(eap)); + + if (eanum == rdeap->ea_no) { + rdeap->ea_sz = eap->ea_namelen + 1 + + eap->ea_vallen; + copyout(EA_NAME(eap),rdeap->ea_data, + rdeap->ea_sz); + error = 0; + break; + } + + eanum++; + passed += sizeof(struct ea) + + eap->ea_namelen + 1 + eap->ea_vallen; + eap = (struct ea *)((caddr_t)hp->h_fn.fn_int + + passed); + } + } else { + error = ENOENT; + } + + break; + } + default: + error = EOPNOTSUPP; + break; + } + return (error); +} + +/* + * Map file offset to disk offset. + */ +int +hpfs_bmap(ap) + struct vop_bmap_args /* { + struct vnode *a_vp; + daddr_t a_bn; + struct vnode **a_vpp; + daddr_t *a_bnp; + int *a_runp; + int *a_runb; + } */ *ap; +{ + register struct hpfsnode *hp = VTOHP(ap->a_vp); + int error; + + if (ap->a_vpp != NULL) + *ap->a_vpp = hp->h_devvp; +#if defined(__FreeBSD__) + if (ap->a_runb != NULL) + *ap->a_runb = 0; +#endif + if (ap->a_bnp == NULL) + return (0); + + dprintf(("hpfs_bmap(0x%x, 0x%x): ",hp->h_no, ap->a_bn)); + + error = hpfs_hpbmap (hp, ap->a_bn, ap->a_bnp, ap->a_runp); + + return (error); +} + +static int +hpfs_read(ap) + struct vop_read_args /* { + struct vnode *a_vp; + struct uio *a_uio; + int a_ioflag; + struct ucred *a_cred; + } */ *ap; +{ + register struct vnode *vp = ap->a_vp; + register struct hpfsnode *hp = VTOHP(vp); + struct uio *uio = ap->a_uio; + struct buf *bp; + u_int xfersz, toread; + u_int off; + daddr_t lbn, bn; + int resid; + int runl; + int error = 0; + + resid = min (uio->uio_resid, hp->h_fn.fn_size - uio->uio_offset); + + dprintf(("hpfs_read(0x%x, off: %d resid: %d, segflg: %d): [resid: 0x%x]\n",hp->h_no,(u_int32_t)uio->uio_offset,uio->uio_resid,uio->uio_segflg, resid)); + + while (resid) { + lbn = uio->uio_offset >> DEV_BSHIFT; + off = uio->uio_offset & (DEV_BSIZE - 1); + dprintf(("hpfs_read: resid: 0x%x lbn: 0x%x off: 0x%x\n", + uio->uio_resid, lbn, off)); + error = hpfs_hpbmap(hp, lbn, &bn, &runl); + if (error) + return (error); + + toread = min(off + resid, min(DFLTPHYS, (runl+1)*DEV_BSIZE)); + xfersz = (toread + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1); + dprintf(("hpfs_read: bn: 0x%x (0x%x) toread: 0x%x (0x%x)\n", + bn, runl, toread, xfersz)); + + if (toread == 0) + break; + + error = bread(hp->h_devvp, bn, xfersz, NOCRED, &bp); + if (error) { + brelse(bp); + break; + } + + error = uiomove(bp->b_data + off, toread - off, uio); + if(error) { + brelse(bp); + break; + } + brelse(bp); + resid -= toread; + } + dprintf(("hpfs_read: successful\n")); + return (error); +} + +static int +hpfs_write(ap) + struct vop_write_args /* { + struct vnode *a_vp; + struct uio *a_uio; + int a_ioflag; + struct ucred *a_cred; + } */ *ap; +{ + register struct vnode *vp = ap->a_vp; + register struct hpfsnode *hp = VTOHP(vp); + struct uio *uio = ap->a_uio; + struct buf *bp; + u_int xfersz, towrite; + u_int off; + daddr_t lbn, bn; + int runl; + int error = 0; + + dprintf(("hpfs_write(0x%x, off: %d resid: %d, segflg: %d):\n",hp->h_no,(u_int32_t)uio->uio_offset,uio->uio_resid,uio->uio_segflg)); + + if (ap->a_ioflag & IO_APPEND) { + dprintf(("hpfs_write: APPEND mode\n")); + uio->uio_offset = hp->h_fn.fn_size; + } + if (uio->uio_offset + uio->uio_resid > hp->h_fn.fn_size) { + error = hpfs_extend (hp, uio->uio_offset + uio->uio_resid); + if (error) { + printf("hpfs_write: hpfs_extend FAILED %d\n", error); + return (error); + } + } + + while (uio->uio_resid) { + lbn = uio->uio_offset >> DEV_BSHIFT; + off = uio->uio_offset & (DEV_BSIZE - 1); + dprintf(("hpfs_write: resid: 0x%x lbn: 0x%x off: 0x%x\n", + uio->uio_resid, lbn, off)); + error = hpfs_hpbmap(hp, lbn, &bn, &runl); + if (error) + return (error); + + towrite = min(off + uio->uio_resid, min(DFLTPHYS, (runl+1)*DEV_BSIZE)); + xfersz = (towrite + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1); + dprintf(("hpfs_write: bn: 0x%x (0x%x) towrite: 0x%x (0x%x)\n", + bn, runl, towrite, xfersz)); + + if ((off == 0) && (towrite == xfersz)) { + bp = getblk(hp->h_devvp, bn, xfersz, 0, 0); + clrbuf(bp); + } else { + error = bread(hp->h_devvp, bn, xfersz, NOCRED, &bp); + if (error) { + brelse(bp); + return (error); + } + } + + error = uiomove(bp->b_data + off, towrite - off, uio); + if(error) { + brelse(bp); + return (error); + } + + if (ap->a_ioflag & IO_SYNC) + bwrite(bp); + else + bawrite(bp); + } + + dprintf(("hpfs_write: successful\n")); + return (0); +} + +static int +hpfs_bypass(ap) + struct vop_generic_args /* { + struct vnodeop_desc *a_desc; + <other random data follows, presumably> + } */ *ap; +{ + dprintf(("hpfs_bypass: %s\n", ap->a_desc->vdesc_name)); + return (0); +} + +/* + * XXXXX do we need hpfsnode locking inside? + */ +static int +hpfs_getattr(ap) + struct vop_getattr_args /* { + struct vnode *a_vp; + struct vattr *a_vap; + struct ucred *a_cred; + struct proc *a_p; + } */ *ap; +{ + register struct vnode *vp = ap->a_vp; + register struct hpfsnode *hp = VTOHP(vp); + register struct vattr *vap = ap->a_vap; + int error; + + dprintf(("hpfs_getattr(0x%x):\n", hp->h_no)); + +#if defined(__FreeBSD__) + vap->va_fsid = dev2udev(hp->h_dev); +#else /* defined(__NetBSD__) */ + vap->va_fsid = ip->i_dev; +#endif + vap->va_fileid = hp->h_no; + vap->va_mode = hp->h_mode; + vap->va_nlink = 1; + vap->va_uid = hp->h_uid; + vap->va_gid = hp->h_gid; + vap->va_rdev = 0; /* XXX UNODEV ? */ + vap->va_size = hp->h_fn.fn_size; + vap->va_bytes = ((hp->h_fn.fn_size + DEV_BSIZE-1) & ~(DEV_BSIZE-1)) + + DEV_BSIZE; + + if (!(hp->h_flag & H_PARVALID)) { + error = hpfs_validateparent(hp); + if (error) + return (error); + } + vap->va_atime = hpfstimetounix(hp->h_atime); + vap->va_mtime = hpfstimetounix(hp->h_mtime); + vap->va_ctime = hpfstimetounix(hp->h_ctime); + + vap->va_flags = 0; + vap->va_gen = 0; + vap->va_blocksize = DEV_BSIZE; + vap->va_type = vp->v_type; + vap->va_filerev = 0; + + return (0); +} + +/* + * XXXXX do we need hpfsnode locking inside? + */ +static int +hpfs_setattr(ap) + struct vop_setattr_args /* { + struct vnode *a_vp; + struct vattr *a_vap; + struct ucred *a_cred; + struct proc *a_p; + } */ *ap; +{ + struct vnode *vp = ap->a_vp; + struct hpfsnode *hp = VTOHP(vp); + struct vattr *vap = ap->a_vap; + struct ucred *cred = ap->a_cred; + struct proc *p = ap->a_p; + int error; + + dprintf(("hpfs_setattr(0x%x):\n", hp->h_no)); + + /* + * Check for unsettable attributes. + */ + if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) || + (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) || + (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) || + (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) { + dprintf(("hpfs_setattr: changing nonsettable attr\n")); + return (EINVAL); + } + + /* Can't change flags XXX Could be implemented */ + if (vap->va_flags != VNOVAL) { + printf("hpfs_setattr: FLAGS CANNOT BE SET\n"); + return (EINVAL); + } + + /* Can't change uid/gid XXX Could be implemented */ + if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) { + printf("hpfs_setattr: UID/GID CANNOT BE SET\n"); + return (EINVAL); + } + + /* Can't change mode XXX Could be implemented */ + if (vap->va_mode != (mode_t)VNOVAL) { + printf("hpfs_setattr: MODE CANNOT BE SET\n"); + return (EINVAL); + } + + /* Update times */ + if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) { + if (vp->v_mount->mnt_flag & MNT_RDONLY) + return (EROFS); + if (cred->cr_uid != hp->h_uid && + (error = suser_xxx(cred, p, PRISON_ROOT)) && + ((vap->va_vaflags & VA_UTIMES_NULL) == 0 || + (error = VOP_ACCESS(vp, VWRITE, cred, p)))) + return (error); + if (vap->va_atime.tv_sec != VNOVAL) + hp->h_atime = vap->va_atime.tv_sec; + if (vap->va_mtime.tv_sec != VNOVAL) + hp->h_mtime = vap->va_mtime.tv_sec; + + hp->h_flag |= H_PARCHANGE; + } + + if (vap->va_size != VNOVAL) { + switch (vp->v_type) { + case VDIR: + return (EISDIR); + case VREG: + if (vp->v_mount->mnt_flag & MNT_RDONLY) + return (EROFS); + break; + default: + printf("hpfs_setattr: WRONG v_type\n"); + return (EINVAL); + } + + if (vap->va_size < hp->h_fn.fn_size) { +#if defined(__FreeBSD__) + error = vtruncbuf(vp, cred, p, vap->va_size, DEV_BSIZE); + if (error) + return (error); +#else /* defined(__NetBSD__) */ +#error Need alternation for vtruncbuf() +#endif + error = hpfs_truncate(hp, vap->va_size); + if (error) + return (error); + + } else if (vap->va_size > hp->h_fn.fn_size) { +#if defined(__FreeBSD__) + vnode_pager_setsize(vp, vap->va_size); +#endif + error = hpfs_extend(hp, vap->va_size); + if (error) + return (error); + } + } + + return (0); +} + +/* + * Last reference to an node. If necessary, write or delete it. + */ +int +hpfs_inactive(ap) + struct vop_inactive_args /* { + struct vnode *a_vp; + } */ *ap; +{ + register struct vnode *vp = ap->a_vp; + register struct hpfsnode *hp = VTOHP(vp); + int error; + + dprintf(("hpfs_inactive(0x%x): \n", hp->h_no)); + + if (hp->h_flag & H_CHANGE) { + dprintf(("hpfs_inactive: node changed, update\n")); + error = hpfs_update (hp); + if (error) + return (error); + } + + if (hp->h_flag & H_PARCHANGE) { + dprintf(("hpfs_inactive: parent node changed, update\n")); + error = hpfs_updateparent (hp); + if (error) + return (error); + } + + if (prtactive && vp->v_usecount != 0) + vprint("hpfs_inactive: pushing active", vp); + + if (hp->h_flag & H_INVAL) { + VOP__UNLOCK(vp,0,ap->a_p); +#if defined(__FreeBSD__) + vrecycle(vp, (struct simplelock *)0, ap->a_p); +#else /* defined(__NetBSD__) */ + vgone(vp); +#endif + return (0); + } + + VOP__UNLOCK(vp,0,ap->a_p); + return (0); +} + +/* + * Reclaim an inode so that it can be used for other purposes. + */ +int +hpfs_reclaim(ap) + struct vop_reclaim_args /* { + struct vnode *a_vp; + } */ *ap; +{ + register struct vnode *vp = ap->a_vp; + register struct hpfsnode *hp = VTOHP(vp); + + dprintf(("hpfs_reclaim(0x%x0): \n", hp->h_no)); + + hpfs_hphashrem(hp); + + /* Purge old data structures associated with the inode. */ + cache_purge(vp); + if (hp->h_devvp) { + vrele(hp->h_devvp); + hp->h_devvp = NULL; + } + + vp->v_data = NULL; + + FREE(hp, M_HPFSNO); + + return (0); +} + +static int +hpfs_print(ap) + struct vop_print_args /* { + struct vnode *a_vp; + } */ *ap; +{ + register struct vnode *vp = ap->a_vp; + register struct hpfsnode *hp = VTOHP(vp); + + printf("tag VT_HPFS, ino 0x%x",hp->h_no); + lockmgr_printinfo(&hp->h_lock); + printf("\n"); + return (0); +} + +/* + * Calculate the logical to physical mapping if not done already, + * then call the device strategy routine. + * + * In order to be able to swap to a file, the VOP_BMAP operation may not + * deadlock on memory. See hpfs_bmap() for details. XXXXXXX (not impl) + */ +int +hpfs_strategy(ap) + struct vop_strategy_args /* { + struct buf *a_bp; + } */ *ap; +{ + register struct buf *bp = ap->a_bp; + register struct vnode *vp = ap->a_vp; + struct vnode *nvp; + int error; + + dprintf(("hpfs_strategy(): \n")); + + if (vp->v_type == VBLK || vp->v_type == VCHR) + panic("hpfs_strategy: spec"); + if (bp->b_blkno == bp->b_lblkno) { + error = VOP_BMAP(vp, bp->b_lblkno, &nvp, &bp->b_blkno, NULL, NULL); + if (error) { + printf("hpfs_strategy: VOP_BMAP FAILED %d\n", error); + bp->b_error = error; + bp->b_flags |= B_ERROR; + biodone(bp); + return (error); + } + if ((long)bp->b_blkno == -1) + vfs_bio_clrbuf(bp); + } + if ((long)bp->b_blkno == -1) { + biodone(bp); + return (0); + } + bp->b_dev = nvp->v_rdev; + VOP_STRATEGY(nvp, bp); + return (0); +} + +/* + * XXXXX do we need hpfsnode locking inside? + */ +int +hpfs_access(ap) + struct vop_access_args /* { + struct vnode *a_vp; + int a_mode; + struct ucred *a_cred; + struct proc *a_p; + } */ *ap; +{ + struct vnode *vp = ap->a_vp; + struct hpfsnode *hp = VTOHP(vp); + struct ucred *cred = ap->a_cred; + mode_t mask, mode = ap->a_mode; + register gid_t *gp; + int i; + + dprintf(("hpfs_access(0x%x):\n", hp->h_no)); + + /* + * Disallow write attempts on read-only file systems; + * unless the file is a socket, fifo, or a block or + * character device resident on the file system. + */ + if (mode & VWRITE) { + switch ((int)vp->v_type) { + case VDIR: + case VLNK: + case VREG: + if (vp->v_mount->mnt_flag & MNT_RDONLY) + return (EROFS); + break; + } + } + + /* Otherwise, user id 0 always gets access. */ + if (cred->cr_uid == 0) + return (0); + + mask = 0; + + /* Otherwise, check the owner. */ + if (cred->cr_uid == hp->h_uid) { + if (mode & VEXEC) + mask |= S_IXUSR; + if (mode & VREAD) + mask |= S_IRUSR; + if (mode & VWRITE) + mask |= S_IWUSR; + return ((hp->h_mode & mask) == mask ? 0 : EACCES); + } + + /* Otherwise, check the groups. */ + for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++) + if (hp->h_gid == *gp) { + if (mode & VEXEC) + mask |= S_IXGRP; + if (mode & VREAD) + mask |= S_IRGRP; + if (mode & VWRITE) + mask |= S_IWGRP; + return ((hp->h_mode & mask) == mask ? 0 : EACCES); + } + + /* Otherwise, check everyone else. */ + if (mode & VEXEC) + mask |= S_IXOTH; + if (mode & VREAD) + mask |= S_IROTH; + if (mode & VWRITE) + mask |= S_IWOTH; + return ((hp->h_mode & mask) == mask ? 0 : EACCES); +} + +/* + * Open called. + * + * Nothing to do. + */ +/* ARGSUSED */ +static int +hpfs_open(ap) + struct vop_open_args /* { + struct vnode *a_vp; + int a_mode; + struct ucred *a_cred; + struct proc *a_p; + } */ *ap; +{ +#if HPFS_DEBUG + register struct vnode *vp = ap->a_vp; + register struct hpfsnode *hp = VTOHP(vp); + + printf("hpfs_open(0x%x):\n",hp->h_no); +#endif + + /* + * Files marked append-only must be opened for appending. + */ + + return (0); +} + +/* + * Close called. + * + * Update the times on the inode. + */ +/* ARGSUSED */ +static int +hpfs_close(ap) + struct vop_close_args /* { + struct vnode *a_vp; + int a_fflag; + struct ucred *a_cred; + struct proc *a_p; + } */ *ap; +{ +#if HPFS_DEBUG + register struct vnode *vp = ap->a_vp; + register struct hpfsnode *hp = VTOHP(vp); + + printf("hpfs_close: %d\n",hp->h_no); +#endif + + return (0); +} + +static int +hpfs_de_uiomove ( + struct hpfsmount *hpmp, + struct hpfsdirent *dep, + struct uio *uio) +{ + struct dirent cde; + int i, error; + + dprintf(("[no: 0x%x, size: %d, name: %2d:%.*s, flag: 0x%x] ", + dep->de_fnode, dep->de_size, dep->de_namelen, + dep->de_namelen, dep->de_name, dep->de_flag)); + + /*strncpy(cde.d_name, dep->de_name, dep->de_namelen);*/ + for (i=0; i<dep->de_namelen; i++) + cde.d_name[i] = hpfs_d2u(hpmp, dep->de_name[i]); + + cde.d_name[dep->de_namelen] = '\0'; + cde.d_namlen = dep->de_namelen; + cde.d_fileno = dep->de_fnode; + cde.d_type = (dep->de_flag & DE_DIR) ? DT_DIR : DT_REG; + cde.d_reclen = sizeof(struct dirent); + + error = uiomove((char *)&cde, sizeof(struct dirent), uio); + if (error) + return (error); + + dprintf(("[0x%x] ", uio->uio_resid)); + return (error); +} + + +static struct dirent hpfs_de_dot = + { 0, sizeof(struct dirent), DT_DIR, 1, "." }; +static struct dirent hpfs_de_dotdot = + { 0, sizeof(struct dirent), DT_DIR, 2, ".." }; +int +hpfs_readdir(ap) + struct vop_readdir_args /* { + struct vnode *a_vp; + struct uio *a_uio; + struct ucred *a_cred; + int *a_ncookies; + u_int **cookies; + } */ *ap; +{ + register struct vnode *vp = ap->a_vp; + register struct hpfsnode *hp = VTOHP(vp); + struct hpfsmount *hpmp = hp->h_hpmp; + struct uio *uio = ap->a_uio; + int ncookies = 0, i, num, cnum; + int error = 0; + off_t off; + struct buf *bp; + struct dirblk *dp; + struct hpfsdirent *dep; + lsn_t olsn; + lsn_t lsn; + int level; + + dprintf(("hpfs_readdir(0x%x, 0x%x, 0x%x): ",hp->h_no,(u_int32_t)uio->uio_offset,uio->uio_resid)); + + off = uio->uio_offset; + + if( uio->uio_offset < sizeof(struct dirent) ) { + dprintf((". faked, ")); + hpfs_de_dot.d_fileno = hp->h_no; + error = uiomove((char *)&hpfs_de_dot,sizeof(struct dirent),uio); + if(error) { + return (error); + } + + ncookies ++; + } + + if( uio->uio_offset < 2 * sizeof(struct dirent) ) { + dprintf((".. faked, ")); + hpfs_de_dotdot.d_fileno = hp->h_fn.fn_parent; + + error = uiomove((char *)&hpfs_de_dotdot, sizeof(struct dirent), + uio); + if(error) { + return (error); + } + + ncookies ++; + } + + num = uio->uio_offset / sizeof(struct dirent) - 2; + cnum = 0; + + lsn = ((alleaf_t *)hp->h_fn.fn_abd)->al_lsn; + + olsn = 0; + level = 1; + +dive: + dprintf(("[dive 0x%x] ", lsn)); + error = bread(hp->h_devvp, lsn, D_BSIZE, NOCRED, &bp); + if (error) { + brelse(bp); + return (error); + } + + dp = (struct dirblk *) bp->b_data; + if (dp->d_magic != D_MAGIC) { + printf("hpfs_readdir: MAGIC DOESN'T MATCH\n"); + brelse(bp); + return (EINVAL); + } + + dep = D_DIRENT(dp); + + if (olsn) { + dprintf(("[restore 0x%x] ", olsn)); + + while(!(dep->de_flag & DE_END) ) { + if((dep->de_flag & DE_DOWN) && + (olsn == DE_DOWNLSN(dep))) + break; + dep = (hpfsdirent_t *)((caddr_t)dep + dep->de_reclen); + } + + if((dep->de_flag & DE_DOWN) && (olsn == DE_DOWNLSN(dep))) { + if (dep->de_flag & DE_END) + goto blockdone; + + if (!(dep->de_flag & DE_SPECIAL)) { + if (num <= cnum) { + if (uio->uio_resid < sizeof(struct dirent)) { + brelse(bp); + dprintf(("[resid] ")); + goto readdone; + } + + error = hpfs_de_uiomove(hpmp, dep, uio); + if (error) { + brelse (bp); + return (error); + } + ncookies++; + + if (uio->uio_resid < sizeof(struct dirent)) { + brelse(bp); + dprintf(("[resid] ")); + goto readdone; + } + } + cnum++; + } + + dep = (hpfsdirent_t *)((caddr_t)dep + dep->de_reclen); + } else { + printf("hpfs_readdir: ERROR! oLSN not found\n"); + brelse(bp); + return (EINVAL); + } + } + + olsn = 0; + + while(!(dep->de_flag & DE_END)) { + if(dep->de_flag & DE_DOWN) { + lsn = DE_DOWNLSN(dep); + brelse(bp); + level++; + goto dive; + } + + if (!(dep->de_flag & DE_SPECIAL)) { + if (num <= cnum) { + if (uio->uio_resid < sizeof(struct dirent)) { + brelse(bp); + dprintf(("[resid] ")); + goto readdone; + } + + error = hpfs_de_uiomove(hpmp, dep, uio); + if (error) { + brelse (bp); + return (error); + } + ncookies++; + + if (uio->uio_resid < sizeof(struct dirent)) { + brelse(bp); + dprintf(("[resid] ")); + goto readdone; + } + } + cnum++; + } + + dep = (hpfsdirent_t *)((caddr_t)dep + dep->de_reclen); + } + + if(dep->de_flag & DE_DOWN) { + dprintf(("[enddive] ")); + lsn = DE_DOWNLSN(dep); + brelse(bp); + level++; + goto dive; + } + +blockdone: + dprintf(("[EOB] ")); + olsn = lsn; + lsn = dp->d_parent; + brelse(bp); + level--; + + dprintf(("[level %d] ", level)); + + if (level > 0) + goto dive; /* undive really */ + + if (ap->a_eofflag) { + dprintf(("[EOF] ")); + *ap->a_eofflag = 1; + } + +readdone: + dprintf(("[readdone]\n")); + if (!error && ap->a_ncookies != NULL) { + struct dirent* dpStart; + struct dirent* dp; +#if defined(__FreeBSD__) + u_long *cookies; + u_long *cookiep; +#else /* defined(__NetBSD__) */ + off_t *cookies; + off_t *cookiep; +#endif + + dprintf(("%d cookies, ",ncookies)); + if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) + panic("hpfs_readdir: unexpected uio from NFS server"); + dpStart = (struct dirent *) + ((caddr_t)uio->uio_iov->iov_base - + (uio->uio_offset - off)); +#if defined(__FreeBSD__) + MALLOC(cookies, u_long *, ncookies * sizeof(u_long), + M_TEMP, M_WAITOK); +#else /* defined(__NetBSD__) */ + MALLOC(cookies, off_t *, ncookies * sizeof(off_t), + M_TEMP, M_WAITOK); +#endif + for (dp = dpStart, cookiep = cookies, i=0; + i < ncookies; + dp = (struct dirent *)((caddr_t) dp + dp->d_reclen), i++) { + off += dp->d_reclen; + *cookiep++ = (u_int) off; + } + *ap->a_ncookies = ncookies; + *ap->a_cookies = cookies; + } + + return (0); +} + +int +hpfs_lookup(ap) + struct vop_lookup_args /* { + struct vnode *a_dvp; + struct vnode **a_vpp; + struct componentname *a_cnp; + } */ *ap; +{ + register struct vnode *dvp = ap->a_dvp; + register struct hpfsnode *dhp = VTOHP(dvp); + struct hpfsmount *hpmp = dhp->h_hpmp; + struct componentname *cnp = ap->a_cnp; + struct ucred *cred = cnp->cn_cred; + int error; + int nameiop = cnp->cn_nameiop; + int flags = cnp->cn_flags; + int lockparent = flags & LOCKPARENT; +#if HPFS_DEBUG + int wantparent = flags & (LOCKPARENT|WANTPARENT); +#endif + dprintf(("hpfs_lookup(0x%x, %s, %ld, %d, %d): \n", + dhp->h_no, cnp->cn_nameptr, cnp->cn_namelen, + lockparent, wantparent)); + + if (nameiop != CREATE && nameiop != DELETE && nameiop != LOOKUP) { + printf("hpfs_lookup: LOOKUP, DELETE and CREATE are only supported\n"); + return (EOPNOTSUPP); + } + + error = VOP_ACCESS(dvp, VEXEC, cred, cnp->cn_proc); + if(error) + return (error); + + if( (cnp->cn_namelen == 1) && + !strncmp(cnp->cn_nameptr,".",1) ) { + dprintf(("hpfs_lookup(0x%x,...): . faked\n",dhp->h_no)); + + VREF(dvp); + *ap->a_vpp = dvp; + + return (0); + } else if( (cnp->cn_namelen == 2) && + !strncmp(cnp->cn_nameptr,"..",2) && (flags & ISDOTDOT) ) { + dprintf(("hpfs_lookup(0x%x,...): .. faked (0x%x)\n", + dhp->h_no, dhp->h_fn.fn_parent)); + + VOP__UNLOCK(dvp,0,cnp->cn_proc); + + error = VFS_VGET(hpmp->hpm_mp, + dhp->h_fn.fn_parent, ap->a_vpp); + if(error) { + VOP__LOCK(dvp, 0, cnp->cn_proc); + return(error); + } + + if( lockparent && (flags & ISLASTCN) && + (error = VOP__LOCK(dvp, 0, cnp->cn_proc)) ) { + vput( *(ap->a_vpp) ); + return (error); + } + return (error); + } else { + struct buf *bp; + struct hpfsdirent *dep; + struct hpfsnode *hp; + + error = hpfs_genlookupbyname(dhp, + cnp->cn_nameptr, cnp->cn_namelen, &bp, &dep); + if (error) { + if ((error == ENOENT) && (flags & ISLASTCN) && + (nameiop == CREATE || nameiop == RENAME)) { + if(!lockparent) + VOP__UNLOCK(dvp, 0, cnp->cn_proc); + cnp->cn_flags |= SAVENAME; + return (EJUSTRETURN); + } + + return (error); + } + + dprintf(("hpfs_lookup: fnode: 0x%x, CPID: 0x%x\n", + dep->de_fnode, dep->de_cpid)); + + if (nameiop == DELETE && (flags & ISLASTCN)) { + error = VOP_ACCESS(dvp, VWRITE, cred, cnp->cn_proc); + if (error) { + brelse(bp); + return (error); + } + } + + if (dhp->h_no == dep->de_fnode) { + brelse(bp); + VREF(dvp); + *ap->a_vpp = dvp; + return (0); + } + + error = VFS_VGET(hpmp->hpm_mp, dep->de_fnode, ap->a_vpp); + if (error) { + printf("hpfs_lookup: VFS_VGET FAILED %d\n", error); + brelse(bp); + return(error); + } + + hp = VTOHP(*ap->a_vpp); + + hp->h_mtime = dep->de_mtime; + hp->h_ctime = dep->de_ctime; + hp->h_atime = dep->de_atime; + bcopy(dep->de_name, hp->h_name, dep->de_namelen); + hp->h_name[dep->de_namelen] = '\0'; + hp->h_namelen = dep->de_namelen; + hp->h_flag |= H_PARVALID; + + brelse(bp); + + if(!lockparent || !(flags & ISLASTCN)) + VOP__UNLOCK(dvp, 0, cnp->cn_proc); + if ((flags & MAKEENTRY) && + (!(flags & ISLASTCN) || + (nameiop != DELETE && nameiop != CREATE))) + cache_enter(dvp, *ap->a_vpp, cnp); + } + return (error); +} + +int +hpfs_remove(ap) + struct vop_remove_args /* { + struct vnode *a_dvp; + struct vnode *a_vp; + struct componentname *a_cnp; + } */ *ap; +{ + int error; + + dprintf(("hpfs_remove(0x%x, %s, %ld): \n", VTOHP(ap->a_vp)->h_no, + ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen)); + + if (ap->a_vp->v_type == VDIR) + return (EPERM); + + error = hpfs_removefnode (ap->a_dvp, ap->a_vp, ap->a_cnp); + return (error); +} + +int +hpfs_create(ap) + struct vop_create_args /* { + struct vnode *a_dvp; + struct vnode **a_vpp; + struct componentname *a_cnp; + struct vattr *a_vap; + } */ *ap; +{ + int error; + + dprintf(("hpfs_create(0x%x, %s, %ld): \n", VTOHP(ap->a_dvp)->h_no, + ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen)); + + if (!(ap->a_cnp->cn_flags & HASBUF)) + panic ("hpfs_create: no name\n"); + + error = hpfs_makefnode (ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap); + + if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF) + zfree(namei_zone, ap->a_cnp->cn_pnbuf); + + return (error); +} + +static int +hpfs_abortop(ap) + struct vop_abortop_args /* { + struct vnode *a_dvp; + struct componentname *a_cnp; + } */ *ap; +{ + dprintf(("hpfs_abortop(0x%x, %s, %ld): \n", VTOHP(ap->a_dvp)->h_no, + ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen)); + + if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF) + zfree(namei_zone, ap->a_cnp->cn_pnbuf); + + return (0); +} + +/* + * Return POSIX pathconf information applicable to NTFS filesystem + */ +int +hpfs_pathconf(ap) + struct vop_pathconf_args /* { + struct vnode *a_vp; + int a_name; + register_t *a_retval; + } */ *ap; +{ + switch (ap->a_name) { + case _PC_LINK_MAX: + *ap->a_retval = 1; + return (0); + case _PC_NAME_MAX: + *ap->a_retval = HPFS_MAXFILENAME; + return (0); + case _PC_PATH_MAX: + *ap->a_retval = PATH_MAX; + return (0); + case _PC_CHOWN_RESTRICTED: + *ap->a_retval = 1; + return (0); + case _PC_NO_TRUNC: + *ap->a_retval = 0; + return (0); +#if defined(__NetBSD__) + case _PC_SYNC_IO: + *ap->a_retval = 1; + return (0); + case _PC_FILESIZEBITS: + *ap->a_retval = 32; + return (0); +#endif + default: + return (EINVAL); + } + /* NOTREACHED */ +} + + +/* + * Global vfs data structures + */ +vop_t **hpfs_vnodeop_p; +#if defined(__FreeBSD__) +struct vnodeopv_entry_desc hpfs_vnodeop_entries[] = { + { &vop_default_desc, (vop_t *)hpfs_bypass }, + + { &vop_getattr_desc, (vop_t *)hpfs_getattr }, + { &vop_setattr_desc, (vop_t *)hpfs_setattr }, + { &vop_inactive_desc, (vop_t *)hpfs_inactive }, + { &vop_reclaim_desc, (vop_t *)hpfs_reclaim }, + { &vop_print_desc, (vop_t *)hpfs_print }, + { &vop_create_desc, (vop_t *)hpfs_create }, + { &vop_remove_desc, (vop_t *)hpfs_remove }, + { &vop_abortop_desc, (vop_t *)hpfs_abortop }, + { &vop_islocked_desc, (vop_t *)vop_stdislocked }, + { &vop_unlock_desc, (vop_t *)vop_stdunlock }, + { &vop_lock_desc, (vop_t *)vop_stdlock }, + { &vop_cachedlookup_desc, (vop_t *)hpfs_lookup }, + { &vop_lookup_desc, (vop_t *)vfs_cache_lookup }, + { &vop_access_desc, (vop_t *)hpfs_access }, + { &vop_close_desc, (vop_t *)hpfs_close }, + { &vop_open_desc, (vop_t *)hpfs_open }, + { &vop_readdir_desc, (vop_t *)hpfs_readdir }, + { &vop_fsync_desc, (vop_t *)hpfs_fsync }, + { &vop_bmap_desc, (vop_t *)hpfs_bmap }, + { &vop_getpages_desc, (vop_t *) hpfs_getpages }, + { &vop_putpages_desc, (vop_t *) hpfs_putpages }, + { &vop_strategy_desc, (vop_t *)hpfs_strategy }, + { &vop_bwrite_desc, (vop_t *)vop_stdbwrite }, + { &vop_read_desc, (vop_t *)hpfs_read }, + { &vop_write_desc, (vop_t *)hpfs_write }, + { &vop_ioctl_desc, (vop_t *)hpfs_ioctl }, + { &vop_pathconf_desc, (vop_t *)hpfs_pathconf }, + { NULL, NULL } +}; + +static +struct vnodeopv_desc hpfs_vnodeop_opv_desc = + { &hpfs_vnodeop_p, hpfs_vnodeop_entries }; + +VNODEOP_SET(hpfs_vnodeop_opv_desc); +#else /* defined(__NetBSD__) */ +struct vnodeopv_entry_desc ntfs_vnodeop_entries[] = { + { &vop_default_desc, (vop_t *) hpfs_bypass }, + { &vop_lookup_desc, (vop_t *) hpfs_lookup }, /* lookup */ + { &vop_create_desc, genfs_eopnotsupp }, /* create */ + { &vop_mknod_desc, genfs_eopnotsupp }, /* mknod */ + { &vop_open_desc, (vop_t *) hpfs_open }, /* open */ + { &vop_close_desc,(vop_t *) hpfs_close }, /* close */ + { &vop_access_desc, (vop_t *) hpfs_access }, /* access */ + { &vop_getattr_desc, (vop_t *) hpfs_getattr }, /* getattr */ + { &vop_setattr_desc, genfs_eopnotsupp }, /* setattr */ + { &vop_read_desc, (vop_t *) hpfs_read }, /* read */ + { &vop_write_desc, (vop_t *) hpfs_write }, /* write */ + { &vop_lease_desc, genfs_lease_check }, /* lease */ + { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */ + { &vop_ioctl_desc, genfs_enoioctl }, /* ioctl */ + { &vop_poll_desc, genfs_poll }, /* poll */ + { &vop_revoke_desc, genfs_revoke }, /* revoke */ + { &vop_mmap_desc, genfs_eopnotsupp }, /* mmap */ + { &vop_fsync_desc, genfs_fsync }, /* fsync */ + { &vop_seek_desc, genfs_seek }, /* seek */ + { &vop_remove_desc, genfs_eopnotsupp }, /* remove */ + { &vop_link_desc, genfs_eopnotsupp }, /* link */ + { &vop_rename_desc, genfs_eopnotsupp }, /* rename */ + { &vop_mkdir_desc, genfs_eopnotsupp }, /* mkdir */ + { &vop_rmdir_desc, genfs_eopnotsupp }, /* rmdir */ + { &vop_symlink_desc, genfs_eopnotsupp }, /* symlink */ + { &vop_readdir_desc, (vop_t *) hpfs_readdir }, /* readdir */ + { &vop_readlink_desc, genfs_eopnotsupp }, /* readlink */ + { &vop_abortop_desc, genfs_abortop }, /* abortop */ + { &vop_inactive_desc, (vop_t *) hpfs_inactive }, /* inactive */ + { &vop_reclaim_desc, (vop_t *) hpfs_reclaim }, /* reclaim */ + { &vop_lock_desc, genfs_lock }, /* lock */ + { &vop_unlock_desc, genfs_unlock }, /* unlock */ + { &vop_bmap_desc, (vop_t *) hpfs_bmap }, /* bmap */ + { &vop_strategy_desc, (vop_t *) hpfs_strategy }, /* strategy */ + { &vop_print_desc, (vop_t *) hpfs_print }, /* print */ + { &vop_islocked_desc, genfs_islocked }, /* islocked */ + { &vop_pathconf_desc, hpfs_pathconf }, /* pathconf */ + { &vop_advlock_desc, genfs_nullop }, /* advlock */ + { &vop_blkatoff_desc, genfs_eopnotsupp }, /* blkatoff */ + { &vop_valloc_desc, genfs_eopnotsupp }, /* valloc */ + { &vop_reallocblks_desc, genfs_eopnotsupp }, /* reallocblks */ + { &vop_vfree_desc, genfs_eopnotsupp }, /* vfree */ + { &vop_truncate_desc, genfs_eopnotsupp }, /* truncate */ + { &vop_update_desc, genfs_eopnotsupp }, /* update */ + { &vop_bwrite_desc, vn_bwrite }, /* bwrite */ + { (struct vnodeop_desc *)NULL, (int (*) __P((void *)))NULL } +}; +struct vnodeopv_desc ntfs_vnodeop_opv_desc = + { &ntfs_vnodeop_p, ntfs_vnodeop_entries }; + +#endif diff --git a/sys/fs/hpfs/hpfsmount.h b/sys/fs/hpfs/hpfsmount.h new file mode 100644 index 0000000..3566776 --- /dev/null +++ b/sys/fs/hpfs/hpfsmount.h @@ -0,0 +1,39 @@ +/*- + * Copyright (c) 1998, 1999 Semen Ustimenko (semenu@FreeBSD.org) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#define HPFSMNT_TABLES 0x0001 +struct hpfs_args { + char *fspec; /* block special device to mount */ + struct export_args export; /* network export information */ + uid_t uid; /* uid that owns hpfs files */ + gid_t gid; /* gid that owns hpfs files */ + mode_t mode; /* mask to be applied for hpfs perms */ + u_long flags; /* additional flags */ + u_char d2u[0x80]; + u_char u2d[0x80]; +}; |