xref: /original-bsd/sys/ufs/ffs/ffs_inode.c (revision 358f5c7d)
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.52 (Berkeley) 05/15/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 	ip->i_uid = ip->i_din.di_ouid;
147 	ip->i_gid = ip->i_din.di_ogid;
148 
149 	*ap->a_vpp = vp;
150 	return (0);
151 }
152 
153 /*
154  * Update the access, modified, and inode change times as specified
155  * by the IACC, IUPD, and ICHG flags respectively. The IMOD flag
156  * is used to specify that the inode needs to be updated but that
157  * the times have already been set. The access and modified times
158  * are taken from the second and third parameters; the inode change
159  * time is always taken from the current time. If waitfor is set,
160  * then wait for the disk write of the inode to complete.
161  */
162 int
163 ffs_update (ap)
164 	struct vop_update_args *ap;
165 {
166 	struct buf *bp;
167 	struct inode *ip;
168 	struct dinode *dp;
169 	register struct fs *fs;
170 	int error;
171 
172 	if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
173 		return (0);
174 	ip = VTOI(ap->a_vp);
175 	if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0)
176 		return (0);
177 	if (ip->i_flag&IACC)
178 		ip->i_atime.tv_sec = ap->a_ta->tv_sec;
179 	if (ip->i_flag&IUPD) {
180 		ip->i_mtime.tv_sec = ap->a_tm->tv_sec;
181 		INCRQUAD(ip->i_modrev);
182 	}
183 	if (ip->i_flag&ICHG)
184 		ip->i_ctime.tv_sec = time.tv_sec;
185 	ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD);
186 	/*
187 	 * Ensure that uid and gid are correct. This is a temporary
188 	 * fix until fsck has been changed to do the update.
189 	 */
190 	ip->i_din.di_ouid = ip->i_uid;
191 	ip->i_din.di_ogid = ip->i_gid;
192 
193 	fs = ip->i_fs;
194 	if (error = bread(ip->i_devvp, fsbtodb(fs, itod(fs, ip->i_number)),
195 		(int)fs->fs_bsize, NOCRED, &bp)) {
196 		brelse(bp);
197 		return (error);
198 	}
199 	dp = bp->b_un.b_dino + itoo(fs, ip->i_number);
200 	*dp = ip->i_din;
201 	if (ap->a_waitfor)
202 		return (bwrite(bp));
203 	else {
204 		bdwrite(bp);
205 		return (0);
206 	}
207 }
208 
209 #define	SINGLE	0	/* index of single indirect block */
210 #define	DOUBLE	1	/* index of double indirect block */
211 #define	TRIPLE	2	/* index of triple indirect block */
212 /*
213  * Truncate the inode ip to at most length size.  Free affected disk
214  * blocks -- the blocks of the file are removed in reverse order.
215  *
216  * NB: triple indirect blocks are untested.
217  */
218 ffs_truncate (ap)
219 	struct vop_truncate_args *ap;
220 {
221 	USES_VOP_UPDATE;
222 	register daddr_t lastblock;
223 	register struct inode *oip;
224 	daddr_t bn, lbn, lastiblock[NIADDR];
225 	register struct fs *fs;
226 	register struct inode *ip;
227 	struct buf *bp;
228 	int offset, size, level;
229 	long count, nblocks, blocksreleased = 0;
230 	register int i;
231 	int aflags, error, allerror;
232 	struct inode tip;
233 	off_t osize;
234 
235 	vnode_pager_setsize(ap->a_vp, (u_long)ap->a_length);
236 	oip = VTOI(ap->a_vp);
237 	if (oip->i_size <= ap->a_length) {
238 		oip->i_flag |= ICHG|IUPD;
239 		error = VOP_UPDATE(ap->a_vp, &time, &time, 1);
240 		return (error);
241 	}
242 	/*
243 	 * Calculate index into inode's block list of
244 	 * last direct and indirect blocks (if any)
245 	 * which we want to keep.  Lastblock is -1 when
246 	 * the file is truncated to 0.
247 	 */
248 	fs = oip->i_fs;
249 	lastblock = lblkno(fs, ap->a_length + fs->fs_bsize - 1) - 1;
250 	lastiblock[SINGLE] = lastblock - NDADDR;
251 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
252 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
253 	nblocks = btodb(fs->fs_bsize);
254 	/*
255 	 * Update the size of the file. If the file is not being
256 	 * truncated to a block boundry, the contents of the
257 	 * partial block following the end of the file must be
258 	 * zero'ed in case it ever become accessable again because
259 	 * of subsequent file growth.
260 	 */
261 	osize = oip->i_size;
262 	offset = blkoff(fs, ap->a_length);
263 	if (offset == 0) {
264 		oip->i_size = ap->a_length;
265 	} else {
266 		lbn = lblkno(fs, ap->a_length);
267 		aflags = B_CLRBUF;
268 		if (ap->a_flags & IO_SYNC)
269 			aflags |= B_SYNC;
270 #ifdef QUOTA
271 		if (error = getinoquota(oip))
272 			return (error);
273 #endif
274 		if (error = ffs_balloc(oip, lbn, offset, ap->a_cred, &bp, aflags))
275 			return (error);
276 		oip->i_size = ap->a_length;
277 		size = blksize(fs, oip, lbn);
278 		(void) vnode_pager_uncache(ap->a_vp);
279 		bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset));
280 		allocbuf(bp, size);
281 		if (ap->a_flags & IO_SYNC)
282 			bwrite(bp);
283 		else
284 			bdwrite(bp);
285 	}
286 	/*
287 	 * Update file and block pointers on disk before we start freeing
288 	 * blocks.  If we crash before free'ing blocks below, the blocks
289 	 * will be returned to the free list.  lastiblock values are also
290 	 * normalized to -1 for calls to ffs_indirtrunc below.
291 	 */
292 	tip = *oip;
293 	tip.i_size = osize;
294 	for (level = TRIPLE; level >= SINGLE; level--)
295 		if (lastiblock[level] < 0) {
296 			oip->i_ib[level] = 0;
297 			lastiblock[level] = -1;
298 		}
299 	for (i = NDADDR - 1; i > lastblock; i--)
300 		oip->i_db[i] = 0;
301 	oip->i_flag |= ICHG|IUPD;
302 	vinvalbuf(ap->a_vp, (ap->a_length > 0));
303 	allerror = VOP_UPDATE(ap->a_vp, &time, &time, MNT_WAIT);
304 
305 	/*
306 	 * Indirect blocks first.
307 	 */
308 	ip = &tip;
309 	for (level = TRIPLE; level >= SINGLE; level--) {
310 		bn = ip->i_ib[level];
311 		if (bn != 0) {
312 			error = ffs_indirtrunc(ip,
313 			    bn, lastiblock[level], level, &count);
314 			if (error)
315 				allerror = error;
316 			blocksreleased += count;
317 			if (lastiblock[level] < 0) {
318 				ip->i_ib[level] = 0;
319 				ffs_blkfree(ip, bn, fs->fs_bsize);
320 				blocksreleased += nblocks;
321 			}
322 		}
323 		if (lastiblock[level] >= 0)
324 			goto done;
325 	}
326 
327 	/*
328 	 * All whole direct blocks or frags.
329 	 */
330 	for (i = NDADDR - 1; i > lastblock; i--) {
331 		register long bsize;
332 
333 		bn = ip->i_db[i];
334 		if (bn == 0)
335 			continue;
336 		ip->i_db[i] = 0;
337 		bsize = blksize(fs, ip, i);
338 		ffs_blkfree(ip, bn, bsize);
339 		blocksreleased += btodb(bsize);
340 	}
341 	if (lastblock < 0)
342 		goto done;
343 
344 	/*
345 	 * Finally, look for a change in size of the
346 	 * last direct block; release any frags.
347 	 */
348 	bn = ip->i_db[lastblock];
349 	if (bn != 0) {
350 		long oldspace, newspace;
351 
352 		/*
353 		 * Calculate amount of space we're giving
354 		 * back as old block size minus new block size.
355 		 */
356 		oldspace = blksize(fs, ip, lastblock);
357 		ip->i_size = ap->a_length;
358 		newspace = blksize(fs, ip, lastblock);
359 		if (newspace == 0)
360 			panic("itrunc: newspace");
361 		if (oldspace - newspace > 0) {
362 			/*
363 			 * Block number of space to be free'd is
364 			 * the old block # plus the number of frags
365 			 * required for the storage we're keeping.
366 			 */
367 			bn += numfrags(fs, newspace);
368 			ffs_blkfree(ip, bn, oldspace - newspace);
369 			blocksreleased += btodb(oldspace - newspace);
370 		}
371 	}
372 done:
373 /* BEGIN PARANOIA */
374 	for (level = SINGLE; level <= TRIPLE; level++)
375 		if (ip->i_ib[level] != oip->i_ib[level])
376 			panic("itrunc1");
377 	for (i = 0; i < NDADDR; i++)
378 		if (ip->i_db[i] != oip->i_db[i])
379 			panic("itrunc2");
380 /* END PARANOIA */
381 	oip->i_blocks -= blocksreleased;
382 	if (oip->i_blocks < 0)			/* sanity */
383 		oip->i_blocks = 0;
384 	oip->i_flag |= ICHG;
385 #ifdef QUOTA
386 	if (!getinoquota(oip))
387 		(void) chkdq(oip, -blocksreleased, NOCRED, 0);
388 #endif
389 	return (allerror);
390 }
391 
392 /*
393  * Release blocks associated with the inode ip and stored in the indirect
394  * block bn.  Blocks are free'd in LIFO order up to (but not including)
395  * lastbn.  If level is greater than SINGLE, the block is an indirect block
396  * and recursive calls to indirtrunc must be used to cleanse other indirect
397  * blocks.
398  *
399  * NB: triple indirect blocks are untested.
400  */
401 static int
402 ffs_indirtrunc(ip, bn, lastbn, level, countp)
403 	register struct inode *ip;
404 	daddr_t bn, lastbn;
405 	int level;
406 	long *countp;
407 {
408 	register int i;
409 	struct buf *bp;
410 	register struct fs *fs = ip->i_fs;
411 	register daddr_t *bap;
412 	daddr_t *copy, nb, last;
413 	long blkcount, factor;
414 	int nblocks, blocksreleased = 0;
415 	int error, allerror = 0;
416 
417 	/*
418 	 * Calculate index in current block of last
419 	 * block to be kept.  -1 indicates the entire
420 	 * block so we need not calculate the index.
421 	 */
422 	factor = 1;
423 	for (i = SINGLE; i < level; i++)
424 		factor *= NINDIR(fs);
425 	last = lastbn;
426 	if (lastbn > 0)
427 		last /= factor;
428 	nblocks = btodb(fs->fs_bsize);
429 	/*
430 	 * Get buffer of block pointers, zero those
431 	 * entries corresponding to blocks to be free'd,
432 	 * and update on disk copy first.
433 	 */
434 	error = bread(ip->i_devvp, fsbtodb(fs, bn), (int)fs->fs_bsize,
435 		NOCRED, &bp);
436 	if (error) {
437 		brelse(bp);
438 		*countp = 0;
439 		return (error);
440 	}
441 	bap = bp->b_un.b_daddr;
442 	MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
443 	bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize);
444 	bzero((caddr_t)&bap[last + 1],
445 	  (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t));
446 	if (last == -1)
447 		bp->b_flags |= B_INVAL;
448 	error = bwrite(bp);
449 	if (error)
450 		allerror = error;
451 	bap = copy;
452 
453 	/*
454 	 * Recursively free totally unused blocks.
455 	 */
456 	for (i = NINDIR(fs) - 1; i > last; i--) {
457 		nb = bap[i];
458 		if (nb == 0)
459 			continue;
460 		if (level > SINGLE) {
461 			if (error = ffs_indirtrunc(ip,
462 			    nb, (daddr_t)-1, level - 1, &blkcount))
463 				allerror = error;
464 			blocksreleased += blkcount;
465 		}
466 		ffs_blkfree(ip, nb, fs->fs_bsize);
467 		blocksreleased += nblocks;
468 	}
469 
470 	/*
471 	 * Recursively free last partial block.
472 	 */
473 	if (level > SINGLE && lastbn >= 0) {
474 		last = lastbn % factor;
475 		nb = bap[i];
476 		if (nb != 0) {
477 			if (error =
478 			    ffs_indirtrunc(ip, nb, last, level - 1, &blkcount))
479 				allerror = error;
480 			blocksreleased += blkcount;
481 		}
482 	}
483 	FREE(copy, M_TEMP);
484 	*countp = blocksreleased;
485 	return (allerror);
486 }
487