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