summaryrefslogtreecommitdiffstats
path: root/sys/fs/hpfs/hpfs_subr.c
blob: c7fe94f72a481931928370f1735a6ff1a69effa6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
/*-
 * 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/bio.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, 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));

	hpmp->hpm_bmind = malloc(hpmp->hpm_dbnum * sizeof(lsn_t),
		M_HPFSMNT, M_WAITOK);

	hpmp->hpm_bitmap = malloc(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 mount *mp,
	struct hpfsmount *hpmp)
{
	struct buf *bp;
	int error, i;
	lsn_t lsn;
	int cpicnt;
	struct cpisec * cpisp;
	struct cpiblk * cpibp;
	struct cpdblk * cpdbp;

	dprintf(("hpfs_cpinit: \n"));

	error = vfs_copyopt(mp->mnt_optnew, "d2u", hpmp->hpm_d2u,
	    sizeof hpmp->hpm_d2u);
	if (error == ENOENT)
		for (i=0x0; i<0x80;i++)
			hpmp->hpm_d2u[i] = i + 0x80;
	else if (error)
		return (error);

	error = vfs_copyopt(mp->mnt_optnew, "u2d", hpmp->hpm_u2d,
	    sizeof hpmp->hpm_u2d);
	if (error == ENOENT)
		for (i=0x0; i<0x80;i++)
			hpmp->hpm_u2d[i] = i + 0x80;
	else if (error)
		return (error);

	cpicnt = hpmp->hpm_sp.sp_cpinum;

	hpmp->hpm_cpdblk = malloc(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,
				 LK_EXCLUSIVE, &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,
				 LK_EXCLUSIVE, &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, 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);
}

OpenPOWER on IntegriCloud