Home | History | Annotate | Line # | Download | only in lfs
lfs_inode.c revision 1.16
      1 /*	$NetBSD: lfs_inode.c,v 1.16 1999/03/05 20:47:07 mycroft 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 #if defined(_KERNEL) && !defined(_LKM)
     39 #include "opt_quota.h"
     40 #endif
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/mount.h>
     45 #include <sys/proc.h>
     46 #include <sys/file.h>
     47 #include <sys/buf.h>
     48 #include <sys/vnode.h>
     49 #include <sys/kernel.h>
     50 #include <sys/malloc.h>
     51 
     52 #include <vm/vm.h>
     53 
     54 #include <ufs/ufs/quota.h>
     55 #include <ufs/ufs/inode.h>
     56 #include <ufs/ufs/ufsmount.h>
     57 #include <ufs/ufs/ufs_extern.h>
     58 
     59 #include <ufs/lfs/lfs.h>
     60 #include <ufs/lfs/lfs_extern.h>
     61 
     62 /* Search a block for a specific dinode. */
     63 struct dinode *
     64 lfs_ifind(fs, ino, dip)
     65 	struct lfs *fs;
     66 	ino_t ino;
     67 	register struct dinode *dip;
     68 {
     69 	register int cnt;
     70 	register struct dinode *ldip;
     71 
     72 	for (cnt = INOPB(fs), ldip = dip + (cnt - 1); cnt--; --ldip)
     73 		if (ldip->di_inumber == ino)
     74 			return (ldip);
     75 
     76 	panic("lfs_ifind: dinode %u not found", ino);
     77 	/* NOTREACHED */
     78 }
     79 
     80 int
     81 lfs_update(v)
     82 	void *v;
     83 {
     84 	struct vop_update_args /* {
     85 		struct vnode *a_vp;
     86 		struct timespec *a_access;
     87 		struct timespec *a_modify;
     88 		int a_waitfor;
     89 	} */ *ap = v;
     90 	struct inode *ip;
     91 	struct vnode *vp = ap->a_vp;
     92 	int mod;
     93 	struct timespec ts;
     94 
     95 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
     96 		return (0);
     97 	ip = VTOI(vp);
     98 	mod = ip->i_flag & IN_MODIFIED;
     99 	TIMEVAL_TO_TIMESPEC(&time, &ts);
    100 	FFS_ITIMES(ip,
    101 	    ap->a_access ? ap->a_access : &ts,
    102 	    ap->a_modify ? ap->a_modify : &ts, &ts);
    103 	if (!mod && ip->i_flag & IN_MODIFIED)
    104 		ip->i_lfs->lfs_uinodes++;
    105 	if ((ip->i_flag & IN_MODIFIED) == 0)
    106 		return (0);
    107 
    108 	/* If sync, push back the vnode and any dirty blocks it may have. */
    109 	return (ap->a_waitfor & LFS_SYNC ? lfs_vflush(vp) : 0);
    110 }
    111 
    112 /* Update segment usage information when removing a block. */
    113 #define UPDATE_SEGUSE \
    114 	if (lastseg != -1) { \
    115 		LFS_SEGENTRY(sup, fs, lastseg, sup_bp); \
    116 		if (num > sup->su_nbytes) \
    117 			panic("lfs_truncate: negative bytes in segment %d\n", \
    118 			    lastseg); \
    119 		sup->su_nbytes -= num; \
    120 		e1 = VOP_BWRITE(sup_bp); \
    121 		fragsreleased += numfrags(fs, num); \
    122 	}
    123 
    124 #define SEGDEC(S) { \
    125 	if (daddr != 0) { \
    126 		if (lastseg != (seg = datosn(fs, daddr))) { \
    127 			UPDATE_SEGUSE; \
    128 			num = (S); \
    129 			lastseg = seg; \
    130 		} else \
    131 			num += (S); \
    132 	} \
    133 }
    134 
    135 /*
    136  * Truncate the inode ip to at most length size.  Update segment usage
    137  * table information.
    138  */
    139 /* ARGSUSED */
    140 int
    141 lfs_truncate(v)
    142 	void *v;
    143 {
    144 	struct vop_truncate_args /* {
    145 		struct vnode *a_vp;
    146 		off_t a_length;
    147 		int a_flags;
    148 		struct ucred *a_cred;
    149 		struct proc *a_p;
    150 	} */ *ap = v;
    151 	register struct indir *inp;
    152 	register int i;
    153 	register ufs_daddr_t *daddrp;
    154 	register struct vnode *vp = ap->a_vp;
    155 	off_t length = ap->a_length;
    156 	struct buf *bp, *sup_bp;
    157 	struct timespec ts;
    158 	struct ifile *ifp;
    159 	struct inode *ip;
    160 	struct lfs *fs;
    161 	struct indir a[NIADDR + 2], a_end[NIADDR + 2];
    162 	SEGUSE *sup;
    163 	ufs_daddr_t daddr, lastblock, lbn, olastblock;
    164 	ufs_daddr_t oldsize_lastblock, oldsize_newlast, newsize;
    165 	long off, a_released, fragsreleased, i_released;
    166 	int e1, e2, depth, lastseg, num, offset, seg, freesize;
    167 
    168 	ip = VTOI(vp);
    169 	TIMEVAL_TO_TIMESPEC(&time, &ts);
    170 	if (vp->v_type == VLNK && vp->v_mount->mnt_maxsymlinklen > 0) {
    171 #ifdef DIAGNOSTIC
    172 		if (length != 0)
    173 			panic("lfs_truncate: partial truncate of symlink");
    174 #endif
    175 		bzero((char *)&ip->i_ffs_shortlink, (u_int)ip->i_ffs_size);
    176 		ip->i_ffs_size = 0;
    177 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    178 		return (VOP_UPDATE(vp, &ts, &ts, 0));
    179 	}
    180 #ifdef UVM
    181 	uvm_vnp_setsize(vp, length);
    182 #else
    183 	vnode_pager_setsize(vp, length);
    184 #endif
    185 
    186 	fs = ip->i_lfs;
    187 
    188 	/* If length is larger than the file, just update the times. */
    189 	if (ip->i_ffs_size <= length) {
    190 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    191 		return (VOP_UPDATE(vp, &ts, &ts, 0));
    192 	}
    193 
    194 	/*
    195 	 * Calculate index into inode's block list of last direct and indirect
    196 	 * blocks (if any) which we want to keep.  Lastblock is 0 when the
    197 	 * file is truncated to 0.
    198 	 */
    199 	lastblock = lblkno(fs, length + fs->lfs_bsize - 1);
    200 	olastblock = lblkno(fs, ip->i_ffs_size + fs->lfs_bsize - 1) - 1;
    201 
    202 	/*
    203 	 * Update the size of the file. If the file is not being truncated to
    204 	 * a block boundry, the contents of the partial block following the end
    205 	 * because of subsequent file growth.  For this part of the code,
    206 	 * oldsize_newlast refers to the old size of the new last block in the
    207 	 * file.
    208 	 */
    209 	offset = blkoff(fs, length);
    210 	lbn = lblkno(fs, length);
    211 	oldsize_newlast = blksize(fs, ip, lbn);
    212 
    213 	/* Now set oldsize to the current size of the current last block */
    214 	oldsize_lastblock = blksize(fs, ip, olastblock);
    215 	if (offset == 0)
    216 		ip->i_ffs_size = length;
    217 	else {
    218 #ifdef QUOTA
    219 		if ((e1 = getinoquota(ip)) != 0)
    220 			return (e1);
    221 #endif
    222 		if ((e1 = bread(vp, lbn, oldsize_newlast, NOCRED, &bp)) != 0) {
    223 			brelse(bp);
    224 			return (e1);
    225 		}
    226 		ip->i_ffs_size = length;
    227 #if defined(UVM)
    228 		(void)uvm_vnp_uncache(vp);
    229 #else
    230 		(void)vnode_pager_uncache(vp);
    231 #endif
    232 		newsize = blksize(fs, ip, lbn);
    233 		bzero((char *)bp->b_data + offset, (u_int)(newsize - offset));
    234 		allocbuf(bp, newsize);
    235 		if ((e1 = VOP_BWRITE(bp)) != 0)
    236 			return (e1);
    237 	}
    238 	/*
    239 	 * Modify sup->su_nbyte counters for each deleted block; keep track
    240 	 * of number of blocks removed for ip->i_ffs_blocks.
    241 	 */
    242 	fragsreleased = 0;
    243 	num = 0;
    244 	lastseg = -1;
    245 
    246 	for (lbn = olastblock; lbn >= lastblock;) {
    247 		/* XXX use run length from bmap array to make this faster */
    248 		ufs_bmaparray(vp, lbn, &daddr, a, &depth, NULL);
    249 		if (lbn == olastblock) {
    250 			for (i = NIADDR + 2; i--;)
    251 				a_end[i] = a[i];
    252 			freesize = oldsize_lastblock;
    253 		} else
    254 			freesize = fs->lfs_bsize;
    255 		switch (depth) {
    256 		case 0:				/* Direct block. */
    257 			daddr = ip->i_ffs_db[lbn];
    258 			SEGDEC(freesize);
    259 			ip->i_ffs_db[lbn] = 0;
    260 			--lbn;
    261 			break;
    262 #ifdef DIAGNOSTIC
    263 		case 1:				/* An indirect block. */
    264 			panic("lfs_truncate: ufs_bmaparray returned depth 1");
    265 			/* NOTREACHED */
    266 #endif
    267 		default:			/* Chain of indirect blocks. */
    268 			inp = a + --depth;
    269 			if (inp->in_off > 0 && lbn != lastblock) {
    270 				lbn -= inp->in_off < lbn - lastblock ?
    271 				    inp->in_off : lbn - lastblock;
    272 				break;
    273 			}
    274 			for (; depth && (inp->in_off == 0 || lbn == lastblock);
    275 			    --inp, --depth) {
    276 				if (bread(vp,
    277 				    inp->in_lbn, fs->lfs_bsize, NOCRED, &bp))
    278 					panic("lfs_truncate: bread bno %d",
    279 					    inp->in_lbn);
    280 				daddrp = (ufs_daddr_t *)bp->b_data + inp->in_off;
    281 				for (i = inp->in_off;
    282 				    i++ <= a_end[depth].in_off;) {
    283 					daddr = *daddrp++;
    284 					SEGDEC(freesize);
    285 				}
    286 				a_end[depth].in_off = NINDIR(fs) - 1;
    287 				if (inp->in_off == 0)
    288 					brelse (bp);
    289 				else {
    290 					bzero((ufs_daddr_t *)bp->b_data +
    291 					    inp->in_off, fs->lfs_bsize -
    292 					    inp->in_off * sizeof(ufs_daddr_t));
    293 					if ((e1 = VOP_BWRITE(bp)) != 0)
    294 						return (e1);
    295 				}
    296 			}
    297 			if (depth == 0 && a[1].in_off == 0) {
    298 				off = a[0].in_off;
    299 				daddr = ip->i_ffs_ib[off];
    300 				SEGDEC(freesize);
    301 				ip->i_ffs_ib[off] = 0;
    302 			}
    303 			if (lbn == lastblock || lbn <= NDADDR)
    304 				--lbn;
    305 			else {
    306 				lbn -= NINDIR(fs);
    307 				if (lbn < lastblock)
    308 					lbn = lastblock;
    309 			}
    310 		}
    311 	}
    312 	UPDATE_SEGUSE;
    313 
    314 	/* If truncating the file to 0, update the version number. */
    315 	if (length == 0) {
    316 		LFS_IENTRY(ifp, fs, ip->i_number, bp);
    317 		++ifp->if_version;
    318 		(void) VOP_BWRITE(bp);
    319 	}
    320 
    321 #ifdef DIAGNOSTIC
    322 	if (ip->i_ffs_blocks < fragstodb(fs, fragsreleased)) {
    323 		printf("lfs_truncate: frag count < 0\n");
    324 		fragsreleased = dbtofrags(fs, ip->i_ffs_blocks);
    325 		panic("lfs_truncate: frag count < 0\n");
    326 	}
    327 #endif
    328 	ip->i_ffs_blocks -= fragstodb(fs, fragsreleased);
    329 	fs->lfs_bfree +=  fragstodb(fs, fragsreleased);
    330 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
    331 	/*
    332 	 * Traverse dirty block list counting number of dirty buffers
    333 	 * that are being deleted out of the cache, so that the lfs_avail
    334 	 * field can be updated.
    335 	 */
    336 	a_released = 0;
    337 	i_released = 0;
    338 	for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = bp->b_vnbufs.le_next)
    339 		if (bp->b_flags & B_LOCKED) {
    340 			a_released += numfrags(fs, bp->b_bcount);
    341 			/*
    342 			 * XXX
    343 			 * When buffers are created in the cache, their block
    344 			 * number is set equal to their logical block number.
    345 			 * If that is still true, we are assuming that the
    346 			 * blocks are new (not yet on disk) and weren't
    347 			 * counted above.  However, there is a slight chance
    348 			 * that a block's disk address is equal to its logical
    349 			 * block number in which case, we'll get an overcounting
    350 			 * here.
    351 			 */
    352 			if (bp->b_blkno == bp->b_lblkno)
    353 				i_released += numfrags(fs, bp->b_bcount);
    354 		}
    355 	fragsreleased = i_released;
    356 #ifdef DIAGNOSTIC
    357 	if (fragsreleased > dbtofrags(fs, ip->i_ffs_blocks)) {
    358 		fragsreleased = dbtofrags(fs, ip->i_ffs_blocks);
    359 		panic("lfs_inode: more frags released than in inode");
    360 	}
    361 #endif
    362 	fs->lfs_bfree += fragstodb(fs, fragsreleased);
    363 	ip->i_ffs_blocks -= fragstodb(fs, fragsreleased);
    364 #ifdef DIAGNOSTIC
    365 	if (length == 0 && ip->i_ffs_blocks != 0) {
    366 		printf("lfs_inode: trunc to zero, but %d blocks left on inode",
    367 			ip->i_ffs_blocks);
    368 		panic("lfs_inode");
    369 	}
    370 #endif
    371 	fs->lfs_avail += fragstodb(fs, a_released);
    372 	e1 = vinvalbuf(vp, (length > 0) ? V_SAVE : 0, ap->a_cred, ap->a_p,
    373 	    0, 0);
    374 	e2 = VOP_UPDATE(vp, &ts, &ts, 0);
    375 	return (e1 ? e1 : e2 ? e2 : 0);
    376 }
    377