File: e2lib.c

package info (click to toggle)
aboot 0.9b-3+etchnhalf.1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 980 kB
  • ctags: 1,656
  • sloc: ansic: 8,975; perl: 738; makefile: 382; asm: 309; sh: 3
file content (1473 lines) | stat: -rw-r--r-- 36,997 bytes parent folder | download | duplicates (6)
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
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
/* This is a library of functions that allow user-level programs to
 * read and manipulate ext2 file systems.  For convenience sake,
 * this library maintains a lot of state information in static
 * variables; therefore,  it's not reentrant.  We don't care for
 * our applications 8-)
 */

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/stat.h>

#include <bio.h>
#include <e2lib.h>


#define		MAX_OPEN_FILES		8 

int				fd = -1; 
struct ext2_super_block		sb;
struct ext2_group_desc		*gds;
int				ngroups = 0;
int				blocksize;	/* Block size of this fs */
int				directlim;	/* Maximum direct blkno */
int				ind1lim;	/* Maximum single-indir blkno */
int				ind2lim;	/* Maximum double-indir blkno */
int				ptrs_per_blk;	/* ptrs/indirect block */
char				filename[256];
int				readonly;	/* Is this FS read-only? */
int				verbose = 0;
int				big_endian = 0;

static void	ext2_ifree(int ino);
static void	ext2_free_indirect(int indirect_blkno, int level);


struct inode_table_entry {
	struct	ext2_inode	inode;
	int			inumber;
	int			free;
	unsigned short		old_mode;
} inode_table[MAX_OPEN_FILES];

/* Utility functions to byte-swap 16 and 32 bit quantities... */

unsigned short
swap16 (unsigned short s)
{
    return((unsigned short)( ((s << 8) & 0xff00) | ((s >> 8) & 0x00ff)));
}

unsigned int
swap32 (unsigned int i)
{
    return((unsigned int)(
                ((i << 24) & 0xff000000) |
                ((i << 8) & 0x00ff0000) |
                ((i >> 8) & 0x0000ff00) |
                ((i >> 24) & 0x000000ff)) );
}

void
ext2_swap_sb (struct ext2_super_block *sb)
{
    sb->s_inodes_count = swap32(sb->s_inodes_count);
    sb->s_blocks_count = swap32(sb->s_blocks_count);
    sb->s_r_blocks_count = swap32(sb->s_r_blocks_count);
    sb->s_free_blocks_count = swap32(sb->s_free_blocks_count);
    sb->s_free_inodes_count = swap32(sb->s_free_inodes_count);
    sb->s_first_data_block = swap32(sb->s_first_data_block);
    sb->s_log_block_size = swap32(sb->s_log_block_size);
    sb->s_log_frag_size = swap32(sb->s_log_frag_size);
    sb->s_blocks_per_group = swap32(sb->s_blocks_per_group);
    sb->s_frags_per_group = swap32(sb->s_frags_per_group);
    sb->s_inodes_per_group = swap32(sb->s_inodes_per_group);
    sb->s_mtime = swap32(sb->s_mtime);
    sb->s_wtime = swap32(sb->s_wtime);
    sb->s_mnt_count = swap16(sb->s_mnt_count);
    sb->s_max_mnt_count = swap16(sb->s_max_mnt_count);
    sb->s_magic = swap16(sb->s_magic);
    sb->s_state = swap16(sb->s_state);
    sb->s_errors = swap16(sb->s_errors);
    sb->s_pad = swap16(sb->s_pad);
    sb->s_lastcheck = swap32(sb->s_lastcheck);
    sb->s_checkinterval = swap32(sb->s_checkinterval);
}

void
ext2_swap_gd (struct ext2_group_desc *gd)
{
	gd->bg_block_bitmap = swap32(gd->bg_block_bitmap);
	gd->bg_inode_bitmap = swap32(gd->bg_inode_bitmap);
	gd->bg_inode_table = swap32(gd->bg_inode_table);
	gd->bg_free_blocks_count = swap16(gd->bg_free_blocks_count);
	gd->bg_free_inodes_count = swap16(gd->bg_free_inodes_count);
	gd->bg_used_dirs_count = swap16(gd->bg_used_dirs_count);
	gd->bg_pad = swap16(gd->bg_pad);
}

void
ext2_swap_inode (struct ext2_inode *ip)
{
    int		i;

    ip->i_mode = swap16(ip->i_mode);
    ip->i_uid = swap16(ip->i_uid);
    ip->i_size = swap32(ip->i_size);
    ip->i_atime = swap32(ip->i_atime);
    ip->i_ctime = swap32(ip->i_ctime);
    ip->i_mtime = swap32(ip->i_mtime);
    ip->i_dtime = swap32(ip->i_dtime);
    ip->i_gid = swap16(ip->i_gid);
    ip->i_links_count = swap16(ip->i_links_count);
    ip->i_blocks = swap32(ip->i_blocks);
    ip->i_flags = swap32(ip->i_flags);
    ip->i_reserved1 = swap32(ip->i_reserved1);
    for(i = 0; i < EXT2_N_BLOCKS; i++) {
	ip->i_block[i] = swap32(ip->i_block[i]);
    }
    ip->i_version = swap32(ip->i_version);
    ip->i_file_acl = swap32(ip->i_file_acl);
    ip->i_dir_acl = swap32(ip->i_dir_acl);
    ip->i_faddr = swap32(ip->i_faddr);
    ip->i_pad1 = swap16(ip->i_pad1);
}



/* Initialize an ext2 filesystem; this is sort-of the same idea as
 * "mounting" it.  Read in the relevant control structures and 
 * make them available to the user.  Returns 0 if successful, -1 on
 * failure.
 */
int
ext2_init (char * name, int access)
{
    int		i;

    /* Initialize the inode table */
    for(i = 0; i < MAX_OPEN_FILES; i++) {
	inode_table[i].free = 1;
	inode_table[i].inumber = 0;
    }

    if((access != O_RDONLY) && (access != O_RDWR)) {
	fprintf(stderr, 
		"ext2_init: Access must be O_RDONLY or O_RDWR, not %d\n",
		access);
	return(-1);
    }

    /* Open the device/file */
    fd = open(name, access);
    if(fd < 0) {
	perror(filename);
	return(-1);
    }

    if(access == O_RDONLY) {
	readonly = 1;
    }

    /* Read in the first superblock */
    lseek(fd, EXT2_MIN_BLOCK_SIZE, SEEK_SET);
    if(read(fd, &sb, sizeof(sb)) != sizeof(sb)) {
        perror("ext2 sb read");
	close(fd);
	return(-1);
    }

    if((sb.s_magic != EXT2_SUPER_MAGIC) && (sb.s_magic != EXT2_SUPER_BIGMAGIC)) {
	fprintf(stderr, "ext2 bad magic 0x%x\n", sb.s_magic);
	close(fd);
	return(-1);
    }

    if(sb.s_magic == EXT2_SUPER_BIGMAGIC) {
	big_endian = 1;

	/* Byte-swap the fields in the superblock... */
	ext2_swap_sb(&sb);
    }

    if(sb.s_first_data_block != 1) {
	fprintf(stderr, 
	    "Brain-damaged utils can't deal with a filesystem\nwhere s_first_data_block != 1.\nRe-initialize the filesystem\n");
	close(fd);
	return(-1);
    }

    ngroups = (sb.s_blocks_count+sb.s_blocks_per_group-1)/sb.s_blocks_per_group;
    gds = (struct ext2_group_desc *)
	      malloc((size_t)(ngroups * sizeof(struct ext2_group_desc)));

    /* Read in the group descriptors (immediately follows superblock) */
    if ((size_t) read(fd, gds, ngroups * sizeof(struct ext2_group_desc))
	!= (ngroups * sizeof(struct ext2_group_desc)))
    {
	perror("ext2_init: group desc read error");
	return(-1);
    }

    if(big_endian) {
	for(i = 0; i < ngroups; i++) {
	    ext2_swap_gd(&(gds[i]));
	}
    }

    strcpy(filename, name);

    /* Calculate direct/indirect block limits for this file system
     * (blocksize dependent)
     */
    blocksize = EXT2_BLOCK_SIZE(&sb);
    directlim = EXT2_NDIR_BLOCKS - 1;
    ptrs_per_blk = blocksize/sizeof(unsigned int);
    ind1lim = ptrs_per_blk + directlim;
    ind2lim = (ptrs_per_blk * ptrs_per_blk) + directlim;

    if(getenv("EXT2_VERBOSE")) {
	verbose = 1;
    }

    binit(fd, blocksize);

    if(verbose) {
	printf("Initialized filesystem %s\n", filename);
	printf("  %d blocks (%dKb), %d free (%dKb)\n", 
		sb.s_blocks_count, (sb.s_blocks_count * blocksize)/1024,
		sb.s_free_blocks_count, 
		(sb.s_free_blocks_count * blocksize)/1024);
	printf("  %d inodes,  %d free\n", 
		sb.s_inodes_count, sb.s_free_inodes_count);
	printf("  %d groups, %d blocks/group\n", 
			ngroups, sb.s_blocks_per_group);
    }

    return(0);
}

int
ext2_blocksize (void)
{
    return blocksize;
}

int
ext2_total_blocks (void)
{
    return sb.s_blocks_count;
}

int
ext2_free_blocks (void)
{
    return sb.s_free_blocks_count;
}

int
ext2_total_inodes (void)
{
    return sb.s_inodes_count;
}

int
ext2_free_inodes (void)
{
    return sb.s_free_inodes_count;
}

/* Call this when we're all done with the file system.  This will write
 * back any superblock and group changes to the file system.
 */
void
ext2_close (void)
{
    int		i;
    int		errors = 0;
    int		blocks_per_group = sb.s_blocks_per_group;

    if(!readonly) {

	if(big_endian) {
	    ext2_swap_sb(&sb);
	    for(i = 0; i < ngroups; i++) {
		ext2_swap_gd(&(gds[i]));
	    }
	}

	for(i = 0; i < ngroups; i++) {
	    lseek(fd, ((i*blocks_per_group)+1)*blocksize, SEEK_SET);
	    if(write(fd, &sb, sizeof(sb)) != sizeof(sb)) {
		perror("sb write");
		errors = 1;
	    }

	    if ((size_t) write(fd, gds, ngroups*sizeof(struct ext2_group_desc))
		!= ngroups*sizeof(struct ext2_group_desc))
	    {
		perror("gds write");
		errors = 1;
	    }

	    bflush();
	}
    }

    close(fd);
    if(errors) {
	fprintf(stderr, "Errors encountered while updating %s\n", filename);
	fprintf(stderr, "e2fsck is STRONGLY recommended!\n");
    }
}

	

/* Read the specified inode from the disk and return it to the user.
 * Returns NULL if the inode can't be read...
 */
struct ext2_inode *
ext2_iget (int ino)
{
    int				i;
    struct ext2_inode *		ip = NULL;
    struct inode_table_entry *	itp = NULL;
    int				group;
    int				blkoffset;
    int				byteoffset;
    char                        inobuf[EXT2_MAX_BLOCK_SIZE];

    for(i = 0; i < MAX_OPEN_FILES; i++) {
	if(inode_table[i].free) {
	    itp = &(inode_table[i]);
	    ip = &(itp->inode);
	    break;
	}
    }
    if(!ip) {
	fprintf(stderr, "ext2_iget: no free inodes\n");
	return(NULL);
    }

    group = ino / sb.s_inodes_per_group;
    blkoffset = (gds[group].bg_inode_table * blocksize);
    byteoffset = ((ino-1) % sb.s_inodes_per_group) * sizeof(struct ext2_inode);
    blkoffset += ((byteoffset / blocksize) * blocksize);
    byteoffset = (byteoffset % blocksize);
    bread(blkoffset/blocksize, inobuf);

    memcpy(ip, &(inobuf[byteoffset]), sizeof(struct ext2_inode));

    if(big_endian) {
	ext2_swap_inode(ip);
    }

    /* Yes, this is ugly, but it makes iput SOOO much easier 8-) */
    itp->free = 0;
    itp->inumber = ino;
    itp->old_mode = ip->i_mode;

    return(ip);
}

/* Put the specified inode back on the disk where it came from. */
void
ext2_iput (struct ext2_inode *ip)
{
    int				group;
    int				blkoffset;
    int				byteoffset;
    int				ino;
    struct inode_table_entry 	*itp;
    char                        inobuf[EXT2_MAX_BLOCK_SIZE];
    int				inode_mode;

    itp = (struct inode_table_entry *)ip;
    ino = itp->inumber;

    if(ip->i_links_count == 0) {
	ext2_ifree(itp->inumber);
    }

    itp->inumber = 0;

    if(!readonly) {
	group = ino / sb.s_inodes_per_group;
	blkoffset = (gds[group].bg_inode_table * blocksize);
	byteoffset = ((ino-1) % sb.s_inodes_per_group) * sizeof(struct ext2_inode);
	blkoffset += (byteoffset / blocksize) * blocksize;
	byteoffset = byteoffset % blocksize;

	inode_mode = ip->i_mode;
	bread(blkoffset/blocksize, inobuf);
	if(big_endian) {
	    ext2_swap_inode(ip);
	}
	memcpy(&(inobuf[byteoffset]), ip, sizeof(struct ext2_inode));
	bwrite(blkoffset/blocksize, inobuf);

	if(S_ISDIR(itp->old_mode) && !S_ISDIR(inode_mode)) {
	    /* We deleted a directory */
	    gds[group].bg_used_dirs_count--;
	}
	if(!S_ISDIR(itp->old_mode) && S_ISDIR(inode_mode)) {
	    /* We created a directory */
	    gds[group].bg_used_dirs_count++;
	}
    }

    itp->free = 1;
}

#define BITS_PER_LONG	(8*sizeof(int))

static int
find_first_zero_bit (unsigned int * addr, unsigned size)
{
	unsigned lwsize;
        unsigned int        *ap = (unsigned int *)addr;
        unsigned int        mask;
        unsigned int        longword, bit;
	unsigned int	    lwval;

	if (!size)
		return 0;

	/* Convert "size" to a whole number of longwords... */
	lwsize = (size + BITS_PER_LONG - 1) >> 5;
	for (longword = 0; longword < lwsize; longword++, ap++) {
	    if(*ap != 0xffffffff) {
		lwval = big_endian ? swap32(*ap) : *ap;

		for (bit = 0, mask = 1; bit < BITS_PER_LONG; bit++, mask <<= 1)
		{
		    if ((lwval & mask) == 0) {
			return (longword*BITS_PER_LONG) + bit;
		    }
		}
	    }
	}
	return size;

}

static void
set_bit (unsigned int *addr, int bitno)
{
    if(big_endian) {
	int	lwval;
	lwval = swap32(addr[bitno/BITS_PER_LONG]);
	lwval |= (1 << (bitno % BITS_PER_LONG));
	addr[bitno/BITS_PER_LONG] = swap32(lwval);
    }
    else {
        addr[bitno / BITS_PER_LONG] |= (1 << (bitno % BITS_PER_LONG));
    }
}

static void
clear_bit (unsigned int *addr, int bitno)
{
    if(big_endian) {
	int	lwval;
	lwval = swap32(addr[bitno/BITS_PER_LONG]);
	lwval &= ~((unsigned int)(1 << (bitno % BITS_PER_LONG)));
	addr[bitno/BITS_PER_LONG] = swap32(lwval);
    }
    else {
        addr[bitno / BITS_PER_LONG] &= 
			~((unsigned int)(1 << (bitno % BITS_PER_LONG)));
    }
}


/* Allocate a block from the file system.  Brain-damaged implementation;
 * doesn't even TRY to do load-balancing among groups... just grabs the
 * first block it can find...
 */
int
ext2_balloc (void)
{
    unsigned int blk, blockmap[256];
    int i;

    if(readonly) {
	fprintf(stderr, "ext2_balloc: readonly filesystem\n");
	return(0);
    }

    for(i = 0; i < ngroups; i++) {
	if(gds[i].bg_free_blocks_count > 0) {
	    bread(gds[i].bg_block_bitmap, blockmap);
	    blk = find_first_zero_bit(blockmap, sb.s_blocks_per_group);
	    if (blk == 0 || blk == sb.s_blocks_per_group) {
		fprintf(stderr, 
			"group %d has %d blocks free but none in bitmap?\n",
			i, gds[i].bg_free_blocks_count);
		continue;
	    }
	    set_bit(blockmap, blk);
	    bwrite(gds[i].bg_block_bitmap, blockmap);
	    gds[i].bg_free_blocks_count--;
	    sb.s_free_blocks_count--;
	    blk = blk + (i*sb.s_blocks_per_group)+1;

	    if(blk == 0) {
		fprintf(stderr, "ext2_balloc: blk == 0?\n");
	    }
	    return(blk);
	}
    }

    if(verbose) {
	printf("ext2_balloc: can't find a free block\n");
    }
    return(0);
}

/* Deallocate a block */
void
ext2_bfree (int blk)
{
    int		i;
    unsigned int	blockmap[256];

    /* Find which group this block is in */
    i = (blk-1) / sb.s_blocks_per_group;

    /* Read the block map */
    bread(gds[i].bg_block_bitmap, blockmap);

    /* Clear the appropriate bit */
    clear_bit(blockmap, (blk-1) % sb.s_blocks_per_group);

    /* Write the block map back out */
    bwrite(gds[i].bg_block_bitmap, blockmap);

    /* Update free block counts. */
    gds[i].bg_free_blocks_count++;
    sb.s_free_blocks_count++;

}

/* Allocate a contiguous range of blocks.  This is used ONLY for
 * initializing the bootstrapper.  It uses a simple-minded algorithm
 * that works best on a clean or nearly clean file system...  we
 * chunk through the bitmap a longword at a time.  Only if the whole
 * longword indicates free blocks do we use it.  On a 32-bit system,
 * this means we allocate blocks only in units of 32.
 */
int
ext2_contiguous_balloc (int nblocks)
{
    int		i, j;
    int		firstlong, lastlong;
    int		longs_needed;
    int		longs_per_group;
    int		blk;
    unsigned int	blockmap[256];

    if(readonly) {
	fprintf(stderr, "ext2_contiguous_balloc: readonly filesystem\n");
	return(0);
    }

    /* Compute how many longwords we need to fulfill this request */
    longs_needed = (nblocks + BITS_PER_LONG - 1) / BITS_PER_LONG;
    longs_per_group = sb.s_blocks_per_group/BITS_PER_LONG;

    for(i = 0; i < ngroups; i++) {
	/* Don't even bother if this group doesn't have enough blocks! */
	if(gds[i].bg_free_blocks_count >= nblocks) {

	    /* Get the block map. */
	    bread(gds[i].bg_block_bitmap, blockmap);

	    /* Find a run of blocks */
	    firstlong = 0;

	    do {
	        for(; firstlong < longs_per_group; firstlong++) {
		    if(blockmap[firstlong] == 0) break;
		}

	        if(firstlong == longs_per_group) {
		    /* No such thing in this group; try another! */
		    break;
	        }

	        for(lastlong = firstlong; lastlong < longs_per_group; 
							lastlong++) {
		    if(blockmap[lastlong] != 0) break;
	        }

		if((lastlong-firstlong) < longs_needed) {
		    firstlong = lastlong;
		}
	    } while((lastlong-firstlong) < longs_needed);

	    /* If we got all the way through the block map, 
	     * try another group.
	     */
	    if(firstlong == longs_per_group) {
		continue;
	    }

	    /* If we get here, then we know that we have a run
	     * that will fit our allocation.  Allocate the *actual*
	     * blocks that we need!
	     */
	    blk = firstlong * BITS_PER_LONG;
	    for(j = 0; j < nblocks; j++) {
		set_bit(blockmap, blk+j);
	    }
	  
	    bwrite(gds[i].bg_block_bitmap, blockmap);
	    gds[i].bg_free_blocks_count -= nblocks;
	    sb.s_free_blocks_count -= nblocks;
	    blk = blk + (i*sb.s_blocks_per_group)+1;

	    if(verbose) {
		printf("ext2_contiguous_balloc: allocated %d blks @%d\n",
			nblocks, blk);
	    }
	    return(blk);
	}
    }

    if(verbose) {
	printf("ext2_contiguous_balloc: can't find %d contiguous free blocks\n", nblocks);
    }
    return(0);
}
    

/* Pre-allocate contiguous blocks to the specified inode.  Note that the 
 * DATA blocks must be contiguous; indirect blocks can come from anywhere.
 * This is for the benefit of the bootstrap loader.
 * If successful, this routine returns the block number of the first 
 * data block of the file.  Otherwise, it returns -1.
 */
int
ext2_fill_contiguous (struct ext2_inode * ip, int nblocks)
{
    int		iblkno = 0;
    int		firstblock;
    int		i;
    unsigned int *lp = NULL;
    char	blkbuf[EXT2_MAX_BLOCK_SIZE];

    /* For simplicity's sake, we only allow single indirection
     * here.  We shouldn't need more than this anyway!
     */
    if(nblocks > ind1lim) {
	fprintf(stderr, 
	  "ext2_fill_contiguous: file size too big (%d); cannot exceed %d\n",
	  nblocks, ind1lim);
	return(-1);
    }

    /* First, try to allocate the data blocks */
    firstblock = ext2_contiguous_balloc(nblocks);

    if(firstblock == 0) {
	fprintf(stderr, 
          "ext2_fill_contiguous: Cannot allocate %d contiguous blocks\n", nblocks);
	return(-1);
    }

    ip->i_blocks = nblocks * (blocksize/512);

    /* If we need the indirect block, then allocate it now. */
    if(nblocks > directlim) {
        iblkno = ext2_balloc();
	if(iblkno == 0) {
	    /* Should rarely happen! */
	    fprintf(stderr,
		"ext2_fill_contiguous: cannot allocate indirect block\n");
	    for(i = 0; i < nblocks; i++) {
		ext2_bfree(i);
	    }
	    return(-1);
	}
	ip->i_blocks += (blocksize/512);
        /* Point to indirect block buffer, in case we need it! */
        lp = (unsigned int *)blkbuf;

	for(i = 0; i < ptrs_per_blk; i++) {
	    lp[i] = 0;
	}

	ip->i_block[EXT2_IND_BLOCK] = iblkno;
    }

    /* All set... let's roll! */
    ip->i_size = nblocks * blocksize;

    for(i = 0; i < nblocks; i++) {
	if(i < EXT2_NDIR_BLOCKS) {
	    ip->i_block[i] = firstblock+i;
	}
	else {
	    *lp++ = big_endian ? swap32(firstblock+i) : firstblock+i;
	}
    }

    /* Write back the indirect block if necessary... */
    if(iblkno) {
	bwrite(iblkno, blkbuf);
    }

    return(firstblock);
}

/* Write out a boot block for this file system.  The caller
 * should have instantiated the block.
 */
void
ext2_write_bootblock (char *bb)
{
    bwrite(0, bb);
}


/* Allocate an inode from the file system.  Brain-damaged implementation;
 * doesn't even TRY to do load-balancing among groups... just grabs the
 * first inode it can find...
 */
int
ext2_ialloc (void)
{
    unsigned int inodemap[256];
    int i, ino;

    if(readonly) {
	return(0);
    }
    for(i = 0; i < ngroups; i++) {
	if(gds[i].bg_free_inodes_count > 4) {
	    /* leave a few inodes in each group for slop... */
	    bread(gds[i].bg_inode_bitmap, inodemap);

	    ino = find_first_zero_bit(inodemap, sb.s_inodes_per_group);
	    if (ino == 0 || (unsigned) ino == sb.s_inodes_per_group) {
		fprintf(stderr, 
			"group %d has %d inodes free but none in bitmap?\n",
			i, gds[i].bg_free_inodes_count);
		continue;
	    }
	    set_bit(inodemap, ino);
	    bwrite(gds[i].bg_inode_bitmap, inodemap);
	    gds[i].bg_free_inodes_count--;
	    sb.s_free_inodes_count--;
	    ino = ino + (i*sb.s_inodes_per_group) + 1;
	    return ino;
	}
    }
    return 0;
}

/* Deallocate an inode */
static void
ext2_ifree (int ino)
{
    int		i;
    unsigned int	inodemap[256];

    /* Find which group this inode is in */
    i = (ino-1) / sb.s_inodes_per_group;

    /* Read the inode map */
    bread(gds[i].bg_inode_bitmap, inodemap);

    /* Clear the appropriate bit */
    clear_bit(inodemap, (ino-1) % sb.s_inodes_per_group);

    /* Write the inode map back out */
    bwrite(gds[i].bg_inode_bitmap, inodemap);

    /* Update free inode counts. */
    gds[i].bg_free_inodes_count++;
    sb.s_free_inodes_count++;
}

/* Map a block offset into a file into an absolute block number.
 * (traverse the indirect blocks if necessary).  Note: Double-indirect
 * blocks allow us to map over 64Mb on a 1k file system.  Therefore, for
 * our purposes, we will NOT bother with triple indirect blocks.
 *
 * The "allocate" argument is set if we want to *allocate* a block
 * and we don't already have one allocated.
 */
int
ext2_blkno (struct ext2_inode *ip, int blkoff, int allocate)
{
    unsigned int	*lp;
    int			blkno;
    int			iblkno;
    int			diblkno;
    char		blkbuf[EXT2_MAX_BLOCK_SIZE];

    if(allocate && readonly) {
	fprintf(stderr, "ext2_blkno: Cannot allocate on a readonly file system!\n");
	return(0);
    }

    lp = (unsigned int *)blkbuf;

    /* If it's a direct block, it's easy! */
    if(blkoff <= directlim) {
	if((ip->i_block[blkoff] == 0) && allocate) {
	    ip->i_block[blkoff] = ext2_balloc();
	    if(verbose) {
		printf("Allocated data block %d\n", ip->i_block[blkoff]);
	    }
	    ip->i_blocks += (blocksize / 512);
	}
	return(ip->i_block[blkoff]);
    }

    /* Is it a single-indirect? */
    if(blkoff <= ind1lim) {
	iblkno = ip->i_block[EXT2_IND_BLOCK];
	if((iblkno == 0) && allocate) {
	    /* No indirect block and we need one, so we allocate
	     * one, zero it, and write it out.
	     */
	    iblkno = ext2_balloc();
	    if(iblkno == 0) {
		return(0);
	    }
	    ip->i_block[EXT2_IND_BLOCK] = iblkno;
	    if(verbose) {
		printf("Allocated indirect block %d\n", iblkno);
	    }
	    ip->i_blocks += (blocksize / 512);
	    memset(blkbuf, 0, blocksize);
	    bwrite(iblkno, blkbuf);
	}

	if(iblkno == 0) {
	    return(0);
	}

	/* Read the indirect block */
	bread(iblkno, blkbuf);
	
	if(big_endian) {
	    blkno = swap32(lp[blkoff-(directlim+1)]);
	}
	else {
	    blkno = lp[blkoff-(directlim+1)];
	}
	if((blkno == 0) && allocate) {
	    /* No block allocated but we need one. */
	    if(big_endian) {
		blkno = ext2_balloc();
		lp[blkoff-(directlim+1)] = swap32(blkno);
	    }
	    else {
	        blkno = lp[blkoff-(directlim+1)] = ext2_balloc();
	    }
	    if(blkno == 0) {
		return(0);
	    }
	    ip->i_blocks += (blocksize / 512);
	    if(verbose) {
		printf("Allocated data block %d\n", blkno);
	    }
	    bwrite(iblkno, blkbuf);
	}
	return(blkno);
    }

    /* Is it a double-indirect? */
    if(blkoff <= ind2lim) {
	/* Find the double-indirect block */
	diblkno = ip->i_block[EXT2_DIND_BLOCK];
	if((diblkno == 0) && allocate) {
	    /* No double-indirect block and we need one.  Allocate one,
	     * fill it with zeros, and write it out.
	     */
	    diblkno = ext2_balloc();
	    if(diblkno == 0) {
		return(0);
	    }
	    ip->i_blocks += (blocksize / 512);
	    if(verbose) {
		printf("Allocated double-indirect block %d\n", diblkno);
	    }
	    memset(blkbuf, 0, blocksize);
	    bwrite(diblkno, blkbuf);
	    ip->i_block[EXT2_DIND_BLOCK] = diblkno;
	}

	if(diblkno == 0) {
	    return(0);
	}

	/* Read in the double-indirect block */
	bread(diblkno, blkbuf);

	/* Find the single-indirect block pointer ... */
	iblkno = lp[(blkoff - (ind1lim+1)) / ptrs_per_blk];
	if(big_endian) {
		iblkno = swap32(iblkno);
	}

	if((iblkno == 0) && allocate) {
	    /* No indirect block and we need one, so we allocate
	     * one, zero it, and write it out.
	     */
	    iblkno = ext2_balloc();
	    if(iblkno == 0) {
		return(0);
	    }
	    ip->i_blocks += (blocksize / 512);
	    if(verbose) {
		printf("Allocated single-indirect block %d\n", iblkno);
	    }
	    lp[(blkoff-(ind1lim+1)) / ptrs_per_blk] = big_endian ? swap32(iblkno) :  iblkno;
	    bwrite(diblkno, blkbuf);

	    memset(blkbuf, 0, blocksize);
	    bwrite(iblkno, blkbuf);
	}

	if(iblkno == 0) {
	    return(0);
	}
	    

	/* Read the indirect block */
	bread(iblkno, blkbuf);
	
	/* Find the block itself. */
	blkno = lp[(blkoff-(ind1lim+1)) % ptrs_per_blk];
	if(big_endian) {
		blkno = swap32(blkno);
	}
	if((blkno == 0) && allocate) {
	    /* No block allocated but we need one. */
	    if(big_endian) {
		blkno = ext2_balloc();
		lp[(blkoff-(ind1lim+1)) % ptrs_per_blk] = swap32(blkno);
	    }
	    else {
	        blkno = lp[(blkoff-(ind1lim+1)) % ptrs_per_blk] = ext2_balloc();
	    }
	    ip->i_blocks += (blocksize / 512);
	    if(verbose) {
		printf("Allocated data block %d\n", blkno);
	    }
	    bwrite(iblkno, blkbuf);
	}
	return(blkno);
    }

    if(blkoff > ind2lim) {
	fprintf(stderr, "ext2_blkno: block number too large: %d\n", blkoff);
	return(0);
    }
    return 0;
}




/* Read block number "blkno" from the specified file */
void
ext2_bread (struct ext2_inode *ip, int blkno, char * buffer)
{
    int		dev_blkno;

    dev_blkno = ext2_blkno(ip, blkno, 0);
    if(dev_blkno == 0) {
	/* This is a "hole" */
	memset(buffer, 0, blocksize);
    }
    else {
	/* Read it for real */
	bread(dev_blkno, buffer);
    }
}

/* Write block number "blkno" to the specified file */
void
ext2_bwrite (struct ext2_inode *ip, int blkno, char * buffer)
{
    int		dev_blkno;

    if(readonly) {
	fprintf(stderr, "ext2_bwrite: Cannot write to a readonly filesystem!\n");
	return;
    }

    dev_blkno = ext2_blkno(ip, blkno, 1);
    if(dev_blkno == 0) {
	fprintf(stderr, "%s: No space on ext2 device\n", filename);
    }
    else {
	/* Write it for real */
	bwrite(dev_blkno, buffer);
    }
}

/* More convenient forms of ext2_bread/ext2_bwrite.  These allow arbitrary
 * data alignment and buffer sizes...
 */
int
ext2_seek_and_read (struct ext2_inode *ip, int offset, char *buffer, int count)
{
    int		blkno;
    int		blkoffset;
    int		bytesleft;
    int		nread;
    int		iosize;
    char	*bufptr;
    char	blkbuf[EXT2_MAX_BLOCK_SIZE];

    bufptr = buffer;
    bytesleft = count;
    nread = 0;
    blkno = offset / blocksize;
    blkoffset = offset % blocksize;

    while(bytesleft > 0) {
	iosize = ((blocksize-blkoffset) > bytesleft) ? 
				bytesleft : (blocksize-blkoffset);
	if((blkoffset == 0) && (iosize == blocksize)) {
	    ext2_bread(ip, blkno, bufptr);
	}
	else {
	    ext2_bread(ip, blkno, blkbuf);
	    memcpy(bufptr, blkbuf+blkoffset, iosize);
	}
   	bytesleft -= iosize;
	bufptr += iosize;
	nread += iosize;
	blkno++;
	blkoffset = 0;
    }
    return(nread);
}

int
ext2_seek_and_write (struct ext2_inode *ip, int offset, char *buffer, int count)
{
    int		blkno;
    int		blkoffset;
    int		bytesleft;
    int		nwritten;
    int		iosize;
    char	*bufptr;
    char	blkbuf[EXT2_MAX_BLOCK_SIZE];

    bufptr = buffer;
    bytesleft = count;
    nwritten = 0;
    blkno = offset / blocksize;
    blkoffset = offset % blocksize;

    while(bytesleft > 0) {
	iosize = ((blocksize-blkoffset) > bytesleft) ? 
				bytesleft : (blocksize-blkoffset);
	if((blkoffset == 0) && (iosize == blocksize)) {
	    ext2_bwrite(ip, blkno, bufptr);
	}
	else {
	    ext2_bread(ip, blkno, blkbuf);
	    memcpy(blkbuf+blkoffset, bufptr, iosize);
	    ext2_bwrite(ip, blkno, blkbuf);
	}
   	bytesleft -= iosize;
	bufptr += iosize;
	nwritten += iosize;
	blkno++;
	blkoffset = 0;
    }
    return(nwritten);
}

struct ext2_inode *
ext2_namei (char *name)
{
    char 	namebuf[256];
    char 	dirbuf[EXT2_MAX_BLOCK_SIZE];
    char *	component;
    struct ext2_inode *		dir_inode;
    struct ext2_dir_entry *dp;
    int		next_ino;

    /* Squirrel away a copy of "namebuf" that we can molest */
    strcpy(namebuf, name);

    /* Start at the root... */
    dir_inode = ext2_iget(EXT2_ROOT_INO);

    component = strtok(namebuf, "/");
    while(component) {
	unsigned diroffset;
	int component_length, blockoffset;

	/* Search for the specified component in the current directory
	 * inode.
	 */

	next_ino = -1;

	component_length = strlen(component);
	diroffset = 0;
	while (diroffset < dir_inode->i_size) {
	    blockoffset = 0;
	    ext2_bread(dir_inode, diroffset / blocksize, dirbuf);
	    while (blockoffset < blocksize) {
		int namelen;

	        dp = (struct ext2_dir_entry *)(dirbuf+blockoffset);
		namelen = big_endian ? swap16(dp->name_len) : dp->name_len;
		if((namelen == component_length) &&
		   (strncmp(component, dp->name, component_length) == 0)) {
			/* Found it! */
			next_ino = big_endian ? swap32(dp->inode) : dp->inode;
			break;
		}
		/* Go to next entry in this block */
		blockoffset += (big_endian ? swap16(dp->rec_len) : dp->rec_len);
	    }
	    if(next_ino >= 0) {
		break;
	    }

	    /* If we got here, then we didn't find the component.
	     * Try the next block in this directory...
	     */
	    diroffset += blocksize;
	}

	/* At this point, we're done with this directory whether
	 * we've succeeded or failed...
	 */
	ext2_iput(dir_inode);

	/* If next_ino is negative, then we've failed (gone all the
	 * way through without finding anything)
	 */
	if(next_ino < 0) {
	    return(NULL);
	}

	/* Otherwise, we can get this inode and find the next
	 * component string...
	 */
	dir_inode = ext2_iget(next_ino);
	
	component = strtok(NULL, "/");
    }

    /* If we get here, then we got through all the components.
     * Whatever we got must match up with the last one.
     */
    return(dir_inode);
}

/* Create a new entry in the specified directory with the specified
 * name/inumber pair.  This routine ASSUMES that the specified 
 * entry does not already exist!  Therefore, we MUST use namei
 * first to try and find the entry...
 */

void
ext2_mknod (struct ext2_inode *dip, char * name, int ino)
{
    unsigned diroffset;
    int blockoffset, namelen, new_reclen;
    struct ext2_dir_entry *dp;
    struct ext2_dir_entry *entry_dp;
    char dirbuf[EXT2_MAX_BLOCK_SIZE];
    int dp_inode, dp_reclen, dp_namelen;

    namelen = strlen(name);

    /* Look for an empty directory entry that can hold this
     * item.
     */
    diroffset = 0;
    entry_dp = NULL;
    while (diroffset < dip->i_size) {
	blockoffset = 0;
	ext2_bread(dip, diroffset / blocksize, dirbuf);
	while(blockoffset < blocksize) {

	    dp = (struct ext2_dir_entry *)(dirbuf+blockoffset);
	    dp_inode = big_endian ? swap32(dp->inode) : dp->inode;
	    dp_reclen = big_endian ? swap16(dp->rec_len) : dp->rec_len;
	    dp_namelen = big_endian ? swap16(dp->name_len) : dp->name_len;

	    if((dp_inode == 0) && (dp_reclen >= EXT2_DIR_REC_LEN(namelen))) {
		/* Found an *empty* entry that can hold this name. */
		entry_dp = dp;
		break;
	    }

	    /* If this entry is in use, see if it has space at the end
	     * to hold the new entry anyway...
	     */
	    if((dp_inode != 0) && 
		((dp_reclen - EXT2_DIR_REC_LEN(dp_namelen)) 
					>= EXT2_DIR_REC_LEN(namelen))) {

		new_reclen = dp_reclen - EXT2_DIR_REC_LEN(dp_namelen);

		/* Chop the in-use entry down to size */
		if(big_endian) {
		    dp_reclen = EXT2_DIR_REC_LEN(swap16(dp->name_len));
		}
		else {
		    dp_reclen = EXT2_DIR_REC_LEN(dp->name_len);
		}
		dp->rec_len = big_endian ? swap16(dp_reclen) : dp_reclen;
		
		/* Point entry_dp to the end of this entry */
		entry_dp = (struct ext2_dir_entry *)((char*)dp + dp_reclen);

		/* Set the record length for this entry */
		entry_dp->rec_len = big_endian ? swap16(new_reclen) : new_reclen;

		/* all set! */
		break;
	    }

	    /* No luck yet... go to next entry in this block */
	    blockoffset += dp_reclen;
	}
	if(entry_dp != NULL) {
	    break;
	}

	/* If we got here, then we didn't find the component.
	 * Try the next block in this directory...
	 */
	diroffset += blocksize;
    }

    /* By the time we get here, one of two things has happened:
     *
     *	If entry_dp is non-NULL, then entry_dp points to the 
     *  place in dirbuf where the entry lives, and diroffset
     *  is the directory offset of the beginning of dirbuf.
     *
     *  If entry_dp is NULL, then we couldn't find an entry,
     *  so we need to add a block to the directory file for
     *  this entry...
     */
    if(entry_dp) {
	entry_dp->inode = big_endian ? swap32(ino) : ino;
	entry_dp->name_len = big_endian ? swap16(namelen) : namelen;
	strncpy(entry_dp->name, name, namelen);
	ext2_bwrite(dip, diroffset/blocksize, dirbuf);
    }
    else {
	entry_dp = (struct ext2_dir_entry *)dirbuf;
	entry_dp->inode = big_endian ? swap32(ino) : ino;
	entry_dp->name_len = big_endian ? swap16(namelen) : namelen;
	strncpy(entry_dp->name, name, namelen);
	entry_dp->rec_len = big_endian ? swap16(blocksize) : blocksize;
	ext2_bwrite(dip, dip->i_size/blocksize, dirbuf);
	dip->i_size += blocksize;
    }
}

/* This is a close cousin to namei, only it *removes* the entry
 * in addition to finding it.  This routine assumes that the specified
 * entry has already been found...
 */
void
ext2_remove_entry (char *name)
{
    char 	namebuf[256];
    char 	dirbuf[EXT2_MAX_BLOCK_SIZE];
    char *	component;
    struct ext2_inode *		dir_inode;
    struct ext2_dir_entry *dp;
    int		next_ino;
    int		dp_inode, dp_reclen, dp_namelen;

    /* Squirrel away a copy of "namebuf" that we can molest */
    strcpy(namebuf, name);

    /* Start at the root... */
    dir_inode = ext2_iget(EXT2_ROOT_INO);

    component = strtok(namebuf, "/");
    while(component) {
	unsigned diroffset;
	int blockoffset, component_length;
	char *next_component;
	struct ext2_dir_entry * pdp;

	/* Search for the specified component in the current directory
	 * inode.
	 */

	next_component = NULL;
	pdp = NULL;
	next_ino = -1;

	component_length = strlen(component);
	diroffset = 0;
	while (diroffset < dir_inode->i_size) {
	    blockoffset = 0;
	    ext2_bread(dir_inode, diroffset / blocksize, dirbuf);
	    while(blockoffset < blocksize) {
	        dp = (struct ext2_dir_entry *)(dirbuf+blockoffset);
		dp_inode = big_endian ? swap32(dp->inode) : dp->inode;
		dp_reclen = big_endian ? swap16(dp->rec_len) : dp->rec_len;
		dp_namelen = big_endian ? swap16(dp->name_len) : dp->name_len;

		if((dp_namelen == component_length) &&
		   (strncmp(component, dp->name, component_length) == 0)) {
			/* Found it! */
			next_component = strtok(NULL, "/");
			if(next_component == NULL) {
			    /* We've found the entry that needs to be
			     * zapped.  If it's at the beginning of the
			     * block, then zap it.  Otherwise, coalesce
			     * it with the previous entry.
			     */
			    if(pdp) {
				if(big_endian) {
				    pdp->rec_len = 
					swap16(swap16(pdp->rec_len)+dp_reclen);
				}
				else {
				    pdp->rec_len += dp_reclen;
				}
			    }
			    else {
				dp->inode = 0;
				dp->name_len = 0;
			    }
	    		    ext2_bwrite(dir_inode, diroffset / blocksize, dirbuf);
			    return;
			}
			next_ino = dp_inode;
			break;
		}
		/* Go to next entry in this block */
		pdp = dp;
		blockoffset += dp_reclen;
	    }
	    if(next_ino >= 0) {
		break;
	    }

	    /* If we got here, then we didn't find the component.
	     * Try the next block in this directory...
	     */
	    diroffset += blocksize;
	}

	/* At this point, we're done with this directory whether
	 * we've succeeded or failed...
	 */
	ext2_iput(dir_inode);

	/* If next_ino is negative, then we've failed (gone all the
	 * way through without finding anything)
	 */
	if(next_ino < 0) {
	    return;
	}

	/* Otherwise, we can get this inode and find the next
	 * component string...
	 */
	dir_inode = ext2_iget(next_ino);
	
	component = next_component;
    }

    ext2_iput(dir_inode);
}


void
ext2_truncate (struct ext2_inode *ip)
{
    int		i;

    /* Deallocate all blocks associated with a particular file
     * and set its size to zero.
     */

    /* Direct blocks */
    for(i = 0; i < EXT2_NDIR_BLOCKS; i++) {
	if(ip->i_block[i]) {
	    ext2_bfree(ip->i_block[i]);
	    ip->i_block[i] = 0;
	}
    }

    /* First-level indirect blocks */
    if(ip->i_block[EXT2_IND_BLOCK]) {
	ext2_free_indirect(ip->i_block[EXT2_IND_BLOCK], 0);
	ip->i_block[EXT2_IND_BLOCK] = 0;
    }

    /* Second-level indirect blocks */
    if(ip->i_block[EXT2_DIND_BLOCK]) {
	ext2_free_indirect(ip->i_block[EXT2_DIND_BLOCK], 1);
	ip->i_block[EXT2_DIND_BLOCK] = 0;
    }

    /* Third-level indirect blocks */
    if(ip->i_block[EXT2_TIND_BLOCK]) {
        ext2_free_indirect(ip->i_block[EXT2_TIND_BLOCK], 2);
        ip->i_block[EXT2_TIND_BLOCK] = 0;
    }

    ip->i_size = 0;
}

/* Recursive routine to free an indirect chain */
static void
ext2_free_indirect (int indirect_blkno, int level)
{
    int i, indirect_block[EXT2_MAX_BLOCK_SIZE/4];

    /* Read the specified indirect block */
    bread(indirect_blkno, indirect_block);

    for(i = 0; i < ptrs_per_blk; i++) {
	if(level == 0) {
	    /* These are pointers to data blocks; just free them up */
	    if(indirect_block[i]) {
		if(big_endian) {
	            ext2_bfree(swap32(indirect_block[i]));
		}
		else {
	            ext2_bfree(indirect_block[i]);
		}
	        indirect_block[i] = 0;
	    }
	}
	else {
	    /* These are pointers to *indirect* blocks.  Go down the chain */
	    if(indirect_block[i]) {
		if(big_endian) {
		    ext2_free_indirect(swap32(indirect_block[i]), level-1);
		}
		else {
		    ext2_free_indirect(indirect_block[i], level-1);
		}
		indirect_block[i] = 0;
	    }
	}
    }
    ext2_bfree(indirect_blkno);
}

int
ext2_get_inumber (struct ext2_inode *ip)
{
    struct inode_table_entry *itp;

    itp = (struct inode_table_entry *)ip;
    return(itp->inumber);
}