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