allocate segment tables out of a private submap that doesn't
[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 *
adb35f79 12 * @(#)vfs_subr.c 8.9 (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);
3e787e54
KM
593 vp->v_freelist.tqe_next = (struct vnode *)0xdeadf;
594 vp->v_freelist.tqe_prev = (struct vnode **)0xdeadb;
595 }
ec04fc59 596 vp->v_usecount++;
3fc2ac18
KM
597 if (lockflag)
598 VOP_LOCK(vp);
3e787e54 599 if (printcnt-- > 0) vprint("vget got", vp);
ef24f6dd 600 return (0);
36d09cb1
KM
601}
602
d32390ea
KM
603int bug_refs = 0;
604
36d09cb1
KM
605/*
606 * Vnode reference, just increment the count
607 */
608void vref(vp)
609 struct vnode *vp;
610{
611
ec04fc59
KM
612 if (vp->v_usecount <= 0)
613 panic("vref used where vget required");
3e787e54
KM
614 if (vp->v_freelist.tqe_next != (struct vnode *)0xdeadf ||
615 vp->v_freelist.tqe_prev != (struct vnode **)0xdeadb)
616 panic("vref: not free");
7f7b7d89 617 vp->v_usecount++;
3e787e54 618 if (printcnt-- > 0) vprint("vref get", vp);
d32390ea
KM
619 if (vp->v_type != VBLK && curproc)
620 curproc->p_spare[0]++;
621 if (bug_refs)
622 vprint("vref: ");
36d09cb1
KM
623}
624
625/*
626 * vput(), just unlock and vrele()
627 */
628void vput(vp)
629 register struct vnode *vp;
630{
4d1ee2eb 631
36d09cb1
KM
632 VOP_UNLOCK(vp);
633 vrele(vp);
634}
635
636/*
637 * Vnode release.
638 * If count drops to zero, call inactive routine and return to freelist.
639 */
640void vrele(vp)
641 register struct vnode *vp;
642{
643
65c3b3a8 644#ifdef DIAGNOSTIC
36d09cb1 645 if (vp == NULL)
ef24f6dd 646 panic("vrele: null vp");
65c3b3a8 647#endif
7f7b7d89 648 vp->v_usecount--;
3e787e54 649 if (printcnt-- > 0) vprint("vrele put", vp);
d32390ea
KM
650 if (vp->v_type != VBLK && curproc)
651 curproc->p_spare[0]--;
652 if (bug_refs)
653 vprint("vref: ");
7f7b7d89 654 if (vp->v_usecount > 0)
36d09cb1 655 return;
65c3b3a8
KM
656#ifdef DIAGNOSTIC
657 if (vp->v_usecount != 0 || vp->v_writecount != 0) {
658 vprint("vrele: bad ref count", vp);
659 panic("vrele: ref cnt");
660 }
661#endif
dc998e72
KM
662 /*
663 * insert at tail of LRU list
664 */
3e787e54
KM
665 if (vp->v_freelist.tqe_next != (struct vnode *)0xdeadf ||
666 vp->v_freelist.tqe_prev != (struct vnode **)0xdeadb)
667 panic("vrele: not free");
3fc2ac18 668 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
d024c2ce 669 VOP_INACTIVE(vp);
ef24f6dd
KM
670}
671
7f7b7d89
KM
672/*
673 * Page or buffer structure gets a reference.
674 */
451df175 675void vhold(vp)
7f7b7d89
KM
676 register struct vnode *vp;
677{
678
679 vp->v_holdcnt++;
680}
681
682/*
683 * Page or buffer structure frees a reference.
684 */
451df175 685void holdrele(vp)
7f7b7d89
KM
686 register struct vnode *vp;
687{
688
689 if (vp->v_holdcnt <= 0)
690 panic("holdrele: holdcnt");
691 vp->v_holdcnt--;
692}
693
f0556f86
KM
694/*
695 * Remove any vnodes in the vnode table belonging to mount point mp.
696 *
697 * If MNT_NOFORCE is specified, there should not be any active ones,
698 * return error if any are found (nb: this is a user error, not a
699 * system error). If MNT_FORCE is specified, detach any active vnodes
700 * that are found.
701 */
8981e258 702#ifdef DIAGNOSTIC
bb4964fd
KM
703int busyprt = 0; /* print out busy vnodes */
704struct ctldebug debug1 = { "busyprt", &busyprt };
8981e258 705#endif
f0556f86
KM
706
707vflush(mp, skipvp, flags)
708 struct mount *mp;
709 struct vnode *skipvp;
710 int flags;
711{
712 register struct vnode *vp, *nvp;
713 int busy = 0;
714
54fb9dc2 715 if ((mp->mnt_flag & MNT_MPBUSY) == 0)
36ef03ec 716 panic("vflush: not busy");
4597dd33 717loop:
3fc2ac18 718 for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) {
4597dd33
KM
719 if (vp->v_mount != mp)
720 goto loop;
3fc2ac18 721 nvp = vp->v_mntvnodes.le_next;
f0556f86
KM
722 /*
723 * Skip over a selected vnode.
f0556f86
KM
724 */
725 if (vp == skipvp)
726 continue;
36ef03ec
KM
727 /*
728 * Skip over a vnodes marked VSYSTEM.
729 */
730 if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM))
731 continue;
da374605
KM
732 /*
733 * If WRITECLOSE is set, only flush out regular file
734 * vnodes open for writing.
735 */
736 if ((flags & WRITECLOSE) &&
737 (vp->v_writecount == 0 || vp->v_type != VREG))
738 continue;
f0556f86 739 /*
7f7b7d89 740 * With v_usecount == 0, all we need to do is clear
f0556f86
KM
741 * out the vnode data structures and we are done.
742 */
7f7b7d89 743 if (vp->v_usecount == 0) {
f0556f86
KM
744 vgone(vp);
745 continue;
746 }
747 /*
da374605 748 * If FORCECLOSE is set, forcibly close the vnode.
f0556f86
KM
749 * For block or character devices, revert to an
750 * anonymous device. For all other files, just kill them.
751 */
36ef03ec 752 if (flags & FORCECLOSE) {
f0556f86
KM
753 if (vp->v_type != VBLK && vp->v_type != VCHR) {
754 vgone(vp);
755 } else {
756 vclean(vp, 0);
9342689a 757 vp->v_op = spec_vnodeop_p;
f0556f86
KM
758 insmntque(vp, (struct mount *)0);
759 }
760 continue;
761 }
8981e258 762#ifdef DIAGNOSTIC
f0556f86 763 if (busyprt)
0bf84b18 764 vprint("vflush: busy vnode", vp);
8981e258 765#endif
f0556f86
KM
766 busy++;
767 }
768 if (busy)
769 return (EBUSY);
770 return (0);
771}
772
ef24f6dd
KM
773/*
774 * Disassociate the underlying file system from a vnode.
ef24f6dd 775 */
ecf75a7d
KM
776void
777vclean(vp, flags)
ef24f6dd 778 register struct vnode *vp;
aacc1bff 779 int flags;
ef24f6dd 780{
2bae1875 781 int active;
ef24f6dd 782
2bae1875
KM
783 /*
784 * Check to see if the vnode is in use.
0bf84b18
KM
785 * If so we have to reference it before we clean it out
786 * so that its count cannot fall to zero and generate a
787 * race against ourselves to recycle it.
2bae1875 788 */
7f7b7d89 789 if (active = vp->v_usecount)
2bae1875 790 VREF(vp);
669df1aa
KM
791 /*
792 * Even if the count is zero, the VOP_INACTIVE routine may still
793 * have the object locked while it cleans it out. The VOP_LOCK
794 * ensures that the VOP_INACTIVE routine is done with its work.
795 * For active vnodes, it ensures that no other activity can
796 * occur while the underlying object is being cleaned out.
797 */
798 VOP_LOCK(vp);
2bae1875
KM
799 /*
800 * Prevent the vnode from being recycled or
801 * brought into use while we clean it out.
802 */
0bf84b18
KM
803 if (vp->v_flag & VXLOCK)
804 panic("vclean: deadlock");
ef24f6dd 805 vp->v_flag |= VXLOCK;
0bf84b18 806 /*
669df1aa 807 * Clean out any buffers associated with the vnode.
0bf84b18 808 */
36ef03ec 809 if (flags & DOCLOSE)
c33e9e8b 810 vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
ef24f6dd 811 /*
669df1aa
KM
812 * Any other processes trying to obtain this lock must first
813 * wait for VXLOCK to clear, then call the new lock operation.
ef24f6dd 814 */
669df1aa 815 VOP_UNLOCK(vp);
ef24f6dd 816 /*
669df1aa
KM
817 * If purging an active vnode, it must be closed and
818 * deactivated before being reclaimed.
ef24f6dd 819 */
2bae1875 820 if (active) {
669df1aa
KM
821 if (flags & DOCLOSE)
822 VOP_CLOSE(vp, IO_NDELAY, NOCRED, NULL);
823 VOP_INACTIVE(vp);
ef24f6dd
KM
824 }
825 /*
826 * Reclaim the vnode.
827 */
669df1aa 828 if (VOP_RECLAIM(vp))
ef24f6dd 829 panic("vclean: cannot reclaim");
2bae1875
KM
830 if (active)
831 vrele(vp);
38c46eee 832
ef24f6dd 833 /*
669df1aa 834 * Done with purge, notify sleepers of the grim news.
ef24f6dd 835 */
669df1aa
KM
836 vp->v_op = dead_vnodeop_p;
837 vp->v_tag = VT_NON;
ef24f6dd
KM
838 vp->v_flag &= ~VXLOCK;
839 if (vp->v_flag & VXWANT) {
840 vp->v_flag &= ~VXWANT;
841 wakeup((caddr_t)vp);
842 }
843}
844
ef62830d
KM
845/*
846 * Eliminate all activity associated with the requested vnode
847 * and with all vnodes aliased to the requested vnode.
848 */
849void vgoneall(vp)
850 register struct vnode *vp;
851{
7f7b7d89 852 register struct vnode *vq;
ef62830d 853
7a7b3a95
KM
854 if (vp->v_flag & VALIASED) {
855 /*
856 * If a vgone (or vclean) is already in progress,
857 * wait until it is done and return.
858 */
859 if (vp->v_flag & VXLOCK) {
860 vp->v_flag |= VXWANT;
861 sleep((caddr_t)vp, PINOD);
862 return;
863 }
864 /*
865 * Ensure that vp will not be vgone'd while we
866 * are eliminating its aliases.
867 */
868 vp->v_flag |= VXLOCK;
869 while (vp->v_flag & VALIASED) {
870 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
871 if (vq->v_rdev != vp->v_rdev ||
872 vq->v_type != vp->v_type || vp == vq)
873 continue;
874 vgone(vq);
875 break;
876 }
ef62830d 877 }
7a7b3a95
KM
878 /*
879 * Remove the lock so that vgone below will
880 * really eliminate the vnode after which time
881 * vgone will awaken any sleepers.
882 */
883 vp->v_flag &= ~VXLOCK;
ef62830d
KM
884 }
885 vgone(vp);
886}
887
ef24f6dd
KM
888/*
889 * Eliminate all activity associated with a vnode
890 * in preparation for reuse.
891 */
892void vgone(vp)
893 register struct vnode *vp;
894{
7f7b7d89 895 register struct vnode *vq;
c0de8792 896 struct vnode *vx;
ef24f6dd 897
4f55e3ec
KM
898 /*
899 * If a vgone (or vclean) is already in progress,
900 * wait until it is done and return.
901 */
902 if (vp->v_flag & VXLOCK) {
903 vp->v_flag |= VXWANT;
904 sleep((caddr_t)vp, PINOD);
905 return;
906 }
ef24f6dd
KM
907 /*
908 * Clean out the filesystem specific data.
909 */
36ef03ec 910 vclean(vp, DOCLOSE);
ef24f6dd
KM
911 /*
912 * Delete from old mount point vnode list, if on one.
913 */
3fc2ac18 914 if (vp->v_mount != NULL) {
3e787e54
KM
915 if (vp->v_mntvnodes.le_next == (struct vnode *)0xdeadf ||
916 vp->v_mntvnodes.le_prev == (struct vnode **)0xdeadb)
917 panic("vgone: not on queue");
3fc2ac18 918 LIST_REMOVE(vp, v_mntvnodes);
3e787e54
KM
919 vp->v_mntvnodes.le_next = (struct vnode *)0xdeadf;
920 vp->v_mntvnodes.le_prev = (struct vnode **)0xdeadb;
d10e9258 921 vp->v_mount = NULL;
ef24f6dd
KM
922 }
923 /*
924 * If special device, remove it from special device alias list.
925 */
926 if (vp->v_type == VBLK || vp->v_type == VCHR) {
7f7b7d89
KM
927 if (*vp->v_hashchain == vp) {
928 *vp->v_hashchain = vp->v_specnext;
ef24f6dd 929 } else {
7f7b7d89 930 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
c0de8792 931 if (vq->v_specnext != vp)
ef24f6dd 932 continue;
c0de8792 933 vq->v_specnext = vp->v_specnext;
ef24f6dd
KM
934 break;
935 }
c0de8792 936 if (vq == NULL)
ef24f6dd
KM
937 panic("missing bdev");
938 }
c0de8792 939 if (vp->v_flag & VALIASED) {
4d1ee2eb 940 vx = NULL;
7f7b7d89 941 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
de81e10c
KM
942 if (vq->v_rdev != vp->v_rdev ||
943 vq->v_type != vp->v_type)
c0de8792 944 continue;
4d1ee2eb
CT
945 if (vx)
946 break;
c0de8792
KM
947 vx = vq;
948 }
4d1ee2eb 949 if (vx == NULL)
c0de8792 950 panic("missing alias");
4d1ee2eb 951 if (vq == NULL)
c0de8792
KM
952 vx->v_flag &= ~VALIASED;
953 vp->v_flag &= ~VALIASED;
954 }
955 FREE(vp->v_specinfo, M_VNODE);
956 vp->v_specinfo = NULL;
ef24f6dd
KM
957 }
958 /*
3387ef89 959 * If it is on the freelist and not already at the head,
0bf9bb76
KM
960 * move it to the head of the list. The test of the back
961 * pointer and the reference count of zero is because
962 * it will be removed from the free list by getnewvnode,
963 * but will not have its reference count incremented until
964 * after calling vgone. If the reference count were
965 * incremented first, vgone would (incorrectly) try to
966 * close the previous instance of the underlying object.
967 * So, the back pointer is explicitly set to `0xdeadb' in
968 * getnewvnode after removing it from the freelist to ensure
969 * that we do not try to move it here.
ef24f6dd 970 */
0bf9bb76
KM
971 if (vp->v_usecount == 0 &&
972 vp->v_freelist.tqe_prev != (struct vnode **)0xdeadb &&
973 vnode_free_list.tqh_first != vp) {
3e787e54
KM
974 if (vp->v_freelist.tqe_next == (struct vnode *)0xdeadf)
975 panic("vgone: use 0, not free");
3fc2ac18
KM
976 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
977 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
ef24f6dd 978 }
2bae1875 979 vp->v_type = VBAD;
36d09cb1 980}
ef62830d 981
2bcd6066
KM
982/*
983 * Lookup a vnode by device number.
984 */
985vfinddev(dev, type, vpp)
986 dev_t dev;
987 enum vtype type;
988 struct vnode **vpp;
989{
990 register struct vnode *vp;
991
992 for (vp = speclisth[SPECHASH(dev)]; vp; vp = vp->v_specnext) {
993 if (dev != vp->v_rdev || type != vp->v_type)
994 continue;
995 *vpp = vp;
05378ee4 996 return (1);
2bcd6066 997 }
05378ee4 998 return (0);
2bcd6066
KM
999}
1000
ef62830d
KM
1001/*
1002 * Calculate the total number of references to a special device.
1003 */
1004vcount(vp)
1005 register struct vnode *vp;
1006{
7f7b7d89 1007 register struct vnode *vq;
ef62830d
KM
1008 int count;
1009
1010 if ((vp->v_flag & VALIASED) == 0)
7f7b7d89 1011 return (vp->v_usecount);
ef62830d 1012loop:
7f7b7d89 1013 for (count = 0, vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
de81e10c 1014 if (vq->v_rdev != vp->v_rdev || vq->v_type != vp->v_type)
ef62830d
KM
1015 continue;
1016 /*
1017 * Alias, but not in use, so flush it out.
1018 */
7f7b7d89 1019 if (vq->v_usecount == 0) {
ef62830d
KM
1020 vgone(vq);
1021 goto loop;
1022 }
7f7b7d89 1023 count += vq->v_usecount;
ef62830d
KM
1024 }
1025 return (count);
1026}
0bf84b18
KM
1027
1028/*
1029 * Print out a description of a vnode.
1030 */
1031static char *typename[] =
61f846a8 1032 { "VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD" };
0bf84b18
KM
1033
1034vprint(label, vp)
1035 char *label;
1036 register struct vnode *vp;
1037{
f2f730c6 1038 char buf[64];
0bf84b18
KM
1039
1040 if (label != NULL)
1041 printf("%s: ", label);
3e787e54 1042 printf("num %d ", vp->v_spare[0]);
65c3b3a8
KM
1043 printf("type %s, usecount %d, writecount %d, refcount %d,",
1044 typename[vp->v_type], vp->v_usecount, vp->v_writecount,
1045 vp->v_holdcnt);
f2f730c6
KM
1046 buf[0] = '\0';
1047 if (vp->v_flag & VROOT)
1048 strcat(buf, "|VROOT");
1049 if (vp->v_flag & VTEXT)
1050 strcat(buf, "|VTEXT");
36ef03ec
KM
1051 if (vp->v_flag & VSYSTEM)
1052 strcat(buf, "|VSYSTEM");
36ef03ec
KM
1053 if (vp->v_flag & VXLOCK)
1054 strcat(buf, "|VXLOCK");
1055 if (vp->v_flag & VXWANT)
1056 strcat(buf, "|VXWANT");
f2f730c6
KM
1057 if (vp->v_flag & VBWAIT)
1058 strcat(buf, "|VBWAIT");
36ef03ec
KM
1059 if (vp->v_flag & VALIASED)
1060 strcat(buf, "|VALIASED");
f2f730c6
KM
1061 if (buf[0] != '\0')
1062 printf(" flags (%s)", &buf[1]);
3fc2ac18
KM
1063 if (vp->v_data == NULL) {
1064 printf("\n");
1065 } else {
1066 printf("\n\t");
1067 VOP_PRINT(vp);
1068 }
0bf84b18 1069}
985cbdd5 1070
34c62e18
KM
1071#ifdef DEBUG
1072/*
1073 * List all of the locked vnodes in the system.
1074 * Called when debugging the kernel.
1075 */
1076printlockedvnodes()
1077{
1078 register struct mount *mp;
1079 register struct vnode *vp;
1080
1081 printf("Locked vnodes\n");
3fc2ac18
KM
1082 for (mp = mountlist.tqh_first; mp != NULL; mp = mp->mnt_list.tqe_next) {
1083 for (vp = mp->mnt_vnodelist.lh_first;
1084 vp != NULL;
1085 vp = vp->v_mntvnodes.le_next)
34c62e18
KM
1086 if (VOP_ISLOCKED(vp))
1087 vprint((char *)0, vp);
3fc2ac18 1088 }
34c62e18
KM
1089}
1090#endif
1091
985cbdd5
MT
1092int kinfo_vdebug = 1;
1093int kinfo_vgetfailed;
1094#define KINFO_VNODESLOP 10
1095/*
786fb484 1096 * Dump vnode list (via sysctl).
985cbdd5
MT
1097 * Copyout address of vnode followed by vnode.
1098 */
aacc1bff 1099/* ARGSUSED */
786fb484 1100sysctl_vnode(where, sizep)
985cbdd5 1101 char *where;
c1909da4 1102 size_t *sizep;
985cbdd5 1103{
3fc2ac18 1104 register struct mount *mp, *nmp;
985cbdd5 1105 struct vnode *vp;
985cbdd5 1106 register char *bp = where, *savebp;
5bf57294 1107 char *ewhere;
985cbdd5
MT
1108 int error;
1109
1110#define VPTRSZ sizeof (struct vnode *)
1111#define VNODESZ sizeof (struct vnode)
1112 if (where == NULL) {
786fb484 1113 *sizep = (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ);
985cbdd5
MT
1114 return (0);
1115 }
786fb484 1116 ewhere = where + *sizep;
985cbdd5 1117
3fc2ac18
KM
1118 for (mp = mountlist.tqh_first; mp != NULL; mp = nmp) {
1119 nmp = mp->mnt_list.tqe_next;
1120 if (vfs_busy(mp))
36ef03ec 1121 continue;
985cbdd5
MT
1122 savebp = bp;
1123again:
3fc2ac18
KM
1124 for (vp = mp->mnt_vnodelist.lh_first;
1125 vp != NULL;
1126 vp = vp->v_mntvnodes.le_next) {
41185b3b
KM
1127 /*
1128 * Check that the vp is still associated with
1129 * this filesystem. RACE: could have been
1130 * recycled onto the same filesystem.
1131 */
4597dd33
KM
1132 if (vp->v_mount != mp) {
1133 if (kinfo_vdebug)
1134 printf("kinfo: vp changed\n");
1135 bp = savebp;
1136 goto again;
1137 }
786fb484
KM
1138 if (bp + VPTRSZ + VNODESZ > ewhere) {
1139 *sizep = bp - where;
1140 return (ENOMEM);
1141 }
1142 if ((error = copyout((caddr_t)&vp, bp, VPTRSZ)) ||
1143 (error = copyout((caddr_t)vp, bp + VPTRSZ, VNODESZ)))
985cbdd5 1144 return (error);
985cbdd5 1145 bp += VPTRSZ + VNODESZ;
985cbdd5 1146 }
3fc2ac18
KM
1147 vfs_unbusy(mp);
1148 }
985cbdd5 1149
786fb484 1150 *sizep = bp - where;
985cbdd5
MT
1151 return (0);
1152}
8981e258
MH
1153
1154/*
1155 * Check to see if a filesystem is mounted on a block device.
1156 */
1157int
1158vfs_mountedon(vp)
1159 register struct vnode *vp;
1160{
1161 register struct vnode *vq;
1162
1163 if (vp->v_specflags & SI_MOUNTEDON)
1164 return (EBUSY);
1165 if (vp->v_flag & VALIASED) {
1166 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
1167 if (vq->v_rdev != vp->v_rdev ||
1168 vq->v_type != vp->v_type)
1169 continue;
1170 if (vq->v_specflags & SI_MOUNTEDON)
1171 return (EBUSY);
1172 }
1173 }
1174 return (0);
1175}
1176
1177/*
1178 * Build hash lists of net addresses and hang them off the mount point.
1179 * Called by ufs_mount() to set up the lists of export addresses.
1180 */
1181static int
1182vfs_hang_addrlist(mp, nep, argp)
1183 struct mount *mp;
1184 struct netexport *nep;
1185 struct export_args *argp;
1186{
1187 register struct netcred *np;
1188 register struct radix_node_head *rnh;
1189 register int i;
1190 struct radix_node *rn;
1191 struct sockaddr *saddr, *smask = 0;
1192 struct domain *dom;
1193 int error;
1194
1195 if (argp->ex_addrlen == 0) {
1196 if (mp->mnt_flag & MNT_DEFEXPORTED)
1197 return (EPERM);
1198 np = &nep->ne_defexported;
1199 np->netc_exflags = argp->ex_flags;
1200 np->netc_anon = argp->ex_anon;
1201 np->netc_anon.cr_ref = 1;
1202 mp->mnt_flag |= MNT_DEFEXPORTED;
1203 return (0);
1204 }
1205 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
1206 np = (struct netcred *)malloc(i, M_NETADDR, M_WAITOK);
1207 bzero((caddr_t)np, i);
1208 saddr = (struct sockaddr *)(np + 1);
1209 if (error = copyin(argp->ex_addr, (caddr_t)saddr, argp->ex_addrlen))
1210 goto out;
1211 if (saddr->sa_len > argp->ex_addrlen)
1212 saddr->sa_len = argp->ex_addrlen;
1213 if (argp->ex_masklen) {
1214 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
1215 error = copyin(argp->ex_addr, (caddr_t)smask, argp->ex_masklen);
1216 if (error)
1217 goto out;
1218 if (smask->sa_len > argp->ex_masklen)
1219 smask->sa_len = argp->ex_masklen;
1220 }
1221 i = saddr->sa_family;
1222 if ((rnh = nep->ne_rtable[i]) == 0) {
1223 /*
1224 * Seems silly to initialize every AF when most are not
1225 * used, do so on demand here
1226 */
1227 for (dom = domains; dom; dom = dom->dom_next)
1228 if (dom->dom_family == i && dom->dom_rtattach) {
1229 dom->dom_rtattach((void **)&nep->ne_rtable[i],
1230 dom->dom_rtoffset);
1231 break;
1232 }
1233 if ((rnh = nep->ne_rtable[i]) == 0) {
1234 error = ENOBUFS;
1235 goto out;
1236 }
1237 }
1238 rn = (*rnh->rnh_addaddr)((caddr_t)saddr, (caddr_t)smask, rnh,
1239 np->netc_rnodes);
1240 if (rn == 0 || np != (struct netcred *)rn) { /* already exists */
1241 error = EPERM;
1242 goto out;
1243 }
1244 np->netc_exflags = argp->ex_flags;
1245 np->netc_anon = argp->ex_anon;
1246 np->netc_anon.cr_ref = 1;
1247 return (0);
1248out:
1249 free(np, M_NETADDR);
1250 return (error);
1251}
1252
1253/* ARGSUSED */
1254static int
1255vfs_free_netcred(rn, w)
1256 struct radix_node *rn;
1257 caddr_t w;
1258{
1259 register struct radix_node_head *rnh = (struct radix_node_head *)w;
1260
1261 (*rnh->rnh_deladdr)(rn->rn_key, rn->rn_mask, rnh);
1262 free((caddr_t)rn, M_NETADDR);
1263 return (0);
1264}
1265
1266/*
1267 * Free the net address hash lists that are hanging off the mount points.
1268 */
1269static void
1270vfs_free_addrlist(nep)
1271 struct netexport *nep;
1272{
1273 register int i;
1274 register struct radix_node_head *rnh;
1275
1276 for (i = 0; i <= AF_MAX; i++)
1277 if (rnh = nep->ne_rtable[i]) {
1278 (*rnh->rnh_walktree)(rnh, vfs_free_netcred,
1279 (caddr_t)rnh);
1280 free((caddr_t)rnh, M_RTABLE);
1281 nep->ne_rtable[i] = 0;
1282 }
1283}
1284
1285int
1286vfs_export(mp, nep, argp)
1287 struct mount *mp;
1288 struct netexport *nep;
1289 struct export_args *argp;
1290{
1291 int error;
1292
1293 if (argp->ex_flags & MNT_DELEXPORT) {
1294 vfs_free_addrlist(nep);
1295 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
1296 }
1297 if (argp->ex_flags & MNT_EXPORTED) {
1298 if (error = vfs_hang_addrlist(mp, nep, argp))
1299 return (error);
1300 mp->mnt_flag |= MNT_EXPORTED;
1301 }
1302 return (0);
1303}
1304
1305struct netcred *
1306vfs_export_lookup(mp, nep, nam)
1307 register struct mount *mp;
1308 struct netexport *nep;
1309 struct mbuf *nam;
1310{
1311 register struct netcred *np;
1312 register struct radix_node_head *rnh;
1313 struct sockaddr *saddr;
1314
1315 np = NULL;
1316 if (mp->mnt_flag & MNT_EXPORTED) {
1317 /*
1318 * Lookup in the export list first.
1319 */
1320 if (nam != NULL) {
1321 saddr = mtod(nam, struct sockaddr *);
1322 rnh = nep->ne_rtable[saddr->sa_family];
1323 if (rnh != NULL) {
1324 np = (struct netcred *)
1325 (*rnh->rnh_matchaddr)((caddr_t)saddr,
1326 rnh);
1327 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
1328 np = NULL;
1329 }
1330 }
1331 /*
1332 * If no address match, use the default if it exists.
1333 */
1334 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
1335 np = &nep->ne_defexported;
1336 }
1337 return (np);
1338}