/
hammer2_chain.c
2560 lines (2406 loc) · 75.5 KB
/
hammer2_chain.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) 2011-2012 The DragonFly Project. All rights reserved.
*
* This code is derived from software contributed to The DragonFly Project
* by Matthew Dillon <dillon@dragonflybsd.org>
* by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
*
* 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.
*/
/*
* This subsystem handles direct and indirect block searches, recursions,
* creation, and deletion. Chains of blockrefs are tracked and modifications
* are flagged for propagation... eventually all the way back to the volume
* header. Any chain except the volume header can be flushed to disk at
* any time... none of it matters until the volume header is dealt with
* (which is not here, see hammer2_vfsops.c for the volume header disk
* sequencing).
*
* Serialized flushes are not handled here, see hammer2_flush.c. This module
* can essentially work on the current version of data, which can be in memory
* as well as on-disk due to the above. However, we are responsible for
* making a copy of the state when a modified chain is part of a flush
* and we attempt to modify it again before the flush gets to it. In that
* situation we create an allocated copy of the state that the flush can
* deal with. If a chain undergoing deletion is part of a flush it is
* marked DELETED and its bref index is kept intact for the flush, but the
* chain is thereafter ignored by this module's because it is no longer
* current.
*/
#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/types.h>
#include <sys/lock.h>
#include <sys/uuid.h>
#include "hammer2.h"
static int hammer2_indirect_optimize; /* XXX SYSCTL */
static hammer2_chain_t *hammer2_chain_create_indirect(
hammer2_mount_t *hmp, hammer2_chain_t *parent,
hammer2_key_t key, int keybits,
int *errorp);
/*
* We use a red-black tree to guarantee safe lookups under shared locks.
*/
RB_GENERATE(hammer2_chain_tree, hammer2_chain, rbnode, hammer2_chain_cmp);
int
hammer2_chain_cmp(hammer2_chain_t *chain1, hammer2_chain_t *chain2)
{
return(chain2->index - chain1->index);
}
/*
* Recursively mark the parent chain elements so flushes can find
* modified elements. Stop when we hit a chain already flagged
* SUBMODIFIED, but ignore the SUBMODIFIED bit that might be set
* in chain itself.
*
* SUBMODIFIED is not set on the chain passed in.
*
* The chain->cst.spin lock can be held to stabilize the chain->parent
* pointer. The first parent is stabilized by virtue of chain being
* fully locked.
*/
void
hammer2_chain_parent_setsubmod(hammer2_mount_t *hmp, hammer2_chain_t *chain)
{
hammer2_chain_t *parent;
parent = chain->parent;
if (parent && (parent->flags & HAMMER2_CHAIN_SUBMODIFIED) == 0) {
spin_lock(&parent->cst.spin);
for (;;) {
atomic_set_int(&parent->flags,
HAMMER2_CHAIN_SUBMODIFIED);
if ((chain = parent->parent) == NULL)
break;
spin_lock(&chain->cst.spin); /* upward interlock */
spin_unlock(&parent->cst.spin);
parent = chain;
}
spin_unlock(&parent->cst.spin);
}
}
/*
* Allocate a new disconnected chain element representing the specified
* bref. The chain element is locked exclusively and refs is set to 1.
*
* This essentially allocates a system memory structure representing one
* of the media structure types, including inodes.
*/
hammer2_chain_t *
hammer2_chain_alloc(hammer2_mount_t *hmp, hammer2_blockref_t *bref)
{
hammer2_chain_t *chain;
hammer2_inode_t *ip;
hammer2_indblock_t *np;
hammer2_data_t *dp;
u_int bytes = 1U << (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
/*
* Construct the appropriate system structure.
*/
switch(bref->type) {
case HAMMER2_BREF_TYPE_INODE:
ip = kmalloc(sizeof(*ip), hmp->minode, M_WAITOK | M_ZERO);
chain = &ip->chain;
chain->u.ip = ip;
ip->hmp = hmp;
break;
case HAMMER2_BREF_TYPE_INDIRECT:
case HAMMER2_BREF_TYPE_FREEMAP_ROOT:
case HAMMER2_BREF_TYPE_FREEMAP_NODE:
np = kmalloc(sizeof(*np), hmp->mchain, M_WAITOK | M_ZERO);
chain = &np->chain;
chain->u.np = np;
break;
case HAMMER2_BREF_TYPE_DATA:
case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
dp = kmalloc(sizeof(*dp), hmp->mchain, M_WAITOK | M_ZERO);
chain = &dp->chain;
chain->u.dp = dp;
break;
case HAMMER2_BREF_TYPE_VOLUME:
chain = NULL;
panic("hammer2_chain_alloc volume type illegal for op");
default:
chain = NULL;
panic("hammer2_chain_alloc: unrecognized blockref type: %d",
bref->type);
}
/*
* Only set bref_flush if the bref has a real media offset, otherwise
* the caller has to wait for the chain to be modified/block-allocated
* before a blockref can be synchronized with its (future) parent.
*/
chain->bref = *bref;
if (bref->data_off & ~HAMMER2_OFF_MASK_RADIX)
chain->bref_flush = *bref;
chain->index = -1; /* not yet assigned */
chain->refs = 1;
chain->bytes = bytes;
ccms_cst_init(&chain->cst, chain);
ccms_thread_lock(&chain->cst, CCMS_STATE_EXCLUSIVE);
return (chain);
}
/*
* Deallocate a chain (the step before freeing it). Remove the chain from
* its parent's tree.
*
* Caller must hold the parent and the chain exclusively locked, and
* chain->refs must be 0.
*
* This function unlocks, removes, and destroys chain, and will recursively
* destroy any sub-chains under chain (whos refs must also be 0 at this
* point).
*
* parent can be NULL.
*/
static void
hammer2_chain_dealloc(hammer2_mount_t *hmp, hammer2_chain_t *chain)
{
hammer2_inode_t *ip;
hammer2_chain_t *parent;
hammer2_chain_t *child;
KKASSERT(chain->refs == 0);
KKASSERT(chain->flushing == 0);
KKASSERT((chain->flags &
(HAMMER2_CHAIN_MOVED | HAMMER2_CHAIN_MODIFIED)) == 0);
if (chain->bref.type == HAMMER2_BREF_TYPE_INODE)
ip = chain->u.ip;
else
ip = NULL;
/*
* If the sub-tree is not empty all the elements on it must have
* 0 refs and be deallocatable.
*/
while ((child = RB_ROOT(&chain->rbhead)) != NULL) {
ccms_thread_lock(&child->cst, CCMS_STATE_EXCLUSIVE);
hammer2_chain_dealloc(hmp, child);
}
/*
* If the DELETED flag is not set the chain must be removed from
* its parent's tree.
*
* WARNING! chain->cst.spin must be held when chain->parent is
* modified, even though we own the full blown lock,
* to deal with setsubmod and rename races.
*/
if (chain->flags & HAMMER2_CHAIN_ONRBTREE) {
spin_lock(&chain->cst.spin); /* shouldn't be needed */
parent = chain->parent;
RB_REMOVE(hammer2_chain_tree, &parent->rbhead, chain);
atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONRBTREE);
if (ip)
ip->pip = NULL;
chain->parent = NULL;
spin_unlock(&chain->cst.spin);
}
/*
* When cleaning out a hammer2_inode we must
* also clean out the related ccms_inode.
*/
if (ip)
ccms_cst_uninit(&ip->topo_cst);
hammer2_chain_free(hmp, chain);
}
/*
* Free a disconnected chain element
*/
void
hammer2_chain_free(hammer2_mount_t *hmp, hammer2_chain_t *chain)
{
void *mem;
if (chain->bref.type == HAMMER2_BREF_TYPE_INODE ||
chain->bref.type == HAMMER2_BREF_TYPE_VOLUME) {
chain->data = NULL;
}
KKASSERT(chain->bp == NULL);
KKASSERT(chain->data == NULL);
KKASSERT(chain->bref.type != HAMMER2_BREF_TYPE_INODE ||
chain->u.ip->vp == NULL);
ccms_thread_unlock(&chain->cst);
KKASSERT(chain->cst.count == 0);
KKASSERT(chain->cst.upgrade == 0);
if ((mem = chain->u.mem) != NULL) {
chain->u.mem = NULL;
if (chain->bref.type == HAMMER2_BREF_TYPE_INODE)
kfree(mem, hmp->minode);
else
kfree(mem, hmp->mchain);
}
}
/*
* Add a reference to a chain element, preventing its destruction.
*
* The parent chain must be locked shared or exclusive or otherwise be
* stable and already have a reference.
*/
void
hammer2_chain_ref(hammer2_mount_t *hmp, hammer2_chain_t *chain)
{
u_int refs;
while (chain) {
refs = chain->refs;
KKASSERT(chain->refs >= 0);
cpu_ccfence();
if (refs == 0) {
/*
* 0 -> 1 transition must bump the refs on the parent
* too. The caller has stabilized the parent.
*/
if (atomic_cmpset_int(&chain->refs, 0, 1)) {
chain = chain->parent;
KKASSERT(chain == NULL || chain->refs > 0);
}
/* retry or continue along the parent chain */
} else {
/*
* N -> N+1
*/
if (atomic_cmpset_int(&chain->refs, refs, refs + 1))
break;
/* retry */
}
}
}
/*
* Drop the callers reference to the chain element. If the ref count
* reaches zero we attempt to recursively drop the parent.
*
* MOVED and MODIFIED elements hold additional references so it should not
* be possible for the count on a modified element to drop to 0.
*
* The chain element must NOT be locked by the caller on the 1->0 transition.
*
* The parent might or might not be locked by the caller. If we are unable
* to lock the parent on the 1->0 transition the destruction of the chain
* will be deferred but we still recurse upward and drop the ref on the
* parent (see the lastdrop() function)
*/
static hammer2_chain_t *hammer2_chain_lastdrop(hammer2_mount_t *hmp,
hammer2_chain_t *chain);
void
hammer2_chain_drop(hammer2_mount_t *hmp, hammer2_chain_t *chain)
{
u_int refs;
while (chain) {
refs = chain->refs;
cpu_ccfence();
KKASSERT(refs > 0);
if (refs == 1) {
/*
* (1) lastdrop successfully drops the chain to 0
* refs and may may not have destroyed it.
* lastdrop will return the parent so we can
* recursively drop the implied ref from the
* 1->0 transition.
*
* (2) lastdrop fails to transition refs from 1 to 0
* and returns the same chain, we retry.
*/
chain = hammer2_chain_lastdrop(hmp, chain);
} else {
if (atomic_cmpset_int(&chain->refs, refs, refs - 1)) {
/*
* Succeeded, count did not reach zero so
* cut out of the loop.
*/
break;
}
/* retry the same chain */
}
}
}
/*
* Handle SMP races during the last drop. We must obtain a lock on
* chain->parent to stabilize the last pointer reference to chain
* (if applicable). This reference does not have a parallel ref count,
* that is idle chains in the topology can have a ref count of 0.
*
* The 1->0 transition implies a ref on the parent.
*/
static
hammer2_chain_t *
hammer2_chain_lastdrop(hammer2_mount_t *hmp, hammer2_chain_t *chain)
{
hammer2_chain_t *parent;
/*
* Stablize chain->parent with the chain cst's spinlock.
* (parent can be NULL here).
*
* cst.spin locks are allowed to be nested bottom-up (the reverse
* of the normal top-down for full-blown cst locks), so this also
* allows us to attempt to obtain the parent's cst lock non-blocking
* (which must acquire the parent's spinlock unconditionally) while
* we are still holding the chain's spinlock.
*/
spin_lock(&chain->cst.spin);
parent = chain->parent;
/*
* If chain->flushing is non-zero we cannot deallocate the chain
* here. The flushing field will be serialized for the inline
* unlock made by the flusher itself and we don't care about races
* in any other situation because the thread lock on the parent
* will fail in other situations.
*
* If we have a non-NULL parent but cannot acquire its thread
* lock, we also cannot deallocate the chain.
*/
if (chain->flushing ||
(parent && ccms_thread_lock_nonblock(&parent->cst,
CCMS_STATE_EXCLUSIVE))) {
if (atomic_cmpset_int(&chain->refs, 1, 0)) {
spin_unlock(&chain->cst.spin); /* success */
return(parent);
} else {
spin_unlock(&chain->cst.spin); /* failure */
return(chain);
}
}
spin_unlock(&chain->cst.spin);
/*
* With the parent now held we control the last pointer reference
* to chain ONLY IF this is the 1->0 drop. If we fail to transition
* from 1->0 we raced a refs change and must retry at chain.
*/
if (atomic_cmpset_int(&chain->refs, 1, 0) == 0) {
/* failure */
if (parent)
ccms_thread_unlock(&parent->cst);
return(chain);
}
/*
* Ok, we succeeded. We now own the implied ref on the parent
* associated with the 1->0 transition of the child. It should not
* be possible for ANYTHING to access the child now, as we own the
* lock on the parent, so we should be able to safely lock the
* child and destroy it.
*/
ccms_thread_lock(&chain->cst, CCMS_STATE_EXCLUSIVE);
hammer2_chain_dealloc(hmp, chain);
/*
* We want to return parent with its implied ref to the caller
* to recurse and drop the parent.
*/
if (parent)
ccms_thread_unlock(&parent->cst);
return (parent);
}
/*
* Ref and lock a chain element, acquiring its data with I/O if necessary,
* and specify how you would like the data to be resolved.
*
* Returns 0 on success or an error code if the data could not be acquired.
* The chain element is locked either way.
*
* The lock is allowed to recurse, multiple locking ops will aggregate
* the requested resolve types. Once data is assigned it will not be
* removed until the last unlock.
*
* HAMMER2_RESOLVE_NEVER - Do not resolve the data element.
* (typically used to avoid device/logical buffer
* aliasing for data)
*
* HAMMER2_RESOLVE_MAYBE - Do not resolve data elements for chains in
* the INITIAL-create state (indirect blocks only).
*
* Do not resolve data elements for DATA chains.
* (typically used to avoid device/logical buffer
* aliasing for data)
*
* HAMMER2_RESOLVE_ALWAYS- Always resolve the data element.
*
* HAMMER2_RESOLVE_SHARED- (flag) The chain is locked shared, otherwise
* it will be locked exclusive.
*
* NOTE: Embedded elements (volume header, inodes) are always resolved
* regardless.
*
* NOTE: Specifying HAMMER2_RESOLVE_ALWAYS on a newly-created non-embedded
* element will instantiate and zero its buffer, and flush it on
* release.
*
* NOTE: (data) elements are normally locked RESOLVE_NEVER or RESOLVE_MAYBE
* so as not to instantiate a device buffer, which could alias against
* a logical file buffer. However, if ALWAYS is specified the
* device buffer will be instantiated anyway.
*/
int
hammer2_chain_lock(hammer2_mount_t *hmp, hammer2_chain_t *chain, int how)
{
hammer2_blockref_t *bref;
hammer2_off_t pbase;
hammer2_off_t peof;
ccms_state_t ostate;
size_t boff;
size_t bbytes;
int error;
char *bdata;
/*
* Ref and lock the element. Recursive locks are allowed.
*/
hammer2_chain_ref(hmp, chain);
if (how & HAMMER2_RESOLVE_SHARED)
ccms_thread_lock(&chain->cst, CCMS_STATE_SHARED);
else
ccms_thread_lock(&chain->cst, CCMS_STATE_EXCLUSIVE);
/*
* If we already have a valid data pointer no further action is
* necessary.
*/
if (chain->data)
return (0);
/*
* Do we have to resolve the data?
*/
switch(how & HAMMER2_RESOLVE_MASK) {
case HAMMER2_RESOLVE_NEVER:
return(0);
case HAMMER2_RESOLVE_MAYBE:
if (chain->flags & HAMMER2_CHAIN_INITIAL)
return(0);
if (chain->bref.type == HAMMER2_BREF_TYPE_DATA)
return(0);
if (chain->bref.type == HAMMER2_BREF_TYPE_FREEMAP_LEAF)
return(0);
/* fall through */
case HAMMER2_RESOLVE_ALWAYS:
break;
}
/*
* Upgrade to an exclusive lock so we can safely manipulate the
* buffer cache. If another thread got to it before us we
* can just return.
*/
ostate = ccms_thread_lock_upgrade(&chain->cst);
if (chain->data) {
ccms_thread_lock_restore(&chain->cst, ostate);
return (0);
}
/*
* We must resolve to a device buffer, either by issuing I/O or
* by creating a zero-fill element. We do not mark the buffer
* dirty when creating a zero-fill element (the hammer2_chain_modify()
* API must still be used to do that).
*
* The device buffer is variable-sized in powers of 2 down
* to HAMMER2_MINALLOCSIZE (typically 1K). A 64K physical storage
* chunk always contains buffers of the same size. (XXX)
*
* The minimum physical IO size may be larger than the variable
* block size.
*/
bref = &chain->bref;
if ((bbytes = chain->bytes) < HAMMER2_MINIOSIZE)
bbytes = HAMMER2_MINIOSIZE;
pbase = bref->data_off & ~(hammer2_off_t)(bbytes - 1);
peof = (pbase + HAMMER2_PBUFSIZE64) & ~HAMMER2_PBUFMASK64;
boff = bref->data_off & HAMMER2_OFF_MASK & (bbytes - 1);
KKASSERT(pbase != 0);
/*
* The getblk() optimization can only be used on newly created
* elements if the physical block size matches the request.
*/
if ((chain->flags & HAMMER2_CHAIN_INITIAL) &&
chain->bytes == bbytes) {
chain->bp = getblk(hmp->devvp, pbase, bbytes, 0, 0);
error = 0;
} else if (hammer2_cluster_enable) {
error = cluster_read(hmp->devvp, peof, pbase, bbytes,
HAMMER2_PBUFSIZE, HAMMER2_PBUFSIZE,
&chain->bp);
} else {
error = bread(hmp->devvp, pbase, bbytes, &chain->bp);
}
if (error) {
kprintf("hammer2_chain_get: I/O error %016jx: %d\n",
(intmax_t)pbase, error);
bqrelse(chain->bp);
chain->bp = NULL;
ccms_thread_lock_restore(&chain->cst, ostate);
return (error);
}
/*
* Zero the data area if the chain is in the INITIAL-create state.
* Mark the buffer for bdwrite().
*/
bdata = (char *)chain->bp->b_data + boff;
if (chain->flags & HAMMER2_CHAIN_INITIAL) {
bzero(bdata, chain->bytes);
atomic_set_int(&chain->flags, HAMMER2_CHAIN_DIRTYBP);
}
/*
* Setup the data pointer, either pointing it to an embedded data
* structure and copying the data from the buffer, or pointing it
* into the buffer.
*
* The buffer is not retained when copying to an embedded data
* structure in order to avoid potential deadlocks or recursions
* on the same physical buffer.
*/
switch (bref->type) {
case HAMMER2_BREF_TYPE_VOLUME:
/*
* Copy data from bp to embedded buffer
*/
panic("hammer2_chain_lock: called on unresolved volume header");
#if 0
/* NOT YET */
KKASSERT(pbase == 0);
KKASSERT(chain->bytes == HAMMER2_PBUFSIZE);
bcopy(bdata, &hmp->voldata, chain->bytes);
chain->data = (void *)&hmp->voldata;
bqrelse(chain->bp);
chain->bp = NULL;
#endif
break;
case HAMMER2_BREF_TYPE_INODE:
/*
* Copy data from bp to embedded buffer, do not retain the
* device buffer.
*/
bcopy(bdata, &chain->u.ip->ip_data, chain->bytes);
chain->data = (void *)&chain->u.ip->ip_data;
bqrelse(chain->bp);
chain->bp = NULL;
break;
case HAMMER2_BREF_TYPE_INDIRECT:
case HAMMER2_BREF_TYPE_DATA:
case HAMMER2_BREF_TYPE_FREEMAP_ROOT:
case HAMMER2_BREF_TYPE_FREEMAP_NODE:
case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
default:
/*
* Point data at the device buffer and leave bp intact.
*/
chain->data = (void *)bdata;
break;
}
/*
* Make sure the bp is not specifically owned by this thread before
* restoring to a possibly shared lock, so another hammer2 thread
* can release it.
*/
if (chain->bp)
BUF_KERNPROC(chain->bp);
ccms_thread_lock_restore(&chain->cst, ostate);
return (0);
}
/*
* Unlock and deref a chain element.
*
* On the last lock release any non-embedded data (chain->bp) will be
* retired.
*/
void
hammer2_chain_unlock(hammer2_mount_t *hmp, hammer2_chain_t *chain)
{
long *counterp;
/*
* Release the CST lock but with a special 1->0 transition case.
*
* Returns non-zero if lock references remain. When zero is
* returned the last lock reference is retained and any shared
* lock is upgraded to an exclusive lock for final disposition.
*/
if (ccms_thread_unlock_zero(&chain->cst)) {
KKASSERT(chain->refs > 1);
atomic_add_int(&chain->refs, -1);
return;
}
/*
* Shortcut the case if the data is embedded or not resolved.
*
* Do NOT null-out pointers to embedded data (e.g. inode).
*
* The DIRTYBP flag is non-applicable in this situation and can
* be cleared to keep the flags state clean.
*/
if (chain->bp == NULL) {
atomic_clear_int(&chain->flags, HAMMER2_CHAIN_DIRTYBP);
ccms_thread_unlock(&chain->cst);
hammer2_chain_drop(hmp, chain);
return;
}
/*
* Statistics
*/
if ((chain->flags & HAMMER2_CHAIN_DIRTYBP) == 0) {
;
} else if (chain->flags & HAMMER2_CHAIN_IOFLUSH) {
switch(chain->bref.type) {
case HAMMER2_BREF_TYPE_DATA:
counterp = &hammer2_ioa_file_write;
break;
case HAMMER2_BREF_TYPE_INODE:
counterp = &hammer2_ioa_meta_write;
break;
case HAMMER2_BREF_TYPE_INDIRECT:
counterp = &hammer2_ioa_indr_write;
break;
case HAMMER2_BREF_TYPE_FREEMAP_ROOT:
case HAMMER2_BREF_TYPE_FREEMAP_NODE:
case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
counterp = &hammer2_ioa_fmap_write;
break;
default:
counterp = &hammer2_ioa_volu_write;
break;
}
++*counterp;
} else {
switch(chain->bref.type) {
case HAMMER2_BREF_TYPE_DATA:
counterp = &hammer2_iod_file_write;
break;
case HAMMER2_BREF_TYPE_INODE:
counterp = &hammer2_iod_meta_write;
break;
case HAMMER2_BREF_TYPE_INDIRECT:
counterp = &hammer2_iod_indr_write;
break;
case HAMMER2_BREF_TYPE_FREEMAP_ROOT:
case HAMMER2_BREF_TYPE_FREEMAP_NODE:
case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
counterp = &hammer2_iod_fmap_write;
break;
default:
counterp = &hammer2_iod_volu_write;
break;
}
++*counterp;
}
/*
* Clean out the bp.
*
* If a device buffer was used for data be sure to destroy the
* buffer when we are done to avoid aliases (XXX what about the
* underlying VM pages?).
*
* NOTE: Freemap leaf's use reserved blocks and thus no aliasing
* is possible.
*/
if (chain->bref.type == HAMMER2_BREF_TYPE_DATA)
chain->bp->b_flags |= B_RELBUF;
/*
* The DIRTYBP flag tracks whether we have to bdwrite() the buffer
* or not. The flag will get re-set when chain_modify() is called,
* even if MODIFIED is already set, allowing the OS to retire the
* buffer independent of a hammer2 flus.
*/
chain->data = NULL;
if (chain->flags & HAMMER2_CHAIN_DIRTYBP) {
atomic_clear_int(&chain->flags, HAMMER2_CHAIN_DIRTYBP);
if (chain->flags & HAMMER2_CHAIN_IOFLUSH) {
atomic_clear_int(&chain->flags,
HAMMER2_CHAIN_IOFLUSH);
chain->bp->b_flags |= B_RELBUF;
cluster_awrite(chain->bp);
} else {
chain->bp->b_flags |= B_CLUSTEROK;
bdwrite(chain->bp);
}
} else {
if (chain->flags & HAMMER2_CHAIN_IOFLUSH) {
atomic_clear_int(&chain->flags,
HAMMER2_CHAIN_IOFLUSH);
chain->bp->b_flags |= B_RELBUF;
brelse(chain->bp);
} else {
/* bp might still be dirty */
bqrelse(chain->bp);
}
}
chain->bp = NULL;
ccms_thread_unlock(&chain->cst);
hammer2_chain_drop(hmp, chain);
}
/*
* Resize the chain's physical storage allocation. Chains can be resized
* smaller without reallocating the storage. Resizing larger will reallocate
* the storage.
*
* Must be passed a locked chain.
*
* If you want the resize code to copy the data to the new block then the
* caller should lock the chain RESOLVE_MAYBE or RESOLVE_ALWAYS.
*
* If the caller already holds a logical buffer containing the data and
* intends to bdwrite() that buffer resolve with RESOLVE_NEVER. The resize
* operation will then not copy the data.
*
* This function is mostly used with DATA blocks locked RESOLVE_NEVER in order
* to avoid instantiating a device buffer that conflicts with the vnode
* data buffer.
*
* XXX flags currently ignored, uses chain->bp to detect data/no-data.
*/
void
hammer2_chain_resize(hammer2_inode_t *ip, hammer2_chain_t *chain,
int nradix, int flags)
{
hammer2_mount_t *hmp = ip->hmp;
struct buf *nbp;
hammer2_off_t pbase;
size_t obytes;
size_t nbytes;
size_t bbytes;
int boff;
char *bdata;
int error;
/*
* Only data and indirect blocks can be resized for now.
* (The volu root, inodes, and freemap elements use a fixed size).
*/
KKASSERT(chain != &hmp->vchain);
KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA ||
chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT);
/*
* Nothing to do if the element is already the proper size
*/
obytes = chain->bytes;
nbytes = 1U << nradix;
if (obytes == nbytes)
return;
/*
* Set MODIFIED and add a chain ref to prevent destruction. Both
* modified flags share the same ref.
*
* If the chain is already marked MODIFIED then we can safely
* return the previous allocation to the pool without having to
* worry about snapshots.
*/
if ((chain->flags & HAMMER2_CHAIN_MODIFIED) == 0) {
atomic_set_int(&chain->flags, HAMMER2_CHAIN_MODIFIED |
HAMMER2_CHAIN_MODIFY_TID);
hammer2_chain_ref(hmp, chain);
} else {
hammer2_freemap_free(hmp, chain->bref.data_off,
chain->bref.type);
}
/*
* Relocate the block, even if making it smaller (because different
* block sizes may be in different regions).
*/
chain->bref.data_off = hammer2_freemap_alloc(hmp, chain->bref.type,
nbytes);
chain->bytes = nbytes;
ip->delta_dcount += (ssize_t)(nbytes - obytes); /* XXX atomic */
/*
* The device buffer may be larger than the allocation size.
*/
if ((bbytes = chain->bytes) < HAMMER2_MINIOSIZE)
bbytes = HAMMER2_MINIOSIZE;
pbase = chain->bref.data_off & ~(hammer2_off_t)(bbytes - 1);
boff = chain->bref.data_off & HAMMER2_OFF_MASK & (bbytes - 1);
/*
* Only copy the data if resolved, otherwise the caller is
* responsible.
*/
if (chain->bp) {
KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT ||
chain->bref.type == HAMMER2_BREF_TYPE_DATA);
KKASSERT(chain != &hmp->vchain); /* safety */
/*
* The getblk() optimization can only be used if the
* physical block size matches the request.
*/
if (nbytes == bbytes) {
nbp = getblk(hmp->devvp, pbase, bbytes, 0, 0);
error = 0;
} else {
error = bread(hmp->devvp, pbase, bbytes, &nbp);
KKASSERT(error == 0);
}
bdata = (char *)nbp->b_data + boff;
if (nbytes < obytes) {
bcopy(chain->data, bdata, nbytes);
} else {
bcopy(chain->data, bdata, obytes);
bzero(bdata + obytes, nbytes - obytes);
}
/*
* NOTE: The INITIAL state of the chain is left intact.
* We depend on hammer2_chain_modify() to do the
* right thing.
*
* NOTE: We set B_NOCACHE to throw away the previous bp and
* any VM backing store, even if it was dirty.
* Otherwise we run the risk of a logical/device
* conflict on reallocation.
*/
chain->bp->b_flags |= B_RELBUF | B_NOCACHE;
brelse(chain->bp);
chain->bp = nbp;
chain->data = (void *)bdata;
hammer2_chain_modify(hmp, chain, 0);
}
/*
* Make sure the chain is marked MOVED and SUBMOD is set in the
* parent(s) so the adjustments are picked up by flush.
*/
if ((chain->flags & HAMMER2_CHAIN_MOVED) == 0) {
hammer2_chain_ref(hmp, chain);
atomic_set_int(&chain->flags, HAMMER2_CHAIN_MOVED);
}
hammer2_chain_parent_setsubmod(hmp, chain);
}
/*
* Convert a locked chain that was retrieved read-only to read-write.
*
* If not already marked modified a new physical block will be allocated
* and assigned to the bref.
*
* Non-data blocks - The chain should be locked to at least the RESOLVE_MAYBE
* level or the COW operation will not work.
*
* Data blocks - The chain is usually locked RESOLVE_NEVER so as not to
* run the data through the device buffers.
*/
void
hammer2_chain_modify(hammer2_mount_t *hmp, hammer2_chain_t *chain, int flags)
{
struct buf *nbp;
int error;
hammer2_off_t pbase;
size_t bbytes;
size_t boff;
void *bdata;
/*
* Tells flush that modify_tid must be updated, otherwise only
* mirror_tid is updated. This is the default.
*/
if ((flags & HAMMER2_MODIFY_NO_MODIFY_TID) == 0)
atomic_set_int(&chain->flags, HAMMER2_CHAIN_MODIFY_TID);
/*
* If the chain is already marked MODIFIED we can just return.
*
* However, it is possible that a prior lock/modify sequence
* retired the buffer. During this lock/modify sequence MODIFIED
* may still be set but the buffer could wind up clean. Since
* the caller is going to modify the buffer further we have to
* be sure that DIRTYBP is set again.
*/
if (chain->flags & HAMMER2_CHAIN_MODIFIED) {
if ((flags & HAMMER2_MODIFY_OPTDATA) == 0 &&
chain->bp == NULL) {
goto skip1;
}
atomic_set_int(&chain->flags, HAMMER2_CHAIN_DIRTYBP);
return;
}
/*
* Set MODIFIED and add a chain ref to prevent destruction. Both
* modified flags share the same ref.
*/
atomic_set_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
hammer2_chain_ref(hmp, chain);
/*
* We must allocate the copy-on-write block.
*
* If the data is embedded no other action is required.
*
* If the data is not embedded we acquire and clear the
* new block. If chain->data is not NULL we then do the
* copy-on-write. chain->data will then be repointed to the new
* buffer and the old buffer will be released.
*
* For newly created elements with no prior allocation we go
* through the copy-on-write steps except without the copying part.
*/
if (chain != &hmp->vchain) {