17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 237c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate /* 317c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 327c478bd9Sstevel@tonic-gate * The Regents of the University of California 337c478bd9Sstevel@tonic-gate * All Rights Reserved 347c478bd9Sstevel@tonic-gate * 357c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 367c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 377c478bd9Sstevel@tonic-gate * contributors. 387c478bd9Sstevel@tonic-gate */ 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate #include <sys/types.h> 447c478bd9Sstevel@tonic-gate #include <sys/t_lock.h> 457c478bd9Sstevel@tonic-gate #include <sys/param.h> 467c478bd9Sstevel@tonic-gate #include <sys/systm.h> 477c478bd9Sstevel@tonic-gate #include <sys/signal.h> 487c478bd9Sstevel@tonic-gate #include <sys/user.h> 497c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 507c478bd9Sstevel@tonic-gate #include <sys/buf.h> 517c478bd9Sstevel@tonic-gate #include <sys/disp.h> 527c478bd9Sstevel@tonic-gate #include <sys/proc.h> 537c478bd9Sstevel@tonic-gate #include <sys/conf.h> 547c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_inode.h> 557c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fs.h> 567c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_quota.h> 577c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_trans.h> 587c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_bio.h> 597c478bd9Sstevel@tonic-gate #include <vm/seg.h> 607c478bd9Sstevel@tonic-gate #include <sys/errno.h> 617c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 627c478bd9Sstevel@tonic-gate #include <sys/vfs.h> 637c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 647c478bd9Sstevel@tonic-gate #include <sys/debug.h> 657c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate /* 687c478bd9Sstevel@tonic-gate * This structure is used to track blocks as we allocate them, so that 697c478bd9Sstevel@tonic-gate * we can free them if we encounter an error during allocation. We 707c478bd9Sstevel@tonic-gate * keep track of five pieces of information for each allocated block: 717c478bd9Sstevel@tonic-gate * - The number of the newly allocated block 727c478bd9Sstevel@tonic-gate * - The size of the block (lets us deal with fragments if we want) 737c478bd9Sstevel@tonic-gate * - The number of the block containing a pointer to it; or whether 747c478bd9Sstevel@tonic-gate * the pointer is in the inode 757c478bd9Sstevel@tonic-gate * - The offset within the block (or inode) containing a pointer to it. 767c478bd9Sstevel@tonic-gate * - A flag indicating the usage of the block. (Logging needs to know 777c478bd9Sstevel@tonic-gate * this to avoid overwriting a data block if it was previously used 787c478bd9Sstevel@tonic-gate * for metadata.) 797c478bd9Sstevel@tonic-gate */ 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate enum ufs_owner_type { 827c478bd9Sstevel@tonic-gate ufs_no_owner, /* Owner has not yet been updated */ 837c478bd9Sstevel@tonic-gate ufs_inode_direct, /* Listed in inode's direct block table */ 847c478bd9Sstevel@tonic-gate ufs_inode_indirect, /* Listed in inode's indirect block table */ 857c478bd9Sstevel@tonic-gate ufs_indirect_block /* Listed in an indirect block */ 867c478bd9Sstevel@tonic-gate }; 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate struct ufs_allocated_block { 897c478bd9Sstevel@tonic-gate daddr_t this_block; /* Number of this block */ 907c478bd9Sstevel@tonic-gate off_t block_size; /* Size of this block, in bytes */ 917c478bd9Sstevel@tonic-gate enum ufs_owner_type owner; /* Who points to this block? */ 927c478bd9Sstevel@tonic-gate daddr_t owner_block; /* Number of the owning block */ 937c478bd9Sstevel@tonic-gate uint_t owner_offset; /* Offset within that block or inode */ 947c478bd9Sstevel@tonic-gate int usage_flags; /* Usage flags, as expected by free() */ 957c478bd9Sstevel@tonic-gate }; 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate static int findextent(struct fs *fs, daddr32_t *sbp, int n, int *lenp, 997c478bd9Sstevel@tonic-gate int maxtrans); 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate static void ufs_undo_allocation(inode_t *ip, int block_count, 1027c478bd9Sstevel@tonic-gate struct ufs_allocated_block table[], int inode_sector_adjust); 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate /* 1057c478bd9Sstevel@tonic-gate * Find the extent and the matching block number. 1067c478bd9Sstevel@tonic-gate * 1077c478bd9Sstevel@tonic-gate * bsize > PAGESIZE 1087c478bd9Sstevel@tonic-gate * boff indicates that we want a page in the middle 1097c478bd9Sstevel@tonic-gate * min expression is supposed to make sure no extra page[s] after EOF 1107c478bd9Sstevel@tonic-gate * PAGESIZE >= bsize 1117c478bd9Sstevel@tonic-gate * we assume that a page is a multiple of bsize, i.e., 1127c478bd9Sstevel@tonic-gate * boff always == 0 1137c478bd9Sstevel@tonic-gate * 1147c478bd9Sstevel@tonic-gate * We always return a length that is suitable for a disk transfer. 1157c478bd9Sstevel@tonic-gate */ 1167c478bd9Sstevel@tonic-gate #define DOEXTENT(fs, lbn, boff, bnp, lenp, size, tblp, n, chkfrag, maxtrans) {\ 1177c478bd9Sstevel@tonic-gate register daddr32_t *dp = (tblp); \ 1187c478bd9Sstevel@tonic-gate register int _chkfrag = chkfrag; /* for lint. sigh */ \ 1197c478bd9Sstevel@tonic-gate \ 1207c478bd9Sstevel@tonic-gate if (*dp == 0) { \ 1217c478bd9Sstevel@tonic-gate *(bnp) = UFS_HOLE; \ 1227c478bd9Sstevel@tonic-gate } else { \ 1237c478bd9Sstevel@tonic-gate register int len; \ 1247c478bd9Sstevel@tonic-gate \ 1257c478bd9Sstevel@tonic-gate len = findextent(fs, dp, (int)(n), lenp, maxtrans) << \ 1267c478bd9Sstevel@tonic-gate (fs)->fs_bshift; \ 1277c478bd9Sstevel@tonic-gate if (_chkfrag) { \ 1287c478bd9Sstevel@tonic-gate register u_offset_t tmp; \ 1297c478bd9Sstevel@tonic-gate \ 1307c478bd9Sstevel@tonic-gate tmp = fragroundup((fs), size) - \ 1317c478bd9Sstevel@tonic-gate (((u_offset_t)lbn) << fs->fs_bshift); \ 1327c478bd9Sstevel@tonic-gate len = (int)MIN(tmp, len); \ 1337c478bd9Sstevel@tonic-gate } \ 1347c478bd9Sstevel@tonic-gate len -= (boff); \ 1357c478bd9Sstevel@tonic-gate if (len <= 0) { \ 1367c478bd9Sstevel@tonic-gate *(bnp) = UFS_HOLE; \ 1377c478bd9Sstevel@tonic-gate } else { \ 1387c478bd9Sstevel@tonic-gate *(bnp) = fsbtodb(fs, *dp) + btodb(boff); \ 1397c478bd9Sstevel@tonic-gate *(lenp) = len; \ 1407c478bd9Sstevel@tonic-gate } \ 1417c478bd9Sstevel@tonic-gate } \ 1427c478bd9Sstevel@tonic-gate } 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate /* 1457c478bd9Sstevel@tonic-gate * The maximum supported file size is actually somewhat less that 1 1467c478bd9Sstevel@tonic-gate * terabyte. This is because the total number of blocks used for the 1477c478bd9Sstevel@tonic-gate * file and its metadata must fit into the ic_blocks field of the 1487c478bd9Sstevel@tonic-gate * inode, which is a signed 32-bit quantity. The metadata allocated 1497c478bd9Sstevel@tonic-gate * for a file (that is, the single, double, and triple indirect blocks 1507c478bd9Sstevel@tonic-gate * used to reference the file blocks) is actually quite small, 1517c478bd9Sstevel@tonic-gate * but just to make sure, we check for overflow in the ic_blocks 1527c478bd9Sstevel@tonic-gate * ic_blocks fields for all files whose total block count is 1537c478bd9Sstevel@tonic-gate * within 1 GB of a terabyte. VERYLARGEFILESIZE below is the number of 1547c478bd9Sstevel@tonic-gate * 512-byte blocks in a terabyte (2^31), minus the number of 512-byte blocks 1557c478bd9Sstevel@tonic-gate * in a gigabyte (2^21). We only check for overflow in the ic_blocks 1567c478bd9Sstevel@tonic-gate * field if the number of blocks currently allocated to the file is 1577c478bd9Sstevel@tonic-gate * greater than VERYLARGEFILESIZE. 1587c478bd9Sstevel@tonic-gate * 1597c478bd9Sstevel@tonic-gate * Note that file "size" is the not the same as file "length". A 1607c478bd9Sstevel@tonic-gate * file's "size" is the number of blocks allocated to it. A file's 1617c478bd9Sstevel@tonic-gate * "length" is the maximum offset in the file. A UFS FILE can have a 1627c478bd9Sstevel@tonic-gate * length of a terabyte, but the size is limited to somewhat less than 1637c478bd9Sstevel@tonic-gate * a terabyte, as described above. 1647c478bd9Sstevel@tonic-gate */ 1657c478bd9Sstevel@tonic-gate #define VERYLARGEFILESIZE 0x7FE00000 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate /* 1687c478bd9Sstevel@tonic-gate * bmap{rd,wr} define the structure of file system storage by mapping 1697c478bd9Sstevel@tonic-gate * a logical offset in a file to a physical block number on the device. 1707c478bd9Sstevel@tonic-gate * It should be called with a locked inode when allocation is to be 1717c478bd9Sstevel@tonic-gate * done (bmapwr). Note this strangeness: bmapwr is always called from 1727c478bd9Sstevel@tonic-gate * getpage(), not putpage(), since getpage() is where all the allocation 1737c478bd9Sstevel@tonic-gate * is done. 1747c478bd9Sstevel@tonic-gate * 1757c478bd9Sstevel@tonic-gate * S_READ, S_OTHER -> bmaprd; S_WRITE -> bmapwr. 1767c478bd9Sstevel@tonic-gate * 1777c478bd9Sstevel@tonic-gate * NOTICE: the block number returned is the disk block number, not the 1787c478bd9Sstevel@tonic-gate * file system block number. All the worries about block offsets and 1797c478bd9Sstevel@tonic-gate * page/block sizes are hidden inside of bmap. Well, not quite, 1807c478bd9Sstevel@tonic-gate * unfortunately. It's impossible to find one place to hide all this 1817c478bd9Sstevel@tonic-gate * mess. There are 3 cases: 1827c478bd9Sstevel@tonic-gate * 1837c478bd9Sstevel@tonic-gate * PAGESIZE < bsize 1847c478bd9Sstevel@tonic-gate * In this case, the {get,put}page routines will attempt to align to 1857c478bd9Sstevel@tonic-gate * a file system block boundry (XXX - maybe this is a mistake?). Since 1867c478bd9Sstevel@tonic-gate * the kluster routines may be out of memory, we don't always get all 1877c478bd9Sstevel@tonic-gate * the pages we wanted. If we called bmap first, to find out how much 1887c478bd9Sstevel@tonic-gate * to kluster, we handed in the block aligned offset. If we didn't get 1897c478bd9Sstevel@tonic-gate * all the pages, we have to chop off the amount we didn't get from the 1907c478bd9Sstevel@tonic-gate * amount handed back by bmap. 1917c478bd9Sstevel@tonic-gate * 1927c478bd9Sstevel@tonic-gate * PAGESIZE == bsize 1937c478bd9Sstevel@tonic-gate * Life is quite pleasant here, no extra work needed, mainly because we 1947c478bd9Sstevel@tonic-gate * (probably?) won't kluster backwards, just forwards. 1957c478bd9Sstevel@tonic-gate * 1967c478bd9Sstevel@tonic-gate * PAGESIZE > bsize 1977c478bd9Sstevel@tonic-gate * This one has a different set of problems, specifically, we may have to 1987c478bd9Sstevel@tonic-gate * do N reads to fill one page. Let us hope that Sun will stay with small 1997c478bd9Sstevel@tonic-gate * pages. 2007c478bd9Sstevel@tonic-gate * 2017c478bd9Sstevel@tonic-gate * Returns 0 on success, or a non-zero errno if an error occurs. 2027c478bd9Sstevel@tonic-gate * 2037c478bd9Sstevel@tonic-gate * TODO 2047c478bd9Sstevel@tonic-gate * LMXXX - add a bmap cache. This could be a couple of extents in the 2057c478bd9Sstevel@tonic-gate * inode. Two is nice for PAGESIZE > bsize. 2067c478bd9Sstevel@tonic-gate */ 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate int 2097c478bd9Sstevel@tonic-gate bmap_read(struct inode *ip, u_offset_t off, daddr_t *bnp, int *lenp) 2107c478bd9Sstevel@tonic-gate { 2117c478bd9Sstevel@tonic-gate daddr_t lbn; 2127c478bd9Sstevel@tonic-gate ufsvfs_t *ufsvfsp = ip->i_ufsvfs; 2137c478bd9Sstevel@tonic-gate struct fs *fs = ufsvfsp->vfs_fs; 2147c478bd9Sstevel@tonic-gate struct buf *bp; 2157c478bd9Sstevel@tonic-gate int i, j, boff; 2167c478bd9Sstevel@tonic-gate int shft; /* we maintain sh = 1 << shft */ 2177c478bd9Sstevel@tonic-gate daddr_t ob, nb, tbn; 2187c478bd9Sstevel@tonic-gate daddr32_t *bap; 2197c478bd9Sstevel@tonic-gate int nindirshift, nindiroffset; 2207c478bd9Sstevel@tonic-gate 2217c478bd9Sstevel@tonic-gate ASSERT(RW_LOCK_HELD(&ip->i_contents)); 2227c478bd9Sstevel@tonic-gate lbn = (daddr_t)lblkno(fs, off); 2237c478bd9Sstevel@tonic-gate boff = (int)blkoff(fs, off); 2247c478bd9Sstevel@tonic-gate if (lbn < 0) 2257c478bd9Sstevel@tonic-gate return (EFBIG); 2267c478bd9Sstevel@tonic-gate 2277c478bd9Sstevel@tonic-gate /* 2287c478bd9Sstevel@tonic-gate * The first NDADDR blocks are direct blocks. 2297c478bd9Sstevel@tonic-gate */ 2307c478bd9Sstevel@tonic-gate if (lbn < NDADDR) { 2317c478bd9Sstevel@tonic-gate DOEXTENT(fs, lbn, boff, bnp, lenp, 2327c478bd9Sstevel@tonic-gate ip->i_size, &ip->i_db[lbn], NDADDR - lbn, 1, 2337c478bd9Sstevel@tonic-gate ufsvfsp->vfs_iotransz); 2347c478bd9Sstevel@tonic-gate return (0); 2357c478bd9Sstevel@tonic-gate } 2367c478bd9Sstevel@tonic-gate 2377c478bd9Sstevel@tonic-gate nindirshift = ufsvfsp->vfs_nindirshift; 2387c478bd9Sstevel@tonic-gate nindiroffset = ufsvfsp->vfs_nindiroffset; 2397c478bd9Sstevel@tonic-gate /* 2407c478bd9Sstevel@tonic-gate * Determine how many levels of indirection. 2417c478bd9Sstevel@tonic-gate */ 2427c478bd9Sstevel@tonic-gate shft = 0; /* sh = 1 */ 2437c478bd9Sstevel@tonic-gate tbn = lbn - NDADDR; 2447c478bd9Sstevel@tonic-gate for (j = NIADDR; j > 0; j--) { 2457c478bd9Sstevel@tonic-gate longlong_t sh; 2467c478bd9Sstevel@tonic-gate 2477c478bd9Sstevel@tonic-gate shft += nindirshift; /* sh *= nindir */ 2487c478bd9Sstevel@tonic-gate sh = 1LL << shft; 2497c478bd9Sstevel@tonic-gate if (tbn < sh) 2507c478bd9Sstevel@tonic-gate break; 2517c478bd9Sstevel@tonic-gate tbn -= sh; 2527c478bd9Sstevel@tonic-gate } 2537c478bd9Sstevel@tonic-gate if (j == 0) 2547c478bd9Sstevel@tonic-gate return (EFBIG); 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate /* 2577c478bd9Sstevel@tonic-gate * Fetch the first indirect block. 2587c478bd9Sstevel@tonic-gate */ 2597c478bd9Sstevel@tonic-gate nb = ip->i_ib[NIADDR - j]; 2607c478bd9Sstevel@tonic-gate if (nb == 0) { 2617c478bd9Sstevel@tonic-gate *bnp = UFS_HOLE; 2627c478bd9Sstevel@tonic-gate return (0); 2637c478bd9Sstevel@tonic-gate } 2647c478bd9Sstevel@tonic-gate 2657c478bd9Sstevel@tonic-gate /* 2667c478bd9Sstevel@tonic-gate * Fetch through the indirect blocks. 2677c478bd9Sstevel@tonic-gate */ 2687c478bd9Sstevel@tonic-gate for (; j <= NIADDR; j++) { 2697c478bd9Sstevel@tonic-gate ob = nb; 2707c478bd9Sstevel@tonic-gate bp = UFS_BREAD(ufsvfsp, 2717c478bd9Sstevel@tonic-gate ip->i_dev, fsbtodb(fs, ob), fs->fs_bsize); 2727c478bd9Sstevel@tonic-gate if (bp->b_flags & B_ERROR) { 2737c478bd9Sstevel@tonic-gate brelse(bp); 2747c478bd9Sstevel@tonic-gate return (EIO); 2757c478bd9Sstevel@tonic-gate } 2767c478bd9Sstevel@tonic-gate bap = bp->b_un.b_daddr; 2777c478bd9Sstevel@tonic-gate 2787c478bd9Sstevel@tonic-gate ASSERT(!ufs_indir_badblock(ip, bap)); 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate shft -= nindirshift; /* sh / nindir */ 2817c478bd9Sstevel@tonic-gate i = (tbn >> shft) & nindiroffset; /* (tbn / sh) % nindir */ 2827c478bd9Sstevel@tonic-gate nb = bap[i]; 2837c478bd9Sstevel@tonic-gate if (nb == 0) { 2847c478bd9Sstevel@tonic-gate *bnp = UFS_HOLE; 2857c478bd9Sstevel@tonic-gate brelse(bp); 2867c478bd9Sstevel@tonic-gate return (0); 2877c478bd9Sstevel@tonic-gate } 2887c478bd9Sstevel@tonic-gate if (j != NIADDR) 2897c478bd9Sstevel@tonic-gate brelse(bp); 2907c478bd9Sstevel@tonic-gate } 2917c478bd9Sstevel@tonic-gate DOEXTENT(fs, lbn, boff, bnp, lenp, ip->i_size, &bap[i], 2927c478bd9Sstevel@tonic-gate MIN(NINDIR(fs) - i, (daddr_t)lblkno(fs, ip->i_size - 1) - lbn + 1), 2937c478bd9Sstevel@tonic-gate 0, ufsvfsp->vfs_iotransz); 2947c478bd9Sstevel@tonic-gate brelse(bp); 2957c478bd9Sstevel@tonic-gate return (0); 2967c478bd9Sstevel@tonic-gate } 2977c478bd9Sstevel@tonic-gate 2987c478bd9Sstevel@tonic-gate /* 2997c478bd9Sstevel@tonic-gate * See bmaprd for general notes. 3007c478bd9Sstevel@tonic-gate * 3017c478bd9Sstevel@tonic-gate * The block must be at least size bytes and will be extended or 3027c478bd9Sstevel@tonic-gate * allocated as needed. If alloc_only is set, bmap will not create 3037c478bd9Sstevel@tonic-gate * any in-core pages that correspond to the new disk allocation. 3047c478bd9Sstevel@tonic-gate * Otherwise, the in-core pages will be created and initialized as 3057c478bd9Sstevel@tonic-gate * needed. 3067c478bd9Sstevel@tonic-gate * 3077c478bd9Sstevel@tonic-gate * Returns 0 on success, or a non-zero errno if an error occurs. 3087c478bd9Sstevel@tonic-gate */ 3097c478bd9Sstevel@tonic-gate 3107c478bd9Sstevel@tonic-gate int 3117c478bd9Sstevel@tonic-gate bmap_write( 3127c478bd9Sstevel@tonic-gate struct inode *ip, 3137c478bd9Sstevel@tonic-gate u_offset_t off, 3147c478bd9Sstevel@tonic-gate int size, 3157c478bd9Sstevel@tonic-gate int alloc_only, 3167c478bd9Sstevel@tonic-gate struct cred *cr) 3177c478bd9Sstevel@tonic-gate { 3187c478bd9Sstevel@tonic-gate struct fs *fs; 3197c478bd9Sstevel@tonic-gate struct buf *bp; 3207c478bd9Sstevel@tonic-gate int i; 3217c478bd9Sstevel@tonic-gate struct buf *nbp; 3227c478bd9Sstevel@tonic-gate int j; 3237c478bd9Sstevel@tonic-gate int shft; /* we maintain sh = 1 << shft */ 3247c478bd9Sstevel@tonic-gate daddr_t ob, nb, pref, lbn, llbn, tbn; 3257c478bd9Sstevel@tonic-gate daddr32_t *bap; 3267c478bd9Sstevel@tonic-gate struct vnode *vp = ITOV(ip); 3277c478bd9Sstevel@tonic-gate long bsize = VBSIZE(vp); 3287c478bd9Sstevel@tonic-gate long osize, nsize; 3297c478bd9Sstevel@tonic-gate int issync, metaflag, isdirquota; 3307c478bd9Sstevel@tonic-gate int err; 3317c478bd9Sstevel@tonic-gate dev_t dev; 3327c478bd9Sstevel@tonic-gate struct fbuf *fbp; 3337c478bd9Sstevel@tonic-gate int nindirshift; 3347c478bd9Sstevel@tonic-gate int nindiroffset; 3357c478bd9Sstevel@tonic-gate struct ufsvfs *ufsvfsp; 3367c478bd9Sstevel@tonic-gate int added_sectors; /* sectors added to this inode */ 3377c478bd9Sstevel@tonic-gate int alloced_blocks; /* fs blocks newly allocated */ 3387c478bd9Sstevel@tonic-gate struct ufs_allocated_block undo_table[NIADDR+1]; 3397c478bd9Sstevel@tonic-gate int verylargefile = 0; 3407c478bd9Sstevel@tonic-gate 3417c478bd9Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&ip->i_contents)); 3427c478bd9Sstevel@tonic-gate 3437c478bd9Sstevel@tonic-gate ufsvfsp = ip->i_ufsvfs; 3447c478bd9Sstevel@tonic-gate fs = ufsvfsp->vfs_bufp->b_un.b_fs; 3457c478bd9Sstevel@tonic-gate lbn = (daddr_t)lblkno(fs, off); 3467c478bd9Sstevel@tonic-gate if (lbn < 0) 3477c478bd9Sstevel@tonic-gate return (EFBIG); 3487c478bd9Sstevel@tonic-gate if (ip->i_blocks >= VERYLARGEFILESIZE) 3497c478bd9Sstevel@tonic-gate verylargefile = 1; 3507c478bd9Sstevel@tonic-gate llbn = (daddr_t)((ip->i_size) ? lblkno(fs, ip->i_size - 1) : 0); 3517c478bd9Sstevel@tonic-gate metaflag = isdirquota = 0; 3527c478bd9Sstevel@tonic-gate if (((ip->i_mode & IFMT) == IFDIR) || 3537c478bd9Sstevel@tonic-gate ((ip->i_mode & IFMT) == IFATTRDIR)) 3547c478bd9Sstevel@tonic-gate isdirquota = metaflag = I_DIR; 3557c478bd9Sstevel@tonic-gate else if ((ip->i_mode & IFMT) == IFSHAD) 3567c478bd9Sstevel@tonic-gate metaflag = I_SHAD; 3577c478bd9Sstevel@tonic-gate else if (ip->i_ufsvfs->vfs_qinod == ip) 3587c478bd9Sstevel@tonic-gate isdirquota = metaflag = I_QUOTA; 3597c478bd9Sstevel@tonic-gate 3607c478bd9Sstevel@tonic-gate issync = ((ip->i_flag & ISYNC) != 0); 3617c478bd9Sstevel@tonic-gate 3627c478bd9Sstevel@tonic-gate if (isdirquota || issync) { 3637c478bd9Sstevel@tonic-gate alloc_only = 0; /* make sure */ 3647c478bd9Sstevel@tonic-gate } 3657c478bd9Sstevel@tonic-gate 3667c478bd9Sstevel@tonic-gate /* 3677c478bd9Sstevel@tonic-gate * If the next write will extend the file into a new block, 3687c478bd9Sstevel@tonic-gate * and the file is currently composed of a fragment 3697c478bd9Sstevel@tonic-gate * this fragment has to be extended to be a full block. 3707c478bd9Sstevel@tonic-gate */ 3717c478bd9Sstevel@tonic-gate if (llbn < NDADDR && llbn < lbn && (ob = ip->i_db[llbn]) != 0) { 3727c478bd9Sstevel@tonic-gate osize = blksize(fs, ip, llbn); 3737c478bd9Sstevel@tonic-gate if (osize < bsize && osize > 0) { 3747c478bd9Sstevel@tonic-gate /* 3757c478bd9Sstevel@tonic-gate * Check to see if doing this will make the file too 3767c478bd9Sstevel@tonic-gate * big. Only check if we are dealing with a very 3777c478bd9Sstevel@tonic-gate * large file. 3787c478bd9Sstevel@tonic-gate */ 3797c478bd9Sstevel@tonic-gate if (verylargefile == 1) { 3807c478bd9Sstevel@tonic-gate if (((unsigned)ip->i_blocks + 3817c478bd9Sstevel@tonic-gate btodb(bsize - osize)) > INT_MAX) { 3827c478bd9Sstevel@tonic-gate return (EFBIG); 3837c478bd9Sstevel@tonic-gate } 3847c478bd9Sstevel@tonic-gate } 3857c478bd9Sstevel@tonic-gate /* 3867c478bd9Sstevel@tonic-gate * Make sure we have all needed pages setup correctly. 3877c478bd9Sstevel@tonic-gate * 3887c478bd9Sstevel@tonic-gate * We pass S_OTHER to fbread here because we want 3897c478bd9Sstevel@tonic-gate * an exclusive lock on the page in question 3907c478bd9Sstevel@tonic-gate * (see ufs_getpage). I/O to the old block location 3917c478bd9Sstevel@tonic-gate * may still be in progress and we are about to free 3927c478bd9Sstevel@tonic-gate * the old block. We don't want anyone else to get 3937c478bd9Sstevel@tonic-gate * a hold of the old block once we free it until 3947c478bd9Sstevel@tonic-gate * the I/O is complete. 3957c478bd9Sstevel@tonic-gate */ 3967c478bd9Sstevel@tonic-gate err = fbread(ITOV(ip), 3977c478bd9Sstevel@tonic-gate ((offset_t)llbn << fs->fs_bshift), 3987c478bd9Sstevel@tonic-gate (uint_t)bsize, S_OTHER, &fbp); 3997c478bd9Sstevel@tonic-gate if (err) 4007c478bd9Sstevel@tonic-gate return (err); 4017c478bd9Sstevel@tonic-gate pref = blkpref(ip, llbn, (int)llbn, &ip->i_db[0]); 4027c478bd9Sstevel@tonic-gate err = realloccg(ip, ob, pref, (int)osize, (int)bsize, 4037c478bd9Sstevel@tonic-gate &nb, cr); 4047c478bd9Sstevel@tonic-gate if (err) { 4057c478bd9Sstevel@tonic-gate if (fbp) 4067c478bd9Sstevel@tonic-gate fbrelse(fbp, S_OTHER); 4077c478bd9Sstevel@tonic-gate return (err); 4087c478bd9Sstevel@tonic-gate } 4097c478bd9Sstevel@tonic-gate ASSERT(!ufs_badblock(ip, nb)); 4107c478bd9Sstevel@tonic-gate 4117c478bd9Sstevel@tonic-gate /* 4127c478bd9Sstevel@tonic-gate * Update the inode before releasing the 4137c478bd9Sstevel@tonic-gate * lock on the page. If we released the page 4147c478bd9Sstevel@tonic-gate * lock first, the data could be written to it's 4157c478bd9Sstevel@tonic-gate * old address and then destroyed. 4167c478bd9Sstevel@tonic-gate */ 4177c478bd9Sstevel@tonic-gate TRANS_MATA_ALLOC(ufsvfsp, ip, nb, bsize, 0); 4187c478bd9Sstevel@tonic-gate ip->i_db[llbn] = nb; 4197c478bd9Sstevel@tonic-gate UFS_SET_ISIZE(((u_offset_t)(llbn + 1)) << fs->fs_bshift, 4207c478bd9Sstevel@tonic-gate ip); 4217c478bd9Sstevel@tonic-gate ip->i_blocks += btodb(bsize - osize); 4227c478bd9Sstevel@tonic-gate ASSERT((unsigned)ip->i_blocks <= INT_MAX); 4237c478bd9Sstevel@tonic-gate TRANS_INODE(ufsvfsp, ip); 4247c478bd9Sstevel@tonic-gate ip->i_flag |= IUPD | ICHG | IATTCHG; 4257c478bd9Sstevel@tonic-gate /* Caller is responsible for updating i_seq */ 4267c478bd9Sstevel@tonic-gate /* 4277c478bd9Sstevel@tonic-gate * Don't check metaflag here, directories won't do this 4287c478bd9Sstevel@tonic-gate * 4297c478bd9Sstevel@tonic-gate */ 4307c478bd9Sstevel@tonic-gate if (issync) { 4317c478bd9Sstevel@tonic-gate (void) ufs_fbiwrite(fbp, ip, nb, fs->fs_fsize); 4327c478bd9Sstevel@tonic-gate } else { 4337c478bd9Sstevel@tonic-gate ASSERT(fbp); 4347c478bd9Sstevel@tonic-gate fbrelse(fbp, S_WRITE); 4357c478bd9Sstevel@tonic-gate } 4367c478bd9Sstevel@tonic-gate 4377c478bd9Sstevel@tonic-gate if (nb != ob) { 4387c478bd9Sstevel@tonic-gate (void) free(ip, ob, (off_t)osize, metaflag); 4397c478bd9Sstevel@tonic-gate } 4407c478bd9Sstevel@tonic-gate } 4417c478bd9Sstevel@tonic-gate } 4427c478bd9Sstevel@tonic-gate 4437c478bd9Sstevel@tonic-gate /* 4447c478bd9Sstevel@tonic-gate * The first NDADDR blocks are direct blocks. 4457c478bd9Sstevel@tonic-gate */ 4467c478bd9Sstevel@tonic-gate if (lbn < NDADDR) { 4477c478bd9Sstevel@tonic-gate nb = ip->i_db[lbn]; 4487c478bd9Sstevel@tonic-gate if (nb == 0 || 4497c478bd9Sstevel@tonic-gate ip->i_size < ((u_offset_t)(lbn + 1)) << fs->fs_bshift) { 4507c478bd9Sstevel@tonic-gate if (nb != 0) { 4517c478bd9Sstevel@tonic-gate /* consider need to reallocate a frag */ 4527c478bd9Sstevel@tonic-gate osize = fragroundup(fs, blkoff(fs, ip->i_size)); 4537c478bd9Sstevel@tonic-gate nsize = fragroundup(fs, size); 4547c478bd9Sstevel@tonic-gate if (nsize <= osize) 4557c478bd9Sstevel@tonic-gate goto gotit; 4567c478bd9Sstevel@tonic-gate /* 4577c478bd9Sstevel@tonic-gate * Check to see if doing this will make the 4587c478bd9Sstevel@tonic-gate * file too big. Only check if we are dealing 4597c478bd9Sstevel@tonic-gate * with a very large file. 4607c478bd9Sstevel@tonic-gate */ 4617c478bd9Sstevel@tonic-gate if (verylargefile == 1) { 4627c478bd9Sstevel@tonic-gate if (((unsigned)ip->i_blocks + 4637c478bd9Sstevel@tonic-gate btodb(nsize - osize)) > INT_MAX) { 4647c478bd9Sstevel@tonic-gate return (EFBIG); 4657c478bd9Sstevel@tonic-gate } 4667c478bd9Sstevel@tonic-gate } 4677c478bd9Sstevel@tonic-gate /* 4687c478bd9Sstevel@tonic-gate * need to allocate a block or frag 4697c478bd9Sstevel@tonic-gate */ 4707c478bd9Sstevel@tonic-gate ob = nb; 4717c478bd9Sstevel@tonic-gate pref = blkpref(ip, lbn, (int)lbn, 4727c478bd9Sstevel@tonic-gate &ip->i_db[0]); 4737c478bd9Sstevel@tonic-gate err = realloccg(ip, ob, pref, (int)osize, 4747c478bd9Sstevel@tonic-gate (int)nsize, &nb, cr); 4757c478bd9Sstevel@tonic-gate if (err) 4767c478bd9Sstevel@tonic-gate return (err); 4777c478bd9Sstevel@tonic-gate ASSERT(!ufs_badblock(ip, nb)); 4787c478bd9Sstevel@tonic-gate 4797c478bd9Sstevel@tonic-gate } else { 4807c478bd9Sstevel@tonic-gate /* 4817c478bd9Sstevel@tonic-gate * need to allocate a block or frag 4827c478bd9Sstevel@tonic-gate */ 4837c478bd9Sstevel@tonic-gate osize = 0; 4847c478bd9Sstevel@tonic-gate if (ip->i_size < 4857c478bd9Sstevel@tonic-gate ((u_offset_t)(lbn + 1)) << fs->fs_bshift) 4867c478bd9Sstevel@tonic-gate nsize = fragroundup(fs, size); 4877c478bd9Sstevel@tonic-gate else 4887c478bd9Sstevel@tonic-gate nsize = bsize; 4897c478bd9Sstevel@tonic-gate /* 4907c478bd9Sstevel@tonic-gate * Check to see if doing this will make the 4917c478bd9Sstevel@tonic-gate * file too big. Only check if we are dealing 4927c478bd9Sstevel@tonic-gate * with a very large file. 4937c478bd9Sstevel@tonic-gate */ 4947c478bd9Sstevel@tonic-gate if (verylargefile == 1) { 4957c478bd9Sstevel@tonic-gate if (((unsigned)ip->i_blocks + 4967c478bd9Sstevel@tonic-gate btodb(nsize - osize)) > INT_MAX) { 4977c478bd9Sstevel@tonic-gate return (EFBIG); 4987c478bd9Sstevel@tonic-gate } 4997c478bd9Sstevel@tonic-gate } 5007c478bd9Sstevel@tonic-gate pref = blkpref(ip, lbn, (int)lbn, &ip->i_db[0]); 5017c478bd9Sstevel@tonic-gate err = alloc(ip, pref, (int)nsize, &nb, cr); 5027c478bd9Sstevel@tonic-gate if (err) 5037c478bd9Sstevel@tonic-gate return (err); 5047c478bd9Sstevel@tonic-gate ASSERT(!ufs_badblock(ip, nb)); 5057c478bd9Sstevel@tonic-gate ob = nb; 5067c478bd9Sstevel@tonic-gate } 5077c478bd9Sstevel@tonic-gate 5087c478bd9Sstevel@tonic-gate /* 5097c478bd9Sstevel@tonic-gate * Read old/create new zero pages 5107c478bd9Sstevel@tonic-gate */ 5117c478bd9Sstevel@tonic-gate fbp = NULL; 5127c478bd9Sstevel@tonic-gate if (osize == 0) { 5137c478bd9Sstevel@tonic-gate /* 5147c478bd9Sstevel@tonic-gate * mmap S_WRITE faults always enter here 5157c478bd9Sstevel@tonic-gate */ 5167c478bd9Sstevel@tonic-gate if (!alloc_only || P2ROUNDUP_TYPED(size, 5177c478bd9Sstevel@tonic-gate PAGESIZE, u_offset_t) < nsize) { 5187c478bd9Sstevel@tonic-gate /* fbzero doesn't cause a pagefault */ 5197c478bd9Sstevel@tonic-gate fbzero(ITOV(ip), 5207c478bd9Sstevel@tonic-gate ((offset_t)lbn << fs->fs_bshift), 5217c478bd9Sstevel@tonic-gate (uint_t)nsize, &fbp); 5227c478bd9Sstevel@tonic-gate } 5237c478bd9Sstevel@tonic-gate } else { 5247c478bd9Sstevel@tonic-gate err = fbread(vp, 5257c478bd9Sstevel@tonic-gate ((offset_t)lbn << fs->fs_bshift), 5267c478bd9Sstevel@tonic-gate (uint_t)nsize, S_OTHER, &fbp); 5277c478bd9Sstevel@tonic-gate if (err) { 5287c478bd9Sstevel@tonic-gate if (nb != ob) { 5297c478bd9Sstevel@tonic-gate (void) free(ip, nb, 5307c478bd9Sstevel@tonic-gate (off_t)nsize, metaflag); 5317c478bd9Sstevel@tonic-gate } else { 5327c478bd9Sstevel@tonic-gate (void) free(ip, 5337c478bd9Sstevel@tonic-gate ob + numfrags(fs, osize), 5347c478bd9Sstevel@tonic-gate (off_t)(nsize - osize), 5357c478bd9Sstevel@tonic-gate metaflag); 5367c478bd9Sstevel@tonic-gate } 5377c478bd9Sstevel@tonic-gate ASSERT(nsize >= osize); 5387c478bd9Sstevel@tonic-gate (void) chkdq(ip, 5397c478bd9Sstevel@tonic-gate -(long)btodb(nsize - osize), 5407c478bd9Sstevel@tonic-gate 0, cr, (char **)NULL, 5417c478bd9Sstevel@tonic-gate (size_t *)NULL); 5427c478bd9Sstevel@tonic-gate return (err); 5437c478bd9Sstevel@tonic-gate } 5447c478bd9Sstevel@tonic-gate } 5457c478bd9Sstevel@tonic-gate TRANS_MATA_ALLOC(ufsvfsp, ip, nb, nsize, 0); 5467c478bd9Sstevel@tonic-gate ip->i_db[lbn] = nb; 5477c478bd9Sstevel@tonic-gate ip->i_blocks += btodb(nsize - osize); 5487c478bd9Sstevel@tonic-gate ASSERT((unsigned)ip->i_blocks <= INT_MAX); 5497c478bd9Sstevel@tonic-gate TRANS_INODE(ufsvfsp, ip); 5507c478bd9Sstevel@tonic-gate ip->i_flag |= IUPD | ICHG | IATTCHG; 5517c478bd9Sstevel@tonic-gate /* Caller is responsible for updating i_seq */ 5527c478bd9Sstevel@tonic-gate 5537c478bd9Sstevel@tonic-gate /* 5547c478bd9Sstevel@tonic-gate * Write directory and shadow blocks synchronously so 5557c478bd9Sstevel@tonic-gate * that they never appear with garbage in them on the 5567c478bd9Sstevel@tonic-gate * disk. 5577c478bd9Sstevel@tonic-gate * 5587c478bd9Sstevel@tonic-gate */ 5597c478bd9Sstevel@tonic-gate if (isdirquota && (ip->i_size || 5607c478bd9Sstevel@tonic-gate TRANS_ISTRANS(ufsvfsp))) { 5617c478bd9Sstevel@tonic-gate /* 5627c478bd9Sstevel@tonic-gate * XXX man not be necessary with harpy trans 5637c478bd9Sstevel@tonic-gate * bug id 1130055 5647c478bd9Sstevel@tonic-gate */ 5657c478bd9Sstevel@tonic-gate (void) ufs_fbiwrite(fbp, ip, nb, fs->fs_fsize); 5667c478bd9Sstevel@tonic-gate } else if (fbp) { 5677c478bd9Sstevel@tonic-gate fbrelse(fbp, S_WRITE); 5687c478bd9Sstevel@tonic-gate } 5697c478bd9Sstevel@tonic-gate 5707c478bd9Sstevel@tonic-gate if (nb != ob) 5717c478bd9Sstevel@tonic-gate (void) free(ip, ob, (off_t)osize, metaflag); 5727c478bd9Sstevel@tonic-gate } 5737c478bd9Sstevel@tonic-gate gotit: 5747c478bd9Sstevel@tonic-gate return (0); 5757c478bd9Sstevel@tonic-gate } 5767c478bd9Sstevel@tonic-gate 5777c478bd9Sstevel@tonic-gate added_sectors = alloced_blocks = 0; /* No blocks alloced yet */ 5787c478bd9Sstevel@tonic-gate 5797c478bd9Sstevel@tonic-gate /* 5807c478bd9Sstevel@tonic-gate * Determine how many levels of indirection. 5817c478bd9Sstevel@tonic-gate */ 5827c478bd9Sstevel@tonic-gate nindirshift = ip->i_ufsvfs->vfs_nindirshift; 5837c478bd9Sstevel@tonic-gate nindiroffset = ip->i_ufsvfs->vfs_nindiroffset; 5847c478bd9Sstevel@tonic-gate pref = 0; 5857c478bd9Sstevel@tonic-gate shft = 0; /* sh = 1 */ 5867c478bd9Sstevel@tonic-gate tbn = lbn - NDADDR; 5877c478bd9Sstevel@tonic-gate for (j = NIADDR; j > 0; j--) { 5887c478bd9Sstevel@tonic-gate longlong_t sh; 5897c478bd9Sstevel@tonic-gate 5907c478bd9Sstevel@tonic-gate shft += nindirshift; /* sh *= nindir */ 5917c478bd9Sstevel@tonic-gate sh = 1LL << shft; 5927c478bd9Sstevel@tonic-gate if (tbn < sh) 5937c478bd9Sstevel@tonic-gate break; 5947c478bd9Sstevel@tonic-gate tbn -= sh; 5957c478bd9Sstevel@tonic-gate } 5967c478bd9Sstevel@tonic-gate 5977c478bd9Sstevel@tonic-gate if (j == 0) 5987c478bd9Sstevel@tonic-gate return (EFBIG); 5997c478bd9Sstevel@tonic-gate 6007c478bd9Sstevel@tonic-gate /* 6017c478bd9Sstevel@tonic-gate * Fetch the first indirect block. 6027c478bd9Sstevel@tonic-gate */ 6037c478bd9Sstevel@tonic-gate dev = ip->i_dev; 6047c478bd9Sstevel@tonic-gate nb = ip->i_ib[NIADDR - j]; 6057c478bd9Sstevel@tonic-gate if (nb == 0) { 6067c478bd9Sstevel@tonic-gate /* 6077c478bd9Sstevel@tonic-gate * Check to see if doing this will make the 6087c478bd9Sstevel@tonic-gate * file too big. Only check if we are dealing 6097c478bd9Sstevel@tonic-gate * with a very large file. 6107c478bd9Sstevel@tonic-gate */ 6117c478bd9Sstevel@tonic-gate if (verylargefile == 1) { 6127c478bd9Sstevel@tonic-gate if (((unsigned)ip->i_blocks + btodb(bsize)) 6137c478bd9Sstevel@tonic-gate > INT_MAX) { 6147c478bd9Sstevel@tonic-gate return (EFBIG); 6157c478bd9Sstevel@tonic-gate } 6167c478bd9Sstevel@tonic-gate } 6177c478bd9Sstevel@tonic-gate /* 6187c478bd9Sstevel@tonic-gate * Need to allocate an indirect block. 6197c478bd9Sstevel@tonic-gate */ 6207c478bd9Sstevel@tonic-gate pref = blkpref(ip, lbn, 0, (daddr32_t *)0); 6217c478bd9Sstevel@tonic-gate err = alloc(ip, pref, (int)bsize, &nb, cr); 6227c478bd9Sstevel@tonic-gate if (err) 6237c478bd9Sstevel@tonic-gate return (err); 6247c478bd9Sstevel@tonic-gate TRANS_MATA_ALLOC(ufsvfsp, ip, nb, bsize, 1); 6257c478bd9Sstevel@tonic-gate ASSERT(!ufs_badblock(ip, nb)); 6267c478bd9Sstevel@tonic-gate 6277c478bd9Sstevel@tonic-gate /* 6287c478bd9Sstevel@tonic-gate * Keep track of this allocation so we can undo it if we 6297c478bd9Sstevel@tonic-gate * get an error later. 6307c478bd9Sstevel@tonic-gate */ 6317c478bd9Sstevel@tonic-gate 6327c478bd9Sstevel@tonic-gate ASSERT(alloced_blocks <= NIADDR); 6337c478bd9Sstevel@tonic-gate 6347c478bd9Sstevel@tonic-gate undo_table[alloced_blocks].this_block = nb; 6357c478bd9Sstevel@tonic-gate undo_table[alloced_blocks].block_size = bsize; 6367c478bd9Sstevel@tonic-gate undo_table[alloced_blocks].owner = ufs_no_owner; 6377c478bd9Sstevel@tonic-gate undo_table[alloced_blocks].usage_flags = metaflag | I_IBLK; 6387c478bd9Sstevel@tonic-gate 6397c478bd9Sstevel@tonic-gate alloced_blocks++; 6407c478bd9Sstevel@tonic-gate 6417c478bd9Sstevel@tonic-gate /* 6427c478bd9Sstevel@tonic-gate * Write zero block synchronously so that 6437c478bd9Sstevel@tonic-gate * indirect blocks never point at garbage. 6447c478bd9Sstevel@tonic-gate */ 6457c478bd9Sstevel@tonic-gate bp = UFS_GETBLK(ufsvfsp, dev, fsbtodb(fs, nb), bsize); 6467c478bd9Sstevel@tonic-gate 6477c478bd9Sstevel@tonic-gate clrbuf(bp); 6487c478bd9Sstevel@tonic-gate /* XXX Maybe special-case this? */ 6497c478bd9Sstevel@tonic-gate TRANS_BUF(ufsvfsp, 0, bsize, bp, DT_ABZERO); 6507c478bd9Sstevel@tonic-gate UFS_BWRITE2(ufsvfsp, bp); 6517c478bd9Sstevel@tonic-gate if (bp->b_flags & B_ERROR) { 6527c478bd9Sstevel@tonic-gate err = geterror(bp); 6537c478bd9Sstevel@tonic-gate brelse(bp); 6547c478bd9Sstevel@tonic-gate ufs_undo_allocation(ip, alloced_blocks, 6557c478bd9Sstevel@tonic-gate undo_table, added_sectors); 6567c478bd9Sstevel@tonic-gate return (err); 6577c478bd9Sstevel@tonic-gate } 6587c478bd9Sstevel@tonic-gate brelse(bp); 6597c478bd9Sstevel@tonic-gate 6607c478bd9Sstevel@tonic-gate ip->i_ib[NIADDR - j] = nb; 6617c478bd9Sstevel@tonic-gate added_sectors += btodb(bsize); 6627c478bd9Sstevel@tonic-gate ip->i_blocks += btodb(bsize); 6637c478bd9Sstevel@tonic-gate ASSERT((unsigned)ip->i_blocks <= INT_MAX); 6647c478bd9Sstevel@tonic-gate TRANS_INODE(ufsvfsp, ip); 6657c478bd9Sstevel@tonic-gate ip->i_flag |= IUPD | ICHG | IATTCHG; 6667c478bd9Sstevel@tonic-gate /* Caller is responsible for updating i_seq */ 6677c478bd9Sstevel@tonic-gate 6687c478bd9Sstevel@tonic-gate /* 6697c478bd9Sstevel@tonic-gate * Update the 'undo table' now that we've linked this block 6707c478bd9Sstevel@tonic-gate * to an inode. 6717c478bd9Sstevel@tonic-gate */ 6727c478bd9Sstevel@tonic-gate 6737c478bd9Sstevel@tonic-gate undo_table[alloced_blocks-1].owner = ufs_inode_indirect; 6747c478bd9Sstevel@tonic-gate undo_table[alloced_blocks-1].owner_offset = NIADDR - j; 6757c478bd9Sstevel@tonic-gate 6767c478bd9Sstevel@tonic-gate /* 6777c478bd9Sstevel@tonic-gate * In the ISYNC case, wrip will notice that the block 6787c478bd9Sstevel@tonic-gate * count on the inode has changed and will be sure to 6797c478bd9Sstevel@tonic-gate * ufs_iupdat the inode at the end of wrip. 6807c478bd9Sstevel@tonic-gate */ 6817c478bd9Sstevel@tonic-gate } 6827c478bd9Sstevel@tonic-gate 6837c478bd9Sstevel@tonic-gate /* 6847c478bd9Sstevel@tonic-gate * Fetch through the indirect blocks. 6857c478bd9Sstevel@tonic-gate */ 6867c478bd9Sstevel@tonic-gate for (; j <= NIADDR; j++) { 6877c478bd9Sstevel@tonic-gate ob = nb; 6887c478bd9Sstevel@tonic-gate bp = UFS_BREAD(ufsvfsp, ip->i_dev, fsbtodb(fs, ob), bsize); 6897c478bd9Sstevel@tonic-gate 6907c478bd9Sstevel@tonic-gate if (bp->b_flags & B_ERROR) { 6917c478bd9Sstevel@tonic-gate err = geterror(bp); 6927c478bd9Sstevel@tonic-gate brelse(bp); 6937c478bd9Sstevel@tonic-gate /* 6947c478bd9Sstevel@tonic-gate * Return any partial allocations. 6957c478bd9Sstevel@tonic-gate * 6967c478bd9Sstevel@tonic-gate * It is possible that we have not yet made any 6977c478bd9Sstevel@tonic-gate * allocations at this point (if this is the first 6987c478bd9Sstevel@tonic-gate * pass through the loop and we didn't have to 6997c478bd9Sstevel@tonic-gate * allocate the first indirect block, above). 7007c478bd9Sstevel@tonic-gate * In this case, alloced_blocks and added_sectors will 7017c478bd9Sstevel@tonic-gate * be zero, and ufs_undo_allocation will do nothing. 7027c478bd9Sstevel@tonic-gate */ 7037c478bd9Sstevel@tonic-gate ufs_undo_allocation(ip, alloced_blocks, 7047c478bd9Sstevel@tonic-gate undo_table, added_sectors); 7057c478bd9Sstevel@tonic-gate return (err); 7067c478bd9Sstevel@tonic-gate } 7077c478bd9Sstevel@tonic-gate bap = bp->b_un.b_daddr; 7087c478bd9Sstevel@tonic-gate shft -= nindirshift; /* sh /= nindir */ 7097c478bd9Sstevel@tonic-gate i = (tbn >> shft) & nindiroffset; /* (tbn / sh) % nindir */ 7107c478bd9Sstevel@tonic-gate nb = bap[i]; 7117c478bd9Sstevel@tonic-gate if (nb == 0) { 7127c478bd9Sstevel@tonic-gate /* 7137c478bd9Sstevel@tonic-gate * Check to see if doing this will make the 7147c478bd9Sstevel@tonic-gate * file too big. Only check if we are dealing 7157c478bd9Sstevel@tonic-gate * with a very large file. 7167c478bd9Sstevel@tonic-gate */ 7177c478bd9Sstevel@tonic-gate if (verylargefile == 1) { 7187c478bd9Sstevel@tonic-gate if (((unsigned)ip->i_blocks + btodb(bsize)) 7197c478bd9Sstevel@tonic-gate > INT_MAX) { 7207c478bd9Sstevel@tonic-gate brelse(bp); 7217c478bd9Sstevel@tonic-gate ufs_undo_allocation(ip, alloced_blocks, 7227c478bd9Sstevel@tonic-gate undo_table, added_sectors); 7237c478bd9Sstevel@tonic-gate return (EFBIG); 7247c478bd9Sstevel@tonic-gate } 7257c478bd9Sstevel@tonic-gate } 7267c478bd9Sstevel@tonic-gate if (pref == 0) { 7277c478bd9Sstevel@tonic-gate if (j < NIADDR) { 7287c478bd9Sstevel@tonic-gate /* Indirect block */ 7297c478bd9Sstevel@tonic-gate pref = blkpref(ip, lbn, 0, 7307c478bd9Sstevel@tonic-gate (daddr32_t *)0); 7317c478bd9Sstevel@tonic-gate } else { 7327c478bd9Sstevel@tonic-gate /* Data block */ 7337c478bd9Sstevel@tonic-gate pref = blkpref(ip, lbn, i, &bap[0]); 7347c478bd9Sstevel@tonic-gate } 7357c478bd9Sstevel@tonic-gate } 7367c478bd9Sstevel@tonic-gate 7377c478bd9Sstevel@tonic-gate /* 7387c478bd9Sstevel@tonic-gate * release "bp" buf to avoid deadlock (re-bread later) 7397c478bd9Sstevel@tonic-gate */ 7407c478bd9Sstevel@tonic-gate brelse(bp); 7417c478bd9Sstevel@tonic-gate 7427c478bd9Sstevel@tonic-gate err = alloc(ip, pref, (int)bsize, &nb, cr); 7437c478bd9Sstevel@tonic-gate if (err) { 7447c478bd9Sstevel@tonic-gate /* 7457c478bd9Sstevel@tonic-gate * Return any partial allocations. 7467c478bd9Sstevel@tonic-gate */ 7477c478bd9Sstevel@tonic-gate ufs_undo_allocation(ip, alloced_blocks, 7487c478bd9Sstevel@tonic-gate undo_table, added_sectors); 7497c478bd9Sstevel@tonic-gate return (err); 7507c478bd9Sstevel@tonic-gate } 7517c478bd9Sstevel@tonic-gate 7527c478bd9Sstevel@tonic-gate ASSERT(!ufs_badblock(ip, nb)); 7537c478bd9Sstevel@tonic-gate 7547c478bd9Sstevel@tonic-gate ASSERT(alloced_blocks <= NIADDR); 7557c478bd9Sstevel@tonic-gate 7567c478bd9Sstevel@tonic-gate undo_table[alloced_blocks].this_block = nb; 7577c478bd9Sstevel@tonic-gate undo_table[alloced_blocks].block_size = bsize; 7587c478bd9Sstevel@tonic-gate undo_table[alloced_blocks].owner = ufs_no_owner; 7597c478bd9Sstevel@tonic-gate undo_table[alloced_blocks].usage_flags = metaflag | 7607c478bd9Sstevel@tonic-gate ((j < NIADDR) ? I_IBLK : 0); 7617c478bd9Sstevel@tonic-gate 7627c478bd9Sstevel@tonic-gate alloced_blocks++; 7637c478bd9Sstevel@tonic-gate 7647c478bd9Sstevel@tonic-gate if (j < NIADDR) { 7657c478bd9Sstevel@tonic-gate TRANS_MATA_ALLOC(ufsvfsp, ip, nb, bsize, 1); 7667c478bd9Sstevel@tonic-gate /* 7677c478bd9Sstevel@tonic-gate * Write synchronously so indirect 7687c478bd9Sstevel@tonic-gate * blocks never point at garbage. 7697c478bd9Sstevel@tonic-gate */ 7707c478bd9Sstevel@tonic-gate nbp = UFS_GETBLK( 7717c478bd9Sstevel@tonic-gate ufsvfsp, dev, fsbtodb(fs, nb), bsize); 7727c478bd9Sstevel@tonic-gate 7737c478bd9Sstevel@tonic-gate clrbuf(nbp); 7747c478bd9Sstevel@tonic-gate /* XXX Maybe special-case this? */ 7757c478bd9Sstevel@tonic-gate TRANS_BUF(ufsvfsp, 0, bsize, nbp, DT_ABZERO); 7767c478bd9Sstevel@tonic-gate UFS_BWRITE2(ufsvfsp, nbp); 7777c478bd9Sstevel@tonic-gate if (nbp->b_flags & B_ERROR) { 7787c478bd9Sstevel@tonic-gate err = geterror(nbp); 7797c478bd9Sstevel@tonic-gate brelse(nbp); 7807c478bd9Sstevel@tonic-gate /* 7817c478bd9Sstevel@tonic-gate * Return any partial 7827c478bd9Sstevel@tonic-gate * allocations. 7837c478bd9Sstevel@tonic-gate */ 7847c478bd9Sstevel@tonic-gate ufs_undo_allocation(ip, 7857c478bd9Sstevel@tonic-gate alloced_blocks, 7867c478bd9Sstevel@tonic-gate undo_table, added_sectors); 7877c478bd9Sstevel@tonic-gate return (err); 7887c478bd9Sstevel@tonic-gate } 7897c478bd9Sstevel@tonic-gate brelse(nbp); 7907c478bd9Sstevel@tonic-gate } else if (!alloc_only || P2ROUNDUP_TYPED(size, 7917c478bd9Sstevel@tonic-gate PAGESIZE, u_offset_t) < bsize) { 7927c478bd9Sstevel@tonic-gate TRANS_MATA_ALLOC(ufsvfsp, ip, nb, bsize, 0); 7937c478bd9Sstevel@tonic-gate fbzero(ITOV(ip), 7947c478bd9Sstevel@tonic-gate ((offset_t)lbn << fs->fs_bshift), 7957c478bd9Sstevel@tonic-gate (uint_t)bsize, &fbp); 7967c478bd9Sstevel@tonic-gate 7977c478bd9Sstevel@tonic-gate /* 7987c478bd9Sstevel@tonic-gate * Cases which we need to do a synchronous 7997c478bd9Sstevel@tonic-gate * write of the zeroed data pages: 8007c478bd9Sstevel@tonic-gate * 8017c478bd9Sstevel@tonic-gate * 1) If we are writing a directory then we 8027c478bd9Sstevel@tonic-gate * want to write synchronously so blocks in 8037c478bd9Sstevel@tonic-gate * directories never contain garbage. 8047c478bd9Sstevel@tonic-gate * 8057c478bd9Sstevel@tonic-gate * 2) If we are filling in a hole and the 8067c478bd9Sstevel@tonic-gate * indirect block is going to be synchronously 8077c478bd9Sstevel@tonic-gate * written back below we need to make sure 8087c478bd9Sstevel@tonic-gate * that the zeroes are written here before 8097c478bd9Sstevel@tonic-gate * the indirect block is updated so that if 8107c478bd9Sstevel@tonic-gate * we crash before the real data is pushed 8117c478bd9Sstevel@tonic-gate * we will not end up with random data is 8127c478bd9Sstevel@tonic-gate * the middle of the file. 8137c478bd9Sstevel@tonic-gate * 8147c478bd9Sstevel@tonic-gate * 3) If the size of the request rounded up 8157c478bd9Sstevel@tonic-gate * to the system page size is smaller than 8167c478bd9Sstevel@tonic-gate * the file system block size, we want to 8177c478bd9Sstevel@tonic-gate * write out all the pages now so that 8187c478bd9Sstevel@tonic-gate * they are not aborted before they actually 8197c478bd9Sstevel@tonic-gate * make it to ufs_putpage since the length 8207c478bd9Sstevel@tonic-gate * of the inode will not include the pages. 8217c478bd9Sstevel@tonic-gate */ 8227c478bd9Sstevel@tonic-gate 8237c478bd9Sstevel@tonic-gate if (isdirquota || (issync && 8247c478bd9Sstevel@tonic-gate lbn < llbn)) 8257c478bd9Sstevel@tonic-gate (void) ufs_fbiwrite(fbp, ip, nb, 8267c478bd9Sstevel@tonic-gate fs->fs_fsize); 8277c478bd9Sstevel@tonic-gate else 8287c478bd9Sstevel@tonic-gate fbrelse(fbp, S_WRITE); 8297c478bd9Sstevel@tonic-gate } 8307c478bd9Sstevel@tonic-gate 8317c478bd9Sstevel@tonic-gate /* 8327c478bd9Sstevel@tonic-gate * re-acquire "bp" buf 8337c478bd9Sstevel@tonic-gate */ 8347c478bd9Sstevel@tonic-gate bp = UFS_BREAD(ufsvfsp, 8357c478bd9Sstevel@tonic-gate ip->i_dev, fsbtodb(fs, ob), bsize); 8367c478bd9Sstevel@tonic-gate if (bp->b_flags & B_ERROR) { 8377c478bd9Sstevel@tonic-gate err = geterror(bp); 8387c478bd9Sstevel@tonic-gate brelse(bp); 8397c478bd9Sstevel@tonic-gate /* 8407c478bd9Sstevel@tonic-gate * Return any partial allocations. 8417c478bd9Sstevel@tonic-gate */ 8427c478bd9Sstevel@tonic-gate ufs_undo_allocation(ip, 8437c478bd9Sstevel@tonic-gate alloced_blocks, 8447c478bd9Sstevel@tonic-gate undo_table, added_sectors); 8457c478bd9Sstevel@tonic-gate return (err); 8467c478bd9Sstevel@tonic-gate } 8477c478bd9Sstevel@tonic-gate bap = bp->b_un.b_daddr; 8487c478bd9Sstevel@tonic-gate bap[i] = nb; 8497c478bd9Sstevel@tonic-gate TRANS_BUF_ITEM_128(ufsvfsp, bap[i], bap, bp, DT_AB); 8507c478bd9Sstevel@tonic-gate added_sectors += btodb(bsize); 8517c478bd9Sstevel@tonic-gate ip->i_blocks += btodb(bsize); 8527c478bd9Sstevel@tonic-gate ASSERT((unsigned)ip->i_blocks <= INT_MAX); 8537c478bd9Sstevel@tonic-gate TRANS_INODE(ufsvfsp, ip); 8547c478bd9Sstevel@tonic-gate ip->i_flag |= IUPD | ICHG | IATTCHG; 8557c478bd9Sstevel@tonic-gate /* Caller is responsible for updating i_seq */ 8567c478bd9Sstevel@tonic-gate 8577c478bd9Sstevel@tonic-gate undo_table[alloced_blocks-1].owner = 8587c478bd9Sstevel@tonic-gate ufs_indirect_block; 8597c478bd9Sstevel@tonic-gate undo_table[alloced_blocks-1].owner_block = ob; 8607c478bd9Sstevel@tonic-gate undo_table[alloced_blocks-1].owner_offset = i; 8617c478bd9Sstevel@tonic-gate 8627c478bd9Sstevel@tonic-gate if (issync) { 8637c478bd9Sstevel@tonic-gate UFS_BWRITE2(ufsvfsp, bp); 8647c478bd9Sstevel@tonic-gate if (bp->b_flags & B_ERROR) { 8657c478bd9Sstevel@tonic-gate err = geterror(bp); 8667c478bd9Sstevel@tonic-gate brelse(bp); 8677c478bd9Sstevel@tonic-gate /* 8687c478bd9Sstevel@tonic-gate * Return any partial 8697c478bd9Sstevel@tonic-gate * allocations. 8707c478bd9Sstevel@tonic-gate */ 8717c478bd9Sstevel@tonic-gate ufs_undo_allocation(ip, 8727c478bd9Sstevel@tonic-gate alloced_blocks, 8737c478bd9Sstevel@tonic-gate undo_table, added_sectors); 8747c478bd9Sstevel@tonic-gate return (err); 8757c478bd9Sstevel@tonic-gate } 8767c478bd9Sstevel@tonic-gate brelse(bp); 8777c478bd9Sstevel@tonic-gate } else { 8787c478bd9Sstevel@tonic-gate bdrwrite(bp); 8797c478bd9Sstevel@tonic-gate } 8807c478bd9Sstevel@tonic-gate } else { 8817c478bd9Sstevel@tonic-gate brelse(bp); 8827c478bd9Sstevel@tonic-gate } 8837c478bd9Sstevel@tonic-gate } 8847c478bd9Sstevel@tonic-gate return (0); 8857c478bd9Sstevel@tonic-gate } 8867c478bd9Sstevel@tonic-gate 8877c478bd9Sstevel@tonic-gate /* 8887c478bd9Sstevel@tonic-gate * Return 1 if inode has unmapped blocks (UFS holes). 8897c478bd9Sstevel@tonic-gate */ 8907c478bd9Sstevel@tonic-gate int 8917c478bd9Sstevel@tonic-gate bmap_has_holes(struct inode *ip) 8927c478bd9Sstevel@tonic-gate { 8937c478bd9Sstevel@tonic-gate struct fs *fs = ip->i_fs; 8947c478bd9Sstevel@tonic-gate uint_t dblks; /* # of data blocks */ 8957c478bd9Sstevel@tonic-gate uint_t mblks; /* # of data + metadata blocks */ 8967c478bd9Sstevel@tonic-gate int nindirshift; 8977c478bd9Sstevel@tonic-gate int nindiroffset; 8987c478bd9Sstevel@tonic-gate uint_t cnt; 8997c478bd9Sstevel@tonic-gate int n, j, shft; 9007c478bd9Sstevel@tonic-gate uint_t nindirblks; 9017c478bd9Sstevel@tonic-gate 9027c478bd9Sstevel@tonic-gate int fsbshift = fs->fs_bshift; 9037c478bd9Sstevel@tonic-gate int fsboffset = (1 << fsbshift) - 1; 9047c478bd9Sstevel@tonic-gate 9057c478bd9Sstevel@tonic-gate dblks = (ip->i_size + fsboffset) >> fsbshift; 9067c478bd9Sstevel@tonic-gate mblks = (ldbtob((u_offset_t)ip->i_blocks) + fsboffset) >> fsbshift; 9077c478bd9Sstevel@tonic-gate 9087c478bd9Sstevel@tonic-gate /* 9097c478bd9Sstevel@tonic-gate * File has only direct blocks. 9107c478bd9Sstevel@tonic-gate */ 9117c478bd9Sstevel@tonic-gate if (dblks <= NDADDR) 9127c478bd9Sstevel@tonic-gate return (mblks < dblks); 9137c478bd9Sstevel@tonic-gate 9147c478bd9Sstevel@tonic-gate nindirshift = ip->i_ufsvfs->vfs_nindirshift; 9157c478bd9Sstevel@tonic-gate nindiroffset = ip->i_ufsvfs->vfs_nindiroffset; 9167c478bd9Sstevel@tonic-gate nindirblks = nindiroffset + 1; 9177c478bd9Sstevel@tonic-gate 9187c478bd9Sstevel@tonic-gate dblks -= NDADDR; 9197c478bd9Sstevel@tonic-gate shft = 0; 9207c478bd9Sstevel@tonic-gate /* 9217c478bd9Sstevel@tonic-gate * Determine how many levels of indirection. 9227c478bd9Sstevel@tonic-gate */ 9237c478bd9Sstevel@tonic-gate for (j = NIADDR; j > 0; j--) { 9247c478bd9Sstevel@tonic-gate longlong_t sh; 9257c478bd9Sstevel@tonic-gate 9267c478bd9Sstevel@tonic-gate shft += nindirshift; /* sh *= nindir */ 9277c478bd9Sstevel@tonic-gate sh = 1LL << shft; 9287c478bd9Sstevel@tonic-gate if (dblks <= sh) 9297c478bd9Sstevel@tonic-gate break; 9307c478bd9Sstevel@tonic-gate dblks -= sh; 9317c478bd9Sstevel@tonic-gate } 9327c478bd9Sstevel@tonic-gate /* LINTED: warning: logical expression always true: op "||" */ 9337c478bd9Sstevel@tonic-gate ASSERT(NIADDR <= 3); 9347c478bd9Sstevel@tonic-gate ASSERT(j <= NIADDR); 9357c478bd9Sstevel@tonic-gate if (j == NIADDR) /* single level indirection */ 9367c478bd9Sstevel@tonic-gate cnt = NDADDR + 1 + dblks; 9377c478bd9Sstevel@tonic-gate else if (j == NIADDR-1) /* double indirection */ 9387c478bd9Sstevel@tonic-gate cnt = NDADDR + 1 + nindirblks + 9397c478bd9Sstevel@tonic-gate 1 + (dblks + nindiroffset)/nindirblks + dblks; 9407c478bd9Sstevel@tonic-gate else if (j == NIADDR-2) { /* triple indirection */ 9417c478bd9Sstevel@tonic-gate n = (dblks + nindiroffset)/nindirblks; 9427c478bd9Sstevel@tonic-gate cnt = NDADDR + 1 + nindirblks + 9437c478bd9Sstevel@tonic-gate 1 + nindirblks + nindirblks*nindirblks + 9447c478bd9Sstevel@tonic-gate 1 + (n + nindiroffset)/nindirblks + n + dblks; 9457c478bd9Sstevel@tonic-gate } 9467c478bd9Sstevel@tonic-gate 9477c478bd9Sstevel@tonic-gate return (mblks < cnt); 9487c478bd9Sstevel@tonic-gate } 9497c478bd9Sstevel@tonic-gate 9507c478bd9Sstevel@tonic-gate /* 9517c478bd9Sstevel@tonic-gate * find some contig blocks starting at *sbp and going for min(n, max_contig) 9527c478bd9Sstevel@tonic-gate * return the number of blocks (not frags) found. 9537c478bd9Sstevel@tonic-gate * The array passed in must be at least [0..n-1]. 9547c478bd9Sstevel@tonic-gate */ 9557c478bd9Sstevel@tonic-gate static int 9567c478bd9Sstevel@tonic-gate findextent(struct fs *fs, daddr32_t *sbp, int n, int *lenp, int maxtransfer) 9577c478bd9Sstevel@tonic-gate { 9587c478bd9Sstevel@tonic-gate register daddr_t bn, nextbn; 9597c478bd9Sstevel@tonic-gate register daddr32_t *bp; 9607c478bd9Sstevel@tonic-gate register int diff; 9617c478bd9Sstevel@tonic-gate int maxtransblk; 9627c478bd9Sstevel@tonic-gate 9637c478bd9Sstevel@tonic-gate if (n <= 0) 9647c478bd9Sstevel@tonic-gate return (0); 9657c478bd9Sstevel@tonic-gate bn = *sbp; 9667c478bd9Sstevel@tonic-gate if (bn == 0) 9677c478bd9Sstevel@tonic-gate return (0); 9687c478bd9Sstevel@tonic-gate diff = fs->fs_frag; 9697c478bd9Sstevel@tonic-gate if (*lenp) { 9707c478bd9Sstevel@tonic-gate n = MIN(n, lblkno(fs, *lenp)); 9717c478bd9Sstevel@tonic-gate } else { 9727c478bd9Sstevel@tonic-gate /* 9737c478bd9Sstevel@tonic-gate * If the user has set the value for maxcontig lower than 9747c478bd9Sstevel@tonic-gate * the drive transfer size, then assume they want this 9757c478bd9Sstevel@tonic-gate * to be the maximum value for the size of the data transfer. 9767c478bd9Sstevel@tonic-gate */ 9777c478bd9Sstevel@tonic-gate maxtransblk = maxtransfer >> DEV_BSHIFT; 9787c478bd9Sstevel@tonic-gate if (fs->fs_maxcontig < maxtransblk) { 9797c478bd9Sstevel@tonic-gate n = MIN(n, fs->fs_maxcontig); 9807c478bd9Sstevel@tonic-gate } else { 9817c478bd9Sstevel@tonic-gate n = MIN(n, maxtransblk); 9827c478bd9Sstevel@tonic-gate } 9837c478bd9Sstevel@tonic-gate } 9847c478bd9Sstevel@tonic-gate bp = sbp; 9857c478bd9Sstevel@tonic-gate while (--n > 0) { 9867c478bd9Sstevel@tonic-gate nextbn = *(bp + 1); 9877c478bd9Sstevel@tonic-gate if (nextbn == 0 || bn + diff != nextbn) 9887c478bd9Sstevel@tonic-gate break; 9897c478bd9Sstevel@tonic-gate bn = nextbn; 9907c478bd9Sstevel@tonic-gate bp++; 9917c478bd9Sstevel@tonic-gate } 9927c478bd9Sstevel@tonic-gate return ((int)(bp - sbp) + 1); 9937c478bd9Sstevel@tonic-gate } 9947c478bd9Sstevel@tonic-gate 9957c478bd9Sstevel@tonic-gate /* 9967c478bd9Sstevel@tonic-gate * Free any blocks which had been successfully allocated. Always called 9977c478bd9Sstevel@tonic-gate * as a result of an error, so we don't bother returning an error code 9987c478bd9Sstevel@tonic-gate * from here. 9997c478bd9Sstevel@tonic-gate * 10007c478bd9Sstevel@tonic-gate * If block_count and inode_sector_adjust are both zero, we'll do nothing. 10017c478bd9Sstevel@tonic-gate * Thus it is safe to call this as part of error handling, whether or not 10027c478bd9Sstevel@tonic-gate * any blocks have been allocated. 10037c478bd9Sstevel@tonic-gate * 10047c478bd9Sstevel@tonic-gate * The ufs_inode_direct case is currently unused. 10057c478bd9Sstevel@tonic-gate */ 10067c478bd9Sstevel@tonic-gate 10077c478bd9Sstevel@tonic-gate static void 10087c478bd9Sstevel@tonic-gate ufs_undo_allocation( 10097c478bd9Sstevel@tonic-gate inode_t *ip, 10107c478bd9Sstevel@tonic-gate int block_count, 10117c478bd9Sstevel@tonic-gate struct ufs_allocated_block table[], 10127c478bd9Sstevel@tonic-gate int inode_sector_adjust) 10137c478bd9Sstevel@tonic-gate { 10147c478bd9Sstevel@tonic-gate int i; 10157c478bd9Sstevel@tonic-gate int inode_changed; 10167c478bd9Sstevel@tonic-gate int error_updating_pointers; 10177c478bd9Sstevel@tonic-gate struct ufsvfs *ufsvfsp; 10187c478bd9Sstevel@tonic-gate 10197c478bd9Sstevel@tonic-gate inode_changed = 0; 10207c478bd9Sstevel@tonic-gate error_updating_pointers = 0; 10217c478bd9Sstevel@tonic-gate 10227c478bd9Sstevel@tonic-gate ufsvfsp = ip->i_ufsvfs; 10237c478bd9Sstevel@tonic-gate 10247c478bd9Sstevel@tonic-gate /* 10257c478bd9Sstevel@tonic-gate * Update pointers on disk before freeing blocks. If we fail, 10267c478bd9Sstevel@tonic-gate * some blocks may remain busy; but they will be reclaimed by 10277c478bd9Sstevel@tonic-gate * an fsck. (This is better than letting a block wind up with 10287c478bd9Sstevel@tonic-gate * two owners if we successfully freed it but could not remove 10297c478bd9Sstevel@tonic-gate * the pointer to it.) 10307c478bd9Sstevel@tonic-gate */ 10317c478bd9Sstevel@tonic-gate 10327c478bd9Sstevel@tonic-gate for (i = 0; i < block_count; i++) { 10337c478bd9Sstevel@tonic-gate switch (table[i].owner) { 10347c478bd9Sstevel@tonic-gate case ufs_no_owner: 10357c478bd9Sstevel@tonic-gate /* Nothing to do here, nobody points to us */ 10367c478bd9Sstevel@tonic-gate break; 10377c478bd9Sstevel@tonic-gate case ufs_inode_direct: 10387c478bd9Sstevel@tonic-gate ASSERT(table[i].owner_offset < NDADDR); 10397c478bd9Sstevel@tonic-gate ip->i_db[table[i].owner_offset] = 0; 10407c478bd9Sstevel@tonic-gate inode_changed = 1; 10417c478bd9Sstevel@tonic-gate break; 10427c478bd9Sstevel@tonic-gate case ufs_inode_indirect: 10437c478bd9Sstevel@tonic-gate ASSERT(table[i].owner_offset < NIADDR); 10447c478bd9Sstevel@tonic-gate ip->i_ib[table[i].owner_offset] = 0; 10457c478bd9Sstevel@tonic-gate inode_changed = 1; 10467c478bd9Sstevel@tonic-gate break; 10477c478bd9Sstevel@tonic-gate case ufs_indirect_block: { 10487c478bd9Sstevel@tonic-gate buf_t *bp; 10497c478bd9Sstevel@tonic-gate daddr32_t *block_data; 10507c478bd9Sstevel@tonic-gate 10517c478bd9Sstevel@tonic-gate /* Read/modify/log/write. */ 10527c478bd9Sstevel@tonic-gate 10537c478bd9Sstevel@tonic-gate ASSERT(table[i].owner_offset < 10547c478bd9Sstevel@tonic-gate (VBSIZE(ITOV(ip)) / sizeof (daddr32_t))); 10557c478bd9Sstevel@tonic-gate 10567c478bd9Sstevel@tonic-gate bp = UFS_BREAD(ufsvfsp, ip->i_dev, 10577c478bd9Sstevel@tonic-gate fsbtodb(ufsvfsp->vfs_fs, table[i].owner_block), 10587c478bd9Sstevel@tonic-gate VBSIZE(ITOV(ip))); 10597c478bd9Sstevel@tonic-gate 10607c478bd9Sstevel@tonic-gate if (bp->b_flags & B_ERROR) { 10617c478bd9Sstevel@tonic-gate /* Couldn't read this block; give up. */ 10627c478bd9Sstevel@tonic-gate error_updating_pointers = 1; 10637c478bd9Sstevel@tonic-gate brelse(bp); 10647c478bd9Sstevel@tonic-gate break; /* out of SWITCH */ 10657c478bd9Sstevel@tonic-gate } 10667c478bd9Sstevel@tonic-gate 10677c478bd9Sstevel@tonic-gate block_data = bp->b_un.b_daddr; 10687c478bd9Sstevel@tonic-gate block_data[table[i].owner_offset] = 0; 10697c478bd9Sstevel@tonic-gate 10707c478bd9Sstevel@tonic-gate /* Write a log entry which includes the zero. */ 10717c478bd9Sstevel@tonic-gate /* It might be possible to optimize this by using */ 10727c478bd9Sstevel@tonic-gate /* TRANS_BUF directly and zeroing only the four */ 10737c478bd9Sstevel@tonic-gate /* bytes involved, but an attempt to do that led */ 10747c478bd9Sstevel@tonic-gate /* to panics in the logging code. The attempt was */ 10757c478bd9Sstevel@tonic-gate /* TRANS_BUF(ufsvfsp, */ 10767c478bd9Sstevel@tonic-gate /* table[i].owner_offset * sizeof (daddr32_t), */ 10777c478bd9Sstevel@tonic-gate /* sizeof (daddr32_t), */ 10787c478bd9Sstevel@tonic-gate /* bp, */ 10797c478bd9Sstevel@tonic-gate /* DT_ABZERO); */ 10807c478bd9Sstevel@tonic-gate 10817c478bd9Sstevel@tonic-gate TRANS_BUF_ITEM_128(ufsvfsp, 10827c478bd9Sstevel@tonic-gate block_data[table[i].owner_offset], 10837c478bd9Sstevel@tonic-gate block_data, bp, DT_AB); 10847c478bd9Sstevel@tonic-gate 10857c478bd9Sstevel@tonic-gate /* Now we can write the buffer itself. */ 10867c478bd9Sstevel@tonic-gate 10877c478bd9Sstevel@tonic-gate UFS_BWRITE2(ufsvfsp, bp); 10887c478bd9Sstevel@tonic-gate 10897c478bd9Sstevel@tonic-gate if (bp->b_flags & B_ERROR) { 10907c478bd9Sstevel@tonic-gate error_updating_pointers = 1; 10917c478bd9Sstevel@tonic-gate } 10927c478bd9Sstevel@tonic-gate 10937c478bd9Sstevel@tonic-gate brelse(bp); 10947c478bd9Sstevel@tonic-gate break; 10957c478bd9Sstevel@tonic-gate } 10967c478bd9Sstevel@tonic-gate default: 10977c478bd9Sstevel@tonic-gate (void) ufs_fault(ITOV(ip), 10987c478bd9Sstevel@tonic-gate "ufs_undo_allocation failure\n"); 10997c478bd9Sstevel@tonic-gate break; 11007c478bd9Sstevel@tonic-gate } 11017c478bd9Sstevel@tonic-gate } 11027c478bd9Sstevel@tonic-gate 11037c478bd9Sstevel@tonic-gate /* 11047c478bd9Sstevel@tonic-gate * If the inode changed, or if we need to update its block count, 11057c478bd9Sstevel@tonic-gate * then do that now. We update the inode synchronously on disk 11067c478bd9Sstevel@tonic-gate * to ensure that it won't transiently point at a block we've 11077c478bd9Sstevel@tonic-gate * freed (only necessary if we're not logging). 11087c478bd9Sstevel@tonic-gate * 11097c478bd9Sstevel@tonic-gate * NOTE: Currently ufs_iupdat() does not check for errors. When 11107c478bd9Sstevel@tonic-gate * it is fixed, we should verify that we successfully updated the 11117c478bd9Sstevel@tonic-gate * inode before freeing blocks below. 11127c478bd9Sstevel@tonic-gate */ 11137c478bd9Sstevel@tonic-gate 11147c478bd9Sstevel@tonic-gate if (inode_changed || (inode_sector_adjust != 0)) { 11157c478bd9Sstevel@tonic-gate ip->i_blocks -= inode_sector_adjust; 11167c478bd9Sstevel@tonic-gate ASSERT((unsigned)ip->i_blocks <= INT_MAX); 11177c478bd9Sstevel@tonic-gate TRANS_INODE(ufsvfsp, ip); 11187c478bd9Sstevel@tonic-gate ip->i_flag |= IUPD | ICHG | IATTCHG; 11197c478bd9Sstevel@tonic-gate ip->i_seq++; 11207c478bd9Sstevel@tonic-gate if (!TRANS_ISTRANS(ufsvfsp)) 11217c478bd9Sstevel@tonic-gate ufs_iupdat(ip, I_SYNC); 11227c478bd9Sstevel@tonic-gate } 11237c478bd9Sstevel@tonic-gate 11247c478bd9Sstevel@tonic-gate /* 11257c478bd9Sstevel@tonic-gate * Now we go through and actually free the blocks, but only if we 11267c478bd9Sstevel@tonic-gate * successfully removed the pointers to them. 11277c478bd9Sstevel@tonic-gate */ 11287c478bd9Sstevel@tonic-gate 11297c478bd9Sstevel@tonic-gate if (!error_updating_pointers) { 11307c478bd9Sstevel@tonic-gate for (i = 0; i < block_count; i++) { 11317c478bd9Sstevel@tonic-gate free(ip, table[i].this_block, table[i].block_size, 11327c478bd9Sstevel@tonic-gate table[i].usage_flags); 11337c478bd9Sstevel@tonic-gate } 11347c478bd9Sstevel@tonic-gate } 11357c478bd9Sstevel@tonic-gate } 11367c478bd9Sstevel@tonic-gate 11377c478bd9Sstevel@tonic-gate /* 11387c478bd9Sstevel@tonic-gate * Find the next hole or data block in file starting at *off 1139*a6595b40Sperrin * Return found offset in *off, which can be less than the 1140*a6595b40Sperrin * starting offset if not block aligned. 11417c478bd9Sstevel@tonic-gate * This code is based on bmap_read(). 11427c478bd9Sstevel@tonic-gate * Errors: ENXIO for end of file 11437c478bd9Sstevel@tonic-gate * EIO for block read error. 11447c478bd9Sstevel@tonic-gate */ 11457c478bd9Sstevel@tonic-gate int 11467c478bd9Sstevel@tonic-gate bmap_find(struct inode *ip, boolean_t hole, u_offset_t *off) 11477c478bd9Sstevel@tonic-gate { 11487c478bd9Sstevel@tonic-gate ufsvfs_t *ufsvfsp = ip->i_ufsvfs; 11497c478bd9Sstevel@tonic-gate struct fs *fs = ufsvfsp->vfs_fs; 11507c478bd9Sstevel@tonic-gate buf_t *bp[NIADDR]; 11517c478bd9Sstevel@tonic-gate int i, j; 11527c478bd9Sstevel@tonic-gate int shft; /* we maintain sh = 1 << shft */ 11537c478bd9Sstevel@tonic-gate int nindirshift, nindiroffset; 11547c478bd9Sstevel@tonic-gate daddr_t ob, nb, tbn, lbn, skip; 11557c478bd9Sstevel@tonic-gate daddr32_t *bap; 11567c478bd9Sstevel@tonic-gate u_offset_t isz = (offset_t)ip->i_size; 11577c478bd9Sstevel@tonic-gate int32_t bs = fs->fs_bsize; /* file system block size */ 11587c478bd9Sstevel@tonic-gate int32_t nindir = fs->fs_nindir; 11597c478bd9Sstevel@tonic-gate dev_t dev; 11607c478bd9Sstevel@tonic-gate int error = 0; 11617c478bd9Sstevel@tonic-gate daddr_t limits[NIADDR]; 11627c478bd9Sstevel@tonic-gate 11637c478bd9Sstevel@tonic-gate ASSERT(*off < isz); 11647c478bd9Sstevel@tonic-gate ASSERT(RW_LOCK_HELD(&ip->i_contents)); 11657c478bd9Sstevel@tonic-gate lbn = (daddr_t)lblkno(fs, *off); 11667c478bd9Sstevel@tonic-gate ASSERT(lbn >= 0); 11677c478bd9Sstevel@tonic-gate 11687c478bd9Sstevel@tonic-gate for (i = 0; i < NIADDR; i++) 11697c478bd9Sstevel@tonic-gate bp[i] = NULL; 11707c478bd9Sstevel@tonic-gate 11717c478bd9Sstevel@tonic-gate /* 11727c478bd9Sstevel@tonic-gate * The first NDADDR blocks are direct blocks. 11737c478bd9Sstevel@tonic-gate */ 11747c478bd9Sstevel@tonic-gate if (lbn < NDADDR) { 11757c478bd9Sstevel@tonic-gate for (; lbn < NDADDR; lbn++) { 11767c478bd9Sstevel@tonic-gate if ((hole && (ip->i_db[lbn] == 0)) || 11777c478bd9Sstevel@tonic-gate (!hole && (ip->i_db[lbn] != 0))) { 11787c478bd9Sstevel@tonic-gate goto out; 11797c478bd9Sstevel@tonic-gate } 11807c478bd9Sstevel@tonic-gate } 11817c478bd9Sstevel@tonic-gate if ((u_offset_t)lbn << fs->fs_bshift >= isz) 11827c478bd9Sstevel@tonic-gate goto out; 11837c478bd9Sstevel@tonic-gate } 11847c478bd9Sstevel@tonic-gate 11857c478bd9Sstevel@tonic-gate nindir = fs->fs_nindir; 11867c478bd9Sstevel@tonic-gate nindirshift = ufsvfsp->vfs_nindirshift; 11877c478bd9Sstevel@tonic-gate nindiroffset = ufsvfsp->vfs_nindiroffset; 11887c478bd9Sstevel@tonic-gate dev = ip->i_dev; 11897c478bd9Sstevel@tonic-gate 11907c478bd9Sstevel@tonic-gate /* Set up limits array */ 11917c478bd9Sstevel@tonic-gate for (limits[0] = NDADDR, j = 1; j < NIADDR; j++) 11927c478bd9Sstevel@tonic-gate limits[j] = limits[j-1] + (1ULL << (nindirshift * j)); 11937c478bd9Sstevel@tonic-gate 11947c478bd9Sstevel@tonic-gate loop: 11957c478bd9Sstevel@tonic-gate /* 11967c478bd9Sstevel@tonic-gate * Determine how many levels of indirection. 11977c478bd9Sstevel@tonic-gate */ 11987c478bd9Sstevel@tonic-gate shft = 0; /* sh = 1 */ 11997c478bd9Sstevel@tonic-gate tbn = lbn - NDADDR; 12007c478bd9Sstevel@tonic-gate for (j = NIADDR; j > 0; j--) { 12017c478bd9Sstevel@tonic-gate longlong_t sh; 12027c478bd9Sstevel@tonic-gate 12037c478bd9Sstevel@tonic-gate shft += nindirshift; /* sh *= nindir */ 12047c478bd9Sstevel@tonic-gate sh = 1LL << shft; 12057c478bd9Sstevel@tonic-gate if (tbn < sh) 12067c478bd9Sstevel@tonic-gate break; 12077c478bd9Sstevel@tonic-gate tbn -= sh; 12087c478bd9Sstevel@tonic-gate } 12097c478bd9Sstevel@tonic-gate if (j == 0) { 12107c478bd9Sstevel@tonic-gate /* must have passed end of file */ 12117c478bd9Sstevel@tonic-gate ASSERT(((u_offset_t)lbn << fs->fs_bshift) >= isz); 12127c478bd9Sstevel@tonic-gate goto out; 12137c478bd9Sstevel@tonic-gate } 12147c478bd9Sstevel@tonic-gate 12157c478bd9Sstevel@tonic-gate /* 12167c478bd9Sstevel@tonic-gate * Fetch the first indirect block. 12177c478bd9Sstevel@tonic-gate */ 12187c478bd9Sstevel@tonic-gate nb = ip->i_ib[NIADDR - j]; 12197c478bd9Sstevel@tonic-gate if (nb == 0) { 12207c478bd9Sstevel@tonic-gate if (hole) { 12217c478bd9Sstevel@tonic-gate lbn = limits[NIADDR - j]; 12227c478bd9Sstevel@tonic-gate goto out; 12237c478bd9Sstevel@tonic-gate } else { 12247c478bd9Sstevel@tonic-gate lbn = limits[NIADDR - j + 1]; 12257c478bd9Sstevel@tonic-gate if ((u_offset_t)lbn << fs->fs_bshift >= isz) 12267c478bd9Sstevel@tonic-gate goto out; 12277c478bd9Sstevel@tonic-gate goto loop; 12287c478bd9Sstevel@tonic-gate } 12297c478bd9Sstevel@tonic-gate } 12307c478bd9Sstevel@tonic-gate 12317c478bd9Sstevel@tonic-gate /* 12327c478bd9Sstevel@tonic-gate * Fetch through the indirect blocks. 12337c478bd9Sstevel@tonic-gate */ 12347c478bd9Sstevel@tonic-gate for (; ((j <= NIADDR) && (nb != 0)); j++) { 12357c478bd9Sstevel@tonic-gate ob = nb; 12367c478bd9Sstevel@tonic-gate /* 12377c478bd9Sstevel@tonic-gate * if there's a different block at this level then release 12387c478bd9Sstevel@tonic-gate * the old one and in with the new. 12397c478bd9Sstevel@tonic-gate */ 12407c478bd9Sstevel@tonic-gate if ((bp[j-1] == NULL) || bp[j-1]->b_blkno != fsbtodb(fs, ob)) { 12417c478bd9Sstevel@tonic-gate if (bp[j-1] != NULL) 12427c478bd9Sstevel@tonic-gate brelse(bp[j-1]); 12437c478bd9Sstevel@tonic-gate bp[j-1] = UFS_BREAD(ufsvfsp, dev, fsbtodb(fs, ob), bs); 12447c478bd9Sstevel@tonic-gate if (bp[j-1]->b_flags & B_ERROR) { 12457c478bd9Sstevel@tonic-gate error = EIO; 12467c478bd9Sstevel@tonic-gate goto out; 12477c478bd9Sstevel@tonic-gate } 12487c478bd9Sstevel@tonic-gate } 12497c478bd9Sstevel@tonic-gate bap = bp[j-1]->b_un.b_daddr; 12507c478bd9Sstevel@tonic-gate 12517c478bd9Sstevel@tonic-gate shft -= nindirshift; /* sh / nindir */ 12527c478bd9Sstevel@tonic-gate i = (tbn >> shft) & nindiroffset; /* (tbn / sh) % nindir */ 12537c478bd9Sstevel@tonic-gate nb = bap[i]; 12547c478bd9Sstevel@tonic-gate skip = 1LL << (nindirshift * (NIADDR - j)); 12557c478bd9Sstevel@tonic-gate } 12567c478bd9Sstevel@tonic-gate 12577c478bd9Sstevel@tonic-gate /* 12587c478bd9Sstevel@tonic-gate * Scan through the blocks in this array. 12597c478bd9Sstevel@tonic-gate */ 12607c478bd9Sstevel@tonic-gate for (; i < nindir; i++, lbn += skip) { 12617c478bd9Sstevel@tonic-gate if (hole && (bap[i] == 0)) 12627c478bd9Sstevel@tonic-gate goto out; 12637c478bd9Sstevel@tonic-gate if (!hole && (bap[i] != 0)) { 12647c478bd9Sstevel@tonic-gate if (skip == 1) { 12657c478bd9Sstevel@tonic-gate /* we're at the lowest level */ 12667c478bd9Sstevel@tonic-gate goto out; 12677c478bd9Sstevel@tonic-gate } else { 12687c478bd9Sstevel@tonic-gate goto loop; 12697c478bd9Sstevel@tonic-gate } 12707c478bd9Sstevel@tonic-gate } 12717c478bd9Sstevel@tonic-gate } 12727c478bd9Sstevel@tonic-gate if (((u_offset_t)lbn << fs->fs_bshift) < isz) 12737c478bd9Sstevel@tonic-gate goto loop; 12747c478bd9Sstevel@tonic-gate out: 12757c478bd9Sstevel@tonic-gate for (i = 0; i < NIADDR; i++) { 12767c478bd9Sstevel@tonic-gate if (bp[i]) 12777c478bd9Sstevel@tonic-gate brelse(bp[i]); 12787c478bd9Sstevel@tonic-gate } 12797c478bd9Sstevel@tonic-gate if (error == 0) { 12807c478bd9Sstevel@tonic-gate if (((u_offset_t)lbn << fs->fs_bshift) >= isz) { 12817c478bd9Sstevel@tonic-gate error = ENXIO; 12827c478bd9Sstevel@tonic-gate } else { 12837c478bd9Sstevel@tonic-gate /* success */ 12847c478bd9Sstevel@tonic-gate *off = (u_offset_t)lbn << fs->fs_bshift; 12857c478bd9Sstevel@tonic-gate } 12867c478bd9Sstevel@tonic-gate } 12877c478bd9Sstevel@tonic-gate return (error); 12887c478bd9Sstevel@tonic-gate } 1289