/
vfs_trans.c
1138 lines (967 loc) · 25 KB
/
vfs_trans.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
/* $NetBSD: vfs_trans.c,v 1.70 2022/11/04 11:20:39 hannken Exp $ */
/*-
* Copyright (c) 2007, 2020 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Juergen Hannken-Illjes.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: vfs_trans.c,v 1.70 2022/11/04 11:20:39 hannken Exp $");
/*
* File system transaction operations.
*/
#ifdef _KERNEL_OPT
#include "opt_ddb.h"
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/atomic.h>
#include <sys/buf.h>
#include <sys/hash.h>
#include <sys/kmem.h>
#include <sys/mount.h>
#include <sys/pserialize.h>
#include <sys/vnode.h>
#include <sys/fstrans.h>
#include <sys/proc.h>
#include <sys/pool.h>
#include <miscfs/deadfs/deadfs.h>
#include <miscfs/specfs/specdev.h>
#define FSTRANS_MOUNT_HASHSIZE 32
enum fstrans_lock_type {
FSTRANS_LAZY, /* Granted while not suspended */
FSTRANS_SHARED /* Granted while not suspending */
};
struct fscow_handler {
LIST_ENTRY(fscow_handler) ch_list;
int (*ch_func)(void *, struct buf *, bool);
void *ch_arg;
};
struct fstrans_lwp_info {
struct fstrans_lwp_info *fli_succ;
struct lwp *fli_self;
struct mount *fli_mount;
struct fstrans_lwp_info *fli_alias;
struct fstrans_mount_info *fli_mountinfo;
int fli_trans_cnt;
int fli_alias_cnt;
int fli_cow_cnt;
enum fstrans_lock_type fli_lock_type;
LIST_ENTRY(fstrans_lwp_info) fli_list;
};
struct fstrans_mount_info {
enum fstrans_state fmi_state;
unsigned int fmi_ref_cnt;
bool fmi_gone;
bool fmi_cow_change;
SLIST_ENTRY(fstrans_mount_info) fmi_hash;
LIST_HEAD(, fscow_handler) fmi_cow_handler;
struct mount *fmi_mount;
struct fstrans_mount_info *fmi_lower_info;
struct lwp *fmi_owner;
};
SLIST_HEAD(fstrans_mount_hashhead, fstrans_mount_info);
static kmutex_t vfs_suspend_lock /* Serialize suspensions. */
__cacheline_aligned;
static kmutex_t fstrans_lock /* Fstrans big lock. */
__cacheline_aligned;
static kcondvar_t fstrans_state_cv; /* Fstrans or cow state changed. */
static kcondvar_t fstrans_count_cv; /* Fstrans or cow count changed. */
static pserialize_t fstrans_psz; /* Pserialize state. */
static LIST_HEAD(fstrans_lwp_head, fstrans_lwp_info) fstrans_fli_head;
/* List of all fstrans_lwp_info. */
static pool_cache_t fstrans_lwp_cache; /* Cache of fstrans_lwp_info. */
static u_long fstrans_mount_hashmask;
static struct fstrans_mount_hashhead *fstrans_mount_hashtab;
static int fstrans_gone_count; /* Number of fstrans_mount_info gone. */
static inline uint32_t fstrans_mount_hash(struct mount *);
static inline struct fstrans_mount_info *fstrans_mount_get(struct mount *);
static void fstrans_mount_dtor(struct fstrans_mount_info *);
static void fstrans_clear_lwp_info(void);
static inline struct fstrans_lwp_info *
fstrans_get_lwp_info(struct mount *, bool);
static struct fstrans_lwp_info *fstrans_alloc_lwp_info(struct mount *);
static int fstrans_lwp_pcc(void *, void *, int);
static void fstrans_lwp_pcd(void *, void *);
static inline int _fstrans_start(struct mount *, enum fstrans_lock_type, int);
static bool grant_lock(const struct fstrans_mount_info *,
const enum fstrans_lock_type);
static bool state_change_done(const struct fstrans_mount_info *);
static bool cow_state_change_done(const struct fstrans_mount_info *);
static void cow_change_enter(struct fstrans_mount_info *);
static void cow_change_done(struct fstrans_mount_info *);
/*
* Initialize.
*/
void
fstrans_init(void)
{
mutex_init(&vfs_suspend_lock, MUTEX_DEFAULT, IPL_NONE);
mutex_init(&fstrans_lock, MUTEX_DEFAULT, IPL_NONE);
cv_init(&fstrans_state_cv, "fstchg");
cv_init(&fstrans_count_cv, "fstcnt");
fstrans_psz = pserialize_create();
LIST_INIT(&fstrans_fli_head);
fstrans_lwp_cache = pool_cache_init(sizeof(struct fstrans_lwp_info),
coherency_unit, 0, 0, "fstlwp", NULL, IPL_NONE,
fstrans_lwp_pcc, fstrans_lwp_pcd, NULL);
KASSERT(fstrans_lwp_cache != NULL);
fstrans_mount_hashtab = hashinit(FSTRANS_MOUNT_HASHSIZE, HASH_SLIST,
true, &fstrans_mount_hashmask);
}
/*
* pool_cache constructor for fstrans_lwp_info. Updating the global list
* produces cache misses on MP. Minimise by keeping free entries on list.
*/
int
fstrans_lwp_pcc(void *arg, void *obj, int flags)
{
struct fstrans_lwp_info *fli = obj;
memset(fli, 0, sizeof(*fli));
mutex_enter(&fstrans_lock);
LIST_INSERT_HEAD(&fstrans_fli_head, fli, fli_list);
mutex_exit(&fstrans_lock);
return 0;
}
/*
* pool_cache destructor
*/
void
fstrans_lwp_pcd(void *arg, void *obj)
{
struct fstrans_lwp_info *fli = obj;
mutex_enter(&fstrans_lock);
LIST_REMOVE(fli, fli_list);
mutex_exit(&fstrans_lock);
}
/*
* Deallocate lwp state.
*/
void
fstrans_lwp_dtor(lwp_t *l)
{
struct fstrans_lwp_info *fli, *fli_next;
if (l->l_fstrans == NULL)
return;
mutex_enter(&fstrans_lock);
for (fli = l->l_fstrans; fli; fli = fli_next) {
KASSERT(fli->fli_trans_cnt == 0);
KASSERT(fli->fli_cow_cnt == 0);
KASSERT(fli->fli_self == l);
if (fli->fli_mount != NULL)
fstrans_mount_dtor(fli->fli_mountinfo);
fli_next = fli->fli_succ;
fli->fli_alias_cnt = 0;
fli->fli_mount = NULL;
fli->fli_alias = NULL;
fli->fli_mountinfo = NULL;
fli->fli_self = NULL;
}
mutex_exit(&fstrans_lock);
for (fli = l->l_fstrans; fli; fli = fli_next) {
fli_next = fli->fli_succ;
pool_cache_put(fstrans_lwp_cache, fli);
}
l->l_fstrans = NULL;
}
/*
* mount pointer to hash
*/
static inline uint32_t
fstrans_mount_hash(struct mount *mp)
{
return hash32_buf(&mp, sizeof(mp), HASH32_BUF_INIT) &
fstrans_mount_hashmask;
}
/*
* retrieve fstrans_mount_info by mount or NULL
*/
static inline struct fstrans_mount_info *
fstrans_mount_get(struct mount *mp)
{
uint32_t indx;
struct fstrans_mount_info *fmi, *fmi_lower;
KASSERT(mutex_owned(&fstrans_lock));
indx = fstrans_mount_hash(mp);
SLIST_FOREACH(fmi, &fstrans_mount_hashtab[indx], fmi_hash) {
if (fmi->fmi_mount == mp) {
if (__predict_false(mp->mnt_lower != NULL &&
fmi->fmi_lower_info == NULL)) {
/*
* Intern the lower/lowest mount into
* this mount info on first lookup.
*/
KASSERT(fmi->fmi_ref_cnt == 1);
fmi_lower = fstrans_mount_get(mp->mnt_lower);
if (fmi_lower && fmi_lower->fmi_lower_info)
fmi_lower = fmi_lower->fmi_lower_info;
if (fmi_lower == NULL)
return NULL;
fmi->fmi_lower_info = fmi_lower;
fmi->fmi_lower_info->fmi_ref_cnt += 1;
}
return fmi;
}
}
return NULL;
}
/*
* Dereference mount state.
*/
static void
fstrans_mount_dtor(struct fstrans_mount_info *fmi)
{
KASSERT(mutex_owned(&fstrans_lock));
KASSERT(fmi != NULL);
fmi->fmi_ref_cnt -= 1;
if (__predict_true(fmi->fmi_ref_cnt > 0)) {
return;
}
KASSERT(fmi->fmi_state == FSTRANS_NORMAL);
KASSERT(LIST_FIRST(&fmi->fmi_cow_handler) == NULL);
KASSERT(fmi->fmi_owner == NULL);
if (fmi->fmi_lower_info)
fstrans_mount_dtor(fmi->fmi_lower_info);
KASSERT(fstrans_gone_count > 0);
fstrans_gone_count -= 1;
KASSERT(fmi->fmi_mount->mnt_lower == NULL);
kmem_free(fmi->fmi_mount, sizeof(*fmi->fmi_mount));
kmem_free(fmi, sizeof(*fmi));
}
/*
* Allocate mount state.
*/
int
fstrans_mount(struct mount *mp)
{
uint32_t indx;
struct fstrans_mount_info *newfmi;
indx = fstrans_mount_hash(mp);
newfmi = kmem_alloc(sizeof(*newfmi), KM_SLEEP);
newfmi->fmi_state = FSTRANS_NORMAL;
newfmi->fmi_ref_cnt = 1;
newfmi->fmi_gone = false;
LIST_INIT(&newfmi->fmi_cow_handler);
newfmi->fmi_cow_change = false;
newfmi->fmi_mount = mp;
newfmi->fmi_lower_info = NULL;
newfmi->fmi_owner = NULL;
mutex_enter(&fstrans_lock);
SLIST_INSERT_HEAD(&fstrans_mount_hashtab[indx], newfmi, fmi_hash);
mutex_exit(&fstrans_lock);
return 0;
}
/*
* Deallocate mount state.
*/
void
fstrans_unmount(struct mount *mp)
{
uint32_t indx;
struct fstrans_mount_info *fmi;
indx = fstrans_mount_hash(mp);
mutex_enter(&fstrans_lock);
fmi = fstrans_mount_get(mp);
KASSERT(fmi != NULL);
fmi->fmi_gone = true;
SLIST_REMOVE(&fstrans_mount_hashtab[indx],
fmi, fstrans_mount_info, fmi_hash);
fstrans_gone_count += 1;
fstrans_mount_dtor(fmi);
mutex_exit(&fstrans_lock);
}
/*
* Clear mount entries whose mount is gone.
*/
static void
fstrans_clear_lwp_info(void)
{
struct fstrans_lwp_info **p, *fli, *tofree = NULL;
/*
* Scan our list clearing entries whose mount is gone.
*/
mutex_enter(&fstrans_lock);
for (p = &curlwp->l_fstrans; *p; ) {
fli = *p;
if (fli->fli_mount != NULL &&
fli->fli_mountinfo->fmi_gone &&
fli->fli_trans_cnt == 0 &&
fli->fli_cow_cnt == 0 &&
fli->fli_alias_cnt == 0) {
*p = (*p)->fli_succ;
fstrans_mount_dtor(fli->fli_mountinfo);
if (fli->fli_alias) {
KASSERT(fli->fli_alias->fli_alias_cnt > 0);
fli->fli_alias->fli_alias_cnt--;
}
fli->fli_mount = NULL;
fli->fli_alias = NULL;
fli->fli_mountinfo = NULL;
fli->fli_self = NULL;
p = &curlwp->l_fstrans;
fli->fli_succ = tofree;
tofree = fli;
} else {
p = &(*p)->fli_succ;
}
}
#ifdef DIAGNOSTIC
for (fli = curlwp->l_fstrans; fli; fli = fli->fli_succ)
if (fli->fli_alias != NULL)
KASSERT(fli->fli_alias->fli_self == curlwp);
#endif /* DIAGNOSTIC */
mutex_exit(&fstrans_lock);
while (tofree != NULL) {
fli = tofree;
tofree = fli->fli_succ;
pool_cache_put(fstrans_lwp_cache, fli);
}
}
/*
* Allocate and return per lwp info for this mount.
*/
static struct fstrans_lwp_info *
fstrans_alloc_lwp_info(struct mount *mp)
{
struct fstrans_lwp_info *fli, *fli_lower;
struct fstrans_mount_info *fmi;
for (fli = curlwp->l_fstrans; fli; fli = fli->fli_succ) {
if (fli->fli_mount == mp)
return fli;
}
/*
* Lookup mount info and get lower mount per lwp info.
*/
mutex_enter(&fstrans_lock);
fmi = fstrans_mount_get(mp);
if (fmi == NULL) {
mutex_exit(&fstrans_lock);
return NULL;
}
fmi->fmi_ref_cnt += 1;
mutex_exit(&fstrans_lock);
if (fmi->fmi_lower_info) {
fli_lower =
fstrans_alloc_lwp_info(fmi->fmi_lower_info->fmi_mount);
if (fli_lower == NULL) {
mutex_enter(&fstrans_lock);
fstrans_mount_dtor(fmi);
mutex_exit(&fstrans_lock);
return NULL;
}
} else {
fli_lower = NULL;
}
/*
* Allocate a new entry.
*/
fli = pool_cache_get(fstrans_lwp_cache, PR_WAITOK);
KASSERT(fli->fli_trans_cnt == 0);
KASSERT(fli->fli_cow_cnt == 0);
KASSERT(fli->fli_alias_cnt == 0);
KASSERT(fli->fli_mount == NULL);
KASSERT(fli->fli_alias == NULL);
KASSERT(fli->fli_mountinfo == NULL);
KASSERT(fli->fli_self == NULL);
/*
* Attach the mount info and alias.
*/
fli->fli_self = curlwp;
fli->fli_mount = mp;
fli->fli_mountinfo = fmi;
fli->fli_succ = curlwp->l_fstrans;
curlwp->l_fstrans = fli;
if (fli_lower) {
fli->fli_alias = fli_lower;
fli->fli_alias->fli_alias_cnt++;
fli = fli->fli_alias;
}
return fli;
}
/*
* Retrieve the per lwp info for this mount allocating if necessary.
*/
static inline struct fstrans_lwp_info *
fstrans_get_lwp_info(struct mount *mp, bool do_alloc)
{
struct fstrans_lwp_info *fli;
/*
* Scan our list for a match.
*/
for (fli = curlwp->l_fstrans; fli; fli = fli->fli_succ) {
if (fli->fli_mount == mp) {
KASSERT(mp->mnt_lower == NULL ||
fli->fli_alias != NULL);
if (fli->fli_alias != NULL)
fli = fli->fli_alias;
break;
}
}
if (do_alloc) {
if (__predict_false(fli == NULL))
fli = fstrans_alloc_lwp_info(mp);
}
return fli;
}
/*
* Check if this lock type is granted at this state.
*/
static bool
grant_lock(const struct fstrans_mount_info *fmi,
const enum fstrans_lock_type type)
{
if (__predict_true(fmi->fmi_state == FSTRANS_NORMAL))
return true;
if (fmi->fmi_owner == curlwp)
return true;
if (fmi->fmi_state == FSTRANS_SUSPENDING && type == FSTRANS_LAZY)
return true;
return false;
}
/*
* Start a transaction. If this thread already has a transaction on this
* file system increment the reference counter.
*/
static inline int
_fstrans_start(struct mount *mp, enum fstrans_lock_type lock_type, int wait)
{
int s;
struct fstrans_lwp_info *fli;
struct fstrans_mount_info *fmi;
ASSERT_SLEEPABLE();
fli = fstrans_get_lwp_info(mp, true);
if (fli == NULL)
return 0;
fmi = fli->fli_mountinfo;
if (fli->fli_trans_cnt > 0) {
fli->fli_trans_cnt += 1;
return 0;
}
s = pserialize_read_enter();
if (__predict_true(grant_lock(fmi, lock_type))) {
fli->fli_trans_cnt = 1;
fli->fli_lock_type = lock_type;
pserialize_read_exit(s);
return 0;
}
pserialize_read_exit(s);
if (! wait)
return EBUSY;
mutex_enter(&fstrans_lock);
while (! grant_lock(fmi, lock_type))
cv_wait(&fstrans_state_cv, &fstrans_lock);
fli->fli_trans_cnt = 1;
fli->fli_lock_type = lock_type;
mutex_exit(&fstrans_lock);
return 0;
}
void
fstrans_start(struct mount *mp)
{
int error __diagused;
error = _fstrans_start(mp, FSTRANS_SHARED, 1);
KASSERT(error == 0);
}
int
fstrans_start_nowait(struct mount *mp)
{
return _fstrans_start(mp, FSTRANS_SHARED, 0);
}
void
fstrans_start_lazy(struct mount *mp)
{
int error __diagused;
error = _fstrans_start(mp, FSTRANS_LAZY, 1);
KASSERT(error == 0);
}
/*
* Finish a transaction.
*/
void
fstrans_done(struct mount *mp)
{
int s;
struct fstrans_lwp_info *fli;
struct fstrans_mount_info *fmi;
fli = fstrans_get_lwp_info(mp, false);
if (fli == NULL)
return;
fmi = fli->fli_mountinfo;
KASSERT(fli->fli_trans_cnt > 0);
if (fli->fli_trans_cnt > 1) {
fli->fli_trans_cnt -= 1;
return;
}
if (__predict_false(fstrans_gone_count > 0))
fstrans_clear_lwp_info();
s = pserialize_read_enter();
if (__predict_true(fmi->fmi_state == FSTRANS_NORMAL)) {
fli->fli_trans_cnt = 0;
pserialize_read_exit(s);
return;
}
pserialize_read_exit(s);
mutex_enter(&fstrans_lock);
fli->fli_trans_cnt = 0;
cv_signal(&fstrans_count_cv);
mutex_exit(&fstrans_lock);
}
/*
* Check if we hold an lock.
*/
int
fstrans_held(struct mount *mp)
{
struct fstrans_lwp_info *fli;
struct fstrans_mount_info *fmi;
KASSERT(mp != dead_rootmount);
fli = fstrans_get_lwp_info(mp, false);
if (fli == NULL)
return 0;
fmi = fli->fli_mountinfo;
return (fli->fli_trans_cnt > 0 || fmi->fmi_owner == curlwp);
}
/*
* Check if this thread has an exclusive lock.
*/
int
fstrans_is_owner(struct mount *mp)
{
struct fstrans_lwp_info *fli;
struct fstrans_mount_info *fmi;
KASSERT(mp != dead_rootmount);
fli = fstrans_get_lwp_info(mp, false);
if (fli == NULL)
return 0;
fmi = fli->fli_mountinfo;
return (fmi->fmi_owner == curlwp);
}
/*
* True, if no thread is in a transaction not granted at the current state.
*/
static bool
state_change_done(const struct fstrans_mount_info *fmi)
{
struct fstrans_lwp_info *fli;
KASSERT(mutex_owned(&fstrans_lock));
LIST_FOREACH(fli, &fstrans_fli_head, fli_list) {
if (fli->fli_mountinfo != fmi)
continue;
if (fli->fli_trans_cnt == 0)
continue;
if (fli->fli_self == curlwp)
continue;
if (grant_lock(fmi, fli->fli_lock_type))
continue;
return false;
}
return true;
}
/*
* Set new file system state.
*/
int
fstrans_setstate(struct mount *mp, enum fstrans_state new_state)
{
int error;
enum fstrans_state old_state;
struct fstrans_lwp_info *fli;
struct fstrans_mount_info *fmi;
KASSERT(mp != dead_rootmount);
fli = fstrans_get_lwp_info(mp, true);
if (fli == NULL)
return ENOENT;
fmi = fli->fli_mountinfo;
old_state = fmi->fmi_state;
if (old_state == new_state)
return 0;
mutex_enter(&fstrans_lock);
fmi->fmi_state = new_state;
pserialize_perform(fstrans_psz);
/*
* All threads see the new state now.
* Wait for transactions invalid at this state to leave.
*/
error = 0;
while (! state_change_done(fmi)) {
error = cv_wait_sig(&fstrans_count_cv, &fstrans_lock);
if (error) {
new_state = fmi->fmi_state = FSTRANS_NORMAL;
break;
}
}
if (old_state != new_state) {
if (old_state == FSTRANS_NORMAL) {
KASSERT(fmi->fmi_owner == NULL);
fmi->fmi_owner = curlwp;
}
if (new_state == FSTRANS_NORMAL) {
KASSERT(fmi->fmi_owner == curlwp);
fmi->fmi_owner = NULL;
}
}
cv_broadcast(&fstrans_state_cv);
mutex_exit(&fstrans_lock);
return error;
}
/*
* Get current file system state.
*/
enum fstrans_state
fstrans_getstate(struct mount *mp)
{
struct fstrans_lwp_info *fli;
struct fstrans_mount_info *fmi;
KASSERT(mp != dead_rootmount);
fli = fstrans_get_lwp_info(mp, true);
KASSERT(fli != NULL);
fmi = fli->fli_mountinfo;
return fmi->fmi_state;
}
/*
* Request a filesystem to suspend all operations.
*/
int
vfs_suspend(struct mount *mp, int nowait)
{
struct fstrans_lwp_info *fli;
int error;
if (mp == dead_rootmount)
return EOPNOTSUPP;
fli = fstrans_get_lwp_info(mp, true);
if (fli == NULL)
return ENOENT;
if (nowait) {
if (!mutex_tryenter(&vfs_suspend_lock))
return EWOULDBLOCK;
} else
mutex_enter(&vfs_suspend_lock);
if ((error = VFS_SUSPENDCTL(fli->fli_mount, SUSPEND_SUSPEND)) != 0) {
mutex_exit(&vfs_suspend_lock);
return error;
}
if ((mp->mnt_iflag & IMNT_GONE) != 0) {
vfs_resume(mp);
return ENOENT;
}
return 0;
}
/*
* Request a filesystem to resume all operations.
*/
void
vfs_resume(struct mount *mp)
{
struct fstrans_lwp_info *fli;
KASSERT(mp != dead_rootmount);
fli = fstrans_get_lwp_info(mp, false);
mp = fli->fli_mount;
VFS_SUSPENDCTL(mp, SUSPEND_RESUME);
mutex_exit(&vfs_suspend_lock);
}
/*
* True, if no thread is running a cow handler.
*/
static bool
cow_state_change_done(const struct fstrans_mount_info *fmi)
{
struct fstrans_lwp_info *fli;
KASSERT(mutex_owned(&fstrans_lock));
KASSERT(fmi->fmi_cow_change);
LIST_FOREACH(fli, &fstrans_fli_head, fli_list) {
if (fli->fli_mount != fmi->fmi_mount)
continue;
if (fli->fli_cow_cnt == 0)
continue;
return false;
}
return true;
}
/*
* Prepare for changing this mounts cow list.
* Returns with fstrans_lock locked.
*/
static void
cow_change_enter(struct fstrans_mount_info *fmi)
{
mutex_enter(&fstrans_lock);
/*
* Wait for other threads changing the list.
*/
while (fmi->fmi_cow_change)
cv_wait(&fstrans_state_cv, &fstrans_lock);
/*
* Wait until all threads are aware of a state change.
*/
fmi->fmi_cow_change = true;
pserialize_perform(fstrans_psz);
while (! cow_state_change_done(fmi))
cv_wait(&fstrans_count_cv, &fstrans_lock);
}
/*
* Done changing this mounts cow list.
*/
static void
cow_change_done(struct fstrans_mount_info *fmi)
{
KASSERT(mutex_owned(&fstrans_lock));
fmi->fmi_cow_change = false;
pserialize_perform(fstrans_psz);
cv_broadcast(&fstrans_state_cv);
mutex_exit(&fstrans_lock);
}
/*
* Add a handler to this mount.
*/
int
fscow_establish(struct mount *mp, int (*func)(void *, struct buf *, bool),
void *arg)
{
struct fstrans_mount_info *fmi;
struct fscow_handler *newch;
KASSERT(mp != dead_rootmount);
mutex_enter(&fstrans_lock);
fmi = fstrans_mount_get(mp);
KASSERT(fmi != NULL);
fmi->fmi_ref_cnt += 1;
mutex_exit(&fstrans_lock);
newch = kmem_alloc(sizeof(*newch), KM_SLEEP);
newch->ch_func = func;
newch->ch_arg = arg;
cow_change_enter(fmi);
LIST_INSERT_HEAD(&fmi->fmi_cow_handler, newch, ch_list);
cow_change_done(fmi);
return 0;
}
/*
* Remove a handler from this mount.
*/
int
fscow_disestablish(struct mount *mp, int (*func)(void *, struct buf *, bool),
void *arg)
{
struct fstrans_mount_info *fmi;
struct fscow_handler *hp = NULL;
KASSERT(mp != dead_rootmount);
mutex_enter(&fstrans_lock);
fmi = fstrans_mount_get(mp);
KASSERT(fmi != NULL);
mutex_exit(&fstrans_lock);
cow_change_enter(fmi);
LIST_FOREACH(hp, &fmi->fmi_cow_handler, ch_list)
if (hp->ch_func == func && hp->ch_arg == arg)
break;
if (hp != NULL) {
LIST_REMOVE(hp, ch_list);
kmem_free(hp, sizeof(*hp));
}
fstrans_mount_dtor(fmi);
cow_change_done(fmi);
return hp ? 0 : EINVAL;
}
/*
* Check for need to copy block that is about to be written.
*/
int
fscow_run(struct buf *bp, bool data_valid)
{
int error, s;
struct mount *mp;
struct fstrans_lwp_info *fli;
struct fstrans_mount_info *fmi;
struct fscow_handler *hp;
/*
* First check if we need run the copy-on-write handler.
*/
if ((bp->b_flags & B_COWDONE))
return 0;
if (bp->b_vp == NULL) {
bp->b_flags |= B_COWDONE;
return 0;
}
if (bp->b_vp->v_type == VBLK)
mp = spec_node_getmountedfs(bp->b_vp);
else
mp = bp->b_vp->v_mount;
if (mp == NULL || mp == dead_rootmount) {
bp->b_flags |= B_COWDONE;
return 0;
}
fli = fstrans_get_lwp_info(mp, true);
KASSERT(fli != NULL);
fmi = fli->fli_mountinfo;
/*
* On non-recursed run check if other threads
* want to change the list.
*/
if (fli->fli_cow_cnt == 0) {
s = pserialize_read_enter();
if (__predict_false(fmi->fmi_cow_change)) {
pserialize_read_exit(s);
mutex_enter(&fstrans_lock);
while (fmi->fmi_cow_change)
cv_wait(&fstrans_state_cv, &fstrans_lock);
fli->fli_cow_cnt = 1;
mutex_exit(&fstrans_lock);
} else {
fli->fli_cow_cnt = 1;
pserialize_read_exit(s);
}
} else
fli->fli_cow_cnt += 1;
/*
* Run all copy-on-write handlers, stop on error.
*/
error = 0;
LIST_FOREACH(hp, &fmi->fmi_cow_handler, ch_list)
if ((error = (*hp->ch_func)(hp->ch_arg, bp, data_valid)) != 0)
break;
if (error == 0)
bp->b_flags |= B_COWDONE;