1/* $NetBSD: vfs_cwd.c,v 1.4 2011/02/15 15:54:28 pooka Exp $ */
2
3/*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
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.
15 *
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.
27 */
28
29/*
30 * Current working directory.
31 */
32
33#include <sys/cdefs.h>
34__KERNEL_RCSID(0, "$NetBSD: vfs_cwd.c,v 1.4 2011/02/15 15:54:28 pooka Exp $");
35
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>
41
42static int cwdi_ctor(void *, void *, int);
43static void cwdi_dtor(void *, void *);
44
45static pool_cache_t cwdi_cache;
46
47void
48cwd_sys_init(void)
49{
50
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);
54}
55
56/*
57 * Create an initial cwdinfo structure, using the same current and root
58 * directories as curproc.
59 */
60struct cwdinfo *
61cwdinit(void)
62{
63 struct cwdinfo *cwdi;
64 struct cwdinfo *copy;
65
66 cwdi = pool_cache_get(cwdi_cache, PR_WAITOK);
67 copy = curproc->p_cwdi;
68
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);
82
83 return (cwdi);
84}
85
86static int
87cwdi_ctor(void *arg, void *obj, int flags)
88{
89 struct cwdinfo *cwdi = obj;
90
91 rw_init(&cwdi->cwdi_lock);
92
93 return 0;
94}
95
96static void
97cwdi_dtor(void *arg, void *obj)
98{
99 struct cwdinfo *cwdi = obj;
100
101 rw_destroy(&cwdi->cwdi_lock);
102}
103
104/*
105 * Make p2 share p1's cwdinfo.
106 */
107void
108cwdshare(struct proc *p2)
109{
110 struct cwdinfo *cwdi;
111
112 cwdi = curproc->p_cwdi;
113
114 atomic_inc_uint(&cwdi->cwdi_refcnt);
115 p2->p_cwdi = cwdi;
116}
117
118/*
119 * Make sure proc has only one reference to its cwdi, creating
120 * a new one if necessary.
121 */
122void
123cwdunshare(struct proc *p)
124{
125 struct cwdinfo *cwdi = p->p_cwdi;
126
127 if (cwdi->cwdi_refcnt > 1) {
128 cwdi = cwdinit();
129 cwdfree(p->p_cwdi);
130 p->p_cwdi = cwdi;
131 }
132}
133
134/*
135 * Release a cwdinfo structure.
136 */
137void
138cwdfree(struct cwdinfo *cwdi)
139{
140
141 if (atomic_dec_uint_nv(&cwdi->cwdi_refcnt) > 0)
142 return;
143
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);
150}
151
152void
153cwdexec(struct proc *p)
154{
155
156 cwdunshare(p);
157
158 if (p->p_cwdi->cwdi_edir) {
159 vrele(p->p_cwdi->cwdi_edir);
160 }
161}
162