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/fs/unionfs/union_vfsops.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 /*-
    2  * Copyright (c) 1994, 1995 The Regents of the University of California.
    3  * Copyright (c) 1994, 1995 Jan-Simon Pendry.
    4  * Copyright (c) 2005, 2006, 2012 Masanori Ozawa <ozawa@ongs.co.jp>, ONGS Inc.
    5  * Copyright (c) 2006, 2012 Daichi Goto <daichi@freebsd.org>
    6  * All rights reserved.
    7  *
    8  * This code is derived from software donated to Berkeley by
    9  * Jan-Simon Pendry.
   10  *
   11  * Redistribution and use in source and binary forms, with or without
   12  * modification, are permitted provided that the following conditions
   13  * are met:
   14  * 1. Redistributions of source code must retain the above copyright
   15  *    notice, this list of conditions and the following disclaimer.
   16  * 2. Redistributions in binary form must reproduce the above copyright
   17  *    notice, this list of conditions and the following disclaimer in the
   18  *    documentation and/or other materials provided with the distribution.
   19  * 4. Neither the name of the University nor the names of its contributors
   20  *    may be used to endorse or promote products derived from this software
   21  *    without specific prior written permission.
   22  *
   23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   33  * SUCH DAMAGE.
   34  *
   35  *      @(#)union_vfsops.c      8.20 (Berkeley) 5/20/95
   36  * $FreeBSD: releng/10.0/sys/fs/unionfs/union_vfsops.c 242833 2012-11-09 18:02:25Z attilio $
   37  */
   38 
   39 #include <sys/param.h>
   40 #include <sys/systm.h>
   41 #include <sys/kdb.h>
   42 #include <sys/fcntl.h>
   43 #include <sys/kernel.h>
   44 #include <sys/lock.h>
   45 #include <sys/malloc.h>
   46 #include <sys/mount.h>
   47 #include <sys/namei.h>
   48 #include <sys/proc.h>
   49 #include <sys/vnode.h>
   50 #include <sys/stat.h>
   51 
   52 #include <fs/unionfs/union.h>
   53 
   54 static MALLOC_DEFINE(M_UNIONFSMNT, "UNIONFS mount", "UNIONFS mount structure");
   55 
   56 static vfs_fhtovp_t     unionfs_fhtovp;
   57 static vfs_checkexp_t   unionfs_checkexp;
   58 static vfs_mount_t      unionfs_domount;
   59 static vfs_quotactl_t   unionfs_quotactl;
   60 static vfs_root_t       unionfs_root;
   61 static vfs_sync_t       unionfs_sync;
   62 static vfs_statfs_t     unionfs_statfs;
   63 static vfs_unmount_t    unionfs_unmount;
   64 static vfs_vget_t       unionfs_vget;
   65 static vfs_extattrctl_t unionfs_extattrctl;
   66 
   67 static struct vfsops unionfs_vfsops;
   68 
   69 /*
   70  * Mount unionfs layer.
   71  */
   72 static int
   73 unionfs_domount(struct mount *mp)
   74 {
   75         int             error;
   76         struct vnode   *lowerrootvp;
   77         struct vnode   *upperrootvp;
   78         struct unionfs_mount *ump;
   79         struct thread *td;
   80         char           *target;
   81         char           *tmp;
   82         char           *ep;
   83         int             len;
   84         size_t          done;
   85         int             below;
   86         uid_t           uid;
   87         gid_t           gid;
   88         u_short         udir;
   89         u_short         ufile;
   90         unionfs_copymode copymode;
   91         unionfs_whitemode whitemode;
   92         struct nameidata nd, *ndp;
   93         struct vattr    va;
   94 
   95         UNIONFSDEBUG("unionfs_mount(mp = %p)\n", (void *)mp);
   96 
   97         error = 0;
   98         below = 0;
   99         uid = 0;
  100         gid = 0;
  101         udir = 0;
  102         ufile = 0;
  103         copymode = UNIONFS_TRANSPARENT; /* default */
  104         whitemode = UNIONFS_WHITE_ALWAYS;
  105         ndp = &nd;
  106         td = curthread;
  107 
  108         if (mp->mnt_flag & MNT_ROOTFS) {
  109                 vfs_mount_error(mp, "Cannot union mount root filesystem");
  110                 return (EOPNOTSUPP);
  111         }
  112 
  113         /*
  114          * Update is a no operation.
  115          */
  116         if (mp->mnt_flag & MNT_UPDATE) {
  117                 vfs_mount_error(mp, "unionfs does not support mount update");
  118                 return (EOPNOTSUPP);
  119         }
  120 
  121         /*
  122          * Get argument
  123          */
  124         error = vfs_getopt(mp->mnt_optnew, "target", (void **)&target, &len);
  125         if (error)
  126                 error = vfs_getopt(mp->mnt_optnew, "from", (void **)&target,
  127                     &len);
  128         if (error || target[len - 1] != '\0') {
  129                 vfs_mount_error(mp, "Invalid target");
  130                 return (EINVAL);
  131         }
  132         if (vfs_getopt(mp->mnt_optnew, "below", NULL, NULL) == 0)
  133                 below = 1;
  134         if (vfs_getopt(mp->mnt_optnew, "udir", (void **)&tmp, NULL) == 0) {
  135                 if (tmp != NULL)
  136                         udir = (mode_t)strtol(tmp, &ep, 8);
  137                 if (tmp == NULL || *ep) {
  138                         vfs_mount_error(mp, "Invalid udir");
  139                         return (EINVAL);
  140                 }
  141                 udir &= S_IRWXU | S_IRWXG | S_IRWXO;
  142         }
  143         if (vfs_getopt(mp->mnt_optnew, "ufile", (void **)&tmp, NULL) == 0) {
  144                 if (tmp != NULL)
  145                         ufile = (mode_t)strtol(tmp, &ep, 8);
  146                 if (tmp == NULL || *ep) {
  147                         vfs_mount_error(mp, "Invalid ufile");
  148                         return (EINVAL);
  149                 }
  150                 ufile &= S_IRWXU | S_IRWXG | S_IRWXO;
  151         }
  152         /* check umask, uid and gid */
  153         if (udir == 0 && ufile != 0)
  154                 udir = ufile;
  155         if (ufile == 0 && udir != 0)
  156                 ufile = udir;
  157 
  158         vn_lock(mp->mnt_vnodecovered, LK_SHARED | LK_RETRY);
  159         error = VOP_GETATTR(mp->mnt_vnodecovered, &va, mp->mnt_cred);
  160         if (!error) {
  161                 if (udir == 0)
  162                         udir = va.va_mode;
  163                 if (ufile == 0)
  164                         ufile = va.va_mode;
  165                 uid = va.va_uid;
  166                 gid = va.va_gid;
  167         }
  168         VOP_UNLOCK(mp->mnt_vnodecovered, LK_RELEASE);
  169         if (error)
  170                 return (error);
  171 
  172         if (mp->mnt_cred->cr_ruid == 0) {       /* root only */
  173                 if (vfs_getopt(mp->mnt_optnew, "uid", (void **)&tmp,
  174                     NULL) == 0) {
  175                         if (tmp != NULL)
  176                                 uid = (uid_t)strtol(tmp, &ep, 10);
  177                         if (tmp == NULL || *ep) {
  178                                 vfs_mount_error(mp, "Invalid uid");
  179                                 return (EINVAL);
  180                         }
  181                 }
  182                 if (vfs_getopt(mp->mnt_optnew, "gid", (void **)&tmp,
  183                     NULL) == 0) {
  184                         if (tmp != NULL)
  185                                 gid = (gid_t)strtol(tmp, &ep, 10);
  186                         if (tmp == NULL || *ep) {
  187                                 vfs_mount_error(mp, "Invalid gid");
  188                                 return (EINVAL);
  189                         }
  190                 }
  191                 if (vfs_getopt(mp->mnt_optnew, "copymode", (void **)&tmp,
  192                     NULL) == 0) {
  193                         if (tmp == NULL) {
  194                                 vfs_mount_error(mp, "Invalid copymode");
  195                                 return (EINVAL);
  196                         } else if (strcasecmp(tmp, "traditional") == 0)
  197                                 copymode = UNIONFS_TRADITIONAL;
  198                         else if (strcasecmp(tmp, "transparent") == 0)
  199                                 copymode = UNIONFS_TRANSPARENT;
  200                         else if (strcasecmp(tmp, "masquerade") == 0)
  201                                 copymode = UNIONFS_MASQUERADE;
  202                         else {
  203                                 vfs_mount_error(mp, "Invalid copymode");
  204                                 return (EINVAL);
  205                         }
  206                 }
  207                 if (vfs_getopt(mp->mnt_optnew, "whiteout", (void **)&tmp,
  208                     NULL) == 0) {
  209                         if (tmp == NULL) {
  210                                 vfs_mount_error(mp, "Invalid whiteout mode");
  211                                 return (EINVAL);
  212                         } else if (strcasecmp(tmp, "always") == 0)
  213                                 whitemode = UNIONFS_WHITE_ALWAYS;
  214                         else if (strcasecmp(tmp, "whenneeded") == 0)
  215                                 whitemode = UNIONFS_WHITE_WHENNEEDED;
  216                         else {
  217                                 vfs_mount_error(mp, "Invalid whiteout mode");
  218                                 return (EINVAL);
  219                         }
  220                 }
  221         }
  222         /* If copymode is UNIONFS_TRADITIONAL, uid/gid is mounted user. */
  223         if (copymode == UNIONFS_TRADITIONAL) {
  224                 uid = mp->mnt_cred->cr_ruid;
  225                 gid = mp->mnt_cred->cr_rgid;
  226         }
  227 
  228         UNIONFSDEBUG("unionfs_mount: uid=%d, gid=%d\n", uid, gid);
  229         UNIONFSDEBUG("unionfs_mount: udir=0%03o, ufile=0%03o\n", udir, ufile);
  230         UNIONFSDEBUG("unionfs_mount: copymode=%d\n", copymode);
  231 
  232         /*
  233          * Find upper node
  234          */
  235         NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, target, td);
  236         if ((error = namei(ndp)))
  237                 return (error);
  238 
  239         NDFREE(ndp, NDF_ONLY_PNBUF);
  240 
  241         /* get root vnodes */
  242         lowerrootvp = mp->mnt_vnodecovered;
  243         upperrootvp = ndp->ni_vp;
  244 
  245         /* create unionfs_mount */
  246         ump = (struct unionfs_mount *)malloc(sizeof(struct unionfs_mount),
  247             M_UNIONFSMNT, M_WAITOK | M_ZERO);
  248 
  249         /*
  250          * Save reference
  251          */
  252         if (below) {
  253                 VOP_UNLOCK(upperrootvp, LK_RELEASE);
  254                 vn_lock(lowerrootvp, LK_EXCLUSIVE | LK_RETRY);
  255                 ump->um_lowervp = upperrootvp;
  256                 ump->um_uppervp = lowerrootvp;
  257         } else {
  258                 ump->um_lowervp = lowerrootvp;
  259                 ump->um_uppervp = upperrootvp;
  260         }
  261         ump->um_rootvp = NULLVP;
  262         ump->um_uid = uid;
  263         ump->um_gid = gid;
  264         ump->um_udir = udir;
  265         ump->um_ufile = ufile;
  266         ump->um_copymode = copymode;
  267         ump->um_whitemode = whitemode;
  268 
  269         mp->mnt_data = ump;
  270 
  271         /*
  272          * Copy upper layer's RDONLY flag.
  273          */
  274         mp->mnt_flag |= ump->um_uppervp->v_mount->mnt_flag & MNT_RDONLY;
  275 
  276         /*
  277          * Unlock the node
  278          */
  279         VOP_UNLOCK(ump->um_uppervp, LK_RELEASE);
  280 
  281         /*
  282          * Get the unionfs root vnode.
  283          */
  284         error = unionfs_nodeget(mp, ump->um_uppervp, ump->um_lowervp,
  285             NULLVP, &(ump->um_rootvp), NULL, td);
  286         vrele(upperrootvp);
  287         if (error) {
  288                 free(ump, M_UNIONFSMNT);
  289                 mp->mnt_data = NULL;
  290                 return (error);
  291         }
  292 
  293         /*
  294          * Check mnt_flag
  295          */
  296         if ((ump->um_lowervp->v_mount->mnt_flag & MNT_LOCAL) &&
  297             (ump->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
  298                 mp->mnt_flag |= MNT_LOCAL;
  299 
  300         /*
  301          * Get new fsid
  302          */
  303         vfs_getnewfsid(mp);
  304 
  305         len = MNAMELEN - 1;
  306         tmp = mp->mnt_stat.f_mntfromname;
  307         copystr((below ? "<below>:" : "<above>:"), tmp, len, &done);
  308         len -= done - 1;
  309         tmp += done - 1;
  310         copystr(target, tmp, len, NULL);
  311 
  312         UNIONFSDEBUG("unionfs_mount: from %s, on %s\n",
  313             mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
  314 
  315         return (0);
  316 }
  317 
  318 /*
  319  * Free reference to unionfs layer
  320  */
  321 static int
  322 unionfs_unmount(struct mount *mp, int mntflags)
  323 {
  324         struct unionfs_mount *ump;
  325         int             error;
  326         int             num;
  327         int             freeing;
  328         int             flags;
  329 
  330         UNIONFSDEBUG("unionfs_unmount: mp = %p\n", (void *)mp);
  331 
  332         ump = MOUNTTOUNIONFSMOUNT(mp);
  333         flags = 0;
  334 
  335         if (mntflags & MNT_FORCE)
  336                 flags |= FORCECLOSE;
  337 
  338         /* vflush (no need to call vrele) */
  339         for (freeing = 0; (error = vflush(mp, 1, flags, curthread)) != 0;) {
  340                 num = mp->mnt_nvnodelistsize;
  341                 if (num == freeing)
  342                         break;
  343                 freeing = num;
  344         }
  345 
  346         if (error)
  347                 return (error);
  348 
  349         free(ump, M_UNIONFSMNT);
  350         mp->mnt_data = NULL;
  351 
  352         return (0);
  353 }
  354 
  355 static int
  356 unionfs_root(struct mount *mp, int flags, struct vnode **vpp)
  357 {
  358         struct unionfs_mount *ump;
  359         struct vnode   *vp;
  360 
  361         ump = MOUNTTOUNIONFSMOUNT(mp);
  362         vp = ump->um_rootvp;
  363 
  364         UNIONFSDEBUG("unionfs_root: rootvp=%p locked=%x\n",
  365             vp, VOP_ISLOCKED(vp));
  366 
  367         vref(vp);
  368         if (flags & LK_TYPE_MASK)
  369                 vn_lock(vp, flags);
  370 
  371         *vpp = vp;
  372 
  373         return (0);
  374 }
  375 
  376 static int
  377 unionfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg)
  378 {
  379         struct unionfs_mount *ump;
  380 
  381         ump = MOUNTTOUNIONFSMOUNT(mp);
  382 
  383         /*
  384          * Writing is always performed to upper vnode.
  385          */
  386         return (VFS_QUOTACTL(ump->um_uppervp->v_mount, cmd, uid, arg));
  387 }
  388 
  389 static int
  390 unionfs_statfs(struct mount *mp, struct statfs *sbp)
  391 {
  392         struct unionfs_mount *ump;
  393         int             error;
  394         struct statfs   mstat;
  395         uint64_t        lbsize;
  396 
  397         ump = MOUNTTOUNIONFSMOUNT(mp);
  398 
  399         UNIONFSDEBUG("unionfs_statfs(mp = %p, lvp = %p, uvp = %p)\n",
  400             (void *)mp, (void *)ump->um_lowervp, (void *)ump->um_uppervp);
  401 
  402         bzero(&mstat, sizeof(mstat));
  403 
  404         error = VFS_STATFS(ump->um_lowervp->v_mount, &mstat);
  405         if (error)
  406                 return (error);
  407 
  408         /* now copy across the "interesting" information and fake the rest */
  409         sbp->f_blocks = mstat.f_blocks;
  410         sbp->f_files = mstat.f_files;
  411 
  412         lbsize = mstat.f_bsize;
  413 
  414         error = VFS_STATFS(ump->um_uppervp->v_mount, &mstat);
  415         if (error)
  416                 return (error);
  417 
  418         /*
  419          * The FS type etc is copy from upper vfs.
  420          * (write able vfs have priority)
  421          */
  422         sbp->f_type = mstat.f_type;
  423         sbp->f_flags = mstat.f_flags;
  424         sbp->f_bsize = mstat.f_bsize;
  425         sbp->f_iosize = mstat.f_iosize;
  426 
  427         if (mstat.f_bsize != lbsize)
  428                 sbp->f_blocks = ((off_t)sbp->f_blocks * lbsize) / mstat.f_bsize;
  429 
  430         sbp->f_blocks += mstat.f_blocks;
  431         sbp->f_bfree = mstat.f_bfree;
  432         sbp->f_bavail = mstat.f_bavail;
  433         sbp->f_files += mstat.f_files;
  434         sbp->f_ffree = mstat.f_ffree;
  435         return (0);
  436 }
  437 
  438 static int
  439 unionfs_sync(struct mount *mp, int waitfor)
  440 {
  441         /* nothing to do */
  442         return (0);
  443 }
  444 
  445 static int
  446 unionfs_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
  447 {
  448         return (EOPNOTSUPP);
  449 }
  450 
  451 static int
  452 unionfs_fhtovp(struct mount *mp, struct fid *fidp, int flags,
  453     struct vnode **vpp)
  454 {
  455         return (EOPNOTSUPP);
  456 }
  457 
  458 static int
  459 unionfs_checkexp(struct mount *mp, struct sockaddr *nam, int *extflagsp,
  460     struct ucred **credanonp, int *numsecflavors, int **secflavors)
  461 {
  462         return (EOPNOTSUPP);
  463 }
  464 
  465 static int
  466 unionfs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
  467     int namespace, const char *attrname)
  468 {
  469         struct unionfs_mount *ump;
  470         struct unionfs_node *unp;
  471 
  472         ump = MOUNTTOUNIONFSMOUNT(mp);
  473         unp = VTOUNIONFS(filename_vp);
  474 
  475         if (unp->un_uppervp != NULLVP) {
  476                 return (VFS_EXTATTRCTL(ump->um_uppervp->v_mount, cmd,
  477                     unp->un_uppervp, namespace, attrname));
  478         } else {
  479                 return (VFS_EXTATTRCTL(ump->um_lowervp->v_mount, cmd,
  480                     unp->un_lowervp, namespace, attrname));
  481         }
  482 }
  483 
  484 static struct vfsops unionfs_vfsops = {
  485         .vfs_checkexp =         unionfs_checkexp,
  486         .vfs_extattrctl =       unionfs_extattrctl,
  487         .vfs_fhtovp =           unionfs_fhtovp,
  488         .vfs_init =             unionfs_init,
  489         .vfs_mount =            unionfs_domount,
  490         .vfs_quotactl =         unionfs_quotactl,
  491         .vfs_root =             unionfs_root,
  492         .vfs_statfs =           unionfs_statfs,
  493         .vfs_sync =             unionfs_sync,
  494         .vfs_uninit =           unionfs_uninit,
  495         .vfs_unmount =          unionfs_unmount,
  496         .vfs_vget =             unionfs_vget,
  497 };
  498 
  499 VFS_SET(unionfs_vfsops, unionfs, VFCF_LOOPBACK);

Cache object: abb7dc4389e5a61321a3e5ce7b79bfc1


[ 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.