xref: /csrg-svn/sys/ufs/ffs/ffs_inode.c (revision 54532)
1 /*
2  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)ffs_inode.c	7.58 (Berkeley) 06/27/92
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 
20 #include <vm/vm.h>
21 
22 #include <ufs/ufs/quota.h>
23 #include <ufs/ufs/inode.h>
24 #include <ufs/ufs/ufsmount.h>
25 #include <ufs/ufs/ufs_extern.h>
26 
27 #include <ufs/ffs/fs.h>
28 #include <ufs/ffs/ffs_extern.h>
29 
30 static int ffs_indirtrunc __P((struct inode *, daddr_t, daddr_t, int, long *));
31 
32 extern u_long nextgennumber;
33 
34 int
35 ffs_init()
36 {
37 	return (ufs_init());
38 }
39 
40 /*
41  * Look up a UFS dinode number to find its incore vnode.
42  * If it is not in core, read it in from the specified device.
43  * If it is in core, wait for the lock bit to clear, then
44  * return the inode locked. Detection and handling of mount
45  * points must be done by the calling routine.
46  */
47 ffs_vget (ap)
48 	struct vop_vget_args *ap;
49 {
50 	register struct fs *fs;
51 	register struct inode *ip;
52 	struct ufsmount *ump;
53 	struct buf *bp;
54 	struct dinode *dp;
55 	struct vnode *vp;
56 	union ihead *ih;
57 	dev_t dev;
58 	int i, type, error;
59 
60 	ump = VFSTOUFS(ap->a_mp);
61 	dev = ump->um_dev;
62 	if ((*ap->a_vpp = ufs_ihashget(dev, ap->a_ino)) != NULL)
63 		return (0);
64 
65 	/* Allocate a new vnode/inode. */
66 	if (error = getnewvnode(VT_UFS, ap->a_mp, ffs_vnodeop_p, &vp)) {
67 		*ap->a_vpp = NULL;
68 		return (error);
69 	}
70 	type = ump->um_devvp->v_tag == VT_MFS ? M_MFSNODE : M_FFSNODE; /* XXX */
71 	MALLOC(ip, struct inode *, sizeof(struct inode), type, M_WAITOK);
72 	vp->v_data = ip;
73 	ip->i_vnode = vp;
74 	ip->i_flag = 0;
75 	ip->i_devvp = 0;
76 	ip->i_mode = 0;
77 	ip->i_diroff = 0;
78 	ip->i_lockf = 0;
79 	ip->i_fs = fs = ump->um_fs;
80 	ip->i_dev = dev;
81 	ip->i_number = ap->a_ino;
82 #ifdef QUOTA
83 	for (i = 0; i < MAXQUOTAS; i++)
84 		ip->i_dquot[i] = NODQUOT;
85 #endif
86 	/*
87 	 * Put it onto its hash chain and lock it so that other requests for
88 	 * this inode will block if they arrive while we are sleeping waiting
89 	 * for old data structures to be purged or for the contents of the
90 	 * disk portion of this inode to be read.
91 	 */
92 	ufs_ihashins(ip);
93 
94 	/* Read in the disk contents for the inode, copy into the inode. */
95 	if (error = bread(ump->um_devvp, fsbtodb(fs, itod(fs, ap->a_ino)),
96 	    (int)fs->fs_bsize, NOCRED, &bp)) {
97 		/*
98 		 * The inode does not contain anything useful, so it would
99 		 * be misleading to leave it on its hash chain. It will be
100 		 * returned to the free list by ufs_iput().
101 		 */
102 		remque(ip);
103 		ip->i_forw = ip;
104 		ip->i_back = ip;
105 
106 		/* Unlock and discard unneeded inode. */
107 		ufs_iput(ip);
108 		brelse(bp);
109 		*ap->a_vpp = NULL;
110 		return (error);
111 	}
112 	dp = bp->b_un.b_dino;
113 	dp += itoo(fs, ap->a_ino);
114 	ip->i_din = *dp;
115 	brelse(bp);
116 
117 	/*
118 	 * Initialize the vnode from the inode, check for aliases.
119 	 * Note that the underlying vnode may have changed.
120 	 */
121 	if (error = ufs_vinit(ap->a_mp, ffs_specop_p, FFS_FIFOOPS, &vp)) {
122 		ufs_iput(ip);
123 		*ap->a_vpp = NULL;
124 		return (error);
125 	}
126 	/*
127 	 * Finish inode initialization now that aliasing has been resolved.
128 	 */
129 	ip->i_devvp = ump->um_devvp;
130 	VREF(ip->i_devvp);
131 	/*
132 	 * Set up a generation number for this inode if it does not
133 	 * already have one. This should only happen on old filesystems.
134 	 */
135 	if (ip->i_gen == 0) {
136 		if (++nextgennumber < (u_long)time.tv_sec)
137 			nextgennumber = time.tv_sec;
138 		ip->i_gen = nextgennumber;
139 		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
140 			ip->i_flag |= IMOD;
141 	}
142 	/*
143 	 * Ensure that uid and gid are correct. This is a temporary
144 	 * fix until fsck has been changed to do the update.
145 	 */
146 	if (fs->fs_inodefmt < FS_44INODEFMT) {		/* XXX */
147 		ip->i_uid = ip->i_din.di_ouid;		/* XXX */
148 		ip->i_gid = ip->i_din.di_ogid;		/* XXX */
149 	}						/* XXX */
150 
151 	*ap->a_vpp = vp;
152 	return (0);
153 }
154 
155 /*
156  * Update the access, modified, and inode change times as specified
157  * by the IACC, IUPD, and ICHG flags respectively. The IMOD flag
158  * is used to specify that the inode needs to be updated but that
159  * the times have already been set. The access and modified times
160  * are taken from the second and third parameters; the inode change
161  * time is always taken from the current time. If waitfor is set,
162  * then wait for the disk write of the inode to complete.
163  */
164 int
165 ffs_update (ap)
166 	struct vop_update_args *ap;
167 {
168 	struct buf *bp;
169 	struct inode *ip;
170 	struct dinode *dp;
171 	register struct fs *fs;
172 	int error;
173 
174 	if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
175 		return (0);
176 	ip = VTOI(ap->a_vp);
177 	if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0)
178 		return (0);
179 	if (ip->i_flag&IACC)
180 		ip->i_atime.ts_sec = ap->a_ta->tv_sec;
181 	if (ip->i_flag&IUPD) {
182 		ip->i_mtime.ts_sec = ap->a_tm->tv_sec;
183 		ip->i_modrev++;
184 	}
185 	if (ip->i_flag&ICHG)
186 		ip->i_ctime.ts_sec = time.tv_sec;
187 	ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD);
188 	fs = ip->i_fs;
189 	/*
190 	 * Ensure that uid and gid are correct. This is a temporary
191 	 * fix until fsck has been changed to do the update.
192 	 */
193 	if (fs->fs_inodefmt < FS_44INODEFMT) {		/* XXX */
194 		ip->i_din.di_ouid = ip->i_uid;		/* XXX */
195 		ip->i_din.di_ogid = ip->i_gid;		/* XXX */
196 	}						/* XXX */
197 	if (error = bread(ip->i_devvp, fsbtodb(fs, itod(fs, ip->i_number)),
198 		(int)fs->fs_bsize, NOCRED, &bp)) {
199 		brelse(bp);
200 		return (error);
201 	}
202 	dp = bp->b_un.b_dino + itoo(fs, ip->i_number);
203 	*dp = ip->i_din;
204 	if (ap->a_waitfor)
205 		return (bwrite(bp));
206 	else {
207 		bdwrite(bp);
208 		return (0);
209 	}
210 }
211 
212 #define	SINGLE	0	/* index of single indirect block */
213 #define	DOUBLE	1	/* index of double indirect block */
214 #define	TRIPLE	2	/* index of triple indirect block */
215 /*
216  * Truncate the inode ip to at most length size.  Free affected disk
217  * blocks -- the blocks of the file are removed in reverse order.
218  *
219  * NB: triple indirect blocks are untested.
220  */
221 ffs_truncate (ap)
222 	struct vop_truncate_args *ap;
223 {
224 	USES_VOP_UPDATE;
225 	register struct vnode *ovp = ap->a_vp;
226 	register daddr_t lastblock;
227 	register struct inode *oip;
228 	daddr_t bn, lbn, lastiblock[NIADDR];
229 	register struct fs *fs;
230 	register struct inode *ip;
231 	struct buf *bp;
232 	int offset, size, level;
233 	long count, nblocks, blocksreleased = 0;
234 	register int i;
235 	int aflags, error, allerror;
236 	struct inode tip;
237 	off_t osize;
238 
239 	oip = VTOI(ovp);
240 	if (ovp->v_type == VLNK && ovp->v_mount->mnt_maxsymlinklen > 0) {
241 #ifdef DIAGNOSTIC
242 		if (ap->a_length != 0)
243 			panic("ffs_truncate: partial truncate of symlink");
244 #endif
245 		bzero((char *)&oip->i_shortlink, (u_int)oip->i_size);
246 		oip->i_size = 0;
247 		oip->i_flag |= ICHG|IUPD;
248 		return (VOP_UPDATE(ovp, &time, &time, 1));
249 	}
250 	if (oip->i_size <= ap->a_length) {
251 		oip->i_flag |= ICHG|IUPD;
252 		return (VOP_UPDATE(ovp, &time, &time, 1));
253 	}
254 	vnode_pager_setsize(ovp, (u_long)ap->a_length);
255 	/*
256 	 * Calculate index into inode's block list of
257 	 * last direct and indirect blocks (if any)
258 	 * which we want to keep.  Lastblock is -1 when
259 	 * the file is truncated to 0.
260 	 */
261 	fs = oip->i_fs;
262 	lastblock = lblkno(fs, ap->a_length + fs->fs_bsize - 1) - 1;
263 	lastiblock[SINGLE] = lastblock - NDADDR;
264 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
265 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
266 	nblocks = btodb(fs->fs_bsize);
267 	/*
268 	 * Update the size of the file. If the file is not being
269 	 * truncated to a block boundry, the contents of the
270 	 * partial block following the end of the file must be
271 	 * zero'ed in case it ever become accessable again because
272 	 * of subsequent file growth.
273 	 */
274 	osize = oip->i_size;
275 	offset = blkoff(fs, ap->a_length);
276 	if (offset == 0) {
277 		oip->i_size = ap->a_length;
278 	} else {
279 		lbn = lblkno(fs, ap->a_length);
280 		aflags = B_CLRBUF;
281 		if (ap->a_flags & IO_SYNC)
282 			aflags |= B_SYNC;
283 #ifdef QUOTA
284 		if (error = getinoquota(oip))
285 			return (error);
286 #endif
287 		if (error = ffs_balloc(oip, lbn, offset, ap->a_cred, &bp, aflags))
288 			return (error);
289 		oip->i_size = ap->a_length;
290 		size = blksize(fs, oip, lbn);
291 		(void) vnode_pager_uncache(ovp);
292 		bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset));
293 		allocbuf(bp, size);
294 		if (ap->a_flags & IO_SYNC)
295 			bwrite(bp);
296 		else
297 			bdwrite(bp);
298 	}
299 	/*
300 	 * Update file and block pointers on disk before we start freeing
301 	 * blocks.  If we crash before free'ing blocks below, the blocks
302 	 * will be returned to the free list.  lastiblock values are also
303 	 * normalized to -1 for calls to ffs_indirtrunc below.
304 	 */
305 	tip = *oip;
306 	tip.i_size = osize;
307 	for (level = TRIPLE; level >= SINGLE; level--)
308 		if (lastiblock[level] < 0) {
309 			oip->i_ib[level] = 0;
310 			lastiblock[level] = -1;
311 		}
312 	for (i = NDADDR - 1; i > lastblock; i--)
313 		oip->i_db[i] = 0;
314 	oip->i_flag |= ICHG|IUPD;
315 	allerror = vinvalbuf(ovp, ap->a_length > 0, ap->a_cred, ap->a_p);
316 	if (error = VOP_UPDATE(ovp, &time, &time, MNT_WAIT))
317 		allerror = error;
318 
319 	/*
320 	 * Indirect blocks first.
321 	 */
322 	ip = &tip;
323 	for (level = TRIPLE; level >= SINGLE; level--) {
324 		bn = ip->i_ib[level];
325 		if (bn != 0) {
326 			error = ffs_indirtrunc(ip,
327 			    bn, lastiblock[level], level, &count);
328 			if (error)
329 				allerror = error;
330 			blocksreleased += count;
331 			if (lastiblock[level] < 0) {
332 				ip->i_ib[level] = 0;
333 				ffs_blkfree(ip, bn, fs->fs_bsize);
334 				blocksreleased += nblocks;
335 			}
336 		}
337 		if (lastiblock[level] >= 0)
338 			goto done;
339 	}
340 
341 	/*
342 	 * All whole direct blocks or frags.
343 	 */
344 	for (i = NDADDR - 1; i > lastblock; i--) {
345 		register long bsize;
346 
347 		bn = ip->i_db[i];
348 		if (bn == 0)
349 			continue;
350 		ip->i_db[i] = 0;
351 		bsize = blksize(fs, ip, i);
352 		ffs_blkfree(ip, bn, bsize);
353 		blocksreleased += btodb(bsize);
354 	}
355 	if (lastblock < 0)
356 		goto done;
357 
358 	/*
359 	 * Finally, look for a change in size of the
360 	 * last direct block; release any frags.
361 	 */
362 	bn = ip->i_db[lastblock];
363 	if (bn != 0) {
364 		long oldspace, newspace;
365 
366 		/*
367 		 * Calculate amount of space we're giving
368 		 * back as old block size minus new block size.
369 		 */
370 		oldspace = blksize(fs, ip, lastblock);
371 		ip->i_size = ap->a_length;
372 		newspace = blksize(fs, ip, lastblock);
373 		if (newspace == 0)
374 			panic("itrunc: newspace");
375 		if (oldspace - newspace > 0) {
376 			/*
377 			 * Block number of space to be free'd is
378 			 * the old block # plus the number of frags
379 			 * required for the storage we're keeping.
380 			 */
381 			bn += numfrags(fs, newspace);
382 			ffs_blkfree(ip, bn, oldspace - newspace);
383 			blocksreleased += btodb(oldspace - newspace);
384 		}
385 	}
386 done:
387 /* BEGIN PARANOIA */
388 	for (level = SINGLE; level <= TRIPLE; level++)
389 		if (ip->i_ib[level] != oip->i_ib[level])
390 			panic("itrunc1");
391 	for (i = 0; i < NDADDR; i++)
392 		if (ip->i_db[i] != oip->i_db[i])
393 			panic("itrunc2");
394 /* END PARANOIA */
395 	oip->i_blocks -= blocksreleased;
396 	if (oip->i_blocks < 0)			/* sanity */
397 		oip->i_blocks = 0;
398 	oip->i_flag |= ICHG;
399 #ifdef QUOTA
400 	if (!getinoquota(oip))
401 		(void) chkdq(oip, -blocksreleased, NOCRED, 0);
402 #endif
403 	return (allerror);
404 }
405 
406 /*
407  * Release blocks associated with the inode ip and stored in the indirect
408  * block bn.  Blocks are free'd in LIFO order up to (but not including)
409  * lastbn.  If level is greater than SINGLE, the block is an indirect block
410  * and recursive calls to indirtrunc must be used to cleanse other indirect
411  * blocks.
412  *
413  * NB: triple indirect blocks are untested.
414  */
415 static int
416 ffs_indirtrunc(ip, bn, lastbn, level, countp)
417 	register struct inode *ip;
418 	daddr_t bn, lastbn;
419 	int level;
420 	long *countp;
421 {
422 	register int i;
423 	struct buf *bp;
424 	register struct fs *fs = ip->i_fs;
425 	register daddr_t *bap;
426 	daddr_t *copy, nb, last;
427 	long blkcount, factor;
428 	int nblocks, blocksreleased = 0;
429 	int error, allerror = 0;
430 
431 	/*
432 	 * Calculate index in current block of last
433 	 * block to be kept.  -1 indicates the entire
434 	 * block so we need not calculate the index.
435 	 */
436 	factor = 1;
437 	for (i = SINGLE; i < level; i++)
438 		factor *= NINDIR(fs);
439 	last = lastbn;
440 	if (lastbn > 0)
441 		last /= factor;
442 	nblocks = btodb(fs->fs_bsize);
443 	/*
444 	 * Get buffer of block pointers, zero those
445 	 * entries corresponding to blocks to be free'd,
446 	 * and update on disk copy first.
447 	 */
448 	error = bread(ip->i_devvp, fsbtodb(fs, bn), (int)fs->fs_bsize,
449 		NOCRED, &bp);
450 	if (error) {
451 		brelse(bp);
452 		*countp = 0;
453 		return (error);
454 	}
455 	bap = bp->b_un.b_daddr;
456 	MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
457 	bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize);
458 	bzero((caddr_t)&bap[last + 1],
459 	  (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t));
460 	if (last == -1)
461 		bp->b_flags |= B_INVAL;
462 	error = bwrite(bp);
463 	if (error)
464 		allerror = error;
465 	bap = copy;
466 
467 	/*
468 	 * Recursively free totally unused blocks.
469 	 */
470 	for (i = NINDIR(fs) - 1; i > last; i--) {
471 		nb = bap[i];
472 		if (nb == 0)
473 			continue;
474 		if (level > SINGLE) {
475 			if (error = ffs_indirtrunc(ip,
476 			    nb, (daddr_t)-1, level - 1, &blkcount))
477 				allerror = error;
478 			blocksreleased += blkcount;
479 		}
480 		ffs_blkfree(ip, nb, fs->fs_bsize);
481 		blocksreleased += nblocks;
482 	}
483 
484 	/*
485 	 * Recursively free last partial block.
486 	 */
487 	if (level > SINGLE && lastbn >= 0) {
488 		last = lastbn % factor;
489 		nb = bap[i];
490 		if (nb != 0) {
491 			if (error =
492 			    ffs_indirtrunc(ip, nb, last, level - 1, &blkcount))
493 				allerror = error;
494 			blocksreleased += blkcount;
495 		}
496 	}
497 	FREE(copy, M_TEMP);
498 	*countp = blocksreleased;
499 	return (allerror);
500 }
501