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