2 * Copyright (c) 1998, 1999 Semen Ustimenko (semenu@FreeBSD.org)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
34 #include <sys/types.h>
36 #include <sys/vnode.h>
37 #include <sys/mount.h>
38 #include <sys/namei.h>
39 #include <sys/malloc.h>
43 #include <fs/hpfs/hpfs.h>
44 #include <fs/hpfs/hpfsmount.h>
45 #include <fs/hpfs/hpfs_subr.h>
54 for (i=0; i < size; i++) {
55 csum += (u_long) *object++;
56 csum = (csum << 7) + (csum >> (25));
63 struct hpfsmount *hpmp)
68 dprintf(("hpmp_bmdeinit: "));
70 if (!(hpmp->hpm_mp->mnt_flag & MNT_RDONLY)) {
74 for (i=0; i<hpmp->hpm_dbnum; i++) {
75 dprintf(("[%d: 0x%x] ", i, hpmp->hpm_bmind[i]));
77 bp = getblk(hpmp->hpm_devvp, hpmp->hpm_bmind[i],
81 bcopy(hpmp->hpm_bitmap + BMSIZE * i, bp->b_data,
88 free(hpmp->hpm_bitmap,M_HPFSMNT);
89 free(hpmp->hpm_bmind,M_HPFSMNT);
95 * Initialize BitMap management, includes calculation of
96 * available blocks number.
100 struct hpfsmount *hpmp)
106 dprintf(("hpfs_bminit: "));
108 hpmp->hpm_dbnum = (hpmp->hpm_su.su_btotal + 0x3FFF) / 0x4000;
110 dprintf(("0x%lx data bands, ", hpmp->hpm_dbnum));
112 hpmp->hpm_bmind = malloc(hpmp->hpm_dbnum * sizeof(lsn_t),
113 M_HPFSMNT, M_WAITOK);
115 hpmp->hpm_bitmap = malloc(hpmp->hpm_dbnum * BMSIZE,
116 M_HPFSMNT, M_WAITOK);
118 error = bread(hpmp->hpm_devvp, hpmp->hpm_su.su_bitmap.lsn1,
119 ((hpmp->hpm_dbnum + 0x7F) & ~(0x7F)) << 2, NOCRED, &bp);
122 free(hpmp->hpm_bitmap, M_HPFSMNT);
123 free(hpmp->hpm_bmind, M_HPFSMNT);
124 dprintf((" error %d\n", error));
127 bcopy(bp->b_data, hpmp->hpm_bmind, hpmp->hpm_dbnum * sizeof(lsn_t));
134 for (i=0; i<hpmp->hpm_dbnum; i++) {
135 dprintf(("[%d: 0x%x] ", i, hpmp->hpm_bmind[i]));
137 error = bread(hpmp->hpm_devvp, hpmp->hpm_bmind[i],
138 BMSIZE, NOCRED, &bp);
141 free(hpmp->hpm_bitmap, M_HPFSMNT);
142 free(hpmp->hpm_bmind, M_HPFSMNT);
143 dprintf((" error %d\n", error));
146 bcopy(bp->b_data, hpmp->hpm_bitmap + BMSIZE * i, BMSIZE);
152 * Look througth BitMap and count free bits
155 for (i=0; i < hpmp->hpm_su.su_btotal >> 5; i++) {
156 register u_int32_t mask;
157 for (k=0, mask=1; k < 32; k++, mask<<=1)
158 if(((u_int32_t *)hpmp->hpm_bitmap)[i] & mask)
162 hpmp->hpm_bavail = dbavail;
169 struct hpfsmount *hpmp,
178 for (i = 0; i < ulen && i < dlen; i++) {
179 res = hpfs_toupper(hpmp, hpfs_u2d(hpmp, uname[i]), cp) -
180 hpfs_toupper(hpmp, dname[i], cp);
184 return (ulen - dlen);
189 struct hpfsmount *hpmp,
199 for (i = 0; i < str1len && i < str2len; i++) {
200 res = (int)hpfs_toupper(hpmp, ((u_char *)str1)[i], str1cp) -
201 (int)hpfs_toupper(hpmp, ((u_char *)str2)[i], str2cp);
205 return (str1len - str2len);
211 struct hpfsmount *hpmp,
212 struct cpiblk *cpibp,
213 struct cpdblk *cpdbp)
216 struct cpdsec * cpdsp;
219 error = bread(hpmp->hpm_devvp, cpibp->b_cpdsec, DEV_BSIZE, NOCRED, &bp);
225 cpdsp = (struct cpdsec *)bp->b_data;
227 for (i=cpdsp->d_cpfirst; i<cpdsp->d_cpcnt; i++) {
228 if (cpdsp->d_cpdblk[i].b_cpid == cpibp->b_cpid) {
229 bcopy(cpdsp->d_cpdblk + i, cpdbp,
230 sizeof(struct cpdblk));
245 * Initialize Code Page information management.
246 * Load all copdepages in memory.
251 struct hpfsmount *hpmp)
257 struct cpisec * cpisp;
258 struct cpiblk * cpibp;
259 struct cpdblk * cpdbp;
261 dprintf(("hpfs_cpinit: \n"));
263 error = vfs_copyopt(mp->mnt_optnew, "d2u", hpmp->hpm_d2u,
264 sizeof hpmp->hpm_d2u);
266 for (i=0x0; i<0x80;i++)
267 hpmp->hpm_d2u[i] = i + 0x80;
271 error = vfs_copyopt(mp->mnt_optnew, "u2d", hpmp->hpm_u2d,
272 sizeof hpmp->hpm_u2d);
274 for (i=0x0; i<0x80;i++)
275 hpmp->hpm_u2d[i] = i + 0x80;
279 cpicnt = hpmp->hpm_sp.sp_cpinum;
281 hpmp->hpm_cpdblk = malloc(cpicnt * sizeof(struct cpdblk),
282 M_HPFSMNT, M_WAITOK);
284 cpdbp = hpmp->hpm_cpdblk;
285 lsn = hpmp->hpm_sp.sp_cpi;
288 error = bread(hpmp->hpm_devvp, lsn, DEV_BSIZE, NOCRED, &bp);
294 cpisp = (struct cpisec *)bp->b_data;
296 cpibp = cpisp->s_cpi;
297 for (i=0; i<cpisp->s_cpicnt; i++, cpicnt --, cpdbp++, cpibp++) {
298 dprintf(("hpfs_cpinit: Country: %d, CP: %d (%d)\n",
299 cpibp->b_country, cpibp->b_cpid,
302 error = hpfs_cpload(hpmp, cpibp, cpdbp);
317 struct hpfsmount *hpmp)
319 dprintf(("hpmp_cpdeinit: "));
320 free(hpmp->hpm_cpdblk,M_HPFSMNT);
325 * Lookup for a run of blocks.
329 struct hpfsmount *hpmp,
330 u_long flags, /* 1 means we want right len blocks in run, not less */
331 lsn_t lsn, /* We want near this one */
332 u_long len, /* We want such long */
333 lsn_t *lsnp, /* We got here */
334 u_long *lenp) /* We got this long */
337 register u_int32_t mask;
343 dprintf(("hpfs_bmlookup: lsn: 0x%x, len 0x%lx | Step1\n", lsn, len));
345 if (lsn > hpmp->hpm_su.su_btotal) {
346 printf("hpfs_bmlookup: OUT OF VOLUME\n");
349 if (len > hpmp->hpm_bavail) {
350 printf("hpfs_bmlookup: OUT OF SPACE\n");
356 bitmap = (u_int32_t *)hpmp->hpm_bitmap + i;
358 if (*bitmap & mask) {
361 for (; k < 32; k++, mask<<=1) {
377 for (; i < hpmp->hpm_su.su_btotal >> 5; i++, bitmap++) {
378 for (k=0, mask=1; k < 32; k++, mask<<=1) {
397 * Lookup all bands begining from cband, lookup for first block
400 dprintf(("hpfs_bmlookup: Step2: band 0x%x (0x%lx)\n",
401 cband, hpmp->hpm_dbnum));
402 for (vcband = 0; vcband < hpmp->hpm_dbnum; vcband ++, cband++) {
403 cband = cband % hpmp->hpm_dbnum;
404 bandsz = min (hpmp->hpm_su.su_btotal - (cband << 14), 0x4000);
405 dprintf(("hpfs_bmlookup: band: %d, sz: 0x%x\n", cband, bandsz));
407 bitmap = (u_int32_t *)hpmp->hpm_bitmap + (cband << 9);
411 for (i=0; i < bandsz >> 5; i++, bitmap++) {
412 for (k=0, mask=1; k < 32; k++, mask<<=1) {
413 if (*bitmap & mask) {
418 *lsnp = (cband << 14) + (i << 5) + k;
422 if ((*lenp) && !(flags & 1)) {
433 if (cband == hpmp->hpm_dbnum - 1) {
434 if ((*lenp) && !(flags & 1)) {
446 * Lookup a single free block. XXX Need locking on BitMap operations
447 * VERY STUPID ROUTINE!!!
451 struct hpfsmount *hpmp,
457 dprintf(("hpfs_bmfblookup: "));
459 bitmap = (u_int32_t *)hpmp->hpm_bitmap;
460 for (i=0; i < hpmp->hpm_su.su_btotal >> 5; i++, bitmap++) {
463 *lp = (i << 5) + k - 1;
464 dprintf((" found: 0x%x\n",*lp));
473 * Mark contignous block of blocks.
477 struct hpfsmount *hpmp,
485 dprintf(("hpfs_bmmark(0x%x, 0x%lx, %d): \n",bn,bl, state));
487 if ((bn > hpmp->hpm_su.su_btotal) || (bn+bl > hpmp->hpm_su.su_btotal)) {
488 printf("hpfs_bmmark: MARKING OUT OF VOLUME\n");
491 bitmap = (u_int32_t *)hpmp->hpm_bitmap;
495 for (i = bn & 0x1F; (i < 0x20) && (bl > 0) ; i++, bl--) {
497 if ( *bitmap & (1 << i)) {
499 printf("hpfs_bmmark: ALREADY FREE\n");
507 if ((~(*bitmap)) & (1 << i)) {
509 printf("hpfs_bmmark: ALREADY BUSY\n");
515 *bitmap &= ~(1 << i);
527 hpfs_validateparent (
530 struct hpfsnode *dhp;
532 struct hpfsmount *hpmp = hp->h_hpmp;
535 struct hpfsdirent *dep;
539 dprintf(("hpfs_validatetimes(0x%x): [parent: 0x%x] ",
540 hp->h_no, hp->h_fn.fn_parent));
542 if (hp->h_no == hp->h_fn.fn_parent) {
545 error = VFS_VGET(hpmp->hpm_mp, hp->h_fn.fn_parent,
552 lsn = ((alleaf_t *)dhp->h_fn.fn_abd)->al_lsn;
559 dprintf(("[dive 0x%x] ", lsn));
562 error = bread(dhp->h_devvp, lsn, D_BSIZE, NOCRED, &bp);
566 dp = (struct dirblk *) bp->b_data;
567 if (dp->d_magic != D_MAGIC) {
568 printf("hpfs_validatetimes: magic doesn't match\n");
576 dprintf(("[restore 0x%x] ", olsn));
578 while(!(dep->de_flag & DE_END) ) {
579 if((dep->de_flag & DE_DOWN) &&
580 (olsn == DE_DOWNLSN(dep)))
582 dep = (hpfsdirent_t *)((caddr_t)dep + dep->de_reclen);
585 if((dep->de_flag & DE_DOWN) && (olsn == DE_DOWNLSN(dep))) {
586 if (dep->de_flag & DE_END)
589 if (hp->h_no == dep->de_fnode) {
590 dprintf(("[found] "));
594 dep = (hpfsdirent_t *)((caddr_t)dep + dep->de_reclen);
596 printf("hpfs_validatetimes: ERROR! oLSN not found\n");
604 while(!(dep->de_flag & DE_END)) {
605 if(dep->de_flag & DE_DOWN) {
606 lsn = DE_DOWNLSN(dep);
611 if (hp->h_no == dep->de_fnode) {
612 dprintf(("[found] "));
616 dep = (hpfsdirent_t *)((caddr_t)dep + dep->de_reclen);
619 if(dep->de_flag & DE_DOWN) {
620 dprintf(("[enddive] "));
621 lsn = DE_DOWNLSN(dep);
631 dprintf(("[level %d] ", level));
633 goto dive; /* undive really */
638 bcopy(dep->de_name,hp->h_name,dep->de_namelen);
639 hp->h_name[dep->de_namelen] = '\0';
640 hp->h_namelen = dep->de_namelen;
641 hp->h_ctime = dep->de_ctime;
642 hp->h_atime = dep->de_atime;
643 hp->h_mtime = dep->de_mtime;
644 hp->h_flag |= H_PARVALID;
646 dprintf(("[readdone]"));
671 * Write down changes done to parent dir, these are only times for now.
672 * hpfsnode have to be locked.
678 struct hpfsnode *dhp;
680 struct hpfsdirent *dep;
684 dprintf(("hpfs_updateparent(0x%x): \n", hp->h_no));
686 if (!(hp->h_flag & H_PARCHANGE))
689 if (!(hp->h_flag & H_PARVALID)) {
690 error = hpfs_validateparent (hp);
695 if (hp->h_no == hp->h_fn.fn_parent) {
698 error = VFS_VGET(hp->h_hpmp->hpm_mp, hp->h_fn.fn_parent,
705 error = hpfs_genlookupbyname (dhp, hp->h_name, hp->h_namelen,
711 dep->de_atime = hp->h_atime;
712 dep->de_mtime = hp->h_mtime;
713 dep->de_size = hp->h_fn.fn_size;
717 hp->h_flag &= ~H_PARCHANGE;
728 * Write down on disk changes done to fnode. hpfsnode have to be locked.
736 dprintf(("hpfs_update(0x%x): \n", hp->h_no));
738 if (!(hp->h_flag & H_CHANGE))
741 bp = getblk(hp->h_devvp, hp->h_no, FNODESIZE, 0, 0, 0);
744 bcopy (&hp->h_fn, bp->b_data, sizeof(struct fnode));
747 hp->h_flag &= ~H_CHANGE;
749 if (hp->h_flag & H_PARCHANGE)
750 return (hpfs_updateparent(hp));
756 * Truncate file to specifed size. hpfsnode have to be locked.
763 struct hpfsmount *hpmp = hp->h_hpmp;
764 lsn_t newblen, oldblen;
767 dprintf(("hpfs_truncate(0x%x, 0x%x -> 0x%lx): ",
768 hp->h_no, hp->h_fn.fn_size, size));
770 newblen = (size + DEV_BSIZE - 1) >> DEV_BSHIFT;
771 oldblen = (hp->h_fn.fn_size + DEV_BSIZE - 1) >> DEV_BSHIFT;
773 dprintf(("blen: 0x%x -> 0x%x\n", oldblen, newblen));
775 error = hpfs_truncatealblk (hpmp, &hp->h_fn.fn_ab, newblen, &pf);
779 hp->h_fn.fn_ab.ab_flag = 0;
780 hp->h_fn.fn_ab.ab_freecnt = 0x8;
781 hp->h_fn.fn_ab.ab_busycnt = 0x0;
782 hp->h_fn.fn_ab.ab_freeoff = sizeof(alblk_t);
785 hp->h_fn.fn_size = size;
787 hp->h_flag |= (H_CHANGE | H_PARCHANGE);
789 dprintf(("hpfs_truncate: successful\n"));
795 * Enlarge file to specifed size. hpfsnode have to be locked.
802 struct hpfsmount *hpmp = hp->h_hpmp;
803 lsn_t newblen, oldblen;
806 dprintf(("hpfs_extend(0x%x, 0x%x -> 0x%lx): ",
807 hp->h_no, hp->h_fn.fn_size, size));
809 if (hpmp->hpm_bavail < 0x10)
812 newblen = (size + DEV_BSIZE - 1) >> DEV_BSHIFT;
813 oldblen = (hp->h_fn.fn_size + DEV_BSIZE - 1) >> DEV_BSHIFT;
815 dprintf(("blen: 0x%x -> 0x%x\n", oldblen, newblen));
817 error = hpfs_addextent(hpmp, hp, newblen - oldblen);
819 printf("hpfs_extend: FAILED TO ADD EXTENT %d\n", error);
823 hp->h_fn.fn_size = size;
825 hp->h_flag |= (H_CHANGE | H_PARCHANGE);
827 dprintf(("hpfs_extend: successful\n"));
833 * Read AlSec structure, and check if magic is valid.
834 * You don't need to brelse buf on error.
838 struct hpfsmount *hpmp,
848 dprintf(("hpfs_breadstruct: reading at 0x%x\n", lsn));
852 error = bread(hpmp->hpm_devvp, lsn, len, NOCRED, &bp);
857 mp = (u_int32_t *) bp->b_data;
860 printf("hpfs_breadstruct: MAGIC DOESN'T MATCH (0x%08x != 0x%08x)\n",