xref: /netbsd-src/sys/ufs/ffs/ffs_inode.c (revision e4d7c2e329d54c97e0c0bd3016bbe74f550c3d5e)
1 /*	$NetBSD: ffs_inode.c,v 1.29 1999/11/15 18:49:13 fvdl Exp $	*/
2 
3 /*
4  * Copyright (c) 1982, 1986, 1989, 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  *	@(#)ffs_inode.c	8.13 (Berkeley) 4/21/95
36  */
37 
38 #if defined(_KERNEL) && !defined(_LKM)
39 #include "opt_ffs.h"
40 #include "opt_quota.h"
41 #endif
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/mount.h>
46 #include <sys/proc.h>
47 #include <sys/file.h>
48 #include <sys/buf.h>
49 #include <sys/vnode.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/trace.h>
53 #include <sys/resourcevar.h>
54 
55 #include <vm/vm.h>
56 
57 #include <uvm/uvm_extern.h>
58 
59 #include <ufs/ufs/quota.h>
60 #include <ufs/ufs/inode.h>
61 #include <ufs/ufs/ufsmount.h>
62 #include <ufs/ufs/ufs_extern.h>
63 #include <ufs/ufs/ufs_bswap.h>
64 
65 #include <ufs/ffs/fs.h>
66 #include <ufs/ffs/ffs_extern.h>
67 
68 static int ffs_indirtrunc __P((struct inode *, ufs_daddr_t, ufs_daddr_t,
69 			       ufs_daddr_t, int, long *));
70 
71 /*
72  * Update the access, modified, and inode change times as specified
73  * by the IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively.
74  * The IN_MODIFIED flag is used to specify that the inode needs to be
75  * updated but that the times have already been set. The access
76  * and modified times are taken from the second and third parameters;
77  * the inode change time is always taken from the current time. If
78  * waitfor is set, then wait for the disk write of the inode to
79  * complete.
80  */
81 
82 int
83 ffs_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 	register struct fs *fs;
93 	struct buf *bp;
94 	struct inode *ip;
95 	int error;
96 	struct timespec ts;
97 	caddr_t cp;
98 
99 	if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
100 		return (0);
101 	ip = VTOI(ap->a_vp);
102 	TIMEVAL_TO_TIMESPEC(&time, &ts);
103 	FFS_ITIMES(ip,
104 	    ap->a_access ? ap->a_access : &ts,
105 	    ap->a_modify ? ap->a_modify : &ts, &ts);
106 	if ((ip->i_flag & IN_MODIFIED) == 0 && ap->a_waitfor != MNT_WAIT)
107 		return (0);
108 	ip->i_flag &= ~IN_MODIFIED;
109 	fs = ip->i_fs;
110 	/*
111 	 * Ensure that uid and gid are correct. This is a temporary
112 	 * fix until fsck has been changed to do the update.
113 	 */
114 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
115 		ip->i_din.ffs_din.di_ouid = ip->i_ffs_uid;	/* XXX */
116 		ip->i_din.ffs_din.di_ogid = ip->i_ffs_gid;	/* XXX */
117 	}							/* XXX */
118 	error = bread(ip->i_devvp,
119 		      fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
120 		      (int)fs->fs_bsize, NOCRED, &bp);
121 	if (error) {
122 		brelse(bp);
123 		return (error);
124 	}
125 	if (DOINGSOFTDEP(ap->a_vp))
126 		softdep_update_inodeblock(ip, bp, ap->a_waitfor);
127 	else if (ip->i_ffs_effnlink != ip->i_ffs_nlink)
128 		panic("ffs_update: bad link cnt");
129 	cp = (caddr_t)bp->b_data +
130 	    (ino_to_fsbo(fs, ip->i_number) * DINODE_SIZE);
131 #ifdef FFS_EI
132 	if (UFS_FSNEEDSWAP(fs))
133 		ffs_dinode_swap(&ip->i_din.ffs_din, (struct dinode *)cp);
134 	else
135 #endif
136 		memcpy(cp, &ip->i_din.ffs_din, DINODE_SIZE);
137 	if (ap->a_waitfor && (ap->a_vp->v_mount->mnt_flag & MNT_ASYNC) == 0) {
138 		return (bwrite(bp));
139 	} else {
140 		bdwrite(bp);
141 		return (0);
142 	}
143 }
144 
145 #define	SINGLE	0	/* index of single indirect block */
146 #define	DOUBLE	1	/* index of double indirect block */
147 #define	TRIPLE	2	/* index of triple indirect block */
148 /*
149  * Truncate the inode oip to at most length size, freeing the
150  * disk blocks.
151  */
152 int
153 ffs_truncate(v)
154 	void *v;
155 {
156 	struct vop_truncate_args /* {
157 		struct vnode *a_vp;
158 		off_t a_length;
159 		int a_flags;
160 		struct ucred *a_cred;
161 		struct proc *a_p;
162 	} */ *ap = v;
163 	register struct vnode *ovp = ap->a_vp;
164 	register ufs_daddr_t lastblock;
165 	register struct inode *oip;
166 	ufs_daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
167 	ufs_daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
168 	off_t length = ap->a_length;
169 	register struct fs *fs;
170 	struct buf *bp;
171 	int offset, size, level;
172 	long count, nblocks, vflags, blocksreleased = 0;
173 	register int i;
174 	int aflags, error, allerror;
175 	off_t osize;
176 
177 	if (length < 0)
178 		return (EINVAL);
179 	oip = VTOI(ovp);
180 #if 1
181 	/*
182 	 * XXX. Was in Kirk's patches. Is it good behavior to just
183 	 * return and not update modification times?
184 	 */
185 	if (oip->i_ffs_size == length)
186 		return (0);
187 #endif
188 	if (ovp->v_type == VLNK &&
189 	    (oip->i_ffs_size < ovp->v_mount->mnt_maxsymlinklen ||
190 	     (ovp->v_mount->mnt_maxsymlinklen == 0 &&
191 	      oip->i_din.ffs_din.di_blocks == 0))) {
192 #ifdef DIAGNOSTIC
193 		if (length != 0)
194 			panic("ffs_truncate: partial truncate of symlink");
195 #endif
196 		memset((char *)&oip->i_ffs_shortlink, 0, (u_int)oip->i_ffs_size);
197 		oip->i_ffs_size = 0;
198 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
199 		return (VOP_UPDATE(ovp, NULL, NULL, 1));
200 	}
201 	if (oip->i_ffs_size == length) {
202 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
203 		return (VOP_UPDATE(ovp, NULL, NULL, 0));
204 	}
205 #ifdef QUOTA
206 	if ((error = getinoquota(oip)) != 0)
207 		return (error);
208 #endif
209 	fs = oip->i_fs;
210 	osize = oip->i_ffs_size;
211 	ovp->v_lasta = ovp->v_clen = ovp->v_cstart = ovp->v_lastw = 0;
212 
213 	if (DOINGSOFTDEP(ovp)) {
214 		uvm_vnp_setsize(ovp, length);
215 		(void) uvm_vnp_uncache(ovp);
216 		if (length > 0) {
217 			/*
218 			 * If a file is only partially truncated, then
219 			 * we have to clean up the data structures
220 			 * describing the allocation past the truncation
221 			 * point. Finding and deallocating those structures
222 			 * is a lot of work. Since partial truncation occurs
223 			 * rarely, we solve the problem by syncing the file
224 			 * so that it will have no data structures left.
225 			 */
226 			if ((error = VOP_FSYNC(ovp, ap->a_cred, FSYNC_WAIT,
227 			    ap->a_p)) != 0)
228 				return (error);
229 		} else {
230 #ifdef QUOTA
231  			(void) chkdq(oip, -oip->i_ffs_blocks, NOCRED, 0);
232 #endif
233 			softdep_setup_freeblocks(oip, length);
234 			(void) vinvalbuf(ovp, 0, ap->a_cred, ap->a_p, 0, 0);
235 			oip->i_flag |= IN_CHANGE | IN_UPDATE;
236 			return (VOP_UPDATE(ovp, NULL, NULL, 0));
237 		}
238 	}
239 	/*
240 	 * Lengthen the size of the file. We must ensure that the
241 	 * last byte of the file is allocated. Since the smallest
242 	 * value of osize is 0, length will be at least 1.
243 	 */
244 	if (osize < length) {
245 		if (length > fs->fs_maxfilesize)
246 			return (EFBIG);
247 		aflags = B_CLRBUF;
248 		if (ap->a_flags & IO_SYNC)
249 			aflags |= B_SYNC;
250 		error = VOP_BALLOC(ovp, length - 1, 1, ap->a_cred, aflags, &bp);
251 		if (error)
252 			return (error);
253 		oip->i_ffs_size = length;
254 		uvm_vnp_setsize(ovp, length);
255 		(void) uvm_vnp_uncache(ovp);
256 		if (aflags & B_SYNC)
257 			bwrite(bp);
258 		else
259 			bawrite(bp);
260 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
261 		return (VOP_UPDATE(ovp, NULL, NULL, 1));
262 	}
263 	/*
264 	 * Shorten the size of the file. If the file is not being
265 	 * truncated to a block boundary, the contents of the
266 	 * partial block following the end of the file must be
267 	 * zero'ed in case it ever becomes accessible again because
268 	 * of subsequent file growth. Directories however are not
269 	 * zero'ed as they should grow back initialized to empty.
270 	 */
271 	offset = blkoff(fs, length);
272 	if (offset == 0) {
273 		oip->i_ffs_size = length;
274 	} else {
275 		lbn = lblkno(fs, length);
276 		aflags = B_CLRBUF;
277 		if (ap->a_flags & IO_SYNC)
278 			aflags |= B_SYNC;
279 		error = VOP_BALLOC(ovp, length - 1, 1, ap->a_cred, aflags, &bp);
280 		if (error)
281 			return (error);
282 		oip->i_ffs_size = length;
283 		size = blksize(fs, oip, lbn);
284 		(void) uvm_vnp_uncache(ovp);
285 		if (ovp->v_type != VDIR)
286 			memset((char *)bp->b_data + offset, 0,
287 			       (u_int)(size - offset));
288 		allocbuf(bp, size);
289 		if (aflags & B_SYNC)
290 			bwrite(bp);
291 		else
292 			bawrite(bp);
293 	}
294 	uvm_vnp_setsize(ovp, length);
295 	/*
296 	 * Calculate index into inode's block list of
297 	 * last direct and indirect blocks (if any)
298 	 * which we want to keep.  Lastblock is -1 when
299 	 * the file is truncated to 0.
300 	 */
301 	lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
302 	lastiblock[SINGLE] = lastblock - NDADDR;
303 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
304 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
305 	nblocks = btodb(fs->fs_bsize);
306 	/*
307 	 * Update file and block pointers on disk before we start freeing
308 	 * blocks.  If we crash before free'ing blocks below, the blocks
309 	 * will be returned to the free list.  lastiblock values are also
310 	 * normalized to -1 for calls to ffs_indirtrunc below.
311 	 */
312 	memcpy((caddr_t)oldblks, (caddr_t)&oip->i_ffs_db[0], sizeof oldblks);
313 	for (level = TRIPLE; level >= SINGLE; level--)
314 		if (lastiblock[level] < 0) {
315 			oip->i_ffs_ib[level] = 0;
316 			lastiblock[level] = -1;
317 		}
318 	for (i = NDADDR - 1; i > lastblock; i--)
319 		oip->i_ffs_db[i] = 0;
320 	oip->i_flag |= IN_CHANGE | IN_UPDATE;
321 	if ((error = VOP_UPDATE(ovp, NULL, NULL, 1)) != 0)
322 		allerror = error;
323 	/*
324 	 * Having written the new inode to disk, save its new configuration
325 	 * and put back the old block pointers long enough to process them.
326 	 * Note that we save the new block configuration so we can check it
327 	 * when we are done.
328 	 */
329 	memcpy((caddr_t)newblks, (caddr_t)&oip->i_ffs_db[0], sizeof newblks);
330 	memcpy((caddr_t)&oip->i_ffs_db[0], (caddr_t)oldblks, sizeof oldblks);
331 	oip->i_ffs_size = osize;
332 	vflags = ((length > 0) ? V_SAVE : 0) | V_SAVEMETA;
333 	allerror = vinvalbuf(ovp, vflags, ap->a_cred, ap->a_p, 0, 0);
334 
335 	/*
336 	 * Indirect blocks first.
337 	 */
338 	indir_lbn[SINGLE] = -NDADDR;
339 	indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
340 	indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
341 	for (level = TRIPLE; level >= SINGLE; level--) {
342 		bn = ufs_rw32(oip->i_ffs_ib[level], UFS_FSNEEDSWAP(fs));
343 		if (bn != 0) {
344 			error = ffs_indirtrunc(oip, indir_lbn[level],
345 			    fsbtodb(fs, bn), lastiblock[level], level, &count);
346 			if (error)
347 				allerror = error;
348 			blocksreleased += count;
349 			if (lastiblock[level] < 0) {
350 				oip->i_ffs_ib[level] = 0;
351 				ffs_blkfree(oip, bn, fs->fs_bsize);
352 				blocksreleased += nblocks;
353 			}
354 		}
355 		if (lastiblock[level] >= 0)
356 			goto done;
357 	}
358 
359 	/*
360 	 * All whole direct blocks or frags.
361 	 */
362 	for (i = NDADDR - 1; i > lastblock; i--) {
363 		register long bsize;
364 
365 		bn = ufs_rw32(oip->i_ffs_db[i], UFS_FSNEEDSWAP(fs));
366 		if (bn == 0)
367 			continue;
368 		oip->i_ffs_db[i] = 0;
369 		bsize = blksize(fs, oip, i);
370 		ffs_blkfree(oip, bn, bsize);
371 		blocksreleased += btodb(bsize);
372 	}
373 	if (lastblock < 0)
374 		goto done;
375 
376 	/*
377 	 * Finally, look for a change in size of the
378 	 * last direct block; release any frags.
379 	 */
380 	bn = ufs_rw32(oip->i_ffs_db[lastblock], UFS_FSNEEDSWAP(fs));
381 	if (bn != 0) {
382 		long oldspace, newspace;
383 
384 		/*
385 		 * Calculate amount of space we're giving
386 		 * back as old block size minus new block size.
387 		 */
388 		oldspace = blksize(fs, oip, lastblock);
389 		oip->i_ffs_size = length;
390 		newspace = blksize(fs, oip, lastblock);
391 		if (newspace == 0)
392 			panic("itrunc: newspace");
393 		if (oldspace - newspace > 0) {
394 			/*
395 			 * Block number of space to be free'd is
396 			 * the old block # plus the number of frags
397 			 * required for the storage we're keeping.
398 			 */
399 			bn += numfrags(fs, newspace);
400 			ffs_blkfree(oip, bn, oldspace - newspace);
401 			blocksreleased += btodb(oldspace - newspace);
402 		}
403 	}
404 done:
405 #ifdef DIAGNOSTIC
406 	for (level = SINGLE; level <= TRIPLE; level++)
407 		if (newblks[NDADDR + level] != oip->i_ffs_ib[level])
408 			panic("itrunc1");
409 	for (i = 0; i < NDADDR; i++)
410 		if (newblks[i] != oip->i_ffs_db[i])
411 			panic("itrunc2");
412 	if (length == 0 &&
413 	    (ovp->v_dirtyblkhd.lh_first || ovp->v_cleanblkhd.lh_first))
414 		panic("itrunc3");
415 #endif /* DIAGNOSTIC */
416 	/*
417 	 * Put back the real size.
418 	 */
419 	oip->i_ffs_size = length;
420 	oip->i_ffs_blocks -= blocksreleased;
421 	if (oip->i_ffs_blocks < 0)			/* sanity */
422 		oip->i_ffs_blocks = 0;
423 	oip->i_flag |= IN_CHANGE;
424 #ifdef QUOTA
425 	(void) chkdq(oip, -blocksreleased, NOCRED, 0);
426 #endif
427 	return (allerror);
428 }
429 
430 /*
431  * Release blocks associated with the inode ip and stored in the indirect
432  * block bn.  Blocks are free'd in LIFO order up to (but not including)
433  * lastbn.  If level is greater than SINGLE, the block is an indirect block
434  * and recursive calls to indirtrunc must be used to cleanse other indirect
435  * blocks.
436  *
437  * NB: triple indirect blocks are untested.
438  */
439 static int
440 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
441 	register struct inode *ip;
442 	ufs_daddr_t lbn, lastbn;
443 	ufs_daddr_t dbn;
444 	int level;
445 	long *countp;
446 {
447 	register int i;
448 	struct buf *bp;
449 	register struct fs *fs = ip->i_fs;
450 	register ufs_daddr_t *bap;
451 	struct vnode *vp;
452 	ufs_daddr_t *copy = NULL, nb, nlbn, last;
453 	long blkcount, factor;
454 	int nblocks, blocksreleased = 0;
455 	int error = 0, allerror = 0;
456 
457 	/*
458 	 * Calculate index in current block of last
459 	 * block to be kept.  -1 indicates the entire
460 	 * block so we need not calculate the index.
461 	 */
462 	factor = 1;
463 	for (i = SINGLE; i < level; i++)
464 		factor *= NINDIR(fs);
465 	last = lastbn;
466 	if (lastbn > 0)
467 		last /= factor;
468 	nblocks = btodb(fs->fs_bsize);
469 	/*
470 	 * Get buffer of block pointers, zero those entries corresponding
471 	 * to blocks to be free'd, and update on disk copy first.  Since
472 	 * double(triple) indirect before single(double) indirect, calls
473 	 * to bmap on these blocks will fail.  However, we already have
474 	 * the on disk address, so we have to set the b_blkno field
475 	 * explicitly instead of letting bread do everything for us.
476 	 */
477 	vp = ITOV(ip);
478 	bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0);
479 	if (bp->b_flags & (B_DONE | B_DELWRI)) {
480 		/* Braces must be here in case trace evaluates to nothing. */
481 		trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn);
482 	} else {
483 		trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn);
484 		curproc->p_stats->p_ru.ru_inblock++;	/* pay for read */
485 		bp->b_flags |= B_READ;
486 		if (bp->b_bcount > bp->b_bufsize)
487 			panic("ffs_indirtrunc: bad buffer size");
488 		bp->b_blkno = dbn;
489 		VOP_STRATEGY(bp);
490 		error = biowait(bp);
491 	}
492 	if (error) {
493 		brelse(bp);
494 		*countp = 0;
495 		return (error);
496 	}
497 
498 	bap = (ufs_daddr_t *)bp->b_data;
499 	if (lastbn != -1) {
500 		MALLOC(copy, ufs_daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
501 		memcpy((caddr_t)copy, (caddr_t)bap, (u_int)fs->fs_bsize);
502 		memset((caddr_t)&bap[last + 1], 0,
503 		  (u_int)(NINDIR(fs) - (last + 1)) * sizeof (ufs_daddr_t));
504 		error = bwrite(bp);
505 		if (error)
506 			allerror = error;
507 		bap = copy;
508 	}
509 
510 	/*
511 	 * Recursively free totally unused blocks.
512 	 */
513 	for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
514 	    i--, nlbn += factor) {
515 		nb = ufs_rw32(bap[i], UFS_FSNEEDSWAP(fs));
516 		if (nb == 0)
517 			continue;
518 		if (level > SINGLE) {
519 			error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
520 					       (ufs_daddr_t)-1, level - 1,
521 					       &blkcount);
522 			if (error)
523 				allerror = error;
524 			blocksreleased += blkcount;
525 		}
526 		ffs_blkfree(ip, nb, fs->fs_bsize);
527 		blocksreleased += nblocks;
528 	}
529 
530 	/*
531 	 * Recursively free last partial block.
532 	 */
533 	if (level > SINGLE && lastbn >= 0) {
534 		last = lastbn % factor;
535 		nb = ufs_rw32(bap[i], UFS_FSNEEDSWAP(fs));
536 		if (nb != 0) {
537 			error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
538 					       last, level - 1, &blkcount);
539 			if (error)
540 				allerror = error;
541 			blocksreleased += blkcount;
542 		}
543 	}
544 
545 	if (copy != NULL) {
546 		FREE(copy, M_TEMP);
547 	} else {
548 		bp->b_flags |= B_INVAL;
549 		brelse(bp);
550 	}
551 
552 	*countp = blocksreleased;
553 	return (allerror);
554 }
555