[BACK]Return to vfs_cwd.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / kern

Annotation of src/sys/kern/vfs_cwd.c, Revision 1.6

1.6     ! ad          1: /*     $NetBSD: vfs_cwd.c,v 1.5 2020/02/23 22:14:03 ad Exp $   */
1.1       pooka       2:
                      3: /*-
1.5       ad          4:  * Copyright (c) 2008, 2020 The NetBSD Foundation, Inc.
1.1       pooka       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>
1.6     ! ad         34: __KERNEL_RCSID(0, "$NetBSD: vfs_cwd.c,v 1.5 2020/02/23 22:14:03 ad Exp $");
1.1       pooka      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:
                     42: static int     cwdi_ctor(void *, void *, int);
                     43: static void    cwdi_dtor(void *, void *);
                     44:
                     45: static pool_cache_t cwdi_cache;
                     46:
                     47: void
                     48: cwd_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:  */
                     60: struct cwdinfo *
                     61: cwdinit(void)
                     62: {
                     63:        struct cwdinfo *cwdi;
                     64:        struct cwdinfo *copy;
                     65:
                     66:        cwdi = pool_cache_get(cwdi_cache, PR_WAITOK);
1.6     ! ad         67:        copy = curproc->p_cwdi;
1.1       pooka      68:
1.6     ! ad         69:        rw_enter(&copy->cwdi_lock, RW_READER);
1.1       pooka      70:        cwdi->cwdi_cdir = copy->cwdi_cdir;
                     71:        if (cwdi->cwdi_cdir)
1.3       pooka      72:                vref(cwdi->cwdi_cdir);
1.1       pooka      73:        cwdi->cwdi_rdir = copy->cwdi_rdir;
                     74:        if (cwdi->cwdi_rdir)
1.3       pooka      75:                vref(cwdi->cwdi_rdir);
1.1       pooka      76:        cwdi->cwdi_edir = copy->cwdi_edir;
                     77:        if (cwdi->cwdi_edir)
1.3       pooka      78:                vref(cwdi->cwdi_edir);
1.2       yamt       79:        cwdi->cwdi_cmask = copy->cwdi_cmask;
1.1       pooka      80:        cwdi->cwdi_refcnt = 1;
1.6     ! ad         81:        rw_exit(&copy->cwdi_lock);
1.1       pooka      82:
                     83:        return (cwdi);
                     84: }
                     85:
                     86: static int
                     87: cwdi_ctor(void *arg, void *obj, int flags)
                     88: {
                     89:        struct cwdinfo *cwdi = obj;
                     90:
1.6     ! ad         91:        rw_init(&cwdi->cwdi_lock);
1.1       pooka      92:
                     93:        return 0;
                     94: }
                     95:
                     96: static void
                     97: cwdi_dtor(void *arg, void *obj)
                     98: {
                     99:        struct cwdinfo *cwdi = obj;
                    100:
1.6     ! ad        101:        rw_destroy(&cwdi->cwdi_lock);
1.1       pooka     102: }
                    103:
                    104: /*
                    105:  * Make p2 share p1's cwdinfo.
                    106:  */
                    107: void
                    108: cwdshare(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:  */
                    122: void
                    123: cwdunshare(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:  */
                    137: void
                    138: cwdfree(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: }
1.4       pooka     151:
                    152: void
                    153: cwdexec(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: }

CVSweb <webmaster@jp.NetBSD.org>