No empty .Rs/.Re
[netbsd-mini2440.git] / sys / kern / vfs_cwd.c
blobcafd9fa1d4e43d1f34b169de569d91392aec15f1
1 /* $NetBSD: vfs_cwd.c,v 1.2 2009/09/24 06:14:22 yamt Exp $ */
3 /*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
30 * Current working directory.
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: vfs_cwd.c,v 1.2 2009/09/24 06:14:22 yamt Exp $");
36 #include <sys/param.h>
37 #include <sys/atomic.h>
38 #include <sys/filedesc.h>
39 #include <sys/proc.h>
40 #include <sys/vnode.h>
42 static int cwdi_ctor(void *, void *, int);
43 static void cwdi_dtor(void *, void *);
45 static pool_cache_t cwdi_cache;
47 void
48 cwd_sys_init(void)
51 cwdi_cache = pool_cache_init(sizeof(struct cwdinfo), coherency_unit,
52 0, 0, "cwdi", NULL, IPL_NONE, cwdi_ctor, cwdi_dtor, NULL);
53 KASSERT(cwdi_cache != NULL);
57 * Create an initial cwdinfo structure, using the same current and root
58 * directories as curproc.
60 struct cwdinfo *
61 cwdinit(void)
63 struct cwdinfo *cwdi;
64 struct cwdinfo *copy;
66 cwdi = pool_cache_get(cwdi_cache, PR_WAITOK);
67 copy = curproc->p_cwdi;
69 rw_enter(&copy->cwdi_lock, RW_READER);
70 cwdi->cwdi_cdir = copy->cwdi_cdir;
71 if (cwdi->cwdi_cdir)
72 vref(cwdi->cwdi_cdir);
73 cwdi->cwdi_rdir = copy->cwdi_rdir;
74 if (cwdi->cwdi_rdir)
75 vref(cwdi->cwdi_rdir);
76 cwdi->cwdi_edir = copy->cwdi_edir;
77 if (cwdi->cwdi_edir)
78 vref(cwdi->cwdi_edir);
79 cwdi->cwdi_cmask = copy->cwdi_cmask;
80 cwdi->cwdi_refcnt = 1;
81 rw_exit(&copy->cwdi_lock);
83 return (cwdi);
86 static int
87 cwdi_ctor(void *arg, void *obj, int flags)
89 struct cwdinfo *cwdi = obj;
91 rw_init(&cwdi->cwdi_lock);
93 return 0;
96 static void
97 cwdi_dtor(void *arg, void *obj)
99 struct cwdinfo *cwdi = obj;
101 rw_destroy(&cwdi->cwdi_lock);
105 * Make p2 share p1's cwdinfo.
107 void
108 cwdshare(struct proc *p2)
110 struct cwdinfo *cwdi;
112 cwdi = curproc->p_cwdi;
114 atomic_inc_uint(&cwdi->cwdi_refcnt);
115 p2->p_cwdi = cwdi;
119 * Make sure proc has only one reference to its cwdi, creating
120 * a new one if necessary.
122 void
123 cwdunshare(struct proc *p)
125 struct cwdinfo *cwdi = p->p_cwdi;
127 if (cwdi->cwdi_refcnt > 1) {
128 cwdi = cwdinit();
129 cwdfree(p->p_cwdi);
130 p->p_cwdi = cwdi;
135 * Release a cwdinfo structure.
137 void
138 cwdfree(struct cwdinfo *cwdi)
141 if (atomic_dec_uint_nv(&cwdi->cwdi_refcnt) > 0)
142 return;
144 vrele(cwdi->cwdi_cdir);
145 if (cwdi->cwdi_rdir)
146 vrele(cwdi->cwdi_rdir);
147 if (cwdi->cwdi_edir)
148 vrele(cwdi->cwdi_edir);
149 pool_cache_put(cwdi_cache, cwdi);