summaryrefslogtreecommitdiffstats
path: root/meta/recipes-devtools/genext2fs/genext2fs-1.4.1/0002-Add-put_blk-and-put_nod-routines.patch
blob: ddcd052edc4ff29bb5bcabd75088fd28fea9a700 (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
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
Upstream-Status: inappropriate

From 8dd6e604777ffeb4d30921592f199cd9bcc8a3e2 Mon Sep 17 00:00:00 2001
From: Corey Minyard <cminyard@mvista.com>
Date: Sat, 4 Jun 2011 15:23:29 -0500
Subject: [PATCH 02/19] Add put_blk and put_nod routines

Add the routines to mark that we are done with a block or inode, and
add the info structures so that get and put will work.  This doesn't
do anything functionally, just getting ready for future changes.

Most of the changes are pretty straightforward.  There were changes in
get_nod() because it could use a later block than the one actually
fetches.  And walk_bw() needed some special handling to avoid using data
after the put routine.
---
 genext2fs.c |  480 ++++++++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 330 insertions(+), 150 deletions(-)

diff --git a/genext2fs.c b/genext2fs.c
index 284862d..bd06369 100644
--- a/genext2fs.c
+++ b/genext2fs.c
@@ -236,18 +236,22 @@ struct stats {
 	(((fs)->sb.s_blocks_count - fs->sb.s_first_data_block + \
 	  (fs)->sb.s_blocks_per_group - 1) / (fs)->sb.s_blocks_per_group)
 
-// Get group block bitmap (bbm) given the group number
-#define GRP_GET_GROUP_BBM(fs,grp) ( get_blk((fs),(fs)->gd[(grp)].bg_block_bitmap) )
+// Get/put group block bitmap (bbm) given the group number
+#define GRP_GET_GROUP_BBM(fs,grp,bi) ( get_blk((fs),(fs)->gd[(grp)].bg_block_bitmap,(bi)) )
+#define GRP_PUT_GROUP_BBM(bi) ( put_blk((bi)) )
 
-// Get group inode bitmap (ibm) given the group number
-#define GRP_GET_GROUP_IBM(fs,grp) ( get_blk((fs),(fs)->gd[(grp)].bg_inode_bitmap) )
+// Get/put group inode bitmap (ibm) given the group number
+#define GRP_GET_GROUP_IBM(fs,grp,bi) ( get_blk((fs),(fs)->gd[(grp)].bg_inode_bitmap,(bi)) )
+#define GRP_PUT_GROUP_IBM(bi) ( put_blk((bi)) )
 		
 // Given an inode number find the group it belongs to
 #define GRP_GROUP_OF_INODE(fs,nod) ( ((nod)-1) / (fs)->sb.s_inodes_per_group)
 
-//Given an inode number get the inode bitmap that covers it
-#define GRP_GET_INODE_BITMAP(fs,nod) \
-	( GRP_GET_GROUP_IBM((fs),GRP_GROUP_OF_INODE((fs),(nod))) )
+//Given an inode number get/put the inode bitmap that covers it
+#define GRP_GET_INODE_BITMAP(fs,nod,bi)				\
+	( GRP_GET_GROUP_IBM((fs),GRP_GROUP_OF_INODE((fs),(nod)),(bi)) )
+#define GRP_PUT_INODE_BITMAP(bi) \
+	( GRP_PUT_GROUP_IBM((bi)) )
 
 //Given an inode number find its offset within the inode bitmap that covers it
 #define GRP_IBM_OFFSET(fs,nod) \
@@ -256,9 +260,11 @@ struct stats {
 // Given a block number find the group it belongs to
 #define GRP_GROUP_OF_BLOCK(fs,blk) ( ((blk)-1) / (fs)->sb.s_blocks_per_group)
 	
-//Given a block number get the block bitmap that covers it
-#define GRP_GET_BLOCK_BITMAP(fs,blk) \
-	( GRP_GET_GROUP_BBM((fs),GRP_GROUP_OF_BLOCK((fs),(blk))) )
+//Given a block number get/put the block bitmap that covers it
+#define GRP_GET_BLOCK_BITMAP(fs,blk,bi)				\
+	( GRP_GET_GROUP_BBM((fs),GRP_GROUP_OF_BLOCK((fs),(blk)),(bi)) )
+#define GRP_PUT_BLOCK_BITMAP(bi) \
+	( GRP_PUT_GROUP_BBM((bi)) )
 
 //Given a block number find its offset within the block bitmap that covers it
 #define GRP_BBM_OFFSET(fs,blk) \
@@ -811,24 +817,59 @@ allocated(block b, uint32 item)
 	return b[(item-1) / 8] & (1 << ((item-1) % 8));
 }
 
-// return a given block from a filesystem
+// Used by get_blk/put_blk to hold information about a block owned
+// by the user.
+typedef struct
+{
+	int dummy;
+} blk_info;
+
+// Return a given block from a filesystem.  Make sure to call
+// put_blk when you are done with it.
 static inline uint8 *
-get_blk(filesystem *fs, uint32 blk)
+get_blk(filesystem *fs, uint32 blk, blk_info **rbi)
 {
 	return (uint8*)fs + blk*BLOCKSIZE;
 }
 
-// return a given inode from a filesystem
+static inline void
+put_blk(blk_info *bi)
+{
+}
+
+// Used by get_nod/put_nod to hold information about an inode owned
+// by the user.
+typedef struct
+{
+	blk_info *bi;
+} nod_info;
+
+// Return a given inode from a filesystem.  Make sure to call put_nod()
+// when you are done with the inode.
 static inline inode *
-get_nod(filesystem *fs, uint32 nod)
+get_nod(filesystem *fs, uint32 nod, nod_info **rni)
 {
-	int grp,offset;
+	int grp, offset, boffset;
 	inode *itab;
+	nod_info *ni;
 
-	offset = GRP_IBM_OFFSET(fs,nod);
+	offset = GRP_IBM_OFFSET(fs,nod) - 1;
+	boffset = offset / (BLOCKSIZE / sizeof(inode));
+	offset %= BLOCKSIZE / sizeof(inode);
 	grp = GRP_GROUP_OF_INODE(fs,nod);
-	itab = (inode *)get_blk(fs, fs->gd[grp].bg_inode_table);
-	return itab+offset-1;
+	ni = malloc(sizeof(*ni));
+	if (!ni)
+		error_msg_and_die("get_nod: out of memory");
+	itab = (inode *)get_blk(fs, fs->gd[grp].bg_inode_table + boffset, &ni->bi);
+	*rni = ni;
+	return itab+offset;
+}
+
+static inline void
+put_nod(nod_info *ni)
+{
+	put_blk(ni->bi);
+	free(ni);
 }
 
 // allocate a given block/inode in the bitmap
@@ -870,12 +911,17 @@ alloc_blk(filesystem *fs, uint32 nod)
 {
 	uint32 bk=0;
 	uint32 grp,nbgroups;
+	blk_info *bi;
 
 	grp = GRP_GROUP_OF_INODE(fs,nod);
 	nbgroups = GRP_NBGROUPS(fs);
-	if(!(bk = allocate(get_blk(fs,fs->gd[grp].bg_block_bitmap), 0))) {
-		for(grp=0;grp<nbgroups && !bk;grp++)
-			bk=allocate(get_blk(fs,fs->gd[grp].bg_block_bitmap),0);
+	bk = allocate(get_blk(fs, fs->gd[grp].bg_block_bitmap, &bi), 0);
+	put_blk(bi);
+	if (!bk) {
+		for (grp=0; grp<nbgroups && !bk; grp++) {
+			bk = allocate(get_blk(fs, fs->gd[grp].bg_block_bitmap, &bi), 0);
+			put_blk(bi);
+		}
 		grp--;
 	}
 	if (!bk)
@@ -892,10 +938,12 @@ static void
 free_blk(filesystem *fs, uint32 bk)
 {
 	uint32 grp;
+	blk_info *bi;
 
 	grp = bk / fs->sb.s_blocks_per_group;
 	bk %= fs->sb.s_blocks_per_group;
-	deallocate(get_blk(fs,fs->gd[grp].bg_block_bitmap), bk);
+	deallocate(get_blk(fs, fs->gd[grp].bg_block_bitmap, &bi), bk);
+	put_blk(bi);
 	fs->gd[grp].bg_free_blocks_count++;
 	fs->sb.s_free_blocks_count++;
 }
@@ -906,6 +954,7 @@ alloc_nod(filesystem *fs)
 {
 	uint32 nod,best_group=0;
 	uint32 grp,nbgroups,avefreei;
+	blk_info *bi;
 
 	nbgroups = GRP_NBGROUPS(fs);
 
@@ -923,8 +972,10 @@ alloc_nod(filesystem *fs)
 			fs->gd[grp].bg_free_blocks_count > fs->gd[best_group].bg_free_blocks_count)
 			best_group = grp;
 	}
-	if (!(nod = allocate(get_blk(fs,fs->gd[best_group].bg_inode_bitmap),0)))
+	if (!(nod = allocate(get_blk(fs, fs->gd[best_group].bg_inode_bitmap,
+				     &bi), 0)))
 		error_msg_and_die("couldn't allocate an inode (no free inode)");
+	put_blk(bi);
 	if(!(fs->gd[best_group].bg_free_inodes_count--))
 		error_msg_and_die("group descr. free blocks count == 0 (corrupted fs?)");
 	if(!(fs->sb.s_free_inodes_count--))
@@ -968,24 +1019,35 @@ static uint32
 walk_bw(filesystem *fs, uint32 nod, blockwalker *bw, int32 *create, uint32 hole)
 {
 	uint32 *bkref = 0;
+	uint32 bk = 0;
 	uint32 *b;
 	int extend = 0, reduce = 0;
+	inode *inod;
+	nod_info *ni;
+	uint32 *iblk;
+	blk_info *bi1 = NULL, *bi2 = NULL, *bi3 = NULL;
+
 	if(create && (*create) < 0)
 		reduce = 1;
-	if(bw->bnum >= get_nod(fs, nod)->i_blocks / INOBLK)
+	inod = get_nod(fs, nod, &ni);
+	if(bw->bnum >= inod->i_blocks / INOBLK)
 	{
 		if(create && (*create) > 0)
 		{
 			(*create)--;
 			extend = 1;
 		}
-		else	
+		else
+		{
+			put_nod(ni);
 			return WALK_END;
+		}
 	}
+	iblk = inod->i_block;
 	// first direct block
 	if(bw->bpdir == EXT2_INIT_BLOCK)
 	{
-		bkref = &get_nod(fs, nod)->i_block[bw->bpdir = 0];
+		bkref = &iblk[bw->bpdir = 0];
 		if(extend) // allocate first block
 			*bkref = hole ? 0 : alloc_blk(fs,nod);
 		if(reduce) // free first block
@@ -994,7 +1056,7 @@ walk_bw(filesystem *fs, uint32 nod, blockwalker *bw, int32 *create, uint32 hole)
 	// direct block
 	else if(bw->bpdir < EXT2_NDIR_BLOCKS)
 	{
-		bkref = &get_nod(fs, nod)->i_block[++bw->bpdir];
+		bkref = &iblk[++bw->bpdir];
 		if(extend) // allocate block
 			*bkref = hole ? 0 : alloc_blk(fs,nod);
 		if(reduce) // free block
@@ -1007,10 +1069,10 @@ walk_bw(filesystem *fs, uint32 nod, blockwalker *bw, int32 *create, uint32 hole)
 		bw->bpdir = EXT2_IND_BLOCK;
 		bw->bpind = 0;
 		if(extend) // allocate indirect block
-			get_nod(fs, nod)->i_block[bw->bpdir] = alloc_blk(fs,nod);
+			iblk[bw->bpdir] = alloc_blk(fs,nod);
 		if(reduce) // free indirect block
-			free_blk(fs, get_nod(fs, nod)->i_block[bw->bpdir]);
-		b = (uint32*)get_blk(fs, get_nod(fs, nod)->i_block[bw->bpdir]);
+			free_blk(fs, iblk[bw->bpdir]);
+		b = (uint32*)get_blk(fs, iblk[bw->bpdir], &bi1);
 		bkref = &b[bw->bpind];
 		if(extend) // allocate first block
 			*bkref = hole ? 0 : alloc_blk(fs,nod);
@@ -1021,7 +1083,7 @@ walk_bw(filesystem *fs, uint32 nod, blockwalker *bw, int32 *create, uint32 hole)
 	else if((bw->bpdir == EXT2_IND_BLOCK) && (bw->bpind < BLOCKSIZE/4 - 1))
 	{
 		bw->bpind++;
-		b = (uint32*)get_blk(fs, get_nod(fs, nod)->i_block[bw->bpdir]);
+		b = (uint32*)get_blk(fs, iblk[bw->bpdir], &bi1);
 		bkref = &b[bw->bpind];
 		if(extend) // allocate block
 			*bkref = hole ? 0 : alloc_blk(fs,nod);
@@ -1036,15 +1098,15 @@ walk_bw(filesystem *fs, uint32 nod, blockwalker *bw, int32 *create, uint32 hole)
 		bw->bpind = 0;
 		bw->bpdind = 0;
 		if(extend) // allocate double indirect block
-			get_nod(fs, nod)->i_block[bw->bpdir] = alloc_blk(fs,nod);
+			iblk[bw->bpdir] = alloc_blk(fs,nod);
 		if(reduce) // free double indirect block
-			free_blk(fs, get_nod(fs, nod)->i_block[bw->bpdir]);
-		b = (uint32*)get_blk(fs, get_nod(fs, nod)->i_block[bw->bpdir]);
+			free_blk(fs, iblk[bw->bpdir]);
+		b = (uint32*)get_blk(fs, iblk[bw->bpdir], &bi1);
 		if(extend) // allocate first indirect block
 			b[bw->bpind] = alloc_blk(fs,nod);
 		if(reduce) // free  firstindirect block
 			free_blk(fs, b[bw->bpind]);
-		b = (uint32*)get_blk(fs, b[bw->bpind]);
+		b = (uint32*)get_blk(fs, b[bw->bpind], &bi1);
 		bkref = &b[bw->bpdind];
 		if(extend) // allocate first block
 			*bkref = hole ? 0 : alloc_blk(fs,nod);
@@ -1055,8 +1117,8 @@ walk_bw(filesystem *fs, uint32 nod, blockwalker *bw, int32 *create, uint32 hole)
 	else if((bw->bpdir == EXT2_DIND_BLOCK) && (bw->bpdind < BLOCKSIZE/4 - 1))
 	{
 		bw->bpdind++;
-		b = (uint32*)get_blk(fs, get_nod(fs, nod)->i_block[bw->bpdir]);
-		b = (uint32*)get_blk(fs, b[bw->bpind]);
+		b = (uint32*)get_blk(fs, iblk[bw->bpdir], &bi1);
+		b = (uint32*)get_blk(fs, b[bw->bpind], &bi2);
 		bkref = &b[bw->bpdind];
 		if(extend) // allocate block
 			*bkref = hole ? 0 : alloc_blk(fs,nod);
@@ -1069,12 +1131,12 @@ walk_bw(filesystem *fs, uint32 nod, blockwalker *bw, int32 *create, uint32 hole)
 		bw->bnum++;
 		bw->bpdind = 0;
 		bw->bpind++;
-		b = (uint32*)get_blk(fs, get_nod(fs, nod)->i_block[bw->bpdir]);
+		b = (uint32*)get_blk(fs, iblk[bw->bpdir], &bi1);
 		if(extend) // allocate indirect block
 			b[bw->bpind] = alloc_blk(fs,nod);
 		if(reduce) // free indirect block
 			free_blk(fs, b[bw->bpind]);
-		b = (uint32*)get_blk(fs, b[bw->bpind]);
+		b = (uint32*)get_blk(fs, b[bw->bpind], &bi2);
 		bkref = &b[bw->bpdind];
 		if(extend) // allocate first block
 			*bkref = hole ? 0 : alloc_blk(fs,nod);
@@ -1094,20 +1156,20 @@ walk_bw(filesystem *fs, uint32 nod, blockwalker *bw, int32 *create, uint32 hole)
 		bw->bpdind = 0;
 		bw->bptind = 0;
 		if(extend) // allocate triple indirect block
-			get_nod(fs, nod)->i_block[bw->bpdir] = alloc_blk(fs,nod);
+			iblk[bw->bpdir] = alloc_blk(fs,nod);
 		if(reduce) // free triple indirect block
-			free_blk(fs, get_nod(fs, nod)->i_block[bw->bpdir]);
-		b = (uint32*)get_blk(fs, get_nod(fs, nod)->i_block[bw->bpdir]);
+			free_blk(fs, iblk[bw->bpdir]);
+		b = (uint32*)get_blk(fs, iblk[bw->bpdir], &bi1);
 		if(extend) // allocate first double indirect block
 			b[bw->bpind] = alloc_blk(fs,nod);
 		if(reduce) // free first double indirect block
 			free_blk(fs, b[bw->bpind]);
-		b = (uint32*)get_blk(fs, b[bw->bpind]);
+		b = (uint32*)get_blk(fs, b[bw->bpind], &bi2);
 		if(extend) // allocate first indirect block
 			b[bw->bpdind] = alloc_blk(fs,nod);
 		if(reduce) // free first indirect block
 			free_blk(fs, b[bw->bpind]);
-		b = (uint32*)get_blk(fs, b[bw->bpdind]);
+		b = (uint32*)get_blk(fs, b[bw->bpdind], &bi3);
 		bkref = &b[bw->bptind];
 		if(extend) // allocate first data block
 			*bkref = hole ? 0 : alloc_blk(fs,nod);
@@ -1121,9 +1183,9 @@ walk_bw(filesystem *fs, uint32 nod, blockwalker *bw, int32 *create, uint32 hole)
 		  (bw->bptind < BLOCKSIZE/4 -1) )
 	{
 		bw->bptind++;
-		b = (uint32*)get_blk(fs, get_nod(fs, nod)->i_block[bw->bpdir]);
-		b = (uint32*)get_blk(fs, b[bw->bpind]);
-		b = (uint32*)get_blk(fs, b[bw->bpdind]);
+		b = (uint32*)get_blk(fs, iblk[bw->bpdir], &bi1);
+		b = (uint32*)get_blk(fs, b[bw->bpind], &bi2);
+		b = (uint32*)get_blk(fs, b[bw->bpdind], &bi3);
 		bkref = &b[bw->bptind];
 		if(extend) // allocate data block
 			*bkref = hole ? 0 : alloc_blk(fs,nod);
@@ -1140,13 +1202,13 @@ walk_bw(filesystem *fs, uint32 nod, blockwalker *bw, int32 *create, uint32 hole)
 		bw->bnum++;
 		bw->bptind = 0;
 		bw->bpdind++;
-		b = (uint32*)get_blk(fs, get_nod(fs, nod)->i_block[bw->bpdir]);
-		b = (uint32*)get_blk(fs, b[bw->bpind]);
+		b = (uint32*)get_blk(fs, iblk[bw->bpdir], &bi1);
+		b = (uint32*)get_blk(fs, b[bw->bpind], &bi2);
 		if(extend) // allocate single indirect block
 			b[bw->bpdind] = alloc_blk(fs,nod);
 		if(reduce) // free indirect block
 			free_blk(fs, b[bw->bpind]);
-		b = (uint32*)get_blk(fs, b[bw->bpdind]);
+		b = (uint32*)get_blk(fs, b[bw->bpdind], &bi3);
 		bkref = &b[bw->bptind];
 		if(extend) // allocate first data block
 			*bkref = hole ? 0 : alloc_blk(fs,nod);
@@ -1163,17 +1225,17 @@ walk_bw(filesystem *fs, uint32 nod, blockwalker *bw, int32 *create, uint32 hole)
 		bw->bpdind = 0;
 		bw->bptind = 0;
 		bw->bpind++;
-		b = (uint32*)get_blk(fs, get_nod(fs, nod)->i_block[bw->bpdir]);
+		b = (uint32*)get_blk(fs, iblk[bw->bpdir], &bi1);
 		if(extend) // allocate double indirect block
 			b[bw->bpind] = alloc_blk(fs,nod);
 		if(reduce) // free double indirect block
 			free_blk(fs, b[bw->bpind]);
-		b = (uint32*)get_blk(fs, b[bw->bpind]);
+		b = (uint32*)get_blk(fs, b[bw->bpind], &bi2);
 		if(extend) // allocate single indirect block
 			b[bw->bpdind] = alloc_blk(fs,nod);
 		if(reduce) // free indirect block
 			free_blk(fs, b[bw->bpind]);
-		b = (uint32*)get_blk(fs, b[bw->bpdind]);
+		b = (uint32*)get_blk(fs, b[bw->bpdind], &bi3);
 		bkref = &b[bw->bptind];
 		if(extend) // allocate first block
 			*bkref = hole ? 0 : alloc_blk(fs,nod);
@@ -1184,15 +1246,28 @@ walk_bw(filesystem *fs, uint32 nod, blockwalker *bw, int32 *create, uint32 hole)
 		error_msg_and_die("file too big !"); 
 	/* End change for walking triple indirection */
 
-	if(*bkref)
+	bk = *bkref;
+	if (bi3)
+		put_blk(bi3);
+	if (bi2)
+		put_blk(bi2);
+	if (bi1)
+		put_blk(bi1);
+
+	if(bk)
 	{
+		blk_info *bi;
+		uint8 *block;
 		bw->bnum++;
-		if(!reduce && !allocated(GRP_GET_BLOCK_BITMAP(fs,*bkref), GRP_BBM_OFFSET(fs,*bkref)))
-			error_msg_and_die("[block %d of inode %d is unallocated !]", *bkref, nod);
+		block = GRP_GET_BLOCK_BITMAP(fs,bk,&bi);
+		if(!reduce && !allocated(block, GRP_BBM_OFFSET(fs,bk)))
+			error_msg_and_die("[block %d of inode %d is unallocated !]", bk, nod);
+		GRP_PUT_BLOCK_BITMAP(bi);
 	}
 	if(extend)
-		get_nod(fs, nod)->i_blocks = bw->bnum * INOBLK;
-	return *bkref;
+		inod->i_blocks = bw->bnum * INOBLK;
+	put_nod(ni);
+	return bk;
 }
 
 // add blocks to an inode (file/dir/etc...)
@@ -1202,15 +1277,19 @@ extend_blk(filesystem *fs, uint32 nod, block b, int amount)
 	int create = amount;
 	blockwalker bw, lbw;
 	uint32 bk;
+	nod_info *ni;
+	inode *inod;
+
+	inod = get_nod(fs, nod, &ni);
 	init_bw(&bw);
 	if(amount < 0)
 	{
 		uint32 i;
-		for(i = 0; i < get_nod(fs, nod)->i_blocks / INOBLK + amount; i++)
+		for(i = 0; i < inod->i_blocks / INOBLK + amount; i++)
 			walk_bw(fs, nod, &bw, 0, 0);
 		while(walk_bw(fs, nod, &bw, &create, 0) != WALK_END)
 			/*nop*/;
-		get_nod(fs, nod)->i_blocks += amount * INOBLK;
+		inod->i_blocks += amount * INOBLK;
 	}
 	else
 	{
@@ -1232,8 +1311,11 @@ extend_blk(filesystem *fs, uint32 nod, block b, int amount)
 					}
 			if((bk = walk_bw(fs, nod, &bw, &create, !copyb)) == WALK_END)
 				break;
-			if(copyb)
-				memcpy(get_blk(fs, bk), b + BLOCKSIZE * (amount - create - 1), BLOCKSIZE);
+			if(copyb) {
+				blk_info *bi;
+				memcpy(get_blk(fs, bk, &bi), b + BLOCKSIZE * (amount - create - 1), BLOCKSIZE);
+				put_blk(bi);
+			}
 		}
 	}
 }
@@ -1245,12 +1327,14 @@ add2dir(filesystem *fs, uint32 dnod, uint32 nod, const char* name)
 	blockwalker bw;
 	uint32 bk;
 	uint8 *b;
+	blk_info *bi;
 	directory *d;
 	int reclen, nlen;
 	inode *node;
 	inode *pnode;
+	nod_info *dni, *ni;
 
-	pnode = get_nod(fs, dnod);
+	pnode = get_nod(fs, dnod, &dni);
 	if((pnode->i_mode & FM_IFMT) != FM_IFDIR)
 		error_msg_and_die("can't add '%s' to a non-directory", name);
 	if(!*name)
@@ -1264,7 +1348,7 @@ add2dir(filesystem *fs, uint32 dnod, uint32 nod, const char* name)
 	init_bw(&bw);
 	while((bk = walk_bw(fs, dnod, &bw, 0, 0)) != WALK_END) // for all blocks in dir
 	{
-		b = get_blk(fs, bk);
+		b = get_blk(fs, bk, &bi);
 		// for all dir entries in block
 		for(d = (directory*)b; (int8*)d + sizeof(*d) < (int8*)b + BLOCKSIZE; d = (directory*)((int8*)d + d->d_rec_len))
 		{
@@ -1272,11 +1356,12 @@ add2dir(filesystem *fs, uint32 dnod, uint32 nod, const char* name)
 			if((!d->d_inode) && (d->d_rec_len >= reclen))
 			{
 				d->d_inode = nod;
-				node = get_nod(fs, nod);
+				node = get_nod(fs, nod, &ni);
 				node->i_links_count++;
 				d->d_name_len = nlen;
 				strncpy(d->d_name, name, nlen);
-				return;
+				put_nod(ni);
+				goto out;
 			}
 			// if entry with enough room (last one?), shrink it & use it
 			if(d->d_rec_len >= (sizeof(directory) + rndup(d->d_name_len, 4) + reclen))
@@ -1287,11 +1372,12 @@ add2dir(filesystem *fs, uint32 dnod, uint32 nod, const char* name)
 				d = (directory*) (((int8*)d) + d->d_rec_len);
 				d->d_rec_len = reclen;
 				d->d_inode = nod;
-				node = get_nod(fs, nod);
+				node = get_nod(fs, nod, &ni);
 				node->i_links_count++;
 				d->d_name_len = nlen;
 				strncpy(d->d_name, name, nlen);
-				return;
+				put_nod(ni);
+				goto out;
 			}
 		}
 	}
@@ -1300,14 +1386,17 @@ add2dir(filesystem *fs, uint32 dnod, uint32 nod, const char* name)
 		error_msg_and_die("get_workblk() failed.");
 	d = (directory*)b;
 	d->d_inode = nod;
-	node = get_nod(fs, nod);
+	node = get_nod(fs, nod, &ni);
 	node->i_links_count++;
+	put_nod(ni);
 	d->d_rec_len = BLOCKSIZE;
 	d->d_name_len = nlen;
 	strncpy(d->d_name, name, nlen);
 	extend_blk(fs, dnod, b, 1);
-	get_nod(fs, dnod)->i_size += BLOCKSIZE;
+	pnode->i_size += BLOCKSIZE;
 	free_workblk(b);
+out:
+	put_nod(dni);
 }
 
 // find an entry in a directory
@@ -1316,16 +1405,20 @@ find_dir(filesystem *fs, uint32 nod, const char * name)
 {
 	blockwalker bw;
 	uint32 bk;
+	blk_info *bi;
 	int nlen = strlen(name);
 	init_bw(&bw);
 	while((bk = walk_bw(fs, nod, &bw, 0, 0)) != WALK_END)
 	{
 		directory *d;
 		uint8 *b;
-		b = get_blk(fs, bk);
+		b = get_blk(fs, bk, &bi);
 		for(d = (directory*)b; (int8*)d + sizeof(*d) < (int8*)b + BLOCKSIZE; d = (directory*)((int8*)d + d->d_rec_len))
-			if(d->d_inode && (nlen == d->d_name_len) && !strncmp(d->d_name, name, nlen))
+			if(d->d_inode && (nlen == d->d_name_len) && !strncmp(d->d_name, name, nlen)) {
+				put_blk(bi);
 				return d->d_inode;
+			}
+		put_blk(bi);
 	}
 	return 0;
 }
@@ -1361,10 +1454,12 @@ void
 chmod_fs(filesystem *fs, uint32 nod, uint16 mode, uint16 uid, uint16 gid)
 {
 	inode *node;
-	node = get_nod(fs, nod);
+	nod_info *ni;
+	node = get_nod(fs, nod, &ni);
 	node->i_mode = (node->i_mode & ~FM_IMASK) | (mode & FM_IMASK);
 	node->i_uid = uid;
 	node->i_gid = gid;
+	put_nod(ni);
 }
 
 // create a simple inode
@@ -1373,33 +1468,34 @@ mknod_fs(filesystem *fs, uint32 parent_nod, const char *name, uint16 mode, uint1
 {
 	uint32 nod;
 	inode *node;
+	nod_info *ni;
+
+	nod = alloc_nod(fs);
+	node = get_nod(fs, nod, &ni);
+	node->i_mode = mode;
+	add2dir(fs, parent_nod, nod, name);
+	switch(mode & FM_IFMT)
 	{
-		nod = alloc_nod(fs);
-		node = get_nod(fs, nod);
-		node->i_mode = mode;
-		add2dir(fs, parent_nod, nod, name);
-		switch(mode & FM_IFMT)
-		{
-			case FM_IFLNK:
-				mode = FM_IFLNK | FM_IRWXU | FM_IRWXG | FM_IRWXO;
-				break;
-			case FM_IFBLK:
-			case FM_IFCHR:
-				((uint8*)get_nod(fs, nod)->i_block)[0] = minor;
-				((uint8*)get_nod(fs, nod)->i_block)[1] = major;
-				break;
-			case FM_IFDIR:
-				add2dir(fs, nod, nod, ".");
-				add2dir(fs, nod, parent_nod, "..");
-				fs->gd[GRP_GROUP_OF_INODE(fs,nod)].bg_used_dirs_count++;
-				break;
-		}
+		case FM_IFLNK:
+			mode = FM_IFLNK | FM_IRWXU | FM_IRWXG | FM_IRWXO;
+			break;
+		case FM_IFBLK:
+		case FM_IFCHR:
+			((uint8*)node->i_block)[0] = minor;
+			((uint8*)node->i_block)[1] = major;
+			break;
+		case FM_IFDIR:
+			add2dir(fs, nod, nod, ".");
+			add2dir(fs, nod, parent_nod, "..");
+			fs->gd[GRP_GROUP_OF_INODE(fs,nod)].bg_used_dirs_count++;
+			break;
 	}
 	node->i_uid = uid;
 	node->i_gid = gid;
 	node->i_atime = mtime;
 	node->i_ctime = ctime;
 	node->i_mtime = mtime;
+	put_nod(ni);
 	return nod;
 }
 
@@ -1416,14 +1512,19 @@ static uint32
 mklink_fs(filesystem *fs, uint32 parent_nod, const char *name, size_t size, uint8 *b, uid_t uid, gid_t gid, uint32 ctime, uint32 mtime)
 {
 	uint32 nod = mknod_fs(fs, parent_nod, name, FM_IFLNK | FM_IRWXU | FM_IRWXG | FM_IRWXO, uid, gid, 0, 0, ctime, mtime);
-	extend_blk(fs, nod, 0, - (int)get_nod(fs, nod)->i_blocks / INOBLK);
-	get_nod(fs, nod)->i_size = size;
+	nod_info *ni;
+	inode *node = get_nod(fs, nod, &ni);
+
+	extend_blk(fs, nod, 0, - (int)node->i_blocks / INOBLK);
+	node->i_size = size;
 	if(size <= 4 * (EXT2_TIND_BLOCK+1))
 	{
-		strncpy((char*)get_nod(fs, nod)->i_block, (char*)b, size);
+		strncpy((char *)node->i_block, (char *)b, size);
+		put_nod(ni);
 		return nod;
 	}
 	extend_blk(fs, nod, b, rndup(size, BLOCKSIZE) / BLOCKSIZE);
+	put_nod(ni);
 	return nod;
 }
 
@@ -1433,8 +1534,11 @@ mkfile_fs(filesystem *fs, uint32 parent_nod, const char *name, uint32 mode, size
 {
 	uint8 * b;
 	uint32 nod = mknod_fs(fs, parent_nod, name, mode|FM_IFREG, uid, gid, 0, 0, ctime, mtime);
-	extend_blk(fs, nod, 0, - (int)get_nod(fs, nod)->i_blocks / INOBLK);
-	get_nod(fs, nod)->i_size = size;
+	nod_info *ni;
+	inode *node = get_nod(fs, nod, &ni);
+
+	extend_blk(fs, nod, 0, - (int)node->i_blocks / INOBLK);
+	node->i_size = size;
 	if (size) {
 		if(!(b = (uint8*)calloc(rndup(size, BLOCKSIZE), 1)))
 			error_msg_and_die("not enough mem to read file '%s'", name);
@@ -1444,6 +1548,7 @@ mkfile_fs(filesystem *fs, uint32 parent_nod, const char *name, uint32 mode, size
 		extend_blk(fs, nod, b, rndup(size, BLOCKSIZE) / BLOCKSIZE);
 		free(b);
 	}
+	put_nod(ni);
 	return nod;
 }
 
@@ -1766,6 +1871,7 @@ swap_goodblocks(filesystem *fs, inode *nod)
 	uint32 i,j;
 	int done=0;
 	uint32 *b,*b2;
+	blk_info *bi, *bi2, *bi3;
 
 	uint32 nblk = nod->i_blocks / INOBLK;
 	if((nod->i_size && !nblk) || ((nod->i_mode & FM_IFBLK) == FM_IFBLK) || ((nod->i_mode & FM_IFCHR) == FM_IFCHR))
@@ -1773,7 +1879,8 @@ swap_goodblocks(filesystem *fs, inode *nod)
 			nod->i_block[i] = swab32(nod->i_block[i]);
 	if(nblk <= EXT2_IND_BLOCK)
 		return;
-	swap_block(get_blk(fs, nod->i_block[EXT2_IND_BLOCK]));
+	swap_block(get_blk(fs, nod->i_block[EXT2_IND_BLOCK], &bi));
+	put_blk(bi);
 	if(nblk <= EXT2_DIND_BLOCK + BLOCKSIZE/4)
 		return;
 	/* Currently this will fail b'cos the number of blocks as stored
@@ -1791,29 +1898,37 @@ swap_goodblocks(filesystem *fs, inode *nod)
 	// ths function needs to be fixed for the same reasons - Xav
 	assert(nod->i_block[EXT2_DIND_BLOCK] != 0);
 	for(i = 0; i < BLOCKSIZE/4; i++)
-		if(nblk > EXT2_IND_BLOCK + BLOCKSIZE/4 + (BLOCKSIZE/4)*i )
-			swap_block(get_blk(fs, ((uint32*)get_blk(fs, nod->i_block[EXT2_DIND_BLOCK]))[i]));
-	swap_block(get_blk(fs, nod->i_block[EXT2_DIND_BLOCK]));
+		if(nblk > EXT2_IND_BLOCK + BLOCKSIZE/4 + (BLOCKSIZE/4)*i ) {
+			swap_block(get_blk(fs, ((uint32*)get_blk(fs, nod->i_block[EXT2_DIND_BLOCK], &bi))[i], &bi2));
+			put_blk(bi);
+			put_blk(bi2);
+		}
+	swap_block(get_blk(fs, nod->i_block[EXT2_DIND_BLOCK], &bi));
+	put_blk(bi);
 	if(nblk <= EXT2_IND_BLOCK + BLOCKSIZE/4 + BLOCKSIZE/4 * BLOCKSIZE/4)
 		return;
 	/* Adding support for triple indirection */
-	b = (uint32*)get_blk(fs,nod->i_block[EXT2_TIND_BLOCK]);
+	b = (uint32*)get_blk(fs,nod->i_block[EXT2_TIND_BLOCK], &bi);
 	for(i=0;i < BLOCKSIZE/4 && !done ; i++) {
-		b2 = (uint32*)get_blk(fs,b[i]); 
+		b2 = (uint32*)get_blk(fs,b[i], &bi2);
 		for(j=0; j<BLOCKSIZE/4;j++) {
 			if (nblk > ( EXT2_IND_BLOCK + BLOCKSIZE/4 + 
 				     (BLOCKSIZE/4)*(BLOCKSIZE/4) + 
 				     i*(BLOCKSIZE/4)*(BLOCKSIZE/4) + 
-				     j*(BLOCKSIZE/4)) ) 
-			  swap_block(get_blk(fs,b2[j]));
+				     j*(BLOCKSIZE/4)) )  {
+				swap_block(get_blk(fs,b2[j],&bi3));
+				put_blk(bi3);
+			}
 			else {
 			  done = 1;
 			  break;
 			}
 		}
 		swap_block((uint8 *)b2);
+		put_blk(bi2);
 	}
 	swap_block((uint8 *)b);
+	put_blk(bi);
 	return;
 }
 
@@ -1823,6 +1938,7 @@ swap_badblocks(filesystem *fs, inode *nod)
 	uint32 i,j;
 	int done=0;
 	uint32 *b,*b2;
+	blk_info *bi, *bi2, *bi3;
 
 	uint32 nblk = nod->i_blocks / INOBLK;
 	if((nod->i_size && !nblk) || ((nod->i_mode & FM_IFBLK) == FM_IFBLK) || ((nod->i_mode & FM_IFCHR) == FM_IFCHR))
@@ -1830,35 +1946,44 @@ swap_badblocks(filesystem *fs, inode *nod)
 			nod->i_block[i] = swab32(nod->i_block[i]);
 	if(nblk <= EXT2_IND_BLOCK)
 		return;
-	swap_block(get_blk(fs, nod->i_block[EXT2_IND_BLOCK]));
+	swap_block(get_blk(fs, nod->i_block[EXT2_IND_BLOCK], &bi));
+	put_blk(bi);
 	if(nblk <= EXT2_DIND_BLOCK + BLOCKSIZE/4)
 		return;
 	/* See comment in swap_goodblocks */
 	assert(nod->i_block[EXT2_DIND_BLOCK] != 0);
-	swap_block(get_blk(fs, nod->i_block[EXT2_DIND_BLOCK]));
+	swap_block(get_blk(fs, nod->i_block[EXT2_DIND_BLOCK], &bi));
+	put_blk(bi);
 	for(i = 0; i < BLOCKSIZE/4; i++)
-		if(nblk > EXT2_IND_BLOCK + BLOCKSIZE/4 + (BLOCKSIZE/4)*i )
-			swap_block(get_blk(fs, ((uint32*)get_blk(fs, nod->i_block[EXT2_DIND_BLOCK]))[i]));
+		if(nblk > EXT2_IND_BLOCK + BLOCKSIZE/4 + (BLOCKSIZE/4)*i ) {
+			swap_block(get_blk(fs, ((uint32*)get_blk(fs, nod->i_block[EXT2_DIND_BLOCK],&bi))[i], &bi2));
+			put_blk(bi);
+			put_blk(bi2);
+		}
 	if(nblk <= EXT2_IND_BLOCK + BLOCKSIZE/4 + BLOCKSIZE/4 * BLOCKSIZE/4)
 		return;
 	/* Adding support for triple indirection */
-	b = (uint32*)get_blk(fs,nod->i_block[EXT2_TIND_BLOCK]);
+	b = (uint32*)get_blk(fs,nod->i_block[EXT2_TIND_BLOCK],&bi);
 	swap_block((uint8 *)b);
 	for(i=0;i < BLOCKSIZE/4 && !done ; i++) {
-		b2 = (uint32*)get_blk(fs,b[i]); 
+		b2 = (uint32*)get_blk(fs,b[i],&bi2);
 		swap_block((uint8 *)b2);
 		for(j=0; j<BLOCKSIZE/4;j++) {
 			if (nblk > ( EXT2_IND_BLOCK + BLOCKSIZE/4 + 
 				     (BLOCKSIZE/4)*(BLOCKSIZE/4) + 
 				     i*(BLOCKSIZE/4)*(BLOCKSIZE/4) + 
-				     j*(BLOCKSIZE/4)) ) 
-			  swap_block(get_blk(fs,b2[j]));
+				     j*(BLOCKSIZE/4)) ) {
+				swap_block(get_blk(fs,b2[j],&bi3));
+				put_blk(bi3);
+			}
 			else {
 			  done = 1;
 			  break;
 			}
 		}
+		put_blk(bi2);
 	}
+	put_blk(bi);
 	return;
 }
 
@@ -1867,9 +1992,11 @@ static void
 swap_goodfs(filesystem *fs)
 {
 	uint32 i;
+	nod_info *ni;
+
 	for(i = 1; i < fs->sb.s_inodes_count; i++)
 	{
-		inode *nod = get_nod(fs, i);
+		inode *nod = get_nod(fs, i, &ni);
 		if(nod->i_mode & FM_IFDIR)
 		{
 			blockwalker bw;
@@ -1879,13 +2006,16 @@ swap_goodfs(filesystem *fs)
 			{
 				directory *d;
 				uint8 *b;
-				b = get_blk(fs, bk);
+				blk_info *bi;
+				b = get_blk(fs, bk, &bi);
 				for(d = (directory*)b; (int8*)d + sizeof(*d) < (int8*)b + BLOCKSIZE; d = (directory*)((int8*)d + swab16(d->d_rec_len)))
 					swap_dir(d);
+				put_blk(bi);
 			}
 		}
 		swap_goodblocks(fs, nod);
 		swap_nod(nod);
+		put_nod(ni);
 	}
 	for(i=0;i<GRP_NBGROUPS(fs);i++)
 		swap_gd(&(fs->gd[i]));
@@ -1901,7 +2031,8 @@ swap_badfs(filesystem *fs)
 		swap_gd(&(fs->gd[i]));
 	for(i = 1; i < fs->sb.s_inodes_count; i++)
 	{
-		inode *nod = get_nod(fs, i);
+		nod_info *ni;
+		inode *nod = get_nod(fs, i, &ni);
 		swap_nod(nod);
 		swap_badblocks(fs, nod);
 		if(nod->i_mode & FM_IFDIR)
@@ -1913,9 +2044,11 @@ swap_badfs(filesystem *fs)
 			{
 				directory *d;
 				uint8 *b;
-				b = get_blk(fs, bk);
+				blk_info *bi;
+				b = get_blk(fs, bk, &bi);
 				for(d = (directory*)b; (int8*)d + sizeof(*d) < (int8*)b + BLOCKSIZE; d = (directory*)((int8*)d + d->d_rec_len))
 					swap_dir(d);
+				put_blk(bi);
 			}
 		}
 	}
@@ -1936,6 +2069,8 @@ init_fs(int nbblocks, int nbinodes, int nbresrvd, int holes, uint32 fs_timestamp
 	uint32 j;
 	uint8 *bbm,*ibm;
 	inode *itab0;
+	blk_info *bi;
+	nod_info *ni;
 	
 	if(nbresrvd < 0)
 		error_msg_and_die("reserved blocks value is invalid. Note: options have changed, see --help or the man page.");
@@ -2014,9 +2149,8 @@ init_fs(int nbblocks, int nbinodes, int nbresrvd, int holes, uint32 fs_timestamp
 	/* Mark non-filesystem blocks and inodes as allocated */
 	/* Mark system blocks and inodes as allocated         */
 	for(i = 0; i<nbgroups;i++) {
-
 		/* Block bitmap */
-		bbm = get_blk(fs,fs->gd[i].bg_block_bitmap);	
+		bbm = get_blk(fs,fs->gd[i].bg_block_bitmap, &bi);
 		//non-filesystem blocks
 		for(j = fs->gd[i].bg_free_blocks_count
 		        + overhead_per_group + 1; j <= BLOCKSIZE * 8; j++)
@@ -2024,9 +2158,10 @@ init_fs(int nbblocks, int nbinodes, int nbresrvd, int holes, uint32 fs_timestamp
 		//system blocks
 		for(j = 1; j <= overhead_per_group; j++)
 			allocate(bbm, j); 
+		put_blk(bi);
 
 		/* Inode bitmap */
-		ibm = get_blk(fs,fs->gd[i].bg_inode_bitmap);	
+		ibm = get_blk(fs,fs->gd[i].bg_inode_bitmap, &bi);
 		//non-filesystem inodes
 		for(j = fs->sb.s_inodes_per_group+1; j <= BLOCKSIZE * 8; j++)
 			allocate(ibm, j);
@@ -2035,6 +2170,7 @@ init_fs(int nbblocks, int nbinodes, int nbresrvd, int holes, uint32 fs_timestamp
 		if(i == 0)
 			for(j = 1; j < EXT2_FIRST_INO; j++)
 				allocate(ibm, j);
+		put_blk(bi);
 	}
 
 	// make root inode and directory
@@ -2042,13 +2178,14 @@ init_fs(int nbblocks, int nbinodes, int nbresrvd, int holes, uint32 fs_timestamp
 	/* Also increment the directory count for group 0 */
 	fs->gd[0].bg_free_inodes_count--;
 	fs->gd[0].bg_used_dirs_count = 1;
-	itab0 = (inode *)get_blk(fs,fs->gd[0].bg_inode_table);
-	itab0[EXT2_ROOT_INO-1].i_mode = FM_IFDIR | FM_IRWXU | FM_IRGRP | FM_IROTH | FM_IXGRP | FM_IXOTH; 
-	itab0[EXT2_ROOT_INO-1].i_ctime = fs_timestamp;
-	itab0[EXT2_ROOT_INO-1].i_mtime = fs_timestamp;
-	itab0[EXT2_ROOT_INO-1].i_atime = fs_timestamp;
-	itab0[EXT2_ROOT_INO-1].i_size = BLOCKSIZE;
-	itab0[EXT2_ROOT_INO-1].i_links_count = 2;
+	itab0 = get_nod(fs, EXT2_ROOT_INO, &ni);
+	itab0->i_mode = FM_IFDIR | FM_IRWXU | FM_IRGRP | FM_IROTH | FM_IXGRP | FM_IXOTH;
+	itab0->i_ctime = fs_timestamp;
+	itab0->i_mtime = fs_timestamp;
+	itab0->i_atime = fs_timestamp;
+	itab0->i_size = BLOCKSIZE;
+	itab0->i_links_count = 2;
+	put_nod(ni);
 
 	if(!(b = get_workblk()))
 		error_msg_and_die("get_workblk() failed.");
@@ -2067,6 +2204,8 @@ init_fs(int nbblocks, int nbinodes, int nbresrvd, int holes, uint32 fs_timestamp
 	// make lost+found directory and reserve blocks
 	if(fs->sb.s_r_blocks_count)
 	{
+		inode *node;
+
 		nod = mkdir_fs(fs, EXT2_ROOT_INO, "lost+found", FM_IRWXU, 0, 0, fs_timestamp, fs_timestamp);
 		memset(b, 0, BLOCKSIZE);
 		((directory*)b)->d_rec_len = BLOCKSIZE;
@@ -2077,7 +2216,9 @@ init_fs(int nbblocks, int nbinodes, int nbresrvd, int holes, uint32 fs_timestamp
 			fs->sb.s_r_blocks_count = fs->sb.s_blocks_count * MAX_RESERVED_BLOCKS;
 		for(i = 1; i < fs->sb.s_r_blocks_count; i++)
 			extend_blk(fs, nod, b, 1);
-		get_nod(fs, nod)->i_size = fs->sb.s_r_blocks_count * BLOCKSIZE;
+		node = get_nod(fs, nod, &ni);
+		node->i_size = fs->sb.s_r_blocks_count * BLOCKSIZE;
+		put_nod(ni);
 	}
 	free_workblk(b);
 
@@ -2153,16 +2294,23 @@ write_blocks(filesystem *fs, uint32 nod, FILE* f)
 {
 	blockwalker bw;
 	uint32 bk;
-	int32 fsize = get_nod(fs, nod)->i_size;
+	nod_info *ni;
+	inode *node = get_nod(fs, nod, &ni);
+	int32 fsize = node->i_size;
+	blk_info *bi;
+
 	init_bw(&bw);
 	while((bk = walk_bw(fs, nod, &bw, 0, 0)) != WALK_END)
 	{
 		if(fsize <= 0)
 			error_msg_and_die("wrong size while saving inode %d", nod);
-		if(fwrite(get_blk(fs, bk), (fsize > BLOCKSIZE) ? BLOCKSIZE : fsize, 1, f) != 1)
+		if(fwrite(get_blk(fs, bk, &bi),
+			  (fsize > BLOCKSIZE) ? BLOCKSIZE : fsize, 1, f) != 1)
 			error_msg_and_die("error while saving inode %d", nod);
+		put_blk(bi);
 		fsize -= BLOCKSIZE;
 	}
+	put_nod(ni);
 }
 
 
@@ -2171,8 +2319,11 @@ static void
 print_dev(filesystem *fs, uint32 nod)
 {
 	int minor, major;
-	minor = ((uint8*)get_nod(fs, nod)->i_block)[0];
-	major = ((uint8*)get_nod(fs, nod)->i_block)[1];
+	nod_info *ni;
+	inode *node = get_nod(fs, nod, &ni);
+	minor = ((uint8*)node->i_block)[0];
+	major = ((uint8*)node->i_block)[1];
+	put_nod(ni);
 	printf("major: %d, minor: %d\n", major, minor);
 }
 
@@ -2188,7 +2339,8 @@ print_dir(filesystem *fs, uint32 nod)
 	{
 		directory *d;
 		uint8 *b;
-		b = get_blk(fs, bk);
+		blk_info *bi;
+		b = get_blk(fs, bk, &bi);
 		for(d = (directory*)b; (int8*)d + sizeof(*d) < (int8*)b + BLOCKSIZE; d = (directory*)((int8*)d + d->d_rec_len))
 			if(d->d_inode)
 			{
@@ -2198,6 +2350,7 @@ print_dir(filesystem *fs, uint32 nod)
 					putchar(d->d_name[i]);
 				printf("' (inode %d): rec_len: %d (name_len: %d)\n", d->d_inode, d->d_rec_len, d->d_name_len);
 			}
+		put_blk(bi);
 	}
 }
 
@@ -2205,14 +2358,18 @@ print_dir(filesystem *fs, uint32 nod)
 static void
 print_link(filesystem *fs, uint32 nod)
 {
-	if(!get_nod(fs, nod)->i_blocks)
-		printf("links to '%s'\n", (char*)get_nod(fs, nod)->i_block);
+	nod_info *ni;
+	inode *node = get_nod(fs, nod, &ni);
+
+	if(!node->i_blocks)
+		printf("links to '%s'\n", (char*)node->i_block);
 	else
 	{
 		printf("links to '");
 		write_blocks(fs, nod, stdout);
 		printf("'\n");
 	}
+	put_nod(ni);
 }
 
 // make a ls-like printout of permissions
@@ -2281,8 +2438,12 @@ print_inode(filesystem *fs, uint32 nod)
 {
 	char *s;
 	char perms[11];
-	if(!get_nod(fs, nod)->i_mode)
-		return;
+	nod_info *ni;
+	inode *node = get_nod(fs, nod, &ni);
+	blk_info *bi;
+
+	if(!node->i_mode)
+		goto out;
 	switch(nod)
 	{
 		case EXT2_BAD_INO:
@@ -2304,15 +2465,18 @@ print_inode(filesystem *fs, uint32 nod)
 		default:
 			s = (nod >= EXT2_FIRST_INO) ? "normal" : "unknown reserved"; 
 	}
-	printf("inode %d (%s, %d links): ", nod, s, get_nod(fs, nod)->i_links_count);
-	if(!allocated(GRP_GET_INODE_BITMAP(fs,nod), GRP_IBM_OFFSET(fs,nod)))
+	printf("inode %d (%s, %d links): ", nod, s, node->i_links_count);
+	if(!allocated(GRP_GET_INODE_BITMAP(fs,nod,&bi), GRP_IBM_OFFSET(fs,nod)))
 	{
+		GRP_PUT_INODE_BITMAP(bi);
 		printf("unallocated\n");
-		return;
+		goto out;
 	}
-	make_perms(get_nod(fs, nod)->i_mode, perms);
-	printf("%s,  size: %d byte%s (%d block%s)\n", perms, plural(get_nod(fs, nod)->i_size), plural(get_nod(fs, nod)->i_blocks / INOBLK));
-	switch(get_nod(fs, nod)->i_mode & FM_IFMT)
+	GRP_PUT_INODE_BITMAP(bi);
+	make_perms(node->i_mode, perms);
+	printf("%s,  size: %d byte%s (%d block%s)\n", perms,
+	       plural(node->i_size), plural(node->i_blocks / INOBLK));
+	switch(node->i_mode & FM_IFMT)
 	{
 		case FM_IFSOCK:
 			list_blocks(fs, nod);
@@ -2340,6 +2504,8 @@ print_inode(filesystem *fs, uint32 nod)
 			list_blocks(fs, nod);
 	}
 	printf("Done with inode %d\n",nod);
+out:
+	put_nod(ni);
 }
 
 // describes various fields in a filesystem
@@ -2347,6 +2513,7 @@ static void
 print_fs(filesystem *fs)
 {
 	uint32 i;
+	blk_info *bi;
 	uint8 *ibm;
 
 	printf("%d blocks (%d free, %d reserved), first data block: %d\n",
@@ -2369,13 +2536,16 @@ print_fs(filesystem *fs)
 		     fs->gd[i].bg_block_bitmap, fs->gd[i].bg_inode_bitmap,
 		     fs->gd[i].bg_inode_table);
 		printf("block bitmap allocation:\n");
-		print_bm(GRP_GET_GROUP_BBM(fs, i),fs->sb.s_blocks_per_group);
+		print_bm(GRP_GET_GROUP_BBM(fs, i, &bi),
+			 fs->sb.s_blocks_per_group);
+		GRP_PUT_GROUP_BBM(bi);
 		printf("inode bitmap allocation:\n");
-		ibm = GRP_GET_GROUP_IBM(fs, i);
+		ibm = GRP_GET_GROUP_IBM(fs, i, &bi);
 		print_bm(ibm, fs->sb.s_inodes_per_group);
 		for (i = 1; i <= fs->sb.s_inodes_per_group; i++)
 			if (allocated(ibm, i))
 				print_inode(fs, i);
+		GRP_PUT_GROUP_IBM(bi);
 	}
 }
 
@@ -2646,9 +2816,17 @@ main(int argc, char **argv)
 
 	if(emptyval) {
 		uint32 b;
-		for(b = 1; b < fs->sb.s_blocks_count; b++)
-			if(!allocated(GRP_GET_BLOCK_BITMAP(fs,b),GRP_BBM_OFFSET(fs,b)))
-				memset(get_blk(fs, b), emptyval, BLOCKSIZE);
+		for(b = 1; b < fs->sb.s_blocks_count; b++) {
+			blk_info *bi;
+			if(!allocated(GRP_GET_BLOCK_BITMAP(fs,b,&bi),
+				      GRP_BBM_OFFSET(fs,b))) {
+				blk_info *bi2;
+				memset(get_blk(fs, b, &bi2), emptyval,
+				       BLOCKSIZE);
+				put_blk(bi2);
+			}
+			GRP_PUT_BLOCK_BITMAP(bi);
+		}
 	}
 	if(verbose)
 		print_fs(fs);
@@ -2658,13 +2836,15 @@ main(int argc, char **argv)
 		char fname[MAX_FILENAME];
 		char *p;
 		FILE *fh;
+		nod_info *ni;
 		if(!(nod = find_path(fs, EXT2_ROOT_INO, gopt[i])))
 			error_msg_and_die("path %s not found in filesystem", gopt[i]);
 		while((p = strchr(gopt[i], '/')))
 			*p = '_';
 		SNPRINTF(fname, MAX_FILENAME-1, "%s.blk", gopt[i]);
 		fh = xfopen(fname, "wb");
-		fprintf(fh, "%d:", get_nod(fs, nod)->i_size);
+		fprintf(fh, "%d:", get_nod(fs, nod, &ni)->i_size);
+		put_nod(ni);
 		flist_blocks(fs, nod, fh);
 		fclose(fh);
 	}
-- 
1.7.4.1