Home | History | Annotate | Line # | Download | only in lfs
lfs_inode.c revision 1.12
      1 /*	$NetBSD: lfs_inode.c,v 1.12 1998/03/01 02:23:24 fvdl Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1986, 1989, 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)lfs_inode.c	8.9 (Berkeley) 5/8/95
     36  */
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/mount.h>
     41 #include <sys/proc.h>
     42 #include <sys/file.h>
     43 #include <sys/buf.h>
     44 #include <sys/vnode.h>
     45 #include <sys/kernel.h>
     46 #include <sys/malloc.h>
     47 
     48 #include <vm/vm.h>
     49 
     50 #include <ufs/ufs/quota.h>
     51 #include <ufs/ufs/inode.h>
     52 #include <ufs/ufs/ufsmount.h>
     53 #include <ufs/ufs/ufs_extern.h>
     54 
     55 #include <ufs/lfs/lfs.h>
     56 #include <ufs/lfs/lfs_extern.h>
     57 
     58 /* Search a block for a specific dinode. */
     59 struct dinode *
     60 lfs_ifind(fs, ino, dip)
     61 	struct lfs *fs;
     62 	ino_t ino;
     63 	register struct dinode *dip;
     64 {
     65 	register int cnt;
     66 	register struct dinode *ldip;
     67 
     68 	for (cnt = INOPB(fs), ldip = dip + (cnt - 1); cnt--; --ldip)
     69 		if (ldip->di_inumber == ino)
     70 			return (ldip);
     71 
     72 	panic("lfs_ifind: dinode %u not found", ino);
     73 	/* NOTREACHED */
     74 }
     75 
     76 int
     77 lfs_update(v)
     78 	void *v;
     79 {
     80 	struct vop_update_args /* {
     81 		struct vnode *a_vp;
     82 		struct timespec *a_access;
     83 		struct timespec *a_modify;
     84 		int a_waitfor;
     85 	} */ *ap = v;
     86 	struct inode *ip;
     87 	struct vnode *vp = ap->a_vp;
     88 	int mod;
     89 	struct timespec ts;
     90 
     91 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
     92 		return (0);
     93 	ip = VTOI(vp);
     94 	mod = ip->i_flag & IN_MODIFIED;
     95 	TIMEVAL_TO_TIMESPEC(&time, &ts);
     96 	FFS_ITIMES(ip, ap->a_access, ap->a_modify, &ts);
     97 	if (!mod && ip->i_flag & IN_MODIFIED)
     98 		ip->i_lfs->lfs_uinodes++;
     99 	if ((ip->i_flag & IN_MODIFIED) == 0)
    100 		return (0);
    101 
    102 	/* If sync, push back the vnode and any dirty blocks it may have. */
    103 	return (ap->a_waitfor & LFS_SYNC ? lfs_vflush(vp) : 0);
    104 }
    105 
    106 /* Update segment usage information when removing a block. */
    107 #define UPDATE_SEGUSE \
    108 	if (lastseg != -1) { \
    109 		LFS_SEGENTRY(sup, fs, lastseg, sup_bp); \
    110 		if (num > sup->su_nbytes) \
    111 			panic("lfs_truncate: negative bytes in segment %d\n", \
    112 			    lastseg); \
    113 		sup->su_nbytes -= num; \
    114 		e1 = VOP_BWRITE(sup_bp); \
    115 		fragsreleased += numfrags(fs, num); \
    116 	}
    117 
    118 #define SEGDEC(S) { \
    119 	if (daddr != 0) { \
    120 		if (lastseg != (seg = datosn(fs, daddr))) { \
    121 			UPDATE_SEGUSE; \
    122 			num = (S); \
    123 			lastseg = seg; \
    124 		} else \
    125 			num += (S); \
    126 	} \
    127 }
    128 
    129 /*
    130  * Truncate the inode ip to at most length size.  Update segment usage
    131  * table information.
    132  */
    133 /* ARGSUSED */
    134 int
    135 lfs_truncate(v)
    136 	void *v;
    137 {
    138 	struct vop_truncate_args /* {
    139 		struct vnode *a_vp;
    140 		off_t a_length;
    141 		int a_flags;
    142 		struct ucred *a_cred;
    143 		struct proc *a_p;
    144 	} */ *ap = v;
    145 	register struct indir *inp;
    146 	register int i;
    147 	register ufs_daddr_t *daddrp;
    148 	register struct vnode *vp = ap->a_vp;
    149 	off_t length = ap->a_length;
    150 	struct buf *bp, *sup_bp;
    151 	struct timespec ts;
    152 	struct ifile *ifp;
    153 	struct inode *ip;
    154 	struct lfs *fs;
    155 	struct indir a[NIADDR + 2], a_end[NIADDR + 2];
    156 	SEGUSE *sup;
    157 	ufs_daddr_t daddr, lastblock, lbn, olastblock;
    158 	ufs_daddr_t oldsize_lastblock, oldsize_newlast, newsize;
    159 	long off, a_released, fragsreleased, i_released;
    160 	int e1, e2, depth, lastseg, num, offset, seg, freesize;
    161 
    162 	ip = VTOI(vp);
    163 	TIMEVAL_TO_TIMESPEC(&time, &ts);
    164 	if (vp->v_type == VLNK && vp->v_mount->mnt_maxsymlinklen > 0) {
    165 #ifdef DIAGNOSTIC
    166 		if (length != 0)
    167 			panic("lfs_truncate: partial truncate of symlink");
    168 #endif
    169 		bzero((char *)&ip->i_ffs_shortlink, (u_int)ip->i_ffs_size);
    170 		ip->i_ffs_size = 0;
    171 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    172 		return (VOP_UPDATE(vp, &ts, &ts, 0));
    173 	}
    174 #ifdef UVM
    175 	uvm_vnp_setsize(vp, length);
    176 #else
    177 	vnode_pager_setsize(vp, length);
    178 #endif
    179 
    180 	fs = ip->i_lfs;
    181 
    182 	/* If length is larger than the file, just update the times. */
    183 	if (ip->i_ffs_size <= length) {
    184 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    185 		return (VOP_UPDATE(vp, &ts, &ts, 0));
    186 	}
    187 
    188 	/*
    189 	 * Calculate index into inode's block list of last direct and indirect
    190 	 * blocks (if any) which we want to keep.  Lastblock is 0 when the
    191 	 * file is truncated to 0.
    192 	 */
    193 	lastblock = lblkno(fs, length + fs->lfs_bsize - 1);
    194 	olastblock = lblkno(fs, ip->i_ffs_size + fs->lfs_bsize - 1) - 1;
    195 
    196 	/*
    197 	 * Update the size of the file. If the file is not being truncated to
    198 	 * a block boundry, the contents of the partial block following the end
    199 	 * because of subsequent file growth.  For this part of the code,
    200 	 * oldsize_newlast refers to the old size of the new last block in the
    201 	 * file.
    202 	 */
    203 	offset = blkoff(fs, length);
    204 	lbn = lblkno(fs, length);
    205 	oldsize_newlast = blksize(fs, ip, lbn);
    206 
    207 	/* Now set oldsize to the current size of the current last block */
    208 	oldsize_lastblock = blksize(fs, ip, olastblock);
    209 	if (offset == 0)
    210 		ip->i_ffs_size = length;
    211 	else {
    212 #ifdef QUOTA
    213 		if ((e1 = getinoquota(ip)) != 0)
    214 			return (e1);
    215 #endif
    216 		if ((e1 = bread(vp, lbn, oldsize_newlast, NOCRED, &bp)) != 0)
    217 			return (e1);
    218 		ip->i_ffs_size = length;
    219 #if defined(UVM)
    220 		(void)uvm_vnp_uncache(vp);
    221 #else
    222 		(void)vnode_pager_uncache(vp);
    223 #endif
    224 		newsize = blksize(fs, ip, lbn);
    225 		bzero((char *)bp->b_data + offset, (u_int)(newsize - offset));
    226 		allocbuf(bp, newsize);
    227 		if ((e1 = VOP_BWRITE(bp)) != 0)
    228 			return (e1);
    229 	}
    230 	/*
    231 	 * Modify sup->su_nbyte counters for each deleted block; keep track
    232 	 * of number of blocks removed for ip->i_ffs_blocks.
    233 	 */
    234 	fragsreleased = 0;
    235 	num = 0;
    236 	lastseg = -1;
    237 
    238 	for (lbn = olastblock; lbn >= lastblock;) {
    239 		/* XXX use run length from bmap array to make this faster */
    240 		ufs_bmaparray(vp, lbn, &daddr, a, &depth, NULL);
    241 		if (lbn == olastblock) {
    242 			for (i = NIADDR + 2; i--;)
    243 				a_end[i] = a[i];
    244 			freesize = oldsize_lastblock;
    245 		} else
    246 			freesize = fs->lfs_bsize;
    247 		switch (depth) {
    248 		case 0:				/* Direct block. */
    249 			daddr = ip->i_ffs_db[lbn];
    250 			SEGDEC(freesize);
    251 			ip->i_ffs_db[lbn] = 0;
    252 			--lbn;
    253 			break;
    254 #ifdef DIAGNOSTIC
    255 		case 1:				/* An indirect block. */
    256 			panic("lfs_truncate: ufs_bmaparray returned depth 1");
    257 			/* NOTREACHED */
    258 #endif
    259 		default:			/* Chain of indirect blocks. */
    260 			inp = a + --depth;
    261 			if (inp->in_off > 0 && lbn != lastblock) {
    262 				lbn -= inp->in_off < lbn - lastblock ?
    263 				    inp->in_off : lbn - lastblock;
    264 				break;
    265 			}
    266 			for (; depth && (inp->in_off == 0 || lbn == lastblock);
    267 			    --inp, --depth) {
    268 				if (bread(vp,
    269 				    inp->in_lbn, fs->lfs_bsize, NOCRED, &bp))
    270 					panic("lfs_truncate: bread bno %d",
    271 					    inp->in_lbn);
    272 				daddrp = (ufs_daddr_t *)bp->b_data + inp->in_off;
    273 				for (i = inp->in_off;
    274 				    i++ <= a_end[depth].in_off;) {
    275 					daddr = *daddrp++;
    276 					SEGDEC(freesize);
    277 				}
    278 				a_end[depth].in_off = NINDIR(fs) - 1;
    279 				if (inp->in_off == 0)
    280 					brelse (bp);
    281 				else {
    282 					bzero((ufs_daddr_t *)bp->b_data +
    283 					    inp->in_off, fs->lfs_bsize -
    284 					    inp->in_off * sizeof(ufs_daddr_t));
    285 					if ((e1 = VOP_BWRITE(bp)) != 0)
    286 						return (e1);
    287 				}
    288 			}
    289 			if (depth == 0 && a[1].in_off == 0) {
    290 				off = a[0].in_off;
    291 				daddr = ip->i_ffs_ib[off];
    292 				SEGDEC(freesize);
    293 				ip->i_ffs_ib[off] = 0;
    294 			}
    295 			if (lbn == lastblock || lbn <= NDADDR)
    296 				--lbn;
    297 			else {
    298 				lbn -= NINDIR(fs);
    299 				if (lbn < lastblock)
    300 					lbn = lastblock;
    301 			}
    302 		}
    303 	}
    304 	UPDATE_SEGUSE;
    305 
    306 	/* If truncating the file to 0, update the version number. */
    307 	if (length == 0) {
    308 		LFS_IENTRY(ifp, fs, ip->i_number, bp);
    309 		++ifp->if_version;
    310 		(void) VOP_BWRITE(bp);
    311 	}
    312 
    313 #ifdef DIAGNOSTIC
    314 	if (ip->i_ffs_blocks < fragstodb(fs, fragsreleased)) {
    315 		printf("lfs_truncate: frag count < 0\n");
    316 		fragsreleased = dbtofrags(fs, ip->i_ffs_blocks);
    317 		panic("lfs_truncate: frag count < 0\n");
    318 	}
    319 #endif
    320 	ip->i_ffs_blocks -= fragstodb(fs, fragsreleased);
    321 	fs->lfs_bfree +=  fragstodb(fs, fragsreleased);
    322 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
    323 	/*
    324 	 * Traverse dirty block list counting number of dirty buffers
    325 	 * that are being deleted out of the cache, so that the lfs_avail
    326 	 * field can be updated.
    327 	 */
    328 	a_released = 0;
    329 	i_released = 0;
    330 	for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = bp->b_vnbufs.le_next)
    331 		if (bp->b_flags & B_LOCKED) {
    332 			a_released += numfrags(fs, bp->b_bcount);
    333 			/*
    334 			 * XXX
    335 			 * When buffers are created in the cache, their block
    336 			 * number is set equal to their logical block number.
    337 			 * If that is still true, we are assuming that the
    338 			 * blocks are new (not yet on disk) and weren't
    339 			 * counted above.  However, there is a slight chance
    340 			 * that a block's disk address is equal to its logical
    341 			 * block number in which case, we'll get an overcounting
    342 			 * here.
    343 			 */
    344 			if (bp->b_blkno == bp->b_lblkno)
    345 				i_released += numfrags(fs, bp->b_bcount);
    346 		}
    347 	fragsreleased = i_released;
    348 #ifdef DIAGNOSTIC
    349 	if (fragsreleased > dbtofrags(fs, ip->i_ffs_blocks)) {
    350 		fragsreleased = dbtofrags(fs, ip->i_ffs_blocks);
    351 		panic("lfs_inode: more frags released than in inode");
    352 	}
    353 #endif
    354 	fs->lfs_bfree += fragstodb(fs, fragsreleased);
    355 	ip->i_ffs_blocks -= fragstodb(fs, fragsreleased);
    356 #ifdef DIAGNOSTIC
    357 	if (length == 0 && ip->i_ffs_blocks != 0) {
    358 		printf("lfs_inode: trunc to zero, but %d blocks left on inode",
    359 			ip->i_ffs_blocks);
    360 		panic("lfs_inode");
    361 	}
    362 #endif
    363 	fs->lfs_avail += fragstodb(fs, a_released);
    364 	e1 = vinvalbuf(vp, (length > 0) ? V_SAVE : 0, ap->a_cred, ap->a_p,
    365 	    0, 0);
    366 	e2 = VOP_UPDATE(vp, &ts, &ts, 0);
    367 	return (e1 ? e1 : e2 ? e2 : 0);
    368 }
    369