xref: /freebsd/sys/ufs/ffs/ffs_subr.c (revision 4f52dfbb8d6c4d446500c5b097e3806ec219fbd4)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)ffs_subr.c	8.5 (Berkeley) 3/21/95
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <sys/param.h>
38 
39 #ifndef _KERNEL
40 #include <stdio.h>
41 #include <string.h>
42 #include <stdlib.h>
43 #include <time.h>
44 #include <sys/errno.h>
45 #include <ufs/ufs/dinode.h>
46 #include <ufs/ffs/fs.h>
47 
48 struct malloc_type;
49 #define UFS_MALLOC(size, type, flags) malloc(size)
50 #define UFS_FREE(ptr, type) free(ptr)
51 #define UFS_TIME time(NULL)
52 
53 #else /* _KERNEL */
54 #include <sys/systm.h>
55 #include <sys/lock.h>
56 #include <sys/malloc.h>
57 #include <sys/mount.h>
58 #include <sys/vnode.h>
59 #include <sys/bio.h>
60 #include <sys/buf.h>
61 #include <sys/ucred.h>
62 
63 #include <ufs/ufs/quota.h>
64 #include <ufs/ufs/inode.h>
65 #include <ufs/ufs/extattr.h>
66 #include <ufs/ufs/ufsmount.h>
67 #include <ufs/ufs/ufs_extern.h>
68 #include <ufs/ffs/ffs_extern.h>
69 #include <ufs/ffs/fs.h>
70 
71 #define UFS_MALLOC(size, type, flags) malloc(size, type, flags)
72 #define UFS_FREE(ptr, type) free(ptr, type)
73 #define UFS_TIME time_second
74 
75 /*
76  * Return buffer with the contents of block "offset" from the beginning of
77  * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
78  * remaining space in the directory.
79  */
80 int
81 ffs_blkatoff(struct vnode *vp, off_t offset, char **res, struct buf **bpp)
82 {
83 	struct inode *ip;
84 	struct fs *fs;
85 	struct buf *bp;
86 	ufs_lbn_t lbn;
87 	int bsize, error;
88 
89 	ip = VTOI(vp);
90 	fs = ITOFS(ip);
91 	lbn = lblkno(fs, offset);
92 	bsize = blksize(fs, ip, lbn);
93 
94 	*bpp = NULL;
95 	error = bread(vp, lbn, bsize, NOCRED, &bp);
96 	if (error) {
97 		brelse(bp);
98 		return (error);
99 	}
100 	if (res)
101 		*res = (char *)bp->b_data + blkoff(fs, offset);
102 	*bpp = bp;
103 	return (0);
104 }
105 
106 /*
107  * Load up the contents of an inode and copy the appropriate pieces
108  * to the incore copy.
109  */
110 void
111 ffs_load_inode(struct buf *bp, struct inode *ip, struct fs *fs, ino_t ino)
112 {
113 
114 	if (I_IS_UFS1(ip)) {
115 		*ip->i_din1 =
116 		    *((struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
117 		ip->i_mode = ip->i_din1->di_mode;
118 		ip->i_nlink = ip->i_din1->di_nlink;
119 		ip->i_size = ip->i_din1->di_size;
120 		ip->i_flags = ip->i_din1->di_flags;
121 		ip->i_gen = ip->i_din1->di_gen;
122 		ip->i_uid = ip->i_din1->di_uid;
123 		ip->i_gid = ip->i_din1->di_gid;
124 	} else {
125 		*ip->i_din2 =
126 		    *((struct ufs2_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
127 		ip->i_mode = ip->i_din2->di_mode;
128 		ip->i_nlink = ip->i_din2->di_nlink;
129 		ip->i_size = ip->i_din2->di_size;
130 		ip->i_flags = ip->i_din2->di_flags;
131 		ip->i_gen = ip->i_din2->di_gen;
132 		ip->i_uid = ip->i_din2->di_uid;
133 		ip->i_gid = ip->i_din2->di_gid;
134 	}
135 }
136 #endif /* KERNEL */
137 
138 /*
139  * These are the low-level functions that actually read and write
140  * the superblock and its associated data.
141  */
142 static off_t sblock_try[] = SBLOCKSEARCH;
143 static int readsuper(void *, struct fs **, off_t, int,
144 	int (*)(void *, off_t, void **, int));
145 
146 /*
147  * Read a superblock from the devfd device.
148  *
149  * If an alternate superblock is specified, it is read. Otherwise the
150  * set of locations given in the SBLOCKSEARCH list is searched for a
151  * superblock. Memory is allocated for the superblock by the readfunc and
152  * is returned. If filltype is non-NULL, additional memory is allocated
153  * of type filltype and filled in with the superblock summary information.
154  * All memory is freed when any error is returned.
155  *
156  * If a superblock is found, zero is returned. Otherwise one of the
157  * following error values is returned:
158  *     EIO: non-existent or truncated superblock.
159  *     EIO: error reading summary information.
160  *     ENOENT: no usable known superblock found.
161  *     ENOSPC: failed to allocate space for the superblock.
162  *     EINVAL: The previous newfs operation on this volume did not complete.
163  *         The administrator must complete newfs before using this volume.
164  */
165 int
166 ffs_sbget(void *devfd, struct fs **fsp, off_t altsblock,
167     struct malloc_type *filltype,
168     int (*readfunc)(void *devfd, off_t loc, void **bufp, int size))
169 {
170 	struct fs *fs;
171 	int i, error, size, blks;
172 	uint8_t *space;
173 	int32_t *lp;
174 	char *buf;
175 
176 	fs = NULL;
177 	*fsp = NULL;
178 	if (altsblock != -1) {
179 		if ((error = readsuper(devfd, &fs, altsblock, 1,
180 		     readfunc)) != 0) {
181 			if (fs != NULL)
182 				UFS_FREE(fs, filltype);
183 			return (error);
184 		}
185 	} else {
186 		for (i = 0; sblock_try[i] != -1; i++) {
187 			if ((error = readsuper(devfd, &fs, sblock_try[i], 0,
188 			     readfunc)) == 0)
189 				break;
190 			if (fs != NULL) {
191 				UFS_FREE(fs, filltype);
192 				fs = NULL;
193 			}
194 			if (error == ENOENT)
195 				continue;
196 			return (error);
197 		}
198 		if (sblock_try[i] == -1)
199 			return (ENOENT);
200 	}
201 	/*
202 	 * Read in the superblock summary information.
203 	 */
204 	size = fs->fs_cssize;
205 	blks = howmany(size, fs->fs_fsize);
206 	if (fs->fs_contigsumsize > 0)
207 		size += fs->fs_ncg * sizeof(int32_t);
208 	size += fs->fs_ncg * sizeof(u_int8_t);
209 	/* When running in libufs or libsa, UFS_MALLOC may fail */
210 	if ((space = UFS_MALLOC(size, filltype, M_WAITOK)) == NULL) {
211 		UFS_FREE(fs, filltype);
212 		return (ENOSPC);
213 	}
214 	fs->fs_csp = (struct csum *)space;
215 	for (i = 0; i < blks; i += fs->fs_frag) {
216 		size = fs->fs_bsize;
217 		if (i + fs->fs_frag > blks)
218 			size = (blks - i) * fs->fs_fsize;
219 		buf = NULL;
220 		error = (*readfunc)(devfd,
221 		    dbtob(fsbtodb(fs, fs->fs_csaddr + i)), (void **)&buf, size);
222 		if (error) {
223 			if (buf != NULL)
224 				UFS_FREE(buf, filltype);
225 			UFS_FREE(fs->fs_csp, filltype);
226 			UFS_FREE(fs, filltype);
227 			return (error);
228 		}
229 		memcpy(space, buf, size);
230 		UFS_FREE(buf, filltype);
231 		space += size;
232 	}
233 	if (fs->fs_contigsumsize > 0) {
234 		fs->fs_maxcluster = lp = (int32_t *)space;
235 		for (i = 0; i < fs->fs_ncg; i++)
236 			*lp++ = fs->fs_contigsumsize;
237 		space = (uint8_t *)lp;
238 	}
239 	size = fs->fs_ncg * sizeof(u_int8_t);
240 	fs->fs_contigdirs = (u_int8_t *)space;
241 	bzero(fs->fs_contigdirs, size);
242 	*fsp = fs;
243 	return (0);
244 }
245 
246 /*
247  * Try to read a superblock from the location specified by sblockloc.
248  * Return zero on success or an errno on failure.
249  */
250 static int
251 readsuper(void *devfd, struct fs **fsp, off_t sblockloc, int isaltsblk,
252     int (*readfunc)(void *devfd, off_t loc, void **bufp, int size))
253 {
254 	struct fs *fs;
255 	int error;
256 
257 	error = (*readfunc)(devfd, sblockloc, (void **)fsp, SBLOCKSIZE);
258 	if (error != 0)
259 		return (error);
260 	fs = *fsp;
261 	if (fs->fs_magic == FS_BAD_MAGIC)
262 		return (EINVAL);
263 	if (((fs->fs_magic == FS_UFS1_MAGIC && (isaltsblk ||
264 	      sblockloc <= SBLOCK_UFS1)) ||
265 	     (fs->fs_magic == FS_UFS2_MAGIC && (isaltsblk ||
266 	      sblockloc == fs->fs_sblockloc))) &&
267 	    fs->fs_ncg >= 1 &&
268 	    fs->fs_bsize >= MINBSIZE &&
269 	    fs->fs_bsize <= MAXBSIZE &&
270 	    fs->fs_bsize >= roundup(sizeof(struct fs), DEV_BSIZE)) {
271 		/* Have to set for old filesystems that predate this field */
272 		fs->fs_sblockactualloc = sblockloc;
273 		/* Not yet any summary information */
274 		fs->fs_csp = NULL;
275 		return (0);
276 	}
277 	return (ENOENT);
278 }
279 
280 /*
281  * Write a superblock to the devfd device from the memory pointed to by fs.
282  * Write out the superblock summary information if it is present.
283  *
284  * If the write is successful, zero is returned. Otherwise one of the
285  * following error values is returned:
286  *     EIO: failed to write superblock.
287  *     EIO: failed to write superblock summary information.
288  */
289 int
290 ffs_sbput(void *devfd, struct fs *fs, off_t loc,
291     int (*writefunc)(void *devfd, off_t loc, void *buf, int size))
292 {
293 	int i, error, blks, size;
294 	uint8_t *space;
295 
296 	/*
297 	 * If there is summary information, write it first, so if there
298 	 * is an error, the superblock will not be marked as clean.
299 	 */
300 	if (fs->fs_csp != NULL) {
301 		blks = howmany(fs->fs_cssize, fs->fs_fsize);
302 		space = (uint8_t *)fs->fs_csp;
303 		for (i = 0; i < blks; i += fs->fs_frag) {
304 			size = fs->fs_bsize;
305 			if (i + fs->fs_frag > blks)
306 				size = (blks - i) * fs->fs_fsize;
307 			if ((error = (*writefunc)(devfd,
308 			     dbtob(fsbtodb(fs, fs->fs_csaddr + i)),
309 			     space, size)) != 0)
310 				return (error);
311 			space += size;
312 		}
313 	}
314 	fs->fs_fmod = 0;
315 	fs->fs_time = UFS_TIME;
316 	if ((error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize)) != 0)
317 		return (error);
318 	return (0);
319 }
320 
321 /*
322  * Update the frsum fields to reflect addition or deletion
323  * of some frags.
324  */
325 void
326 ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt)
327 {
328 	int inblk;
329 	int field, subfield;
330 	int siz, pos;
331 
332 	inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
333 	fragmap <<= 1;
334 	for (siz = 1; siz < fs->fs_frag; siz++) {
335 		if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
336 			continue;
337 		field = around[siz];
338 		subfield = inside[siz];
339 		for (pos = siz; pos <= fs->fs_frag; pos++) {
340 			if ((fragmap & field) == subfield) {
341 				fraglist[siz] += cnt;
342 				pos += siz;
343 				field <<= siz;
344 				subfield <<= siz;
345 			}
346 			field <<= 1;
347 			subfield <<= 1;
348 		}
349 	}
350 }
351 
352 /*
353  * block operations
354  *
355  * check if a block is available
356  */
357 int
358 ffs_isblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
359 {
360 	unsigned char mask;
361 
362 	switch ((int)fs->fs_frag) {
363 	case 8:
364 		return (cp[h] == 0xff);
365 	case 4:
366 		mask = 0x0f << ((h & 0x1) << 2);
367 		return ((cp[h >> 1] & mask) == mask);
368 	case 2:
369 		mask = 0x03 << ((h & 0x3) << 1);
370 		return ((cp[h >> 2] & mask) == mask);
371 	case 1:
372 		mask = 0x01 << (h & 0x7);
373 		return ((cp[h >> 3] & mask) == mask);
374 	default:
375 #ifdef _KERNEL
376 		panic("ffs_isblock");
377 #endif
378 		break;
379 	}
380 	return (0);
381 }
382 
383 /*
384  * check if a block is free
385  */
386 int
387 ffs_isfreeblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
388 {
389 
390 	switch ((int)fs->fs_frag) {
391 	case 8:
392 		return (cp[h] == 0);
393 	case 4:
394 		return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
395 	case 2:
396 		return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
397 	case 1:
398 		return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
399 	default:
400 #ifdef _KERNEL
401 		panic("ffs_isfreeblock");
402 #endif
403 		break;
404 	}
405 	return (0);
406 }
407 
408 /*
409  * take a block out of the map
410  */
411 void
412 ffs_clrblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
413 {
414 
415 	switch ((int)fs->fs_frag) {
416 	case 8:
417 		cp[h] = 0;
418 		return;
419 	case 4:
420 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
421 		return;
422 	case 2:
423 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
424 		return;
425 	case 1:
426 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
427 		return;
428 	default:
429 #ifdef _KERNEL
430 		panic("ffs_clrblock");
431 #endif
432 		break;
433 	}
434 }
435 
436 /*
437  * put a block into the map
438  */
439 void
440 ffs_setblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
441 {
442 
443 	switch ((int)fs->fs_frag) {
444 
445 	case 8:
446 		cp[h] = 0xff;
447 		return;
448 	case 4:
449 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
450 		return;
451 	case 2:
452 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
453 		return;
454 	case 1:
455 		cp[h >> 3] |= (0x01 << (h & 0x7));
456 		return;
457 	default:
458 #ifdef _KERNEL
459 		panic("ffs_setblock");
460 #endif
461 		break;
462 	}
463 }
464 
465 /*
466  * Update the cluster map because of an allocation or free.
467  *
468  * Cnt == 1 means free; cnt == -1 means allocating.
469  */
470 void
471 ffs_clusteracct(struct fs *fs, struct cg *cgp, ufs1_daddr_t blkno, int cnt)
472 {
473 	int32_t *sump;
474 	int32_t *lp;
475 	u_char *freemapp, *mapp;
476 	int i, start, end, forw, back, map, bit;
477 
478 	if (fs->fs_contigsumsize <= 0)
479 		return;
480 	freemapp = cg_clustersfree(cgp);
481 	sump = cg_clustersum(cgp);
482 	/*
483 	 * Allocate or clear the actual block.
484 	 */
485 	if (cnt > 0)
486 		setbit(freemapp, blkno);
487 	else
488 		clrbit(freemapp, blkno);
489 	/*
490 	 * Find the size of the cluster going forward.
491 	 */
492 	start = blkno + 1;
493 	end = start + fs->fs_contigsumsize;
494 	if (end >= cgp->cg_nclusterblks)
495 		end = cgp->cg_nclusterblks;
496 	mapp = &freemapp[start / NBBY];
497 	map = *mapp++;
498 	bit = 1 << (start % NBBY);
499 	for (i = start; i < end; i++) {
500 		if ((map & bit) == 0)
501 			break;
502 		if ((i & (NBBY - 1)) != (NBBY - 1)) {
503 			bit <<= 1;
504 		} else {
505 			map = *mapp++;
506 			bit = 1;
507 		}
508 	}
509 	forw = i - start;
510 	/*
511 	 * Find the size of the cluster going backward.
512 	 */
513 	start = blkno - 1;
514 	end = start - fs->fs_contigsumsize;
515 	if (end < 0)
516 		end = -1;
517 	mapp = &freemapp[start / NBBY];
518 	map = *mapp--;
519 	bit = 1 << (start % NBBY);
520 	for (i = start; i > end; i--) {
521 		if ((map & bit) == 0)
522 			break;
523 		if ((i & (NBBY - 1)) != 0) {
524 			bit >>= 1;
525 		} else {
526 			map = *mapp--;
527 			bit = 1 << (NBBY - 1);
528 		}
529 	}
530 	back = start - i;
531 	/*
532 	 * Account for old cluster and the possibly new forward and
533 	 * back clusters.
534 	 */
535 	i = back + forw + 1;
536 	if (i > fs->fs_contigsumsize)
537 		i = fs->fs_contigsumsize;
538 	sump[i] += cnt;
539 	if (back > 0)
540 		sump[back] -= cnt;
541 	if (forw > 0)
542 		sump[forw] -= cnt;
543 	/*
544 	 * Update cluster summary information.
545 	 */
546 	lp = &sump[fs->fs_contigsumsize];
547 	for (i = fs->fs_contigsumsize; i > 0; i--)
548 		if (*lp-- > 0)
549 			break;
550 	fs->fs_maxcluster[cgp->cg_cgx] = i;
551 }
552