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: stable/12/sys/ufs/ffs/ffs_subr.c 371940 2022-04-10 05:02:22Z git2svn $");
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/gsb_crc32.h>
56 #include <sys/lock.h>
57 #include <sys/malloc.h>
58 #include <sys/mount.h>
59 #include <sys/vnode.h>
60 #include <sys/bio.h>
61 #include <sys/buf.h>
62 #include <sys/ucred.h>
63
64 #include <ufs/ufs/quota.h>
65 #include <ufs/ufs/inode.h>
66 #include <ufs/ufs/extattr.h>
67 #include <ufs/ufs/ufsmount.h>
68 #include <ufs/ufs/ufs_extern.h>
69 #include <ufs/ffs/ffs_extern.h>
70 #include <ufs/ffs/fs.h>
71
72 #define UFS_MALLOC(size, type, flags) malloc(size, type, flags)
73 #define UFS_FREE(ptr, type) free(ptr, type)
74 #define UFS_TIME time_second
75
76 /*
77 * Return buffer with the contents of block "offset" from the beginning of
78 * directory "ip". If "res" is non-zero, fill it in with a pointer to the
79 * remaining space in the directory.
80 */
81 int
ffs_blkatoff(struct vnode * vp,off_t offset,char ** res,struct buf ** bpp)82 ffs_blkatoff(struct vnode *vp, off_t offset, char **res, struct buf **bpp)
83 {
84 struct inode *ip;
85 struct fs *fs;
86 struct buf *bp;
87 ufs_lbn_t lbn;
88 int bsize, error;
89
90 ip = VTOI(vp);
91 fs = ITOFS(ip);
92 lbn = lblkno(fs, offset);
93 bsize = blksize(fs, ip, lbn);
94
95 *bpp = NULL;
96 error = bread(vp, lbn, bsize, NOCRED, &bp);
97 if (error) {
98 brelse(bp);
99 return (error);
100 }
101 if (res)
102 *res = (char *)bp->b_data + blkoff(fs, offset);
103 *bpp = bp;
104 return (0);
105 }
106
107 /*
108 * Load up the contents of an inode and copy the appropriate pieces
109 * to the incore copy.
110 */
111 void
ffs_load_inode(struct buf * bp,struct inode * ip,struct fs * fs,ino_t ino)112 ffs_load_inode(struct buf *bp, struct inode *ip, struct fs *fs, ino_t ino)
113 {
114
115 if (I_IS_UFS1(ip)) {
116 *ip->i_din1 =
117 *((struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
118 ip->i_mode = ip->i_din1->di_mode;
119 ip->i_nlink = ip->i_din1->di_nlink;
120 ip->i_size = ip->i_din1->di_size;
121 ip->i_flags = ip->i_din1->di_flags;
122 ip->i_gen = ip->i_din1->di_gen;
123 ip->i_uid = ip->i_din1->di_uid;
124 ip->i_gid = ip->i_din1->di_gid;
125 } else {
126 *ip->i_din2 =
127 *((struct ufs2_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
128 ip->i_mode = ip->i_din2->di_mode;
129 ip->i_nlink = ip->i_din2->di_nlink;
130 ip->i_size = ip->i_din2->di_size;
131 ip->i_flags = ip->i_din2->di_flags;
132 ip->i_gen = ip->i_din2->di_gen;
133 ip->i_uid = ip->i_din2->di_uid;
134 ip->i_gid = ip->i_din2->di_gid;
135 }
136 }
137
138 /*
139 * Verify that a filesystem block number is a valid data block.
140 * This routine is only called on untrusted filesystems.
141 */
142 int
ffs_check_blkno(struct mount * mp,ino_t inum,ufs2_daddr_t daddr,int blksize)143 ffs_check_blkno(struct mount *mp, ino_t inum, ufs2_daddr_t daddr, int blksize)
144 {
145 struct fs *fs;
146 struct ufsmount *ump;
147 ufs2_daddr_t end_daddr;
148 int cg, havemtx;
149
150 KASSERT((mp->mnt_flag & MNT_UNTRUSTED) != 0,
151 ("ffs_check_blkno called on a trusted file system"));
152 ump = VFSTOUFS(mp);
153 fs = ump->um_fs;
154 cg = dtog(fs, daddr);
155 end_daddr = daddr + numfrags(fs, blksize);
156 /*
157 * Verify that the block number is a valid data block. Also check
158 * that it does not point to an inode block or a superblock. Accept
159 * blocks that are unalloacted (0) or part of snapshot metadata
160 * (BLK_NOCOPY or BLK_SNAP).
161 *
162 * Thus, the block must be in a valid range for the filesystem and
163 * either in the space before a backup superblock (except the first
164 * cylinder group where that space is used by the bootstrap code) or
165 * after the inode blocks and before the end of the cylinder group.
166 */
167 if ((uint64_t)daddr <= BLK_SNAP ||
168 ((uint64_t)end_daddr <= fs->fs_size &&
169 ((cg > 0 && end_daddr <= cgsblock(fs, cg)) ||
170 (daddr >= cgdmin(fs, cg) &&
171 end_daddr <= cgbase(fs, cg) + fs->fs_fpg))))
172 return (0);
173 if ((havemtx = mtx_owned(UFS_MTX(ump))) == 0)
174 UFS_LOCK(ump);
175 if (ppsratecheck(&ump->um_last_integritymsg,
176 &ump->um_secs_integritymsg, 1)) {
177 UFS_UNLOCK(ump);
178 uprintf("\n%s: inode %jd, out-of-range indirect block "
179 "number %jd\n", mp->mnt_stat.f_mntonname, inum, daddr);
180 if (havemtx)
181 UFS_LOCK(ump);
182 } else if (!havemtx)
183 UFS_UNLOCK(ump);
184 return (EIO);
185 }
186 #endif /* _KERNEL */
187
188 /*
189 * These are the low-level functions that actually read and write
190 * the superblock and its associated data.
191 */
192 static off_t sblock_try[] = SBLOCKSEARCH;
193 static int readsuper(void *, struct fs **, off_t, int,
194 int (*)(void *, off_t, void **, int));
195
196 /*
197 * Read a superblock from the devfd device.
198 *
199 * If an alternate superblock is specified, it is read. Otherwise the
200 * set of locations given in the SBLOCKSEARCH list is searched for a
201 * superblock. Memory is allocated for the superblock by the readfunc and
202 * is returned. If filltype is non-NULL, additional memory is allocated
203 * of type filltype and filled in with the superblock summary information.
204 * All memory is freed when any error is returned.
205 *
206 * If a superblock is found, zero is returned. Otherwise one of the
207 * following error values is returned:
208 * EIO: non-existent or truncated superblock.
209 * EIO: error reading summary information.
210 * ENOENT: no usable known superblock found.
211 * ENOSPC: failed to allocate space for the superblock.
212 * EINVAL: The previous newfs operation on this volume did not complete.
213 * The administrator must complete newfs before using this volume.
214 */
215 int
ffs_sbget(void * devfd,struct fs ** fsp,off_t altsblock,struct malloc_type * filltype,int (* readfunc)(void * devfd,off_t loc,void ** bufp,int size))216 ffs_sbget(void *devfd, struct fs **fsp, off_t altsblock,
217 struct malloc_type *filltype,
218 int (*readfunc)(void *devfd, off_t loc, void **bufp, int size))
219 {
220 struct fs *fs;
221 int i, error, size, blks;
222 uint8_t *space;
223 int32_t *lp;
224 char *buf;
225
226 fs = NULL;
227 *fsp = NULL;
228 if (altsblock != -1) {
229 if ((error = readsuper(devfd, &fs, altsblock, 1,
230 readfunc)) != 0) {
231 if (fs != NULL)
232 UFS_FREE(fs, filltype);
233 return (error);
234 }
235 } else {
236 for (i = 0; sblock_try[i] != -1; i++) {
237 if ((error = readsuper(devfd, &fs, sblock_try[i], 0,
238 readfunc)) == 0)
239 break;
240 if (fs != NULL) {
241 UFS_FREE(fs, filltype);
242 fs = NULL;
243 }
244 if (error == ENOENT)
245 continue;
246 return (error);
247 }
248 if (sblock_try[i] == -1)
249 return (ENOENT);
250 }
251 /*
252 * Read in the superblock summary information.
253 */
254 size = fs->fs_cssize;
255 blks = howmany(size, fs->fs_fsize);
256 if (fs->fs_contigsumsize > 0)
257 size += fs->fs_ncg * sizeof(int32_t);
258 size += fs->fs_ncg * sizeof(u_int8_t);
259 /* When running in libufs or libsa, UFS_MALLOC may fail */
260 if ((space = UFS_MALLOC(size, filltype, M_WAITOK)) == NULL) {
261 UFS_FREE(fs, filltype);
262 return (ENOSPC);
263 }
264 fs->fs_csp = (struct csum *)space;
265 for (i = 0; i < blks; i += fs->fs_frag) {
266 size = fs->fs_bsize;
267 if (i + fs->fs_frag > blks)
268 size = (blks - i) * fs->fs_fsize;
269 buf = NULL;
270 error = (*readfunc)(devfd,
271 dbtob(fsbtodb(fs, fs->fs_csaddr + i)), (void **)&buf, size);
272 if (error) {
273 if (buf != NULL)
274 UFS_FREE(buf, filltype);
275 UFS_FREE(fs->fs_csp, filltype);
276 UFS_FREE(fs, filltype);
277 return (error);
278 }
279 memcpy(space, buf, size);
280 UFS_FREE(buf, filltype);
281 space += size;
282 }
283 if (fs->fs_contigsumsize > 0) {
284 fs->fs_maxcluster = lp = (int32_t *)space;
285 for (i = 0; i < fs->fs_ncg; i++)
286 *lp++ = fs->fs_contigsumsize;
287 space = (uint8_t *)lp;
288 }
289 size = fs->fs_ncg * sizeof(u_int8_t);
290 fs->fs_contigdirs = (u_int8_t *)space;
291 bzero(fs->fs_contigdirs, size);
292 *fsp = fs;
293 return (0);
294 }
295
296 /*
297 * Try to read a superblock from the location specified by sblockloc.
298 * Return zero on success or an errno on failure.
299 */
300 static int
readsuper(void * devfd,struct fs ** fsp,off_t sblockloc,int isaltsblk,int (* readfunc)(void * devfd,off_t loc,void ** bufp,int size))301 readsuper(void *devfd, struct fs **fsp, off_t sblockloc, int isaltsblk,
302 int (*readfunc)(void *devfd, off_t loc, void **bufp, int size))
303 {
304 struct fs *fs;
305 int error;
306
307 error = (*readfunc)(devfd, sblockloc, (void **)fsp, SBLOCKSIZE);
308 if (error != 0)
309 return (error);
310 fs = *fsp;
311 if (fs->fs_magic == FS_BAD_MAGIC)
312 return (EINVAL);
313 if (((fs->fs_magic == FS_UFS1_MAGIC && (isaltsblk ||
314 sblockloc <= SBLOCK_UFS1)) ||
315 (fs->fs_magic == FS_UFS2_MAGIC && (isaltsblk ||
316 sblockloc == fs->fs_sblockloc))) &&
317 fs->fs_ncg >= 1 &&
318 fs->fs_bsize >= MINBSIZE &&
319 fs->fs_bsize <= MAXBSIZE &&
320 fs->fs_bsize >= roundup(sizeof(struct fs), DEV_BSIZE) &&
321 fs->fs_sbsize <= SBLOCKSIZE) {
322 /*
323 * If the filesystem has been run on a kernel without
324 * metadata check hashes, disable them.
325 */
326 if ((fs->fs_flags & FS_METACKHASH) == 0)
327 fs->fs_metackhash = 0;
328 /*
329 * Clear any check-hashes that are not maintained
330 * by this kernel. Also clear any unsupported flags.
331 */
332 fs->fs_metackhash &= CK_SUPPORTED;
333 fs->fs_flags &= FS_SUPPORTED;
334 /* Have to set for old filesystems that predate this field */
335 fs->fs_sblockactualloc = sblockloc;
336 /* Not yet any summary information */
337 fs->fs_csp = NULL;
338 return (0);
339 }
340 return (ENOENT);
341 }
342
343 /*
344 * Write a superblock to the devfd device from the memory pointed to by fs.
345 * Write out the superblock summary information if it is present.
346 *
347 * If the write is successful, zero is returned. Otherwise one of the
348 * following error values is returned:
349 * EIO: failed to write superblock.
350 * EIO: failed to write superblock summary information.
351 */
352 int
ffs_sbput(void * devfd,struct fs * fs,off_t loc,int (* writefunc)(void * devfd,off_t loc,void * buf,int size))353 ffs_sbput(void *devfd, struct fs *fs, off_t loc,
354 int (*writefunc)(void *devfd, off_t loc, void *buf, int size))
355 {
356 int i, error, blks, size;
357 uint8_t *space;
358
359 /*
360 * If there is summary information, write it first, so if there
361 * is an error, the superblock will not be marked as clean.
362 */
363 if (fs->fs_csp != NULL) {
364 blks = howmany(fs->fs_cssize, fs->fs_fsize);
365 space = (uint8_t *)fs->fs_csp;
366 for (i = 0; i < blks; i += fs->fs_frag) {
367 size = fs->fs_bsize;
368 if (i + fs->fs_frag > blks)
369 size = (blks - i) * fs->fs_fsize;
370 if ((error = (*writefunc)(devfd,
371 dbtob(fsbtodb(fs, fs->fs_csaddr + i)),
372 space, size)) != 0)
373 return (error);
374 space += size;
375 }
376 }
377 fs->fs_fmod = 0;
378 fs->fs_time = UFS_TIME;
379 if ((error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize)) != 0)
380 return (error);
381 return (0);
382 }
383
384 /*
385 * Update the frsum fields to reflect addition or deletion
386 * of some frags.
387 */
388 void
ffs_fragacct(struct fs * fs,int fragmap,int32_t fraglist[],int cnt)389 ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt)
390 {
391 int inblk;
392 int field, subfield;
393 int siz, pos;
394
395 inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
396 fragmap <<= 1;
397 for (siz = 1; siz < fs->fs_frag; siz++) {
398 if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
399 continue;
400 field = around[siz];
401 subfield = inside[siz];
402 for (pos = siz; pos <= fs->fs_frag; pos++) {
403 if ((fragmap & field) == subfield) {
404 fraglist[siz] += cnt;
405 pos += siz;
406 field <<= siz;
407 subfield <<= siz;
408 }
409 field <<= 1;
410 subfield <<= 1;
411 }
412 }
413 }
414
415 /*
416 * block operations
417 *
418 * check if a block is available
419 */
420 int
ffs_isblock(struct fs * fs,unsigned char * cp,ufs1_daddr_t h)421 ffs_isblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
422 {
423 unsigned char mask;
424
425 switch ((int)fs->fs_frag) {
426 case 8:
427 return (cp[h] == 0xff);
428 case 4:
429 mask = 0x0f << ((h & 0x1) << 2);
430 return ((cp[h >> 1] & mask) == mask);
431 case 2:
432 mask = 0x03 << ((h & 0x3) << 1);
433 return ((cp[h >> 2] & mask) == mask);
434 case 1:
435 mask = 0x01 << (h & 0x7);
436 return ((cp[h >> 3] & mask) == mask);
437 default:
438 #ifdef _KERNEL
439 panic("ffs_isblock");
440 #endif
441 break;
442 }
443 return (0);
444 }
445
446 /*
447 * check if a block is free
448 */
449 int
ffs_isfreeblock(struct fs * fs,u_char * cp,ufs1_daddr_t h)450 ffs_isfreeblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
451 {
452
453 switch ((int)fs->fs_frag) {
454 case 8:
455 return (cp[h] == 0);
456 case 4:
457 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
458 case 2:
459 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
460 case 1:
461 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
462 default:
463 #ifdef _KERNEL
464 panic("ffs_isfreeblock");
465 #endif
466 break;
467 }
468 return (0);
469 }
470
471 /*
472 * take a block out of the map
473 */
474 void
ffs_clrblock(struct fs * fs,u_char * cp,ufs1_daddr_t h)475 ffs_clrblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
476 {
477
478 switch ((int)fs->fs_frag) {
479 case 8:
480 cp[h] = 0;
481 return;
482 case 4:
483 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
484 return;
485 case 2:
486 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
487 return;
488 case 1:
489 cp[h >> 3] &= ~(0x01 << (h & 0x7));
490 return;
491 default:
492 #ifdef _KERNEL
493 panic("ffs_clrblock");
494 #endif
495 break;
496 }
497 }
498
499 /*
500 * put a block into the map
501 */
502 void
ffs_setblock(struct fs * fs,unsigned char * cp,ufs1_daddr_t h)503 ffs_setblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
504 {
505
506 switch ((int)fs->fs_frag) {
507
508 case 8:
509 cp[h] = 0xff;
510 return;
511 case 4:
512 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
513 return;
514 case 2:
515 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
516 return;
517 case 1:
518 cp[h >> 3] |= (0x01 << (h & 0x7));
519 return;
520 default:
521 #ifdef _KERNEL
522 panic("ffs_setblock");
523 #endif
524 break;
525 }
526 }
527
528 /*
529 * Update the cluster map because of an allocation or free.
530 *
531 * Cnt == 1 means free; cnt == -1 means allocating.
532 */
533 void
ffs_clusteracct(struct fs * fs,struct cg * cgp,ufs1_daddr_t blkno,int cnt)534 ffs_clusteracct(struct fs *fs, struct cg *cgp, ufs1_daddr_t blkno, int cnt)
535 {
536 int32_t *sump;
537 int32_t *lp;
538 u_char *freemapp, *mapp;
539 int i, start, end, forw, back, map;
540 u_int bit;
541
542 if (fs->fs_contigsumsize <= 0)
543 return;
544 freemapp = cg_clustersfree(cgp);
545 sump = cg_clustersum(cgp);
546 /*
547 * Allocate or clear the actual block.
548 */
549 if (cnt > 0)
550 setbit(freemapp, blkno);
551 else
552 clrbit(freemapp, blkno);
553 /*
554 * Find the size of the cluster going forward.
555 */
556 start = blkno + 1;
557 end = start + fs->fs_contigsumsize;
558 if (end >= cgp->cg_nclusterblks)
559 end = cgp->cg_nclusterblks;
560 mapp = &freemapp[start / NBBY];
561 map = *mapp++;
562 bit = 1U << (start % NBBY);
563 for (i = start; i < end; i++) {
564 if ((map & bit) == 0)
565 break;
566 if ((i & (NBBY - 1)) != (NBBY - 1)) {
567 bit <<= 1;
568 } else {
569 map = *mapp++;
570 bit = 1;
571 }
572 }
573 forw = i - start;
574 /*
575 * Find the size of the cluster going backward.
576 */
577 start = blkno - 1;
578 end = start - fs->fs_contigsumsize;
579 if (end < 0)
580 end = -1;
581 mapp = &freemapp[start / NBBY];
582 map = *mapp--;
583 bit = 1U << (start % NBBY);
584 for (i = start; i > end; i--) {
585 if ((map & bit) == 0)
586 break;
587 if ((i & (NBBY - 1)) != 0) {
588 bit >>= 1;
589 } else {
590 map = *mapp--;
591 bit = 1U << (NBBY - 1);
592 }
593 }
594 back = start - i;
595 /*
596 * Account for old cluster and the possibly new forward and
597 * back clusters.
598 */
599 i = back + forw + 1;
600 if (i > fs->fs_contigsumsize)
601 i = fs->fs_contigsumsize;
602 sump[i] += cnt;
603 if (back > 0)
604 sump[back] -= cnt;
605 if (forw > 0)
606 sump[forw] -= cnt;
607 /*
608 * Update cluster summary information.
609 */
610 lp = &sump[fs->fs_contigsumsize];
611 for (i = fs->fs_contigsumsize; i > 0; i--)
612 if (*lp-- > 0)
613 break;
614 fs->fs_maxcluster[cgp->cg_cgx] = i;
615 }
616