10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 230Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 280Sstevel@tonic-gate /* All Rights Reserved */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate /* 310Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 320Sstevel@tonic-gate * The Regents of the University of California 330Sstevel@tonic-gate * All Rights Reserved 340Sstevel@tonic-gate * 350Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 360Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 370Sstevel@tonic-gate * contributors. 380Sstevel@tonic-gate */ 390Sstevel@tonic-gate 400Sstevel@tonic-gate 410Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 420Sstevel@tonic-gate 430Sstevel@tonic-gate #include <sys/types.h> 440Sstevel@tonic-gate #include <sys/t_lock.h> 450Sstevel@tonic-gate #include <sys/debug.h> 460Sstevel@tonic-gate #include <sys/param.h> 470Sstevel@tonic-gate #include <sys/systm.h> 480Sstevel@tonic-gate #include <sys/signal.h> 490Sstevel@tonic-gate #include <sys/cred.h> 500Sstevel@tonic-gate #include <sys/proc.h> 510Sstevel@tonic-gate #include <sys/disp.h> 520Sstevel@tonic-gate #include <sys/user.h> 530Sstevel@tonic-gate #include <sys/buf.h> 540Sstevel@tonic-gate #include <sys/vfs.h> 550Sstevel@tonic-gate #include <sys/vnode.h> 560Sstevel@tonic-gate #include <sys/acl.h> 570Sstevel@tonic-gate #include <sys/fs/ufs_fs.h> 580Sstevel@tonic-gate #include <sys/fs/ufs_inode.h> 590Sstevel@tonic-gate #include <sys/fs/ufs_acl.h> 600Sstevel@tonic-gate #include <sys/fs/ufs_bio.h> 610Sstevel@tonic-gate #include <sys/fs/ufs_quota.h> 620Sstevel@tonic-gate #include <sys/kmem.h> 630Sstevel@tonic-gate #include <sys/fs/ufs_trans.h> 640Sstevel@tonic-gate #include <sys/fs/ufs_panic.h> 650Sstevel@tonic-gate #include <sys/errno.h> 660Sstevel@tonic-gate #include <sys/time.h> 670Sstevel@tonic-gate #include <sys/sysmacros.h> 680Sstevel@tonic-gate #include <sys/file.h> 690Sstevel@tonic-gate #include <sys/fcntl.h> 700Sstevel@tonic-gate #include <sys/flock.h> 710Sstevel@tonic-gate #include <fs/fs_subr.h> 720Sstevel@tonic-gate #include <sys/cmn_err.h> 730Sstevel@tonic-gate #include <sys/policy.h> 740Sstevel@tonic-gate 750Sstevel@tonic-gate static ino_t hashalloc(); 760Sstevel@tonic-gate static daddr_t fragextend(); 770Sstevel@tonic-gate static daddr_t alloccg(); 780Sstevel@tonic-gate static daddr_t alloccgblk(); 790Sstevel@tonic-gate static ino_t ialloccg(); 800Sstevel@tonic-gate static daddr_t mapsearch(); 810Sstevel@tonic-gate 820Sstevel@tonic-gate extern int inside[], around[]; 830Sstevel@tonic-gate extern uchar_t *fragtbl[]; 840Sstevel@tonic-gate void delay(); 850Sstevel@tonic-gate 860Sstevel@tonic-gate /* 870Sstevel@tonic-gate * Allocate a block in the file system. 880Sstevel@tonic-gate * 890Sstevel@tonic-gate * The size of the requested block is given, which must be some 900Sstevel@tonic-gate * multiple of fs_fsize and <= fs_bsize. 910Sstevel@tonic-gate * A preference may be optionally specified. If a preference is given 920Sstevel@tonic-gate * the following hierarchy is used to allocate a block: 930Sstevel@tonic-gate * 1) allocate the requested block. 940Sstevel@tonic-gate * 2) allocate a rotationally optimal block in the same cylinder. 950Sstevel@tonic-gate * 3) allocate a block in the same cylinder group. 960Sstevel@tonic-gate * 4) quadratically rehash into other cylinder groups, until an 970Sstevel@tonic-gate * available block is located. 980Sstevel@tonic-gate * If no block preference is given the following hierarchy is used 990Sstevel@tonic-gate * to allocate a block: 1000Sstevel@tonic-gate * 1) allocate a block in the cylinder group that contains the 1010Sstevel@tonic-gate * inode for the file. 1020Sstevel@tonic-gate * 2) quadratically rehash into other cylinder groups, until an 1030Sstevel@tonic-gate * available block is located. 1040Sstevel@tonic-gate */ 1050Sstevel@tonic-gate int 1060Sstevel@tonic-gate alloc(struct inode *ip, daddr_t bpref, int size, daddr_t *bnp, cred_t *cr) 1070Sstevel@tonic-gate { 1080Sstevel@tonic-gate struct fs *fs; 1090Sstevel@tonic-gate struct ufsvfs *ufsvfsp; 1100Sstevel@tonic-gate daddr_t bno; 1110Sstevel@tonic-gate int cg; 1120Sstevel@tonic-gate int err; 1130Sstevel@tonic-gate char *errmsg = NULL; 1140Sstevel@tonic-gate size_t len; 1150Sstevel@tonic-gate 1160Sstevel@tonic-gate ufsvfsp = ip->i_ufsvfs; 1170Sstevel@tonic-gate fs = ufsvfsp->vfs_fs; 1180Sstevel@tonic-gate if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { 1190Sstevel@tonic-gate err = ufs_fault(ITOV(ip), 1200Sstevel@tonic-gate "alloc: bad size, dev = 0x%lx, bsize = %d, size = %d, fs = %s\n", 1210Sstevel@tonic-gate ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 1220Sstevel@tonic-gate return (err); 1230Sstevel@tonic-gate } 1240Sstevel@tonic-gate if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0) 1250Sstevel@tonic-gate goto nospace; 1260Sstevel@tonic-gate if (freespace(fs, ufsvfsp) <= 0 && 1270Sstevel@tonic-gate secpolicy_fs_minfree(cr, ufsvfsp->vfs_vfs) != 0) 1280Sstevel@tonic-gate goto nospace; 1290Sstevel@tonic-gate err = chkdq(ip, (long)btodb(size), 0, cr, &errmsg, &len); 1300Sstevel@tonic-gate /* Note that may not have err, but may have errmsg */ 1310Sstevel@tonic-gate if (errmsg != NULL) { 1320Sstevel@tonic-gate uprintf(errmsg); 1330Sstevel@tonic-gate kmem_free(errmsg, len); 1340Sstevel@tonic-gate errmsg = NULL; 1350Sstevel@tonic-gate } 1360Sstevel@tonic-gate if (err) 1370Sstevel@tonic-gate return (err); 1380Sstevel@tonic-gate if (bpref >= fs->fs_size) 1390Sstevel@tonic-gate bpref = 0; 1400Sstevel@tonic-gate if (bpref == 0) 1410Sstevel@tonic-gate cg = (int)itog(fs, ip->i_number); 1420Sstevel@tonic-gate else 1430Sstevel@tonic-gate cg = dtog(fs, bpref); 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate bno = (daddr_t)hashalloc(ip, cg, (long)bpref, size, 1460Sstevel@tonic-gate (ulong_t (*)())alloccg); 1470Sstevel@tonic-gate if (bno > 0) { 1480Sstevel@tonic-gate *bnp = bno; 1490Sstevel@tonic-gate return (0); 1500Sstevel@tonic-gate } 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate /* 1530Sstevel@tonic-gate * hashalloc() failed because some other thread grabbed 1540Sstevel@tonic-gate * the last block so unwind the quota operation. We can 1550Sstevel@tonic-gate * ignore the return because subtractions don't fail and 1560Sstevel@tonic-gate * size is guaranteed to be >= zero by our caller. 1570Sstevel@tonic-gate */ 1580Sstevel@tonic-gate (void) chkdq(ip, -(long)btodb(size), 0, cr, (char **)NULL, 1590Sstevel@tonic-gate (size_t *)NULL); 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate nospace: 1620Sstevel@tonic-gate mutex_enter(&ufsvfsp->vfs_lock); 1630Sstevel@tonic-gate if ((lbolt - ufsvfsp->vfs_lastwhinetime) > (hz << 2) && 1640Sstevel@tonic-gate (!(TRANS_ISTRANS(ufsvfsp)) || !(ip->i_flag & IQUIET))) { 1650Sstevel@tonic-gate ufsvfsp->vfs_lastwhinetime = lbolt; 1660Sstevel@tonic-gate cmn_err(CE_NOTE, "alloc: %s: file system full", fs->fs_fsmnt); 1670Sstevel@tonic-gate } 1680Sstevel@tonic-gate mutex_exit(&ufsvfsp->vfs_lock); 1690Sstevel@tonic-gate return (ENOSPC); 1700Sstevel@tonic-gate } 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate /* 1730Sstevel@tonic-gate * Reallocate a fragment to a bigger size 1740Sstevel@tonic-gate * 1750Sstevel@tonic-gate * The number and size of the old block is given, and a preference 1760Sstevel@tonic-gate * and new size is also specified. The allocator attempts to extend 1770Sstevel@tonic-gate * the original block. Failing that, the regular block allocator is 1780Sstevel@tonic-gate * invoked to get an appropriate block. 1790Sstevel@tonic-gate */ 1800Sstevel@tonic-gate int 1810Sstevel@tonic-gate realloccg(struct inode *ip, daddr_t bprev, daddr_t bpref, int osize, 1820Sstevel@tonic-gate int nsize, daddr_t *bnp, cred_t *cr) 1830Sstevel@tonic-gate { 1840Sstevel@tonic-gate daddr_t bno; 1850Sstevel@tonic-gate struct fs *fs; 1860Sstevel@tonic-gate struct ufsvfs *ufsvfsp; 1870Sstevel@tonic-gate int cg, request; 1880Sstevel@tonic-gate int err; 1890Sstevel@tonic-gate char *errmsg = NULL; 1900Sstevel@tonic-gate size_t len; 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate ufsvfsp = ip->i_ufsvfs; 1930Sstevel@tonic-gate fs = ufsvfsp->vfs_fs; 1940Sstevel@tonic-gate if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 || 1950Sstevel@tonic-gate (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) { 1960Sstevel@tonic-gate err = ufs_fault(ITOV(ip), 1970Sstevel@tonic-gate "realloccg: bad size, dev=0x%lx, bsize=%d, osize=%d, nsize=%d, fs=%s\n", 1980Sstevel@tonic-gate ip->i_dev, fs->fs_bsize, osize, nsize, 1990Sstevel@tonic-gate fs->fs_fsmnt); 2000Sstevel@tonic-gate return (err); 2010Sstevel@tonic-gate } 2020Sstevel@tonic-gate if (freespace(fs, ufsvfsp) <= 0 && 2030Sstevel@tonic-gate secpolicy_fs_minfree(cr, ufsvfsp->vfs_vfs) != 0) 2040Sstevel@tonic-gate goto nospace; 2050Sstevel@tonic-gate if (bprev == 0) { 2060Sstevel@tonic-gate err = ufs_fault(ITOV(ip), 2070Sstevel@tonic-gate "realloccg: bad bprev, dev = 0x%lx, bsize = %d, bprev = %ld, fs = %s\n", 2080Sstevel@tonic-gate ip->i_dev, fs->fs_bsize, bprev, 2090Sstevel@tonic-gate fs->fs_fsmnt); 2100Sstevel@tonic-gate return (err); 2110Sstevel@tonic-gate } 2120Sstevel@tonic-gate err = chkdq(ip, (long)btodb(nsize - osize), 0, cr, &errmsg, &len); 2130Sstevel@tonic-gate /* Note that may not have err, but may have errmsg */ 2140Sstevel@tonic-gate if (errmsg != NULL) { 2150Sstevel@tonic-gate uprintf(errmsg); 2160Sstevel@tonic-gate kmem_free(errmsg, len); 2170Sstevel@tonic-gate errmsg = NULL; 2180Sstevel@tonic-gate } 2190Sstevel@tonic-gate if (err) 2200Sstevel@tonic-gate return (err); 2210Sstevel@tonic-gate cg = dtog(fs, bprev); 2220Sstevel@tonic-gate bno = fragextend(ip, cg, (long)bprev, osize, nsize); 2230Sstevel@tonic-gate if (bno != 0) { 2240Sstevel@tonic-gate *bnp = bno; 2250Sstevel@tonic-gate return (0); 2260Sstevel@tonic-gate } 2270Sstevel@tonic-gate if (bpref >= fs->fs_size) 2280Sstevel@tonic-gate bpref = 0; 2290Sstevel@tonic-gate 2300Sstevel@tonic-gate /* 2310Sstevel@tonic-gate * When optimizing for time we allocate a full block and 2320Sstevel@tonic-gate * then only use the upper portion for this request. When 2330Sstevel@tonic-gate * this file grows again it will grow into the unused portion 2340Sstevel@tonic-gate * of the block (See fragextend() above). This saves time 2350Sstevel@tonic-gate * because an extra disk write would be needed if the frags 2360Sstevel@tonic-gate * following the current allocation were not free. The extra 2370Sstevel@tonic-gate * disk write is needed to move the data from its current 2380Sstevel@tonic-gate * location into the newly allocated position. 2390Sstevel@tonic-gate * 2400Sstevel@tonic-gate * When optimizing for space we allocate a run of frags 2410Sstevel@tonic-gate * that is just the right size for this request. 2420Sstevel@tonic-gate */ 2430Sstevel@tonic-gate request = (fs->fs_optim == FS_OPTTIME) ? fs->fs_bsize : nsize; 2440Sstevel@tonic-gate bno = (daddr_t)hashalloc(ip, cg, (long)bpref, request, 2450Sstevel@tonic-gate (ulong_t (*)())alloccg); 2460Sstevel@tonic-gate if (bno > 0) { 2470Sstevel@tonic-gate *bnp = bno; 2480Sstevel@tonic-gate if (nsize < request) 2490Sstevel@tonic-gate (void) free(ip, bno + numfrags(fs, nsize), 2500Sstevel@tonic-gate (off_t)(request - nsize), I_NOCANCEL); 2510Sstevel@tonic-gate return (0); 2520Sstevel@tonic-gate } 2530Sstevel@tonic-gate 2540Sstevel@tonic-gate /* 2550Sstevel@tonic-gate * hashalloc() failed because some other thread grabbed 2560Sstevel@tonic-gate * the last block so unwind the quota operation. We can 2570Sstevel@tonic-gate * ignore the return because subtractions don't fail, and 2580Sstevel@tonic-gate * our caller guarantees nsize >= osize. 2590Sstevel@tonic-gate */ 2600Sstevel@tonic-gate (void) chkdq(ip, -(long)btodb(nsize - osize), 0, cr, (char **)NULL, 2610Sstevel@tonic-gate (size_t *)NULL); 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate nospace: 2640Sstevel@tonic-gate mutex_enter(&ufsvfsp->vfs_lock); 2650Sstevel@tonic-gate if ((lbolt - ufsvfsp->vfs_lastwhinetime) > (hz << 2) && 2660Sstevel@tonic-gate (!(TRANS_ISTRANS(ufsvfsp)) || !(ip->i_flag & IQUIET))) { 2670Sstevel@tonic-gate ufsvfsp->vfs_lastwhinetime = lbolt; 2680Sstevel@tonic-gate cmn_err(CE_NOTE, 2690Sstevel@tonic-gate "realloccg %s: file system full", fs->fs_fsmnt); 2700Sstevel@tonic-gate } 2710Sstevel@tonic-gate mutex_exit(&ufsvfsp->vfs_lock); 2720Sstevel@tonic-gate return (ENOSPC); 2730Sstevel@tonic-gate } 2740Sstevel@tonic-gate 2750Sstevel@tonic-gate /* 2760Sstevel@tonic-gate * Allocate an inode in the file system. 2770Sstevel@tonic-gate * 2780Sstevel@tonic-gate * A preference may be optionally specified. If a preference is given 2790Sstevel@tonic-gate * the following hierarchy is used to allocate an inode: 2800Sstevel@tonic-gate * 1) allocate the requested inode. 2810Sstevel@tonic-gate * 2) allocate an inode in the same cylinder group. 2820Sstevel@tonic-gate * 3) quadratically rehash into other cylinder groups, until an 2830Sstevel@tonic-gate * available inode is located. 2840Sstevel@tonic-gate * If no inode preference is given the following hierarchy is used 2850Sstevel@tonic-gate * to allocate an inode: 2860Sstevel@tonic-gate * 1) allocate an inode in cylinder group 0. 2870Sstevel@tonic-gate * 2) quadratically rehash into other cylinder groups, until an 2880Sstevel@tonic-gate * available inode is located. 2890Sstevel@tonic-gate */ 2900Sstevel@tonic-gate int 2910Sstevel@tonic-gate ufs_ialloc(struct inode *pip, 2920Sstevel@tonic-gate ino_t ipref, mode_t mode, struct inode **ipp, cred_t *cr) 2930Sstevel@tonic-gate { 2940Sstevel@tonic-gate struct inode *ip; 2950Sstevel@tonic-gate struct fs *fs; 2960Sstevel@tonic-gate int cg; 2970Sstevel@tonic-gate ino_t ino; 2980Sstevel@tonic-gate int err; 2990Sstevel@tonic-gate int nifree; 3000Sstevel@tonic-gate struct ufsvfs *ufsvfsp = pip->i_ufsvfs; 3010Sstevel@tonic-gate char *errmsg = NULL; 3020Sstevel@tonic-gate size_t len; 3030Sstevel@tonic-gate 3040Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&pip->i_rwlock)); 3050Sstevel@tonic-gate fs = pip->i_fs; 3060Sstevel@tonic-gate loop: 3070Sstevel@tonic-gate nifree = fs->fs_cstotal.cs_nifree; 3080Sstevel@tonic-gate 3090Sstevel@tonic-gate if (nifree == 0) 3100Sstevel@tonic-gate goto noinodes; 3110Sstevel@tonic-gate /* 3120Sstevel@tonic-gate * Shadow inodes don't count against a user's inode allocation. 3130Sstevel@tonic-gate * They are an implementation method and not a resource. 3140Sstevel@tonic-gate */ 3150Sstevel@tonic-gate if ((mode != IFSHAD) && (mode != IFATTRDIR)) { 3160Sstevel@tonic-gate err = chkiq((struct ufsvfs *)ITOV(pip)->v_vfsp->vfs_data, 3170Sstevel@tonic-gate /* change */ 1, (struct inode *)NULL, crgetuid(cr), 0, 3180Sstevel@tonic-gate cr, &errmsg, &len); 3190Sstevel@tonic-gate /* 3200Sstevel@tonic-gate * As we haven't acquired any locks yet, dump the message 3210Sstevel@tonic-gate * now. 3220Sstevel@tonic-gate */ 3230Sstevel@tonic-gate if (errmsg != NULL) { 3240Sstevel@tonic-gate uprintf(errmsg); 3250Sstevel@tonic-gate kmem_free(errmsg, len); 3260Sstevel@tonic-gate errmsg = NULL; 3270Sstevel@tonic-gate } 3280Sstevel@tonic-gate if (err) 3290Sstevel@tonic-gate return (err); 3300Sstevel@tonic-gate } 3310Sstevel@tonic-gate 3320Sstevel@tonic-gate if (ipref >= (ulong_t)(fs->fs_ncg * fs->fs_ipg)) 3330Sstevel@tonic-gate ipref = 0; 3340Sstevel@tonic-gate cg = (int)itog(fs, ipref); 3350Sstevel@tonic-gate ino = (ino_t)hashalloc(pip, cg, (long)ipref, (int)mode, 3360Sstevel@tonic-gate (ulong_t (*)())ialloccg); 3370Sstevel@tonic-gate if (ino == 0) { 3380Sstevel@tonic-gate if ((mode != IFSHAD) && (mode != IFATTRDIR)) { 3390Sstevel@tonic-gate /* 3400Sstevel@tonic-gate * We can safely ignore the return from chkiq() 3410Sstevel@tonic-gate * because deallocations can only fail if we 3420Sstevel@tonic-gate * can't get the user's quota info record off 3430Sstevel@tonic-gate * the disk due to an I/O error. In that case, 3440Sstevel@tonic-gate * the quota subsystem is already messed up. 3450Sstevel@tonic-gate */ 3460Sstevel@tonic-gate (void) chkiq(ufsvfsp, /* change */ -1, 3470Sstevel@tonic-gate (struct inode *)NULL, crgetuid(cr), 0, cr, 3480Sstevel@tonic-gate (char **)NULL, (size_t *)NULL); 3490Sstevel@tonic-gate } 3500Sstevel@tonic-gate goto noinodes; 3510Sstevel@tonic-gate } 3520Sstevel@tonic-gate err = ufs_iget(pip->i_vfs, ino, ipp, cr); 3530Sstevel@tonic-gate if (err) { 3540Sstevel@tonic-gate if ((mode != IFSHAD) && (mode != IFATTRDIR)) { 3550Sstevel@tonic-gate /* 3560Sstevel@tonic-gate * See above comment about why it is safe to ignore an 3570Sstevel@tonic-gate * error return here. 3580Sstevel@tonic-gate */ 3590Sstevel@tonic-gate (void) chkiq(ufsvfsp, /* change */ -1, 3600Sstevel@tonic-gate (struct inode *)NULL, crgetuid(cr), 0, cr, 3610Sstevel@tonic-gate (char **)NULL, (size_t *)NULL); 3620Sstevel@tonic-gate } 3630Sstevel@tonic-gate ufs_ifree(pip, ino, 0); 3640Sstevel@tonic-gate return (err); 3650Sstevel@tonic-gate } 3660Sstevel@tonic-gate ip = *ipp; 3670Sstevel@tonic-gate ASSERT(!ip->i_ufs_acl); 3680Sstevel@tonic-gate ASSERT(!ip->i_dquot); 3690Sstevel@tonic-gate rw_enter(&ip->i_contents, RW_WRITER); 3700Sstevel@tonic-gate 3710Sstevel@tonic-gate /* 3720Sstevel@tonic-gate * Check if we really got a free inode, if not then complain 3730Sstevel@tonic-gate * and mark the inode ISTALE so that it will be freed by the 3740Sstevel@tonic-gate * ufs idle thread eventually and will not be sent to ufs_delete(). 3750Sstevel@tonic-gate */ 3760Sstevel@tonic-gate if (ip->i_mode || (ip->i_nlink > 0)) { 3770Sstevel@tonic-gate ip->i_flag |= ISTALE; 3780Sstevel@tonic-gate rw_exit(&ip->i_contents); 3790Sstevel@tonic-gate VN_RELE(ITOV(ip)); 3800Sstevel@tonic-gate cmn_err(CE_WARN, 3810Sstevel@tonic-gate "%s: unexpected allocated inode %d, run fsck(1M)%s", 3820Sstevel@tonic-gate fs->fs_fsmnt, (int)ino, 3830Sstevel@tonic-gate (TRANS_ISTRANS(ufsvfsp) ? " -o f" : "")); 3840Sstevel@tonic-gate goto loop; 3850Sstevel@tonic-gate } 3860Sstevel@tonic-gate 3870Sstevel@tonic-gate /* 3880Sstevel@tonic-gate * Check the inode has no size or data blocks. 3890Sstevel@tonic-gate * This could have happened if the truncation failed when 3900Sstevel@tonic-gate * deleting the inode. It used to be possible for this to occur 3910Sstevel@tonic-gate * if a block allocation failed when iteratively truncating a 3920Sstevel@tonic-gate * large file using logging and with a full file system. 3930Sstevel@tonic-gate * This was fixed with bug fix 4348738. However, truncation may 3940Sstevel@tonic-gate * still fail on an IO error. So in all cases for safety and 3950Sstevel@tonic-gate * security we clear out the size; the blocks allocated; and 3960Sstevel@tonic-gate * pointers to the blocks. This will ultimately cause a fsck 3970Sstevel@tonic-gate * error of un-accounted for blocks, but its a fairly benign error, 3980Sstevel@tonic-gate * and possibly the correct thing to do anyway as accesssing those 3990Sstevel@tonic-gate * blocks agains may lead to more IO errors. 4000Sstevel@tonic-gate */ 4010Sstevel@tonic-gate if (ip->i_size || ip->i_blocks) { 4020Sstevel@tonic-gate int i; 4030Sstevel@tonic-gate 4040Sstevel@tonic-gate if (ip->i_size) { 4050Sstevel@tonic-gate cmn_err(CE_WARN, 4060Sstevel@tonic-gate "%s: free inode %d had size 0x%llx, run fsck(1M)%s", 4070Sstevel@tonic-gate fs->fs_fsmnt, (int)ino, ip->i_size, 4080Sstevel@tonic-gate (TRANS_ISTRANS(ufsvfsp) ? " -o f" : "")); 4090Sstevel@tonic-gate } 4100Sstevel@tonic-gate /* 4110Sstevel@tonic-gate * Clear any garbage left behind. 4120Sstevel@tonic-gate */ 4130Sstevel@tonic-gate ip->i_size = (u_offset_t)0; 4140Sstevel@tonic-gate ip->i_blocks = 0; 4150Sstevel@tonic-gate for (i = 0; i < NDADDR; i++) 4160Sstevel@tonic-gate ip->i_db[i] = 0; 4170Sstevel@tonic-gate for (i = 0; i < NIADDR; i++) 4180Sstevel@tonic-gate ip->i_ib[i] = 0; 4190Sstevel@tonic-gate } 4200Sstevel@tonic-gate 4210Sstevel@tonic-gate /* 4220Sstevel@tonic-gate * Initialize the link count 4230Sstevel@tonic-gate */ 4240Sstevel@tonic-gate ip->i_nlink = 0; 4250Sstevel@tonic-gate 4260Sstevel@tonic-gate /* 4270Sstevel@tonic-gate * Clear the old flags 4280Sstevel@tonic-gate */ 4290Sstevel@tonic-gate ip->i_flag &= IREF; 4300Sstevel@tonic-gate 4310Sstevel@tonic-gate /* 4320Sstevel@tonic-gate * Access times are not really defined if the fs is mounted 4330Sstevel@tonic-gate * with 'noatime'. But it can cause nfs clients to fail 4340Sstevel@tonic-gate * open() if the atime is not a legal value. Set a legal value 4350Sstevel@tonic-gate * here when the inode is allocated. 4360Sstevel@tonic-gate */ 4370Sstevel@tonic-gate if (ufsvfsp->vfs_noatime) { 4380Sstevel@tonic-gate mutex_enter(&ufs_iuniqtime_lock); 4390Sstevel@tonic-gate ip->i_atime = iuniqtime; 4400Sstevel@tonic-gate mutex_exit(&ufs_iuniqtime_lock); 4410Sstevel@tonic-gate } 4420Sstevel@tonic-gate rw_exit(&ip->i_contents); 4430Sstevel@tonic-gate return (0); 4440Sstevel@tonic-gate noinodes: 4450Sstevel@tonic-gate if (!(TRANS_ISTRANS(ufsvfsp)) || !(pip->i_flag & IQUIET)) 4460Sstevel@tonic-gate cmn_err(CE_NOTE, "%s: out of inodes\n", fs->fs_fsmnt); 4470Sstevel@tonic-gate return (ENOSPC); 4480Sstevel@tonic-gate } 4490Sstevel@tonic-gate 4500Sstevel@tonic-gate /* 4510Sstevel@tonic-gate * Find a cylinder group to place a directory. 4520Sstevel@tonic-gate * Returns an inumber within the selected cylinder group. 4530Sstevel@tonic-gate * Note, the vfs_lock is not needed as we don't require exact cg summary info. 4540Sstevel@tonic-gate * 4550Sstevel@tonic-gate * If the switch ufs_close_dirs is set, then the policy is to use 4560Sstevel@tonic-gate * the current cg if it has more than 25% free inodes and more 4570Sstevel@tonic-gate * than 25% free blocks. Otherwise the cgs are searched from 4580Sstevel@tonic-gate * the beginning and the first cg with the same criteria is 4590Sstevel@tonic-gate * used. If that is also null then we revert to the old algorithm. 4600Sstevel@tonic-gate * This tends to cluster files at the beginning of the disk 4610Sstevel@tonic-gate * until the disk gets full. 4620Sstevel@tonic-gate * 4630Sstevel@tonic-gate * Otherwise if ufs_close_dirs is not set then the original policy is 4640Sstevel@tonic-gate * used which is to select from among those cylinder groups with 4650Sstevel@tonic-gate * above the average number of free inodes, the one with the smallest 4660Sstevel@tonic-gate * number of directories. 4670Sstevel@tonic-gate */ 4680Sstevel@tonic-gate 4690Sstevel@tonic-gate int ufs_close_dirs = 1; /* allocate directories close as possible */ 4700Sstevel@tonic-gate 4710Sstevel@tonic-gate ino_t 4720Sstevel@tonic-gate dirpref(inode_t *dp) 4730Sstevel@tonic-gate { 4740Sstevel@tonic-gate int cg, minndir, mincg, avgifree, mininode, minbpg, ifree; 4750Sstevel@tonic-gate struct fs *fs = dp->i_fs; 4760Sstevel@tonic-gate 4770Sstevel@tonic-gate cg = itog(fs, dp->i_number); 4780Sstevel@tonic-gate mininode = fs->fs_ipg >> 2; 4790Sstevel@tonic-gate minbpg = fs->fs_maxbpg >> 2; 4800Sstevel@tonic-gate if (ufs_close_dirs && 4810Sstevel@tonic-gate (fs->fs_cs(fs, cg).cs_nifree > mininode) && 4820Sstevel@tonic-gate (fs->fs_cs(fs, cg).cs_nbfree > minbpg)) { 4830Sstevel@tonic-gate return (dp->i_number); 4840Sstevel@tonic-gate } 4850Sstevel@tonic-gate 4860Sstevel@tonic-gate avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg; 4870Sstevel@tonic-gate minndir = fs->fs_ipg; 4880Sstevel@tonic-gate mincg = 0; 4890Sstevel@tonic-gate for (cg = 0; cg < fs->fs_ncg; cg++) { 4900Sstevel@tonic-gate ifree = fs->fs_cs(fs, cg).cs_nifree; 4910Sstevel@tonic-gate if (ufs_close_dirs && 4920Sstevel@tonic-gate (ifree > mininode) && 4930Sstevel@tonic-gate (fs->fs_cs(fs, cg).cs_nbfree > minbpg)) { 4940Sstevel@tonic-gate return ((ino_t)(fs->fs_ipg * cg)); 4950Sstevel@tonic-gate } 4960Sstevel@tonic-gate if ((fs->fs_cs(fs, cg).cs_ndir < minndir) && 4970Sstevel@tonic-gate (ifree >= avgifree)) { 4980Sstevel@tonic-gate mincg = cg; 4990Sstevel@tonic-gate minndir = fs->fs_cs(fs, cg).cs_ndir; 5000Sstevel@tonic-gate } 5010Sstevel@tonic-gate } 5020Sstevel@tonic-gate return ((ino_t)(fs->fs_ipg * mincg)); 5030Sstevel@tonic-gate } 5040Sstevel@tonic-gate 5050Sstevel@tonic-gate /* 5060Sstevel@tonic-gate * Select the desired position for the next block in a file. The file is 5070Sstevel@tonic-gate * logically divided into sections. The first section is composed of the 5080Sstevel@tonic-gate * direct blocks. Each additional section contains fs_maxbpg blocks. 5090Sstevel@tonic-gate * 5100Sstevel@tonic-gate * If no blocks have been allocated in the first section, the policy is to 5110Sstevel@tonic-gate * request a block in the same cylinder group as the inode that describes 5120Sstevel@tonic-gate * the file. If no blocks have been allocated in any other section, the 5130Sstevel@tonic-gate * policy is to place the section in a cylinder group with a greater than 5140Sstevel@tonic-gate * average number of free blocks. An appropriate cylinder group is found 5150Sstevel@tonic-gate * by using a rotor that sweeps the cylinder groups. When a new group of 5160Sstevel@tonic-gate * blocks is needed, the sweep begins in the cylinder group following the 5170Sstevel@tonic-gate * cylinder group from which the previous allocation was made. The sweep 5180Sstevel@tonic-gate * continues until a cylinder group with greater than the average number 5190Sstevel@tonic-gate * of free blocks is found. If the allocation is for the first block in an 5200Sstevel@tonic-gate * indirect block, the information on the previous allocation is unavailable; 5210Sstevel@tonic-gate * here a best guess is made based upon the logical block number being 5220Sstevel@tonic-gate * allocated. 5230Sstevel@tonic-gate * 5240Sstevel@tonic-gate * If a section is already partially allocated, the policy is to 5250Sstevel@tonic-gate * contiguously allocate fs_maxcontig blocks. The end of one of these 5260Sstevel@tonic-gate * contiguous blocks and the beginning of the next is physically separated 5270Sstevel@tonic-gate * so that the disk head will be in transit between them for at least 5280Sstevel@tonic-gate * fs_rotdelay milliseconds. This is to allow time for the processor to 5290Sstevel@tonic-gate * schedule another I/O transfer. 5300Sstevel@tonic-gate */ 5310Sstevel@tonic-gate daddr_t 5320Sstevel@tonic-gate blkpref(struct inode *ip, daddr_t lbn, int indx, daddr32_t *bap) 5330Sstevel@tonic-gate { 5340Sstevel@tonic-gate struct fs *fs; 5350Sstevel@tonic-gate struct ufsvfs *ufsvfsp; 5360Sstevel@tonic-gate int cg; 5370Sstevel@tonic-gate int avgbfree, startcg; 5380Sstevel@tonic-gate daddr_t nextblk; 5390Sstevel@tonic-gate 5400Sstevel@tonic-gate ufsvfsp = ip->i_ufsvfs; 5410Sstevel@tonic-gate fs = ip->i_fs; 5420Sstevel@tonic-gate if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { 5430Sstevel@tonic-gate if (lbn < NDADDR) { 5440Sstevel@tonic-gate cg = itog(fs, ip->i_number); 5450Sstevel@tonic-gate return (fs->fs_fpg * cg + fs->fs_frag); 5460Sstevel@tonic-gate } 5470Sstevel@tonic-gate /* 5480Sstevel@tonic-gate * Find a cylinder with greater than average 5490Sstevel@tonic-gate * number of unused data blocks. 5500Sstevel@tonic-gate */ 5510Sstevel@tonic-gate if (indx == 0 || bap[indx - 1] == 0) 5520Sstevel@tonic-gate startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg; 5530Sstevel@tonic-gate else 5540Sstevel@tonic-gate startcg = dtog(fs, bap[indx - 1]) + 1; 5550Sstevel@tonic-gate startcg %= fs->fs_ncg; 5560Sstevel@tonic-gate 5570Sstevel@tonic-gate mutex_enter(&ufsvfsp->vfs_lock); 5580Sstevel@tonic-gate avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 5590Sstevel@tonic-gate /* 5600Sstevel@tonic-gate * used for computing log space for writes/truncs 5610Sstevel@tonic-gate */ 5620Sstevel@tonic-gate ufsvfsp->vfs_avgbfree = avgbfree; 5630Sstevel@tonic-gate for (cg = startcg; cg < fs->fs_ncg; cg++) 5640Sstevel@tonic-gate if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 5650Sstevel@tonic-gate fs->fs_cgrotor = cg; 5660Sstevel@tonic-gate mutex_exit(&ufsvfsp->vfs_lock); 5670Sstevel@tonic-gate return (fs->fs_fpg * cg + fs->fs_frag); 5680Sstevel@tonic-gate } 5690Sstevel@tonic-gate for (cg = 0; cg <= startcg; cg++) 5700Sstevel@tonic-gate if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 5710Sstevel@tonic-gate fs->fs_cgrotor = cg; 5720Sstevel@tonic-gate mutex_exit(&ufsvfsp->vfs_lock); 5730Sstevel@tonic-gate return (fs->fs_fpg * cg + fs->fs_frag); 5740Sstevel@tonic-gate } 5750Sstevel@tonic-gate mutex_exit(&ufsvfsp->vfs_lock); 5760Sstevel@tonic-gate return (NULL); 5770Sstevel@tonic-gate } 5780Sstevel@tonic-gate /* 5790Sstevel@tonic-gate * One or more previous blocks have been laid out. If less 5800Sstevel@tonic-gate * than fs_maxcontig previous blocks are contiguous, the 5810Sstevel@tonic-gate * next block is requested contiguously, otherwise it is 5820Sstevel@tonic-gate * requested rotationally delayed by fs_rotdelay milliseconds. 5830Sstevel@tonic-gate */ 5840Sstevel@tonic-gate nextblk = bap[indx - 1] + fs->fs_frag; 5850Sstevel@tonic-gate if (indx > fs->fs_maxcontig && 5860Sstevel@tonic-gate bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig) 5870Sstevel@tonic-gate != nextblk) 5880Sstevel@tonic-gate return (nextblk); 5890Sstevel@tonic-gate if (fs->fs_rotdelay != 0) 5900Sstevel@tonic-gate /* 5910Sstevel@tonic-gate * Here we convert ms of delay to frags as: 5920Sstevel@tonic-gate * (frags) = (ms) * (rev/sec) * (sect/rev) / 5930Sstevel@tonic-gate * ((sect/frag) * (ms/sec)) 5940Sstevel@tonic-gate * then round up to the next block. 5950Sstevel@tonic-gate */ 5960Sstevel@tonic-gate nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect / 5970Sstevel@tonic-gate (NSPF(fs) * 1000), fs->fs_frag); 5980Sstevel@tonic-gate return (nextblk); 5990Sstevel@tonic-gate } 6000Sstevel@tonic-gate 6010Sstevel@tonic-gate /* 6020Sstevel@tonic-gate * Free a block or fragment. 6030Sstevel@tonic-gate * 6040Sstevel@tonic-gate * The specified block or fragment is placed back in the 6050Sstevel@tonic-gate * free map. If a fragment is deallocated, a possible 6060Sstevel@tonic-gate * block reassembly is checked. 6070Sstevel@tonic-gate */ 6080Sstevel@tonic-gate void 6090Sstevel@tonic-gate free(struct inode *ip, daddr_t bno, off_t size, int flags) 6100Sstevel@tonic-gate { 6110Sstevel@tonic-gate struct fs *fs = ip->i_fs; 6120Sstevel@tonic-gate struct ufsvfs *ufsvfsp = ip->i_ufsvfs; 613*512Sjkennedy struct ufs_q *delq = &ufsvfsp->vfs_delete; 614*512Sjkennedy struct ufs_delq_info *delq_info = &ufsvfsp->vfs_delete_info; 6150Sstevel@tonic-gate struct cg *cgp; 6160Sstevel@tonic-gate struct buf *bp; 6170Sstevel@tonic-gate int cg, bmap, bbase; 6180Sstevel@tonic-gate int i; 6190Sstevel@tonic-gate uchar_t *blksfree; 6200Sstevel@tonic-gate int *blktot; 6210Sstevel@tonic-gate short *blks; 6220Sstevel@tonic-gate daddr_t blkno, cylno, rpos; 6230Sstevel@tonic-gate 6240Sstevel@tonic-gate if ((unsigned long)size > fs->fs_bsize || fragoff(fs, size) != 0) { 6250Sstevel@tonic-gate (void) ufs_fault(ITOV(ip), 6260Sstevel@tonic-gate "free: bad size, dev = 0x%lx, bsize = %d, size = %d, fs = %s\n", 6270Sstevel@tonic-gate ip->i_dev, fs->fs_bsize, (int)size, fs->fs_fsmnt); 6280Sstevel@tonic-gate return; 6290Sstevel@tonic-gate } 6300Sstevel@tonic-gate cg = dtog(fs, bno); 6310Sstevel@tonic-gate ASSERT(!ufs_badblock(ip, bno)); 6320Sstevel@tonic-gate bp = UFS_BREAD(ufsvfsp, ip->i_dev, (daddr_t)fsbtodb(fs, cgtod(fs, cg)), 6330Sstevel@tonic-gate (int)fs->fs_cgsize); 6340Sstevel@tonic-gate 6350Sstevel@tonic-gate cgp = bp->b_un.b_cg; 6360Sstevel@tonic-gate if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) { 6370Sstevel@tonic-gate brelse(bp); 6380Sstevel@tonic-gate return; 6390Sstevel@tonic-gate } 6400Sstevel@tonic-gate 6410Sstevel@tonic-gate if (!(flags & I_NOCANCEL)) 6420Sstevel@tonic-gate TRANS_CANCEL(ufsvfsp, ldbtob(fsbtodb(fs, bno)), size, flags); 6430Sstevel@tonic-gate if (flags & (I_DIR|I_IBLK|I_SHAD|I_QUOTA)) { 6440Sstevel@tonic-gate TRANS_MATA_FREE(ufsvfsp, ldbtob(fsbtodb(fs, bno)), size); 6450Sstevel@tonic-gate } 6460Sstevel@tonic-gate blksfree = cg_blksfree(cgp); 6470Sstevel@tonic-gate blktot = cg_blktot(cgp); 6480Sstevel@tonic-gate mutex_enter(&ufsvfsp->vfs_lock); 6490Sstevel@tonic-gate cgp->cg_time = gethrestime_sec(); 6500Sstevel@tonic-gate bno = dtogd(fs, bno); 6510Sstevel@tonic-gate if (size == fs->fs_bsize) { 6520Sstevel@tonic-gate blkno = fragstoblks(fs, bno); 6530Sstevel@tonic-gate cylno = cbtocylno(fs, bno); 6540Sstevel@tonic-gate rpos = cbtorpos(ufsvfsp, bno); 6550Sstevel@tonic-gate blks = cg_blks(ufsvfsp, cgp, cylno); 6560Sstevel@tonic-gate if (!isclrblock(fs, blksfree, blkno)) { 6570Sstevel@tonic-gate mutex_exit(&ufsvfsp->vfs_lock); 6580Sstevel@tonic-gate brelse(bp); 6590Sstevel@tonic-gate (void) ufs_fault(ITOV(ip), "free: freeing free block, " 6600Sstevel@tonic-gate "dev:0x%lx, block:%ld, ino:%lu, fs:%s", 6610Sstevel@tonic-gate ip->i_dev, bno, ip->i_number, fs->fs_fsmnt); 6620Sstevel@tonic-gate return; 6630Sstevel@tonic-gate } 6640Sstevel@tonic-gate setblock(fs, blksfree, blkno); 6650Sstevel@tonic-gate blks[rpos]++; 6660Sstevel@tonic-gate blktot[cylno]++; 6670Sstevel@tonic-gate cgp->cg_cs.cs_nbfree++; /* Log below */ 6680Sstevel@tonic-gate fs->fs_cstotal.cs_nbfree++; 6690Sstevel@tonic-gate fs->fs_cs(fs, cg).cs_nbfree++; 670*512Sjkennedy if (TRANS_ISTRANS(ufsvfsp) && (flags & I_ACCT)) { 671*512Sjkennedy mutex_enter(&delq->uq_mutex); 672*512Sjkennedy delq_info->delq_unreclaimed_blocks -= 673*512Sjkennedy btodb(fs->fs_bsize); 674*512Sjkennedy mutex_exit(&delq->uq_mutex); 675*512Sjkennedy } 6760Sstevel@tonic-gate } else { 6770Sstevel@tonic-gate bbase = bno - fragnum(fs, bno); 6780Sstevel@tonic-gate /* 6790Sstevel@tonic-gate * Decrement the counts associated with the old frags 6800Sstevel@tonic-gate */ 6810Sstevel@tonic-gate bmap = blkmap(fs, blksfree, bbase); 6820Sstevel@tonic-gate fragacct(fs, bmap, cgp->cg_frsum, -1); 6830Sstevel@tonic-gate /* 6840Sstevel@tonic-gate * Deallocate the fragment 6850Sstevel@tonic-gate */ 6860Sstevel@tonic-gate for (i = 0; i < numfrags(fs, size); i++) { 6870Sstevel@tonic-gate if (isset(blksfree, bno + i)) { 6880Sstevel@tonic-gate brelse(bp); 6890Sstevel@tonic-gate mutex_exit(&ufsvfsp->vfs_lock); 6900Sstevel@tonic-gate (void) ufs_fault(ITOV(ip), 6910Sstevel@tonic-gate "free: freeing free frag, " 6920Sstevel@tonic-gate "dev:0x%lx, blk:%ld, cg:%d, " 6930Sstevel@tonic-gate "ino:%lu, fs:%s", 6940Sstevel@tonic-gate ip->i_dev, 6950Sstevel@tonic-gate bno + i, 6960Sstevel@tonic-gate cgp->cg_cgx, 6970Sstevel@tonic-gate ip->i_number, 6980Sstevel@tonic-gate fs->fs_fsmnt); 6990Sstevel@tonic-gate return; 7000Sstevel@tonic-gate } 7010Sstevel@tonic-gate setbit(blksfree, bno + i); 7020Sstevel@tonic-gate } 7030Sstevel@tonic-gate cgp->cg_cs.cs_nffree += i; 7040Sstevel@tonic-gate fs->fs_cstotal.cs_nffree += i; 7050Sstevel@tonic-gate fs->fs_cs(fs, cg).cs_nffree += i; 706*512Sjkennedy if (TRANS_ISTRANS(ufsvfsp) && (flags & I_ACCT)) { 707*512Sjkennedy mutex_enter(&delq->uq_mutex); 708*512Sjkennedy delq_info->delq_unreclaimed_blocks -= 709*512Sjkennedy btodb(i * fs->fs_fsize); 710*512Sjkennedy mutex_exit(&delq->uq_mutex); 711*512Sjkennedy } 7120Sstevel@tonic-gate /* 7130Sstevel@tonic-gate * Add back in counts associated with the new frags 7140Sstevel@tonic-gate */ 7150Sstevel@tonic-gate bmap = blkmap(fs, blksfree, bbase); 7160Sstevel@tonic-gate fragacct(fs, bmap, cgp->cg_frsum, 1); 7170Sstevel@tonic-gate /* 7180Sstevel@tonic-gate * If a complete block has been reassembled, account for it 7190Sstevel@tonic-gate */ 7200Sstevel@tonic-gate blkno = fragstoblks(fs, bbase); 7210Sstevel@tonic-gate if (isblock(fs, blksfree, blkno)) { 7220Sstevel@tonic-gate cylno = cbtocylno(fs, bbase); 7230Sstevel@tonic-gate rpos = cbtorpos(ufsvfsp, bbase); 7240Sstevel@tonic-gate blks = cg_blks(ufsvfsp, cgp, cylno); 7250Sstevel@tonic-gate blks[rpos]++; 7260Sstevel@tonic-gate blktot[cylno]++; 7270Sstevel@tonic-gate cgp->cg_cs.cs_nffree -= fs->fs_frag; 7280Sstevel@tonic-gate fs->fs_cstotal.cs_nffree -= fs->fs_frag; 7290Sstevel@tonic-gate fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 7300Sstevel@tonic-gate cgp->cg_cs.cs_nbfree++; 7310Sstevel@tonic-gate fs->fs_cstotal.cs_nbfree++; 7320Sstevel@tonic-gate fs->fs_cs(fs, cg).cs_nbfree++; 7330Sstevel@tonic-gate } 7340Sstevel@tonic-gate } 7350Sstevel@tonic-gate fs->fs_fmod = 1; 7360Sstevel@tonic-gate ufs_notclean(ufsvfsp); 7370Sstevel@tonic-gate TRANS_BUF(ufsvfsp, 0, fs->fs_cgsize, bp, DT_CG); 7380Sstevel@tonic-gate TRANS_SI(ufsvfsp, fs, cg); 7390Sstevel@tonic-gate bdrwrite(bp); 7400Sstevel@tonic-gate } 7410Sstevel@tonic-gate 7420Sstevel@tonic-gate /* 7430Sstevel@tonic-gate * Free an inode. 7440Sstevel@tonic-gate * 7450Sstevel@tonic-gate * The specified inode is placed back in the free map. 7460Sstevel@tonic-gate */ 7470Sstevel@tonic-gate void 7480Sstevel@tonic-gate ufs_ifree(struct inode *ip, ino_t ino, mode_t mode) 7490Sstevel@tonic-gate { 7500Sstevel@tonic-gate struct fs *fs = ip->i_fs; 7510Sstevel@tonic-gate struct ufsvfs *ufsvfsp = ip->i_ufsvfs; 7520Sstevel@tonic-gate struct cg *cgp; 7530Sstevel@tonic-gate struct buf *bp; 7540Sstevel@tonic-gate unsigned int inot; 7550Sstevel@tonic-gate int cg; 7560Sstevel@tonic-gate char *iused; 7570Sstevel@tonic-gate 7580Sstevel@tonic-gate if (ip->i_number == ino && ip->i_mode != 0) { 7590Sstevel@tonic-gate (void) ufs_fault(ITOV(ip), 7600Sstevel@tonic-gate "ufs_ifree: illegal mode: (imode) %o, (omode) %o, ino %d, " 7610Sstevel@tonic-gate "fs = %s\n", 7620Sstevel@tonic-gate ip->i_mode, mode, (int)ip->i_number, fs->fs_fsmnt); 7630Sstevel@tonic-gate return; 7640Sstevel@tonic-gate } 7650Sstevel@tonic-gate if (ino >= fs->fs_ipg * fs->fs_ncg) { 7660Sstevel@tonic-gate (void) ufs_fault(ITOV(ip), 7670Sstevel@tonic-gate "ifree: range, dev = 0x%x, ino = %d, fs = %s\n", 7680Sstevel@tonic-gate (int)ip->i_dev, (int)ino, fs->fs_fsmnt); 7690Sstevel@tonic-gate return; 7700Sstevel@tonic-gate } 7710Sstevel@tonic-gate cg = (int)itog(fs, ino); 7720Sstevel@tonic-gate bp = UFS_BREAD(ufsvfsp, ip->i_dev, (daddr_t)fsbtodb(fs, cgtod(fs, cg)), 7730Sstevel@tonic-gate (int)fs->fs_cgsize); 7740Sstevel@tonic-gate 7750Sstevel@tonic-gate cgp = bp->b_un.b_cg; 7760Sstevel@tonic-gate if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) { 7770Sstevel@tonic-gate brelse(bp); 7780Sstevel@tonic-gate return; 7790Sstevel@tonic-gate } 7800Sstevel@tonic-gate mutex_enter(&ufsvfsp->vfs_lock); 7810Sstevel@tonic-gate cgp->cg_time = gethrestime_sec(); 7820Sstevel@tonic-gate iused = cg_inosused(cgp); 7830Sstevel@tonic-gate inot = (unsigned int)(ino % (ulong_t)fs->fs_ipg); 7840Sstevel@tonic-gate if (isclr(iused, inot)) { 7850Sstevel@tonic-gate mutex_exit(&ufsvfsp->vfs_lock); 7860Sstevel@tonic-gate brelse(bp); 7870Sstevel@tonic-gate (void) ufs_fault(ITOV(ip), "ufs_ifree: freeing free inode, " 7880Sstevel@tonic-gate "mode: (imode) %o, (omode) %o, ino:%d, " 7890Sstevel@tonic-gate "fs:%s", 7900Sstevel@tonic-gate ip->i_mode, mode, (int)ino, fs->fs_fsmnt); 7910Sstevel@tonic-gate return; 7920Sstevel@tonic-gate } 7930Sstevel@tonic-gate clrbit(iused, inot); 7940Sstevel@tonic-gate 7950Sstevel@tonic-gate if (inot < (ulong_t)cgp->cg_irotor) 7960Sstevel@tonic-gate cgp->cg_irotor = inot; 7970Sstevel@tonic-gate cgp->cg_cs.cs_nifree++; 7980Sstevel@tonic-gate fs->fs_cstotal.cs_nifree++; 7990Sstevel@tonic-gate fs->fs_cs(fs, cg).cs_nifree++; 8000Sstevel@tonic-gate if (((mode & IFMT) == IFDIR) || ((mode & IFMT) == IFATTRDIR)) { 8010Sstevel@tonic-gate cgp->cg_cs.cs_ndir--; 8020Sstevel@tonic-gate fs->fs_cstotal.cs_ndir--; 8030Sstevel@tonic-gate fs->fs_cs(fs, cg).cs_ndir--; 8040Sstevel@tonic-gate } 8050Sstevel@tonic-gate fs->fs_fmod = 1; 8060Sstevel@tonic-gate ufs_notclean(ufsvfsp); 8070Sstevel@tonic-gate TRANS_BUF(ufsvfsp, 0, fs->fs_cgsize, bp, DT_CG); 8080Sstevel@tonic-gate TRANS_SI(ufsvfsp, fs, cg); 8090Sstevel@tonic-gate bdrwrite(bp); 8100Sstevel@tonic-gate } 8110Sstevel@tonic-gate 8120Sstevel@tonic-gate /* 8130Sstevel@tonic-gate * Implement the cylinder overflow algorithm. 8140Sstevel@tonic-gate * 8150Sstevel@tonic-gate * The policy implemented by this algorithm is: 8160Sstevel@tonic-gate * 1) allocate the block in its requested cylinder group. 8170Sstevel@tonic-gate * 2) quadratically rehash on the cylinder group number. 8180Sstevel@tonic-gate * 3) brute force search for a free block. 8190Sstevel@tonic-gate * The size parameter means size for data blocks, mode for inodes. 8200Sstevel@tonic-gate */ 8210Sstevel@tonic-gate static ino_t 8220Sstevel@tonic-gate hashalloc(struct inode *ip, int cg, long pref, int size, ulong_t (*allocator)()) 8230Sstevel@tonic-gate { 8240Sstevel@tonic-gate struct fs *fs; 8250Sstevel@tonic-gate int i; 8260Sstevel@tonic-gate long result; 8270Sstevel@tonic-gate int icg = cg; 8280Sstevel@tonic-gate 8290Sstevel@tonic-gate fs = ip->i_fs; 8300Sstevel@tonic-gate /* 8310Sstevel@tonic-gate * 1: preferred cylinder group 8320Sstevel@tonic-gate */ 8330Sstevel@tonic-gate result = (*allocator)(ip, cg, pref, size); 8340Sstevel@tonic-gate if (result) 8350Sstevel@tonic-gate return (result); 8360Sstevel@tonic-gate /* 8370Sstevel@tonic-gate * 2: quadratic rehash 8380Sstevel@tonic-gate */ 8390Sstevel@tonic-gate for (i = 1; i < fs->fs_ncg; i *= 2) { 8400Sstevel@tonic-gate cg += i; 8410Sstevel@tonic-gate if (cg >= fs->fs_ncg) 8420Sstevel@tonic-gate cg -= fs->fs_ncg; 8430Sstevel@tonic-gate result = (*allocator)(ip, cg, 0, size); 8440Sstevel@tonic-gate if (result) 8450Sstevel@tonic-gate return (result); 8460Sstevel@tonic-gate } 8470Sstevel@tonic-gate /* 8480Sstevel@tonic-gate * 3: brute force search 8490Sstevel@tonic-gate * Note that we start at i == 2, since 0 was checked initially, 8500Sstevel@tonic-gate * and 1 is always checked in the quadratic rehash. 8510Sstevel@tonic-gate */ 8520Sstevel@tonic-gate cg = (icg + 2) % fs->fs_ncg; 8530Sstevel@tonic-gate for (i = 2; i < fs->fs_ncg; i++) { 8540Sstevel@tonic-gate result = (*allocator)(ip, cg, 0, size); 8550Sstevel@tonic-gate if (result) 8560Sstevel@tonic-gate return (result); 8570Sstevel@tonic-gate cg++; 8580Sstevel@tonic-gate if (cg == fs->fs_ncg) 8590Sstevel@tonic-gate cg = 0; 8600Sstevel@tonic-gate } 8610Sstevel@tonic-gate return (NULL); 8620Sstevel@tonic-gate } 8630Sstevel@tonic-gate 8640Sstevel@tonic-gate /* 8650Sstevel@tonic-gate * Determine whether a fragment can be extended. 8660Sstevel@tonic-gate * 8670Sstevel@tonic-gate * Check to see if the necessary fragments are available, and 8680Sstevel@tonic-gate * if they are, allocate them. 8690Sstevel@tonic-gate */ 8700Sstevel@tonic-gate static daddr_t 8710Sstevel@tonic-gate fragextend(struct inode *ip, int cg, long bprev, int osize, int nsize) 8720Sstevel@tonic-gate { 8730Sstevel@tonic-gate struct ufsvfs *ufsvfsp = ip->i_ufsvfs; 8740Sstevel@tonic-gate struct fs *fs = ip->i_fs; 8750Sstevel@tonic-gate struct buf *bp; 8760Sstevel@tonic-gate struct cg *cgp; 8770Sstevel@tonic-gate uchar_t *blksfree; 8780Sstevel@tonic-gate long bno; 8790Sstevel@tonic-gate int frags, bbase; 8800Sstevel@tonic-gate int i, j; 8810Sstevel@tonic-gate 8820Sstevel@tonic-gate if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize)) 8830Sstevel@tonic-gate return (NULL); 8840Sstevel@tonic-gate frags = numfrags(fs, nsize); 8850Sstevel@tonic-gate bbase = (int)fragnum(fs, bprev); 8860Sstevel@tonic-gate if (bbase > fragnum(fs, (bprev + frags - 1))) { 8870Sstevel@tonic-gate /* cannot extend across a block boundary */ 8880Sstevel@tonic-gate return (NULL); 8890Sstevel@tonic-gate } 8900Sstevel@tonic-gate 8910Sstevel@tonic-gate bp = UFS_BREAD(ufsvfsp, ip->i_dev, (daddr_t)fsbtodb(fs, cgtod(fs, cg)), 8920Sstevel@tonic-gate (int)fs->fs_cgsize); 8930Sstevel@tonic-gate cgp = bp->b_un.b_cg; 8940Sstevel@tonic-gate if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) { 8950Sstevel@tonic-gate brelse(bp); 8960Sstevel@tonic-gate return (NULL); 8970Sstevel@tonic-gate } 8980Sstevel@tonic-gate 8990Sstevel@tonic-gate blksfree = cg_blksfree(cgp); 9000Sstevel@tonic-gate mutex_enter(&ufsvfsp->vfs_lock); 9010Sstevel@tonic-gate bno = dtogd(fs, bprev); 9020Sstevel@tonic-gate for (i = numfrags(fs, osize); i < frags; i++) { 9030Sstevel@tonic-gate if (isclr(blksfree, bno + i)) { 9040Sstevel@tonic-gate mutex_exit(&ufsvfsp->vfs_lock); 9050Sstevel@tonic-gate brelse(bp); 9060Sstevel@tonic-gate return (NULL); 9070Sstevel@tonic-gate } 9080Sstevel@tonic-gate if ((TRANS_ISCANCEL(ufsvfsp, ldbtob(fsbtodb(fs, bprev + i)), 9090Sstevel@tonic-gate fs->fs_fsize))) { 9100Sstevel@tonic-gate mutex_exit(&ufsvfsp->vfs_lock); 9110Sstevel@tonic-gate brelse(bp); 9120Sstevel@tonic-gate return (NULL); 9130Sstevel@tonic-gate } 9140Sstevel@tonic-gate } 9150Sstevel@tonic-gate 9160Sstevel@tonic-gate cgp->cg_time = gethrestime_sec(); 9170Sstevel@tonic-gate /* 9180Sstevel@tonic-gate * The current fragment can be extended, 9190Sstevel@tonic-gate * deduct the count on fragment being extended into 9200Sstevel@tonic-gate * increase the count on the remaining fragment (if any) 9210Sstevel@tonic-gate * allocate the extended piece. 9220Sstevel@tonic-gate */ 9230Sstevel@tonic-gate for (i = frags; i < fs->fs_frag - bbase; i++) 9240Sstevel@tonic-gate if (isclr(blksfree, bno + i)) 9250Sstevel@tonic-gate break; 9260Sstevel@tonic-gate j = i - numfrags(fs, osize); 9270Sstevel@tonic-gate cgp->cg_frsum[j]--; 9280Sstevel@tonic-gate ASSERT(cgp->cg_frsum[j] >= 0); 9290Sstevel@tonic-gate if (i != frags) 9300Sstevel@tonic-gate cgp->cg_frsum[i - frags]++; 9310Sstevel@tonic-gate for (i = numfrags(fs, osize); i < frags; i++) { 9320Sstevel@tonic-gate clrbit(blksfree, bno + i); 9330Sstevel@tonic-gate cgp->cg_cs.cs_nffree--; 9340Sstevel@tonic-gate fs->fs_cs(fs, cg).cs_nffree--; 9350Sstevel@tonic-gate fs->fs_cstotal.cs_nffree--; 9360Sstevel@tonic-gate } 9370Sstevel@tonic-gate fs->fs_fmod = 1; 9380Sstevel@tonic-gate ufs_notclean(ufsvfsp); 9390Sstevel@tonic-gate TRANS_BUF(ufsvfsp, 0, fs->fs_cgsize, bp, DT_CG); 9400Sstevel@tonic-gate TRANS_SI(ufsvfsp, fs, cg); 9410Sstevel@tonic-gate bdrwrite(bp); 9420Sstevel@tonic-gate return ((daddr_t)bprev); 9430Sstevel@tonic-gate } 9440Sstevel@tonic-gate 9450Sstevel@tonic-gate /* 9460Sstevel@tonic-gate * Determine whether a block can be allocated. 9470Sstevel@tonic-gate * 9480Sstevel@tonic-gate * Check to see if a block of the apprpriate size 9490Sstevel@tonic-gate * is available, and if it is, allocate it. 9500Sstevel@tonic-gate */ 9510Sstevel@tonic-gate static daddr_t 9520Sstevel@tonic-gate alloccg(struct inode *ip, int cg, daddr_t bpref, int size) 9530Sstevel@tonic-gate { 9540Sstevel@tonic-gate struct ufsvfs *ufsvfsp = ip->i_ufsvfs; 9550Sstevel@tonic-gate struct fs *fs = ip->i_fs; 9560Sstevel@tonic-gate struct buf *bp; 9570Sstevel@tonic-gate struct cg *cgp; 9580Sstevel@tonic-gate uchar_t *blksfree; 9590Sstevel@tonic-gate int bno, frags; 9600Sstevel@tonic-gate int allocsiz; 9610Sstevel@tonic-gate int i; 9620Sstevel@tonic-gate 9630Sstevel@tonic-gate if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) 9640Sstevel@tonic-gate return (0); 9650Sstevel@tonic-gate bp = UFS_BREAD(ufsvfsp, ip->i_dev, (daddr_t)fsbtodb(fs, cgtod(fs, cg)), 9660Sstevel@tonic-gate (int)fs->fs_cgsize); 9670Sstevel@tonic-gate 9680Sstevel@tonic-gate cgp = bp->b_un.b_cg; 9690Sstevel@tonic-gate if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp) || 9700Sstevel@tonic-gate (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) { 9710Sstevel@tonic-gate brelse(bp); 9720Sstevel@tonic-gate return (0); 9730Sstevel@tonic-gate } 9740Sstevel@tonic-gate blksfree = cg_blksfree(cgp); 9750Sstevel@tonic-gate mutex_enter(&ufsvfsp->vfs_lock); 9760Sstevel@tonic-gate cgp->cg_time = gethrestime_sec(); 9770Sstevel@tonic-gate if (size == fs->fs_bsize) { 9780Sstevel@tonic-gate if ((bno = alloccgblk(ufsvfsp, cgp, bpref, bp)) == 0) 9790Sstevel@tonic-gate goto errout; 9800Sstevel@tonic-gate fs->fs_fmod = 1; 9810Sstevel@tonic-gate ufs_notclean(ufsvfsp); 9820Sstevel@tonic-gate TRANS_SI(ufsvfsp, fs, cg); 9830Sstevel@tonic-gate bdrwrite(bp); 9840Sstevel@tonic-gate return (bno); 9850Sstevel@tonic-gate } 9860Sstevel@tonic-gate /* 9870Sstevel@tonic-gate * Check to see if any fragments are already available 9880Sstevel@tonic-gate * allocsiz is the size which will be allocated, hacking 9890Sstevel@tonic-gate * it down to a smaller size if necessary. 9900Sstevel@tonic-gate */ 9910Sstevel@tonic-gate frags = numfrags(fs, size); 9920Sstevel@tonic-gate for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) 9930Sstevel@tonic-gate if (cgp->cg_frsum[allocsiz] != 0) 9940Sstevel@tonic-gate break; 9950Sstevel@tonic-gate 9960Sstevel@tonic-gate if (allocsiz != fs->fs_frag) 9970Sstevel@tonic-gate bno = mapsearch(ufsvfsp, cgp, bpref, allocsiz); 9980Sstevel@tonic-gate 9990Sstevel@tonic-gate if (allocsiz == fs->fs_frag || bno < 0) { 10000Sstevel@tonic-gate /* 10010Sstevel@tonic-gate * No fragments were available, so a block 10020Sstevel@tonic-gate * will be allocated and hacked up. 10030Sstevel@tonic-gate */ 10040Sstevel@tonic-gate if (cgp->cg_cs.cs_nbfree == 0) 10050Sstevel@tonic-gate goto errout; 10060Sstevel@tonic-gate if ((bno = alloccgblk(ufsvfsp, cgp, bpref, bp)) == 0) 10070Sstevel@tonic-gate goto errout; 10080Sstevel@tonic-gate bpref = dtogd(fs, bno); 10090Sstevel@tonic-gate for (i = frags; i < fs->fs_frag; i++) 10100Sstevel@tonic-gate setbit(blksfree, bpref + i); 10110Sstevel@tonic-gate i = fs->fs_frag - frags; 10120Sstevel@tonic-gate cgp->cg_cs.cs_nffree += i; 10130Sstevel@tonic-gate fs->fs_cstotal.cs_nffree += i; 10140Sstevel@tonic-gate fs->fs_cs(fs, cg).cs_nffree += i; 10150Sstevel@tonic-gate cgp->cg_frsum[i]++; 10160Sstevel@tonic-gate fs->fs_fmod = 1; 10170Sstevel@tonic-gate ufs_notclean(ufsvfsp); 10180Sstevel@tonic-gate TRANS_SI(ufsvfsp, fs, cg); 10190Sstevel@tonic-gate bdrwrite(bp); 10200Sstevel@tonic-gate return (bno); 10210Sstevel@tonic-gate } 10220Sstevel@tonic-gate 10230Sstevel@tonic-gate for (i = 0; i < frags; i++) 10240Sstevel@tonic-gate clrbit(blksfree, bno + i); 10250Sstevel@tonic-gate cgp->cg_cs.cs_nffree -= frags; 10260Sstevel@tonic-gate fs->fs_cstotal.cs_nffree -= frags; 10270Sstevel@tonic-gate fs->fs_cs(fs, cg).cs_nffree -= frags; 10280Sstevel@tonic-gate cgp->cg_frsum[allocsiz]--; 10290Sstevel@tonic-gate ASSERT(cgp->cg_frsum[allocsiz] >= 0); 10300Sstevel@tonic-gate if (frags != allocsiz) { 10310Sstevel@tonic-gate cgp->cg_frsum[allocsiz - frags]++; 10320Sstevel@tonic-gate } 10330Sstevel@tonic-gate fs->fs_fmod = 1; 10340Sstevel@tonic-gate ufs_notclean(ufsvfsp); 10350Sstevel@tonic-gate TRANS_BUF(ufsvfsp, 0, fs->fs_cgsize, bp, DT_CG); 10360Sstevel@tonic-gate TRANS_SI(ufsvfsp, fs, cg); 10370Sstevel@tonic-gate bdrwrite(bp); 10380Sstevel@tonic-gate return (cg * fs->fs_fpg + bno); 10390Sstevel@tonic-gate errout: 10400Sstevel@tonic-gate mutex_exit(&ufsvfsp->vfs_lock); 10410Sstevel@tonic-gate brelse(bp); 10420Sstevel@tonic-gate return (0); 10430Sstevel@tonic-gate } 10440Sstevel@tonic-gate 10450Sstevel@tonic-gate /* 10460Sstevel@tonic-gate * Allocate a block in a cylinder group. 10470Sstevel@tonic-gate * 10480Sstevel@tonic-gate * This algorithm implements the following policy: 10490Sstevel@tonic-gate * 1) allocate the requested block. 10500Sstevel@tonic-gate * 2) allocate a rotationally optimal block in the same cylinder. 10510Sstevel@tonic-gate * 3) allocate the next available block on the block rotor for the 10520Sstevel@tonic-gate * specified cylinder group. 10530Sstevel@tonic-gate * Note that this routine only allocates fs_bsize blocks; these 10540Sstevel@tonic-gate * blocks may be fragmented by the routine that allocates them. 10550Sstevel@tonic-gate */ 10560Sstevel@tonic-gate static daddr_t 10570Sstevel@tonic-gate alloccgblk( 10580Sstevel@tonic-gate struct ufsvfs *ufsvfsp, 10590Sstevel@tonic-gate struct cg *cgp, 10600Sstevel@tonic-gate daddr_t bpref, 10610Sstevel@tonic-gate struct buf *bp) 10620Sstevel@tonic-gate { 10630Sstevel@tonic-gate daddr_t bno; 10640Sstevel@tonic-gate int cylno, pos, delta, rotbl_size; 10650Sstevel@tonic-gate short *cylbp; 10660Sstevel@tonic-gate int i; 10670Sstevel@tonic-gate struct fs *fs; 10680Sstevel@tonic-gate uchar_t *blksfree; 10690Sstevel@tonic-gate daddr_t blkno, rpos, frag; 10700Sstevel@tonic-gate short *blks; 10710Sstevel@tonic-gate int32_t *blktot; 10720Sstevel@tonic-gate 10730Sstevel@tonic-gate ASSERT(MUTEX_HELD(&ufsvfsp->vfs_lock)); 10740Sstevel@tonic-gate fs = ufsvfsp->vfs_fs; 10750Sstevel@tonic-gate blksfree = cg_blksfree(cgp); 10760Sstevel@tonic-gate if (bpref == 0) { 10770Sstevel@tonic-gate bpref = cgp->cg_rotor; 10780Sstevel@tonic-gate goto norot; 10790Sstevel@tonic-gate } 10800Sstevel@tonic-gate bpref = blknum(fs, bpref); 10810Sstevel@tonic-gate bpref = dtogd(fs, bpref); 10820Sstevel@tonic-gate /* 10830Sstevel@tonic-gate * If the requested block is available, use it. 10840Sstevel@tonic-gate */ 10850Sstevel@tonic-gate if (isblock(fs, blksfree, (daddr_t)fragstoblks(fs, bpref))) { 10860Sstevel@tonic-gate bno = bpref; 10870Sstevel@tonic-gate goto gotit; 10880Sstevel@tonic-gate } 10890Sstevel@tonic-gate /* 10900Sstevel@tonic-gate * Check for a block available on the same cylinder. 10910Sstevel@tonic-gate */ 10920Sstevel@tonic-gate cylno = cbtocylno(fs, bpref); 10930Sstevel@tonic-gate if (cg_blktot(cgp)[cylno] == 0) 10940Sstevel@tonic-gate goto norot; 10950Sstevel@tonic-gate if (fs->fs_cpc == 0) { 10960Sstevel@tonic-gate /* 10970Sstevel@tonic-gate * Block layout info is not available, so just 10980Sstevel@tonic-gate * have to take any block in this cylinder. 10990Sstevel@tonic-gate */ 11000Sstevel@tonic-gate bpref = howmany(fs->fs_spc * cylno, NSPF(fs)); 11010Sstevel@tonic-gate goto norot; 11020Sstevel@tonic-gate } 11030Sstevel@tonic-gate /* 11040Sstevel@tonic-gate * Check the summary information to see if a block is 11050Sstevel@tonic-gate * available in the requested cylinder starting at the 11060Sstevel@tonic-gate * requested rotational position and proceeding around. 11070Sstevel@tonic-gate */ 11080Sstevel@tonic-gate cylbp = cg_blks(ufsvfsp, cgp, cylno); 11090Sstevel@tonic-gate pos = cbtorpos(ufsvfsp, bpref); 11100Sstevel@tonic-gate for (i = pos; i < ufsvfsp->vfs_nrpos; i++) 11110Sstevel@tonic-gate if (cylbp[i] > 0) 11120Sstevel@tonic-gate break; 11130Sstevel@tonic-gate if (i == ufsvfsp->vfs_nrpos) 11140Sstevel@tonic-gate for (i = 0; i < pos; i++) 11150Sstevel@tonic-gate if (cylbp[i] > 0) 11160Sstevel@tonic-gate break; 11170Sstevel@tonic-gate if (cylbp[i] > 0) { 11180Sstevel@tonic-gate /* 11190Sstevel@tonic-gate * Found a rotational position, now find the actual 11200Sstevel@tonic-gate * block. A "panic" if none is actually there. 11210Sstevel@tonic-gate */ 11220Sstevel@tonic-gate 11230Sstevel@tonic-gate /* 11240Sstevel@tonic-gate * Up to this point, "pos" has referred to the rotational 11250Sstevel@tonic-gate * position of the desired block. From now on, it holds 11260Sstevel@tonic-gate * the offset of the current cylinder within a cylinder 11270Sstevel@tonic-gate * cycle. (A cylinder cycle refers to a set of cylinders 11280Sstevel@tonic-gate * which are described by a single rotational table; the 11290Sstevel@tonic-gate * size of the cycle is fs_cpc.) 11300Sstevel@tonic-gate * 11310Sstevel@tonic-gate * bno is set to the block number of the first block within 11320Sstevel@tonic-gate * the current cylinder cycle. 11330Sstevel@tonic-gate */ 11340Sstevel@tonic-gate 11350Sstevel@tonic-gate pos = cylno % fs->fs_cpc; 11360Sstevel@tonic-gate bno = (cylno - pos) * fs->fs_spc / NSPB(fs); 11370Sstevel@tonic-gate 11380Sstevel@tonic-gate /* 11390Sstevel@tonic-gate * The blocks within a cylinder are grouped into equivalence 11400Sstevel@tonic-gate * classes according to their "rotational position." There 11410Sstevel@tonic-gate * are two tables used to determine these classes. 11420Sstevel@tonic-gate * 11430Sstevel@tonic-gate * The positional offset table (fs_postbl) has an entry for 11440Sstevel@tonic-gate * each rotational position of each cylinder in a cylinder 11450Sstevel@tonic-gate * cycle. This entry contains the relative block number 11460Sstevel@tonic-gate * (counting from the start of the cylinder cycle) of the 11470Sstevel@tonic-gate * first block in the equivalence class for that position 11480Sstevel@tonic-gate * and that cylinder. Positions for which no blocks exist 11490Sstevel@tonic-gate * are indicated by a -1. 11500Sstevel@tonic-gate * 11510Sstevel@tonic-gate * The rotational delta table (fs_rotbl) has an entry for 11520Sstevel@tonic-gate * each block in a cylinder cycle. This entry contains 11530Sstevel@tonic-gate * the offset from that block to the next block in the 11540Sstevel@tonic-gate * same equivalence class. The last block in the class 11550Sstevel@tonic-gate * is indicated by a zero in the table. 11560Sstevel@tonic-gate * 11570Sstevel@tonic-gate * The following code, then, walks through all of the blocks 11580Sstevel@tonic-gate * in the cylinder (cylno) which we're allocating within 11590Sstevel@tonic-gate * which are in the equivalence class for the rotational 11600Sstevel@tonic-gate * position (i) which we're allocating within. 11610Sstevel@tonic-gate */ 11620Sstevel@tonic-gate 11630Sstevel@tonic-gate if (fs_postbl(ufsvfsp, pos)[i] == -1) { 11640Sstevel@tonic-gate (void) ufs_fault(ufsvfsp->vfs_root, 11650Sstevel@tonic-gate "alloccgblk: cyl groups corrupted, pos = %d, i = %d, fs = %s\n", 11660Sstevel@tonic-gate pos, i, fs->fs_fsmnt); 11670Sstevel@tonic-gate return (0); 11680Sstevel@tonic-gate } 11690Sstevel@tonic-gate 11700Sstevel@tonic-gate /* 11710Sstevel@tonic-gate * There is one entry in the rotational table for each block 11720Sstevel@tonic-gate * in the cylinder cycle. These are whole blocks, not frags. 11730Sstevel@tonic-gate */ 11740Sstevel@tonic-gate 11750Sstevel@tonic-gate rotbl_size = (fs->fs_cpc * fs->fs_spc) >> 11760Sstevel@tonic-gate (fs->fs_fragshift + fs->fs_fsbtodb); 11770Sstevel@tonic-gate 11780Sstevel@tonic-gate /* 11790Sstevel@tonic-gate * As we start, "i" is the rotational position within which 11800Sstevel@tonic-gate * we're searching. After the next line, it will be a block 11810Sstevel@tonic-gate * number (relative to the start of the cylinder cycle) 11820Sstevel@tonic-gate * within the equivalence class of that rotational position. 11830Sstevel@tonic-gate */ 11840Sstevel@tonic-gate 11850Sstevel@tonic-gate i = fs_postbl(ufsvfsp, pos)[i]; 11860Sstevel@tonic-gate 11870Sstevel@tonic-gate for (;;) { 11880Sstevel@tonic-gate if (isblock(fs, blksfree, (daddr_t)(bno + i))) { 11890Sstevel@tonic-gate bno = blkstofrags(fs, (bno + i)); 11900Sstevel@tonic-gate goto gotit; 11910Sstevel@tonic-gate } 11920Sstevel@tonic-gate delta = fs_rotbl(fs)[i]; 11930Sstevel@tonic-gate if (delta <= 0 || /* End of chain, or */ 11940Sstevel@tonic-gate delta + i > rotbl_size) /* end of table? */ 11950Sstevel@tonic-gate break; /* If so, panic. */ 11960Sstevel@tonic-gate i += delta; 11970Sstevel@tonic-gate } 11980Sstevel@tonic-gate (void) ufs_fault(ufsvfsp->vfs_root, 11990Sstevel@tonic-gate "alloccgblk: can't find blk in cyl, pos:%d, i:%d, fs:%s bno: %x\n", 12000Sstevel@tonic-gate pos, i, fs->fs_fsmnt, (int)bno); 12010Sstevel@tonic-gate return (0); 12020Sstevel@tonic-gate } 12030Sstevel@tonic-gate norot: 12040Sstevel@tonic-gate /* 12050Sstevel@tonic-gate * No blocks in the requested cylinder, so take 12060Sstevel@tonic-gate * next available one in this cylinder group. 12070Sstevel@tonic-gate */ 12080Sstevel@tonic-gate bno = mapsearch(ufsvfsp, cgp, bpref, (int)fs->fs_frag); 12090Sstevel@tonic-gate if (bno < 0) 12100Sstevel@tonic-gate return (0); 12110Sstevel@tonic-gate cgp->cg_rotor = bno; 12120Sstevel@tonic-gate gotit: 12130Sstevel@tonic-gate blkno = fragstoblks(fs, bno); 12140Sstevel@tonic-gate frag = (cgp->cg_cgx * fs->fs_fpg) + bno; 12150Sstevel@tonic-gate if (TRANS_ISCANCEL(ufsvfsp, ldbtob(fsbtodb(fs, frag)), fs->fs_bsize)) 12160Sstevel@tonic-gate goto norot; 12170Sstevel@tonic-gate clrblock(fs, blksfree, (long)blkno); 12180Sstevel@tonic-gate /* 12190Sstevel@tonic-gate * the other cg/sb/si fields are TRANS'ed by the caller 12200Sstevel@tonic-gate */ 12210Sstevel@tonic-gate cgp->cg_cs.cs_nbfree--; 12220Sstevel@tonic-gate fs->fs_cstotal.cs_nbfree--; 12230Sstevel@tonic-gate fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 12240Sstevel@tonic-gate cylno = cbtocylno(fs, bno); 12250Sstevel@tonic-gate blks = cg_blks(ufsvfsp, cgp, cylno); 12260Sstevel@tonic-gate rpos = cbtorpos(ufsvfsp, bno); 12270Sstevel@tonic-gate blktot = cg_blktot(cgp); 12280Sstevel@tonic-gate blks[rpos]--; 12290Sstevel@tonic-gate blktot[cylno]--; 12300Sstevel@tonic-gate TRANS_BUF(ufsvfsp, 0, fs->fs_cgsize, bp, DT_CG); 12310Sstevel@tonic-gate fs->fs_fmod = 1; 12320Sstevel@tonic-gate return (frag); 12330Sstevel@tonic-gate } 12340Sstevel@tonic-gate 12350Sstevel@tonic-gate /* 12360Sstevel@tonic-gate * Determine whether an inode can be allocated. 12370Sstevel@tonic-gate * 12380Sstevel@tonic-gate * Check to see if an inode is available, and if it is, 12390Sstevel@tonic-gate * allocate it using the following policy: 12400Sstevel@tonic-gate * 1) allocate the requested inode. 12410Sstevel@tonic-gate * 2) allocate the next available inode after the requested 12420Sstevel@tonic-gate * inode in the specified cylinder group. 12430Sstevel@tonic-gate */ 12440Sstevel@tonic-gate static ino_t 12450Sstevel@tonic-gate ialloccg(struct inode *ip, int cg, daddr_t ipref, int mode) 12460Sstevel@tonic-gate { 12470Sstevel@tonic-gate struct ufsvfs *ufsvfsp = ip->i_ufsvfs; 12480Sstevel@tonic-gate struct fs *fs = ip->i_fs; 12490Sstevel@tonic-gate struct cg *cgp; 12500Sstevel@tonic-gate struct buf *bp; 12510Sstevel@tonic-gate int start, len, loc, map, i; 12520Sstevel@tonic-gate char *iused; 12530Sstevel@tonic-gate 12540Sstevel@tonic-gate if (fs->fs_cs(fs, cg).cs_nifree == 0) 12550Sstevel@tonic-gate return (0); 12560Sstevel@tonic-gate bp = UFS_BREAD(ufsvfsp, ip->i_dev, (daddr_t)fsbtodb(fs, cgtod(fs, cg)), 12570Sstevel@tonic-gate (int)fs->fs_cgsize); 12580Sstevel@tonic-gate 12590Sstevel@tonic-gate cgp = bp->b_un.b_cg; 12600Sstevel@tonic-gate if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp) || 12610Sstevel@tonic-gate cgp->cg_cs.cs_nifree == 0) { 12620Sstevel@tonic-gate brelse(bp); 12630Sstevel@tonic-gate return (0); 12640Sstevel@tonic-gate } 12650Sstevel@tonic-gate iused = cg_inosused(cgp); 12660Sstevel@tonic-gate mutex_enter(&ufsvfsp->vfs_lock); 12670Sstevel@tonic-gate /* 12680Sstevel@tonic-gate * While we are waiting for the mutex, someone may have taken 12690Sstevel@tonic-gate * the last available inode. Need to recheck. 12700Sstevel@tonic-gate */ 12710Sstevel@tonic-gate if (cgp->cg_cs.cs_nifree == 0) { 12720Sstevel@tonic-gate mutex_exit(&ufsvfsp->vfs_lock); 12730Sstevel@tonic-gate brelse(bp); 12740Sstevel@tonic-gate return (0); 12750Sstevel@tonic-gate } 12760Sstevel@tonic-gate 12770Sstevel@tonic-gate cgp->cg_time = gethrestime_sec(); 12780Sstevel@tonic-gate if (ipref) { 12790Sstevel@tonic-gate ipref %= fs->fs_ipg; 12800Sstevel@tonic-gate if (isclr(iused, ipref)) 12810Sstevel@tonic-gate goto gotit; 12820Sstevel@tonic-gate } 12830Sstevel@tonic-gate start = cgp->cg_irotor / NBBY; 12840Sstevel@tonic-gate len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY); 12850Sstevel@tonic-gate loc = skpc(0xff, (uint_t)len, &iused[start]); 12860Sstevel@tonic-gate if (loc == 0) { 12870Sstevel@tonic-gate len = start + 1; 12880Sstevel@tonic-gate start = 0; 12890Sstevel@tonic-gate loc = skpc(0xff, (uint_t)len, &iused[0]); 12900Sstevel@tonic-gate if (loc == 0) { 12910Sstevel@tonic-gate mutex_exit(&ufsvfsp->vfs_lock); 12920Sstevel@tonic-gate (void) ufs_fault(ITOV(ip), 12930Sstevel@tonic-gate "ialloccg: map corrupted, cg = %d, irotor = %d, fs = %s\n", 12940Sstevel@tonic-gate cg, (int)cgp->cg_irotor, fs->fs_fsmnt); 12950Sstevel@tonic-gate return (0); 12960Sstevel@tonic-gate } 12970Sstevel@tonic-gate } 12980Sstevel@tonic-gate i = start + len - loc; 12990Sstevel@tonic-gate map = iused[i]; 13000Sstevel@tonic-gate ipref = i * NBBY; 13010Sstevel@tonic-gate for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) { 13020Sstevel@tonic-gate if ((map & i) == 0) { 13030Sstevel@tonic-gate cgp->cg_irotor = ipref; 13040Sstevel@tonic-gate goto gotit; 13050Sstevel@tonic-gate } 13060Sstevel@tonic-gate } 13070Sstevel@tonic-gate 13080Sstevel@tonic-gate mutex_exit(&ufsvfsp->vfs_lock); 13090Sstevel@tonic-gate (void) ufs_fault(ITOV(ip), "ialloccg: block not in mapfs = %s", 13100Sstevel@tonic-gate fs->fs_fsmnt); 13110Sstevel@tonic-gate return (0); 13120Sstevel@tonic-gate gotit: 13130Sstevel@tonic-gate setbit(iused, ipref); 13140Sstevel@tonic-gate cgp->cg_cs.cs_nifree--; 13150Sstevel@tonic-gate fs->fs_cstotal.cs_nifree--; 13160Sstevel@tonic-gate fs->fs_cs(fs, cg).cs_nifree--; 13170Sstevel@tonic-gate if (((mode & IFMT) == IFDIR) || ((mode & IFMT) == IFATTRDIR)) { 13180Sstevel@tonic-gate cgp->cg_cs.cs_ndir++; 13190Sstevel@tonic-gate fs->fs_cstotal.cs_ndir++; 13200Sstevel@tonic-gate fs->fs_cs(fs, cg).cs_ndir++; 13210Sstevel@tonic-gate } 13220Sstevel@tonic-gate fs->fs_fmod = 1; 13230Sstevel@tonic-gate ufs_notclean(ufsvfsp); 13240Sstevel@tonic-gate TRANS_BUF(ufsvfsp, 0, fs->fs_cgsize, bp, DT_CG); 13250Sstevel@tonic-gate TRANS_SI(ufsvfsp, fs, cg); 13260Sstevel@tonic-gate bdrwrite(bp); 13270Sstevel@tonic-gate return (cg * fs->fs_ipg + ipref); 13280Sstevel@tonic-gate } 13290Sstevel@tonic-gate 13300Sstevel@tonic-gate /* 13310Sstevel@tonic-gate * Find a block of the specified size in the specified cylinder group. 13320Sstevel@tonic-gate * 13330Sstevel@tonic-gate * It is a panic if a request is made to find a block if none are 13340Sstevel@tonic-gate * available. 13350Sstevel@tonic-gate */ 13360Sstevel@tonic-gate static daddr_t 13370Sstevel@tonic-gate mapsearch(struct ufsvfs *ufsvfsp, struct cg *cgp, daddr_t bpref, 13380Sstevel@tonic-gate int allocsiz) 13390Sstevel@tonic-gate { 13400Sstevel@tonic-gate struct fs *fs = ufsvfsp->vfs_fs; 13410Sstevel@tonic-gate daddr_t bno, cfrag; 13420Sstevel@tonic-gate int start, len, loc, i, last, first, secondtime; 13430Sstevel@tonic-gate int blk, field, subfield, pos; 13440Sstevel@tonic-gate int gotit; 13450Sstevel@tonic-gate 13460Sstevel@tonic-gate /* 13470Sstevel@tonic-gate * ufsvfs->vfs_lock is held when calling this. 13480Sstevel@tonic-gate */ 13490Sstevel@tonic-gate /* 13500Sstevel@tonic-gate * Find the fragment by searching through the 13510Sstevel@tonic-gate * free block map for an appropriate bit pattern. 13520Sstevel@tonic-gate */ 13530Sstevel@tonic-gate if (bpref) 13540Sstevel@tonic-gate start = dtogd(fs, bpref) / NBBY; 13550Sstevel@tonic-gate else 13560Sstevel@tonic-gate start = cgp->cg_frotor / NBBY; 13570Sstevel@tonic-gate /* 13580Sstevel@tonic-gate * the following loop performs two scans -- the first scan 13590Sstevel@tonic-gate * searches the bottom half of the array for a match and the 13600Sstevel@tonic-gate * second scan searches the top half of the array. The loops 13610Sstevel@tonic-gate * have been merged just to make things difficult. 13620Sstevel@tonic-gate */ 13630Sstevel@tonic-gate first = start; 13640Sstevel@tonic-gate last = howmany(fs->fs_fpg, NBBY); 13650Sstevel@tonic-gate secondtime = 0; 13660Sstevel@tonic-gate cfrag = cgp->cg_cgx * fs->fs_fpg; 13670Sstevel@tonic-gate while (first < last) { 13680Sstevel@tonic-gate len = last - first; 13690Sstevel@tonic-gate /* 13700Sstevel@tonic-gate * search the array for a match 13710Sstevel@tonic-gate */ 13720Sstevel@tonic-gate loc = scanc((unsigned)len, (uchar_t *)&cg_blksfree(cgp)[first], 13730Sstevel@tonic-gate (uchar_t *)fragtbl[fs->fs_frag], 13740Sstevel@tonic-gate (int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 13750Sstevel@tonic-gate /* 13760Sstevel@tonic-gate * match found 13770Sstevel@tonic-gate */ 13780Sstevel@tonic-gate if (loc) { 13790Sstevel@tonic-gate bno = (last - loc) * NBBY; 13800Sstevel@tonic-gate 13810Sstevel@tonic-gate /* 13820Sstevel@tonic-gate * Found the byte in the map, sift 13830Sstevel@tonic-gate * through the bits to find the selected frag 13840Sstevel@tonic-gate */ 13850Sstevel@tonic-gate cgp->cg_frotor = bno; 13860Sstevel@tonic-gate gotit = 0; 13870Sstevel@tonic-gate for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { 13880Sstevel@tonic-gate blk = blkmap(fs, cg_blksfree(cgp), bno); 13890Sstevel@tonic-gate blk <<= 1; 13900Sstevel@tonic-gate field = around[allocsiz]; 13910Sstevel@tonic-gate subfield = inside[allocsiz]; 13920Sstevel@tonic-gate for (pos = 0; 13930Sstevel@tonic-gate pos <= fs->fs_frag - allocsiz; 13940Sstevel@tonic-gate pos++) { 13950Sstevel@tonic-gate if ((blk & field) == subfield) { 13960Sstevel@tonic-gate gotit++; 13970Sstevel@tonic-gate break; 13980Sstevel@tonic-gate } 13990Sstevel@tonic-gate field <<= 1; 14000Sstevel@tonic-gate subfield <<= 1; 14010Sstevel@tonic-gate } 14020Sstevel@tonic-gate if (gotit) 14030Sstevel@tonic-gate break; 14040Sstevel@tonic-gate } 14050Sstevel@tonic-gate bno += pos; 14060Sstevel@tonic-gate 14070Sstevel@tonic-gate /* 14080Sstevel@tonic-gate * success if block is *not* being converted from 14090Sstevel@tonic-gate * metadata into userdata (harpy). If so, ignore. 14100Sstevel@tonic-gate */ 14110Sstevel@tonic-gate if (!TRANS_ISCANCEL(ufsvfsp, 14120Sstevel@tonic-gate ldbtob(fsbtodb(fs, (cfrag+bno))), 14130Sstevel@tonic-gate allocsiz * fs->fs_fsize)) 14140Sstevel@tonic-gate return (bno); 14150Sstevel@tonic-gate /* 14160Sstevel@tonic-gate * keep looking -- this block is being converted 14170Sstevel@tonic-gate */ 14180Sstevel@tonic-gate first = (last - loc) + 1; 14190Sstevel@tonic-gate loc = 0; 14200Sstevel@tonic-gate if (first < last) 14210Sstevel@tonic-gate continue; 14220Sstevel@tonic-gate } 14230Sstevel@tonic-gate /* 14240Sstevel@tonic-gate * no usable matches in bottom half -- now search the top half 14250Sstevel@tonic-gate */ 14260Sstevel@tonic-gate if (secondtime) 14270Sstevel@tonic-gate /* 14280Sstevel@tonic-gate * no usable matches in top half -- all done 14290Sstevel@tonic-gate */ 14300Sstevel@tonic-gate break; 14310Sstevel@tonic-gate secondtime = 1; 14320Sstevel@tonic-gate last = start + 1; 14330Sstevel@tonic-gate first = 0; 14340Sstevel@tonic-gate } 14350Sstevel@tonic-gate /* 14360Sstevel@tonic-gate * no usable matches 14370Sstevel@tonic-gate */ 14380Sstevel@tonic-gate return ((daddr_t)-1); 14390Sstevel@tonic-gate } 14400Sstevel@tonic-gate 14410Sstevel@tonic-gate #define UFSNADDR (NDADDR + NIADDR) /* NADDR applies to (obsolete) S5FS */ 14420Sstevel@tonic-gate #define IB(i) (NDADDR + (i)) /* index of i'th indirect block ptr */ 14430Sstevel@tonic-gate #define SINGLE 0 /* single indirect block ptr */ 14440Sstevel@tonic-gate #define DOUBLE 1 /* double indirect block ptr */ 14450Sstevel@tonic-gate #define TRIPLE 2 /* triple indirect block ptr */ 14460Sstevel@tonic-gate 14470Sstevel@tonic-gate /* 14480Sstevel@tonic-gate * Free storage space associated with the specified inode. The portion 14490Sstevel@tonic-gate * to be freed is specified by lp->l_start and lp->l_len (already 14500Sstevel@tonic-gate * normalized to a "whence" of 0). 14510Sstevel@tonic-gate * 14520Sstevel@tonic-gate * This is an experimental facility whose continued existence is not 14530Sstevel@tonic-gate * guaranteed. Currently, we only support the special case 14540Sstevel@tonic-gate * of l_len == 0, meaning free to end of file. 14550Sstevel@tonic-gate * 14560Sstevel@tonic-gate * Blocks are freed in reverse order. This FILO algorithm will tend to 14570Sstevel@tonic-gate * maintain a contiguous free list much longer than FIFO. 14580Sstevel@tonic-gate * See also ufs_itrunc() in ufs_inode.c. 14590Sstevel@tonic-gate * 14600Sstevel@tonic-gate * Bug: unused bytes in the last retained block are not cleared. 14610Sstevel@tonic-gate * This may result in a "hole" in the file that does not read as zeroes. 14620Sstevel@tonic-gate */ 14630Sstevel@tonic-gate /* ARGSUSED */ 14640Sstevel@tonic-gate int 14650Sstevel@tonic-gate ufs_freesp(struct vnode *vp, struct flock64 *lp, int flag, cred_t *cr) 14660Sstevel@tonic-gate { 14670Sstevel@tonic-gate int i; 14680Sstevel@tonic-gate struct inode *ip = VTOI(vp); 14690Sstevel@tonic-gate int error; 14700Sstevel@tonic-gate 14710Sstevel@tonic-gate ASSERT(vp->v_type == VREG); 14720Sstevel@tonic-gate ASSERT(lp->l_start >= 0); /* checked by convoff */ 14730Sstevel@tonic-gate 14740Sstevel@tonic-gate if (lp->l_len != 0) 14750Sstevel@tonic-gate return (EINVAL); 14760Sstevel@tonic-gate 14770Sstevel@tonic-gate rw_enter(&ip->i_contents, RW_READER); 14780Sstevel@tonic-gate if (ip->i_size == (u_offset_t)lp->l_start) { 14790Sstevel@tonic-gate rw_exit(&ip->i_contents); 14800Sstevel@tonic-gate return (0); 14810Sstevel@tonic-gate } 14820Sstevel@tonic-gate 14830Sstevel@tonic-gate /* 14840Sstevel@tonic-gate * Check if there is any active mandatory lock on the 14850Sstevel@tonic-gate * range that will be truncated/expanded. 14860Sstevel@tonic-gate */ 14870Sstevel@tonic-gate if (MANDLOCK(vp, ip->i_mode)) { 14880Sstevel@tonic-gate offset_t save_start; 14890Sstevel@tonic-gate 14900Sstevel@tonic-gate save_start = lp->l_start; 14910Sstevel@tonic-gate 14920Sstevel@tonic-gate if (ip->i_size < lp->l_start) { 14930Sstevel@tonic-gate /* 14940Sstevel@tonic-gate * "Truncate up" case: need to make sure there 14950Sstevel@tonic-gate * is no lock beyond current end-of-file. To 14960Sstevel@tonic-gate * do so, we need to set l_start to the size 14970Sstevel@tonic-gate * of the file temporarily. 14980Sstevel@tonic-gate */ 14990Sstevel@tonic-gate lp->l_start = ip->i_size; 15000Sstevel@tonic-gate } 15010Sstevel@tonic-gate lp->l_type = F_WRLCK; 15020Sstevel@tonic-gate lp->l_sysid = 0; 15030Sstevel@tonic-gate lp->l_pid = ttoproc(curthread)->p_pid; 15040Sstevel@tonic-gate i = (flag & (FNDELAY|FNONBLOCK)) ? 0 : SLPFLCK; 15050Sstevel@tonic-gate rw_exit(&ip->i_contents); 15060Sstevel@tonic-gate if ((i = reclock(vp, lp, i, 0, lp->l_start, NULL)) != 0 || 15070Sstevel@tonic-gate lp->l_type != F_UNLCK) { 15080Sstevel@tonic-gate return (i ? i : EAGAIN); 15090Sstevel@tonic-gate } 15100Sstevel@tonic-gate rw_enter(&ip->i_contents, RW_READER); 15110Sstevel@tonic-gate 15120Sstevel@tonic-gate lp->l_start = save_start; 15130Sstevel@tonic-gate } 15140Sstevel@tonic-gate 15150Sstevel@tonic-gate /* 15160Sstevel@tonic-gate * Make sure a write isn't in progress (allocating blocks) 15170Sstevel@tonic-gate * by acquiring i_rwlock (we promised ufs_bmap we wouldn't 15180Sstevel@tonic-gate * truncate while it was allocating blocks). 15190Sstevel@tonic-gate * Grab the locks in the right order. 15200Sstevel@tonic-gate */ 15210Sstevel@tonic-gate rw_exit(&ip->i_contents); 15220Sstevel@tonic-gate rw_enter(&ip->i_rwlock, RW_WRITER); 15230Sstevel@tonic-gate error = TRANS_ITRUNC(ip, (u_offset_t)lp->l_start, 0, cr); 15240Sstevel@tonic-gate rw_exit(&ip->i_rwlock); 15250Sstevel@tonic-gate return (error); 15260Sstevel@tonic-gate } 15270Sstevel@tonic-gate 15280Sstevel@tonic-gate /* 15290Sstevel@tonic-gate * Find a cg with as close to nb contiguous bytes as possible 15300Sstevel@tonic-gate * THIS MAY TAKE MANY DISK READS! 15310Sstevel@tonic-gate * 15320Sstevel@tonic-gate * Implemented in an attempt to allocate contiguous blocks for 15330Sstevel@tonic-gate * writing the ufs log file to, minimizing future disk head seeking 15340Sstevel@tonic-gate */ 15350Sstevel@tonic-gate daddr_t 15360Sstevel@tonic-gate contigpref(ufsvfs_t *ufsvfsp, size_t nb) 15370Sstevel@tonic-gate { 15380Sstevel@tonic-gate struct fs *fs = ufsvfsp->vfs_fs; 15390Sstevel@tonic-gate daddr_t nblk = lblkno(fs, blkroundup(fs, nb)); 15400Sstevel@tonic-gate daddr_t savebno, curbno, cgbno; 15410Sstevel@tonic-gate int cg, cgblks, savecg, savenblk, curnblk; 15420Sstevel@tonic-gate uchar_t *blksfree; 15430Sstevel@tonic-gate buf_t *bp; 15440Sstevel@tonic-gate struct cg *cgp; 15450Sstevel@tonic-gate 15460Sstevel@tonic-gate savenblk = 0; 15470Sstevel@tonic-gate savecg = 0; 15480Sstevel@tonic-gate savebno = 0; 15490Sstevel@tonic-gate for (cg = 0; cg < fs->fs_ncg; ++cg) { 15500Sstevel@tonic-gate 15510Sstevel@tonic-gate /* not enough free blks for a contig check */ 15520Sstevel@tonic-gate if (fs->fs_cs(fs, cg).cs_nbfree < nblk) 15530Sstevel@tonic-gate continue; 15540Sstevel@tonic-gate 15550Sstevel@tonic-gate /* 15560Sstevel@tonic-gate * find the largest contiguous range in this cg 15570Sstevel@tonic-gate */ 15580Sstevel@tonic-gate bp = UFS_BREAD(ufsvfsp, ufsvfsp->vfs_dev, 15590Sstevel@tonic-gate (daddr_t)fsbtodb(fs, cgtod(fs, cg)), 15600Sstevel@tonic-gate (int)fs->fs_cgsize); 15610Sstevel@tonic-gate cgp = bp->b_un.b_cg; 15620Sstevel@tonic-gate if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) { 15630Sstevel@tonic-gate brelse(bp); 15640Sstevel@tonic-gate continue; 15650Sstevel@tonic-gate } 15660Sstevel@tonic-gate blksfree = cg_blksfree(cgp); /* free array */ 15670Sstevel@tonic-gate cgblks = fragstoblks(fs, fs->fs_fpg); /* blks in free array */ 15680Sstevel@tonic-gate cgbno = 0; 15690Sstevel@tonic-gate while (cgbno < cgblks && savenblk < nblk) { 15700Sstevel@tonic-gate /* find a free block */ 15710Sstevel@tonic-gate for (; cgbno < cgblks; ++cgbno) 15720Sstevel@tonic-gate if (isblock(fs, blksfree, cgbno)) 15730Sstevel@tonic-gate break; 15740Sstevel@tonic-gate curbno = cgbno; 15750Sstevel@tonic-gate /* count the number of free blocks */ 15760Sstevel@tonic-gate for (curnblk = 0; cgbno < cgblks; ++cgbno) { 15770Sstevel@tonic-gate if (!isblock(fs, blksfree, cgbno)) 15780Sstevel@tonic-gate break; 15790Sstevel@tonic-gate if (++curnblk >= nblk) 15800Sstevel@tonic-gate break; 15810Sstevel@tonic-gate } 15820Sstevel@tonic-gate if (curnblk > savenblk) { 15830Sstevel@tonic-gate savecg = cg; 15840Sstevel@tonic-gate savenblk = curnblk; 15850Sstevel@tonic-gate savebno = curbno; 15860Sstevel@tonic-gate } 15870Sstevel@tonic-gate } 15880Sstevel@tonic-gate brelse(bp); 15890Sstevel@tonic-gate if (savenblk >= nblk) 15900Sstevel@tonic-gate break; 15910Sstevel@tonic-gate } 15920Sstevel@tonic-gate 15930Sstevel@tonic-gate /* convert block offset in cg to frag offset in cg */ 15940Sstevel@tonic-gate savebno = blkstofrags(fs, savebno); 15950Sstevel@tonic-gate 15960Sstevel@tonic-gate /* convert frag offset in cg to frag offset in fs */ 15970Sstevel@tonic-gate savebno += (savecg * fs->fs_fpg); 15980Sstevel@tonic-gate 15990Sstevel@tonic-gate return (savebno); 16000Sstevel@tonic-gate } 1601