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