xref: /original-bsd/sys/ufs/ffs/ffs_inode.c (revision f4a18198)
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.7 (Berkeley) 06/15/94
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 	if (length < 0)
139 		return (EINVAL);
140 	oip = VTOI(ovp);
141 	tv = time;
142 	if (ovp->v_type == VLNK &&
143 	    oip->i_size < ovp->v_mount->mnt_maxsymlinklen) {
144 #ifdef DIAGNOSTIC
145 		if (length != 0)
146 			panic("ffs_truncate: partial truncate of symlink");
147 #endif
148 		bzero((char *)&oip->i_shortlink, (u_int)oip->i_size);
149 		oip->i_size = 0;
150 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
151 		return (VOP_UPDATE(ovp, &tv, &tv, 1));
152 	}
153 	if (oip->i_size == length) {
154 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
155 		return (VOP_UPDATE(ovp, &tv, &tv, 0));
156 	}
157 #ifdef QUOTA
158 	if (error = getinoquota(oip))
159 		return (error);
160 #endif
161 	vnode_pager_setsize(ovp, (u_long)length);
162 	fs = oip->i_fs;
163 	osize = oip->i_size;
164 	/*
165 	 * Lengthen the size of the file. We must ensure that the
166 	 * last byte of the file is allocated. Since the smallest
167 	 * value of oszie is 0, length will be at least 1.
168 	 */
169 	if (osize < length) {
170 		offset = blkoff(fs, length - 1);
171 		lbn = lblkno(fs, length - 1);
172 		aflags = B_CLRBUF;
173 		if (ap->a_flags & IO_SYNC)
174 			aflags |= B_SYNC;
175 		if (error = ffs_balloc(oip, lbn, offset + 1, ap->a_cred, &bp,
176 		    aflags))
177 			return (error);
178 		oip->i_size = length;
179 		(void) vnode_pager_uncache(ovp);
180 		if (aflags & B_SYNC)
181 			bwrite(bp);
182 		else
183 			bawrite(bp);
184 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
185 		return (VOP_UPDATE(ovp, &tv, &tv, 1));
186 	}
187 	/*
188 	 * Shorten the size of the file. If the file is not being
189 	 * truncated to a block boundry, the contents of the
190 	 * partial block following the end of the file must be
191 	 * zero'ed in case it ever become accessable again because
192 	 * of subsequent file growth.
193 	 */
194 	offset = blkoff(fs, length);
195 	if (offset == 0) {
196 		oip->i_size = length;
197 	} else {
198 		lbn = lblkno(fs, length);
199 		aflags = B_CLRBUF;
200 		if (ap->a_flags & IO_SYNC)
201 			aflags |= B_SYNC;
202 		if (error = ffs_balloc(oip, lbn, offset, ap->a_cred, &bp,
203 		    aflags))
204 			return (error);
205 		oip->i_size = length;
206 		size = blksize(fs, oip, lbn);
207 		(void) vnode_pager_uncache(ovp);
208 		bzero((char *)bp->b_data + offset, (u_int)(size - offset));
209 		allocbuf(bp, size);
210 		if (aflags & B_SYNC)
211 			bwrite(bp);
212 		else
213 			bawrite(bp);
214 	}
215 	/*
216 	 * Calculate index into inode's block list of
217 	 * last direct and indirect blocks (if any)
218 	 * which we want to keep.  Lastblock is -1 when
219 	 * the file is truncated to 0.
220 	 */
221 	lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
222 	lastiblock[SINGLE] = lastblock - NDADDR;
223 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
224 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
225 	nblocks = btodb(fs->fs_bsize);
226 	/*
227 	 * Update file and block pointers on disk before we start freeing
228 	 * blocks.  If we crash before free'ing blocks below, the blocks
229 	 * will be returned to the free list.  lastiblock values are also
230 	 * normalized to -1 for calls to ffs_indirtrunc below.
231 	 */
232 	bcopy((caddr_t)&oip->i_db[0], (caddr_t)oldblks, sizeof oldblks);
233 	for (level = TRIPLE; level >= SINGLE; level--)
234 		if (lastiblock[level] < 0) {
235 			oip->i_ib[level] = 0;
236 			lastiblock[level] = -1;
237 		}
238 	for (i = NDADDR - 1; i > lastblock; i--)
239 		oip->i_db[i] = 0;
240 	oip->i_flag |= IN_CHANGE | IN_UPDATE;
241 	if (error = VOP_UPDATE(ovp, &tv, &tv, MNT_WAIT))
242 		allerror = error;
243 	/*
244 	 * Having written the new inode to disk, save its new configuration
245 	 * and put back the old block pointers long enough to process them.
246 	 * Note that we save the new block configuration so we can check it
247 	 * when we are done.
248 	 */
249 	bcopy((caddr_t)&oip->i_db[0], (caddr_t)newblks, sizeof newblks);
250 	bcopy((caddr_t)oldblks, (caddr_t)&oip->i_db[0], sizeof oldblks);
251 	oip->i_size = osize;
252 	vflags = ((length > 0) ? V_SAVE : 0) | V_SAVEMETA;
253 	allerror = vinvalbuf(ovp, vflags, ap->a_cred, ap->a_p, 0, 0);
254 
255 	/*
256 	 * Indirect blocks first.
257 	 */
258 	indir_lbn[SINGLE] = -NDADDR;
259 	indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
260 	indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
261 	for (level = TRIPLE; level >= SINGLE; level--) {
262 		bn = oip->i_ib[level];
263 		if (bn != 0) {
264 			error = ffs_indirtrunc(oip, indir_lbn[level],
265 			    fsbtodb(fs, bn), lastiblock[level], level, &count);
266 			if (error)
267 				allerror = error;
268 			blocksreleased += count;
269 			if (lastiblock[level] < 0) {
270 				oip->i_ib[level] = 0;
271 				ffs_blkfree(oip, bn, fs->fs_bsize);
272 				blocksreleased += nblocks;
273 			}
274 		}
275 		if (lastiblock[level] >= 0)
276 			goto done;
277 	}
278 
279 	/*
280 	 * All whole direct blocks or frags.
281 	 */
282 	for (i = NDADDR - 1; i > lastblock; i--) {
283 		register long bsize;
284 
285 		bn = oip->i_db[i];
286 		if (bn == 0)
287 			continue;
288 		oip->i_db[i] = 0;
289 		bsize = blksize(fs, oip, i);
290 		ffs_blkfree(oip, bn, bsize);
291 		blocksreleased += btodb(bsize);
292 	}
293 	if (lastblock < 0)
294 		goto done;
295 
296 	/*
297 	 * Finally, look for a change in size of the
298 	 * last direct block; release any frags.
299 	 */
300 	bn = oip->i_db[lastblock];
301 	if (bn != 0) {
302 		long oldspace, newspace;
303 
304 		/*
305 		 * Calculate amount of space we're giving
306 		 * back as old block size minus new block size.
307 		 */
308 		oldspace = blksize(fs, oip, lastblock);
309 		oip->i_size = length;
310 		newspace = blksize(fs, oip, lastblock);
311 		if (newspace == 0)
312 			panic("itrunc: newspace");
313 		if (oldspace - newspace > 0) {
314 			/*
315 			 * Block number of space to be free'd is
316 			 * the old block # plus the number of frags
317 			 * required for the storage we're keeping.
318 			 */
319 			bn += numfrags(fs, newspace);
320 			ffs_blkfree(oip, bn, oldspace - newspace);
321 			blocksreleased += btodb(oldspace - newspace);
322 		}
323 	}
324 done:
325 #ifdef DIAGNOSTIC
326 	for (level = SINGLE; level <= TRIPLE; level++)
327 		if (newblks[NDADDR + level] != oip->i_ib[level])
328 			panic("itrunc1");
329 	for (i = 0; i < NDADDR; i++)
330 		if (newblks[i] != oip->i_db[i])
331 			panic("itrunc2");
332 	if (length == 0 &&
333 	    (ovp->v_dirtyblkhd.lh_first || ovp->v_cleanblkhd.lh_first))
334 		panic("itrunc3");
335 #endif /* DIAGNOSTIC */
336 	/*
337 	 * Put back the real size.
338 	 */
339 	oip->i_size = length;
340 	oip->i_blocks -= blocksreleased;
341 	if (oip->i_blocks < 0)			/* sanity */
342 		oip->i_blocks = 0;
343 	oip->i_flag |= IN_CHANGE;
344 #ifdef QUOTA
345 	(void) chkdq(oip, -blocksreleased, NOCRED, 0);
346 #endif
347 	return (allerror);
348 }
349 
350 /*
351  * Release blocks associated with the inode ip and stored in the indirect
352  * block bn.  Blocks are free'd in LIFO order up to (but not including)
353  * lastbn.  If level is greater than SINGLE, the block is an indirect block
354  * and recursive calls to indirtrunc must be used to cleanse other indirect
355  * blocks.
356  *
357  * NB: triple indirect blocks are untested.
358  */
359 static int
360 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
361 	register struct inode *ip;
362 	daddr_t lbn, lastbn;
363 	daddr_t dbn;
364 	int level;
365 	long *countp;
366 {
367 	register int i;
368 	struct buf *bp;
369 	register struct fs *fs = ip->i_fs;
370 	register daddr_t *bap;
371 	struct vnode *vp;
372 	daddr_t *copy, nb, nlbn, last;
373 	long blkcount, factor;
374 	int nblocks, blocksreleased = 0;
375 	int error = 0, allerror = 0;
376 
377 	/*
378 	 * Calculate index in current block of last
379 	 * block to be kept.  -1 indicates the entire
380 	 * block so we need not calculate the index.
381 	 */
382 	factor = 1;
383 	for (i = SINGLE; i < level; i++)
384 		factor *= NINDIR(fs);
385 	last = lastbn;
386 	if (lastbn > 0)
387 		last /= factor;
388 	nblocks = btodb(fs->fs_bsize);
389 	/*
390 	 * Get buffer of block pointers, zero those entries corresponding
391 	 * to blocks to be free'd, and update on disk copy first.  Since
392 	 * double(triple) indirect before single(double) indirect, calls
393 	 * to bmap on these blocks will fail.  However, we already have
394 	 * the on disk address, so we have to set the b_blkno field
395 	 * explicitly instead of letting bread do everything for us.
396 	 */
397 	vp = ITOV(ip);
398 	bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0);
399 	if (bp->b_flags & (B_DONE | B_DELWRI)) {
400 		/* Braces must be here in case trace evaluates to nothing. */
401 		trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn);
402 	} else {
403 		trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn);
404 		curproc->p_stats->p_ru.ru_inblock++;	/* pay for read */
405 		bp->b_flags |= B_READ;
406 		if (bp->b_bcount > bp->b_bufsize)
407 			panic("ffs_indirtrunc: bad buffer size");
408 		bp->b_blkno = dbn;
409 		VOP_STRATEGY(bp);
410 		error = biowait(bp);
411 	}
412 	if (error) {
413 		brelse(bp);
414 		*countp = 0;
415 		return (error);
416 	}
417 
418 	bap = (daddr_t *)bp->b_data;
419 	MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
420 	bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize);
421 	bzero((caddr_t)&bap[last + 1],
422 	  (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t));
423 	if (last == -1)
424 		bp->b_flags |= B_INVAL;
425 	error = bwrite(bp);
426 	if (error)
427 		allerror = error;
428 	bap = copy;
429 
430 	/*
431 	 * Recursively free totally unused blocks.
432 	 */
433 	for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
434 	    i--, nlbn += factor) {
435 		nb = bap[i];
436 		if (nb == 0)
437 			continue;
438 		if (level > SINGLE) {
439 			if (error = ffs_indirtrunc(ip, nlbn,
440 			    fsbtodb(fs, nb), (daddr_t)-1, level - 1, &blkcount))
441 				allerror = error;
442 			blocksreleased += blkcount;
443 		}
444 		ffs_blkfree(ip, nb, fs->fs_bsize);
445 		blocksreleased += nblocks;
446 	}
447 
448 	/*
449 	 * Recursively free last partial block.
450 	 */
451 	if (level > SINGLE && lastbn >= 0) {
452 		last = lastbn % factor;
453 		nb = bap[i];
454 		if (nb != 0) {
455 			if (error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
456 			    last, level - 1, &blkcount))
457 				allerror = error;
458 			blocksreleased += blkcount;
459 		}
460 	}
461 	FREE(copy, M_TEMP);
462 	*countp = blocksreleased;
463 	return (allerror);
464 }
465