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