Home | History | Annotate | Line # | Download | only in ffs
ffs_alloc.c revision 1.151.2.1
      1  1.151.2.1  pgoyette /*	$NetBSD: ffs_alloc.c,v 1.151.2.1 2016/11/04 14:49:22 pgoyette Exp $	*/
      2      1.111    simonb 
      3      1.111    simonb /*-
      4      1.122        ad  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
      5      1.111    simonb  * All rights reserved.
      6      1.111    simonb  *
      7      1.111    simonb  * This code is derived from software contributed to The NetBSD Foundation
      8      1.111    simonb  * by Wasabi Systems, Inc.
      9      1.111    simonb  *
     10      1.111    simonb  * Redistribution and use in source and binary forms, with or without
     11      1.111    simonb  * modification, are permitted provided that the following conditions
     12      1.111    simonb  * are met:
     13      1.111    simonb  * 1. Redistributions of source code must retain the above copyright
     14      1.111    simonb  *    notice, this list of conditions and the following disclaimer.
     15      1.111    simonb  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.111    simonb  *    notice, this list of conditions and the following disclaimer in the
     17      1.111    simonb  *    documentation and/or other materials provided with the distribution.
     18      1.111    simonb  *
     19      1.111    simonb  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20      1.111    simonb  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21      1.111    simonb  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22      1.111    simonb  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23      1.111    simonb  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24      1.111    simonb  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25      1.111    simonb  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26      1.111    simonb  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27      1.111    simonb  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28      1.111    simonb  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29      1.111    simonb  * POSSIBILITY OF SUCH DAMAGE.
     30      1.111    simonb  */
     31        1.2       cgd 
     32        1.1   mycroft /*
     33       1.60      fvdl  * Copyright (c) 2002 Networks Associates Technology, Inc.
     34       1.60      fvdl  * All rights reserved.
     35       1.60      fvdl  *
     36       1.60      fvdl  * This software was developed for the FreeBSD Project by Marshall
     37       1.60      fvdl  * Kirk McKusick and Network Associates Laboratories, the Security
     38       1.60      fvdl  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
     39       1.60      fvdl  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
     40       1.60      fvdl  * research program
     41       1.60      fvdl  *
     42        1.1   mycroft  * Copyright (c) 1982, 1986, 1989, 1993
     43        1.1   mycroft  *	The Regents of the University of California.  All rights reserved.
     44        1.1   mycroft  *
     45        1.1   mycroft  * Redistribution and use in source and binary forms, with or without
     46        1.1   mycroft  * modification, are permitted provided that the following conditions
     47        1.1   mycroft  * are met:
     48        1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     49        1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     50        1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     51        1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     52        1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     53       1.69       agc  * 3. Neither the name of the University nor the names of its contributors
     54        1.1   mycroft  *    may be used to endorse or promote products derived from this software
     55        1.1   mycroft  *    without specific prior written permission.
     56        1.1   mycroft  *
     57        1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     58        1.1   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     59        1.1   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     60        1.1   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     61        1.1   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     62        1.1   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     63        1.1   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     64        1.1   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     65        1.1   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     66        1.1   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     67        1.1   mycroft  * SUCH DAMAGE.
     68        1.1   mycroft  *
     69       1.18      fvdl  *	@(#)ffs_alloc.c	8.19 (Berkeley) 7/13/95
     70        1.1   mycroft  */
     71       1.53     lukem 
     72       1.53     lukem #include <sys/cdefs.h>
     73  1.151.2.1  pgoyette __KERNEL_RCSID(0, "$NetBSD: ffs_alloc.c,v 1.151.2.1 2016/11/04 14:49:22 pgoyette Exp $");
     74       1.17       mrg 
     75       1.43       mrg #if defined(_KERNEL_OPT)
     76       1.27   thorpej #include "opt_ffs.h"
     77       1.21    scottr #include "opt_quota.h"
     78      1.129       chs #include "opt_uvm_page_trkown.h"
     79       1.22    scottr #endif
     80        1.1   mycroft 
     81        1.1   mycroft #include <sys/param.h>
     82        1.1   mycroft #include <sys/systm.h>
     83        1.1   mycroft #include <sys/buf.h>
     84      1.130       tls #include <sys/cprng.h>
     85      1.111    simonb #include <sys/fstrans.h>
     86      1.111    simonb #include <sys/kauth.h>
     87      1.111    simonb #include <sys/kernel.h>
     88      1.111    simonb #include <sys/mount.h>
     89        1.1   mycroft #include <sys/proc.h>
     90      1.111    simonb #include <sys/syslog.h>
     91        1.1   mycroft #include <sys/vnode.h>
     92      1.111    simonb #include <sys/wapbl.h>
     93      1.147     joerg #include <sys/cprng.h>
     94       1.29       mrg 
     95       1.76   hannken #include <miscfs/specfs/specdev.h>
     96        1.1   mycroft #include <ufs/ufs/quota.h>
     97       1.19    bouyer #include <ufs/ufs/ufsmount.h>
     98        1.1   mycroft #include <ufs/ufs/inode.h>
     99        1.9  christos #include <ufs/ufs/ufs_extern.h>
    100       1.19    bouyer #include <ufs/ufs/ufs_bswap.h>
    101      1.111    simonb #include <ufs/ufs/ufs_wapbl.h>
    102        1.1   mycroft 
    103        1.1   mycroft #include <ufs/ffs/fs.h>
    104        1.1   mycroft #include <ufs/ffs/ffs_extern.h>
    105        1.1   mycroft 
    106      1.129       chs #ifdef UVM_PAGE_TRKOWN
    107      1.129       chs #include <uvm/uvm.h>
    108      1.129       chs #endif
    109      1.129       chs 
    110  1.151.2.1  pgoyette static daddr_t ffs_alloccg(struct inode *, int, daddr_t, int, int, int);
    111  1.151.2.1  pgoyette static daddr_t ffs_alloccgblk(struct inode *, struct buf *, daddr_t, int, int);
    112       1.85   thorpej static ino_t ffs_dirpref(struct inode *);
    113       1.85   thorpej static daddr_t ffs_fragextend(struct inode *, int, daddr_t, int, int);
    114      1.150   mlelstv static void ffs_fserr(struct fs *, kauth_cred_t, const char *);
    115  1.151.2.1  pgoyette static daddr_t ffs_hashalloc(struct inode *, int, daddr_t, int, int, int,
    116  1.151.2.1  pgoyette     daddr_t (*)(struct inode *, int, daddr_t, int, int, int));
    117  1.151.2.1  pgoyette static daddr_t ffs_nodealloccg(struct inode *, int, daddr_t, int, int, int);
    118       1.85   thorpej static int32_t ffs_mapsearch(struct fs *, struct cg *,
    119       1.85   thorpej 				      daddr_t, int);
    120      1.119     joerg static void ffs_blkfree_common(struct ufsmount *, struct fs *, dev_t, struct buf *,
    121      1.116     joerg     daddr_t, long, bool);
    122      1.119     joerg static void ffs_freefile_common(struct ufsmount *, struct fs *, dev_t, struct buf *, ino_t,
    123      1.119     joerg     int, bool);
    124       1.23  drochner 
    125       1.34  jdolecek /* if 1, changes in optimalization strategy are logged */
    126       1.34  jdolecek int ffs_log_changeopt = 0;
    127       1.34  jdolecek 
    128       1.23  drochner /* in ffs_tables.c */
    129       1.40  jdolecek extern const int inside[], around[];
    130       1.40  jdolecek extern const u_char * const fragtbl[];
    131        1.1   mycroft 
    132      1.116     joerg /* Basic consistency check for block allocations */
    133      1.116     joerg static int
    134      1.116     joerg ffs_check_bad_allocation(const char *func, struct fs *fs, daddr_t bno,
    135      1.116     joerg     long size, dev_t dev, ino_t inum)
    136      1.116     joerg {
    137      1.134  dholland 	if ((u_int)size > fs->fs_bsize || ffs_fragoff(fs, size) != 0 ||
    138      1.138  dholland 	    ffs_fragnum(fs, bno) + ffs_numfrags(fs, size) > fs->fs_frag) {
    139  1.151.2.1  pgoyette 		panic("%s: bad size: dev = 0x%llx, bno = %" PRId64
    140  1.151.2.1  pgoyette 		    " bsize = %d, size = %ld, fs = %s", func,
    141      1.120  christos 		    (long long)dev, bno, fs->fs_bsize, size, fs->fs_fsmnt);
    142      1.116     joerg 	}
    143      1.116     joerg 
    144      1.116     joerg 	if (bno >= fs->fs_size) {
    145  1.151.2.1  pgoyette 		printf("%s: bad block %" PRId64 ", ino %llu\n", func, bno,
    146      1.116     joerg 		    (unsigned long long)inum);
    147      1.150   mlelstv 		ffs_fserr(fs, NOCRED, "bad block");
    148      1.116     joerg 		return EINVAL;
    149      1.116     joerg 	}
    150      1.116     joerg 	return 0;
    151      1.116     joerg }
    152      1.116     joerg 
    153        1.1   mycroft /*
    154        1.1   mycroft  * Allocate a block in the file system.
    155       1.81     perry  *
    156        1.1   mycroft  * The size of the requested block is given, which must be some
    157        1.1   mycroft  * multiple of fs_fsize and <= fs_bsize.
    158        1.1   mycroft  * A preference may be optionally specified. If a preference is given
    159        1.1   mycroft  * the following hierarchy is used to allocate a block:
    160        1.1   mycroft  *   1) allocate the requested block.
    161        1.1   mycroft  *   2) allocate a rotationally optimal block in the same cylinder.
    162        1.1   mycroft  *   3) allocate a block in the same cylinder group.
    163        1.1   mycroft  *   4) quadradically rehash into other cylinder groups, until an
    164        1.1   mycroft  *      available block is located.
    165       1.47       wiz  * If no block preference is given the following hierarchy is used
    166        1.1   mycroft  * to allocate a block:
    167        1.1   mycroft  *   1) allocate a block in the cylinder group that contains the
    168        1.1   mycroft  *      inode for the file.
    169        1.1   mycroft  *   2) quadradically rehash into other cylinder groups, until an
    170        1.1   mycroft  *      available block is located.
    171      1.106     pooka  *
    172      1.106     pooka  * => called with um_lock held
    173      1.106     pooka  * => releases um_lock before returning
    174        1.1   mycroft  */
    175        1.9  christos int
    176  1.151.2.1  pgoyette ffs_alloc(struct inode *ip, daddr_t lbn, daddr_t bpref, int size,
    177  1.151.2.1  pgoyette     int flags, kauth_cred_t cred, daddr_t *bnp)
    178        1.1   mycroft {
    179      1.101        ad 	struct ufsmount *ump;
    180       1.62      fvdl 	struct fs *fs;
    181       1.58      fvdl 	daddr_t bno;
    182        1.9  christos 	int cg;
    183      1.127    bouyer #if defined(QUOTA) || defined(QUOTA2)
    184        1.9  christos 	int error;
    185        1.9  christos #endif
    186       1.81     perry 
    187       1.62      fvdl 	fs = ip->i_fs;
    188      1.101        ad 	ump = ip->i_ump;
    189      1.101        ad 
    190      1.101        ad 	KASSERT(mutex_owned(&ump->um_lock));
    191       1.62      fvdl 
    192       1.37       chs #ifdef UVM_PAGE_TRKOWN
    193      1.129       chs 
    194      1.129       chs 	/*
    195      1.129       chs 	 * Sanity-check that allocations within the file size
    196      1.129       chs 	 * do not allow other threads to read the stale contents
    197      1.129       chs 	 * of newly allocated blocks.
    198      1.129       chs 	 * Usually pages will exist to cover the new allocation.
    199      1.129       chs 	 * There is an optimization in ffs_write() where we skip
    200      1.129       chs 	 * creating pages if several conditions are met:
    201      1.129       chs 	 *  - the file must not be mapped (in any user address space).
    202      1.129       chs 	 *  - the write must cover whole pages and whole blocks.
    203      1.129       chs 	 * If those conditions are not met then pages must exist and
    204      1.129       chs 	 * be locked by the current thread.
    205      1.129       chs 	 */
    206      1.129       chs 
    207       1.51       chs 	if (ITOV(ip)->v_type == VREG &&
    208      1.137  dholland 	    ffs_lblktosize(fs, (voff_t)lbn) < round_page(ITOV(ip)->v_size)) {
    209       1.37       chs 		struct vm_page *pg;
    210      1.129       chs 		struct vnode *vp = ITOV(ip);
    211      1.129       chs 		struct uvm_object *uobj = &vp->v_uobj;
    212      1.137  dholland 		voff_t off = trunc_page(ffs_lblktosize(fs, lbn));
    213      1.137  dholland 		voff_t endoff = round_page(ffs_lblktosize(fs, lbn) + size);
    214       1.37       chs 
    215      1.128     rmind 		mutex_enter(uobj->vmobjlock);
    216       1.37       chs 		while (off < endoff) {
    217       1.37       chs 			pg = uvm_pagelookup(uobj, off);
    218      1.129       chs 			KASSERT((pg == NULL && (vp->v_vflag & VV_MAPPED) == 0 &&
    219      1.129       chs 				 (size & PAGE_MASK) == 0 &&
    220      1.135  dholland 				 ffs_blkoff(fs, size) == 0) ||
    221      1.129       chs 				(pg != NULL && pg->owner == curproc->p_pid &&
    222      1.129       chs 				 pg->lowner == curlwp->l_lid));
    223       1.37       chs 			off += PAGE_SIZE;
    224       1.37       chs 		}
    225      1.128     rmind 		mutex_exit(uobj->vmobjlock);
    226       1.37       chs 	}
    227       1.37       chs #endif
    228       1.37       chs 
    229        1.1   mycroft 	*bnp = 0;
    230        1.1   mycroft #ifdef DIAGNOSTIC
    231  1.151.2.1  pgoyette 	if (cred == NOCRED)
    232  1.151.2.1  pgoyette 		panic("%s: missing credential", __func__);
    233      1.134  dholland 	if ((u_int)size > fs->fs_bsize || ffs_fragoff(fs, size) != 0) {
    234  1.151.2.1  pgoyette 		panic("%s: bad size: dev = 0x%llx, bsize = %d, size = %d, "
    235  1.151.2.1  pgoyette 		    "fs = %s", __func__, (unsigned long long)ip->i_dev,
    236  1.151.2.1  pgoyette 		    fs->fs_bsize, size, fs->fs_fsmnt);
    237        1.1   mycroft 	}
    238        1.1   mycroft #endif /* DIAGNOSTIC */
    239        1.1   mycroft 	if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
    240        1.1   mycroft 		goto nospace;
    241       1.99     pooka 	if (freespace(fs, fs->fs_minfree) <= 0 &&
    242      1.124      elad 	    kauth_authorize_system(cred, KAUTH_SYSTEM_FS_RESERVEDSPACE, 0, NULL,
    243      1.124      elad 	    NULL, NULL) != 0)
    244        1.1   mycroft 		goto nospace;
    245      1.127    bouyer #if defined(QUOTA) || defined(QUOTA2)
    246      1.101        ad 	mutex_exit(&ump->um_lock);
    247       1.60      fvdl 	if ((error = chkdq(ip, btodb(size), cred, 0)) != 0)
    248        1.1   mycroft 		return (error);
    249      1.101        ad 	mutex_enter(&ump->um_lock);
    250        1.1   mycroft #endif
    251      1.111    simonb 
    252        1.1   mycroft 	if (bpref >= fs->fs_size)
    253        1.1   mycroft 		bpref = 0;
    254        1.1   mycroft 	if (bpref == 0)
    255        1.1   mycroft 		cg = ino_to_cg(fs, ip->i_number);
    256        1.1   mycroft 	else
    257        1.1   mycroft 		cg = dtog(fs, bpref);
    258  1.151.2.1  pgoyette 	bno = ffs_hashalloc(ip, cg, bpref, size, 0, flags, ffs_alloccg);
    259        1.1   mycroft 	if (bno > 0) {
    260       1.65  kristerw 		DIP_ADD(ip, blocks, btodb(size));
    261        1.1   mycroft 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    262        1.1   mycroft 		*bnp = bno;
    263        1.1   mycroft 		return (0);
    264        1.1   mycroft 	}
    265      1.127    bouyer #if defined(QUOTA) || defined(QUOTA2)
    266        1.1   mycroft 	/*
    267        1.1   mycroft 	 * Restore user's disk quota because allocation failed.
    268        1.1   mycroft 	 */
    269       1.60      fvdl 	(void) chkdq(ip, -btodb(size), cred, FORCE);
    270        1.1   mycroft #endif
    271      1.111    simonb 	if (flags & B_CONTIG) {
    272      1.111    simonb 		/*
    273      1.111    simonb 		 * XXX ump->um_lock handling is "suspect" at best.
    274      1.111    simonb 		 * For the case where ffs_hashalloc() fails early
    275      1.111    simonb 		 * in the B_CONTIG case we reach here with um_lock
    276      1.111    simonb 		 * already unlocked, so we can't release it again
    277      1.111    simonb 		 * like in the normal error path.  See kern/39206.
    278      1.111    simonb 		 *
    279      1.111    simonb 		 *
    280      1.111    simonb 		 * Fail silently - it's up to our caller to report
    281      1.111    simonb 		 * errors.
    282      1.111    simonb 		 */
    283      1.111    simonb 		return (ENOSPC);
    284      1.111    simonb 	}
    285        1.1   mycroft nospace:
    286      1.101        ad 	mutex_exit(&ump->um_lock);
    287      1.150   mlelstv 	ffs_fserr(fs, cred, "file system full");
    288        1.1   mycroft 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
    289        1.1   mycroft 	return (ENOSPC);
    290        1.1   mycroft }
    291        1.1   mycroft 
    292        1.1   mycroft /*
    293        1.1   mycroft  * Reallocate a fragment to a bigger size
    294        1.1   mycroft  *
    295        1.1   mycroft  * The number and size of the old block is given, and a preference
    296        1.1   mycroft  * and new size is also specified. The allocator attempts to extend
    297        1.1   mycroft  * the original block. Failing that, the regular block allocator is
    298        1.1   mycroft  * invoked to get an appropriate block.
    299      1.106     pooka  *
    300      1.106     pooka  * => called with um_lock held
    301      1.106     pooka  * => return with um_lock released
    302        1.1   mycroft  */
    303        1.9  christos int
    304       1.85   thorpej ffs_realloccg(struct inode *ip, daddr_t lbprev, daddr_t bpref, int osize,
    305       1.91      elad     int nsize, kauth_cred_t cred, struct buf **bpp, daddr_t *blknop)
    306        1.1   mycroft {
    307      1.101        ad 	struct ufsmount *ump;
    308       1.62      fvdl 	struct fs *fs;
    309        1.1   mycroft 	struct buf *bp;
    310        1.1   mycroft 	int cg, request, error;
    311       1.58      fvdl 	daddr_t bprev, bno;
    312       1.25   thorpej 
    313       1.62      fvdl 	fs = ip->i_fs;
    314      1.101        ad 	ump = ip->i_ump;
    315      1.101        ad 
    316      1.101        ad 	KASSERT(mutex_owned(&ump->um_lock));
    317      1.101        ad 
    318       1.37       chs #ifdef UVM_PAGE_TRKOWN
    319      1.129       chs 
    320      1.129       chs 	/*
    321      1.129       chs 	 * Sanity-check that allocations within the file size
    322      1.129       chs 	 * do not allow other threads to read the stale contents
    323      1.129       chs 	 * of newly allocated blocks.
    324      1.129       chs 	 * Unlike in ffs_alloc(), here pages must always exist
    325      1.129       chs 	 * for such allocations, because only the last block of a file
    326      1.129       chs 	 * can be a fragment and ffs_write() will reallocate the
    327      1.129       chs 	 * fragment to the new size using ufs_balloc_range(),
    328      1.129       chs 	 * which always creates pages to cover blocks it allocates.
    329      1.129       chs 	 */
    330      1.129       chs 
    331       1.37       chs 	if (ITOV(ip)->v_type == VREG) {
    332       1.37       chs 		struct vm_page *pg;
    333       1.51       chs 		struct uvm_object *uobj = &ITOV(ip)->v_uobj;
    334      1.137  dholland 		voff_t off = trunc_page(ffs_lblktosize(fs, lbprev));
    335      1.137  dholland 		voff_t endoff = round_page(ffs_lblktosize(fs, lbprev) + osize);
    336       1.37       chs 
    337      1.128     rmind 		mutex_enter(uobj->vmobjlock);
    338       1.37       chs 		while (off < endoff) {
    339       1.37       chs 			pg = uvm_pagelookup(uobj, off);
    340      1.129       chs 			KASSERT(pg->owner == curproc->p_pid &&
    341      1.129       chs 				pg->lowner == curlwp->l_lid);
    342       1.37       chs 			off += PAGE_SIZE;
    343       1.37       chs 		}
    344      1.128     rmind 		mutex_exit(uobj->vmobjlock);
    345       1.37       chs 	}
    346       1.37       chs #endif
    347       1.37       chs 
    348        1.1   mycroft #ifdef DIAGNOSTIC
    349  1.151.2.1  pgoyette 	if (cred == NOCRED)
    350  1.151.2.1  pgoyette 		panic("%s: missing credential", __func__);
    351      1.134  dholland 	if ((u_int)osize > fs->fs_bsize || ffs_fragoff(fs, osize) != 0 ||
    352      1.134  dholland 	    (u_int)nsize > fs->fs_bsize || ffs_fragoff(fs, nsize) != 0) {
    353  1.151.2.1  pgoyette 		panic("%s: bad size: dev = 0x%llx, bsize = %d, osize = %d, "
    354  1.151.2.1  pgoyette 		    "nsize = %d, fs = %s", __func__,
    355      1.120  christos 		    (unsigned long long)ip->i_dev, fs->fs_bsize, osize, nsize,
    356      1.120  christos 		    fs->fs_fsmnt);
    357        1.1   mycroft 	}
    358        1.1   mycroft #endif /* DIAGNOSTIC */
    359       1.99     pooka 	if (freespace(fs, fs->fs_minfree) <= 0 &&
    360      1.124      elad 	    kauth_authorize_system(cred, KAUTH_SYSTEM_FS_RESERVEDSPACE, 0, NULL,
    361      1.124      elad 	    NULL, NULL) != 0) {
    362      1.101        ad 		mutex_exit(&ump->um_lock);
    363        1.1   mycroft 		goto nospace;
    364      1.101        ad 	}
    365       1.60      fvdl 	if (fs->fs_magic == FS_UFS2_MAGIC)
    366       1.60      fvdl 		bprev = ufs_rw64(ip->i_ffs2_db[lbprev], UFS_FSNEEDSWAP(fs));
    367       1.60      fvdl 	else
    368       1.60      fvdl 		bprev = ufs_rw32(ip->i_ffs1_db[lbprev], UFS_FSNEEDSWAP(fs));
    369       1.60      fvdl 
    370       1.60      fvdl 	if (bprev == 0) {
    371  1.151.2.1  pgoyette 		panic("%s: bad bprev: dev = 0x%llx, bsize = %d, bprev = %"
    372  1.151.2.1  pgoyette 		    PRId64 ", fs = %s", __func__,
    373      1.120  christos 		    (unsigned long long)ip->i_dev, fs->fs_bsize, bprev,
    374      1.120  christos 		    fs->fs_fsmnt);
    375        1.1   mycroft 	}
    376      1.101        ad 	mutex_exit(&ump->um_lock);
    377      1.101        ad 
    378        1.1   mycroft 	/*
    379        1.1   mycroft 	 * Allocate the extra space in the buffer.
    380        1.1   mycroft 	 */
    381       1.37       chs 	if (bpp != NULL &&
    382      1.149      maxv 	    (error = bread(ITOV(ip), lbprev, osize, 0, &bp)) != 0) {
    383        1.1   mycroft 		return (error);
    384        1.1   mycroft 	}
    385      1.127    bouyer #if defined(QUOTA) || defined(QUOTA2)
    386       1.60      fvdl 	if ((error = chkdq(ip, btodb(nsize - osize), cred, 0)) != 0) {
    387       1.44       chs 		if (bpp != NULL) {
    388      1.101        ad 			brelse(bp, 0);
    389       1.44       chs 		}
    390        1.1   mycroft 		return (error);
    391        1.1   mycroft 	}
    392        1.1   mycroft #endif
    393        1.1   mycroft 	/*
    394        1.1   mycroft 	 * Check for extension in the existing location.
    395        1.1   mycroft 	 */
    396        1.1   mycroft 	cg = dtog(fs, bprev);
    397      1.101        ad 	mutex_enter(&ump->um_lock);
    398       1.60      fvdl 	if ((bno = ffs_fragextend(ip, cg, bprev, osize, nsize)) != 0) {
    399       1.65  kristerw 		DIP_ADD(ip, blocks, btodb(nsize - osize));
    400        1.1   mycroft 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    401       1.37       chs 
    402       1.37       chs 		if (bpp != NULL) {
    403  1.151.2.1  pgoyette 			if (bp->b_blkno != FFS_FSBTODB(fs, bno)) {
    404  1.151.2.1  pgoyette 				panic("%s: bad blockno %#llx != %#llx",
    405  1.151.2.1  pgoyette 				    __func__, (unsigned long long) bp->b_blkno,
    406  1.151.2.1  pgoyette 				    (unsigned long long)FFS_FSBTODB(fs, bno));
    407  1.151.2.1  pgoyette 			}
    408       1.72        pk 			allocbuf(bp, nsize, 1);
    409       1.98  christos 			memset((char *)bp->b_data + osize, 0, nsize - osize);
    410      1.105        ad 			mutex_enter(bp->b_objlock);
    411      1.109        ad 			KASSERT(!cv_has_waiters(&bp->b_done));
    412      1.105        ad 			bp->b_oflags |= BO_DONE;
    413      1.105        ad 			mutex_exit(bp->b_objlock);
    414       1.37       chs 			*bpp = bp;
    415       1.37       chs 		}
    416       1.37       chs 		if (blknop != NULL) {
    417       1.37       chs 			*blknop = bno;
    418       1.37       chs 		}
    419        1.1   mycroft 		return (0);
    420        1.1   mycroft 	}
    421        1.1   mycroft 	/*
    422        1.1   mycroft 	 * Allocate a new disk location.
    423        1.1   mycroft 	 */
    424        1.1   mycroft 	if (bpref >= fs->fs_size)
    425        1.1   mycroft 		bpref = 0;
    426        1.1   mycroft 	switch ((int)fs->fs_optim) {
    427        1.1   mycroft 	case FS_OPTSPACE:
    428        1.1   mycroft 		/*
    429       1.81     perry 		 * Allocate an exact sized fragment. Although this makes
    430       1.81     perry 		 * best use of space, we will waste time relocating it if
    431        1.1   mycroft 		 * the file continues to grow. If the fragmentation is
    432        1.1   mycroft 		 * less than half of the minimum free reserve, we choose
    433        1.1   mycroft 		 * to begin optimizing for time.
    434        1.1   mycroft 		 */
    435        1.1   mycroft 		request = nsize;
    436        1.1   mycroft 		if (fs->fs_minfree < 5 ||
    437        1.1   mycroft 		    fs->fs_cstotal.cs_nffree >
    438        1.1   mycroft 		    fs->fs_dsize * fs->fs_minfree / (2 * 100))
    439        1.1   mycroft 			break;
    440       1.34  jdolecek 
    441       1.34  jdolecek 		if (ffs_log_changeopt) {
    442       1.34  jdolecek 			log(LOG_NOTICE,
    443       1.34  jdolecek 				"%s: optimization changed from SPACE to TIME\n",
    444       1.34  jdolecek 				fs->fs_fsmnt);
    445       1.34  jdolecek 		}
    446       1.34  jdolecek 
    447        1.1   mycroft 		fs->fs_optim = FS_OPTTIME;
    448        1.1   mycroft 		break;
    449        1.1   mycroft 	case FS_OPTTIME:
    450        1.1   mycroft 		/*
    451        1.1   mycroft 		 * At this point we have discovered a file that is trying to
    452        1.1   mycroft 		 * grow a small fragment to a larger fragment. To save time,
    453        1.1   mycroft 		 * we allocate a full sized block, then free the unused portion.
    454        1.1   mycroft 		 * If the file continues to grow, the `ffs_fragextend' call
    455        1.1   mycroft 		 * above will be able to grow it in place without further
    456        1.1   mycroft 		 * copying. If aberrant programs cause disk fragmentation to
    457        1.1   mycroft 		 * grow within 2% of the free reserve, we choose to begin
    458        1.1   mycroft 		 * optimizing for space.
    459        1.1   mycroft 		 */
    460        1.1   mycroft 		request = fs->fs_bsize;
    461        1.1   mycroft 		if (fs->fs_cstotal.cs_nffree <
    462        1.1   mycroft 		    fs->fs_dsize * (fs->fs_minfree - 2) / 100)
    463        1.1   mycroft 			break;
    464       1.34  jdolecek 
    465       1.34  jdolecek 		if (ffs_log_changeopt) {
    466       1.34  jdolecek 			log(LOG_NOTICE,
    467       1.34  jdolecek 				"%s: optimization changed from TIME to SPACE\n",
    468       1.34  jdolecek 				fs->fs_fsmnt);
    469       1.34  jdolecek 		}
    470       1.34  jdolecek 
    471        1.1   mycroft 		fs->fs_optim = FS_OPTSPACE;
    472        1.1   mycroft 		break;
    473        1.1   mycroft 	default:
    474  1.151.2.1  pgoyette 		panic("%s: bad optim: dev = 0x%llx, optim = %d, fs = %s",
    475  1.151.2.1  pgoyette 		    __func__, (unsigned long long)ip->i_dev, fs->fs_optim,
    476  1.151.2.1  pgoyette 		    fs->fs_fsmnt);
    477        1.1   mycroft 		/* NOTREACHED */
    478        1.1   mycroft 	}
    479  1.151.2.1  pgoyette 	bno = ffs_hashalloc(ip, cg, bpref, request, nsize, 0, ffs_alloccg);
    480        1.1   mycroft 	if (bno > 0) {
    481  1.151.2.1  pgoyette 		/*
    482  1.151.2.1  pgoyette 		 * Use forced deallocation registration, we can't handle
    483  1.151.2.1  pgoyette 		 * failure here. This is safe, as this place is ever hit
    484  1.151.2.1  pgoyette 		 * maximum once per write operation, when fragment is extended
    485  1.151.2.1  pgoyette 		 * to longer fragment, or a full block.
    486  1.151.2.1  pgoyette 		 */
    487      1.122        ad 		if ((ip->i_ump->um_mountp->mnt_wapbl) &&
    488      1.122        ad 		    (ITOV(ip)->v_type != VREG)) {
    489  1.151.2.1  pgoyette 			/* this should never fail */
    490  1.151.2.1  pgoyette 			error = UFS_WAPBL_REGISTER_DEALLOCATION_FORCE(
    491      1.136  dholland 			    ip->i_ump->um_mountp, FFS_FSBTODB(fs, bprev),
    492      1.122        ad 			    osize);
    493  1.151.2.1  pgoyette 			if (error)
    494  1.151.2.1  pgoyette 				panic("ffs_realloccg: dealloc registration failed");
    495      1.122        ad 		} else {
    496      1.122        ad 			ffs_blkfree(fs, ip->i_devvp, bprev, (long)osize,
    497      1.122        ad 			    ip->i_number);
    498      1.111    simonb 		}
    499       1.65  kristerw 		DIP_ADD(ip, blocks, btodb(nsize - osize));
    500        1.1   mycroft 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    501       1.37       chs 		if (bpp != NULL) {
    502      1.136  dholland 			bp->b_blkno = FFS_FSBTODB(fs, bno);
    503       1.72        pk 			allocbuf(bp, nsize, 1);
    504       1.98  christos 			memset((char *)bp->b_data + osize, 0, (u_int)nsize - osize);
    505      1.105        ad 			mutex_enter(bp->b_objlock);
    506      1.109        ad 			KASSERT(!cv_has_waiters(&bp->b_done));
    507      1.105        ad 			bp->b_oflags |= BO_DONE;
    508      1.105        ad 			mutex_exit(bp->b_objlock);
    509       1.37       chs 			*bpp = bp;
    510       1.37       chs 		}
    511       1.37       chs 		if (blknop != NULL) {
    512       1.37       chs 			*blknop = bno;
    513       1.37       chs 		}
    514        1.1   mycroft 		return (0);
    515        1.1   mycroft 	}
    516      1.101        ad 	mutex_exit(&ump->um_lock);
    517      1.101        ad 
    518      1.127    bouyer #if defined(QUOTA) || defined(QUOTA2)
    519        1.1   mycroft 	/*
    520        1.1   mycroft 	 * Restore user's disk quota because allocation failed.
    521        1.1   mycroft 	 */
    522       1.60      fvdl 	(void) chkdq(ip, -btodb(nsize - osize), cred, FORCE);
    523        1.1   mycroft #endif
    524       1.37       chs 	if (bpp != NULL) {
    525      1.101        ad 		brelse(bp, 0);
    526       1.37       chs 	}
    527       1.37       chs 
    528        1.1   mycroft nospace:
    529        1.1   mycroft 	/*
    530        1.1   mycroft 	 * no space available
    531        1.1   mycroft 	 */
    532      1.150   mlelstv 	ffs_fserr(fs, cred, "file system full");
    533        1.1   mycroft 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
    534        1.1   mycroft 	return (ENOSPC);
    535        1.1   mycroft }
    536        1.1   mycroft 
    537        1.1   mycroft /*
    538        1.1   mycroft  * Allocate an inode in the file system.
    539       1.81     perry  *
    540        1.1   mycroft  * If allocating a directory, use ffs_dirpref to select the inode.
    541        1.1   mycroft  * If allocating in a directory, the following hierarchy is followed:
    542        1.1   mycroft  *   1) allocate the preferred inode.
    543        1.1   mycroft  *   2) allocate an inode in the same cylinder group.
    544        1.1   mycroft  *   3) quadradically rehash into other cylinder groups, until an
    545        1.1   mycroft  *      available inode is located.
    546       1.47       wiz  * If no inode preference is given the following hierarchy is used
    547        1.1   mycroft  * to allocate an inode:
    548        1.1   mycroft  *   1) allocate an inode in cylinder group 0.
    549        1.1   mycroft  *   2) quadradically rehash into other cylinder groups, until an
    550        1.1   mycroft  *      available inode is located.
    551      1.106     pooka  *
    552      1.106     pooka  * => um_lock not held upon entry or return
    553        1.1   mycroft  */
    554        1.9  christos int
    555      1.148   hannken ffs_valloc(struct vnode *pvp, int mode, kauth_cred_t cred, ino_t *inop)
    556        1.9  christos {
    557      1.101        ad 	struct ufsmount *ump;
    558       1.33  augustss 	struct inode *pip;
    559       1.33  augustss 	struct fs *fs;
    560        1.1   mycroft 	ino_t ino, ipref;
    561        1.1   mycroft 	int cg, error;
    562       1.81     perry 
    563      1.111    simonb 	UFS_WAPBL_JUNLOCK_ASSERT(pvp->v_mount);
    564      1.111    simonb 
    565        1.1   mycroft 	pip = VTOI(pvp);
    566        1.1   mycroft 	fs = pip->i_fs;
    567      1.101        ad 	ump = pip->i_ump;
    568      1.101        ad 
    569      1.111    simonb 	error = UFS_WAPBL_BEGIN(pvp->v_mount);
    570      1.111    simonb 	if (error) {
    571      1.111    simonb 		return error;
    572      1.111    simonb 	}
    573      1.101        ad 	mutex_enter(&ump->um_lock);
    574        1.1   mycroft 	if (fs->fs_cstotal.cs_nifree == 0)
    575        1.1   mycroft 		goto noinodes;
    576        1.1   mycroft 
    577        1.1   mycroft 	if ((mode & IFMT) == IFDIR)
    578       1.50     lukem 		ipref = ffs_dirpref(pip);
    579       1.50     lukem 	else
    580       1.50     lukem 		ipref = pip->i_number;
    581        1.1   mycroft 	if (ipref >= fs->fs_ncg * fs->fs_ipg)
    582        1.1   mycroft 		ipref = 0;
    583        1.1   mycroft 	cg = ino_to_cg(fs, ipref);
    584       1.50     lukem 	/*
    585       1.50     lukem 	 * Track number of dirs created one after another
    586       1.50     lukem 	 * in a same cg without intervening by files.
    587       1.50     lukem 	 */
    588       1.50     lukem 	if ((mode & IFMT) == IFDIR) {
    589       1.63      fvdl 		if (fs->fs_contigdirs[cg] < 255)
    590       1.50     lukem 			fs->fs_contigdirs[cg]++;
    591       1.50     lukem 	} else {
    592       1.50     lukem 		if (fs->fs_contigdirs[cg] > 0)
    593       1.50     lukem 			fs->fs_contigdirs[cg]--;
    594       1.50     lukem 	}
    595  1.151.2.1  pgoyette 	ino = (ino_t)ffs_hashalloc(pip, cg, ipref, mode, 0, 0, ffs_nodealloccg);
    596        1.1   mycroft 	if (ino == 0)
    597        1.1   mycroft 		goto noinodes;
    598      1.111    simonb 	UFS_WAPBL_END(pvp->v_mount);
    599      1.148   hannken 	*inop = ino;
    600      1.148   hannken 	return 0;
    601       1.60      fvdl 
    602        1.1   mycroft noinodes:
    603      1.101        ad 	mutex_exit(&ump->um_lock);
    604      1.111    simonb 	UFS_WAPBL_END(pvp->v_mount);
    605      1.150   mlelstv 	ffs_fserr(fs, cred, "out of inodes");
    606        1.1   mycroft 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
    607      1.148   hannken 	return ENOSPC;
    608        1.1   mycroft }
    609        1.1   mycroft 
    610        1.1   mycroft /*
    611       1.50     lukem  * Find a cylinder group in which to place a directory.
    612       1.42  sommerfe  *
    613       1.50     lukem  * The policy implemented by this algorithm is to allocate a
    614       1.50     lukem  * directory inode in the same cylinder group as its parent
    615       1.50     lukem  * directory, but also to reserve space for its files inodes
    616       1.50     lukem  * and data. Restrict the number of directories which may be
    617       1.50     lukem  * allocated one after another in the same cylinder group
    618       1.50     lukem  * without intervening allocation of files.
    619       1.42  sommerfe  *
    620       1.50     lukem  * If we allocate a first level directory then force allocation
    621       1.50     lukem  * in another cylinder group.
    622        1.1   mycroft  */
    623        1.1   mycroft static ino_t
    624       1.85   thorpej ffs_dirpref(struct inode *pip)
    625        1.1   mycroft {
    626       1.50     lukem 	register struct fs *fs;
    627       1.74     soren 	int cg, prefcg;
    628       1.89       dsl 	int64_t dirsize, cgsize, curdsz;
    629       1.89       dsl 	int avgifree, avgbfree, avgndir;
    630       1.50     lukem 	int minifree, minbfree, maxndir;
    631       1.50     lukem 	int mincg, minndir;
    632       1.50     lukem 	int maxcontigdirs;
    633       1.50     lukem 
    634      1.101        ad 	KASSERT(mutex_owned(&pip->i_ump->um_lock));
    635      1.101        ad 
    636       1.50     lukem 	fs = pip->i_fs;
    637        1.1   mycroft 
    638        1.1   mycroft 	avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
    639       1.50     lukem 	avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
    640       1.50     lukem 	avgndir = fs->fs_cstotal.cs_ndir / fs->fs_ncg;
    641       1.50     lukem 
    642       1.50     lukem 	/*
    643       1.50     lukem 	 * Force allocation in another cg if creating a first level dir.
    644       1.50     lukem 	 */
    645      1.102        ad 	if (ITOV(pip)->v_vflag & VV_ROOT) {
    646      1.147     joerg 		prefcg = cprng_fast32() % fs->fs_ncg;
    647       1.50     lukem 		mincg = prefcg;
    648       1.50     lukem 		minndir = fs->fs_ipg;
    649       1.50     lukem 		for (cg = prefcg; cg < fs->fs_ncg; cg++)
    650       1.50     lukem 			if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
    651       1.50     lukem 			    fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
    652       1.50     lukem 			    fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
    653       1.42  sommerfe 				mincg = cg;
    654       1.50     lukem 				minndir = fs->fs_cs(fs, cg).cs_ndir;
    655       1.42  sommerfe 			}
    656       1.50     lukem 		for (cg = 0; cg < prefcg; cg++)
    657       1.50     lukem 			if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
    658       1.50     lukem 			    fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
    659       1.50     lukem 			    fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
    660       1.50     lukem 				mincg = cg;
    661       1.50     lukem 				minndir = fs->fs_cs(fs, cg).cs_ndir;
    662       1.42  sommerfe 			}
    663       1.50     lukem 		return ((ino_t)(fs->fs_ipg * mincg));
    664       1.42  sommerfe 	}
    665       1.50     lukem 
    666       1.50     lukem 	/*
    667       1.50     lukem 	 * Count various limits which used for
    668       1.50     lukem 	 * optimal allocation of a directory inode.
    669      1.144       bad 	 * Try cylinder groups with >75% avgifree and avgbfree.
    670      1.144       bad 	 * Avoid cylinder groups with no free blocks or inodes as that
    671      1.144       bad 	 * triggers an I/O-expensive cylinder group scan.
    672       1.50     lukem 	 */
    673       1.50     lukem 	maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg);
    674      1.144       bad 	minifree = avgifree - avgifree / 4;
    675      1.144       bad 	if (minifree < 1)
    676      1.144       bad 		minifree = 1;
    677      1.144       bad 	minbfree = avgbfree - avgbfree / 4;
    678      1.144       bad 	if (minbfree < 1)
    679      1.144       bad 		minbfree = 1;
    680       1.89       dsl 	cgsize = (int64_t)fs->fs_fsize * fs->fs_fpg;
    681       1.89       dsl 	dirsize = (int64_t)fs->fs_avgfilesize * fs->fs_avgfpdir;
    682       1.89       dsl 	if (avgndir != 0) {
    683       1.89       dsl 		curdsz = (cgsize - (int64_t)avgbfree * fs->fs_bsize) / avgndir;
    684       1.89       dsl 		if (dirsize < curdsz)
    685       1.89       dsl 			dirsize = curdsz;
    686       1.89       dsl 	}
    687       1.89       dsl 	if (cgsize < dirsize * 255)
    688      1.144       bad 		maxcontigdirs = (avgbfree * fs->fs_bsize) / dirsize;
    689       1.89       dsl 	else
    690       1.89       dsl 		maxcontigdirs = 255;
    691       1.50     lukem 	if (fs->fs_avgfpdir > 0)
    692       1.50     lukem 		maxcontigdirs = min(maxcontigdirs,
    693       1.50     lukem 				    fs->fs_ipg / fs->fs_avgfpdir);
    694       1.50     lukem 	if (maxcontigdirs == 0)
    695       1.50     lukem 		maxcontigdirs = 1;
    696       1.50     lukem 
    697       1.50     lukem 	/*
    698       1.81     perry 	 * Limit number of dirs in one cg and reserve space for
    699       1.50     lukem 	 * regular files, but only if we have no deficit in
    700       1.50     lukem 	 * inodes or space.
    701       1.50     lukem 	 */
    702       1.50     lukem 	prefcg = ino_to_cg(fs, pip->i_number);
    703       1.50     lukem 	for (cg = prefcg; cg < fs->fs_ncg; cg++)
    704       1.50     lukem 		if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
    705       1.50     lukem 		    fs->fs_cs(fs, cg).cs_nifree >= minifree &&
    706       1.50     lukem 	    	    fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
    707       1.50     lukem 			if (fs->fs_contigdirs[cg] < maxcontigdirs)
    708       1.50     lukem 				return ((ino_t)(fs->fs_ipg * cg));
    709       1.50     lukem 		}
    710       1.50     lukem 	for (cg = 0; cg < prefcg; cg++)
    711       1.50     lukem 		if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
    712       1.50     lukem 		    fs->fs_cs(fs, cg).cs_nifree >= minifree &&
    713       1.50     lukem 	    	    fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
    714       1.50     lukem 			if (fs->fs_contigdirs[cg] < maxcontigdirs)
    715       1.50     lukem 				return ((ino_t)(fs->fs_ipg * cg));
    716       1.50     lukem 		}
    717       1.50     lukem 	/*
    718       1.50     lukem 	 * This is a backstop when we are deficient in space.
    719       1.50     lukem 	 */
    720       1.50     lukem 	for (cg = prefcg; cg < fs->fs_ncg; cg++)
    721       1.50     lukem 		if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
    722       1.50     lukem 			return ((ino_t)(fs->fs_ipg * cg));
    723       1.50     lukem 	for (cg = 0; cg < prefcg; cg++)
    724       1.50     lukem 		if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
    725       1.50     lukem 			break;
    726       1.50     lukem 	return ((ino_t)(fs->fs_ipg * cg));
    727        1.1   mycroft }
    728        1.1   mycroft 
    729        1.1   mycroft /*
    730        1.1   mycroft  * Select the desired position for the next block in a file.  The file is
    731        1.1   mycroft  * logically divided into sections. The first section is composed of the
    732        1.1   mycroft  * direct blocks. Each additional section contains fs_maxbpg blocks.
    733       1.81     perry  *
    734        1.1   mycroft  * If no blocks have been allocated in the first section, the policy is to
    735        1.1   mycroft  * request a block in the same cylinder group as the inode that describes
    736        1.1   mycroft  * the file. If no blocks have been allocated in any other section, the
    737        1.1   mycroft  * policy is to place the section in a cylinder group with a greater than
    738        1.1   mycroft  * average number of free blocks.  An appropriate cylinder group is found
    739        1.1   mycroft  * by using a rotor that sweeps the cylinder groups. When a new group of
    740        1.1   mycroft  * blocks is needed, the sweep begins in the cylinder group following the
    741        1.1   mycroft  * cylinder group from which the previous allocation was made. The sweep
    742        1.1   mycroft  * continues until a cylinder group with greater than the average number
    743        1.1   mycroft  * of free blocks is found. If the allocation is for the first block in an
    744        1.1   mycroft  * indirect block, the information on the previous allocation is unavailable;
    745        1.1   mycroft  * here a best guess is made based upon the logical block number being
    746        1.1   mycroft  * allocated.
    747       1.81     perry  *
    748        1.1   mycroft  * If a section is already partially allocated, the policy is to
    749        1.1   mycroft  * contiguously allocate fs_maxcontig blocks.  The end of one of these
    750       1.60      fvdl  * contiguous blocks and the beginning of the next is laid out
    751       1.60      fvdl  * contigously if possible.
    752      1.106     pooka  *
    753      1.106     pooka  * => um_lock held on entry and exit
    754        1.1   mycroft  */
    755       1.58      fvdl daddr_t
    756      1.111    simonb ffs_blkpref_ufs1(struct inode *ip, daddr_t lbn, int indx, int flags,
    757       1.85   thorpej     int32_t *bap /* XXX ondisk32 */)
    758        1.1   mycroft {
    759       1.33  augustss 	struct fs *fs;
    760       1.33  augustss 	int cg;
    761        1.1   mycroft 	int avgbfree, startcg;
    762        1.1   mycroft 
    763      1.101        ad 	KASSERT(mutex_owned(&ip->i_ump->um_lock));
    764      1.101        ad 
    765        1.1   mycroft 	fs = ip->i_fs;
    766      1.111    simonb 
    767      1.111    simonb 	/*
    768      1.111    simonb 	 * If allocating a contiguous file with B_CONTIG, use the hints
    769      1.111    simonb 	 * in the inode extentions to return the desired block.
    770      1.111    simonb 	 *
    771      1.111    simonb 	 * For metadata (indirect blocks) return the address of where
    772      1.111    simonb 	 * the first indirect block resides - we'll scan for the next
    773      1.111    simonb 	 * available slot if we need to allocate more than one indirect
    774      1.111    simonb 	 * block.  For data, return the address of the actual block
    775      1.111    simonb 	 * relative to the address of the first data block.
    776      1.111    simonb 	 */
    777      1.111    simonb 	if (flags & B_CONTIG) {
    778      1.111    simonb 		KASSERT(ip->i_ffs_first_data_blk != 0);
    779      1.111    simonb 		KASSERT(ip->i_ffs_first_indir_blk != 0);
    780      1.111    simonb 		if (flags & B_METAONLY)
    781      1.111    simonb 			return ip->i_ffs_first_indir_blk;
    782      1.111    simonb 		else
    783      1.138  dholland 			return ip->i_ffs_first_data_blk + ffs_blkstofrags(fs, lbn);
    784      1.111    simonb 	}
    785      1.111    simonb 
    786        1.1   mycroft 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
    787      1.134  dholland 		if (lbn < UFS_NDADDR + FFS_NINDIR(fs)) {
    788        1.1   mycroft 			cg = ino_to_cg(fs, ip->i_number);
    789      1.110    simonb 			return (cgbase(fs, cg) + fs->fs_frag);
    790        1.1   mycroft 		}
    791        1.1   mycroft 		/*
    792        1.1   mycroft 		 * Find a cylinder with greater than average number of
    793        1.1   mycroft 		 * unused data blocks.
    794        1.1   mycroft 		 */
    795        1.1   mycroft 		if (indx == 0 || bap[indx - 1] == 0)
    796        1.1   mycroft 			startcg =
    797        1.1   mycroft 			    ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
    798        1.1   mycroft 		else
    799       1.19    bouyer 			startcg = dtog(fs,
    800       1.30      fvdl 				ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1);
    801        1.1   mycroft 		startcg %= fs->fs_ncg;
    802        1.1   mycroft 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
    803        1.1   mycroft 		for (cg = startcg; cg < fs->fs_ncg; cg++)
    804        1.1   mycroft 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
    805      1.110    simonb 				return (cgbase(fs, cg) + fs->fs_frag);
    806        1.1   mycroft 			}
    807       1.52     lukem 		for (cg = 0; cg < startcg; cg++)
    808        1.1   mycroft 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
    809      1.110    simonb 				return (cgbase(fs, cg) + fs->fs_frag);
    810        1.1   mycroft 			}
    811       1.35   thorpej 		return (0);
    812        1.1   mycroft 	}
    813        1.1   mycroft 	/*
    814       1.60      fvdl 	 * We just always try to lay things out contiguously.
    815       1.60      fvdl 	 */
    816       1.60      fvdl 	return ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
    817       1.60      fvdl }
    818       1.60      fvdl 
    819       1.60      fvdl daddr_t
    820      1.111    simonb ffs_blkpref_ufs2(struct inode *ip, daddr_t lbn, int indx, int flags,
    821      1.111    simonb     int64_t *bap)
    822       1.60      fvdl {
    823       1.60      fvdl 	struct fs *fs;
    824       1.60      fvdl 	int cg;
    825       1.60      fvdl 	int avgbfree, startcg;
    826       1.60      fvdl 
    827      1.101        ad 	KASSERT(mutex_owned(&ip->i_ump->um_lock));
    828      1.101        ad 
    829       1.60      fvdl 	fs = ip->i_fs;
    830      1.111    simonb 
    831      1.111    simonb 	/*
    832      1.111    simonb 	 * If allocating a contiguous file with B_CONTIG, use the hints
    833      1.111    simonb 	 * in the inode extentions to return the desired block.
    834      1.111    simonb 	 *
    835      1.111    simonb 	 * For metadata (indirect blocks) return the address of where
    836      1.111    simonb 	 * the first indirect block resides - we'll scan for the next
    837      1.111    simonb 	 * available slot if we need to allocate more than one indirect
    838      1.111    simonb 	 * block.  For data, return the address of the actual block
    839      1.111    simonb 	 * relative to the address of the first data block.
    840      1.111    simonb 	 */
    841      1.111    simonb 	if (flags & B_CONTIG) {
    842      1.111    simonb 		KASSERT(ip->i_ffs_first_data_blk != 0);
    843      1.111    simonb 		KASSERT(ip->i_ffs_first_indir_blk != 0);
    844      1.111    simonb 		if (flags & B_METAONLY)
    845      1.111    simonb 			return ip->i_ffs_first_indir_blk;
    846      1.111    simonb 		else
    847      1.138  dholland 			return ip->i_ffs_first_data_blk + ffs_blkstofrags(fs, lbn);
    848      1.111    simonb 	}
    849      1.111    simonb 
    850       1.60      fvdl 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
    851      1.134  dholland 		if (lbn < UFS_NDADDR + FFS_NINDIR(fs)) {
    852       1.60      fvdl 			cg = ino_to_cg(fs, ip->i_number);
    853      1.110    simonb 			return (cgbase(fs, cg) + fs->fs_frag);
    854       1.60      fvdl 		}
    855        1.1   mycroft 		/*
    856       1.60      fvdl 		 * Find a cylinder with greater than average number of
    857       1.60      fvdl 		 * unused data blocks.
    858        1.1   mycroft 		 */
    859       1.60      fvdl 		if (indx == 0 || bap[indx - 1] == 0)
    860       1.60      fvdl 			startcg =
    861       1.60      fvdl 			    ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
    862       1.60      fvdl 		else
    863       1.60      fvdl 			startcg = dtog(fs,
    864       1.60      fvdl 				ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1);
    865       1.60      fvdl 		startcg %= fs->fs_ncg;
    866       1.60      fvdl 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
    867       1.60      fvdl 		for (cg = startcg; cg < fs->fs_ncg; cg++)
    868       1.60      fvdl 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
    869      1.110    simonb 				return (cgbase(fs, cg) + fs->fs_frag);
    870       1.60      fvdl 			}
    871       1.60      fvdl 		for (cg = 0; cg < startcg; cg++)
    872       1.60      fvdl 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
    873      1.110    simonb 				return (cgbase(fs, cg) + fs->fs_frag);
    874       1.60      fvdl 			}
    875       1.60      fvdl 		return (0);
    876       1.60      fvdl 	}
    877       1.60      fvdl 	/*
    878       1.60      fvdl 	 * We just always try to lay things out contiguously.
    879       1.60      fvdl 	 */
    880       1.60      fvdl 	return ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
    881        1.1   mycroft }
    882        1.1   mycroft 
    883       1.60      fvdl 
    884        1.1   mycroft /*
    885        1.1   mycroft  * Implement the cylinder overflow algorithm.
    886        1.1   mycroft  *
    887        1.1   mycroft  * The policy implemented by this algorithm is:
    888        1.1   mycroft  *   1) allocate the block in its requested cylinder group.
    889        1.1   mycroft  *   2) quadradically rehash on the cylinder group number.
    890        1.1   mycroft  *   3) brute force search for a free block.
    891      1.106     pooka  *
    892      1.106     pooka  * => called with um_lock held
    893      1.106     pooka  * => returns with um_lock released on success, held on failure
    894      1.106     pooka  *    (*allocator releases lock on success, retains lock on failure)
    895        1.1   mycroft  */
    896        1.1   mycroft /*VARARGS5*/
    897       1.58      fvdl static daddr_t
    898       1.85   thorpej ffs_hashalloc(struct inode *ip, int cg, daddr_t pref,
    899       1.85   thorpej     int size /* size for data blocks, mode for inodes */,
    900  1.151.2.1  pgoyette     int realsize,
    901  1.151.2.1  pgoyette     int flags,
    902  1.151.2.1  pgoyette     daddr_t (*allocator)(struct inode *, int, daddr_t, int, int, int))
    903        1.1   mycroft {
    904       1.33  augustss 	struct fs *fs;
    905       1.58      fvdl 	daddr_t result;
    906        1.1   mycroft 	int i, icg = cg;
    907        1.1   mycroft 
    908        1.1   mycroft 	fs = ip->i_fs;
    909        1.1   mycroft 	/*
    910        1.1   mycroft 	 * 1: preferred cylinder group
    911        1.1   mycroft 	 */
    912  1.151.2.1  pgoyette 	result = (*allocator)(ip, cg, pref, size, realsize, flags);
    913        1.1   mycroft 	if (result)
    914        1.1   mycroft 		return (result);
    915      1.111    simonb 
    916      1.111    simonb 	if (flags & B_CONTIG)
    917      1.111    simonb 		return (result);
    918        1.1   mycroft 	/*
    919        1.1   mycroft 	 * 2: quadratic rehash
    920        1.1   mycroft 	 */
    921        1.1   mycroft 	for (i = 1; i < fs->fs_ncg; i *= 2) {
    922        1.1   mycroft 		cg += i;
    923        1.1   mycroft 		if (cg >= fs->fs_ncg)
    924        1.1   mycroft 			cg -= fs->fs_ncg;
    925  1.151.2.1  pgoyette 		result = (*allocator)(ip, cg, 0, size, realsize, flags);
    926        1.1   mycroft 		if (result)
    927        1.1   mycroft 			return (result);
    928        1.1   mycroft 	}
    929        1.1   mycroft 	/*
    930        1.1   mycroft 	 * 3: brute force search
    931        1.1   mycroft 	 * Note that we start at i == 2, since 0 was checked initially,
    932        1.1   mycroft 	 * and 1 is always checked in the quadratic rehash.
    933        1.1   mycroft 	 */
    934        1.1   mycroft 	cg = (icg + 2) % fs->fs_ncg;
    935        1.1   mycroft 	for (i = 2; i < fs->fs_ncg; i++) {
    936  1.151.2.1  pgoyette 		result = (*allocator)(ip, cg, 0, size, realsize, flags);
    937        1.1   mycroft 		if (result)
    938        1.1   mycroft 			return (result);
    939        1.1   mycroft 		cg++;
    940        1.1   mycroft 		if (cg == fs->fs_ncg)
    941        1.1   mycroft 			cg = 0;
    942        1.1   mycroft 	}
    943       1.35   thorpej 	return (0);
    944        1.1   mycroft }
    945        1.1   mycroft 
    946        1.1   mycroft /*
    947        1.1   mycroft  * Determine whether a fragment can be extended.
    948        1.1   mycroft  *
    949       1.81     perry  * Check to see if the necessary fragments are available, and
    950        1.1   mycroft  * if they are, allocate them.
    951      1.106     pooka  *
    952      1.106     pooka  * => called with um_lock held
    953      1.106     pooka  * => returns with um_lock released on success, held on failure
    954        1.1   mycroft  */
    955       1.58      fvdl static daddr_t
    956       1.85   thorpej ffs_fragextend(struct inode *ip, int cg, daddr_t bprev, int osize, int nsize)
    957        1.1   mycroft {
    958      1.101        ad 	struct ufsmount *ump;
    959       1.33  augustss 	struct fs *fs;
    960       1.33  augustss 	struct cg *cgp;
    961        1.1   mycroft 	struct buf *bp;
    962       1.58      fvdl 	daddr_t bno;
    963        1.1   mycroft 	int frags, bbase;
    964        1.1   mycroft 	int i, error;
    965       1.62      fvdl 	u_int8_t *blksfree;
    966        1.1   mycroft 
    967        1.1   mycroft 	fs = ip->i_fs;
    968      1.101        ad 	ump = ip->i_ump;
    969      1.101        ad 
    970      1.101        ad 	KASSERT(mutex_owned(&ump->um_lock));
    971      1.101        ad 
    972      1.137  dholland 	if (fs->fs_cs(fs, cg).cs_nffree < ffs_numfrags(fs, nsize - osize))
    973       1.35   thorpej 		return (0);
    974      1.137  dholland 	frags = ffs_numfrags(fs, nsize);
    975      1.138  dholland 	bbase = ffs_fragnum(fs, bprev);
    976      1.138  dholland 	if (bbase > ffs_fragnum(fs, (bprev + frags - 1))) {
    977        1.1   mycroft 		/* cannot extend across a block boundary */
    978       1.35   thorpej 		return (0);
    979        1.1   mycroft 	}
    980      1.101        ad 	mutex_exit(&ump->um_lock);
    981      1.136  dholland 	error = bread(ip->i_devvp, FFS_FSBTODB(fs, cgtod(fs, cg)),
    982      1.149      maxv 		(int)fs->fs_cgsize, B_MODIFY, &bp);
    983      1.101        ad 	if (error)
    984      1.101        ad 		goto fail;
    985        1.1   mycroft 	cgp = (struct cg *)bp->b_data;
    986      1.101        ad 	if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs)))
    987      1.101        ad 		goto fail;
    988       1.92    kardel 	cgp->cg_old_time = ufs_rw32(time_second, UFS_FSNEEDSWAP(fs));
    989       1.73       dbj 	if ((fs->fs_magic != FS_UFS1_MAGIC) ||
    990       1.73       dbj 	    (fs->fs_old_flags & FS_FLAGS_UPDATED))
    991       1.92    kardel 		cgp->cg_time = ufs_rw64(time_second, UFS_FSNEEDSWAP(fs));
    992        1.1   mycroft 	bno = dtogd(fs, bprev);
    993       1.62      fvdl 	blksfree = cg_blksfree(cgp, UFS_FSNEEDSWAP(fs));
    994      1.137  dholland 	for (i = ffs_numfrags(fs, osize); i < frags; i++)
    995      1.101        ad 		if (isclr(blksfree, bno + i))
    996      1.101        ad 			goto fail;
    997        1.1   mycroft 	/*
    998        1.1   mycroft 	 * the current fragment can be extended
    999        1.1   mycroft 	 * deduct the count on fragment being extended into
   1000        1.1   mycroft 	 * increase the count on the remaining fragment (if any)
   1001        1.1   mycroft 	 * allocate the extended piece
   1002        1.1   mycroft 	 */
   1003        1.1   mycroft 	for (i = frags; i < fs->fs_frag - bbase; i++)
   1004       1.62      fvdl 		if (isclr(blksfree, bno + i))
   1005        1.1   mycroft 			break;
   1006      1.137  dholland 	ufs_add32(cgp->cg_frsum[i - ffs_numfrags(fs, osize)], -1, UFS_FSNEEDSWAP(fs));
   1007        1.1   mycroft 	if (i != frags)
   1008       1.30      fvdl 		ufs_add32(cgp->cg_frsum[i - frags], 1, UFS_FSNEEDSWAP(fs));
   1009      1.101        ad 	mutex_enter(&ump->um_lock);
   1010      1.137  dholland 	for (i = ffs_numfrags(fs, osize); i < frags; i++) {
   1011       1.62      fvdl 		clrbit(blksfree, bno + i);
   1012       1.30      fvdl 		ufs_add32(cgp->cg_cs.cs_nffree, -1, UFS_FSNEEDSWAP(fs));
   1013        1.1   mycroft 		fs->fs_cstotal.cs_nffree--;
   1014        1.1   mycroft 		fs->fs_cs(fs, cg).cs_nffree--;
   1015        1.1   mycroft 	}
   1016        1.1   mycroft 	fs->fs_fmod = 1;
   1017      1.101        ad 	ACTIVECG_CLR(fs, cg);
   1018      1.101        ad 	mutex_exit(&ump->um_lock);
   1019        1.1   mycroft 	bdwrite(bp);
   1020        1.1   mycroft 	return (bprev);
   1021      1.101        ad 
   1022      1.101        ad  fail:
   1023      1.132   hannken  	if (bp != NULL)
   1024      1.132   hannken 		brelse(bp, 0);
   1025      1.101        ad  	mutex_enter(&ump->um_lock);
   1026      1.101        ad  	return (0);
   1027        1.1   mycroft }
   1028        1.1   mycroft 
   1029        1.1   mycroft /*
   1030        1.1   mycroft  * Determine whether a block can be allocated.
   1031        1.1   mycroft  *
   1032        1.1   mycroft  * Check to see if a block of the appropriate size is available,
   1033        1.1   mycroft  * and if it is, allocate it.
   1034        1.1   mycroft  */
   1035       1.58      fvdl static daddr_t
   1036  1.151.2.1  pgoyette ffs_alloccg(struct inode *ip, int cg, daddr_t bpref, int size, int realsize,
   1037  1.151.2.1  pgoyette     int flags)
   1038        1.1   mycroft {
   1039      1.101        ad 	struct ufsmount *ump;
   1040       1.62      fvdl 	struct fs *fs = ip->i_fs;
   1041       1.30      fvdl 	struct cg *cgp;
   1042        1.1   mycroft 	struct buf *bp;
   1043       1.60      fvdl 	int32_t bno;
   1044       1.60      fvdl 	daddr_t blkno;
   1045       1.30      fvdl 	int error, frags, allocsiz, i;
   1046       1.62      fvdl 	u_int8_t *blksfree;
   1047       1.30      fvdl 	const int needswap = UFS_FSNEEDSWAP(fs);
   1048        1.1   mycroft 
   1049      1.101        ad 	ump = ip->i_ump;
   1050      1.101        ad 
   1051      1.101        ad 	KASSERT(mutex_owned(&ump->um_lock));
   1052      1.101        ad 
   1053        1.1   mycroft 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
   1054       1.35   thorpej 		return (0);
   1055      1.101        ad 	mutex_exit(&ump->um_lock);
   1056      1.136  dholland 	error = bread(ip->i_devvp, FFS_FSBTODB(fs, cgtod(fs, cg)),
   1057      1.149      maxv 		(int)fs->fs_cgsize, B_MODIFY, &bp);
   1058      1.101        ad 	if (error)
   1059      1.101        ad 		goto fail;
   1060        1.1   mycroft 	cgp = (struct cg *)bp->b_data;
   1061       1.19    bouyer 	if (!cg_chkmagic(cgp, needswap) ||
   1062      1.101        ad 	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize))
   1063      1.101        ad 		goto fail;
   1064       1.92    kardel 	cgp->cg_old_time = ufs_rw32(time_second, needswap);
   1065       1.73       dbj 	if ((fs->fs_magic != FS_UFS1_MAGIC) ||
   1066       1.73       dbj 	    (fs->fs_old_flags & FS_FLAGS_UPDATED))
   1067       1.92    kardel 		cgp->cg_time = ufs_rw64(time_second, needswap);
   1068        1.1   mycroft 	if (size == fs->fs_bsize) {
   1069      1.101        ad 		mutex_enter(&ump->um_lock);
   1070  1.151.2.1  pgoyette 		blkno = ffs_alloccgblk(ip, bp, bpref, realsize, flags);
   1071       1.76   hannken 		ACTIVECG_CLR(fs, cg);
   1072      1.101        ad 		mutex_exit(&ump->um_lock);
   1073  1.151.2.1  pgoyette 
   1074  1.151.2.1  pgoyette 		/*
   1075  1.151.2.1  pgoyette 		 * If actually needed size is lower, free the extra blocks now.
   1076  1.151.2.1  pgoyette 		 * This is safe to call here, there is no outside reference
   1077  1.151.2.1  pgoyette 		 * to this block yet. It is not necessary to keep um_lock
   1078  1.151.2.1  pgoyette 		 * locked.
   1079  1.151.2.1  pgoyette 		 */
   1080  1.151.2.1  pgoyette 		if (realsize != 0 && realsize < size) {
   1081  1.151.2.1  pgoyette 			ffs_blkfree_common(ip->i_ump, ip->i_fs,
   1082  1.151.2.1  pgoyette 			    ip->i_devvp->v_rdev,
   1083  1.151.2.1  pgoyette 			    bp, blkno + ffs_numfrags(fs, realsize),
   1084  1.151.2.1  pgoyette 			    (long)(size - realsize), false);
   1085  1.151.2.1  pgoyette 		}
   1086  1.151.2.1  pgoyette 
   1087        1.1   mycroft 		bdwrite(bp);
   1088       1.60      fvdl 		return (blkno);
   1089        1.1   mycroft 	}
   1090        1.1   mycroft 	/*
   1091        1.1   mycroft 	 * check to see if any fragments are already available
   1092        1.1   mycroft 	 * allocsiz is the size which will be allocated, hacking
   1093        1.1   mycroft 	 * it down to a smaller size if necessary
   1094        1.1   mycroft 	 */
   1095       1.62      fvdl 	blksfree = cg_blksfree(cgp, needswap);
   1096      1.137  dholland 	frags = ffs_numfrags(fs, size);
   1097        1.1   mycroft 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
   1098        1.1   mycroft 		if (cgp->cg_frsum[allocsiz] != 0)
   1099        1.1   mycroft 			break;
   1100        1.1   mycroft 	if (allocsiz == fs->fs_frag) {
   1101        1.1   mycroft 		/*
   1102       1.81     perry 		 * no fragments were available, so a block will be
   1103        1.1   mycroft 		 * allocated, and hacked up
   1104        1.1   mycroft 		 */
   1105      1.101        ad 		if (cgp->cg_cs.cs_nbfree == 0)
   1106      1.101        ad 			goto fail;
   1107      1.101        ad 		mutex_enter(&ump->um_lock);
   1108  1.151.2.1  pgoyette 		blkno = ffs_alloccgblk(ip, bp, bpref, realsize, flags);
   1109       1.60      fvdl 		bno = dtogd(fs, blkno);
   1110        1.1   mycroft 		for (i = frags; i < fs->fs_frag; i++)
   1111       1.62      fvdl 			setbit(blksfree, bno + i);
   1112        1.1   mycroft 		i = fs->fs_frag - frags;
   1113       1.19    bouyer 		ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
   1114        1.1   mycroft 		fs->fs_cstotal.cs_nffree += i;
   1115       1.30      fvdl 		fs->fs_cs(fs, cg).cs_nffree += i;
   1116        1.1   mycroft 		fs->fs_fmod = 1;
   1117       1.19    bouyer 		ufs_add32(cgp->cg_frsum[i], 1, needswap);
   1118       1.76   hannken 		ACTIVECG_CLR(fs, cg);
   1119      1.101        ad 		mutex_exit(&ump->um_lock);
   1120        1.1   mycroft 		bdwrite(bp);
   1121       1.60      fvdl 		return (blkno);
   1122        1.1   mycroft 	}
   1123       1.30      fvdl 	bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
   1124       1.30      fvdl #if 0
   1125       1.30      fvdl 	/*
   1126       1.30      fvdl 	 * XXX fvdl mapsearch will panic, and never return -1
   1127       1.58      fvdl 	 *          also: returning NULL as daddr_t ?
   1128       1.30      fvdl 	 */
   1129      1.101        ad 	if (bno < 0)
   1130      1.101        ad 		goto fail;
   1131       1.30      fvdl #endif
   1132        1.1   mycroft 	for (i = 0; i < frags; i++)
   1133       1.62      fvdl 		clrbit(blksfree, bno + i);
   1134      1.101        ad 	mutex_enter(&ump->um_lock);
   1135       1.19    bouyer 	ufs_add32(cgp->cg_cs.cs_nffree, -frags, needswap);
   1136        1.1   mycroft 	fs->fs_cstotal.cs_nffree -= frags;
   1137        1.1   mycroft 	fs->fs_cs(fs, cg).cs_nffree -= frags;
   1138        1.1   mycroft 	fs->fs_fmod = 1;
   1139       1.19    bouyer 	ufs_add32(cgp->cg_frsum[allocsiz], -1, needswap);
   1140        1.1   mycroft 	if (frags != allocsiz)
   1141       1.19    bouyer 		ufs_add32(cgp->cg_frsum[allocsiz - frags], 1, needswap);
   1142      1.123  sborrill 	blkno = cgbase(fs, cg) + bno;
   1143      1.101        ad 	ACTIVECG_CLR(fs, cg);
   1144      1.101        ad 	mutex_exit(&ump->um_lock);
   1145        1.1   mycroft 	bdwrite(bp);
   1146       1.30      fvdl 	return blkno;
   1147      1.101        ad 
   1148      1.101        ad  fail:
   1149      1.132   hannken  	if (bp != NULL)
   1150      1.132   hannken 		brelse(bp, 0);
   1151      1.101        ad  	mutex_enter(&ump->um_lock);
   1152      1.101        ad  	return (0);
   1153        1.1   mycroft }
   1154        1.1   mycroft 
   1155        1.1   mycroft /*
   1156        1.1   mycroft  * Allocate a block in a cylinder group.
   1157        1.1   mycroft  *
   1158        1.1   mycroft  * This algorithm implements the following policy:
   1159        1.1   mycroft  *   1) allocate the requested block.
   1160        1.1   mycroft  *   2) allocate a rotationally optimal block in the same cylinder.
   1161        1.1   mycroft  *   3) allocate the next available block on the block rotor for the
   1162        1.1   mycroft  *      specified cylinder group.
   1163        1.1   mycroft  * Note that this routine only allocates fs_bsize blocks; these
   1164        1.1   mycroft  * blocks may be fragmented by the routine that allocates them.
   1165        1.1   mycroft  */
   1166       1.58      fvdl static daddr_t
   1167  1.151.2.1  pgoyette ffs_alloccgblk(struct inode *ip, struct buf *bp, daddr_t bpref, int realsize,
   1168  1.151.2.1  pgoyette     int flags)
   1169        1.1   mycroft {
   1170       1.62      fvdl 	struct fs *fs = ip->i_fs;
   1171       1.30      fvdl 	struct cg *cgp;
   1172      1.123  sborrill 	int cg;
   1173       1.60      fvdl 	daddr_t blkno;
   1174       1.60      fvdl 	int32_t bno;
   1175       1.60      fvdl 	u_int8_t *blksfree;
   1176       1.30      fvdl 	const int needswap = UFS_FSNEEDSWAP(fs);
   1177        1.1   mycroft 
   1178      1.141    martin 	KASSERT(mutex_owned(&ip->i_ump->um_lock));
   1179      1.101        ad 
   1180       1.30      fvdl 	cgp = (struct cg *)bp->b_data;
   1181       1.60      fvdl 	blksfree = cg_blksfree(cgp, needswap);
   1182       1.30      fvdl 	if (bpref == 0 || dtog(fs, bpref) != ufs_rw32(cgp->cg_cgx, needswap)) {
   1183       1.19    bouyer 		bpref = ufs_rw32(cgp->cg_rotor, needswap);
   1184       1.60      fvdl 	} else {
   1185      1.138  dholland 		bpref = ffs_blknum(fs, bpref);
   1186       1.60      fvdl 		bno = dtogd(fs, bpref);
   1187        1.1   mycroft 		/*
   1188       1.60      fvdl 		 * if the requested block is available, use it
   1189        1.1   mycroft 		 */
   1190      1.138  dholland 		if (ffs_isblock(fs, blksfree, ffs_fragstoblks(fs, bno)))
   1191       1.60      fvdl 			goto gotit;
   1192      1.111    simonb 		/*
   1193      1.111    simonb 		 * if the requested data block isn't available and we are
   1194      1.111    simonb 		 * trying to allocate a contiguous file, return an error.
   1195      1.111    simonb 		 */
   1196      1.111    simonb 		if ((flags & (B_CONTIG | B_METAONLY)) == B_CONTIG)
   1197      1.111    simonb 			return (0);
   1198        1.1   mycroft 	}
   1199      1.111    simonb 
   1200        1.1   mycroft 	/*
   1201       1.60      fvdl 	 * Take the next available block in this cylinder group.
   1202        1.1   mycroft 	 */
   1203       1.30      fvdl 	bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
   1204  1.151.2.1  pgoyette #if 0
   1205  1.151.2.1  pgoyette 	/*
   1206  1.151.2.1  pgoyette 	 * XXX jdolecek ffs_mapsearch() succeeds or panics
   1207  1.151.2.1  pgoyette 	 */
   1208        1.1   mycroft 	if (bno < 0)
   1209       1.35   thorpej 		return (0);
   1210  1.151.2.1  pgoyette #endif
   1211       1.60      fvdl 	cgp->cg_rotor = ufs_rw32(bno, needswap);
   1212        1.1   mycroft gotit:
   1213      1.138  dholland 	blkno = ffs_fragstoblks(fs, bno);
   1214       1.60      fvdl 	ffs_clrblock(fs, blksfree, blkno);
   1215       1.30      fvdl 	ffs_clusteracct(fs, cgp, blkno, -1);
   1216       1.19    bouyer 	ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap);
   1217        1.1   mycroft 	fs->fs_cstotal.cs_nbfree--;
   1218       1.19    bouyer 	fs->fs_cs(fs, ufs_rw32(cgp->cg_cgx, needswap)).cs_nbfree--;
   1219       1.73       dbj 	if ((fs->fs_magic == FS_UFS1_MAGIC) &&
   1220       1.73       dbj 	    ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0)) {
   1221       1.73       dbj 		int cylno;
   1222       1.73       dbj 		cylno = old_cbtocylno(fs, bno);
   1223       1.75       dbj 		KASSERT(cylno >= 0);
   1224       1.75       dbj 		KASSERT(cylno < fs->fs_old_ncyl);
   1225       1.75       dbj 		KASSERT(old_cbtorpos(fs, bno) >= 0);
   1226       1.75       dbj 		KASSERT(fs->fs_old_nrpos == 0 || old_cbtorpos(fs, bno) < fs->fs_old_nrpos);
   1227       1.73       dbj 		ufs_add16(old_cg_blks(fs, cgp, cylno, needswap)[old_cbtorpos(fs, bno)], -1,
   1228       1.73       dbj 		    needswap);
   1229       1.73       dbj 		ufs_add32(old_cg_blktot(cgp, needswap)[cylno], -1, needswap);
   1230       1.73       dbj 	}
   1231        1.1   mycroft 	fs->fs_fmod = 1;
   1232      1.123  sborrill 	cg = ufs_rw32(cgp->cg_cgx, needswap);
   1233      1.123  sborrill 	blkno = cgbase(fs, cg) + bno;
   1234       1.30      fvdl 	return (blkno);
   1235        1.1   mycroft }
   1236        1.1   mycroft 
   1237        1.1   mycroft /*
   1238        1.1   mycroft  * Determine whether an inode can be allocated.
   1239        1.1   mycroft  *
   1240        1.1   mycroft  * Check to see if an inode is available, and if it is,
   1241        1.1   mycroft  * allocate it using the following policy:
   1242        1.1   mycroft  *   1) allocate the requested inode.
   1243        1.1   mycroft  *   2) allocate the next available inode after the requested
   1244        1.1   mycroft  *      inode in the specified cylinder group.
   1245        1.1   mycroft  */
   1246       1.58      fvdl static daddr_t
   1247  1.151.2.1  pgoyette ffs_nodealloccg(struct inode *ip, int cg, daddr_t ipref, int mode, int realsize,
   1248  1.151.2.1  pgoyette     int flags)
   1249        1.1   mycroft {
   1250      1.101        ad 	struct ufsmount *ump = ip->i_ump;
   1251       1.62      fvdl 	struct fs *fs = ip->i_fs;
   1252       1.33  augustss 	struct cg *cgp;
   1253       1.60      fvdl 	struct buf *bp, *ibp;
   1254       1.60      fvdl 	u_int8_t *inosused;
   1255        1.1   mycroft 	int error, start, len, loc, map, i;
   1256       1.60      fvdl 	int32_t initediblk;
   1257      1.112   hannken 	daddr_t nalloc;
   1258       1.60      fvdl 	struct ufs2_dinode *dp2;
   1259       1.30      fvdl 	const int needswap = UFS_FSNEEDSWAP(fs);
   1260        1.1   mycroft 
   1261      1.101        ad 	KASSERT(mutex_owned(&ump->um_lock));
   1262      1.111    simonb 	UFS_WAPBL_JLOCK_ASSERT(ip->i_ump->um_mountp);
   1263      1.101        ad 
   1264        1.1   mycroft 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
   1265       1.35   thorpej 		return (0);
   1266      1.101        ad 	mutex_exit(&ump->um_lock);
   1267      1.112   hannken 	ibp = NULL;
   1268      1.112   hannken 	initediblk = -1;
   1269      1.112   hannken retry:
   1270      1.136  dholland 	error = bread(ip->i_devvp, FFS_FSBTODB(fs, cgtod(fs, cg)),
   1271      1.149      maxv 		(int)fs->fs_cgsize, B_MODIFY, &bp);
   1272      1.101        ad 	if (error)
   1273      1.101        ad 		goto fail;
   1274        1.1   mycroft 	cgp = (struct cg *)bp->b_data;
   1275      1.101        ad 	if (!cg_chkmagic(cgp, needswap) || cgp->cg_cs.cs_nifree == 0)
   1276      1.101        ad 		goto fail;
   1277      1.112   hannken 
   1278      1.112   hannken 	if (ibp != NULL &&
   1279      1.112   hannken 	    initediblk != ufs_rw32(cgp->cg_initediblk, needswap)) {
   1280      1.112   hannken 		/* Another thread allocated more inodes so we retry the test. */
   1281      1.121        ad 		brelse(ibp, 0);
   1282      1.112   hannken 		ibp = NULL;
   1283      1.112   hannken 	}
   1284      1.112   hannken 	/*
   1285      1.112   hannken 	 * Check to see if we need to initialize more inodes.
   1286      1.112   hannken 	 */
   1287      1.112   hannken 	if (fs->fs_magic == FS_UFS2_MAGIC && ibp == NULL) {
   1288      1.112   hannken 		initediblk = ufs_rw32(cgp->cg_initediblk, needswap);
   1289      1.112   hannken 		nalloc = fs->fs_ipg - ufs_rw32(cgp->cg_cs.cs_nifree, needswap);
   1290      1.134  dholland 		if (nalloc + FFS_INOPB(fs) > initediblk &&
   1291      1.112   hannken 		    initediblk < ufs_rw32(cgp->cg_niblk, needswap)) {
   1292      1.112   hannken 			/*
   1293      1.112   hannken 			 * We have to release the cg buffer here to prevent
   1294      1.112   hannken 			 * a deadlock when reading the inode block will
   1295      1.112   hannken 			 * run a copy-on-write that might use this cg.
   1296      1.112   hannken 			 */
   1297      1.112   hannken 			brelse(bp, 0);
   1298      1.112   hannken 			bp = NULL;
   1299      1.136  dholland 			error = ffs_getblk(ip->i_devvp, FFS_FSBTODB(fs,
   1300      1.112   hannken 			    ino_to_fsba(fs, cg * fs->fs_ipg + initediblk)),
   1301      1.112   hannken 			    FFS_NOBLK, fs->fs_bsize, false, &ibp);
   1302      1.112   hannken 			if (error)
   1303      1.112   hannken 				goto fail;
   1304      1.112   hannken 			goto retry;
   1305      1.112   hannken 		}
   1306      1.112   hannken 	}
   1307      1.112   hannken 
   1308       1.92    kardel 	cgp->cg_old_time = ufs_rw32(time_second, needswap);
   1309       1.73       dbj 	if ((fs->fs_magic != FS_UFS1_MAGIC) ||
   1310       1.73       dbj 	    (fs->fs_old_flags & FS_FLAGS_UPDATED))
   1311       1.92    kardel 		cgp->cg_time = ufs_rw64(time_second, needswap);
   1312       1.60      fvdl 	inosused = cg_inosused(cgp, needswap);
   1313        1.1   mycroft 	if (ipref) {
   1314        1.1   mycroft 		ipref %= fs->fs_ipg;
   1315       1.60      fvdl 		if (isclr(inosused, ipref))
   1316        1.1   mycroft 			goto gotit;
   1317        1.1   mycroft 	}
   1318       1.19    bouyer 	start = ufs_rw32(cgp->cg_irotor, needswap) / NBBY;
   1319       1.19    bouyer 	len = howmany(fs->fs_ipg - ufs_rw32(cgp->cg_irotor, needswap),
   1320       1.19    bouyer 		NBBY);
   1321       1.60      fvdl 	loc = skpc(0xff, len, &inosused[start]);
   1322        1.1   mycroft 	if (loc == 0) {
   1323        1.1   mycroft 		len = start + 1;
   1324        1.1   mycroft 		start = 0;
   1325       1.60      fvdl 		loc = skpc(0xff, len, &inosused[0]);
   1326        1.1   mycroft 		if (loc == 0) {
   1327  1.151.2.1  pgoyette 			panic("%s: map corrupted: cg=%d, irotor=%d, fs=%s",
   1328  1.151.2.1  pgoyette 			    __func__, cg, ufs_rw32(cgp->cg_irotor, needswap),
   1329  1.151.2.1  pgoyette 			    fs->fs_fsmnt);
   1330        1.1   mycroft 			/* NOTREACHED */
   1331        1.1   mycroft 		}
   1332        1.1   mycroft 	}
   1333        1.1   mycroft 	i = start + len - loc;
   1334      1.126     rmind 	map = inosused[i] ^ 0xff;
   1335      1.126     rmind 	if (map == 0) {
   1336  1.151.2.1  pgoyette 		panic("%s: block not in map: fs=%s", __func__, fs->fs_fsmnt);
   1337        1.1   mycroft 	}
   1338      1.126     rmind 	ipref = i * NBBY + ffs(map) - 1;
   1339      1.126     rmind 	cgp->cg_irotor = ufs_rw32(ipref, needswap);
   1340        1.1   mycroft gotit:
   1341      1.111    simonb 	UFS_WAPBL_REGISTER_INODE(ip->i_ump->um_mountp, cg * fs->fs_ipg + ipref,
   1342      1.111    simonb 	    mode);
   1343       1.60      fvdl 	/*
   1344       1.60      fvdl 	 * Check to see if we need to initialize more inodes.
   1345       1.60      fvdl 	 */
   1346      1.112   hannken 	if (ibp != NULL) {
   1347      1.112   hannken 		KASSERT(initediblk == ufs_rw32(cgp->cg_initediblk, needswap));
   1348      1.108   hannken 		memset(ibp->b_data, 0, fs->fs_bsize);
   1349      1.108   hannken 		dp2 = (struct ufs2_dinode *)(ibp->b_data);
   1350      1.134  dholland 		for (i = 0; i < FFS_INOPB(fs); i++) {
   1351       1.60      fvdl 			/*
   1352       1.60      fvdl 			 * Don't bother to swap, it's supposed to be
   1353       1.60      fvdl 			 * random, after all.
   1354       1.60      fvdl 			 */
   1355      1.130       tls 			dp2->di_gen = (cprng_fast32() & INT32_MAX) / 2 + 1;
   1356       1.60      fvdl 			dp2++;
   1357       1.60      fvdl 		}
   1358      1.134  dholland 		initediblk += FFS_INOPB(fs);
   1359       1.60      fvdl 		cgp->cg_initediblk = ufs_rw32(initediblk, needswap);
   1360       1.60      fvdl 	}
   1361       1.60      fvdl 
   1362      1.101        ad 	mutex_enter(&ump->um_lock);
   1363       1.76   hannken 	ACTIVECG_CLR(fs, cg);
   1364      1.101        ad 	setbit(inosused, ipref);
   1365      1.101        ad 	ufs_add32(cgp->cg_cs.cs_nifree, -1, needswap);
   1366      1.101        ad 	fs->fs_cstotal.cs_nifree--;
   1367      1.101        ad 	fs->fs_cs(fs, cg).cs_nifree--;
   1368      1.101        ad 	fs->fs_fmod = 1;
   1369      1.101        ad 	if ((mode & IFMT) == IFDIR) {
   1370      1.101        ad 		ufs_add32(cgp->cg_cs.cs_ndir, 1, needswap);
   1371      1.101        ad 		fs->fs_cstotal.cs_ndir++;
   1372      1.101        ad 		fs->fs_cs(fs, cg).cs_ndir++;
   1373      1.101        ad 	}
   1374      1.101        ad 	mutex_exit(&ump->um_lock);
   1375      1.112   hannken 	if (ibp != NULL) {
   1376      1.112   hannken 		bwrite(bp);
   1377      1.104   hannken 		bawrite(ibp);
   1378      1.112   hannken 	} else
   1379      1.112   hannken 		bdwrite(bp);
   1380        1.1   mycroft 	return (cg * fs->fs_ipg + ipref);
   1381      1.101        ad  fail:
   1382      1.112   hannken 	if (bp != NULL)
   1383      1.112   hannken 		brelse(bp, 0);
   1384      1.112   hannken 	if (ibp != NULL)
   1385      1.121        ad 		brelse(ibp, 0);
   1386      1.101        ad 	mutex_enter(&ump->um_lock);
   1387      1.101        ad 	return (0);
   1388        1.1   mycroft }
   1389        1.1   mycroft 
   1390        1.1   mycroft /*
   1391      1.111    simonb  * Allocate a block or fragment.
   1392      1.111    simonb  *
   1393      1.111    simonb  * The specified block or fragment is removed from the
   1394      1.111    simonb  * free map, possibly fragmenting a block in the process.
   1395      1.111    simonb  *
   1396      1.111    simonb  * This implementation should mirror fs_blkfree
   1397      1.111    simonb  *
   1398      1.111    simonb  * => um_lock not held on entry or exit
   1399      1.111    simonb  */
   1400      1.111    simonb int
   1401      1.111    simonb ffs_blkalloc(struct inode *ip, daddr_t bno, long size)
   1402      1.111    simonb {
   1403      1.116     joerg 	int error;
   1404      1.111    simonb 
   1405      1.116     joerg 	error = ffs_check_bad_allocation(__func__, ip->i_fs, bno, size,
   1406      1.116     joerg 	    ip->i_dev, ip->i_uid);
   1407      1.116     joerg 	if (error)
   1408      1.116     joerg 		return error;
   1409      1.115     joerg 
   1410      1.115     joerg 	return ffs_blkalloc_ump(ip->i_ump, bno, size);
   1411      1.115     joerg }
   1412      1.115     joerg 
   1413      1.115     joerg int
   1414      1.115     joerg ffs_blkalloc_ump(struct ufsmount *ump, daddr_t bno, long size)
   1415      1.115     joerg {
   1416      1.115     joerg 	struct fs *fs = ump->um_fs;
   1417      1.115     joerg 	struct cg *cgp;
   1418      1.115     joerg 	struct buf *bp;
   1419      1.115     joerg 	int32_t fragno, cgbno;
   1420      1.115     joerg 	int i, error, cg, blk, frags, bbase;
   1421      1.115     joerg 	u_int8_t *blksfree;
   1422      1.115     joerg 	const int needswap = UFS_FSNEEDSWAP(fs);
   1423      1.115     joerg 
   1424      1.134  dholland 	KASSERT((u_int)size <= fs->fs_bsize && ffs_fragoff(fs, size) == 0 &&
   1425      1.138  dholland 	    ffs_fragnum(fs, bno) + ffs_numfrags(fs, size) <= fs->fs_frag);
   1426      1.115     joerg 	KASSERT(bno < fs->fs_size);
   1427      1.115     joerg 
   1428      1.115     joerg 	cg = dtog(fs, bno);
   1429      1.136  dholland 	error = bread(ump->um_devvp, FFS_FSBTODB(fs, cgtod(fs, cg)),
   1430      1.149      maxv 		(int)fs->fs_cgsize, B_MODIFY, &bp);
   1431      1.111    simonb 	if (error) {
   1432      1.111    simonb 		return error;
   1433      1.111    simonb 	}
   1434      1.111    simonb 	cgp = (struct cg *)bp->b_data;
   1435      1.111    simonb 	if (!cg_chkmagic(cgp, needswap)) {
   1436      1.111    simonb 		brelse(bp, 0);
   1437      1.111    simonb 		return EIO;
   1438      1.111    simonb 	}
   1439      1.111    simonb 	cgp->cg_old_time = ufs_rw32(time_second, needswap);
   1440      1.111    simonb 	cgp->cg_time = ufs_rw64(time_second, needswap);
   1441      1.111    simonb 	cgbno = dtogd(fs, bno);
   1442      1.111    simonb 	blksfree = cg_blksfree(cgp, needswap);
   1443      1.111    simonb 
   1444      1.111    simonb 	mutex_enter(&ump->um_lock);
   1445      1.111    simonb 	if (size == fs->fs_bsize) {
   1446      1.138  dholland 		fragno = ffs_fragstoblks(fs, cgbno);
   1447      1.111    simonb 		if (!ffs_isblock(fs, blksfree, fragno)) {
   1448      1.111    simonb 			mutex_exit(&ump->um_lock);
   1449      1.111    simonb 			brelse(bp, 0);
   1450      1.111    simonb 			return EBUSY;
   1451      1.111    simonb 		}
   1452      1.111    simonb 		ffs_clrblock(fs, blksfree, fragno);
   1453      1.111    simonb 		ffs_clusteracct(fs, cgp, fragno, -1);
   1454      1.111    simonb 		ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap);
   1455      1.111    simonb 		fs->fs_cstotal.cs_nbfree--;
   1456      1.111    simonb 		fs->fs_cs(fs, cg).cs_nbfree--;
   1457      1.111    simonb 	} else {
   1458      1.138  dholland 		bbase = cgbno - ffs_fragnum(fs, cgbno);
   1459      1.111    simonb 
   1460      1.137  dholland 		frags = ffs_numfrags(fs, size);
   1461      1.111    simonb 		for (i = 0; i < frags; i++) {
   1462      1.111    simonb 			if (isclr(blksfree, cgbno + i)) {
   1463      1.111    simonb 				mutex_exit(&ump->um_lock);
   1464      1.111    simonb 				brelse(bp, 0);
   1465      1.111    simonb 				return EBUSY;
   1466      1.111    simonb 			}
   1467      1.111    simonb 		}
   1468      1.111    simonb 		/*
   1469      1.111    simonb 		 * if a complete block is being split, account for it
   1470      1.111    simonb 		 */
   1471      1.138  dholland 		fragno = ffs_fragstoblks(fs, bbase);
   1472      1.111    simonb 		if (ffs_isblock(fs, blksfree, fragno)) {
   1473      1.111    simonb 			ufs_add32(cgp->cg_cs.cs_nffree, fs->fs_frag, needswap);
   1474      1.111    simonb 			fs->fs_cstotal.cs_nffree += fs->fs_frag;
   1475      1.111    simonb 			fs->fs_cs(fs, cg).cs_nffree += fs->fs_frag;
   1476      1.111    simonb 			ffs_clusteracct(fs, cgp, fragno, -1);
   1477      1.111    simonb 			ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap);
   1478      1.111    simonb 			fs->fs_cstotal.cs_nbfree--;
   1479      1.111    simonb 			fs->fs_cs(fs, cg).cs_nbfree--;
   1480      1.111    simonb 		}
   1481      1.111    simonb 		/*
   1482      1.111    simonb 		 * decrement the counts associated with the old frags
   1483      1.111    simonb 		 */
   1484      1.111    simonb 		blk = blkmap(fs, blksfree, bbase);
   1485      1.111    simonb 		ffs_fragacct(fs, blk, cgp->cg_frsum, -1, needswap);
   1486      1.111    simonb 		/*
   1487      1.111    simonb 		 * allocate the fragment
   1488      1.111    simonb 		 */
   1489      1.111    simonb 		for (i = 0; i < frags; i++) {
   1490      1.111    simonb 			clrbit(blksfree, cgbno + i);
   1491      1.111    simonb 		}
   1492      1.111    simonb 		ufs_add32(cgp->cg_cs.cs_nffree, -i, needswap);
   1493      1.111    simonb 		fs->fs_cstotal.cs_nffree -= i;
   1494      1.111    simonb 		fs->fs_cs(fs, cg).cs_nffree -= i;
   1495      1.111    simonb 		/*
   1496      1.111    simonb 		 * add back in counts associated with the new frags
   1497      1.111    simonb 		 */
   1498      1.111    simonb 		blk = blkmap(fs, blksfree, bbase);
   1499      1.111    simonb 		ffs_fragacct(fs, blk, cgp->cg_frsum, 1, needswap);
   1500      1.111    simonb 	}
   1501      1.111    simonb 	fs->fs_fmod = 1;
   1502      1.111    simonb 	ACTIVECG_CLR(fs, cg);
   1503      1.111    simonb 	mutex_exit(&ump->um_lock);
   1504      1.111    simonb 	bdwrite(bp);
   1505      1.111    simonb 	return 0;
   1506      1.111    simonb }
   1507      1.111    simonb 
   1508      1.111    simonb /*
   1509        1.1   mycroft  * Free a block or fragment.
   1510        1.1   mycroft  *
   1511        1.1   mycroft  * The specified block or fragment is placed back in the
   1512       1.81     perry  * free map. If a fragment is deallocated, a possible
   1513        1.1   mycroft  * block reassembly is checked.
   1514      1.106     pooka  *
   1515      1.106     pooka  * => um_lock not held on entry or exit
   1516        1.1   mycroft  */
   1517      1.131  drochner static void
   1518      1.131  drochner ffs_blkfree_cg(struct fs *fs, struct vnode *devvp, daddr_t bno, long size)
   1519        1.1   mycroft {
   1520       1.33  augustss 	struct cg *cgp;
   1521        1.1   mycroft 	struct buf *bp;
   1522       1.76   hannken 	struct ufsmount *ump;
   1523       1.76   hannken 	daddr_t cgblkno;
   1524      1.116     joerg 	int error, cg;
   1525       1.76   hannken 	dev_t dev;
   1526      1.113   hannken 	const bool devvp_is_snapshot = (devvp->v_type != VBLK);
   1527      1.118     joerg 	const int needswap = UFS_FSNEEDSWAP(fs);
   1528        1.1   mycroft 
   1529      1.116     joerg 	KASSERT(!devvp_is_snapshot);
   1530      1.116     joerg 
   1531       1.76   hannken 	cg = dtog(fs, bno);
   1532      1.116     joerg 	dev = devvp->v_rdev;
   1533      1.140   hannken 	ump = VFSTOUFS(spec_node_getmountedfs(devvp));
   1534      1.119     joerg 	KASSERT(fs == ump->um_fs);
   1535      1.136  dholland 	cgblkno = FFS_FSBTODB(fs, cgtod(fs, cg));
   1536      1.116     joerg 
   1537      1.116     joerg 	error = bread(devvp, cgblkno, (int)fs->fs_cgsize,
   1538      1.149      maxv 	    B_MODIFY, &bp);
   1539      1.116     joerg 	if (error) {
   1540      1.116     joerg 		return;
   1541       1.76   hannken 	}
   1542      1.116     joerg 	cgp = (struct cg *)bp->b_data;
   1543      1.116     joerg 	if (!cg_chkmagic(cgp, needswap)) {
   1544      1.116     joerg 		brelse(bp, 0);
   1545      1.116     joerg 		return;
   1546        1.1   mycroft 	}
   1547       1.76   hannken 
   1548      1.119     joerg 	ffs_blkfree_common(ump, fs, dev, bp, bno, size, devvp_is_snapshot);
   1549      1.119     joerg 
   1550      1.119     joerg 	bdwrite(bp);
   1551      1.116     joerg }
   1552      1.116     joerg 
   1553      1.131  drochner struct discardopdata {
   1554      1.131  drochner 	struct work wk; /* must be first */
   1555      1.131  drochner 	struct vnode *devvp;
   1556      1.131  drochner 	daddr_t bno;
   1557      1.131  drochner 	long size;
   1558      1.131  drochner };
   1559      1.131  drochner 
   1560      1.131  drochner struct discarddata {
   1561      1.131  drochner 	struct fs *fs;
   1562      1.131  drochner 	struct discardopdata *entry;
   1563      1.131  drochner 	long maxsize;
   1564      1.131  drochner 	kmutex_t entrylk;
   1565      1.131  drochner 	struct workqueue *wq;
   1566      1.131  drochner 	int wqcnt, wqdraining;
   1567      1.131  drochner 	kmutex_t wqlk;
   1568      1.131  drochner 	kcondvar_t wqcv;
   1569      1.131  drochner 	/* timer for flush? */
   1570      1.131  drochner };
   1571      1.131  drochner 
   1572      1.131  drochner static void
   1573      1.131  drochner ffs_blkfree_td(struct fs *fs, struct discardopdata *td)
   1574      1.131  drochner {
   1575      1.151  riastrad 	struct mount *mp = spec_node_getmountedfs(td->devvp);
   1576      1.131  drochner 	long todo;
   1577      1.151  riastrad 	int error;
   1578      1.131  drochner 
   1579      1.131  drochner 	while (td->size) {
   1580      1.131  drochner 		todo = min(td->size,
   1581      1.138  dholland 		  ffs_lfragtosize(fs, (fs->fs_frag - ffs_fragnum(fs, td->bno))));
   1582      1.151  riastrad 		error = UFS_WAPBL_BEGIN(mp);
   1583      1.151  riastrad 		if (error) {
   1584      1.151  riastrad 			printf("ffs: failed to begin wapbl transaction"
   1585      1.151  riastrad 			    " for discard: %d\n", error);
   1586      1.151  riastrad 			break;
   1587      1.151  riastrad 		}
   1588      1.131  drochner 		ffs_blkfree_cg(fs, td->devvp, td->bno, todo);
   1589      1.151  riastrad 		UFS_WAPBL_END(mp);
   1590      1.137  dholland 		td->bno += ffs_numfrags(fs, todo);
   1591      1.131  drochner 		td->size -= todo;
   1592      1.131  drochner 	}
   1593      1.131  drochner }
   1594      1.131  drochner 
   1595      1.131  drochner static void
   1596      1.131  drochner ffs_discardcb(struct work *wk, void *arg)
   1597      1.131  drochner {
   1598      1.131  drochner 	struct discardopdata *td = (void *)wk;
   1599      1.131  drochner 	struct discarddata *ts = arg;
   1600      1.131  drochner 	struct fs *fs = ts->fs;
   1601      1.146  dholland 	off_t start, len;
   1602      1.139    martin #ifdef TRIMDEBUG
   1603      1.131  drochner 	int error;
   1604      1.139    martin #endif
   1605      1.131  drochner 
   1606      1.146  dholland /* like FSBTODB but emits bytes; XXX move to fs.h */
   1607      1.146  dholland #ifndef FFS_FSBTOBYTES
   1608      1.146  dholland #define FFS_FSBTOBYTES(fs, b) ((b) << (fs)->fs_fshift)
   1609      1.146  dholland #endif
   1610      1.146  dholland 
   1611      1.146  dholland 	start = FFS_FSBTOBYTES(fs, td->bno);
   1612      1.146  dholland 	len = td->size;
   1613      1.139    martin #ifdef TRIMDEBUG
   1614      1.139    martin 	error =
   1615      1.139    martin #endif
   1616      1.146  dholland 		VOP_FDISCARD(td->devvp, start, len);
   1617      1.131  drochner #ifdef TRIMDEBUG
   1618      1.131  drochner 	printf("trim(%" PRId64 ",%ld):%d\n", td->bno, td->size, error);
   1619      1.131  drochner #endif
   1620      1.131  drochner 
   1621      1.131  drochner 	ffs_blkfree_td(fs, td);
   1622      1.131  drochner 	kmem_free(td, sizeof(*td));
   1623      1.131  drochner 	mutex_enter(&ts->wqlk);
   1624      1.131  drochner 	ts->wqcnt--;
   1625      1.131  drochner 	if (ts->wqdraining && !ts->wqcnt)
   1626      1.131  drochner 		cv_signal(&ts->wqcv);
   1627      1.131  drochner 	mutex_exit(&ts->wqlk);
   1628      1.131  drochner }
   1629      1.131  drochner 
   1630      1.131  drochner void *
   1631      1.131  drochner ffs_discard_init(struct vnode *devvp, struct fs *fs)
   1632      1.131  drochner {
   1633      1.131  drochner 	struct discarddata *ts;
   1634      1.131  drochner 	int error;
   1635      1.131  drochner 
   1636      1.131  drochner 	ts = kmem_zalloc(sizeof (*ts), KM_SLEEP);
   1637      1.131  drochner 	error = workqueue_create(&ts->wq, "trimwq", ffs_discardcb, ts,
   1638      1.131  drochner 				 0, 0, 0);
   1639      1.131  drochner 	if (error) {
   1640      1.131  drochner 		kmem_free(ts, sizeof (*ts));
   1641      1.131  drochner 		return NULL;
   1642      1.131  drochner 	}
   1643      1.131  drochner 	mutex_init(&ts->entrylk, MUTEX_DEFAULT, IPL_NONE);
   1644      1.131  drochner 	mutex_init(&ts->wqlk, MUTEX_DEFAULT, IPL_NONE);
   1645      1.131  drochner 	cv_init(&ts->wqcv, "trimwqcv");
   1646      1.146  dholland 	ts->maxsize = 100*1024; /* XXX */
   1647      1.131  drochner 	ts->fs = fs;
   1648      1.131  drochner 	return ts;
   1649      1.131  drochner }
   1650      1.131  drochner 
   1651      1.131  drochner void
   1652      1.131  drochner ffs_discard_finish(void *vts, int flags)
   1653      1.131  drochner {
   1654      1.131  drochner 	struct discarddata *ts = vts;
   1655      1.131  drochner 	struct discardopdata *td = NULL;
   1656      1.131  drochner 	int res = 0;
   1657      1.131  drochner 
   1658      1.131  drochner 	/* wait for workqueue to drain */
   1659      1.131  drochner 	mutex_enter(&ts->wqlk);
   1660      1.131  drochner 	if (ts->wqcnt) {
   1661      1.131  drochner 		ts->wqdraining = 1;
   1662      1.131  drochner 		res = cv_timedwait(&ts->wqcv, &ts->wqlk, mstohz(5000));
   1663      1.131  drochner 	}
   1664      1.131  drochner 	mutex_exit(&ts->wqlk);
   1665      1.131  drochner 	if (res)
   1666      1.131  drochner 		printf("ffs_discarddata drain timeout\n");
   1667      1.131  drochner 
   1668      1.131  drochner 	mutex_enter(&ts->entrylk);
   1669      1.131  drochner 	if (ts->entry) {
   1670      1.131  drochner 		td = ts->entry;
   1671      1.131  drochner 		ts->entry = NULL;
   1672      1.131  drochner 	}
   1673      1.131  drochner 	mutex_exit(&ts->entrylk);
   1674      1.131  drochner 	if (td) {
   1675      1.131  drochner 		/* XXX don't tell disk, its optional */
   1676      1.131  drochner 		ffs_blkfree_td(ts->fs, td);
   1677      1.131  drochner #ifdef TRIMDEBUG
   1678      1.131  drochner 		printf("finish(%" PRId64 ",%ld)\n", td->bno, td->size);
   1679      1.131  drochner #endif
   1680      1.131  drochner 		kmem_free(td, sizeof(*td));
   1681      1.131  drochner 	}
   1682      1.131  drochner 
   1683      1.131  drochner 	cv_destroy(&ts->wqcv);
   1684      1.131  drochner 	mutex_destroy(&ts->entrylk);
   1685      1.131  drochner 	mutex_destroy(&ts->wqlk);
   1686      1.131  drochner 	workqueue_destroy(ts->wq);
   1687      1.131  drochner 	kmem_free(ts, sizeof(*ts));
   1688      1.131  drochner }
   1689      1.131  drochner 
   1690      1.131  drochner void
   1691      1.131  drochner ffs_blkfree(struct fs *fs, struct vnode *devvp, daddr_t bno, long size,
   1692      1.131  drochner     ino_t inum)
   1693      1.131  drochner {
   1694      1.131  drochner 	struct ufsmount *ump;
   1695      1.131  drochner 	int error;
   1696      1.131  drochner 	dev_t dev;
   1697      1.131  drochner 	struct discarddata *ts;
   1698      1.131  drochner 	struct discardopdata *td;
   1699      1.131  drochner 
   1700      1.131  drochner 	dev = devvp->v_rdev;
   1701      1.140   hannken 	ump = VFSTOUFS(spec_node_getmountedfs(devvp));
   1702      1.131  drochner 	if (ffs_snapblkfree(fs, devvp, bno, size, inum))
   1703      1.131  drochner 		return;
   1704      1.131  drochner 
   1705      1.131  drochner 	error = ffs_check_bad_allocation(__func__, fs, bno, size, dev, inum);
   1706      1.131  drochner 	if (error)
   1707      1.131  drochner 		return;
   1708      1.131  drochner 
   1709      1.131  drochner 	if (!ump->um_discarddata) {
   1710      1.131  drochner 		ffs_blkfree_cg(fs, devvp, bno, size);
   1711      1.131  drochner 		return;
   1712      1.131  drochner 	}
   1713      1.131  drochner 
   1714      1.131  drochner #ifdef TRIMDEBUG
   1715      1.131  drochner 	printf("blkfree(%" PRId64 ",%ld)\n", bno, size);
   1716      1.131  drochner #endif
   1717      1.131  drochner 	ts = ump->um_discarddata;
   1718      1.131  drochner 	td = NULL;
   1719      1.131  drochner 
   1720      1.131  drochner 	mutex_enter(&ts->entrylk);
   1721      1.131  drochner 	if (ts->entry) {
   1722      1.131  drochner 		td = ts->entry;
   1723      1.131  drochner 		/* ffs deallocs backwards, check for prepend only */
   1724      1.137  dholland 		if (td->bno == bno + ffs_numfrags(fs, size)
   1725      1.131  drochner 		    && td->size + size <= ts->maxsize) {
   1726      1.131  drochner 			td->bno = bno;
   1727      1.131  drochner 			td->size += size;
   1728      1.131  drochner 			if (td->size < ts->maxsize) {
   1729      1.131  drochner #ifdef TRIMDEBUG
   1730      1.131  drochner 				printf("defer(%" PRId64 ",%ld)\n", td->bno, td->size);
   1731      1.131  drochner #endif
   1732      1.131  drochner 				mutex_exit(&ts->entrylk);
   1733      1.131  drochner 				return;
   1734      1.131  drochner 			}
   1735      1.131  drochner 			size = 0; /* mark done */
   1736      1.131  drochner 		}
   1737      1.131  drochner 		ts->entry = NULL;
   1738      1.131  drochner 	}
   1739      1.131  drochner 	mutex_exit(&ts->entrylk);
   1740      1.131  drochner 
   1741      1.131  drochner 	if (td) {
   1742      1.131  drochner #ifdef TRIMDEBUG
   1743      1.131  drochner 		printf("enq old(%" PRId64 ",%ld)\n", td->bno, td->size);
   1744      1.131  drochner #endif
   1745      1.131  drochner 		mutex_enter(&ts->wqlk);
   1746      1.131  drochner 		ts->wqcnt++;
   1747      1.131  drochner 		mutex_exit(&ts->wqlk);
   1748      1.131  drochner 		workqueue_enqueue(ts->wq, &td->wk, NULL);
   1749      1.131  drochner 	}
   1750      1.131  drochner 	if (!size)
   1751      1.131  drochner 		return;
   1752      1.131  drochner 
   1753      1.131  drochner 	td = kmem_alloc(sizeof(*td), KM_SLEEP);
   1754      1.131  drochner 	td->devvp = devvp;
   1755      1.131  drochner 	td->bno = bno;
   1756      1.131  drochner 	td->size = size;
   1757      1.131  drochner 
   1758      1.131  drochner 	if (td->size < ts->maxsize) { /* XXX always the case */
   1759      1.131  drochner 		mutex_enter(&ts->entrylk);
   1760      1.131  drochner 		if (!ts->entry) { /* possible race? */
   1761      1.131  drochner #ifdef TRIMDEBUG
   1762      1.131  drochner 			printf("defer(%" PRId64 ",%ld)\n", td->bno, td->size);
   1763      1.131  drochner #endif
   1764      1.131  drochner 			ts->entry = td;
   1765      1.131  drochner 			td = NULL;
   1766      1.131  drochner 		}
   1767      1.131  drochner 		mutex_exit(&ts->entrylk);
   1768      1.131  drochner 	}
   1769      1.131  drochner 	if (td) {
   1770      1.131  drochner #ifdef TRIMDEBUG
   1771      1.131  drochner 		printf("enq new(%" PRId64 ",%ld)\n", td->bno, td->size);
   1772      1.131  drochner #endif
   1773      1.131  drochner 		mutex_enter(&ts->wqlk);
   1774      1.131  drochner 		ts->wqcnt++;
   1775      1.131  drochner 		mutex_exit(&ts->wqlk);
   1776      1.131  drochner 		workqueue_enqueue(ts->wq, &td->wk, NULL);
   1777      1.131  drochner 	}
   1778      1.131  drochner }
   1779      1.131  drochner 
   1780      1.116     joerg /*
   1781      1.116     joerg  * Free a block or fragment from a snapshot cg copy.
   1782      1.116     joerg  *
   1783      1.116     joerg  * The specified block or fragment is placed back in the
   1784      1.116     joerg  * free map. If a fragment is deallocated, a possible
   1785      1.116     joerg  * block reassembly is checked.
   1786      1.116     joerg  *
   1787      1.116     joerg  * => um_lock not held on entry or exit
   1788      1.116     joerg  */
   1789      1.116     joerg void
   1790      1.116     joerg ffs_blkfree_snap(struct fs *fs, struct vnode *devvp, daddr_t bno, long size,
   1791      1.116     joerg     ino_t inum)
   1792      1.116     joerg {
   1793      1.116     joerg 	struct cg *cgp;
   1794      1.116     joerg 	struct buf *bp;
   1795      1.116     joerg 	struct ufsmount *ump;
   1796      1.116     joerg 	daddr_t cgblkno;
   1797      1.116     joerg 	int error, cg;
   1798      1.116     joerg 	dev_t dev;
   1799      1.116     joerg 	const bool devvp_is_snapshot = (devvp->v_type != VBLK);
   1800      1.118     joerg 	const int needswap = UFS_FSNEEDSWAP(fs);
   1801      1.116     joerg 
   1802      1.116     joerg 	KASSERT(devvp_is_snapshot);
   1803      1.116     joerg 
   1804      1.116     joerg 	cg = dtog(fs, bno);
   1805      1.116     joerg 	dev = VTOI(devvp)->i_devvp->v_rdev;
   1806      1.116     joerg 	ump = VFSTOUFS(devvp->v_mount);
   1807      1.138  dholland 	cgblkno = ffs_fragstoblks(fs, cgtod(fs, cg));
   1808      1.116     joerg 
   1809      1.116     joerg 	error = ffs_check_bad_allocation(__func__, fs, bno, size, dev, inum);
   1810      1.116     joerg 	if (error)
   1811        1.1   mycroft 		return;
   1812      1.116     joerg 
   1813      1.107   hannken 	error = bread(devvp, cgblkno, (int)fs->fs_cgsize,
   1814      1.149      maxv 	    B_MODIFY, &bp);
   1815        1.1   mycroft 	if (error) {
   1816        1.1   mycroft 		return;
   1817        1.1   mycroft 	}
   1818        1.1   mycroft 	cgp = (struct cg *)bp->b_data;
   1819       1.19    bouyer 	if (!cg_chkmagic(cgp, needswap)) {
   1820      1.101        ad 		brelse(bp, 0);
   1821        1.1   mycroft 		return;
   1822        1.1   mycroft 	}
   1823      1.116     joerg 
   1824      1.119     joerg 	ffs_blkfree_common(ump, fs, dev, bp, bno, size, devvp_is_snapshot);
   1825      1.119     joerg 
   1826      1.119     joerg 	bdwrite(bp);
   1827      1.116     joerg }
   1828      1.116     joerg 
   1829      1.116     joerg static void
   1830      1.119     joerg ffs_blkfree_common(struct ufsmount *ump, struct fs *fs, dev_t dev,
   1831      1.119     joerg     struct buf *bp, daddr_t bno, long size, bool devvp_is_snapshot)
   1832      1.116     joerg {
   1833      1.116     joerg 	struct cg *cgp;
   1834      1.116     joerg 	int32_t fragno, cgbno;
   1835      1.116     joerg 	int i, cg, blk, frags, bbase;
   1836      1.116     joerg 	u_int8_t *blksfree;
   1837      1.116     joerg 	const int needswap = UFS_FSNEEDSWAP(fs);
   1838      1.116     joerg 
   1839      1.116     joerg 	cg = dtog(fs, bno);
   1840      1.116     joerg 	cgp = (struct cg *)bp->b_data;
   1841       1.92    kardel 	cgp->cg_old_time = ufs_rw32(time_second, needswap);
   1842       1.73       dbj 	if ((fs->fs_magic != FS_UFS1_MAGIC) ||
   1843       1.73       dbj 	    (fs->fs_old_flags & FS_FLAGS_UPDATED))
   1844       1.92    kardel 		cgp->cg_time = ufs_rw64(time_second, needswap);
   1845       1.60      fvdl 	cgbno = dtogd(fs, bno);
   1846       1.62      fvdl 	blksfree = cg_blksfree(cgp, needswap);
   1847      1.101        ad 	mutex_enter(&ump->um_lock);
   1848        1.1   mycroft 	if (size == fs->fs_bsize) {
   1849      1.138  dholland 		fragno = ffs_fragstoblks(fs, cgbno);
   1850       1.62      fvdl 		if (!ffs_isfreeblock(fs, blksfree, fragno)) {
   1851      1.113   hannken 			if (devvp_is_snapshot) {
   1852      1.101        ad 				mutex_exit(&ump->um_lock);
   1853       1.76   hannken 				return;
   1854       1.76   hannken 			}
   1855  1.151.2.1  pgoyette 			panic("%s: freeing free block: dev = 0x%llx, block = %"
   1856  1.151.2.1  pgoyette 			    PRId64 ", fs = %s", __func__,
   1857      1.120  christos 			    (unsigned long long)dev, bno, fs->fs_fsmnt);
   1858        1.1   mycroft 		}
   1859       1.62      fvdl 		ffs_setblock(fs, blksfree, fragno);
   1860       1.60      fvdl 		ffs_clusteracct(fs, cgp, fragno, 1);
   1861       1.19    bouyer 		ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
   1862        1.1   mycroft 		fs->fs_cstotal.cs_nbfree++;
   1863        1.1   mycroft 		fs->fs_cs(fs, cg).cs_nbfree++;
   1864       1.73       dbj 		if ((fs->fs_magic == FS_UFS1_MAGIC) &&
   1865       1.73       dbj 		    ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0)) {
   1866       1.73       dbj 			i = old_cbtocylno(fs, cgbno);
   1867       1.75       dbj 			KASSERT(i >= 0);
   1868       1.75       dbj 			KASSERT(i < fs->fs_old_ncyl);
   1869       1.75       dbj 			KASSERT(old_cbtorpos(fs, cgbno) >= 0);
   1870       1.75       dbj 			KASSERT(fs->fs_old_nrpos == 0 || old_cbtorpos(fs, cgbno) < fs->fs_old_nrpos);
   1871       1.73       dbj 			ufs_add16(old_cg_blks(fs, cgp, i, needswap)[old_cbtorpos(fs, cgbno)], 1,
   1872       1.73       dbj 			    needswap);
   1873       1.73       dbj 			ufs_add32(old_cg_blktot(cgp, needswap)[i], 1, needswap);
   1874       1.73       dbj 		}
   1875        1.1   mycroft 	} else {
   1876      1.138  dholland 		bbase = cgbno - ffs_fragnum(fs, cgbno);
   1877        1.1   mycroft 		/*
   1878        1.1   mycroft 		 * decrement the counts associated with the old frags
   1879        1.1   mycroft 		 */
   1880       1.62      fvdl 		blk = blkmap(fs, blksfree, bbase);
   1881       1.19    bouyer 		ffs_fragacct(fs, blk, cgp->cg_frsum, -1, needswap);
   1882        1.1   mycroft 		/*
   1883        1.1   mycroft 		 * deallocate the fragment
   1884        1.1   mycroft 		 */
   1885      1.137  dholland 		frags = ffs_numfrags(fs, size);
   1886        1.1   mycroft 		for (i = 0; i < frags; i++) {
   1887       1.62      fvdl 			if (isset(blksfree, cgbno + i)) {
   1888  1.151.2.1  pgoyette 				panic("%s: freeing free frag: "
   1889  1.151.2.1  pgoyette 				    "dev = 0x%llx, block = %" PRId64
   1890  1.151.2.1  pgoyette 				    ", fs = %s", __func__,
   1891      1.120  christos 				    (unsigned long long)dev, bno + i,
   1892      1.120  christos 				    fs->fs_fsmnt);
   1893        1.1   mycroft 			}
   1894       1.62      fvdl 			setbit(blksfree, cgbno + i);
   1895        1.1   mycroft 		}
   1896       1.19    bouyer 		ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
   1897        1.1   mycroft 		fs->fs_cstotal.cs_nffree += i;
   1898       1.30      fvdl 		fs->fs_cs(fs, cg).cs_nffree += i;
   1899        1.1   mycroft 		/*
   1900        1.1   mycroft 		 * add back in counts associated with the new frags
   1901        1.1   mycroft 		 */
   1902       1.62      fvdl 		blk = blkmap(fs, blksfree, bbase);
   1903       1.19    bouyer 		ffs_fragacct(fs, blk, cgp->cg_frsum, 1, needswap);
   1904        1.1   mycroft 		/*
   1905        1.1   mycroft 		 * if a complete block has been reassembled, account for it
   1906        1.1   mycroft 		 */
   1907      1.138  dholland 		fragno = ffs_fragstoblks(fs, bbase);
   1908       1.62      fvdl 		if (ffs_isblock(fs, blksfree, fragno)) {
   1909       1.19    bouyer 			ufs_add32(cgp->cg_cs.cs_nffree, -fs->fs_frag, needswap);
   1910        1.1   mycroft 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
   1911        1.1   mycroft 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
   1912       1.60      fvdl 			ffs_clusteracct(fs, cgp, fragno, 1);
   1913       1.19    bouyer 			ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
   1914        1.1   mycroft 			fs->fs_cstotal.cs_nbfree++;
   1915        1.1   mycroft 			fs->fs_cs(fs, cg).cs_nbfree++;
   1916       1.73       dbj 			if ((fs->fs_magic == FS_UFS1_MAGIC) &&
   1917       1.73       dbj 			    ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0)) {
   1918       1.73       dbj 				i = old_cbtocylno(fs, bbase);
   1919       1.75       dbj 				KASSERT(i >= 0);
   1920       1.75       dbj 				KASSERT(i < fs->fs_old_ncyl);
   1921       1.75       dbj 				KASSERT(old_cbtorpos(fs, bbase) >= 0);
   1922       1.75       dbj 				KASSERT(fs->fs_old_nrpos == 0 || old_cbtorpos(fs, bbase) < fs->fs_old_nrpos);
   1923       1.73       dbj 				ufs_add16(old_cg_blks(fs, cgp, i, needswap)[old_cbtorpos(fs,
   1924       1.73       dbj 				    bbase)], 1, needswap);
   1925       1.73       dbj 				ufs_add32(old_cg_blktot(cgp, needswap)[i], 1, needswap);
   1926       1.73       dbj 			}
   1927        1.1   mycroft 		}
   1928        1.1   mycroft 	}
   1929        1.1   mycroft 	fs->fs_fmod = 1;
   1930       1.76   hannken 	ACTIVECG_CLR(fs, cg);
   1931      1.101        ad 	mutex_exit(&ump->um_lock);
   1932        1.1   mycroft }
   1933        1.1   mycroft 
   1934        1.1   mycroft /*
   1935        1.1   mycroft  * Free an inode.
   1936       1.30      fvdl  */
   1937       1.30      fvdl int
   1938       1.88      yamt ffs_vfree(struct vnode *vp, ino_t ino, int mode)
   1939       1.30      fvdl {
   1940       1.30      fvdl 
   1941      1.119     joerg 	return ffs_freefile(vp->v_mount, ino, mode);
   1942       1.30      fvdl }
   1943       1.30      fvdl 
   1944       1.30      fvdl /*
   1945       1.30      fvdl  * Do the actual free operation.
   1946        1.1   mycroft  * The specified inode is placed back in the free map.
   1947      1.111    simonb  *
   1948      1.111    simonb  * => um_lock not held on entry or exit
   1949        1.1   mycroft  */
   1950        1.1   mycroft int
   1951      1.119     joerg ffs_freefile(struct mount *mp, ino_t ino, int mode)
   1952      1.119     joerg {
   1953      1.119     joerg 	struct ufsmount *ump = VFSTOUFS(mp);
   1954      1.119     joerg 	struct fs *fs = ump->um_fs;
   1955      1.119     joerg 	struct vnode *devvp;
   1956      1.119     joerg 	struct cg *cgp;
   1957      1.119     joerg 	struct buf *bp;
   1958      1.119     joerg 	int error, cg;
   1959      1.119     joerg 	daddr_t cgbno;
   1960      1.119     joerg 	dev_t dev;
   1961      1.119     joerg 	const int needswap = UFS_FSNEEDSWAP(fs);
   1962      1.119     joerg 
   1963      1.119     joerg 	cg = ino_to_cg(fs, ino);
   1964      1.119     joerg 	devvp = ump->um_devvp;
   1965      1.119     joerg 	dev = devvp->v_rdev;
   1966      1.136  dholland 	cgbno = FFS_FSBTODB(fs, cgtod(fs, cg));
   1967      1.119     joerg 
   1968      1.119     joerg 	if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
   1969  1.151.2.1  pgoyette 		panic("%s: range: dev = 0x%llx, ino = %llu, fs = %s", __func__,
   1970      1.120  christos 		    (long long)dev, (unsigned long long)ino, fs->fs_fsmnt);
   1971      1.119     joerg 	error = bread(devvp, cgbno, (int)fs->fs_cgsize,
   1972      1.149      maxv 	    B_MODIFY, &bp);
   1973      1.119     joerg 	if (error) {
   1974      1.119     joerg 		return (error);
   1975      1.119     joerg 	}
   1976      1.119     joerg 	cgp = (struct cg *)bp->b_data;
   1977      1.119     joerg 	if (!cg_chkmagic(cgp, needswap)) {
   1978      1.119     joerg 		brelse(bp, 0);
   1979      1.119     joerg 		return (0);
   1980      1.119     joerg 	}
   1981      1.119     joerg 
   1982      1.119     joerg 	ffs_freefile_common(ump, fs, dev, bp, ino, mode, false);
   1983      1.119     joerg 
   1984      1.119     joerg 	bdwrite(bp);
   1985      1.119     joerg 
   1986      1.119     joerg 	return 0;
   1987      1.119     joerg }
   1988      1.119     joerg 
   1989      1.119     joerg int
   1990      1.119     joerg ffs_freefile_snap(struct fs *fs, struct vnode *devvp, ino_t ino, int mode)
   1991        1.9  christos {
   1992      1.101        ad 	struct ufsmount *ump;
   1993       1.33  augustss 	struct cg *cgp;
   1994        1.1   mycroft 	struct buf *bp;
   1995        1.1   mycroft 	int error, cg;
   1996       1.76   hannken 	daddr_t cgbno;
   1997       1.78   hannken 	dev_t dev;
   1998       1.30      fvdl 	const int needswap = UFS_FSNEEDSWAP(fs);
   1999        1.1   mycroft 
   2000      1.119     joerg 	KASSERT(devvp->v_type != VBLK);
   2001      1.111    simonb 
   2002       1.76   hannken 	cg = ino_to_cg(fs, ino);
   2003      1.119     joerg 	dev = VTOI(devvp)->i_devvp->v_rdev;
   2004      1.119     joerg 	ump = VFSTOUFS(devvp->v_mount);
   2005      1.138  dholland 	cgbno = ffs_fragstoblks(fs, cgtod(fs, cg));
   2006        1.1   mycroft 	if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
   2007  1.151.2.1  pgoyette 		panic("%s: range: dev = 0x%llx, ino = %llu, fs = %s", __func__,
   2008      1.120  christos 		    (unsigned long long)dev, (unsigned long long)ino,
   2009      1.120  christos 		    fs->fs_fsmnt);
   2010      1.107   hannken 	error = bread(devvp, cgbno, (int)fs->fs_cgsize,
   2011      1.149      maxv 	    B_MODIFY, &bp);
   2012        1.1   mycroft 	if (error) {
   2013       1.30      fvdl 		return (error);
   2014        1.1   mycroft 	}
   2015        1.1   mycroft 	cgp = (struct cg *)bp->b_data;
   2016       1.19    bouyer 	if (!cg_chkmagic(cgp, needswap)) {
   2017      1.101        ad 		brelse(bp, 0);
   2018        1.1   mycroft 		return (0);
   2019        1.1   mycroft 	}
   2020      1.119     joerg 	ffs_freefile_common(ump, fs, dev, bp, ino, mode, true);
   2021      1.119     joerg 
   2022      1.119     joerg 	bdwrite(bp);
   2023      1.119     joerg 
   2024      1.119     joerg 	return 0;
   2025      1.119     joerg }
   2026      1.119     joerg 
   2027      1.119     joerg static void
   2028      1.119     joerg ffs_freefile_common(struct ufsmount *ump, struct fs *fs, dev_t dev,
   2029      1.119     joerg     struct buf *bp, ino_t ino, int mode, bool devvp_is_snapshot)
   2030      1.119     joerg {
   2031      1.119     joerg 	int cg;
   2032      1.119     joerg 	struct cg *cgp;
   2033      1.119     joerg 	u_int8_t *inosused;
   2034      1.119     joerg 	const int needswap = UFS_FSNEEDSWAP(fs);
   2035      1.119     joerg 
   2036      1.119     joerg 	cg = ino_to_cg(fs, ino);
   2037      1.119     joerg 	cgp = (struct cg *)bp->b_data;
   2038       1.92    kardel 	cgp->cg_old_time = ufs_rw32(time_second, needswap);
   2039       1.73       dbj 	if ((fs->fs_magic != FS_UFS1_MAGIC) ||
   2040       1.73       dbj 	    (fs->fs_old_flags & FS_FLAGS_UPDATED))
   2041       1.92    kardel 		cgp->cg_time = ufs_rw64(time_second, needswap);
   2042       1.62      fvdl 	inosused = cg_inosused(cgp, needswap);
   2043        1.1   mycroft 	ino %= fs->fs_ipg;
   2044       1.62      fvdl 	if (isclr(inosused, ino)) {
   2045      1.120  christos 		printf("ifree: dev = 0x%llx, ino = %llu, fs = %s\n",
   2046      1.120  christos 		    (unsigned long long)dev, (unsigned long long)ino +
   2047      1.120  christos 		    cg * fs->fs_ipg, fs->fs_fsmnt);
   2048        1.1   mycroft 		if (fs->fs_ronly == 0)
   2049  1.151.2.1  pgoyette 			panic("%s: freeing free inode", __func__);
   2050        1.1   mycroft 	}
   2051       1.62      fvdl 	clrbit(inosused, ino);
   2052      1.113   hannken 	if (!devvp_is_snapshot)
   2053      1.119     joerg 		UFS_WAPBL_UNREGISTER_INODE(ump->um_mountp,
   2054      1.113   hannken 		    ino + cg * fs->fs_ipg, mode);
   2055       1.19    bouyer 	if (ino < ufs_rw32(cgp->cg_irotor, needswap))
   2056       1.19    bouyer 		cgp->cg_irotor = ufs_rw32(ino, needswap);
   2057       1.19    bouyer 	ufs_add32(cgp->cg_cs.cs_nifree, 1, needswap);
   2058      1.101        ad 	mutex_enter(&ump->um_lock);
   2059        1.1   mycroft 	fs->fs_cstotal.cs_nifree++;
   2060        1.1   mycroft 	fs->fs_cs(fs, cg).cs_nifree++;
   2061       1.78   hannken 	if ((mode & IFMT) == IFDIR) {
   2062       1.19    bouyer 		ufs_add32(cgp->cg_cs.cs_ndir, -1, needswap);
   2063        1.1   mycroft 		fs->fs_cstotal.cs_ndir--;
   2064        1.1   mycroft 		fs->fs_cs(fs, cg).cs_ndir--;
   2065        1.1   mycroft 	}
   2066        1.1   mycroft 	fs->fs_fmod = 1;
   2067       1.82   hannken 	ACTIVECG_CLR(fs, cg);
   2068      1.101        ad 	mutex_exit(&ump->um_lock);
   2069        1.1   mycroft }
   2070        1.1   mycroft 
   2071        1.1   mycroft /*
   2072       1.76   hannken  * Check to see if a file is free.
   2073       1.76   hannken  */
   2074       1.76   hannken int
   2075       1.85   thorpej ffs_checkfreefile(struct fs *fs, struct vnode *devvp, ino_t ino)
   2076       1.76   hannken {
   2077       1.76   hannken 	struct cg *cgp;
   2078       1.76   hannken 	struct buf *bp;
   2079       1.76   hannken 	daddr_t cgbno;
   2080       1.76   hannken 	int ret, cg;
   2081       1.76   hannken 	u_int8_t *inosused;
   2082      1.113   hannken 	const bool devvp_is_snapshot = (devvp->v_type != VBLK);
   2083       1.76   hannken 
   2084      1.119     joerg 	KASSERT(devvp_is_snapshot);
   2085      1.119     joerg 
   2086       1.76   hannken 	cg = ino_to_cg(fs, ino);
   2087      1.113   hannken 	if (devvp_is_snapshot)
   2088      1.138  dholland 		cgbno = ffs_fragstoblks(fs, cgtod(fs, cg));
   2089      1.113   hannken 	else
   2090      1.136  dholland 		cgbno = FFS_FSBTODB(fs, cgtod(fs, cg));
   2091       1.76   hannken 	if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
   2092       1.76   hannken 		return 1;
   2093      1.149      maxv 	if (bread(devvp, cgbno, (int)fs->fs_cgsize, 0, &bp)) {
   2094       1.76   hannken 		return 1;
   2095       1.76   hannken 	}
   2096       1.76   hannken 	cgp = (struct cg *)bp->b_data;
   2097       1.76   hannken 	if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs))) {
   2098      1.101        ad 		brelse(bp, 0);
   2099       1.76   hannken 		return 1;
   2100       1.76   hannken 	}
   2101       1.76   hannken 	inosused = cg_inosused(cgp, UFS_FSNEEDSWAP(fs));
   2102       1.76   hannken 	ino %= fs->fs_ipg;
   2103       1.76   hannken 	ret = isclr(inosused, ino);
   2104      1.101        ad 	brelse(bp, 0);
   2105       1.76   hannken 	return ret;
   2106       1.76   hannken }
   2107       1.76   hannken 
   2108       1.76   hannken /*
   2109        1.1   mycroft  * Find a block of the specified size in the specified cylinder group.
   2110        1.1   mycroft  *
   2111        1.1   mycroft  * It is a panic if a request is made to find a block if none are
   2112        1.1   mycroft  * available.
   2113        1.1   mycroft  */
   2114       1.60      fvdl static int32_t
   2115       1.85   thorpej ffs_mapsearch(struct fs *fs, struct cg *cgp, daddr_t bpref, int allocsiz)
   2116        1.1   mycroft {
   2117       1.60      fvdl 	int32_t bno;
   2118        1.1   mycroft 	int start, len, loc, i;
   2119        1.1   mycroft 	int blk, field, subfield, pos;
   2120       1.19    bouyer 	int ostart, olen;
   2121       1.62      fvdl 	u_int8_t *blksfree;
   2122       1.30      fvdl 	const int needswap = UFS_FSNEEDSWAP(fs);
   2123        1.1   mycroft 
   2124      1.101        ad 	/* KASSERT(mutex_owned(&ump->um_lock)); */
   2125      1.101        ad 
   2126        1.1   mycroft 	/*
   2127        1.1   mycroft 	 * find the fragment by searching through the free block
   2128        1.1   mycroft 	 * map for an appropriate bit pattern
   2129        1.1   mycroft 	 */
   2130        1.1   mycroft 	if (bpref)
   2131        1.1   mycroft 		start = dtogd(fs, bpref) / NBBY;
   2132        1.1   mycroft 	else
   2133       1.19    bouyer 		start = ufs_rw32(cgp->cg_frotor, needswap) / NBBY;
   2134       1.62      fvdl 	blksfree = cg_blksfree(cgp, needswap);
   2135        1.1   mycroft 	len = howmany(fs->fs_fpg, NBBY) - start;
   2136       1.19    bouyer 	ostart = start;
   2137       1.19    bouyer 	olen = len;
   2138       1.45     lukem 	loc = scanc((u_int)len,
   2139       1.62      fvdl 		(const u_char *)&blksfree[start],
   2140       1.45     lukem 		(const u_char *)fragtbl[fs->fs_frag],
   2141       1.54   mycroft 		(1 << (allocsiz - 1 + (fs->fs_frag & (NBBY - 1)))));
   2142        1.1   mycroft 	if (loc == 0) {
   2143        1.1   mycroft 		len = start + 1;
   2144        1.1   mycroft 		start = 0;
   2145       1.45     lukem 		loc = scanc((u_int)len,
   2146       1.62      fvdl 			(const u_char *)&blksfree[0],
   2147       1.45     lukem 			(const u_char *)fragtbl[fs->fs_frag],
   2148       1.54   mycroft 			(1 << (allocsiz - 1 + (fs->fs_frag & (NBBY - 1)))));
   2149        1.1   mycroft 		if (loc == 0) {
   2150  1.151.2.1  pgoyette 			panic("%s: map corrupted: start=%d, len=%d, "
   2151  1.151.2.1  pgoyette 			    "fs = %s, offset=%d/%ld, cg %d", __func__,
   2152  1.151.2.1  pgoyette 			    ostart, olen, fs->fs_fsmnt,
   2153  1.151.2.1  pgoyette 			    ufs_rw32(cgp->cg_freeoff, needswap),
   2154  1.151.2.1  pgoyette 			    (long)blksfree - (long)cgp, cgp->cg_cgx);
   2155        1.1   mycroft 			/* NOTREACHED */
   2156        1.1   mycroft 		}
   2157        1.1   mycroft 	}
   2158        1.1   mycroft 	bno = (start + len - loc) * NBBY;
   2159       1.19    bouyer 	cgp->cg_frotor = ufs_rw32(bno, needswap);
   2160        1.1   mycroft 	/*
   2161        1.1   mycroft 	 * found the byte in the map
   2162        1.1   mycroft 	 * sift through the bits to find the selected frag
   2163        1.1   mycroft 	 */
   2164        1.1   mycroft 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
   2165       1.62      fvdl 		blk = blkmap(fs, blksfree, bno);
   2166        1.1   mycroft 		blk <<= 1;
   2167        1.1   mycroft 		field = around[allocsiz];
   2168        1.1   mycroft 		subfield = inside[allocsiz];
   2169        1.1   mycroft 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
   2170        1.1   mycroft 			if ((blk & field) == subfield)
   2171        1.1   mycroft 				return (bno + pos);
   2172        1.1   mycroft 			field <<= 1;
   2173        1.1   mycroft 			subfield <<= 1;
   2174        1.1   mycroft 		}
   2175        1.1   mycroft 	}
   2176  1.151.2.1  pgoyette 	panic("%s: block not in map: bno=%d, fs=%s", __func__,
   2177  1.151.2.1  pgoyette 	    bno, fs->fs_fsmnt);
   2178       1.58      fvdl 	/* return (-1); */
   2179        1.1   mycroft }
   2180        1.1   mycroft 
   2181        1.1   mycroft /*
   2182        1.1   mycroft  * Fserr prints the name of a file system with an error diagnostic.
   2183       1.81     perry  *
   2184        1.1   mycroft  * The form of the error message is:
   2185        1.1   mycroft  *	fs: error message
   2186        1.1   mycroft  */
   2187        1.1   mycroft static void
   2188      1.150   mlelstv ffs_fserr(struct fs *fs, kauth_cred_t cred, const char *cp)
   2189        1.1   mycroft {
   2190      1.150   mlelstv 	KASSERT(cred != NULL);
   2191        1.1   mycroft 
   2192      1.150   mlelstv 	if (cred == NOCRED || cred == FSCRED) {
   2193      1.150   mlelstv 		log(LOG_ERR, "pid %d, command %s, on %s: %s\n",
   2194      1.150   mlelstv 		    curproc->p_pid, curproc->p_comm,
   2195      1.150   mlelstv 		    fs->fs_fsmnt, cp);
   2196      1.150   mlelstv 	} else {
   2197      1.150   mlelstv 		log(LOG_ERR, "uid %d, pid %d, command %s, on %s: %s\n",
   2198      1.150   mlelstv 		    kauth_cred_getuid(cred), curproc->p_pid, curproc->p_comm,
   2199      1.150   mlelstv 		    fs->fs_fsmnt, cp);
   2200      1.150   mlelstv 	}
   2201        1.1   mycroft }
   2202