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/ufs/ffs/ffs_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) 1989, 1991, 1993, 1994
    3  *      The Regents of the University of California.  All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 4. Neither the name of the University nor the names of its contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  *
   29  *      @(#)ffs_vfsops.c        8.31 (Berkeley) 5/20/95
   30  */
   31 
   32 #include <sys/cdefs.h>
   33 __FBSDID("$FreeBSD$");
   34 
   35 #include "opt_mac.h"
   36 #include "opt_quota.h"
   37 #include "opt_ufs.h"
   38 #include "opt_ffs.h"
   39 
   40 #include <sys/param.h>
   41 #include <sys/systm.h>
   42 #include <sys/namei.h>
   43 #include <sys/priv.h>
   44 #include <sys/proc.h>
   45 #include <sys/kernel.h>
   46 #include <sys/vnode.h>
   47 #include <sys/mount.h>
   48 #include <sys/bio.h>
   49 #include <sys/buf.h>
   50 #include <sys/conf.h>
   51 #include <sys/fcntl.h>
   52 #include <sys/malloc.h>
   53 #include <sys/mutex.h>
   54 
   55 #include <security/mac/mac_framework.h>
   56 
   57 #include <ufs/ufs/extattr.h>
   58 #include <ufs/ufs/gjournal.h>
   59 #include <ufs/ufs/quota.h>
   60 #include <ufs/ufs/ufsmount.h>
   61 #include <ufs/ufs/inode.h>
   62 #include <ufs/ufs/ufs_extern.h>
   63 
   64 #include <ufs/ffs/fs.h>
   65 #include <ufs/ffs/ffs_extern.h>
   66 
   67 #include <vm/vm.h>
   68 #include <vm/uma.h>
   69 #include <vm/vm_page.h>
   70 
   71 #include <geom/geom.h>
   72 #include <geom/geom_vfs.h>
   73 
   74 static uma_zone_t uma_inode, uma_ufs1, uma_ufs2;
   75 
   76 static int      ffs_reload(struct mount *, struct thread *);
   77 static int      ffs_mountfs(struct vnode *, struct mount *, struct thread *);
   78 static void     ffs_oldfscompat_read(struct fs *, struct ufsmount *,
   79                     ufs2_daddr_t);
   80 static void     ffs_oldfscompat_write(struct fs *, struct ufsmount *);
   81 static void     ffs_ifree(struct ufsmount *ump, struct inode *ip);
   82 static vfs_init_t ffs_init;
   83 static vfs_uninit_t ffs_uninit;
   84 static vfs_extattrctl_t ffs_extattrctl;
   85 static vfs_cmount_t ffs_cmount;
   86 static vfs_unmount_t ffs_unmount;
   87 static vfs_mount_t ffs_mount;
   88 static vfs_statfs_t ffs_statfs;
   89 static vfs_fhtovp_t ffs_fhtovp;
   90 static vfs_sync_t ffs_sync;
   91 
   92 static struct vfsops ufs_vfsops = {
   93         .vfs_extattrctl =       ffs_extattrctl,
   94         .vfs_fhtovp =           ffs_fhtovp,
   95         .vfs_init =             ffs_init,
   96         .vfs_mount =            ffs_mount,
   97         .vfs_cmount =           ffs_cmount,
   98         .vfs_quotactl =         ufs_quotactl,
   99         .vfs_root =             ufs_root,
  100         .vfs_statfs =           ffs_statfs,
  101         .vfs_sync =             ffs_sync,
  102         .vfs_uninit =           ffs_uninit,
  103         .vfs_unmount =          ffs_unmount,
  104         .vfs_vget =             ffs_vget,
  105 };
  106 
  107 VFS_SET(ufs_vfsops, ufs, 0);
  108 MODULE_VERSION(ufs, 1);
  109 
  110 static b_strategy_t ffs_geom_strategy;
  111 static b_write_t ffs_bufwrite;
  112 
  113 static struct buf_ops ffs_ops = {
  114         .bop_name =     "FFS",
  115         .bop_write =    ffs_bufwrite,
  116         .bop_strategy = ffs_geom_strategy,
  117         .bop_sync =     bufsync,
  118 #ifdef NO_FFS_SNAPSHOT
  119         .bop_bdflush =  bufbdflush,
  120 #else
  121         .bop_bdflush =  ffs_bdflush,
  122 #endif
  123 };
  124 
  125 static const char *ffs_opts[] = { "acls", "async", "noatime", "noclusterr",
  126     "noclusterw", "noexec", "export", "force", "from", "multilabel", 
  127     "snapshot", "nosuid", "suiddir", "nosymfollow", "sync",
  128     "union", NULL };
  129 
  130 static int
  131 ffs_mount(struct mount *mp, struct thread *td)
  132 {
  133         struct vnode *devvp;
  134         struct ufsmount *ump = 0;
  135         struct fs *fs;
  136         int error, flags;
  137         u_int mntorflags, mntandnotflags;
  138         mode_t accessmode;
  139         struct nameidata ndp;
  140         char *fspec;
  141 
  142         if (vfs_filteropt(mp->mnt_optnew, ffs_opts))
  143                 return (EINVAL);
  144         if (uma_inode == NULL) {
  145                 uma_inode = uma_zcreate("FFS inode",
  146                     sizeof(struct inode), NULL, NULL, NULL, NULL,
  147                     UMA_ALIGN_PTR, 0);
  148                 uma_ufs1 = uma_zcreate("FFS1 dinode",
  149                     sizeof(struct ufs1_dinode), NULL, NULL, NULL, NULL,
  150                     UMA_ALIGN_PTR, 0);
  151                 uma_ufs2 = uma_zcreate("FFS2 dinode",
  152                     sizeof(struct ufs2_dinode), NULL, NULL, NULL, NULL,
  153                     UMA_ALIGN_PTR, 0);
  154         }
  155 
  156         fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
  157         if (error)
  158                 return (error);
  159 
  160         mntorflags = 0;
  161         mntandnotflags = 0;
  162         if (vfs_getopt(mp->mnt_optnew, "acls", NULL, NULL) == 0)
  163                 mntorflags |= MNT_ACLS;
  164 
  165         if (vfs_getopt(mp->mnt_optnew, "snapshot", NULL, NULL) == 0) {
  166                 mntorflags |= MNT_SNAPSHOT;
  167                 /*
  168                  * Once we have set the MNT_SNAPSHOT flag, do not
  169                  * persist "snapshot" in the options list.
  170                  */
  171                 vfs_deleteopt(mp->mnt_optnew, "snapshot");
  172                 vfs_deleteopt(mp->mnt_opt, "snapshot");
  173         }
  174 
  175         MNT_ILOCK(mp);
  176         mp->mnt_flag = (mp->mnt_flag | mntorflags) & ~mntandnotflags;
  177         MNT_IUNLOCK(mp);
  178         /*
  179          * If updating, check whether changing from read-only to
  180          * read/write; if there is no device name, that's all we do.
  181          */
  182         if (mp->mnt_flag & MNT_UPDATE) {
  183                 ump = VFSTOUFS(mp);
  184                 fs = ump->um_fs;
  185                 devvp = ump->um_devvp;
  186                 if (fs->fs_ronly == 0 &&
  187                     vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
  188                         if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
  189                                 return (error);
  190                         /*
  191                          * Flush any dirty data.
  192                          */
  193                         if ((error = ffs_sync(mp, MNT_WAIT, td)) != 0) {
  194                                 vn_finished_write(mp);
  195                                 return (error);
  196                         }
  197                         /*
  198                          * Check for and optionally get rid of files open
  199                          * for writing.
  200                          */
  201                         flags = WRITECLOSE;
  202                         if (mp->mnt_flag & MNT_FORCE)
  203                                 flags |= FORCECLOSE;
  204                         if (mp->mnt_flag & MNT_SOFTDEP) {
  205                                 error = softdep_flushfiles(mp, flags, td);
  206                         } else {
  207                                 error = ffs_flushfiles(mp, flags, td);
  208                         }
  209                         if (error) {
  210                                 vn_finished_write(mp);
  211                                 return (error);
  212                         }
  213                         if (fs->fs_pendingblocks != 0 ||
  214                             fs->fs_pendinginodes != 0) {
  215                                 printf("%s: %s: blocks %jd files %d\n",
  216                                     fs->fs_fsmnt, "update error",
  217                                     (intmax_t)fs->fs_pendingblocks,
  218                                     fs->fs_pendinginodes);
  219                                 fs->fs_pendingblocks = 0;
  220                                 fs->fs_pendinginodes = 0;
  221                         }
  222                         if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
  223                                 fs->fs_clean = 1;
  224                         if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
  225                                 fs->fs_ronly = 0;
  226                                 fs->fs_clean = 0;
  227                                 vn_finished_write(mp);
  228                                 return (error);
  229                         }
  230                         vn_finished_write(mp);
  231                         DROP_GIANT();
  232                         g_topology_lock();
  233                         g_access(ump->um_cp, 0, -1, 0);
  234                         g_topology_unlock();
  235                         PICKUP_GIANT();
  236                         fs->fs_ronly = 1;
  237                         MNT_ILOCK(mp);
  238                         mp->mnt_flag |= MNT_RDONLY;
  239                         MNT_IUNLOCK(mp);
  240                 }
  241                 if ((mp->mnt_flag & MNT_RELOAD) &&
  242                     (error = ffs_reload(mp, td)) != 0)
  243                         return (error);
  244                 if (fs->fs_ronly &&
  245                     !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
  246                         /*
  247                          * If upgrade to read-write by non-root, then verify
  248                          * that user has necessary permissions on the device.
  249                          */
  250                         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
  251                         error = VOP_ACCESS(devvp, VREAD | VWRITE,
  252                             td->td_ucred, td);
  253                         if (error)
  254                                 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
  255                         if (error) {
  256                                 VOP_UNLOCK(devvp, 0, td);
  257                                 return (error);
  258                         }
  259                         VOP_UNLOCK(devvp, 0, td);
  260                         fs->fs_flags &= ~FS_UNCLEAN;
  261                         if (fs->fs_clean == 0) {
  262                                 fs->fs_flags |= FS_UNCLEAN;
  263                                 if ((mp->mnt_flag & MNT_FORCE) ||
  264                                     ((fs->fs_flags & FS_NEEDSFSCK) == 0 &&
  265                                      (fs->fs_flags & FS_DOSOFTDEP))) {
  266                                         printf("WARNING: %s was not %s\n",
  267                                            fs->fs_fsmnt, "properly dismounted");
  268                                 } else {
  269                                         printf(
  270 "WARNING: R/W mount of %s denied.  Filesystem is not clean - run fsck\n",
  271                                             fs->fs_fsmnt);
  272                                         return (EPERM);
  273                                 }
  274                         }
  275                         DROP_GIANT();
  276                         g_topology_lock();
  277                         /*
  278                          * If we're the root device, we may not have an E count
  279                          * yet, get it now.
  280                          */
  281                         if (ump->um_cp->ace == 0)
  282                                 error = g_access(ump->um_cp, 0, 1, 1);
  283                         else
  284                                 error = g_access(ump->um_cp, 0, 1, 0);
  285                         g_topology_unlock();
  286                         PICKUP_GIANT();
  287                         if (error)
  288                                 return (error);
  289                         if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
  290                                 return (error);
  291                         fs->fs_ronly = 0;
  292                         MNT_ILOCK(mp);
  293                         mp->mnt_flag &= ~MNT_RDONLY;
  294                         MNT_IUNLOCK(mp);
  295                         fs->fs_clean = 0;
  296                         if ((error = ffs_sbupdate(ump, MNT_WAIT, 0)) != 0) {
  297                                 vn_finished_write(mp);
  298                                 return (error);
  299                         }
  300                         /* check to see if we need to start softdep */
  301                         if ((fs->fs_flags & FS_DOSOFTDEP) &&
  302                             (error = softdep_mount(devvp, mp, fs, td->td_ucred))){
  303                                 vn_finished_write(mp);
  304                                 return (error);
  305                         }
  306                         if (fs->fs_snapinum[0] != 0)
  307                                 ffs_snapshot_mount(mp);
  308                         vn_finished_write(mp);
  309                 }
  310                 /*
  311                  * Soft updates is incompatible with "async",
  312                  * so if we are doing softupdates stop the user
  313                  * from setting the async flag in an update.
  314                  * Softdep_mount() clears it in an initial mount 
  315                  * or ro->rw remount.
  316                  */
  317                 if (mp->mnt_flag & MNT_SOFTDEP) {
  318                         /* XXX: Reset too late ? */
  319                         MNT_ILOCK(mp);
  320                         mp->mnt_flag &= ~MNT_ASYNC;
  321                         MNT_IUNLOCK(mp);
  322                 }
  323                 /*
  324                  * Keep MNT_ACLS flag if it is stored in superblock.
  325                  */
  326                 if ((fs->fs_flags & FS_ACLS) != 0) {
  327                         /* XXX: Set too late ? */
  328                         MNT_ILOCK(mp);
  329                         mp->mnt_flag |= MNT_ACLS;
  330                         MNT_IUNLOCK(mp);
  331                 }
  332 
  333                 /*
  334                  * If this is a snapshot request, take the snapshot.
  335                  */
  336                 if (mp->mnt_flag & MNT_SNAPSHOT)
  337                         return (ffs_snapshot(mp, fspec));
  338         }
  339 
  340         /*
  341          * Not an update, or updating the name: look up the name
  342          * and verify that it refers to a sensible disk device.
  343          */
  344         NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
  345         if ((error = namei(&ndp)) != 0)
  346                 return (error);
  347         NDFREE(&ndp, NDF_ONLY_PNBUF);
  348         devvp = ndp.ni_vp;
  349         if (!vn_isdisk(devvp, &error)) {
  350                 vput(devvp);
  351                 return (error);
  352         }
  353 
  354         /*
  355          * If mount by non-root, then verify that user has necessary
  356          * permissions on the device.
  357          */
  358         accessmode = VREAD;
  359         if ((mp->mnt_flag & MNT_RDONLY) == 0)
  360                 accessmode |= VWRITE;
  361         error = VOP_ACCESS(devvp, accessmode, td->td_ucred, td);
  362         if (error)
  363                 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
  364         if (error) {
  365                 vput(devvp);
  366                 return (error);
  367         }
  368 
  369         if (mp->mnt_flag & MNT_UPDATE) {
  370                 /*
  371                  * Update only
  372                  *
  373                  * If it's not the same vnode, or at least the same device
  374                  * then it's not correct.
  375                  */
  376 
  377                 if (devvp->v_rdev != ump->um_devvp->v_rdev)
  378                         error = EINVAL; /* needs translation */
  379                 vput(devvp);
  380                 if (error)
  381                         return (error);
  382         } else {
  383                 /*
  384                  * New mount
  385                  *
  386                  * We need the name for the mount point (also used for
  387                  * "last mounted on") copied in. If an error occurs,
  388                  * the mount point is discarded by the upper level code.
  389                  * Note that vfs_mount() populates f_mntonname for us.
  390                  */
  391                 if ((error = ffs_mountfs(devvp, mp, td)) != 0) {
  392                         vrele(devvp);
  393                         return (error);
  394                 }
  395         }
  396         vfs_mountedfrom(mp, fspec);
  397         return (0);
  398 }
  399 
  400 /*
  401  * Compatibility with old mount system call.
  402  */
  403 
  404 static int
  405 ffs_cmount(struct mntarg *ma, void *data, int flags, struct thread *td)
  406 {
  407         struct ufs_args args;
  408         int error;
  409 
  410         if (data == NULL)
  411                 return (EINVAL);
  412         error = copyin(data, &args, sizeof args);
  413         if (error)
  414                 return (error);
  415 
  416         ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
  417         ma = mount_arg(ma, "export", &args.export, sizeof args.export);
  418         error = kernel_mount(ma, flags);
  419 
  420         return (error);
  421 }
  422 
  423 /*
  424  * Reload all incore data for a filesystem (used after running fsck on
  425  * the root filesystem and finding things to fix). The filesystem must
  426  * be mounted read-only.
  427  *
  428  * Things to do to update the mount:
  429  *      1) invalidate all cached meta-data.
  430  *      2) re-read superblock from disk.
  431  *      3) re-read summary information from disk.
  432  *      4) invalidate all inactive vnodes.
  433  *      5) invalidate all cached file data.
  434  *      6) re-read inode data for all active vnodes.
  435  */
  436 static int
  437 ffs_reload(struct mount *mp, struct thread *td)
  438 {
  439         struct vnode *vp, *mvp, *devvp;
  440         struct inode *ip;
  441         void *space;
  442         struct buf *bp;
  443         struct fs *fs, *newfs;
  444         struct ufsmount *ump;
  445         ufs2_daddr_t sblockloc;
  446         int i, blks, size, error;
  447         int32_t *lp;
  448 
  449         if ((mp->mnt_flag & MNT_RDONLY) == 0)
  450                 return (EINVAL);
  451         ump = VFSTOUFS(mp);
  452         /*
  453          * Step 1: invalidate all cached meta-data.
  454          */
  455         devvp = VFSTOUFS(mp)->um_devvp;
  456         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
  457         if (vinvalbuf(devvp, 0, td, 0, 0) != 0)
  458                 panic("ffs_reload: dirty1");
  459         VOP_UNLOCK(devvp, 0, td);
  460 
  461         /*
  462          * Step 2: re-read superblock from disk.
  463          */
  464         fs = VFSTOUFS(mp)->um_fs;
  465         if ((error = bread(devvp, btodb(fs->fs_sblockloc), fs->fs_sbsize,
  466             NOCRED, &bp)) != 0)
  467                 return (error);
  468         newfs = (struct fs *)bp->b_data;
  469         if ((newfs->fs_magic != FS_UFS1_MAGIC &&
  470              newfs->fs_magic != FS_UFS2_MAGIC) ||
  471             newfs->fs_bsize > MAXBSIZE ||
  472             newfs->fs_bsize < sizeof(struct fs)) {
  473                         brelse(bp);
  474                         return (EIO);           /* XXX needs translation */
  475         }
  476         /*
  477          * Copy pointer fields back into superblock before copying in   XXX
  478          * new superblock. These should really be in the ufsmount.      XXX
  479          * Note that important parameters (eg fs_ncg) are unchanged.
  480          */
  481         newfs->fs_csp = fs->fs_csp;
  482         newfs->fs_maxcluster = fs->fs_maxcluster;
  483         newfs->fs_contigdirs = fs->fs_contigdirs;
  484         newfs->fs_active = fs->fs_active;
  485         /* The file system is still read-only. */
  486         newfs->fs_ronly = 1;
  487         sblockloc = fs->fs_sblockloc;
  488         bcopy(newfs, fs, (u_int)fs->fs_sbsize);
  489         brelse(bp);
  490         mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
  491         ffs_oldfscompat_read(fs, VFSTOUFS(mp), sblockloc);
  492         UFS_LOCK(ump);
  493         if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
  494                 printf("%s: reload pending error: blocks %jd files %d\n",
  495                     fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
  496                     fs->fs_pendinginodes);
  497                 fs->fs_pendingblocks = 0;
  498                 fs->fs_pendinginodes = 0;
  499         }
  500         UFS_UNLOCK(ump);
  501 
  502         /*
  503          * Step 3: re-read summary information from disk.
  504          */
  505         blks = howmany(fs->fs_cssize, fs->fs_fsize);
  506         space = fs->fs_csp;
  507         for (i = 0; i < blks; i += fs->fs_frag) {
  508                 size = fs->fs_bsize;
  509                 if (i + fs->fs_frag > blks)
  510                         size = (blks - i) * fs->fs_fsize;
  511                 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
  512                     NOCRED, &bp);
  513                 if (error)
  514                         return (error);
  515                 bcopy(bp->b_data, space, (u_int)size);
  516                 space = (char *)space + size;
  517                 brelse(bp);
  518         }
  519         /*
  520          * We no longer know anything about clusters per cylinder group.
  521          */
  522         if (fs->fs_contigsumsize > 0) {
  523                 lp = fs->fs_maxcluster;
  524                 for (i = 0; i < fs->fs_ncg; i++)
  525                         *lp++ = fs->fs_contigsumsize;
  526         }
  527 
  528 loop:
  529         MNT_ILOCK(mp);
  530         MNT_VNODE_FOREACH(vp, mp, mvp) {
  531                 VI_LOCK(vp);
  532                 if (vp->v_iflag & VI_DOOMED) {
  533                         VI_UNLOCK(vp);
  534                         continue;
  535                 }
  536                 MNT_IUNLOCK(mp);
  537                 /*
  538                  * Step 4: invalidate all cached file data.
  539                  */
  540                 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
  541                         MNT_VNODE_FOREACH_ABORT(mp, mvp);
  542                         goto loop;
  543                 }
  544                 if (vinvalbuf(vp, 0, td, 0, 0))
  545                         panic("ffs_reload: dirty2");
  546                 /*
  547                  * Step 5: re-read inode data for all active vnodes.
  548                  */
  549                 ip = VTOI(vp);
  550                 error =
  551                     bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
  552                     (int)fs->fs_bsize, NOCRED, &bp);
  553                 if (error) {
  554                         VOP_UNLOCK(vp, 0, td);
  555                         vrele(vp);
  556                         MNT_VNODE_FOREACH_ABORT(mp, mvp);
  557                         return (error);
  558                 }
  559                 ffs_load_inode(bp, ip, fs, ip->i_number);
  560                 ip->i_effnlink = ip->i_nlink;
  561                 brelse(bp);
  562                 VOP_UNLOCK(vp, 0, td);
  563                 vrele(vp);
  564                 MNT_ILOCK(mp);
  565         }
  566         MNT_IUNLOCK(mp);
  567         return (0);
  568 }
  569 
  570 /*
  571  * Possible superblock locations ordered from most to least likely.
  572  */
  573 static int sblock_try[] = SBLOCKSEARCH;
  574 
  575 /*
  576  * Common code for mount and mountroot
  577  */
  578 static int
  579 ffs_mountfs(devvp, mp, td)
  580         struct vnode *devvp;
  581         struct mount *mp;
  582         struct thread *td;
  583 {
  584         struct ufsmount *ump;
  585         struct buf *bp;
  586         struct fs *fs;
  587         struct cdev *dev;
  588         void *space;
  589         ufs2_daddr_t sblockloc;
  590         int error, i, blks, size, ronly;
  591         int32_t *lp;
  592         struct ucred *cred;
  593         struct g_consumer *cp;
  594         struct mount *nmp;
  595 
  596         dev = devvp->v_rdev;
  597         cred = td ? td->td_ucred : NOCRED;
  598 
  599         ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
  600         DROP_GIANT();
  601         g_topology_lock();
  602         error = g_vfs_open(devvp, &cp, "ffs", ronly ? 0 : 1);
  603 
  604         /*
  605          * If we are a root mount, drop the E flag so fsck can do its magic.
  606          * We will pick it up again when we remount R/W.
  607          */
  608         if (error == 0 && ronly && (mp->mnt_flag & MNT_ROOTFS))
  609                 error = g_access(cp, 0, 0, -1);
  610         g_topology_unlock();
  611         PICKUP_GIANT();
  612         VOP_UNLOCK(devvp, 0, td);
  613         if (error)
  614                 return (error);
  615         if (devvp->v_rdev->si_iosize_max != 0)
  616                 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
  617         if (mp->mnt_iosize_max > MAXPHYS)
  618                 mp->mnt_iosize_max = MAXPHYS;
  619 
  620         devvp->v_bufobj.bo_private = cp;
  621         devvp->v_bufobj.bo_ops = &ffs_ops;
  622 
  623         bp = NULL;
  624         ump = NULL;
  625         fs = NULL;
  626         sblockloc = 0;
  627         /*
  628          * Try reading the superblock in each of its possible locations.
  629          */
  630         for (i = 0; sblock_try[i] != -1; i++) {
  631                 if ((SBLOCKSIZE % cp->provider->sectorsize) != 0) {
  632                         error = EINVAL;
  633                         vfs_mount_error(mp,
  634                             "Invalid sectorsize %d for superblock size %d",
  635                             cp->provider->sectorsize, SBLOCKSIZE);
  636                         goto out;
  637                 }
  638                 if ((error = bread(devvp, btodb(sblock_try[i]), SBLOCKSIZE,
  639                     cred, &bp)) != 0)
  640                         goto out;
  641                 fs = (struct fs *)bp->b_data;
  642                 sblockloc = sblock_try[i];
  643                 if ((fs->fs_magic == FS_UFS1_MAGIC ||
  644                      (fs->fs_magic == FS_UFS2_MAGIC &&
  645                       (fs->fs_sblockloc == sblockloc ||
  646                        (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0))) &&
  647                     fs->fs_bsize <= MAXBSIZE &&
  648                     fs->fs_bsize >= sizeof(struct fs))
  649                         break;
  650                 brelse(bp);
  651                 bp = NULL;
  652         }
  653         if (sblock_try[i] == -1) {
  654                 error = EINVAL;         /* XXX needs translation */
  655                 goto out;
  656         }
  657         fs->fs_fmod = 0;
  658         fs->fs_flags &= ~FS_INDEXDIRS;  /* no support for directory indicies */
  659         fs->fs_flags &= ~FS_UNCLEAN;
  660         if (fs->fs_clean == 0) {
  661                 fs->fs_flags |= FS_UNCLEAN;
  662                 if (ronly || (mp->mnt_flag & MNT_FORCE) ||
  663                     ((fs->fs_flags & FS_NEEDSFSCK) == 0 &&
  664                      (fs->fs_flags & FS_DOSOFTDEP))) {
  665                         printf(
  666 "WARNING: %s was not properly dismounted\n",
  667                             fs->fs_fsmnt);
  668                 } else {
  669                         printf(
  670 "WARNING: R/W mount of %s denied.  Filesystem is not clean - run fsck\n",
  671                             fs->fs_fsmnt);
  672                         error = EPERM;
  673                         goto out;
  674                 }
  675                 if ((fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) &&
  676                     (mp->mnt_flag & MNT_FORCE)) {
  677                         printf("%s: lost blocks %jd files %d\n", fs->fs_fsmnt,
  678                             (intmax_t)fs->fs_pendingblocks,
  679                             fs->fs_pendinginodes);
  680                         fs->fs_pendingblocks = 0;
  681                         fs->fs_pendinginodes = 0;
  682                 }
  683         }
  684         if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
  685                 printf("%s: mount pending error: blocks %jd files %d\n",
  686                     fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
  687                     fs->fs_pendinginodes);
  688                 fs->fs_pendingblocks = 0;
  689                 fs->fs_pendinginodes = 0;
  690         }
  691         if ((fs->fs_flags & FS_GJOURNAL) != 0) {
  692 #ifdef UFS_GJOURNAL
  693                 /*
  694                  * Get journal provider name.
  695                  */
  696                 size = 1024;
  697                 mp->mnt_gjprovider = malloc(size, M_UFSMNT, M_WAITOK);
  698                 if (g_io_getattr("GJOURNAL::provider", cp, &size,
  699                     mp->mnt_gjprovider) == 0) {
  700                         mp->mnt_gjprovider = realloc(mp->mnt_gjprovider, size,
  701                             M_UFSMNT, M_WAITOK);
  702                         MNT_ILOCK(mp);
  703                         mp->mnt_flag |= MNT_GJOURNAL;
  704                         MNT_IUNLOCK(mp);
  705                 } else {
  706                         printf(
  707 "WARNING: %s: GJOURNAL flag on fs but no gjournal provider below\n",
  708                             mp->mnt_stat.f_mntonname);
  709                         free(mp->mnt_gjprovider, M_UFSMNT);
  710                         mp->mnt_gjprovider = NULL;
  711                 }
  712 #else
  713                 printf(
  714 "WARNING: %s: GJOURNAL flag on fs but no UFS_GJOURNAL support\n",
  715                     mp->mnt_stat.f_mntonname);
  716 #endif
  717         } else {
  718                 mp->mnt_gjprovider = NULL;
  719         }
  720         ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
  721         ump->um_cp = cp;
  722         ump->um_bo = &devvp->v_bufobj;
  723         ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT, M_WAITOK);
  724         if (fs->fs_magic == FS_UFS1_MAGIC) {
  725                 ump->um_fstype = UFS1;
  726                 ump->um_balloc = ffs_balloc_ufs1;
  727         } else {
  728                 ump->um_fstype = UFS2;
  729                 ump->um_balloc = ffs_balloc_ufs2;
  730         }
  731         ump->um_blkatoff = ffs_blkatoff;
  732         ump->um_truncate = ffs_truncate;
  733         ump->um_update = ffs_update;
  734         ump->um_valloc = ffs_valloc;
  735         ump->um_vfree = ffs_vfree;
  736         ump->um_ifree = ffs_ifree;
  737         mtx_init(UFS_MTX(ump), "FFS", "FFS Lock", MTX_DEF);
  738         bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize);
  739         if (fs->fs_sbsize < SBLOCKSIZE)
  740                 bp->b_flags |= B_INVAL | B_NOCACHE;
  741         brelse(bp);
  742         bp = NULL;
  743         fs = ump->um_fs;
  744         ffs_oldfscompat_read(fs, ump, sblockloc);
  745         fs->fs_ronly = ronly;
  746         size = fs->fs_cssize;
  747         blks = howmany(size, fs->fs_fsize);
  748         if (fs->fs_contigsumsize > 0)
  749                 size += fs->fs_ncg * sizeof(int32_t);
  750         size += fs->fs_ncg * sizeof(u_int8_t);
  751         space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
  752         fs->fs_csp = space;
  753         for (i = 0; i < blks; i += fs->fs_frag) {
  754                 size = fs->fs_bsize;
  755                 if (i + fs->fs_frag > blks)
  756                         size = (blks - i) * fs->fs_fsize;
  757                 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
  758                     cred, &bp)) != 0) {
  759                         free(fs->fs_csp, M_UFSMNT);
  760                         goto out;
  761                 }
  762                 bcopy(bp->b_data, space, (u_int)size);
  763                 space = (char *)space + size;
  764                 brelse(bp);
  765                 bp = NULL;
  766         }
  767         if (fs->fs_contigsumsize > 0) {
  768                 fs->fs_maxcluster = lp = space;
  769                 for (i = 0; i < fs->fs_ncg; i++)
  770                         *lp++ = fs->fs_contigsumsize;
  771                 space = lp;
  772         }
  773         size = fs->fs_ncg * sizeof(u_int8_t);
  774         fs->fs_contigdirs = (u_int8_t *)space;
  775         bzero(fs->fs_contigdirs, size);
  776         fs->fs_active = NULL;
  777         mp->mnt_data = (qaddr_t)ump;
  778         mp->mnt_stat.f_fsid.val[0] = fs->fs_id[0];
  779         mp->mnt_stat.f_fsid.val[1] = fs->fs_id[1];
  780         nmp = NULL;
  781         if (fs->fs_id[0] == 0 || fs->fs_id[1] == 0 || 
  782             (nmp = vfs_getvfs(&mp->mnt_stat.f_fsid))) {
  783                 if (nmp)
  784                         vfs_rel(nmp);
  785                 vfs_getnewfsid(mp);
  786         }
  787         mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
  788         MNT_ILOCK(mp);
  789         mp->mnt_flag |= MNT_LOCAL;
  790         MNT_IUNLOCK(mp);
  791         if ((fs->fs_flags & FS_MULTILABEL) != 0) {
  792 #ifdef MAC
  793                 MNT_ILOCK(mp);
  794                 mp->mnt_flag |= MNT_MULTILABEL;
  795                 MNT_IUNLOCK(mp);
  796 #else
  797                 printf(
  798 "WARNING: %s: multilabel flag on fs but no MAC support\n",
  799                     mp->mnt_stat.f_mntonname);
  800 #endif
  801         }
  802         if ((fs->fs_flags & FS_ACLS) != 0) {
  803 #ifdef UFS_ACL
  804                 MNT_ILOCK(mp);
  805                 mp->mnt_flag |= MNT_ACLS;
  806                 MNT_IUNLOCK(mp);
  807 #else
  808                 printf(
  809 "WARNING: %s: ACLs flag on fs but no ACLs support\n",
  810                     mp->mnt_stat.f_mntonname);
  811 #endif
  812         }
  813         ump->um_mountp = mp;
  814         ump->um_dev = dev;
  815         ump->um_devvp = devvp;
  816         ump->um_nindir = fs->fs_nindir;
  817         ump->um_bptrtodb = fs->fs_fsbtodb;
  818         ump->um_seqinc = fs->fs_frag;
  819         for (i = 0; i < MAXQUOTAS; i++)
  820                 ump->um_quotas[i] = NULLVP;
  821 #ifdef UFS_EXTATTR
  822         ufs_extattr_uepm_init(&ump->um_extattr);
  823 #endif
  824         /*
  825          * Set FS local "last mounted on" information (NULL pad)
  826          */
  827         bzero(fs->fs_fsmnt, MAXMNTLEN);
  828         strlcpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MAXMNTLEN);
  829 
  830         if( mp->mnt_flag & MNT_ROOTFS) {
  831                 /*
  832                  * Root mount; update timestamp in mount structure.
  833                  * this will be used by the common root mount code
  834                  * to update the system clock.
  835                  */
  836                 mp->mnt_time = fs->fs_time;
  837         }
  838 
  839         if (ronly == 0) {
  840                 if ((fs->fs_flags & FS_DOSOFTDEP) &&
  841                     (error = softdep_mount(devvp, mp, fs, cred)) != 0) {
  842                         free(fs->fs_csp, M_UFSMNT);
  843                         goto out;
  844                 }
  845                 if (fs->fs_snapinum[0] != 0)
  846                         ffs_snapshot_mount(mp);
  847                 fs->fs_fmod = 1;
  848                 fs->fs_clean = 0;
  849                 (void) ffs_sbupdate(ump, MNT_WAIT, 0);
  850         }
  851         /*
  852          * Initialize filesystem stat information in mount struct.
  853          */
  854         MNT_ILOCK(mp);
  855         mp->mnt_kern_flag |= MNTK_MPSAFE;
  856         MNT_IUNLOCK(mp);
  857 #ifdef UFS_EXTATTR
  858 #ifdef UFS_EXTATTR_AUTOSTART
  859         /*
  860          *
  861          * Auto-starting does the following:
  862          *      - check for /.attribute in the fs, and extattr_start if so
  863          *      - for each file in .attribute, enable that file with
  864          *        an attribute of the same name.
  865          * Not clear how to report errors -- probably eat them.
  866          * This would all happen while the filesystem was busy/not
  867          * available, so would effectively be "atomic".
  868          */
  869         mp->mnt_stat.f_iosize = fs->fs_bsize;
  870         (void) ufs_extattr_autostart(mp, td);
  871 #endif /* !UFS_EXTATTR_AUTOSTART */
  872 #endif /* !UFS_EXTATTR */
  873         return (0);
  874 out:
  875         if (bp)
  876                 brelse(bp);
  877         if (cp != NULL) {
  878                 DROP_GIANT();
  879                 g_topology_lock();
  880                 g_vfs_close(cp, td);
  881                 g_topology_unlock();
  882                 PICKUP_GIANT();
  883         }
  884         if (ump) {
  885                 mtx_destroy(UFS_MTX(ump));
  886                 if (mp->mnt_gjprovider != NULL) {
  887                         free(mp->mnt_gjprovider, M_UFSMNT);
  888                         mp->mnt_gjprovider = NULL;
  889                 }
  890                 free(ump->um_fs, M_UFSMNT);
  891                 free(ump, M_UFSMNT);
  892                 mp->mnt_data = (qaddr_t)0;
  893         }
  894         return (error);
  895 }
  896 
  897 #include <sys/sysctl.h>
  898 static int bigcgs = 0;
  899 SYSCTL_INT(_debug, OID_AUTO, bigcgs, CTLFLAG_RW, &bigcgs, 0, "");
  900 
  901 /*
  902  * Sanity checks for loading old filesystem superblocks.
  903  * See ffs_oldfscompat_write below for unwound actions.
  904  *
  905  * XXX - Parts get retired eventually.
  906  * Unfortunately new bits get added.
  907  */
  908 static void
  909 ffs_oldfscompat_read(fs, ump, sblockloc)
  910         struct fs *fs;
  911         struct ufsmount *ump;
  912         ufs2_daddr_t sblockloc;
  913 {
  914         off_t maxfilesize;
  915 
  916         /*
  917          * If not yet done, update fs_flags location and value of fs_sblockloc.
  918          */
  919         if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
  920                 fs->fs_flags = fs->fs_old_flags;
  921                 fs->fs_old_flags |= FS_FLAGS_UPDATED;
  922                 fs->fs_sblockloc = sblockloc;
  923         }
  924         /*
  925          * If not yet done, update UFS1 superblock with new wider fields.
  926          */
  927         if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_maxbsize != fs->fs_bsize) {
  928                 fs->fs_maxbsize = fs->fs_bsize;
  929                 fs->fs_time = fs->fs_old_time;
  930                 fs->fs_size = fs->fs_old_size;
  931                 fs->fs_dsize = fs->fs_old_dsize;
  932                 fs->fs_csaddr = fs->fs_old_csaddr;
  933                 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
  934                 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
  935                 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
  936                 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
  937         }
  938         if (fs->fs_magic == FS_UFS1_MAGIC &&
  939             fs->fs_old_inodefmt < FS_44INODEFMT) {
  940                 fs->fs_maxfilesize = ((uint64_t)1 << 31) - 1;
  941                 fs->fs_qbmask = ~fs->fs_bmask;
  942                 fs->fs_qfmask = ~fs->fs_fmask;
  943         }
  944         if (fs->fs_magic == FS_UFS1_MAGIC) {
  945                 ump->um_savedmaxfilesize = fs->fs_maxfilesize;
  946                 maxfilesize = (uint64_t)0x80000000 * fs->fs_bsize - 1;
  947                 if (fs->fs_maxfilesize > maxfilesize)
  948                         fs->fs_maxfilesize = maxfilesize;
  949         }
  950         /* Compatibility for old filesystems */
  951         if (fs->fs_avgfilesize <= 0)
  952                 fs->fs_avgfilesize = AVFILESIZ;
  953         if (fs->fs_avgfpdir <= 0)
  954                 fs->fs_avgfpdir = AFPDIR;
  955         if (bigcgs) {
  956                 fs->fs_save_cgsize = fs->fs_cgsize;
  957                 fs->fs_cgsize = fs->fs_bsize;
  958         }
  959 }
  960 
  961 /*
  962  * Unwinding superblock updates for old filesystems.
  963  * See ffs_oldfscompat_read above for details.
  964  *
  965  * XXX - Parts get retired eventually.
  966  * Unfortunately new bits get added.
  967  */
  968 static void
  969 ffs_oldfscompat_write(fs, ump)
  970         struct fs *fs;
  971         struct ufsmount *ump;
  972 {
  973 
  974         /*
  975          * Copy back UFS2 updated fields that UFS1 inspects.
  976          */
  977         if (fs->fs_magic == FS_UFS1_MAGIC) {
  978                 fs->fs_old_time = fs->fs_time;
  979                 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
  980                 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
  981                 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
  982                 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
  983                 fs->fs_maxfilesize = ump->um_savedmaxfilesize;
  984         }
  985         if (bigcgs) {
  986                 fs->fs_cgsize = fs->fs_save_cgsize;
  987                 fs->fs_save_cgsize = 0;
  988         }
  989 }
  990 
  991 /*
  992  * unmount system call
  993  */
  994 static int
  995 ffs_unmount(mp, mntflags, td)
  996         struct mount *mp;
  997         int mntflags;
  998         struct thread *td;
  999 {
 1000         struct ufsmount *ump = VFSTOUFS(mp);
 1001         struct fs *fs;
 1002         int error, flags;
 1003 
 1004         flags = 0;
 1005         if (mntflags & MNT_FORCE) {
 1006                 flags |= FORCECLOSE;
 1007         }
 1008 #ifdef UFS_EXTATTR
 1009         if ((error = ufs_extattr_stop(mp, td))) {
 1010                 if (error != EOPNOTSUPP)
 1011                         printf("ffs_unmount: ufs_extattr_stop returned %d\n",
 1012                             error);
 1013         } else {
 1014                 ufs_extattr_uepm_destroy(&ump->um_extattr);
 1015         }
 1016 #endif
 1017         if (mp->mnt_flag & MNT_SOFTDEP) {
 1018                 if ((error = softdep_flushfiles(mp, flags, td)) != 0)
 1019                         return (error);
 1020         } else {
 1021                 if ((error = ffs_flushfiles(mp, flags, td)) != 0)
 1022                         return (error);
 1023         }
 1024         fs = ump->um_fs;
 1025         UFS_LOCK(ump);
 1026         if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) {
 1027                 printf("%s: unmount pending error: blocks %jd files %d\n",
 1028                     fs->fs_fsmnt, (intmax_t)fs->fs_pendingblocks,
 1029                     fs->fs_pendinginodes);
 1030                 fs->fs_pendingblocks = 0;
 1031                 fs->fs_pendinginodes = 0;
 1032         }
 1033         UFS_UNLOCK(ump);
 1034         if (fs->fs_ronly == 0) {
 1035                 fs->fs_clean = fs->fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK) ? 0 : 1;
 1036                 error = ffs_sbupdate(ump, MNT_WAIT, 0);
 1037                 if (error) {
 1038                         fs->fs_clean = 0;
 1039                         return (error);
 1040                 }
 1041         }
 1042         DROP_GIANT();
 1043         g_topology_lock();
 1044         g_vfs_close(ump->um_cp, td);
 1045         g_topology_unlock();
 1046         PICKUP_GIANT();
 1047         vrele(ump->um_devvp);
 1048         mtx_destroy(UFS_MTX(ump));
 1049         if (mp->mnt_gjprovider != NULL) {
 1050                 free(mp->mnt_gjprovider, M_UFSMNT);
 1051                 mp->mnt_gjprovider = NULL;
 1052         }
 1053         free(fs->fs_csp, M_UFSMNT);
 1054         free(fs, M_UFSMNT);
 1055         free(ump, M_UFSMNT);
 1056         mp->mnt_data = (qaddr_t)0;
 1057         MNT_ILOCK(mp);
 1058         mp->mnt_flag &= ~MNT_LOCAL;
 1059         MNT_IUNLOCK(mp);
 1060         return (error);
 1061 }
 1062 
 1063 /*
 1064  * Flush out all the files in a filesystem.
 1065  */
 1066 int
 1067 ffs_flushfiles(mp, flags, td)
 1068         struct mount *mp;
 1069         int flags;
 1070         struct thread *td;
 1071 {
 1072         struct ufsmount *ump;
 1073         int error;
 1074 
 1075         ump = VFSTOUFS(mp);
 1076 #ifdef QUOTA
 1077         if (mp->mnt_flag & MNT_QUOTA) {
 1078                 int i;
 1079                 error = vflush(mp, 0, SKIPSYSTEM|flags, td);
 1080                 if (error)
 1081                         return (error);
 1082                 for (i = 0; i < MAXQUOTAS; i++) {
 1083                         quotaoff(td, mp, i);
 1084                 }
 1085                 /*
 1086                  * Here we fall through to vflush again to ensure
 1087                  * that we have gotten rid of all the system vnodes.
 1088                  */
 1089         }
 1090 #endif
 1091         ASSERT_VOP_LOCKED(ump->um_devvp, "ffs_flushfiles");
 1092         if (ump->um_devvp->v_vflag & VV_COPYONWRITE) {
 1093                 if ((error = vflush(mp, 0, SKIPSYSTEM | flags, td)) != 0)
 1094                         return (error);
 1095                 ffs_snapshot_unmount(mp);
 1096                 flags |= FORCECLOSE;
 1097                 /*
 1098                  * Here we fall through to vflush again to ensure
 1099                  * that we have gotten rid of all the system vnodes.
 1100                  */
 1101         }
 1102         /*
 1103          * Flush all the files.
 1104          */
 1105         if ((error = vflush(mp, 0, flags, td)) != 0)
 1106                 return (error);
 1107         /*
 1108          * Flush filesystem metadata.
 1109          */
 1110         vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, td);
 1111         error = VOP_FSYNC(ump->um_devvp, MNT_WAIT, td);
 1112         VOP_UNLOCK(ump->um_devvp, 0, td);
 1113         return (error);
 1114 }
 1115 
 1116 /*
 1117  * Get filesystem statistics.
 1118  */
 1119 static int
 1120 ffs_statfs(mp, sbp, td)
 1121         struct mount *mp;
 1122         struct statfs *sbp;
 1123         struct thread *td;
 1124 {
 1125         struct ufsmount *ump;
 1126         struct fs *fs;
 1127 
 1128         ump = VFSTOUFS(mp);
 1129         fs = ump->um_fs;
 1130         if (fs->fs_magic != FS_UFS1_MAGIC && fs->fs_magic != FS_UFS2_MAGIC)
 1131                 panic("ffs_statfs");
 1132         sbp->f_version = STATFS_VERSION;
 1133         sbp->f_bsize = fs->fs_fsize;
 1134         sbp->f_iosize = fs->fs_bsize;
 1135         sbp->f_blocks = fs->fs_dsize;
 1136         UFS_LOCK(ump);
 1137         sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
 1138             fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
 1139         sbp->f_bavail = freespace(fs, fs->fs_minfree) +
 1140             dbtofsb(fs, fs->fs_pendingblocks);
 1141         sbp->f_files =  fs->fs_ncg * fs->fs_ipg - ROOTINO;
 1142         sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
 1143         UFS_UNLOCK(ump);
 1144         sbp->f_namemax = NAME_MAX;
 1145         return (0);
 1146 }
 1147 
 1148 /*
 1149  * Go through the disk queues to initiate sandbagged IO;
 1150  * go through the inodes to write those that have been modified;
 1151  * initiate the writing of the super block if it has been modified.
 1152  *
 1153  * Note: we are always called with the filesystem marked `MPBUSY'.
 1154  */
 1155 static int
 1156 ffs_sync(mp, waitfor, td)
 1157         struct mount *mp;
 1158         int waitfor;
 1159         struct thread *td;
 1160 {
 1161         struct vnode *mvp, *vp, *devvp;
 1162         struct inode *ip;
 1163         struct ufsmount *ump = VFSTOUFS(mp);
 1164         struct fs *fs;
 1165         int error, count, wait, lockreq, allerror = 0;
 1166         int suspend;
 1167         int suspended;
 1168         int secondary_writes;
 1169         int secondary_accwrites;
 1170         int softdep_deps;
 1171         int softdep_accdeps;
 1172         struct bufobj *bo;
 1173 
 1174         fs = ump->um_fs;
 1175         if (fs->fs_fmod != 0 && fs->fs_ronly != 0) {            /* XXX */
 1176                 printf("fs = %s\n", fs->fs_fsmnt);
 1177                 panic("ffs_sync: rofs mod");
 1178         }
 1179         /*
 1180          * Write back each (modified) inode.
 1181          */
 1182         wait = 0;
 1183         suspend = 0;
 1184         suspended = 0;
 1185         lockreq = LK_EXCLUSIVE | LK_NOWAIT;
 1186         if (waitfor == MNT_SUSPEND) {
 1187                 suspend = 1;
 1188                 waitfor = MNT_WAIT;
 1189         }
 1190         if (waitfor == MNT_WAIT) {
 1191                 wait = 1;
 1192                 lockreq = LK_EXCLUSIVE;
 1193         }
 1194         lockreq |= LK_INTERLOCK | LK_SLEEPFAIL;
 1195         MNT_ILOCK(mp);
 1196 loop:
 1197         /* Grab snapshot of secondary write counts */
 1198         secondary_writes = mp->mnt_secondary_writes;
 1199         secondary_accwrites = mp->mnt_secondary_accwrites;
 1200 
 1201         /* Grab snapshot of softdep dependency counts */
 1202         MNT_IUNLOCK(mp);
 1203         softdep_get_depcounts(mp, &softdep_deps, &softdep_accdeps);
 1204         MNT_ILOCK(mp);
 1205 
 1206         MNT_VNODE_FOREACH(vp, mp, mvp) {
 1207                 /*
 1208                  * Depend on the mntvnode_slock to keep things stable enough
 1209                  * for a quick test.  Since there might be hundreds of
 1210                  * thousands of vnodes, we cannot afford even a subroutine
 1211                  * call unless there's a good chance that we have work to do.
 1212                  */
 1213                 VI_LOCK(vp);
 1214                 if (vp->v_iflag & VI_DOOMED) {
 1215                         VI_UNLOCK(vp);
 1216                         continue;
 1217                 }
 1218                 ip = VTOI(vp);
 1219                 if (vp->v_type == VNON || ((ip->i_flag &
 1220                     (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
 1221                     vp->v_bufobj.bo_dirty.bv_cnt == 0)) {
 1222                         VI_UNLOCK(vp);
 1223                         continue;
 1224                 }
 1225                 MNT_IUNLOCK(mp);
 1226                 if ((error = vget(vp, lockreq, td)) != 0) {
 1227                         MNT_ILOCK(mp);
 1228                         if (error == ENOENT || error == ENOLCK) {
 1229                                 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
 1230                                 goto loop;
 1231                         }
 1232                         continue;
 1233                 }
 1234                 if ((error = ffs_syncvnode(vp, waitfor)) != 0)
 1235                         allerror = error;
 1236                 vput(vp);
 1237                 MNT_ILOCK(mp);
 1238         }
 1239         MNT_IUNLOCK(mp);
 1240         /*
 1241          * Force stale filesystem control information to be flushed.
 1242          */
 1243         if (waitfor == MNT_WAIT) {
 1244                 if ((error = softdep_flushworklist(ump->um_mountp, &count, td)))
 1245                         allerror = error;
 1246                 /* Flushed work items may create new vnodes to clean */
 1247                 if (allerror == 0 && count) {
 1248                         MNT_ILOCK(mp);
 1249                         goto loop;
 1250                 }
 1251         }
 1252 #ifdef QUOTA
 1253         qsync(mp);
 1254 #endif
 1255         devvp = ump->um_devvp;
 1256         VI_LOCK(devvp);
 1257         bo = &devvp->v_bufobj;
 1258         if (waitfor != MNT_LAZY &&
 1259             (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)) {
 1260                 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK, td);
 1261                 if ((error = VOP_FSYNC(devvp, waitfor, td)) != 0)
 1262                         allerror = error;
 1263                 VOP_UNLOCK(devvp, 0, td);
 1264                 if (allerror == 0 && waitfor == MNT_WAIT) {
 1265                         MNT_ILOCK(mp);
 1266                         goto loop;
 1267                 }
 1268         } else if (suspend != 0) {
 1269                 if (softdep_check_suspend(mp,
 1270                                           devvp,
 1271                                           softdep_deps,
 1272                                           softdep_accdeps,
 1273                                           secondary_writes,
 1274                                           secondary_accwrites) != 0)
 1275                         goto loop;      /* More work needed */
 1276                 mtx_assert(MNT_MTX(mp), MA_OWNED);
 1277                 mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
 1278                 MNT_IUNLOCK(mp);
 1279                 suspended = 1;
 1280         } else
 1281                 VI_UNLOCK(devvp);
 1282         /*
 1283          * Write back modified superblock.
 1284          */
 1285         if (fs->fs_fmod != 0 &&
 1286             (error = ffs_sbupdate(ump, waitfor, suspended)) != 0)
 1287                 allerror = error;
 1288         return (allerror);
 1289 }
 1290 
 1291 int
 1292 ffs_vget(mp, ino, flags, vpp)
 1293         struct mount *mp;
 1294         ino_t ino;
 1295         int flags;
 1296         struct vnode **vpp;
 1297 {
 1298         struct fs *fs;
 1299         struct inode *ip;
 1300         struct ufsmount *ump;
 1301         struct buf *bp;
 1302         struct vnode *vp;
 1303         struct cdev *dev;
 1304         int error;
 1305         struct thread *td;
 1306 
 1307         error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
 1308         if (error || *vpp != NULL)
 1309                 return (error);
 1310 
 1311         /*
 1312          * We must promote to an exclusive lock for vnode creation.  This
 1313          * can happen if lookup is passed LOCKSHARED.
 1314          */
 1315         if ((flags & LK_TYPE_MASK) == LK_SHARED) {
 1316                 flags &= ~LK_TYPE_MASK;
 1317                 flags |= LK_EXCLUSIVE;
 1318         }
 1319 
 1320         /*
 1321          * We do not lock vnode creation as it is believed to be too
 1322          * expensive for such rare case as simultaneous creation of vnode
 1323          * for same ino by different processes. We just allow them to race
 1324          * and check later to decide who wins. Let the race begin!
 1325          */
 1326 
 1327         ump = VFSTOUFS(mp);
 1328         dev = ump->um_dev;
 1329         fs = ump->um_fs;
 1330 
 1331         /*
 1332          * If this MALLOC() is performed after the getnewvnode()
 1333          * it might block, leaving a vnode with a NULL v_data to be
 1334          * found by ffs_sync() if a sync happens to fire right then,
 1335          * which will cause a panic because ffs_sync() blindly
 1336          * dereferences vp->v_data (as well it should).
 1337          */
 1338         ip = uma_zalloc(uma_inode, M_WAITOK | M_ZERO);
 1339 
 1340         /* Allocate a new vnode/inode. */
 1341         if (fs->fs_magic == FS_UFS1_MAGIC)
 1342                 error = getnewvnode("ufs", mp, &ffs_vnodeops1, &vp);
 1343         else
 1344                 error = getnewvnode("ufs", mp, &ffs_vnodeops2, &vp);
 1345         if (error) {
 1346                 *vpp = NULL;
 1347                 uma_zfree(uma_inode, ip);
 1348                 return (error);
 1349         }
 1350         /*
 1351          * FFS supports recursive and shared locking.
 1352          */
 1353         vp->v_vnlock->lk_flags |= LK_CANRECURSE;
 1354         vp->v_vnlock->lk_flags &= ~LK_NOSHARE;
 1355         vp->v_data = ip;
 1356         vp->v_bufobj.bo_bsize = fs->fs_bsize;
 1357         ip->i_vnode = vp;
 1358         ip->i_ump = ump;
 1359         ip->i_fs = fs;
 1360         ip->i_dev = dev;
 1361         ip->i_number = ino;
 1362 #ifdef QUOTA
 1363         {
 1364                 int i;
 1365                 for (i = 0; i < MAXQUOTAS; i++)
 1366                         ip->i_dquot[i] = NODQUOT;
 1367         }
 1368 #endif
 1369 
 1370         td = curthread;
 1371         lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL, td);
 1372         error = insmntque(vp, mp);
 1373         if (error != 0) {
 1374                 uma_zfree(uma_inode, ip);
 1375                 *vpp = NULL;
 1376                 return (error);
 1377         }
 1378         error = vfs_hash_insert(vp, ino, flags, td, vpp, NULL, NULL);
 1379         if (error || *vpp != NULL)
 1380                 return (error);
 1381 
 1382         /* Read in the disk contents for the inode, copy into the inode. */
 1383         error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
 1384             (int)fs->fs_bsize, NOCRED, &bp);
 1385         if (error) {
 1386                 /*
 1387                  * The inode does not contain anything useful, so it would
 1388                  * be misleading to leave it on its hash chain. With mode
 1389                  * still zero, it will be unlinked and returned to the free
 1390                  * list by vput().
 1391                  */
 1392                 brelse(bp);
 1393                 vput(vp);
 1394                 *vpp = NULL;
 1395                 return (error);
 1396         }
 1397         if (ip->i_ump->um_fstype == UFS1)
 1398                 ip->i_din1 = uma_zalloc(uma_ufs1, M_WAITOK);
 1399         else
 1400                 ip->i_din2 = uma_zalloc(uma_ufs2, M_WAITOK);
 1401         ffs_load_inode(bp, ip, fs, ino);
 1402         if (DOINGSOFTDEP(vp))
 1403                 softdep_load_inodeblock(ip);
 1404         else
 1405                 ip->i_effnlink = ip->i_nlink;
 1406         bqrelse(bp);
 1407 
 1408         /*
 1409          * Initialize the vnode from the inode, check for aliases.
 1410          * Note that the underlying vnode may have changed.
 1411          */
 1412         if (ip->i_ump->um_fstype == UFS1)
 1413                 error = ufs_vinit(mp, &ffs_fifoops1, &vp);
 1414         else
 1415                 error = ufs_vinit(mp, &ffs_fifoops2, &vp);
 1416         if (error) {
 1417                 vput(vp);
 1418                 *vpp = NULL;
 1419                 return (error);
 1420         }
 1421 
 1422         /*
 1423          * Finish inode initialization.
 1424          */
 1425 
 1426         /*
 1427          * Set up a generation number for this inode if it does not
 1428          * already have one. This should only happen on old filesystems.
 1429          */
 1430         if (ip->i_gen == 0) {
 1431                 ip->i_gen = arc4random() / 2 + 1;
 1432                 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
 1433                         ip->i_flag |= IN_MODIFIED;
 1434                         DIP_SET(ip, i_gen, ip->i_gen);
 1435                 }
 1436         }
 1437         /*
 1438          * Ensure that uid and gid are correct. This is a temporary
 1439          * fix until fsck has been changed to do the update.
 1440          */
 1441         if (fs->fs_magic == FS_UFS1_MAGIC &&            /* XXX */
 1442             fs->fs_old_inodefmt < FS_44INODEFMT) {      /* XXX */
 1443                 ip->i_uid = ip->i_din1->di_ouid;        /* XXX */
 1444                 ip->i_gid = ip->i_din1->di_ogid;        /* XXX */
 1445         }                                               /* XXX */
 1446 
 1447 #ifdef MAC
 1448         if ((mp->mnt_flag & MNT_MULTILABEL) && ip->i_mode) {
 1449                 /*
 1450                  * If this vnode is already allocated, and we're running
 1451                  * multi-label, attempt to perform a label association
 1452                  * from the extended attributes on the inode.
 1453                  */
 1454                 error = mac_associate_vnode_extattr(mp, vp);
 1455                 if (error) {
 1456                         /* ufs_inactive will release ip->i_devvp ref. */
 1457                         vput(vp);
 1458                         *vpp = NULL;
 1459                         return (error);
 1460                 }
 1461         }
 1462 #endif
 1463 
 1464         *vpp = vp;
 1465         return (0);
 1466 }
 1467 
 1468 /*
 1469  * File handle to vnode
 1470  *
 1471  * Have to be really careful about stale file handles:
 1472  * - check that the inode number is valid
 1473  * - call ffs_vget() to get the locked inode
 1474  * - check for an unallocated inode (i_mode == 0)
 1475  * - check that the given client host has export rights and return
 1476  *   those rights via. exflagsp and credanonp
 1477  */
 1478 static int
 1479 ffs_fhtovp(mp, fhp, vpp)
 1480         struct mount *mp;
 1481         struct fid *fhp;
 1482         struct vnode **vpp;
 1483 {
 1484         struct ufid *ufhp;
 1485         struct fs *fs;
 1486 
 1487         ufhp = (struct ufid *)fhp;
 1488         fs = VFSTOUFS(mp)->um_fs;
 1489         if (ufhp->ufid_ino < ROOTINO ||
 1490             ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
 1491                 return (ESTALE);
 1492         return (ufs_fhtovp(mp, ufhp, vpp));
 1493 }
 1494 
 1495 /*
 1496  * Initialize the filesystem.
 1497  */
 1498 static int
 1499 ffs_init(vfsp)
 1500         struct vfsconf *vfsp;
 1501 {
 1502 
 1503         softdep_initialize();
 1504         return (ufs_init(vfsp));
 1505 }
 1506 
 1507 /*
 1508  * Undo the work of ffs_init().
 1509  */
 1510 static int
 1511 ffs_uninit(vfsp)
 1512         struct vfsconf *vfsp;
 1513 {
 1514         int ret;
 1515 
 1516         ret = ufs_uninit(vfsp);
 1517         softdep_uninitialize();
 1518         return (ret);
 1519 }
 1520 
 1521 /*
 1522  * Write a superblock and associated information back to disk.
 1523  */
 1524 int
 1525 ffs_sbupdate(mp, waitfor, suspended)
 1526         struct ufsmount *mp;
 1527         int waitfor;
 1528         int suspended;
 1529 {
 1530         struct fs *fs = mp->um_fs;
 1531         struct buf *sbbp;
 1532         struct buf *bp;
 1533         int blks;
 1534         void *space;
 1535         int i, size, error, allerror = 0;
 1536 
 1537         if (fs->fs_ronly == 1 &&
 1538             (mp->um_mountp->mnt_flag & (MNT_RDONLY | MNT_UPDATE)) != 
 1539             (MNT_RDONLY | MNT_UPDATE))
 1540                 panic("ffs_sbupdate: write read-only filesystem");
 1541         /*
 1542          * We use the superblock's buf to serialize calls to ffs_sbupdate().
 1543          */
 1544         sbbp = getblk(mp->um_devvp, btodb(fs->fs_sblockloc), (int)fs->fs_sbsize,
 1545             0, 0, 0);
 1546         /*
 1547          * First write back the summary information.
 1548          */
 1549         blks = howmany(fs->fs_cssize, fs->fs_fsize);
 1550         space = fs->fs_csp;
 1551         for (i = 0; i < blks; i += fs->fs_frag) {
 1552                 size = fs->fs_bsize;
 1553                 if (i + fs->fs_frag > blks)
 1554                         size = (blks - i) * fs->fs_fsize;
 1555                 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
 1556                     size, 0, 0, 0);
 1557                 bcopy(space, bp->b_data, (u_int)size);
 1558                 space = (char *)space + size;
 1559                 if (suspended)
 1560                         bp->b_flags |= B_VALIDSUSPWRT;
 1561                 if (waitfor != MNT_WAIT)
 1562                         bawrite(bp);
 1563                 else if ((error = bwrite(bp)) != 0)
 1564                         allerror = error;
 1565         }
 1566         /*
 1567          * Now write back the superblock itself. If any errors occurred
 1568          * up to this point, then fail so that the superblock avoids
 1569          * being written out as clean.
 1570          */
 1571         if (allerror) {
 1572                 brelse(sbbp);
 1573                 return (allerror);
 1574         }
 1575         bp = sbbp;
 1576         if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_sblockloc != SBLOCK_UFS1 &&
 1577             (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
 1578                 printf("%s: correcting fs_sblockloc from %jd to %d\n",
 1579                     fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
 1580                 fs->fs_sblockloc = SBLOCK_UFS1;
 1581         }
 1582         if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_sblockloc != SBLOCK_UFS2 &&
 1583             (fs->fs_flags & FS_FLAGS_UPDATED) == 0) {
 1584                 printf("%s: correcting fs_sblockloc from %jd to %d\n",
 1585                     fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
 1586                 fs->fs_sblockloc = SBLOCK_UFS2;
 1587         }
 1588         fs->fs_fmod = 0;
 1589         fs->fs_time = time_second;
 1590         bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
 1591         ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
 1592         if (suspended)
 1593                 bp->b_flags |= B_VALIDSUSPWRT;
 1594         if (waitfor != MNT_WAIT)
 1595                 bawrite(bp);
 1596         else if ((error = bwrite(bp)) != 0)
 1597                 allerror = error;
 1598         return (allerror);
 1599 }
 1600 
 1601 static int
 1602 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
 1603         int attrnamespace, const char *attrname, struct thread *td)
 1604 {
 1605 
 1606 #ifdef UFS_EXTATTR
 1607         return (ufs_extattrctl(mp, cmd, filename_vp, attrnamespace,
 1608             attrname, td));
 1609 #else
 1610         return (vfs_stdextattrctl(mp, cmd, filename_vp, attrnamespace,
 1611             attrname, td));
 1612 #endif
 1613 }
 1614 
 1615 static void
 1616 ffs_ifree(struct ufsmount *ump, struct inode *ip)
 1617 {
 1618 
 1619         if (ump->um_fstype == UFS1 && ip->i_din1 != NULL)
 1620                 uma_zfree(uma_ufs1, ip->i_din1);
 1621         else if (ip->i_din2 != NULL)
 1622                 uma_zfree(uma_ufs2, ip->i_din2);
 1623         uma_zfree(uma_inode, ip);
 1624 }
 1625 
 1626 static int dobkgrdwrite = 1;
 1627 SYSCTL_INT(_debug, OID_AUTO, dobkgrdwrite, CTLFLAG_RW, &dobkgrdwrite, 0,
 1628     "Do background writes (honoring the BV_BKGRDWRITE flag)?");
 1629 
 1630 /*
 1631  * Complete a background write started from bwrite.
 1632  */
 1633 static void
 1634 ffs_backgroundwritedone(struct buf *bp)
 1635 {
 1636         struct bufobj *bufobj;
 1637         struct buf *origbp;
 1638 
 1639         /*
 1640          * Find the original buffer that we are writing.
 1641          */
 1642         bufobj = bp->b_bufobj;
 1643         BO_LOCK(bufobj);
 1644         if ((origbp = gbincore(bp->b_bufobj, bp->b_lblkno)) == NULL)
 1645                 panic("backgroundwritedone: lost buffer");
 1646         /* Grab an extra reference to be dropped by the bufdone() below. */
 1647         bufobj_wrefl(bufobj);
 1648         BO_UNLOCK(bufobj);
 1649         /*
 1650          * Process dependencies then return any unfinished ones.
 1651          */
 1652         if (!LIST_EMPTY(&bp->b_dep))
 1653                 buf_complete(bp);
 1654 #ifdef SOFTUPDATES
 1655         if (!LIST_EMPTY(&bp->b_dep))
 1656                 softdep_move_dependencies(bp, origbp);
 1657 #endif
 1658         /*
 1659          * This buffer is marked B_NOCACHE so when it is released
 1660          * by biodone it will be tossed.
 1661          */
 1662         bp->b_flags |= B_NOCACHE;
 1663         bp->b_flags &= ~B_CACHE;
 1664         bufdone(bp);
 1665         BO_LOCK(bufobj);
 1666         /*
 1667          * Clear the BV_BKGRDINPROG flag in the original buffer
 1668          * and awaken it if it is waiting for the write to complete.
 1669          * If BV_BKGRDINPROG is not set in the original buffer it must
 1670          * have been released and re-instantiated - which is not legal.
 1671          */
 1672         KASSERT((origbp->b_vflags & BV_BKGRDINPROG),
 1673             ("backgroundwritedone: lost buffer2"));
 1674         origbp->b_vflags &= ~BV_BKGRDINPROG;
 1675         if (origbp->b_vflags & BV_BKGRDWAIT) {
 1676                 origbp->b_vflags &= ~BV_BKGRDWAIT;
 1677                 wakeup(&origbp->b_xflags);
 1678         }
 1679         BO_UNLOCK(bufobj);
 1680 }
 1681 
 1682 
 1683 /*
 1684  * Write, release buffer on completion.  (Done by iodone
 1685  * if async).  Do not bother writing anything if the buffer
 1686  * is invalid.
 1687  *
 1688  * Note that we set B_CACHE here, indicating that buffer is
 1689  * fully valid and thus cacheable.  This is true even of NFS
 1690  * now so we set it generally.  This could be set either here 
 1691  * or in biodone() since the I/O is synchronous.  We put it
 1692  * here.
 1693  */
 1694 static int
 1695 ffs_bufwrite(struct buf *bp)
 1696 {
 1697         int oldflags, s;
 1698         struct buf *newbp;
 1699 
 1700         CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
 1701         if (bp->b_flags & B_INVAL) {
 1702                 brelse(bp);
 1703                 return (0);
 1704         }
 1705 
 1706         oldflags = bp->b_flags;
 1707 
 1708         if (BUF_REFCNT(bp) == 0)
 1709                 panic("bufwrite: buffer is not busy???");
 1710         s = splbio();
 1711         /*
 1712          * If a background write is already in progress, delay
 1713          * writing this block if it is asynchronous. Otherwise
 1714          * wait for the background write to complete.
 1715          */
 1716         BO_LOCK(bp->b_bufobj);
 1717         if (bp->b_vflags & BV_BKGRDINPROG) {
 1718                 if (bp->b_flags & B_ASYNC) {
 1719                         BO_UNLOCK(bp->b_bufobj);
 1720                         splx(s);
 1721                         bdwrite(bp);
 1722                         return (0);
 1723                 }
 1724                 bp->b_vflags |= BV_BKGRDWAIT;
 1725                 msleep(&bp->b_xflags, BO_MTX(bp->b_bufobj), PRIBIO, "bwrbg", 0);
 1726                 if (bp->b_vflags & BV_BKGRDINPROG)
 1727                         panic("bufwrite: still writing");
 1728         }
 1729         BO_UNLOCK(bp->b_bufobj);
 1730 
 1731         /* Mark the buffer clean */
 1732         bundirty(bp);
 1733 
 1734         /*
 1735          * If this buffer is marked for background writing and we
 1736          * do not have to wait for it, make a copy and write the
 1737          * copy so as to leave this buffer ready for further use.
 1738          *
 1739          * This optimization eats a lot of memory.  If we have a page
 1740          * or buffer shortfall we can't do it.
 1741          */
 1742         if (dobkgrdwrite && (bp->b_xflags & BX_BKGRDWRITE) && 
 1743             (bp->b_flags & B_ASYNC) &&
 1744             !vm_page_count_severe() &&
 1745             !buf_dirty_count_severe()) {
 1746                 KASSERT(bp->b_iodone == NULL,
 1747                     ("bufwrite: needs chained iodone (%p)", bp->b_iodone));
 1748 
 1749                 /* get a new block */
 1750                 newbp = geteblk(bp->b_bufsize);
 1751 
 1752                 /*
 1753                  * set it to be identical to the old block.  We have to
 1754                  * set b_lblkno and BKGRDMARKER before calling bgetvp()
 1755                  * to avoid confusing the splay tree and gbincore().
 1756                  */
 1757                 memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
 1758                 newbp->b_lblkno = bp->b_lblkno;
 1759                 newbp->b_xflags |= BX_BKGRDMARKER;
 1760                 BO_LOCK(bp->b_bufobj);
 1761                 bp->b_vflags |= BV_BKGRDINPROG;
 1762                 bgetvp(bp->b_vp, newbp);
 1763                 BO_UNLOCK(bp->b_bufobj);
 1764                 newbp->b_bufobj = &bp->b_vp->v_bufobj;
 1765                 newbp->b_blkno = bp->b_blkno;
 1766                 newbp->b_offset = bp->b_offset;
 1767                 newbp->b_iodone = ffs_backgroundwritedone;
 1768                 newbp->b_flags |= B_ASYNC;
 1769                 newbp->b_flags &= ~B_INVAL;
 1770 
 1771 #ifdef SOFTUPDATES
 1772                 /* move over the dependencies */
 1773                 if (!LIST_EMPTY(&bp->b_dep))
 1774                         softdep_move_dependencies(bp, newbp);
 1775 #endif 
 1776 
 1777                 /*
 1778                  * Initiate write on the copy, release the original to
 1779                  * the B_LOCKED queue so that it cannot go away until
 1780                  * the background write completes. If not locked it could go
 1781                  * away and then be reconstituted while it was being written.
 1782                  * If the reconstituted buffer were written, we could end up
 1783                  * with two background copies being written at the same time.
 1784                  */
 1785                 bqrelse(bp);
 1786                 bp = newbp;
 1787         }
 1788 
 1789         /* Let the normal bufwrite do the rest for us */
 1790         return (bufwrite(bp));
 1791 }
 1792 
 1793 
 1794 static void
 1795 ffs_geom_strategy(struct bufobj *bo, struct buf *bp)
 1796 {
 1797         struct vnode *vp;
 1798         int error;
 1799         struct buf *tbp;
 1800 
 1801         vp = bo->__bo_vnode;
 1802         if (bp->b_iocmd == BIO_WRITE) {
 1803                 if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
 1804                     bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
 1805                     (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
 1806                         panic("ffs_geom_strategy: bad I/O");
 1807                 bp->b_flags &= ~B_VALIDSUSPWRT;
 1808                 if ((vp->v_vflag & VV_COPYONWRITE) &&
 1809                     vp->v_rdev->si_snapdata != NULL) {
 1810                         if ((bp->b_flags & B_CLUSTER) != 0) {
 1811                                 runningbufwakeup(bp);
 1812                                 TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
 1813                                               b_cluster.cluster_entry) {
 1814                                         error = ffs_copyonwrite(vp, tbp);
 1815                                         if (error != 0 &&
 1816                                             error != EOPNOTSUPP) {
 1817                                                 bp->b_error = error;
 1818                                                 bp->b_ioflags |= BIO_ERROR;
 1819                                                 bufdone(bp);
 1820                                                 return;
 1821                                         }
 1822                                 }
 1823                                 bp->b_runningbufspace = bp->b_bufsize;
 1824                                 atomic_add_int(&runningbufspace,
 1825                                                bp->b_runningbufspace);
 1826                         } else {
 1827                                 error = ffs_copyonwrite(vp, bp);
 1828                                 if (error != 0 && error != EOPNOTSUPP) {
 1829                                         bp->b_error = error;
 1830                                         bp->b_ioflags |= BIO_ERROR;
 1831                                         bufdone(bp);
 1832                                         return;
 1833                                 }
 1834                         }
 1835                 }
 1836 #ifdef SOFTUPDATES
 1837                 if ((bp->b_flags & B_CLUSTER) != 0) {
 1838                         TAILQ_FOREACH(tbp, &bp->b_cluster.cluster_head,
 1839                                       b_cluster.cluster_entry) {
 1840                                 if (!LIST_EMPTY(&tbp->b_dep))
 1841                                         buf_start(tbp);
 1842                         }
 1843                 } else {
 1844                         if (!LIST_EMPTY(&bp->b_dep))
 1845                                 buf_start(bp);
 1846                 }
 1847 
 1848 #endif
 1849         }
 1850         g_vfs_strategy(bo, bp);
 1851 }

Cache object: eb17cbc381158522ec888366fd4986e4


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