The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/vfs_cwd.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: vfs_cwd.c,v 1.8 2022/04/09 23:38:33 riastradh Exp $    */
    2 
    3 /*-
    4  * Copyright (c) 2008, 2020 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.8 2022/04/09 23:38:33 riastradh 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 
   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);
   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 
   86 static int
   87 cwdi_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 
   96 static void
   97 cwdi_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  */
  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         membar_release();
  142         if (atomic_dec_uint_nv(&cwdi->cwdi_refcnt) > 0)
  143                 return;
  144         membar_acquire();
  145 
  146         vrele(cwdi->cwdi_cdir);
  147         if (cwdi->cwdi_rdir)
  148                 vrele(cwdi->cwdi_rdir);
  149         if (cwdi->cwdi_edir)
  150                 vrele(cwdi->cwdi_edir);
  151         pool_cache_put(cwdi_cache, cwdi);
  152 }
  153 
  154 void
  155 cwdexec(struct proc *p)
  156 {
  157 
  158         cwdunshare(p);
  159 
  160         if (p->p_cwdi->cwdi_edir) {
  161                 vrele(p->p_cwdi->cwdi_edir);
  162         }
  163 }

Cache object: a6f6db6a362aa748a794c5873b4de037


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.