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