1 /* $NetBSD: vfs_cwd.c,v 1.2 2009/09/24 06:14:22 yamt Exp $ */
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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>
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
;
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.
66 cwdi
= pool_cache_get(cwdi_cache
, PR_WAITOK
);
67 copy
= curproc
->p_cwdi
;
69 rw_enter(©
->cwdi_lock
, RW_READER
);
70 cwdi
->cwdi_cdir
= copy
->cwdi_cdir
;
72 vref(cwdi
->cwdi_cdir
);
73 cwdi
->cwdi_rdir
= copy
->cwdi_rdir
;
75 vref(cwdi
->cwdi_rdir
);
76 cwdi
->cwdi_edir
= copy
->cwdi_edir
;
78 vref(cwdi
->cwdi_edir
);
79 cwdi
->cwdi_cmask
= copy
->cwdi_cmask
;
80 cwdi
->cwdi_refcnt
= 1;
81 rw_exit(©
->cwdi_lock
);
87 cwdi_ctor(void *arg
, void *obj
, int flags
)
89 struct cwdinfo
*cwdi
= obj
;
91 rw_init(&cwdi
->cwdi_lock
);
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.
108 cwdshare(struct proc
*p2
)
110 struct cwdinfo
*cwdi
;
112 cwdi
= curproc
->p_cwdi
;
114 atomic_inc_uint(&cwdi
->cwdi_refcnt
);
119 * Make sure proc has only one reference to its cwdi, creating
120 * a new one if necessary.
123 cwdunshare(struct proc
*p
)
125 struct cwdinfo
*cwdi
= p
->p_cwdi
;
127 if (cwdi
->cwdi_refcnt
> 1) {
135 * Release a cwdinfo structure.
138 cwdfree(struct cwdinfo
*cwdi
)
141 if (atomic_dec_uint_nv(&cwdi
->cwdi_refcnt
) > 0)
144 vrele(cwdi
->cwdi_cdir
);
146 vrele(cwdi
->cwdi_rdir
);
148 vrele(cwdi
->cwdi_edir
);
149 pool_cache_put(cwdi_cache
, cwdi
);