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 5*4454Smishra * Common Development and Distribution License (the "License"). 6*4454Smishra * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*4454Smishra * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 270Sstevel@tonic-gate /* All Rights Reserved */ 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 310Sstevel@tonic-gate * The Regents of the University of California 320Sstevel@tonic-gate * All Rights Reserved 330Sstevel@tonic-gate * 340Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 350Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 360Sstevel@tonic-gate * contributors. 370Sstevel@tonic-gate */ 380Sstevel@tonic-gate 390Sstevel@tonic-gate 400Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 410Sstevel@tonic-gate 42923Ssdebnath #include <sys/condvar_impl.h> 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) { 119923Ssdebnath err = ufs_fault(ITOV(ip), "alloc: bad size, dev = 0x%lx," 120923Ssdebnath " bsize = %d, size = %d, fs = %s\n", 121923Ssdebnath 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), 197923Ssdebnath "realloccg: bad size, dev=0x%lx, bsize=%d, " 198923Ssdebnath "osize=%d, nsize=%d, fs=%s\n", 199923Ssdebnath ip->i_dev, fs->fs_bsize, osize, nsize, 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), 207923Ssdebnath "realloccg: bad bprev, dev = 0x%lx, bsize = %d," 208923Ssdebnath " bprev = %ld, fs = %s\n", 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, 406923Ssdebnath "%s: free inode %d had size 0x%llx, run fsck(1M)%s", 407923Ssdebnath fs->fs_fsmnt, (int)ino, ip->i_size, 408923Ssdebnath (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 */ 584923Ssdebnath 585923Ssdebnath nextblk = bap[indx - 1]; 586923Ssdebnath /* 587923Ssdebnath * Provision for fallocate to return positive 588923Ssdebnath * blk preference based on last allocation 589923Ssdebnath */ 590923Ssdebnath if (nextblk < 0 && nextblk != UFS_HOLE) { 591923Ssdebnath nextblk = (-bap[indx - 1]) + fs->fs_frag; 592923Ssdebnath } else { 593923Ssdebnath nextblk = bap[indx - 1] + fs->fs_frag; 594923Ssdebnath } 595923Ssdebnath 596923Ssdebnath if (indx > fs->fs_maxcontig && bap[indx - fs->fs_maxcontig] + 597923Ssdebnath blkstofrags(fs, fs->fs_maxcontig) != nextblk) { 5980Sstevel@tonic-gate return (nextblk); 599923Ssdebnath } 6000Sstevel@tonic-gate if (fs->fs_rotdelay != 0) 6010Sstevel@tonic-gate /* 6020Sstevel@tonic-gate * Here we convert ms of delay to frags as: 6030Sstevel@tonic-gate * (frags) = (ms) * (rev/sec) * (sect/rev) / 604923Ssdebnath * ((sect/frag) * (ms/sec)) 6050Sstevel@tonic-gate * then round up to the next block. 6060Sstevel@tonic-gate */ 6070Sstevel@tonic-gate nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect / 6080Sstevel@tonic-gate (NSPF(fs) * 1000), fs->fs_frag); 6090Sstevel@tonic-gate return (nextblk); 6100Sstevel@tonic-gate } 6110Sstevel@tonic-gate 6120Sstevel@tonic-gate /* 6130Sstevel@tonic-gate * Free a block or fragment. 6140Sstevel@tonic-gate * 6150Sstevel@tonic-gate * The specified block or fragment is placed back in the 6160Sstevel@tonic-gate * free map. If a fragment is deallocated, a possible 6170Sstevel@tonic-gate * block reassembly is checked. 6180Sstevel@tonic-gate */ 6190Sstevel@tonic-gate void 6200Sstevel@tonic-gate free(struct inode *ip, daddr_t bno, off_t size, int flags) 6210Sstevel@tonic-gate { 6220Sstevel@tonic-gate struct fs *fs = ip->i_fs; 6230Sstevel@tonic-gate struct ufsvfs *ufsvfsp = ip->i_ufsvfs; 624512Sjkennedy struct ufs_q *delq = &ufsvfsp->vfs_delete; 625512Sjkennedy struct ufs_delq_info *delq_info = &ufsvfsp->vfs_delete_info; 6260Sstevel@tonic-gate struct cg *cgp; 6270Sstevel@tonic-gate struct buf *bp; 6280Sstevel@tonic-gate int cg, bmap, bbase; 6290Sstevel@tonic-gate int i; 6300Sstevel@tonic-gate uchar_t *blksfree; 6310Sstevel@tonic-gate int *blktot; 6320Sstevel@tonic-gate short *blks; 6330Sstevel@tonic-gate daddr_t blkno, cylno, rpos; 6340Sstevel@tonic-gate 635923Ssdebnath /* 636923Ssdebnath * fallocate'd files will have negative block address. 637923Ssdebnath * So negate it again to get original block address. 638923Ssdebnath */ 639*4454Smishra if (bno < 0 && (bno % fs->fs_frag == 0) && bno != UFS_HOLE) { 640923Ssdebnath bno = -bno; 641923Ssdebnath } 642923Ssdebnath 6430Sstevel@tonic-gate if ((unsigned long)size > fs->fs_bsize || fragoff(fs, size) != 0) { 6440Sstevel@tonic-gate (void) ufs_fault(ITOV(ip), 645923Ssdebnath "free: bad size, dev = 0x%lx, bsize = %d, size = %d, " 646923Ssdebnath "fs = %s\n", ip->i_dev, fs->fs_bsize, 647923Ssdebnath (int)size, fs->fs_fsmnt); 6480Sstevel@tonic-gate return; 6490Sstevel@tonic-gate } 6500Sstevel@tonic-gate cg = dtog(fs, bno); 6510Sstevel@tonic-gate ASSERT(!ufs_badblock(ip, bno)); 6520Sstevel@tonic-gate bp = UFS_BREAD(ufsvfsp, ip->i_dev, (daddr_t)fsbtodb(fs, cgtod(fs, cg)), 653923Ssdebnath (int)fs->fs_cgsize); 6540Sstevel@tonic-gate 6550Sstevel@tonic-gate cgp = bp->b_un.b_cg; 6560Sstevel@tonic-gate if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) { 6570Sstevel@tonic-gate brelse(bp); 6580Sstevel@tonic-gate return; 6590Sstevel@tonic-gate } 6600Sstevel@tonic-gate 6610Sstevel@tonic-gate if (!(flags & I_NOCANCEL)) 6620Sstevel@tonic-gate TRANS_CANCEL(ufsvfsp, ldbtob(fsbtodb(fs, bno)), size, flags); 6630Sstevel@tonic-gate if (flags & (I_DIR|I_IBLK|I_SHAD|I_QUOTA)) { 6640Sstevel@tonic-gate TRANS_MATA_FREE(ufsvfsp, ldbtob(fsbtodb(fs, bno)), size); 6650Sstevel@tonic-gate } 6660Sstevel@tonic-gate blksfree = cg_blksfree(cgp); 6670Sstevel@tonic-gate blktot = cg_blktot(cgp); 6680Sstevel@tonic-gate mutex_enter(&ufsvfsp->vfs_lock); 6690Sstevel@tonic-gate cgp->cg_time = gethrestime_sec(); 6700Sstevel@tonic-gate bno = dtogd(fs, bno); 6710Sstevel@tonic-gate if (size == fs->fs_bsize) { 6720Sstevel@tonic-gate blkno = fragstoblks(fs, bno); 6730Sstevel@tonic-gate cylno = cbtocylno(fs, bno); 6740Sstevel@tonic-gate rpos = cbtorpos(ufsvfsp, bno); 6750Sstevel@tonic-gate blks = cg_blks(ufsvfsp, cgp, cylno); 6760Sstevel@tonic-gate if (!isclrblock(fs, blksfree, blkno)) { 6770Sstevel@tonic-gate mutex_exit(&ufsvfsp->vfs_lock); 6780Sstevel@tonic-gate brelse(bp); 6790Sstevel@tonic-gate (void) ufs_fault(ITOV(ip), "free: freeing free block, " 6800Sstevel@tonic-gate "dev:0x%lx, block:%ld, ino:%lu, fs:%s", 6810Sstevel@tonic-gate ip->i_dev, bno, ip->i_number, fs->fs_fsmnt); 6820Sstevel@tonic-gate return; 6830Sstevel@tonic-gate } 6840Sstevel@tonic-gate setblock(fs, blksfree, blkno); 6850Sstevel@tonic-gate blks[rpos]++; 6860Sstevel@tonic-gate blktot[cylno]++; 6870Sstevel@tonic-gate cgp->cg_cs.cs_nbfree++; /* Log below */ 6880Sstevel@tonic-gate fs->fs_cstotal.cs_nbfree++; 6890Sstevel@tonic-gate fs->fs_cs(fs, cg).cs_nbfree++; 690512Sjkennedy if (TRANS_ISTRANS(ufsvfsp) && (flags & I_ACCT)) { 691512Sjkennedy mutex_enter(&delq->uq_mutex); 692512Sjkennedy delq_info->delq_unreclaimed_blocks -= 693512Sjkennedy btodb(fs->fs_bsize); 694512Sjkennedy mutex_exit(&delq->uq_mutex); 695512Sjkennedy } 6960Sstevel@tonic-gate } else { 6970Sstevel@tonic-gate bbase = bno - fragnum(fs, bno); 6980Sstevel@tonic-gate /* 6990Sstevel@tonic-gate * Decrement the counts associated with the old frags 7000Sstevel@tonic-gate */ 7010Sstevel@tonic-gate bmap = blkmap(fs, blksfree, bbase); 7020Sstevel@tonic-gate fragacct(fs, bmap, cgp->cg_frsum, -1); 7030Sstevel@tonic-gate /* 7040Sstevel@tonic-gate * Deallocate the fragment 7050Sstevel@tonic-gate */ 7060Sstevel@tonic-gate for (i = 0; i < numfrags(fs, size); i++) { 7070Sstevel@tonic-gate if (isset(blksfree, bno + i)) { 7080Sstevel@tonic-gate brelse(bp); 7090Sstevel@tonic-gate mutex_exit(&ufsvfsp->vfs_lock); 7100Sstevel@tonic-gate (void) ufs_fault(ITOV(ip), 7110Sstevel@tonic-gate "free: freeing free frag, " 7120Sstevel@tonic-gate "dev:0x%lx, blk:%ld, cg:%d, " 7130Sstevel@tonic-gate "ino:%lu, fs:%s", 7140Sstevel@tonic-gate ip->i_dev, 7150Sstevel@tonic-gate bno + i, 7160Sstevel@tonic-gate cgp->cg_cgx, 7170Sstevel@tonic-gate ip->i_number, 7180Sstevel@tonic-gate fs->fs_fsmnt); 7190Sstevel@tonic-gate return; 7200Sstevel@tonic-gate } 7210Sstevel@tonic-gate setbit(blksfree, bno + i); 7220Sstevel@tonic-gate } 7230Sstevel@tonic-gate cgp->cg_cs.cs_nffree += i; 7240Sstevel@tonic-gate fs->fs_cstotal.cs_nffree += i; 7250Sstevel@tonic-gate fs->fs_cs(fs, cg).cs_nffree += i; 726512Sjkennedy if (TRANS_ISTRANS(ufsvfsp) && (flags & I_ACCT)) { 727512Sjkennedy mutex_enter(&delq->uq_mutex); 728512Sjkennedy delq_info->delq_unreclaimed_blocks -= 729512Sjkennedy btodb(i * fs->fs_fsize); 730512Sjkennedy mutex_exit(&delq->uq_mutex); 731512Sjkennedy } 7320Sstevel@tonic-gate /* 7330Sstevel@tonic-gate * Add back in counts associated with the new frags 7340Sstevel@tonic-gate */ 7350Sstevel@tonic-gate bmap = blkmap(fs, blksfree, bbase); 7360Sstevel@tonic-gate fragacct(fs, bmap, cgp->cg_frsum, 1); 7370Sstevel@tonic-gate /* 7380Sstevel@tonic-gate * If a complete block has been reassembled, account for it 7390Sstevel@tonic-gate */ 7400Sstevel@tonic-gate blkno = fragstoblks(fs, bbase); 7410Sstevel@tonic-gate if (isblock(fs, blksfree, blkno)) { 7420Sstevel@tonic-gate cylno = cbtocylno(fs, bbase); 7430Sstevel@tonic-gate rpos = cbtorpos(ufsvfsp, bbase); 7440Sstevel@tonic-gate blks = cg_blks(ufsvfsp, cgp, cylno); 7450Sstevel@tonic-gate blks[rpos]++; 7460Sstevel@tonic-gate blktot[cylno]++; 7470Sstevel@tonic-gate cgp->cg_cs.cs_nffree -= fs->fs_frag; 7480Sstevel@tonic-gate fs->fs_cstotal.cs_nffree -= fs->fs_frag; 7490Sstevel@tonic-gate fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 7500Sstevel@tonic-gate cgp->cg_cs.cs_nbfree++; 7510Sstevel@tonic-gate fs->fs_cstotal.cs_nbfree++; 7520Sstevel@tonic-gate fs->fs_cs(fs, cg).cs_nbfree++; 7530Sstevel@tonic-gate } 7540Sstevel@tonic-gate } 7550Sstevel@tonic-gate fs->fs_fmod = 1; 7560Sstevel@tonic-gate ufs_notclean(ufsvfsp); 7570Sstevel@tonic-gate TRANS_BUF(ufsvfsp, 0, fs->fs_cgsize, bp, DT_CG); 7580Sstevel@tonic-gate TRANS_SI(ufsvfsp, fs, cg); 7590Sstevel@tonic-gate bdrwrite(bp); 7600Sstevel@tonic-gate } 7610Sstevel@tonic-gate 7620Sstevel@tonic-gate /* 7630Sstevel@tonic-gate * Free an inode. 7640Sstevel@tonic-gate * 7650Sstevel@tonic-gate * The specified inode is placed back in the free map. 7660Sstevel@tonic-gate */ 7670Sstevel@tonic-gate void 7680Sstevel@tonic-gate ufs_ifree(struct inode *ip, ino_t ino, mode_t mode) 7690Sstevel@tonic-gate { 7700Sstevel@tonic-gate struct fs *fs = ip->i_fs; 7710Sstevel@tonic-gate struct ufsvfs *ufsvfsp = ip->i_ufsvfs; 7720Sstevel@tonic-gate struct cg *cgp; 7730Sstevel@tonic-gate struct buf *bp; 7740Sstevel@tonic-gate unsigned int inot; 7750Sstevel@tonic-gate int cg; 7760Sstevel@tonic-gate char *iused; 7770Sstevel@tonic-gate 7780Sstevel@tonic-gate if (ip->i_number == ino && ip->i_mode != 0) { 7790Sstevel@tonic-gate (void) ufs_fault(ITOV(ip), 7800Sstevel@tonic-gate "ufs_ifree: illegal mode: (imode) %o, (omode) %o, ino %d, " 7810Sstevel@tonic-gate "fs = %s\n", 7820Sstevel@tonic-gate ip->i_mode, mode, (int)ip->i_number, fs->fs_fsmnt); 7830Sstevel@tonic-gate return; 7840Sstevel@tonic-gate } 7850Sstevel@tonic-gate if (ino >= fs->fs_ipg * fs->fs_ncg) { 7860Sstevel@tonic-gate (void) ufs_fault(ITOV(ip), 7870Sstevel@tonic-gate "ifree: range, dev = 0x%x, ino = %d, fs = %s\n", 7880Sstevel@tonic-gate (int)ip->i_dev, (int)ino, fs->fs_fsmnt); 7890Sstevel@tonic-gate return; 7900Sstevel@tonic-gate } 7910Sstevel@tonic-gate cg = (int)itog(fs, ino); 7920Sstevel@tonic-gate bp = UFS_BREAD(ufsvfsp, ip->i_dev, (daddr_t)fsbtodb(fs, cgtod(fs, cg)), 793923Ssdebnath (int)fs->fs_cgsize); 7940Sstevel@tonic-gate 7950Sstevel@tonic-gate cgp = bp->b_un.b_cg; 7960Sstevel@tonic-gate if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) { 7970Sstevel@tonic-gate brelse(bp); 7980Sstevel@tonic-gate return; 7990Sstevel@tonic-gate } 8000Sstevel@tonic-gate mutex_enter(&ufsvfsp->vfs_lock); 8010Sstevel@tonic-gate cgp->cg_time = gethrestime_sec(); 8020Sstevel@tonic-gate iused = cg_inosused(cgp); 8030Sstevel@tonic-gate inot = (unsigned int)(ino % (ulong_t)fs->fs_ipg); 8040Sstevel@tonic-gate if (isclr(iused, inot)) { 8050Sstevel@tonic-gate mutex_exit(&ufsvfsp->vfs_lock); 8060Sstevel@tonic-gate brelse(bp); 8070Sstevel@tonic-gate (void) ufs_fault(ITOV(ip), "ufs_ifree: freeing free inode, " 808923Ssdebnath "mode: (imode) %o, (omode) %o, ino:%d, " 809923Ssdebnath "fs:%s", 810923Ssdebnath ip->i_mode, mode, (int)ino, fs->fs_fsmnt); 8110Sstevel@tonic-gate return; 8120Sstevel@tonic-gate } 8130Sstevel@tonic-gate clrbit(iused, inot); 8140Sstevel@tonic-gate 8150Sstevel@tonic-gate if (inot < (ulong_t)cgp->cg_irotor) 8160Sstevel@tonic-gate cgp->cg_irotor = inot; 8170Sstevel@tonic-gate cgp->cg_cs.cs_nifree++; 8180Sstevel@tonic-gate fs->fs_cstotal.cs_nifree++; 8190Sstevel@tonic-gate fs->fs_cs(fs, cg).cs_nifree++; 8200Sstevel@tonic-gate if (((mode & IFMT) == IFDIR) || ((mode & IFMT) == IFATTRDIR)) { 8210Sstevel@tonic-gate cgp->cg_cs.cs_ndir--; 8220Sstevel@tonic-gate fs->fs_cstotal.cs_ndir--; 8230Sstevel@tonic-gate fs->fs_cs(fs, cg).cs_ndir--; 8240Sstevel@tonic-gate } 8250Sstevel@tonic-gate fs->fs_fmod = 1; 8260Sstevel@tonic-gate ufs_notclean(ufsvfsp); 8270Sstevel@tonic-gate TRANS_BUF(ufsvfsp, 0, fs->fs_cgsize, bp, DT_CG); 8280Sstevel@tonic-gate TRANS_SI(ufsvfsp, fs, cg); 8290Sstevel@tonic-gate bdrwrite(bp); 8300Sstevel@tonic-gate } 8310Sstevel@tonic-gate 8320Sstevel@tonic-gate /* 8330Sstevel@tonic-gate * Implement the cylinder overflow algorithm. 8340Sstevel@tonic-gate * 8350Sstevel@tonic-gate * The policy implemented by this algorithm is: 8360Sstevel@tonic-gate * 1) allocate the block in its requested cylinder group. 8370Sstevel@tonic-gate * 2) quadratically rehash on the cylinder group number. 8380Sstevel@tonic-gate * 3) brute force search for a free block. 8390Sstevel@tonic-gate * The size parameter means size for data blocks, mode for inodes. 8400Sstevel@tonic-gate */ 8410Sstevel@tonic-gate static ino_t 8420Sstevel@tonic-gate hashalloc(struct inode *ip, int cg, long pref, int size, ulong_t (*allocator)()) 8430Sstevel@tonic-gate { 8440Sstevel@tonic-gate struct fs *fs; 8450Sstevel@tonic-gate int i; 8460Sstevel@tonic-gate long result; 8470Sstevel@tonic-gate int icg = cg; 8480Sstevel@tonic-gate 8490Sstevel@tonic-gate fs = ip->i_fs; 8500Sstevel@tonic-gate /* 8510Sstevel@tonic-gate * 1: preferred cylinder group 8520Sstevel@tonic-gate */ 8530Sstevel@tonic-gate result = (*allocator)(ip, cg, pref, size); 8540Sstevel@tonic-gate if (result) 8550Sstevel@tonic-gate return (result); 8560Sstevel@tonic-gate /* 8570Sstevel@tonic-gate * 2: quadratic rehash 8580Sstevel@tonic-gate */ 8590Sstevel@tonic-gate for (i = 1; i < fs->fs_ncg; i *= 2) { 8600Sstevel@tonic-gate cg += i; 8610Sstevel@tonic-gate if (cg >= fs->fs_ncg) 8620Sstevel@tonic-gate cg -= fs->fs_ncg; 8630Sstevel@tonic-gate result = (*allocator)(ip, cg, 0, size); 8640Sstevel@tonic-gate if (result) 8650Sstevel@tonic-gate return (result); 8660Sstevel@tonic-gate } 8670Sstevel@tonic-gate /* 8680Sstevel@tonic-gate * 3: brute force search 8690Sstevel@tonic-gate * Note that we start at i == 2, since 0 was checked initially, 8700Sstevel@tonic-gate * and 1 is always checked in the quadratic rehash. 8710Sstevel@tonic-gate */ 8720Sstevel@tonic-gate cg = (icg + 2) % fs->fs_ncg; 8730Sstevel@tonic-gate for (i = 2; i < fs->fs_ncg; i++) { 8740Sstevel@tonic-gate result = (*allocator)(ip, cg, 0, size); 8750Sstevel@tonic-gate if (result) 8760Sstevel@tonic-gate return (result); 8770Sstevel@tonic-gate cg++; 8780Sstevel@tonic-gate if (cg == fs->fs_ncg) 8790Sstevel@tonic-gate cg = 0; 8800Sstevel@tonic-gate } 8810Sstevel@tonic-gate return (NULL); 8820Sstevel@tonic-gate } 8830Sstevel@tonic-gate 8840Sstevel@tonic-gate /* 8850Sstevel@tonic-gate * Determine whether a fragment can be extended. 8860Sstevel@tonic-gate * 8870Sstevel@tonic-gate * Check to see if the necessary fragments are available, and 8880Sstevel@tonic-gate * if they are, allocate them. 8890Sstevel@tonic-gate */ 8900Sstevel@tonic-gate static daddr_t 8910Sstevel@tonic-gate fragextend(struct inode *ip, int cg, long bprev, int osize, int nsize) 8920Sstevel@tonic-gate { 8930Sstevel@tonic-gate struct ufsvfs *ufsvfsp = ip->i_ufsvfs; 8940Sstevel@tonic-gate struct fs *fs = ip->i_fs; 8950Sstevel@tonic-gate struct buf *bp; 8960Sstevel@tonic-gate struct cg *cgp; 8970Sstevel@tonic-gate uchar_t *blksfree; 8980Sstevel@tonic-gate long bno; 8990Sstevel@tonic-gate int frags, bbase; 9000Sstevel@tonic-gate int i, j; 9010Sstevel@tonic-gate 9020Sstevel@tonic-gate if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize)) 9030Sstevel@tonic-gate return (NULL); 9040Sstevel@tonic-gate frags = numfrags(fs, nsize); 9050Sstevel@tonic-gate bbase = (int)fragnum(fs, bprev); 9060Sstevel@tonic-gate if (bbase > fragnum(fs, (bprev + frags - 1))) { 9070Sstevel@tonic-gate /* cannot extend across a block boundary */ 9080Sstevel@tonic-gate return (NULL); 9090Sstevel@tonic-gate } 9100Sstevel@tonic-gate 9110Sstevel@tonic-gate bp = UFS_BREAD(ufsvfsp, ip->i_dev, (daddr_t)fsbtodb(fs, cgtod(fs, cg)), 912923Ssdebnath (int)fs->fs_cgsize); 9130Sstevel@tonic-gate cgp = bp->b_un.b_cg; 9140Sstevel@tonic-gate if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) { 9150Sstevel@tonic-gate brelse(bp); 9160Sstevel@tonic-gate return (NULL); 9170Sstevel@tonic-gate } 9180Sstevel@tonic-gate 9190Sstevel@tonic-gate blksfree = cg_blksfree(cgp); 9200Sstevel@tonic-gate mutex_enter(&ufsvfsp->vfs_lock); 9210Sstevel@tonic-gate bno = dtogd(fs, bprev); 9220Sstevel@tonic-gate for (i = numfrags(fs, osize); i < frags; i++) { 9230Sstevel@tonic-gate if (isclr(blksfree, bno + i)) { 9240Sstevel@tonic-gate mutex_exit(&ufsvfsp->vfs_lock); 9250Sstevel@tonic-gate brelse(bp); 9260Sstevel@tonic-gate return (NULL); 9270Sstevel@tonic-gate } 9280Sstevel@tonic-gate if ((TRANS_ISCANCEL(ufsvfsp, ldbtob(fsbtodb(fs, bprev + i)), 9290Sstevel@tonic-gate fs->fs_fsize))) { 9300Sstevel@tonic-gate mutex_exit(&ufsvfsp->vfs_lock); 9310Sstevel@tonic-gate brelse(bp); 9320Sstevel@tonic-gate return (NULL); 9330Sstevel@tonic-gate } 9340Sstevel@tonic-gate } 9350Sstevel@tonic-gate 9360Sstevel@tonic-gate cgp->cg_time = gethrestime_sec(); 9370Sstevel@tonic-gate /* 9380Sstevel@tonic-gate * The current fragment can be extended, 9390Sstevel@tonic-gate * deduct the count on fragment being extended into 9400Sstevel@tonic-gate * increase the count on the remaining fragment (if any) 9410Sstevel@tonic-gate * allocate the extended piece. 9420Sstevel@tonic-gate */ 9430Sstevel@tonic-gate for (i = frags; i < fs->fs_frag - bbase; i++) 9440Sstevel@tonic-gate if (isclr(blksfree, bno + i)) 9450Sstevel@tonic-gate break; 9460Sstevel@tonic-gate j = i - numfrags(fs, osize); 9470Sstevel@tonic-gate cgp->cg_frsum[j]--; 9480Sstevel@tonic-gate ASSERT(cgp->cg_frsum[j] >= 0); 9490Sstevel@tonic-gate if (i != frags) 9500Sstevel@tonic-gate cgp->cg_frsum[i - frags]++; 9510Sstevel@tonic-gate for (i = numfrags(fs, osize); i < frags; i++) { 9520Sstevel@tonic-gate clrbit(blksfree, bno + i); 9530Sstevel@tonic-gate cgp->cg_cs.cs_nffree--; 9540Sstevel@tonic-gate fs->fs_cs(fs, cg).cs_nffree--; 9550Sstevel@tonic-gate fs->fs_cstotal.cs_nffree--; 9560Sstevel@tonic-gate } 9570Sstevel@tonic-gate fs->fs_fmod = 1; 9580Sstevel@tonic-gate ufs_notclean(ufsvfsp); 9590Sstevel@tonic-gate TRANS_BUF(ufsvfsp, 0, fs->fs_cgsize, bp, DT_CG); 9600Sstevel@tonic-gate TRANS_SI(ufsvfsp, fs, cg); 9610Sstevel@tonic-gate bdrwrite(bp); 9620Sstevel@tonic-gate return ((daddr_t)bprev); 9630Sstevel@tonic-gate } 9640Sstevel@tonic-gate 9650Sstevel@tonic-gate /* 9660Sstevel@tonic-gate * Determine whether a block can be allocated. 9670Sstevel@tonic-gate * 9680Sstevel@tonic-gate * Check to see if a block of the apprpriate size 9690Sstevel@tonic-gate * is available, and if it is, allocate it. 9700Sstevel@tonic-gate */ 9710Sstevel@tonic-gate static daddr_t 9720Sstevel@tonic-gate alloccg(struct inode *ip, int cg, daddr_t bpref, int size) 9730Sstevel@tonic-gate { 9740Sstevel@tonic-gate struct ufsvfs *ufsvfsp = ip->i_ufsvfs; 9750Sstevel@tonic-gate struct fs *fs = ip->i_fs; 9760Sstevel@tonic-gate struct buf *bp; 9770Sstevel@tonic-gate struct cg *cgp; 9780Sstevel@tonic-gate uchar_t *blksfree; 9790Sstevel@tonic-gate int bno, frags; 9800Sstevel@tonic-gate int allocsiz; 9810Sstevel@tonic-gate int i; 9820Sstevel@tonic-gate 9830Sstevel@tonic-gate if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) 9840Sstevel@tonic-gate return (0); 9850Sstevel@tonic-gate bp = UFS_BREAD(ufsvfsp, ip->i_dev, (daddr_t)fsbtodb(fs, cgtod(fs, cg)), 986923Ssdebnath (int)fs->fs_cgsize); 9870Sstevel@tonic-gate 9880Sstevel@tonic-gate cgp = bp->b_un.b_cg; 9890Sstevel@tonic-gate if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp) || 9900Sstevel@tonic-gate (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) { 9910Sstevel@tonic-gate brelse(bp); 9920Sstevel@tonic-gate return (0); 9930Sstevel@tonic-gate } 9940Sstevel@tonic-gate blksfree = cg_blksfree(cgp); 9950Sstevel@tonic-gate mutex_enter(&ufsvfsp->vfs_lock); 9960Sstevel@tonic-gate cgp->cg_time = gethrestime_sec(); 9970Sstevel@tonic-gate if (size == fs->fs_bsize) { 9980Sstevel@tonic-gate if ((bno = alloccgblk(ufsvfsp, cgp, bpref, bp)) == 0) 9990Sstevel@tonic-gate goto errout; 10000Sstevel@tonic-gate fs->fs_fmod = 1; 10010Sstevel@tonic-gate ufs_notclean(ufsvfsp); 10020Sstevel@tonic-gate TRANS_SI(ufsvfsp, fs, cg); 10030Sstevel@tonic-gate bdrwrite(bp); 10040Sstevel@tonic-gate return (bno); 10050Sstevel@tonic-gate } 10060Sstevel@tonic-gate /* 10070Sstevel@tonic-gate * Check to see if any fragments are already available 10080Sstevel@tonic-gate * allocsiz is the size which will be allocated, hacking 10090Sstevel@tonic-gate * it down to a smaller size if necessary. 10100Sstevel@tonic-gate */ 10110Sstevel@tonic-gate frags = numfrags(fs, size); 10120Sstevel@tonic-gate for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) 10130Sstevel@tonic-gate if (cgp->cg_frsum[allocsiz] != 0) 10140Sstevel@tonic-gate break; 10150Sstevel@tonic-gate 10160Sstevel@tonic-gate if (allocsiz != fs->fs_frag) 10170Sstevel@tonic-gate bno = mapsearch(ufsvfsp, cgp, bpref, allocsiz); 10180Sstevel@tonic-gate 10190Sstevel@tonic-gate if (allocsiz == fs->fs_frag || bno < 0) { 10200Sstevel@tonic-gate /* 10210Sstevel@tonic-gate * No fragments were available, so a block 10220Sstevel@tonic-gate * will be allocated and hacked up. 10230Sstevel@tonic-gate */ 10240Sstevel@tonic-gate if (cgp->cg_cs.cs_nbfree == 0) 10250Sstevel@tonic-gate goto errout; 10260Sstevel@tonic-gate if ((bno = alloccgblk(ufsvfsp, cgp, bpref, bp)) == 0) 10270Sstevel@tonic-gate goto errout; 10280Sstevel@tonic-gate bpref = dtogd(fs, bno); 10290Sstevel@tonic-gate for (i = frags; i < fs->fs_frag; i++) 10300Sstevel@tonic-gate setbit(blksfree, bpref + i); 10310Sstevel@tonic-gate i = fs->fs_frag - frags; 10320Sstevel@tonic-gate cgp->cg_cs.cs_nffree += i; 10330Sstevel@tonic-gate fs->fs_cstotal.cs_nffree += i; 10340Sstevel@tonic-gate fs->fs_cs(fs, cg).cs_nffree += i; 10350Sstevel@tonic-gate cgp->cg_frsum[i]++; 10360Sstevel@tonic-gate fs->fs_fmod = 1; 10370Sstevel@tonic-gate ufs_notclean(ufsvfsp); 10380Sstevel@tonic-gate TRANS_SI(ufsvfsp, fs, cg); 10390Sstevel@tonic-gate bdrwrite(bp); 10400Sstevel@tonic-gate return (bno); 10410Sstevel@tonic-gate } 10420Sstevel@tonic-gate 10430Sstevel@tonic-gate for (i = 0; i < frags; i++) 10440Sstevel@tonic-gate clrbit(blksfree, bno + i); 10450Sstevel@tonic-gate cgp->cg_cs.cs_nffree -= frags; 10460Sstevel@tonic-gate fs->fs_cstotal.cs_nffree -= frags; 10470Sstevel@tonic-gate fs->fs_cs(fs, cg).cs_nffree -= frags; 10480Sstevel@tonic-gate cgp->cg_frsum[allocsiz]--; 10490Sstevel@tonic-gate ASSERT(cgp->cg_frsum[allocsiz] >= 0); 10500Sstevel@tonic-gate if (frags != allocsiz) { 10510Sstevel@tonic-gate cgp->cg_frsum[allocsiz - frags]++; 10520Sstevel@tonic-gate } 10530Sstevel@tonic-gate fs->fs_fmod = 1; 10540Sstevel@tonic-gate ufs_notclean(ufsvfsp); 10550Sstevel@tonic-gate TRANS_BUF(ufsvfsp, 0, fs->fs_cgsize, bp, DT_CG); 10560Sstevel@tonic-gate TRANS_SI(ufsvfsp, fs, cg); 10570Sstevel@tonic-gate bdrwrite(bp); 10580Sstevel@tonic-gate return (cg * fs->fs_fpg + bno); 10590Sstevel@tonic-gate errout: 10600Sstevel@tonic-gate mutex_exit(&ufsvfsp->vfs_lock); 10610Sstevel@tonic-gate brelse(bp); 10620Sstevel@tonic-gate return (0); 10630Sstevel@tonic-gate } 10640Sstevel@tonic-gate 10650Sstevel@tonic-gate /* 10660Sstevel@tonic-gate * Allocate a block in a cylinder group. 10670Sstevel@tonic-gate * 10680Sstevel@tonic-gate * This algorithm implements the following policy: 10690Sstevel@tonic-gate * 1) allocate the requested block. 10700Sstevel@tonic-gate * 2) allocate a rotationally optimal block in the same cylinder. 10710Sstevel@tonic-gate * 3) allocate the next available block on the block rotor for the 10720Sstevel@tonic-gate * specified cylinder group. 10730Sstevel@tonic-gate * Note that this routine only allocates fs_bsize blocks; these 10740Sstevel@tonic-gate * blocks may be fragmented by the routine that allocates them. 10750Sstevel@tonic-gate */ 10760Sstevel@tonic-gate static daddr_t 10770Sstevel@tonic-gate alloccgblk( 10780Sstevel@tonic-gate struct ufsvfs *ufsvfsp, 10790Sstevel@tonic-gate struct cg *cgp, 10800Sstevel@tonic-gate daddr_t bpref, 10810Sstevel@tonic-gate struct buf *bp) 10820Sstevel@tonic-gate { 10830Sstevel@tonic-gate daddr_t bno; 10840Sstevel@tonic-gate int cylno, pos, delta, rotbl_size; 10850Sstevel@tonic-gate short *cylbp; 10860Sstevel@tonic-gate int i; 10870Sstevel@tonic-gate struct fs *fs; 10880Sstevel@tonic-gate uchar_t *blksfree; 10890Sstevel@tonic-gate daddr_t blkno, rpos, frag; 10900Sstevel@tonic-gate short *blks; 10910Sstevel@tonic-gate int32_t *blktot; 10920Sstevel@tonic-gate 10930Sstevel@tonic-gate ASSERT(MUTEX_HELD(&ufsvfsp->vfs_lock)); 10940Sstevel@tonic-gate fs = ufsvfsp->vfs_fs; 10950Sstevel@tonic-gate blksfree = cg_blksfree(cgp); 10960Sstevel@tonic-gate if (bpref == 0) { 10970Sstevel@tonic-gate bpref = cgp->cg_rotor; 10980Sstevel@tonic-gate goto norot; 10990Sstevel@tonic-gate } 11000Sstevel@tonic-gate bpref = blknum(fs, bpref); 11010Sstevel@tonic-gate bpref = dtogd(fs, bpref); 11020Sstevel@tonic-gate /* 11030Sstevel@tonic-gate * If the requested block is available, use it. 11040Sstevel@tonic-gate */ 11050Sstevel@tonic-gate if (isblock(fs, blksfree, (daddr_t)fragstoblks(fs, bpref))) { 11060Sstevel@tonic-gate bno = bpref; 11070Sstevel@tonic-gate goto gotit; 11080Sstevel@tonic-gate } 11090Sstevel@tonic-gate /* 11100Sstevel@tonic-gate * Check for a block available on the same cylinder. 11110Sstevel@tonic-gate */ 11120Sstevel@tonic-gate cylno = cbtocylno(fs, bpref); 11130Sstevel@tonic-gate if (cg_blktot(cgp)[cylno] == 0) 11140Sstevel@tonic-gate goto norot; 11150Sstevel@tonic-gate if (fs->fs_cpc == 0) { 11160Sstevel@tonic-gate /* 11170Sstevel@tonic-gate * Block layout info is not available, so just 11180Sstevel@tonic-gate * have to take any block in this cylinder. 11190Sstevel@tonic-gate */ 11200Sstevel@tonic-gate bpref = howmany(fs->fs_spc * cylno, NSPF(fs)); 11210Sstevel@tonic-gate goto norot; 11220Sstevel@tonic-gate } 11230Sstevel@tonic-gate /* 11240Sstevel@tonic-gate * Check the summary information to see if a block is 11250Sstevel@tonic-gate * available in the requested cylinder starting at the 11260Sstevel@tonic-gate * requested rotational position and proceeding around. 11270Sstevel@tonic-gate */ 11280Sstevel@tonic-gate cylbp = cg_blks(ufsvfsp, cgp, cylno); 11290Sstevel@tonic-gate pos = cbtorpos(ufsvfsp, bpref); 11300Sstevel@tonic-gate for (i = pos; i < ufsvfsp->vfs_nrpos; i++) 11310Sstevel@tonic-gate if (cylbp[i] > 0) 11320Sstevel@tonic-gate break; 11330Sstevel@tonic-gate if (i == ufsvfsp->vfs_nrpos) 11340Sstevel@tonic-gate for (i = 0; i < pos; i++) 11350Sstevel@tonic-gate if (cylbp[i] > 0) 11360Sstevel@tonic-gate break; 11370Sstevel@tonic-gate if (cylbp[i] > 0) { 11380Sstevel@tonic-gate /* 11390Sstevel@tonic-gate * Found a rotational position, now find the actual 11400Sstevel@tonic-gate * block. A "panic" if none is actually there. 11410Sstevel@tonic-gate */ 11420Sstevel@tonic-gate 11430Sstevel@tonic-gate /* 11440Sstevel@tonic-gate * Up to this point, "pos" has referred to the rotational 11450Sstevel@tonic-gate * position of the desired block. From now on, it holds 11460Sstevel@tonic-gate * the offset of the current cylinder within a cylinder 11470Sstevel@tonic-gate * cycle. (A cylinder cycle refers to a set of cylinders 11480Sstevel@tonic-gate * which are described by a single rotational table; the 11490Sstevel@tonic-gate * size of the cycle is fs_cpc.) 11500Sstevel@tonic-gate * 11510Sstevel@tonic-gate * bno is set to the block number of the first block within 11520Sstevel@tonic-gate * the current cylinder cycle. 11530Sstevel@tonic-gate */ 11540Sstevel@tonic-gate 11550Sstevel@tonic-gate pos = cylno % fs->fs_cpc; 11560Sstevel@tonic-gate bno = (cylno - pos) * fs->fs_spc / NSPB(fs); 11570Sstevel@tonic-gate 11580Sstevel@tonic-gate /* 11590Sstevel@tonic-gate * The blocks within a cylinder are grouped into equivalence 11600Sstevel@tonic-gate * classes according to their "rotational position." There 11610Sstevel@tonic-gate * are two tables used to determine these classes. 11620Sstevel@tonic-gate * 11630Sstevel@tonic-gate * The positional offset table (fs_postbl) has an entry for 11640Sstevel@tonic-gate * each rotational position of each cylinder in a cylinder 11650Sstevel@tonic-gate * cycle. This entry contains the relative block number 11660Sstevel@tonic-gate * (counting from the start of the cylinder cycle) of the 11670Sstevel@tonic-gate * first block in the equivalence class for that position 11680Sstevel@tonic-gate * and that cylinder. Positions for which no blocks exist 11690Sstevel@tonic-gate * are indicated by a -1. 11700Sstevel@tonic-gate * 11710Sstevel@tonic-gate * The rotational delta table (fs_rotbl) has an entry for 11720Sstevel@tonic-gate * each block in a cylinder cycle. This entry contains 11730Sstevel@tonic-gate * the offset from that block to the next block in the 11740Sstevel@tonic-gate * same equivalence class. The last block in the class 11750Sstevel@tonic-gate * is indicated by a zero in the table. 11760Sstevel@tonic-gate * 11770Sstevel@tonic-gate * The following code, then, walks through all of the blocks 11780Sstevel@tonic-gate * in the cylinder (cylno) which we're allocating within 11790Sstevel@tonic-gate * which are in the equivalence class for the rotational 11800Sstevel@tonic-gate * position (i) which we're allocating within. 11810Sstevel@tonic-gate */ 11820Sstevel@tonic-gate 11830Sstevel@tonic-gate if (fs_postbl(ufsvfsp, pos)[i] == -1) { 11840Sstevel@tonic-gate (void) ufs_fault(ufsvfsp->vfs_root, 1185923Ssdebnath "alloccgblk: cyl groups corrupted, pos = %d, " 1186923Ssdebnath "i = %d, fs = %s\n", pos, i, fs->fs_fsmnt); 11870Sstevel@tonic-gate return (0); 11880Sstevel@tonic-gate } 11890Sstevel@tonic-gate 11900Sstevel@tonic-gate /* 11910Sstevel@tonic-gate * There is one entry in the rotational table for each block 11920Sstevel@tonic-gate * in the cylinder cycle. These are whole blocks, not frags. 11930Sstevel@tonic-gate */ 11940Sstevel@tonic-gate 11950Sstevel@tonic-gate rotbl_size = (fs->fs_cpc * fs->fs_spc) >> 11960Sstevel@tonic-gate (fs->fs_fragshift + fs->fs_fsbtodb); 11970Sstevel@tonic-gate 11980Sstevel@tonic-gate /* 11990Sstevel@tonic-gate * As we start, "i" is the rotational position within which 12000Sstevel@tonic-gate * we're searching. After the next line, it will be a block 12010Sstevel@tonic-gate * number (relative to the start of the cylinder cycle) 12020Sstevel@tonic-gate * within the equivalence class of that rotational position. 12030Sstevel@tonic-gate */ 12040Sstevel@tonic-gate 12050Sstevel@tonic-gate i = fs_postbl(ufsvfsp, pos)[i]; 12060Sstevel@tonic-gate 12070Sstevel@tonic-gate for (;;) { 12080Sstevel@tonic-gate if (isblock(fs, blksfree, (daddr_t)(bno + i))) { 12090Sstevel@tonic-gate bno = blkstofrags(fs, (bno + i)); 12100Sstevel@tonic-gate goto gotit; 12110Sstevel@tonic-gate } 12120Sstevel@tonic-gate delta = fs_rotbl(fs)[i]; 12130Sstevel@tonic-gate if (delta <= 0 || /* End of chain, or */ 12140Sstevel@tonic-gate delta + i > rotbl_size) /* end of table? */ 12150Sstevel@tonic-gate break; /* If so, panic. */ 12160Sstevel@tonic-gate i += delta; 12170Sstevel@tonic-gate } 12180Sstevel@tonic-gate (void) ufs_fault(ufsvfsp->vfs_root, 1219923Ssdebnath "alloccgblk: can't find blk in cyl, pos:%d, i:%d, " 1220923Ssdebnath "fs:%s bno: %x\n", pos, i, fs->fs_fsmnt, (int)bno); 12210Sstevel@tonic-gate return (0); 12220Sstevel@tonic-gate } 12230Sstevel@tonic-gate norot: 12240Sstevel@tonic-gate /* 12250Sstevel@tonic-gate * No blocks in the requested cylinder, so take 12260Sstevel@tonic-gate * next available one in this cylinder group. 12270Sstevel@tonic-gate */ 12280Sstevel@tonic-gate bno = mapsearch(ufsvfsp, cgp, bpref, (int)fs->fs_frag); 12290Sstevel@tonic-gate if (bno < 0) 12300Sstevel@tonic-gate return (0); 12310Sstevel@tonic-gate cgp->cg_rotor = bno; 12320Sstevel@tonic-gate gotit: 12330Sstevel@tonic-gate blkno = fragstoblks(fs, bno); 12340Sstevel@tonic-gate frag = (cgp->cg_cgx * fs->fs_fpg) + bno; 12350Sstevel@tonic-gate if (TRANS_ISCANCEL(ufsvfsp, ldbtob(fsbtodb(fs, frag)), fs->fs_bsize)) 12360Sstevel@tonic-gate goto norot; 12370Sstevel@tonic-gate clrblock(fs, blksfree, (long)blkno); 12380Sstevel@tonic-gate /* 12390Sstevel@tonic-gate * the other cg/sb/si fields are TRANS'ed by the caller 12400Sstevel@tonic-gate */ 12410Sstevel@tonic-gate cgp->cg_cs.cs_nbfree--; 12420Sstevel@tonic-gate fs->fs_cstotal.cs_nbfree--; 12430Sstevel@tonic-gate fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 12440Sstevel@tonic-gate cylno = cbtocylno(fs, bno); 12450Sstevel@tonic-gate blks = cg_blks(ufsvfsp, cgp, cylno); 12460Sstevel@tonic-gate rpos = cbtorpos(ufsvfsp, bno); 12470Sstevel@tonic-gate blktot = cg_blktot(cgp); 12480Sstevel@tonic-gate blks[rpos]--; 12490Sstevel@tonic-gate blktot[cylno]--; 12500Sstevel@tonic-gate TRANS_BUF(ufsvfsp, 0, fs->fs_cgsize, bp, DT_CG); 12510Sstevel@tonic-gate fs->fs_fmod = 1; 12520Sstevel@tonic-gate return (frag); 12530Sstevel@tonic-gate } 12540Sstevel@tonic-gate 12550Sstevel@tonic-gate /* 12560Sstevel@tonic-gate * Determine whether an inode can be allocated. 12570Sstevel@tonic-gate * 12580Sstevel@tonic-gate * Check to see if an inode is available, and if it is, 12590Sstevel@tonic-gate * allocate it using the following policy: 12600Sstevel@tonic-gate * 1) allocate the requested inode. 12610Sstevel@tonic-gate * 2) allocate the next available inode after the requested 12620Sstevel@tonic-gate * inode in the specified cylinder group. 12630Sstevel@tonic-gate */ 12640Sstevel@tonic-gate static ino_t 12650Sstevel@tonic-gate ialloccg(struct inode *ip, int cg, daddr_t ipref, int mode) 12660Sstevel@tonic-gate { 12670Sstevel@tonic-gate struct ufsvfs *ufsvfsp = ip->i_ufsvfs; 12680Sstevel@tonic-gate struct fs *fs = ip->i_fs; 12690Sstevel@tonic-gate struct cg *cgp; 12700Sstevel@tonic-gate struct buf *bp; 12710Sstevel@tonic-gate int start, len, loc, map, i; 12720Sstevel@tonic-gate char *iused; 12730Sstevel@tonic-gate 12740Sstevel@tonic-gate if (fs->fs_cs(fs, cg).cs_nifree == 0) 12750Sstevel@tonic-gate return (0); 12760Sstevel@tonic-gate bp = UFS_BREAD(ufsvfsp, ip->i_dev, (daddr_t)fsbtodb(fs, cgtod(fs, cg)), 12770Sstevel@tonic-gate (int)fs->fs_cgsize); 12780Sstevel@tonic-gate 12790Sstevel@tonic-gate cgp = bp->b_un.b_cg; 12800Sstevel@tonic-gate if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp) || 12810Sstevel@tonic-gate cgp->cg_cs.cs_nifree == 0) { 12820Sstevel@tonic-gate brelse(bp); 12830Sstevel@tonic-gate return (0); 12840Sstevel@tonic-gate } 12850Sstevel@tonic-gate iused = cg_inosused(cgp); 12860Sstevel@tonic-gate mutex_enter(&ufsvfsp->vfs_lock); 12870Sstevel@tonic-gate /* 12880Sstevel@tonic-gate * While we are waiting for the mutex, someone may have taken 12890Sstevel@tonic-gate * the last available inode. Need to recheck. 12900Sstevel@tonic-gate */ 12910Sstevel@tonic-gate if (cgp->cg_cs.cs_nifree == 0) { 12920Sstevel@tonic-gate mutex_exit(&ufsvfsp->vfs_lock); 12930Sstevel@tonic-gate brelse(bp); 12940Sstevel@tonic-gate return (0); 12950Sstevel@tonic-gate } 12960Sstevel@tonic-gate 12970Sstevel@tonic-gate cgp->cg_time = gethrestime_sec(); 12980Sstevel@tonic-gate if (ipref) { 12990Sstevel@tonic-gate ipref %= fs->fs_ipg; 13000Sstevel@tonic-gate if (isclr(iused, ipref)) 13010Sstevel@tonic-gate goto gotit; 13020Sstevel@tonic-gate } 13030Sstevel@tonic-gate start = cgp->cg_irotor / NBBY; 13040Sstevel@tonic-gate len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY); 13050Sstevel@tonic-gate loc = skpc(0xff, (uint_t)len, &iused[start]); 13060Sstevel@tonic-gate if (loc == 0) { 13070Sstevel@tonic-gate len = start + 1; 13080Sstevel@tonic-gate start = 0; 13090Sstevel@tonic-gate loc = skpc(0xff, (uint_t)len, &iused[0]); 13100Sstevel@tonic-gate if (loc == 0) { 13110Sstevel@tonic-gate mutex_exit(&ufsvfsp->vfs_lock); 13120Sstevel@tonic-gate (void) ufs_fault(ITOV(ip), 1313923Ssdebnath "ialloccg: map corrupted, cg = %d, irotor = %d, " 1314923Ssdebnath "fs = %s\n", cg, (int)cgp->cg_irotor, fs->fs_fsmnt); 13150Sstevel@tonic-gate return (0); 13160Sstevel@tonic-gate } 13170Sstevel@tonic-gate } 13180Sstevel@tonic-gate i = start + len - loc; 13190Sstevel@tonic-gate map = iused[i]; 13200Sstevel@tonic-gate ipref = i * NBBY; 13210Sstevel@tonic-gate for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) { 13220Sstevel@tonic-gate if ((map & i) == 0) { 13230Sstevel@tonic-gate cgp->cg_irotor = ipref; 13240Sstevel@tonic-gate goto gotit; 13250Sstevel@tonic-gate } 13260Sstevel@tonic-gate } 13270Sstevel@tonic-gate 13280Sstevel@tonic-gate mutex_exit(&ufsvfsp->vfs_lock); 13290Sstevel@tonic-gate (void) ufs_fault(ITOV(ip), "ialloccg: block not in mapfs = %s", 13300Sstevel@tonic-gate fs->fs_fsmnt); 13310Sstevel@tonic-gate return (0); 13320Sstevel@tonic-gate gotit: 13330Sstevel@tonic-gate setbit(iused, ipref); 13340Sstevel@tonic-gate cgp->cg_cs.cs_nifree--; 13350Sstevel@tonic-gate fs->fs_cstotal.cs_nifree--; 13360Sstevel@tonic-gate fs->fs_cs(fs, cg).cs_nifree--; 13370Sstevel@tonic-gate if (((mode & IFMT) == IFDIR) || ((mode & IFMT) == IFATTRDIR)) { 13380Sstevel@tonic-gate cgp->cg_cs.cs_ndir++; 13390Sstevel@tonic-gate fs->fs_cstotal.cs_ndir++; 13400Sstevel@tonic-gate fs->fs_cs(fs, cg).cs_ndir++; 13410Sstevel@tonic-gate } 13420Sstevel@tonic-gate fs->fs_fmod = 1; 13430Sstevel@tonic-gate ufs_notclean(ufsvfsp); 13440Sstevel@tonic-gate TRANS_BUF(ufsvfsp, 0, fs->fs_cgsize, bp, DT_CG); 13450Sstevel@tonic-gate TRANS_SI(ufsvfsp, fs, cg); 13460Sstevel@tonic-gate bdrwrite(bp); 13470Sstevel@tonic-gate return (cg * fs->fs_ipg + ipref); 13480Sstevel@tonic-gate } 13490Sstevel@tonic-gate 13500Sstevel@tonic-gate /* 13510Sstevel@tonic-gate * Find a block of the specified size in the specified cylinder group. 13520Sstevel@tonic-gate * 13530Sstevel@tonic-gate * It is a panic if a request is made to find a block if none are 13540Sstevel@tonic-gate * available. 13550Sstevel@tonic-gate */ 13560Sstevel@tonic-gate static daddr_t 13570Sstevel@tonic-gate mapsearch(struct ufsvfs *ufsvfsp, struct cg *cgp, daddr_t bpref, 13580Sstevel@tonic-gate int allocsiz) 13590Sstevel@tonic-gate { 13600Sstevel@tonic-gate struct fs *fs = ufsvfsp->vfs_fs; 13610Sstevel@tonic-gate daddr_t bno, cfrag; 13620Sstevel@tonic-gate int start, len, loc, i, last, first, secondtime; 13630Sstevel@tonic-gate int blk, field, subfield, pos; 13640Sstevel@tonic-gate int gotit; 13650Sstevel@tonic-gate 13660Sstevel@tonic-gate /* 13670Sstevel@tonic-gate * ufsvfs->vfs_lock is held when calling this. 13680Sstevel@tonic-gate */ 13690Sstevel@tonic-gate /* 13700Sstevel@tonic-gate * Find the fragment by searching through the 13710Sstevel@tonic-gate * free block map for an appropriate bit pattern. 13720Sstevel@tonic-gate */ 13730Sstevel@tonic-gate if (bpref) 13740Sstevel@tonic-gate start = dtogd(fs, bpref) / NBBY; 13750Sstevel@tonic-gate else 13760Sstevel@tonic-gate start = cgp->cg_frotor / NBBY; 13770Sstevel@tonic-gate /* 13780Sstevel@tonic-gate * the following loop performs two scans -- the first scan 13790Sstevel@tonic-gate * searches the bottom half of the array for a match and the 13800Sstevel@tonic-gate * second scan searches the top half of the array. The loops 13810Sstevel@tonic-gate * have been merged just to make things difficult. 13820Sstevel@tonic-gate */ 13830Sstevel@tonic-gate first = start; 13840Sstevel@tonic-gate last = howmany(fs->fs_fpg, NBBY); 13850Sstevel@tonic-gate secondtime = 0; 13860Sstevel@tonic-gate cfrag = cgp->cg_cgx * fs->fs_fpg; 13870Sstevel@tonic-gate while (first < last) { 13880Sstevel@tonic-gate len = last - first; 13890Sstevel@tonic-gate /* 13900Sstevel@tonic-gate * search the array for a match 13910Sstevel@tonic-gate */ 13920Sstevel@tonic-gate loc = scanc((unsigned)len, (uchar_t *)&cg_blksfree(cgp)[first], 13930Sstevel@tonic-gate (uchar_t *)fragtbl[fs->fs_frag], 13940Sstevel@tonic-gate (int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 13950Sstevel@tonic-gate /* 13960Sstevel@tonic-gate * match found 13970Sstevel@tonic-gate */ 13980Sstevel@tonic-gate if (loc) { 13990Sstevel@tonic-gate bno = (last - loc) * NBBY; 14000Sstevel@tonic-gate 14010Sstevel@tonic-gate /* 14020Sstevel@tonic-gate * Found the byte in the map, sift 14030Sstevel@tonic-gate * through the bits to find the selected frag 14040Sstevel@tonic-gate */ 14050Sstevel@tonic-gate cgp->cg_frotor = bno; 14060Sstevel@tonic-gate gotit = 0; 14070Sstevel@tonic-gate for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { 14080Sstevel@tonic-gate blk = blkmap(fs, cg_blksfree(cgp), bno); 14090Sstevel@tonic-gate blk <<= 1; 14100Sstevel@tonic-gate field = around[allocsiz]; 14110Sstevel@tonic-gate subfield = inside[allocsiz]; 14120Sstevel@tonic-gate for (pos = 0; 14130Sstevel@tonic-gate pos <= fs->fs_frag - allocsiz; 14140Sstevel@tonic-gate pos++) { 14150Sstevel@tonic-gate if ((blk & field) == subfield) { 14160Sstevel@tonic-gate gotit++; 14170Sstevel@tonic-gate break; 14180Sstevel@tonic-gate } 14190Sstevel@tonic-gate field <<= 1; 14200Sstevel@tonic-gate subfield <<= 1; 14210Sstevel@tonic-gate } 14220Sstevel@tonic-gate if (gotit) 14230Sstevel@tonic-gate break; 14240Sstevel@tonic-gate } 14250Sstevel@tonic-gate bno += pos; 14260Sstevel@tonic-gate 14270Sstevel@tonic-gate /* 14280Sstevel@tonic-gate * success if block is *not* being converted from 14290Sstevel@tonic-gate * metadata into userdata (harpy). If so, ignore. 14300Sstevel@tonic-gate */ 14310Sstevel@tonic-gate if (!TRANS_ISCANCEL(ufsvfsp, 1432923Ssdebnath ldbtob(fsbtodb(fs, (cfrag+bno))), 1433923Ssdebnath allocsiz * fs->fs_fsize)) 14340Sstevel@tonic-gate return (bno); 1435923Ssdebnath 14360Sstevel@tonic-gate /* 14370Sstevel@tonic-gate * keep looking -- this block is being converted 14380Sstevel@tonic-gate */ 14390Sstevel@tonic-gate first = (last - loc) + 1; 14400Sstevel@tonic-gate loc = 0; 14410Sstevel@tonic-gate if (first < last) 14420Sstevel@tonic-gate continue; 14430Sstevel@tonic-gate } 14440Sstevel@tonic-gate /* 14450Sstevel@tonic-gate * no usable matches in bottom half -- now search the top half 14460Sstevel@tonic-gate */ 14470Sstevel@tonic-gate if (secondtime) 14480Sstevel@tonic-gate /* 14490Sstevel@tonic-gate * no usable matches in top half -- all done 14500Sstevel@tonic-gate */ 14510Sstevel@tonic-gate break; 14520Sstevel@tonic-gate secondtime = 1; 14530Sstevel@tonic-gate last = start + 1; 14540Sstevel@tonic-gate first = 0; 14550Sstevel@tonic-gate } 14560Sstevel@tonic-gate /* 14570Sstevel@tonic-gate * no usable matches 14580Sstevel@tonic-gate */ 14590Sstevel@tonic-gate return ((daddr_t)-1); 14600Sstevel@tonic-gate } 14610Sstevel@tonic-gate 14620Sstevel@tonic-gate #define UFSNADDR (NDADDR + NIADDR) /* NADDR applies to (obsolete) S5FS */ 14630Sstevel@tonic-gate #define IB(i) (NDADDR + (i)) /* index of i'th indirect block ptr */ 14640Sstevel@tonic-gate #define SINGLE 0 /* single indirect block ptr */ 14650Sstevel@tonic-gate #define DOUBLE 1 /* double indirect block ptr */ 14660Sstevel@tonic-gate #define TRIPLE 2 /* triple indirect block ptr */ 14670Sstevel@tonic-gate 14680Sstevel@tonic-gate /* 1469923Ssdebnath * Acquire a write lock, and keep trying till we get it 1470923Ssdebnath */ 1471923Ssdebnath static int 1472923Ssdebnath allocsp_wlockfs(struct vnode *vp, struct lockfs *lf) 1473923Ssdebnath { 1474923Ssdebnath int err = 0; 1475923Ssdebnath 1476923Ssdebnath lockagain: 1477923Ssdebnath do { 1478923Ssdebnath err = ufs_fiolfss(vp, lf); 1479923Ssdebnath if (err) 1480923Ssdebnath return (err); 1481923Ssdebnath } while (!LOCKFS_IS_ULOCK(lf)); 1482923Ssdebnath 1483923Ssdebnath lf->lf_lock = LOCKFS_WLOCK; 1484923Ssdebnath lf->lf_flags = 0; 1485923Ssdebnath lf->lf_comment = NULL; 1486923Ssdebnath err = ufs__fiolfs(vp, lf, 1, 0); 1487923Ssdebnath 1488923Ssdebnath if (err == EBUSY || err == EINVAL) 1489923Ssdebnath goto lockagain; 1490923Ssdebnath 1491923Ssdebnath return (err); 1492923Ssdebnath } 1493923Ssdebnath 1494923Ssdebnath /* 1495923Ssdebnath * Release the write lock 1496923Ssdebnath */ 1497923Ssdebnath static int 1498923Ssdebnath allocsp_unlockfs(struct vnode *vp, struct lockfs *lf) 1499923Ssdebnath { 1500923Ssdebnath int err = 0; 1501923Ssdebnath 1502923Ssdebnath lf->lf_lock = LOCKFS_ULOCK; 1503923Ssdebnath lf->lf_flags = 0; 1504923Ssdebnath err = ufs__fiolfs(vp, lf, 1, 0); 1505923Ssdebnath return (err); 1506923Ssdebnath } 1507923Ssdebnath 1508923Ssdebnath struct allocsp_undo { 1509923Ssdebnath daddr_t offset; 1510923Ssdebnath daddr_t blk; 1511923Ssdebnath struct allocsp_undo *next; 1512923Ssdebnath }; 1513923Ssdebnath 1514923Ssdebnath /* 1515923Ssdebnath * ufs_allocsp() can be used to pre-allocate blocks for a file on a given 1516923Ssdebnath * file system. The blocks are not initialized and are only marked as allocated. 1517923Ssdebnath * These addresses are then stored as negative block numbers in the inode to 1518923Ssdebnath * imply special handling. UFS has been modified where necessary to understand 1519923Ssdebnath * this new notion. Successfully fallocated files will have IFALLOCATE cflag 1520923Ssdebnath * set in the inode. 1521923Ssdebnath */ 1522923Ssdebnath int 1523923Ssdebnath ufs_allocsp(struct vnode *vp, struct flock64 *lp, cred_t *cr) 1524923Ssdebnath { 1525923Ssdebnath struct lockfs lf; 1526923Ssdebnath int berr, err, resv, issync; 1527923Ssdebnath off_t start, istart, len; /* istart, special for idb */ 1528923Ssdebnath struct inode *ip; 1529923Ssdebnath struct fs *fs; 1530923Ssdebnath struct ufsvfs *ufsvfsp; 1531923Ssdebnath u_offset_t resid, i; 1532923Ssdebnath daddr32_t db_undo[NDADDR]; /* old direct blocks */ 1533923Ssdebnath struct allocsp_undo *ib_undo = NULL; /* ib undo */ 1534923Ssdebnath struct allocsp_undo *undo = NULL; 1535923Ssdebnath u_offset_t osz; /* old file size */ 1536923Ssdebnath int chunkblks = 0; /* # of blocks in 1 allocation */ 1537923Ssdebnath int cnt = 0; 1538923Ssdebnath daddr_t allocblk; 1539923Ssdebnath daddr_t totblks = 0; 1540923Ssdebnath struct ulockfs *ulp; 1541923Ssdebnath 1542923Ssdebnath ASSERT(vp->v_type == VREG); 1543923Ssdebnath 1544923Ssdebnath ip = VTOI(vp); 1545923Ssdebnath fs = ip->i_fs; 1546923Ssdebnath if ((ufsvfsp = ip->i_ufsvfs) == NULL) { 1547923Ssdebnath err = EIO; 1548923Ssdebnath goto out_allocsp; 1549923Ssdebnath } 1550923Ssdebnath 1551923Ssdebnath istart = start = blkroundup(fs, (lp->l_start)); 1552923Ssdebnath len = blkroundup(fs, (lp->l_len)); 1553923Ssdebnath chunkblks = blkroundup(fs, ufsvfsp->vfs_iotransz) / fs->fs_bsize; 1554923Ssdebnath ulp = &ufsvfsp->vfs_ulockfs; 1555923Ssdebnath 1556923Ssdebnath if (lp->l_start < 0 || lp->l_len <= 0) 1557923Ssdebnath return (EINVAL); 1558923Ssdebnath 1559923Ssdebnath /* Quickly check to make sure we have space before we proceed */ 1560923Ssdebnath if (lblkno(fs, len) > fs->fs_cstotal.cs_nbfree) { 1561923Ssdebnath if (TRANS_ISTRANS(ufsvfsp)) { 1562923Ssdebnath ufs_delete_drain_wait(ufsvfsp, 1); 1563923Ssdebnath if (lblkno(fs, len) > fs->fs_cstotal.cs_nbfree) 1564923Ssdebnath return (ENOSPC); 1565923Ssdebnath } else 1566923Ssdebnath return (ENOSPC); 1567923Ssdebnath } 1568923Ssdebnath 1569923Ssdebnath /* 1570923Ssdebnath * We will keep i_rwlock locked as WRITER through out the function 1571923Ssdebnath * since we don't want anyone else reading or writing to the inode 1572923Ssdebnath * while we are in the middle of fallocating the file. 1573923Ssdebnath */ 1574923Ssdebnath rw_enter(&ip->i_rwlock, RW_WRITER); 1575923Ssdebnath 1576923Ssdebnath /* Back up the direct block list, used for undo later if necessary */ 1577923Ssdebnath rw_enter(&ip->i_contents, RW_READER); 1578923Ssdebnath for (i = 0; i < NDADDR; i++) 1579923Ssdebnath db_undo[i] = ip->i_db[i]; 1580923Ssdebnath osz = ip->i_size; 1581923Ssdebnath rw_exit(&ip->i_contents); 1582923Ssdebnath 1583923Ssdebnath /* Allocate any direct blocks now before we write lock the fs */ 1584923Ssdebnath if (lblkno(fs, start) < NDADDR) { 1585923Ssdebnath ufs_trans_trunc_resv(ip, ip->i_size + (NDADDR * fs->fs_bsize), 1586923Ssdebnath &resv, &resid); 1587923Ssdebnath TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_ALLOCSP, resv); 1588923Ssdebnath 1589923Ssdebnath rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER); 1590923Ssdebnath rw_enter(&ip->i_contents, RW_WRITER); 1591923Ssdebnath 1592*4454Smishra for (i = start; (i < (start + len)) && (lblkno(fs, i) < NDADDR); 1593923Ssdebnath i += fs->fs_bsize) { 1594923Ssdebnath berr = bmap_write(ip, i, fs->fs_bsize, BI_FALLOCATE, 1595923Ssdebnath &allocblk, cr); 1596923Ssdebnath /* Yikes error, quit */ 1597923Ssdebnath if (berr) { 1598923Ssdebnath TRANS_INODE(ufsvfsp, ip); 1599923Ssdebnath rw_exit(&ip->i_contents); 1600923Ssdebnath rw_exit(&ufsvfsp->vfs_dqrwlock); 1601923Ssdebnath TRANS_END_CSYNC(ufsvfsp, err, issync, 1602923Ssdebnath TOP_ALLOCSP, resv); 1603923Ssdebnath goto exit; 1604923Ssdebnath } 1605923Ssdebnath 1606923Ssdebnath if (allocblk) { 1607923Ssdebnath totblks++; 1608*4454Smishra if (i >= ip->i_size) 1609*4454Smishra ip->i_size += fs->fs_bsize; 1610923Ssdebnath } 1611923Ssdebnath } 1612923Ssdebnath 1613923Ssdebnath TRANS_INODE(ufsvfsp, ip); 1614923Ssdebnath rw_exit(&ip->i_contents); 1615923Ssdebnath rw_exit(&ufsvfsp->vfs_dqrwlock); 1616923Ssdebnath TRANS_END_CSYNC(ufsvfsp, err, issync, TOP_ALLOCSP, resv); 1617923Ssdebnath 1618923Ssdebnath istart = i; /* start offset for indirect allocation */ 1619923Ssdebnath } 1620923Ssdebnath 1621923Ssdebnath /* Write lock the file system */ 1622923Ssdebnath if (err = allocsp_wlockfs(vp, &lf)) 1623923Ssdebnath goto exit; 1624923Ssdebnath 1625923Ssdebnath /* Break the transactions into vfs_iotransz units */ 1626923Ssdebnath ufs_trans_trunc_resv(ip, ip->i_size + 1627923Ssdebnath blkroundup(fs, ufsvfsp->vfs_iotransz), &resv, &resid); 1628923Ssdebnath TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_ALLOCSP, resv); 1629923Ssdebnath 1630923Ssdebnath rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER); 1631923Ssdebnath rw_enter(&ip->i_contents, RW_WRITER); 1632923Ssdebnath 1633923Ssdebnath /* Now go about fallocating necessary indirect blocks */ 1634*4454Smishra for (i = istart; i < (start + len); i += fs->fs_bsize) { 1635923Ssdebnath berr = bmap_write(ip, i, fs->fs_bsize, BI_FALLOCATE, 1636923Ssdebnath &allocblk, cr); 1637923Ssdebnath if (berr) { 1638923Ssdebnath TRANS_INODE(ufsvfsp, ip); 1639923Ssdebnath rw_exit(&ip->i_contents); 1640923Ssdebnath rw_exit(&ufsvfsp->vfs_dqrwlock); 1641923Ssdebnath TRANS_END_CSYNC(ufsvfsp, err, issync, 1642923Ssdebnath TOP_ALLOCSP, resv); 1643923Ssdebnath err = allocsp_unlockfs(vp, &lf); 1644923Ssdebnath goto exit; 1645923Ssdebnath } 1646923Ssdebnath 1647923Ssdebnath /* Update the blk counter only if new block was added */ 1648923Ssdebnath if (allocblk) { 1649923Ssdebnath /* Save undo information */ 1650923Ssdebnath undo = kmem_alloc(sizeof (struct allocsp_undo), 1651923Ssdebnath KM_SLEEP); 1652923Ssdebnath undo->offset = i; 1653923Ssdebnath undo->blk = allocblk; 1654923Ssdebnath undo->next = ib_undo; 1655923Ssdebnath ib_undo = undo; 1656923Ssdebnath totblks++; 1657*4454Smishra 1658*4454Smishra if (i >= ip->i_size) 1659*4454Smishra ip->i_size += fs->fs_bsize; 1660923Ssdebnath } 1661923Ssdebnath cnt++; 1662923Ssdebnath 1663923Ssdebnath /* Being a good UFS citizen, let others get a share */ 1664923Ssdebnath if (cnt == chunkblks) { 1665923Ssdebnath /* 1666923Ssdebnath * If there are waiters or the fs is hard locked, 1667923Ssdebnath * error locked, or read-only error locked, 1668923Ssdebnath * quit with EIO 1669923Ssdebnath */ 1670923Ssdebnath if (ULOCKFS_IS_HLOCK(ulp) || ULOCKFS_IS_ELOCK(ulp) || 1671923Ssdebnath ULOCKFS_IS_ROELOCK(ulp)) { 1672923Ssdebnath ip->i_cflags |= IFALLOCATE; 1673923Ssdebnath TRANS_INODE(ufsvfsp, ip); 1674923Ssdebnath rw_exit(&ip->i_contents); 1675923Ssdebnath rw_exit(&ufsvfsp->vfs_dqrwlock); 1676923Ssdebnath 1677923Ssdebnath TRANS_END_CSYNC(ufsvfsp, err, issync, 1678923Ssdebnath TOP_ALLOCSP, resv); 1679923Ssdebnath rw_exit(&ip->i_rwlock); 1680923Ssdebnath return (EIO); 1681923Ssdebnath } 1682923Ssdebnath 1683923Ssdebnath TRANS_INODE(ufsvfsp, ip); 1684923Ssdebnath rw_exit(&ip->i_contents); 1685923Ssdebnath rw_exit(&ufsvfsp->vfs_dqrwlock); 1686923Ssdebnath 1687923Ssdebnath /* End the current transaction */ 1688923Ssdebnath TRANS_END_CSYNC(ufsvfsp, err, issync, 1689923Ssdebnath TOP_ALLOCSP, resv); 1690923Ssdebnath 1691923Ssdebnath if (CV_HAS_WAITERS(&ulp->ul_cv)) { 1692923Ssdebnath /* Release the write lock */ 1693923Ssdebnath if (err = allocsp_unlockfs(vp, &lf)) 1694923Ssdebnath goto exit; 1695923Ssdebnath 1696923Ssdebnath /* Wake up others waiting to do operations */ 1697923Ssdebnath mutex_enter(&ulp->ul_lock); 1698923Ssdebnath cv_broadcast(&ulp->ul_cv); 1699923Ssdebnath mutex_exit(&ulp->ul_lock); 1700923Ssdebnath 1701923Ssdebnath /* Grab the write lock again */ 1702923Ssdebnath if (err = allocsp_wlockfs(vp, &lf)) 1703923Ssdebnath goto exit; 1704923Ssdebnath } /* end of CV_HAS_WAITERS(&ulp->ul_cv) */ 1705923Ssdebnath 1706923Ssdebnath /* Reserve more space in log for this file */ 1707923Ssdebnath ufs_trans_trunc_resv(ip, 1708923Ssdebnath ip->i_size + blkroundup(fs, ufsvfsp->vfs_iotransz), 1709923Ssdebnath &resv, &resid); 1710923Ssdebnath TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_ALLOCSP, resv); 1711923Ssdebnath 1712923Ssdebnath rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER); 1713923Ssdebnath rw_enter(&ip->i_contents, RW_WRITER); 1714923Ssdebnath 1715923Ssdebnath cnt = 0; /* reset cnt b/c of new transaction */ 1716923Ssdebnath } 1717923Ssdebnath } 1718923Ssdebnath 1719923Ssdebnath if (!err && !berr) 1720923Ssdebnath ip->i_cflags |= IFALLOCATE; 1721923Ssdebnath 1722923Ssdebnath /* Release locks, end log transaction and unlock fs */ 1723923Ssdebnath TRANS_INODE(ufsvfsp, ip); 1724923Ssdebnath rw_exit(&ip->i_contents); 1725923Ssdebnath rw_exit(&ufsvfsp->vfs_dqrwlock); 1726923Ssdebnath 1727923Ssdebnath TRANS_END_CSYNC(ufsvfsp, err, issync, TOP_ALLOCSP, resv); 1728923Ssdebnath err = allocsp_unlockfs(vp, &lf); 1729923Ssdebnath 1730923Ssdebnath /* 1731923Ssdebnath * @ exit label, we should no longer be holding the fs write lock, and 1732923Ssdebnath * all logging transactions should have been ended. We still hold 1733923Ssdebnath * ip->i_rwlock. 1734923Ssdebnath */ 1735923Ssdebnath exit: 1736923Ssdebnath /* 1737923Ssdebnath * File has grown larger than 2GB. Set flag 1738923Ssdebnath * in superblock to indicate this, if it 1739923Ssdebnath * is not already set. 1740923Ssdebnath */ 1741923Ssdebnath if ((ip->i_size > MAXOFF32_T) && 1742923Ssdebnath !(fs->fs_flags & FSLARGEFILES)) { 1743923Ssdebnath ASSERT(ufsvfsp->vfs_lfflags & UFS_LARGEFILES); 1744923Ssdebnath mutex_enter(&ufsvfsp->vfs_lock); 1745923Ssdebnath fs->fs_flags |= FSLARGEFILES; 1746923Ssdebnath ufs_sbwrite(ufsvfsp); 1747923Ssdebnath mutex_exit(&ufsvfsp->vfs_lock); 1748923Ssdebnath } 1749923Ssdebnath 1750923Ssdebnath /* 1751923Ssdebnath * Since we couldn't allocate completely, we will undo the allocations. 1752923Ssdebnath */ 1753923Ssdebnath if (berr) { 1754923Ssdebnath ufs_trans_trunc_resv(ip, totblks * fs->fs_bsize, &resv, &resid); 1755923Ssdebnath TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_ALLOCSP, resv); 1756923Ssdebnath 1757923Ssdebnath rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER); 1758923Ssdebnath rw_enter(&ip->i_contents, RW_WRITER); 1759923Ssdebnath 1760923Ssdebnath /* Direct blocks */ 1761923Ssdebnath for (i = 0; i < NDADDR; i++) { 1762923Ssdebnath /* 1763923Ssdebnath * Only free the block if they are not same, and 1764923Ssdebnath * the old one isn't zero (the fragment was 1765923Ssdebnath * re-allocated). 1766923Ssdebnath */ 1767923Ssdebnath if (db_undo[i] != ip->i_db[i] && db_undo[i] == 0) { 1768923Ssdebnath free(ip, ip->i_db[i], fs->fs_bsize, 0); 1769923Ssdebnath ip->i_db[i] = 0; 1770923Ssdebnath } 1771923Ssdebnath } 1772923Ssdebnath 1773923Ssdebnath /* Undo the indirect blocks */ 1774923Ssdebnath while (ib_undo != NULL) { 1775923Ssdebnath undo = ib_undo; 1776923Ssdebnath err = bmap_set_bn(vp, undo->offset, 0); 1777923Ssdebnath if (err) 1778923Ssdebnath cmn_err(CE_PANIC, "ufs_allocsp(): failed to " 1779923Ssdebnath "undo allocation of block %ld", 1780923Ssdebnath undo->offset); 1781923Ssdebnath free(ip, undo->blk, fs->fs_bsize, I_IBLK); 1782923Ssdebnath ib_undo = undo->next; 1783923Ssdebnath kmem_free(undo, sizeof (struct allocsp_undo)); 1784923Ssdebnath } 1785923Ssdebnath 1786923Ssdebnath ip->i_size = osz; 1787923Ssdebnath TRANS_INODE(ufsvfsp, ip); 1788923Ssdebnath 1789923Ssdebnath rw_exit(&ip->i_contents); 1790923Ssdebnath rw_exit(&ufsvfsp->vfs_dqrwlock); 1791923Ssdebnath 1792923Ssdebnath TRANS_END_CSYNC(ufsvfsp, err, issync, TOP_ALLOCSP, resv); 1793923Ssdebnath 1794923Ssdebnath rw_exit(&ip->i_rwlock); 1795923Ssdebnath return (berr); 1796923Ssdebnath } 1797923Ssdebnath 1798923Ssdebnath /* 1799923Ssdebnath * Don't forget to free the undo chain :) 1800923Ssdebnath */ 1801923Ssdebnath while (ib_undo != NULL) { 1802923Ssdebnath undo = ib_undo; 1803923Ssdebnath ib_undo = undo->next; 1804923Ssdebnath kmem_free(undo, sizeof (struct allocsp_undo)); 1805923Ssdebnath } 1806923Ssdebnath 1807923Ssdebnath rw_exit(&ip->i_rwlock); 1808923Ssdebnath 1809923Ssdebnath out_allocsp: 1810923Ssdebnath return (err); 1811923Ssdebnath } 1812923Ssdebnath 1813923Ssdebnath /* 18140Sstevel@tonic-gate * Free storage space associated with the specified inode. The portion 18150Sstevel@tonic-gate * to be freed is specified by lp->l_start and lp->l_len (already 18160Sstevel@tonic-gate * normalized to a "whence" of 0). 18170Sstevel@tonic-gate * 18180Sstevel@tonic-gate * This is an experimental facility whose continued existence is not 18190Sstevel@tonic-gate * guaranteed. Currently, we only support the special case 18200Sstevel@tonic-gate * of l_len == 0, meaning free to end of file. 18210Sstevel@tonic-gate * 18220Sstevel@tonic-gate * Blocks are freed in reverse order. This FILO algorithm will tend to 18230Sstevel@tonic-gate * maintain a contiguous free list much longer than FIFO. 18240Sstevel@tonic-gate * See also ufs_itrunc() in ufs_inode.c. 18250Sstevel@tonic-gate * 18260Sstevel@tonic-gate * Bug: unused bytes in the last retained block are not cleared. 18270Sstevel@tonic-gate * This may result in a "hole" in the file that does not read as zeroes. 18280Sstevel@tonic-gate */ 18290Sstevel@tonic-gate /* ARGSUSED */ 18300Sstevel@tonic-gate int 18310Sstevel@tonic-gate ufs_freesp(struct vnode *vp, struct flock64 *lp, int flag, cred_t *cr) 18320Sstevel@tonic-gate { 18330Sstevel@tonic-gate int i; 18340Sstevel@tonic-gate struct inode *ip = VTOI(vp); 18350Sstevel@tonic-gate int error; 18360Sstevel@tonic-gate 18370Sstevel@tonic-gate ASSERT(vp->v_type == VREG); 18380Sstevel@tonic-gate ASSERT(lp->l_start >= 0); /* checked by convoff */ 18390Sstevel@tonic-gate 18400Sstevel@tonic-gate if (lp->l_len != 0) 18410Sstevel@tonic-gate return (EINVAL); 18420Sstevel@tonic-gate 18430Sstevel@tonic-gate rw_enter(&ip->i_contents, RW_READER); 18440Sstevel@tonic-gate if (ip->i_size == (u_offset_t)lp->l_start) { 18450Sstevel@tonic-gate rw_exit(&ip->i_contents); 18460Sstevel@tonic-gate return (0); 18470Sstevel@tonic-gate } 18480Sstevel@tonic-gate 18490Sstevel@tonic-gate /* 18500Sstevel@tonic-gate * Check if there is any active mandatory lock on the 18510Sstevel@tonic-gate * range that will be truncated/expanded. 18520Sstevel@tonic-gate */ 18530Sstevel@tonic-gate if (MANDLOCK(vp, ip->i_mode)) { 18540Sstevel@tonic-gate offset_t save_start; 18550Sstevel@tonic-gate 18560Sstevel@tonic-gate save_start = lp->l_start; 18570Sstevel@tonic-gate 18580Sstevel@tonic-gate if (ip->i_size < lp->l_start) { 18590Sstevel@tonic-gate /* 18600Sstevel@tonic-gate * "Truncate up" case: need to make sure there 18610Sstevel@tonic-gate * is no lock beyond current end-of-file. To 18620Sstevel@tonic-gate * do so, we need to set l_start to the size 18630Sstevel@tonic-gate * of the file temporarily. 18640Sstevel@tonic-gate */ 18650Sstevel@tonic-gate lp->l_start = ip->i_size; 18660Sstevel@tonic-gate } 18670Sstevel@tonic-gate lp->l_type = F_WRLCK; 18680Sstevel@tonic-gate lp->l_sysid = 0; 18690Sstevel@tonic-gate lp->l_pid = ttoproc(curthread)->p_pid; 18700Sstevel@tonic-gate i = (flag & (FNDELAY|FNONBLOCK)) ? 0 : SLPFLCK; 18710Sstevel@tonic-gate rw_exit(&ip->i_contents); 18720Sstevel@tonic-gate if ((i = reclock(vp, lp, i, 0, lp->l_start, NULL)) != 0 || 18730Sstevel@tonic-gate lp->l_type != F_UNLCK) { 18740Sstevel@tonic-gate return (i ? i : EAGAIN); 18750Sstevel@tonic-gate } 18760Sstevel@tonic-gate rw_enter(&ip->i_contents, RW_READER); 18770Sstevel@tonic-gate 18780Sstevel@tonic-gate lp->l_start = save_start; 18790Sstevel@tonic-gate } 18800Sstevel@tonic-gate 18810Sstevel@tonic-gate /* 18820Sstevel@tonic-gate * Make sure a write isn't in progress (allocating blocks) 18830Sstevel@tonic-gate * by acquiring i_rwlock (we promised ufs_bmap we wouldn't 18840Sstevel@tonic-gate * truncate while it was allocating blocks). 18850Sstevel@tonic-gate * Grab the locks in the right order. 18860Sstevel@tonic-gate */ 18870Sstevel@tonic-gate rw_exit(&ip->i_contents); 18880Sstevel@tonic-gate rw_enter(&ip->i_rwlock, RW_WRITER); 18890Sstevel@tonic-gate error = TRANS_ITRUNC(ip, (u_offset_t)lp->l_start, 0, cr); 18900Sstevel@tonic-gate rw_exit(&ip->i_rwlock); 18910Sstevel@tonic-gate return (error); 18920Sstevel@tonic-gate } 18930Sstevel@tonic-gate 18940Sstevel@tonic-gate /* 18950Sstevel@tonic-gate * Find a cg with as close to nb contiguous bytes as possible 18960Sstevel@tonic-gate * THIS MAY TAKE MANY DISK READS! 18970Sstevel@tonic-gate * 18980Sstevel@tonic-gate * Implemented in an attempt to allocate contiguous blocks for 18990Sstevel@tonic-gate * writing the ufs log file to, minimizing future disk head seeking 19000Sstevel@tonic-gate */ 19010Sstevel@tonic-gate daddr_t 19020Sstevel@tonic-gate contigpref(ufsvfs_t *ufsvfsp, size_t nb) 19030Sstevel@tonic-gate { 19040Sstevel@tonic-gate struct fs *fs = ufsvfsp->vfs_fs; 19050Sstevel@tonic-gate daddr_t nblk = lblkno(fs, blkroundup(fs, nb)); 19060Sstevel@tonic-gate daddr_t savebno, curbno, cgbno; 19070Sstevel@tonic-gate int cg, cgblks, savecg, savenblk, curnblk; 19080Sstevel@tonic-gate uchar_t *blksfree; 19090Sstevel@tonic-gate buf_t *bp; 19100Sstevel@tonic-gate struct cg *cgp; 19110Sstevel@tonic-gate 19120Sstevel@tonic-gate savenblk = 0; 19130Sstevel@tonic-gate savecg = 0; 19140Sstevel@tonic-gate savebno = 0; 19150Sstevel@tonic-gate for (cg = 0; cg < fs->fs_ncg; ++cg) { 19160Sstevel@tonic-gate 19170Sstevel@tonic-gate /* not enough free blks for a contig check */ 19180Sstevel@tonic-gate if (fs->fs_cs(fs, cg).cs_nbfree < nblk) 19190Sstevel@tonic-gate continue; 19200Sstevel@tonic-gate 19210Sstevel@tonic-gate /* 19220Sstevel@tonic-gate * find the largest contiguous range in this cg 19230Sstevel@tonic-gate */ 19240Sstevel@tonic-gate bp = UFS_BREAD(ufsvfsp, ufsvfsp->vfs_dev, 1925923Ssdebnath (daddr_t)fsbtodb(fs, cgtod(fs, cg)), 1926923Ssdebnath (int)fs->fs_cgsize); 19270Sstevel@tonic-gate cgp = bp->b_un.b_cg; 19280Sstevel@tonic-gate if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) { 19290Sstevel@tonic-gate brelse(bp); 19300Sstevel@tonic-gate continue; 19310Sstevel@tonic-gate } 19320Sstevel@tonic-gate blksfree = cg_blksfree(cgp); /* free array */ 19330Sstevel@tonic-gate cgblks = fragstoblks(fs, fs->fs_fpg); /* blks in free array */ 19340Sstevel@tonic-gate cgbno = 0; 19350Sstevel@tonic-gate while (cgbno < cgblks && savenblk < nblk) { 19360Sstevel@tonic-gate /* find a free block */ 19370Sstevel@tonic-gate for (; cgbno < cgblks; ++cgbno) 19380Sstevel@tonic-gate if (isblock(fs, blksfree, cgbno)) 19390Sstevel@tonic-gate break; 19400Sstevel@tonic-gate curbno = cgbno; 19410Sstevel@tonic-gate /* count the number of free blocks */ 19420Sstevel@tonic-gate for (curnblk = 0; cgbno < cgblks; ++cgbno) { 19430Sstevel@tonic-gate if (!isblock(fs, blksfree, cgbno)) 19440Sstevel@tonic-gate break; 19450Sstevel@tonic-gate if (++curnblk >= nblk) 19460Sstevel@tonic-gate break; 19470Sstevel@tonic-gate } 19480Sstevel@tonic-gate if (curnblk > savenblk) { 19490Sstevel@tonic-gate savecg = cg; 19500Sstevel@tonic-gate savenblk = curnblk; 19510Sstevel@tonic-gate savebno = curbno; 19520Sstevel@tonic-gate } 19530Sstevel@tonic-gate } 19540Sstevel@tonic-gate brelse(bp); 19550Sstevel@tonic-gate if (savenblk >= nblk) 19560Sstevel@tonic-gate break; 19570Sstevel@tonic-gate } 19580Sstevel@tonic-gate 19590Sstevel@tonic-gate /* convert block offset in cg to frag offset in cg */ 19600Sstevel@tonic-gate savebno = blkstofrags(fs, savebno); 19610Sstevel@tonic-gate 19620Sstevel@tonic-gate /* convert frag offset in cg to frag offset in fs */ 19630Sstevel@tonic-gate savebno += (savecg * fs->fs_fpg); 19640Sstevel@tonic-gate 19650Sstevel@tonic-gate return (savebno); 19660Sstevel@tonic-gate } 1967