This repository was archived by the owner on May 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathvfs_subr.c
11423 lines (9964 loc) · 286 KB
/
vfs_subr.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) 2000-2019 Apple Inc. All rights reserved.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. The rights granted to you under the License
* may not be used to create, or enable the creation or redistribution of,
* unlawful or unlicensed copies of an Apple operating system, or to
* circumvent, violate, or enable the circumvention or violation of, any
* terms of an Apple operating system software license agreement.
*
* Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_END@
*/
/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
/*
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
* (c) UNIX System Laboratories, Inc.
* All or some portions of this file are derived from material licensed
* to the University of California by American Telephone and Telegraph
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
* the permission of UNIX System Laboratories, Inc.
*
* 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. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University 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 REGENTS 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 REGENTS 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.
*
* @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
*/
/*
* NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
* support for mandatory and extensible security protections. This notice
* is included in support of clause 2.2 (b) of the Apple Public License,
* Version 2.0.
*/
/*
* External virtual filesystem routines
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc_internal.h>
#include <sys/kauth.h>
#include <sys/mount_internal.h>
#include <sys/time.h>
#include <sys/lock.h>
#include <sys/vnode.h>
#include <sys/vnode_internal.h>
#include <sys/stat.h>
#include <sys/namei.h>
#include <sys/ucred.h>
#include <sys/buf_internal.h>
#include <sys/errno.h>
#include <kern/kalloc.h>
#include <sys/uio_internal.h>
#include <sys/uio.h>
#include <sys/domain.h>
#include <sys/mbuf.h>
#include <sys/syslog.h>
#include <sys/ubc_internal.h>
#include <sys/vm.h>
#include <sys/sysctl.h>
#include <sys/filedesc.h>
#include <sys/event.h>
#include <sys/kdebug.h>
#include <sys/kauth.h>
#include <sys/user.h>
#include <sys/systm.h>
#include <sys/kern_memorystatus.h>
#include <sys/lockf.h>
#include <sys/reboot.h>
#include <miscfs/fifofs/fifo.h>
#include <nfs/nfs_conf.h>
#include <string.h>
#include <machine/machine_routines.h>
#include <kern/assert.h>
#include <mach/kern_return.h>
#include <kern/thread.h>
#include <kern/sched_prim.h>
#include <miscfs/specfs/specdev.h>
#include <mach/mach_types.h>
#include <mach/memory_object_types.h>
#include <mach/memory_object_control.h>
#include <kern/kalloc.h> /* kalloc()/kfree() */
#include <kern/clock.h> /* delay_for_interval() */
#include <libkern/OSAtomic.h> /* OSAddAtomic() */
#include <os/atomic_private.h>
#if defined(XNU_TARGET_OS_OSX)
#include <console/video_console.h>
#endif
#ifdef JOE_DEBUG
#include <libkern/OSDebug.h>
#endif
#include <vm/vm_protos.h> /* vnode_pager_vrele() */
#if CONFIG_MACF
#include <security/mac_framework.h>
#endif
#include <vfs/vfs_disk_conditioner.h>
#include <libkern/section_keywords.h>
static LCK_GRP_DECLARE(vnode_lck_grp, "vnode");
static LCK_ATTR_DECLARE(vnode_lck_attr, 0, 0);
#if CONFIG_TRIGGERS
static LCK_GRP_DECLARE(trigger_vnode_lck_grp, "trigger_vnode");
static LCK_ATTR_DECLARE(trigger_vnode_lck_attr, 0, 0);
#endif
extern lck_mtx_t mnt_list_mtx_lock;
ZONE_DECLARE(specinfo_zone, "specinfo",
sizeof(struct specinfo), ZC_NOENCRYPT | ZC_ZFREE_CLEARMEM);
ZONE_DECLARE(vnode_zone, "vnodes",
sizeof(struct vnode), ZC_NOENCRYPT | ZC_NOGC | ZC_ZFREE_CLEARMEM);
enum vtype iftovt_tab[16] = {
VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
};
int vttoif_tab[9] = {
0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
S_IFSOCK, S_IFIFO, S_IFMT,
};
/* XXX These should be in a BSD accessible Mach header, but aren't. */
extern void memory_object_mark_used(
memory_object_control_t control);
extern void memory_object_mark_unused(
memory_object_control_t control,
boolean_t rage);
extern void memory_object_mark_io_tracking(
memory_object_control_t control);
/* XXX next protptype should be from <nfs/nfs.h> */
extern int nfs_vinvalbuf(vnode_t, int, vfs_context_t, int);
extern int paniclog_append_noflush(const char *format, ...);
/* XXX next prototytype should be from libsa/stdlib.h> but conflicts libkern */
__private_extern__ void qsort(
void * array,
size_t nmembers,
size_t member_size,
int (*)(const void *, const void *));
__private_extern__ void vntblinit(void);
__private_extern__ int unlink1(vfs_context_t, vnode_t, user_addr_t,
enum uio_seg, int);
static void vnode_list_add(vnode_t);
static void vnode_async_list_add(vnode_t);
static void vnode_list_remove(vnode_t);
static void vnode_list_remove_locked(vnode_t);
static void vnode_abort_advlocks(vnode_t);
static errno_t vnode_drain(vnode_t);
static void vgone(vnode_t, int flags);
static void vclean(vnode_t vp, int flag);
static void vnode_reclaim_internal(vnode_t, int, int, int);
static void vnode_dropiocount(vnode_t);
static vnode_t checkalias(vnode_t vp, dev_t nvp_rdev);
static int vnode_reload(vnode_t);
static int unmount_callback(mount_t, __unused void *);
static void insmntque(vnode_t vp, mount_t mp);
static int mount_getvfscnt(void);
static int mount_fillfsids(fsid_t *, int );
static void vnode_iterate_setup(mount_t);
int vnode_umount_preflight(mount_t, vnode_t, int);
static int vnode_iterate_prepare(mount_t);
static int vnode_iterate_reloadq(mount_t);
static void vnode_iterate_clear(mount_t);
static mount_t vfs_getvfs_locked(fsid_t *);
static int vn_create_reg(vnode_t dvp, vnode_t *vpp, struct nameidata *ndp,
struct vnode_attr *vap, uint32_t flags, int fmode, uint32_t *statusp, vfs_context_t ctx);
static int vnode_authattr_new_internal(vnode_t dvp, struct vnode_attr *vap, int noauth, uint32_t *defaulted_fieldsp, vfs_context_t ctx);
errno_t rmdir_remove_orphaned_appleDouble(vnode_t, vfs_context_t, int *);
#ifdef JOE_DEBUG
static void record_vp(vnode_t vp, int count);
#endif
#if CONFIG_JETSAM && (DEVELOPMENT || DEBUG)
extern int bootarg_no_vnode_jetsam; /* from bsd_init.c default value is 0 */
#endif /* CONFIG_JETSAM && (DEVELOPMENT || DEBUG) */
extern int bootarg_no_vnode_drain; /* from bsd_init.c default value is 0 */
boolean_t root_is_CF_drive = FALSE;
#if CONFIG_TRIGGERS
static int vnode_resolver_create(mount_t, vnode_t, struct vnode_trigger_param *, boolean_t external);
static void vnode_resolver_detach(vnode_t);
#endif
TAILQ_HEAD(freelst, vnode) vnode_free_list; /* vnode free list */
TAILQ_HEAD(deadlst, vnode) vnode_dead_list; /* vnode dead list */
TAILQ_HEAD(async_work_lst, vnode) vnode_async_work_list;
TAILQ_HEAD(ragelst, vnode) vnode_rage_list; /* vnode rapid age list */
struct timeval rage_tv;
int rage_limit = 0;
int ragevnodes = 0;
int deadvnodes_low = 0;
int deadvnodes_high = 0;
uint64_t newvnode = 0;
uint64_t newvnode_nodead = 0;
static int vfs_unmountall_started = 0;
#define RAGE_LIMIT_MIN 100
#define RAGE_TIME_LIMIT 5
/*
* ROSV definitions
* NOTE: These are shadowed from PlatformSupport definitions, but XNU
* builds standalone.
*/
#define PLATFORM_DATA_VOLUME_MOUNT_POINT "/System/Volumes/Data"
/*
* These could be in PlatformSupport but aren't yet
*/
#define PLATFORM_PREBOOT_VOLUME_MOUNT_POINT "/System/Volumes/Preboot"
#define PLATFORM_RECOVERY_VOLUME_MOUNT_POINT "/System/Volumes/Recovery"
#if CONFIG_MOUNT_VM
#define PLATFORM_VM_VOLUME_MOUNT_POINT "/System/Volumes/VM"
#endif
struct mntlist mountlist; /* mounted filesystem list */
static int nummounts = 0;
static int print_busy_vnodes = 0; /* print out busy vnodes */
#if DIAGNOSTIC
#define VLISTCHECK(fun, vp, list) \
if ((vp)->v_freelist.tqe_prev == (struct vnode **)0xdeadb) \
panic("%s: %s vnode not on %slist", (fun), (list), (list));
#else
#define VLISTCHECK(fun, vp, list)
#endif /* DIAGNOSTIC */
#define VLISTNONE(vp) \
do { \
(vp)->v_freelist.tqe_next = (struct vnode *)0; \
(vp)->v_freelist.tqe_prev = (struct vnode **)0xdeadb; \
} while(0)
#define VONLIST(vp) \
((vp)->v_freelist.tqe_prev != (struct vnode **)0xdeadb)
/* remove a vnode from free vnode list */
#define VREMFREE(fun, vp) \
do { \
VLISTCHECK((fun), (vp), "free"); \
TAILQ_REMOVE(&vnode_free_list, (vp), v_freelist); \
VLISTNONE((vp)); \
freevnodes--; \
} while(0)
/* remove a vnode from dead vnode list */
#define VREMDEAD(fun, vp) \
do { \
VLISTCHECK((fun), (vp), "dead"); \
TAILQ_REMOVE(&vnode_dead_list, (vp), v_freelist); \
VLISTNONE((vp)); \
vp->v_listflag &= ~VLIST_DEAD; \
deadvnodes--; \
} while(0)
/* remove a vnode from async work vnode list */
#define VREMASYNC_WORK(fun, vp) \
do { \
VLISTCHECK((fun), (vp), "async_work"); \
TAILQ_REMOVE(&vnode_async_work_list, (vp), v_freelist); \
VLISTNONE((vp)); \
vp->v_listflag &= ~VLIST_ASYNC_WORK; \
async_work_vnodes--; \
} while(0)
/* remove a vnode from rage vnode list */
#define VREMRAGE(fun, vp) \
do { \
if ( !(vp->v_listflag & VLIST_RAGE)) \
panic("VREMRAGE: vp not on rage list"); \
VLISTCHECK((fun), (vp), "rage"); \
TAILQ_REMOVE(&vnode_rage_list, (vp), v_freelist); \
VLISTNONE((vp)); \
vp->v_listflag &= ~VLIST_RAGE; \
ragevnodes--; \
} while(0)
static void async_work_continue(void);
static void vn_laundry_continue(void);
/*
* Initialize the vnode management data structures.
*/
__private_extern__ void
vntblinit(void)
{
thread_t thread = THREAD_NULL;
TAILQ_INIT(&vnode_free_list);
TAILQ_INIT(&vnode_rage_list);
TAILQ_INIT(&vnode_dead_list);
TAILQ_INIT(&vnode_async_work_list);
TAILQ_INIT(&mountlist);
microuptime(&rage_tv);
rage_limit = desiredvnodes / 100;
if (rage_limit < RAGE_LIMIT_MIN) {
rage_limit = RAGE_LIMIT_MIN;
}
deadvnodes_low = (desiredvnodes) / 100;
if (deadvnodes_low > 300) {
deadvnodes_low = 300;
}
deadvnodes_high = deadvnodes_low * 2;
/*
* create worker threads
*/
kernel_thread_start((thread_continue_t)async_work_continue, NULL, &thread);
thread_deallocate(thread);
kernel_thread_start((thread_continue_t)vn_laundry_continue, NULL, &thread);
thread_deallocate(thread);
}
/* the timeout is in 10 msecs */
int
vnode_waitforwrites(vnode_t vp, int output_target, int slpflag, int slptimeout, const char *msg)
{
int error = 0;
struct timespec ts;
if (output_target < 0) {
return EINVAL;
}
KERNEL_DEBUG(0x3010280 | DBG_FUNC_START, (int)vp, output_target, vp->v_numoutput, 0, 0);
if (vp->v_numoutput > output_target) {
slpflag |= PDROP;
vnode_lock_spin(vp);
while ((vp->v_numoutput > output_target) && error == 0) {
if (output_target) {
vp->v_flag |= VTHROTTLED;
} else {
vp->v_flag |= VBWAIT;
}
ts.tv_sec = (slptimeout / 100);
ts.tv_nsec = (slptimeout % 1000) * 10 * NSEC_PER_USEC * 1000;
error = msleep((caddr_t)&vp->v_numoutput, &vp->v_lock, (slpflag | (PRIBIO + 1)), msg, &ts);
vnode_lock_spin(vp);
}
vnode_unlock(vp);
}
KERNEL_DEBUG(0x3010280 | DBG_FUNC_END, (int)vp, output_target, vp->v_numoutput, error, 0);
return error;
}
void
vnode_startwrite(vnode_t vp)
{
OSAddAtomic(1, &vp->v_numoutput);
}
void
vnode_writedone(vnode_t vp)
{
if (vp) {
int need_wakeup = 0;
OSAddAtomic(-1, &vp->v_numoutput);
vnode_lock_spin(vp);
if (vp->v_numoutput < 0) {
panic("vnode_writedone: numoutput < 0");
}
if ((vp->v_flag & VTHROTTLED)) {
vp->v_flag &= ~VTHROTTLED;
need_wakeup = 1;
}
if ((vp->v_flag & VBWAIT) && (vp->v_numoutput == 0)) {
vp->v_flag &= ~VBWAIT;
need_wakeup = 1;
}
vnode_unlock(vp);
if (need_wakeup) {
wakeup((caddr_t)&vp->v_numoutput);
}
}
}
int
vnode_hasdirtyblks(vnode_t vp)
{
struct cl_writebehind *wbp;
/*
* Not taking the buf_mtx as there is little
* point doing it. Even if the lock is taken the
* state can change right after that. If their
* needs to be a synchronization, it must be driven
* by the caller
*/
if (vp->v_dirtyblkhd.lh_first) {
return 1;
}
if (!UBCINFOEXISTS(vp)) {
return 0;
}
wbp = vp->v_ubcinfo->cl_wbehind;
if (wbp && (wbp->cl_number || wbp->cl_scmap)) {
return 1;
}
return 0;
}
int
vnode_hascleanblks(vnode_t vp)
{
/*
* Not taking the buf_mtx as there is little
* point doing it. Even if the lock is taken the
* state can change right after that. If their
* needs to be a synchronization, it must be driven
* by the caller
*/
if (vp->v_cleanblkhd.lh_first) {
return 1;
}
return 0;
}
void
vnode_iterate_setup(mount_t mp)
{
mp->mnt_lflag |= MNT_LITER;
}
int
vnode_umount_preflight(mount_t mp, vnode_t skipvp, int flags)
{
vnode_t vp;
int ret = 0;
TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
if (vp->v_type == VDIR) {
continue;
}
if (vp == skipvp) {
continue;
}
if ((flags & SKIPSYSTEM) && ((vp->v_flag & VSYSTEM) || (vp->v_flag & VNOFLUSH))) {
continue;
}
if ((flags & SKIPSWAP) && (vp->v_flag & VSWAP)) {
continue;
}
if ((flags & WRITECLOSE) && (vp->v_writecount == 0 || vp->v_type != VREG)) {
continue;
}
/* Look for busy vnode */
if ((vp->v_usecount != 0) && ((vp->v_usecount - vp->v_kusecount) != 0)) {
ret = 1;
if (print_busy_vnodes && ((flags & FORCECLOSE) == 0)) {
vprint("vnode_umount_preflight - busy vnode", vp);
} else {
return ret;
}
} else if (vp->v_iocount > 0) {
/* Busy if iocount is > 0 for more than 3 seconds */
tsleep(&vp->v_iocount, PVFS, "vnode_drain_network", 3 * hz);
if (vp->v_iocount > 0) {
ret = 1;
if (print_busy_vnodes && ((flags & FORCECLOSE) == 0)) {
vprint("vnode_umount_preflight - busy vnode", vp);
} else {
return ret;
}
}
continue;
}
}
return ret;
}
/*
* This routine prepares iteration by moving all the vnodes to worker queue
* called with mount lock held
*/
int
vnode_iterate_prepare(mount_t mp)
{
vnode_t vp;
if (TAILQ_EMPTY(&mp->mnt_vnodelist)) {
/* nothing to do */
return 0;
}
vp = TAILQ_FIRST(&mp->mnt_vnodelist);
vp->v_mntvnodes.tqe_prev = &(mp->mnt_workerqueue.tqh_first);
mp->mnt_workerqueue.tqh_first = mp->mnt_vnodelist.tqh_first;
mp->mnt_workerqueue.tqh_last = mp->mnt_vnodelist.tqh_last;
TAILQ_INIT(&mp->mnt_vnodelist);
if (mp->mnt_newvnodes.tqh_first != NULL) {
panic("vnode_iterate_prepare: newvnode when entering vnode");
}
TAILQ_INIT(&mp->mnt_newvnodes);
return 1;
}
/* called with mount lock held */
int
vnode_iterate_reloadq(mount_t mp)
{
int moved = 0;
/* add the remaining entries in workerq to the end of mount vnode list */
if (!TAILQ_EMPTY(&mp->mnt_workerqueue)) {
struct vnode * mvp;
mvp = TAILQ_LAST(&mp->mnt_vnodelist, vnodelst);
/* Joining the workerque entities to mount vnode list */
if (mvp) {
mvp->v_mntvnodes.tqe_next = mp->mnt_workerqueue.tqh_first;
} else {
mp->mnt_vnodelist.tqh_first = mp->mnt_workerqueue.tqh_first;
}
mp->mnt_workerqueue.tqh_first->v_mntvnodes.tqe_prev = mp->mnt_vnodelist.tqh_last;
mp->mnt_vnodelist.tqh_last = mp->mnt_workerqueue.tqh_last;
TAILQ_INIT(&mp->mnt_workerqueue);
}
/* add the newvnodes to the head of mount vnode list */
if (!TAILQ_EMPTY(&mp->mnt_newvnodes)) {
struct vnode * nlvp;
nlvp = TAILQ_LAST(&mp->mnt_newvnodes, vnodelst);
mp->mnt_newvnodes.tqh_first->v_mntvnodes.tqe_prev = &mp->mnt_vnodelist.tqh_first;
nlvp->v_mntvnodes.tqe_next = mp->mnt_vnodelist.tqh_first;
if (mp->mnt_vnodelist.tqh_first) {
mp->mnt_vnodelist.tqh_first->v_mntvnodes.tqe_prev = &nlvp->v_mntvnodes.tqe_next;
} else {
mp->mnt_vnodelist.tqh_last = mp->mnt_newvnodes.tqh_last;
}
mp->mnt_vnodelist.tqh_first = mp->mnt_newvnodes.tqh_first;
TAILQ_INIT(&mp->mnt_newvnodes);
moved = 1;
}
return moved;
}
void
vnode_iterate_clear(mount_t mp)
{
mp->mnt_lflag &= ~MNT_LITER;
}
#if defined(__x86_64__)
#include <i386/panic_hooks.h>
struct vnode_iterate_panic_hook {
panic_hook_t hook;
mount_t mp;
struct vnode *vp;
};
static void
vnode_iterate_panic_hook(panic_hook_t *hook_)
{
struct vnode_iterate_panic_hook *hook = (struct vnode_iterate_panic_hook *)hook_;
panic_phys_range_t range;
uint64_t phys;
if (panic_phys_range_before(hook->mp, &phys, &range)) {
paniclog_append_noflush("mp = %p, phys = %p, prev (%p: %p-%p)\n",
hook->mp, phys, range.type, range.phys_start,
range.phys_start + range.len);
} else {
paniclog_append_noflush("mp = %p, phys = %p, prev (!)\n", hook->mp, phys);
}
if (panic_phys_range_before(hook->vp, &phys, &range)) {
paniclog_append_noflush("vp = %p, phys = %p, prev (%p: %p-%p)\n",
hook->vp, phys, range.type, range.phys_start,
range.phys_start + range.len);
} else {
paniclog_append_noflush("vp = %p, phys = %p, prev (!)\n", hook->vp, phys);
}
panic_dump_mem((void *)(((vm_offset_t)hook->mp - 4096) & ~4095), 12288);
}
#endif /* defined(__x86_64__) */
int
vnode_iterate(mount_t mp, int flags, int (*callout)(struct vnode *, void *),
void *arg)
{
struct vnode *vp;
int vid, retval;
int ret = 0;
/*
* The mount iterate mutex is held for the duration of the iteration.
* This can be done by a state flag on the mount structure but we can
* run into priority inversion issues sometimes.
* Using a mutex allows us to benefit from the priority donation
* mechanisms in the kernel for locks. This mutex should never be
* acquired in spin mode and it should be acquired before attempting to
* acquire the mount lock.
*/
mount_iterate_lock(mp);
mount_lock(mp);
vnode_iterate_setup(mp);
/* If it returns 0 then there is nothing to do */
retval = vnode_iterate_prepare(mp);
if (retval == 0) {
vnode_iterate_clear(mp);
mount_unlock(mp);
mount_iterate_unlock(mp);
return ret;
}
#if defined(__x86_64__)
struct vnode_iterate_panic_hook hook;
hook.mp = mp;
hook.vp = NULL;
panic_hook(&hook.hook, vnode_iterate_panic_hook);
#endif
/* iterate over all the vnodes */
while (!TAILQ_EMPTY(&mp->mnt_workerqueue)) {
vp = TAILQ_FIRST(&mp->mnt_workerqueue);
#if defined(__x86_64__)
hook.vp = vp;
#endif
TAILQ_REMOVE(&mp->mnt_workerqueue, vp, v_mntvnodes);
TAILQ_INSERT_TAIL(&mp->mnt_vnodelist, vp, v_mntvnodes);
vid = vp->v_id;
if ((vp->v_data == NULL) || (vp->v_type == VNON) || (vp->v_mount != mp)) {
continue;
}
mount_unlock(mp);
if (vget_internal(vp, vid, (flags | VNODE_NODEAD | VNODE_WITHID | VNODE_NOSUSPEND))) {
mount_lock(mp);
continue;
}
if (flags & VNODE_RELOAD) {
/*
* we're reloading the filesystem
* cast out any inactive vnodes...
*/
if (vnode_reload(vp)) {
/* vnode will be recycled on the refcount drop */
vnode_put(vp);
mount_lock(mp);
continue;
}
}
retval = callout(vp, arg);
switch (retval) {
case VNODE_RETURNED:
case VNODE_RETURNED_DONE:
vnode_put(vp);
if (retval == VNODE_RETURNED_DONE) {
mount_lock(mp);
ret = 0;
goto out;
}
break;
case VNODE_CLAIMED_DONE:
mount_lock(mp);
ret = 0;
goto out;
case VNODE_CLAIMED:
default:
break;
}
mount_lock(mp);
}
out:
#if defined(__x86_64__)
panic_unhook(&hook.hook);
#endif
(void)vnode_iterate_reloadq(mp);
vnode_iterate_clear(mp);
mount_unlock(mp);
mount_iterate_unlock(mp);
return ret;
}
void
mount_lock_renames(mount_t mp)
{
lck_mtx_lock(&mp->mnt_renamelock);
}
void
mount_unlock_renames(mount_t mp)
{
lck_mtx_unlock(&mp->mnt_renamelock);
}
void
mount_iterate_lock(mount_t mp)
{
lck_mtx_lock(&mp->mnt_iter_lock);
}
void
mount_iterate_unlock(mount_t mp)
{
lck_mtx_unlock(&mp->mnt_iter_lock);
}
void
mount_lock(mount_t mp)
{
lck_mtx_lock(&mp->mnt_mlock);
}
void
mount_lock_spin(mount_t mp)
{
lck_mtx_lock_spin(&mp->mnt_mlock);
}
void
mount_unlock(mount_t mp)
{
lck_mtx_unlock(&mp->mnt_mlock);
}
void
mount_ref(mount_t mp, int locked)
{
if (!locked) {
mount_lock_spin(mp);
}
mp->mnt_count++;
if (!locked) {
mount_unlock(mp);
}
}
void
mount_drop(mount_t mp, int locked)
{
if (!locked) {
mount_lock_spin(mp);
}
mp->mnt_count--;
if (mp->mnt_count == 0 && (mp->mnt_lflag & MNT_LDRAIN)) {
wakeup(&mp->mnt_lflag);
}
if (!locked) {
mount_unlock(mp);
}
}
int
mount_iterref(mount_t mp, int locked)
{
int retval = 0;
if (!locked) {
mount_list_lock();
}
if (mp->mnt_iterref < 0) {
retval = 1;
} else {
mp->mnt_iterref++;
}
if (!locked) {
mount_list_unlock();
}
return retval;
}
int
mount_isdrained(mount_t mp, int locked)
{
int retval;
if (!locked) {
mount_list_lock();
}
if (mp->mnt_iterref < 0) {
retval = 1;
} else {
retval = 0;
}
if (!locked) {
mount_list_unlock();
}
return retval;
}
void
mount_iterdrop(mount_t mp)
{
mount_list_lock();
mp->mnt_iterref--;
wakeup(&mp->mnt_iterref);
mount_list_unlock();
}
void
mount_iterdrain(mount_t mp)
{
mount_list_lock();
while (mp->mnt_iterref) {
msleep((caddr_t)&mp->mnt_iterref, &mnt_list_mtx_lock, PVFS, "mount_iterdrain", NULL);
}
/* mount iterations drained */
mp->mnt_iterref = -1;
mount_list_unlock();
}
void
mount_iterreset(mount_t mp)
{
mount_list_lock();
if (mp->mnt_iterref == -1) {
mp->mnt_iterref = 0;
}
mount_list_unlock();
}
/* always called with mount lock held */
int
mount_refdrain(mount_t mp)
{
if (mp->mnt_lflag & MNT_LDRAIN) {
panic("already in drain");
}
mp->mnt_lflag |= MNT_LDRAIN;
while (mp->mnt_count) {
msleep((caddr_t)&mp->mnt_lflag, &mp->mnt_mlock, PVFS, "mount_drain", NULL);
}
if (mp->mnt_vnodelist.tqh_first != NULL) {
panic("mount_refdrain: dangling vnode");
}
mp->mnt_lflag &= ~MNT_LDRAIN;
return 0;
}
/* Tags the mount point as not supportine extended readdir for NFS exports */
void
mount_set_noreaddirext(mount_t mp)
{
mount_lock(mp);
mp->mnt_kern_flag |= MNTK_DENY_READDIREXT;
mount_unlock(mp);
}
/*
* Mark a mount point as busy. Used to synchronize access and to delay
* unmounting.
*/
int
vfs_busy(mount_t mp, int flags)
{
restart:
if (mp->mnt_lflag & MNT_LDEAD) {
return ENOENT;
}
mount_lock(mp);
if (mp->mnt_lflag & MNT_LUNMOUNT) {
if (flags & LK_NOWAIT || mp->mnt_lflag & MNT_LDEAD) {
mount_unlock(mp);
return ENOENT;
}
/*
* Since all busy locks are shared except the exclusive
* lock granted when unmounting, the only place that a
* wakeup needs to be done is at the release of the
* exclusive lock at the end of dounmount.
*/
mp->mnt_lflag |= MNT_LWAIT;
msleep((caddr_t)mp, &mp->mnt_mlock, (PVFS | PDROP), "vfsbusy", NULL);
return ENOENT;
}
mount_unlock(mp);