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