forked from dlorch/hammer-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhammer_ondisk.c
1534 lines (1406 loc) · 39.3 KB
/
hammer_ondisk.c
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
/*
* Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
*
* This code is derived from software contributed to The DragonFly Project
* by Matthew Dillon <[email protected]>
*
* 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.
* 3. Neither the name of The DragonFly Project nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific, prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
* COPYRIGHT HOLDERS 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.
*
* $DragonFly: src/sys/vfs/hammer/hammer_ondisk.c,v 1.76 2008/08/29 20:19:08 dillon Exp $
*/
/*
* Manage HAMMER's on-disk structures. These routines are primarily
* responsible for interfacing with the kernel's I/O subsystem and for
* managing in-memory structures.
*/
#include "dfly_wrap.h"
#include "hammer.h"
#include <sys/fcntl.h>
#include <sys/nlookup.h>
#include <sys/buf.h>
#include <sys/buf2.h>
static void hammer_free_volume(hammer_volume_t volume);
static int hammer_load_volume(hammer_volume_t volume);
static int hammer_load_buffer(hammer_buffer_t buffer, int isnew);
static int hammer_load_node(hammer_node_t node, int isnew);
static int
hammer_vol_rb_compare(hammer_volume_t vol1, hammer_volume_t vol2)
{
if (vol1->vol_no < vol2->vol_no)
return(-1);
if (vol1->vol_no > vol2->vol_no)
return(1);
return(0);
}
static int
hammer_buf_rb_compare(hammer_buffer_t buf1, hammer_buffer_t buf2)
{
if (buf1->zoneX_offset < buf2->zoneX_offset)
return(-1);
if (buf1->zoneX_offset > buf2->zoneX_offset)
return(1);
return(0);
}
static int
hammer_nod_rb_compare(hammer_node_t node1, hammer_node_t node2)
{
if (node1->node_offset < node2->node_offset)
return(-1);
if (node1->node_offset > node2->node_offset)
return(1);
return(0);
}
RB_GENERATE2(hammer_vol_rb_tree, hammer_volume, rb_node,
hammer_vol_rb_compare, int32_t, vol_no);
RB_GENERATE2(hammer_buf_rb_tree, hammer_buffer, rb_node,
hammer_buf_rb_compare, hammer_off_t, zoneX_offset);
RB_GENERATE2(hammer_nod_rb_tree, hammer_node, rb_node,
hammer_nod_rb_compare, hammer_off_t, node_offset);
/************************************************************************
* VOLUMES *
************************************************************************
*
* Load a HAMMER volume by name. Returns 0 on success or a positive error
* code on failure. Volumes must be loaded at mount time, get_volume() will
* not load a new volume.
*
* Calls made to hammer_load_volume() or single-threaded
*/
int
hammer_install_volume(struct hammer_mount *hmp, const char *volname,
struct vnode *devvp)
{
struct mount *mp;
hammer_volume_t volume;
struct hammer_volume_ondisk *ondisk;
struct nlookupdata nd;
struct buf *bp = NULL;
int error;
int ronly;
int setmp = 0;
mp = hmp->mp;
ronly = ((mp->mnt_flag & MNT_RDONLY) ? 1 : 0);
/*
* Allocate a volume structure
*/
++hammer_count_volumes;
volume = kmalloc(sizeof(*volume), hmp->m_misc, M_WAITOK|M_ZERO);
volume->vol_name = kstrdup(volname, hmp->m_misc);
volume->io.hmp = hmp; /* bootstrap */
hammer_io_init(&volume->io, volume, HAMMER_STRUCTURE_VOLUME);
volume->io.offset = 0LL;
volume->io.bytes = HAMMER_BUFSIZE;
/*
* Get the device vnode
*/
if (devvp == NULL) {
error = nlookup_init(&nd, volume->vol_name, UIO_SYSSPACE, NLC_FOLLOW);
if (error == 0)
error = nlookup(&nd);
if (error == 0)
error = cache_vref(&nd.nl_nch, nd.nl_cred, &volume->devvp);
nlookup_done(&nd);
} else {
error = 0;
volume->devvp = devvp;
}
if (error == 0) {
if (vn_isdisk(volume->devvp, &error)) {
error = vfs_mountedon(volume->devvp);
}
}
if (error == 0 &&
count_udev(volume->devvp->v_umajor, volume->devvp->v_uminor) > 0) {
error = EBUSY;
}
if (error == 0) {
vn_lock(volume->devvp, LK_EXCLUSIVE | LK_RETRY);
error = vinvalbuf(volume->devvp, V_SAVE, 0, 0);
if (error == 0) {
error = VOP_OPEN(volume->devvp,
(ronly ? FREAD : FREAD|FWRITE),
FSCRED, NULL);
}
vn_unlock(volume->devvp);
}
if (error) {
hammer_free_volume(volume);
return(error);
}
volume->devvp->v_rdev->si_mountpoint = mp;
setmp = 1;
/*
* Extract the volume number from the volume header and do various
* sanity checks.
*/
error = bread(volume->devvp, 0LL, HAMMER_BUFSIZE, &bp);
if (error)
goto late_failure;
ondisk = (void *)bp->b_data;
if (ondisk->vol_signature != HAMMER_FSBUF_VOLUME) {
kprintf("hammer_mount: volume %s has an invalid header\n",
volume->vol_name);
error = EFTYPE;
goto late_failure;
}
volume->vol_no = ondisk->vol_no;
volume->buffer_base = ondisk->vol_buf_beg;
volume->vol_flags = ondisk->vol_flags;
volume->nblocks = ondisk->vol_nblocks;
volume->maxbuf_off = HAMMER_ENCODE_RAW_BUFFER(volume->vol_no,
ondisk->vol_buf_end - ondisk->vol_buf_beg);
volume->maxraw_off = ondisk->vol_buf_end;
if (RB_EMPTY(&hmp->rb_vols_root)) {
hmp->fsid = ondisk->vol_fsid;
} else if (bcmp(&hmp->fsid, &ondisk->vol_fsid, sizeof(uuid_t))) {
kprintf("hammer_mount: volume %s's fsid does not match "
"other volumes\n", volume->vol_name);
error = EFTYPE;
goto late_failure;
}
/*
* Insert the volume structure into the red-black tree.
*/
if (RB_INSERT(hammer_vol_rb_tree, &hmp->rb_vols_root, volume)) {
kprintf("hammer_mount: volume %s has a duplicate vol_no %d\n",
volume->vol_name, volume->vol_no);
error = EEXIST;
}
/*
* Set the root volume . HAMMER special cases rootvol the structure.
* We do not hold a ref because this would prevent related I/O
* from being flushed.
*/
if (error == 0 && ondisk->vol_rootvol == ondisk->vol_no) {
hmp->rootvol = volume;
hmp->nvolumes = ondisk->vol_count;
if (bp) {
dfly_brelse(bp);
bp = NULL;
}
hmp->mp->mnt_stat.f_blocks += ondisk->vol0_stat_bigblocks *
(HAMMER_LARGEBLOCK_SIZE / HAMMER_BUFSIZE);
hmp->mp->mnt_vstat.f_blocks += ondisk->vol0_stat_bigblocks *
(HAMMER_LARGEBLOCK_SIZE / HAMMER_BUFSIZE);
}
late_failure:
if (bp)
dfly_brelse(bp);
if (error) {
/*vinvalbuf(volume->devvp, V_SAVE, 0, 0);*/
if (setmp)
volume->devvp->v_rdev->si_mountpoint = NULL;
VOP_CLOSE(volume->devvp, ronly ? FREAD : FREAD|FWRITE);
hammer_free_volume(volume);
}
return (error);
}
/*
* This is called for each volume when updating the mount point from
* read-write to read-only or vise-versa.
*/
int
hammer_adjust_volume_mode(hammer_volume_t volume, void *data __unused)
{
if (volume->devvp) {
vn_lock(volume->devvp, LK_EXCLUSIVE | LK_RETRY);
if (volume->io.hmp->ronly) {
/* do not call vinvalbuf */
VOP_OPEN(volume->devvp, FREAD, FSCRED, NULL);
VOP_CLOSE(volume->devvp, FREAD|FWRITE);
} else {
/* do not call vinvalbuf */
VOP_OPEN(volume->devvp, FREAD|FWRITE, FSCRED, NULL);
VOP_CLOSE(volume->devvp, FREAD);
}
vn_unlock(volume->devvp);
}
return(0);
}
/*
* Unload and free a HAMMER volume. Must return >= 0 to continue scan
* so returns -1 on failure.
*/
int
hammer_unload_volume(hammer_volume_t volume, void *data __unused)
{
hammer_mount_t hmp = volume->io.hmp;
int ronly = ((hmp->mp->mnt_flag & MNT_RDONLY) ? 1 : 0);
struct buf *bp;
/*
* Clean up the root volume pointer, which is held unlocked in hmp.
*/
if (hmp->rootvol == volume)
hmp->rootvol = NULL;
/*
* We must not flush a dirty buffer to disk on umount. It should
* have already been dealt with by the flusher, or we may be in
* catastrophic failure.
*/
hammer_io_clear_modify(&volume->io, 1);
volume->io.waitdep = 1;
bp = hammer_io_release(&volume->io, 1);
/*
* Clean up the persistent ref ioerror might have on the volume
*/
if (volume->io.ioerror) {
volume->io.ioerror = 0;
hammer_unref(&volume->io.lock);
}
/*
* There should be no references on the volume, no clusters, and
* no super-clusters.
*/
KKASSERT(volume->io.lock.refs == 0);
if (bp)
dfly_brelse(bp);
volume->ondisk = NULL;
if (volume->devvp) {
if (volume->devvp->v_rdev &&
volume->devvp->v_rdev->si_mountpoint == hmp->mp
) {
volume->devvp->v_rdev->si_mountpoint = NULL;
}
if (ronly) {
/*
* Make sure we don't sync anything to disk if we
* are in read-only mode (1) or critically-errored
* (2). Note that there may be dirty buffers in
* normal read-only mode from crash recovery.
*/
vinvalbuf(volume->devvp, 0, 0, 0);
VOP_CLOSE(volume->devvp, FREAD);
} else {
/*
* Normal termination, save any dirty buffers
* (XXX there really shouldn't be any).
*/
vinvalbuf(volume->devvp, V_SAVE, 0, 0);
VOP_CLOSE(volume->devvp, FREAD|FWRITE);
}
}
/*
* Destroy the structure
*/
RB_REMOVE(hammer_vol_rb_tree, &hmp->rb_vols_root, volume);
hammer_free_volume(volume);
return(0);
}
static
void
hammer_free_volume(hammer_volume_t volume)
{
hammer_mount_t hmp = volume->io.hmp;
if (volume->vol_name) {
kfree(volume->vol_name, hmp->m_misc);
volume->vol_name = NULL;
}
if (volume->devvp) {
vrele(volume->devvp);
volume->devvp = NULL;
}
--hammer_count_volumes;
kfree(volume, hmp->m_misc);
}
/*
* Get a HAMMER volume. The volume must already exist.
*/
hammer_volume_t
hammer_get_volume(struct hammer_mount *hmp, int32_t vol_no, int *errorp)
{
struct hammer_volume *volume;
/*
* Locate the volume structure
*/
volume = RB_LOOKUP(hammer_vol_rb_tree, &hmp->rb_vols_root, vol_no);
if (volume == NULL) {
*errorp = ENOENT;
return(NULL);
}
hammer_ref(&volume->io.lock);
/*
* Deal with on-disk info
*/
if (volume->ondisk == NULL || volume->io.loading) {
*errorp = hammer_load_volume(volume);
if (*errorp) {
hammer_rel_volume(volume, 1);
volume = NULL;
}
} else {
*errorp = 0;
}
return(volume);
}
int
hammer_ref_volume(hammer_volume_t volume)
{
int error;
hammer_ref(&volume->io.lock);
/*
* Deal with on-disk info
*/
if (volume->ondisk == NULL || volume->io.loading) {
error = hammer_load_volume(volume);
if (error)
hammer_rel_volume(volume, 1);
} else {
error = 0;
}
return (error);
}
hammer_volume_t
hammer_get_root_volume(struct hammer_mount *hmp, int *errorp)
{
hammer_volume_t volume;
volume = hmp->rootvol;
KKASSERT(volume != NULL);
hammer_ref(&volume->io.lock);
/*
* Deal with on-disk info
*/
if (volume->ondisk == NULL || volume->io.loading) {
*errorp = hammer_load_volume(volume);
if (*errorp) {
hammer_rel_volume(volume, 1);
volume = NULL;
}
} else {
*errorp = 0;
}
return (volume);
}
/*
* Load a volume's on-disk information. The volume must be referenced and
* not locked. We temporarily acquire an exclusive lock to interlock
* against releases or multiple get's.
*/
static int
hammer_load_volume(hammer_volume_t volume)
{
int error;
++volume->io.loading;
hammer_lock_ex(&volume->io.lock);
if (volume->ondisk == NULL) {
error = hammer_io_read(volume->sb, &volume->io,
volume->maxraw_off);
if (error == 0)
volume->ondisk = (void *)volume->io.bp->b_data;
} else {
error = 0;
}
--volume->io.loading;
hammer_unlock(&volume->io.lock);
return(error);
}
/*
* Release a volume. Call hammer_io_release on the last reference. We have
* to acquire an exclusive lock to interlock against volume->ondisk tests
* in hammer_load_volume(), and hammer_io_release() also expects an exclusive
* lock to be held.
*
* Volumes are not unloaded from memory during normal operation.
*/
void
hammer_rel_volume(hammer_volume_t volume, int flush)
{
struct buf *bp = NULL;
crit_enter();
if (volume->io.lock.refs == 1) {
++volume->io.loading;
hammer_lock_ex(&volume->io.lock);
if (volume->io.lock.refs == 1) {
volume->ondisk = NULL;
bp = hammer_io_release(&volume->io, flush);
}
--volume->io.loading;
hammer_unlock(&volume->io.lock);
}
hammer_unref(&volume->io.lock);
if (bp)
dfly_brelse(bp);
crit_exit();
}
int
hammer_mountcheck_volumes(struct hammer_mount *hmp)
{
hammer_volume_t vol;
int i;
for (i = 0; i < hmp->nvolumes; ++i) {
vol = RB_LOOKUP(hammer_vol_rb_tree, &hmp->rb_vols_root, i);
if (vol == NULL)
return(EINVAL);
}
return(0);
}
/************************************************************************
* BUFFERS *
************************************************************************
*
* Manage buffers. Currently all blockmap-backed zones are translated
* to zone-2 buffer offsets.
*/
hammer_buffer_t
hammer_get_buffer(hammer_mount_t hmp, hammer_off_t buf_offset,
int bytes, int isnew, int *errorp)
{
hammer_buffer_t buffer;
hammer_volume_t volume;
hammer_off_t zone2_offset;
hammer_io_type_t iotype;
int vol_no;
int zone;
buf_offset &= ~HAMMER_BUFMASK64;
again:
/*
* Shortcut if the buffer is already cached
*/
buffer = RB_LOOKUP(hammer_buf_rb_tree, &hmp->rb_bufs_root, buf_offset);
if (buffer) {
if (buffer->io.lock.refs == 0)
++hammer_count_refedbufs;
hammer_ref(&buffer->io.lock);
/*
* Once refed the ondisk field will not be cleared by
* any other action.
*/
if (buffer->ondisk && buffer->io.loading == 0) {
*errorp = 0;
return(buffer);
}
/*
* The buffer is no longer loose if it has a ref, and
* cannot become loose once it gains a ref. Loose
* buffers will never be in a modified state. This should
* only occur on the 0->1 transition of refs.
*
* lose_list can be modified via a biodone() interrupt.
*/
if (buffer->io.mod_list == &hmp->lose_list) {
crit_enter(); /* biodone race against list */
TAILQ_REMOVE(buffer->io.mod_list, &buffer->io,
mod_entry);
crit_exit();
buffer->io.mod_list = NULL;
KKASSERT(buffer->io.modified == 0);
}
goto found;
}
/*
* What is the buffer class?
*/
zone = HAMMER_ZONE_DECODE(buf_offset);
switch(zone) {
case HAMMER_ZONE_LARGE_DATA_INDEX:
case HAMMER_ZONE_SMALL_DATA_INDEX:
iotype = HAMMER_STRUCTURE_DATA_BUFFER;
break;
case HAMMER_ZONE_UNDO_INDEX:
iotype = HAMMER_STRUCTURE_UNDO_BUFFER;
break;
case HAMMER_ZONE_META_INDEX:
default:
/*
* NOTE: inode data and directory entries are placed in this
* zone. inode atime/mtime is updated in-place and thus
* buffers containing inodes must be synchronized as
* meta-buffers, same as buffers containing B-Tree info.
*/
iotype = HAMMER_STRUCTURE_META_BUFFER;
break;
}
/*
* Handle blockmap offset translations
*/
if (zone >= HAMMER_ZONE_BTREE_INDEX) {
zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, errorp);
} else if (zone == HAMMER_ZONE_UNDO_INDEX) {
zone2_offset = hammer_undo_lookup(hmp, buf_offset, errorp);
} else {
KKASSERT(zone == HAMMER_ZONE_RAW_BUFFER_INDEX);
zone2_offset = buf_offset;
*errorp = 0;
}
if (*errorp)
return(NULL);
/*
* NOTE: zone2_offset and maxbuf_off are both full zone-2 offset
* specifications.
*/
KKASSERT((zone2_offset & HAMMER_OFF_ZONE_MASK) ==
HAMMER_ZONE_RAW_BUFFER);
vol_no = HAMMER_VOL_DECODE(zone2_offset);
volume = hammer_get_volume(hmp, vol_no, errorp);
if (volume == NULL)
return(NULL);
KKASSERT(zone2_offset < volume->maxbuf_off);
/*
* Allocate a new buffer structure. We will check for races later.
*/
++hammer_count_buffers;
buffer = kmalloc(sizeof(*buffer), hmp->m_misc,
M_WAITOK|M_ZERO|M_USE_RESERVE);
buffer->zone2_offset = zone2_offset;
buffer->zoneX_offset = buf_offset;
hammer_io_init(&buffer->io, volume, iotype);
buffer->io.offset = volume->ondisk->vol_buf_beg +
(zone2_offset & HAMMER_OFF_SHORT_MASK);
buffer->io.bytes = bytes;
TAILQ_INIT(&buffer->clist);
hammer_ref(&buffer->io.lock);
/*
* Insert the buffer into the RB tree and handle late collisions.
*/
if (RB_INSERT(hammer_buf_rb_tree, &hmp->rb_bufs_root, buffer)) {
hammer_unref(&buffer->io.lock);
--hammer_count_buffers;
kfree(buffer, hmp->m_misc);
goto again;
}
++hammer_count_refedbufs;
found:
/*
* Deal with on-disk info and loading races.
*/
if (buffer->ondisk == NULL || buffer->io.loading) {
*errorp = hammer_load_buffer(buffer, isnew);
if (*errorp) {
hammer_rel_buffer(buffer, 1);
buffer = NULL;
}
} else {
*errorp = 0;
}
return(buffer);
}
/*
* This is used by the direct-read code to deal with large-data buffers
* created by the reblocker and mirror-write code. The direct-read code
* bypasses the HAMMER buffer subsystem and so any aliased dirty or write-
* running hammer buffers must be fully synced to disk before we can issue
* the direct-read.
*
* This code path is not considered critical as only the rebocker and
* mirror-write code will create large-data buffers via the HAMMER buffer
* subsystem. They do that because they operate at the B-Tree level and
* do not access the vnode/inode structures.
*/
void
hammer_sync_buffers(hammer_mount_t hmp, hammer_off_t base_offset, int bytes)
{
hammer_buffer_t buffer;
int error;
KKASSERT((base_offset & HAMMER_OFF_ZONE_MASK) ==
HAMMER_ZONE_LARGE_DATA);
while (bytes > 0) {
buffer = RB_LOOKUP(hammer_buf_rb_tree, &hmp->rb_bufs_root,
base_offset);
if (buffer && (buffer->io.modified || buffer->io.running)) {
error = hammer_ref_buffer(buffer);
if (error == 0) {
hammer_io_wait(&buffer->io);
if (buffer->io.modified) {
hammer_io_write_interlock(&buffer->io);
hammer_io_flush(&buffer->io);
hammer_io_done_interlock(&buffer->io);
hammer_io_wait(&buffer->io);
}
hammer_rel_buffer(buffer, 0);
}
}
base_offset += HAMMER_BUFSIZE;
bytes -= HAMMER_BUFSIZE;
}
}
/*
* Destroy all buffers covering the specified zoneX offset range. This
* is called when the related blockmap layer2 entry is freed or when
* a direct write bypasses our buffer/buffer-cache subsystem.
*
* The buffers may be referenced by the caller itself. Setting reclaim
* will cause the buffer to be destroyed when it's ref count reaches zero.
*/
void
hammer_del_buffers(hammer_mount_t hmp, hammer_off_t base_offset,
hammer_off_t zone2_offset, int bytes)
{
hammer_buffer_t buffer;
hammer_volume_t volume;
int vol_no;
int error;
vol_no = HAMMER_VOL_DECODE(zone2_offset);
volume = hammer_get_volume(hmp, vol_no, &error);
KKASSERT(error == 0);
while (bytes > 0) {
buffer = RB_LOOKUP(hammer_buf_rb_tree, &hmp->rb_bufs_root,
base_offset);
if (buffer) {
error = hammer_ref_buffer(buffer);
if (error == 0) {
KKASSERT(buffer->zone2_offset == zone2_offset);
hammer_io_clear_modify(&buffer->io, 1);
buffer->io.reclaim = 1;
buffer->io.waitdep = 1;
KKASSERT(buffer->io.volume == volume);
hammer_rel_buffer(buffer, 0);
}
} else {
hammer_io_inval(volume, zone2_offset);
}
base_offset += HAMMER_BUFSIZE;
zone2_offset += HAMMER_BUFSIZE;
bytes -= HAMMER_BUFSIZE;
}
hammer_rel_volume(volume, 0);
}
static int
hammer_load_buffer(hammer_buffer_t buffer, int isnew)
{
hammer_volume_t volume;
int error;
/*
* Load the buffer's on-disk info
*/
volume = buffer->io.volume;
++buffer->io.loading;
hammer_lock_ex(&buffer->io.lock);
if (hammer_debug_io & 0x0001) {
kprintf("load_buffer %016llx %016llx isnew=%d od=%p\n",
buffer->zoneX_offset, buffer->zone2_offset, isnew,
buffer->ondisk);
}
if (buffer->ondisk == NULL) {
if (isnew) {
error = hammer_io_new(volume->sb, &buffer->io);
} else {
error = hammer_io_read(volume->sb, &buffer->io,
volume->maxraw_off);
}
if (error == 0)
buffer->ondisk = (void *)buffer->io.bp->b_data;
} else if (isnew) {
error = hammer_io_new(volume->sb, &buffer->io);
} else {
error = 0;
}
--buffer->io.loading;
hammer_unlock(&buffer->io.lock);
return (error);
}
/*
* NOTE: Called from RB_SCAN, must return >= 0 for scan to continue.
* This routine is only called during unmount.
*/
int
hammer_unload_buffer(hammer_buffer_t buffer, void *data __unused)
{
/*
* Clean up the persistent ref ioerror might have on the buffer
* and acquire a ref (steal ioerror's if we can).
*/
if (buffer->io.ioerror) {
buffer->io.ioerror = 0;
} else {
if (buffer->io.lock.refs == 0)
++hammer_count_refedbufs;
hammer_ref(&buffer->io.lock);
}
/*
* We must not flush a dirty buffer to disk on umount. It should
* have already been dealt with by the flusher, or we may be in
* catastrophic failure.
*/
hammer_io_clear_modify(&buffer->io, 1);
hammer_flush_buffer_nodes(buffer);
KKASSERT(buffer->io.lock.refs == 1);
hammer_rel_buffer(buffer, 2);
return(0);
}
/*
* Reference a buffer that is either already referenced or via a specially
* handled pointer (aka cursor->buffer).
*/
int
hammer_ref_buffer(hammer_buffer_t buffer)
{
int error;
if (buffer->io.lock.refs == 0)
++hammer_count_refedbufs;
hammer_ref(&buffer->io.lock);
/*
* At this point a biodone() will not touch the buffer other then
* incidental bits. However, lose_list can be modified via
* a biodone() interrupt.
*
* No longer loose
*/
if (buffer->io.mod_list == &buffer->io.hmp->lose_list) {
crit_enter();
TAILQ_REMOVE(buffer->io.mod_list, &buffer->io, mod_entry);
buffer->io.mod_list = NULL;
crit_exit();
}
if (buffer->ondisk == NULL || buffer->io.loading) {
error = hammer_load_buffer(buffer, 0);
if (error) {
hammer_rel_buffer(buffer, 1);
/*
* NOTE: buffer pointer can become stale after
* the above release.
*/
}
} else {
error = 0;
}
return(error);
}
/*
* Release a buffer. We have to deal with several places where
* another thread can ref the buffer.
*
* Only destroy the structure itself if the related buffer cache buffer
* was disassociated from it. This ties the management of the structure
* to the buffer cache subsystem. buffer->ondisk determines whether the
* embedded io is referenced or not.
*/
void
hammer_rel_buffer(hammer_buffer_t buffer, int flush)
{
hammer_volume_t volume;
hammer_mount_t hmp;
struct buf *bp = NULL;
int freeme = 0;
hmp = buffer->io.hmp;
crit_enter();
if (buffer->io.lock.refs == 1) {
++buffer->io.loading; /* force interlock check */
hammer_lock_ex(&buffer->io.lock);
if (buffer->io.lock.refs == 1) {
bp = hammer_io_release(&buffer->io, flush);
if (buffer->io.lock.refs == 1)
--hammer_count_refedbufs;
if (buffer->io.bp == NULL &&
buffer->io.lock.refs == 1) {
/*
* Final cleanup
*
* NOTE: It is impossible for any associated
* B-Tree nodes to have refs if the buffer
* has no additional refs.
*/
RB_REMOVE(hammer_buf_rb_tree,
&buffer->io.hmp->rb_bufs_root,
buffer);
volume = buffer->io.volume;
buffer->io.volume = NULL; /* sanity */
hammer_rel_volume(volume, 0);
hammer_io_clear_modlist(&buffer->io);
hammer_flush_buffer_nodes(buffer);
KKASSERT(TAILQ_EMPTY(&buffer->clist));
freeme = 1;
}
}
--buffer->io.loading;
hammer_unlock(&buffer->io.lock);
}
hammer_unref(&buffer->io.lock);
crit_exit();
if (bp)
dfly_brelse(bp);
if (freeme) {
--hammer_count_buffers;
kfree(buffer, hmp->m_misc);
}
}
/*
* Access the filesystem buffer containing the specified hammer offset.
* buf_offset is a conglomeration of the volume number and vol_buf_beg
* relative buffer offset. It must also have bit 55 set to be valid.
* (see hammer_off_t in hammer_disk.h).
*
* Any prior buffer in *bufferp will be released and replaced by the
* requested buffer.
*/
static __inline
void *
_hammer_bread(hammer_mount_t hmp, hammer_off_t buf_offset, int bytes,
int *errorp, struct hammer_buffer **bufferp)
{
hammer_buffer_t buffer;
int32_t xoff = (int32_t)buf_offset & HAMMER_BUFMASK;
buf_offset &= ~HAMMER_BUFMASK64;
KKASSERT((buf_offset & HAMMER_OFF_ZONE_MASK) != 0);
buffer = *bufferp;
if (buffer == NULL || (buffer->zone2_offset != buf_offset &&
buffer->zoneX_offset != buf_offset)) {
if (buffer)
hammer_rel_buffer(buffer, 0);
buffer = hammer_get_buffer(hmp, buf_offset, bytes, 0, errorp);
*bufferp = buffer;
} else {
*errorp = 0;
}
/*
* Return a pointer to the buffer data.
*/
if (buffer == NULL)
return(NULL);
else
return((char *)buffer->ondisk + xoff);
}
void *
hammer_bread(hammer_mount_t hmp, hammer_off_t buf_offset,
int *errorp, struct hammer_buffer **bufferp)
{
return(_hammer_bread(hmp, buf_offset, HAMMER_BUFSIZE, errorp, bufferp));
}
void *
hammer_bread_ext(hammer_mount_t hmp, hammer_off_t buf_offset, int bytes,
int *errorp, struct hammer_buffer **bufferp)
{
bytes = (bytes + HAMMER_BUFMASK) & ~HAMMER_BUFMASK;
return(_hammer_bread(hmp, buf_offset, bytes, errorp, bufferp));
}
/*
* Access the filesystem buffer containing the specified hammer offset.
* No disk read operation occurs. The result buffer may contain garbage.
*
* Any prior buffer in *bufferp will be released and replaced by the
* requested buffer.
*
* This function marks the buffer dirty but does not increment its
* modify_refs count.
*/
static __inline
void *
_hammer_bnew(hammer_mount_t hmp, hammer_off_t buf_offset, int bytes,
int *errorp, struct hammer_buffer **bufferp)
{
hammer_buffer_t buffer;
int32_t xoff = (int32_t)buf_offset & HAMMER_BUFMASK;
buf_offset &= ~HAMMER_BUFMASK64;
buffer = *bufferp;
if (buffer == NULL || (buffer->zone2_offset != buf_offset &&
buffer->zoneX_offset != buf_offset)) {
if (buffer)
hammer_rel_buffer(buffer, 0);
buffer = hammer_get_buffer(hmp, buf_offset, bytes, 1, errorp);
*bufferp = buffer;
} else {
*errorp = 0;