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
26 * $FreeBSD: src/sys/fs/hpfs/hpfs_subr.c,v 1.1 1999/12/09 19:09:59 semenu Exp $
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>
42 #include <fs/hpfs/hpfs.h>
43 #include <fs/hpfs/hpfsmount.h>
44 #include <fs/hpfs/hpfs_subr.h>
53 for (i=0; i < size; i++) {
54 csum += (u_long) *object++;
55 csum = (csum << 7) + (csum >> (25));
62 struct hpfsmount *hpmp)
67 dprintf(("hpmp_bmdeinit: "));
69 if (!(hpmp->hpm_mp->mnt_flag & MNT_RDONLY)) {
73 for (i=0; i<hpmp->hpm_dbnum; i++) {
74 dprintf(("[%d: 0x%x] ", i, hpmp->hpm_bmind[i]));
76 bp = getblk(hpmp->hpm_devvp, hpmp->hpm_bmind[i],
80 bcopy(hpmp->hpm_bitmap + BMSIZE * i, bp->b_data,
87 FREE(hpmp->hpm_bitmap,M_HPFSMNT);
88 FREE(hpmp->hpm_bmind,M_HPFSMNT);
94 * Initialize BitMap management, includes calculation of
95 * available blocks number.
99 struct hpfsmount *hpmp)
105 dprintf(("hpfs_bminit: "));
107 hpmp->hpm_dbnum = (hpmp->hpm_su.su_btotal + 0x3FFF) / 0x4000;
109 dprintf(("0x%lx data bands, ", hpmp->hpm_dbnum));
111 MALLOC(hpmp->hpm_bmind, lsn_t *, hpmp->hpm_dbnum * sizeof(lsn_t),
112 M_HPFSMNT, M_WAITOK);
114 MALLOC(hpmp->hpm_bitmap, u_int8_t *, hpmp->hpm_dbnum * BMSIZE,
115 M_HPFSMNT, M_WAITOK);
117 error = bread(hpmp->hpm_devvp, hpmp->hpm_su.su_bitmap.lsn1,
118 ((hpmp->hpm_dbnum + 0x7F) & ~(0x7F)) << 2, NOCRED, &bp);
121 FREE(hpmp->hpm_bitmap, M_HPFSMNT);
122 FREE(hpmp->hpm_bmind, M_HPFSMNT);
123 dprintf((" error %d\n", error));
126 bcopy(bp->b_data, hpmp->hpm_bmind, hpmp->hpm_dbnum * sizeof(lsn_t));
133 for (i=0; i<hpmp->hpm_dbnum; i++) {
134 dprintf(("[%d: 0x%x] ", i, hpmp->hpm_bmind[i]));
136 error = bread(hpmp->hpm_devvp, hpmp->hpm_bmind[i],
137 BMSIZE, NOCRED, &bp);
140 FREE(hpmp->hpm_bitmap, M_HPFSMNT);
141 FREE(hpmp->hpm_bmind, M_HPFSMNT);
142 dprintf((" error %d\n", error));
145 bcopy(bp->b_data, hpmp->hpm_bitmap + BMSIZE * i, BMSIZE);
151 * Look througth BitMap and count free bits
154 for (i=0; i < hpmp->hpm_su.su_btotal >> 5; i++) {
155 register u_int32_t mask;
156 for (k=0, mask=1; k < 32; k++, mask<<=1)
157 if(((u_int32_t *)hpmp->hpm_bitmap)[i] & mask)
161 hpmp->hpm_bavail = dbavail;
168 struct hpfsmount *hpmp,
177 for (i = 0; i < ulen && i < dlen; i++) {
178 res = hpfs_toupper(hpmp, hpfs_u2d(hpmp, uname[i]), cp) -
179 hpfs_toupper(hpmp, dname[i], cp);
183 return (ulen - dlen);
188 struct hpfsmount *hpmp,
198 for (i = 0; i < str1len && i < str2len; i++) {
199 res = (int)hpfs_toupper(hpmp, ((u_char *)str1)[i], str1cp) -
200 (int)hpfs_toupper(hpmp, ((u_char *)str2)[i], str2cp);
204 return (str1len - str2len);
210 struct hpfsmount *hpmp,
211 struct cpiblk *cpibp,
212 struct cpdblk *cpdbp)
215 struct cpdsec * cpdsp;
218 error = bread(hpmp->hpm_devvp, cpibp->b_cpdsec, DEV_BSIZE, NOCRED, &bp);
224 cpdsp = (struct cpdsec *)bp->b_data;
226 for (i=cpdsp->d_cpfirst; i<cpdsp->d_cpcnt; i++) {
227 if (cpdsp->d_cpdblk[i].b_cpid == cpibp->b_cpid) {
228 bcopy(cpdsp->d_cpdblk + i, cpdbp,
229 sizeof(struct cpdblk));
244 * Initialize Code Page information management.
245 * Load all copdepages in memory.
249 struct hpfsmount *hpmp,
250 struct hpfs_args *argsp)
256 struct cpisec * cpisp;
257 struct cpiblk * cpibp;
258 struct cpdblk * cpdbp;
260 dprintf(("hpfs_cpinit: \n"));
262 if (argsp->flags & HPFSMNT_TABLES) {
263 bcopy(argsp->d2u, hpmp->hpm_d2u, sizeof(u_char) * 0x80);
264 bcopy(argsp->u2d, hpmp->hpm_u2d, sizeof(u_char) * 0x80);
266 for (i=0x0; i<0x80;i++) {
267 hpmp->hpm_d2u[i] = i + 0x80;
268 hpmp->hpm_u2d[i] = i + 0x80;
272 cpicnt = hpmp->hpm_sp.sp_cpinum;
274 MALLOC(hpmp->hpm_cpdblk, struct cpdblk *,
275 cpicnt * sizeof(struct cpdblk), M_HPFSMNT, M_WAITOK);
277 cpdbp = hpmp->hpm_cpdblk;
278 lsn = hpmp->hpm_sp.sp_cpi;
281 error = bread(hpmp->hpm_devvp, lsn, DEV_BSIZE, NOCRED, &bp);
287 cpisp = (struct cpisec *)bp->b_data;
289 cpibp = cpisp->s_cpi;
290 for (i=0; i<cpisp->s_cpicnt; i++, cpicnt --, cpdbp++, cpibp++) {
291 dprintf(("hpfs_cpinit: Country: %d, CP: %d (%d)\n",
292 cpibp->b_country, cpibp->b_cpid,
295 error = hpfs_cpload(hpmp, cpibp, cpdbp);
310 struct hpfsmount *hpmp)
312 dprintf(("hpmp_cpdeinit: "));
313 FREE(hpmp->hpm_cpdblk,M_HPFSMNT);
318 * Lookup for a run of blocks.
322 struct hpfsmount *hpmp,
323 u_long flags, /* 1 means we want right len blocks in run, not less */
324 lsn_t lsn, /* We want near this one */
325 u_long len, /* We want such long */
326 lsn_t *lsnp, /* We got here */
327 u_long *lenp) /* We got this long */
330 register u_int32_t mask;
336 dprintf(("hpfs_bmlookup: lsn: 0x%x, len 0x%lx | Step1\n", lsn, len));
338 if (lsn > hpmp->hpm_su.su_btotal) {
339 printf("hpfs_bmlookup: OUT OF VOLUME\n");
342 if (len > hpmp->hpm_bavail) {
343 printf("hpfs_bmlookup: OUT OF SPACE\n");
349 bitmap = (u_int32_t *)hpmp->hpm_bitmap + i;
351 if (*bitmap & mask) {
354 for (; k < 32; k++, mask<<=1) {
370 for (; i < hpmp->hpm_su.su_btotal >> 5; i++, bitmap++) {
371 for (k=0, mask=1; k < 32; k++, mask<<=1) {
390 * Lookup all bands begining from cband, lookup for first block
393 dprintf(("hpfs_bmlookup: Step2: band 0x%x (0x%lx)\n",
394 cband, hpmp->hpm_dbnum));
395 for (vcband = 0; vcband < hpmp->hpm_dbnum; vcband ++, cband++) {
396 cband = cband % hpmp->hpm_dbnum;
397 bandsz = min (hpmp->hpm_su.su_btotal - (cband << 14), 0x4000);
398 dprintf(("hpfs_bmlookup: band: %d, sz: 0x%x\n", cband, bandsz));
400 bitmap = (u_int32_t *)hpmp->hpm_bitmap + (cband << 9);
404 for (i=0; i < bandsz >> 5; i++, bitmap++) {
405 for (k=0, mask=1; k < 32; k++, mask<<=1) {
406 if (*bitmap & mask) {
411 *lsnp = (cband << 14) + (i << 5) + k;
415 if ((*lenp) && !(flags & 1)) {
426 if (cband == hpmp->hpm_dbnum - 1) {
427 if ((*lenp) && !(flags & 1)) {
439 * Lookup a single free block. XXX Need locking on BitMap operations
440 * VERY STUPID ROUTINE!!!
444 struct hpfsmount *hpmp,
450 dprintf(("hpfs_bmfblookup: "));
452 bitmap = (u_int32_t *)hpmp->hpm_bitmap;
453 for (i=0; i < hpmp->hpm_su.su_btotal >> 5; i++, bitmap++) {
456 *lp = (i << 5) + k - 1;
457 dprintf((" found: 0x%x\n",*lp));
466 * Mark contignous block of blocks.
470 struct hpfsmount *hpmp,
478 dprintf(("hpfs_bmmark(0x%x, 0x%lx, %d): \n",bn,bl, state));
480 if ((bn > hpmp->hpm_su.su_btotal) || (bn+bl > hpmp->hpm_su.su_btotal)) {
481 printf("hpfs_bmmark: MARKING OUT OF VOLUME\n");
484 bitmap = (u_int32_t *)hpmp->hpm_bitmap;
488 for (i = bn & 0x1F; (i < 0x20) && (bl > 0) ; i++, bl--) {
490 if ( *bitmap & (1 << i)) {
492 printf("hpfs_bmmark: ALREADY FREE\n");
500 if ((~(*bitmap)) & (1 << i)) {
502 printf("hpfs_bmmark: ALREADY BUSY\n");
508 *bitmap &= ~(1 << i);
520 hpfs_validateparent (
523 struct hpfsnode *dhp;
525 struct hpfsmount *hpmp = hp->h_hpmp;
528 struct hpfsdirent *dep;
532 dprintf(("hpfs_validatetimes(0x%x): [parent: 0x%x] ",
533 hp->h_no, hp->h_fn.fn_parent));
535 if (hp->h_no == hp->h_fn.fn_parent) {
538 error = VFS_VGET(hpmp->hpm_mp, hp->h_fn.fn_parent, &dvp);
544 lsn = ((alleaf_t *)dhp->h_fn.fn_abd)->al_lsn;
551 dprintf(("[dive 0x%x] ", lsn));
554 error = bread(dhp->h_devvp, lsn, D_BSIZE, NOCRED, &bp);
558 dp = (struct dirblk *) bp->b_data;
559 if (dp->d_magic != D_MAGIC) {
560 printf("hpfs_validatetimes: magic doesn't match\n");
568 dprintf(("[restore 0x%x] ", olsn));
570 while(!(dep->de_flag & DE_END) ) {
571 if((dep->de_flag & DE_DOWN) &&
572 (olsn == DE_DOWNLSN(dep)))
574 dep = (hpfsdirent_t *)((caddr_t)dep + dep->de_reclen);
577 if((dep->de_flag & DE_DOWN) && (olsn == DE_DOWNLSN(dep))) {
578 if (dep->de_flag & DE_END)
581 if (hp->h_no == dep->de_fnode) {
582 dprintf(("[found] "));
586 dep = (hpfsdirent_t *)((caddr_t)dep + dep->de_reclen);
588 printf("hpfs_validatetimes: ERROR! oLSN not found\n");
596 while(!(dep->de_flag & DE_END)) {
597 if(dep->de_flag & DE_DOWN) {
598 lsn = DE_DOWNLSN(dep);
603 if (hp->h_no == dep->de_fnode) {
604 dprintf(("[found] "));
608 dep = (hpfsdirent_t *)((caddr_t)dep + dep->de_reclen);
611 if(dep->de_flag & DE_DOWN) {
612 dprintf(("[enddive] "));
613 lsn = DE_DOWNLSN(dep);
623 dprintf(("[level %d] ", level));
625 goto dive; /* undive really */
630 bcopy(dep->de_name,hp->h_name,dep->de_namelen);
631 hp->h_name[dep->de_namelen] = '\0';
632 hp->h_namelen = dep->de_namelen;
633 hp->h_ctime = dep->de_ctime;
634 hp->h_atime = dep->de_atime;
635 hp->h_mtime = dep->de_mtime;
636 hp->h_flag |= H_PARVALID;
638 dprintf(("[readdone]"));
663 * Write down changes done to parent dir, these are only times for now.
664 * hpfsnode have to be locked.
670 struct hpfsnode *dhp;
672 struct hpfsdirent *dep;
676 dprintf(("hpfs_updateparent(0x%x): \n", hp->h_no));
678 if (!(hp->h_flag & H_PARCHANGE))
681 if (!(hp->h_flag & H_PARVALID)) {
682 error = hpfs_validateparent (hp);
687 if (hp->h_no == hp->h_fn.fn_parent) {
690 error = VFS_VGET(hp->h_hpmp->hpm_mp, hp->h_fn.fn_parent,
697 error = hpfs_genlookupbyname (dhp, hp->h_name, hp->h_namelen,
703 dep->de_atime = hp->h_atime;
704 dep->de_mtime = hp->h_mtime;
705 dep->de_size = hp->h_fn.fn_size;
709 hp->h_flag &= ~H_PARCHANGE;
720 * Write down on disk changes done to fnode. hpfsnode have to be locked.
728 dprintf(("hpfs_update(0x%x): \n", hp->h_no));
730 if (!(hp->h_flag & H_CHANGE))
733 bp = getblk(hp->h_devvp, hp->h_no, FNODESIZE, 0, 0);
736 bcopy (&hp->h_fn, bp->b_data, sizeof(struct fnode));
739 hp->h_flag &= ~H_CHANGE;
741 if (hp->h_flag & H_PARCHANGE)
742 return (hpfs_updateparent(hp));
748 * Truncate file to specifed size. hpfsnode have to be locked.
755 struct hpfsmount *hpmp = hp->h_hpmp;
756 lsn_t newblen, oldblen;
759 dprintf(("hpfs_truncate(0x%x, 0x%x -> 0x%lx): ",
760 hp->h_no, hp->h_fn.fn_size, size));
762 newblen = (size + DEV_BSIZE - 1) >> DEV_BSHIFT;
763 oldblen = (hp->h_fn.fn_size + DEV_BSIZE - 1) >> DEV_BSHIFT;
765 dprintf(("blen: 0x%x -> 0x%x\n", oldblen, newblen));
767 error = hpfs_truncatealblk (hpmp, &hp->h_fn.fn_ab, newblen, &pf);
771 hp->h_fn.fn_ab.ab_flag = 0;
772 hp->h_fn.fn_ab.ab_freecnt = 0x8;
773 hp->h_fn.fn_ab.ab_busycnt = 0x0;
774 hp->h_fn.fn_ab.ab_freeoff = sizeof(alblk_t);
777 hp->h_fn.fn_size = size;
779 hp->h_flag |= (H_CHANGE | H_PARCHANGE);
781 dprintf(("hpfs_truncate: successful\n"));
787 * Enlarge file to specifed size. hpfsnode have to be locked.
794 struct hpfsmount *hpmp = hp->h_hpmp;
795 lsn_t newblen, oldblen;
798 dprintf(("hpfs_extend(0x%x, 0x%x -> 0x%lx): ",
799 hp->h_no, hp->h_fn.fn_size, size));
801 if (hpmp->hpm_bavail < 0x10)
804 newblen = (size + DEV_BSIZE - 1) >> DEV_BSHIFT;
805 oldblen = (hp->h_fn.fn_size + DEV_BSIZE - 1) >> DEV_BSHIFT;
807 dprintf(("blen: 0x%x -> 0x%x\n", oldblen, newblen));
809 error = hpfs_addextent(hpmp, hp, newblen - oldblen);
811 printf("hpfs_extend: FAILED TO ADD EXTENT %d\n", error);
815 hp->h_fn.fn_size = size;
817 hp->h_flag |= (H_CHANGE | H_PARCHANGE);
819 dprintf(("hpfs_extend: successful\n"));
825 * Read AlSec structure, and check if magic is valid.
826 * You don't need to brelse buf on error.
830 struct hpfsmount *hpmp,
840 dprintf(("hpfs_breadstruct: reading at 0x%x\n", lsn));
844 error = bread(hpmp->hpm_devvp, lsn, len, NOCRED, &bp);
849 mp = (u_int32_t *) bp->b_data;
852 printf("hpfs_breadstruct: MAGIC DOESN'T MATCH (0x%08x != 0x%08x)\n",