The test for rootfs (now mountlist) is to avoid panic'ing in sync().
[unix-history] / usr / src / sys / kern / vfs_subr.c
CommitLineData
3c4390e8 1/*
ec54f0cc
KB
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
3c4390e8 4 *
dbf0c423 5 * %sccs.include.redist.c%
3c4390e8 6 *
8981e258 7 * @(#)vfs_subr.c 8.7 (Berkeley) %G%
3c4390e8
KM
8 */
9
10/*
11 * External virtual filesystem routines
12 */
13
cb796a23 14#include <sys/param.h>
917dc539 15#include <sys/systm.h>
cb796a23
KB
16#include <sys/proc.h>
17#include <sys/mount.h>
18#include <sys/time.h>
19#include <sys/vnode.h>
807cc430 20#include <sys/stat.h>
cb796a23
KB
21#include <sys/namei.h>
22#include <sys/ucred.h>
23#include <sys/buf.h>
24#include <sys/errno.h>
25#include <sys/malloc.h>
8981e258
MH
26#include <sys/domain.h>
27#include <sys/mbuf.h>
3c4390e8 28
bb4964fd
KM
29#include <vm/vm.h>
30#include <sys/sysctl.h>
31
021de758
JSP
32#include <miscfs/specfs/specdev.h>
33
807cc430
KM
34enum vtype iftovt_tab[16] = {
35 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
36 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
37};
38int vttoif_tab[9] = {
39 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
40 S_IFSOCK, S_IFIFO, S_IFMT,
41};
42
e3249ec0
KM
43/*
44 * Insq/Remq for the vnode usage lists.
45 */
3fc2ac18
KM
46#define bufinsvn(bp, dp) LIST_INSERT_HEAD(dp, bp, b_vnbufs)
47#define bufremvn(bp) { \
48 LIST_REMOVE(bp, b_vnbufs); \
49 (bp)->b_vnbufs.le_next = NOLIST; \
50}
51
52TAILQ_HEAD(freelst, vnode) vnode_free_list; /* vnode free list */
53struct mntlist mountlist; /* mounted filesystem list */
e3249ec0 54
3c4390e8 55/*
3fc2ac18 56 * Initialize the vnode management data structures.
3c4390e8 57 */
3fc2ac18 58vntblinit()
3c4390e8
KM
59{
60
3fc2ac18
KM
61 TAILQ_INIT(&vnode_free_list);
62 TAILQ_INIT(&mountlist);
3c4390e8
KM
63}
64
65/*
66 * Lock a filesystem.
67 * Used to prevent access to it while mounting and unmounting.
68 */
69vfs_lock(mp)
70 register struct mount *mp;
71{
72
54fb9dc2
KM
73 while(mp->mnt_flag & MNT_MLOCK) {
74 mp->mnt_flag |= MNT_MWAIT;
594501df
KM
75 sleep((caddr_t)mp, PVFS);
76 }
54fb9dc2 77 mp->mnt_flag |= MNT_MLOCK;
3c4390e8
KM
78 return (0);
79}
80
81/*
82 * Unlock a locked filesystem.
83 * Panic if filesystem is not locked.
84 */
85void
86vfs_unlock(mp)
87 register struct mount *mp;
88{
89
54fb9dc2 90 if ((mp->mnt_flag & MNT_MLOCK) == 0)
36ef03ec 91 panic("vfs_unlock: not locked");
54fb9dc2
KM
92 mp->mnt_flag &= ~MNT_MLOCK;
93 if (mp->mnt_flag & MNT_MWAIT) {
94 mp->mnt_flag &= ~MNT_MWAIT;
3c4390e8
KM
95 wakeup((caddr_t)mp);
96 }
97}
98
36ef03ec
KM
99/*
100 * Mark a mount point as busy.
101 * Used to synchronize access and to delay unmounting.
102 */
103vfs_busy(mp)
104 register struct mount *mp;
105{
106
54fb9dc2
KM
107 while(mp->mnt_flag & MNT_MPBUSY) {
108 mp->mnt_flag |= MNT_MPWANT;
109 sleep((caddr_t)&mp->mnt_flag, PVFS);
36ef03ec 110 }
d8b63609
KM
111 if (mp->mnt_flag & MNT_UNMOUNT)
112 return (1);
54fb9dc2 113 mp->mnt_flag |= MNT_MPBUSY;
36ef03ec
KM
114 return (0);
115}
116
117/*
118 * Free a busy filesystem.
119 * Panic if filesystem is not busy.
120 */
36ef03ec
KM
121vfs_unbusy(mp)
122 register struct mount *mp;
123{
124
54fb9dc2 125 if ((mp->mnt_flag & MNT_MPBUSY) == 0)
36ef03ec 126 panic("vfs_unbusy: not busy");
54fb9dc2
KM
127 mp->mnt_flag &= ~MNT_MPBUSY;
128 if (mp->mnt_flag & MNT_MPWANT) {
129 mp->mnt_flag &= ~MNT_MPWANT;
130 wakeup((caddr_t)&mp->mnt_flag);
36ef03ec
KM
131 }
132}
133
3c4390e8
KM
134/*
135 * Lookup a mount point by filesystem identifier.
136 */
137struct mount *
138getvfs(fsid)
139 fsid_t *fsid;
140{
141 register struct mount *mp;
142
3fc2ac18 143 for (mp = mountlist.tqh_first; mp != NULL; mp = mp->mnt_list.tqe_next) {
54fb9dc2 144 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
3fc2ac18 145 mp->mnt_stat.f_fsid.val[1] == fsid->val[1])
d713f801 146 return (mp);
3fc2ac18 147 }
d713f801 148 return ((struct mount *)0);
3c4390e8
KM
149}
150
917dc539
JSP
151/*
152 * Get a new unique fsid
153 */
154void
155getnewfsid(mp, mtype)
156 struct mount *mp;
157 int mtype;
158{
159static u_short xxxfs_mntid;
160
161 fsid_t tfsid;
162
1209b9a4 163 mp->mnt_stat.f_fsid.val[0] = makedev(nblkdev + mtype, 0);
917dc539
JSP
164 mp->mnt_stat.f_fsid.val[1] = mtype;
165 if (xxxfs_mntid == 0)
166 ++xxxfs_mntid;
1209b9a4 167 tfsid.val[0] = makedev(nblkdev + mtype, xxxfs_mntid);
917dc539 168 tfsid.val[1] = mtype;
3fc2ac18 169 if (mountlist.tqh_first != NULL) {
17fd1cc7
JSP
170 while (getvfs(&tfsid)) {
171 tfsid.val[0]++;
172 xxxfs_mntid++;
173 }
917dc539
JSP
174 }
175 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
176}
177
3c4390e8
KM
178/*
179 * Set vnode attributes to VNOVAL
180 */
181void vattr_null(vap)
182 register struct vattr *vap;
183{
184
185 vap->va_type = VNON;
83504fd5 186 vap->va_size = vap->va_bytes = VNOVAL;
3c4390e8 187 vap->va_mode = vap->va_nlink = vap->va_uid = vap->va_gid =
83504fd5
KM
188 vap->va_fsid = vap->va_fileid =
189 vap->va_blocksize = vap->va_rdev =
ecf75a7d
KM
190 vap->va_atime.ts_sec = vap->va_atime.ts_nsec =
191 vap->va_mtime.ts_sec = vap->va_mtime.ts_nsec =
192 vap->va_ctime.ts_sec = vap->va_ctime.ts_nsec =
8cf4d4fb 193 vap->va_flags = vap->va_gen = VNOVAL;
fcba749b 194 vap->va_vaflags = 0;
3c4390e8 195}
c60798ca 196
36d09cb1
KM
197/*
198 * Routines having to do with the management of the vnode table.
199 */
9342689a 200extern int (**dead_vnodeop_p)();
32339c94 201extern void vclean();
1a80f56e 202long numvnodes;
e781da98 203extern struct vattr va_null;
3e787e54
KM
204int newnodes = 0;
205int printcnt = 0;
36d09cb1
KM
206
207/*
208 * Return the next vnode from the free list.
209 */
210getnewvnode(tag, mp, vops, vpp)
211 enum vtagtype tag;
212 struct mount *mp;
cf74dd57 213 int (**vops)();
36d09cb1
KM
214 struct vnode **vpp;
215{
c768e50f 216 register struct vnode *vp;
1f9d2249 217 int s;
36d09cb1 218
3e787e54 219newnodes++;
3fc2ac18
KM
220 if ((vnode_free_list.tqh_first == NULL &&
221 numvnodes < 2 * desiredvnodes) ||
ecf75a7d 222 numvnodes < desiredvnodes) {
aacc1bff
KM
223 vp = (struct vnode *)malloc((u_long)sizeof *vp,
224 M_VNODE, M_WAITOK);
1a80f56e 225 bzero((char *)vp, sizeof *vp);
3e787e54
KM
226 vp->v_freelist.tqe_next = (struct vnode *)0xdeadf;
227 vp->v_freelist.tqe_prev = (struct vnode **)0xdeadb;
228 vp->v_mntvnodes.le_next = (struct vnode *)0xdeadf;
229 vp->v_mntvnodes.le_prev = (struct vnode **)0xdeadb;
1a80f56e 230 numvnodes++;
3e787e54 231 vp->v_spare[0] = numvnodes;
1a80f56e 232 } else {
3fc2ac18 233 if ((vp = vnode_free_list.tqh_first) == NULL) {
1a80f56e
KM
234 tablefull("vnode");
235 *vpp = 0;
236 return (ENFILE);
237 }
238 if (vp->v_usecount)
239 panic("free vnode isn't");
3e787e54
KM
240 if (vp->v_freelist.tqe_next == (struct vnode *)0xdeadf ||
241 vp->v_freelist.tqe_prev == (struct vnode **)0xdeadb)
242 panic("getnewvnode: not on queue");
3fc2ac18 243 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
3e787e54 244 vp->v_freelist.tqe_next = (struct vnode *)0xdeadf;
0bf9bb76
KM
245 /* see comment on why 0xdeadb is set at end of vgone (below) */
246 vp->v_freelist.tqe_prev = (struct vnode **)0xdeadb;
39b99eb6 247 vp->v_lease = NULL;
1a80f56e
KM
248 if (vp->v_type != VBAD)
249 vgone(vp);
1f9d2249 250#ifdef DIAGNOSTIC
2345b093
KM
251 if (vp->v_data)
252 panic("cleaned vnode isn't");
1f9d2249
MS
253 s = splbio();
254 if (vp->v_numoutput)
255 panic("Clean vnode has pending I/O's");
256 splx(s);
257#endif
1a80f56e 258 vp->v_flag = 0;
1a80f56e 259 vp->v_lastr = 0;
1f9d2249
MS
260 vp->v_lastw = 0;
261 vp->v_lasta = 0;
262 vp->v_cstart = 0;
263 vp->v_clen = 0;
1a80f56e 264 vp->v_socket = 0;
36d09cb1 265 }
1f9d2249 266 vp->v_ralen = 1;
b027498b 267 vp->v_type = VNON;
36d09cb1
KM
268 cache_purge(vp);
269 vp->v_tag = tag;
ef24f6dd 270 vp->v_op = vops;
36d09cb1 271 insmntque(vp, mp);
36d09cb1 272 *vpp = vp;
0bf9bb76 273 vp->v_usecount = 1;
3fc2ac18 274 vp->v_data = 0;
3e787e54 275 if (printcnt-- > 0) vprint("getnewvnode got", vp);
36d09cb1
KM
276 return (0);
277}
8981e258 278
36d09cb1
KM
279/*
280 * Move a vnode from one mount queue to another.
281 */
282insmntque(vp, mp)
283 register struct vnode *vp;
284 register struct mount *mp;
285{
36d09cb1
KM
286
287 /*
288 * Delete from old mount point vnode list, if on one.
289 */
3e787e54
KM
290 if (vp->v_mount != NULL) {
291 if (vp->v_mntvnodes.le_next == (struct vnode *)0xdeadf ||
292 vp->v_mntvnodes.le_prev == (struct vnode **)0xdeadb)
293 panic("insmntque: not on queue");
3fc2ac18 294 LIST_REMOVE(vp, v_mntvnodes);
3e787e54
KM
295 vp->v_mntvnodes.le_next = (struct vnode *)0xdeadf;
296 vp->v_mntvnodes.le_prev = (struct vnode **)0xdeadb;
297 }
36d09cb1
KM
298 /*
299 * Insert into list of vnodes for the new mount point, if available.
300 */
3fc2ac18 301 if ((vp->v_mount = mp) == NULL)
36d09cb1 302 return;
3e787e54
KM
303 if (vp->v_mntvnodes.le_next != (struct vnode *)0xdeadf ||
304 vp->v_mntvnodes.le_prev != (struct vnode **)0xdeadb)
305 panic("insmntque: already on queue");
3fc2ac18 306 LIST_INSERT_HEAD(&mp->mnt_vnodelist, vp, v_mntvnodes);
36d09cb1
KM
307}
308
76429560
KM
309/*
310 * Update outstanding I/O count and do wakeup if requested.
311 */
312vwakeup(bp)
313 register struct buf *bp;
314{
315 register struct vnode *vp;
316
a9338fad 317 bp->b_flags &= ~B_WRITEINPROG;
76429560
KM
318 if (vp = bp->b_vp) {
319 vp->v_numoutput--;
1f9d2249
MS
320 if (vp->v_numoutput < 0)
321 panic("vwakeup: neg numoutput");
76429560
KM
322 if ((vp->v_flag & VBWAIT) && vp->v_numoutput <= 0) {
323 if (vp->v_numoutput < 0)
324 panic("vwakeup: neg numoutput");
325 vp->v_flag &= ~VBWAIT;
326 wakeup((caddr_t)&vp->v_numoutput);
327 }
328 }
329}
330
76429560
KM
331/*
332 * Flush out and invalidate all buffers associated with a vnode.
333 * Called with the underlying object locked.
334 */
d024c2ce 335int
c33e9e8b 336vinvalbuf(vp, flags, cred, p, slpflag, slptimeo)
76429560 337 register struct vnode *vp;
12079a9d 338 int flags;
d024c2ce
KM
339 struct ucred *cred;
340 struct proc *p;
c33e9e8b 341 int slpflag, slptimeo;
76429560
KM
342{
343 register struct buf *bp;
344 struct buf *nbp, *blist;
d024c2ce 345 int s, error;
76429560 346
12079a9d 347 if (flags & V_SAVE) {
d024c2ce
KM
348 if (error = VOP_FSYNC(vp, cred, MNT_WAIT, p))
349 return (error);
3fc2ac18 350 if (vp->v_dirtyblkhd.lh_first != NULL)
d024c2ce
KM
351 panic("vinvalbuf: dirty bufs");
352 }
76429560 353 for (;;) {
3fc2ac18 354 if ((blist = vp->v_cleanblkhd.lh_first) && flags & V_SAVEMETA)
12079a9d 355 while (blist && blist->b_lblkno < 0)
3fc2ac18
KM
356 blist = blist->b_vnbufs.le_next;
357 if (!blist && (blist = vp->v_dirtyblkhd.lh_first) &&
e3249ec0 358 (flags & V_SAVEMETA))
12079a9d 359 while (blist && blist->b_lblkno < 0)
3fc2ac18 360 blist = blist->b_vnbufs.le_next;
12079a9d 361 if (!blist)
76429560 362 break;
12079a9d 363
76429560 364 for (bp = blist; bp; bp = nbp) {
3fc2ac18 365 nbp = bp->b_vnbufs.le_next;
12079a9d
MS
366 if (flags & V_SAVEMETA && bp->b_lblkno < 0)
367 continue;
76429560
KM
368 s = splbio();
369 if (bp->b_flags & B_BUSY) {
370 bp->b_flags |= B_WANTED;
c33e9e8b
KM
371 error = tsleep((caddr_t)bp,
372 slpflag | (PRIBIO + 1), "vinvalbuf",
373 slptimeo);
76429560 374 splx(s);
c33e9e8b
KM
375 if (error)
376 return (error);
76429560
KM
377 break;
378 }
379 bremfree(bp);
380 bp->b_flags |= B_BUSY;
381 splx(s);
c33e9e8b
KM
382 /*
383 * XXX Since there are no node locks for NFS, I believe
384 * there is a slight chance that a delayed write will
385 * occur while sleeping just above, so check for it.
386 */
387 if ((bp->b_flags & B_DELWRI) && (flags & V_SAVE)) {
388 (void) VOP_BWRITE(bp);
389 break;
390 }
12079a9d 391 bp->b_flags |= B_INVAL;
76429560
KM
392 brelse(bp);
393 }
394 }
e3249ec0 395 if (!(flags & V_SAVEMETA) &&
3fc2ac18 396 (vp->v_dirtyblkhd.lh_first || vp->v_cleanblkhd.lh_first))
76429560 397 panic("vinvalbuf: flush failed");
d024c2ce 398 return (0);
76429560
KM
399}
400
401/*
402 * Associate a buffer with a vnode.
403 */
404bgetvp(vp, bp)
405 register struct vnode *vp;
406 register struct buf *bp;
407{
408
409 if (bp->b_vp)
410 panic("bgetvp: not free");
411 VHOLD(vp);
412 bp->b_vp = vp;
413 if (vp->v_type == VBLK || vp->v_type == VCHR)
414 bp->b_dev = vp->v_rdev;
415 else
416 bp->b_dev = NODEV;
417 /*
418 * Insert onto list for new vnode.
419 */
e3249ec0 420 bufinsvn(bp, &vp->v_cleanblkhd);
76429560
KM
421}
422
423/*
424 * Disassociate a buffer from a vnode.
425 */
426brelvp(bp)
427 register struct buf *bp;
428{
76429560
KM
429 struct vnode *vp;
430
431 if (bp->b_vp == (struct vnode *) 0)
432 panic("brelvp: NULL");
433 /*
434 * Delete from old vnode list, if on one.
435 */
3fc2ac18 436 if (bp->b_vnbufs.le_next != NOLIST)
e3249ec0 437 bufremvn(bp);
76429560
KM
438 vp = bp->b_vp;
439 bp->b_vp = (struct vnode *) 0;
440 HOLDRELE(vp);
441}
442
443/*
444 * Reassign a buffer from one vnode to another.
445 * Used to assign file specific control information
446 * (indirect blocks) to the vnode to which they belong.
447 */
448reassignbuf(bp, newvp)
449 register struct buf *bp;
450 register struct vnode *newvp;
451{
3fc2ac18 452 register struct buflists *listheadp;
76429560 453
e5c3f16e
KM
454 if (newvp == NULL) {
455 printf("reassignbuf: NULL");
456 return;
457 }
76429560
KM
458 /*
459 * Delete from old vnode list, if on one.
460 */
3fc2ac18 461 if (bp->b_vnbufs.le_next != NOLIST)
e3249ec0 462 bufremvn(bp);
76429560
KM
463 /*
464 * If dirty, put on list of dirty buffers;
465 * otherwise insert onto list of clean buffers.
466 */
467 if (bp->b_flags & B_DELWRI)
468 listheadp = &newvp->v_dirtyblkhd;
469 else
470 listheadp = &newvp->v_cleanblkhd;
e3249ec0 471 bufinsvn(bp, listheadp);
76429560
KM
472}
473
36d09cb1 474/*
ef24f6dd
KM
475 * Create a vnode for a block device.
476 * Used for root filesystem, argdev, and swap areas.
477 * Also used for memory file system special devices.
478 */
479bdevvp(dev, vpp)
480 dev_t dev;
481 struct vnode **vpp;
482{
ef24f6dd
KM
483 register struct vnode *vp;
484 struct vnode *nvp;
485 int error;
486
1c89915d
KM
487 if (dev == NODEV)
488 return (0);
9342689a 489 error = getnewvnode(VT_NON, (struct mount *)0, spec_vnodeop_p, &nvp);
ef24f6dd
KM
490 if (error) {
491 *vpp = 0;
492 return (error);
493 }
494 vp = nvp;
495 vp->v_type = VBLK;
c0de8792 496 if (nvp = checkalias(vp, dev, (struct mount *)0)) {
ef24f6dd
KM
497 vput(vp);
498 vp = nvp;
499 }
500 *vpp = vp;
501 return (0);
502}
503
504/*
505 * Check to see if the new vnode represents a special device
506 * for which we already have a vnode (either because of
507 * bdevvp() or because of a different vnode representing
508 * the same block device). If such an alias exists, deallocate
f0556f86 509 * the existing contents and return the aliased vnode. The
ef24f6dd
KM
510 * caller is responsible for filling it with its new contents.
511 */
512struct vnode *
c0de8792 513checkalias(nvp, nvp_rdev, mp)
ef24f6dd 514 register struct vnode *nvp;
c0de8792 515 dev_t nvp_rdev;
ef24f6dd
KM
516 struct mount *mp;
517{
518 register struct vnode *vp;
c0de8792 519 struct vnode **vpp;
ef24f6dd
KM
520
521 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
54fb9dc2 522 return (NULLVP);
c0de8792
KM
523
524 vpp = &speclisth[SPECHASH(nvp_rdev)];
ef24f6dd 525loop:
c0de8792
KM
526 for (vp = *vpp; vp; vp = vp->v_specnext) {
527 if (nvp_rdev != vp->v_rdev || nvp->v_type != vp->v_type)
ef24f6dd 528 continue;
c0de8792
KM
529 /*
530 * Alias, but not in use, so flush it out.
531 */
7f7b7d89 532 if (vp->v_usecount == 0) {
c0de8792
KM
533 vgone(vp);
534 goto loop;
535 }
3fc2ac18 536 if (vget(vp, 1))
ef62830d 537 goto loop;
ef24f6dd
KM
538 break;
539 }
c0de8792 540 if (vp == NULL || vp->v_tag != VT_NON) {
c0de8792
KM
541 MALLOC(nvp->v_specinfo, struct specinfo *,
542 sizeof(struct specinfo), M_VNODE, M_WAITOK);
543 nvp->v_rdev = nvp_rdev;
7f7b7d89 544 nvp->v_hashchain = vpp;
c0de8792 545 nvp->v_specnext = *vpp;
2c957a90 546 nvp->v_specflags = 0;
c0de8792 547 *vpp = nvp;
40452d5e
KM
548 if (vp != NULL) {
549 nvp->v_flag |= VALIASED;
550 vp->v_flag |= VALIASED;
551 vput(vp);
552 }
54fb9dc2 553 return (NULLVP);
ef24f6dd 554 }
2bae1875
KM
555 VOP_UNLOCK(vp);
556 vclean(vp, 0);
ef24f6dd
KM
557 vp->v_op = nvp->v_op;
558 vp->v_tag = nvp->v_tag;
559 nvp->v_type = VNON;
560 insmntque(vp, mp);
561 return (vp);
562}
563
564/*
565 * Grab a particular vnode from the free list, increment its
566 * reference count and lock it. The vnode lock bit is set the
567 * vnode is being eliminated in vgone. The process is awakened
568 * when the transition is completed, and an error returned to
569 * indicate that the vnode is no longer usable (possibly having
570 * been changed to a new file system type).
36d09cb1 571 */
3fc2ac18 572vget(vp, lockflag)
36d09cb1 573 register struct vnode *vp;
3fc2ac18 574 int lockflag;
36d09cb1 575{
36d09cb1 576
ef24f6dd
KM
577 if (vp->v_flag & VXLOCK) {
578 vp->v_flag |= VXWANT;
579 sleep((caddr_t)vp, PINOD);
580 return (1);
581 }
3e787e54
KM
582 if (vp->v_usecount == 0) {
583 if (vp->v_freelist.tqe_next == (struct vnode *)0xdeadf ||
584 vp->v_freelist.tqe_prev == (struct vnode **)0xdeadb)
585 panic("vget: not on queue");
3fc2ac18 586 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
3e787e54
KM
587 vp->v_freelist.tqe_next = (struct vnode *)0xdeadf;
588 vp->v_freelist.tqe_prev = (struct vnode **)0xdeadb;
589 }
ec04fc59 590 vp->v_usecount++;
3fc2ac18
KM
591 if (lockflag)
592 VOP_LOCK(vp);
3e787e54 593 if (printcnt-- > 0) vprint("vget got", vp);
ef24f6dd 594 return (0);
36d09cb1
KM
595}
596
d32390ea
KM
597int bug_refs = 0;
598
36d09cb1
KM
599/*
600 * Vnode reference, just increment the count
601 */
602void vref(vp)
603 struct vnode *vp;
604{
605
ec04fc59
KM
606 if (vp->v_usecount <= 0)
607 panic("vref used where vget required");
3e787e54
KM
608 if (vp->v_freelist.tqe_next != (struct vnode *)0xdeadf ||
609 vp->v_freelist.tqe_prev != (struct vnode **)0xdeadb)
610 panic("vref: not free");
7f7b7d89 611 vp->v_usecount++;
3e787e54 612 if (printcnt-- > 0) vprint("vref get", vp);
d32390ea
KM
613 if (vp->v_type != VBLK && curproc)
614 curproc->p_spare[0]++;
615 if (bug_refs)
616 vprint("vref: ");
36d09cb1
KM
617}
618
619/*
620 * vput(), just unlock and vrele()
621 */
622void vput(vp)
623 register struct vnode *vp;
624{
4d1ee2eb 625
36d09cb1
KM
626 VOP_UNLOCK(vp);
627 vrele(vp);
628}
629
630/*
631 * Vnode release.
632 * If count drops to zero, call inactive routine and return to freelist.
633 */
634void vrele(vp)
635 register struct vnode *vp;
636{
637
65c3b3a8 638#ifdef DIAGNOSTIC
36d09cb1 639 if (vp == NULL)
ef24f6dd 640 panic("vrele: null vp");
65c3b3a8 641#endif
7f7b7d89 642 vp->v_usecount--;
3e787e54 643 if (printcnt-- > 0) vprint("vrele put", vp);
d32390ea
KM
644 if (vp->v_type != VBLK && curproc)
645 curproc->p_spare[0]--;
646 if (bug_refs)
647 vprint("vref: ");
7f7b7d89 648 if (vp->v_usecount > 0)
36d09cb1 649 return;
65c3b3a8
KM
650#ifdef DIAGNOSTIC
651 if (vp->v_usecount != 0 || vp->v_writecount != 0) {
652 vprint("vrele: bad ref count", vp);
653 panic("vrele: ref cnt");
654 }
655#endif
dc998e72
KM
656 /*
657 * insert at tail of LRU list
658 */
3e787e54
KM
659 if (vp->v_freelist.tqe_next != (struct vnode *)0xdeadf ||
660 vp->v_freelist.tqe_prev != (struct vnode **)0xdeadb)
661 panic("vrele: not free");
3fc2ac18 662 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
d024c2ce 663 VOP_INACTIVE(vp);
ef24f6dd
KM
664}
665
7f7b7d89
KM
666/*
667 * Page or buffer structure gets a reference.
668 */
451df175 669void vhold(vp)
7f7b7d89
KM
670 register struct vnode *vp;
671{
672
673 vp->v_holdcnt++;
674}
675
676/*
677 * Page or buffer structure frees a reference.
678 */
451df175 679void holdrele(vp)
7f7b7d89
KM
680 register struct vnode *vp;
681{
682
683 if (vp->v_holdcnt <= 0)
684 panic("holdrele: holdcnt");
685 vp->v_holdcnt--;
686}
687
f0556f86
KM
688/*
689 * Remove any vnodes in the vnode table belonging to mount point mp.
690 *
691 * If MNT_NOFORCE is specified, there should not be any active ones,
692 * return error if any are found (nb: this is a user error, not a
693 * system error). If MNT_FORCE is specified, detach any active vnodes
694 * that are found.
695 */
8981e258 696#ifdef DIAGNOSTIC
bb4964fd
KM
697int busyprt = 0; /* print out busy vnodes */
698struct ctldebug debug1 = { "busyprt", &busyprt };
8981e258 699#endif
f0556f86
KM
700
701vflush(mp, skipvp, flags)
702 struct mount *mp;
703 struct vnode *skipvp;
704 int flags;
705{
706 register struct vnode *vp, *nvp;
707 int busy = 0;
708
54fb9dc2 709 if ((mp->mnt_flag & MNT_MPBUSY) == 0)
36ef03ec 710 panic("vflush: not busy");
4597dd33 711loop:
3fc2ac18 712 for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) {
4597dd33
KM
713 if (vp->v_mount != mp)
714 goto loop;
3fc2ac18 715 nvp = vp->v_mntvnodes.le_next;
f0556f86
KM
716 /*
717 * Skip over a selected vnode.
f0556f86
KM
718 */
719 if (vp == skipvp)
720 continue;
36ef03ec
KM
721 /*
722 * Skip over a vnodes marked VSYSTEM.
723 */
724 if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM))
725 continue;
da374605
KM
726 /*
727 * If WRITECLOSE is set, only flush out regular file
728 * vnodes open for writing.
729 */
730 if ((flags & WRITECLOSE) &&
731 (vp->v_writecount == 0 || vp->v_type != VREG))
732 continue;
f0556f86 733 /*
7f7b7d89 734 * With v_usecount == 0, all we need to do is clear
f0556f86
KM
735 * out the vnode data structures and we are done.
736 */
7f7b7d89 737 if (vp->v_usecount == 0) {
f0556f86
KM
738 vgone(vp);
739 continue;
740 }
741 /*
da374605 742 * If FORCECLOSE is set, forcibly close the vnode.
f0556f86
KM
743 * For block or character devices, revert to an
744 * anonymous device. For all other files, just kill them.
745 */
36ef03ec 746 if (flags & FORCECLOSE) {
f0556f86
KM
747 if (vp->v_type != VBLK && vp->v_type != VCHR) {
748 vgone(vp);
749 } else {
750 vclean(vp, 0);
9342689a 751 vp->v_op = spec_vnodeop_p;
f0556f86
KM
752 insmntque(vp, (struct mount *)0);
753 }
754 continue;
755 }
8981e258 756#ifdef DIAGNOSTIC
f0556f86 757 if (busyprt)
0bf84b18 758 vprint("vflush: busy vnode", vp);
8981e258 759#endif
f0556f86
KM
760 busy++;
761 }
762 if (busy)
763 return (EBUSY);
764 return (0);
765}
766
ef24f6dd
KM
767/*
768 * Disassociate the underlying file system from a vnode.
ef24f6dd 769 */
ecf75a7d
KM
770void
771vclean(vp, flags)
ef24f6dd 772 register struct vnode *vp;
aacc1bff 773 int flags;
ef24f6dd 774{
2bae1875 775 int active;
ef24f6dd 776
2bae1875
KM
777 /*
778 * Check to see if the vnode is in use.
0bf84b18
KM
779 * If so we have to reference it before we clean it out
780 * so that its count cannot fall to zero and generate a
781 * race against ourselves to recycle it.
2bae1875 782 */
7f7b7d89 783 if (active = vp->v_usecount)
2bae1875 784 VREF(vp);
669df1aa
KM
785 /*
786 * Even if the count is zero, the VOP_INACTIVE routine may still
787 * have the object locked while it cleans it out. The VOP_LOCK
788 * ensures that the VOP_INACTIVE routine is done with its work.
789 * For active vnodes, it ensures that no other activity can
790 * occur while the underlying object is being cleaned out.
791 */
792 VOP_LOCK(vp);
2bae1875
KM
793 /*
794 * Prevent the vnode from being recycled or
795 * brought into use while we clean it out.
796 */
0bf84b18
KM
797 if (vp->v_flag & VXLOCK)
798 panic("vclean: deadlock");
ef24f6dd 799 vp->v_flag |= VXLOCK;
0bf84b18 800 /*
669df1aa 801 * Clean out any buffers associated with the vnode.
0bf84b18 802 */
36ef03ec 803 if (flags & DOCLOSE)
c33e9e8b 804 vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
ef24f6dd 805 /*
669df1aa
KM
806 * Any other processes trying to obtain this lock must first
807 * wait for VXLOCK to clear, then call the new lock operation.
ef24f6dd 808 */
669df1aa 809 VOP_UNLOCK(vp);
ef24f6dd 810 /*
669df1aa
KM
811 * If purging an active vnode, it must be closed and
812 * deactivated before being reclaimed.
ef24f6dd 813 */
2bae1875 814 if (active) {
669df1aa
KM
815 if (flags & DOCLOSE)
816 VOP_CLOSE(vp, IO_NDELAY, NOCRED, NULL);
817 VOP_INACTIVE(vp);
ef24f6dd
KM
818 }
819 /*
820 * Reclaim the vnode.
821 */
669df1aa 822 if (VOP_RECLAIM(vp))
ef24f6dd 823 panic("vclean: cannot reclaim");
2bae1875
KM
824 if (active)
825 vrele(vp);
38c46eee 826
ef24f6dd 827 /*
669df1aa 828 * Done with purge, notify sleepers of the grim news.
ef24f6dd 829 */
669df1aa
KM
830 vp->v_op = dead_vnodeop_p;
831 vp->v_tag = VT_NON;
ef24f6dd
KM
832 vp->v_flag &= ~VXLOCK;
833 if (vp->v_flag & VXWANT) {
834 vp->v_flag &= ~VXWANT;
835 wakeup((caddr_t)vp);
836 }
837}
838
ef62830d
KM
839/*
840 * Eliminate all activity associated with the requested vnode
841 * and with all vnodes aliased to the requested vnode.
842 */
843void vgoneall(vp)
844 register struct vnode *vp;
845{
7f7b7d89 846 register struct vnode *vq;
ef62830d 847
7a7b3a95
KM
848 if (vp->v_flag & VALIASED) {
849 /*
850 * If a vgone (or vclean) is already in progress,
851 * wait until it is done and return.
852 */
853 if (vp->v_flag & VXLOCK) {
854 vp->v_flag |= VXWANT;
855 sleep((caddr_t)vp, PINOD);
856 return;
857 }
858 /*
859 * Ensure that vp will not be vgone'd while we
860 * are eliminating its aliases.
861 */
862 vp->v_flag |= VXLOCK;
863 while (vp->v_flag & VALIASED) {
864 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
865 if (vq->v_rdev != vp->v_rdev ||
866 vq->v_type != vp->v_type || vp == vq)
867 continue;
868 vgone(vq);
869 break;
870 }
ef62830d 871 }
7a7b3a95
KM
872 /*
873 * Remove the lock so that vgone below will
874 * really eliminate the vnode after which time
875 * vgone will awaken any sleepers.
876 */
877 vp->v_flag &= ~VXLOCK;
ef62830d
KM
878 }
879 vgone(vp);
880}
881
ef24f6dd
KM
882/*
883 * Eliminate all activity associated with a vnode
884 * in preparation for reuse.
885 */
886void vgone(vp)
887 register struct vnode *vp;
888{
7f7b7d89 889 register struct vnode *vq;
c0de8792 890 struct vnode *vx;
ef24f6dd 891
4f55e3ec
KM
892 /*
893 * If a vgone (or vclean) is already in progress,
894 * wait until it is done and return.
895 */
896 if (vp->v_flag & VXLOCK) {
897 vp->v_flag |= VXWANT;
898 sleep((caddr_t)vp, PINOD);
899 return;
900 }
ef24f6dd
KM
901 /*
902 * Clean out the filesystem specific data.
903 */
36ef03ec 904 vclean(vp, DOCLOSE);
ef24f6dd
KM
905 /*
906 * Delete from old mount point vnode list, if on one.
907 */
3fc2ac18 908 if (vp->v_mount != NULL) {
3e787e54
KM
909 if (vp->v_mntvnodes.le_next == (struct vnode *)0xdeadf ||
910 vp->v_mntvnodes.le_prev == (struct vnode **)0xdeadb)
911 panic("vgone: not on queue");
3fc2ac18 912 LIST_REMOVE(vp, v_mntvnodes);
3e787e54
KM
913 vp->v_mntvnodes.le_next = (struct vnode *)0xdeadf;
914 vp->v_mntvnodes.le_prev = (struct vnode **)0xdeadb;
d10e9258 915 vp->v_mount = NULL;
ef24f6dd
KM
916 }
917 /*
918 * If special device, remove it from special device alias list.
919 */
920 if (vp->v_type == VBLK || vp->v_type == VCHR) {
7f7b7d89
KM
921 if (*vp->v_hashchain == vp) {
922 *vp->v_hashchain = vp->v_specnext;
ef24f6dd 923 } else {
7f7b7d89 924 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
c0de8792 925 if (vq->v_specnext != vp)
ef24f6dd 926 continue;
c0de8792 927 vq->v_specnext = vp->v_specnext;
ef24f6dd
KM
928 break;
929 }
c0de8792 930 if (vq == NULL)
ef24f6dd
KM
931 panic("missing bdev");
932 }
c0de8792 933 if (vp->v_flag & VALIASED) {
4d1ee2eb 934 vx = NULL;
7f7b7d89 935 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
de81e10c
KM
936 if (vq->v_rdev != vp->v_rdev ||
937 vq->v_type != vp->v_type)
c0de8792 938 continue;
4d1ee2eb
CT
939 if (vx)
940 break;
c0de8792
KM
941 vx = vq;
942 }
4d1ee2eb 943 if (vx == NULL)
c0de8792 944 panic("missing alias");
4d1ee2eb 945 if (vq == NULL)
c0de8792
KM
946 vx->v_flag &= ~VALIASED;
947 vp->v_flag &= ~VALIASED;
948 }
949 FREE(vp->v_specinfo, M_VNODE);
950 vp->v_specinfo = NULL;
ef24f6dd
KM
951 }
952 /*
3387ef89 953 * If it is on the freelist and not already at the head,
0bf9bb76
KM
954 * move it to the head of the list. The test of the back
955 * pointer and the reference count of zero is because
956 * it will be removed from the free list by getnewvnode,
957 * but will not have its reference count incremented until
958 * after calling vgone. If the reference count were
959 * incremented first, vgone would (incorrectly) try to
960 * close the previous instance of the underlying object.
961 * So, the back pointer is explicitly set to `0xdeadb' in
962 * getnewvnode after removing it from the freelist to ensure
963 * that we do not try to move it here.
ef24f6dd 964 */
0bf9bb76
KM
965 if (vp->v_usecount == 0 &&
966 vp->v_freelist.tqe_prev != (struct vnode **)0xdeadb &&
967 vnode_free_list.tqh_first != vp) {
3e787e54
KM
968 if (vp->v_freelist.tqe_next == (struct vnode *)0xdeadf)
969 panic("vgone: use 0, not free");
3fc2ac18
KM
970 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
971 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
ef24f6dd 972 }
2bae1875 973 vp->v_type = VBAD;
36d09cb1 974}
ef62830d 975
2bcd6066
KM
976/*
977 * Lookup a vnode by device number.
978 */
979vfinddev(dev, type, vpp)
980 dev_t dev;
981 enum vtype type;
982 struct vnode **vpp;
983{
984 register struct vnode *vp;
985
986 for (vp = speclisth[SPECHASH(dev)]; vp; vp = vp->v_specnext) {
987 if (dev != vp->v_rdev || type != vp->v_type)
988 continue;
989 *vpp = vp;
05378ee4 990 return (1);
2bcd6066 991 }
05378ee4 992 return (0);
2bcd6066
KM
993}
994
ef62830d
KM
995/*
996 * Calculate the total number of references to a special device.
997 */
998vcount(vp)
999 register struct vnode *vp;
1000{
7f7b7d89 1001 register struct vnode *vq;
ef62830d
KM
1002 int count;
1003
1004 if ((vp->v_flag & VALIASED) == 0)
7f7b7d89 1005 return (vp->v_usecount);
ef62830d 1006loop:
7f7b7d89 1007 for (count = 0, vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
de81e10c 1008 if (vq->v_rdev != vp->v_rdev || vq->v_type != vp->v_type)
ef62830d
KM
1009 continue;
1010 /*
1011 * Alias, but not in use, so flush it out.
1012 */
7f7b7d89 1013 if (vq->v_usecount == 0) {
ef62830d
KM
1014 vgone(vq);
1015 goto loop;
1016 }
7f7b7d89 1017 count += vq->v_usecount;
ef62830d
KM
1018 }
1019 return (count);
1020}
0bf84b18
KM
1021
1022/*
1023 * Print out a description of a vnode.
1024 */
1025static char *typename[] =
61f846a8 1026 { "VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD" };
0bf84b18
KM
1027
1028vprint(label, vp)
1029 char *label;
1030 register struct vnode *vp;
1031{
f2f730c6 1032 char buf[64];
0bf84b18
KM
1033
1034 if (label != NULL)
1035 printf("%s: ", label);
3e787e54 1036 printf("num %d ", vp->v_spare[0]);
65c3b3a8
KM
1037 printf("type %s, usecount %d, writecount %d, refcount %d,",
1038 typename[vp->v_type], vp->v_usecount, vp->v_writecount,
1039 vp->v_holdcnt);
f2f730c6
KM
1040 buf[0] = '\0';
1041 if (vp->v_flag & VROOT)
1042 strcat(buf, "|VROOT");
1043 if (vp->v_flag & VTEXT)
1044 strcat(buf, "|VTEXT");
36ef03ec
KM
1045 if (vp->v_flag & VSYSTEM)
1046 strcat(buf, "|VSYSTEM");
36ef03ec
KM
1047 if (vp->v_flag & VXLOCK)
1048 strcat(buf, "|VXLOCK");
1049 if (vp->v_flag & VXWANT)
1050 strcat(buf, "|VXWANT");
f2f730c6
KM
1051 if (vp->v_flag & VBWAIT)
1052 strcat(buf, "|VBWAIT");
36ef03ec
KM
1053 if (vp->v_flag & VALIASED)
1054 strcat(buf, "|VALIASED");
f2f730c6
KM
1055 if (buf[0] != '\0')
1056 printf(" flags (%s)", &buf[1]);
3fc2ac18
KM
1057 if (vp->v_data == NULL) {
1058 printf("\n");
1059 } else {
1060 printf("\n\t");
1061 VOP_PRINT(vp);
1062 }
0bf84b18 1063}
985cbdd5 1064
34c62e18
KM
1065#ifdef DEBUG
1066/*
1067 * List all of the locked vnodes in the system.
1068 * Called when debugging the kernel.
1069 */
1070printlockedvnodes()
1071{
1072 register struct mount *mp;
1073 register struct vnode *vp;
1074
1075 printf("Locked vnodes\n");
3fc2ac18
KM
1076 for (mp = mountlist.tqh_first; mp != NULL; mp = mp->mnt_list.tqe_next) {
1077 for (vp = mp->mnt_vnodelist.lh_first;
1078 vp != NULL;
1079 vp = vp->v_mntvnodes.le_next)
34c62e18
KM
1080 if (VOP_ISLOCKED(vp))
1081 vprint((char *)0, vp);
3fc2ac18 1082 }
34c62e18
KM
1083}
1084#endif
1085
985cbdd5
MT
1086int kinfo_vdebug = 1;
1087int kinfo_vgetfailed;
1088#define KINFO_VNODESLOP 10
1089/*
786fb484 1090 * Dump vnode list (via sysctl).
985cbdd5
MT
1091 * Copyout address of vnode followed by vnode.
1092 */
aacc1bff 1093/* ARGSUSED */
786fb484 1094sysctl_vnode(where, sizep)
985cbdd5 1095 char *where;
c1909da4 1096 size_t *sizep;
985cbdd5 1097{
3fc2ac18 1098 register struct mount *mp, *nmp;
985cbdd5 1099 struct vnode *vp;
985cbdd5 1100 register char *bp = where, *savebp;
5bf57294 1101 char *ewhere;
985cbdd5
MT
1102 int error;
1103
1104#define VPTRSZ sizeof (struct vnode *)
1105#define VNODESZ sizeof (struct vnode)
1106 if (where == NULL) {
786fb484 1107 *sizep = (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ);
985cbdd5
MT
1108 return (0);
1109 }
786fb484 1110 ewhere = where + *sizep;
985cbdd5 1111
3fc2ac18
KM
1112 for (mp = mountlist.tqh_first; mp != NULL; mp = nmp) {
1113 nmp = mp->mnt_list.tqe_next;
1114 if (vfs_busy(mp))
36ef03ec 1115 continue;
985cbdd5
MT
1116 savebp = bp;
1117again:
3fc2ac18
KM
1118 for (vp = mp->mnt_vnodelist.lh_first;
1119 vp != NULL;
1120 vp = vp->v_mntvnodes.le_next) {
41185b3b
KM
1121 /*
1122 * Check that the vp is still associated with
1123 * this filesystem. RACE: could have been
1124 * recycled onto the same filesystem.
1125 */
4597dd33
KM
1126 if (vp->v_mount != mp) {
1127 if (kinfo_vdebug)
1128 printf("kinfo: vp changed\n");
1129 bp = savebp;
1130 goto again;
1131 }
786fb484
KM
1132 if (bp + VPTRSZ + VNODESZ > ewhere) {
1133 *sizep = bp - where;
1134 return (ENOMEM);
1135 }
1136 if ((error = copyout((caddr_t)&vp, bp, VPTRSZ)) ||
1137 (error = copyout((caddr_t)vp, bp + VPTRSZ, VNODESZ)))
985cbdd5 1138 return (error);
985cbdd5 1139 bp += VPTRSZ + VNODESZ;
985cbdd5 1140 }
3fc2ac18
KM
1141 vfs_unbusy(mp);
1142 }
985cbdd5 1143
786fb484 1144 *sizep = bp - where;
985cbdd5
MT
1145 return (0);
1146}
8981e258
MH
1147
1148/*
1149 * Check to see if a filesystem is mounted on a block device.
1150 */
1151int
1152vfs_mountedon(vp)
1153 register struct vnode *vp;
1154{
1155 register struct vnode *vq;
1156
1157 if (vp->v_specflags & SI_MOUNTEDON)
1158 return (EBUSY);
1159 if (vp->v_flag & VALIASED) {
1160 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
1161 if (vq->v_rdev != vp->v_rdev ||
1162 vq->v_type != vp->v_type)
1163 continue;
1164 if (vq->v_specflags & SI_MOUNTEDON)
1165 return (EBUSY);
1166 }
1167 }
1168 return (0);
1169}
1170
1171/*
1172 * Build hash lists of net addresses and hang them off the mount point.
1173 * Called by ufs_mount() to set up the lists of export addresses.
1174 */
1175static int
1176vfs_hang_addrlist(mp, nep, argp)
1177 struct mount *mp;
1178 struct netexport *nep;
1179 struct export_args *argp;
1180{
1181 register struct netcred *np;
1182 register struct radix_node_head *rnh;
1183 register int i;
1184 struct radix_node *rn;
1185 struct sockaddr *saddr, *smask = 0;
1186 struct domain *dom;
1187 int error;
1188
1189 if (argp->ex_addrlen == 0) {
1190 if (mp->mnt_flag & MNT_DEFEXPORTED)
1191 return (EPERM);
1192 np = &nep->ne_defexported;
1193 np->netc_exflags = argp->ex_flags;
1194 np->netc_anon = argp->ex_anon;
1195 np->netc_anon.cr_ref = 1;
1196 mp->mnt_flag |= MNT_DEFEXPORTED;
1197 return (0);
1198 }
1199 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
1200 np = (struct netcred *)malloc(i, M_NETADDR, M_WAITOK);
1201 bzero((caddr_t)np, i);
1202 saddr = (struct sockaddr *)(np + 1);
1203 if (error = copyin(argp->ex_addr, (caddr_t)saddr, argp->ex_addrlen))
1204 goto out;
1205 if (saddr->sa_len > argp->ex_addrlen)
1206 saddr->sa_len = argp->ex_addrlen;
1207 if (argp->ex_masklen) {
1208 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
1209 error = copyin(argp->ex_addr, (caddr_t)smask, argp->ex_masklen);
1210 if (error)
1211 goto out;
1212 if (smask->sa_len > argp->ex_masklen)
1213 smask->sa_len = argp->ex_masklen;
1214 }
1215 i = saddr->sa_family;
1216 if ((rnh = nep->ne_rtable[i]) == 0) {
1217 /*
1218 * Seems silly to initialize every AF when most are not
1219 * used, do so on demand here
1220 */
1221 for (dom = domains; dom; dom = dom->dom_next)
1222 if (dom->dom_family == i && dom->dom_rtattach) {
1223 dom->dom_rtattach((void **)&nep->ne_rtable[i],
1224 dom->dom_rtoffset);
1225 break;
1226 }
1227 if ((rnh = nep->ne_rtable[i]) == 0) {
1228 error = ENOBUFS;
1229 goto out;
1230 }
1231 }
1232 rn = (*rnh->rnh_addaddr)((caddr_t)saddr, (caddr_t)smask, rnh,
1233 np->netc_rnodes);
1234 if (rn == 0 || np != (struct netcred *)rn) { /* already exists */
1235 error = EPERM;
1236 goto out;
1237 }
1238 np->netc_exflags = argp->ex_flags;
1239 np->netc_anon = argp->ex_anon;
1240 np->netc_anon.cr_ref = 1;
1241 return (0);
1242out:
1243 free(np, M_NETADDR);
1244 return (error);
1245}
1246
1247/* ARGSUSED */
1248static int
1249vfs_free_netcred(rn, w)
1250 struct radix_node *rn;
1251 caddr_t w;
1252{
1253 register struct radix_node_head *rnh = (struct radix_node_head *)w;
1254
1255 (*rnh->rnh_deladdr)(rn->rn_key, rn->rn_mask, rnh);
1256 free((caddr_t)rn, M_NETADDR);
1257 return (0);
1258}
1259
1260/*
1261 * Free the net address hash lists that are hanging off the mount points.
1262 */
1263static void
1264vfs_free_addrlist(nep)
1265 struct netexport *nep;
1266{
1267 register int i;
1268 register struct radix_node_head *rnh;
1269
1270 for (i = 0; i <= AF_MAX; i++)
1271 if (rnh = nep->ne_rtable[i]) {
1272 (*rnh->rnh_walktree)(rnh, vfs_free_netcred,
1273 (caddr_t)rnh);
1274 free((caddr_t)rnh, M_RTABLE);
1275 nep->ne_rtable[i] = 0;
1276 }
1277}
1278
1279int
1280vfs_export(mp, nep, argp)
1281 struct mount *mp;
1282 struct netexport *nep;
1283 struct export_args *argp;
1284{
1285 int error;
1286
1287 if (argp->ex_flags & MNT_DELEXPORT) {
1288 vfs_free_addrlist(nep);
1289 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
1290 }
1291 if (argp->ex_flags & MNT_EXPORTED) {
1292 if (error = vfs_hang_addrlist(mp, nep, argp))
1293 return (error);
1294 mp->mnt_flag |= MNT_EXPORTED;
1295 }
1296 return (0);
1297}
1298
1299struct netcred *
1300vfs_export_lookup(mp, nep, nam)
1301 register struct mount *mp;
1302 struct netexport *nep;
1303 struct mbuf *nam;
1304{
1305 register struct netcred *np;
1306 register struct radix_node_head *rnh;
1307 struct sockaddr *saddr;
1308
1309 np = NULL;
1310 if (mp->mnt_flag & MNT_EXPORTED) {
1311 /*
1312 * Lookup in the export list first.
1313 */
1314 if (nam != NULL) {
1315 saddr = mtod(nam, struct sockaddr *);
1316 rnh = nep->ne_rtable[saddr->sa_family];
1317 if (rnh != NULL) {
1318 np = (struct netcred *)
1319 (*rnh->rnh_matchaddr)((caddr_t)saddr,
1320 rnh);
1321 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
1322 np = NULL;
1323 }
1324 }
1325 /*
1326 * If no address match, use the default if it exists.
1327 */
1328 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
1329 np = &nep->ne_defexported;
1330 }
1331 return (np);
1332}