xref: /csrg-svn/sys/ufs/ffs/ffs_inode.c (revision 57800)
1 /*
2  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)ffs_inode.c	7.67 (Berkeley) 02/02/93
8  */
9 
10 #include <sys/param.h>
11 #include <sys/systm.h>
12 #include <sys/mount.h>
13 #include <sys/proc.h>
14 #include <sys/file.h>
15 #include <sys/buf.h>
16 #include <sys/vnode.h>
17 #include <sys/kernel.h>
18 #include <sys/malloc.h>
19 #include <sys/trace.h>
20 #include <sys/resourcevar.h>
21 
22 #include <vm/vm.h>
23 
24 #include <ufs/ufs/quota.h>
25 #include <ufs/ufs/inode.h>
26 #include <ufs/ufs/ufsmount.h>
27 #include <ufs/ufs/ufs_extern.h>
28 
29 #include <ufs/ffs/fs.h>
30 #include <ufs/ffs/ffs_extern.h>
31 
32 static int ffs_indirtrunc __P((struct inode *, daddr_t, daddr_t, daddr_t, int,
33 	    long *));
34 
35 int
36 ffs_init()
37 {
38 	return (ufs_init());
39 }
40 
41 /*
42  * Update the access, modified, and inode change times as specified
43  * by the IACC, IUPD, and ICHG flags respectively. The IMOD flag
44  * is used to specify that the inode needs to be updated but that
45  * the times have already been set. The access and modified times
46  * are taken from the second and third parameters; the inode change
47  * time is always taken from the current time. If waitfor is set,
48  * then wait for the disk write of the inode to complete.
49  */
50 int
51 ffs_update(ap)
52 	struct vop_update_args /* {
53 		struct vnode *a_vp;
54 		struct timeval *a_ta;
55 		struct timeval *a_tm;
56 		int a_waitfor;
57 	} */ *ap;
58 {
59 	struct buf *bp;
60 	struct inode *ip;
61 	struct dinode *dp;
62 	register struct fs *fs;
63 	int error;
64 
65 	ip = VTOI(ap->a_vp);
66 	if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) {
67 		ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD);
68 		return (0);
69 	}
70 	if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0)
71 		return (0);
72 	if (ip->i_flag&IACC)
73 		ip->i_atime.ts_sec = ap->a_ta->tv_sec;
74 	if (ip->i_flag&IUPD) {
75 		ip->i_mtime.ts_sec = ap->a_tm->tv_sec;
76 		ip->i_modrev++;
77 	}
78 	if (ip->i_flag&ICHG)
79 		ip->i_ctime.ts_sec = time.tv_sec;
80 	ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD);
81 	fs = ip->i_fs;
82 	/*
83 	 * Ensure that uid and gid are correct. This is a temporary
84 	 * fix until fsck has been changed to do the update.
85 	 */
86 	if (fs->fs_inodefmt < FS_44INODEFMT) {		/* XXX */
87 		ip->i_din.di_ouid = ip->i_uid;		/* XXX */
88 		ip->i_din.di_ogid = ip->i_gid;		/* XXX */
89 	}						/* XXX */
90 	if (error = bread(ip->i_devvp, fsbtodb(fs, itod(fs, ip->i_number)),
91 		(int)fs->fs_bsize, NOCRED, &bp)) {
92 		brelse(bp);
93 		return (error);
94 	}
95 	dp = bp->b_un.b_dino + itoo(fs, ip->i_number);
96 	*dp = ip->i_din;
97 	if (ap->a_waitfor)
98 		return (bwrite(bp));
99 	else {
100 		bdwrite(bp);
101 		return (0);
102 	}
103 }
104 
105 #define	SINGLE	0	/* index of single indirect block */
106 #define	DOUBLE	1	/* index of double indirect block */
107 #define	TRIPLE	2	/* index of triple indirect block */
108 /*
109  * Truncate the inode ip to at most length size.  Free affected disk
110  * blocks -- the blocks of the file are removed in reverse order.
111  */
112 ffs_truncate(ap)
113 	struct vop_truncate_args /* {
114 		struct vnode *a_vp;
115 		off_t a_length;
116 		int a_flags;
117 		struct ucred *a_cred;
118 		struct proc *a_p;
119 	} */ *ap;
120 {
121 	register struct vnode *ovp = ap->a_vp;
122 	register daddr_t lastblock;
123 	register struct inode *oip;
124 	daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
125 	off_t length = ap->a_length;
126 	register struct fs *fs;
127 	register struct inode *ip;
128 	struct buf *bp;
129 	int offset, size, level;
130 	long count, nblocks, vflags, blocksreleased = 0;
131 	struct timeval tv;
132 	register int i;
133 	int aflags, error, allerror;
134 	off_t osize;
135 
136 	oip = VTOI(ovp);
137 	tv = time;
138 	if (ovp->v_type == VLNK &&
139 	    oip->i_size < ovp->v_mount->mnt_maxsymlinklen) {
140 #ifdef DIAGNOSTIC
141 		if (length != 0)
142 			panic("ffs_truncate: partial truncate of symlink");
143 #endif
144 		bzero((char *)&oip->i_shortlink, (u_int)oip->i_size);
145 		oip->i_size = 0;
146 		oip->i_flag |= ICHG|IUPD;
147 		return (VOP_UPDATE(ovp, &tv, &tv, 1));
148 	}
149 	if (oip->i_size <= length) {
150 		oip->i_flag |= ICHG|IUPD;
151 		return (VOP_UPDATE(ovp, &tv, &tv, 1));
152 	}
153 	vnode_pager_setsize(ovp, (u_long)length);
154 	/*
155 	 * Calculate index into inode's block list of
156 	 * last direct and indirect blocks (if any)
157 	 * which we want to keep.  Lastblock is -1 when
158 	 * the file is truncated to 0.
159 	 */
160 	fs = oip->i_fs;
161 	lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
162 	lastiblock[SINGLE] = lastblock - NDADDR;
163 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
164 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
165 	nblocks = btodb(fs->fs_bsize);
166 	/*
167 	 * Update the size of the file. If the file is not being
168 	 * truncated to a block boundry, the contents of the
169 	 * partial block following the end of the file must be
170 	 * zero'ed in case it ever become accessable again because
171 	 * of subsequent file growth.
172 	 */
173 	osize = oip->i_size;
174 	offset = blkoff(fs, length);
175 	if (offset == 0) {
176 		oip->i_size = length;
177 	} else {
178 		lbn = lblkno(fs, length);
179 		aflags = B_CLRBUF;
180 		if (ap->a_flags & IO_SYNC)
181 			aflags |= B_SYNC;
182 #ifdef QUOTA
183 		if (error = getinoquota(oip))
184 			return (error);
185 #endif
186 		if (error = ffs_balloc(oip, lbn, offset, ap->a_cred, &bp, aflags))
187 			return (error);
188 		oip->i_size = length;
189 		size = blksize(fs, oip, lbn);
190 		(void) vnode_pager_uncache(ovp);
191 		bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset));
192 		allocbuf(bp, size);
193 		if (ap->a_flags & IO_SYNC)
194 			bwrite(bp);
195 		else
196 			bdwrite(bp);
197 	}
198 	/*
199 	 * Update file and block pointers on disk before we start freeing
200 	 * blocks.  If we crash before free'ing blocks below, the blocks
201 	 * will be returned to the free list.  lastiblock values are also
202 	 * normalized to -1 for calls to ffs_indirtrunc below.
203 	 */
204 	MALLOC(ip, struct inode *, sizeof(*ip), M_FFSNODE, M_WAITOK);
205 	*ip = *oip;
206 	ip->i_size = osize;
207 	for (level = TRIPLE; level >= SINGLE; level--)
208 		if (lastiblock[level] < 0) {
209 			oip->i_ib[level] = 0;
210 			lastiblock[level] = -1;
211 		}
212 	for (i = NDADDR - 1; i > lastblock; i--)
213 		oip->i_db[i] = 0;
214 	oip->i_flag |= ICHG|IUPD;
215 	vflags = ((length > 0) ? V_SAVE : 0) | V_SAVEMETA;
216 	allerror = vinvalbuf(ovp, vflags, ap->a_cred, ap->a_p, 0, 0);
217 	if (error = VOP_UPDATE(ovp, &tv, &tv, MNT_WAIT))
218 		allerror = error;
219 
220 	/*
221 	 * Indirect blocks first.
222 	 */
223 	indir_lbn[SINGLE] = -NDADDR;
224 	indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
225 	indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
226 	ITOV(ip)->v_data = ip;
227 	for (level = TRIPLE; level >= SINGLE; level--) {
228 		bn = ip->i_ib[level];
229 		if (bn != 0) {
230 			error = ffs_indirtrunc(ip, indir_lbn[level],
231 			    fsbtodb(fs, bn), lastiblock[level], level, &count);
232 			if (error)
233 				allerror = error;
234 			blocksreleased += count;
235 			if (lastiblock[level] < 0) {
236 				ip->i_ib[level] = 0;
237 				ffs_blkfree(ip, bn, fs->fs_bsize);
238 				blocksreleased += nblocks;
239 			}
240 		}
241 		if (lastiblock[level] >= 0)
242 			goto done;
243 	}
244 
245 	/*
246 	 * All whole direct blocks or frags.
247 	 */
248 	for (i = NDADDR - 1; i > lastblock; i--) {
249 		register long bsize;
250 
251 		bn = ip->i_db[i];
252 		if (bn == 0)
253 			continue;
254 		ip->i_db[i] = 0;
255 		bsize = blksize(fs, ip, i);
256 		ffs_blkfree(ip, bn, bsize);
257 		blocksreleased += btodb(bsize);
258 	}
259 	if (lastblock < 0)
260 		goto done;
261 
262 	/*
263 	 * Finally, look for a change in size of the
264 	 * last direct block; release any frags.
265 	 */
266 	bn = ip->i_db[lastblock];
267 	if (bn != 0) {
268 		long oldspace, newspace;
269 
270 		/*
271 		 * Calculate amount of space we're giving
272 		 * back as old block size minus new block size.
273 		 */
274 		oldspace = blksize(fs, ip, lastblock);
275 		ip->i_size = length;
276 		newspace = blksize(fs, ip, lastblock);
277 		if (newspace == 0)
278 			panic("itrunc: newspace");
279 		if (oldspace - newspace > 0) {
280 			/*
281 			 * Block number of space to be free'd is
282 			 * the old block # plus the number of frags
283 			 * required for the storage we're keeping.
284 			 */
285 			bn += numfrags(fs, newspace);
286 			ffs_blkfree(ip, bn, oldspace - newspace);
287 			blocksreleased += btodb(oldspace - newspace);
288 		}
289 	}
290 done:
291 #ifdef DIAGNOSTIC
292 	for (level = SINGLE; level <= TRIPLE; level++)
293 		if (ip->i_ib[level] != oip->i_ib[level])
294 			panic("itrunc1");
295 	for (i = 0; i < NDADDR; i++)
296 		if (ip->i_db[i] != oip->i_db[i])
297 			panic("itrunc2");
298 	if (length == 0 &&
299 	    (ovp->v_dirtyblkhd.le_next || ovp->v_cleanblkhd.le_next))
300 		panic("itrunc3");
301 #endif /* DIAGNOSTIC */
302 	ITOV(ip)->v_data = oip;
303 	FREE(ip, M_FFSNODE);
304 	oip->i_blocks -= blocksreleased;
305 	if (oip->i_blocks < 0)			/* sanity */
306 		oip->i_blocks = 0;
307 	oip->i_flag |= ICHG;
308 #ifdef QUOTA
309 	if (!getinoquota(oip))
310 		(void) chkdq(oip, -blocksreleased, NOCRED, 0);
311 #endif
312 	return (allerror);
313 }
314 
315 /*
316  * Release blocks associated with the inode ip and stored in the indirect
317  * block bn.  Blocks are free'd in LIFO order up to (but not including)
318  * lastbn.  If level is greater than SINGLE, the block is an indirect block
319  * and recursive calls to indirtrunc must be used to cleanse other indirect
320  * blocks.
321  *
322  * NB: triple indirect blocks are untested.
323  */
324 static int
325 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
326 	register struct inode *ip;
327 	daddr_t lbn, lastbn;
328 	daddr_t dbn;
329 	int level;
330 	long *countp;
331 {
332 	register int i;
333 	struct buf *bp;
334 	register struct fs *fs = ip->i_fs;
335 	register daddr_t *bap;
336 	struct vnode *vp;
337 	daddr_t *copy, nb, nlbn, last;
338 	long blkcount, factor;
339 	int nblocks, blocksreleased = 0;
340 	int error = 0, allerror = 0;
341 
342 	/*
343 	 * Calculate index in current block of last
344 	 * block to be kept.  -1 indicates the entire
345 	 * block so we need not calculate the index.
346 	 */
347 	factor = 1;
348 	for (i = SINGLE; i < level; i++)
349 		factor *= NINDIR(fs);
350 	last = lastbn;
351 	if (lastbn > 0)
352 		last /= factor;
353 	nblocks = btodb(fs->fs_bsize);
354 	/*
355 	 * Get buffer of block pointers, zero those entries corresponding
356 	 * to blocks to be free'd, and update on disk copy first.  Since
357 	 * double(triple) indirect before single(double) indirect, calls
358 	 * to bmap on these blocks will fail.  However, we already have
359 	 * the on disk address, so we have to set the b_blkno field
360 	 * explicitly instead of letting bread do everything for us.
361 	 */
362 	vp = ITOV(ip);
363 	bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0);
364 	if (bp->b_flags & (B_DONE | B_DELWRI)) {
365 		/* Braces must be here in case trace evaluates to nothing. */
366 		trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn);
367 	} else {
368 		trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn);
369 		curproc->p_stats->p_ru.ru_inblock++;	/* pay for read */
370 		bp->b_flags |= B_READ;
371 		if (bp->b_bcount > bp->b_bufsize)
372 			panic("ffs_indirtrunc: bad buffer size");
373 		bp->b_blkno = dbn;
374 		VOP_STRATEGY(bp);
375 		error = biowait(bp);
376 	}
377 	if (error) {
378 		brelse(bp);
379 		*countp = 0;
380 		return (error);
381 	}
382 
383 	bap = bp->b_un.b_daddr;
384 	MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
385 	bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize);
386 	bzero((caddr_t)&bap[last + 1],
387 	  (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t));
388 	if (last == -1)
389 		bp->b_flags |= B_INVAL;
390 	error = bwrite(bp);
391 	if (error)
392 		allerror = error;
393 	bap = copy;
394 
395 	/*
396 	 * Recursively free totally unused blocks.
397 	 */
398 	for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
399 	    i--, nlbn += factor) {
400 		nb = bap[i];
401 		if (nb == 0)
402 			continue;
403 		if (level > SINGLE) {
404 			if (error = ffs_indirtrunc(ip, nlbn,
405 			    fsbtodb(fs, nb), (daddr_t)-1, level - 1, &blkcount))
406 				allerror = error;
407 			blocksreleased += blkcount;
408 		}
409 		ffs_blkfree(ip, nb, fs->fs_bsize);
410 		blocksreleased += nblocks;
411 	}
412 
413 	/*
414 	 * Recursively free last partial block.
415 	 */
416 	if (level > SINGLE && lastbn >= 0) {
417 		last = lastbn % factor;
418 		nb = bap[i];
419 		if (nb != 0) {
420 			if (error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
421 			    last, level - 1, &blkcount))
422 				allerror = error;
423 			blocksreleased += blkcount;
424 		}
425 	}
426 	FREE(copy, M_TEMP);
427 	*countp = blocksreleased;
428 	return (allerror);
429 }
430