xref: /freebsd/sys/ufs/ffs/ffs_inode.c (revision 0de89efe5c443f213c7ea28773ef2dc6cf3af2ed)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)ffs_inode.c	8.13 (Berkeley) 4/21/95
34  * $Id: ffs_inode.c,v 1.26 1997/03/22 06:53:29 bde Exp $
35  */
36 
37 #include "opt_quota.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/mount.h>
42 #include <sys/proc.h>
43 #include <sys/buf.h>
44 #include <sys/vnode.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/resourcevar.h>
48 
49 #include <vm/vm.h>
50 #include <vm/vm_extern.h>
51 
52 #include <ufs/ufs/quota.h>
53 #include <ufs/ufs/inode.h>
54 
55 #include <ufs/ffs/fs.h>
56 #include <ufs/ffs/ffs_extern.h>
57 
58 static int ffs_indirtrunc __P((struct inode *, ufs_daddr_t, ufs_daddr_t,
59 	    ufs_daddr_t, int, long *));
60 
61 /*
62  * Update the access, modified, and inode change times as specified by the
63  * IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively. The IN_MODIFIED
64  * flag is used to specify that the inode needs to be updated even if none
65  * of the times needs to be updated. The access and modified times are taken
66  * from the second and third parameters; the inode change time is always
67  * taken from the current time. If waitfor is set, then wait for the disk
68  * write of the inode to complete.
69  */
70 int
71 ffs_update(ap)
72 	struct vop_update_args /* {
73 		struct vnode *a_vp;
74 		struct timeval *a_access;
75 		struct timeval *a_modify;
76 		int a_waitfor;
77 	} */ *ap;
78 {
79 	register struct fs *fs;
80 	struct buf *bp;
81 	struct inode *ip;
82 	int error;
83 	time_t tv_sec;
84 
85 	ip = VTOI(ap->a_vp);
86 	if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) {
87 		ip->i_flag &=
88 		    ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE);
89 		return (0);
90 	}
91 	if ((ip->i_flag &
92 	    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0)
93 		return (0);
94 	/*
95 	 * Use a copy of the current time to get consistent timestamps
96 	 * (a_access and a_modify are sometimes aliases for &time).
97 	 *
98 	 * XXX in 2.0, a_access and a_modify are often pointers to the
99 	 * same copy of `time'.  This is not as good.  Some callers forget
100 	 * to make a copy; others make a copy too early (before the i/o
101 	 * has completed)...
102 	 *
103 	 * XXX there should be a function or macro for reading the time
104 	 * (e.g., some machines may require splclock()).
105 	 */
106 	tv_sec = time.tv_sec;
107 	if (ip->i_flag & IN_ACCESS)
108 		ip->i_atime =
109 		    (ap->a_access == &time ? tv_sec : ap->a_access->tv_sec);
110 	if (ip->i_flag & IN_UPDATE) {
111 		ip->i_mtime =
112 		    (ap->a_modify == &time ? tv_sec : ap->a_modify->tv_sec);
113 		ip->i_modrev++;
114 	}
115 	if (ip->i_flag & IN_CHANGE)
116 		ip->i_ctime = tv_sec;
117 	ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE);
118 	fs = ip->i_fs;
119 	/*
120 	 * Ensure that uid and gid are correct. This is a temporary
121 	 * fix until fsck has been changed to do the update.
122 	 */
123 	if (fs->fs_inodefmt < FS_44INODEFMT) {		/* XXX */
124 		ip->i_din.di_ouid = ip->i_uid;		/* XXX */
125 		ip->i_din.di_ogid = ip->i_gid;		/* XXX */
126 	}						/* XXX */
127 	error = bread(ip->i_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
128 		(int)fs->fs_bsize, NOCRED, &bp);
129 	if (error) {
130 		brelse(bp);
131 		return (error);
132 	}
133 	*((struct dinode *)bp->b_data +
134 	    ino_to_fsbo(fs, ip->i_number)) = ip->i_din;
135 	if (ap->a_waitfor && (ap->a_vp->v_mount->mnt_flag & MNT_ASYNC) == 0)
136 		return (bwrite(bp));
137 	else {
138 		bp->b_flags |= B_CLUSTEROK;
139 		bdwrite(bp);
140 		return (0);
141 	}
142 }
143 
144 #define	SINGLE	0	/* index of single indirect block */
145 #define	DOUBLE	1	/* index of double indirect block */
146 #define	TRIPLE	2	/* index of triple indirect block */
147 /*
148  * Truncate the inode oip to at most length size, freeing the
149  * disk blocks.
150  */
151 int
152 ffs_truncate(ap)
153 	struct vop_truncate_args /* {
154 		struct vnode *a_vp;
155 		off_t a_length;
156 		int a_flags;
157 		struct ucred *a_cred;
158 		struct proc *a_p;
159 	} */ *ap;
160 {
161 	register struct vnode *ovp = ap->a_vp;
162 	ufs_daddr_t lastblock;
163 	register struct inode *oip;
164 	ufs_daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
165 	ufs_daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
166 	off_t length = ap->a_length;
167 	register struct fs *fs;
168 	struct buf *bp;
169 	int offset, size, level;
170 	long count, nblocks, vflags, blocksreleased = 0;
171 	struct timeval tv;
172 	register int i;
173 	int aflags, error, allerror;
174 	off_t osize;
175 
176 	oip = VTOI(ovp);
177 	fs = oip->i_fs;
178 	if (length < 0)
179 		return (EINVAL);
180 	if (length > fs->fs_maxfilesize)
181 		return (EFBIG);
182 	gettime(&tv);
183 	if (ovp->v_type == VLNK &&
184 	    (oip->i_size < ovp->v_mount->mnt_maxsymlinklen || oip->i_din.di_blocks == 0)) {
185 #ifdef DIAGNOSTIC
186 		if (length != 0)
187 			panic("ffs_truncate: partial truncate of symlink");
188 #endif
189 		bzero((char *)&oip->i_shortlink, (u_int)oip->i_size);
190 		oip->i_size = 0;
191 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
192 		return (VOP_UPDATE(ovp, &tv, &tv, 1));
193 	}
194 	if (oip->i_size == length) {
195 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
196 		return (VOP_UPDATE(ovp, &tv, &tv, 0));
197 	}
198 #ifdef QUOTA
199 	error = getinoquota(oip);
200 	if (error)
201 		return (error);
202 #endif
203 	osize = oip->i_size;
204 	/*
205 	 * Lengthen the size of the file. We must ensure that the
206 	 * last byte of the file is allocated. Since the smallest
207 	 * value of osize is 0, length will be at least 1.
208 	 */
209 	if (osize < length) {
210 		offset = blkoff(fs, length - 1);
211 		lbn = lblkno(fs, length - 1);
212 		aflags = B_CLRBUF;
213 		if (ap->a_flags & IO_SYNC)
214 			aflags |= B_SYNC;
215 		error = ffs_balloc(oip, lbn, offset + 1, ap->a_cred,
216 		    &bp, aflags);
217 		if (error)
218 			return (error);
219 		oip->i_size = length;
220 		vnode_pager_setsize(ovp, length);
221 		if (aflags & B_SYNC)
222 			bwrite(bp);
223 		else if (ovp->v_mount->mnt_flag & MNT_ASYNC)
224 			bdwrite(bp);
225 		else
226 			bawrite(bp);
227 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
228 		return (VOP_UPDATE(ovp, &tv, &tv, 1));
229 	}
230 	/*
231 	 * Shorten the size of the file. If the file is not being
232 	 * truncated to a block boundry, the contents of the
233 	 * partial block following the end of the file must be
234 	 * zero'ed in case it ever become accessable again because
235 	 * of subsequent file growth.
236 	 */
237 	offset = blkoff(fs, length);
238 	if (offset == 0) {
239 		oip->i_size = length;
240 	} else {
241 		lbn = lblkno(fs, length);
242 		aflags = B_CLRBUF;
243 		if (ap->a_flags & IO_SYNC)
244 			aflags |= B_SYNC;
245 		error = ffs_balloc(oip, lbn, offset, ap->a_cred, &bp, aflags);
246 		if (error)
247 			return (error);
248 		oip->i_size = length;
249 		size = blksize(fs, oip, lbn);
250 		bzero((char *)bp->b_data + offset, (u_int)(size - offset));
251 		allocbuf(bp, size);
252 		if (aflags & B_SYNC)
253 			bwrite(bp);
254 		else if (ovp->v_mount->mnt_flag & MNT_ASYNC)
255 			bdwrite(bp);
256 		else
257 			bawrite(bp);
258 	}
259 	vnode_pager_setsize(ovp, length);
260 	/*
261 	 * Calculate index into inode's block list of
262 	 * last direct and indirect blocks (if any)
263 	 * which we want to keep.  Lastblock is -1 when
264 	 * the file is truncated to 0.
265 	 */
266 	lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
267 	lastiblock[SINGLE] = lastblock - NDADDR;
268 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
269 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
270 	nblocks = btodb(fs->fs_bsize);
271 	/*
272 	 * Update file and block pointers on disk before we start freeing
273 	 * blocks.  If we crash before free'ing blocks below, the blocks
274 	 * will be returned to the free list.  lastiblock values are also
275 	 * normalized to -1 for calls to ffs_indirtrunc below.
276 	 */
277 	bcopy((caddr_t)&oip->i_db[0], (caddr_t)oldblks, sizeof oldblks);
278 	for (level = TRIPLE; level >= SINGLE; level--)
279 		if (lastiblock[level] < 0) {
280 			oip->i_ib[level] = 0;
281 			lastiblock[level] = -1;
282 		}
283 	for (i = NDADDR - 1; i > lastblock; i--)
284 		oip->i_db[i] = 0;
285 	oip->i_flag |= IN_CHANGE | IN_UPDATE;
286 	error = VOP_UPDATE(ovp, &tv, &tv, ((length > 0) ? 0 : 1));
287 	if (error)
288 		allerror = error;
289 	/*
290 	 * Having written the new inode to disk, save its new configuration
291 	 * and put back the old block pointers long enough to process them.
292 	 * Note that we save the new block configuration so we can check it
293 	 * when we are done.
294 	 */
295 	bcopy((caddr_t)&oip->i_db[0], (caddr_t)newblks, sizeof newblks);
296 	bcopy((caddr_t)oldblks, (caddr_t)&oip->i_db[0], sizeof oldblks);
297 	oip->i_size = osize;
298 	vflags = ((length > 0) ? V_SAVE : 0) | V_SAVEMETA;
299 	allerror = vinvalbuf(ovp, vflags, ap->a_cred, ap->a_p, 0, 0);
300 
301 	/*
302 	 * Indirect blocks first.
303 	 */
304 	indir_lbn[SINGLE] = -NDADDR;
305 	indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
306 	indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
307 	for (level = TRIPLE; level >= SINGLE; level--) {
308 		bn = oip->i_ib[level];
309 		if (bn != 0) {
310 			error = ffs_indirtrunc(oip, indir_lbn[level],
311 			    fsbtodb(fs, bn), lastiblock[level], level, &count);
312 			if (error)
313 				allerror = error;
314 			blocksreleased += count;
315 			if (lastiblock[level] < 0) {
316 				oip->i_ib[level] = 0;
317 				ffs_blkfree(oip, bn, fs->fs_bsize);
318 				blocksreleased += nblocks;
319 			}
320 		}
321 		if (lastiblock[level] >= 0)
322 			goto done;
323 	}
324 
325 	/*
326 	 * All whole direct blocks or frags.
327 	 */
328 	for (i = NDADDR - 1; i > lastblock; i--) {
329 		register long bsize;
330 
331 		bn = oip->i_db[i];
332 		if (bn == 0)
333 			continue;
334 		oip->i_db[i] = 0;
335 		bsize = blksize(fs, oip, i);
336 		ffs_blkfree(oip, bn, bsize);
337 		blocksreleased += btodb(bsize);
338 	}
339 	if (lastblock < 0)
340 		goto done;
341 
342 	/*
343 	 * Finally, look for a change in size of the
344 	 * last direct block; release any frags.
345 	 */
346 	bn = oip->i_db[lastblock];
347 	if (bn != 0) {
348 		long oldspace, newspace;
349 
350 		/*
351 		 * Calculate amount of space we're giving
352 		 * back as old block size minus new block size.
353 		 */
354 		oldspace = blksize(fs, oip, lastblock);
355 		oip->i_size = length;
356 		newspace = blksize(fs, oip, lastblock);
357 		if (newspace == 0)
358 			panic("ffs_truncate: newspace");
359 		if (oldspace - newspace > 0) {
360 			/*
361 			 * Block number of space to be free'd is
362 			 * the old block # plus the number of frags
363 			 * required for the storage we're keeping.
364 			 */
365 			bn += numfrags(fs, newspace);
366 			ffs_blkfree(oip, bn, oldspace - newspace);
367 			blocksreleased += btodb(oldspace - newspace);
368 		}
369 	}
370 done:
371 #ifdef DIAGNOSTIC
372 	for (level = SINGLE; level <= TRIPLE; level++)
373 		if (newblks[NDADDR + level] != oip->i_ib[level])
374 			panic("ffs_truncate1");
375 	for (i = 0; i < NDADDR; i++)
376 		if (newblks[i] != oip->i_db[i])
377 			panic("ffs_truncate2");
378 	if (length == 0 &&
379 	    (ovp->v_dirtyblkhd.lh_first || ovp->v_cleanblkhd.lh_first))
380 		panic("ffs_truncate3");
381 #endif /* DIAGNOSTIC */
382 	/*
383 	 * Put back the real size.
384 	 */
385 	oip->i_size = length;
386 	oip->i_blocks -= blocksreleased;
387 	if (oip->i_blocks < 0)			/* sanity */
388 		oip->i_blocks = 0;
389 	oip->i_flag |= IN_CHANGE;
390 	vnode_pager_setsize(ovp, length);
391 #ifdef QUOTA
392 	(void) chkdq(oip, -blocksreleased, NOCRED, 0);
393 #endif
394 	return (allerror);
395 }
396 
397 /*
398  * Release blocks associated with the inode ip and stored in the indirect
399  * block bn.  Blocks are free'd in LIFO order up to (but not including)
400  * lastbn.  If level is greater than SINGLE, the block is an indirect block
401  * and recursive calls to indirtrunc must be used to cleanse other indirect
402  * blocks.
403  *
404  * NB: triple indirect blocks are untested.
405  */
406 static int
407 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
408 	register struct inode *ip;
409 	ufs_daddr_t lbn, lastbn;
410 	ufs_daddr_t dbn;
411 	int level;
412 	long *countp;
413 {
414 	register int i;
415 	struct buf *bp;
416 	register struct fs *fs = ip->i_fs;
417 	register ufs_daddr_t *bap;
418 	struct vnode *vp;
419 	ufs_daddr_t *copy = NULL, nb, nlbn, last;
420 	long blkcount, factor;
421 	int nblocks, blocksreleased = 0;
422 	int error = 0, allerror = 0;
423 
424 	/*
425 	 * Calculate index in current block of last
426 	 * block to be kept.  -1 indicates the entire
427 	 * block so we need not calculate the index.
428 	 */
429 	factor = 1;
430 	for (i = SINGLE; i < level; i++)
431 		factor *= NINDIR(fs);
432 	last = lastbn;
433 	if (lastbn > 0)
434 		last /= factor;
435 	nblocks = btodb(fs->fs_bsize);
436 	/*
437 	 * Get buffer of block pointers, zero those entries corresponding
438 	 * to blocks to be free'd, and update on disk copy first.  Since
439 	 * double(triple) indirect before single(double) indirect, calls
440 	 * to bmap on these blocks will fail.  However, we already have
441 	 * the on disk address, so we have to set the b_blkno field
442 	 * explicitly instead of letting bread do everything for us.
443 	 */
444 	vp = ITOV(ip);
445 	bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0);
446 	if ((bp->b_flags & B_CACHE) == 0) {
447 		curproc->p_stats->p_ru.ru_inblock++;	/* pay for read */
448 		bp->b_flags |= B_READ;
449 		if (bp->b_bcount > bp->b_bufsize)
450 			panic("ffs_indirtrunc: bad buffer size");
451 		bp->b_blkno = dbn;
452 		vfs_busy_pages(bp, 0);
453 		VOP_STRATEGY(bp);
454 		error = biowait(bp);
455 	}
456 	if (error) {
457 		brelse(bp);
458 		*countp = 0;
459 		return (error);
460 	}
461 
462 	bap = (ufs_daddr_t *)bp->b_data;
463 	if (lastbn != -1) {
464 		MALLOC(copy, ufs_daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
465 		bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize);
466 		bzero((caddr_t)&bap[last + 1],
467 		    (u_int)(NINDIR(fs) - (last + 1)) * sizeof (ufs_daddr_t));
468 		if ((vp->v_mount->mnt_flag & MNT_ASYNC) == 0) {
469 			error = bwrite(bp);
470 			if (error)
471 				allerror = error;
472 		} else {
473 			bawrite(bp);
474 		}
475 		bap = copy;
476 	}
477 
478 	/*
479 	 * Recursively free totally unused blocks.
480 	 */
481 	for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
482 	    i--, nlbn += factor) {
483 		nb = bap[i];
484 		if (nb == 0)
485 			continue;
486 		if (level > SINGLE) {
487 			if (error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
488 			    (ufs_daddr_t)-1, level - 1, &blkcount))
489 				allerror = error;
490 			blocksreleased += blkcount;
491 		}
492 		ffs_blkfree(ip, nb, fs->fs_bsize);
493 		blocksreleased += nblocks;
494 	}
495 
496 	/*
497 	 * Recursively free last partial block.
498 	 */
499 	if (level > SINGLE && lastbn >= 0) {
500 		last = lastbn % factor;
501 		nb = bap[i];
502 		if (nb != 0) {
503 			error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
504 			    last, level - 1, &blkcount);
505 			if (error)
506 				allerror = error;
507 			blocksreleased += blkcount;
508 		}
509 	}
510 	if (copy != NULL) {
511 		FREE(copy, M_TEMP);
512 	} else {
513 		bp->b_flags |= B_INVAL | B_NOCACHE;
514 		brelse(bp);
515 	}
516 
517 	*countp = blocksreleased;
518 	return (allerror);
519 }
520