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_alloc.c

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

    1 /*      $NetBSD: ffs_alloc.c,v 1.74.2.1 2004/04/27 17:55:26 jdc Exp $   */
    2 
    3 /*
    4  * Copyright (c) 2002 Networks Associates Technology, Inc.
    5  * All rights reserved.
    6  *
    7  * This software was developed for the FreeBSD Project by Marshall
    8  * Kirk McKusick and Network Associates Laboratories, the Security
    9  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
   10  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
   11  * research program
   12  *
   13  * Copyright (c) 1982, 1986, 1989, 1993
   14  *      The Regents of the University of California.  All rights reserved.
   15  *
   16  * Redistribution and use in source and binary forms, with or without
   17  * modification, are permitted provided that the following conditions
   18  * are met:
   19  * 1. Redistributions of source code must retain the above copyright
   20  *    notice, this list of conditions and the following disclaimer.
   21  * 2. Redistributions in binary form must reproduce the above copyright
   22  *    notice, this list of conditions and the following disclaimer in the
   23  *    documentation and/or other materials provided with the distribution.
   24  * 3. Neither the name of the University nor the names of its contributors
   25  *    may be used to endorse or promote products derived from this software
   26  *    without specific prior written permission.
   27  *
   28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   38  * SUCH DAMAGE.
   39  *
   40  *      @(#)ffs_alloc.c 8.19 (Berkeley) 7/13/95
   41  */
   42 
   43 #include <sys/cdefs.h>
   44 __KERNEL_RCSID(0, "$NetBSD: ffs_alloc.c,v 1.74.2.1 2004/04/27 17:55:26 jdc Exp $");
   45 
   46 #if defined(_KERNEL_OPT)
   47 #include "opt_ffs.h"
   48 #include "opt_quota.h"
   49 #endif
   50 
   51 #include <sys/param.h>
   52 #include <sys/systm.h>
   53 #include <sys/buf.h>
   54 #include <sys/proc.h>
   55 #include <sys/vnode.h>
   56 #include <sys/mount.h>
   57 #include <sys/kernel.h>
   58 #include <sys/syslog.h>
   59 
   60 #include <ufs/ufs/quota.h>
   61 #include <ufs/ufs/ufsmount.h>
   62 #include <ufs/ufs/inode.h>
   63 #include <ufs/ufs/ufs_extern.h>
   64 #include <ufs/ufs/ufs_bswap.h>
   65 
   66 #include <ufs/ffs/fs.h>
   67 #include <ufs/ffs/ffs_extern.h>
   68 
   69 static daddr_t ffs_alloccg __P((struct inode *, int, daddr_t, int));
   70 static daddr_t ffs_alloccgblk __P((struct inode *, struct buf *, daddr_t));
   71 #ifdef XXXUBC
   72 static daddr_t ffs_clusteralloc __P((struct inode *, int, daddr_t, int));
   73 #endif
   74 static ino_t ffs_dirpref __P((struct inode *));
   75 static daddr_t ffs_fragextend __P((struct inode *, int, daddr_t, int, int));
   76 static void ffs_fserr __P((struct fs *, u_int, char *));
   77 static daddr_t ffs_hashalloc __P((struct inode *, int, daddr_t, int,
   78     daddr_t (*)(struct inode *, int, daddr_t, int)));
   79 static daddr_t ffs_nodealloccg __P((struct inode *, int, daddr_t, int));
   80 static int32_t ffs_mapsearch __P((struct fs *, struct cg *,
   81                                       daddr_t, int));
   82 #if defined(DIAGNOSTIC) || defined(DEBUG)
   83 #ifdef XXXUBC
   84 static int ffs_checkblk __P((struct inode *, daddr_t, long size));
   85 #endif
   86 #endif
   87 
   88 /* if 1, changes in optimalization strategy are logged */
   89 int ffs_log_changeopt = 0;
   90 
   91 /* in ffs_tables.c */
   92 extern const int inside[], around[];
   93 extern const u_char * const fragtbl[];
   94 
   95 /*
   96  * Allocate a block in the file system.
   97  * 
   98  * The size of the requested block is given, which must be some
   99  * multiple of fs_fsize and <= fs_bsize.
  100  * A preference may be optionally specified. If a preference is given
  101  * the following hierarchy is used to allocate a block:
  102  *   1) allocate the requested block.
  103  *   2) allocate a rotationally optimal block in the same cylinder.
  104  *   3) allocate a block in the same cylinder group.
  105  *   4) quadradically rehash into other cylinder groups, until an
  106  *      available block is located.
  107  * If no block preference is given the following hierarchy is used
  108  * to allocate a block:
  109  *   1) allocate a block in the cylinder group that contains the
  110  *      inode for the file.
  111  *   2) quadradically rehash into other cylinder groups, until an
  112  *      available block is located.
  113  */
  114 int
  115 ffs_alloc(ip, lbn, bpref, size, cred, bnp)
  116         struct inode *ip;
  117         daddr_t lbn, bpref;
  118         int size;
  119         struct ucred *cred;
  120         daddr_t *bnp;
  121 {
  122         struct fs *fs;
  123         daddr_t bno;
  124         int cg;
  125 #ifdef QUOTA
  126         int error;
  127 #endif
  128         
  129         fs = ip->i_fs;
  130 
  131 #ifdef UVM_PAGE_TRKOWN
  132         if (ITOV(ip)->v_type == VREG &&
  133             lblktosize(fs, (voff_t)lbn) < round_page(ITOV(ip)->v_size)) {
  134                 struct vm_page *pg;
  135                 struct uvm_object *uobj = &ITOV(ip)->v_uobj;
  136                 voff_t off = trunc_page(lblktosize(fs, lbn));
  137                 voff_t endoff = round_page(lblktosize(fs, lbn) + size);
  138 
  139                 simple_lock(&uobj->vmobjlock);
  140                 while (off < endoff) {
  141                         pg = uvm_pagelookup(uobj, off);
  142                         KASSERT(pg != NULL);
  143                         KASSERT(pg->owner == curproc->p_pid);
  144                         KASSERT((pg->flags & PG_CLEAN) == 0);
  145                         off += PAGE_SIZE;
  146                 }
  147                 simple_unlock(&uobj->vmobjlock);
  148         }
  149 #endif
  150 
  151         *bnp = 0;
  152 #ifdef DIAGNOSTIC
  153         if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
  154                 printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
  155                     ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
  156                 panic("ffs_alloc: bad size");
  157         }
  158         if (cred == NOCRED)
  159                 panic("ffs_alloc: missing credential");
  160 #endif /* DIAGNOSTIC */
  161         if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
  162                 goto nospace;
  163         if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
  164                 goto nospace;
  165 #ifdef QUOTA
  166         if ((error = chkdq(ip, btodb(size), cred, 0)) != 0)
  167                 return (error);
  168 #endif
  169         if (bpref >= fs->fs_size)
  170                 bpref = 0;
  171         if (bpref == 0)
  172                 cg = ino_to_cg(fs, ip->i_number);
  173         else
  174                 cg = dtog(fs, bpref);
  175         bno = ffs_hashalloc(ip, cg, (long)bpref, size,
  176                                      ffs_alloccg);
  177         if (bno > 0) {
  178                 DIP_ADD(ip, blocks, btodb(size));
  179                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
  180                 *bnp = bno;
  181                 return (0);
  182         }
  183 #ifdef QUOTA
  184         /*
  185          * Restore user's disk quota because allocation failed.
  186          */
  187         (void) chkdq(ip, -btodb(size), cred, FORCE);
  188 #endif
  189 nospace:
  190         ffs_fserr(fs, cred->cr_uid, "file system full");
  191         uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
  192         return (ENOSPC);
  193 }
  194 
  195 /*
  196  * Reallocate a fragment to a bigger size
  197  *
  198  * The number and size of the old block is given, and a preference
  199  * and new size is also specified. The allocator attempts to extend
  200  * the original block. Failing that, the regular block allocator is
  201  * invoked to get an appropriate block.
  202  */
  203 int
  204 ffs_realloccg(ip, lbprev, bpref, osize, nsize, cred, bpp, blknop)
  205         struct inode *ip;
  206         daddr_t lbprev;
  207         daddr_t bpref;
  208         int osize, nsize;
  209         struct ucred *cred;
  210         struct buf **bpp;
  211         daddr_t *blknop;
  212 {
  213         struct fs *fs;
  214         struct buf *bp;
  215         int cg, request, error;
  216         daddr_t bprev, bno;
  217 
  218         fs = ip->i_fs;
  219 #ifdef UVM_PAGE_TRKOWN
  220         if (ITOV(ip)->v_type == VREG) {
  221                 struct vm_page *pg;
  222                 struct uvm_object *uobj = &ITOV(ip)->v_uobj;
  223                 voff_t off = trunc_page(lblktosize(fs, lbprev));
  224                 voff_t endoff = round_page(lblktosize(fs, lbprev) + osize);
  225 
  226                 simple_lock(&uobj->vmobjlock);
  227                 while (off < endoff) {
  228                         pg = uvm_pagelookup(uobj, off);
  229                         KASSERT(pg != NULL);
  230                         KASSERT(pg->owner == curproc->p_pid);
  231                         KASSERT((pg->flags & PG_CLEAN) == 0);
  232                         off += PAGE_SIZE;
  233                 }
  234                 simple_unlock(&uobj->vmobjlock);
  235         }
  236 #endif
  237 
  238 #ifdef DIAGNOSTIC
  239         if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
  240             (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
  241                 printf(
  242                     "dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
  243                     ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
  244                 panic("ffs_realloccg: bad size");
  245         }
  246         if (cred == NOCRED)
  247                 panic("ffs_realloccg: missing credential");
  248 #endif /* DIAGNOSTIC */
  249         if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
  250                 goto nospace;
  251         if (fs->fs_magic == FS_UFS2_MAGIC)
  252                 bprev = ufs_rw64(ip->i_ffs2_db[lbprev], UFS_FSNEEDSWAP(fs));
  253         else
  254                 bprev = ufs_rw32(ip->i_ffs1_db[lbprev], UFS_FSNEEDSWAP(fs));
  255 
  256         if (bprev == 0) {
  257                 printf("dev = 0x%x, bsize = %d, bprev = %" PRId64 ", fs = %s\n",
  258                     ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
  259                 panic("ffs_realloccg: bad bprev");
  260         }
  261         /*
  262          * Allocate the extra space in the buffer.
  263          */
  264         if (bpp != NULL &&
  265             (error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp)) != 0) {
  266                 brelse(bp);
  267                 return (error);
  268         }
  269 #ifdef QUOTA
  270         if ((error = chkdq(ip, btodb(nsize - osize), cred, 0)) != 0) {
  271                 if (bpp != NULL) {
  272                         brelse(bp);
  273                 }
  274                 return (error);
  275         }
  276 #endif
  277         /*
  278          * Check for extension in the existing location.
  279          */
  280         cg = dtog(fs, bprev);
  281         if ((bno = ffs_fragextend(ip, cg, bprev, osize, nsize)) != 0) {
  282                 DIP_ADD(ip, blocks, btodb(nsize - osize));
  283                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
  284 
  285                 if (bpp != NULL) {
  286                         if (bp->b_blkno != fsbtodb(fs, bno))
  287                                 panic("bad blockno");
  288                         allocbuf(bp, nsize, 1);
  289                         bp->b_flags |= B_DONE;
  290                         memset(bp->b_data + osize, 0, nsize - osize);
  291                         *bpp = bp;
  292                 }
  293                 if (blknop != NULL) {
  294                         *blknop = bno;
  295                 }
  296                 return (0);
  297         }
  298         /*
  299          * Allocate a new disk location.
  300          */
  301         if (bpref >= fs->fs_size)
  302                 bpref = 0;
  303         switch ((int)fs->fs_optim) {
  304         case FS_OPTSPACE:
  305                 /*
  306                  * Allocate an exact sized fragment. Although this makes 
  307                  * best use of space, we will waste time relocating it if 
  308                  * the file continues to grow. If the fragmentation is
  309                  * less than half of the minimum free reserve, we choose
  310                  * to begin optimizing for time.
  311                  */
  312                 request = nsize;
  313                 if (fs->fs_minfree < 5 ||
  314                     fs->fs_cstotal.cs_nffree >
  315                     fs->fs_dsize * fs->fs_minfree / (2 * 100))
  316                         break;
  317 
  318                 if (ffs_log_changeopt) {
  319                         log(LOG_NOTICE,
  320                                 "%s: optimization changed from SPACE to TIME\n",
  321                                 fs->fs_fsmnt);
  322                 }
  323 
  324                 fs->fs_optim = FS_OPTTIME;
  325                 break;
  326         case FS_OPTTIME:
  327                 /*
  328                  * At this point we have discovered a file that is trying to
  329                  * grow a small fragment to a larger fragment. To save time,
  330                  * we allocate a full sized block, then free the unused portion.
  331                  * If the file continues to grow, the `ffs_fragextend' call
  332                  * above will be able to grow it in place without further
  333                  * copying. If aberrant programs cause disk fragmentation to
  334                  * grow within 2% of the free reserve, we choose to begin
  335                  * optimizing for space.
  336                  */
  337                 request = fs->fs_bsize;
  338                 if (fs->fs_cstotal.cs_nffree <
  339                     fs->fs_dsize * (fs->fs_minfree - 2) / 100)
  340                         break;
  341 
  342                 if (ffs_log_changeopt) {
  343                         log(LOG_NOTICE,
  344                                 "%s: optimization changed from TIME to SPACE\n",
  345                                 fs->fs_fsmnt);
  346                 }
  347 
  348                 fs->fs_optim = FS_OPTSPACE;
  349                 break;
  350         default:
  351                 printf("dev = 0x%x, optim = %d, fs = %s\n",
  352                     ip->i_dev, fs->fs_optim, fs->fs_fsmnt);
  353                 panic("ffs_realloccg: bad optim");
  354                 /* NOTREACHED */
  355         }
  356         bno = ffs_hashalloc(ip, cg, bpref, request, ffs_alloccg);
  357         if (bno > 0) {
  358                 if (!DOINGSOFTDEP(ITOV(ip)))
  359                         ffs_blkfree(ip, bprev, (long)osize);
  360                 if (nsize < request)
  361                         ffs_blkfree(ip, bno + numfrags(fs, nsize),
  362                             (long)(request - nsize));
  363                 DIP_ADD(ip, blocks, btodb(nsize - osize));
  364                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
  365                 if (bpp != NULL) {
  366                         bp->b_blkno = fsbtodb(fs, bno);
  367                         allocbuf(bp, nsize, 1);
  368                         bp->b_flags |= B_DONE;
  369                         memset(bp->b_data + osize, 0, (u_int)nsize - osize);
  370                         *bpp = bp;
  371                 }
  372                 if (blknop != NULL) {
  373                         *blknop = bno;
  374                 }
  375                 return (0);
  376         }
  377 #ifdef QUOTA
  378         /*
  379          * Restore user's disk quota because allocation failed.
  380          */
  381         (void) chkdq(ip, -btodb(nsize - osize), cred, FORCE);
  382 #endif
  383         if (bpp != NULL) {
  384                 brelse(bp);
  385         }
  386 
  387 nospace:
  388         /*
  389          * no space available
  390          */
  391         ffs_fserr(fs, cred->cr_uid, "file system full");
  392         uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
  393         return (ENOSPC);
  394 }
  395 
  396 /*
  397  * Reallocate a sequence of blocks into a contiguous sequence of blocks.
  398  *
  399  * The vnode and an array of buffer pointers for a range of sequential
  400  * logical blocks to be made contiguous is given. The allocator attempts
  401  * to find a range of sequential blocks starting as close as possible
  402  * from the end of the allocation for the logical block immediately
  403  * preceding the current range. If successful, the physical block numbers
  404  * in the buffer pointers and in the inode are changed to reflect the new
  405  * allocation. If unsuccessful, the allocation is left unchanged. The
  406  * success in doing the reallocation is returned. Note that the error
  407  * return is not reflected back to the user. Rather the previous block
  408  * allocation will be used.
  409 
  410  */
  411 #ifdef XXXUBC
  412 #ifdef DEBUG
  413 #include <sys/sysctl.h>
  414 int prtrealloc = 0;
  415 struct ctldebug debug15 = { "prtrealloc", &prtrealloc };
  416 #endif
  417 #endif
  418 
  419 /*
  420  * NOTE: when re-enabling this, it must be updated for UFS2.
  421  */
  422 
  423 int doasyncfree = 1;
  424 
  425 int
  426 ffs_reallocblks(v)
  427         void *v;
  428 {
  429 #ifdef XXXUBC
  430         struct vop_reallocblks_args /* {
  431                 struct vnode *a_vp;
  432                 struct cluster_save *a_buflist;
  433         } */ *ap = v;
  434         struct fs *fs;
  435         struct inode *ip;
  436         struct vnode *vp;
  437         struct buf *sbp, *ebp;
  438         int32_t *bap, *ebap = NULL, *sbap;      /* XXX ondisk32 */
  439         struct cluster_save *buflist;
  440         daddr_t start_lbn, end_lbn, soff, newblk, blkno;
  441         struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
  442         int i, len, start_lvl, end_lvl, pref, ssize;
  443 #endif /* XXXUBC */
  444 
  445         /* XXXUBC don't reallocblks for now */
  446         return ENOSPC;
  447 
  448 #ifdef XXXUBC
  449         vp = ap->a_vp;
  450         ip = VTOI(vp);
  451         fs = ip->i_fs;
  452         if (fs->fs_contigsumsize <= 0)
  453                 return (ENOSPC);
  454         buflist = ap->a_buflist;
  455         len = buflist->bs_nchildren;
  456         start_lbn = buflist->bs_children[0]->b_lblkno;
  457         end_lbn = start_lbn + len - 1;
  458 #ifdef DIAGNOSTIC
  459         for (i = 0; i < len; i++)
  460                 if (!ffs_checkblk(ip,
  461                    dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
  462                         panic("ffs_reallocblks: unallocated block 1");
  463         for (i = 1; i < len; i++)
  464                 if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
  465                         panic("ffs_reallocblks: non-logical cluster");
  466         blkno = buflist->bs_children[0]->b_blkno;
  467         ssize = fsbtodb(fs, fs->fs_frag);
  468         for (i = 1; i < len - 1; i++)
  469                 if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize))
  470                         panic("ffs_reallocblks: non-physical cluster %d", i);
  471 #endif
  472         /*
  473          * If the latest allocation is in a new cylinder group, assume that
  474          * the filesystem has decided to move and do not force it back to
  475          * the previous cylinder group.
  476          */
  477         if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
  478             dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
  479                 return (ENOSPC);
  480         if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
  481             ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
  482                 return (ENOSPC);
  483         /*
  484          * Get the starting offset and block map for the first block.
  485          */
  486         if (start_lvl == 0) {
  487                 sbap = &ip->i_ffs1_db[0];
  488                 soff = start_lbn;
  489         } else {
  490                 idp = &start_ap[start_lvl - 1];
  491                 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
  492                         brelse(sbp);
  493                         return (ENOSPC);
  494                 }
  495                 sbap = (int32_t *)sbp->b_data;
  496                 soff = idp->in_off;
  497         }
  498         /*
  499          * Find the preferred location for the cluster.
  500          */
  501         pref = ffs_blkpref(ip, start_lbn, soff, sbap);
  502         /*
  503          * If the block range spans two block maps, get the second map.
  504          */
  505         if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
  506                 ssize = len;
  507         } else {
  508 #ifdef DIAGNOSTIC
  509                 if (start_ap[start_lvl-1].in_lbn == idp->in_lbn)
  510                         panic("ffs_reallocblk: start == end");
  511 #endif
  512                 ssize = len - (idp->in_off + 1);
  513                 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
  514                         goto fail;
  515                 ebap = (int32_t *)ebp->b_data;  /* XXX ondisk32 */
  516         }
  517         /*
  518          * Search the block map looking for an allocation of the desired size.
  519          */
  520         if ((newblk = (daddr_t)ffs_hashalloc(ip, dtog(fs, pref), (long)pref,
  521             len, ffs_clusteralloc)) == 0)
  522                 goto fail;
  523         /*
  524          * We have found a new contiguous block.
  525          *
  526          * First we have to replace the old block pointers with the new
  527          * block pointers in the inode and indirect blocks associated
  528          * with the file.
  529          */
  530 #ifdef DEBUG
  531         if (prtrealloc)
  532                 printf("realloc: ino %d, lbns %d-%d\n\told:", ip->i_number,
  533                     start_lbn, end_lbn);
  534 #endif
  535         blkno = newblk;
  536         for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
  537                 daddr_t ba;
  538 
  539                 if (i == ssize) {
  540                         bap = ebap;
  541                         soff = -i;
  542                 }
  543                 /* XXX ondisk32 */
  544                 ba = ufs_rw32(*bap, UFS_FSNEEDSWAP(fs));
  545 #ifdef DIAGNOSTIC
  546                 if (!ffs_checkblk(ip,
  547                    dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
  548                         panic("ffs_reallocblks: unallocated block 2");
  549                 if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != ba)
  550                         panic("ffs_reallocblks: alloc mismatch");
  551 #endif
  552 #ifdef DEBUG
  553                 if (prtrealloc)
  554                         printf(" %d,", ba);
  555 #endif
  556                 if (DOINGSOFTDEP(vp)) {
  557                         if (sbap == &ip->i_ffs1_db[0] && i < ssize)
  558                                 softdep_setup_allocdirect(ip, start_lbn + i,
  559                                     blkno, ba, fs->fs_bsize, fs->fs_bsize,
  560                                     buflist->bs_children[i]);
  561                         else
  562                                 softdep_setup_allocindir_page(ip, start_lbn + i,
  563                                     i < ssize ? sbp : ebp, soff + i, blkno,
  564                                     ba, buflist->bs_children[i]);
  565                 }
  566                 /* XXX ondisk32 */
  567                 *bap++ = ufs_rw32((int32_t)blkno, UFS_FSNEEDSWAP(fs));
  568         }
  569         /*
  570          * Next we must write out the modified inode and indirect blocks.
  571          * For strict correctness, the writes should be synchronous since
  572          * the old block values may have been written to disk. In practise
  573          * they are almost never written, but if we are concerned about 
  574          * strict correctness, the `doasyncfree' flag should be set to zero.
  575          *
  576          * The test on `doasyncfree' should be changed to test a flag
  577          * that shows whether the associated buffers and inodes have
  578          * been written. The flag should be set when the cluster is
  579          * started and cleared whenever the buffer or inode is flushed.
  580          * We can then check below to see if it is set, and do the
  581          * synchronous write only when it has been cleared.
  582          */
  583         if (sbap != &ip->i_ffs1_db[0]) {
  584                 if (doasyncfree)
  585                         bdwrite(sbp);
  586                 else
  587                         bwrite(sbp);
  588         } else {
  589                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
  590                 if (!doasyncfree)
  591                         VOP_UPDATE(vp, NULL, NULL, 1);
  592         }
  593         if (ssize < len) {
  594                 if (doasyncfree)
  595                         bdwrite(ebp);
  596                 else
  597                         bwrite(ebp);
  598         }
  599         /*
  600          * Last, free the old blocks and assign the new blocks to the buffers.
  601          */
  602 #ifdef DEBUG
  603         if (prtrealloc)
  604                 printf("\n\tnew:");
  605 #endif
  606         for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
  607                 if (!DOINGSOFTDEP(vp))
  608                         ffs_blkfree(ip,
  609                             dbtofsb(fs, buflist->bs_children[i]->b_blkno),
  610                             fs->fs_bsize);
  611                 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
  612 #ifdef DEBUG
  613                 if (!ffs_checkblk(ip,
  614                    dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
  615                         panic("ffs_reallocblks: unallocated block 3");
  616                 if (prtrealloc)
  617                         printf(" %d,", blkno);
  618 #endif
  619         }
  620 #ifdef DEBUG
  621         if (prtrealloc) {
  622                 prtrealloc--;
  623                 printf("\n");
  624         }
  625 #endif
  626         return (0);
  627 
  628 fail:
  629         if (ssize < len)
  630                 brelse(ebp);
  631         if (sbap != &ip->i_ffs1_db[0])
  632                 brelse(sbp);
  633         return (ENOSPC);
  634 #endif /* XXXUBC */
  635 }
  636 
  637 /*
  638  * Allocate an inode in the file system.
  639  * 
  640  * If allocating a directory, use ffs_dirpref to select the inode.
  641  * If allocating in a directory, the following hierarchy is followed:
  642  *   1) allocate the preferred inode.
  643  *   2) allocate an inode in the same cylinder group.
  644  *   3) quadradically rehash into other cylinder groups, until an
  645  *      available inode is located.
  646  * If no inode preference is given the following hierarchy is used
  647  * to allocate an inode:
  648  *   1) allocate an inode in cylinder group 0.
  649  *   2) quadradically rehash into other cylinder groups, until an
  650  *      available inode is located.
  651  */
  652 int
  653 ffs_valloc(v)
  654         void *v;
  655 {
  656         struct vop_valloc_args /* {
  657                 struct vnode *a_pvp;
  658                 int a_mode;
  659                 struct ucred *a_cred;
  660                 struct vnode **a_vpp;
  661         } */ *ap = v;
  662         struct vnode *pvp = ap->a_pvp;
  663         struct inode *pip;
  664         struct fs *fs;
  665         struct inode *ip;
  666         struct timespec ts;
  667         mode_t mode = ap->a_mode;
  668         ino_t ino, ipref;
  669         int cg, error;
  670         
  671         *ap->a_vpp = NULL;
  672         pip = VTOI(pvp);
  673         fs = pip->i_fs;
  674         if (fs->fs_cstotal.cs_nifree == 0)
  675                 goto noinodes;
  676 
  677         if ((mode & IFMT) == IFDIR)
  678                 ipref = ffs_dirpref(pip);
  679         else
  680                 ipref = pip->i_number;
  681         if (ipref >= fs->fs_ncg * fs->fs_ipg)
  682                 ipref = 0;
  683         cg = ino_to_cg(fs, ipref);
  684         /*
  685          * Track number of dirs created one after another
  686          * in a same cg without intervening by files.
  687          */
  688         if ((mode & IFMT) == IFDIR) {
  689                 if (fs->fs_contigdirs[cg] < 255)
  690                         fs->fs_contigdirs[cg]++;
  691         } else {
  692                 if (fs->fs_contigdirs[cg] > 0)
  693                         fs->fs_contigdirs[cg]--;
  694         }
  695         ino = (ino_t)ffs_hashalloc(pip, cg, ipref, mode, ffs_nodealloccg);
  696         if (ino == 0)
  697                 goto noinodes;
  698         error = VFS_VGET(pvp->v_mount, ino, ap->a_vpp);
  699         if (error) {
  700                 VOP_VFREE(pvp, ino, mode);
  701                 return (error);
  702         }
  703         ip = VTOI(*ap->a_vpp);
  704         if (ip->i_mode) {
  705 #if 0
  706                 printf("mode = 0%o, inum = %d, fs = %s\n",
  707                     ip->i_mode, ip->i_number, fs->fs_fsmnt);
  708 #else
  709                 printf("dmode %x mode %x dgen %x gen %x\n",
  710                     DIP(ip, mode), ip->i_mode,
  711                     DIP(ip, gen), ip->i_gen);
  712                 printf("size %llx blocks %llx\n",
  713                     (long long)DIP(ip, size), (long long)DIP(ip, blocks));
  714                 printf("ino %u ipref %u\n", ino, ipref);
  715 #if 0
  716                 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
  717                     (int)fs->fs_bsize, NOCRED, &bp);
  718 #endif
  719 
  720 #endif
  721                 panic("ffs_valloc: dup alloc");
  722         }
  723         if (DIP(ip, blocks)) {                          /* XXX */
  724                 printf("free inode %s/%d had %" PRId64 " blocks\n",
  725                     fs->fs_fsmnt, ino, DIP(ip, blocks));
  726                 DIP_ASSIGN(ip, blocks, 0);
  727         }
  728         ip->i_flag &= ~IN_SPACECOUNTED;
  729         ip->i_flags = 0;
  730         DIP_ASSIGN(ip, flags, 0);
  731         /*
  732          * Set up a new generation number for this inode.
  733          */
  734         ip->i_gen++;
  735         DIP_ASSIGN(ip, gen, ip->i_gen);
  736         if (fs->fs_magic == FS_UFS2_MAGIC) {
  737                 TIMEVAL_TO_TIMESPEC(&time, &ts);
  738                 ip->i_ffs2_birthtime = ts.tv_sec;
  739                 ip->i_ffs2_birthnsec = ts.tv_nsec;
  740         }
  741         return (0);
  742 noinodes:
  743         ffs_fserr(fs, ap->a_cred->cr_uid, "out of inodes");
  744         uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
  745         return (ENOSPC);
  746 }
  747 
  748 /*
  749  * Find a cylinder group in which to place a directory.
  750  *
  751  * The policy implemented by this algorithm is to allocate a
  752  * directory inode in the same cylinder group as its parent
  753  * directory, but also to reserve space for its files inodes
  754  * and data. Restrict the number of directories which may be
  755  * allocated one after another in the same cylinder group
  756  * without intervening allocation of files.
  757  *
  758  * If we allocate a first level directory then force allocation
  759  * in another cylinder group.
  760  */
  761 static ino_t
  762 ffs_dirpref(pip)
  763         struct inode *pip;
  764 {
  765         register struct fs *fs;
  766         int cg, prefcg;
  767         int64_t dirsize, cgsize;
  768         int avgifree, avgbfree, avgndir, curdirsize;
  769         int minifree, minbfree, maxndir;
  770         int mincg, minndir;
  771         int maxcontigdirs;
  772 
  773         fs = pip->i_fs;
  774 
  775         avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
  776         avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
  777         avgndir = fs->fs_cstotal.cs_ndir / fs->fs_ncg;
  778 
  779         /*
  780          * Force allocation in another cg if creating a first level dir.
  781          */
  782         if (ITOV(pip)->v_flag & VROOT) {
  783                 prefcg = random() % fs->fs_ncg;
  784                 mincg = prefcg;
  785                 minndir = fs->fs_ipg;
  786                 for (cg = prefcg; cg < fs->fs_ncg; cg++)
  787                         if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
  788                             fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
  789                             fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
  790                                 mincg = cg;
  791                                 minndir = fs->fs_cs(fs, cg).cs_ndir;
  792                         }
  793                 for (cg = 0; cg < prefcg; cg++)
  794                         if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
  795                             fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
  796                             fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
  797                                 mincg = cg;
  798                                 minndir = fs->fs_cs(fs, cg).cs_ndir;
  799                         }
  800                 return ((ino_t)(fs->fs_ipg * mincg));
  801         }
  802 
  803         /*
  804          * Count various limits which used for
  805          * optimal allocation of a directory inode.
  806          */
  807         maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg);
  808         minifree = avgifree - fs->fs_ipg / 4;
  809         if (minifree < 0)
  810                 minifree = 0;
  811         minbfree = avgbfree - fragstoblks(fs, fs->fs_fpg) / 4;
  812         if (minbfree < 0)
  813                 minbfree = 0;
  814         cgsize = fs->fs_fsize * fs->fs_fpg;
  815         dirsize = fs->fs_avgfilesize * fs->fs_avgfpdir;
  816         curdirsize = avgndir ? (cgsize - avgbfree * fs->fs_bsize) / avgndir : 0;
  817         if (dirsize < curdirsize)
  818                 dirsize = curdirsize;
  819         maxcontigdirs = min(cgsize / dirsize, 255);
  820         if (fs->fs_avgfpdir > 0)
  821                 maxcontigdirs = min(maxcontigdirs,
  822                                     fs->fs_ipg / fs->fs_avgfpdir);
  823         if (maxcontigdirs == 0)
  824                 maxcontigdirs = 1;
  825 
  826         /*
  827          * Limit number of dirs in one cg and reserve space for 
  828          * regular files, but only if we have no deficit in
  829          * inodes or space.
  830          */
  831         prefcg = ino_to_cg(fs, pip->i_number);
  832         for (cg = prefcg; cg < fs->fs_ncg; cg++)
  833                 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
  834                     fs->fs_cs(fs, cg).cs_nifree >= minifree &&
  835                     fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
  836                         if (fs->fs_contigdirs[cg] < maxcontigdirs)
  837                                 return ((ino_t)(fs->fs_ipg * cg));
  838                 }
  839         for (cg = 0; cg < prefcg; cg++)
  840                 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
  841                     fs->fs_cs(fs, cg).cs_nifree >= minifree &&
  842                     fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
  843                         if (fs->fs_contigdirs[cg] < maxcontigdirs)
  844                                 return ((ino_t)(fs->fs_ipg * cg));
  845                 }
  846         /*
  847          * This is a backstop when we are deficient in space.
  848          */
  849         for (cg = prefcg; cg < fs->fs_ncg; cg++)
  850                 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
  851                         return ((ino_t)(fs->fs_ipg * cg));
  852         for (cg = 0; cg < prefcg; cg++)
  853                 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
  854                         break;
  855         return ((ino_t)(fs->fs_ipg * cg));
  856 }
  857 
  858 /*
  859  * Select the desired position for the next block in a file.  The file is
  860  * logically divided into sections. The first section is composed of the
  861  * direct blocks. Each additional section contains fs_maxbpg blocks.
  862  * 
  863  * If no blocks have been allocated in the first section, the policy is to
  864  * request a block in the same cylinder group as the inode that describes
  865  * the file. If no blocks have been allocated in any other section, the
  866  * policy is to place the section in a cylinder group with a greater than
  867  * average number of free blocks.  An appropriate cylinder group is found
  868  * by using a rotor that sweeps the cylinder groups. When a new group of
  869  * blocks is needed, the sweep begins in the cylinder group following the
  870  * cylinder group from which the previous allocation was made. The sweep
  871  * continues until a cylinder group with greater than the average number
  872  * of free blocks is found. If the allocation is for the first block in an
  873  * indirect block, the information on the previous allocation is unavailable;
  874  * here a best guess is made based upon the logical block number being
  875  * allocated.
  876  * 
  877  * If a section is already partially allocated, the policy is to
  878  * contiguously allocate fs_maxcontig blocks.  The end of one of these
  879  * contiguous blocks and the beginning of the next is laid out
  880  * contigously if possible.
  881  */
  882 daddr_t
  883 ffs_blkpref_ufs1(ip, lbn, indx, bap)
  884         struct inode *ip;
  885         daddr_t lbn;
  886         int indx;
  887         int32_t *bap;   /* XXX ondisk32 */
  888 {
  889         struct fs *fs;
  890         int cg;
  891         int avgbfree, startcg;
  892 
  893         fs = ip->i_fs;
  894         if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
  895                 if (lbn < NDADDR + NINDIR(fs)) {
  896                         cg = ino_to_cg(fs, ip->i_number);
  897                         return (fs->fs_fpg * cg + fs->fs_frag);
  898                 }
  899                 /*
  900                  * Find a cylinder with greater than average number of
  901                  * unused data blocks.
  902                  */
  903                 if (indx == 0 || bap[indx - 1] == 0)
  904                         startcg =
  905                             ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
  906                 else
  907                         startcg = dtog(fs,
  908                                 ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1);
  909                 startcg %= fs->fs_ncg;
  910                 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
  911                 for (cg = startcg; cg < fs->fs_ncg; cg++)
  912                         if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
  913                                 return (fs->fs_fpg * cg + fs->fs_frag);
  914                         }
  915                 for (cg = 0; cg < startcg; cg++)
  916                         if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
  917                                 return (fs->fs_fpg * cg + fs->fs_frag);
  918                         }
  919                 return (0);
  920         }
  921         /*
  922          * We just always try to lay things out contiguously.
  923          */
  924         return ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
  925 }
  926 
  927 daddr_t
  928 ffs_blkpref_ufs2(ip, lbn, indx, bap)
  929         struct inode *ip;
  930         daddr_t lbn;
  931         int indx;
  932         int64_t *bap;
  933 {
  934         struct fs *fs;
  935         int cg;
  936         int avgbfree, startcg;
  937 
  938         fs = ip->i_fs;
  939         if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
  940                 if (lbn < NDADDR + NINDIR(fs)) {
  941                         cg = ino_to_cg(fs, ip->i_number);
  942                         return (fs->fs_fpg * cg + fs->fs_frag);
  943                 }
  944                 /*
  945                  * Find a cylinder with greater than average number of
  946                  * unused data blocks.
  947                  */
  948                 if (indx == 0 || bap[indx - 1] == 0)
  949                         startcg =
  950                             ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
  951                 else
  952                         startcg = dtog(fs,
  953                                 ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1);
  954                 startcg %= fs->fs_ncg;
  955                 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
  956                 for (cg = startcg; cg < fs->fs_ncg; cg++)
  957                         if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
  958                                 return (fs->fs_fpg * cg + fs->fs_frag);
  959                         }
  960                 for (cg = 0; cg < startcg; cg++)
  961                         if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
  962                                 return (fs->fs_fpg * cg + fs->fs_frag);
  963                         }
  964                 return (0);
  965         }
  966         /*
  967          * We just always try to lay things out contiguously.
  968          */
  969         return ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
  970 }
  971 
  972 
  973 /*
  974  * Implement the cylinder overflow algorithm.
  975  *
  976  * The policy implemented by this algorithm is:
  977  *   1) allocate the block in its requested cylinder group.
  978  *   2) quadradically rehash on the cylinder group number.
  979  *   3) brute force search for a free block.
  980  */
  981 /*VARARGS5*/
  982 static daddr_t
  983 ffs_hashalloc(ip, cg, pref, size, allocator)
  984         struct inode *ip;
  985         int cg;
  986         daddr_t pref;
  987         int size;       /* size for data blocks, mode for inodes */
  988         daddr_t (*allocator) __P((struct inode *, int, daddr_t, int));
  989 {
  990         struct fs *fs;
  991         daddr_t result;
  992         int i, icg = cg;
  993 
  994         fs = ip->i_fs;
  995         /*
  996          * 1: preferred cylinder group
  997          */
  998         result = (*allocator)(ip, cg, pref, size);
  999         if (result)
 1000                 return (result);
 1001         /*
 1002          * 2: quadratic rehash
 1003          */
 1004         for (i = 1; i < fs->fs_ncg; i *= 2) {
 1005                 cg += i;
 1006                 if (cg >= fs->fs_ncg)
 1007                         cg -= fs->fs_ncg;
 1008                 result = (*allocator)(ip, cg, 0, size);
 1009                 if (result)
 1010                         return (result);
 1011         }
 1012         /*
 1013          * 3: brute force search
 1014          * Note that we start at i == 2, since 0 was checked initially,
 1015          * and 1 is always checked in the quadratic rehash.
 1016          */
 1017         cg = (icg + 2) % fs->fs_ncg;
 1018         for (i = 2; i < fs->fs_ncg; i++) {
 1019                 result = (*allocator)(ip, cg, 0, size);
 1020                 if (result)
 1021                         return (result);
 1022                 cg++;
 1023                 if (cg == fs->fs_ncg)
 1024                         cg = 0;
 1025         }
 1026         return (0);
 1027 }
 1028 
 1029 /*
 1030  * Determine whether a fragment can be extended.
 1031  *
 1032  * Check to see if the necessary fragments are available, and 
 1033  * if they are, allocate them.
 1034  */
 1035 static daddr_t
 1036 ffs_fragextend(ip, cg, bprev, osize, nsize)
 1037         struct inode *ip;
 1038         int cg;
 1039         daddr_t bprev;
 1040         int osize, nsize;
 1041 {
 1042         struct fs *fs;
 1043         struct cg *cgp;
 1044         struct buf *bp;
 1045         daddr_t bno;
 1046         int frags, bbase;
 1047         int i, error;
 1048         u_int8_t *blksfree;
 1049 
 1050         fs = ip->i_fs;
 1051         if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
 1052                 return (0);
 1053         frags = numfrags(fs, nsize);
 1054         bbase = fragnum(fs, bprev);
 1055         if (bbase > fragnum(fs, (bprev + frags - 1))) {
 1056                 /* cannot extend across a block boundary */
 1057                 return (0);
 1058         }
 1059         error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
 1060                 (int)fs->fs_cgsize, NOCRED, &bp);
 1061         if (error) {
 1062                 brelse(bp);
 1063                 return (0);
 1064         }
 1065         cgp = (struct cg *)bp->b_data;
 1066         if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs))) {
 1067                 brelse(bp);
 1068                 return (0);
 1069         }
 1070         cgp->cg_old_time = ufs_rw32(time.tv_sec, UFS_FSNEEDSWAP(fs));
 1071         if ((fs->fs_magic != FS_UFS1_MAGIC) ||
 1072             (fs->fs_old_flags & FS_FLAGS_UPDATED))
 1073                 cgp->cg_time = ufs_rw64(time.tv_sec, UFS_FSNEEDSWAP(fs));
 1074         bno = dtogd(fs, bprev);
 1075         blksfree = cg_blksfree(cgp, UFS_FSNEEDSWAP(fs));
 1076         for (i = numfrags(fs, osize); i < frags; i++)
 1077                 if (isclr(blksfree, bno + i)) {
 1078                         brelse(bp);
 1079                         return (0);
 1080                 }
 1081         /*
 1082          * the current fragment can be extended
 1083          * deduct the count on fragment being extended into
 1084          * increase the count on the remaining fragment (if any)
 1085          * allocate the extended piece
 1086          */
 1087         for (i = frags; i < fs->fs_frag - bbase; i++)
 1088                 if (isclr(blksfree, bno + i))
 1089                         break;
 1090         ufs_add32(cgp->cg_frsum[i - numfrags(fs, osize)], -1, UFS_FSNEEDSWAP(fs));
 1091         if (i != frags)
 1092                 ufs_add32(cgp->cg_frsum[i - frags], 1, UFS_FSNEEDSWAP(fs));
 1093         for (i = numfrags(fs, osize); i < frags; i++) {
 1094                 clrbit(blksfree, bno + i);
 1095                 ufs_add32(cgp->cg_cs.cs_nffree, -1, UFS_FSNEEDSWAP(fs));
 1096                 fs->fs_cstotal.cs_nffree--;
 1097                 fs->fs_cs(fs, cg).cs_nffree--;
 1098         }
 1099         fs->fs_fmod = 1;
 1100         if (DOINGSOFTDEP(ITOV(ip)))
 1101                 softdep_setup_blkmapdep(bp, fs, bprev);
 1102         bdwrite(bp);
 1103         return (bprev);
 1104 }
 1105 
 1106 /*
 1107  * Determine whether a block can be allocated.
 1108  *
 1109  * Check to see if a block of the appropriate size is available,
 1110  * and if it is, allocate it.
 1111  */
 1112 static daddr_t
 1113 ffs_alloccg(ip, cg, bpref, size)
 1114         struct inode *ip;
 1115         int cg;
 1116         daddr_t bpref;
 1117         int size;
 1118 {
 1119         struct fs *fs = ip->i_fs;
 1120         struct cg *cgp;
 1121         struct buf *bp;
 1122         int32_t bno;
 1123         daddr_t blkno;
 1124         int error, frags, allocsiz, i;
 1125         u_int8_t *blksfree;
 1126 #ifdef FFS_EI
 1127         const int needswap = UFS_FSNEEDSWAP(fs);
 1128 #endif
 1129 
 1130         if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
 1131                 return (0);
 1132         error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
 1133                 (int)fs->fs_cgsize, NOCRED, &bp);
 1134         if (error) {
 1135                 brelse(bp);
 1136                 return (0);
 1137         }
 1138         cgp = (struct cg *)bp->b_data;
 1139         if (!cg_chkmagic(cgp, needswap) ||
 1140             (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
 1141                 brelse(bp);
 1142                 return (0);
 1143         }
 1144         cgp->cg_old_time = ufs_rw32(time.tv_sec, needswap);
 1145         if ((fs->fs_magic != FS_UFS1_MAGIC) ||
 1146             (fs->fs_old_flags & FS_FLAGS_UPDATED))
 1147                 cgp->cg_time = ufs_rw64(time.tv_sec, needswap);
 1148         if (size == fs->fs_bsize) {
 1149                 blkno = ffs_alloccgblk(ip, bp, bpref);
 1150                 bdwrite(bp);
 1151                 return (blkno);
 1152         }
 1153         /*
 1154          * check to see if any fragments are already available
 1155          * allocsiz is the size which will be allocated, hacking
 1156          * it down to a smaller size if necessary
 1157          */
 1158         blksfree = cg_blksfree(cgp, needswap);
 1159         frags = numfrags(fs, size);
 1160         for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
 1161                 if (cgp->cg_frsum[allocsiz] != 0)
 1162                         break;
 1163         if (allocsiz == fs->fs_frag) {
 1164                 /*
 1165                  * no fragments were available, so a block will be 
 1166                  * allocated, and hacked up
 1167                  */
 1168                 if (cgp->cg_cs.cs_nbfree == 0) {
 1169                         brelse(bp);
 1170                         return (0);
 1171                 }
 1172                 blkno = ffs_alloccgblk(ip, bp, bpref);
 1173                 bno = dtogd(fs, blkno);
 1174                 for (i = frags; i < fs->fs_frag; i++)
 1175                         setbit(blksfree, bno + i);
 1176                 i = fs->fs_frag - frags;
 1177                 ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
 1178                 fs->fs_cstotal.cs_nffree += i;
 1179                 fs->fs_cs(fs, cg).cs_nffree += i;
 1180                 fs->fs_fmod = 1;
 1181                 ufs_add32(cgp->cg_frsum[i], 1, needswap);
 1182                 bdwrite(bp);
 1183                 return (blkno);
 1184         }
 1185         bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
 1186 #if 0
 1187         /*
 1188          * XXX fvdl mapsearch will panic, and never return -1
 1189          *          also: returning NULL as daddr_t ?
 1190          */
 1191         if (bno < 0) {
 1192                 brelse(bp);
 1193                 return (0);
 1194         }
 1195 #endif
 1196         for (i = 0; i < frags; i++)
 1197                 clrbit(blksfree, bno + i);
 1198         ufs_add32(cgp->cg_cs.cs_nffree, -frags, needswap);
 1199         fs->fs_cstotal.cs_nffree -= frags;
 1200         fs->fs_cs(fs, cg).cs_nffree -= frags;
 1201         fs->fs_fmod = 1;
 1202         ufs_add32(cgp->cg_frsum[allocsiz], -1, needswap);
 1203         if (frags != allocsiz)
 1204                 ufs_add32(cgp->cg_frsum[allocsiz - frags], 1, needswap);
 1205         blkno = cg * fs->fs_fpg + bno;
 1206         if (DOINGSOFTDEP(ITOV(ip)))
 1207                 softdep_setup_blkmapdep(bp, fs, blkno);
 1208         bdwrite(bp);
 1209         return blkno;
 1210 }
 1211 
 1212 /*
 1213  * Allocate a block in a cylinder group.
 1214  *
 1215  * This algorithm implements the following policy:
 1216  *   1) allocate the requested block.
 1217  *   2) allocate a rotationally optimal block in the same cylinder.
 1218  *   3) allocate the next available block on the block rotor for the
 1219  *      specified cylinder group.
 1220  * Note that this routine only allocates fs_bsize blocks; these
 1221  * blocks may be fragmented by the routine that allocates them.
 1222  */
 1223 static daddr_t
 1224 ffs_alloccgblk(ip, bp, bpref)
 1225         struct inode *ip;
 1226         struct buf *bp;
 1227         daddr_t bpref;
 1228 {
 1229         struct fs *fs = ip->i_fs;
 1230         struct cg *cgp;
 1231         daddr_t blkno;
 1232         int32_t bno;
 1233         u_int8_t *blksfree;
 1234 #ifdef FFS_EI
 1235         const int needswap = UFS_FSNEEDSWAP(fs);
 1236 #endif
 1237 
 1238         cgp = (struct cg *)bp->b_data;
 1239         blksfree = cg_blksfree(cgp, needswap);
 1240         if (bpref == 0 || dtog(fs, bpref) != ufs_rw32(cgp->cg_cgx, needswap)) {
 1241                 bpref = ufs_rw32(cgp->cg_rotor, needswap);
 1242         } else {
 1243                 bpref = blknum(fs, bpref);
 1244                 bno = dtogd(fs, bpref);
 1245                 /*
 1246                  * if the requested block is available, use it
 1247                  */
 1248                 if (ffs_isblock(fs, blksfree, fragstoblks(fs, bno)))
 1249                         goto gotit;
 1250         }
 1251         /*
 1252          * Take the next available block in this cylinder group.
 1253          */
 1254         bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
 1255         if (bno < 0)
 1256                 return (0);
 1257         cgp->cg_rotor = ufs_rw32(bno, needswap);
 1258 gotit:
 1259         blkno = fragstoblks(fs, bno);
 1260         ffs_clrblock(fs, blksfree, blkno);
 1261         ffs_clusteracct(fs, cgp, blkno, -1);
 1262         ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap);
 1263         fs->fs_cstotal.cs_nbfree--;
 1264         fs->fs_cs(fs, ufs_rw32(cgp->cg_cgx, needswap)).cs_nbfree--;
 1265         if ((fs->fs_magic == FS_UFS1_MAGIC) &&
 1266             ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0)) {
 1267                 int cylno;
 1268                 cylno = old_cbtocylno(fs, bno);
 1269                 KASSERT(cylno >= 0);
 1270                 KASSERT(cylno < fs->fs_old_ncyl);
 1271                 KASSERT(old_cbtorpos(fs, bno) >= 0);
 1272                 KASSERT(fs->fs_old_nrpos == 0 || old_cbtorpos(fs, bno) < fs->fs_old_nrpos);
 1273                 ufs_add16(old_cg_blks(fs, cgp, cylno, needswap)[old_cbtorpos(fs, bno)], -1,
 1274                     needswap);
 1275                 ufs_add32(old_cg_blktot(cgp, needswap)[cylno], -1, needswap);
 1276         }
 1277         fs->fs_fmod = 1;
 1278         blkno = ufs_rw32(cgp->cg_cgx, needswap) * fs->fs_fpg + bno;
 1279         if (DOINGSOFTDEP(ITOV(ip)))
 1280                 softdep_setup_blkmapdep(bp, fs, blkno);
 1281         return (blkno);
 1282 }
 1283 
 1284 #ifdef XXXUBC
 1285 /*
 1286  * Determine whether a cluster can be allocated.
 1287  *
 1288  * We do not currently check for optimal rotational layout if there
 1289  * are multiple choices in the same cylinder group. Instead we just
 1290  * take the first one that we find following bpref.
 1291  */
 1292 
 1293 /*
 1294  * This function must be fixed for UFS2 if re-enabled.
 1295  */
 1296 static daddr_t
 1297 ffs_clusteralloc(ip, cg, bpref, len)
 1298         struct inode *ip;
 1299         int cg;
 1300         daddr_t bpref;
 1301         int len;
 1302 {
 1303         struct fs *fs;
 1304         struct cg *cgp;
 1305         struct buf *bp;
 1306         int i, got, run, bno, bit, map;
 1307         u_char *mapp;
 1308         int32_t *lp;
 1309 
 1310         fs = ip->i_fs;
 1311         if (fs->fs_maxcluster[cg] < len)
 1312                 return (0);
 1313         if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
 1314             NOCRED, &bp))
 1315                 goto fail;
 1316         cgp = (struct cg *)bp->b_data;
 1317         if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs)))
 1318                 goto fail;
 1319         /*
 1320          * Check to see if a cluster of the needed size (or bigger) is
 1321          * available in this cylinder group.
 1322          */
 1323         lp = &cg_clustersum(cgp, UFS_FSNEEDSWAP(fs))[len];
 1324         for (i = len; i <= fs->fs_contigsumsize; i++)
 1325                 if (ufs_rw32(*lp++, UFS_FSNEEDSWAP(fs)) > 0)
 1326                         break;
 1327         if (i > fs->fs_contigsumsize) {
 1328                 /*
 1329                  * This is the first time looking for a cluster in this
 1330                  * cylinder group. Update the cluster summary information
 1331                  * to reflect the true maximum sized cluster so that
 1332                  * future cluster allocation requests can avoid reading
 1333                  * the cylinder group map only to find no clusters.
 1334                  */
 1335                 lp = &cg_clustersum(cgp, UFS_FSNEEDSWAP(fs))[len - 1];
 1336                 for (i = len - 1; i > 0; i--)
 1337                         if (ufs_rw32(*lp--, UFS_FSNEEDSWAP(fs)) > 0)
 1338                                 break;
 1339                 fs->fs_maxcluster[cg] = i;
 1340                 goto fail;
 1341         }
 1342         /*
 1343          * Search the cluster map to find a big enough cluster.
 1344          * We take the first one that we find, even if it is larger
 1345          * than we need as we prefer to get one close to the previous
 1346          * block allocation. We do not search before the current
 1347          * preference point as we do not want to allocate a block
 1348          * that is allocated before the previous one (as we will
 1349          * then have to wait for another pass of the elevator
 1350          * algorithm before it will be read). We prefer to fail and
 1351          * be recalled to try an allocation in the next cylinder group.
 1352          */
 1353         if (dtog(fs, bpref) != cg)
 1354                 bpref = 0;
 1355         else
 1356                 bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref)));
 1357         mapp = &cg_clustersfree(cgp, UFS_FSNEEDSWAP(fs))[bpref / NBBY];
 1358         map = *mapp++;
 1359         bit = 1 << (bpref % NBBY);
 1360         for (run = 0, got = bpref;
 1361                 got < ufs_rw32(cgp->cg_nclusterblks, UFS_FSNEEDSWAP(fs)); got++) {
 1362                 if ((map & bit) == 0) {
 1363                         run = 0;
 1364                 } else {
 1365                         run++;
 1366                         if (run == len)
 1367                                 break;
 1368                 }
 1369                 if ((got & (NBBY - 1)) != (NBBY - 1)) {
 1370                         bit <<= 1;
 1371                 } else {
 1372                         map = *mapp++;
 1373                         bit = 1;
 1374                 }
 1375         }
 1376         if (got == ufs_rw32(cgp->cg_nclusterblks, UFS_FSNEEDSWAP(fs)))
 1377                 goto fail;
 1378         /*
 1379          * Allocate the cluster that we have found.
 1380          */
 1381 #ifdef DIAGNOSTIC
 1382         for (i = 1; i <= len; i++)
 1383                 if (!ffs_isblock(fs, cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)),
 1384                     got - run + i))
 1385                         panic("ffs_clusteralloc: map mismatch");
 1386 #endif
 1387         bno = cg * fs->fs_fpg + blkstofrags(fs, got - run + 1);
 1388         if (dtog(fs, bno) != cg)
 1389                 panic("ffs_clusteralloc: allocated out of group");
 1390         len = blkstofrags(fs, len);
 1391         for (i = 0; i < len; i += fs->fs_frag)
 1392                 if ((got = ffs_alloccgblk(ip, bp, bno + i)) != bno + i)
 1393                         panic("ffs_clusteralloc: lost block");
 1394         bdwrite(bp);
 1395         return (bno);
 1396 
 1397 fail:
 1398         brelse(bp);
 1399         return (0);
 1400 }
 1401 #endif /* XXXUBC */
 1402 
 1403 /*
 1404  * Determine whether an inode can be allocated.
 1405  *
 1406  * Check to see if an inode is available, and if it is,
 1407  * allocate it using the following policy:
 1408  *   1) allocate the requested inode.
 1409  *   2) allocate the next available inode after the requested
 1410  *      inode in the specified cylinder group.
 1411  */
 1412 static daddr_t
 1413 ffs_nodealloccg(ip, cg, ipref, mode)
 1414         struct inode *ip;
 1415         int cg;
 1416         daddr_t ipref;
 1417         int mode;
 1418 {
 1419         struct fs *fs = ip->i_fs;
 1420         struct cg *cgp;
 1421         struct buf *bp, *ibp;
 1422         u_int8_t *inosused;
 1423         int error, start, len, loc, map, i;
 1424         int32_t initediblk;
 1425         struct ufs2_dinode *dp2;
 1426 #ifdef FFS_EI
 1427         const int needswap = UFS_FSNEEDSWAP(fs);
 1428 #endif
 1429 
 1430         if (fs->fs_cs(fs, cg).cs_nifree == 0)
 1431                 return (0);
 1432         error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
 1433                 (int)fs->fs_cgsize, NOCRED, &bp);
 1434         if (error) {
 1435                 brelse(bp);
 1436                 return (0);
 1437         }
 1438         cgp = (struct cg *)bp->b_data;
 1439         if (!cg_chkmagic(cgp, needswap) || cgp->cg_cs.cs_nifree == 0) {
 1440                 brelse(bp);
 1441                 return (0);
 1442         }
 1443         cgp->cg_old_time = ufs_rw32(time.tv_sec, needswap);
 1444         if ((fs->fs_magic != FS_UFS1_MAGIC) ||
 1445             (fs->fs_old_flags & FS_FLAGS_UPDATED))
 1446                 cgp->cg_time = ufs_rw64(time.tv_sec, needswap);
 1447         inosused = cg_inosused(cgp, needswap);
 1448         if (ipref) {
 1449                 ipref %= fs->fs_ipg;
 1450                 if (isclr(inosused, ipref))
 1451                         goto gotit;
 1452         }
 1453         start = ufs_rw32(cgp->cg_irotor, needswap) / NBBY;
 1454         len = howmany(fs->fs_ipg - ufs_rw32(cgp->cg_irotor, needswap),
 1455                 NBBY);
 1456         loc = skpc(0xff, len, &inosused[start]);
 1457         if (loc == 0) {
 1458                 len = start + 1;
 1459                 start = 0;
 1460                 loc = skpc(0xff, len, &inosused[0]);
 1461                 if (loc == 0) {
 1462                         printf("cg = %d, irotor = %d, fs = %s\n",
 1463                             cg, ufs_rw32(cgp->cg_irotor, needswap),
 1464                                 fs->fs_fsmnt);
 1465                         panic("ffs_nodealloccg: map corrupted");
 1466                         /* NOTREACHED */
 1467                 }
 1468         }
 1469         i = start + len - loc;
 1470         map = inosused[i];
 1471         ipref = i * NBBY;
 1472         for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
 1473                 if ((map & i) == 0) {
 1474                         cgp->cg_irotor = ufs_rw32(ipref, needswap);
 1475                         goto gotit;
 1476                 }
 1477         }
 1478         printf("fs = %s\n", fs->fs_fsmnt);
 1479         panic("ffs_nodealloccg: block not in map");
 1480         /* NOTREACHED */
 1481 gotit:
 1482         if (DOINGSOFTDEP(ITOV(ip)))
 1483                 softdep_setup_inomapdep(bp, ip, cg * fs->fs_ipg + ipref);
 1484         setbit(inosused, ipref);
 1485         ufs_add32(cgp->cg_cs.cs_nifree, -1, needswap);
 1486         fs->fs_cstotal.cs_nifree--;
 1487         fs->fs_cs(fs, cg).cs_nifree--;
 1488         fs->fs_fmod = 1;
 1489         if ((mode & IFMT) == IFDIR) {
 1490                 ufs_add32(cgp->cg_cs.cs_ndir, 1, needswap);
 1491                 fs->fs_cstotal.cs_ndir++;
 1492                 fs->fs_cs(fs, cg).cs_ndir++;
 1493         }
 1494         /*
 1495          * Check to see if we need to initialize more inodes.
 1496          */
 1497         initediblk = ufs_rw32(cgp->cg_initediblk, needswap);
 1498         if (fs->fs_magic == FS_UFS2_MAGIC &&
 1499             ipref + INOPB(fs) > initediblk &&
 1500             initediblk < ufs_rw32(cgp->cg_niblk, needswap)) {
 1501                 ibp = getblk(ip->i_devvp, fsbtodb(fs,
 1502                     ino_to_fsba(fs, cg * fs->fs_ipg + initediblk)),
 1503                     (int)fs->fs_bsize, 0, 0);
 1504                     memset(ibp->b_data, 0, fs->fs_bsize);
 1505                     dp2 = (struct ufs2_dinode *)(ibp->b_data);
 1506                     for (i = 0; i < INOPB(fs); i++) {
 1507                         /*
 1508                          * Don't bother to swap, it's supposed to be
 1509                          * random, after all.
 1510                          */
 1511                         dp2->di_gen = (arc4random() & INT32_MAX) / 2 + 1;
 1512                         dp2++;
 1513                 }
 1514                 bawrite(ibp);
 1515                 initediblk += INOPB(fs);
 1516                 cgp->cg_initediblk = ufs_rw32(initediblk, needswap);
 1517         }
 1518 
 1519         bdwrite(bp);
 1520         return (cg * fs->fs_ipg + ipref);
 1521 }
 1522 
 1523 /*
 1524  * Free a block or fragment.
 1525  *
 1526  * The specified block or fragment is placed back in the
 1527  * free map. If a fragment is deallocated, a possible 
 1528  * block reassembly is checked.
 1529  */
 1530 void
 1531 ffs_blkfree(ip, bno, size)
 1532         struct inode *ip;
 1533         daddr_t bno;
 1534         long size;
 1535 {
 1536         struct fs *fs = ip->i_fs;
 1537         struct cg *cgp;
 1538         struct buf *bp;
 1539         int32_t fragno, cgbno;
 1540         int i, error, cg, blk, frags, bbase;
 1541         u_int8_t *blksfree;
 1542         const int needswap = UFS_FSNEEDSWAP(fs);
 1543 
 1544         if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0 ||
 1545             fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) {
 1546                 printf("dev = 0x%x, bno = %" PRId64 " bsize = %d, "
 1547                        "size = %ld, fs = %s\n",
 1548                     ip->i_dev, bno, fs->fs_bsize, size, fs->fs_fsmnt);
 1549                 panic("blkfree: bad size");
 1550         }
 1551         cg = dtog(fs, bno);
 1552         if (bno >= fs->fs_size) {
 1553                 printf("bad block %" PRId64 ", ino %d\n", bno, ip->i_number);
 1554                 ffs_fserr(fs, ip->i_uid, "bad block");
 1555                 return;
 1556         }
 1557         error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
 1558                 (int)fs->fs_cgsize, NOCRED, &bp);
 1559         if (error) {
 1560                 brelse(bp);
 1561                 return;
 1562         }
 1563         cgp = (struct cg *)bp->b_data;
 1564         if (!cg_chkmagic(cgp, needswap)) {
 1565                 brelse(bp);
 1566                 return;
 1567         }
 1568         cgp->cg_old_time = ufs_rw32(time.tv_sec, needswap);
 1569         if ((fs->fs_magic != FS_UFS1_MAGIC) ||
 1570             (fs->fs_old_flags & FS_FLAGS_UPDATED))
 1571                 cgp->cg_time = ufs_rw64(time.tv_sec, needswap);
 1572         cgbno = dtogd(fs, bno);
 1573         blksfree = cg_blksfree(cgp, needswap);
 1574         if (size == fs->fs_bsize) {
 1575                 fragno = fragstoblks(fs, cgbno);
 1576                 if (!ffs_isfreeblock(fs, blksfree, fragno)) {
 1577                         printf("dev = 0x%x, block = %" PRId64 ", fs = %s\n",
 1578                             ip->i_dev, bno, fs->fs_fsmnt);
 1579                         panic("blkfree: freeing free block");
 1580                 }
 1581                 ffs_setblock(fs, blksfree, fragno);
 1582                 ffs_clusteracct(fs, cgp, fragno, 1);
 1583                 ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
 1584                 fs->fs_cstotal.cs_nbfree++;
 1585                 fs->fs_cs(fs, cg).cs_nbfree++;
 1586                 if ((fs->fs_magic == FS_UFS1_MAGIC) &&
 1587                     ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0)) {
 1588                         i = old_cbtocylno(fs, cgbno);
 1589                         KASSERT(i >= 0);
 1590                         KASSERT(i < fs->fs_old_ncyl);
 1591                         KASSERT(old_cbtorpos(fs, cgbno) >= 0);
 1592                         KASSERT(fs->fs_old_nrpos == 0 || old_cbtorpos(fs, cgbno) < fs->fs_old_nrpos);
 1593                         ufs_add16(old_cg_blks(fs, cgp, i, needswap)[old_cbtorpos(fs, cgbno)], 1,
 1594                             needswap);
 1595                         ufs_add32(old_cg_blktot(cgp, needswap)[i], 1, needswap);
 1596                 }
 1597         } else {
 1598                 bbase = cgbno - fragnum(fs, cgbno);
 1599                 /*
 1600                  * decrement the counts associated with the old frags
 1601                  */
 1602                 blk = blkmap(fs, blksfree, bbase);
 1603                 ffs_fragacct(fs, blk, cgp->cg_frsum, -1, needswap);
 1604                 /*
 1605                  * deallocate the fragment
 1606                  */
 1607                 frags = numfrags(fs, size);
 1608                 for (i = 0; i < frags; i++) {
 1609                         if (isset(blksfree, cgbno + i)) {
 1610                                 printf("dev = 0x%x, block = %" PRId64
 1611                                        ", fs = %s\n",
 1612                                     ip->i_dev, bno + i, fs->fs_fsmnt);
 1613                                 panic("blkfree: freeing free frag");
 1614                         }
 1615                         setbit(blksfree, cgbno + i);
 1616                 }
 1617                 ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
 1618                 fs->fs_cstotal.cs_nffree += i;
 1619                 fs->fs_cs(fs, cg).cs_nffree += i;
 1620                 /*
 1621                  * add back in counts associated with the new frags
 1622                  */
 1623                 blk = blkmap(fs, blksfree, bbase);
 1624                 ffs_fragacct(fs, blk, cgp->cg_frsum, 1, needswap);
 1625                 /*
 1626                  * if a complete block has been reassembled, account for it
 1627                  */
 1628                 fragno = fragstoblks(fs, bbase);
 1629                 if (ffs_isblock(fs, blksfree, fragno)) {
 1630                         ufs_add32(cgp->cg_cs.cs_nffree, -fs->fs_frag, needswap);
 1631                         fs->fs_cstotal.cs_nffree -= fs->fs_frag;
 1632                         fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
 1633                         ffs_clusteracct(fs, cgp, fragno, 1);
 1634                         ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
 1635                         fs->fs_cstotal.cs_nbfree++;
 1636                         fs->fs_cs(fs, cg).cs_nbfree++;
 1637                         if ((fs->fs_magic == FS_UFS1_MAGIC) &&
 1638                             ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0)) {
 1639                                 i = old_cbtocylno(fs, bbase);
 1640                                 KASSERT(i >= 0);
 1641                                 KASSERT(i < fs->fs_old_ncyl);
 1642                                 KASSERT(old_cbtorpos(fs, bbase) >= 0);
 1643                                 KASSERT(fs->fs_old_nrpos == 0 || old_cbtorpos(fs, bbase) < fs->fs_old_nrpos);
 1644                                 ufs_add16(old_cg_blks(fs, cgp, i, needswap)[old_cbtorpos(fs,
 1645                                     bbase)], 1, needswap);
 1646                                 ufs_add32(old_cg_blktot(cgp, needswap)[i], 1, needswap);
 1647                         }
 1648                 }
 1649         }
 1650         fs->fs_fmod = 1;
 1651         bdwrite(bp);
 1652 }
 1653 
 1654 #if defined(DIAGNOSTIC) || defined(DEBUG)
 1655 #ifdef XXXUBC
 1656 /*
 1657  * Verify allocation of a block or fragment. Returns true if block or
 1658  * fragment is allocated, false if it is free.
 1659  */
 1660 static int
 1661 ffs_checkblk(ip, bno, size)
 1662         struct inode *ip;
 1663         daddr_t bno;
 1664         long size;
 1665 {
 1666         struct fs *fs;
 1667         struct cg *cgp;
 1668         struct buf *bp;
 1669         int i, error, frags, free;
 1670 
 1671         fs = ip->i_fs;
 1672         if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
 1673                 printf("bsize = %d, size = %ld, fs = %s\n",
 1674                     fs->fs_bsize, size, fs->fs_fsmnt);
 1675                 panic("checkblk: bad size");
 1676         }
 1677         if (bno >= fs->fs_size)
 1678                 panic("checkblk: bad block %d", bno);
 1679         error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, dtog(fs, bno))),
 1680                 (int)fs->fs_cgsize, NOCRED, &bp);
 1681         if (error) {
 1682                 brelse(bp);
 1683                 return 0;
 1684         }
 1685         cgp = (struct cg *)bp->b_data;
 1686         if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs))) {
 1687                 brelse(bp);
 1688                 return 0;
 1689         }
 1690         bno = dtogd(fs, bno);
 1691         if (size == fs->fs_bsize) {
 1692                 free = ffs_isblock(fs, cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)),
 1693                         fragstoblks(fs, bno));
 1694         } else {
 1695                 frags = numfrags(fs, size);
 1696                 for (free = 0, i = 0; i < frags; i++)
 1697                         if (isset(cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)), bno + i))
 1698                                 free++;
 1699                 if (free != 0 && free != frags)
 1700                         panic("checkblk: partially free fragment");
 1701         }
 1702         brelse(bp);
 1703         return (!free);
 1704 }
 1705 #endif /* XXXUBC */
 1706 #endif /* DIAGNOSTIC */
 1707 
 1708 /*
 1709  * Free an inode.
 1710  */
 1711 int
 1712 ffs_vfree(v)
 1713         void *v;
 1714 {
 1715         struct vop_vfree_args /* {
 1716                 struct vnode *a_pvp;
 1717                 ino_t a_ino;
 1718                 int a_mode;
 1719         } */ *ap = v;
 1720 
 1721         if (DOINGSOFTDEP(ap->a_pvp)) {
 1722                 softdep_freefile(ap);
 1723                 return (0);
 1724         }
 1725         return (ffs_freefile(ap));
 1726 }
 1727 
 1728 /*
 1729  * Do the actual free operation.
 1730  * The specified inode is placed back in the free map.
 1731  */
 1732 int
 1733 ffs_freefile(v)
 1734         void *v;
 1735 {
 1736         struct vop_vfree_args /* {
 1737                 struct vnode *a_pvp;
 1738                 ino_t a_ino;
 1739                 int a_mode;
 1740         } */ *ap = v;
 1741         struct cg *cgp;
 1742         struct inode *pip = VTOI(ap->a_pvp);
 1743         struct fs *fs = pip->i_fs;
 1744         ino_t ino = ap->a_ino;
 1745         struct buf *bp;
 1746         int error, cg;
 1747         u_int8_t *inosused;
 1748 #ifdef FFS_EI
 1749         const int needswap = UFS_FSNEEDSWAP(fs);
 1750 #endif
 1751 
 1752         if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
 1753                 panic("ifree: range: dev = 0x%x, ino = %d, fs = %s",
 1754                     pip->i_dev, ino, fs->fs_fsmnt);
 1755         cg = ino_to_cg(fs, ino);
 1756         error = bread(pip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
 1757                 (int)fs->fs_cgsize, NOCRED, &bp);
 1758         if (error) {
 1759                 brelse(bp);
 1760                 return (error);
 1761         }
 1762         cgp = (struct cg *)bp->b_data;
 1763         if (!cg_chkmagic(cgp, needswap)) {
 1764                 brelse(bp);
 1765                 return (0);
 1766         }
 1767         cgp->cg_old_time = ufs_rw32(time.tv_sec, needswap);
 1768         if ((fs->fs_magic != FS_UFS1_MAGIC) ||
 1769             (fs->fs_old_flags & FS_FLAGS_UPDATED))
 1770                 cgp->cg_time = ufs_rw64(time.tv_sec, needswap);
 1771         inosused = cg_inosused(cgp, needswap);
 1772         ino %= fs->fs_ipg;
 1773         if (isclr(inosused, ino)) {
 1774                 printf("dev = 0x%x, ino = %d, fs = %s\n",
 1775                     pip->i_dev, ino, fs->fs_fsmnt);
 1776                 if (fs->fs_ronly == 0)
 1777                         panic("ifree: freeing free inode");
 1778         }
 1779         clrbit(inosused, ino);
 1780         if (ino < ufs_rw32(cgp->cg_irotor, needswap))
 1781                 cgp->cg_irotor = ufs_rw32(ino, needswap);
 1782         ufs_add32(cgp->cg_cs.cs_nifree, 1, needswap);
 1783         fs->fs_cstotal.cs_nifree++;
 1784         fs->fs_cs(fs, cg).cs_nifree++;
 1785         if ((ap->a_mode & IFMT) == IFDIR) {
 1786                 ufs_add32(cgp->cg_cs.cs_ndir, -1, needswap);
 1787                 fs->fs_cstotal.cs_ndir--;
 1788                 fs->fs_cs(fs, cg).cs_ndir--;
 1789         }
 1790         fs->fs_fmod = 1;
 1791         bdwrite(bp);
 1792         return (0);
 1793 }
 1794 
 1795 /*
 1796  * Find a block of the specified size in the specified cylinder group.
 1797  *
 1798  * It is a panic if a request is made to find a block if none are
 1799  * available.
 1800  */
 1801 static int32_t
 1802 ffs_mapsearch(fs, cgp, bpref, allocsiz)
 1803         struct fs *fs;
 1804         struct cg *cgp;
 1805         daddr_t bpref;
 1806         int allocsiz;
 1807 {
 1808         int32_t bno;
 1809         int start, len, loc, i;
 1810         int blk, field, subfield, pos;
 1811         int ostart, olen;
 1812         u_int8_t *blksfree;
 1813 #ifdef FFS_EI
 1814         const int needswap = UFS_FSNEEDSWAP(fs);
 1815 #endif
 1816 
 1817         /*
 1818          * find the fragment by searching through the free block
 1819          * map for an appropriate bit pattern
 1820          */
 1821         if (bpref)
 1822                 start = dtogd(fs, bpref) / NBBY;
 1823         else
 1824                 start = ufs_rw32(cgp->cg_frotor, needswap) / NBBY;
 1825         blksfree = cg_blksfree(cgp, needswap);
 1826         len = howmany(fs->fs_fpg, NBBY) - start;
 1827         ostart = start;
 1828         olen = len;
 1829         loc = scanc((u_int)len,
 1830                 (const u_char *)&blksfree[start],
 1831                 (const u_char *)fragtbl[fs->fs_frag],
 1832                 (1 << (allocsiz - 1 + (fs->fs_frag & (NBBY - 1)))));
 1833         if (loc == 0) {
 1834                 len = start + 1;
 1835                 start = 0;
 1836                 loc = scanc((u_int)len,
 1837                         (const u_char *)&blksfree[0],
 1838                         (const u_char *)fragtbl[fs->fs_frag],
 1839                         (1 << (allocsiz - 1 + (fs->fs_frag & (NBBY - 1)))));
 1840                 if (loc == 0) {
 1841                         printf("start = %d, len = %d, fs = %s\n",
 1842                             ostart, olen, fs->fs_fsmnt);
 1843                         printf("offset=%d %ld\n",
 1844                                 ufs_rw32(cgp->cg_freeoff, needswap),
 1845                                 (long)blksfree - (long)cgp);
 1846                         printf("cg %d\n", cgp->cg_cgx);
 1847                         panic("ffs_alloccg: map corrupted");
 1848                         /* NOTREACHED */
 1849                 }
 1850         }
 1851         bno = (start + len - loc) * NBBY;
 1852         cgp->cg_frotor = ufs_rw32(bno, needswap);
 1853         /*
 1854          * found the byte in the map
 1855          * sift through the bits to find the selected frag
 1856          */
 1857         for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
 1858                 blk = blkmap(fs, blksfree, bno);
 1859                 blk <<= 1;
 1860                 field = around[allocsiz];
 1861                 subfield = inside[allocsiz];
 1862                 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
 1863                         if ((blk & field) == subfield)
 1864                                 return (bno + pos);
 1865                         field <<= 1;
 1866                         subfield <<= 1;
 1867                 }
 1868         }
 1869         printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
 1870         panic("ffs_alloccg: block not in map");
 1871         /* return (-1); */
 1872 }
 1873 
 1874 /*
 1875  * Update the cluster map because of an allocation or free.
 1876  *
 1877  * Cnt == 1 means free; cnt == -1 means allocating.
 1878  */
 1879 void
 1880 ffs_clusteracct(fs, cgp, blkno, cnt)
 1881         struct fs *fs;
 1882         struct cg *cgp;
 1883         int32_t blkno;
 1884         int cnt;
 1885 {
 1886         int32_t *sump;
 1887         int32_t *lp;
 1888         u_char *freemapp, *mapp;
 1889         int i, start, end, forw, back, map, bit;
 1890 #ifdef FFS_EI
 1891         const int needswap = UFS_FSNEEDSWAP(fs);
 1892 #endif
 1893 
 1894         if (fs->fs_contigsumsize <= 0)
 1895                 return;
 1896         freemapp = cg_clustersfree(cgp, needswap);
 1897         sump = cg_clustersum(cgp, needswap);
 1898         /*
 1899          * Allocate or clear the actual block.
 1900          */
 1901         if (cnt > 0)
 1902                 setbit(freemapp, blkno);
 1903         else
 1904                 clrbit(freemapp, blkno);
 1905         /*
 1906          * Find the size of the cluster going forward.
 1907          */
 1908         start = blkno + 1;
 1909         end = start + fs->fs_contigsumsize;
 1910         if (end >= ufs_rw32(cgp->cg_nclusterblks, needswap))
 1911                 end = ufs_rw32(cgp->cg_nclusterblks, needswap);
 1912         mapp = &freemapp[start / NBBY];
 1913         map = *mapp++;
 1914         bit = 1 << (start % NBBY);
 1915         for (i = start; i < end; i++) {
 1916                 if ((map & bit) == 0)
 1917                         break;
 1918                 if ((i & (NBBY - 1)) != (NBBY - 1)) {
 1919                         bit <<= 1;
 1920                 } else {
 1921                         map = *mapp++;
 1922                         bit = 1;
 1923                 }
 1924         }
 1925         forw = i - start;
 1926         /*
 1927          * Find the size of the cluster going backward.
 1928          */
 1929         start = blkno - 1;
 1930         end = start - fs->fs_contigsumsize;
 1931         if (end < 0)
 1932                 end = -1;
 1933         mapp = &freemapp[start / NBBY];
 1934         map = *mapp--;
 1935         bit = 1 << (start % NBBY);
 1936         for (i = start; i > end; i--) {
 1937                 if ((map & bit) == 0)
 1938                         break;
 1939                 if ((i & (NBBY - 1)) != 0) {
 1940                         bit >>= 1;
 1941                 } else {
 1942                         map = *mapp--;
 1943                         bit = 1 << (NBBY - 1);
 1944                 }
 1945         }
 1946         back = start - i;
 1947         /*
 1948          * Account for old cluster and the possibly new forward and
 1949          * back clusters.
 1950          */
 1951         i = back + forw + 1;
 1952         if (i > fs->fs_contigsumsize)
 1953                 i = fs->fs_contigsumsize;
 1954         ufs_add32(sump[i], cnt, needswap);
 1955         if (back > 0)
 1956                 ufs_add32(sump[back], -cnt, needswap);
 1957         if (forw > 0)
 1958                 ufs_add32(sump[forw], -cnt, needswap);
 1959 
 1960         /*
 1961          * Update cluster summary information.
 1962          */
 1963         lp = &sump[fs->fs_contigsumsize];
 1964         for (i = fs->fs_contigsumsize; i > 0; i--)
 1965                 if (ufs_rw32(*lp--, needswap) > 0)
 1966                         break;
 1967         fs->fs_maxcluster[ufs_rw32(cgp->cg_cgx, needswap)] = i;
 1968 }
 1969 
 1970 /*
 1971  * Fserr prints the name of a file system with an error diagnostic.
 1972  * 
 1973  * The form of the error message is:
 1974  *      fs: error message
 1975  */
 1976 static void
 1977 ffs_fserr(fs, uid, cp)
 1978         struct fs *fs;
 1979         u_int uid;
 1980         char *cp;
 1981 {
 1982 
 1983         log(LOG_ERR, "uid %d, pid %d, command %s, on %s: %s\n",
 1984             uid, curproc->p_pid, curproc->p_comm, fs->fs_fsmnt, cp);
 1985 }

Cache object: 7c6f383363372b3d46f8f3a7cad06acd


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